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 Andrés J. Díaz
8 * Copyright (C) 2009 Manuel Sanmartin
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 * Lyonel Vincent <lyonel at ezix.org>
26 * Florian octo Forster <octo at verplant.org>
27 * Oleg King <king2 at kaluga.ru>
28 * Sebastian Harl <sh at tokkee.org>
29 * Andrés J. Díaz <ajdiaz at connectical.com>
36 #include "configfile.h"
38 /* Include header files for the mach system, if they exist.. */
40 # if HAVE_MACH_MACH_INIT_H
41 # include <mach/mach_init.h>
43 # if HAVE_MACH_HOST_PRIV_H
44 # include <mach/host_priv.h>
46 # if HAVE_MACH_MACH_ERROR_H
47 # include <mach/mach_error.h>
49 # if HAVE_MACH_MACH_HOST_H
50 # include <mach/mach_host.h>
52 # if HAVE_MACH_MACH_PORT_H
53 # include <mach/mach_port.h>
55 # if HAVE_MACH_MACH_TYPES_H
56 # include <mach/mach_types.h>
58 # if HAVE_MACH_MESSAGE_H
59 # include <mach/message.h>
61 # if HAVE_MACH_PROCESSOR_SET_H
62 # include <mach/processor_set.h>
65 # include <mach/task.h>
67 # if HAVE_MACH_THREAD_ACT_H
68 # include <mach/thread_act.h>
70 # if HAVE_MACH_VM_REGION_H
71 # include <mach/vm_region.h>
73 # if HAVE_MACH_VM_MAP_H
74 # include <mach/vm_map.h>
76 # if HAVE_MACH_VM_PROT_H
77 # include <mach/vm_prot.h>
79 # if HAVE_SYS_SYSCTL_H
80 # include <sys/sysctl.h>
82 /* #endif HAVE_THREAD_INFO */
85 # if HAVE_LINUX_CONFIG_H
86 # include <linux/config.h>
89 # define CONFIG_HZ 100
91 /* #endif KERNEL_LINUX */
93 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD
95 # include <sys/param.h>
96 # include <sys/sysctl.h>
97 # include <sys/user.h>
98 # include <sys/proc.h>
99 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
101 #elif HAVE_PROCINFO_H
102 # include <procinfo.h>
103 # include <sys/types.h>
105 #define MAXPROCENTRY 32
106 #define MAXTHRDENTRY 16
107 #define MAXARGLN 1024
108 /* #endif HAVE_PROCINFO_H */
111 # error "No applicable input method."
119 # define ARG_MAX 4096
124 static const char *config_keys[] =
129 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
131 typedef struct procstat_entry_s
136 unsigned long num_proc;
137 unsigned long num_lwp;
138 unsigned long vmem_size;
139 unsigned long vmem_rss;
140 unsigned long vmem_data;
141 unsigned long vmem_code;
142 unsigned long stack_size;
144 unsigned long vmem_minflt;
145 unsigned long vmem_majflt;
146 unsigned long vmem_minflt_counter;
147 unsigned long vmem_majflt_counter;
149 unsigned long cpu_user;
150 unsigned long cpu_system;
151 unsigned long cpu_user_counter;
152 unsigned long cpu_system_counter;
160 struct procstat_entry_s *next;
163 #define PROCSTAT_NAME_LEN 256
164 typedef struct procstat
166 char name[PROCSTAT_NAME_LEN];
171 unsigned long num_proc;
172 unsigned long num_lwp;
173 unsigned long vmem_size;
174 unsigned long vmem_rss;
175 unsigned long vmem_data;
176 unsigned long vmem_code;
177 unsigned long stack_size;
179 unsigned long vmem_minflt_counter;
180 unsigned long vmem_majflt_counter;
182 unsigned long cpu_user_counter;
183 unsigned long cpu_system_counter;
191 struct procstat *next;
192 struct procstat_entry_s *instances;
195 static procstat_t *list_head_g = NULL;
198 static mach_port_t port_host_self;
199 static mach_port_t port_task_self;
201 static processor_set_name_array_t pset_list;
202 static mach_msg_type_number_t pset_list_len;
203 /* #endif HAVE_THREAD_INFO */
206 static long pagesize_g;
207 /* #endif KERNEL_LINUX */
209 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD
210 /* no global variables */
211 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
213 #elif HAVE_PROCINFO_H
214 static struct procentry64 procentry[MAXPROCENTRY];
215 static struct thrdentry64 thrdentry[MAXTHRDENTRY];
218 #ifndef _AIXVERSION_610
219 int getprocs64 (void *procsinfo, int sizproc, void *fdsinfo, int sizfd, pid_t *index, int count);
220 int getthrds64( pid_t, void *, int, tid64_t *, int );
222 int getargs (struct procentry64 *processBuffer, int bufferLen, char *argsBuffer, int argsLen);
223 #endif /* HAVE_PROCINFO_H */
225 /* put name of process from config to list_head_g tree
226 list_head_g is a list of 'procstat_t' structs with
227 processes names we want to watch */
228 static void ps_list_register (const char *name, const char *regexp)
234 new = (procstat_t *) malloc (sizeof (procstat_t));
237 ERROR ("processes plugin: ps_list_register: malloc failed.");
240 memset (new, 0, sizeof (procstat_t));
241 sstrncpy (new->name, name, sizeof (new->name));
246 DEBUG ("ProcessMatch: adding \"%s\" as criteria to process %s.", regexp, name);
247 new->re = (regex_t *) malloc (sizeof (regex_t));
250 ERROR ("processes plugin: ps_list_register: malloc failed.");
255 status = regcomp (new->re, regexp, REG_EXTENDED | REG_NOSUB);
258 DEBUG ("ProcessMatch: compiling the regular expression \"%s\" failed.", regexp);
266 ERROR ("processes plugin: ps_list_register: "
267 "Regular expression \"%s\" found in config "
268 "file, but support for regular expressions "
269 "has been disabled at compile time.",
276 for (ptr = list_head_g; ptr != NULL; ptr = ptr->next)
278 if (strcmp (ptr->name, name) == 0)
280 WARNING ("processes plugin: You have configured more "
281 "than one `Process' or "
282 "`ProcessMatch' with the same name. "
283 "All but the first setting will be "
290 if (ptr->next == NULL)
298 } /* void ps_list_register */
300 /* try to match name against entry, returns 1 if success */
301 static int ps_list_match (const char *name, const char *cmdline, procstat_t *ps)
310 if ((str == NULL) || (str[0] == 0))
313 assert (str != NULL);
315 status = regexec (ps->re, str,
324 if (strcmp (ps->name, name) == 0)
328 } /* int ps_list_match */
330 /* add process entry to 'instances' of process 'name' (or refresh it) */
331 static void ps_list_add (const char *name, const char *cmdline, procstat_entry_t *entry)
334 procstat_entry_t *pse;
339 for (ps = list_head_g; ps != NULL; ps = ps->next)
341 if ((ps_list_match (name, cmdline, ps)) == 0)
344 for (pse = ps->instances; pse != NULL; pse = pse->next)
345 if ((pse->id == entry->id) || (pse->next == NULL))
348 if ((pse == NULL) || (pse->id != entry->id))
350 procstat_entry_t *new;
352 new = (procstat_entry_t *) malloc (sizeof (procstat_entry_t));
355 memset (new, 0, sizeof (procstat_entry_t));
367 pse->num_proc = entry->num_proc;
368 pse->num_lwp = entry->num_lwp;
369 pse->vmem_size = entry->vmem_size;
370 pse->vmem_rss = entry->vmem_rss;
371 pse->vmem_data = entry->vmem_data;
372 pse->vmem_code = entry->vmem_code;
373 pse->stack_size = entry->stack_size;
374 pse->io_rchar = entry->io_rchar;
375 pse->io_wchar = entry->io_wchar;
376 pse->io_syscr = entry->io_syscr;
377 pse->io_syscw = entry->io_syscw;
379 ps->num_proc += pse->num_proc;
380 ps->num_lwp += pse->num_lwp;
381 ps->vmem_size += pse->vmem_size;
382 ps->vmem_rss += pse->vmem_rss;
383 ps->vmem_data += pse->vmem_data;
384 ps->vmem_code += pse->vmem_code;
385 ps->stack_size += pse->stack_size;
387 ps->io_rchar += ((pse->io_rchar == -1)?0:pse->io_rchar);
388 ps->io_wchar += ((pse->io_wchar == -1)?0:pse->io_wchar);
389 ps->io_syscr += ((pse->io_syscr == -1)?0:pse->io_syscr);
390 ps->io_syscw += ((pse->io_syscw == -1)?0:pse->io_syscw);
392 if ((entry->vmem_minflt_counter == 0)
393 && (entry->vmem_majflt_counter == 0))
395 pse->vmem_minflt_counter += entry->vmem_minflt;
396 pse->vmem_minflt = entry->vmem_minflt;
398 pse->vmem_majflt_counter += entry->vmem_majflt;
399 pse->vmem_majflt = entry->vmem_majflt;
403 if (entry->vmem_minflt_counter < pse->vmem_minflt_counter)
405 pse->vmem_minflt = entry->vmem_minflt_counter
406 + (ULONG_MAX - pse->vmem_minflt_counter);
410 pse->vmem_minflt = entry->vmem_minflt_counter - pse->vmem_minflt_counter;
412 pse->vmem_minflt_counter = entry->vmem_minflt_counter;
414 if (entry->vmem_majflt_counter < pse->vmem_majflt_counter)
416 pse->vmem_majflt = entry->vmem_majflt_counter
417 + (ULONG_MAX - pse->vmem_majflt_counter);
421 pse->vmem_majflt = entry->vmem_majflt_counter - pse->vmem_majflt_counter;
423 pse->vmem_majflt_counter = entry->vmem_majflt_counter;
426 ps->vmem_minflt_counter += pse->vmem_minflt;
427 ps->vmem_majflt_counter += pse->vmem_majflt;
429 if ((entry->cpu_user_counter == 0)
430 && (entry->cpu_system_counter == 0))
432 pse->cpu_user_counter += entry->cpu_user;
433 pse->cpu_user = entry->cpu_user;
435 pse->cpu_system_counter += entry->cpu_system;
436 pse->cpu_system = entry->cpu_system;
440 if (entry->cpu_user_counter < pse->cpu_user_counter)
442 pse->cpu_user = entry->cpu_user_counter
443 + (ULONG_MAX - pse->cpu_user_counter);
447 pse->cpu_user = entry->cpu_user_counter - pse->cpu_user_counter;
449 pse->cpu_user_counter = entry->cpu_user_counter;
451 if (entry->cpu_system_counter < pse->cpu_system_counter)
453 pse->cpu_system = entry->cpu_system_counter
454 + (ULONG_MAX - pse->cpu_system_counter);
458 pse->cpu_system = entry->cpu_system_counter - pse->cpu_system_counter;
460 pse->cpu_system_counter = entry->cpu_system_counter;
463 ps->cpu_user_counter += pse->cpu_user;
464 ps->cpu_system_counter += pse->cpu_system;
468 /* remove old entries from instances of processes in list_head_g */
469 static void ps_list_reset (void)
472 procstat_entry_t *pse;
473 procstat_entry_t *pse_prev;
475 for (ps = list_head_g; ps != NULL; ps = ps->next)
495 DEBUG ("Removing this procstat entry cause it's too old: "
496 "id = %lu; name = %s;",
499 if (pse_prev == NULL)
501 ps->instances = pse->next;
507 pse_prev->next = pse->next;
509 pse = pse_prev->next;
518 } /* while (pse != NULL) */
519 } /* for (ps = list_head_g; ps != NULL; ps = ps->next) */
522 /* put all pre-defined 'Process' names from config to list_head_g tree */
523 static int ps_config (const char *key, const char *value)
525 if (strcasecmp (key, "Process") == 0)
527 ps_list_register (value, NULL);
529 else if (strcasecmp (key, "ProcessMatch") == 0)
535 new_val = strdup (value);
536 if (new_val == NULL) {
537 ERROR ("processes plugin: strdup failed when processing "
538 "`ProcessMatch %s'.", value);
542 fields_num = strsplit (new_val, fields,
543 STATIC_ARRAY_SIZE (fields));
546 ERROR ("processes plugin: `ProcessMatch' needs exactly "
547 "two string arguments.");
551 ps_list_register (fields[0], fields[1]);
556 ERROR ("processes plugin: The `%s' configuration option is not "
557 "understood and will be ignored.", key);
564 static int ps_init (void)
567 kern_return_t status;
569 port_host_self = mach_host_self ();
570 port_task_self = mach_task_self ();
572 if (pset_list != NULL)
574 vm_deallocate (port_task_self,
575 (vm_address_t) pset_list,
576 pset_list_len * sizeof (processor_set_t));
581 if ((status = host_processor_sets (port_host_self,
583 &pset_list_len)) != KERN_SUCCESS)
585 ERROR ("host_processor_sets failed: %s\n",
586 mach_error_string (status));
591 /* #endif HAVE_THREAD_INFO */
594 pagesize_g = sysconf(_SC_PAGESIZE);
595 DEBUG ("pagesize_g = %li; CONFIG_HZ = %i;",
596 pagesize_g, CONFIG_HZ);
597 /* #endif KERNEL_LINUX */
599 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD
600 /* no initialization */
601 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
603 #elif HAVE_PROCINFO_H
604 pagesize = getpagesize();
605 #endif /* HAVE_PROCINFO_H */
610 /* submit global state (e.g.: qty of zombies, running, etc..) */
611 static void ps_submit_state (const char *state, double value)
614 value_list_t vl = VALUE_LIST_INIT;
616 values[0].gauge = value;
620 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
621 sstrncpy (vl.plugin, "processes", sizeof (vl.plugin));
622 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
623 sstrncpy (vl.type, "ps_state", sizeof (vl.type));
624 sstrncpy (vl.type_instance, state, sizeof (vl.type_instance));
626 plugin_dispatch_values (&vl);
629 /* submit info about specific process (e.g.: memory taken, cpu usage, etc..) */
630 static void ps_submit_proc_list (procstat_t *ps)
633 value_list_t vl = VALUE_LIST_INIT;
637 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
638 sstrncpy (vl.plugin, "processes", sizeof (vl.plugin));
639 sstrncpy (vl.plugin_instance, ps->name, sizeof (vl.plugin_instance));
641 sstrncpy (vl.type, "ps_vm", sizeof (vl.type));
642 vl.values[0].gauge = ps->vmem_size;
644 plugin_dispatch_values (&vl);
646 sstrncpy (vl.type, "ps_rss", sizeof (vl.type));
647 vl.values[0].gauge = ps->vmem_rss;
649 plugin_dispatch_values (&vl);
651 sstrncpy (vl.type, "ps_data", sizeof (vl.type));
652 vl.values[0].gauge = ps->vmem_data;
654 plugin_dispatch_values (&vl);
656 sstrncpy (vl.type, "ps_code", sizeof (vl.type));
657 vl.values[0].gauge = ps->vmem_code;
659 plugin_dispatch_values (&vl);
661 sstrncpy (vl.type, "ps_stacksize", sizeof (vl.type));
662 vl.values[0].gauge = ps->stack_size;
664 plugin_dispatch_values (&vl);
666 sstrncpy (vl.type, "ps_cputime", sizeof (vl.type));
667 vl.values[0].counter = ps->cpu_user_counter;
668 vl.values[1].counter = ps->cpu_system_counter;
670 plugin_dispatch_values (&vl);
672 sstrncpy (vl.type, "ps_count", sizeof (vl.type));
673 vl.values[0].gauge = ps->num_proc;
674 vl.values[1].gauge = ps->num_lwp;
676 plugin_dispatch_values (&vl);
678 sstrncpy (vl.type, "ps_pagefaults", sizeof (vl.type));
679 vl.values[0].counter = ps->vmem_minflt_counter;
680 vl.values[1].counter = ps->vmem_majflt_counter;
682 plugin_dispatch_values (&vl);
684 if ( (ps->io_rchar != -1) && (ps->io_wchar != -1) )
686 sstrncpy (vl.type, "ps_disk_octets", sizeof (vl.type));
687 vl.values[0].derive = ps->io_rchar;
688 vl.values[1].derive = ps->io_wchar;
690 plugin_dispatch_values (&vl);
693 if ( (ps->io_syscr != -1) && (ps->io_syscw != -1) )
695 sstrncpy (vl.type, "ps_disk_ops", sizeof (vl.type));
696 vl.values[0].derive = ps->io_syscr;
697 vl.values[1].derive = ps->io_syscw;
699 plugin_dispatch_values (&vl);
702 DEBUG ("name = %s; num_proc = %lu; num_lwp = %lu; "
703 "vmem_size = %lu; vmem_rss = %lu; vmem_data = %lu; "
705 "vmem_minflt_counter = %lu; vmem_majflt_counter = %lu; "
706 "cpu_user_counter = %lu; cpu_system_counter = %lu; "
707 "io_rchar = %"PRIi64"; io_wchar = %"PRIi64"; "
708 "io_syscr = %"PRIi64"; io_syscw = %"PRIi64";",
709 ps->name, ps->num_proc, ps->num_lwp,
710 ps->vmem_size, ps->vmem_rss,
711 ps->vmem_data, ps->vmem_code,
712 ps->vmem_minflt_counter, ps->vmem_majflt_counter,
713 ps->cpu_user_counter, ps->cpu_system_counter,
714 ps->io_rchar, ps->io_wchar, ps->io_syscr, ps->io_syscw);
715 } /* void ps_submit_proc_list */
717 /* ------- additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
719 static int ps_read_tasks (int pid)
726 ssnprintf (dirname, sizeof (dirname), "/proc/%i/task", pid);
728 if ((dh = opendir (dirname)) == NULL)
730 DEBUG ("Failed to open directory `%s'", dirname);
734 while ((ent = readdir (dh)) != NULL)
736 if (!isdigit ((int) ent->d_name[0]))
743 return ((count >= 1) ? count : 1);
744 } /* int *ps_read_tasks */
746 /* Read advanced virtual memory data from /proc/pid/status */
747 static procstat_t *ps_read_vmem (int pid, procstat_t *ps)
752 unsigned long long lib = 0;
753 unsigned long long exe = 0;
754 unsigned long long data = 0;
758 ssnprintf (filename, sizeof (filename), "/proc/%i/status", pid);
759 if ((fh = fopen (filename, "r")) == NULL)
762 while (fgets (buffer, sizeof(buffer), fh) != NULL)
767 if (strncmp (buffer, "Vm", 2) != 0)
770 numfields = strsplit (buffer, fields,
771 STATIC_ARRAY_SIZE (fields));
778 tmp = strtoll (fields[1], &endptr, /* base = */ 10);
779 if ((errno == 0) && (endptr != fields[1]))
781 if (strncmp (buffer, "VmData", 6) == 0)
785 else if (strncmp (buffer, "VmLib", 5) == 0)
789 else if (strncmp(buffer, "VmExe", 5) == 0)
794 } /* while (fgets) */
799 WARNING ("processes: fclose: %s",
800 sstrerror (errno, errbuf, sizeof (errbuf)));
803 ps->vmem_data = data * 1024;
804 ps->vmem_code = (exe + lib) * 1024;
807 } /* procstat_t *ps_read_vmem */
809 static procstat_t *ps_read_io (int pid, procstat_t *ps)
818 ssnprintf (filename, sizeof (filename), "/proc/%i/io", pid);
819 if ((fh = fopen (filename, "r")) == NULL)
822 while (fgets (buffer, 1024, fh) != NULL)
824 derive_t *val = NULL;
828 if (strncasecmp (buffer, "rchar:", 6) == 0)
829 val = &(ps->io_rchar);
830 else if (strncasecmp (buffer, "wchar:", 6) == 0)
831 val = &(ps->io_wchar);
832 else if (strncasecmp (buffer, "syscr:", 6) == 0)
833 val = &(ps->io_syscr);
834 else if (strncasecmp (buffer, "syscw:", 6) == 0)
835 val = &(ps->io_syscw);
839 numfields = strsplit (buffer, fields, 8);
846 tmp = strtoll (fields[1], &endptr, /* base = */ 10);
847 if ((errno != 0) || (endptr == fields[1]))
850 *val = (derive_t) tmp;
851 } /* while (fgets) */
856 WARNING ("processes: fclose: %s",
857 sstrerror (errno, errbuf, sizeof (errbuf)));
861 } /* procstat_t *ps_read_io */
863 int ps_read_process (int pid, procstat_t *ps, char *state)
876 long long unsigned cpu_user_counter;
877 long long unsigned cpu_system_counter;
878 long long unsigned vmem_size;
879 long long unsigned vmem_rss;
880 long long unsigned stack_size;
882 memset (ps, 0, sizeof (procstat_t));
884 ssnprintf (filename, sizeof (filename), "/proc/%i/stat", pid);
886 i = read_file_contents (filename, buffer, sizeof(buffer) - 1);
891 fields_len = strsplit (buffer, fields, 64);
894 DEBUG ("processes plugin: ps_read_process (pid = %i):"
895 " `%s' has only %i fields..",
896 (int) pid, filename, fields_len);
900 /* copy the name, strip brackets in the process */
901 name_len = strlen (fields[1]) - 2;
902 if ((fields[1][0] != '(') || (fields[1][name_len + 1] != ')'))
904 DEBUG ("No brackets found in process name: `%s'", fields[1]);
907 fields[1] = fields[1] + 1;
908 fields[1][name_len] = '\0';
909 strncpy (ps->name, fields[1], PROCSTAT_NAME_LEN);
911 ppid = atoi (fields[3]);
913 *state = fields[2][0];
922 if ( (ps->num_lwp = ps_read_tasks (pid)) == -1 )
924 /* returns -1 => kernel 2.4 */
930 /* Leave the rest at zero if this is only a zombi */
931 if (ps->num_proc == 0)
933 DEBUG ("processes plugin: This is only a zombi: pid = %i; "
934 "name = %s;", pid, ps->name);
938 cpu_user_counter = atoll (fields[13]);
939 cpu_system_counter = atoll (fields[14]);
940 vmem_size = atoll (fields[22]);
941 vmem_rss = atoll (fields[23]);
942 ps->vmem_minflt_counter = atol (fields[9]);
943 ps->vmem_majflt_counter = atol (fields[11]);
946 unsigned long long stack_start = atoll (fields[27]);
947 unsigned long long stack_ptr = atoll (fields[28]);
949 stack_size = (stack_start > stack_ptr)
950 ? stack_start - stack_ptr
951 : stack_ptr - stack_start;
954 /* Convert jiffies to useconds */
955 cpu_user_counter = cpu_user_counter * 1000000 / CONFIG_HZ;
956 cpu_system_counter = cpu_system_counter * 1000000 / CONFIG_HZ;
957 vmem_rss = vmem_rss * pagesize_g;
959 if ( (ps_read_vmem(pid, ps)) == NULL)
964 DEBUG("ps_read_process: did not get vmem data for pid %i",pid);
967 ps->cpu_user_counter = (unsigned long) cpu_user_counter;
968 ps->cpu_system_counter = (unsigned long) cpu_system_counter;
969 ps->vmem_size = (unsigned long) vmem_size;
970 ps->vmem_rss = (unsigned long) vmem_rss;
971 ps->stack_size = (unsigned long) stack_size;
973 if ( (ps_read_io (pid, ps)) == NULL)
981 DEBUG("ps_read_process: not get io data for pid %i",pid);
986 } /* int ps_read_process (...) */
988 static char *ps_get_cmdline (pid_t pid, char *name, char *buf, size_t buf_len)
998 if ((pid < 1) || (NULL == buf) || (buf_len < 2))
1001 ssnprintf (file, sizeof (file), "/proc/%u/cmdline", pid);
1003 fd = open (file, O_RDONLY);
1006 WARNING ("processes plugin: Failed to open `%s': %s.", file,
1007 sstrerror (errno, errbuf, sizeof (errbuf)));
1019 status = read (fd, (void *)buf_ptr, len);
1024 if ((EAGAIN == errno) || (EINTR == errno))
1027 WARNING ("processes plugin: Failed to read from `%s': %s.", file,
1028 sstrerror (errno, errbuf, sizeof (errbuf)));
1048 /* cmdline not available; e.g. kernel thread, zombie */
1052 ssnprintf (buf, buf_len, "[%s]", name);
1056 assert (n <= buf_len);
1063 /* remove trailing whitespace */
1064 while ((n > 0) && (isspace (buf[n]) || ('\0' == buf[n]))) {
1069 /* arguments are separated by '\0' in /proc/<pid>/cmdline */
1076 } /* char *ps_get_cmdline (...) */
1078 static unsigned long read_fork_rate ()
1082 unsigned long result = 0;
1086 proc_stat = fopen("/proc/stat", "r");
1087 if (proc_stat == NULL) {
1089 ERROR ("processes plugin: fopen (/proc/stat) failed: %s",
1090 sstrerror (errno, errbuf, sizeof (errbuf)));
1094 while (fgets (buf, sizeof(buf), proc_stat) != NULL)
1098 numfields = strsplit(buf, fields, STATIC_ARRAY_SIZE (fields));
1102 if (strcmp ("processes", fields[0]) != 0)
1107 result = strtoul(fields[1], &endptr, 10);
1108 if ((endptr == fields[1]) || (errno != 0)) {
1109 ERROR ("processes plugin: Cannot parse fork rate: %s",
1123 static void ps_submit_fork_rate (unsigned long value)
1126 value_list_t vl = VALUE_LIST_INIT;
1128 values[0].derive = (derive_t) value;
1132 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
1133 sstrncpy (vl.plugin, "processes", sizeof (vl.plugin));
1134 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
1135 sstrncpy (vl.type, "fork_rate", sizeof (vl.type));
1136 sstrncpy (vl.type_instance, "", sizeof (vl.type_instance));
1138 plugin_dispatch_values (&vl);
1141 #endif /* KERNEL_LINUX */
1143 #if HAVE_THREAD_INFO
1144 static int mach_get_task_name (task_t t, int *pid, char *name, size_t name_max_len)
1148 struct kinfo_proc kp;
1153 mib[2] = KERN_PROC_PID;
1155 if (pid_for_task (t, pid) != KERN_SUCCESS)
1159 kp_size = sizeof (kp);
1160 if (sysctl (mib, 4, &kp, &kp_size, NULL, 0) != 0)
1163 if (name_max_len > (MAXCOMLEN + 1))
1164 name_max_len = MAXCOMLEN + 1;
1166 strncpy (name, kp.kp_proc.p_comm, name_max_len - 1);
1167 name[name_max_len - 1] = '\0';
1169 DEBUG ("pid = %i; name = %s;", *pid, name);
1171 /* We don't do the special handling for `p_comm == "LaunchCFMApp"' as
1172 * `top' does it, because it is a lot of work and only used when
1173 * debugging. -octo */
1177 #endif /* HAVE_THREAD_INFO */
1178 /* ------- end of additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
1180 /* do actual readings from kernel */
1181 static int ps_read (void)
1183 #if HAVE_THREAD_INFO
1184 kern_return_t status;
1187 processor_set_t port_pset_priv;
1190 task_array_t task_list;
1191 mach_msg_type_number_t task_list_len;
1194 char task_name[MAXCOMLEN + 1];
1197 thread_act_array_t thread_list;
1198 mach_msg_type_number_t thread_list_len;
1199 thread_basic_info_data_t thread_data;
1200 mach_msg_type_number_t thread_data_len;
1209 procstat_entry_t pse;
1214 * The Mach-concept is a little different from the traditional UNIX
1215 * concept: All the work is done in threads. Threads are contained in
1216 * `tasks'. Therefore, `task status' doesn't make much sense, since
1217 * it's actually a `thread status'.
1218 * Tasks are assigned to sets of processors, so that's where you go to
1221 for (pset = 0; pset < pset_list_len; pset++)
1223 if ((status = host_processor_set_priv (port_host_self,
1225 &port_pset_priv)) != KERN_SUCCESS)
1227 ERROR ("host_processor_set_priv failed: %s\n",
1228 mach_error_string (status));
1232 if ((status = processor_set_tasks (port_pset_priv,
1234 &task_list_len)) != KERN_SUCCESS)
1236 ERROR ("processor_set_tasks failed: %s\n",
1237 mach_error_string (status));
1238 mach_port_deallocate (port_task_self, port_pset_priv);
1242 for (task = 0; task < task_list_len; task++)
1245 if (mach_get_task_name (task_list[task],
1247 task_name, PROCSTAT_NAME_LEN) == 0)
1249 /* search for at least one match */
1250 for (ps = list_head_g; ps != NULL; ps = ps->next)
1251 /* FIXME: cmdline should be here instead of NULL */
1252 if (ps_list_match (task_name, NULL, ps) == 1)
1256 /* Collect more detailed statistics for this process */
1259 task_basic_info_data_t task_basic_info;
1260 mach_msg_type_number_t task_basic_info_len;
1261 task_events_info_data_t task_events_info;
1262 mach_msg_type_number_t task_events_info_len;
1263 task_absolutetime_info_data_t task_absolutetime_info;
1264 mach_msg_type_number_t task_absolutetime_info_len;
1266 memset (&pse, '\0', sizeof (pse));
1269 task_basic_info_len = TASK_BASIC_INFO_COUNT;
1270 status = task_info (task_list[task],
1272 (task_info_t) &task_basic_info,
1273 &task_basic_info_len);
1274 if (status != KERN_SUCCESS)
1276 ERROR ("task_info failed: %s",
1277 mach_error_string (status));
1278 continue; /* with next thread_list */
1281 task_events_info_len = TASK_EVENTS_INFO_COUNT;
1282 status = task_info (task_list[task],
1284 (task_info_t) &task_events_info,
1285 &task_events_info_len);
1286 if (status != KERN_SUCCESS)
1288 ERROR ("task_info failed: %s",
1289 mach_error_string (status));
1290 continue; /* with next thread_list */
1293 task_absolutetime_info_len = TASK_ABSOLUTETIME_INFO_COUNT;
1294 status = task_info (task_list[task],
1295 TASK_ABSOLUTETIME_INFO,
1296 (task_info_t) &task_absolutetime_info,
1297 &task_absolutetime_info_len);
1298 if (status != KERN_SUCCESS)
1300 ERROR ("task_info failed: %s",
1301 mach_error_string (status));
1302 continue; /* with next thread_list */
1306 pse.vmem_size = task_basic_info.virtual_size;
1307 pse.vmem_rss = task_basic_info.resident_size;
1308 /* Does not seem to be easily exposed */
1312 pse.vmem_minflt_counter = task_events_info.cow_faults;
1313 pse.vmem_majflt_counter = task_events_info.faults;
1315 pse.cpu_user_counter = task_absolutetime_info.total_user;
1316 pse.cpu_system_counter = task_absolutetime_info.total_system;
1319 status = task_threads (task_list[task], &thread_list,
1321 if (status != KERN_SUCCESS)
1323 /* Apple's `top' treats this case a zombie. It
1324 * makes sense to some extend: A `zombie'
1325 * thread is nonsense, since the task/process
1328 DEBUG ("task_threads failed: %s",
1329 mach_error_string (status));
1330 if (task_list[task] != port_task_self)
1331 mach_port_deallocate (port_task_self,
1333 continue; /* with next task_list */
1336 for (thread = 0; thread < thread_list_len; thread++)
1338 thread_data_len = THREAD_BASIC_INFO_COUNT;
1339 status = thread_info (thread_list[thread],
1341 (thread_info_t) &thread_data,
1343 if (status != KERN_SUCCESS)
1345 ERROR ("thread_info failed: %s",
1346 mach_error_string (status));
1347 if (task_list[task] != port_task_self)
1348 mach_port_deallocate (port_task_self,
1349 thread_list[thread]);
1350 continue; /* with next thread_list */
1356 switch (thread_data.run_state)
1358 case TH_STATE_RUNNING:
1361 case TH_STATE_STOPPED:
1362 /* What exactly is `halted'? */
1363 case TH_STATE_HALTED:
1366 case TH_STATE_WAITING:
1369 case TH_STATE_UNINTERRUPTIBLE:
1372 /* There is no `zombie' case here,
1373 * since there are no zombie-threads.
1374 * There's only zombie tasks, which are
1377 WARNING ("Unknown thread status: %i",
1378 thread_data.run_state);
1380 } /* switch (thread_data.run_state) */
1382 if (task_list[task] != port_task_self)
1384 status = mach_port_deallocate (port_task_self,
1385 thread_list[thread]);
1386 if (status != KERN_SUCCESS)
1387 ERROR ("mach_port_deallocate failed: %s",
1388 mach_error_string (status));
1390 } /* for (thread_list) */
1392 if ((status = vm_deallocate (port_task_self,
1393 (vm_address_t) thread_list,
1394 thread_list_len * sizeof (thread_act_t)))
1397 ERROR ("vm_deallocate failed: %s",
1398 mach_error_string (status));
1401 thread_list_len = 0;
1403 /* Only deallocate the task port, if it isn't our own.
1404 * Don't know what would happen in that case, but this
1405 * is what Apple's top does.. ;) */
1406 if (task_list[task] != port_task_self)
1408 status = mach_port_deallocate (port_task_self,
1410 if (status != KERN_SUCCESS)
1411 ERROR ("mach_port_deallocate failed: %s",
1412 mach_error_string (status));
1416 /* FIXME: cmdline should be here instead of NULL */
1417 ps_list_add (task_name, NULL, &pse);
1418 } /* for (task_list) */
1420 if ((status = vm_deallocate (port_task_self,
1421 (vm_address_t) task_list,
1422 task_list_len * sizeof (task_t))) != KERN_SUCCESS)
1424 ERROR ("vm_deallocate failed: %s",
1425 mach_error_string (status));
1430 if ((status = mach_port_deallocate (port_task_self, port_pset_priv))
1433 ERROR ("mach_port_deallocate failed: %s",
1434 mach_error_string (status));
1436 } /* for (pset_list) */
1438 ps_submit_state ("running", running);
1439 ps_submit_state ("sleeping", sleeping);
1440 ps_submit_state ("zombies", zombies);
1441 ps_submit_state ("stopped", stopped);
1442 ps_submit_state ("blocked", blocked);
1444 for (ps = list_head_g; ps != NULL; ps = ps->next)
1445 ps_submit_proc_list (ps);
1446 /* #endif HAVE_THREAD_INFO */
1460 char cmdline[ARG_MAX];
1464 procstat_entry_t pse;
1467 unsigned long fork_rate;
1471 running = sleeping = zombies = stopped = paging = blocked = 0;
1474 if ((proc = opendir ("/proc")) == NULL)
1477 ERROR ("Cannot open `/proc': %s",
1478 sstrerror (errno, errbuf, sizeof (errbuf)));
1482 while ((ent = readdir (proc)) != NULL)
1484 if (!isdigit (ent->d_name[0]))
1487 if ((pid = atoi (ent->d_name)) < 1)
1490 status = ps_read_process (pid, &ps, &state);
1493 DEBUG ("ps_read_process failed: %i", status);
1500 pse.num_proc = ps.num_proc;
1501 pse.num_lwp = ps.num_lwp;
1502 pse.vmem_size = ps.vmem_size;
1503 pse.vmem_rss = ps.vmem_rss;
1504 pse.vmem_data = ps.vmem_data;
1505 pse.vmem_code = ps.vmem_code;
1506 pse.stack_size = ps.stack_size;
1508 pse.vmem_minflt = 0;
1509 pse.vmem_minflt_counter = ps.vmem_minflt_counter;
1510 pse.vmem_majflt = 0;
1511 pse.vmem_majflt_counter = ps.vmem_majflt_counter;
1514 pse.cpu_user_counter = ps.cpu_user_counter;
1516 pse.cpu_system_counter = ps.cpu_system_counter;
1518 pse.io_rchar = ps.io_rchar;
1519 pse.io_wchar = ps.io_wchar;
1520 pse.io_syscr = ps.io_syscr;
1521 pse.io_syscw = ps.io_syscw;
1525 case 'R': running++; break;
1526 case 'S': sleeping++; break;
1527 case 'D': blocked++; break;
1528 case 'Z': zombies++; break;
1529 case 'T': stopped++; break;
1530 case 'W': paging++; break;
1533 ps_list_add (ps.name,
1534 ps_get_cmdline (pid, ps.name, cmdline, sizeof (cmdline)),
1540 ps_submit_state ("running", running);
1541 ps_submit_state ("sleeping", sleeping);
1542 ps_submit_state ("zombies", zombies);
1543 ps_submit_state ("stopped", stopped);
1544 ps_submit_state ("paging", paging);
1545 ps_submit_state ("blocked", blocked);
1547 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1548 ps_submit_proc_list (ps_ptr);
1550 fork_rate = read_fork_rate();
1551 if (fork_rate != ULONG_MAX)
1552 ps_submit_fork_rate(fork_rate);
1553 /* #endif KERNEL_LINUX */
1555 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD
1566 char cmdline[ARG_MAX];
1568 struct kinfo_proc *procs; /* array of processes */
1570 int count; /* returns number of processes */
1574 procstat_entry_t pse;
1578 /* Open the kvm interface, get a descriptor */
1579 kd = kvm_open (NULL, NULL, NULL, 0, errbuf);
1582 ERROR ("processes plugin: Cannot open kvm interface: %s",
1587 /* Get the list of processes. */
1588 procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, &count);
1592 ERROR ("processes plugin: Cannot get kvm processes list: %s",
1597 /* Iterate through the processes in kinfo_proc */
1598 for (i = 0; i < count; i++)
1600 /* retrieve the arguments */
1604 argv = kvm_getargv (kd, (const struct kinfo_proc *) &(procs[i]), 0);
1611 while (argv[argc] != NULL)
1614 status = strjoin (cmdline, sizeof (cmdline),
1619 WARNING ("processes plugin: Command line did "
1620 "not fit into buffer.");
1624 cmdline_ptr = &cmdline[0];
1628 pse.id = procs[i].ki_pid;
1632 pse.num_lwp = procs[i].ki_numthreads;
1634 pse.vmem_size = procs[i].ki_size;
1635 pse.vmem_rss = procs[i].ki_rssize * getpagesize();
1636 pse.vmem_data = procs[i].ki_dsize * getpagesize();
1637 pse.vmem_code = procs[i].ki_tsize * getpagesize();
1638 pse.stack_size = procs[i].ki_ssize * getpagesize();
1639 pse.vmem_minflt = 0;
1640 pse.vmem_minflt_counter = procs[i].ki_rusage.ru_minflt;
1641 pse.vmem_majflt = 0;
1642 pse.vmem_majflt_counter = procs[i].ki_rusage.ru_majflt;
1645 pse.cpu_user_counter = procs[i].ki_rusage.ru_utime.tv_sec
1647 + procs[i].ki_rusage.ru_utime.tv_usec;
1649 pse.cpu_system_counter = procs[i].ki_rusage.ru_stime.tv_sec
1651 + procs[i].ki_rusage.ru_stime.tv_usec;
1659 switch (procs[i].ki_stat)
1661 case SSTOP: stopped++; break;
1662 case SSLEEP: sleeping++; break;
1663 case SRUN: running++; break;
1664 case SIDL: idle++; break;
1665 case SWAIT: wait++; break;
1666 case SLOCK: blocked++; break;
1667 case SZOMB: zombies++; break;
1670 ps_list_add (procs[i].ki_comm, cmdline_ptr, &pse);
1675 ps_submit_state ("running", running);
1676 ps_submit_state ("sleeping", sleeping);
1677 ps_submit_state ("zombies", zombies);
1678 ps_submit_state ("stopped", stopped);
1679 ps_submit_state ("blocked", blocked);
1680 ps_submit_state ("idle", idle);
1681 ps_submit_state ("wait", wait);
1683 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1684 ps_submit_proc_list (ps_ptr);
1685 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
1687 #elif HAVE_PROCINFO_H
1700 procstat_entry_t pse;
1703 while ((nprocs = getprocs64 (procentry, sizeof(struct procentry64),
1704 /* fdsinfo = */ NULL, sizeof(struct fdsinfo64),
1705 &pindex, MAXPROCENTRY)) > 0)
1709 for (i = 0; i < nprocs; i++)
1713 char arglist[MAXARGLN+1];
1717 if (procentry[i].pi_state == SNONE) continue;
1718 /* if (procentry[i].pi_state == SZOMB) FIXME */
1720 cmdline = procentry[i].pi_comm;
1721 cargs = procentry[i].pi_comm;
1722 if ( procentry[i].pi_flags & SKPROC )
1724 if (procentry[i].pi_pid == 0)
1725 cmdline = "swapper";
1730 if (getargs(&procentry[i], sizeof(struct procentry64), arglist, MAXARGLN) >= 0)
1735 while (++n < MAXARGLN)
1737 if (arglist[n] == '\0')
1739 if (arglist[n+1] == '\0')
1748 pse.id = procentry[i].pi_pid;
1750 pse.num_lwp = procentry[i].pi_thcount;
1754 while ((nthreads = getthrds64(procentry[i].pi_pid,
1755 thrdentry, sizeof(struct thrdentry64),
1756 &thindex, MAXTHRDENTRY)) > 0)
1760 for (j=0; j< nthreads; j++)
1762 switch (thrdentry[j].ti_state)
1764 /* case TSNONE: break; */
1765 case TSIDL: blocked++; break; /* FIXME is really blocked */
1766 case TSRUN: running++; break;
1767 case TSSLEEP: sleeping++; break;
1768 case TSSWAP: paging++; break;
1769 case TSSTOP: stopped++; break;
1770 case TSZOMB: zombies++; break;
1773 if (nthreads < MAXTHRDENTRY)
1778 /* tv_usec is nanosec ??? */
1779 pse.cpu_user_counter = procentry[i].pi_ru.ru_utime.tv_sec * 1000000 +
1780 procentry[i].pi_ru.ru_utime.tv_usec / 1000;
1783 /* tv_usec is nanosec ??? */
1784 pse.cpu_system_counter = procentry[i].pi_ru.ru_stime.tv_sec * 1000000 +
1785 procentry[i].pi_ru.ru_stime.tv_usec / 1000;
1787 pse.vmem_minflt = 0;
1788 pse.vmem_minflt_counter = procentry[i].pi_minflt;
1789 pse.vmem_majflt = 0;
1790 pse.vmem_majflt_counter = procentry[i].pi_majflt;
1792 pse.vmem_size = procentry[i].pi_tsize + procentry[i].pi_dvm * pagesize;
1793 pse.vmem_rss = (procentry[i].pi_drss + procentry[i].pi_trss) * pagesize;
1799 ps_list_add (cmdline, cargs, &pse);
1800 } /* for (i = 0 .. nprocs) */
1802 if (nprocs < MAXPROCENTRY)
1804 } /* while (getprocs64() > 0) */
1805 ps_submit_state ("running", running);
1806 ps_submit_state ("sleeping", sleeping);
1807 ps_submit_state ("zombies", zombies);
1808 ps_submit_state ("stopped", stopped);
1809 ps_submit_state ("paging", paging);
1810 ps_submit_state ("blocked", blocked);
1812 for (ps = list_head_g; ps != NULL; ps = ps->next)
1813 ps_submit_proc_list (ps);
1814 #endif /* HAVE_PROCINFO_H */
1819 void module_register (void)
1821 plugin_register_config ("processes", ps_config,
1822 config_keys, config_keys_num);
1823 plugin_register_init ("processes", ps_init);
1824 plugin_register_read ("processes", ps_read);
1825 } /* void module_register */