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:
parent
a0760bde96
commit
664f458da6
@ -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.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.h
|
||||
|
||||
|
||||
@ -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
22
main.c
@ -6,35 +6,31 @@
|
||||
#include <sys/unistd.h>
|
||||
#include <pthread.h>
|
||||
|
||||
#define DEBUG
|
||||
|
||||
int main() {
|
||||
int rc;
|
||||
pthread_t unix_domain_server;
|
||||
|
||||
freeProcInfoList();
|
||||
|
||||
//init
|
||||
//get /data/data/dir
|
||||
//get whitelist
|
||||
//init Proc_List
|
||||
//init Global_Info
|
||||
int i = 0;
|
||||
|
||||
while (1) {
|
||||
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();
|
||||
//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
|
||||
// rc = pthread_create(&unix_domain_server, NULL, (void *)&server, NULL);
|
||||
// if (rc != 0) {
|
||||
|
||||
@ -9,13 +9,14 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define LOG
|
||||
#define BUFFER_SIZE_PROC_STAT 1024
|
||||
#define BUFFER_SIZE_PROC_STATUS 128
|
||||
#define STAT_UTIME 13
|
||||
#define STAT_STIME 14
|
||||
#define STAT_VSZ 22
|
||||
#define STAT_RSS 23
|
||||
|
||||
extern int Proc_List_CURRENT;
|
||||
extern Proc_Info Proc_List[LIMIT_PROCESS_NUMBER];
|
||||
|
||||
inline void freeProcInfo(Proc_Info *proc) {
|
||||
@ -33,54 +34,97 @@ inline void freeProcInfo(Proc_Info *proc) {
|
||||
return;
|
||||
}
|
||||
|
||||
int foundProcNode(Proc_Info *pList, unsigned int pid) {
|
||||
void freeProcInfoList() {
|
||||
int 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 -1;
|
||||
}
|
||||
|
||||
int foundFreeProcNode(Proc_Info *pList) {
|
||||
int foundFreeProcNode() {
|
||||
int i;
|
||||
i = foundProcNode(pList, 0);
|
||||
i = foundProcNode(0);
|
||||
if (i == -1) {
|
||||
perror("No free procInfo node in procList");
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
unsigned int *getProcess() {
|
||||
static unsigned int ids[LIMIT_PROCESS_NUMBER] = {0};
|
||||
DIR *dirp;
|
||||
struct dirent *direntp;
|
||||
unsigned int id = 0;
|
||||
if ((dirp = opendir("./proc/")) == NULL) {
|
||||
exit(1);
|
||||
int setFreeProcNode(unsigned int pid) {
|
||||
//found PID name
|
||||
char buf[BUFFER_SIZE_PROC_STATUS] = {0};
|
||||
char PID_name[LIMIT_PROCESS_Name_Length] = {0};
|
||||
char file_path[64] = {0};
|
||||
sprintf(file_path, "/proc/%d/status", pid);
|
||||
FILE *fp;
|
||||
if ((fp = fopen(file_path, "r")) == NULL) {
|
||||
perror("fail to read /proc/PID/status");
|
||||
return -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++;
|
||||
}
|
||||
fgets(buf, BUFFER_SIZE_PROC_STATUS, fp);
|
||||
#if defined(LOG)
|
||||
printf("/proc/%d/status: %s", pid, buf);
|
||||
#endif
|
||||
sscanf(buf, "Name: %s", PID_name);
|
||||
|
||||
//compare PID name with whitelist
|
||||
|
||||
//check PID in ProcList
|
||||
if (foundProcNode(pid) == -1) {
|
||||
return -1;
|
||||
}
|
||||
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
|
||||
//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
|
||||
@ -144,9 +188,13 @@ int updateProcInfo(Proc_Info *proc) {
|
||||
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 %lu %lu %*s %*s %*s %*s %*s %*s %*s %*s %lu %lu ",
|
||||
proc->name, &utime, &stime, &vsz, &rss);
|
||||
printf("%d %s %ld %ld %ld %ld\n", proc->pid, proc->name, utime, stime, vsz, rss);
|
||||
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 ",
|
||||
&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 %= UPDATE_FREQUENCY;
|
||||
proc->cpuInfo[proc->point].utime = utime;
|
||||
@ -159,29 +207,29 @@ int updateProcInfo(Proc_Info *proc) {
|
||||
}
|
||||
|
||||
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};
|
||||
while (1) {
|
||||
if (Proc_List_CURRENT == LIMIT_PROCESS_NUMBER) {
|
||||
Proc_List_CURRENT = 0;
|
||||
usleep(60000 / UPDATE_FREQUENCY);
|
||||
}
|
||||
*proc = Proc_List[Proc_List_CURRENT];
|
||||
int currentProcNode;
|
||||
for (currentProcNode = 0; currentProcNode < LIMIT_PROCESS_NUMBER; currentProcNode++) {
|
||||
//check PID
|
||||
if (&proc->pid != 0) {
|
||||
if (updateProcInfo(proc) == -1) {
|
||||
if (Proc_List[currentProcNode].pid != 0) {
|
||||
if (updateProcInfo(&Proc_List[currentProcNode]) == -1) {
|
||||
//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)) {
|
||||
proc->pid = 0;
|
||||
freeProcInfo(&Proc_List[currentProcNode]);
|
||||
}
|
||||
};
|
||||
}
|
||||
Proc_List_CURRENT++;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
void initProcList() {
|
||||
return;
|
||||
}
|
||||
@ -7,6 +7,7 @@
|
||||
|
||||
#define UPDATE_FREQUENCY 60 //update times per minute
|
||||
#define LIMIT_PROCESS_NUMBER 64 //max monitor 64 process
|
||||
#define LIMIT_PROCESS_Name_Length 128
|
||||
|
||||
//proc/PID/stat
|
||||
typedef struct Proc_CPU_Time_ {
|
||||
@ -22,13 +23,14 @@ typedef struct Proc_Mem_ {
|
||||
|
||||
typedef struct Proc_Info_ {
|
||||
unsigned int pid;
|
||||
char name[128];
|
||||
char name[LIMIT_PROCESS_Name_Length];
|
||||
int point; //[0,UPDATE_FREQUENCY-1)
|
||||
Proc_CPU_Time cpuInfo[UPDATE_FREQUENCY];
|
||||
Proc_Mem memInfo[UPDATE_FREQUENCY];
|
||||
} Proc_Info;
|
||||
|
||||
void freeProcInfo(Proc_Info *proc);
|
||||
void freeProcInfoList();
|
||||
|
||||
//kill this process
|
||||
void findProcListByMem();
|
||||
@ -36,7 +38,8 @@ void findProcListByMem();
|
||||
//kill this process
|
||||
void findProcListByCPU();
|
||||
|
||||
int Proc_List_CURRENT;
|
||||
void updateProcList();
|
||||
|
||||
Proc_Info Proc_List[LIMIT_PROCESS_NUMBER]; //max monitor 64 process
|
||||
|
||||
#endif //ATOP_PROCESSINFO_H
|
||||
|
||||
@ -3,12 +3,10 @@
|
||||
//
|
||||
|
||||
#include "SystemInfo.h"
|
||||
#include "ProcessInfo.h"
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define DEBUG
|
||||
#define LOG
|
||||
#define BUFFER_SIZE_STATINFO 256
|
||||
#define BUFFER_SIZE_MEMINFO 128
|
||||
#define SYSTEM_CPU_INFO_PATH "/proc/stat"
|
||||
@ -25,7 +23,7 @@ void updateSystemCPU(Sys_CPU_Time *cpuInfo) {
|
||||
return;
|
||||
}
|
||||
fgets(buf, BUFFER_SIZE_STATINFO, fp);
|
||||
#if defined(DEBUG)
|
||||
#if defined(LOG)
|
||||
printf("/proc/stat:%s", buf);
|
||||
#endif
|
||||
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 +
|
||||
cpuInfo->Unit_Cpu_Time.idle;
|
||||
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,
|
||||
cpuInfo->Total_Cpu_Time.nice, cpuInfo->Total_Cpu_Time.system, cpuInfo->Total_Cpu_Time.idle);
|
||||
#endif
|
||||
@ -65,17 +63,24 @@ void updateSystemMem(Sys_Mem *memInfo) {
|
||||
}
|
||||
|
||||
fgets(buf, BUFFER_SIZE_MEMINFO, fp);
|
||||
sscanf(buf, "MemTotal: %lu kB", &memInfo->All);
|
||||
#if defined(DEBUG)
|
||||
printf("/proc/stat:%s", buf);
|
||||
sscanf(buf, "MemTotal: %lu kB", &memInfo->Total);
|
||||
#if defined(LOG)
|
||||
printf("/proc/stat: %s", buf);
|
||||
#endif
|
||||
|
||||
fgets(buf, BUFFER_SIZE_MEMINFO, fp);
|
||||
sscanf(buf, "MemFree: %lu kB", &memInfo->Free);
|
||||
memInfo->Usage = 1 - (float) memInfo->Free / memInfo->All;
|
||||
#if defined(DEBUG)
|
||||
printf("/proc/stat:%s", buf);
|
||||
printf("SystemMem:Usage=%f,Total=%ld,Free=%ld\n", memInfo->Usage, memInfo->All, memInfo->Free);
|
||||
#if defined(LOG)
|
||||
printf("/proc/stat: %s", buf);
|
||||
#endif
|
||||
|
||||
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
|
||||
|
||||
|
||||
@ -85,6 +90,9 @@ void updateSystemMem(Sys_Mem *memInfo) {
|
||||
}
|
||||
|
||||
void updateSystemInfo() {
|
||||
#if defined(LOG)
|
||||
printf("/****** System Info ******/\n");
|
||||
#endif
|
||||
updateSystemCPU(&Sys_CPU_Time_Info);
|
||||
updateSystemMem(&Sys_Mem_Info);
|
||||
}
|
||||
|
||||
@ -20,8 +20,9 @@ typedef struct Sys_CPU_Time_ {
|
||||
|
||||
typedef struct Sys_Mem_ {
|
||||
float Usage;
|
||||
unsigned long All;
|
||||
unsigned long Total;
|
||||
unsigned long Free;
|
||||
unsigned long Available;
|
||||
} Sys_Mem;
|
||||
|
||||
void updateSystemInfo();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user