今天折腾了一下,本意如题,方法如下:
1.使用popen执行相关的命令,然后通过awk进行截取相关的数据。这种方法获取的数据精度不高。
2.访问/proc下的各个进程文件,打开各个文件访问每个进程的信息,然后进行统计计算。此种访求可能会有权限不够。另外,较复杂。具体实现参见Linux top.c 源码实现。
3.调用系统的相关接口API,获取CPU,内存等信息。AIX平台的相关帮助为:http://publib.boulder.ibm.com/infocenter/aix/v6r1/index.jsp?topic=/com.ibm.aix.prftools/doc/prftools/prftools07.htm
其方法一,二代码如下:
@1
#include <stdio.h>
#include <string.h>
#include <pthread.h>
static pthread_mutex_t popen_lock = PTHREAD_MUTEX_INITIALIZER;
int get_popen_result(char *cmd,char *result,int size)
{
char buf[128] ={0};
char *pStr = NULL;
FILE* pfile = NULL;
int lockRet = -1;
int ret = 0;
if(cmd == NULL || result == NULL || size <= 0)
{
return -1;
}
lockRet = pthread_mutex_trylock(&popen_lock);
if(lockRet != 0)
{
perror("获取popen锁出错");
return -1;
}
pfile = popen(cmd,"r");
if(pfile == NULL)
{
perror("popen 出错");
ret = -1;
goto exit;
}
fflush(pfile);
if((pStr = fgets(buf,sizeof(buf),pfile)) != NULL)
{
strncpy(result,buf,size);
}
pclose(pfile);
exit:
pthread_mutex_unlock(&popen_lock);
return ret;
}
int get_cpu_mem_used(int *cpuUsed,float *memUsed)
{
char result[128] = {0};
int memTotal = 0;
int memoryUsed = 0;
char temp[16] = {0};
char *pPos = NULL;
double md = 0;
int ret = get_popen_result("vmstat | awk 'NR==7 {print $14}'",result,sizeof(result));
if(ret != -1)
{
*cpuUsed = atol(result);
}
ret = get_popen_result("svmon -G | awk 'NR==2 {print $2,$3}'",result,sizeof(result));
if(ret != -1 )
{
pPos = strchr(result,32);
if(pPos != NULL);
{
strncpy(temp,result,pPos-result);
memTotal = atol(temp);
memset(temp,0,sizeof(temp));
strcpy(temp,pPos + 1);
memoryUsed = atol(temp);
}
if(memTotal != 0 && memoryUsed != 0)
{
*memUsed = ((double)memoryUsed/(double)memTotal)*100;
}
}
return 0;
}
int main(int argc,char *argv[])
{
float memU = 0;
int cpuU = 0;
int i= 0;
for(i= 0 ; i< 100; i++)
{
get_cpu_mem_used(&cpuU,&memU);
printf("cpu Used : %d , mem Used : %f \n", cpuU,memU );
sleep(2);
}
return 0;
}
@2
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <libperfstat.h>
5 #include <sys/systemcfg.h>
6
7 int get_perfstat_result(double*cpuUsed,double*memUsed)
8 {
9 perfstat_cpu_total_t cpustat1,cpustat2;
10 perfstat_memory_total_t meminfo;
11 u_longlong_t c1 =0,c2 =0,cpu_delta =0;
12
13 if (!perfstat_cpu_total(NULL, &cpustat1, sizeof(perfstat_cpu_total_t), 1)){
14 perror("perfstat_cpu_total");
15 return-1;
16 }
17 c1 = cpustat1.user + cpustat1.sys + cpustat1.idle + cpustat1.wait;
18 sleep(1);
19
20 if (!perfstat_cpu_total(NULL, &cpustat2, sizeof(perfstat_cpu_total_t), 1)){
21 perror("perfstat_cpu_total");
22 return-1;
23 }
24
25 if(!perfstat_memory_total(NULL, &meminfo, sizeof(perfstat_memory_total_t), 1)){
26 perror("perfstat_memory_total");
27 return-1;
28 }
29 c2 = cpustat2.user + cpustat2.sys + cpustat2.idle + cpustat2.wait;
30 cpu_delta = cpustat2.user + cpustat2.sys - cpustat1.user - cpustat1.sys;
31
32 *cpuUsed =100.000* cpu_delta/(c2-c1) ;
33 *memUsed =100.000* (meminfo.real_inuse)/(meminfo.real_total) ;
34 printf("CPU used : %6.2f , mem Used : %6.2f \n",*cpuUsed,*memUsed);
35 //printf("Total : %lu ,%lu\n",meminfo.real_total,meminfo.real_free+ meminfo.real_inuse);
36 }
37
38 int main(int argc,char* argv[])
39 {
40 int i=0;
41 double t1 =0.0,t2 =0.0;
42 for(i=0 ; i<200 ;i++)
43 {
44 get_perfstat_result(&t1,&t2);
45 }
46 return0;
47 }
链接时参数为:xlC -q64 -g -o perf perf.c -lperfstat