read process info

This commit is contained in:
veypi
2021-07-20 16:55:01 +08:00
parent 208f4aeac2
commit d936f5f514
4 changed files with 181 additions and 15 deletions
+1 -1
View File
@@ -202,4 +202,4 @@ fabric.properties
.idea/caches/build_file_checksums.ser .idea/caches/build_file_checksums.ser
vperf vperf
test/ build/
+24 -2
View File
@@ -7,12 +7,34 @@
#ifndef TEST_H #ifndef TEST_H
#define 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); 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 */ #endif /* !TEST_H */
+17 -7
View File
@@ -9,7 +9,6 @@
#include"./include/test.h" #include"./include/test.h"
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#define MAX_LINE 1024
struct memGlobal { struct memGlobal {
unsigned int All; unsigned int All;
float Useage; float Useage;
@@ -20,15 +19,26 @@ struct cpuGlobal {
float Useage; float Useage;
} cpuG; } cpuG;
struct process { struct Proc_Info pList[MaxCache];
char Name[128];
unsigned id;
float cpu;
float mem;
} pList[1024];
int main() 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); updateMem(&memG.All, &memG.Free, &memG.Useage);
printf("mem: all %u free: %u usage: %f\n", memG.All, memG.Free, memG.Useage); printf("mem: all %u free: %u usage: %f\n", memG.All, memG.Free, memG.Useage);
return 0; return 0;
+139 -5
View File
@@ -7,13 +7,11 @@
#include "./include/test.h" #include "./include/test.h"
#include<stdio.h> #include<stdio.h>
#include<dirent.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LINE 1024 #define MAX_LINE 1024
double get_sqrt(double var1)
{
return sqrt(var1);
}
void updateMem(unsigned int *all, unsigned int *fre, float *useage) { void updateMem(unsigned int *all, unsigned int *fre, float *useage) {
char buf[MAX_LINE]; /*缓冲区*/ char buf[MAX_LINE]; /*缓冲区*/
FILE *fp; /*文件指针*/ FILE *fp; /*文件指针*/
@@ -28,3 +26,139 @@ void updateMem(unsigned int *all, unsigned int *fre, float *useage) {
sscanf(buf, "%*s %u kB",fre); sscanf(buf, "%*s %u kB",fre);
*useage = 1 - (float) *fre / *all; *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 进程独占内存,单位pages,4K 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;
}