最新消息:

网络栈内存不足引发进程挂起问题

字体 admin 3116浏览 0评论

 

我们知道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

这里要记住的是:TCP协议栈内存是不可交换物理内存,用一字节少一字节。

也正是由于这一点,操作系统出厂的时候上面的默认的内存设置都不算太大。对于一个不是网络密集型的服务器问题不大,但是对于如承担C1M链接的服务器来讲,问题就来了。我们在实践中会发现tcp服务经常超时,有时候超过100ms. 那么这个问题如何定位呢?

 

我们知道当协议栈缺少内存的时候会调用sk_stream_wait_memory等待其他进程释放出内存,所以这个函数的等待时间就是我们的进程被阻塞的时间。

下面我们来验证下:

$   cat /usr/share/doc/systemtap-1.6/examples/network/sk_stream_wait_memory.stp
#!/usr/bin/stap
# 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 buffern",
                gettimeofday_us(), execname(), pid())
}

probe kernel.function("sk_stream_wait_memory").return
{
        printf("%u: %s(%d) recovered from full send buffern",
                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

如果我们观察到了进程由于缺少内存被阻塞,那么是时候调整协议栈的内存限制了。

小结:网络很复杂,需要定量分析!

 

转载请注明:爱开源 » 网络栈内存不足引发进程挂起问题

您必须 登录 才能发表评论!