dcmtk 3.6 网络性能优化

背景

在项目中使用dcmtk 3.6.2之后版本开发SCP(windows系统),使用中发现性能很差。DCMTK自己编译提供的storescp程序,如果不加任何参数,一样有问题。经过调研,找到解决方案。

  • 主要现象
    1. scu建立accociation的请求发出后,大约要等待10s左右,才能开始发送第一张图。
    2. 在某些windows 10机器上,即使在局域网内,1s中接收非压缩CT图像的速度大约只有4张。状态好一点的机器,大约11张左右。本机scu发给同一台机器的scp速出超过20张。

问题1 - 建立association慢解决方案

出现问题1是由于默认情况,dcmtk收到accosication请求之后,会试图把IP转成hostname。在局域网没有DNS支持的情况下,会耗费很多时间。

对于预编译的storescp.exe 可以通过-disable-host-lookup" (short: -dhl) 禁用这步操作。

对于开发者,可以在程序开始的时候,设置全局变量 dcmDisableGethostbyaddr.set(OFTrue); 禁用这个操作改善速度。

dcmtk开发这对此的说明见
https://forum.dcmtk.org/viewtopic.php?f=4&t=25

The problem is most likely related to reverse DNS lookup. When accepting an association, DCMTK attempts to find out the remote system’s hostname. This can take some time if the remote system is not known to the local DNS, in particular on Windows systems. Try to start the application with the additional command line option “–disable-host-lookup” (short: -dhl) which was introduced with DCMTK 3.5.0.

问题2 - 小文件传输慢解决方案

通过添加log和用网络工具抓包,发现性能的主要瓶颈是:在scp接收完一张图之后,向SCU发送response花了大约200ms。从代码看,一个图接收完,立刻就调用了resoponse发送。但是,从IP数据包看,这个response实际上在大约200ms后才真正发给scu。

经过深入调研,发现和TCP网络的Nagle algorithm有关。这个算法试图把一些小的数据包堆在一起发送。所以,对于response这种小数据包,就会阻塞一直到这个算法设置的超时(windows下是200ms)。微软对于这个问题的解释可以参考 https://support.microsoft.com/en-us/help/214397/design-issues-sending-small-data-segments-over-tcp-with-winsock

DCMTK对此提供两个解决方法, 见https://forum.dcmtk.org/viewtopic.php?f=4&t=4632

Starting with DCMTK 3.6.2, the Nagle algorithm is not disabled by default since this does not seem to be appropriate (anymore) for most modern operating systems. In order to change this default, the environment variable TCP_NODELAY can be set to “1” (see envvars.txt for details). Alternatively, the macro DISABLE_NAGLE_ALGORITHM can be defined to change this setting at compilation time (see macros.txt for details).

翻译过来就是:

开发者觉得在现在操作系统中禁用Nagle algorithm不合适。所以,从DCMTK 3.6.2,不再默认禁用Nagle algorithm。如果希望继续禁用Nagle algorithm,可以在环境变量汇总把TCP_NODELAY设置成"1" (envvars.txt中有详细说明)。或者,通过定义DISABLE_NAGLE_ALGORITHM宏,在编译的时候禁用(macros.txt中有详细说明)。

简单说,如果程序已经编译好了无法更改,可以设置环境变量TCP_NODELAY=1。这个设置影响操作系统中所有网络程序。更好的方式是重新编译DCMTK,增加预编译宏DISABLE_NAGLE_ALGORITHM

Windows下如果希望通过cmake直接指定这个宏,同时dcmtk编译成静态库,需要把DCMTK_OVERWRITE_WIN32_COMPILER_FLAGS设成OFF,否则,CMAKE_CXX_FLAGS会被设置成固定值。参考CMake/dcmtkPrepare.cmake

经过测试,局域网内这个修改可以把CT image的接收速度提高到20张/s以上。

有兴趣,可以研究这个文件里面的相关代码dcmnet/libsrc/dul.cc

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值