add init.h and init.c; not finished; go home to continue

This commit is contained in:
Iain 2021-07-19 22:17:46 +08:00
parent 5cecf3b5ba
commit f40406bd0d
6 changed files with 144 additions and 122 deletions

13
main.c
View File

@ -13,9 +13,22 @@ int main() {
//get /data/data/dir
//get whitelist
//init Proc_List
//init Global_Info
printf("%ld",Proc_List[0].pid);
init();
//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) {

View File

@ -14,57 +14,57 @@
extern int Proc_List_Free_ID;
extern int Proc_List_ID;
extern Proc_Info Proc_List[64]; //max monitor 64 process
extern Proc_Info Proc_List[PROCESS_NUMBER];
//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 pages4K 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
* 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 pages4K 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
*/
int updateProcInfo(Proc_Info *proc) {
char file_path[64] = {0};
@ -113,7 +113,7 @@ void updateProcList() {
Proc_Info *proc;
char file_path[64] = {0};
while (1) {
if (Proc_List_ID == 30) {
if (Proc_List_ID == PROCESS_NUMBER) {
Proc_List_ID = 0;
usleep(1000);
}

View File

@ -5,6 +5,8 @@
#ifndef ATOP_MONITORPROCESS_H
#define ATOP_MONITORPROCESS_H
#define PROCESS_NUMBER 64 //max monitor 64 process
//proc/PID/stat
typedef struct {
unsigned long utime; //user time

View File

@ -3,17 +3,90 @@
//
#include "init.h"
#include "MonitorProcess.h"
#include <stdio.h>
#include <dirent.h> // read dir
#include <regex.h> // regular expression
#include <string.h>
int initCPUInfo() {
/*
* dirent的属性d_name里存储了app的名字
* c语言没有字符串数组app名dirent*
* app_list[index]->d_name就可以获取文件名
*/
#define AOSP_APP_Path "/data/data"
#define White_List_Path "atop_whitelist.txt"
static char Regex_Pattern[2][64] = {{"com.android.*"},
{"com.google.*"}};
static regex_t Regex_Compare[2]; //the Regex_Compare array size must equal to Regex_Rule array size
int filterByRegex(char *buf) {
// variable declaration for regular expression
err_code = regexec(&reg, buf, 0, NULL, 0)
return 0;
}
int initMemInfo() {
int filterByWhiteList(char *buf) {
return 0;
}
int getAppName() {
DIR *dir = NULL;
int app_index = 0;
struct dirent *read_result;
struct dirent *app_list[PROCESS_NUMBER];
if ((dir = opendir(AOSP_APP_Path)) == NULL) {
printf("open dir failed\n");
return -1;
}
while ((read_result = readdir(dir)) != NULL) {
// filter, ignore current dir + parent dir
if (filterByRegex(read_result->d_name) == 0 ||
filterByWhiteList(read_result->d_name) == 0 ||
strcmp(read_result->d_name, ".") == 0 ||
strcmp(read_result->d_name, "..") == 0)
continue;
// d_type = 4 means it's a directory
if (read_result->d_type == 4) {
app_list[app_index] = read_result;
app_index++;
}
}
// print apps' names
printf("we have %d apps\n", app_index);
for (int i = 0; i < app_index; i++) {
printf("app %d : %s\n", i, app_list[i]->d_name);
}
return 0;
}
int init_regex() {
int rule_num = sizeof(Regex_Pattern);
if (sizeof(Regex_Compare) != rule_num) {
return -1;
}
int id;
int err_code;
int compression_flags = REG_EXTENDED | REG_ICASE;
for (id = 0; id < rule_num; id++)
if ((err_code = regcomp(&Regex_Compare[id], Regex_Pattern[id], compression_flags)) != 0) {
perror("set regular expression failed:\n");
return -1;
}
return 0;
}
int init() {
initCPUInfo();
initMemInfo();
if (init_regex() == -1) {
return -1;
}
return 0;
}

View File

@ -5,8 +5,6 @@
#ifndef ATOP_INIT_H
#define ATOP_INIT_H
int initCPUInfo();
int initMemInfo();
int init();

View File

@ -1,64 +0,0 @@
#include <stdio.h>
#include <dirent.h> // read dir
#include <regex.h> // regular expression
#include <string.h>
/*
* dirent的属性d_name里存储了app的名字
* c语言没有字符串数组app名dirent*
* app_list[index]->d_name就可以获取文件名
*/
int main() {
// variable declaration for reading directory
DIR* dir = NULL;
int app_max = 50; // c doesn't have dynamic array like "std::vector" in c++, so I set a large size for array
int app_index = 0;
struct dirent* read_result;
struct dirent* app_list[app_max];
const char* app_path = "/home/wwd/CLionProjects/AndroidTest/data/data"; // test, should be /data/data in android
// variable declaration for regular expression
regex_t reg;
int err_code;
int compression_flags = REG_EXTENDED | REG_ICASE;
char err_buff[256];
char* reg_str = "com.android.*";
// set regular expression and handle error
if ((err_code = regcomp(&reg, reg_str, compression_flags)) != 0){
printf("set regular expression failed:\n");
regerror(err_code, &reg, err_buff, sizeof(err_buff));
fprintf(stderr, "%s\n", err_buff);
return -1;
}
// read dir
if ((dir = opendir(app_path)) == NULL){
printf("open dir failed\n");
return -1;
}
// get result using loop
while ((read_result = readdir(dir)) != NULL){
// filter, ignore current dir + parent dir + com.android.* app
if (strcmp(read_result->d_name, ".") == 0
|| strcmp(read_result->d_name, "..") == 0
|| (err_code = regexec(&reg, read_result->d_name, 0, NULL, 0)) == 0)
continue;
// d_type = 4 means it's a directory
if (read_result->d_type == 4){
app_list[app_index] = read_result;
app_index++;
}
}
// print apps' names
printf("we have %d apps\n", app_index);
for (int i = 0; i < app_index; i++){
printf("app %d : %s\n", i, app_list[i]->d_name);
}
regfree(&reg);
return 0;
}