问题描述
我想获取每个进程的网络带宽使用情况.我找了很多这方面的资料,比如iftop、nethogs、linux 进程浏览器...但是他们都通过捕获包(libpcap)获得进程品牌带宽的使用,通过我在linux中的测试它消耗了大量的cpu(%10-%15)并且流速度为11MByte/s.如果我可以获得每个端口的流量,我就可以解决这个问题,因为我有关于使用的进程端口的表格.所以我想知道有没有其他方法可以在不捕获数据包的情况下获取端口流量.
I want to get network bandwidth usage for each process. I have found a lot of information about this, such as iftop, nethogs, linux process explorer...But all of them get process brandwidth usage by capture packet(libpcap), by my test in linux it consume a lot of cpu(%10-%15) and the speed of flow is 11MByte/s. If I can get flow rate for each port I can solve this questions because I hava get the table about process port used. So I want to know is there any other way to get port flow without capture packet.
推荐答案
据我所知,Linux 没有提供 pcap 的替代接口来计算网络使用情况./proc//stat(us)
包含各种进程信息,但没有关于网络访问,只有包括磁盘访问在内的总 I/O 使用情况.
As far as I know Linux doesn't offer an alternative interface to pcap for calculating network usage. /proc/<PID>/stat(us)
contains various process information but nothing about network access, only the total I/O usage including disk access.
同样,要知道端口,您至少必须读取 IP 标头.因此我认为不可能显着加快速度,因为分析用户空间中的所有数据包总是很慢.此任务的内核模块似乎是唯一的选择.
Similarly, to know the port you have to read at least the IP header. Hence I assume it is not possible to speed this up significantly, because analyzing all packets in user space will always be slow. A kernel module for this task seems to be the only option.
这篇关于如何在linux中获取进程或端口网络带宽使用情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!