为什么这个 C 程序报告的吞吐量比 nload 更多?
我在两台具有 10GibE 的机器之间运行以下 C 程序;该程序报告 12Gib/s,而 nload
报告(更可信)9.2Gib/s。谁能告诉我我在程序中做错了什么?
.
.
#define BUFFSZ (4*1024)
char buffer[BUFFSZ];
.
.
start = clock();
while (1) {
n = write(sockfd, buffer, BUFFSZ);
if (n < 0)
error("ERROR writing to socket");
if (++blocks % (1024*1024) == 0)
{
blocks = 0;
printf("32Gib at %6.2lf Gib/s\n", 32.0/(((double) (clock() - start)) / CLOCKS_PER_SEC));
start = clock();
}
}
这是 Linux 2.6.32 上的 CentOs 6.0; nload 0.7.3,gcc 4.4.4。
I run the following C program between two machines with 10GibE; the program reports 12Gib/s whereas nload
reports a (more believable) 9.2Gib/s. Can anyone tell me what I'm doing wrong in the program?
.
.
#define BUFFSZ (4*1024)
char buffer[BUFFSZ];
.
.
start = clock();
while (1) {
n = write(sockfd, buffer, BUFFSZ);
if (n < 0)
error("ERROR writing to socket");
if (++blocks % (1024*1024) == 0)
{
blocks = 0;
printf("32Gib at %6.2lf Gib/s\n", 32.0/(((double) (clock() - start)) / CLOCKS_PER_SEC));
start = clock();
}
}
This is CentOs 6.0 on Linux 2.6.32; nload 0.7.3, gcc 4.4.4.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,
clock()
返回程序使用的CPU 时间的估计值,而不是挂钟时间 - 因此您的计算表明您每秒传输 12GiB使用的 CPU 时间。相反,请使用clock_gettime()
和时钟 IDCLOCK_MONOTONIC
来测量挂钟时间。其次,在
write()
返回之后,数据还不一定被发送到网络——只是复制到内核缓冲区中以便发送。这将在连接开始时为您提供更高的报告传输速率。Firstly,
clock()
returns an estimate of the CPU time used by the program, not the wall-clock time - so your calculation indicates that you are transferring 12GiB per second of CPU time used. Instead, useclock_gettime()
with the clock IDCLOCK_MONOTONIC
to measure wall-clock time.Secondly, after
write()
returns the data hasn't necessarily been sent to the network yet - merely copied into the kernel buffers for sending. This will give you a higher reported transfer rate at the start of the connection.检查 read() n 的返回值可能比 BUFFSZ 短。
编辑:哎呀,那应该是 write()。
Check the return value from read() n might be shorter than BUFFSZ.
EDIT: oops, that should have been write().