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 stack_size;
142 unsigned long vmem_minflt;
143 unsigned long vmem_majflt;
144 unsigned long vmem_minflt_counter;
145 unsigned long vmem_majflt_counter;
147 unsigned long cpu_user;
148 unsigned long cpu_system;
149 unsigned long cpu_user_counter;
150 unsigned long cpu_system_counter;
158 struct procstat_entry_s *next;
161 #define PROCSTAT_NAME_LEN 256
162 typedef struct procstat
164 char name[PROCSTAT_NAME_LEN];
169 unsigned long num_proc;
170 unsigned long num_lwp;
171 unsigned long vmem_size;
172 unsigned long vmem_rss;
173 unsigned long stack_size;
175 unsigned long vmem_minflt_counter;
176 unsigned long vmem_majflt_counter;
178 unsigned long cpu_user_counter;
179 unsigned long cpu_system_counter;
187 struct procstat *next;
188 struct procstat_entry_s *instances;
191 static procstat_t *list_head_g = NULL;
194 static mach_port_t port_host_self;
195 static mach_port_t port_task_self;
197 static processor_set_name_array_t pset_list;
198 static mach_msg_type_number_t pset_list_len;
199 /* #endif HAVE_THREAD_INFO */
202 static long pagesize_g;
203 /* #endif KERNEL_LINUX */
205 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD
206 /* no global variables */
207 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
209 #elif HAVE_PROCINFO_H
210 static struct procentry64 procentry[MAXPROCENTRY];
211 static struct thrdentry64 thrdentry[MAXTHRDENTRY];
214 #ifndef _AIXVERSION_610
215 int getprocs64 (void *procsinfo, int sizproc, void *fdsinfo, int sizfd, pid_t *index, int count);
216 int getthrds64( pid_t, void *, int, tid64_t *, int );
218 int getargs (struct procentry64 *processBuffer, int bufferLen, char *argsBuffer, int argsLen);
219 #endif /* HAVE_PROCINFO_H */
221 /* put name of process from config to list_head_g tree
222 list_head_g is a list of 'procstat_t' structs with
223 processes names we want to watch */
224 static void ps_list_register (const char *name, const char *regexp)
230 new = (procstat_t *) malloc (sizeof (procstat_t));
233 ERROR ("processes plugin: ps_list_register: malloc failed.");
236 memset (new, 0, sizeof (procstat_t));
237 sstrncpy (new->name, name, sizeof (new->name));
242 DEBUG ("ProcessMatch: adding \"%s\" as criteria to process %s.", regexp, name);
243 new->re = (regex_t *) malloc (sizeof (regex_t));
246 ERROR ("processes plugin: ps_list_register: malloc failed.");
251 status = regcomp (new->re, regexp, REG_EXTENDED | REG_NOSUB);
254 DEBUG ("ProcessMatch: compiling the regular expression \"%s\" failed.", regexp);
262 ERROR ("processes plugin: ps_list_register: "
263 "Regular expression \"%s\" found in config "
264 "file, but support for regular expressions "
265 "has been disabled at compile time.",
272 for (ptr = list_head_g; ptr != NULL; ptr = ptr->next)
274 if (strcmp (ptr->name, name) == 0)
276 WARNING ("processes plugin: You have configured more "
277 "than one `Process' or "
278 "`ProcessMatch' with the same name. "
279 "All but the first setting will be "
286 if (ptr->next == NULL)
294 } /* void ps_list_register */
296 /* try to match name against entry, returns 1 if success */
297 static int ps_list_match (const char *name, const char *cmdline, procstat_t *ps)
306 if ((str == NULL) || (str[0] == 0))
309 assert (str != NULL);
311 status = regexec (ps->re, str,
320 if (strcmp (ps->name, name) == 0)
324 } /* int ps_list_match */
326 /* add process entry to 'instances' of process 'name' (or refresh it) */
327 static void ps_list_add (const char *name, const char *cmdline, procstat_entry_t *entry)
330 procstat_entry_t *pse;
335 for (ps = list_head_g; ps != NULL; ps = ps->next)
337 if ((ps_list_match (name, cmdline, ps)) == 0)
340 for (pse = ps->instances; pse != NULL; pse = pse->next)
341 if ((pse->id == entry->id) || (pse->next == NULL))
344 if ((pse == NULL) || (pse->id != entry->id))
346 procstat_entry_t *new;
348 new = (procstat_entry_t *) malloc (sizeof (procstat_entry_t));
351 memset (new, 0, sizeof (procstat_entry_t));
363 pse->num_proc = entry->num_proc;
364 pse->num_lwp = entry->num_lwp;
365 pse->vmem_size = entry->vmem_size;
366 pse->vmem_rss = entry->vmem_rss;
367 pse->stack_size = entry->stack_size;
368 pse->io_rchar = entry->io_rchar;
369 pse->io_wchar = entry->io_wchar;
370 pse->io_syscr = entry->io_syscr;
371 pse->io_syscw = entry->io_syscw;
373 ps->num_proc += pse->num_proc;
374 ps->num_lwp += pse->num_lwp;
375 ps->vmem_size += pse->vmem_size;
376 ps->vmem_rss += pse->vmem_rss;
377 ps->stack_size += pse->stack_size;
379 ps->io_rchar += ((pse->io_rchar == -1)?0:pse->io_rchar);
380 ps->io_wchar += ((pse->io_wchar == -1)?0:pse->io_wchar);
381 ps->io_syscr += ((pse->io_syscr == -1)?0:pse->io_syscr);
382 ps->io_syscw += ((pse->io_syscw == -1)?0:pse->io_syscw);
384 if ((entry->vmem_minflt_counter == 0)
385 && (entry->vmem_majflt_counter == 0))
387 pse->vmem_minflt_counter += entry->vmem_minflt;
388 pse->vmem_minflt = entry->vmem_minflt;
390 pse->vmem_majflt_counter += entry->vmem_majflt;
391 pse->vmem_majflt = entry->vmem_majflt;
395 if (entry->vmem_minflt_counter < pse->vmem_minflt_counter)
397 pse->vmem_minflt = entry->vmem_minflt_counter
398 + (ULONG_MAX - pse->vmem_minflt_counter);
402 pse->vmem_minflt = entry->vmem_minflt_counter - pse->vmem_minflt_counter;
404 pse->vmem_minflt_counter = entry->vmem_minflt_counter;
406 if (entry->vmem_majflt_counter < pse->vmem_majflt_counter)
408 pse->vmem_majflt = entry->vmem_majflt_counter
409 + (ULONG_MAX - pse->vmem_majflt_counter);
413 pse->vmem_majflt = entry->vmem_majflt_counter - pse->vmem_majflt_counter;
415 pse->vmem_majflt_counter = entry->vmem_majflt_counter;
418 ps->vmem_minflt_counter += pse->vmem_minflt;
419 ps->vmem_majflt_counter += pse->vmem_majflt;
421 if ((entry->cpu_user_counter == 0)
422 && (entry->cpu_system_counter == 0))
424 pse->cpu_user_counter += entry->cpu_user;
425 pse->cpu_user = entry->cpu_user;
427 pse->cpu_system_counter += entry->cpu_system;
428 pse->cpu_system = entry->cpu_system;
432 if (entry->cpu_user_counter < pse->cpu_user_counter)
434 pse->cpu_user = entry->cpu_user_counter
435 + (ULONG_MAX - pse->cpu_user_counter);
439 pse->cpu_user = entry->cpu_user_counter - pse->cpu_user_counter;
441 pse->cpu_user_counter = entry->cpu_user_counter;
443 if (entry->cpu_system_counter < pse->cpu_system_counter)
445 pse->cpu_system = entry->cpu_system_counter
446 + (ULONG_MAX - pse->cpu_system_counter);
450 pse->cpu_system = entry->cpu_system_counter - pse->cpu_system_counter;
452 pse->cpu_system_counter = entry->cpu_system_counter;
455 ps->cpu_user_counter += pse->cpu_user;
456 ps->cpu_system_counter += pse->cpu_system;
460 /* remove old entries from instances of processes in list_head_g */
461 static void ps_list_reset (void)
464 procstat_entry_t *pse;
465 procstat_entry_t *pse_prev;
467 for (ps = list_head_g; ps != NULL; ps = ps->next)
485 DEBUG ("Removing this procstat entry cause it's too old: "
486 "id = %lu; name = %s;",
489 if (pse_prev == NULL)
491 ps->instances = pse->next;
497 pse_prev->next = pse->next;
499 pse = pse_prev->next;
508 } /* while (pse != NULL) */
509 } /* for (ps = list_head_g; ps != NULL; ps = ps->next) */
512 /* put all pre-defined 'Process' names from config to list_head_g tree */
513 static int ps_config (const char *key, const char *value)
515 if (strcasecmp (key, "Process") == 0)
517 ps_list_register (value, NULL);
519 else if (strcasecmp (key, "ProcessMatch") == 0)
525 new_val = strdup (value);
526 if (new_val == NULL) {
527 ERROR ("processes plugin: strdup failed when processing "
528 "`ProcessMatch %s'.", value);
532 fields_num = strsplit (new_val, fields,
533 STATIC_ARRAY_SIZE (fields));
536 ERROR ("processes plugin: `ProcessMatch' needs exactly "
537 "two string arguments.");
541 ps_list_register (fields[0], fields[1]);
546 ERROR ("processes plugin: The `%s' configuration option is not "
547 "understood and will be ignored.", key);
554 static int ps_init (void)
557 kern_return_t status;
559 port_host_self = mach_host_self ();
560 port_task_self = mach_task_self ();
562 if (pset_list != NULL)
564 vm_deallocate (port_task_self,
565 (vm_address_t) pset_list,
566 pset_list_len * sizeof (processor_set_t));
571 if ((status = host_processor_sets (port_host_self,
573 &pset_list_len)) != KERN_SUCCESS)
575 ERROR ("host_processor_sets failed: %s\n",
576 mach_error_string (status));
581 /* #endif HAVE_THREAD_INFO */
584 pagesize_g = sysconf(_SC_PAGESIZE);
585 DEBUG ("pagesize_g = %li; CONFIG_HZ = %i;",
586 pagesize_g, CONFIG_HZ);
587 /* #endif KERNEL_LINUX */
589 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD
590 /* no initialization */
591 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
593 #elif HAVE_PROCINFO_H
594 pagesize = getpagesize();
595 #endif /* HAVE_PROCINFO_H */
600 /* submit global state (e.g.: qty of zombies, running, etc..) */
601 static void ps_submit_state (const char *state, double value)
604 value_list_t vl = VALUE_LIST_INIT;
606 values[0].gauge = value;
610 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
611 sstrncpy (vl.plugin, "processes", sizeof (vl.plugin));
612 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
613 sstrncpy (vl.type, "ps_state", sizeof (vl.type));
614 sstrncpy (vl.type_instance, state, sizeof (vl.type_instance));
616 plugin_dispatch_values (&vl);
619 /* submit info about specific process (e.g.: memory taken, cpu usage, etc..) */
620 static void ps_submit_proc_list (procstat_t *ps)
623 value_list_t vl = VALUE_LIST_INIT;
627 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
628 sstrncpy (vl.plugin, "processes", sizeof (vl.plugin));
629 sstrncpy (vl.plugin_instance, ps->name, sizeof (vl.plugin_instance));
631 sstrncpy (vl.type, "ps_vm", sizeof (vl.type));
632 vl.values[0].gauge = ps->vmem_size;
634 plugin_dispatch_values (&vl);
636 sstrncpy (vl.type, "ps_rss", sizeof (vl.type));
637 vl.values[0].gauge = ps->vmem_rss;
639 plugin_dispatch_values (&vl);
641 sstrncpy (vl.type, "ps_stacksize", sizeof (vl.type));
642 vl.values[0].gauge = ps->stack_size;
644 plugin_dispatch_values (&vl);
646 sstrncpy (vl.type, "ps_cputime", sizeof (vl.type));
647 vl.values[0].counter = ps->cpu_user_counter;
648 vl.values[1].counter = ps->cpu_system_counter;
650 plugin_dispatch_values (&vl);
652 sstrncpy (vl.type, "ps_count", sizeof (vl.type));
653 vl.values[0].gauge = ps->num_proc;
654 vl.values[1].gauge = ps->num_lwp;
656 plugin_dispatch_values (&vl);
658 sstrncpy (vl.type, "ps_pagefaults", sizeof (vl.type));
659 vl.values[0].counter = ps->vmem_minflt_counter;
660 vl.values[1].counter = ps->vmem_majflt_counter;
662 plugin_dispatch_values (&vl);
664 if ( (ps->io_rchar != -1) && (ps->io_wchar != -1) )
666 sstrncpy (vl.type, "ps_disk_octets", sizeof (vl.type));
667 vl.values[0].derive = ps->io_rchar;
668 vl.values[1].derive = ps->io_wchar;
670 plugin_dispatch_values (&vl);
673 if ( (ps->io_syscr != -1) && (ps->io_syscw != -1) )
675 sstrncpy (vl.type, "ps_disk_ops", sizeof (vl.type));
676 vl.values[0].derive = ps->io_syscr;
677 vl.values[1].derive = ps->io_syscw;
679 plugin_dispatch_values (&vl);
682 DEBUG ("name = %s; num_proc = %lu; num_lwp = %lu; vmem_rss = %lu; "
683 "vmem_minflt_counter = %lu; vmem_majflt_counter = %lu; "
684 "cpu_user_counter = %lu; cpu_system_counter = %lu; "
685 "io_rchar = %"PRIi64"; io_wchar = %"PRIi64"; "
686 "io_syscr = %"PRIi64"; io_syscw = %"PRIi64";",
687 ps->name, ps->num_proc, ps->num_lwp, ps->vmem_rss,
688 ps->vmem_minflt_counter, ps->vmem_majflt_counter,
689 ps->cpu_user_counter, ps->cpu_system_counter,
690 ps->io_rchar, ps->io_wchar, ps->io_syscr, ps->io_syscw);
691 } /* void ps_submit_proc_list */
693 /* ------- additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
695 static int ps_read_tasks (int pid)
702 ssnprintf (dirname, sizeof (dirname), "/proc/%i/task", pid);
704 if ((dh = opendir (dirname)) == NULL)
706 DEBUG ("Failed to open directory `%s'", dirname);
710 while ((ent = readdir (dh)) != NULL)
712 if (!isdigit ((int) ent->d_name[0]))
719 return ((count >= 1) ? count : 1);
720 } /* int *ps_read_tasks */
722 static procstat_t *ps_read_io (int pid, procstat_t *ps)
731 ssnprintf (filename, sizeof (filename), "/proc/%i/io", pid);
732 if ((fh = fopen (filename, "r")) == NULL)
735 while (fgets (buffer, 1024, fh) != NULL)
737 derive_t *val = NULL;
741 if (strncasecmp (buffer, "rchar:", 6) == 0)
742 val = &(ps->io_rchar);
743 else if (strncasecmp (buffer, "wchar:", 6) == 0)
744 val = &(ps->io_wchar);
745 else if (strncasecmp (buffer, "syscr:", 6) == 0)
746 val = &(ps->io_syscr);
747 else if (strncasecmp (buffer, "syscw:", 6) == 0)
748 val = &(ps->io_syscw);
752 numfields = strsplit (buffer, fields, 8);
759 tmp = strtoll (fields[1], &endptr, /* base = */ 10);
760 if ((errno != 0) || (endptr == fields[1]))
763 *val = (derive_t) tmp;
764 } /* while (fgets) */
769 WARNING ("processes: fclose: %s",
770 sstrerror (errno, errbuf, sizeof (errbuf)));
774 } /* procstat_t *ps_read_io */
776 int ps_read_process (int pid, procstat_t *ps, char *state)
789 long long unsigned cpu_user_counter;
790 long long unsigned cpu_system_counter;
791 long long unsigned vmem_size;
792 long long unsigned vmem_rss;
793 long long unsigned stack_size;
795 memset (ps, 0, sizeof (procstat_t));
797 ssnprintf (filename, sizeof (filename), "/proc/%i/stat", pid);
799 i = read_file_contents (filename, buffer, sizeof(buffer) - 1);
804 fields_len = strsplit (buffer, fields, 64);
807 DEBUG ("processes plugin: ps_read_process (pid = %i):"
808 " `%s' has only %i fields..",
809 (int) pid, filename, fields_len);
813 /* copy the name, strip brackets in the process */
814 name_len = strlen (fields[1]) - 2;
815 if ((fields[1][0] != '(') || (fields[1][name_len + 1] != ')'))
817 DEBUG ("No brackets found in process name: `%s'", fields[1]);
820 fields[1] = fields[1] + 1;
821 fields[1][name_len] = '\0';
822 strncpy (ps->name, fields[1], PROCSTAT_NAME_LEN);
824 ppid = atoi (fields[3]);
826 *state = fields[2][0];
835 if ( (ps->num_lwp = ps_read_tasks (pid)) == -1 )
837 /* returns -1 => kernel 2.4 */
843 /* Leave the rest at zero if this is only a zombi */
844 if (ps->num_proc == 0)
846 DEBUG ("processes plugin: This is only a zombi: pid = %i; "
847 "name = %s;", pid, ps->name);
851 cpu_user_counter = atoll (fields[13]);
852 cpu_system_counter = atoll (fields[14]);
853 vmem_size = atoll (fields[22]);
854 vmem_rss = atoll (fields[23]);
855 ps->vmem_minflt_counter = atol (fields[9]);
856 ps->vmem_majflt_counter = atol (fields[11]);
859 unsigned long long stack_start = atoll (fields[27]);
860 unsigned long long stack_ptr = atoll (fields[28]);
862 stack_size = (stack_start > stack_ptr)
863 ? stack_start - stack_ptr
864 : stack_ptr - stack_start;
867 /* Convert jiffies to useconds */
868 cpu_user_counter = cpu_user_counter * 1000000 / CONFIG_HZ;
869 cpu_system_counter = cpu_system_counter * 1000000 / CONFIG_HZ;
870 vmem_rss = vmem_rss * pagesize_g;
872 ps->cpu_user_counter = (unsigned long) cpu_user_counter;
873 ps->cpu_system_counter = (unsigned long) cpu_system_counter;
874 ps->vmem_size = (unsigned long) vmem_size;
875 ps->vmem_rss = (unsigned long) vmem_rss;
876 ps->stack_size = (unsigned long) stack_size;
878 if ( (ps_read_io (pid, ps)) == NULL)
886 DEBUG("ps_read_process: not get io data for pid %i",pid);
891 } /* int ps_read_process (...) */
893 static char *ps_get_cmdline (pid_t pid, char *name, char *buf, size_t buf_len)
903 if ((pid < 1) || (NULL == buf) || (buf_len < 2))
906 ssnprintf (file, sizeof (file), "/proc/%u/cmdline", pid);
908 fd = open (file, O_RDONLY);
911 WARNING ("processes plugin: Failed to open `%s': %s.", file,
912 sstrerror (errno, errbuf, sizeof (errbuf)));
924 status = read (fd, (void *)buf_ptr, len);
929 if ((EAGAIN == errno) || (EINTR == errno))
932 WARNING ("processes plugin: Failed to read from `%s': %s.", file,
933 sstrerror (errno, errbuf, sizeof (errbuf)));
953 /* cmdline not available; e.g. kernel thread, zombie */
957 ssnprintf (buf, buf_len, "[%s]", name);
961 assert (n <= buf_len);
968 /* remove trailing whitespace */
969 while ((n > 0) && (isspace (buf[n]) || ('\0' == buf[n]))) {
974 /* arguments are separated by '\0' in /proc/<pid>/cmdline */
981 } /* char *ps_get_cmdline (...) */
983 static unsigned long read_fork_rate ()
987 unsigned long result = 0;
991 proc_stat = fopen("/proc/stat", "r");
992 if (proc_stat == NULL) {
994 ERROR ("processes plugin: fopen (/proc/stat) failed: %s",
995 sstrerror (errno, errbuf, sizeof (errbuf)));
999 while (fgets (buf, sizeof(buf), proc_stat) != NULL)
1003 numfields = strsplit(buf, fields, STATIC_ARRAY_SIZE (fields));
1007 if (strcmp ("processes", fields[0]) != 0)
1012 result = strtoul(fields[1], &endptr, 10);
1013 if ((endptr == fields[1]) || (errno != 0)) {
1014 ERROR ("processes plugin: Cannot parse fork rate: %s",
1028 static void ps_submit_fork_rate (unsigned long value)
1031 value_list_t vl = VALUE_LIST_INIT;
1033 values[0].derive = (derive_t) value;
1037 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
1038 sstrncpy (vl.plugin, "processes", sizeof (vl.plugin));
1039 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
1040 sstrncpy (vl.type, "fork_rate", sizeof (vl.type));
1041 sstrncpy (vl.type_instance, "", sizeof (vl.type_instance));
1043 plugin_dispatch_values (&vl);
1046 #endif /* KERNEL_LINUX */
1048 #if HAVE_THREAD_INFO
1049 static int mach_get_task_name (task_t t, int *pid, char *name, size_t name_max_len)
1053 struct kinfo_proc kp;
1058 mib[2] = KERN_PROC_PID;
1060 if (pid_for_task (t, pid) != KERN_SUCCESS)
1064 kp_size = sizeof (kp);
1065 if (sysctl (mib, 4, &kp, &kp_size, NULL, 0) != 0)
1068 if (name_max_len > (MAXCOMLEN + 1))
1069 name_max_len = MAXCOMLEN + 1;
1071 strncpy (name, kp.kp_proc.p_comm, name_max_len - 1);
1072 name[name_max_len - 1] = '\0';
1074 DEBUG ("pid = %i; name = %s;", *pid, name);
1076 /* We don't do the special handling for `p_comm == "LaunchCFMApp"' as
1077 * `top' does it, because it is a lot of work and only used when
1078 * debugging. -octo */
1082 #endif /* HAVE_THREAD_INFO */
1083 /* ------- end of additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
1085 /* do actual readings from kernel */
1086 static int ps_read (void)
1088 #if HAVE_THREAD_INFO
1089 kern_return_t status;
1092 processor_set_t port_pset_priv;
1095 task_array_t task_list;
1096 mach_msg_type_number_t task_list_len;
1099 char task_name[MAXCOMLEN + 1];
1102 thread_act_array_t thread_list;
1103 mach_msg_type_number_t thread_list_len;
1104 thread_basic_info_data_t thread_data;
1105 mach_msg_type_number_t thread_data_len;
1114 procstat_entry_t pse;
1119 * The Mach-concept is a little different from the traditional UNIX
1120 * concept: All the work is done in threads. Threads are contained in
1121 * `tasks'. Therefore, `task status' doesn't make much sense, since
1122 * it's actually a `thread status'.
1123 * Tasks are assigned to sets of processors, so that's where you go to
1126 for (pset = 0; pset < pset_list_len; pset++)
1128 if ((status = host_processor_set_priv (port_host_self,
1130 &port_pset_priv)) != KERN_SUCCESS)
1132 ERROR ("host_processor_set_priv failed: %s\n",
1133 mach_error_string (status));
1137 if ((status = processor_set_tasks (port_pset_priv,
1139 &task_list_len)) != KERN_SUCCESS)
1141 ERROR ("processor_set_tasks failed: %s\n",
1142 mach_error_string (status));
1143 mach_port_deallocate (port_task_self, port_pset_priv);
1147 for (task = 0; task < task_list_len; task++)
1150 if (mach_get_task_name (task_list[task],
1152 task_name, PROCSTAT_NAME_LEN) == 0)
1154 /* search for at least one match */
1155 for (ps = list_head_g; ps != NULL; ps = ps->next)
1156 /* FIXME: cmdline should be here instead of NULL */
1157 if (ps_list_match (task_name, NULL, ps) == 1)
1161 /* Collect more detailed statistics for this process */
1164 task_basic_info_data_t task_basic_info;
1165 mach_msg_type_number_t task_basic_info_len;
1166 task_events_info_data_t task_events_info;
1167 mach_msg_type_number_t task_events_info_len;
1168 task_absolutetime_info_data_t task_absolutetime_info;
1169 mach_msg_type_number_t task_absolutetime_info_len;
1171 memset (&pse, '\0', sizeof (pse));
1174 task_basic_info_len = TASK_BASIC_INFO_COUNT;
1175 status = task_info (task_list[task],
1177 (task_info_t) &task_basic_info,
1178 &task_basic_info_len);
1179 if (status != KERN_SUCCESS)
1181 ERROR ("task_info failed: %s",
1182 mach_error_string (status));
1183 continue; /* with next thread_list */
1186 task_events_info_len = TASK_EVENTS_INFO_COUNT;
1187 status = task_info (task_list[task],
1189 (task_info_t) &task_events_info,
1190 &task_events_info_len);
1191 if (status != KERN_SUCCESS)
1193 ERROR ("task_info failed: %s",
1194 mach_error_string (status));
1195 continue; /* with next thread_list */
1198 task_absolutetime_info_len = TASK_ABSOLUTETIME_INFO_COUNT;
1199 status = task_info (task_list[task],
1200 TASK_ABSOLUTETIME_INFO,
1201 (task_info_t) &task_absolutetime_info,
1202 &task_absolutetime_info_len);
1203 if (status != KERN_SUCCESS)
1205 ERROR ("task_info failed: %s",
1206 mach_error_string (status));
1207 continue; /* with next thread_list */
1211 pse.vmem_rss = task_basic_info.resident_size;
1213 pse.vmem_minflt_counter = task_events_info.cow_faults;
1214 pse.vmem_majflt_counter = task_events_info.faults;
1216 pse.cpu_user_counter = task_absolutetime_info.total_user;
1217 pse.cpu_system_counter = task_absolutetime_info.total_system;
1220 status = task_threads (task_list[task], &thread_list,
1222 if (status != KERN_SUCCESS)
1224 /* Apple's `top' treats this case a zombie. It
1225 * makes sense to some extend: A `zombie'
1226 * thread is nonsense, since the task/process
1229 DEBUG ("task_threads failed: %s",
1230 mach_error_string (status));
1231 if (task_list[task] != port_task_self)
1232 mach_port_deallocate (port_task_self,
1234 continue; /* with next task_list */
1237 for (thread = 0; thread < thread_list_len; thread++)
1239 thread_data_len = THREAD_BASIC_INFO_COUNT;
1240 status = thread_info (thread_list[thread],
1242 (thread_info_t) &thread_data,
1244 if (status != KERN_SUCCESS)
1246 ERROR ("thread_info failed: %s",
1247 mach_error_string (status));
1248 if (task_list[task] != port_task_self)
1249 mach_port_deallocate (port_task_self,
1250 thread_list[thread]);
1251 continue; /* with next thread_list */
1257 switch (thread_data.run_state)
1259 case TH_STATE_RUNNING:
1262 case TH_STATE_STOPPED:
1263 /* What exactly is `halted'? */
1264 case TH_STATE_HALTED:
1267 case TH_STATE_WAITING:
1270 case TH_STATE_UNINTERRUPTIBLE:
1273 /* There is no `zombie' case here,
1274 * since there are no zombie-threads.
1275 * There's only zombie tasks, which are
1278 WARNING ("Unknown thread status: %i",
1279 thread_data.run_state);
1281 } /* switch (thread_data.run_state) */
1283 if (task_list[task] != port_task_self)
1285 status = mach_port_deallocate (port_task_self,
1286 thread_list[thread]);
1287 if (status != KERN_SUCCESS)
1288 ERROR ("mach_port_deallocate failed: %s",
1289 mach_error_string (status));
1291 } /* for (thread_list) */
1293 if ((status = vm_deallocate (port_task_self,
1294 (vm_address_t) thread_list,
1295 thread_list_len * sizeof (thread_act_t)))
1298 ERROR ("vm_deallocate failed: %s",
1299 mach_error_string (status));
1302 thread_list_len = 0;
1304 /* Only deallocate the task port, if it isn't our own.
1305 * Don't know what would happen in that case, but this
1306 * is what Apple's top does.. ;) */
1307 if (task_list[task] != port_task_self)
1309 status = mach_port_deallocate (port_task_self,
1311 if (status != KERN_SUCCESS)
1312 ERROR ("mach_port_deallocate failed: %s",
1313 mach_error_string (status));
1317 /* FIXME: cmdline should be here instead of NULL */
1318 ps_list_add (task_name, NULL, &pse);
1319 } /* for (task_list) */
1321 if ((status = vm_deallocate (port_task_self,
1322 (vm_address_t) task_list,
1323 task_list_len * sizeof (task_t))) != KERN_SUCCESS)
1325 ERROR ("vm_deallocate failed: %s",
1326 mach_error_string (status));
1331 if ((status = mach_port_deallocate (port_task_self, port_pset_priv))
1334 ERROR ("mach_port_deallocate failed: %s",
1335 mach_error_string (status));
1337 } /* for (pset_list) */
1339 ps_submit_state ("running", running);
1340 ps_submit_state ("sleeping", sleeping);
1341 ps_submit_state ("zombies", zombies);
1342 ps_submit_state ("stopped", stopped);
1343 ps_submit_state ("blocked", blocked);
1345 for (ps = list_head_g; ps != NULL; ps = ps->next)
1346 ps_submit_proc_list (ps);
1347 /* #endif HAVE_THREAD_INFO */
1361 char cmdline[ARG_MAX];
1365 procstat_entry_t pse;
1368 unsigned long fork_rate;
1372 running = sleeping = zombies = stopped = paging = blocked = 0;
1375 if ((proc = opendir ("/proc")) == NULL)
1378 ERROR ("Cannot open `/proc': %s",
1379 sstrerror (errno, errbuf, sizeof (errbuf)));
1383 while ((ent = readdir (proc)) != NULL)
1385 if (!isdigit (ent->d_name[0]))
1388 if ((pid = atoi (ent->d_name)) < 1)
1391 status = ps_read_process (pid, &ps, &state);
1394 DEBUG ("ps_read_process failed: %i", status);
1401 pse.num_proc = ps.num_proc;
1402 pse.num_lwp = ps.num_lwp;
1403 pse.vmem_size = ps.vmem_size;
1404 pse.vmem_rss = ps.vmem_rss;
1405 pse.stack_size = ps.stack_size;
1407 pse.vmem_minflt = 0;
1408 pse.vmem_minflt_counter = ps.vmem_minflt_counter;
1409 pse.vmem_majflt = 0;
1410 pse.vmem_majflt_counter = ps.vmem_majflt_counter;
1413 pse.cpu_user_counter = ps.cpu_user_counter;
1415 pse.cpu_system_counter = ps.cpu_system_counter;
1417 pse.io_rchar = ps.io_rchar;
1418 pse.io_wchar = ps.io_wchar;
1419 pse.io_syscr = ps.io_syscr;
1420 pse.io_syscw = ps.io_syscw;
1424 case 'R': running++; break;
1425 case 'S': sleeping++; break;
1426 case 'D': blocked++; break;
1427 case 'Z': zombies++; break;
1428 case 'T': stopped++; break;
1429 case 'W': paging++; break;
1432 ps_list_add (ps.name,
1433 ps_get_cmdline (pid, ps.name, cmdline, sizeof (cmdline)),
1439 ps_submit_state ("running", running);
1440 ps_submit_state ("sleeping", sleeping);
1441 ps_submit_state ("zombies", zombies);
1442 ps_submit_state ("stopped", stopped);
1443 ps_submit_state ("paging", paging);
1444 ps_submit_state ("blocked", blocked);
1446 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1447 ps_submit_proc_list (ps_ptr);
1449 fork_rate = read_fork_rate();
1450 if (fork_rate != ULONG_MAX)
1451 ps_submit_fork_rate(fork_rate);
1452 /* #endif KERNEL_LINUX */
1454 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD
1465 char cmdline[ARG_MAX];
1467 struct kinfo_proc *procs; /* array of processes */
1469 int count; /* returns number of processes */
1473 procstat_entry_t pse;
1477 /* Open the kvm interface, get a descriptor */
1478 kd = kvm_open (NULL, NULL, NULL, 0, errbuf);
1481 ERROR ("processes plugin: Cannot open kvm interface: %s",
1486 /* Get the list of processes. */
1487 procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, &count);
1491 ERROR ("processes plugin: Cannot get kvm processes list: %s",
1496 /* Iterate through the processes in kinfo_proc */
1497 for (i = 0; i < count; i++)
1499 /* retrieve the arguments */
1503 argv = kvm_getargv (kd, (const struct kinfo_proc *) &(procs[i]), 0);
1510 while (argv[argc] != NULL)
1513 status = strjoin (cmdline, sizeof (cmdline),
1518 WARNING ("processes plugin: Command line did "
1519 "not fit into buffer.");
1523 cmdline_ptr = &cmdline[0];
1527 pse.id = procs[i].ki_pid;
1531 pse.num_lwp = procs[i].ki_numthreads;
1533 pse.vmem_size = procs[i].ki_size;
1534 pse.vmem_rss = procs[i].ki_rssize * getpagesize();
1535 pse.stack_size = procs[i].ki_ssize * getpagesize();
1536 pse.vmem_minflt = 0;
1537 pse.vmem_minflt_counter = procs[i].ki_rusage.ru_minflt;
1538 pse.vmem_majflt = 0;
1539 pse.vmem_majflt_counter = procs[i].ki_rusage.ru_majflt;
1542 pse.cpu_user_counter = procs[i].ki_rusage.ru_utime.tv_sec
1544 + procs[i].ki_rusage.ru_utime.tv_usec;
1546 pse.cpu_system_counter = procs[i].ki_rusage.ru_stime.tv_sec
1548 + procs[i].ki_rusage.ru_stime.tv_usec;
1556 switch (procs[i].ki_stat)
1558 case SSTOP: stopped++; break;
1559 case SSLEEP: sleeping++; break;
1560 case SRUN: running++; break;
1561 case SIDL: idle++; break;
1562 case SWAIT: wait++; break;
1563 case SLOCK: blocked++; break;
1564 case SZOMB: zombies++; break;
1567 ps_list_add (procs[i].ki_comm, cmdline_ptr, &pse);
1572 ps_submit_state ("running", running);
1573 ps_submit_state ("sleeping", sleeping);
1574 ps_submit_state ("zombies", zombies);
1575 ps_submit_state ("stopped", stopped);
1576 ps_submit_state ("blocked", blocked);
1577 ps_submit_state ("idle", idle);
1578 ps_submit_state ("wait", wait);
1580 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1581 ps_submit_proc_list (ps_ptr);
1582 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
1584 #elif HAVE_PROCINFO_H
1597 procstat_entry_t pse;
1600 while ((nprocs = getprocs64 (procentry, sizeof(struct procentry64),
1601 /* fdsinfo = */ NULL, sizeof(struct fdsinfo64),
1602 &pindex, MAXPROCENTRY)) > 0)
1606 for (i = 0; i < nprocs; i++)
1610 char arglist[MAXARGLN+1];
1614 if (procentry[i].pi_state == SNONE) continue;
1615 /* if (procentry[i].pi_state == SZOMB) FIXME */
1617 cmdline = procentry[i].pi_comm;
1618 cargs = procentry[i].pi_comm;
1619 if ( procentry[i].pi_flags & SKPROC )
1621 if (procentry[i].pi_pid == 0)
1622 cmdline = "swapper";
1627 if (getargs(&procentry[i], sizeof(struct procentry64), arglist, MAXARGLN) >= 0)
1632 while (++n < MAXARGLN)
1634 if (arglist[n] == '\0')
1636 if (arglist[n+1] == '\0')
1645 pse.id = procentry[i].pi_pid;
1647 pse.num_lwp = procentry[i].pi_thcount;
1651 while ((nthreads = getthrds64(procentry[i].pi_pid,
1652 thrdentry, sizeof(struct thrdentry64),
1653 &thindex, MAXTHRDENTRY)) > 0)
1657 for (j=0; j< nthreads; j++)
1659 switch (thrdentry[j].ti_state)
1661 /* case TSNONE: break; */
1662 case TSIDL: blocked++; break; /* FIXME is really blocked */
1663 case TSRUN: running++; break;
1664 case TSSLEEP: sleeping++; break;
1665 case TSSWAP: paging++; break;
1666 case TSSTOP: stopped++; break;
1667 case TSZOMB: zombies++; break;
1670 if (nthreads < MAXTHRDENTRY)
1675 /* tv_usec is nanosec ??? */
1676 pse.cpu_user_counter = procentry[i].pi_ru.ru_utime.tv_sec * 1000000 +
1677 procentry[i].pi_ru.ru_utime.tv_usec / 1000;
1680 /* tv_usec is nanosec ??? */
1681 pse.cpu_system_counter = procentry[i].pi_ru.ru_stime.tv_sec * 1000000 +
1682 procentry[i].pi_ru.ru_stime.tv_usec / 1000;
1684 pse.vmem_minflt = 0;
1685 pse.vmem_minflt_counter = procentry[i].pi_minflt;
1686 pse.vmem_majflt = 0;
1687 pse.vmem_majflt_counter = procentry[i].pi_majflt;
1689 pse.vmem_size = procentry[i].pi_tsize + procentry[i].pi_dvm * pagesize;
1690 pse.vmem_rss = (procentry[i].pi_drss + procentry[i].pi_trss) * pagesize;
1698 ps_list_add (cmdline, cargs, &pse);
1699 } /* for (i = 0 .. nprocs) */
1701 if (nprocs < MAXPROCENTRY)
1703 } /* while (getprocs64() > 0) */
1704 ps_submit_state ("running", running);
1705 ps_submit_state ("sleeping", sleeping);
1706 ps_submit_state ("zombies", zombies);
1707 ps_submit_state ("stopped", stopped);
1708 ps_submit_state ("paging", paging);
1709 ps_submit_state ("blocked", blocked);
1711 for (ps = list_head_g; ps != NULL; ps = ps->next)
1712 ps_submit_proc_list (ps);
1713 #endif /* HAVE_PROCINFO_H */
1718 void module_register (void)
1720 plugin_register_config ("processes", ps_config,
1721 config_keys, config_keys_num);
1722 plugin_register_init ("processes", ps_init);
1723 plugin_register_read ("processes", ps_read);
1724 } /* void module_register */