This commit is contained in:
Your Name
2021-07-19 19:38:43 +08:00
parent 208f4aeac2
commit 8c5dd9ca47
17 changed files with 3772 additions and 345 deletions
+133
View File
@@ -0,0 +1,133 @@
//
// Created by Iain on 2021/7/17.
//
#include "MonitorProcess.h"
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#define STAT_UTIME 13
#define STAT_STIME 14
#define STAT_VSZ 22
#define STAT_RSS 23
//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
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
*/
inline int updateProcInfo(struct Proc_Info *proc) {
char file_path[64] = {0};
char buffer[512] = {0};
sprintf(file_path, "/proc/%d/stat", &proc->pid);
FILE *fd = NULL;
fd = fopen(file_path, "r");
if (fd == NULL) {
return -1;
}
const char *p = buffer;
int len = strlen(buffer);
int i;
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)
int pass;
}
}
p++;
}
fgets(line_buff, sizeof(line_buff), fd);
sscanf(line_buff, "%ld %ld ", &proc->memInfo->vsz, &proc->memInfo->rss);
proc->memInfo->vsz *= 4;
proc->memInfo->rss *= 4;
fclose(fd);
return 0;
}
void updateProcList() {
struct Proc_Info *proc;
char file_path[64] = {0};
while (1) {
if (Proc_List_ID == 30) {
Proc_List_ID = 0;
usleep(1000);
}
*proc = Proc_List[Proc_List_ID];
//check PID
if (&proc->pid != 0) {
if (updateProcInfo(proc) == -1) {
//check path exist
sprintf(file_path, "/proc/%d", &proc->pid);
if (access(file_path, F_OK)) {
proc->pid = 0;
}
};
}
Proc_List_ID++;
}
}
+38
View File
@@ -0,0 +1,38 @@
//
// Created by Iain on 2021/7/17.
//
#ifndef ATOP_MONITORPROCESS_H
#define ATOP_MONITORPROCESS_H
//proc/PID/stat
typedef struct Proc_CPU_Time {
unsigned long utime; //user time
unsigned long stime; //kernel time
};
//proc/PID/statm,the value should multiply 4
typedef struct Proc_Mem {
unsigned long vsz; //Virtual Memory Size, includes all memory that the process can access
unsigned long rss; //Resident Set Size
};
typedef struct Proc_Info {
unsigned int pid;
char name[128];
int point; //0-29
struct Proc_CPU_Time cpuInfo[30];
struct Proc_Mem memInfo[30];
};
int Proc_List_Free_ID = 0;
int Proc_List_ID = 0;
struct Proc_Info Proc_List[64]; //max monitor 64 process
//kill this process
void findProcListByMem();
//kill this process
void findProcListByCPU();
#endif //ATOP_MONITORPROCESS_H
+5
View File
@@ -0,0 +1,5 @@
//
// Created by Iain on 2021/7/17.
//
#include "MonitorSys.h"
+15
View File
@@ -0,0 +1,15 @@
//
// Created by Iain on 2021/7/17.
//
#ifndef ATOP_MONITORSYS_H
#define ATOP_MONITORSYS_H
typedef struct {
unsigned long user;
unsigned long nice;
unsigned long system;
unsigned long idle;
} Total_CPU_Time;
#endif //ATOP_MONITORSYS_H
+19
View File
@@ -0,0 +1,19 @@
//
// Created by Iain on 2021/7/17.
//
#include "init.h"
int initCPUInfo() {
return 0;
}
int initMemInfo() {
return 0;
}
int init() {
initCPUInfo();
initMemInfo();
}
+13
View File
@@ -0,0 +1,13 @@
//
// Created by Iain on 2021/7/17.
//
#ifndef ATOP_INIT_H
#define ATOP_INIT_H
int initCPUInfo();
int initMemInfo();
int init();
#endif //ATOP_INIT_H
+97
View File
@@ -0,0 +1,97 @@
//
// Created by Iain on 2021/7/18.
//
#include "server.h"
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
//unix socket server
#define SOCK_PATH "/var/atop.server"
#define SOCK_LIMIT 5
#define DATA "Hello from server"
int server() {
int server_sock, client_sock, len, rc;
int bytes_rec = 0;
struct sockaddr_un server_sockaddr;
struct sockaddr_un client_sockaddr;
char buf[256];
memset(&server_sockaddr, 0, sizeof(struct sockaddr_un));
memset(&client_sockaddr, 0, sizeof(struct sockaddr_un));
memset(buf, 0, 256);
server_sock = socket(AF_UNIX, SOCK_STREAM, 0);
if (server_sock == -1) {
perror("ERROR:Unix Domain Socket Open");
exit(1);
}
server_sockaddr.sun_family = AF_UNIX;
strcpy(server_sockaddr.sun_path, SOCK_PATH);
len = sizeof(server_sockaddr);
unlink(SOCK_PATH);
rc = bind(server_sock, (struct sockaddr *) &server_sockaddr, len);
if (rc == -1) {
perror("ERROR:Unix Domain Socket Bind");
close(server_sock);
exit(1);
}
rc = listen(server_sock, SOCK_LIMIT);
if (rc == -1) {
perror("ERROR:Unix Domain Socket Listen");
close(server_sock);
exit(1);
}
printf("socket listening...\n");
while (1) {
client_sock = accept(server_sock, (struct sockaddr *) &client_sockaddr, &len);
if (client_sock == -1) {
perror("ERROR:Unix Domain Socket Accept");
close(server_sock);
close(client_sock);
exit(1);
}
len = sizeof(client_sockaddr);
rc = getpeername(client_sock, (struct sockaddr *) &client_sockaddr, &len);
if (rc == -1) {
close(server_sock);
close(client_sock);
exit(1);
} else {
printf("Client socket filepath: %s\n", client_sockaddr.sun_path);
}
printf("waiting to read...\n");
bytes_rec = recv(client_sock, buf, sizeof(buf), 0);
if (bytes_rec == -1) {
perror("ERROR:Unix Domain Socket Recv");
close(server_sock);
close(client_sock);
exit(1);
} else {
printf("DATA RECEIVED = %s\n", buf);
}
memset(buf, 0, 256);
strcpy(buf, DATA);
printf("Sending data...\n");
rc = send(client_sock, buf, strlen(buf), 0);
if (rc == -1) {
perror("ERROR:Unix Domain Socket Send");
close(server_sock);
close(client_sock);
exit(1);
}
close(client_sock);
}
}
+10
View File
@@ -0,0 +1,10 @@
//
// Created by Iain on 2021/7/18.
//
#ifndef ATOP_SERVER_H
#define ATOP_SERVER_H
int server();
#endif //ATOP_SERVER_H