分类: LINUX
2014-07-08 14:59:40
原文地址:linux meminfo 代码解析 作者:KYlinux
|
|
|
static inline unsigned long global_page_state(enum zone_stat_item item)
{
long x = atomic_long_read(&vm_stat[item]);
#ifdef CONFIG_SMP
if (x < 0)
x = 0;
#endif
return x;
}
下面根据struct sysinfo结构,简单分析CPU和内存使用信息。
#include <stdio.h>
#include <linux/unistd.h> /* 包含调用 _syscallX 宏等相关信息*/
#include <linux/kernel.h> /* 包含sysinfo结构体信息*/
int main(int argc, char *agrv[])
{
struct sysinfo s_info;
int error;
error = sysinfo(&s_info);
printf("\n\ncode error=%d\n",error);
printf("Uptime = %ds\nLoad: 1 min%d / 5 min %d / 15 min %d\n"
"RAM: total %d / free %d /shared%d\n"
"Memory in buffers = %d\nSwap:total%d/free%d\n"
"Number of processes = %d\n",
s_info.uptime, s_info.loads[0],
s_info.loads[1], s_info.loads[2],
s_info.totalram, s_info.freeram,
s_info.totalswap, s_info.freeswap,
s_info.procs);
return 0;
}
结果:
code error=0
Uptime = 8329s
Load: 1 min37152 / 5 min 37792 / 15 min 48672
RAM: total 519659520 / free 9031680 /shared1003474944
Memory in buffers = 937451520
Swap:total223/free-1078732672
Number of processes = -1078732608