/* * test.c * Copyright (C) 2021 light * * Distributed under terms of the MIT license. */ #include "./include/test.h" #include #include #include #include #define MAX_LINE 1024 void updateMem(unsigned int *all, unsigned int *fre, float *useage) { char buf[MAX_LINE]; /*缓冲区*/ FILE *fp; /*文件指针*/ if((fp = fopen("./meminfo","r")) == NULL) { perror("fail to read"); return; } fgets(buf,MAX_LINE,fp); sscanf(buf, "%*s %u kB",all); fgets(buf,MAX_LINE,fp); 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 进程独占内存,单位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; ipid == id) { return i; } } return -1; } int assertProcess(Proc_Info *pList, unsigned int id) { int i, j; for (i =0; ipid == 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; }