merge light's code by hand

This commit is contained in:
Iain 2021-07-20 21:07:55 +08:00
parent f40406bd0d
commit 6ff6a58132
4 changed files with 120 additions and 48 deletions

View File

@ -5,16 +5,33 @@
#include "MonitorProcess.h" #include "MonitorProcess.h"
#include <unistd.h> #include <unistd.h>
#include <stdio.h> #include <stdio.h>
#include<dirent.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#define BUFFER_SIZE_PROC_STAT 1024
#define STAT_UTIME 13 #define STAT_UTIME 13
#define STAT_STIME 14 #define STAT_STIME 14
#define STAT_VSZ 22 #define STAT_VSZ 22
#define STAT_RSS 23 #define STAT_RSS 23
extern int Proc_List_Free_ID; extern int Proc_List_CURRENT;
extern int Proc_List_ID; extern Proc_Info Proc_List[MONITOR_PROCESS_NUMBER];
extern Proc_Info Proc_List[PROCESS_NUMBER];
void cleanProc_Info(Proc_Info *proc) {
int i;
proc->pid = 0;
proc->point = 0;
strcpy(proc->name, "");
for (i = 0; i < UPDATE_FREQUENCY; i++) {
proc->cpuInfo[i].utime = 0;
proc->cpuInfo[i].stime = 0;
proc->memInfo[i].vsz = 0;
proc->memInfo[i].vsz = 0;
}
return;
}
//update process cpu time and mem info by /proc/PID/stat //update process cpu time and mem info by /proc/PID/stat
//example: 1 (systemd) S 0 1 1 0 -1 4194560 16465 675919 50 519 44 100 678 258 20 0 1 0 13 44421120 1253 18446744073709551615 94869015482368 94869016924823 140724538057904 0 0 0 671173123 4096 1260 1 0 0 17 0 0 0 14 0 0 94869019025816 94869019170360 94869036843008 140724538064796 140724538064863 140724538064863 140724538064863 0 //example: 1 (systemd) S 0 1 1 0 -1 4194560 16465 675919 50 519 44 100 678 258 20 0 1 0 13 44421120 1253 18446744073709551615 94869015482368 94869016924823 140724538057904 0 0 0 671173123 4096 1260 1 0 0 17 0 0 0 14 0 0 94869019025816 94869019170360 94869036843008 140724538064796 140724538064863 140724538064863 140724538064863 0
@ -68,56 +85,80 @@ extern Proc_Info Proc_List[PROCESS_NUMBER];
*/ */
int updateProcInfo(Proc_Info *proc) { int updateProcInfo(Proc_Info *proc) {
char file_path[64] = {0}; char file_path[64] = {0};
char buffer[512] = {0}; char buffer[BUFFER_SIZE_PROC_STAT] = {0};
sprintf(file_path, "/proc/%ls/stat", &proc->pid); sprintf(file_path, "./proc/%d/stat", proc->pid);
FILE *fd = NULL; FILE *fd = NULL;
fd = fopen(file_path, "r"); fd = fopen(file_path, "r");
if (fd == NULL) { if (fd == NULL) {
return -1; return -1;
} }
unsigned long utime, stime, vsz, rss;
fgets(buffer, BUFFER_SIZE_PROC_STAT, 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);
proc->point++;
proc->point %= UPDATE_FREQUENCY;
const char *p = buffer; proc->cpuInfo[proc->point].utime = utime;
proc->cpuInfo[proc->point].stime = stime;
int len = strlen(buffer); proc->memInfo[proc->point].vsz = vsz;
int i; proc->memInfo[proc->point].rss = rss * 4;
int count = 0;
for (i = 0; i < len; i++) {
if (' ' == *p) {
count++;
if (count < STAT_UTIME) {
continue;
}
if (count == STAT_STIME) {
int pass;
}
if (count == STAT_UTIME) {
int pass;
}
if (count == STAT_VSZ) {
int pass;
}
if (count == STAT_RSS) { //counted by page(4K)
proc->memInfo->rss *= 4;
int pass;
}
}
p++;
}
fclose(fd); fclose(fd);
return 0; return 0;
} }
int foundProcess(Proc_Info *pList, unsigned int id) {
int i;
for (i = 0; i < MONITOR_PROCESS_NUMBER; i++) {
if ((pList + i)->pid == id) {
return i;
}
}
return -1;
}
unsigned int *getProcess() {
static unsigned int ids[MONITOR_PROCESS_NUMBER] = {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];
}
void updateProcList() { void updateProcList() {
Proc_Info *proc; Proc_Info *proc;
char file_path[64] = {0}; char file_path[64] = {0};
while (1) { while (1) {
if (Proc_List_ID == PROCESS_NUMBER) { if (Proc_List_CURRENT == MONITOR_PROCESS_NUMBER) {
Proc_List_ID = 0; Proc_List_CURRENT = 0;
usleep(1000); usleep(60000 / UPDATE_FREQUENCY);
} }
*proc = Proc_List[Proc_List_ID]; *proc = Proc_List[Proc_List_CURRENT];
//check PID //check PID
if (&proc->pid != 0) { if (&proc->pid != 0) {
if (updateProcInfo(proc) == -1) { if (updateProcInfo(proc) == -1) {
@ -128,7 +169,7 @@ void updateProcList() {
} }
}; };
} }
Proc_List_ID++; Proc_List_CURRENT++;
} }
return; return;
} }

View File

@ -5,15 +5,16 @@
#ifndef ATOP_MONITORPROCESS_H #ifndef ATOP_MONITORPROCESS_H
#define ATOP_MONITORPROCESS_H #define ATOP_MONITORPROCESS_H
#define PROCESS_NUMBER 64 //max monitor 64 process #define UPDATE_FREQUENCY 60 //update times per minute
#define MONITOR_PROCESS_NUMBER 64 //max monitor 64 process
//proc/PID/stat //proc/PID/stat
typedef struct { typedef struct Proc_CPU_Time_ {
unsigned long utime; //user time unsigned long utime; //user time
unsigned long stime; //kernel time unsigned long stime; //kernel time
} Proc_CPU_Time; } Proc_CPU_Time;
//proc/PID/statm,the value should multiply 4 //proc/PID/stat
typedef struct Proc_Mem_ { typedef struct Proc_Mem_ {
unsigned long vsz; //Virtual Memory Size, includes all memory that the process can access unsigned long vsz; //Virtual Memory Size, includes all memory that the process can access
unsigned long rss; //Resident Set Size unsigned long rss; //Resident Set Size
@ -22,19 +23,21 @@ typedef struct Proc_Mem_ {
typedef struct Proc_Info_ { typedef struct Proc_Info_ {
unsigned int pid; unsigned int pid;
char name[128]; char name[128];
int point; //0-29 int point; //[0,UPDATE_FREQUENCY-1)
Proc_CPU_Time cpuInfo[30]; Proc_CPU_Time cpuInfo[UPDATE_FREQUENCY];
Proc_Mem memInfo[30]; Proc_Mem memInfo[UPDATE_FREQUENCY];
} Proc_Info; } Proc_Info;
//kill this process
void cleanProc_Info(Proc_Info *proc);
//kill this process //kill this process
void findProcListByMem(); void findProcListByMem();
//kill this process //kill this process
void findProcListByCPU(); void findProcListByCPU();
int Proc_List_Free_ID; int Proc_List_CURRENT;
int Proc_List_ID; Proc_Info Proc_List[MONITOR_PROCESS_NUMBER]; //max monitor 64 process
Proc_Info Proc_List[64]; //max monitor 64 process
#endif //ATOP_MONITORPROCESS_H #endif //ATOP_MONITORPROCESS_H

View File

@ -2,4 +2,22 @@
// Created by Iain on 2021/7/17. // Created by Iain on 2021/7/17.
// //
#include "MonitorSys.h" #include<stdio.h>
#include <string.h>
#define BUFFER_SIZE_MEMINFO 128
#define SYSTEM_MEM_INFO_PATH "/proc/meminfo"
void updateSystemMem(unsigned int *all, unsigned int *fre, float *useage) {
char buf[BUFFER_SIZE_MEMINFO]; /*缓冲区*/
FILE *fp; /*文件指针*/
if ((fp = fopen(SYSTEM_MEM_INFO_PATH, "r")) == NULL) {
perror("fail to read /proc/meminfo");
return;
}
fgets(buf, BUFFER_SIZE_MEMINFO, fp);
sscanf(buf, "MemTotal: %u kB", all);
fgets(buf, BUFFER_SIZE_MEMINFO, fp);
sscanf(buf, "MemFree %u kB", fre);
*useage = 1 - (float) *fre / *all;
}

View File

@ -12,4 +12,14 @@ typedef struct {
unsigned long idle; unsigned long idle;
} Total_CPU_Time; } Total_CPU_Time;
struct memGlobal {
unsigned int All;
float Usage;
unsigned int Free;
} memG;
struct cpuGlobal {
float Usage;
} cpuG;
#endif //ATOP_MONITORSYS_H #endif //ATOP_MONITORSYS_H