System CPU and Mem is OK
This commit is contained in:
+8
-8
@@ -5,7 +5,7 @@
|
||||
#include "ProcessInfo.h"
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include<dirent.h>
|
||||
#include <dirent.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
extern int Proc_List_CURRENT;
|
||||
extern Proc_Info Proc_List[LIMIT_PROCESS_NUMBER];
|
||||
|
||||
inline void freeProc_Info(Proc_Info *proc) {
|
||||
inline void freeProcInfo(Proc_Info *proc) {
|
||||
int i;
|
||||
proc->pid = 0;
|
||||
proc->point = 0;
|
||||
@@ -33,7 +33,7 @@ inline void freeProc_Info(Proc_Info *proc) {
|
||||
return;
|
||||
}
|
||||
|
||||
inline int foundProcNode(Proc_Info *pList, unsigned int pid) {
|
||||
int foundProcNode(Proc_Info *pList, unsigned int pid) {
|
||||
int i;
|
||||
for (i = 0; i < LIMIT_PROCESS_NUMBER; i++) {
|
||||
if ((pList + i)->pid == pid) {
|
||||
@@ -45,8 +45,8 @@ inline int foundProcNode(Proc_Info *pList, unsigned int pid) {
|
||||
|
||||
int foundFreeProcNode(Proc_Info *pList) {
|
||||
int i;
|
||||
i=foundProcNode(pList, 0);
|
||||
if (i==-1) {
|
||||
i = foundProcNode(pList, 0);
|
||||
if (i == -1) {
|
||||
perror("No free procInfo node in procList");
|
||||
}
|
||||
return i;
|
||||
@@ -144,9 +144,9 @@ 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 %u %u %*s %*s %*s %*s %*s %*s %*s %*s %u %u ",
|
||||
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 %d %d %d %d\n", proc->pid, proc->name, utime, stime, vsz, rss);
|
||||
printf("%d %s %ld %ld %ld %ld\n", proc->pid, proc->name, utime, stime, vsz, rss);
|
||||
proc->point++;
|
||||
proc->point %= UPDATE_FREQUENCY;
|
||||
proc->cpuInfo[proc->point].utime = utime;
|
||||
@@ -162,7 +162,7 @@ void updateProcList() {
|
||||
Proc_Info *proc;
|
||||
char file_path[64] = {0};
|
||||
while (1) {
|
||||
if (Proc_List_CURRENT == MONITOR_PROCESS_NUMBER) {
|
||||
if (Proc_List_CURRENT == LIMIT_PROCESS_NUMBER) {
|
||||
Proc_List_CURRENT = 0;
|
||||
usleep(60000 / UPDATE_FREQUENCY);
|
||||
}
|
||||
|
||||
+2
-2
@@ -28,7 +28,7 @@ typedef struct Proc_Info_ {
|
||||
Proc_Mem memInfo[UPDATE_FREQUENCY];
|
||||
} Proc_Info;
|
||||
|
||||
void freeProc_Info(Proc_Info *proc);
|
||||
void freeProcInfo(Proc_Info *proc);
|
||||
|
||||
//kill this process
|
||||
void findProcListByMem();
|
||||
@@ -37,6 +37,6 @@ void findProcListByMem();
|
||||
void findProcListByCPU();
|
||||
|
||||
int Proc_List_CURRENT;
|
||||
Proc_Info Proc_List[MONITOR_PROCESS_NUMBER]; //max monitor 64 process
|
||||
Proc_Info Proc_List[LIMIT_PROCESS_NUMBER]; //max monitor 64 process
|
||||
|
||||
#endif //ATOP_PROCESSINFO_H
|
||||
|
||||
+59
-8
@@ -3,37 +3,88 @@
|
||||
//
|
||||
|
||||
#include "SystemInfo.h"
|
||||
#include<stdio.h>
|
||||
#include "ProcessInfo.h"
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define DEBUG
|
||||
#define BUFFER_SIZE_STATINFO 256
|
||||
#define BUFFER_SIZE_MEMINFO 128
|
||||
#define SYSTEM_CPU_INFO_PATH "/proc/stat"
|
||||
#define SYSTEM_MEM_INFO_PATH "/proc/meminfo"
|
||||
|
||||
extern Sys_CPU_Time Sys_CPU_Time_Info;
|
||||
extern Sys_Mem Sys_Mem_Info;
|
||||
|
||||
void updateSystemCPU(Sys_CPU_Time *cpuInfo) {
|
||||
char buf[BUFFER_SIZE_MEMINFO]; /*缓冲区*/
|
||||
char buf[BUFFER_SIZE_STATINFO]; /*缓冲区*/
|
||||
FILE *fp; /*文件指针*/
|
||||
if ((fp = fopen(SYSTEM_CPU_INFO_PATH, "r")) == NULL) {
|
||||
perror("fail to read /proc/meminfo");
|
||||
perror("fail to read /proc/stat");
|
||||
return;
|
||||
}
|
||||
fgets(buf, BUFFER_SIZE_MEMINFO, fp);
|
||||
sscanf(buf, "cpu: %u kB", cpuInfo->user, cpuInfo->nice, cpuInfo->system, cpuInfo->idle);
|
||||
*useage = 1 - (float) *fre / *all;
|
||||
fgets(buf, BUFFER_SIZE_STATINFO, fp);
|
||||
#if defined(DEBUG)
|
||||
printf("/proc/stat:%s", buf);
|
||||
#endif
|
||||
unsigned long cpu_user, cpu_nice, cpu_sys, cpu_idle;
|
||||
sscanf(buf, "cpu %ld %ld %ld %ld", &cpu_user, &cpu_nice, &cpu_sys, &cpu_idle);
|
||||
if (cpuInfo->Total_Cpu_Time.user == cpu_user && cpuInfo->Total_Cpu_Time.idle == cpu_idle) {
|
||||
return;
|
||||
}
|
||||
|
||||
cpuInfo->Unit_Cpu_Time.user = cpu_user - cpuInfo->Total_Cpu_Time.user;
|
||||
cpuInfo->Unit_Cpu_Time.nice = cpu_nice - cpuInfo->Total_Cpu_Time.nice;
|
||||
cpuInfo->Unit_Cpu_Time.system = cpu_sys - cpuInfo->Total_Cpu_Time.system;
|
||||
cpuInfo->Unit_Cpu_Time.idle = cpu_idle - cpuInfo->Total_Cpu_Time.idle;
|
||||
cpuInfo->Total_Cpu_Time.user = cpu_user;
|
||||
cpuInfo->Total_Cpu_Time.nice = cpu_nice;
|
||||
cpuInfo->Total_Cpu_Time.system = cpu_sys;
|
||||
cpuInfo->Total_Cpu_Time.idle = cpu_idle;
|
||||
int total_cpu_time;
|
||||
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)
|
||||
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
|
||||
|
||||
fclose(fp);
|
||||
return;
|
||||
}
|
||||
|
||||
void updateSystemMem(Sys_Mem *memInfo) {
|
||||
char buf[BUFFER_SIZE_MEMINFO]; /*缓冲区*/
|
||||
FILE *fp; /*文件指针*/
|
||||
|
||||
if ((fp = fopen(SYSTEM_MEM_INFO_PATH, "r")) == NULL) {
|
||||
perror("fail to read /proc/meminfo");
|
||||
return;
|
||||
}
|
||||
|
||||
fgets(buf, BUFFER_SIZE_MEMINFO, fp);
|
||||
sscanf(buf, "MemTotal: %u kB", memInfo->All);
|
||||
sscanf(buf, "MemTotal: %lu kB", &memInfo->All);
|
||||
#if defined(DEBUG)
|
||||
printf("/proc/stat:%s", buf);
|
||||
#endif
|
||||
|
||||
fgets(buf, BUFFER_SIZE_MEMINFO, fp);
|
||||
sscanf(buf, "MemFree %u kB", memInfo->Free);
|
||||
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);
|
||||
#endif
|
||||
|
||||
|
||||
fclose(fp);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void updateSystemInfo() {
|
||||
updateSystemCPU(&Sys_CPU_Time_Info);
|
||||
updateSystemMem(&Sys_Mem_Info);
|
||||
}
|
||||
|
||||
+13
-4
@@ -5,18 +5,27 @@
|
||||
#ifndef ATOP_SYSTEMINFO_H
|
||||
#define ATOP_SYSTEMINFO_H
|
||||
|
||||
typedef struct Sys_CPU_Time_ {
|
||||
float Usage;
|
||||
typedef struct CPU_Time_ {
|
||||
unsigned long user;
|
||||
unsigned long nice;
|
||||
unsigned long system;
|
||||
unsigned long idle;
|
||||
} CPU_Time;
|
||||
|
||||
typedef struct Sys_CPU_Time_ {
|
||||
float Usage;
|
||||
CPU_Time Unit_Cpu_Time;
|
||||
CPU_Time Total_Cpu_Time;
|
||||
} Sys_CPU_Time;
|
||||
|
||||
typedef struct Sys_Mem_ {
|
||||
float Usage;
|
||||
unsigned int All;
|
||||
unsigned int Free;
|
||||
unsigned long All;
|
||||
unsigned long Free;
|
||||
} Sys_Mem;
|
||||
|
||||
void updateSystemInfo();
|
||||
|
||||
Sys_CPU_Time Sys_CPU_Time_Info;
|
||||
Sys_Mem Sys_Mem_Info;
|
||||
#endif //ATOP_SYSTEMINFO_H
|
||||
|
||||
+72
-72
@@ -17,76 +17,76 @@
|
||||
#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(®, buf, 0, NULL, 0)
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
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() {
|
||||
if (init_regex() == -1) {
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
//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(®, buf, 0, NULL, 0)
|
||||
// return 0;
|
||||
//}
|
||||
//
|
||||
//
|
||||
//int filterByWhiteList(char *buf) {
|
||||
// return 0;
|
||||
//}
|
||||
//
|
||||
//int getAppName() {
|
||||
// DIR *dir = NULL;
|
||||
// int app_index = 0;
|
||||
// struct dirent *read_result;
|
||||
// struct dirent *app_list[LIMIT_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() {
|
||||
// if (init_regex() == -1) {
|
||||
// return -1;
|
||||
// }
|
||||
// return 0;
|
||||
//}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user