系统技术深度探索和应用
Home > Linux, 工具介绍, 网络编程, 调优 > 网络栈内存不足引发进程挂起问题
网络栈内存不足引发进程挂起问题
February 26th, 2013Yu Feng
原创文章,转载请注明: 转载自系统技术非业余研究
本文链接地址: 网络栈内存不足引发进程挂起问题
我们知道TCP socket有发送缓冲区和接收缓冲区,这二个缓冲区都可以透过setsockopt设置SO_SNDBUF,SO_RCVBUF来修改,但是这些值设多大呢?这些值和协议栈的内存控制相关的值什么关系呢?
我们来解释下:
$ sysctl net|grep mem
net.core.wmem_max = 131071
net.core.rmem_max = 131071
net.core.wmem_default = 124928
net.core.rmem_default = 124928
net.core.optmem_max = 20480
net.ipv4.igmp_max_memberships = 20
net.ipv4.tcp_mem = 4631520 6175360 9263040
net.ipv4.tcp_wmem = 4096 16384 4194304
net.ipv4.tcp_rmem = 4096 87380 4194304
net.ipv4.udp_mem = 4631520 6175360 9263040
net.ipv4.udp_rmem_min = 4096
net.ipv4.udp_wmem_min = 4096
下面的图很好的解释了上面的问题:
![socket_mem](https://i-blog.csdnimg.cn/blog_migrate/6996370f4a68bad56a8a24bb6436cb24.jpeg)
这里要记住的是:TCP协议栈内存是不可交换物理内存,用一字节少一字节。
也正是由于这一点,操作系统出厂的时候上面的默认的内存设置都不算太大。对于一个不是网络密集型的服务器问题不大,但是对于如承担C1M链接的服务器来讲,问题就来了。我们在实践中会发现tcp服务经常超时,有时候超过100ms. 那么这个问题如何定位呢?
我们知道当协议栈缺少内存的时候会调用sk_stream_wait_memory等待其他进程释放出内存,所以这个函数的等待时间就是我们的进程被阻塞的时间。
下面我们来验证下:
$ cat /usr/share/doc/systemtap-1.6/examples/network/sk_stream_wait_memory.stp |
# Simple probe to detect when a process is waiting for more socket send |
# buffer memory. Usually means the process is doing writes larger than the |
# socket send buffer size or there is a slow receiver at the other side. |
# Increasing the socket's send buffer size might help decrease application |
# latencies, but it might also make it worse, so buyer beware. |
# Typical output: timestamp in microseconds: procname(pid) event |
# 1218230114875167: python(17631) blocked on full send buffer |
# 1218230114876196: python(17631) recovered from full send buffer |
# 1218230114876271: python(17631) blocked on full send buffer |
# 1218230114876479: python(17631) recovered from full send buffer |
probe kernel. function ( "sk_stream_wait_memory" ) |
printf ( "%u: %s(%d) blocked on full send buffer\n" , |
gettimeofday_us(), execname(), pid()) |
probe kernel. function ( "sk_stream_wait_memory" ). return |
printf ( "%u: %s(%d) recovered from full send buffer\n" , |
gettimeofday_us(), execname(), pid()) |
$ sudo stap sk_stream_wait_memory.stp |
1218230114875167: python(17631) blocked on full send buffer |
1218230114876196: python(17631) recovered from full send buffer |
1218230114876271: python(17631) blocked on full send buffer |
1218230114876479: python(17631) recovered from full send buffer |
如果我们观察到了进程由于缺少内存被阻塞,那么是时候调整协议栈的内存限制了。
小结:网络很复杂,需要定量分析!
祝玩得开心。
Post Footer automatically generated by wp-posturl plugin for wordpress.
Related posts:
- 用systemtap来修改下linux内核变量的值
- IO模式调查利器blkiomon介绍
- systemtap观察page_cache的使用情况
- Linux文件预读分析以及评估对系统的影响
- dropwatch 网络协议栈丢包检查利器
http://blog.yufeng.info/archives/2511