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