From b97fb4cfdf3d33ed26b78d5d30c4284ae26e8d74 Mon Sep 17 00:00:00 2001 From: Kondo Takeo Date: Wed, 17 Feb 2021 23:02:27 +0900 Subject: [PATCH] Issue: #96 - Garbage collect inodeproc once every 50 ui refresh as default. - Reduce default frequency to avoid performance issue. - The frequency of it can be modified with `-g` option and can be disabled with `-g 0`. --- doc/nethogs.8 | 4 ++++ src/inode2prog.cpp | 3 ++- src/main.cpp | 16 ++++++++++++++-- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/doc/nethogs.8 b/doc/nethogs.8 index efa5f1e..7c987b7 100644 --- a/doc/nethogs.8 +++ b/doc/nethogs.8 @@ -19,6 +19,7 @@ nethogs \- Net top tool grouping bandwidth per process .RB [ "\-l" ] .RB [ "\-f filter" ] .RB [ "\-C" ] +.RB [ "\-g period" ] .RI [device(s)] .SH DESCRIPTION NetHogs is a small 'net top' tool. Instead of breaking the traffic down per protocol or per subnet, like most such tools do, it groups bandwidth by process - and does not rely on a special kernel module to be loaded. So if there's suddenly a lot of network traffic, you can fire up NetHogs and immediately see which PID is causing this, and if it's some kind of spinning process, kill it. @@ -61,6 +62,9 @@ monitor all devices, even loopback/stopped ones. \fB-C\fP capture TCP and UDP. .TP +\fB-g\fP +garbage collection period in number of refresh. default is 50. +.TP \fB-f\fP EXPERIMENTAL: specify string pcap filter (like tcpdump). This may be removed or changed in a future version. .TP diff --git a/src/inode2prog.cpp b/src/inode2prog.cpp index 17f59b4..a370b46 100644 --- a/src/inode2prog.cpp +++ b/src/inode2prog.cpp @@ -235,7 +235,8 @@ static quad_t get_ms() { static void get_pids(std::set *pids) { DIR *proc = opendir("/proc"); if (proc == 0) { - std::cerr << "Error reading /proc, needed to get inode-to-pid-mapping" << std::endl; + std::cerr << "Error reading /proc, needed to get inode-to-pid-mapping" + << std::endl; exit(1); } dirent *entry; diff --git a/src/main.cpp b/src/main.cpp index d32b8d8..52964ce 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -46,6 +46,8 @@ static void help(bool iserror) { output << " -a : monitor all devices, even loopback/stopped " "ones.\n"; output << " -C : capture TCP and UDP.\n"; + output << " -g : garbage collection period in number of refresh. " + "default is 50.\n"; output << " -f : EXPERIMENTAL: specify string pcap filter (like " "tcpdump)." " This may be removed or changed in a future version.\n"; @@ -140,9 +142,10 @@ int main(int argc, char **argv) { int promisc = 0; bool all = false; char *filter = NULL; + int garbage_collection_period = 50; int opt; - while ((opt = getopt(argc, argv, "Vhbtpsd:v:c:laf:C")) != -1) { + while ((opt = getopt(argc, argv, "Vhbtpsd:v:c:laf:Cg:")) != -1) { switch (opt) { case 'V': versiondisplay(); @@ -184,6 +187,9 @@ int main(int argc, char **argv) { case 'C': catchall = true; break; + case 'g': + garbage_collection_period = (time_t)atoi(optarg); + break; default: help(true); exit(EXIT_FAILURE); @@ -294,6 +300,7 @@ int main(int argc, char **argv) { struct dpargs *userdata = (dpargs *)malloc(sizeof(struct dpargs)); // Main loop: + int refresh_count = 0; while (1) { bool packets_read = false; @@ -322,7 +329,12 @@ int main(int argc, char **argv) { ui_tick(); } do_refresh(); - garbage_collect_processes(); + ++refresh_count; + + if ((garbage_collection_period > 0) && + (refresh_count % garbage_collection_period == 0)) { + garbage_collect_processes(); + } } // if not packets, do a select() until next packet