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