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