Merge branch 'collectd-4.5' into collectd-4.6
[collectd.git] / src / processes.c
1 /**
2  * collectd - src/processes.c
3  * Copyright (C) 2005       Lyonel Vincent
4  * Copyright (C) 2006-2008  Florian octo Forster
5  * Copyright (C) 2008       Oleg King
6  * Copyright (C) 2009       Sebastian Harl
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by the
10  * Free Software Foundation; either version 2 of the License, or (at your
11  * option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
21  *
22  * Authors:
23  *   Lyonel Vincent <lyonel at ezix.org>
24  *   Florian octo Forster <octo at verplant.org>
25  *   Oleg King <king2 at kaluga.ru>
26  *   Sebastian Harl <sh at tokkee.org>
27  **/
28
29 #include "collectd.h"
30 #include "common.h"
31 #include "plugin.h"
32 #include "configfile.h"
33
34 /* Include header files for the mach system, if they exist.. */
35 #if HAVE_THREAD_INFO
36 #  if HAVE_MACH_MACH_INIT_H
37 #    include <mach/mach_init.h>
38 #  endif
39 #  if HAVE_MACH_HOST_PRIV_H
40 #    include <mach/host_priv.h>
41 #  endif
42 #  if HAVE_MACH_MACH_ERROR_H
43 #    include <mach/mach_error.h>
44 #  endif
45 #  if HAVE_MACH_MACH_HOST_H
46 #    include <mach/mach_host.h>
47 #  endif
48 #  if HAVE_MACH_MACH_PORT_H
49 #    include <mach/mach_port.h>
50 #  endif
51 #  if HAVE_MACH_MACH_TYPES_H
52 #    include <mach/mach_types.h>
53 #  endif
54 #  if HAVE_MACH_MESSAGE_H
55 #    include <mach/message.h>
56 #  endif
57 #  if HAVE_MACH_PROCESSOR_SET_H
58 #    include <mach/processor_set.h>
59 #  endif
60 #  if HAVE_MACH_TASK_H
61 #    include <mach/task.h>
62 #  endif
63 #  if HAVE_MACH_THREAD_ACT_H
64 #    include <mach/thread_act.h>
65 #  endif
66 #  if HAVE_MACH_VM_REGION_H
67 #    include <mach/vm_region.h>
68 #  endif
69 #  if HAVE_MACH_VM_MAP_H
70 #    include <mach/vm_map.h>
71 #  endif
72 #  if HAVE_MACH_VM_PROT_H
73 #    include <mach/vm_prot.h>
74 #  endif
75 #  if HAVE_SYS_SYSCTL_H
76 #    include <sys/sysctl.h>
77 #  endif
78 /* #endif HAVE_THREAD_INFO */
79
80 #elif KERNEL_LINUX
81 #  if HAVE_LINUX_CONFIG_H
82 #    include <linux/config.h>
83 #  endif
84 #  ifndef CONFIG_HZ
85 #    define CONFIG_HZ 100
86 #  endif
87 /* #endif KERNEL_LINUX */
88
89 #elif HAVE_LIBKVM_GETPROCS
90 #  include <kvm.h>
91 #  include <sys/user.h>
92 #  include <sys/proc.h>
93 #  if HAVE_SYS_SYSCTL_H
94 #    include <sys/sysctl.h>
95 #  endif
96 /* #endif HAVE_LIBKVM_GETPROCS */
97
98 #else
99 # error "No applicable input method."
100 #endif
101
102 #if HAVE_REGEX_H
103 # include <regex.h>
104 #endif
105
106 #ifndef ARG_MAX
107 #  define ARG_MAX 4096
108 #endif
109
110 #define BUFSIZE 256
111
112 static const char *config_keys[] =
113 {
114         "Process",
115         "ProcessMatch"
116 };
117 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
118
119 typedef struct procstat_entry_s
120 {
121         unsigned long id;
122         unsigned long age;
123
124         unsigned long num_proc;
125         unsigned long num_lwp;
126         unsigned long vmem_size;
127         unsigned long vmem_rss;
128         unsigned long stack_size;
129
130         unsigned long vmem_minflt;
131         unsigned long vmem_majflt;
132         unsigned long vmem_minflt_counter;
133         unsigned long vmem_majflt_counter;
134
135         unsigned long cpu_user;
136         unsigned long cpu_system;
137         unsigned long cpu_user_counter;
138         unsigned long cpu_system_counter;
139
140         struct procstat_entry_s *next;
141 } procstat_entry_t;
142
143 #define PROCSTAT_NAME_LEN 256
144 typedef struct procstat
145 {
146         char          name[PROCSTAT_NAME_LEN];
147 #if HAVE_REGEX_H
148         regex_t *re;
149 #endif
150
151         unsigned long num_proc;
152         unsigned long num_lwp;
153         unsigned long vmem_size;
154         unsigned long vmem_rss;
155         unsigned long stack_size;
156
157         unsigned long vmem_minflt_counter;
158         unsigned long vmem_majflt_counter;
159
160         unsigned long cpu_user_counter;
161         unsigned long cpu_system_counter;
162
163         struct procstat   *next;
164         struct procstat_entry_s *instances;
165 } procstat_t;
166
167 static procstat_t *list_head_g = NULL;
168
169 #if HAVE_THREAD_INFO
170 static mach_port_t port_host_self;
171 static mach_port_t port_task_self;
172
173 static processor_set_name_array_t pset_list;
174 static mach_msg_type_number_t     pset_list_len;
175 /* #endif HAVE_THREAD_INFO */
176
177 #elif KERNEL_LINUX
178 static long pagesize_g;
179 /* #endif KERNEL_LINUX */
180
181 #elif HAVE_LIBKVM_GETPROCS
182 /* no global variables */
183 #endif /* HAVE_LIBKVM_GETPROCS */
184
185 /* put name of process from config to list_head_g tree
186    list_head_g is a list of 'procstat_t' structs with
187    processes names we want to watch */
188 static void ps_list_register (const char *name, const char *regexp)
189 {
190         procstat_t *new;
191         procstat_t *ptr;
192         int status;
193
194         new = (procstat_t *) malloc (sizeof (procstat_t));
195         if (new == NULL)
196         {
197                 ERROR ("processes plugin: ps_list_register: malloc failed.");
198                 return;
199         }
200         memset (new, 0, sizeof (procstat_t));
201         sstrncpy (new->name, name, sizeof (new->name));
202
203 #if HAVE_REGEX_H
204         if (regexp != NULL)
205         {
206                 DEBUG ("ProcessMatch: adding \"%s\" as criteria to process %s.", regexp, name);
207                 new->re = (regex_t *) malloc (sizeof (regex_t));
208                 if (new->re == NULL)
209                 {
210                         ERROR ("processes plugin: ps_list_register: malloc failed.");
211                         sfree (new);
212                         return;
213                 }
214
215                 status = regcomp (new->re, regexp, REG_EXTENDED | REG_NOSUB);
216                 if (status != 0)
217                 {
218                         DEBUG ("ProcessMatch: compiling the regular expression \"%s\" failed.", regexp);
219                         sfree(new->re);
220                         return;
221                 }
222         }
223 #else
224         if (regexp != NULL)
225         {
226                 ERROR ("processes plugin: ps_list_register: "
227                                 "Regular expression \"%s\" found in config "
228                                 "file, but support for regular expressions "
229                                 "has been dispabled at compile time.",
230                                 regexp);
231                 sfree (new);
232                 return;
233         }
234 #endif
235         
236         for (ptr = list_head_g; ptr != NULL; ptr = ptr->next)
237         {
238                 if (strcmp (ptr->name, name) == 0)
239                 {
240                         WARNING ("processes plugin: You have configured more "
241                                         "than one `Process' or "
242                                         "`ProcessMatch' with the same name. "
243                                         "All but the first setting will be "
244                                         "ignored.");
245                         sfree (new->re);
246                         sfree (new);
247                         return;
248                 }
249
250                 if (ptr->next == NULL)
251                         break;
252         }
253
254         if (ptr == NULL)
255                 list_head_g = new;
256         else
257                 ptr->next = new;
258 } /* void ps_list_register */
259
260 /* try to match name against entry, returns 1 if success */
261 static int ps_list_match (const char *name, const char *cmdline, procstat_t *ps)
262 {
263 #if HAVE_REGEX_H
264         if (ps->re != NULL)
265         {
266                 int status;
267                 const char *str;
268
269                 str = cmdline;
270                 if ((str == NULL) || (str[0] == 0))
271                         str = name;
272
273                 assert (str != NULL);
274
275                 status = regexec (ps->re, str,
276                                 /* nmatch = */ 0,
277                                 /* pmatch = */ NULL,
278                                 /* eflags = */ 0);
279                 if (status == 0)
280                         return (1);
281         }
282         else
283 #endif
284         if (strcmp (ps->name, name) == 0)
285                 return (1);
286
287         return (0);
288 } /* int ps_list_match */
289
290 /* add process entry to 'instances' of process 'name' (or refresh it) */
291 static void ps_list_add (const char *name, const char *cmdline, procstat_entry_t *entry)
292 {
293         procstat_t *ps;
294         procstat_entry_t *pse;
295
296         if (entry->id == 0)
297                 return;
298
299         for (ps = list_head_g; ps != NULL; ps = ps->next)
300         {
301                 if ((ps_list_match (name, cmdline, ps)) == 0)
302                         continue;
303
304                 for (pse = ps->instances; pse != NULL; pse = pse->next)
305                         if ((pse->id == entry->id) || (pse->next == NULL))
306                                 break;
307
308                 if ((pse == NULL) || (pse->id != entry->id))
309                 {
310                         procstat_entry_t *new;
311                         
312                         new = (procstat_entry_t *) malloc (sizeof (procstat_entry_t));
313                         if (new == NULL)
314                                 return;
315                         memset (new, 0, sizeof (procstat_entry_t));
316                         new->id = entry->id;
317                         
318                         if (pse == NULL)
319                                 ps->instances = new;
320                         else
321                                 pse->next = new;
322
323                         pse = new;
324                 }
325
326                 pse->age = 0;
327                 pse->num_proc   = entry->num_proc;
328                 pse->num_lwp    = entry->num_lwp;
329                 pse->vmem_size  = entry->vmem_size;
330                 pse->vmem_rss   = entry->vmem_rss;
331                 pse->stack_size = entry->stack_size;
332
333                 ps->num_proc   += pse->num_proc;
334                 ps->num_lwp    += pse->num_lwp;
335                 ps->vmem_size  += pse->vmem_size;
336                 ps->vmem_rss   += pse->vmem_rss;
337                 ps->stack_size += pse->stack_size;
338
339                 if ((entry->vmem_minflt_counter == 0)
340                                 && (entry->vmem_majflt_counter == 0))
341                 {
342                         pse->vmem_minflt_counter += entry->vmem_minflt;
343                         pse->vmem_minflt = entry->vmem_minflt;
344
345                         pse->vmem_majflt_counter += entry->vmem_majflt;
346                         pse->vmem_majflt = entry->vmem_majflt;
347                 }
348                 else
349                 {
350                         if (entry->vmem_minflt_counter < pse->vmem_minflt_counter)
351                         {
352                                 pse->vmem_minflt = entry->vmem_minflt_counter
353                                         + (ULONG_MAX - pse->vmem_minflt_counter);
354                         }
355                         else
356                         {
357                                 pse->vmem_minflt = entry->vmem_minflt_counter - pse->vmem_minflt_counter;
358                         }
359                         pse->vmem_minflt_counter = entry->vmem_minflt_counter;
360                         
361                         if (entry->vmem_majflt_counter < pse->vmem_majflt_counter)
362                         {
363                                 pse->vmem_majflt = entry->vmem_majflt_counter
364                                         + (ULONG_MAX - pse->vmem_majflt_counter);
365                         }
366                         else
367                         {
368                                 pse->vmem_majflt = entry->vmem_majflt_counter - pse->vmem_majflt_counter;
369                         }
370                         pse->vmem_majflt_counter = entry->vmem_majflt_counter;
371                 }
372
373                 ps->vmem_minflt_counter += pse->vmem_minflt;
374                 ps->vmem_majflt_counter += pse->vmem_majflt;
375
376                 if ((entry->cpu_user_counter == 0)
377                                 && (entry->cpu_system_counter == 0))
378                 {
379                         pse->cpu_user_counter += entry->cpu_user;
380                         pse->cpu_user = entry->cpu_user;
381
382                         pse->cpu_system_counter += entry->cpu_system;
383                         pse->cpu_system = entry->cpu_system;
384                 }
385                 else
386                 {
387                         if (entry->cpu_user_counter < pse->cpu_user_counter)
388                         {
389                                 pse->cpu_user = entry->cpu_user_counter
390                                         + (ULONG_MAX - pse->cpu_user_counter);
391                         }
392                         else
393                         {
394                                 pse->cpu_user = entry->cpu_user_counter - pse->cpu_user_counter;
395                         }
396                         pse->cpu_user_counter = entry->cpu_user_counter;
397                         
398                         if (entry->cpu_system_counter < pse->cpu_system_counter)
399                         {
400                                 pse->cpu_system = entry->cpu_system_counter
401                                         + (ULONG_MAX - pse->cpu_system_counter);
402                         }
403                         else
404                         {
405                                 pse->cpu_system = entry->cpu_system_counter - pse->cpu_system_counter;
406                         }
407                         pse->cpu_system_counter = entry->cpu_system_counter;
408                 }
409
410                 ps->cpu_user_counter   += pse->cpu_user;
411                 ps->cpu_system_counter += pse->cpu_system;
412         }
413 }
414
415 /* remove old entries from instances of processes in list_head_g */
416 static void ps_list_reset (void)
417 {
418         procstat_t *ps;
419         procstat_entry_t *pse;
420         procstat_entry_t *pse_prev;
421
422         for (ps = list_head_g; ps != NULL; ps = ps->next)
423         {
424                 ps->num_proc    = 0;
425                 ps->num_lwp     = 0;
426                 ps->vmem_size   = 0;
427                 ps->vmem_rss    = 0;
428                 ps->stack_size  = 0;
429
430                 pse_prev = NULL;
431                 pse = ps->instances;
432                 while (pse != NULL)
433                 {
434                         if (pse->age > 10)
435                         {
436                                 DEBUG ("Removing this procstat entry cause it's too old: "
437                                                 "id = %lu; name = %s;",
438                                                 pse->id, ps->name);
439
440                                 if (pse_prev == NULL)
441                                 {
442                                         ps->instances = pse->next;
443                                         free (pse);
444                                         pse = ps->instances;
445                                 }
446                                 else
447                                 {
448                                         pse_prev->next = pse->next;
449                                         free (pse);
450                                         pse = pse_prev->next;
451                                 }
452                         }
453                         else
454                         {
455                                 pse->age++;
456                                 pse_prev = pse;
457                                 pse = pse->next;
458                         }
459                 } /* while (pse != NULL) */
460         } /* for (ps = list_head_g; ps != NULL; ps = ps->next) */
461 }
462
463 /* put all pre-defined 'Process' names from config to list_head_g tree */
464 static int ps_config (const char *key, const char *value)
465 {
466         if (strcasecmp (key, "Process") == 0)
467         {
468                 ps_list_register (value, NULL);
469         }
470         else if (strcasecmp (key, "ProcessMatch") == 0)
471         {
472                 char *new_val;
473                 char *fields[3];
474                 int fields_num;
475
476                 new_val = strdup (value);
477                 if (new_val == NULL) {
478                         ERROR ("processes plugin: strdup failed when processing "
479                                         "`ProcessMatch %s'.", value);
480                         return (1);
481                 }
482
483                 fields_num = strsplit (new_val, fields,
484                                 STATIC_ARRAY_SIZE (fields));
485                 if (fields_num != 2)
486                 {
487                         ERROR ("processes plugin: `ProcessMatch' needs exactly "
488                                         "two string arguments.");
489                         sfree (new_val);
490                         return (1);
491                 }
492                 ps_list_register (fields[0], fields[1]);
493                 sfree (new_val);
494         }
495         else
496         {
497                 ERROR ("processes plugin: The `%s' configuration option is not "
498                                 "understood and will be ignored.", key);
499                 return (-1);
500         }
501
502         return (0);
503 }
504
505 static int ps_init (void)
506 {
507 #if HAVE_THREAD_INFO
508         kern_return_t status;
509
510         port_host_self = mach_host_self ();
511         port_task_self = mach_task_self ();
512
513         if (pset_list != NULL)
514         {
515                 vm_deallocate (port_task_self,
516                                 (vm_address_t) pset_list,
517                                 pset_list_len * sizeof (processor_set_t));
518                 pset_list = NULL;
519                 pset_list_len = 0;
520         }
521
522         if ((status = host_processor_sets (port_host_self,
523                                         &pset_list,
524                                         &pset_list_len)) != KERN_SUCCESS)
525         {
526                 ERROR ("host_processor_sets failed: %s\n",
527                                 mach_error_string (status));
528                 pset_list = NULL;
529                 pset_list_len = 0;
530                 return (-1);
531         }
532 /* #endif HAVE_THREAD_INFO */
533
534 #elif KERNEL_LINUX
535         pagesize_g = sysconf(_SC_PAGESIZE);
536         DEBUG ("pagesize_g = %li; CONFIG_HZ = %i;",
537                         pagesize_g, CONFIG_HZ);
538 /* #endif KERNEL_LINUX */
539
540 #elif HAVE_LIBKVM_GETPROCS
541 /* no initialization */
542 #endif /* HAVE_LIBKVM_GETPROCS */
543
544         return (0);
545 } /* int ps_init */
546
547 /* submit global state (e.g.: qty of zombies, running, etc..) */
548 static void ps_submit_state (const char *state, double value)
549 {
550         value_t values[1];
551         value_list_t vl = VALUE_LIST_INIT;
552
553         values[0].gauge = value;
554
555         vl.values = values;
556         vl.values_len = 1;
557         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
558         sstrncpy (vl.plugin, "processes", sizeof (vl.plugin));
559         sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
560         sstrncpy (vl.type, "ps_state", sizeof (vl.type));
561         sstrncpy (vl.type_instance, state, sizeof (vl.type_instance));
562
563         plugin_dispatch_values (&vl);
564 }
565
566 /* submit info about specific process (e.g.: memory taken, cpu usage, etc..) */
567 static void ps_submit_proc_list (procstat_t *ps)
568 {
569         value_t values[2];
570         value_list_t vl = VALUE_LIST_INIT;
571
572         vl.values = values;
573         vl.values_len = 2;
574         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
575         sstrncpy (vl.plugin, "processes", sizeof (vl.plugin));
576         sstrncpy (vl.plugin_instance, ps->name, sizeof (vl.plugin_instance));
577
578         sstrncpy (vl.type, "ps_vm", sizeof (vl.type));
579         vl.values[0].gauge = ps->vmem_size;
580         vl.values_len = 1;
581         plugin_dispatch_values (&vl);
582
583         sstrncpy (vl.type, "ps_rss", sizeof (vl.type));
584         vl.values[0].gauge = ps->vmem_rss;
585         vl.values_len = 1;
586         plugin_dispatch_values (&vl);
587
588         sstrncpy (vl.type, "ps_stacksize", sizeof (vl.type));
589         vl.values[0].gauge = ps->stack_size;
590         vl.values_len = 1;
591         plugin_dispatch_values (&vl);
592
593         sstrncpy (vl.type, "ps_cputime", sizeof (vl.type));
594         vl.values[0].counter = ps->cpu_user_counter;
595         vl.values[1].counter = ps->cpu_system_counter;
596         vl.values_len = 2;
597         plugin_dispatch_values (&vl);
598
599         sstrncpy (vl.type, "ps_count", sizeof (vl.type));
600         vl.values[0].gauge = ps->num_proc;
601         vl.values[1].gauge = ps->num_lwp;
602         vl.values_len = 2;
603         plugin_dispatch_values (&vl);
604
605         sstrncpy (vl.type, "ps_pagefaults", sizeof (vl.type));
606         vl.values[0].counter = ps->vmem_minflt_counter;
607         vl.values[1].counter = ps->vmem_majflt_counter;
608         vl.values_len = 2;
609         plugin_dispatch_values (&vl);
610
611         DEBUG ("name = %s; num_proc = %lu; num_lwp = %lu; vmem_rss = %lu; "
612                         "vmem_minflt_counter = %lu; vmem_majflt_counter = %lu; "
613                         "cpu_user_counter = %lu; cpu_system_counter = %lu;",
614                         ps->name, ps->num_proc, ps->num_lwp, ps->vmem_rss,
615                         ps->vmem_minflt_counter, ps->vmem_majflt_counter,
616                         ps->cpu_user_counter, ps->cpu_system_counter);
617 } /* void ps_submit_proc_list */
618
619 /* ------- additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
620 #if KERNEL_LINUX
621 static int *ps_read_tasks (int pid)
622 {
623         int *list = NULL;
624         int  list_size = 1; /* size of allocated space, in elements */
625         int  list_len = 0;  /* number of currently used elements */
626
627         char           dirname[64];
628         DIR           *dh;
629         struct dirent *ent;
630
631         ssnprintf (dirname, sizeof (dirname), "/proc/%i/task", pid);
632
633         if ((dh = opendir (dirname)) == NULL)
634         {
635                 DEBUG ("Failed to open directory `%s'", dirname);
636                 return (NULL);
637         }
638
639         while ((ent = readdir (dh)) != NULL)
640         {
641                 if (!isdigit (ent->d_name[0]))
642                         continue;
643
644                 if ((list_len + 1) >= list_size)
645                 {
646                         int *new_ptr;
647                         int  new_size = 2 * list_size;
648                         /* Comes in sizes: 2, 4, 8, 16, ... */
649
650                         new_ptr = (int *) realloc (list, (size_t) (sizeof (int) * new_size));
651                         if (new_ptr == NULL)
652                         {
653                                 if (list != NULL)
654                                         free (list);
655                                 ERROR ("processes plugin: "
656                                                 "Failed to allocate more memory.");
657                                 return (NULL);
658                         }
659
660                         list = new_ptr;
661                         list_size = new_size;
662
663                         memset (list + list_len, 0, sizeof (int) * (list_size - list_len));
664                 }
665
666                 list[list_len] = atoi (ent->d_name);
667                 if (list[list_len] != 0)
668                         list_len++;
669         }
670
671         closedir (dh);
672
673         if (list_len == 0)
674                 return (NULL);
675
676         assert (list_len < list_size);
677         assert (list[list_len] == 0);
678
679         return (list);
680 } /* int *ps_read_tasks */
681
682 int ps_read_process (int pid, procstat_t *ps, char *state)
683 {
684         char  filename[64];
685         char  buffer[1024];
686
687         char *fields[64];
688         char  fields_len;
689
690         int  *tasks;
691         int   i;
692
693         int   ppid;
694         int   name_len;
695
696         long long unsigned cpu_user_counter;
697         long long unsigned cpu_system_counter;
698         long long unsigned vmem_size;
699         long long unsigned vmem_rss;
700         long long unsigned stack_size;
701
702         memset (ps, 0, sizeof (procstat_t));
703
704         ssnprintf (filename, sizeof (filename), "/proc/%i/stat", pid);
705
706         i = read_file_contents (filename, buffer, sizeof(buffer) - 1);
707         if (i <= 0)
708                 return (-1);
709         buffer[i] = 0;
710
711         fields_len = strsplit (buffer, fields, 64);
712         if (fields_len < 24)
713         {
714                 DEBUG ("processes plugin: ps_read_process (pid = %i):"
715                                 " `%s' has only %i fields..",
716                                 (int) pid, filename, fields_len);
717                 return (-1);
718         }
719
720         /* copy the name, strip brackets in the process */
721         name_len = strlen (fields[1]) - 2;
722         if ((fields[1][0] != '(') || (fields[1][name_len + 1] != ')'))
723         {
724                 DEBUG ("No brackets found in process name: `%s'", fields[1]);
725                 return (-1);
726         }
727         fields[1] = fields[1] + 1;
728         fields[1][name_len] = '\0';
729         strncpy (ps->name, fields[1], PROCSTAT_NAME_LEN);
730
731         ppid = atoi (fields[3]);
732
733         *state = fields[2][0];
734
735         if (*state == 'Z')
736         {
737                 ps->num_lwp  = 0;
738                 ps->num_proc = 0;
739         }
740         else if ((tasks = ps_read_tasks (pid)) == NULL)
741         {
742                 /* Kernel 2.4 or so */
743                 ps->num_lwp  = 1;
744                 ps->num_proc = 1;
745         }
746         else
747         {
748                 ps->num_lwp  = 0;
749                 ps->num_proc = 1;
750                 for (i = 0; tasks[i] != 0; i++)
751                         ps->num_lwp++;
752
753                 free (tasks);
754                 tasks = NULL;
755         }
756
757         /* Leave the rest at zero if this is only a zombi */
758         if (ps->num_proc == 0)
759         {
760                 DEBUG ("processes plugin: This is only a zombi: pid = %i; "
761                                 "name = %s;", pid, ps->name);
762                 return (0);
763         }
764
765         cpu_user_counter   = atoll (fields[13]);
766         cpu_system_counter = atoll (fields[14]);
767         vmem_size          = atoll (fields[22]);
768         vmem_rss           = atoll (fields[23]);
769         ps->vmem_minflt_counter = atol (fields[9]);
770         ps->vmem_majflt_counter = atol (fields[11]);
771
772         {
773                 unsigned long long stack_start = atoll (fields[27]);
774                 unsigned long long stack_ptr   = atoll (fields[28]);
775
776                 stack_size = (stack_start > stack_ptr)
777                         ? stack_start - stack_ptr
778                         : stack_ptr - stack_start;
779         }
780
781         /* Convert jiffies to useconds */
782         cpu_user_counter   = cpu_user_counter   * 1000000 / CONFIG_HZ;
783         cpu_system_counter = cpu_system_counter * 1000000 / CONFIG_HZ;
784         vmem_rss = vmem_rss * pagesize_g;
785
786         ps->cpu_user_counter = (unsigned long) cpu_user_counter;
787         ps->cpu_system_counter = (unsigned long) cpu_system_counter;
788         ps->vmem_size = (unsigned long) vmem_size;
789         ps->vmem_rss = (unsigned long) vmem_rss;
790         ps->stack_size = (unsigned long) stack_size;
791
792         /* success */
793         return (0);
794 } /* int ps_read_process (...) */
795
796 static char *ps_get_cmdline (pid_t pid, char *name, char *buf, size_t buf_len)
797 {
798         char  *buf_ptr;
799         size_t len;
800
801         char file[PATH_MAX];
802         int  fd;
803
804         size_t n;
805
806         if ((pid < 1) || (NULL == buf) || (buf_len < 2))
807                 return NULL;
808
809         ssnprintf (file, sizeof (file), "/proc/%u/cmdline", pid);
810
811         fd = open (file, O_RDONLY);
812         if (fd < 0) {
813                 char errbuf[4096];
814                 WARNING ("processes plugin: Failed to open `%s': %s.", file,
815                                 sstrerror (errno, errbuf, sizeof (errbuf)));
816                 return NULL;
817         }
818
819         buf_ptr = buf;
820         len     = buf_len;
821
822         n = 0;
823
824         while (42) {
825                 ssize_t status;
826
827                 status = read (fd, (void *)buf_ptr, len);
828
829                 if (status < 0) {
830                         char errbuf[4096];
831
832                         if ((EAGAIN == errno) || (EINTR == errno))
833                                 continue;
834
835                         WARNING ("processes plugin: Failed to read from `%s': %s.", file,
836                                         sstrerror (errno, errbuf, sizeof (errbuf)));
837                         close (fd);
838                         return NULL;
839                 }
840
841                 n += status;
842
843                 if (status == 0)
844                         break;
845
846                 buf_ptr += status;
847                 len     -= status;
848
849                 if (len <= 0)
850                         break;
851         }
852
853         close (fd);
854
855         if (0 == n) {
856                 /* cmdline not available; e.g. kernel thread, zombie */
857                 if (NULL == name)
858                         return NULL;
859
860                 ssnprintf (buf, buf_len, "[%s]", name);
861                 return buf;
862         }
863
864         assert (n <= buf_len);
865
866         if (n == buf_len)
867                 --n;
868         buf[n] = '\0';
869
870         --n;
871         /* remove trailing whitespace */
872         while ((n > 0) && (isspace (buf[n]) || ('\0' == buf[n]))) {
873                 buf[n] = '\0';
874                 --n;
875         }
876
877         /* arguments are separated by '\0' in /proc/<pid>/cmdline */
878         while (n > 0) {
879                 if ('\0' == buf[n])
880                         buf[n] = ' ';
881                 --n;
882         }
883         return buf;
884 } /* char *ps_get_cmdline (...) */
885 #endif /* KERNEL_LINUX */
886
887 #if HAVE_THREAD_INFO
888 static int mach_get_task_name (task_t t, int *pid, char *name, size_t name_max_len)
889 {
890         int mib[4];
891
892         struct kinfo_proc kp;
893         size_t            kp_size;
894
895         mib[0] = CTL_KERN;
896         mib[1] = KERN_PROC;
897         mib[2] = KERN_PROC_PID;
898
899         if (pid_for_task (t, pid) != KERN_SUCCESS)
900                 return (-1);
901         mib[3] = *pid;
902
903         kp_size = sizeof (kp);
904         if (sysctl (mib, 4, &kp, &kp_size, NULL, 0) != 0)
905                 return (-1);
906
907         if (name_max_len > (MAXCOMLEN + 1))
908                 name_max_len = MAXCOMLEN + 1;
909
910         strncpy (name, kp.kp_proc.p_comm, name_max_len - 1);
911         name[name_max_len - 1] = '\0';
912
913         DEBUG ("pid = %i; name = %s;", *pid, name);
914
915         /* We don't do the special handling for `p_comm == "LaunchCFMApp"' as
916          * `top' does it, because it is a lot of work and only used when
917          * debugging. -octo */
918
919         return (0);
920 }
921 #endif /* HAVE_THREAD_INFO */
922 /* ------- end of additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
923
924 /* do actual readings from kernel */
925 static int ps_read (void)
926 {
927 #if HAVE_THREAD_INFO
928         kern_return_t            status;
929
930         int                      pset;
931         processor_set_t          port_pset_priv;
932
933         int                      task;
934         task_array_t             task_list;
935         mach_msg_type_number_t   task_list_len;
936
937         int                      task_pid;
938         char                     task_name[MAXCOMLEN + 1];
939
940         int                      thread;
941         thread_act_array_t       thread_list;
942         mach_msg_type_number_t   thread_list_len;
943         thread_basic_info_data_t thread_data;
944         mach_msg_type_number_t   thread_data_len;
945
946         int running  = 0;
947         int sleeping = 0;
948         int zombies  = 0;
949         int stopped  = 0;
950         int blocked  = 0;
951
952         procstat_t *ps;
953         procstat_entry_t pse;
954
955         ps_list_reset ();
956
957         /*
958          * The Mach-concept is a little different from the traditional UNIX
959          * concept: All the work is done in threads. Threads are contained in
960          * `tasks'. Therefore, `task status' doesn't make much sense, since
961          * it's actually a `thread status'.
962          * Tasks are assigned to sets of processors, so that's where you go to
963          * get a list.
964          */
965         for (pset = 0; pset < pset_list_len; pset++)
966         {
967                 if ((status = host_processor_set_priv (port_host_self,
968                                                 pset_list[pset],
969                                                 &port_pset_priv)) != KERN_SUCCESS)
970                 {
971                         ERROR ("host_processor_set_priv failed: %s\n",
972                                         mach_error_string (status));
973                         continue;
974                 }
975
976                 if ((status = processor_set_tasks (port_pset_priv,
977                                                 &task_list,
978                                                 &task_list_len)) != KERN_SUCCESS)
979                 {
980                         ERROR ("processor_set_tasks failed: %s\n",
981                                         mach_error_string (status));
982                         mach_port_deallocate (port_task_self, port_pset_priv);
983                         continue;
984                 }
985
986                 for (task = 0; task < task_list_len; task++)
987                 {
988                         ps = NULL;
989                         if (mach_get_task_name (task_list[task],
990                                                 &task_pid,
991                                                 task_name, PROCSTAT_NAME_LEN) == 0)
992                         {
993                                 /* search for at least one match */
994                                 for (ps = list_head_g; ps != NULL; ps = ps->next)
995                                         /* FIXME: cmdline should be here instead of NULL */
996                                         if (ps_list_match (task_name, NULL, ps) == 1)
997                                                 break;
998                         }
999
1000                         /* Collect more detailed statistics for this process */
1001                         if (ps != NULL)
1002                         {
1003                                 task_basic_info_data_t        task_basic_info;
1004                                 mach_msg_type_number_t        task_basic_info_len;
1005                                 task_events_info_data_t       task_events_info;
1006                                 mach_msg_type_number_t        task_events_info_len;
1007                                 task_absolutetime_info_data_t task_absolutetime_info;
1008                                 mach_msg_type_number_t        task_absolutetime_info_len;
1009
1010                                 memset (&pse, '\0', sizeof (pse));
1011                                 pse.id = task_pid;
1012
1013                                 task_basic_info_len = TASK_BASIC_INFO_COUNT;
1014                                 status = task_info (task_list[task],
1015                                                 TASK_BASIC_INFO,
1016                                                 (task_info_t) &task_basic_info,
1017                                                 &task_basic_info_len);
1018                                 if (status != KERN_SUCCESS)
1019                                 {
1020                                         ERROR ("task_info failed: %s",
1021                                                         mach_error_string (status));
1022                                         continue; /* with next thread_list */
1023                                 }
1024
1025                                 task_events_info_len = TASK_EVENTS_INFO_COUNT;
1026                                 status = task_info (task_list[task],
1027                                                 TASK_EVENTS_INFO,
1028                                                 (task_info_t) &task_events_info,
1029                                                 &task_events_info_len);
1030                                 if (status != KERN_SUCCESS)
1031                                 {
1032                                         ERROR ("task_info failed: %s",
1033                                                         mach_error_string (status));
1034                                         continue; /* with next thread_list */
1035                                 }
1036
1037                                 task_absolutetime_info_len = TASK_ABSOLUTETIME_INFO_COUNT;
1038                                 status = task_info (task_list[task],
1039                                                 TASK_ABSOLUTETIME_INFO,
1040                                                 (task_info_t) &task_absolutetime_info,
1041                                                 &task_absolutetime_info_len);
1042                                 if (status != KERN_SUCCESS)
1043                                 {
1044                                         ERROR ("task_info failed: %s",
1045                                                         mach_error_string (status));
1046                                         continue; /* with next thread_list */
1047                                 }
1048
1049                                 pse.num_proc++;
1050                                 pse.vmem_rss = task_basic_info.resident_size;
1051
1052                                 pse.vmem_minflt_counter = task_events_info.cow_faults;
1053                                 pse.vmem_majflt_counter = task_events_info.faults;
1054
1055                                 pse.cpu_user_counter = task_absolutetime_info.total_user;
1056                                 pse.cpu_system_counter = task_absolutetime_info.total_system;
1057                         }
1058
1059                         status = task_threads (task_list[task], &thread_list,
1060                                         &thread_list_len);
1061                         if (status != KERN_SUCCESS)
1062                         {
1063                                 /* Apple's `top' treats this case a zombie. It
1064                                  * makes sense to some extend: A `zombie'
1065                                  * thread is nonsense, since the task/process
1066                                  * is dead. */
1067                                 zombies++;
1068                                 DEBUG ("task_threads failed: %s",
1069                                                 mach_error_string (status));
1070                                 if (task_list[task] != port_task_self)
1071                                         mach_port_deallocate (port_task_self,
1072                                                         task_list[task]);
1073                                 continue; /* with next task_list */
1074                         }
1075
1076                         for (thread = 0; thread < thread_list_len; thread++)
1077                         {
1078                                 thread_data_len = THREAD_BASIC_INFO_COUNT;
1079                                 status = thread_info (thread_list[thread],
1080                                                 THREAD_BASIC_INFO,
1081                                                 (thread_info_t) &thread_data,
1082                                                 &thread_data_len);
1083                                 if (status != KERN_SUCCESS)
1084                                 {
1085                                         ERROR ("thread_info failed: %s",
1086                                                         mach_error_string (status));
1087                                         if (task_list[task] != port_task_self)
1088                                                 mach_port_deallocate (port_task_self,
1089                                                                 thread_list[thread]);
1090                                         continue; /* with next thread_list */
1091                                 }
1092
1093                                 if (ps != NULL)
1094                                         pse.num_lwp++;
1095
1096                                 switch (thread_data.run_state)
1097                                 {
1098                                         case TH_STATE_RUNNING:
1099                                                 running++;
1100                                                 break;
1101                                         case TH_STATE_STOPPED:
1102                                         /* What exactly is `halted'? */
1103                                         case TH_STATE_HALTED:
1104                                                 stopped++;
1105                                                 break;
1106                                         case TH_STATE_WAITING:
1107                                                 sleeping++;
1108                                                 break;
1109                                         case TH_STATE_UNINTERRUPTIBLE:
1110                                                 blocked++;
1111                                                 break;
1112                                         /* There is no `zombie' case here,
1113                                          * since there are no zombie-threads.
1114                                          * There's only zombie tasks, which are
1115                                          * handled above. */
1116                                         default:
1117                                                 WARNING ("Unknown thread status: %i",
1118                                                                 thread_data.run_state);
1119                                                 break;
1120                                 } /* switch (thread_data.run_state) */
1121
1122                                 if (task_list[task] != port_task_self)
1123                                 {
1124                                         status = mach_port_deallocate (port_task_self,
1125                                                         thread_list[thread]);
1126                                         if (status != KERN_SUCCESS)
1127                                                 ERROR ("mach_port_deallocate failed: %s",
1128                                                                 mach_error_string (status));
1129                                 }
1130                         } /* for (thread_list) */
1131
1132                         if ((status = vm_deallocate (port_task_self,
1133                                                         (vm_address_t) thread_list,
1134                                                         thread_list_len * sizeof (thread_act_t)))
1135                                         != KERN_SUCCESS)
1136                         {
1137                                 ERROR ("vm_deallocate failed: %s",
1138                                                 mach_error_string (status));
1139                         }
1140                         thread_list = NULL;
1141                         thread_list_len = 0;
1142
1143                         /* Only deallocate the task port, if it isn't our own.
1144                          * Don't know what would happen in that case, but this
1145                          * is what Apple's top does.. ;) */
1146                         if (task_list[task] != port_task_self)
1147                         {
1148                                 status = mach_port_deallocate (port_task_self,
1149                                                 task_list[task]);
1150                                 if (status != KERN_SUCCESS)
1151                                         ERROR ("mach_port_deallocate failed: %s",
1152                                                         mach_error_string (status));
1153                         }
1154
1155                         if (ps != NULL)
1156                                 /* FIXME: cmdline should be here instead of NULL */
1157                                 ps_list_add (task_name, NULL, &pse);
1158                 } /* for (task_list) */
1159
1160                 if ((status = vm_deallocate (port_task_self,
1161                                 (vm_address_t) task_list,
1162                                 task_list_len * sizeof (task_t))) != KERN_SUCCESS)
1163                 {
1164                         ERROR ("vm_deallocate failed: %s",
1165                                         mach_error_string (status));
1166                 }
1167                 task_list = NULL;
1168                 task_list_len = 0;
1169
1170                 if ((status = mach_port_deallocate (port_task_self, port_pset_priv))
1171                                 != KERN_SUCCESS)
1172                 {
1173                         ERROR ("mach_port_deallocate failed: %s",
1174                                         mach_error_string (status));
1175                 }
1176         } /* for (pset_list) */
1177
1178         ps_submit_state ("running", running);
1179         ps_submit_state ("sleeping", sleeping);
1180         ps_submit_state ("zombies", zombies);
1181         ps_submit_state ("stopped", stopped);
1182         ps_submit_state ("blocked", blocked);
1183
1184         for (ps = list_head_g; ps != NULL; ps = ps->next)
1185                 ps_submit_proc_list (ps);
1186 /* #endif HAVE_THREAD_INFO */
1187
1188 #elif KERNEL_LINUX
1189         int running  = 0;
1190         int sleeping = 0;
1191         int zombies  = 0;
1192         int stopped  = 0;
1193         int paging   = 0;
1194         int blocked  = 0;
1195
1196         struct dirent *ent;
1197         DIR           *proc;
1198         int            pid;
1199
1200         char cmdline[ARG_MAX];
1201
1202         int        status;
1203         procstat_t ps;
1204         procstat_entry_t pse;
1205         char       state;
1206
1207         procstat_t *ps_ptr;
1208
1209         running = sleeping = zombies = stopped = paging = blocked = 0;
1210         ps_list_reset ();
1211
1212         if ((proc = opendir ("/proc")) == NULL)
1213         {
1214                 char errbuf[1024];
1215                 ERROR ("Cannot open `/proc': %s",
1216                                 sstrerror (errno, errbuf, sizeof (errbuf)));
1217                 return (-1);
1218         }
1219
1220         while ((ent = readdir (proc)) != NULL)
1221         {
1222                 if (!isdigit (ent->d_name[0]))
1223                         continue;
1224
1225                 if ((pid = atoi (ent->d_name)) < 1)
1226                         continue;
1227
1228                 status = ps_read_process (pid, &ps, &state);
1229                 if (status != 0)
1230                 {
1231                         DEBUG ("ps_read_process failed: %i", status);
1232                         continue;
1233                 }
1234
1235                 pse.id       = pid;
1236                 pse.age      = 0;
1237
1238                 pse.num_proc   = ps.num_proc;
1239                 pse.num_lwp    = ps.num_lwp;
1240                 pse.vmem_size  = ps.vmem_size;
1241                 pse.vmem_rss   = ps.vmem_rss;
1242                 pse.stack_size = ps.stack_size;
1243
1244                 pse.vmem_minflt = 0;
1245                 pse.vmem_minflt_counter = ps.vmem_minflt_counter;
1246                 pse.vmem_majflt = 0;
1247                 pse.vmem_majflt_counter = ps.vmem_majflt_counter;
1248
1249                 pse.cpu_user = 0;
1250                 pse.cpu_user_counter = ps.cpu_user_counter;
1251                 pse.cpu_system = 0;
1252                 pse.cpu_system_counter = ps.cpu_system_counter;
1253
1254                 switch (state)
1255                 {
1256                         case 'R': running++;  break;
1257                         case 'S': sleeping++; break;
1258                         case 'D': blocked++;  break;
1259                         case 'Z': zombies++;  break;
1260                         case 'T': stopped++;  break;
1261                         case 'W': paging++;   break;
1262                 }
1263
1264                 ps_list_add (ps.name,
1265                                 ps_get_cmdline (pid, ps.name, cmdline, sizeof (cmdline)),
1266                                 &pse);
1267         }
1268
1269         closedir (proc);
1270
1271         ps_submit_state ("running",  running);
1272         ps_submit_state ("sleeping", sleeping);
1273         ps_submit_state ("zombies",  zombies);
1274         ps_submit_state ("stopped",  stopped);
1275         ps_submit_state ("paging",   paging);
1276         ps_submit_state ("blocked",  blocked);
1277
1278         for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1279                 ps_submit_proc_list (ps_ptr);
1280 /* #endif KERNEL_LINUX */
1281
1282 #elif HAVE_LIBKVM_GETPROCS
1283         int running  = 0;
1284         int sleeping = 0;
1285         int zombies  = 0;
1286         int stopped  = 0;
1287         int blocked  = 0;
1288         int idle     = 0;
1289         int wait     = 0;
1290
1291         kvm_t *kd;
1292         char errbuf[1024];
1293         char cmdline[ARG_MAX];
1294         char *cmdline_ptr;
1295         struct kinfo_proc *procs;          /* array of processes */
1296         char **argv;
1297         int count;                         /* returns number of processes */
1298         int i;
1299
1300         procstat_t *ps_ptr;
1301         procstat_entry_t pse;
1302
1303         ps_list_reset ();
1304
1305         /* Open the kvm interface, get a descriptor */
1306         kd = kvm_open (NULL, NULL, NULL, 0, errbuf);
1307         if (kd == NULL)
1308         {
1309                 ERROR ("processes plugin: Cannot open kvm interface: %s",
1310                                 errbuf);
1311                 return (0);
1312         }
1313
1314         /* Get the list of processes. */
1315         procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, &count);
1316         if (procs == NULL)
1317         {
1318                 kvm_close (kd);
1319                 ERROR ("processes plugin: Cannot get kvm processes list: %s",
1320                                 kvm_geterr(kd));
1321                 return (0);
1322         }
1323
1324         /* Iterate through the processes in kinfo_proc */
1325         for (i = 0; i < count; i++)
1326         {
1327                 /* retrieve the arguments */
1328                 cmdline[0] = 0;
1329                 cmdline_ptr = NULL;
1330
1331                 argv = kvm_getargv (kd, (const struct kinfo_proc *) &(procs[i]), 0);
1332                 if (argv != NULL)
1333                 {
1334                         int status;
1335                         int argc;
1336
1337                         argc = 0;
1338                         while (argv[argc] != NULL)
1339                                 argc++;
1340
1341                         status = strjoin (cmdline, sizeof (cmdline),
1342                                         argv, argc, " ");
1343
1344                         if (status < 0)
1345                         {
1346                                 WARNING ("processes plugin: Command line did "
1347                                                 "not fit into buffer.");
1348                         }
1349                         else
1350                         {
1351                                 cmdline_ptr = &cmdline[0];
1352                         }
1353                 }
1354
1355                 pse.id       = procs[i].ki_pid;
1356                 pse.age      = 0;
1357
1358                 pse.num_proc = 1;
1359                 pse.num_lwp  = procs[i].ki_numthreads;
1360
1361                 /* pse.vmem_size = procs[i].ki_size; */
1362                 pse.vmem_rss = procs[i].ki_rssize * getpagesize();
1363                 /* pse.stack_size = procs[i].ki_ssize * getpagesize(); */
1364                 pse.vmem_minflt = 0;
1365                 pse.vmem_minflt_counter = procs[i].ki_rusage.ru_minflt;
1366                 pse.vmem_majflt = 0;
1367                 pse.vmem_majflt_counter = procs[i].ki_rusage.ru_majflt;
1368
1369                 pse.cpu_user = 0;
1370                 pse.cpu_user_counter = procs[i].ki_rusage.ru_utime.tv_sec
1371                         * 1000
1372                         + procs[i].ki_rusage.ru_utime.tv_usec;
1373                 pse.cpu_system = 0;
1374                 pse.cpu_system_counter = procs[i].ki_rusage.ru_stime.tv_sec
1375                         * 1000
1376                         + procs[i].ki_rusage.ru_stime.tv_usec;
1377
1378                 switch (procs[i].ki_stat)
1379                 {
1380                         case SSTOP:     stopped++;      break;
1381                         case SSLEEP:    sleeping++;     break;
1382                         case SRUN:      running++;      break;
1383                         case SIDL:      idle++;         break;
1384                         case SWAIT:     wait++;         break;
1385                         case SLOCK:     blocked++;      break;
1386                         case SZOMB:     zombies++;      break;
1387                 }
1388
1389                 ps_list_add (procs[i].ki_comm, cmdline_ptr, &pse);
1390         }
1391
1392         kvm_close(kd);
1393
1394         ps_submit_state ("running",  running);
1395         ps_submit_state ("sleeping", sleeping);
1396         ps_submit_state ("zombies",  zombies);
1397         ps_submit_state ("stopped",  stopped);
1398         ps_submit_state ("blocked",  blocked);
1399         ps_submit_state ("idle",     idle);
1400         ps_submit_state ("wait",     wait);
1401
1402         for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1403                 ps_submit_proc_list (ps_ptr);
1404 #endif /* HAVE_LIBKVM_GETPROCS */
1405
1406         return (0);
1407 } /* int ps_read */
1408
1409 void module_register (void)
1410 {
1411         plugin_register_config ("processes", ps_config,
1412                         config_keys, config_keys_num);
1413         plugin_register_init ("processes", ps_init);
1414         plugin_register_read ("processes", ps_read);
1415 } /* void module_register */