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