From 54baf62b58d71b79a5c2c103cf729ffb56a6a1cf Mon Sep 17 00:00:00 2001 From: netblue30 Date: Fri, 29 Apr 2022 09:31:04 -0400 Subject: fix firemon, speed-up --- src/firemon/netstats.c | 4 ++-- src/firemon/top.c | 4 ++-- src/lib/pid.c | 28 ++++++++++++++++++---------- 3 files changed, 22 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/firemon/netstats.c b/src/firemon/netstats.c index 0a1b7e0c4..7d86ca45e 100644 --- a/src/firemon/netstats.c +++ b/src/firemon/netstats.c @@ -47,7 +47,7 @@ static char *get_user_name(uid_t uid) { static char *get_header(void) { char *rv; - if (asprintf(&rv, "%-5.5s %-9.9s %-10.10s %-10.10s %s", + if (asprintf(&rv, "%-7.7s %-9.9s %-10.10s %-10.10s %s", "PID", "User", "RX(KB/s)", "TX(KB/s)", "Command") == -1) errExit("asprintf"); @@ -183,7 +183,7 @@ static void print_proc(int index, int itv, int col) { sprintf(ptrtx, "%.03f", tx_kbps); char buf[1024 + 1]; - snprintf(buf, 1024, "%-5.5s %-9.9s %-10.10s %-10.10s %s", + snprintf(buf, 1024, "%-7.7s %-9.9s %-10.10s %-10.10s %s", pidstr, ptruser, ptrrx, ptrtx, ptrcmd); if (col < 1024) buf[col] = '\0'; diff --git a/src/firemon/top.c b/src/firemon/top.c index 2bfa63380..0633370ac 100644 --- a/src/firemon/top.c +++ b/src/firemon/top.c @@ -47,7 +47,7 @@ static char *get_user_name(uid_t uid) { static char *get_header(void) { char *rv; - if (asprintf(&rv, "%-5.5s %-9.9s %-8.8s %-8.8s %-5.5s %-4.4s %-9.9s %s", + if (asprintf(&rv, "%-7.7s %-9.9s %-8.8s %-8.8s %-5.5s %-4.4s %-9.9s %s", "PID", "User", "RES(KiB)", "SHR(KiB)", "CPU%", "Prcs", "Uptime", "Command") == -1) errExit("asprintf"); @@ -165,7 +165,7 @@ static char *print_top(unsigned index, unsigned parent, unsigned *utime, unsigne char prcs_str[10]; snprintf(prcs_str, 10, "%d", *cnt); - if (asprintf(&rv, "%-5.5s %-9.9s %-8.8s %-8.8s %-5.5s %-4.4s %-9.9s %s", + if (asprintf(&rv, "%-7.7s %-9.9s %-8.8s %-8.8s %-5.5s %-4.4s %-9.9s %s", pidstr, ptruser, rss, shared, cpu_str, prcs_str, uptime_str, ptrcmd) == -1) errExit("asprintf"); diff --git a/src/lib/pid.c b/src/lib/pid.c index ad6403f65..3cf0df909 100644 --- a/src/lib/pid.c +++ b/src/lib/pid.c @@ -30,7 +30,7 @@ #define PIDS_BUFLEN 4096 //Process pids[max_pids]; Process *pids = NULL; -int max_pids=32769; +int max_pids=32769; // recalculated for every read_pid() call // get the memory associated with this pid void pid_getmem(unsigned pid, unsigned *rss, unsigned *shared) { @@ -303,20 +303,22 @@ void pid_store_cpu(unsigned index, unsigned parent, unsigned *utime, unsigned *s // mon_pid: pid of sandbox to be monitored, 0 if all sandboxes are included void pid_read(pid_t mon_pid) { - if (pids == NULL) { - FILE *fp = fopen("/proc/sys/kernel/pid_max", "r"); - if (fp) { - int val; - if (fscanf(fp, "%d", &val) == 1) { - if (val >= max_pids) - max_pids = val + 1; - } - fclose(fp); + FILE *fp = fopen("/proc/sys/kernel/pid_max", "r"); + if (fp) { + int val; + if (fscanf(fp, "%d", &val) == 1) { + if (val >= max_pids) + max_pids = val + 1; } + fclose(fp); + } + + if (pids == NULL) { pids = malloc(sizeof(Process) * max_pids); if (pids == NULL) errExit("malloc"); } + memset(pids, 0, sizeof(Process) * max_pids); pid_t mypid = getpid(); @@ -332,9 +334,12 @@ void pid_read(pid_t mon_pid) { struct dirent *entry; char *end; + pid_t new_max_pids = 0; while ((entry = readdir(dir))) { pid_t pid = strtol(entry->d_name, &end, 10); pid %= max_pids; + if (pid > new_max_pids) + new_max_pids = pid; if (end == entry->d_name || *end) continue; if (pid == mypid) @@ -418,6 +423,9 @@ void pid_read(pid_t mon_pid) { } closedir(dir); + // update max_pid + max_pids = new_max_pids; + pid_t pid; for (pid = 0; pid < max_pids; pid++) { int parent = pids[pid].parent; -- cgit v1.2.3-70-g09d2