获取操做系统CPU及内存使用信息的另外一种方法

咱们可使用两种方法来获取CPU及内存信息:使用Linux自带的top工具,或者直接读取文件系统中目录/proc/{进程ID}/stat。那么在这里我要介绍另外一种获取这些信息的方法,不管是系统全局的仍是具体到某个进程都适用。获取这种方法更容易掌握。咱们将使用libgtop库来实现。接下来就开始介绍libgtop并使用它来编写一个简单的示例工具。编程

首先需在系统中安装libgtop库,如未安装能够在网上搜索并下载该库。值得注意的是libgtop-2.0依赖于glib-2.0库,所以需确保glib-2.0库已经正确安装。在装好libgtop-2.0以后,可使用其包含的头文件来编程了。这里就是一个监控CPU及内存使用率的例子:ide


#include <stdio.h>
#include <glibtop.h>
#include <glibtop/cpu.h>
#include <glibtop/mem.h>
#include <glibtop/proctime.h>
#include <glibtop/procmem.h>
#include <unistd.h>
int main(int argc,char *argv[])
{
 glibtop_cpu cpu_begin,cpu_end;                                 /////////////////////////////
 glibtop_proc_time proctime_begin,proctime_end;                 ///Declare the CPU info and
 glibtop_mem memory;                                            ///memory info struct
 glibtop_proc_mem procmem;                                      ///////////////////////////////函数

 int du,dn,ds,di;
 int dpu,dps;
 float cpurate,memrate;
 int pid = fork();                                              //create a process to run the specified program
 if(pid ==0)                                                    //the child process
 {
  execvp(argv[1],&argv[1]);
 }
 else                                                           //the parent process
 {
  while(1)
  {
    glibtop_get_cpu (&cpu_begin);
    glibtop_get_proc_time(&proctime_begin,pid);
    sleep(1);                                                   //the interval time is 1 second
    glibtop_get_cpu (&cpu_end);
    glibtop_get_proc_time(&proctime_end,pid);
    du = cpu_end.user - cpu_begin.user;
    dn = cpu_end.nice - cpu_begin.nice;
    ds = cpu_end.sys - cpu_begin.sys;
    di = cpu_end.idle - cpu_begin.idle;
    dpu = proctime_end.utime -  proctime_begin.utime;
    dps = proctime_end.stime - proctime_begin.stime;
    cpurate =100.0* (dpu+dps)/((du+dn+ds+di)*1.0);              //calculate the CPU usage
    glibtop_get_mem(&memory);
    glibtop_get_proc_mem(&procmem,pid);
    memrate = 100*(procmem.resident) /((memory.total)*1.0);     // calculate the memory usage工具

    fprintf(stderr,"du:%d, dn:%d, ds:%d, di:%d, ",du,dn,ds,di);
    fprintf(stderr,"dpu:%d, dps:%d ",dpu,dps);
    fprintf(stderr,"cpu rate is: %0.3f%   ",cpurate);
    fprintf(stderr,"mem rate is: %0.3f%\n",memrate);
  }
 }
 Return 0;
}测试

 

而后使用下面命令编译程序:spa


gcc procmonitor.c -o procmonitor -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -I/usr/include/libgtop-2.0 -lgtop-2.0 -lglib-2.0
获得可执行程序procmonitor,而后经过命令行参数传递启动该程序,如:命令行


./procmonitor mplayer movie.mkv
播放器mplayer将启动并播放文件movie.mkv,同时CPU及内存信息也将在命令行中显示出来。也可使用重定向'>'符号来将这些信息打印到文件中,以下所示:进程


./procmonitor mplayer movie.mkv 2>infofile.txt
注意:内存

一、构建本程序时需同时指定glib-2.0及libgtop-2.0库。ci

二、全部涉及的结构体及函数原型均可在/usr/include/libgtop-2.0中找到。

三、计算内存使用率的公式为:


(memory of resident) ÷(memory of total).
CPU使用率计算公式:


(user_mode CPU time + kernel_mode CPU time) ÷(total CPU time).
Compare with the result of top, the mem% is basic equal , but the CUP% is totally different. It is because that the machine we test with has more than one CPU. So the result calculated by the top is the usage of the CPU which only used by the process. But the result we get is the average usage of all the CPU. Certainly, we can get the same result using the same calculate mothod with other member of structure, such as xcpu_utime [GLIBTOP_NCPU] and xcpu_stime[GLIBTOP_NCPU] in structure glibtop_proc_time.

与top命令的结果相比较,内存使用率基本相同,但CPU使用率百分比彻底不一样。那是由于咱们测试的目标机有多个CPU所致。所以top命令所计算的CPU使用率是指进程所使用的该CPU使用率,而咱们程序所得结果是指全部CPU的平均使用率。固然咱们也能够获得与top相同的结果,这就须要使用其余结构体成员(如:glibtop_proc_time中的 xcpu_utime [GLIBTOP_NCPU] and xcpu_stime[GLIBTOP_NCPU])采用一样计算方法便可。

所以使用libgtop库,咱们能够更简便更灵活的获取CPU及内存信息。

 

CPU%(top)

mem%(top)

cpuusage%( procmonitor)

Memusage(procmonitor)

process

16.9

0.7

4.26

0.7

2:32.41 mplayer

17.6

0.7

4.354

0.7

2:32.94 mplayer

16.9

0.7

4.341

0.7

2:33.45 mplayer

17.0

0.7

4.218

0.7

2:33.96 mplayer

17.9

0.7

4.281

0.7

2:34.50 mplayer

17.3

0.7

4.401

0.7

2:35.02 mplayer

7.3

0.7

4.233

0.7

2:35.54 mplayer

16.9

0.7

4.285

0.7

2:36.05 mplayer

17.6

0.7

4.38

0.7

2:36.58 mplayer

17.3

0.7

4.324

0.7

2:37.10 mplayer

相关文章
相关标签/搜索