From: Bart De Vos Date: Wed, 7 Aug 2019 14:29:46 +0000 (+0200) Subject: fix number of running processes X-Git-Tag: collectd-5.11.0~8^2~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9114d76499ba00d29641d8e49baccae89613abcd;p=thirdparty%2Fcollectd.git fix number of running processes scanning /proc/*/stat AND computing other process stats takes too much time. Consequently, the number of running processes based on the occurences of 'R' as character indicating the running state is typically zero. Due to processes are actually changing state during the evaluation of it's stat(s). The 'procs_running' number in /proc/stat on the other hand is more accurate, and can be retrieved in one single 'read' call. In the unlikely case that procs_running can not be retrieved from /proc/stat, (read interrupted prior to reading up to 'procs_running' the value '1' assumed, since this plugin is at least being executed. --- diff --git a/src/processes.c b/src/processes.c index f83913afb..f86cd39b1 100644 --- a/src/processes.c +++ b/src/processes.c @@ -1429,6 +1429,29 @@ static int ps_read_process(long pid, process_entry_t *ps, char *state) { return 0; } /* int ps_read_process (...) */ +static int procs_running(void) { + char buffer[4096] = {}; + + char *running; + + ssize_t status; + + status = read_file_contents("/proc/stat", buffer, sizeof(buffer) - 1); + if (status <= 0) { + return -1; + } + + running = strstr(buffer, "procs_running"); + if (!running) { + WARNING("procs_running not found, assuming 1"); + return 1; + } + + running += 14; + + return atoi(running); +} + static char *ps_get_cmdline(long pid, char *name, char *buf, size_t buf_len) { char *buf_ptr; size_t len; @@ -2084,6 +2107,8 @@ static int ps_read(void) { closedir(proc); + running = procs_running(); + ps_submit_state("running", running); ps_submit_state("sleeping", sleeping); ps_submit_state("zombies", zombies);