咱們可使用兩種方法來獲取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 |