processes plugin: Read number of threads from /proc/$pid/stat as a fallback.
[collectd.git] / src / processes.c
index de776a2..176cdf9 100644 (file)
@@ -778,33 +778,6 @@ static void ps_submit_fork_rate (derive_t value)
 
 /* ------- additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
 #if KERNEL_LINUX
-static int ps_read_tasks (int pid)
-{
-       char           dirname[64];
-       DIR           *dh;
-       struct dirent *ent;
-       int count = 0;
-
-       ssnprintf (dirname, sizeof (dirname), "/proc/%i/task", pid);
-
-       if ((dh = opendir (dirname)) == NULL)
-       {
-               DEBUG ("Failed to open directory `%s'", dirname);
-               return (-1);
-       }
-
-       while ((ent = readdir (dh)) != NULL)
-       {
-               if (!isdigit ((int) ent->d_name[0]))
-                       continue;
-               else
-                       count++;
-       }
-       closedir (dh);
-
-       return ((count >= 1) ? count : 1);
-} /* int *ps_read_tasks */
-
 /* Read data from /proc/pid/status */
 static procstat_t *ps_read_status (int pid, procstat_t *ps)
 {
@@ -870,7 +843,8 @@ static procstat_t *ps_read_status (int pid, procstat_t *ps)
 
        ps->vmem_data = data * 1024;
        ps->vmem_code = (exe + lib) * 1024;
-       ps->num_lwp = threads;
+       if (threads != 0)
+               ps->num_lwp = threads;
 
        return (ps);
 } /* procstat_t *ps_read_vmem */
@@ -1013,22 +987,16 @@ int ps_read_process (int pid, procstat_t *ps, char *state)
        }
        else
        {
-               if ( (ps_read_status(pid, ps)) == NULL)
+               ps->num_lwp = strtoul (fields[17], /* endptr = */ NULL, /* base = */ 10);
+               if ((ps_read_status(pid, ps)) == NULL)
                {
                        /* No VMem data */
                        ps->vmem_data = -1;
                        ps->vmem_code = -1;
-                       ps->num_lwp  = 0;
                        DEBUG("ps_read_process: did not get vmem data for pid %i",pid);
                }
-               if ( ps->num_lwp <= 0)
-               {
-                       if ( (ps->num_lwp = ps_read_tasks (pid)) == -1 )
-                       {
-                               /* returns -1 => kernel 2.4 */
-                               ps->num_lwp = 1;
-                       }
-               }
+               if (ps->num_lwp <= 0)
+                       ps->num_lwp = 1;
                ps->num_proc = 1;
        }