Make some virtual memory size info available in procfs

This commit is contained in:
K. Lange 2018-09-14 20:40:05 +09:00
parent 0d2617831b
commit 5fa5d2020d
2 changed files with 45 additions and 3 deletions

View File

@ -26,14 +26,16 @@
static int show_all = 0;
static int show_threads = 0;
static int show_username = 0;
static int show_mem = 0;
static int collect_commandline = 0;
static int widths[5] = {3,3,4,0,0};
static int widths[5] = {3,3,4,3,0};
struct process {
int uid;
int pid;
int tid;
int mem;
char * process;
char * command_line;
};
@ -55,7 +57,7 @@ struct process * process_entry(struct dirent *dent) {
FILE * f;
char line[LINE_LEN];
int pid = 0, uid = 0, tgid = 0;
int pid = 0, uid = 0, tgid = 0, mem = 0;
char name[100];
sprintf(tmp, "/proc/%s/status", dent->d_name);
@ -83,6 +85,8 @@ struct process * process_entry(struct dirent *dent) {
tgid = atoi(tab);
} else if (strstr(line, "Name:") == line) {
strcpy(name, tab);
} else if (strstr(line, "VmSize:") == line) {
mem = atoi(tab);
}
}
@ -101,6 +105,7 @@ struct process * process_entry(struct dirent *dent) {
out->uid = uid;
out->pid = tgid;
out->tid = pid;
out->mem = mem;
out->process = strdup(name);
out->command_line = NULL;
@ -109,6 +114,7 @@ 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;
struct passwd * p = getpwuid(out->uid);
if (p) {
@ -149,6 +155,9 @@ void print_header(void) {
if (show_threads) {
printf("%*s ", widths[1], "TID");
}
if (show_mem) {
printf("%*s ", widths[3], "VSZ");
}
printf("CMD\n");
}
@ -166,6 +175,9 @@ void print_entry(struct process * out) {
if (show_threads) {
printf("%*d ", widths[1], out->tid);
}
if (show_mem) {
printf("%*d ", widths[3], out->mem);
}
if (out->command_line) {
printf("%s\n", out->command_line);
} else {
@ -214,6 +226,7 @@ int main (int argc, char * argv[]) {
switch (*show) {
case 'u':
show_username = 1;
show_mem = 1;
// fallthrough
case 'a':
collect_commandline = 1;

View File

@ -87,6 +87,30 @@ static uint32_t proc_cmdline_func(fs_node_t *node, uint32_t offset, uint32_t siz
return size;
}
static size_t calculate_memory_usage(page_directory_t * src) {
size_t pages = 0;
for (uint32_t i = 0; i < 1024; ++i) {
if (!src->tables[i] || (uintptr_t)src->tables[i] == (uintptr_t)0xFFFFFFFF) {
continue;
}
if (kernel_directory->tables[i] == src->tables[i]) {
continue;
}
/* For each table */
if (i * 0x1000 * 1024 < SHM_START) {
/* Ignore shared memory for now */
for (int j = 0; j < 1024; ++j) {
/* For each frame in the table... */
if (!src->tables[i]->pages[j].frame) {
continue;
}
pages++;
}
}
}
return pages;
}
static uint32_t proc_status_func(fs_node_t *node, uint32_t offset, uint32_t size, uint8_t *buffer) {
char buf[2048];
process_t * proc = process_from_pid(node->inode);
@ -109,6 +133,9 @@ static uint32_t proc_status_func(fs_node_t *node, uint32_t offset, uint32_t size
name--;
}
/* Calculate process memory usage */
int mem_usage = calculate_memory_usage(proc->thread.page_directory) * 4;
sprintf(buf,
"Name:\t%s\n" /* name */
"State:\t%c\n" /* yeah, do this at some point */
@ -124,6 +151,7 @@ static uint32_t proc_status_func(fs_node_t *node, uint32_t offset, uint32_t size
"SC3:\t0x%x\n"
"SC4:\t0x%x\n"
"Path:\t%s\n"
"VmSize:\t %d kB\n"
,
name,
state,
@ -138,7 +166,8 @@ static uint32_t proc_status_func(fs_node_t *node, uint32_t offset, uint32_t size
proc->syscall_registers ? proc->syscall_registers->edx : 0,
proc->syscall_registers ? proc->syscall_registers->esi : 0,
proc->syscall_registers ? proc->syscall_registers->edi : 0,
proc->cmdline ? proc->cmdline[0] : "(none)"
proc->cmdline ? proc->cmdline[0] : "(none)",
mem_usage
);
size_t _bsize = strlen(buf);