SYSINFO系统调用返回Linux上的错误负载平均值 [英] sysinfo system call returns wrong load average values on linux

查看:1170
本文介绍了SYSINFO系统调用返回Linux上的错误负载平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些打印系统统计的C程序。

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;
}

我的问题是,平均负载值比的/ proc / loadavg

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

为什么呢?我打印13248,但顶或猫的/ proc / loadavg给出0.24。有什么问题?

Why? I am printing 13248 but "top" or "cat /proc/loadavg" gives 0.24. What is the problem?

推荐答案

该调用不返回错误的值。注意,类型为整数,因此它不能被用来返回由运行时间打印的浮点数。

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.

请参阅this线程在如何跨preT返回值,以及如何转换为更熟悉的花车进行调查。

See this thread for an investigation in how to interpret the returned values, and how to convert to the more familiar floats.

我的猜测(读取链接之前)是,它只是重新presented由65,535放大(2 16 ),这似乎是他们发现了什么,太。因此,通过65536.0分裂,或者是方式更清洁的(如在评论中指出)使用SI_LOAD_SHIFT不变,除以(浮点)(1 LT;&LT; SI_LOAD_SHIFT)

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).

这篇关于SYSINFO系统调用返回Linux上的错误负载平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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