From 97dd5a169bce2d51b5182de5c7029218b04fcd32 Mon Sep 17 00:00:00 2001 From: xcjusuih <71764219+xcjusuih@users.noreply.github.com> Date: Fri, 28 May 2021 18:22:27 +0800 Subject: [PATCH 1/2] Update process.cpp --- src/process.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/process.cpp b/src/process.cpp index 81ae448..d52dcca 100644 --- a/src/process.cpp +++ b/src/process.cpp @@ -50,11 +50,19 @@ proc_vector* get_all_processes() { sprintf(statm_file_name, "/proc/%s/statm", entry->d_name); FILE* stat_fp = fopen(stat_file_name, "r"); + if (!stat_fp) { + delete proc; + continue; + } fscanf(stat_fp, "%d %s %c", &proc->pid, proc->pname, &proc->state); fclose(stat_fp); // TODO, Here just read the memory usage in page. FILE* statm_fp = fopen(statm_file_name, "r"); + if (!statm_fp) { + delete proc; + continue; + } fscanf(statm_fp, "%lu %lu %lu %lu %*u %lu", &proc->memory_size, &proc->rss_size, @@ -105,10 +113,18 @@ thrd_vector* get_all_threads(int pid) { sprintf(statm_file_name, "%s/%s/statm", dir_name, entry->d_name); stat_fp = fopen(stat_file_name, "r"); + if (!stat_fp) { + delete thrd; + continue; + } fscanf(stat_fp, "%d %*s %c", &thrd->tid, &thrd->state); fclose(stat_fp); statm_fp = fopen(statm_file_name, "r"); + if (!statm_fp) { + delete thrd; + continue; + } fscanf(statm_fp, "%lu %lu %lu %lu %*u %lu", &thrd->memory_size, &thrd->rss_size, From 93db5fe1cfbdb3755b952c608d34cb5a12a702ef Mon Sep 17 00:00:00 2001 From: xcjusuih <71764219+xcjusuih@users.noreply.github.com> Date: Fri, 28 May 2021 19:21:12 +0800 Subject: [PATCH 2/2] All commit --- inc/process.hpp | 1 + src/main.cpp | 81 +++++++++++++++++++++++++++++++-------- src/process.cpp | 100 ++++++++++++++++++++++++++++++++++++------------ 3 files changed, 140 insertions(+), 42 deletions(-) diff --git a/inc/process.hpp b/inc/process.hpp index 9496056..3aacdc4 100644 --- a/inc/process.hpp +++ b/inc/process.hpp @@ -60,5 +60,6 @@ proc_vector* get_all_processes(); thrd_vector* get_all_threads(int pid); void delete_process_vec(proc_vector*); memory* get_memory_info(); +void sort_mem_vector(proc_vector* vec, int sort_method); #endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index dedc198..feb5827 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,22 +5,37 @@ #include #define ESCAPE 27 +#define ENTER 10 proc_vector* vec; -int begin = 0; +int begin = 0, sort_method = 0, choose = 0; clock_t starttime; memory* mem; void show_win() { - mvprintw(0,0,"KB Mem: %u total, %u free, %u available, %u buffer, %u cached\n", + attroff(A_REVERSE); + mvprintw(0, 0, "KB Mem: %u total, %u free, %u available, %u buffer, %u cached\n", mem->memTotal, mem->memFree, mem->MemAvailable, mem->Buffers, mem->Cached); - attron(A_REVERSE); + // attron(A_REVERSE); mvprintw(1, 0, "PID\tSTATUS\tMEMORY\tRSS\tSHARED\tTEXT\tDATA\tNAME\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\n"); + attron(A_REVERSE); + switch (choose) { + case 0: + mvprintw(1, 0, "PID"); + break; + case 1: + mvprintw(1, 16, "MEMORY"); + break; + case 2: + mvprintw(1, 56, "NAME"); + break; + } attroff(A_REVERSE); + // attroff(A_REVERSE); int maxy, maxx; getmaxyx(stdscr, maxy, maxx); if (begin + maxy - 1 >= vec->size()) { @@ -31,25 +46,38 @@ void show_win() { { process* it = vec->at((i + begin) % vec->size()); - mvprintw(cnt++, 0, "%u\t%c\t%d\t%d\t%d\t%d\t%d\t%s", + + mvprintw(cnt++, 0, "%u\t%c\t%d\t%d\t%d\t%d\t%d\t%s\n", it->pid, it->state, it->memory_size, it->rss_size, it->shared_size, it->text_size, it->data_size, it->pname); + if (it->thread_vector->size() != 1 + || it->thread_vector->at(0)->tid != it->pid && cnt < maxy - 1) { + thrd_vector* thread_vector = it->thread_vector; + mvprintw(cnt - 1, 90, "-> with %d sub threads.\n", thread_vector->size()); + + } + + if (cnt >= maxy - 1) { + break; + } } + attron(A_REVERSE); + mvprintw(maxy - 1, 0, "p: exit\tup/down: scroll the information\t\tleft/right+Enter: sort method"); if ((clock() - starttime) / CLOCKS_PER_SEC >= 1) { - for (proc_vector_iter it = vec->begin(); it != vec->end(); ++it) { - delete((*it)); - } - delete(vec); + delete_process_vec(vec); + delete mem; vec = get_all_processes(); + sort_mem_vector(vec, sort_method); starttime = clock(); - // mvprintw(70, 0, "%d", starttime); - mem=get_memory_info(); + mem = get_memory_info(); } + refresh(); } + int main() { initscr(); noecho(); @@ -63,17 +91,36 @@ int main() { { show_win(); int key = getch(); - if (key == KEY_DOWN) { + if (key == KEY_DOWN) begin++; - } - if (key == KEY_UP) { + + if (key == KEY_UP && begin>0) { begin--; - if (begin < 0) - begin = 0; } - mvprintw(0, 50, "%d", vec->size()); + if (key == ESCAPE || key == 'q') + break; - } + if (key == KEY_LEFT && choose > 0) { + choose=(choose-1)%3; + } + if (key == KEY_RIGHT && choose < 2) { + choose=(choose+1)%3; + } + if (key == ENTER) { + if (sort_method / 2 != choose) + sort_method = choose * 2; + else { + if (sort_method % 2 == 0) { + sort_method++; + } + else { + sort_method--; + } + } + } + } + delete_process_vec(vec); + delete mem; endwin(); } diff --git a/src/process.cpp b/src/process.cpp index d52dcca..f19ee7f 100644 --- a/src/process.cpp +++ b/src/process.cpp @@ -4,6 +4,8 @@ #include #include #include +#include +#include using namespace std; @@ -23,7 +25,7 @@ bool is_number_folder(dirent* entry) { /** * @brief Get the all processes object - * + * * @return proc_vector* , a vector which contains memory usage info a process */ proc_vector* get_all_processes() { @@ -36,9 +38,9 @@ proc_vector* get_all_processes() { return NULL; } - char *stat_file_name = new char[255 + 20]; - char *statm_file_name = new char[255 + 20]; - FILE *stat_fp, *statm_fp; + char* stat_file_name = new char[255 + 20]; + char* statm_file_name = new char[255 + 20]; + FILE* stat_fp, * statm_fp; while ((entry = readdir(dp))) { @@ -87,27 +89,27 @@ proc_vector* get_all_processes() { /** * @brief Get all the threads' information about a process * @par pid, the process id of the process you want to find -* @return thrd_vector*, a pointer to a vector which contains information about all the threads. +* @return thrd_vector*, a pointer to a vector which contains information about all the threads. */ thrd_vector* get_all_threads(int pid) { - char *dir_name = new char[255+20]; + char* dir_name = new char[255 + 20]; sprintf(dir_name, "/proc/%d/task", pid); - DIR *dp = opendir(dir_name); - dirent *entry; - thrd_vector *vec = new thrd_vector(); + DIR* dp = opendir(dir_name); + dirent* entry; + thrd_vector* vec = new thrd_vector(); if (!dp) return NULL; - char *stat_file_name = new char[255 + 20]; - char *statm_file_name = new char[255 + 20]; - FILE *stat_fp, *statm_fp; + char* stat_file_name = new char[255 + 20]; + char* statm_file_name = new char[255 + 20]; + FILE* stat_fp, * statm_fp; - while((entry = readdir(dp))) { + while ((entry = readdir(dp))) { if (!is_number_folder(entry)) continue; - thread *thrd = new thread(); + thread* thrd = new thread(); sprintf(stat_file_name, "%s/%s/stat", dir_name, entry->d_name); sprintf(statm_file_name, "%s/%s/statm", dir_name, entry->d_name); @@ -126,15 +128,15 @@ thrd_vector* get_all_threads(int pid) { continue; } fscanf(statm_fp, "%lu %lu %lu %lu %*u %lu", - &thrd->memory_size, - &thrd->rss_size, - &thrd->shared_size, - &thrd->text_size, - &thrd->data_size); + &thrd->memory_size, + &thrd->rss_size, + &thrd->shared_size, + &thrd->text_size, + &thrd->data_size); fclose(statm_fp); vec->push_back(thrd); - + } closedir(dp); @@ -147,15 +149,15 @@ thrd_vector* get_all_threads(int pid) { void delete_process_vec(proc_vector* vec) { for (proc_vector_iter it = vec->begin(); it != vec->end(); it++) { - thrd_vector *thread_vector = (*it)->thread_vector; + thrd_vector* thread_vector = (*it)->thread_vector; if (thread_vector != NULL) { for (thrd_vector_iter thrd_it = thread_vector->begin(); thrd_it != thread_vector->end(); ++thrd_it) { - delete *thrd_it; + delete* thrd_it; } delete (*it)->thread_vector; } - - delete[] (*it)->pname; + + delete[](*it)->pname; delete (*it); } delete vec; @@ -169,8 +171,56 @@ memory* get_memory_info() { char* tmp = new char[256]; memory* mem = new memory(); infile >> tmp >> mem->memTotal >> tmp >> tmp >> mem->memFree - >> tmp >> tmp >> mem->MemAvailable >> tmp >> tmp + >> tmp >> tmp >> mem->MemAvailable >> tmp >> tmp >> mem->Buffers >> tmp >> tmp >> mem->Cached; return mem; +} + +int cmp_pid(process* a, process* b) { + return a->pid < b->pid; +} + +int cmp_pid_inv(process* a, process* b) { + return a->pid > b->pid; +} + +int cmp_mem(process* a, process* b) { + return a->memory_size < b->memory_size; +} + +int cmp_mem_inv(process* a, process* b) { + return a->memory_size > b->memory_size; +} + +int cmp_name(process* a, process* b) { + return strcmp(a->pname, b->pname)<0; +} + +int cmp_name_inv(process* a, process* b) { + return strcmp(a->pname, b->pname)>0; +} + + +void sort_mem_vector(proc_vector* vec, int sort_method) { + switch (sort_method) { + case 0: + sort(vec->begin(), vec->end(), cmp_pid); + break; + case 1: + sort(vec->begin(), vec->end(), cmp_pid_inv); + break; + case 2: + sort(vec->begin(), vec->end(), cmp_mem); + break; + case 3: + sort(vec->begin(), vec->end(), cmp_mem_inv); + break; + case 4: + sort(vec->begin(), vec->end(), cmp_name); + break; + case 5: + sort(vec->begin(), vec->end(), cmp_name_inv); + break; + } } \ No newline at end of file