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