]> git.ipfire.org Git - thirdparty/collectd.git/commitdiff
fix number of running processes
authorBart De Vos <bart-de-vos@telenet.be>
Wed, 7 Aug 2019 14:29:46 +0000 (16:29 +0200)
committerBart De Vos <bart-de-vos@telenet.be>
Thu, 8 Aug 2019 07:59:00 +0000 (09:59 +0200)
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.

src/processes.c

index f83913afb9ca77cd97d3063127768a2a0c2e0da1..f86cd39b1a7bdd71a2ec1067831143dea086dd2d 100644 (file)
@@ -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);