linux系统下的进程以及cpu信息都实时存储在/proc/stat文件里,只需要提取对应的时间信息就可以获取cpu的信息,进程的信息则存储在/proc/pid/stat。/proc/stat文件包含了所有CPU活动的信息,该文件中的所有值都是从系统启动开始累计到当前时刻。该代码会一直运行,时间间隔为5s,进程cpu计算公式为:proc=((进程时间1-进程时间0)-(cpu时间1-cpu时间0))*100
公式转为C++为:
void conputing(unsigned long proc0,unsigned long proc1,unsigned long cpu0,unsigned long cpu1)
{
float proc;
proc = 100*((proc1-proc0)-(cpu0-cpu1));
return proc
下面的代码主要是提取信息并且存储在test.csv文件里。
#include <iostream>
#include <fstream>
#include <unistd.h>
#include <sys/time.h>
#include <string.h>
#include <stdlib.h>
#include <cstdio>
#include <dirent.h>
#include <time.h>
#define Times 5000000
using namespace std;
DIR *pdir = NULL;
struct dirent *pde = NULL;
typedef struct{
char name[16];
unsigned long user;
unsigned long nice;
unsigned long system;
unsigned long idle;
}CPU_OCCUPY;
typedef struct{
unsigned int pid;
unsigned long utime;
unsigned long stime;
unsigned long cutime;
unsigned long cstime;
}Proc_Cpu;
//获取cpu的总时间
unsigned long get_cpu_total_time()
{
FILE *fd;
char buff[1024]={0};
CPU_OCCUPY c;
fd = fopen("/proc/stat","r");
if(nullptr == fd)
{
return 0;
cout << "open stat err" << endl;
}
fgets(buff,sizeof(buff),fd);
char name[64] = {0};
sscanf(buff,"%s %ld %ld %ld %ld",name,&c.user,&c.nice,&c.system,&c.idle);
fclose(fd);
return(c.user+c.nice+c.system+c.idle);
}
char pname[16] = {0};
unsigned long get_cpu_proc_time(unsigned int pid)
{
char file_name[512] = {0};
Proc_Cpu p;
FILE *fd;
char line_buff[1024] = {0};
sprintf(file_name,"/proc/%d/stat",pid);
//printf("%s\n",file_name);
fd = fopen(file_name,"r");
if(nullptr == fd)
{
cout << "file open err: " << file_name << endl;
return 0;
}
fread(line_buff,1,sizeof(line_buff),fd);
memset(pname,0,sizeof(pname));
sscanf(line_buff,"%u (%s)",&p.pid,pname);
pname[strlen(pname)-1] = 0;
sscanf(line_buff,"%*u%*s%*s%*d%*d%*d%*d%*d%*d%*d%*d%*d%*d%ld%ld%ld%ld",&p.utime,&p.stime,&p.cutime,&p.cstime);
fclose(fd);
return(p.utime+p.stime+p.cutime+p.cstime);
}
int main()
{
ofstream outFile;
outFile.open("test.csv",ios::out);
outFile << "name" << "," << "pid" << "," << "cpu_time0" << "," << "proc_time0" << "," << "cpu_time1" << "," << "proc_time1" << "," << "pid_num" << "," << "time" <<endl;
outFile.close();
ofstream outFile2;
outFile2.open("test1.csv",ios::out);
//outFile2 << "name" << "," << "pid" << "," << "cpu_time0" << "," << "proc_time0" << "," << "cpu_time1" << "," << "proc_time1" << "," << "pid_num" << "," << "time" << endl;
outFile2 << "name" << "," << "pid" << "," << "time" << "," << "pid_data" <<endl;
outFile2.close();
ofstream outFile1;
outFile1.open("result.csv",ios::out);
outFile1 << "average" <<"," << "pid1" << "," <<"data_name" << endl;
outFile1.close();
while(1)
{
time_t timep;
unsigned int pid_name[1024] = {0};
string pid_buff[512];
unsigned long cpu_time0[1024] = {0};
unsigned long proc_time0[1024] = {0};
unsigned long cpu_time1[1024] = {0};
unsigned long proc_time1[1024] = {0};
int pid_num = 0;//进程个数
//ofstream outFile; //csv
outFile.open("test.csv",ios::out | ios::app);
//outFile2.open("test1.csv",ios::out | ios::app);
//outFile << "name" << "," << "pid" << "," << "cpu_time0" << "," << "proc_time0" << "," << "cpu_time1" << "," << "proc_time1" << endl;
pdir = opendir("/proc");
if(!pdir){
cout << "proc open err!" << endl;
return 0;
}
int i =0;
while(pde = readdir(pdir)){
if((pde->d_name[0] < '0') || (pde->d_name[0] > '9')){
continue;
}
else{
pid_name[i] = atoi(pde->d_name);
//cout << pid_name[i] << endl;
i++;
pid_num++;
}
}
cout << "总进程的个数: " << pid_num << endl;
closedir(pdir);
for(int j =0 ; j<pid_num ; j++){
cpu_time0[j] = get_cpu_total_time();
proc_time0[j] = get_cpu_proc_time(pid_name[j]);
pid_buff[j] = pname;
}
usleep(Times);
for(int k =0 ; k<pid_num ;k++){
cpu_time1[k] = get_cpu_total_time();
proc_time1[k] = get_cpu_proc_time(pid_name[k]);
}
time(&timep);
char temp[64];
strftime(temp,sizeof(temp),"%H:%M:%S",localtime(&timep));
for(int n=0 ;n<pid_num ; n++){
outFile << pid_buff[n] << "," << pid_name[n] << "," << cpu_time0[n] << "," << proc_time0[n] << "," << cpu_time1[n] << "," << proc_time1[n] << "," << pid_num << "," << temp << endl;
//outFile2 << pid_buff[n] << "," << pid_name[n] << "," << temp << endl;
}
//outFile2 << "end" << "," << "end" << "," << "end" << "," << "end" << "," << "end" << "," << "end" << "," << "end" << endl;
//outFile2.close();
outFile.close();
usleep(Times);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)