sysinfo 系统调用在 Linux 上返回错误的负载平均值
我有一个 C 程序,可以打印一些系统统计信息。
#include <sys/sysinfo.h>
#include <stdio.h>
int main() {
int days, hours, mins;
struct sysinfo sys_info;
if(sysinfo(&sys_info) != 0)
perror("sysinfo");
// Uptime
days = sys_info.uptime / 86400;
hours = (sys_info.uptime / 3600) - (days * 24);
mins = (sys_info.uptime / 60) - (days * 1440) - (hours * 60);
printf("Uptime: %ddays, %dhours, %dminutes, %ldseconds\n",
days, hours, mins, sys_info.uptime % 60);
// Load Averages for 1,5 and 15 minutes
printf("Load Avgs: 1min(%ld) 5min(%ld) 15min(%ld)\n",
sys_info.loads[0], sys_info.loads[1], sys_info.loads[2]);
printf("Total Ram: %lluk\tFree: %lluk\n",
sys_info.totalram *(unsigned long long)sys_info.mem_unit / 1024,
sys_info.freeram *(unsigned long long)sys_info.mem_unit/ 1024);
// Number of processes currently running.
printf("Number of processes: %d\n", sys_info.procs);
return 0;
}
我的问题是负载平均值与 /proc/loadavg 不同,
./a.out
Uptime: 1days, 4hours, 1minutes, 16seconds
Load Avgs: 1min(13248) 5min(14880) 15min(11840)
Total Ram: 2052956k Free: 188104k
Number of processes: 265
为什么?我正在打印 13248,但“top”或“cat /proc/loadavg”给出 0.24。问题是什么?
I have a C program that prints some system statistics.
#include <sys/sysinfo.h>
#include <stdio.h>
int main() {
int days, hours, mins;
struct sysinfo sys_info;
if(sysinfo(&sys_info) != 0)
perror("sysinfo");
// Uptime
days = sys_info.uptime / 86400;
hours = (sys_info.uptime / 3600) - (days * 24);
mins = (sys_info.uptime / 60) - (days * 1440) - (hours * 60);
printf("Uptime: %ddays, %dhours, %dminutes, %ldseconds\n",
days, hours, mins, sys_info.uptime % 60);
// Load Averages for 1,5 and 15 minutes
printf("Load Avgs: 1min(%ld) 5min(%ld) 15min(%ld)\n",
sys_info.loads[0], sys_info.loads[1], sys_info.loads[2]);
printf("Total Ram: %lluk\tFree: %lluk\n",
sys_info.totalram *(unsigned long long)sys_info.mem_unit / 1024,
sys_info.freeram *(unsigned long long)sys_info.mem_unit/ 1024);
// Number of processes currently running.
printf("Number of processes: %d\n", sys_info.procs);
return 0;
}
My problem is that the load average values are different than /proc/loadavg
./a.out
Uptime: 1days, 4hours, 1minutes, 16seconds
Load Avgs: 1min(13248) 5min(14880) 15min(11840)
Total Ram: 2052956k Free: 188104k
Number of processes: 265
Why? I am printing 13248 but "top" or "cat /proc/loadavg" gives 0.24. What is the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该调用不会返回“错误”值。请注意,该类型是整数,因此它不能返回正常运行时间打印的浮点数。
请参阅此线程了解研究如何解释返回值以及如何转换为更熟悉的浮点数。
我的猜测(在阅读链接之前)是它只是按比例放大了 65,535 (216),这似乎也是他们发现的。因此,除以 65536.0,或者为了更清晰(如评论中指出的那样),请使用 SI_LOAD_SHIFT 常量并除以
(float)(1 << SI_LOAD_SHIFT)
。The call doesn't return the "wrong" values. Note that the type is integer, so it can't be intended to return the floating-point numbers printed by uptime.
See this thread for an investigation in how to interpret the returned values, and how to convert to the more familiar floats.
My guess (before reading the link) was that it's just represented as scaled up by 65,535 (216), and that seems to be what they found, too. So divide by 65536.0, or to be way cleaner (as pointed out in a comment) use the SI_LOAD_SHIFT constant and divide by
(float)(1 << SI_LOAD_SHIFT)
.