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
9 * Copyright (C) 2010 Clément Stenac
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 * Lyonel Vincent <lyonel at ezix.org>
27 * Florian octo Forster <octo at verplant.org>
28 * Oleg King <king2 at kaluga.ru>
29 * Sebastian Harl <sh at tokkee.org>
30 * Andrés J. Díaz <ajdiaz at connectical.com>
32 * Clément Stenac <clement.stenac at diwi.org>
38 #include "configfile.h"
40 /* Include header files for the mach system, if they exist.. */
42 # if HAVE_MACH_MACH_INIT_H
43 # include <mach/mach_init.h>
45 # if HAVE_MACH_HOST_PRIV_H
46 # include <mach/host_priv.h>
48 # if HAVE_MACH_MACH_ERROR_H
49 # include <mach/mach_error.h>
51 # if HAVE_MACH_MACH_HOST_H
52 # include <mach/mach_host.h>
54 # if HAVE_MACH_MACH_PORT_H
55 # include <mach/mach_port.h>
57 # if HAVE_MACH_MACH_TYPES_H
58 # include <mach/mach_types.h>
60 # if HAVE_MACH_MESSAGE_H
61 # include <mach/message.h>
63 # if HAVE_MACH_PROCESSOR_SET_H
64 # include <mach/processor_set.h>
67 # include <mach/task.h>
69 # if HAVE_MACH_THREAD_ACT_H
70 # include <mach/thread_act.h>
72 # if HAVE_MACH_VM_REGION_H
73 # include <mach/vm_region.h>
75 # if HAVE_MACH_VM_MAP_H
76 # include <mach/vm_map.h>
78 # if HAVE_MACH_VM_PROT_H
79 # include <mach/vm_prot.h>
81 # if HAVE_SYS_SYSCTL_H
82 # include <sys/sysctl.h>
84 /* #endif HAVE_THREAD_INFO */
87 # if HAVE_LINUX_CONFIG_H
88 # include <linux/config.h>
91 # define CONFIG_HZ 100
93 /* #endif KERNEL_LINUX */
95 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD
97 # include <sys/param.h>
98 # include <sys/sysctl.h>
99 # include <sys/user.h>
100 # include <sys/proc.h>
101 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
103 #elif HAVE_PROCINFO_H
104 # include <procinfo.h>
105 # include <sys/types.h>
107 #define MAXPROCENTRY 32
108 #define MAXTHRDENTRY 16
109 #define MAXARGLN 1024
110 /* #endif HAVE_PROCINFO_H */
113 # error "No applicable input method."
121 # define ARG_MAX 4096
124 typedef struct procstat_entry_s
129 unsigned long num_proc;
130 unsigned long num_lwp;
131 unsigned long vmem_size;
132 unsigned long vmem_rss;
133 unsigned long vmem_data;
134 unsigned long vmem_code;
135 unsigned long stack_size;
137 unsigned long vmem_minflt;
138 unsigned long vmem_majflt;
139 unsigned long vmem_minflt_counter;
140 unsigned long vmem_majflt_counter;
142 unsigned long cpu_user;
143 unsigned long cpu_system;
144 unsigned long cpu_user_counter;
145 unsigned long cpu_system_counter;
153 struct procstat_entry_s *next;
156 #define PROCSTAT_NAME_LEN 256
157 typedef struct procstat
159 char name[PROCSTAT_NAME_LEN];
164 unsigned long num_proc;
165 unsigned long num_lwp;
166 unsigned long vmem_size;
167 unsigned long vmem_rss;
168 unsigned long vmem_data;
169 unsigned long vmem_code;
170 unsigned long stack_size;
172 unsigned long vmem_minflt_counter;
173 unsigned long vmem_majflt_counter;
175 unsigned long cpu_user_counter;
176 unsigned long cpu_system_counter;
184 struct procstat *next;
185 struct procstat_entry_s *instances;
188 static procstat_t *list_head_g = NULL;
191 static mach_port_t port_host_self;
192 static mach_port_t port_task_self;
194 static processor_set_name_array_t pset_list;
195 static mach_msg_type_number_t pset_list_len;
196 /* #endif HAVE_THREAD_INFO */
199 static long pagesize_g;
200 /* #endif KERNEL_LINUX */
202 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD
203 /* no global variables */
204 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
206 #elif HAVE_PROCINFO_H
207 static struct procentry64 procentry[MAXPROCENTRY];
208 static struct thrdentry64 thrdentry[MAXTHRDENTRY];
211 #ifndef _AIXVERSION_610
212 int getprocs64 (void *procsinfo, int sizproc, void *fdsinfo, int sizfd, pid_t *index, int count);
213 int getthrds64( pid_t, void *, int, tid64_t *, int );
215 int getargs (struct procentry64 *processBuffer, int bufferLen, char *argsBuffer, int argsLen);
216 #endif /* HAVE_PROCINFO_H */
218 /* put name of process from config to list_head_g tree
219 list_head_g is a list of 'procstat_t' structs with
220 processes names we want to watch */
221 static void ps_list_register (const char *name, const char *regexp)
227 new = (procstat_t *) malloc (sizeof (procstat_t));
230 ERROR ("processes plugin: ps_list_register: malloc failed.");
233 memset (new, 0, sizeof (procstat_t));
234 sstrncpy (new->name, name, sizeof (new->name));
239 DEBUG ("ProcessMatch: adding \"%s\" as criteria to process %s.", regexp, name);
240 new->re = (regex_t *) malloc (sizeof (regex_t));
243 ERROR ("processes plugin: ps_list_register: malloc failed.");
248 status = regcomp (new->re, regexp, REG_EXTENDED | REG_NOSUB);
251 DEBUG ("ProcessMatch: compiling the regular expression \"%s\" failed.", regexp);
259 ERROR ("processes plugin: ps_list_register: "
260 "Regular expression \"%s\" found in config "
261 "file, but support for regular expressions "
262 "has been disabled at compile time.",
269 for (ptr = list_head_g; ptr != NULL; ptr = ptr->next)
271 if (strcmp (ptr->name, name) == 0)
273 WARNING ("processes plugin: You have configured more "
274 "than one `Process' or "
275 "`ProcessMatch' with the same name. "
276 "All but the first setting will be "
283 if (ptr->next == NULL)
291 } /* void ps_list_register */
293 /* try to match name against entry, returns 1 if success */
294 static int ps_list_match (const char *name, const char *cmdline, procstat_t *ps)
303 if ((str == NULL) || (str[0] == 0))
306 assert (str != NULL);
308 status = regexec (ps->re, str,
317 if (strcmp (ps->name, name) == 0)
321 } /* int ps_list_match */
323 /* add process entry to 'instances' of process 'name' (or refresh it) */
324 static void ps_list_add (const char *name, const char *cmdline, procstat_entry_t *entry)
327 procstat_entry_t *pse;
332 for (ps = list_head_g; ps != NULL; ps = ps->next)
334 if ((ps_list_match (name, cmdline, ps)) == 0)
337 for (pse = ps->instances; pse != NULL; pse = pse->next)
338 if ((pse->id == entry->id) || (pse->next == NULL))
341 if ((pse == NULL) || (pse->id != entry->id))
343 procstat_entry_t *new;
345 new = (procstat_entry_t *) malloc (sizeof (procstat_entry_t));
348 memset (new, 0, sizeof (procstat_entry_t));
360 pse->num_proc = entry->num_proc;
361 pse->num_lwp = entry->num_lwp;
362 pse->vmem_size = entry->vmem_size;
363 pse->vmem_rss = entry->vmem_rss;
364 pse->vmem_data = entry->vmem_data;
365 pse->vmem_code = entry->vmem_code;
366 pse->stack_size = entry->stack_size;
367 pse->io_rchar = entry->io_rchar;
368 pse->io_wchar = entry->io_wchar;
369 pse->io_syscr = entry->io_syscr;
370 pse->io_syscw = entry->io_syscw;
372 ps->num_proc += pse->num_proc;
373 ps->num_lwp += pse->num_lwp;
374 ps->vmem_size += pse->vmem_size;
375 ps->vmem_rss += pse->vmem_rss;
376 ps->vmem_data += pse->vmem_data;
377 ps->vmem_code += pse->vmem_code;
378 ps->stack_size += pse->stack_size;
380 ps->io_rchar += ((pse->io_rchar == -1)?0:pse->io_rchar);
381 ps->io_wchar += ((pse->io_wchar == -1)?0:pse->io_wchar);
382 ps->io_syscr += ((pse->io_syscr == -1)?0:pse->io_syscr);
383 ps->io_syscw += ((pse->io_syscw == -1)?0:pse->io_syscw);
385 if ((entry->vmem_minflt_counter == 0)
386 && (entry->vmem_majflt_counter == 0))
388 pse->vmem_minflt_counter += entry->vmem_minflt;
389 pse->vmem_minflt = entry->vmem_minflt;
391 pse->vmem_majflt_counter += entry->vmem_majflt;
392 pse->vmem_majflt = entry->vmem_majflt;
396 if (entry->vmem_minflt_counter < pse->vmem_minflt_counter)
398 pse->vmem_minflt = entry->vmem_minflt_counter
399 + (ULONG_MAX - pse->vmem_minflt_counter);
403 pse->vmem_minflt = entry->vmem_minflt_counter - pse->vmem_minflt_counter;
405 pse->vmem_minflt_counter = entry->vmem_minflt_counter;
407 if (entry->vmem_majflt_counter < pse->vmem_majflt_counter)
409 pse->vmem_majflt = entry->vmem_majflt_counter
410 + (ULONG_MAX - pse->vmem_majflt_counter);
414 pse->vmem_majflt = entry->vmem_majflt_counter - pse->vmem_majflt_counter;
416 pse->vmem_majflt_counter = entry->vmem_majflt_counter;
419 ps->vmem_minflt_counter += pse->vmem_minflt;
420 ps->vmem_majflt_counter += pse->vmem_majflt;
422 if ((entry->cpu_user_counter == 0)
423 && (entry->cpu_system_counter == 0))
425 pse->cpu_user_counter += entry->cpu_user;
426 pse->cpu_user = entry->cpu_user;
428 pse->cpu_system_counter += entry->cpu_system;
429 pse->cpu_system = entry->cpu_system;
433 if (entry->cpu_user_counter < pse->cpu_user_counter)
435 pse->cpu_user = entry->cpu_user_counter
436 + (ULONG_MAX - pse->cpu_user_counter);
440 pse->cpu_user = entry->cpu_user_counter - pse->cpu_user_counter;
442 pse->cpu_user_counter = entry->cpu_user_counter;
444 if (entry->cpu_system_counter < pse->cpu_system_counter)
446 pse->cpu_system = entry->cpu_system_counter
447 + (ULONG_MAX - pse->cpu_system_counter);
451 pse->cpu_system = entry->cpu_system_counter - pse->cpu_system_counter;
453 pse->cpu_system_counter = entry->cpu_system_counter;
456 ps->cpu_user_counter += pse->cpu_user;
457 ps->cpu_system_counter += pse->cpu_system;
461 /* remove old entries from instances of processes in list_head_g */
462 static void ps_list_reset (void)
465 procstat_entry_t *pse;
466 procstat_entry_t *pse_prev;
468 for (ps = list_head_g; ps != NULL; ps = ps->next)
488 DEBUG ("Removing this procstat entry cause it's too old: "
489 "id = %lu; name = %s;",
492 if (pse_prev == NULL)
494 ps->instances = pse->next;
500 pse_prev->next = pse->next;
502 pse = pse_prev->next;
511 } /* while (pse != NULL) */
512 } /* for (ps = list_head_g; ps != NULL; ps = ps->next) */
515 /* put all pre-defined 'Process' names from config to list_head_g tree */
516 static int ps_config (oconfig_item_t *ci)
520 for (i = 0; i < ci->children_num; ++i) {
521 oconfig_item_t *c = ci->children + i;
523 if (strcasecmp (c->key, "Process") == 0)
525 if ((c->values_num != 1)
526 || (OCONFIG_TYPE_STRING != c->values[0].type)) {
527 ERROR ("processes plugin: `Process' expects exactly "
528 "one string argument (got %i).",
533 if (c->children_num != 0) {
534 WARNING ("processes plugin: the `Process' config option "
535 "does not expect any child elements -- ignoring "
536 "content (%i elements) of the <Process '%s'> block.",
537 c->children_num, c->values[0].value.string);
540 ps_list_register (c->values[0].value.string, NULL);
542 else if (strcasecmp (c->key, "ProcessMatch") == 0)
544 if ((c->values_num != 2)
545 || (OCONFIG_TYPE_STRING != c->values[0].type)
546 || (OCONFIG_TYPE_STRING != c->values[1].type))
548 ERROR ("processes plugin: `ProcessMatch' needs exactly "
549 "two string arguments (got %i).",
554 if (c->children_num != 0) {
555 WARNING ("processes plugin: the `ProcessMatch' config option "
556 "does not expect any child elements -- ignoring "
557 "content (%i elements) of the <ProcessMatch '%s' '%s'> "
558 "block.", c->children_num, c->values[0].value.string,
559 c->values[1].value.string);
562 ps_list_register (c->values[0].value.string,
563 c->values[1].value.string);
567 ERROR ("processes plugin: The `%s' configuration option is not "
568 "understood and will be ignored.", c->key);
576 static int ps_init (void)
579 kern_return_t status;
581 port_host_self = mach_host_self ();
582 port_task_self = mach_task_self ();
584 if (pset_list != NULL)
586 vm_deallocate (port_task_self,
587 (vm_address_t) pset_list,
588 pset_list_len * sizeof (processor_set_t));
593 if ((status = host_processor_sets (port_host_self,
595 &pset_list_len)) != KERN_SUCCESS)
597 ERROR ("host_processor_sets failed: %s\n",
598 mach_error_string (status));
603 /* #endif HAVE_THREAD_INFO */
606 pagesize_g = sysconf(_SC_PAGESIZE);
607 DEBUG ("pagesize_g = %li; CONFIG_HZ = %i;",
608 pagesize_g, CONFIG_HZ);
609 /* #endif KERNEL_LINUX */
611 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD
612 /* no initialization */
613 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
615 #elif HAVE_PROCINFO_H
616 pagesize = getpagesize();
617 #endif /* HAVE_PROCINFO_H */
622 /* submit global state (e.g.: qty of zombies, running, etc..) */
623 static void ps_submit_state (const char *state, double value)
626 value_list_t vl = VALUE_LIST_INIT;
628 values[0].gauge = value;
632 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
633 sstrncpy (vl.plugin, "processes", sizeof (vl.plugin));
634 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
635 sstrncpy (vl.type, "ps_state", sizeof (vl.type));
636 sstrncpy (vl.type_instance, state, sizeof (vl.type_instance));
638 plugin_dispatch_values (&vl);
641 /* submit info about specific process (e.g.: memory taken, cpu usage, etc..) */
642 static void ps_submit_proc_list (procstat_t *ps)
645 value_list_t vl = VALUE_LIST_INIT;
649 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
650 sstrncpy (vl.plugin, "processes", sizeof (vl.plugin));
651 sstrncpy (vl.plugin_instance, ps->name, sizeof (vl.plugin_instance));
653 sstrncpy (vl.type, "ps_vm", sizeof (vl.type));
654 vl.values[0].gauge = ps->vmem_size;
656 plugin_dispatch_values (&vl);
658 sstrncpy (vl.type, "ps_rss", sizeof (vl.type));
659 vl.values[0].gauge = ps->vmem_rss;
661 plugin_dispatch_values (&vl);
663 sstrncpy (vl.type, "ps_data", sizeof (vl.type));
664 vl.values[0].gauge = ps->vmem_data;
666 plugin_dispatch_values (&vl);
668 sstrncpy (vl.type, "ps_code", sizeof (vl.type));
669 vl.values[0].gauge = ps->vmem_code;
671 plugin_dispatch_values (&vl);
673 sstrncpy (vl.type, "ps_stacksize", sizeof (vl.type));
674 vl.values[0].gauge = ps->stack_size;
676 plugin_dispatch_values (&vl);
678 sstrncpy (vl.type, "ps_cputime", sizeof (vl.type));
679 vl.values[0].counter = ps->cpu_user_counter;
680 vl.values[1].counter = ps->cpu_system_counter;
682 plugin_dispatch_values (&vl);
684 sstrncpy (vl.type, "ps_count", sizeof (vl.type));
685 vl.values[0].gauge = ps->num_proc;
686 vl.values[1].gauge = ps->num_lwp;
688 plugin_dispatch_values (&vl);
690 sstrncpy (vl.type, "ps_pagefaults", sizeof (vl.type));
691 vl.values[0].counter = ps->vmem_minflt_counter;
692 vl.values[1].counter = ps->vmem_majflt_counter;
694 plugin_dispatch_values (&vl);
696 if ( (ps->io_rchar != -1) && (ps->io_wchar != -1) )
698 sstrncpy (vl.type, "ps_disk_octets", sizeof (vl.type));
699 vl.values[0].derive = ps->io_rchar;
700 vl.values[1].derive = ps->io_wchar;
702 plugin_dispatch_values (&vl);
705 if ( (ps->io_syscr != -1) && (ps->io_syscw != -1) )
707 sstrncpy (vl.type, "ps_disk_ops", sizeof (vl.type));
708 vl.values[0].derive = ps->io_syscr;
709 vl.values[1].derive = ps->io_syscw;
711 plugin_dispatch_values (&vl);
714 DEBUG ("name = %s; num_proc = %lu; num_lwp = %lu; "
715 "vmem_size = %lu; vmem_rss = %lu; vmem_data = %lu; "
717 "vmem_minflt_counter = %lu; vmem_majflt_counter = %lu; "
718 "cpu_user_counter = %lu; cpu_system_counter = %lu; "
719 "io_rchar = %"PRIi64"; io_wchar = %"PRIi64"; "
720 "io_syscr = %"PRIi64"; io_syscw = %"PRIi64";",
721 ps->name, ps->num_proc, ps->num_lwp,
722 ps->vmem_size, ps->vmem_rss,
723 ps->vmem_data, ps->vmem_code,
724 ps->vmem_minflt_counter, ps->vmem_majflt_counter,
725 ps->cpu_user_counter, ps->cpu_system_counter,
726 ps->io_rchar, ps->io_wchar, ps->io_syscr, ps->io_syscw);
727 } /* void ps_submit_proc_list */
729 /* ------- additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
731 static int ps_read_tasks (int pid)
738 ssnprintf (dirname, sizeof (dirname), "/proc/%i/task", pid);
740 if ((dh = opendir (dirname)) == NULL)
742 DEBUG ("Failed to open directory `%s'", dirname);
746 while ((ent = readdir (dh)) != NULL)
748 if (!isdigit ((int) ent->d_name[0]))
755 return ((count >= 1) ? count : 1);
756 } /* int *ps_read_tasks */
758 /* Read advanced virtual memory data from /proc/pid/status */
759 static procstat_t *ps_read_vmem (int pid, procstat_t *ps)
764 unsigned long long lib = 0;
765 unsigned long long exe = 0;
766 unsigned long long data = 0;
770 ssnprintf (filename, sizeof (filename), "/proc/%i/status", pid);
771 if ((fh = fopen (filename, "r")) == NULL)
774 while (fgets (buffer, sizeof(buffer), fh) != NULL)
779 if (strncmp (buffer, "Vm", 2) != 0)
782 numfields = strsplit (buffer, fields,
783 STATIC_ARRAY_SIZE (fields));
790 tmp = strtoll (fields[1], &endptr, /* base = */ 10);
791 if ((errno == 0) && (endptr != fields[1]))
793 if (strncmp (buffer, "VmData", 6) == 0)
797 else if (strncmp (buffer, "VmLib", 5) == 0)
801 else if (strncmp(buffer, "VmExe", 5) == 0)
806 } /* while (fgets) */
811 WARNING ("processes: fclose: %s",
812 sstrerror (errno, errbuf, sizeof (errbuf)));
815 ps->vmem_data = data * 1024;
816 ps->vmem_code = (exe + lib) * 1024;
819 } /* procstat_t *ps_read_vmem */
821 static procstat_t *ps_read_io (int pid, procstat_t *ps)
830 ssnprintf (filename, sizeof (filename), "/proc/%i/io", pid);
831 if ((fh = fopen (filename, "r")) == NULL)
834 while (fgets (buffer, sizeof (buffer), fh) != NULL)
836 derive_t *val = NULL;
840 if (strncasecmp (buffer, "rchar:", 6) == 0)
841 val = &(ps->io_rchar);
842 else if (strncasecmp (buffer, "wchar:", 6) == 0)
843 val = &(ps->io_wchar);
844 else if (strncasecmp (buffer, "syscr:", 6) == 0)
845 val = &(ps->io_syscr);
846 else if (strncasecmp (buffer, "syscw:", 6) == 0)
847 val = &(ps->io_syscw);
851 numfields = strsplit (buffer, fields,
852 STATIC_ARRAY_SIZE (fields));
859 tmp = strtoll (fields[1], &endptr, /* base = */ 10);
860 if ((errno != 0) || (endptr == fields[1]))
863 *val = (derive_t) tmp;
864 } /* while (fgets) */
869 WARNING ("processes: fclose: %s",
870 sstrerror (errno, errbuf, sizeof (errbuf)));
874 } /* procstat_t *ps_read_io */
876 int ps_read_process (int pid, procstat_t *ps, char *state)
887 size_t name_start_pos;
891 long long unsigned cpu_user_counter;
892 long long unsigned cpu_system_counter;
893 long long unsigned vmem_size;
894 long long unsigned vmem_rss;
895 long long unsigned stack_size;
897 memset (ps, 0, sizeof (procstat_t));
899 ssnprintf (filename, sizeof (filename), "/proc/%i/stat", pid);
901 buffer_len = read_file_contents (filename,
902 buffer, sizeof(buffer) - 1);
905 buffer[buffer_len] = 0;
907 /* The name of the process is enclosed in parens. Since the name can
908 * contain parens itself, spaces, numbers and pretty much everything
909 * else, use these to determine the process name. We don't use
910 * strchr(3) and strrchr(3) to avoid pointer arithmetic which would
911 * otherwise be required to determine name_len. */
913 while ((buffer[name_start_pos] != '(')
914 && (name_start_pos < buffer_len))
917 name_end_pos = buffer_len;
918 while ((buffer[name_end_pos] != ')')
919 && (name_end_pos > 0))
922 /* Either '(' or ')' is not found or they are in the wrong order.
923 * Anyway, something weird that shouldn't happen ever. */
924 if (name_start_pos >= name_end_pos)
926 ERROR ("processes plugin: name_start_pos = %zu >= name_end_pos = %zu",
927 name_start_pos, name_end_pos);
931 name_len = (name_end_pos - name_start_pos) - 1;
932 if (name_len >= sizeof (ps->name))
933 name_len = sizeof (ps->name) - 1;
935 sstrncpy (ps->name, &buffer[name_start_pos + 1], name_len + 1);
937 if ((buffer_len - name_end_pos) < 2)
939 buffer_ptr = &buffer[name_end_pos + 2];
941 fields_len = strsplit (buffer_ptr, fields, STATIC_ARRAY_SIZE (fields));
944 DEBUG ("processes plugin: ps_read_process (pid = %i):"
945 " `%s' has only %i fields..",
946 (int) pid, filename, fields_len);
950 *state = fields[0][0];
959 if ( (ps->num_lwp = ps_read_tasks (pid)) == -1 )
961 /* returns -1 => kernel 2.4 */
967 /* Leave the rest at zero if this is only a zombi */
968 if (ps->num_proc == 0)
970 DEBUG ("processes plugin: This is only a zombi: pid = %i; "
971 "name = %s;", pid, ps->name);
975 cpu_user_counter = atoll (fields[11]);
976 cpu_system_counter = atoll (fields[12]);
977 vmem_size = atoll (fields[20]);
978 vmem_rss = atoll (fields[21]);
979 ps->vmem_minflt_counter = atol (fields[7]);
980 ps->vmem_majflt_counter = atol (fields[9]);
983 unsigned long long stack_start = atoll (fields[25]);
984 unsigned long long stack_ptr = atoll (fields[26]);
986 stack_size = (stack_start > stack_ptr)
987 ? stack_start - stack_ptr
988 : stack_ptr - stack_start;
991 /* Convert jiffies to useconds */
992 cpu_user_counter = cpu_user_counter * 1000000 / CONFIG_HZ;
993 cpu_system_counter = cpu_system_counter * 1000000 / CONFIG_HZ;
994 vmem_rss = vmem_rss * pagesize_g;
996 if ( (ps_read_vmem(pid, ps)) == NULL)
1001 DEBUG("ps_read_process: did not get vmem data for pid %i",pid);
1004 ps->cpu_user_counter = (unsigned long) cpu_user_counter;
1005 ps->cpu_system_counter = (unsigned long) cpu_system_counter;
1006 ps->vmem_size = (unsigned long) vmem_size;
1007 ps->vmem_rss = (unsigned long) vmem_rss;
1008 ps->stack_size = (unsigned long) stack_size;
1010 if ( (ps_read_io (pid, ps)) == NULL)
1018 DEBUG("ps_read_process: not get io data for pid %i",pid);
1023 } /* int ps_read_process (...) */
1025 static char *ps_get_cmdline (pid_t pid, char *name, char *buf, size_t buf_len)
1030 char file[PATH_MAX];
1035 if ((pid < 1) || (NULL == buf) || (buf_len < 2))
1038 ssnprintf (file, sizeof (file), "/proc/%u/cmdline",
1039 (unsigned int) pid);
1042 fd = open (file, O_RDONLY);
1045 /* ENOENT means the process exited while we were handling it.
1046 * Don't complain about this, it only fills the logs. */
1047 if (errno != ENOENT)
1048 WARNING ("processes plugin: Failed to open `%s': %s.", file,
1049 sstrerror (errno, errbuf, sizeof (errbuf)));
1061 status = read (fd, (void *)buf_ptr, len);
1066 if ((EAGAIN == errno) || (EINTR == errno))
1069 WARNING ("processes plugin: Failed to read from `%s': %s.", file,
1070 sstrerror (errno, errbuf, sizeof (errbuf)));
1090 /* cmdline not available; e.g. kernel thread, zombie */
1094 ssnprintf (buf, buf_len, "[%s]", name);
1098 assert (n <= buf_len);
1105 /* remove trailing whitespace */
1106 while ((n > 0) && (isspace (buf[n]) || ('\0' == buf[n]))) {
1111 /* arguments are separated by '\0' in /proc/<pid>/cmdline */
1118 } /* char *ps_get_cmdline (...) */
1120 static unsigned long read_fork_rate ()
1124 unsigned long result = 0;
1128 proc_stat = fopen("/proc/stat", "r");
1129 if (proc_stat == NULL) {
1131 ERROR ("processes plugin: fopen (/proc/stat) failed: %s",
1132 sstrerror (errno, errbuf, sizeof (errbuf)));
1136 while (fgets (buf, sizeof(buf), proc_stat) != NULL)
1140 numfields = strsplit(buf, fields, STATIC_ARRAY_SIZE (fields));
1144 if (strcmp ("processes", fields[0]) != 0)
1149 result = strtoul(fields[1], &endptr, /* base = */ 10);
1150 if ((endptr == fields[1]) || (errno != 0)) {
1151 ERROR ("processes plugin: Cannot parse fork rate: %s",
1165 static void ps_submit_fork_rate (unsigned long value)
1168 value_list_t vl = VALUE_LIST_INIT;
1170 values[0].derive = (derive_t) value;
1174 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
1175 sstrncpy (vl.plugin, "processes", sizeof (vl.plugin));
1176 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
1177 sstrncpy (vl.type, "fork_rate", sizeof (vl.type));
1178 sstrncpy (vl.type_instance, "", sizeof (vl.type_instance));
1180 plugin_dispatch_values (&vl);
1183 #endif /* KERNEL_LINUX */
1185 #if HAVE_THREAD_INFO
1186 static int mach_get_task_name (task_t t, int *pid, char *name, size_t name_max_len)
1190 struct kinfo_proc kp;
1195 mib[2] = KERN_PROC_PID;
1197 if (pid_for_task (t, pid) != KERN_SUCCESS)
1201 kp_size = sizeof (kp);
1202 if (sysctl (mib, 4, &kp, &kp_size, NULL, 0) != 0)
1205 if (name_max_len > (MAXCOMLEN + 1))
1206 name_max_len = MAXCOMLEN + 1;
1208 strncpy (name, kp.kp_proc.p_comm, name_max_len - 1);
1209 name[name_max_len - 1] = '\0';
1211 DEBUG ("pid = %i; name = %s;", *pid, name);
1213 /* We don't do the special handling for `p_comm == "LaunchCFMApp"' as
1214 * `top' does it, because it is a lot of work and only used when
1215 * debugging. -octo */
1219 #endif /* HAVE_THREAD_INFO */
1220 /* ------- end of additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
1222 /* do actual readings from kernel */
1223 static int ps_read (void)
1225 #if HAVE_THREAD_INFO
1226 kern_return_t status;
1229 processor_set_t port_pset_priv;
1232 task_array_t task_list;
1233 mach_msg_type_number_t task_list_len;
1236 char task_name[MAXCOMLEN + 1];
1239 thread_act_array_t thread_list;
1240 mach_msg_type_number_t thread_list_len;
1241 thread_basic_info_data_t thread_data;
1242 mach_msg_type_number_t thread_data_len;
1251 procstat_entry_t pse;
1256 * The Mach-concept is a little different from the traditional UNIX
1257 * concept: All the work is done in threads. Threads are contained in
1258 * `tasks'. Therefore, `task status' doesn't make much sense, since
1259 * it's actually a `thread status'.
1260 * Tasks are assigned to sets of processors, so that's where you go to
1263 for (pset = 0; pset < pset_list_len; pset++)
1265 if ((status = host_processor_set_priv (port_host_self,
1267 &port_pset_priv)) != KERN_SUCCESS)
1269 ERROR ("host_processor_set_priv failed: %s\n",
1270 mach_error_string (status));
1274 if ((status = processor_set_tasks (port_pset_priv,
1276 &task_list_len)) != KERN_SUCCESS)
1278 ERROR ("processor_set_tasks failed: %s\n",
1279 mach_error_string (status));
1280 mach_port_deallocate (port_task_self, port_pset_priv);
1284 for (task = 0; task < task_list_len; task++)
1287 if (mach_get_task_name (task_list[task],
1289 task_name, PROCSTAT_NAME_LEN) == 0)
1291 /* search for at least one match */
1292 for (ps = list_head_g; ps != NULL; ps = ps->next)
1293 /* FIXME: cmdline should be here instead of NULL */
1294 if (ps_list_match (task_name, NULL, ps) == 1)
1298 /* Collect more detailed statistics for this process */
1301 task_basic_info_data_t task_basic_info;
1302 mach_msg_type_number_t task_basic_info_len;
1303 task_events_info_data_t task_events_info;
1304 mach_msg_type_number_t task_events_info_len;
1305 task_absolutetime_info_data_t task_absolutetime_info;
1306 mach_msg_type_number_t task_absolutetime_info_len;
1308 memset (&pse, '\0', sizeof (pse));
1311 task_basic_info_len = TASK_BASIC_INFO_COUNT;
1312 status = task_info (task_list[task],
1314 (task_info_t) &task_basic_info,
1315 &task_basic_info_len);
1316 if (status != KERN_SUCCESS)
1318 ERROR ("task_info failed: %s",
1319 mach_error_string (status));
1320 continue; /* with next thread_list */
1323 task_events_info_len = TASK_EVENTS_INFO_COUNT;
1324 status = task_info (task_list[task],
1326 (task_info_t) &task_events_info,
1327 &task_events_info_len);
1328 if (status != KERN_SUCCESS)
1330 ERROR ("task_info failed: %s",
1331 mach_error_string (status));
1332 continue; /* with next thread_list */
1335 task_absolutetime_info_len = TASK_ABSOLUTETIME_INFO_COUNT;
1336 status = task_info (task_list[task],
1337 TASK_ABSOLUTETIME_INFO,
1338 (task_info_t) &task_absolutetime_info,
1339 &task_absolutetime_info_len);
1340 if (status != KERN_SUCCESS)
1342 ERROR ("task_info failed: %s",
1343 mach_error_string (status));
1344 continue; /* with next thread_list */
1348 pse.vmem_size = task_basic_info.virtual_size;
1349 pse.vmem_rss = task_basic_info.resident_size;
1350 /* Does not seem to be easily exposed */
1354 pse.vmem_minflt_counter = task_events_info.cow_faults;
1355 pse.vmem_majflt_counter = task_events_info.faults;
1357 pse.cpu_user_counter = task_absolutetime_info.total_user;
1358 pse.cpu_system_counter = task_absolutetime_info.total_system;
1361 status = task_threads (task_list[task], &thread_list,
1363 if (status != KERN_SUCCESS)
1365 /* Apple's `top' treats this case a zombie. It
1366 * makes sense to some extend: A `zombie'
1367 * thread is nonsense, since the task/process
1370 DEBUG ("task_threads failed: %s",
1371 mach_error_string (status));
1372 if (task_list[task] != port_task_self)
1373 mach_port_deallocate (port_task_self,
1375 continue; /* with next task_list */
1378 for (thread = 0; thread < thread_list_len; thread++)
1380 thread_data_len = THREAD_BASIC_INFO_COUNT;
1381 status = thread_info (thread_list[thread],
1383 (thread_info_t) &thread_data,
1385 if (status != KERN_SUCCESS)
1387 ERROR ("thread_info failed: %s",
1388 mach_error_string (status));
1389 if (task_list[task] != port_task_self)
1390 mach_port_deallocate (port_task_self,
1391 thread_list[thread]);
1392 continue; /* with next thread_list */
1398 switch (thread_data.run_state)
1400 case TH_STATE_RUNNING:
1403 case TH_STATE_STOPPED:
1404 /* What exactly is `halted'? */
1405 case TH_STATE_HALTED:
1408 case TH_STATE_WAITING:
1411 case TH_STATE_UNINTERRUPTIBLE:
1414 /* There is no `zombie' case here,
1415 * since there are no zombie-threads.
1416 * There's only zombie tasks, which are
1419 WARNING ("Unknown thread status: %i",
1420 thread_data.run_state);
1422 } /* switch (thread_data.run_state) */
1424 if (task_list[task] != port_task_self)
1426 status = mach_port_deallocate (port_task_self,
1427 thread_list[thread]);
1428 if (status != KERN_SUCCESS)
1429 ERROR ("mach_port_deallocate failed: %s",
1430 mach_error_string (status));
1432 } /* for (thread_list) */
1434 if ((status = vm_deallocate (port_task_self,
1435 (vm_address_t) thread_list,
1436 thread_list_len * sizeof (thread_act_t)))
1439 ERROR ("vm_deallocate failed: %s",
1440 mach_error_string (status));
1443 thread_list_len = 0;
1445 /* Only deallocate the task port, if it isn't our own.
1446 * Don't know what would happen in that case, but this
1447 * is what Apple's top does.. ;) */
1448 if (task_list[task] != port_task_self)
1450 status = mach_port_deallocate (port_task_self,
1452 if (status != KERN_SUCCESS)
1453 ERROR ("mach_port_deallocate failed: %s",
1454 mach_error_string (status));
1458 /* FIXME: cmdline should be here instead of NULL */
1459 ps_list_add (task_name, NULL, &pse);
1460 } /* for (task_list) */
1462 if ((status = vm_deallocate (port_task_self,
1463 (vm_address_t) task_list,
1464 task_list_len * sizeof (task_t))) != KERN_SUCCESS)
1466 ERROR ("vm_deallocate failed: %s",
1467 mach_error_string (status));
1472 if ((status = mach_port_deallocate (port_task_self, port_pset_priv))
1475 ERROR ("mach_port_deallocate failed: %s",
1476 mach_error_string (status));
1478 } /* for (pset_list) */
1480 ps_submit_state ("running", running);
1481 ps_submit_state ("sleeping", sleeping);
1482 ps_submit_state ("zombies", zombies);
1483 ps_submit_state ("stopped", stopped);
1484 ps_submit_state ("blocked", blocked);
1486 for (ps = list_head_g; ps != NULL; ps = ps->next)
1487 ps_submit_proc_list (ps);
1488 /* #endif HAVE_THREAD_INFO */
1502 char cmdline[ARG_MAX];
1506 procstat_entry_t pse;
1509 unsigned long fork_rate;
1513 running = sleeping = zombies = stopped = paging = blocked = 0;
1516 if ((proc = opendir ("/proc")) == NULL)
1519 ERROR ("Cannot open `/proc': %s",
1520 sstrerror (errno, errbuf, sizeof (errbuf)));
1524 while ((ent = readdir (proc)) != NULL)
1526 if (!isdigit (ent->d_name[0]))
1529 if ((pid = atoi (ent->d_name)) < 1)
1532 status = ps_read_process (pid, &ps, &state);
1535 DEBUG ("ps_read_process failed: %i", status);
1542 pse.num_proc = ps.num_proc;
1543 pse.num_lwp = ps.num_lwp;
1544 pse.vmem_size = ps.vmem_size;
1545 pse.vmem_rss = ps.vmem_rss;
1546 pse.vmem_data = ps.vmem_data;
1547 pse.vmem_code = ps.vmem_code;
1548 pse.stack_size = ps.stack_size;
1550 pse.vmem_minflt = 0;
1551 pse.vmem_minflt_counter = ps.vmem_minflt_counter;
1552 pse.vmem_majflt = 0;
1553 pse.vmem_majflt_counter = ps.vmem_majflt_counter;
1556 pse.cpu_user_counter = ps.cpu_user_counter;
1558 pse.cpu_system_counter = ps.cpu_system_counter;
1560 pse.io_rchar = ps.io_rchar;
1561 pse.io_wchar = ps.io_wchar;
1562 pse.io_syscr = ps.io_syscr;
1563 pse.io_syscw = ps.io_syscw;
1567 case 'R': running++; break;
1568 case 'S': sleeping++; break;
1569 case 'D': blocked++; break;
1570 case 'Z': zombies++; break;
1571 case 'T': stopped++; break;
1572 case 'W': paging++; break;
1575 ps_list_add (ps.name,
1576 ps_get_cmdline (pid, ps.name, cmdline, sizeof (cmdline)),
1582 ps_submit_state ("running", running);
1583 ps_submit_state ("sleeping", sleeping);
1584 ps_submit_state ("zombies", zombies);
1585 ps_submit_state ("stopped", stopped);
1586 ps_submit_state ("paging", paging);
1587 ps_submit_state ("blocked", blocked);
1589 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1590 ps_submit_proc_list (ps_ptr);
1592 fork_rate = read_fork_rate();
1593 if (fork_rate != ULONG_MAX)
1594 ps_submit_fork_rate(fork_rate);
1595 /* #endif KERNEL_LINUX */
1597 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD
1608 char cmdline[ARG_MAX];
1610 struct kinfo_proc *procs; /* array of processes */
1612 int count; /* returns number of processes */
1616 procstat_entry_t pse;
1620 /* Open the kvm interface, get a descriptor */
1621 kd = kvm_open (NULL, NULL, NULL, 0, errbuf);
1624 ERROR ("processes plugin: Cannot open kvm interface: %s",
1629 /* Get the list of processes. */
1630 procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, &count);
1633 ERROR ("processes plugin: Cannot get kvm processes list: %s",
1639 /* Iterate through the processes in kinfo_proc */
1640 for (i = 0; i < count; i++)
1642 /* retrieve the arguments */
1646 argv = kvm_getargv (kd, (const struct kinfo_proc *) &(procs[i]), 0);
1653 while (argv[argc] != NULL)
1656 status = strjoin (cmdline, sizeof (cmdline),
1661 WARNING ("processes plugin: Command line did "
1662 "not fit into buffer.");
1666 cmdline_ptr = &cmdline[0];
1670 pse.id = procs[i].ki_pid;
1674 pse.num_lwp = procs[i].ki_numthreads;
1676 pse.vmem_size = procs[i].ki_size;
1677 pse.vmem_rss = procs[i].ki_rssize * getpagesize();
1678 pse.vmem_data = procs[i].ki_dsize * getpagesize();
1679 pse.vmem_code = procs[i].ki_tsize * getpagesize();
1680 pse.stack_size = procs[i].ki_ssize * getpagesize();
1681 pse.vmem_minflt = 0;
1682 pse.vmem_minflt_counter = procs[i].ki_rusage.ru_minflt;
1683 pse.vmem_majflt = 0;
1684 pse.vmem_majflt_counter = procs[i].ki_rusage.ru_majflt;
1687 pse.cpu_user_counter = procs[i].ki_rusage.ru_utime.tv_sec
1689 + procs[i].ki_rusage.ru_utime.tv_usec;
1691 pse.cpu_system_counter = procs[i].ki_rusage.ru_stime.tv_sec
1693 + procs[i].ki_rusage.ru_stime.tv_usec;
1701 switch (procs[i].ki_stat)
1703 case SSTOP: stopped++; break;
1704 case SSLEEP: sleeping++; break;
1705 case SRUN: running++; break;
1706 case SIDL: idle++; break;
1707 case SWAIT: wait++; break;
1708 case SLOCK: blocked++; break;
1709 case SZOMB: zombies++; break;
1712 ps_list_add (procs[i].ki_comm, cmdline_ptr, &pse);
1717 ps_submit_state ("running", running);
1718 ps_submit_state ("sleeping", sleeping);
1719 ps_submit_state ("zombies", zombies);
1720 ps_submit_state ("stopped", stopped);
1721 ps_submit_state ("blocked", blocked);
1722 ps_submit_state ("idle", idle);
1723 ps_submit_state ("wait", wait);
1725 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1726 ps_submit_proc_list (ps_ptr);
1727 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
1729 #elif HAVE_PROCINFO_H
1742 procstat_entry_t pse;
1745 while ((nprocs = getprocs64 (procentry, sizeof(struct procentry64),
1746 /* fdsinfo = */ NULL, sizeof(struct fdsinfo64),
1747 &pindex, MAXPROCENTRY)) > 0)
1751 for (i = 0; i < nprocs; i++)
1755 char arglist[MAXARGLN+1];
1759 if (procentry[i].pi_state == SNONE) continue;
1760 /* if (procentry[i].pi_state == SZOMB) FIXME */
1762 cmdline = procentry[i].pi_comm;
1763 cargs = procentry[i].pi_comm;
1764 if ( procentry[i].pi_flags & SKPROC )
1766 if (procentry[i].pi_pid == 0)
1767 cmdline = "swapper";
1772 if (getargs(&procentry[i], sizeof(struct procentry64), arglist, MAXARGLN) >= 0)
1777 while (++n < MAXARGLN)
1779 if (arglist[n] == '\0')
1781 if (arglist[n+1] == '\0')
1790 pse.id = procentry[i].pi_pid;
1792 pse.num_lwp = procentry[i].pi_thcount;
1796 while ((nthreads = getthrds64(procentry[i].pi_pid,
1797 thrdentry, sizeof(struct thrdentry64),
1798 &thindex, MAXTHRDENTRY)) > 0)
1802 for (j=0; j< nthreads; j++)
1804 switch (thrdentry[j].ti_state)
1806 /* case TSNONE: break; */
1807 case TSIDL: blocked++; break; /* FIXME is really blocked */
1808 case TSRUN: running++; break;
1809 case TSSLEEP: sleeping++; break;
1810 case TSSWAP: paging++; break;
1811 case TSSTOP: stopped++; break;
1812 case TSZOMB: zombies++; break;
1815 if (nthreads < MAXTHRDENTRY)
1820 /* tv_usec is nanosec ??? */
1821 pse.cpu_user_counter = procentry[i].pi_ru.ru_utime.tv_sec * 1000000 +
1822 procentry[i].pi_ru.ru_utime.tv_usec / 1000;
1825 /* tv_usec is nanosec ??? */
1826 pse.cpu_system_counter = procentry[i].pi_ru.ru_stime.tv_sec * 1000000 +
1827 procentry[i].pi_ru.ru_stime.tv_usec / 1000;
1829 pse.vmem_minflt = 0;
1830 pse.vmem_minflt_counter = procentry[i].pi_minflt;
1831 pse.vmem_majflt = 0;
1832 pse.vmem_majflt_counter = procentry[i].pi_majflt;
1834 pse.vmem_size = procentry[i].pi_tsize + procentry[i].pi_dvm * pagesize;
1835 pse.vmem_rss = (procentry[i].pi_drss + procentry[i].pi_trss) * pagesize;
1846 ps_list_add (cmdline, cargs, &pse);
1847 } /* for (i = 0 .. nprocs) */
1849 if (nprocs < MAXPROCENTRY)
1851 } /* while (getprocs64() > 0) */
1852 ps_submit_state ("running", running);
1853 ps_submit_state ("sleeping", sleeping);
1854 ps_submit_state ("zombies", zombies);
1855 ps_submit_state ("stopped", stopped);
1856 ps_submit_state ("paging", paging);
1857 ps_submit_state ("blocked", blocked);
1859 for (ps = list_head_g; ps != NULL; ps = ps->next)
1860 ps_submit_proc_list (ps);
1861 #endif /* HAVE_PROCINFO_H */
1866 void module_register (void)
1868 plugin_register_complex_config ("processes", ps_config);
1869 plugin_register_init ("processes", ps_init);
1870 plugin_register_read ("processes", ps_read);
1871 } /* void module_register */