From 053d49a231b863e8c0fafda28427fbe6a40d0ad2 Mon Sep 17 00:00:00 2001 From: "K. Lange" Date: Fri, 14 Sep 2018 21:11:26 +0900 Subject: [PATCH] Also calculate rough percentage As this includes shm, it overestimates and values may add up to more than 100% --- apps/ps.c | 19 ++++++++++++++----- modules/procfs.c | 4 +++- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/apps/ps.c b/apps/ps.c index 50a51183..f07acc72 100644 --- a/apps/ps.c +++ b/apps/ps.c @@ -29,13 +29,14 @@ static int show_username = 0; static int show_mem = 0; static int collect_commandline = 0; -static int widths[5] = {3,3,4,3,3}; +static int widths[] = {3,3,4,3,3,4}; struct process { int uid; int pid; int tid; int mem; + int vsz; int shm; char * process; char * command_line; @@ -58,7 +59,7 @@ struct process * process_entry(struct dirent *dent) { FILE * f; char line[LINE_LEN]; - int pid = 0, uid = 0, tgid = 0, mem = 0, shm = 0; + int pid = 0, uid = 0, tgid = 0, mem = 0, shm = 0, vsz = 0; char name[100]; sprintf(tmp, "/proc/%s/status", dent->d_name); @@ -87,9 +88,11 @@ struct process * process_entry(struct dirent *dent) { } else if (strstr(line, "Name:") == line) { strcpy(name, tab); } else if (strstr(line, "VmSize:") == line) { - mem = atoi(tab); + vsz = atoi(tab); } else if (strstr(line, "RssShmem:") == line) { shm = atoi(tab); + } else if (strstr(line, "MemPermille:") == line) { + mem = atoi(tab); } } @@ -110,6 +113,7 @@ struct process * process_entry(struct dirent *dent) { out->tid = pid; out->mem = mem; out->shm = shm; + out->vsz = vsz; out->process = strdup(name); out->command_line = NULL; @@ -118,8 +122,9 @@ struct process * process_entry(struct dirent *dent) { if ((len = sprintf(garbage, "%d", out->pid)) > widths[0]) widths[0] = len; if ((len = sprintf(garbage, "%d", out->tid)) > widths[1]) widths[1] = len; - if ((len = sprintf(garbage, "%d", out->mem)) > widths[3]) widths[3] = len; + if ((len = sprintf(garbage, "%d", out->vsz)) > widths[3]) widths[3] = len; if ((len = sprintf(garbage, "%d", out->shm)) > widths[4]) widths[4] = len; + if ((len = sprintf(garbage, "%d.%01d", out->mem / 10, out->mem % 10)) > widths[5]) widths[5] = len; struct passwd * p = getpwuid(out->uid); if (p) { @@ -161,6 +166,7 @@ void print_header(void) { printf("%*s ", widths[1], "TID"); } if (show_mem) { + printf("%*s ", widths[5], "MEM%"); printf("%*s ", widths[3], "VSZ"); printf("%*s ", widths[4], "SHM"); } @@ -182,7 +188,10 @@ void print_entry(struct process * out) { printf("%*d ", widths[1], out->tid); } if (show_mem) { - printf("%*d ", widths[3], out->mem); + char tmp[6]; + sprintf(tmp, "%*d.%01d", widths[5]-2, out->mem / 10, out->mem % 10); + printf("%*s ", widths[5], tmp); + printf("%*d ", widths[3], out->vsz); printf("%*d ", widths[4], out->shm); } if (out->command_line) { diff --git a/modules/procfs.c b/modules/procfs.c index 826c88f5..7d91ea8e 100644 --- a/modules/procfs.c +++ b/modules/procfs.c @@ -159,6 +159,7 @@ static uint32_t proc_status_func(fs_node_t *node, uint32_t offset, uint32_t size /* Calculate process memory usage */ int mem_usage = calculate_memory_usage(proc->thread.page_directory) * 4; int shm_usage = calculate_shm_resident(proc->thread.page_directory) * 4; + int mem_permille = 1000 * (mem_usage + shm_usage) / memory_total(); sprintf(buf, "Name:\t%s\n" /* name */ @@ -177,6 +178,7 @@ static uint32_t proc_status_func(fs_node_t *node, uint32_t offset, uint32_t size "Path:\t%s\n" "VmSize:\t %d kB\n" "RssShmem:\t %d kB\n" + "MemPermille:\t %d\n" , name, state, @@ -192,7 +194,7 @@ static uint32_t proc_status_func(fs_node_t *node, uint32_t offset, uint32_t size proc->syscall_registers ? proc->syscall_registers->esi : 0, proc->syscall_registers ? proc->syscall_registers->edi : 0, proc->cmdline ? proc->cmdline[0] : "(none)", - mem_usage, shm_usage + mem_usage, shm_usage, mem_permille ); size_t _bsize = strlen(buf);