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 * Copyright (C) 2009 Manuel Sanmartin
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 * Lyonel Vincent <lyonel at ezix.org>
25 * Florian octo Forster <octo at verplant.org>
26 * Oleg King <king2 at kaluga.ru>
27 * Sebastian Harl <sh at tokkee.org>
34 #include "configfile.h"
36 /* Include header files for the mach system, if they exist.. */
38 # if HAVE_MACH_MACH_INIT_H
39 # include <mach/mach_init.h>
41 # if HAVE_MACH_HOST_PRIV_H
42 # include <mach/host_priv.h>
44 # if HAVE_MACH_MACH_ERROR_H
45 # include <mach/mach_error.h>
47 # if HAVE_MACH_MACH_HOST_H
48 # include <mach/mach_host.h>
50 # if HAVE_MACH_MACH_PORT_H
51 # include <mach/mach_port.h>
53 # if HAVE_MACH_MACH_TYPES_H
54 # include <mach/mach_types.h>
56 # if HAVE_MACH_MESSAGE_H
57 # include <mach/message.h>
59 # if HAVE_MACH_PROCESSOR_SET_H
60 # include <mach/processor_set.h>
63 # include <mach/task.h>
65 # if HAVE_MACH_THREAD_ACT_H
66 # include <mach/thread_act.h>
68 # if HAVE_MACH_VM_REGION_H
69 # include <mach/vm_region.h>
71 # if HAVE_MACH_VM_MAP_H
72 # include <mach/vm_map.h>
74 # if HAVE_MACH_VM_PROT_H
75 # include <mach/vm_prot.h>
77 # if HAVE_SYS_SYSCTL_H
78 # include <sys/sysctl.h>
80 /* #endif HAVE_THREAD_INFO */
83 # if HAVE_LINUX_CONFIG_H
84 # include <linux/config.h>
87 # define CONFIG_HZ 100
89 /* #endif KERNEL_LINUX */
91 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD
93 # include <sys/param.h>
94 # include <sys/sysctl.h>
95 # include <sys/user.h>
96 # include <sys/proc.h>
97 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
100 # include <procinfo.h>
101 # include <sys/types.h>
103 #define MAXPROCENTRY 32
104 #define MAXTHRDENTRY 16
105 #define MAXARGLN 1024
106 /* #endif HAVE_PROCINFO_H */
109 # error "No applicable input method."
117 # define ARG_MAX 4096
122 static const char *config_keys[] =
127 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
129 typedef struct procstat_entry_s
134 unsigned long num_proc;
135 unsigned long num_lwp;
136 unsigned long vmem_size;
137 unsigned long vmem_rss;
138 unsigned long stack_size;
140 unsigned long vmem_minflt;
141 unsigned long vmem_majflt;
142 unsigned long vmem_minflt_counter;
143 unsigned long vmem_majflt_counter;
145 unsigned long cpu_user;
146 unsigned long cpu_system;
147 unsigned long cpu_user_counter;
148 unsigned long cpu_system_counter;
156 struct procstat_entry_s *next;
159 #define PROCSTAT_NAME_LEN 256
160 typedef struct procstat
162 char name[PROCSTAT_NAME_LEN];
167 unsigned long num_proc;
168 unsigned long num_lwp;
169 unsigned long vmem_size;
170 unsigned long vmem_rss;
171 unsigned long stack_size;
173 unsigned long vmem_minflt_counter;
174 unsigned long vmem_majflt_counter;
176 unsigned long cpu_user_counter;
177 unsigned long cpu_system_counter;
185 struct procstat *next;
186 struct procstat_entry_s *instances;
189 static procstat_t *list_head_g = NULL;
192 static mach_port_t port_host_self;
193 static mach_port_t port_task_self;
195 static processor_set_name_array_t pset_list;
196 static mach_msg_type_number_t pset_list_len;
197 /* #endif HAVE_THREAD_INFO */
200 static long pagesize_g;
201 /* #endif KERNEL_LINUX */
203 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD
204 /* no global variables */
205 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
207 #elif HAVE_PROCINFO_H
208 static struct procentry64 procentry[MAXPROCENTRY];
209 static struct thrdentry64 thrdentry[MAXTHRDENTRY];
212 #ifndef _AIXVERSION_610
213 int getprocs64 (void *procsinfo, int sizproc, void *fdsinfo, int sizfd, pid_t *index, int count);
214 int getthrds64( pid_t, void *, int, tid64_t *, int );
216 int getargs (struct procentry64 *processBuffer, int bufferLen, char *argsBuffer, int argsLen);
217 #endif /* HAVE_PROCINFO_H */
219 /* put name of process from config to list_head_g tree
220 list_head_g is a list of 'procstat_t' structs with
221 processes names we want to watch */
222 static void ps_list_register (const char *name, const char *regexp)
228 new = (procstat_t *) malloc (sizeof (procstat_t));
231 ERROR ("processes plugin: ps_list_register: malloc failed.");
234 memset (new, 0, sizeof (procstat_t));
235 sstrncpy (new->name, name, sizeof (new->name));
240 DEBUG ("ProcessMatch: adding \"%s\" as criteria to process %s.", regexp, name);
241 new->re = (regex_t *) malloc (sizeof (regex_t));
244 ERROR ("processes plugin: ps_list_register: malloc failed.");
249 status = regcomp (new->re, regexp, REG_EXTENDED | REG_NOSUB);
252 DEBUG ("ProcessMatch: compiling the regular expression \"%s\" failed.", regexp);
260 ERROR ("processes plugin: ps_list_register: "
261 "Regular expression \"%s\" found in config "
262 "file, but support for regular expressions "
263 "has been dispabled at compile time.",
270 for (ptr = list_head_g; ptr != NULL; ptr = ptr->next)
272 if (strcmp (ptr->name, name) == 0)
274 WARNING ("processes plugin: You have configured more "
275 "than one `Process' or "
276 "`ProcessMatch' with the same name. "
277 "All but the first setting will be "
284 if (ptr->next == NULL)
292 } /* void ps_list_register */
294 /* try to match name against entry, returns 1 if success */
295 static int ps_list_match (const char *name, const char *cmdline, procstat_t *ps)
304 if ((str == NULL) || (str[0] == 0))
307 assert (str != NULL);
309 status = regexec (ps->re, str,
318 if (strcmp (ps->name, name) == 0)
322 } /* int ps_list_match */
324 /* add process entry to 'instances' of process 'name' (or refresh it) */
325 static void ps_list_add (const char *name, const char *cmdline, procstat_entry_t *entry)
328 procstat_entry_t *pse;
333 for (ps = list_head_g; ps != NULL; ps = ps->next)
335 if ((ps_list_match (name, cmdline, ps)) == 0)
338 for (pse = ps->instances; pse != NULL; pse = pse->next)
339 if ((pse->id == entry->id) || (pse->next == NULL))
342 if ((pse == NULL) || (pse->id != entry->id))
344 procstat_entry_t *new;
346 new = (procstat_entry_t *) malloc (sizeof (procstat_entry_t));
349 memset (new, 0, sizeof (procstat_entry_t));
361 pse->num_proc = entry->num_proc;
362 pse->num_lwp = entry->num_lwp;
363 pse->vmem_size = entry->vmem_size;
364 pse->vmem_rss = entry->vmem_rss;
365 pse->stack_size = entry->stack_size;
366 pse->io_rchar = entry->io_rchar;
367 pse->io_wchar = entry->io_wchar;
368 pse->io_syscr = entry->io_syscr;
369 pse->io_syscw = entry->io_syscw;
371 ps->num_proc += pse->num_proc;
372 ps->num_lwp += pse->num_lwp;
373 ps->vmem_size += pse->vmem_size;
374 ps->vmem_rss += pse->vmem_rss;
375 ps->stack_size += pse->stack_size;
377 ps->io_rchar += ((pse->io_rchar == -1)?0:pse->io_rchar);
378 ps->io_wchar += ((pse->io_wchar == -1)?0:pse->io_wchar);
379 ps->io_syscr += ((pse->io_syscr == -1)?0:pse->io_syscr);
380 ps->io_syscw += ((pse->io_syscw == -1)?0:pse->io_syscw);
382 if ((entry->vmem_minflt_counter == 0)
383 && (entry->vmem_majflt_counter == 0))
385 pse->vmem_minflt_counter += entry->vmem_minflt;
386 pse->vmem_minflt = entry->vmem_minflt;
388 pse->vmem_majflt_counter += entry->vmem_majflt;
389 pse->vmem_majflt = entry->vmem_majflt;
393 if (entry->vmem_minflt_counter < pse->vmem_minflt_counter)
395 pse->vmem_minflt = entry->vmem_minflt_counter
396 + (ULONG_MAX - pse->vmem_minflt_counter);
400 pse->vmem_minflt = entry->vmem_minflt_counter - pse->vmem_minflt_counter;
402 pse->vmem_minflt_counter = entry->vmem_minflt_counter;
404 if (entry->vmem_majflt_counter < pse->vmem_majflt_counter)
406 pse->vmem_majflt = entry->vmem_majflt_counter
407 + (ULONG_MAX - pse->vmem_majflt_counter);
411 pse->vmem_majflt = entry->vmem_majflt_counter - pse->vmem_majflt_counter;
413 pse->vmem_majflt_counter = entry->vmem_majflt_counter;
416 ps->vmem_minflt_counter += pse->vmem_minflt;
417 ps->vmem_majflt_counter += pse->vmem_majflt;
419 if ((entry->cpu_user_counter == 0)
420 && (entry->cpu_system_counter == 0))
422 pse->cpu_user_counter += entry->cpu_user;
423 pse->cpu_user = entry->cpu_user;
425 pse->cpu_system_counter += entry->cpu_system;
426 pse->cpu_system = entry->cpu_system;
430 if (entry->cpu_user_counter < pse->cpu_user_counter)
432 pse->cpu_user = entry->cpu_user_counter
433 + (ULONG_MAX - pse->cpu_user_counter);
437 pse->cpu_user = entry->cpu_user_counter - pse->cpu_user_counter;
439 pse->cpu_user_counter = entry->cpu_user_counter;
441 if (entry->cpu_system_counter < pse->cpu_system_counter)
443 pse->cpu_system = entry->cpu_system_counter
444 + (ULONG_MAX - pse->cpu_system_counter);
448 pse->cpu_system = entry->cpu_system_counter - pse->cpu_system_counter;
450 pse->cpu_system_counter = entry->cpu_system_counter;
453 ps->cpu_user_counter += pse->cpu_user;
454 ps->cpu_system_counter += pse->cpu_system;
458 /* remove old entries from instances of processes in list_head_g */
459 static void ps_list_reset (void)
462 procstat_entry_t *pse;
463 procstat_entry_t *pse_prev;
465 for (ps = list_head_g; ps != NULL; ps = ps->next)
483 DEBUG ("Removing this procstat entry cause it's too old: "
484 "id = %lu; name = %s;",
487 if (pse_prev == NULL)
489 ps->instances = pse->next;
495 pse_prev->next = pse->next;
497 pse = pse_prev->next;
506 } /* while (pse != NULL) */
507 } /* for (ps = list_head_g; ps != NULL; ps = ps->next) */
510 /* put all pre-defined 'Process' names from config to list_head_g tree */
511 static int ps_config (const char *key, const char *value)
513 if (strcasecmp (key, "Process") == 0)
515 ps_list_register (value, NULL);
517 else if (strcasecmp (key, "ProcessMatch") == 0)
523 new_val = strdup (value);
524 if (new_val == NULL) {
525 ERROR ("processes plugin: strdup failed when processing "
526 "`ProcessMatch %s'.", value);
530 fields_num = strsplit (new_val, fields,
531 STATIC_ARRAY_SIZE (fields));
534 ERROR ("processes plugin: `ProcessMatch' needs exactly "
535 "two string arguments.");
539 ps_list_register (fields[0], fields[1]);
544 ERROR ("processes plugin: The `%s' configuration option is not "
545 "understood and will be ignored.", key);
552 static int ps_init (void)
555 kern_return_t status;
557 port_host_self = mach_host_self ();
558 port_task_self = mach_task_self ();
560 if (pset_list != NULL)
562 vm_deallocate (port_task_self,
563 (vm_address_t) pset_list,
564 pset_list_len * sizeof (processor_set_t));
569 if ((status = host_processor_sets (port_host_self,
571 &pset_list_len)) != KERN_SUCCESS)
573 ERROR ("host_processor_sets failed: %s\n",
574 mach_error_string (status));
579 /* #endif HAVE_THREAD_INFO */
582 pagesize_g = sysconf(_SC_PAGESIZE);
583 DEBUG ("pagesize_g = %li; CONFIG_HZ = %i;",
584 pagesize_g, CONFIG_HZ);
585 /* #endif KERNEL_LINUX */
587 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD
588 /* no initialization */
589 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
591 #elif HAVE_PROCINFO_H
592 pagesize = getpagesize();
593 #endif /* HAVE_PROCINFO_H */
598 /* submit global state (e.g.: qty of zombies, running, etc..) */
599 static void ps_submit_state (const char *state, double value)
602 value_list_t vl = VALUE_LIST_INIT;
604 values[0].gauge = value;
608 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
609 sstrncpy (vl.plugin, "processes", sizeof (vl.plugin));
610 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
611 sstrncpy (vl.type, "ps_state", sizeof (vl.type));
612 sstrncpy (vl.type_instance, state, sizeof (vl.type_instance));
614 plugin_dispatch_values (&vl);
617 /* submit info about specific process (e.g.: memory taken, cpu usage, etc..) */
618 static void ps_submit_proc_list (procstat_t *ps)
621 value_list_t vl = VALUE_LIST_INIT;
625 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
626 sstrncpy (vl.plugin, "processes", sizeof (vl.plugin));
627 sstrncpy (vl.plugin_instance, ps->name, sizeof (vl.plugin_instance));
629 sstrncpy (vl.type, "ps_vm", sizeof (vl.type));
630 vl.values[0].gauge = ps->vmem_size;
632 plugin_dispatch_values (&vl);
634 sstrncpy (vl.type, "ps_rss", sizeof (vl.type));
635 vl.values[0].gauge = ps->vmem_rss;
637 plugin_dispatch_values (&vl);
639 sstrncpy (vl.type, "ps_stacksize", sizeof (vl.type));
640 vl.values[0].gauge = ps->stack_size;
642 plugin_dispatch_values (&vl);
644 sstrncpy (vl.type, "ps_cputime", sizeof (vl.type));
645 vl.values[0].counter = ps->cpu_user_counter;
646 vl.values[1].counter = ps->cpu_system_counter;
648 plugin_dispatch_values (&vl);
650 sstrncpy (vl.type, "ps_count", sizeof (vl.type));
651 vl.values[0].gauge = ps->num_proc;
652 vl.values[1].gauge = ps->num_lwp;
654 plugin_dispatch_values (&vl);
656 sstrncpy (vl.type, "ps_pagefaults", sizeof (vl.type));
657 vl.values[0].counter = ps->vmem_minflt_counter;
658 vl.values[1].counter = ps->vmem_majflt_counter;
660 plugin_dispatch_values (&vl);
662 if ( (ps->io_rchar != -1) && (ps->io_wchar != -1) )
664 sstrncpy (vl.type, "ps_disk_octets", sizeof (vl.type));
665 vl.values[0].counter = ps->io_rchar;
666 vl.values[1].counter = ps->io_wchar;
668 plugin_dispatch_values (&vl);
671 if ( (ps->io_syscr != -1) && (ps->io_syscw != -1) )
673 sstrncpy (vl.type, "ps_disk_ops", sizeof (vl.type));
674 vl.values[0].counter = ps->io_syscr;
675 vl.values[1].counter = ps->io_syscw;
677 plugin_dispatch_values (&vl);
680 DEBUG ("name = %s; num_proc = %lu; num_lwp = %lu; vmem_rss = %lu; "
681 "vmem_minflt_counter = %lu; vmem_majflt_counter = %lu; "
682 "cpu_user_counter = %lu; cpu_system_counter = %lu; "
683 "io_rchar = %ld; io_wchar = %ld; "
684 "io_syscr = %ld; io_syscw = %ld;",
685 ps->name, ps->num_proc, ps->num_lwp, ps->vmem_rss,
686 ps->vmem_minflt_counter, ps->vmem_majflt_counter,
687 ps->cpu_user_counter, ps->cpu_system_counter,
688 ps->io_rchar, ps->io_wchar, ps->io_syscr, ps->io_syscw);
689 } /* void ps_submit_proc_list */
691 /* ------- additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
693 static int ps_read_tasks (int pid)
700 ssnprintf (dirname, sizeof (dirname), "/proc/%i/task", pid);
702 if ((dh = opendir (dirname)) == NULL)
704 DEBUG ("Failed to open directory `%s'", dirname);
708 while ((ent = readdir (dh)) != NULL)
710 if (!isdigit ((int) ent->d_name[0]))
717 return ((count >= 1) ? count : 1);
718 } /* int *ps_read_tasks */
720 static procstat_t *ps_read_io (int pid, procstat_t *ps)
729 ssnprintf (filename, sizeof (filename), "/proc/%i/io", pid);
730 if ((fh = fopen (filename, "r")) == NULL)
733 while (fgets (buffer, 1024, fh) != NULL)
737 if (strncasecmp (buffer, "rchar:", 6) == 0)
738 val = &(ps->io_rchar);
739 else if (strncasecmp (buffer, "wchar:", 6) == 0)
740 val = &(ps->io_wchar);
741 else if (strncasecmp (buffer, "syscr:", 6) == 0)
742 val = &(ps->io_syscr);
743 else if (strncasecmp (buffer, "syscw:", 6) == 0)
744 val = &(ps->io_syscw);
748 numfields = strsplit (buffer, fields, 8);
753 *val = atol (fields[1]);
759 WARNING ("processes: fclose: %s",
760 sstrerror (errno, errbuf, sizeof (errbuf)));
764 } /* procstat_t *ps_read_io */
766 int ps_read_process (int pid, procstat_t *ps, char *state)
779 long long unsigned cpu_user_counter;
780 long long unsigned cpu_system_counter;
781 long long unsigned vmem_size;
782 long long unsigned vmem_rss;
783 long long unsigned stack_size;
785 memset (ps, 0, sizeof (procstat_t));
787 ssnprintf (filename, sizeof (filename), "/proc/%i/stat", pid);
789 i = read_file_contents (filename, buffer, sizeof(buffer) - 1);
794 fields_len = strsplit (buffer, fields, 64);
797 DEBUG ("processes plugin: ps_read_process (pid = %i):"
798 " `%s' has only %i fields..",
799 (int) pid, filename, fields_len);
803 /* copy the name, strip brackets in the process */
804 name_len = strlen (fields[1]) - 2;
805 if ((fields[1][0] != '(') || (fields[1][name_len + 1] != ')'))
807 DEBUG ("No brackets found in process name: `%s'", fields[1]);
810 fields[1] = fields[1] + 1;
811 fields[1][name_len] = '\0';
812 strncpy (ps->name, fields[1], PROCSTAT_NAME_LEN);
814 ppid = atoi (fields[3]);
816 *state = fields[2][0];
825 if ( (ps->num_lwp = ps_read_tasks (pid)) == -1 )
827 /* returns -1 => kernel 2.4 */
833 /* Leave the rest at zero if this is only a zombi */
834 if (ps->num_proc == 0)
836 DEBUG ("processes plugin: This is only a zombi: pid = %i; "
837 "name = %s;", pid, ps->name);
841 cpu_user_counter = atoll (fields[13]);
842 cpu_system_counter = atoll (fields[14]);
843 vmem_size = atoll (fields[22]);
844 vmem_rss = atoll (fields[23]);
845 ps->vmem_minflt_counter = atol (fields[9]);
846 ps->vmem_majflt_counter = atol (fields[11]);
849 unsigned long long stack_start = atoll (fields[27]);
850 unsigned long long stack_ptr = atoll (fields[28]);
852 stack_size = (stack_start > stack_ptr)
853 ? stack_start - stack_ptr
854 : stack_ptr - stack_start;
857 /* Convert jiffies to useconds */
858 cpu_user_counter = cpu_user_counter * 1000000 / CONFIG_HZ;
859 cpu_system_counter = cpu_system_counter * 1000000 / CONFIG_HZ;
860 vmem_rss = vmem_rss * pagesize_g;
862 ps->cpu_user_counter = (unsigned long) cpu_user_counter;
863 ps->cpu_system_counter = (unsigned long) cpu_system_counter;
864 ps->vmem_size = (unsigned long) vmem_size;
865 ps->vmem_rss = (unsigned long) vmem_rss;
866 ps->stack_size = (unsigned long) stack_size;
868 if ( (ps_read_io (pid, ps)) == NULL)
876 DEBUG("ps_read_process: not get io data for pid %i",pid);
881 } /* int ps_read_process (...) */
883 static char *ps_get_cmdline (pid_t pid, char *name, char *buf, size_t buf_len)
893 if ((pid < 1) || (NULL == buf) || (buf_len < 2))
896 ssnprintf (file, sizeof (file), "/proc/%u/cmdline", pid);
898 fd = open (file, O_RDONLY);
901 WARNING ("processes plugin: Failed to open `%s': %s.", file,
902 sstrerror (errno, errbuf, sizeof (errbuf)));
914 status = read (fd, (void *)buf_ptr, len);
919 if ((EAGAIN == errno) || (EINTR == errno))
922 WARNING ("processes plugin: Failed to read from `%s': %s.", file,
923 sstrerror (errno, errbuf, sizeof (errbuf)));
943 /* cmdline not available; e.g. kernel thread, zombie */
947 ssnprintf (buf, buf_len, "[%s]", name);
951 assert (n <= buf_len);
958 /* remove trailing whitespace */
959 while ((n > 0) && (isspace (buf[n]) || ('\0' == buf[n]))) {
964 /* arguments are separated by '\0' in /proc/<pid>/cmdline */
971 } /* char *ps_get_cmdline (...) */
973 static unsigned long read_fork_rate ()
977 unsigned long result = 0;
981 proc_stat = fopen("/proc/stat", "r");
982 if (proc_stat == NULL) {
984 ERROR ("processes plugin: fopen (/proc/stat) failed: %s",
985 sstrerror (errno, errbuf, sizeof (errbuf)));
989 while (fgets (buf, sizeof(buf), proc_stat) != NULL)
993 numfields = strsplit(buf, fields, STATIC_ARRAY_SIZE (fields));
997 if (strcmp ("processes", fields[0]) != 0)
1002 result = strtoul(fields[1], &endptr, 10);
1003 if ((endptr == fields[1]) || (errno != 0)) {
1004 ERROR ("processes plugin: Cannot parse fork rate: %s",
1018 static void ps_submit_fork_rate (unsigned long value)
1021 value_list_t vl = VALUE_LIST_INIT;
1023 values[0].derive = (derive_t) value;
1027 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
1028 sstrncpy (vl.plugin, "processes", sizeof (vl.plugin));
1029 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
1030 sstrncpy (vl.type, "fork_rate", sizeof (vl.type));
1031 sstrncpy (vl.type_instance, "", sizeof (vl.type_instance));
1033 plugin_dispatch_values (&vl);
1036 #endif /* KERNEL_LINUX */
1038 #if HAVE_THREAD_INFO
1039 static int mach_get_task_name (task_t t, int *pid, char *name, size_t name_max_len)
1043 struct kinfo_proc kp;
1048 mib[2] = KERN_PROC_PID;
1050 if (pid_for_task (t, pid) != KERN_SUCCESS)
1054 kp_size = sizeof (kp);
1055 if (sysctl (mib, 4, &kp, &kp_size, NULL, 0) != 0)
1058 if (name_max_len > (MAXCOMLEN + 1))
1059 name_max_len = MAXCOMLEN + 1;
1061 strncpy (name, kp.kp_proc.p_comm, name_max_len - 1);
1062 name[name_max_len - 1] = '\0';
1064 DEBUG ("pid = %i; name = %s;", *pid, name);
1066 /* We don't do the special handling for `p_comm == "LaunchCFMApp"' as
1067 * `top' does it, because it is a lot of work and only used when
1068 * debugging. -octo */
1072 #endif /* HAVE_THREAD_INFO */
1073 /* ------- end of additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
1075 /* do actual readings from kernel */
1076 static int ps_read (void)
1078 #if HAVE_THREAD_INFO
1079 kern_return_t status;
1082 processor_set_t port_pset_priv;
1085 task_array_t task_list;
1086 mach_msg_type_number_t task_list_len;
1089 char task_name[MAXCOMLEN + 1];
1092 thread_act_array_t thread_list;
1093 mach_msg_type_number_t thread_list_len;
1094 thread_basic_info_data_t thread_data;
1095 mach_msg_type_number_t thread_data_len;
1104 procstat_entry_t pse;
1109 * The Mach-concept is a little different from the traditional UNIX
1110 * concept: All the work is done in threads. Threads are contained in
1111 * `tasks'. Therefore, `task status' doesn't make much sense, since
1112 * it's actually a `thread status'.
1113 * Tasks are assigned to sets of processors, so that's where you go to
1116 for (pset = 0; pset < pset_list_len; pset++)
1118 if ((status = host_processor_set_priv (port_host_self,
1120 &port_pset_priv)) != KERN_SUCCESS)
1122 ERROR ("host_processor_set_priv failed: %s\n",
1123 mach_error_string (status));
1127 if ((status = processor_set_tasks (port_pset_priv,
1129 &task_list_len)) != KERN_SUCCESS)
1131 ERROR ("processor_set_tasks failed: %s\n",
1132 mach_error_string (status));
1133 mach_port_deallocate (port_task_self, port_pset_priv);
1137 for (task = 0; task < task_list_len; task++)
1140 if (mach_get_task_name (task_list[task],
1142 task_name, PROCSTAT_NAME_LEN) == 0)
1144 /* search for at least one match */
1145 for (ps = list_head_g; ps != NULL; ps = ps->next)
1146 /* FIXME: cmdline should be here instead of NULL */
1147 if (ps_list_match (task_name, NULL, ps) == 1)
1151 /* Collect more detailed statistics for this process */
1154 task_basic_info_data_t task_basic_info;
1155 mach_msg_type_number_t task_basic_info_len;
1156 task_events_info_data_t task_events_info;
1157 mach_msg_type_number_t task_events_info_len;
1158 task_absolutetime_info_data_t task_absolutetime_info;
1159 mach_msg_type_number_t task_absolutetime_info_len;
1161 memset (&pse, '\0', sizeof (pse));
1164 task_basic_info_len = TASK_BASIC_INFO_COUNT;
1165 status = task_info (task_list[task],
1167 (task_info_t) &task_basic_info,
1168 &task_basic_info_len);
1169 if (status != KERN_SUCCESS)
1171 ERROR ("task_info failed: %s",
1172 mach_error_string (status));
1173 continue; /* with next thread_list */
1176 task_events_info_len = TASK_EVENTS_INFO_COUNT;
1177 status = task_info (task_list[task],
1179 (task_info_t) &task_events_info,
1180 &task_events_info_len);
1181 if (status != KERN_SUCCESS)
1183 ERROR ("task_info failed: %s",
1184 mach_error_string (status));
1185 continue; /* with next thread_list */
1188 task_absolutetime_info_len = TASK_ABSOLUTETIME_INFO_COUNT;
1189 status = task_info (task_list[task],
1190 TASK_ABSOLUTETIME_INFO,
1191 (task_info_t) &task_absolutetime_info,
1192 &task_absolutetime_info_len);
1193 if (status != KERN_SUCCESS)
1195 ERROR ("task_info failed: %s",
1196 mach_error_string (status));
1197 continue; /* with next thread_list */
1201 pse.vmem_rss = task_basic_info.resident_size;
1203 pse.vmem_minflt_counter = task_events_info.cow_faults;
1204 pse.vmem_majflt_counter = task_events_info.faults;
1206 pse.cpu_user_counter = task_absolutetime_info.total_user;
1207 pse.cpu_system_counter = task_absolutetime_info.total_system;
1210 status = task_threads (task_list[task], &thread_list,
1212 if (status != KERN_SUCCESS)
1214 /* Apple's `top' treats this case a zombie. It
1215 * makes sense to some extend: A `zombie'
1216 * thread is nonsense, since the task/process
1219 DEBUG ("task_threads failed: %s",
1220 mach_error_string (status));
1221 if (task_list[task] != port_task_self)
1222 mach_port_deallocate (port_task_self,
1224 continue; /* with next task_list */
1227 for (thread = 0; thread < thread_list_len; thread++)
1229 thread_data_len = THREAD_BASIC_INFO_COUNT;
1230 status = thread_info (thread_list[thread],
1232 (thread_info_t) &thread_data,
1234 if (status != KERN_SUCCESS)
1236 ERROR ("thread_info failed: %s",
1237 mach_error_string (status));
1238 if (task_list[task] != port_task_self)
1239 mach_port_deallocate (port_task_self,
1240 thread_list[thread]);
1241 continue; /* with next thread_list */
1247 switch (thread_data.run_state)
1249 case TH_STATE_RUNNING:
1252 case TH_STATE_STOPPED:
1253 /* What exactly is `halted'? */
1254 case TH_STATE_HALTED:
1257 case TH_STATE_WAITING:
1260 case TH_STATE_UNINTERRUPTIBLE:
1263 /* There is no `zombie' case here,
1264 * since there are no zombie-threads.
1265 * There's only zombie tasks, which are
1268 WARNING ("Unknown thread status: %i",
1269 thread_data.run_state);
1271 } /* switch (thread_data.run_state) */
1273 if (task_list[task] != port_task_self)
1275 status = mach_port_deallocate (port_task_self,
1276 thread_list[thread]);
1277 if (status != KERN_SUCCESS)
1278 ERROR ("mach_port_deallocate failed: %s",
1279 mach_error_string (status));
1281 } /* for (thread_list) */
1283 if ((status = vm_deallocate (port_task_self,
1284 (vm_address_t) thread_list,
1285 thread_list_len * sizeof (thread_act_t)))
1288 ERROR ("vm_deallocate failed: %s",
1289 mach_error_string (status));
1292 thread_list_len = 0;
1294 /* Only deallocate the task port, if it isn't our own.
1295 * Don't know what would happen in that case, but this
1296 * is what Apple's top does.. ;) */
1297 if (task_list[task] != port_task_self)
1299 status = mach_port_deallocate (port_task_self,
1301 if (status != KERN_SUCCESS)
1302 ERROR ("mach_port_deallocate failed: %s",
1303 mach_error_string (status));
1307 /* FIXME: cmdline should be here instead of NULL */
1308 ps_list_add (task_name, NULL, &pse);
1309 } /* for (task_list) */
1311 if ((status = vm_deallocate (port_task_self,
1312 (vm_address_t) task_list,
1313 task_list_len * sizeof (task_t))) != KERN_SUCCESS)
1315 ERROR ("vm_deallocate failed: %s",
1316 mach_error_string (status));
1321 if ((status = mach_port_deallocate (port_task_self, port_pset_priv))
1324 ERROR ("mach_port_deallocate failed: %s",
1325 mach_error_string (status));
1327 } /* for (pset_list) */
1329 ps_submit_state ("running", running);
1330 ps_submit_state ("sleeping", sleeping);
1331 ps_submit_state ("zombies", zombies);
1332 ps_submit_state ("stopped", stopped);
1333 ps_submit_state ("blocked", blocked);
1335 for (ps = list_head_g; ps != NULL; ps = ps->next)
1336 ps_submit_proc_list (ps);
1337 /* #endif HAVE_THREAD_INFO */
1351 char cmdline[ARG_MAX];
1355 procstat_entry_t pse;
1358 unsigned long fork_rate;
1362 running = sleeping = zombies = stopped = paging = blocked = 0;
1365 if ((proc = opendir ("/proc")) == NULL)
1368 ERROR ("Cannot open `/proc': %s",
1369 sstrerror (errno, errbuf, sizeof (errbuf)));
1373 while ((ent = readdir (proc)) != NULL)
1375 if (!isdigit (ent->d_name[0]))
1378 if ((pid = atoi (ent->d_name)) < 1)
1381 status = ps_read_process (pid, &ps, &state);
1384 DEBUG ("ps_read_process failed: %i", status);
1391 pse.num_proc = ps.num_proc;
1392 pse.num_lwp = ps.num_lwp;
1393 pse.vmem_size = ps.vmem_size;
1394 pse.vmem_rss = ps.vmem_rss;
1395 pse.stack_size = ps.stack_size;
1397 pse.vmem_minflt = 0;
1398 pse.vmem_minflt_counter = ps.vmem_minflt_counter;
1399 pse.vmem_majflt = 0;
1400 pse.vmem_majflt_counter = ps.vmem_majflt_counter;
1403 pse.cpu_user_counter = ps.cpu_user_counter;
1405 pse.cpu_system_counter = ps.cpu_system_counter;
1407 pse.io_rchar = ps.io_rchar;
1408 pse.io_wchar = ps.io_wchar;
1409 pse.io_syscr = ps.io_syscr;
1410 pse.io_syscw = ps.io_syscw;
1414 case 'R': running++; break;
1415 case 'S': sleeping++; break;
1416 case 'D': blocked++; break;
1417 case 'Z': zombies++; break;
1418 case 'T': stopped++; break;
1419 case 'W': paging++; break;
1422 ps_list_add (ps.name,
1423 ps_get_cmdline (pid, ps.name, cmdline, sizeof (cmdline)),
1429 ps_submit_state ("running", running);
1430 ps_submit_state ("sleeping", sleeping);
1431 ps_submit_state ("zombies", zombies);
1432 ps_submit_state ("stopped", stopped);
1433 ps_submit_state ("paging", paging);
1434 ps_submit_state ("blocked", blocked);
1436 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1437 ps_submit_proc_list (ps_ptr);
1439 fork_rate = read_fork_rate();
1440 if (fork_rate != ULONG_MAX)
1441 ps_submit_fork_rate(fork_rate);
1442 /* #endif KERNEL_LINUX */
1444 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD
1455 char cmdline[ARG_MAX];
1457 struct kinfo_proc *procs; /* array of processes */
1459 int count; /* returns number of processes */
1463 procstat_entry_t pse;
1467 /* Open the kvm interface, get a descriptor */
1468 kd = kvm_open (NULL, NULL, NULL, 0, errbuf);
1471 ERROR ("processes plugin: Cannot open kvm interface: %s",
1476 /* Get the list of processes. */
1477 procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, &count);
1481 ERROR ("processes plugin: Cannot get kvm processes list: %s",
1486 /* Iterate through the processes in kinfo_proc */
1487 for (i = 0; i < count; i++)
1489 /* retrieve the arguments */
1493 argv = kvm_getargv (kd, (const struct kinfo_proc *) &(procs[i]), 0);
1500 while (argv[argc] != NULL)
1503 status = strjoin (cmdline, sizeof (cmdline),
1508 WARNING ("processes plugin: Command line did "
1509 "not fit into buffer.");
1513 cmdline_ptr = &cmdline[0];
1517 pse.id = procs[i].ki_pid;
1521 pse.num_lwp = procs[i].ki_numthreads;
1523 pse.vmem_size = procs[i].ki_size;
1524 pse.vmem_rss = procs[i].ki_rssize * getpagesize();
1525 pse.stack_size = procs[i].ki_ssize * getpagesize();
1526 pse.vmem_minflt = 0;
1527 pse.vmem_minflt_counter = procs[i].ki_rusage.ru_minflt;
1528 pse.vmem_majflt = 0;
1529 pse.vmem_majflt_counter = procs[i].ki_rusage.ru_majflt;
1532 pse.cpu_user_counter = procs[i].ki_rusage.ru_utime.tv_sec
1534 + procs[i].ki_rusage.ru_utime.tv_usec;
1536 pse.cpu_system_counter = procs[i].ki_rusage.ru_stime.tv_sec
1538 + procs[i].ki_rusage.ru_stime.tv_usec;
1546 switch (procs[i].ki_stat)
1548 case SSTOP: stopped++; break;
1549 case SSLEEP: sleeping++; break;
1550 case SRUN: running++; break;
1551 case SIDL: idle++; break;
1552 case SWAIT: wait++; break;
1553 case SLOCK: blocked++; break;
1554 case SZOMB: zombies++; break;
1557 ps_list_add (procs[i].ki_comm, cmdline_ptr, &pse);
1562 ps_submit_state ("running", running);
1563 ps_submit_state ("sleeping", sleeping);
1564 ps_submit_state ("zombies", zombies);
1565 ps_submit_state ("stopped", stopped);
1566 ps_submit_state ("blocked", blocked);
1567 ps_submit_state ("idle", idle);
1568 ps_submit_state ("wait", wait);
1570 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1571 ps_submit_proc_list (ps_ptr);
1572 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
1574 #elif HAVE_PROCINFO_H
1587 procstat_entry_t pse;
1590 while ((nprocs = getprocs64 (procentry, sizeof(struct procentry64),
1591 /* fdsinfo = */ NULL, sizeof(struct fdsinfo64),
1592 &pindex, MAXPROCENTRY)) > 0)
1596 for (i = 0; i < nprocs; i++)
1600 char arglist[MAXARGLN+1];
1604 if (procentry[i].pi_state == SNONE) continue;
1605 /* if (procentry[i].pi_state == SZOMB) FIXME */
1607 cmdline = procentry[i].pi_comm;
1608 cargs = procentry[i].pi_comm;
1609 if ( procentry[i].pi_flags & SKPROC )
1611 if (procentry[i].pi_pid == 0)
1612 cmdline = "swapper";
1617 if (getargs(&procentry[i], sizeof(struct procentry64), arglist, MAXARGLN) >= 0)
1622 while (++n < MAXARGLN)
1624 if (arglist[n] == '\0')
1626 if (arglist[n+1] == '\0')
1635 pse.id = procentry[i].pi_pid;
1637 pse.num_lwp = procentry[i].pi_thcount;
1641 while ((nthreads = getthrds64(procentry[i].pi_pid,
1642 thrdentry, sizeof(struct thrdentry64),
1643 &thindex, MAXTHRDENTRY)) > 0)
1647 for (j=0; j< nthreads; j++)
1649 switch (thrdentry[j].ti_state)
1651 /* case TSNONE: break; */
1652 case TSIDL: blocked++; break; /* FIXME is really blocked */
1653 case TSRUN: running++; break;
1654 case TSSLEEP: sleeping++; break;
1655 case TSSWAP: paging++; break;
1656 case TSSTOP: stopped++; break;
1657 case TSZOMB: zombies++; break;
1660 if (nthreads < MAXTHRDENTRY)
1665 /* tv_usec is nanosec ??? */
1666 pse.cpu_user_counter = procentry[i].pi_ru.ru_utime.tv_sec * 1000000 +
1667 procentry[i].pi_ru.ru_utime.tv_usec / 1000;
1670 /* tv_usec is nanosec ??? */
1671 pse.cpu_system_counter = procentry[i].pi_ru.ru_stime.tv_sec * 1000000 +
1672 procentry[i].pi_ru.ru_stime.tv_usec / 1000;
1674 pse.vmem_minflt = 0;
1675 pse.vmem_minflt_counter = procentry[i].pi_minflt;
1676 pse.vmem_majflt = 0;
1677 pse.vmem_majflt_counter = procentry[i].pi_majflt;
1679 pse.vmem_size = procentry[i].pi_tsize + procentry[i].pi_dvm * pagesize;
1680 pse.vmem_rss = (procentry[i].pi_drss + procentry[i].pi_trss) * pagesize;
1683 ps_list_add (cmdline, cargs, &pse);
1684 } /* for (i = 0 .. nprocs) */
1686 if (nprocs < MAXPROCENTRY)
1688 } /* while (getprocs64() > 0) */
1689 ps_submit_state ("running", running);
1690 ps_submit_state ("sleeping", sleeping);
1691 ps_submit_state ("zombies", zombies);
1692 ps_submit_state ("stopped", stopped);
1693 ps_submit_state ("paging", paging);
1694 ps_submit_state ("blocked", blocked);
1696 for (ps = list_head_g; ps != NULL; ps = ps->next)
1697 ps_submit_proc_list (ps);
1698 #endif /* HAVE_PROCINFO_H */
1703 void module_register (void)
1705 plugin_register_config ("processes", ps_config,
1706 config_keys, config_keys_num);
1707 plugin_register_init ("processes", ps_init);
1708 plugin_register_read ("processes", ps_read);
1709 } /* void module_register */