Linux 如何获取 UDP 套接字的排队数据量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9278189/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get amount of queued data for UDP socket?
提问by che
To see how well I'm doing in processing incoming data, I'd like to measure the queue length at my TCP and UDP sockets.
为了了解我在处理传入数据方面做得如何,我想测量我的 TCP 和 UDP 套接字的队列长度。
I know that I can get the queue size via SO_RCVBUF
socket option, and that ioctl(<sockfd>, SIOCINQ, &<some_int>)
tells me the information for TCP sockets. But for UDP the SIOCINQ
/FIONREAD
ioctl returns only the size of next pending datagram. Is there a way how to get queue size for UDP, without having to parse system tables such as /proc/net/udp
?
我知道我可以通过SO_RCVBUF
套接字选项获取队列大小,这ioctl(<sockfd>, SIOCINQ, &<some_int>)
告诉我 TCP 套接字的信息。但是对于UDP,SIOCINQ
/ FIONREAD
ioctl 只返回下一个待处理数据报的大小。有没有一种方法可以获取 UDP 的队列大小,而无需解析系统表,例如/proc/net/udp
?
采纳答案by EdwardH
As ldx mentioned, it is not supported through ioctl or getsockopt. It seems to me that the current implementation of SIOCINQ was aimed to determine how much buffer is needed to read the entire waiting buffer (but I guess it is not so useful for that, as it can change between the read of it to the actual buffer read).
正如 ldx 所提到的,ioctl 或 getsockopt 不支持它。在我看来,SIOCINQ 的当前实现旨在确定读取整个等待缓冲区需要多少缓冲区(但我想这不是那么有用,因为它可以在读取到实际缓冲区之间发生变化读)。
There are many other telemetries which are not supported though such system calls, I guess there is no real need in normal production usage.
尽管此类系统调用不支持许多其他遥测,我想在正常的生产使用中没有真正的需要。
You can check the drops/errors through "netstat -su" , or better using SNMP (udpInErrors) if you just want to monitor the machine state.
您可以通过“netstat -su”检查掉线/错误,或者如果您只想监控机器状态,最好使用 SNMP (udpInErrors)。
BTW: You always have the option to hack in the Kernel code and add this value (or others).
顺便说一句:您始终可以选择在内核代码中进行 hack 并添加此值(或其他值)。
回答by Seth Noble
FWIW, I did some experiments to map out the behavior of FIONREAD
on different platforms.
FWIW,我做了一些实验来绘制FIONREAD
不同平台上的行为。
Platforms where FIONREAD
returns all the data pending in a SOCK_DGRAM
socket:
FIONREAD
返回SOCK_DGRAM
套接字中挂起的所有数据的平台:
Mac OS X, NetBSD, FreeBSD, Solaris, HP-UX, AIX, Windows
Mac OS X、NetBSD、FreeBSD、Solaris、HP-UX、AIX、Windows
Platforms where FIONREAD
returns only the bytes for the first pending datagram:
FIONREAD
仅返回第一个待处理数据报的字节的平台:
Linux
Linux
It might also be worth noting that some implementations include headers or other overhead bytes in the count, while others only count the payload bytes. Linux appears to return the payload size, not including IP headers.
可能还值得注意的是,一些实现在计数中包括标头或其他开销字节,而其他实现只计算有效载荷字节。Linux 似乎返回有效负载大小,不包括 IP 标头。