Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions inc/process.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
81 changes: 64 additions & 17 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,37 @@
#include <time.h>

#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()) {
Expand All @@ -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();
Expand All @@ -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();
}
116 changes: 91 additions & 25 deletions src/process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <cstdlib>
#include <dirent.h>
#include <fstream>
#include <algorithm>
#include <cstring>
using namespace std;


Expand All @@ -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() {
Expand All @@ -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))) {

Expand All @@ -50,11 +52,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,
Expand All @@ -79,46 +89,54 @@ 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);

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,
&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);
Expand All @@ -131,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;
Expand All @@ -153,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;
}
}