如何计算网络利用率发送和接收 [英] How do I calculate network utilization for both transmit and receive

查看:1145
本文介绍了如何计算网络利用率发送和接收的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何计算网络利用率发送和接收或者使用C或shell脚本?

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

我的系统是一个嵌入式Linux。我目前的方法是记录收到的字节(B1),等待1秒钟,然后再记录(B2)。然后知道链路速度,我计算使用的接收带宽的百分比。

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.

收到利用率=(((B2 - B1)* 8)/ LINK_SPEED)* 100

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

有没有更好的方法?

推荐答案

由于CSL指着我在vnstat的方向。使用此处vnstat例子是如何计算网络的利用率。

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;

这篇关于如何计算网络利用率发送和接收的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆