Linux中sysinfo的用法
来源:互联网 发布:运动软件 佳明 编辑:程序博客网 时间:2024/06/05 06:02
sysinfo结构体
Linux中,可以用sysinfo来获取系统相关信息。
Linux中,sysinfo是用来获取系统相关信息的结构体。
函数声明和原型:
#include <sys/sysinfo.h>
int sysinfo(struct sysinfo *info);
在Linux中不同对版本结构体不一样
在Linux 2.3.16中,结构体的信息是(单位是字节):
- struct sysinfo {
- long uptime;
- /* 启动到现在经过的时间 */
- unsigned long loads[3];
- /* 1, 5, and 15 minute load averages */
- unsigned long totalram; /* 总的可用的内存大小 */
- unsigned long freeram; /* 还未被使用的内存大小 */
- unsigned long sharedram; /* 共享的存储器的大小 */
- unsigned long bufferram; /* 缓冲区大小 */
- unsigned long totalswap; /* 交换区大小 */
- unsigned long freeswap; /* 还可用的交换区大小 */
- unsigned short procs; /* 当前进程数目 */
- char _f[22]; /* 64字节的补丁结构 */
在Linux 2.3.23(i386), 2.3.48 (all architectures)中,结构体的信息是(单位是mem_unit字节):
- struct sysinfo {
- long uptime; /* 启动到现在经过的时间 */
- unsigned long loads[3];
- /* 1, 5, and 15 minute load averages */
- unsigned long totalram; /* 总的可用的内存大小 */
- unsigned long freeram; /* 还未被使用的内存大小 */
- unsigned long sharedram; /* 共享的存储器的大小*/
- unsigned long bufferram; /* 共享的存储器的大小 */
- unsigned long totalswap; /* 交换区大小 */
- unsigned long freeswap; /* 还可用的交换区大小 */
- unsigned short procs; /* 当前进程数目 */
- unsigned long totalhigh; /* 总的高内存大小 */
- unsigned long freehigh; /* 可用的高内存大小 */
- unsigned int mem_unit; /* 以字节为单位的内存大小 */
- char _f[20-2*sizeof(long)-sizeof(int)];
- /* libc5的补丁
- };
#include <sys/sysinfo.h>
{
struct sysinfo info;
int iRetVal = -1;/*用于获取函数的返回值,默认为-1*/
printf("return val : %d\n",iRetVal);//打印函数返回值,成功为0,失败为-1
printf("uptime : %ld\n",info.uptime);//打印从设备开启到现在的时间,单位为秒
printf("1 min load average : %lu\n",info.loads[0]);
printf("5 min load average : %lu\n",info.loads[1]);
printf("15 min load average: %lu\n",info.loads[2]);//平均负载
printf("totalram : %lu\n",info.totalram);//总可用内存大小
printf("freeram : %lu\n",info.freeram); //剩余内存
printf("procs : %u\n",info.procs); //进程数
}
uptime : 7595
1 min load ave : 7424
5 min laod ave : 17024
15 min load ave: 20032
totalarm : 1713119232
freeram : 1489481728
procs : 148
average load?表示系统在一段时间内的平均进程个数,也就是表示系统的繁忙程度。average load和CPU利用率不一样,更加能够表示系统的繁忙程度,下面将就系统的average load的计算和相关进行简单介绍。
查看方法
在Linux系统下使用uptime命令,或者查看/proc/loadavg都可以看到系统负载average load。使用uptime命令会显示系统分别在过去的1分钟,5分钟和10分钟里的平均负载。
[root@localhost ~]# uptime
19:32:09 up 5 days, 8:53, 5 users, load average: 0.05, 0.04, 0.05
[root@localhost ~]# cat /proc/loadavg
0.04 0.04 0.05 1/394 23203
那么uptime命令计算load average的工作原理是什么呢?
计算方法
对于单cpu和多cpu情况,系统的average load情况稍有不同。单cpu是最简单的情形,比如过去的平均一分钟里面,判断系统处于运行或者等待状态的进程数则表示系统的平均负载,但是在linux系统下稍有不同,那些处于io等待状态的进程也会被纳入去计算。这样就导致CPU利用率可能和平均负载很不同,在大部分进程都在做IO的时候,即使平均负载很大,也会没有很大的CPU利用率。另外,有些系统对于进程和线程的处理也很不一样,有的对于每一个线程都会进行计算,有的只关注进程,对于超线程技术的线程来说,可能又是别的处理方式。对于多CPU的平均负载的计算,是在单CPU的情况下再除以CPU的个数。
文件: kernel/timer.c:
unsigned long avenrun[3];static inline void calc_load(unsigned long ticks){unsigned long active_tasks; /* fixed-point */static int count = LOAD_FREQ;count -= ticks;if (count < 0) {count += LOAD_FREQ;active_tasks = count_active_tasks();CALC_LOAD(avenrun[0], EXP_1, active_tasks);CALC_LOAD(avenrun[1], EXP_5, active_tasks);CALC_LOAD(avenrun[2], EXP_15, active_tasks);}}
内核中的函数sched.h
/* * These are the constant used to fake the fixed-point load-average * counting. Some notes: * - 11 bit fractions expand to 22 bits by the multiplies: this gives * a load-average precision of 10 bits integer + 11 bits fractional * - if you want to count load-averages more often, you need more * precision, or rounding will get you. With 2-second counting freq, * the EXP_n values would be 1981, 2034 and 2043 if still using only * 11 bit fractions. */extern unsigned long avenrun[]; /* Load averages */extern void get_avenrun(unsigned long *loads, unsigned long offset, int shift);#define FSHIFT 11 /* nr of bits of precision */#define FIXED_1 (1<<FSHIFT) /* 1.0 as fixed-point */#define LOAD_FREQ (5*HZ+1) /* 5 sec intervals */#define EXP_1 1884 /* 1/exp(5sec/1min) as fixed-point */#define EXP_5 2014 /* 1/exp(5sec/5min) */#define EXP_15 2037 /* 1/exp(5sec/15min) */#define CALC_LOAD(load,exp,n) \ load *= exp; \ load += n*(FIXED_1-exp); \ load >>= FSHIFT;extern unsigned long total_forks;extern int nr_threads;DECLARE_PER_CPU(unsigned long, process_counts);extern int nr_processes(void);extern unsigned long nr_running(void);extern unsigned long nr_uninterruptible(void);extern unsigned long nr_iowait(void);extern unsigned long nr_iowait_cpu(int cpu);extern unsigned long this_cpu_load(void);
ewma算法
linux系统在很多方面都使用了这个算法,比如除了这个还有TC系统的CBQ算法,这是统计学的一个算法,具体请参考http://en.wikipedia.org/wiki/EWMA_chart和http://en.wikipedia.org/wiki/Moving_average
参考文献
http://man.he.net/man8/tc-cbq-details
linux内核代码
http://en.wikipedia.org/wiki/EWMA_chart
http://en.wikipedia.org/wiki/Moving_average
- Linux中sysinfo的用法
- linux sysinfo ()
- struct sysinfo 用法
- linux sysinfo 获取内存大小
- linux获取系统信息(sysinfo函数的使用)
- SysInfo
- sysinfo
- sysinfo
- Linux系统调用之sysinfo函数解析
- Linux sysinfo函数、结构体使用
- Linux sysinfo函数、结构体使用
- 使用库函数API和C代码中嵌入汇编代码两种方式实现对sysinfo的系统调用
- linux中for的用法
- linux中for的用法
- linux中for的用法
- linux中for的用法
- linux中for的用法
- linux中for的用法
- Python3:《机器学习实战》之支持向量机(4)核函数及其实现
- C语言——#ifndef
- 第二次作业,模拟CPU与硬盘容量
- bzoj1711 [Usaco2007 Open]Dining吃饭
- 配置Caffe遇到hdf5.h的错误:No such file or directory
- Linux中sysinfo的用法
- ubuntu16.04下快速配置jdk环境的方法
- 给 Android 开发者的 RxJava 详解
- java内存模型中的happens-before
- DOS命令
- EL表达式的使用
- 目标检测之focal loss
- top命令
- centOS 7.3版本下搭建Tomcat