Compare commits

...

1 Commits
iain ... master

Author SHA1 Message Date
veypi
d936f5f514 read process info 2021-07-20 16:55:01 +08:00
4 changed files with 181 additions and 15 deletions

2
.gitignore vendored
View File

@ -202,4 +202,4 @@ fabric.properties
.idea/caches/build_file_checksums.ser
vperf
test/
build/

View File

@ -7,12 +7,34 @@
#ifndef TEST_H
#define TEST_H
#define MaxCache 256
//proc/PID/stat
typedef struct Proc_CPU_Time {
unsigned long utime; //user time
unsigned long stime; //kernel time
} Proc_CPU_Time;
#include<math.h>
//proc/PID/statm,the value should multiply 4
typedef struct Proc_Mem {
unsigned long vsz; //Virtual Memory Size, includes all memory that the process can access
unsigned long rss; //Resident Set Size
} Proc_Mem;
double get_sqrt(double var1);
typedef struct Proc_Info {
unsigned int pid;
char name[128];
int point; //0-29
struct Proc_CPU_Time cpuInfo[30];
struct Proc_Mem memInfo[30];
} Proc_Info;
void updateMem(unsigned int *all, unsigned int *fre, float *useage);
int updateProcInfo(Proc_Info *proc);
unsigned int* getProcess();
int foundProcess(Proc_Info *pList, unsigned int id);
int assertProcess(Proc_Info *pList, unsigned int id);
#endif /* !TEST_H */

24
main.c
View File

@ -9,7 +9,6 @@
#include"./include/test.h"
#include <stdlib.h>
#include <string.h>
#define MAX_LINE 1024
struct memGlobal {
unsigned int All;
float Useage;
@ -20,15 +19,26 @@ struct cpuGlobal {
float Useage;
} cpuG;
struct process {
char Name[128];
unsigned id;
float cpu;
float mem;
} pList[1024];
struct Proc_Info pList[MaxCache];
int main()
{
unsigned int *ids;
ids = getProcess();
int i, index;
pList[3].pid = 2;
for(i=0;i<128;i++) {
index = foundProcess(pList, *ids);
if (index == -1) {
index = assertProcess(pList, *ids);
}
updateProcInfo(&pList[index]);
// printf("/proc/%d %d \n", *ids, index);
ids ++;
if (*ids == 0) {
break;
}
}
updateMem(&memG.All, &memG.Free, &memG.Useage);
printf("mem: all %u free: %u usage: %f\n", memG.All, memG.Free, memG.Useage);
return 0;

144
test.c
View File

@ -7,13 +7,11 @@
#include "./include/test.h"
#include<stdio.h>
#include<dirent.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LINE 1024
double get_sqrt(double var1)
{
return sqrt(var1);
}
void updateMem(unsigned int *all, unsigned int *fre, float *useage) {
char buf[MAX_LINE]; /*缓冲区*/
FILE *fp; /*文件指针*/
@ -28,3 +26,139 @@ void updateMem(unsigned int *all, unsigned int *fre, float *useage) {
sscanf(buf, "%*s %u kB",fre);
*useage = 1 - (float) *fre / *all;
}
/* pid 进程ID 0
comm task_struct结构体的进程名
state , S
ppid ID fork方式clone并非父进程
pgrp ID
session ID
tty_nr tty终点设备号
tpgid
flags include/linux/sched.h中的PF
minflt . COW和匿名页
cminfl minflt
majflt . map文件
majflt majflt
utime jiffies 13
stime jiffies 14
cutime utime
cstime utime
priority , 10.
nice nice值[19, -20]-10
num_threads 线, 221
itrealvalue 0
starttime jiffies
vsize bytes 22
rss pages4K 23
rsslim rss大小上限
start_code
end_code
start_stack
kstkesp esp(32 ) ,
kstkeip , EIP(32 )
pendingsig
block_sig
sigign
sigcatch
wchan
nswap swapped的页数
cnswap swapped
exit_signal
task_cpu CPU
task_rt_priority
task_policy 0=1=FIFO实时进程2=RR实时进程
blio_ticks IO的时间
gtime guest time of the task in jiffies
cgtime guest time of the task children in jiffies
start_data address above which program data+bss is placed
end_data address below which program data+bss is placed
start_brk address above which program heap can be expanded with br
*/
int updateProcInfo(Proc_Info *proc) {
char file_path[64] = {0};
char buffer[MAX_LINE] = {0};
sprintf(file_path, "./proc/%d/stat", proc->pid);
FILE *fd = NULL;
fd = fopen(file_path, "r");
if (fd == NULL) {
return -1;
}
unsigned long utime, stime, vsz, rss;
fgets(buffer,MAX_LINE,fd);
// 14 utime 15 stime 23 24 vsz rss
// 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
sscanf(buffer, "%*s %s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %u %u %*s %*s %*s %*s %*s %*s %*s %*s %u %u ", proc->name, &utime, &stime, &vsz, &rss);
printf("%d %s %d %d %d %d\n",proc->pid, proc->name, utime, stime, vsz, rss);
if (proc->point == 29) {
proc->point = 0;
} else {
proc->point ++;
}
proc->memInfo[proc->point].rss = rss;
proc->memInfo[proc->point].vsz = vsz;
proc->cpuInfo[proc->point].stime = stime;
proc->cpuInfo[proc->point].utime = utime;
fclose(fd);
return 0;
}
unsigned int* getProcess() {
static unsigned int ids[MaxCache] = {0};
DIR *dirp;
struct dirent *direntp;
unsigned int id = 0;
if ((dirp = opendir("./proc/")) == NULL) {
exit(1);
}
int index = 0;
int i;
unsigned int temp;
while ((direntp = readdir(dirp)) != NULL){
id = atoi(direntp->d_name);
if (id > 0) {
for (i = 0; i < index; i++) {
if (ids[i] > id) {
temp = id;
id = ids[i];
ids[i] = temp;
}
}
ids[index] = id;
index ++;
}
}
closedir(dirp);
return &ids[0];
}
int foundProcess(Proc_Info *pList, unsigned int id) {
int i;
for (i = 0; i<MaxCache; i++) {
if ((pList+i)->pid == id) {
return i;
}
}
return -1;
}
int assertProcess(Proc_Info *pList, unsigned int id) {
int i, j;
for (i =0; i<MaxCache;i++) {
if ((pList+i)->pid == 0) {
(pList+i)->pid = id;
strcpy((pList+i)->name, "");
(pList+i)->point = -1;
for (j =0; j<30;j++) {
(pList+i)->cpuInfo[j].utime = 0;
(pList+i)->cpuInfo[j].stime = 0;
(pList+i)->memInfo[j].vsz = 0;
(pList+i)->memInfo[j].vsz = 0;
}
return i;
}
}
return -1;
}