could make and build without error,could monitor system and process cpu,mem,but the filter.h and get ProcDir not finished

This commit is contained in:
Iain 2021-07-21 22:05:28 +08:00
parent a0760bde96
commit 664f458da6
7 changed files with 135 additions and 80 deletions

View File

@ -13,7 +13,6 @@ CMakeFiles/Atop.dir/main.c.o: ../src/server.h
CMakeFiles/Atop.dir/src/ProcessInfo.c.o: ../src/ProcessInfo.c CMakeFiles/Atop.dir/src/ProcessInfo.c.o: ../src/ProcessInfo.c
CMakeFiles/Atop.dir/src/ProcessInfo.c.o: ../src/ProcessInfo.h CMakeFiles/Atop.dir/src/ProcessInfo.c.o: ../src/ProcessInfo.h
CMakeFiles/Atop.dir/src/SystemInfo.c.o: ../src/ProcessInfo.h
CMakeFiles/Atop.dir/src/SystemInfo.c.o: ../src/SystemInfo.c CMakeFiles/Atop.dir/src/SystemInfo.c.o: ../src/SystemInfo.c
CMakeFiles/Atop.dir/src/SystemInfo.c.o: ../src/SystemInfo.h CMakeFiles/Atop.dir/src/SystemInfo.c.o: ../src/SystemInfo.h

View File

@ -1,3 +1,3 @@
Start testing: Jul 21 19:19 CST Start testing: Jul 21 21:35 CST
---------------------------------------------------------- ----------------------------------------------------------
End testing: Jul 21 19:19 CST End testing: Jul 21 21:35 CST

22
main.c
View File

@ -6,35 +6,31 @@
#include <sys/unistd.h> #include <sys/unistd.h>
#include <pthread.h> #include <pthread.h>
#define DEBUG
int main() { int main() {
int rc; int rc;
pthread_t unix_domain_server; pthread_t unix_domain_server;
freeProcInfoList();
//init //init
//get /data/data/dir //get /data/data/dir
//get whitelist //get whitelist
//init Proc_List //init Proc_List
//init Global_Info //init Global_Info
int i = 0; int i = 0;
while (1) { while (1) {
i++; i++;
printf("\nloop:%d\n", i); printf("\nloop:%d\n", i);
usleep(60000000 / UPDATE_FREQUENCY); usleep(60000000 / UPDATE_FREQUENCY * 0.98);
//monitor Global_Info and decicde to kill some process
updateSystemInfo(); updateSystemInfo();
//monitor Proc_List
updateProcList();
} }
//monitor Global_Info and decicde to kill some process
// rc = pthread_create(&unix_domain_server, NULL, (void *)&server, NULL);
// if (rc != 0) {
// perror("unix_domain_server thread fail");
// }
//monitor Proc_List
// rc = pthread_create(&unix_domain_server, NULL, (void *)&server, NULL);
// if (rc != 0) {
// perror("unix_domain_server thread fail");
// }
//server //server
// rc = pthread_create(&unix_domain_server, NULL, (void *)&server, NULL); // rc = pthread_create(&unix_domain_server, NULL, (void *)&server, NULL);
// if (rc != 0) { // if (rc != 0) {

View File

@ -9,13 +9,14 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#define LOG
#define BUFFER_SIZE_PROC_STAT 1024 #define BUFFER_SIZE_PROC_STAT 1024
#define BUFFER_SIZE_PROC_STATUS 128
#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_CURRENT;
extern Proc_Info Proc_List[LIMIT_PROCESS_NUMBER]; extern Proc_Info Proc_List[LIMIT_PROCESS_NUMBER];
inline void freeProcInfo(Proc_Info *proc) { inline void freeProcInfo(Proc_Info *proc) {
@ -33,54 +34,97 @@ inline void freeProcInfo(Proc_Info *proc) {
return; return;
} }
int foundProcNode(Proc_Info *pList, unsigned int pid) { void freeProcInfoList() {
int i; int i;
for (i = 0; i < LIMIT_PROCESS_NUMBER; i++) { for (i = 0; i < LIMIT_PROCESS_NUMBER; i++) {
if ((pList + i)->pid == pid) { freeProcInfo(&Proc_List[i]);
}
return;
}
int foundProcNode(unsigned int pid) {
int i;
for (i = 0; i < LIMIT_PROCESS_NUMBER; i++) {
if ((Proc_List + i)->pid == pid) {
return i; return i;
} }
} }
return -1; return -1;
} }
int foundFreeProcNode(Proc_Info *pList) { int foundFreeProcNode() {
int i; int i;
i = foundProcNode(pList, 0); i = foundProcNode(0);
if (i == -1) { if (i == -1) {
perror("No free procInfo node in procList"); perror("No free procInfo node in procList");
} }
return i; return i;
} }
unsigned int *getProcess() { int setFreeProcNode(unsigned int pid) {
static unsigned int ids[LIMIT_PROCESS_NUMBER] = {0}; //found PID name
DIR *dirp; char buf[BUFFER_SIZE_PROC_STATUS] = {0};
struct dirent *direntp; char PID_name[LIMIT_PROCESS_Name_Length] = {0};
unsigned int id = 0; char file_path[64] = {0};
if ((dirp = opendir("./proc/")) == NULL) { sprintf(file_path, "/proc/%d/status", pid);
exit(1); FILE *fp;
if ((fp = fopen(file_path, "r")) == NULL) {
perror("fail to read /proc/PID/status");
return -1;
} }
int index = 0; fgets(buf, BUFFER_SIZE_PROC_STATUS, fp);
int i; #if defined(LOG)
unsigned int temp; printf("/proc/%d/status: %s", pid, buf);
while ((direntp = readdir(dirp)) != NULL) { #endif
id = atoi(direntp->d_name); sscanf(buf, "Name: %s", PID_name);
if (id > 0) {
for (i = 0; i < index; i++) { //compare PID name with whitelist
if (ids[i] > id) {
temp = id; //check PID in ProcList
id = ids[i]; if (foundProcNode(pid) == -1) {
ids[i] = temp; return -1;
}
}
ids[index] = id;
index++;
}
} }
closedir(dirp);
return &ids[0]; //found free Proc Node
int i = foundFreeProcNode();
//set Proc Info
Proc_List[i].pid = pid;
strcpy(Proc_List[i].name, PID_name);
} }
//unsigned int *getProcess() {
// DIR *dir;
// struct dirent *entry;
// unsigned int id = 0;
// static unsigned int ids[LIMIT_PROCESS_NUMBER] = {0};
//
// if ((dir = opendir("/proc/")) == NULL) {
// perror("cannot access /proc directory");
// 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];
//}
//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
/* pid 进程ID 0 /* pid 进程ID 0
@ -144,9 +188,13 @@ int updateProcInfo(Proc_Info *proc) {
fgets(buffer, BUFFER_SIZE_PROC_STAT, fd); fgets(buffer, BUFFER_SIZE_PROC_STAT, fd);
// 14 utime 15 stime 23 24 vsz rss // 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 // 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 %lu %lu %*s %*s %*s %*s %*s %*s %*s %*s %lu %lu ", sscanf(buffer, "%*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %lu %lu %*s %*s %*s %*s %*s %*s %*s %*s %lu %lu ",
proc->name, &utime, &stime, &vsz, &rss); &utime, &stime, &vsz, &rss);
printf("%d %s %ld %ld %ld %ld\n", proc->pid, proc->name, utime, stime, vsz, rss); #if defined(LOG)
printf("ProcInfo:PID=%d,Name=%s,utime=%ld,stime=%ld,vsz=%ld,rss=%ld\n", proc->pid, proc->name, utime, stime, vsz,
rss);
#endif
proc->point++; proc->point++;
proc->point %= UPDATE_FREQUENCY; proc->point %= UPDATE_FREQUENCY;
proc->cpuInfo[proc->point].utime = utime; proc->cpuInfo[proc->point].utime = utime;
@ -159,29 +207,29 @@ int updateProcInfo(Proc_Info *proc) {
} }
void updateProcList() { void updateProcList() {
Proc_Info *proc; #if defined(LOG)
printf("/****** Proc Info ******/\n");
int i;
for (i = 0; i < LIMIT_PROCESS_NUMBER; i++) {
printf("id=%d,", Proc_List[i].pid);
}
printf("\n");
#endif
setFreeProcNode(1);
setFreeProcNode(101);
char file_path[64] = {0}; char file_path[64] = {0};
while (1) { int currentProcNode;
if (Proc_List_CURRENT == LIMIT_PROCESS_NUMBER) { for (currentProcNode = 0; currentProcNode < LIMIT_PROCESS_NUMBER; currentProcNode++) {
Proc_List_CURRENT = 0;
usleep(60000 / UPDATE_FREQUENCY);
}
*proc = Proc_List[Proc_List_CURRENT];
//check PID //check PID
if (&proc->pid != 0) { if (Proc_List[currentProcNode].pid != 0) {
if (updateProcInfo(proc) == -1) { if (updateProcInfo(&Proc_List[currentProcNode]) == -1) {
//check path exist //check path exist
sprintf(file_path, "/proc/%ls", &proc->pid); sprintf(file_path, "/proc/%d", Proc_List[currentProcNode].pid);
if (access(file_path, F_OK)) { if (access(file_path, F_OK)) {
proc->pid = 0; freeProcInfo(&Proc_List[currentProcNode]);
} }
}; };
} }
Proc_List_CURRENT++;
} }
return; return;
}
void initProcList() {
return;
} }

View File

@ -7,6 +7,7 @@
#define UPDATE_FREQUENCY 60 //update times per minute #define UPDATE_FREQUENCY 60 //update times per minute
#define LIMIT_PROCESS_NUMBER 64 //max monitor 64 process #define LIMIT_PROCESS_NUMBER 64 //max monitor 64 process
#define LIMIT_PROCESS_Name_Length 128
//proc/PID/stat //proc/PID/stat
typedef struct Proc_CPU_Time_ { typedef struct Proc_CPU_Time_ {
@ -22,13 +23,14 @@ typedef struct Proc_Mem_ {
typedef struct Proc_Info_ { typedef struct Proc_Info_ {
unsigned int pid; unsigned int pid;
char name[128]; char name[LIMIT_PROCESS_Name_Length];
int point; //[0,UPDATE_FREQUENCY-1) int point; //[0,UPDATE_FREQUENCY-1)
Proc_CPU_Time cpuInfo[UPDATE_FREQUENCY]; Proc_CPU_Time cpuInfo[UPDATE_FREQUENCY];
Proc_Mem memInfo[UPDATE_FREQUENCY]; Proc_Mem memInfo[UPDATE_FREQUENCY];
} Proc_Info; } Proc_Info;
void freeProcInfo(Proc_Info *proc); void freeProcInfo(Proc_Info *proc);
void freeProcInfoList();
//kill this process //kill this process
void findProcListByMem(); void findProcListByMem();
@ -36,7 +38,8 @@ void findProcListByMem();
//kill this process //kill this process
void findProcListByCPU(); void findProcListByCPU();
int Proc_List_CURRENT; void updateProcList();
Proc_Info Proc_List[LIMIT_PROCESS_NUMBER]; //max monitor 64 process Proc_Info Proc_List[LIMIT_PROCESS_NUMBER]; //max monitor 64 process
#endif //ATOP_PROCESSINFO_H #endif //ATOP_PROCESSINFO_H

View File

@ -3,12 +3,10 @@
// //
#include "SystemInfo.h" #include "SystemInfo.h"
#include "ProcessInfo.h"
#include <unistd.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#define DEBUG #define LOG
#define BUFFER_SIZE_STATINFO 256 #define BUFFER_SIZE_STATINFO 256
#define BUFFER_SIZE_MEMINFO 128 #define BUFFER_SIZE_MEMINFO 128
#define SYSTEM_CPU_INFO_PATH "/proc/stat" #define SYSTEM_CPU_INFO_PATH "/proc/stat"
@ -25,7 +23,7 @@ void updateSystemCPU(Sys_CPU_Time *cpuInfo) {
return; return;
} }
fgets(buf, BUFFER_SIZE_STATINFO, fp); fgets(buf, BUFFER_SIZE_STATINFO, fp);
#if defined(DEBUG) #if defined(LOG)
printf("/proc/stat:%s", buf); printf("/proc/stat:%s", buf);
#endif #endif
unsigned long cpu_user, cpu_nice, cpu_sys, cpu_idle; unsigned long cpu_user, cpu_nice, cpu_sys, cpu_idle;
@ -46,7 +44,7 @@ void updateSystemCPU(Sys_CPU_Time *cpuInfo) {
total_cpu_time = cpuInfo->Unit_Cpu_Time.user + cpuInfo->Unit_Cpu_Time.nice + cpuInfo->Unit_Cpu_Time.system + total_cpu_time = cpuInfo->Unit_Cpu_Time.user + cpuInfo->Unit_Cpu_Time.nice + cpuInfo->Unit_Cpu_Time.system +
cpuInfo->Unit_Cpu_Time.idle; cpuInfo->Unit_Cpu_Time.idle;
cpuInfo->Usage = 1 - (float) cpuInfo->Unit_Cpu_Time.idle / total_cpu_time; cpuInfo->Usage = 1 - (float) cpuInfo->Unit_Cpu_Time.idle / total_cpu_time;
#if defined(DEBUG) #if defined(LOG)
printf("SystemCPU:Usage=%f,User=%ld,Nice=%ld,Sys=%ld,Idle=%ld\n", cpuInfo->Usage, cpuInfo->Total_Cpu_Time.user, printf("SystemCPU:Usage=%f,User=%ld,Nice=%ld,Sys=%ld,Idle=%ld\n", cpuInfo->Usage, cpuInfo->Total_Cpu_Time.user,
cpuInfo->Total_Cpu_Time.nice, cpuInfo->Total_Cpu_Time.system, cpuInfo->Total_Cpu_Time.idle); cpuInfo->Total_Cpu_Time.nice, cpuInfo->Total_Cpu_Time.system, cpuInfo->Total_Cpu_Time.idle);
#endif #endif
@ -65,17 +63,24 @@ void updateSystemMem(Sys_Mem *memInfo) {
} }
fgets(buf, BUFFER_SIZE_MEMINFO, fp); fgets(buf, BUFFER_SIZE_MEMINFO, fp);
sscanf(buf, "MemTotal: %lu kB", &memInfo->All); sscanf(buf, "MemTotal: %lu kB", &memInfo->Total);
#if defined(DEBUG) #if defined(LOG)
printf("/proc/stat:%s", buf); printf("/proc/stat: %s", buf);
#endif #endif
fgets(buf, BUFFER_SIZE_MEMINFO, fp); fgets(buf, BUFFER_SIZE_MEMINFO, fp);
sscanf(buf, "MemFree: %lu kB", &memInfo->Free); sscanf(buf, "MemFree: %lu kB", &memInfo->Free);
memInfo->Usage = 1 - (float) memInfo->Free / memInfo->All; #if defined(LOG)
#if defined(DEBUG) printf("/proc/stat: %s", buf);
printf("/proc/stat:%s", buf); #endif
printf("SystemMem:Usage=%f,Total=%ld,Free=%ld\n", memInfo->Usage, memInfo->All, memInfo->Free);
fgets(buf, BUFFER_SIZE_MEMINFO, fp);
sscanf(buf, "MemAvailable: %lu kB", &memInfo->Available);
memInfo->Usage = 1 - (float) memInfo->Available / memInfo->Total;
#if defined(LOG)
printf("/proc/stat: %s", buf);
printf("SystemMem:Usage=%f,Total=%ld,Free=%ld,Available=%ld\n", memInfo->Usage, memInfo->Total, memInfo->Free,
memInfo->Available);
#endif #endif
@ -85,6 +90,9 @@ void updateSystemMem(Sys_Mem *memInfo) {
} }
void updateSystemInfo() { void updateSystemInfo() {
#if defined(LOG)
printf("/****** System Info ******/\n");
#endif
updateSystemCPU(&Sys_CPU_Time_Info); updateSystemCPU(&Sys_CPU_Time_Info);
updateSystemMem(&Sys_Mem_Info); updateSystemMem(&Sys_Mem_Info);
} }

View File

@ -20,8 +20,9 @@ typedef struct Sys_CPU_Time_ {
typedef struct Sys_Mem_ { typedef struct Sys_Mem_ {
float Usage; float Usage;
unsigned long All; unsigned long Total;
unsigned long Free; unsigned long Free;
unsigned long Available;
} Sys_Mem; } Sys_Mem;
void updateSystemInfo(); void updateSystemInfo();