just sync

This commit is contained in:
Your Name
2021-07-21 00:38:01 +08:00
parent 6ff6a58132
commit 34fe72877e
36 changed files with 2539 additions and 74 deletions
+56 -44
View File
@@ -2,7 +2,7 @@
// Created by Iain on 2021/7/17.
//
#include "MonitorProcess.h"
#include "ProcessInfo.h"
#include <unistd.h>
#include <stdio.h>
#include<dirent.h>
@@ -16,9 +16,9 @@
#define STAT_RSS 23
extern int Proc_List_CURRENT;
extern Proc_Info Proc_List[MONITOR_PROCESS_NUMBER];
extern Proc_Info Proc_List[LIMIT_PROCESS_NUMBER];
void cleanProc_Info(Proc_Info *proc) {
inline void freeProc_Info(Proc_Info *proc) {
int i;
proc->pid = 0;
proc->point = 0;
@@ -33,6 +33,54 @@ void cleanProc_Info(Proc_Info *proc) {
return;
}
inline int foundProcNode(Proc_Info *pList, unsigned int pid) {
int i;
for (i = 0; i < LIMIT_PROCESS_NUMBER; i++) {
if ((pList + i)->pid == pid) {
return i;
}
}
return -1;
}
int foundFreeProcNode(Proc_Info *pList) {
int i;
i=foundProcNode(pList, 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 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
@@ -86,7 +134,7 @@ void cleanProc_Info(Proc_Info *proc) {
int updateProcInfo(Proc_Info *proc) {
char file_path[64] = {0};
char buffer[BUFFER_SIZE_PROC_STAT] = {0};
sprintf(file_path, "./proc/%d/stat", proc->pid);
sprintf(file_path, "/proc/%d/stat", proc->pid);
FILE *fd = NULL;
fd = fopen(file_path, "r");
if (fd == NULL) {
@@ -101,7 +149,6 @@ int updateProcInfo(Proc_Info *proc) {
printf("%d %s %d %d %d %d\n", proc->pid, proc->name, utime, stime, vsz, rss);
proc->point++;
proc->point %= UPDATE_FREQUENCY;
proc->cpuInfo[proc->point].utime = utime;
proc->cpuInfo[proc->point].stime = stime;
proc->memInfo[proc->point].vsz = vsz;
@@ -111,45 +158,6 @@ int updateProcInfo(Proc_Info *proc) {
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() {
Proc_Info *proc;
char file_path[64] = {0};
@@ -172,4 +180,8 @@ void updateProcList() {
Proc_List_CURRENT++;
}
return;
}
void initProcList() {
return;
}
+5 -6
View File
@@ -2,11 +2,11 @@
// Created by Iain on 2021/7/17.
//
#ifndef ATOP_MONITORPROCESS_H
#define ATOP_MONITORPROCESS_H
#ifndef ATOP_PROCESSINFO_H
#define ATOP_PROCESSINFO_H
#define UPDATE_FREQUENCY 60 //update times per minute
#define MONITOR_PROCESS_NUMBER 64 //max monitor 64 process
#define LIMIT_PROCESS_NUMBER 64 //max monitor 64 process
//proc/PID/stat
typedef struct Proc_CPU_Time_ {
@@ -28,8 +28,7 @@ typedef struct Proc_Info_ {
Proc_Mem memInfo[UPDATE_FREQUENCY];
} Proc_Info;
//kill this process
void cleanProc_Info(Proc_Info *proc);
void freeProc_Info(Proc_Info *proc);
//kill this process
void findProcListByMem();
@@ -40,4 +39,4 @@ void findProcListByCPU();
int Proc_List_CURRENT;
Proc_Info Proc_List[MONITOR_PROCESS_NUMBER]; //max monitor 64 process
#endif //ATOP_MONITORPROCESS_H
#endif //ATOP_PROCESSINFO_H
+7 -3
View File
@@ -2,8 +2,8 @@
// Created by Iain on 2021/7/17.
//
#ifndef ATOP_MONITORSYS_H
#define ATOP_MONITORSYS_H
#ifndef ATOP_SYSTEMINFO_H
#define ATOP_SYSTEMINFO_H
typedef struct {
unsigned long user;
@@ -19,7 +19,11 @@ struct memGlobal {
} memG;
struct cpuGlobal {
unsigned long user;
unsigned long nice;
unsigned long system;
unsigned long idle;
float Usage;
} cpuG;
#endif //ATOP_MONITORSYS_H
#endif //ATOP_SYSTEMINFO_H
+2 -2
View File
@@ -2,8 +2,8 @@
// Created by Iain on 2021/7/17.
//
#include "init.h"
#include "MonitorProcess.h"
#include "filter.h"
#include "ProcessInfo.h"
#include <stdio.h>
#include <dirent.h> // read dir
#include <regex.h> // regular expression
+11
View File
@@ -0,0 +1,11 @@
//
// Created by Iain on 2021/7/17.
//
#ifndef ATOP_FILTER_H
#define ATOP_FILTER_H
int init();
#endif //ATOP_FILTER_H
-11
View File
@@ -1,11 +0,0 @@
//
// Created by Iain on 2021/7/17.
//
#ifndef ATOP_INIT_H
#define ATOP_INIT_H
int init();
#endif //ATOP_INIT_H