先看看文档,man netstat中有Recv-Q和Send-Q的含义;man ss中没有Recv-Q和Send-Q的含义
https://man7.org/linux/man-pages/man8/netstat.8.html
https://man7.org/linux/man-pages/man8/ss.8.html
man netstat是这么说的
Recv-Q
Established: The count of bytes not copied by the user program connected to this socket.
Listening: Since Kernel 2.6.18 this column contains the current syn backlog.Send-Q
Established: The count of bytes not acknowledged by the remote host.
Listening: Since Kernel 2.6.18 this column contains the maximum size of the syn backlog.
先说我的结论
一:Established时
netstat和ss 都
Recv-Q:“OS持有的,尚未交付给应用的 数据的 字节数”
Send-Q:“已经发送给对端应用,但,对端应用尚未ack的 字节数。此时,这些数据依然要由OS持有”
二:Listen时
ss的Recv-Q:“已建立成功(状态为ESTABLISHED),但,尚未交付给应用的” tcp连接的数量。
该值最大为:Send-Q+1,即:min(backlog, somaxconn)+1。
之所以加1,是因为OS内核在判断队列是否已满时,用的是>(应该用>=),这导致当已创建成功的连接数量正好等于min(backlog, somaxconn)时,还会再多创建一个tcp连接,最终结果就是:min(backlog, somaxconn)+1
见:https://segmentfault.com/a/1190000019252960
ss的Send-Q:listen时,backlog的大小。其值为min(backlog, somaxconn)
netstat的Recv-Q :含义同ss的Recv-Q
netstat的Send-Q: 尽管文档中说是"Since Kernel 2.6.18 this column contains the maximum size of the syn backlog",但,实验中看不出来
我们做下面实验验证下。使用的条件如下
- uname -a
Linux vm204 5.4.0-47-generic #51~18.04.1-Ubuntu SMP Sat Sep 5 14:35:50 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux - http接口直接sleep 3600s
- tomcat只有1个处理线程
- tomcat的acceptCount==5
- tomcat的maxConnections==7
- 利用JMeter,发送9个请求
@RequestMapping("/greeting")
public @ResponseBody
String greeting(HttpServletResponse rsp, @RequestParam(name = "cost") long cost) {
try {
Thread.sleep(cost * 1000L);
} catch (Exception e) {
e.printStackTrace();
}
String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date());
LOG.info(format);
return format;
}
此时,看到的情况
最大连接数量为:
maxConnections + ( min(acceptCount, somaxconn) + 1 ) == 7 + ( min(5, 4096) + 1 ) == 7+ ( 5+1 ) == 13
即:从上图可以看到:
我们只要求建立 9 个连接(JMeter使用9个线程做请求),由于maxConnections==7,于是tomcat只能收到 7 个连接
- 还有9-7==2个连接,已经创建成功但由OS代为持有,所以:netstat和ss 的Recv-Q都显示2
- tomcat的backlog为5(即:acceptCount为5),所以:ss 的Send-Q显示5(netstat的Send-Q,实验不出来)
OK,上面是Listen状态时,Recv-Q和Send-Q的含义
Established状态时的实验见这里:netstat输出中Established时Recv-Q
完