Merge branch 'master' into collectd-4
[collectd.git] / src / processes.c
1 /**
2  * collectd - src/processes.c
3  * Copyright (C) 2005  Lyonel Vincent
4  * Copyright (C) 2006-2007  Florian Forster (Mach code)
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the
8  * Free Software Foundation; either version 2 of the License, or (at your
9  * option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  *
20  * Authors:
21  *   Lyonel Vincent <lyonel at ezix.org>
22  *   Florian octo Forster <octo at verplant.org>
23  **/
24
25 #include "collectd.h"
26 #include "common.h"
27 #include "plugin.h"
28 #include "configfile.h"
29
30 /* Include header files for the mach system, if they exist.. */
31 #if HAVE_THREAD_INFO
32 #  if HAVE_MACH_MACH_INIT_H
33 #    include <mach/mach_init.h>
34 #  endif
35 #  if HAVE_MACH_HOST_PRIV_H
36 #    include <mach/host_priv.h>
37 #  endif
38 #  if HAVE_MACH_MACH_ERROR_H
39 #    include <mach/mach_error.h>
40 #  endif
41 #  if HAVE_MACH_MACH_HOST_H
42 #    include <mach/mach_host.h>
43 #  endif
44 #  if HAVE_MACH_MACH_PORT_H
45 #    include <mach/mach_port.h>
46 #  endif
47 #  if HAVE_MACH_MACH_TYPES_H
48 #    include <mach/mach_types.h>
49 #  endif
50 #  if HAVE_MACH_MESSAGE_H
51 #    include <mach/message.h>
52 #  endif
53 #  if HAVE_MACH_PROCESSOR_SET_H
54 #    include <mach/processor_set.h>
55 #  endif
56 #  if HAVE_MACH_TASK_H
57 #    include <mach/task.h>
58 #  endif
59 #  if HAVE_MACH_THREAD_ACT_H
60 #    include <mach/thread_act.h>
61 #  endif
62 #  if HAVE_MACH_VM_REGION_H
63 #    include <mach/vm_region.h>
64 #  endif
65 #  if HAVE_MACH_VM_MAP_H
66 #    include <mach/vm_map.h>
67 #  endif
68 #  if HAVE_MACH_VM_PROT_H
69 #    include <mach/vm_prot.h>
70 #  endif
71 #  if HAVE_SYS_SYSCTL_H
72 #    include <sys/sysctl.h>
73 #  endif
74 /* #endif HAVE_THREAD_INFO */
75
76 #elif KERNEL_LINUX
77 #  if HAVE_LINUX_CONFIG_H
78 #    include <linux/config.h>
79 #  endif
80 #  ifndef CONFIG_HZ
81 #    define CONFIG_HZ 100
82 #  endif
83 #endif /* KERNEL_LINUX */
84
85 #define MODULE_NAME "processes"
86
87 #if HAVE_THREAD_INFO || KERNEL_LINUX
88 # define PROCESSES_HAVE_READ 1
89 #else
90 # define PROCESSES_HAVE_READ 0
91 #endif
92
93 #define BUFSIZE 256
94
95 static data_source_t state_dsrc[1] =
96 {
97         {"value", DS_TYPE_GAUGE, 0.0, 65535.0}
98 };
99
100 static data_set_t state_ds =
101 {
102         "ps_state", 1, state_dsrc
103 };
104
105 static data_source_t rss_dsrc[1] =
106 {
107         /* max = 2^63 - 1 */
108         {"value", DS_TYPE_GAUGE, 0.0, 9223372036854775807.0}
109 };
110
111 static data_set_t rss_ds =
112 {
113         "ps_rss", 1, rss_dsrc
114 };
115
116 static data_source_t time_dsrc[2] =
117 {
118         /* 1 second in user-mode per second ought to be enough.. */
119         {"user", DS_TYPE_COUNTER, 0.0, 1000000.0},
120         {"syst", DS_TYPE_COUNTER, 0.0, 1000000.0}
121 };
122
123 static data_set_t time_ds =
124 {
125         "ps_cputime", 2, time_dsrc
126 };
127
128 static data_source_t count_dsrc[2] =
129 {
130         /* 1 second in user-mode per second ought to be enough.. */
131         {"processes", DS_TYPE_GAUGE, 0.0, 1000000.0},
132         {"threads",   DS_TYPE_GAUGE, 0.0, 1000000.0}
133 };
134
135 static data_set_t count_ds =
136 {
137         "ps_count", 2, count_dsrc
138 };
139
140 static data_source_t pagefaults_dsrc[2] =
141 {
142         /* max = 2^63 - 1 */
143         {"minflt", DS_TYPE_COUNTER, 0.0, 9223372036854775807.0},
144         {"majflt", DS_TYPE_COUNTER, 0.0, 9223372036854775807.0}
145 };
146
147 static data_set_t pagefaults_ds =
148 {
149         "ps_pagefaults", 2, pagefaults_dsrc
150 };
151
152 #if PROCESSES_HAVE_READ
153 #if HAVE_THREAD_INFO | KERNEL_LINUX
154 static const char *config_keys[] =
155 {
156         "Process",
157         NULL
158 };
159 static int config_keys_num = 1;
160 #endif
161
162 typedef struct procstat_entry_s
163 {
164         unsigned long id;
165         unsigned long age;
166
167         unsigned long num_proc;
168         unsigned long num_lwp;
169         unsigned long vmem_rss;
170
171         unsigned long vmem_minflt;
172         unsigned long vmem_majflt;
173         unsigned long vmem_minflt_counter;
174         unsigned long vmem_majflt_counter;
175
176         unsigned long cpu_user;
177         unsigned long cpu_system;
178         unsigned long cpu_user_counter;
179         unsigned long cpu_system_counter;
180
181         struct procstat_entry_s *next;
182 } procstat_entry_t;
183
184 #define PROCSTAT_NAME_LEN 256
185 typedef struct procstat
186 {
187         char          name[PROCSTAT_NAME_LEN];
188
189         unsigned long num_proc;
190         unsigned long num_lwp;
191         unsigned long vmem_rss;
192
193         unsigned long vmem_minflt_counter;
194         unsigned long vmem_majflt_counter;
195
196         unsigned long cpu_user_counter;
197         unsigned long cpu_system_counter;
198
199         struct procstat   *next;
200         struct procstat_entry_s *instances;
201 } procstat_t;
202
203 #if HAVE_THREAD_INFO | KERNEL_LINUX
204 static procstat_t *list_head_g = NULL;
205 #endif
206
207 #if HAVE_THREAD_INFO
208 static mach_port_t port_host_self;
209 static mach_port_t port_task_self;
210
211 static processor_set_name_array_t pset_list;
212 static mach_msg_type_number_t     pset_list_len;
213 /* #endif HAVE_THREAD_INFO */
214
215 #elif KERNEL_LINUX
216 static long pagesize_g;
217 #endif /* KERNEL_LINUX */
218
219 #if HAVE_THREAD_INFO | KERNEL_LINUX
220 static void ps_list_register (const char *name)
221 {
222         procstat_t *new;
223         procstat_t *ptr;
224
225         if ((new = (procstat_t *) malloc (sizeof (procstat_t))) == NULL)
226                 return;
227         memset (new, 0, sizeof (procstat_t));
228         strncpy (new->name, name, PROCSTAT_NAME_LEN);
229
230         for (ptr = list_head_g; ptr != NULL; ptr = ptr->next)
231         {
232                 if (strcmp (ptr->name, name) == 0)
233                         return;
234                 if (ptr->next == NULL)
235                         break;
236         }
237
238         if (ptr == NULL)
239                 list_head_g = new;
240         else
241                 ptr->next = new;
242 }
243
244 static procstat_t *ps_list_search (const char *name)
245 {
246         procstat_t *ptr;
247
248         for (ptr = list_head_g; ptr != NULL; ptr = ptr->next)
249                 if (strcmp (ptr->name, name) == 0)
250                         break;
251
252         return (ptr);
253 }
254
255 static void ps_list_add (const char *name, procstat_entry_t *entry)
256 {
257         procstat_t *ps;
258         procstat_entry_t *pse;
259
260         if (entry->id == 0)
261                 return;
262
263         if ((ps = ps_list_search (name)) == NULL)
264                 return;
265
266         for (pse = ps->instances; pse != NULL; pse = pse->next)
267                 if ((pse->id == entry->id) || (pse->next == NULL))
268                         break;
269
270         if ((pse == NULL) || (pse->id != entry->id))
271         {
272                 procstat_entry_t *new;
273
274                 new = (procstat_entry_t *) malloc (sizeof (procstat_entry_t));
275                 if (new == NULL)
276                         return;
277                 memset (new, 0, sizeof (procstat_entry_t));
278                 new->id = entry->id;
279
280                 if (pse == NULL)
281                         ps->instances = new;
282                 else
283                         pse->next = new;
284
285                 pse = new;
286         }
287
288         pse->age = 0;
289         pse->num_proc = entry->num_proc;
290         pse->num_lwp  = entry->num_lwp;
291         pse->vmem_rss = entry->vmem_rss;
292
293         ps->num_proc += pse->num_proc;
294         ps->num_lwp  += pse->num_lwp;
295         ps->vmem_rss += pse->vmem_rss;
296
297         if ((entry->vmem_minflt_counter == 0)
298                         && (entry->vmem_majflt_counter == 0))
299         {
300                 pse->vmem_minflt_counter += entry->vmem_minflt;
301                 pse->vmem_minflt = entry->vmem_minflt;
302
303                 pse->vmem_majflt_counter += entry->vmem_majflt;
304                 pse->vmem_majflt = entry->vmem_majflt;
305         }
306         else
307         {
308                 if (entry->vmem_minflt_counter < pse->vmem_minflt_counter)
309                 {
310                         pse->vmem_minflt = entry->vmem_minflt_counter
311                                 + (ULONG_MAX - pse->vmem_minflt_counter);
312                 }
313                 else
314                 {
315                         pse->vmem_minflt = entry->vmem_minflt_counter - pse->vmem_minflt_counter;
316                 }
317                 pse->vmem_minflt_counter = entry->vmem_minflt_counter;
318
319                 if (entry->vmem_majflt_counter < pse->vmem_majflt_counter)
320                 {
321                         pse->vmem_majflt = entry->vmem_majflt_counter
322                                 + (ULONG_MAX - pse->vmem_majflt_counter);
323                 }
324                 else
325                 {
326                         pse->vmem_majflt = entry->vmem_majflt_counter - pse->vmem_majflt_counter;
327                 }
328                 pse->vmem_majflt_counter = entry->vmem_majflt_counter;
329         }
330
331         ps->vmem_minflt_counter += pse->vmem_minflt;
332         ps->vmem_majflt_counter += pse->vmem_majflt;
333
334         if ((entry->cpu_user_counter == 0)
335                         && (entry->cpu_system_counter == 0))
336         {
337                 pse->cpu_user_counter += entry->cpu_user;
338                 pse->cpu_user = entry->cpu_user;
339
340                 pse->cpu_system_counter += entry->cpu_system;
341                 pse->cpu_system = entry->cpu_system;
342         }
343         else
344         {
345                 if (entry->cpu_user_counter < pse->cpu_user_counter)
346                 {
347                         pse->cpu_user = entry->cpu_user_counter
348                                 + (ULONG_MAX - pse->cpu_user_counter);
349                 }
350                 else
351                 {
352                         pse->cpu_user = entry->cpu_user_counter - pse->cpu_user_counter;
353                 }
354                 pse->cpu_user_counter = entry->cpu_user_counter;
355
356                 if (entry->cpu_system_counter < pse->cpu_system_counter)
357                 {
358                         pse->cpu_system = entry->cpu_system_counter
359                                 + (ULONG_MAX - pse->cpu_system_counter);
360                 }
361                 else
362                 {
363                         pse->cpu_system = entry->cpu_system_counter - pse->cpu_system_counter;
364                 }
365                 pse->cpu_system_counter = entry->cpu_system_counter;
366         }
367
368         ps->cpu_user_counter   += pse->cpu_user;
369         ps->cpu_system_counter += pse->cpu_system;
370 }
371
372 static void ps_list_reset (void)
373 {
374         procstat_t *ps;
375         procstat_entry_t *pse;
376         procstat_entry_t *pse_prev;
377
378         for (ps = list_head_g; ps != NULL; ps = ps->next)
379         {
380                 ps->num_proc    = 0;
381                 ps->num_lwp     = 0;
382                 ps->vmem_rss    = 0;
383
384                 pse_prev = NULL;
385                 pse = ps->instances;
386                 while (pse != NULL)
387                 {
388                         if (pse->age > 10)
389                         {
390                                 DEBUG ("Removing this procstat entry cause it's too old: "
391                                                 "id = %lu; name = %s;",
392                                                 pse->id, ps->name);
393
394                                 if (pse_prev == NULL)
395                                 {
396                                         ps->instances = pse->next;
397                                         free (pse);
398                                         pse = ps->instances;
399                                 }
400                                 else
401                                 {
402                                         pse_prev->next = pse->next;
403                                         free (pse);
404                                         pse = pse_prev->next;
405                                 }
406                         }
407                         else
408                         {
409                                 pse->age++;
410                                 pse_prev = pse;
411                                 pse = pse->next;
412                         }
413                 } /* while (pse != NULL) */
414         } /* for (ps = list_head_g; ps != NULL; ps = ps->next) */
415 }
416
417 static int ps_config (const char *key, const char *value)
418 {
419         if (strcasecmp (key, "Process") == 0)
420         {
421                 ps_list_register (value);
422         }
423         else
424         {
425                 return (-1);
426         }
427
428         return (0);
429 }
430 #endif /* HAVE_THREAD_INFO | KERNEL_LINUX */
431
432 static int ps_init (void)
433 {
434 #if HAVE_THREAD_INFO
435         kern_return_t status;
436
437         port_host_self = mach_host_self ();
438         port_task_self = mach_task_self ();
439
440         if (pset_list != NULL)
441         {
442                 vm_deallocate (port_task_self,
443                                 (vm_address_t) pset_list,
444                                 pset_list_len * sizeof (processor_set_t));
445                 pset_list = NULL;
446                 pset_list_len = 0;
447         }
448
449         if ((status = host_processor_sets (port_host_self,
450                                         &pset_list,
451                                         &pset_list_len)) != KERN_SUCCESS)
452         {
453                 ERROR ("host_processor_sets failed: %s\n",
454                                 mach_error_string (status));
455                 pset_list = NULL;
456                 pset_list_len = 0;
457                 return (-1);
458         }
459 /* #endif HAVE_THREAD_INFO */
460
461 #elif KERNEL_LINUX
462         pagesize_g = sysconf(_SC_PAGESIZE);
463         DEBUG ("pagesize_g = %li; CONFIG_HZ = %i;",
464                         pagesize_g, CONFIG_HZ);
465 #endif /* KERNEL_LINUX */
466
467         return (0);
468 } /* int ps_init */
469
470 static void ps_submit_state (const char *state, double value)
471 {
472         value_t values[1];
473         value_list_t vl = VALUE_LIST_INIT;
474
475         values[0].gauge = value;
476
477         vl.values = values;
478         vl.values_len = 1;
479         vl.time = time (NULL);
480         strcpy (vl.host, hostname_g);
481         strcpy (vl.plugin, "processes");
482         strcpy (vl.plugin_instance, "");
483         strncpy (vl.type_instance, state, sizeof (vl.type_instance));
484
485         plugin_dispatch_values ("ps_state", &vl);
486 }
487
488 static void ps_submit_proc_list (procstat_t *ps)
489 {
490         value_t values[2];
491         value_list_t vl = VALUE_LIST_INIT;
492
493         vl.values = values;
494         vl.values_len = 2;
495         vl.time = time (NULL);
496         strcpy (vl.host, hostname_g);
497         strcpy (vl.plugin, "processes");
498         strncpy (vl.plugin_instance, ps->name, sizeof (vl.plugin_instance));
499
500         vl.values[0].gauge = ps->vmem_rss;
501         vl.values_len = 1;
502         plugin_dispatch_values ("ps_rss", &vl);
503
504         vl.values[0].counter = ps->cpu_user_counter;
505         vl.values[1].counter = ps->cpu_system_counter;
506         vl.values_len = 2;
507         plugin_dispatch_values ("ps_cputime", &vl);
508
509         vl.values[0].gauge = ps->num_proc;
510         vl.values[1].gauge = ps->num_lwp;
511         vl.values_len = 2;
512         plugin_dispatch_values ("ps_count", &vl);
513
514         vl.values[0].counter = ps->vmem_minflt_counter;
515         vl.values[1].counter = ps->vmem_majflt_counter;
516         vl.values_len = 2;
517         plugin_dispatch_values ("ps_pagefaults", &vl);
518
519         DEBUG ("name = %s; num_proc = %lu; num_lwp = %lu; vmem_rss = %lu; "
520                         "vmem_minflt_counter = %lu; vmem_majflt_counter = %lu; "
521                         "cpu_user_counter = %lu; cpu_system_counter = %lu;",
522                         ps->name, ps->num_proc, ps->num_lwp, ps->vmem_rss,
523                         ps->vmem_minflt_counter, ps->vmem_majflt_counter,
524                         ps->cpu_user_counter, ps->cpu_system_counter);
525 } /* void ps_submit_proc_list */
526
527 #if KERNEL_LINUX
528 static int *ps_read_tasks (int pid)
529 {
530         int *list = NULL;
531         int  list_size = 1; /* size of allocated space, in elements */
532         int  list_len = 0;  /* number of currently used elements */
533
534         char           dirname[64];
535         DIR           *dh;
536         struct dirent *ent;
537
538         snprintf (dirname, 64, "/proc/%i/task", pid);
539         dirname[63] = '\0';
540
541         if ((dh = opendir (dirname)) == NULL)
542         {
543                 DEBUG ("Failed to open directory `%s'", dirname);
544                 return (NULL);
545         }
546
547         while ((ent = readdir (dh)) != NULL)
548         {
549                 if (!isdigit (ent->d_name[0]))
550                         continue;
551
552                 if ((list_len + 1) >= list_size)
553                 {
554                         int *new_ptr;
555                         int  new_size = 2 * list_size;
556                         /* Comes in sizes: 2, 4, 8, 16, ... */
557
558                         new_ptr = (int *) realloc (list, (size_t) (sizeof (int) * new_size));
559                         if (new_ptr == NULL)
560                         {
561                                 if (list != NULL)
562                                         free (list);
563                                 ERROR ("processes plugin: "
564                                                 "Failed to allocate more memory.");
565                                 return (NULL);
566                         }
567
568                         list = new_ptr;
569                         list_size = new_size;
570
571                         memset (list + list_len, 0, sizeof (int) * (list_size - list_len));
572                 }
573
574                 list[list_len] = atoi (ent->d_name);
575                 if (list[list_len] != 0)
576                         list_len++;
577         }
578
579         closedir (dh);
580
581         assert (list_len < list_size);
582         assert (list[list_len] == 0);
583
584         return (list);
585 }
586
587 int ps_read_process (int pid, procstat_t *ps, char *state)
588 {
589         char  filename[64];
590         char  buffer[1024];
591         FILE *fh;
592
593         char *fields[64];
594         char  fields_len;
595
596         int  *tasks;
597         int   i;
598
599         int   ppid;
600         int   name_len;
601
602         long long unsigned cpu_user_counter;
603         long long unsigned cpu_system_counter;
604         long long unsigned vmem_rss;
605
606         memset (ps, 0, sizeof (procstat_t));
607
608         snprintf (filename, 64, "/proc/%i/stat", pid);
609         filename[63] = '\0';
610
611         if ((fh = fopen (filename, "r")) == NULL)
612                 return (-1);
613
614         if (fgets (buffer, 1024, fh) == NULL)
615         {
616                 fclose (fh);
617                 return (-1);
618         }
619
620         fclose (fh);
621
622         fields_len = strsplit (buffer, fields, 64);
623         if (fields_len < 24)
624         {
625                 DEBUG ("processes plugin: ps_read_process (pid = %i):"
626                                 " `%s' has only %i fields..",
627                                 (int) pid, filename, fields_len);
628                 return (-1);
629         }
630
631         /* copy the name, strip brackets in the process */
632         name_len = strlen (fields[1]) - 2;
633         if ((fields[1][0] != '(') || (fields[1][name_len + 1] != ')'))
634         {
635                 DEBUG ("No brackets found in process name: `%s'", fields[1]);
636                 return (-1);
637         }
638         fields[1] = fields[1] + 1;
639         fields[1][name_len] = '\0';
640         strncpy (ps->name, fields[1], PROCSTAT_NAME_LEN);
641
642         ppid = atoi (fields[3]);
643
644         if ((tasks = ps_read_tasks (pid)) == NULL)
645         {
646                 /* This happends for zombied, e.g. */
647                 DEBUG ("ps_read_tasks (%i) failed.", pid);
648                 *state = 'Z';
649                 ps->num_lwp  = 0;
650                 ps->num_proc = 0;
651         }
652         else
653         {
654                 *state = '\0';
655                 ps->num_lwp  = 0;
656                 ps->num_proc = 1;
657                 for (i = 0; tasks[i] != 0; i++)
658                         ps->num_lwp++;
659
660                 free (tasks);
661                 tasks = NULL;
662         }
663
664         /* Leave the rest at zero if this is only an LWP */
665         if (ps->num_proc == 0)
666         {
667                 DEBUG ("This is only an LWP: pid = %i; name = %s;",
668                                 pid, ps->name);
669                 return (0);
670         }
671
672         cpu_user_counter   = atoll (fields[13]);
673         cpu_system_counter = atoll (fields[14]);
674         vmem_rss = atoll (fields[23]);
675         ps->vmem_minflt_counter = atol (fields[9]);
676         ps->vmem_majflt_counter = atol (fields[11]);
677         
678         /* Convert jiffies to useconds */
679         cpu_user_counter   = cpu_user_counter   * 1000000 / CONFIG_HZ;
680         cpu_system_counter = cpu_system_counter * 1000000 / CONFIG_HZ;
681         vmem_rss = vmem_rss * pagesize_g;
682
683         ps->cpu_user_counter = (unsigned long) cpu_user_counter;
684         ps->cpu_system_counter = (unsigned long) cpu_system_counter;
685         ps->vmem_rss = (unsigned long) vmem_rss;
686
687         *state = fields[2][0];
688
689         /* success */
690         return (0);
691 } /* int ps_read_process (...) */
692 #endif /* KERNEL_LINUX */
693
694 #if HAVE_THREAD_INFO
695 static int mach_get_task_name (task_t t, int *pid, char *name, size_t name_max_len)
696 {
697         int mib[4];
698
699         struct kinfo_proc kp;
700         size_t            kp_size;
701
702         mib[0] = CTL_KERN;
703         mib[1] = KERN_PROC;
704         mib[2] = KERN_PROC_PID;
705
706         if (pid_for_task (t, pid) != KERN_SUCCESS)
707                 return (-1);
708         mib[3] = *pid;
709
710         kp_size = sizeof (kp);
711         if (sysctl (mib, 4, &kp, &kp_size, NULL, 0) != 0)
712                 return (-1);
713
714         if (name_max_len > (MAXCOMLEN + 1))
715                 name_max_len = MAXCOMLEN + 1;
716
717         strncpy (name, kp.kp_proc.p_comm, name_max_len - 1);
718         name[name_max_len - 1] = '\0';
719
720         DEBUG ("pid = %i; name = %s;", *pid, name);
721
722         /* We don't do the special handling for `p_comm == "LaunchCFMApp"' as
723          * `top' does it, because it is a lot of work and only used when
724          * debugging. -octo */
725
726         return (0);
727 }
728 #endif /* HAVE_THREAD_INFO */
729
730 static int ps_read (void)
731 {
732 #if HAVE_THREAD_INFO
733         kern_return_t            status;
734
735         int                      pset;
736         processor_set_t          port_pset_priv;
737
738         int                      task;
739         task_array_t             task_list;
740         mach_msg_type_number_t   task_list_len;
741
742         int                      task_pid;
743         char                     task_name[MAXCOMLEN + 1];
744
745         int                      thread;
746         thread_act_array_t       thread_list;
747         mach_msg_type_number_t   thread_list_len;
748         thread_basic_info_data_t thread_data;
749         mach_msg_type_number_t   thread_data_len;
750
751         int running  = 0;
752         int sleeping = 0;
753         int zombies  = 0;
754         int stopped  = 0;
755         int blocked  = 0;
756
757         procstat_t *ps;
758         procstat_entry_t pse;
759
760         ps_list_reset ();
761
762         /*
763          * The Mach-concept is a little different from the traditional UNIX
764          * concept: All the work is done in threads. Threads are contained in
765          * `tasks'. Therefore, `task status' doesn't make much sense, since
766          * it's actually a `thread status'.
767          * Tasks are assigned to sets of processors, so that's where you go to
768          * get a list.
769          */
770         for (pset = 0; pset < pset_list_len; pset++)
771         {
772                 if ((status = host_processor_set_priv (port_host_self,
773                                                 pset_list[pset],
774                                                 &port_pset_priv)) != KERN_SUCCESS)
775                 {
776                         ERROR ("host_processor_set_priv failed: %s\n",
777                                         mach_error_string (status));
778                         continue;
779                 }
780
781                 if ((status = processor_set_tasks (port_pset_priv,
782                                                 &task_list,
783                                                 &task_list_len)) != KERN_SUCCESS)
784                 {
785                         ERROR ("processor_set_tasks failed: %s\n",
786                                         mach_error_string (status));
787                         mach_port_deallocate (port_task_self, port_pset_priv);
788                         continue;
789                 }
790
791                 for (task = 0; task < task_list_len; task++)
792                 {
793                         ps = NULL;
794                         if (mach_get_task_name (task_list[task],
795                                                 &task_pid,
796                                                 task_name, PROCSTAT_NAME_LEN) == 0)
797                                 ps = ps_list_search (task_name);
798
799                         /* Collect more detailed statistics for this process */
800                         if (ps != NULL)
801                         {
802                                 task_basic_info_data_t        task_basic_info;
803                                 mach_msg_type_number_t        task_basic_info_len;
804                                 task_events_info_data_t       task_events_info;
805                                 mach_msg_type_number_t        task_events_info_len;
806                                 task_absolutetime_info_data_t task_absolutetime_info;
807                                 mach_msg_type_number_t        task_absolutetime_info_len;
808
809                                 memset (&pse, '\0', sizeof (pse));
810                                 pse.id = task_pid;
811
812                                 task_basic_info_len = TASK_BASIC_INFO_COUNT;
813                                 status = task_info (task_list[task],
814                                                 TASK_BASIC_INFO,
815                                                 (task_info_t) &task_basic_info,
816                                                 &task_basic_info_len);
817                                 if (status != KERN_SUCCESS)
818                                 {
819                                         ERROR ("task_info failed: %s",
820                                                         mach_error_string (status));
821                                         continue; /* with next thread_list */
822                                 }
823
824                                 task_events_info_len = TASK_EVENTS_INFO_COUNT;
825                                 status = task_info (task_list[task],
826                                                 TASK_EVENTS_INFO,
827                                                 (task_info_t) &task_events_info,
828                                                 &task_events_info_len);
829                                 if (status != KERN_SUCCESS)
830                                 {
831                                         ERROR ("task_info failed: %s",
832                                                         mach_error_string (status));
833                                         continue; /* with next thread_list */
834                                 }
835
836                                 task_absolutetime_info_len = TASK_ABSOLUTETIME_INFO_COUNT;
837                                 status = task_info (task_list[task],
838                                                 TASK_ABSOLUTETIME_INFO,
839                                                 (task_info_t) &task_absolutetime_info,
840                                                 &task_absolutetime_info_len);
841                                 if (status != KERN_SUCCESS)
842                                 {
843                                         ERROR ("task_info failed: %s",
844                                                         mach_error_string (status));
845                                         continue; /* with next thread_list */
846                                 }
847
848                                 pse.num_proc++;
849                                 pse.vmem_rss = task_basic_info.resident_size;
850
851                                 pse.vmem_minflt_counter = task_events_info.cow_faults;
852                                 pse.vmem_majflt_counter = task_events_info.faults;
853
854                                 pse.cpu_user_counter = task_absolutetime_info.total_user;
855                                 pse.cpu_system_counter = task_absolutetime_info.total_system;
856                         }
857
858                         status = task_threads (task_list[task], &thread_list,
859                                         &thread_list_len);
860                         if (status != KERN_SUCCESS)
861                         {
862                                 /* Apple's `top' treats this case a zombie. It
863                                  * makes sense to some extend: A `zombie'
864                                  * thread is nonsense, since the task/process
865                                  * is dead. */
866                                 zombies++;
867                                 DEBUG ("task_threads failed: %s",
868                                                 mach_error_string (status));
869                                 if (task_list[task] != port_task_self)
870                                         mach_port_deallocate (port_task_self,
871                                                         task_list[task]);
872                                 continue; /* with next task_list */
873                         }
874
875                         for (thread = 0; thread < thread_list_len; thread++)
876                         {
877                                 thread_data_len = THREAD_BASIC_INFO_COUNT;
878                                 status = thread_info (thread_list[thread],
879                                                 THREAD_BASIC_INFO,
880                                                 (thread_info_t) &thread_data,
881                                                 &thread_data_len);
882                                 if (status != KERN_SUCCESS)
883                                 {
884                                         ERROR ("thread_info failed: %s",
885                                                         mach_error_string (status));
886                                         if (task_list[task] != port_task_self)
887                                                 mach_port_deallocate (port_task_self,
888                                                                 thread_list[thread]);
889                                         continue; /* with next thread_list */
890                                 }
891
892                                 if (ps != NULL)
893                                         pse.num_lwp++;
894
895                                 switch (thread_data.run_state)
896                                 {
897                                         case TH_STATE_RUNNING:
898                                                 running++;
899                                                 break;
900                                         case TH_STATE_STOPPED:
901                                         /* What exactly is `halted'? */
902                                         case TH_STATE_HALTED:
903                                                 stopped++;
904                                                 break;
905                                         case TH_STATE_WAITING:
906                                                 sleeping++;
907                                                 break;
908                                         case TH_STATE_UNINTERRUPTIBLE:
909                                                 blocked++;
910                                                 break;
911                                         /* There is no `zombie' case here,
912                                          * since there are no zombie-threads.
913                                          * There's only zombie tasks, which are
914                                          * handled above. */
915                                         default:
916                                                 WARNING ("Unknown thread status: %s",
917                                                                 thread_data.run_state);
918                                                 break;
919                                 } /* switch (thread_data.run_state) */
920
921                                 if (task_list[task] != port_task_self)
922                                 {
923                                         status = mach_port_deallocate (port_task_self,
924                                                         thread_list[thread]);
925                                         if (status != KERN_SUCCESS)
926                                                 ERROR ("mach_port_deallocate failed: %s",
927                                                                 mach_error_string (status));
928                                 }
929                         } /* for (thread_list) */
930
931                         if ((status = vm_deallocate (port_task_self,
932                                                         (vm_address_t) thread_list,
933                                                         thread_list_len * sizeof (thread_act_t)))
934                                         != KERN_SUCCESS)
935                         {
936                                 ERROR ("vm_deallocate failed: %s",
937                                                 mach_error_string (status));
938                         }
939                         thread_list = NULL;
940                         thread_list_len = 0;
941
942                         /* Only deallocate the task port, if it isn't our own.
943                          * Don't know what would happen in that case, but this
944                          * is what Apple's top does.. ;) */
945                         if (task_list[task] != port_task_self)
946                         {
947                                 status = mach_port_deallocate (port_task_self,
948                                                 task_list[task]);
949                                 if (status != KERN_SUCCESS)
950                                         ERROR ("mach_port_deallocate failed: %s",
951                                                         mach_error_string (status));
952                         }
953
954                         if (ps != NULL)
955                                 ps_list_add (task_name, &pse);
956                 } /* for (task_list) */
957
958                 if ((status = vm_deallocate (port_task_self,
959                                 (vm_address_t) task_list,
960                                 task_list_len * sizeof (task_t))) != KERN_SUCCESS)
961                 {
962                         ERROR ("vm_deallocate failed: %s",
963                                         mach_error_string (status));
964                 }
965                 task_list = NULL;
966                 task_list_len = 0;
967
968                 if ((status = mach_port_deallocate (port_task_self, port_pset_priv))
969                                 != KERN_SUCCESS)
970                 {
971                         ERROR ("mach_port_deallocate failed: %s",
972                                         mach_error_string (status));
973                 }
974         } /* for (pset_list) */
975
976         ps_submit_state ("running", running);
977         ps_submit_state ("sleeping", sleeping);
978         ps_submit_state ("zombies", zombies);
979         ps_submit_state ("stopped", stopped);
980         ps_submit_state ("blocked", blocked);
981
982         for (ps = list_head_g; ps != NULL; ps = ps->next)
983                 ps_submit_proc_list (ps);
984 /* #endif HAVE_THREAD_INFO */
985
986 #elif KERNEL_LINUX
987         int running  = 0;
988         int sleeping = 0;
989         int zombies  = 0;
990         int stopped  = 0;
991         int paging   = 0;
992         int blocked  = 0;
993
994         struct dirent *ent;
995         DIR           *proc;
996         int            pid;
997
998         int        status;
999         procstat_t ps;
1000         procstat_entry_t pse;
1001         char       state;
1002
1003         procstat_t *ps_ptr;
1004
1005         running = sleeping = zombies = stopped = paging = blocked = 0;
1006         ps_list_reset ();
1007
1008         if ((proc = opendir ("/proc")) == NULL)
1009         {
1010                 char errbuf[1024];
1011                 ERROR ("Cannot open `/proc': %s",
1012                                 sstrerror (errno, errbuf, sizeof (errbuf)));
1013                 return (-1);
1014         }
1015
1016         while ((ent = readdir (proc)) != NULL)
1017         {
1018                 if (!isdigit (ent->d_name[0]))
1019                         continue;
1020
1021                 if ((pid = atoi (ent->d_name)) < 1)
1022                         continue;
1023
1024                 status = ps_read_process (pid, &ps, &state);
1025                 if (status != 0)
1026                 {
1027                         DEBUG ("ps_read_process failed: %i", status);
1028                         continue;
1029                 }
1030
1031                 pse.id       = pid;
1032                 pse.age      = 0;
1033
1034                 pse.num_proc = ps.num_proc;
1035                 pse.num_lwp  = ps.num_lwp;
1036                 pse.vmem_rss = ps.vmem_rss;
1037
1038                 pse.vmem_minflt = 0;
1039                 pse.vmem_minflt_counter = ps.vmem_minflt_counter;
1040                 pse.vmem_majflt = 0;
1041                 pse.vmem_majflt_counter = ps.vmem_majflt_counter;
1042
1043                 pse.cpu_user = 0;
1044                 pse.cpu_user_counter = ps.cpu_user_counter;
1045                 pse.cpu_system = 0;
1046                 pse.cpu_system_counter = ps.cpu_system_counter;
1047
1048                 switch (state)
1049                 {
1050                         case 'R': running++;  break;
1051                         case 'S': sleeping++; break;
1052                         case 'D': blocked++;  break;
1053                         case 'Z': zombies++;  break;
1054                         case 'T': stopped++;  break;
1055                         case 'W': paging++;   break;
1056                 }
1057
1058                 ps_list_add (ps.name, &pse);
1059         }
1060
1061         closedir (proc);
1062
1063         ps_submit_state ("running",  running);
1064         ps_submit_state ("sleeping", sleeping);
1065         ps_submit_state ("zombies",  zombies);
1066         ps_submit_state ("stopped",  stopped);
1067         ps_submit_state ("paging",   paging);
1068         ps_submit_state ("blocked",  blocked);
1069
1070         for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1071                 ps_submit_proc_list (ps_ptr);
1072 #endif /* KERNEL_LINUX */
1073
1074         return (0);
1075 } /* int ps_read */
1076 #endif /* PROCESSES_HAVE_READ */
1077
1078 void module_register (modreg_e load)
1079 {
1080         if (load & MR_DATASETS)
1081         {
1082                 plugin_register_data_set (&state_ds);
1083                 plugin_register_data_set (&rss_ds);
1084                 plugin_register_data_set (&time_ds);
1085                 plugin_register_data_set (&count_ds);
1086                 plugin_register_data_set (&pagefaults_ds);
1087         }
1088
1089 #if PROCESSES_HAVE_READ
1090         if (load & MR_READ)
1091         {
1092 #if HAVE_THREAD_INFO | KERNEL_LINUX
1093                 plugin_register_config ("processes", ps_config,
1094                                 config_keys, config_keys_num);
1095 #endif
1096                 plugin_register_init ("processes", ps_init);
1097                 plugin_register_read ("processes", ps_read);
1098         }
1099 #endif /* PROCESSES_HAVE_READ */
1100 } /* void module_register */