如何计算传输和接收的网络利用率

发布于 2024-08-06 18:10:01 字数 185 浏览 0 评论 0 原文

如何使用 C 或 shell 脚本计算传输和接收的网络利用率?

我的系统是嵌入式linux。我当前的方法是记录收到的字节(b1),等待1秒,然后再次记录(b2)。然后知道链接速度,我计算所使用的接收带宽的百分比。

接收利用率 = (((b2 - b1)*8)/link_speed)*100

有更好的方法吗?

How do I calculate network utilization for both transmit and receive either using C or a shell script?

My system is an embedded linux. My current method is to recorded bytes received (b1), wait 1 second, then recorded again (b2). Then knowing the link speed, I calculate the percentage of the receive bandwidth used.

receive utilization = (((b2 - b1)*8)/link_speed)*100

is there a better method?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雪落纷纷 2024-08-13 18:10:02

感谢“csl”为我指明了 vnstat 的方向。这里使用 vnstat 示例是我计算网络利用率的方法。

#define FP32 4294967295ULL
#define FP64 18446744073709551615ULL
#define COUNTERCALC(a,b) ( b>a ? b-a : ( a > FP32 ? FP64-a-b : FP32-a-b))
int sample_time = 2; /* seconds */
int link_speed = 100; /* Mbits/s */
uint64_t rx, rx1, rx2;
float rate;

/* 
 * Either read:
 * '/proc/net/dev' 
 * or 
 * '/sys/class/net/%s/statistics/rx_bytes'
 * for bytes received counter
 */

rx1 = read_bytes_received("eth0"); 
sleep(sample_time); /* wait */
rx2 = read_bytes_received("eth0");

/* calculate MB/s first the convert to Mbits/s*/
rx = rintf(COUNTERCALC(rx1, rx2)/(float)1048576);
rate = (rx*8)/(float)sample_time;

percent = (rate/(float)link_speed)*100;

thanks to 'csl' for pointing me in the direction of vnstat. using vnstat example here is how I calculate network utilization.

#define FP32 4294967295ULL
#define FP64 18446744073709551615ULL
#define COUNTERCALC(a,b) ( b>a ? b-a : ( a > FP32 ? FP64-a-b : FP32-a-b))
int sample_time = 2; /* seconds */
int link_speed = 100; /* Mbits/s */
uint64_t rx, rx1, rx2;
float rate;

/* 
 * Either read:
 * '/proc/net/dev' 
 * or 
 * '/sys/class/net/%s/statistics/rx_bytes'
 * for bytes received counter
 */

rx1 = read_bytes_received("eth0"); 
sleep(sample_time); /* wait */
rx2 = read_bytes_received("eth0");

/* calculate MB/s first the convert to Mbits/s*/
rx = rintf(COUNTERCALC(rx1, rx2)/(float)1048576);
rate = (rx*8)/(float)sample_time;

percent = (rate/(float)link_speed)*100;
み零 2024-08-13 18:10:01

查看执行类似操作的开源程序。

我的搜索发现了一个名为 vnstat 的小工具。

它尝试查询 /proc 文件系统(如果可用),并使用 getifaddrs 适用于没有它的系统。然后它会获取正确的 AF_LINK 接口,获取相应的 if_data 结构,然后读出发送和接收的字节,如下所示:

ifinfo.rx = ifd->ifi_ibytes;
ifinfo.tx = ifd->ifi_obytes;

另请记住 sleep() 可能睡眠时间超过 1 秒,因此您应该使用高分辨率(挂钟) ) 计时器在你的等式中——或者你可以深入研究 if 函数和结构,看看是否找到适合你任务的东西。

Check out open source programs that does something similar.

My search turned up a little tool called vnstat.

It tries to query the /proc file system, if available, and uses getifaddrs for systems that do not have it. It then fetches the correct AF_LINK interface, fetches the corresponding if_data struct and then reads out transmitted and received bytes, like this:

ifinfo.rx = ifd->ifi_ibytes;
ifinfo.tx = ifd->ifi_obytes;

Also remember that sleep() might sleep longer than exactly 1 second, so you should probably use a high resolution (wall clock) timer in your equation -- or you could delve into the if-functions and structures to see if you find anything appropriate for your task.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文