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
126 static const char *config_keys[] =
131 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
133 typedef struct procstat_entry_s
138 unsigned long num_proc;
139 unsigned long num_lwp;
140 unsigned long vmem_size;
141 unsigned long vmem_rss;
142 unsigned long vmem_data;
143 unsigned long vmem_code;
144 unsigned long stack_size;
146 unsigned long vmem_minflt;
147 unsigned long vmem_majflt;
148 unsigned long vmem_minflt_counter;
149 unsigned long vmem_majflt_counter;
151 unsigned long cpu_user;
152 unsigned long cpu_system;
153 unsigned long cpu_user_counter;
154 unsigned long cpu_system_counter;
162 struct procstat_entry_s *next;
165 #define PROCSTAT_NAME_LEN 256
166 typedef struct procstat
168 char name[PROCSTAT_NAME_LEN];
173 unsigned long num_proc;
174 unsigned long num_lwp;
175 unsigned long vmem_size;
176 unsigned long vmem_rss;
177 unsigned long vmem_data;
178 unsigned long vmem_code;
179 unsigned long stack_size;
181 unsigned long vmem_minflt_counter;
182 unsigned long vmem_majflt_counter;
184 unsigned long cpu_user_counter;
185 unsigned long cpu_system_counter;
193 struct procstat *next;
194 struct procstat_entry_s *instances;
197 static procstat_t *list_head_g = NULL;
200 static mach_port_t port_host_self;
201 static mach_port_t port_task_self;
203 static processor_set_name_array_t pset_list;
204 static mach_msg_type_number_t pset_list_len;
205 /* #endif HAVE_THREAD_INFO */
208 static long pagesize_g;
209 /* #endif KERNEL_LINUX */
211 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD
212 /* no global variables */
213 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
215 #elif HAVE_PROCINFO_H
216 static struct procentry64 procentry[MAXPROCENTRY];
217 static struct thrdentry64 thrdentry[MAXTHRDENTRY];
220 #ifndef _AIXVERSION_610
221 int getprocs64 (void *procsinfo, int sizproc, void *fdsinfo, int sizfd, pid_t *index, int count);
222 int getthrds64( pid_t, void *, int, tid64_t *, int );
224 int getargs (struct procentry64 *processBuffer, int bufferLen, char *argsBuffer, int argsLen);
225 #endif /* HAVE_PROCINFO_H */
227 /* put name of process from config to list_head_g tree
228 list_head_g is a list of 'procstat_t' structs with
229 processes names we want to watch */
230 static void ps_list_register (const char *name, const char *regexp)
236 new = (procstat_t *) malloc (sizeof (procstat_t));
239 ERROR ("processes plugin: ps_list_register: malloc failed.");
242 memset (new, 0, sizeof (procstat_t));
243 sstrncpy (new->name, name, sizeof (new->name));
248 DEBUG ("ProcessMatch: adding \"%s\" as criteria to process %s.", regexp, name);
249 new->re = (regex_t *) malloc (sizeof (regex_t));
252 ERROR ("processes plugin: ps_list_register: malloc failed.");
257 status = regcomp (new->re, regexp, REG_EXTENDED | REG_NOSUB);
260 DEBUG ("ProcessMatch: compiling the regular expression \"%s\" failed.", regexp);
268 ERROR ("processes plugin: ps_list_register: "
269 "Regular expression \"%s\" found in config "
270 "file, but support for regular expressions "
271 "has been disabled at compile time.",
278 for (ptr = list_head_g; ptr != NULL; ptr = ptr->next)
280 if (strcmp (ptr->name, name) == 0)
282 WARNING ("processes plugin: You have configured more "
283 "than one `Process' or "
284 "`ProcessMatch' with the same name. "
285 "All but the first setting will be "
292 if (ptr->next == NULL)
300 } /* void ps_list_register */
302 /* try to match name against entry, returns 1 if success */
303 static int ps_list_match (const char *name, const char *cmdline, procstat_t *ps)
312 if ((str == NULL) || (str[0] == 0))
315 assert (str != NULL);
317 status = regexec (ps->re, str,
326 if (strcmp (ps->name, name) == 0)
330 } /* int ps_list_match */
332 /* add process entry to 'instances' of process 'name' (or refresh it) */
333 static void ps_list_add (const char *name, const char *cmdline, procstat_entry_t *entry)
336 procstat_entry_t *pse;
341 for (ps = list_head_g; ps != NULL; ps = ps->next)
343 if ((ps_list_match (name, cmdline, ps)) == 0)
346 for (pse = ps->instances; pse != NULL; pse = pse->next)
347 if ((pse->id == entry->id) || (pse->next == NULL))
350 if ((pse == NULL) || (pse->id != entry->id))
352 procstat_entry_t *new;
354 new = (procstat_entry_t *) malloc (sizeof (procstat_entry_t));
357 memset (new, 0, sizeof (procstat_entry_t));
369 pse->num_proc = entry->num_proc;
370 pse->num_lwp = entry->num_lwp;
371 pse->vmem_size = entry->vmem_size;
372 pse->vmem_rss = entry->vmem_rss;
373 pse->vmem_data = entry->vmem_data;
374 pse->vmem_code = entry->vmem_code;
375 pse->stack_size = entry->stack_size;
376 pse->io_rchar = entry->io_rchar;
377 pse->io_wchar = entry->io_wchar;
378 pse->io_syscr = entry->io_syscr;
379 pse->io_syscw = entry->io_syscw;
381 ps->num_proc += pse->num_proc;
382 ps->num_lwp += pse->num_lwp;
383 ps->vmem_size += pse->vmem_size;
384 ps->vmem_rss += pse->vmem_rss;
385 ps->vmem_data += pse->vmem_data;
386 ps->vmem_code += pse->vmem_code;
387 ps->stack_size += pse->stack_size;
389 ps->io_rchar += ((pse->io_rchar == -1)?0:pse->io_rchar);
390 ps->io_wchar += ((pse->io_wchar == -1)?0:pse->io_wchar);
391 ps->io_syscr += ((pse->io_syscr == -1)?0:pse->io_syscr);
392 ps->io_syscw += ((pse->io_syscw == -1)?0:pse->io_syscw);
394 if ((entry->vmem_minflt_counter == 0)
395 && (entry->vmem_majflt_counter == 0))
397 pse->vmem_minflt_counter += entry->vmem_minflt;
398 pse->vmem_minflt = entry->vmem_minflt;
400 pse->vmem_majflt_counter += entry->vmem_majflt;
401 pse->vmem_majflt = entry->vmem_majflt;
405 if (entry->vmem_minflt_counter < pse->vmem_minflt_counter)
407 pse->vmem_minflt = entry->vmem_minflt_counter
408 + (ULONG_MAX - pse->vmem_minflt_counter);
412 pse->vmem_minflt = entry->vmem_minflt_counter - pse->vmem_minflt_counter;
414 pse->vmem_minflt_counter = entry->vmem_minflt_counter;
416 if (entry->vmem_majflt_counter < pse->vmem_majflt_counter)
418 pse->vmem_majflt = entry->vmem_majflt_counter
419 + (ULONG_MAX - pse->vmem_majflt_counter);
423 pse->vmem_majflt = entry->vmem_majflt_counter - pse->vmem_majflt_counter;
425 pse->vmem_majflt_counter = entry->vmem_majflt_counter;
428 ps->vmem_minflt_counter += pse->vmem_minflt;
429 ps->vmem_majflt_counter += pse->vmem_majflt;
431 if ((entry->cpu_user_counter == 0)
432 && (entry->cpu_system_counter == 0))
434 pse->cpu_user_counter += entry->cpu_user;
435 pse->cpu_user = entry->cpu_user;
437 pse->cpu_system_counter += entry->cpu_system;
438 pse->cpu_system = entry->cpu_system;
442 if (entry->cpu_user_counter < pse->cpu_user_counter)
444 pse->cpu_user = entry->cpu_user_counter
445 + (ULONG_MAX - pse->cpu_user_counter);
449 pse->cpu_user = entry->cpu_user_counter - pse->cpu_user_counter;
451 pse->cpu_user_counter = entry->cpu_user_counter;
453 if (entry->cpu_system_counter < pse->cpu_system_counter)
455 pse->cpu_system = entry->cpu_system_counter
456 + (ULONG_MAX - pse->cpu_system_counter);
460 pse->cpu_system = entry->cpu_system_counter - pse->cpu_system_counter;
462 pse->cpu_system_counter = entry->cpu_system_counter;
465 ps->cpu_user_counter += pse->cpu_user;
466 ps->cpu_system_counter += pse->cpu_system;
470 /* remove old entries from instances of processes in list_head_g */
471 static void ps_list_reset (void)
474 procstat_entry_t *pse;
475 procstat_entry_t *pse_prev;
477 for (ps = list_head_g; ps != NULL; ps = ps->next)
497 DEBUG ("Removing this procstat entry cause it's too old: "
498 "id = %lu; name = %s;",
501 if (pse_prev == NULL)
503 ps->instances = pse->next;
509 pse_prev->next = pse->next;
511 pse = pse_prev->next;
520 } /* while (pse != NULL) */
521 } /* for (ps = list_head_g; ps != NULL; ps = ps->next) */
524 /* put all pre-defined 'Process' names from config to list_head_g tree */
525 static int ps_config (const char *key, const char *value)
527 if (strcasecmp (key, "Process") == 0)
529 ps_list_register (value, NULL);
531 else if (strcasecmp (key, "ProcessMatch") == 0)
537 new_val = strdup (value);
538 if (new_val == NULL) {
539 ERROR ("processes plugin: strdup failed when processing "
540 "`ProcessMatch %s'.", value);
544 fields_num = strsplit (new_val, fields,
545 STATIC_ARRAY_SIZE (fields));
548 ERROR ("processes plugin: `ProcessMatch' needs exactly "
549 "two string arguments.");
553 ps_list_register (fields[0], fields[1]);
558 ERROR ("processes plugin: The `%s' configuration option is not "
559 "understood and will be ignored.", key);
566 static int ps_init (void)
569 kern_return_t status;
571 port_host_self = mach_host_self ();
572 port_task_self = mach_task_self ();
574 if (pset_list != NULL)
576 vm_deallocate (port_task_self,
577 (vm_address_t) pset_list,
578 pset_list_len * sizeof (processor_set_t));
583 if ((status = host_processor_sets (port_host_self,
585 &pset_list_len)) != KERN_SUCCESS)
587 ERROR ("host_processor_sets failed: %s\n",
588 mach_error_string (status));
593 /* #endif HAVE_THREAD_INFO */
596 pagesize_g = sysconf(_SC_PAGESIZE);
597 DEBUG ("pagesize_g = %li; CONFIG_HZ = %i;",
598 pagesize_g, CONFIG_HZ);
599 /* #endif KERNEL_LINUX */
601 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD
602 /* no initialization */
603 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
605 #elif HAVE_PROCINFO_H
606 pagesize = getpagesize();
607 #endif /* HAVE_PROCINFO_H */
612 /* submit global state (e.g.: qty of zombies, running, etc..) */
613 static void ps_submit_state (const char *state, double value)
616 value_list_t vl = VALUE_LIST_INIT;
618 values[0].gauge = value;
622 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
623 sstrncpy (vl.plugin, "processes", sizeof (vl.plugin));
624 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
625 sstrncpy (vl.type, "ps_state", sizeof (vl.type));
626 sstrncpy (vl.type_instance, state, sizeof (vl.type_instance));
628 plugin_dispatch_values (&vl);
631 /* submit info about specific process (e.g.: memory taken, cpu usage, etc..) */
632 static void ps_submit_proc_list (procstat_t *ps)
635 value_list_t vl = VALUE_LIST_INIT;
639 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
640 sstrncpy (vl.plugin, "processes", sizeof (vl.plugin));
641 sstrncpy (vl.plugin_instance, ps->name, sizeof (vl.plugin_instance));
643 sstrncpy (vl.type, "ps_vm", sizeof (vl.type));
644 vl.values[0].gauge = ps->vmem_size;
646 plugin_dispatch_values (&vl);
648 sstrncpy (vl.type, "ps_rss", sizeof (vl.type));
649 vl.values[0].gauge = ps->vmem_rss;
651 plugin_dispatch_values (&vl);
653 sstrncpy (vl.type, "ps_data", sizeof (vl.type));
654 vl.values[0].gauge = ps->vmem_data;
656 plugin_dispatch_values (&vl);
658 sstrncpy (vl.type, "ps_code", sizeof (vl.type));
659 vl.values[0].gauge = ps->vmem_code;
661 plugin_dispatch_values (&vl);
663 sstrncpy (vl.type, "ps_stacksize", sizeof (vl.type));
664 vl.values[0].gauge = ps->stack_size;
666 plugin_dispatch_values (&vl);
668 sstrncpy (vl.type, "ps_cputime", sizeof (vl.type));
669 vl.values[0].counter = ps->cpu_user_counter;
670 vl.values[1].counter = ps->cpu_system_counter;
672 plugin_dispatch_values (&vl);
674 sstrncpy (vl.type, "ps_count", sizeof (vl.type));
675 vl.values[0].gauge = ps->num_proc;
676 vl.values[1].gauge = ps->num_lwp;
678 plugin_dispatch_values (&vl);
680 sstrncpy (vl.type, "ps_pagefaults", sizeof (vl.type));
681 vl.values[0].counter = ps->vmem_minflt_counter;
682 vl.values[1].counter = ps->vmem_majflt_counter;
684 plugin_dispatch_values (&vl);
686 if ( (ps->io_rchar != -1) && (ps->io_wchar != -1) )
688 sstrncpy (vl.type, "ps_disk_octets", sizeof (vl.type));
689 vl.values[0].derive = ps->io_rchar;
690 vl.values[1].derive = ps->io_wchar;
692 plugin_dispatch_values (&vl);
695 if ( (ps->io_syscr != -1) && (ps->io_syscw != -1) )
697 sstrncpy (vl.type, "ps_disk_ops", sizeof (vl.type));
698 vl.values[0].derive = ps->io_syscr;
699 vl.values[1].derive = ps->io_syscw;
701 plugin_dispatch_values (&vl);
704 DEBUG ("name = %s; num_proc = %lu; num_lwp = %lu; "
705 "vmem_size = %lu; vmem_rss = %lu; vmem_data = %lu; "
707 "vmem_minflt_counter = %lu; vmem_majflt_counter = %lu; "
708 "cpu_user_counter = %lu; cpu_system_counter = %lu; "
709 "io_rchar = %"PRIi64"; io_wchar = %"PRIi64"; "
710 "io_syscr = %"PRIi64"; io_syscw = %"PRIi64";",
711 ps->name, ps->num_proc, ps->num_lwp,
712 ps->vmem_size, ps->vmem_rss,
713 ps->vmem_data, ps->vmem_code,
714 ps->vmem_minflt_counter, ps->vmem_majflt_counter,
715 ps->cpu_user_counter, ps->cpu_system_counter,
716 ps->io_rchar, ps->io_wchar, ps->io_syscr, ps->io_syscw);
717 } /* void ps_submit_proc_list */
719 /* ------- additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
721 static int ps_read_tasks (int pid)
728 ssnprintf (dirname, sizeof (dirname), "/proc/%i/task", pid);
730 if ((dh = opendir (dirname)) == NULL)
732 DEBUG ("Failed to open directory `%s'", dirname);
736 while ((ent = readdir (dh)) != NULL)
738 if (!isdigit ((int) ent->d_name[0]))
745 return ((count >= 1) ? count : 1);
746 } /* int *ps_read_tasks */
748 /* Read advanced virtual memory data from /proc/pid/status */
749 static procstat_t *ps_read_vmem (int pid, procstat_t *ps)
754 unsigned long long lib = 0;
755 unsigned long long exe = 0;
756 unsigned long long data = 0;
760 ssnprintf (filename, sizeof (filename), "/proc/%i/status", pid);
761 if ((fh = fopen (filename, "r")) == NULL)
764 while (fgets (buffer, sizeof(buffer), fh) != NULL)
769 if (strncmp (buffer, "Vm", 2) != 0)
772 numfields = strsplit (buffer, fields,
773 STATIC_ARRAY_SIZE (fields));
780 tmp = strtoll (fields[1], &endptr, /* base = */ 10);
781 if ((errno == 0) && (endptr != fields[1]))
783 if (strncmp (buffer, "VmData", 6) == 0)
787 else if (strncmp (buffer, "VmLib", 5) == 0)
791 else if (strncmp(buffer, "VmExe", 5) == 0)
796 } /* while (fgets) */
801 WARNING ("processes: fclose: %s",
802 sstrerror (errno, errbuf, sizeof (errbuf)));
805 ps->vmem_data = data * 1024;
806 ps->vmem_code = (exe + lib) * 1024;
809 } /* procstat_t *ps_read_vmem */
811 static procstat_t *ps_read_io (int pid, procstat_t *ps)
820 ssnprintf (filename, sizeof (filename), "/proc/%i/io", pid);
821 if ((fh = fopen (filename, "r")) == NULL)
824 while (fgets (buffer, 1024, fh) != NULL)
826 derive_t *val = NULL;
830 if (strncasecmp (buffer, "rchar:", 6) == 0)
831 val = &(ps->io_rchar);
832 else if (strncasecmp (buffer, "wchar:", 6) == 0)
833 val = &(ps->io_wchar);
834 else if (strncasecmp (buffer, "syscr:", 6) == 0)
835 val = &(ps->io_syscr);
836 else if (strncasecmp (buffer, "syscw:", 6) == 0)
837 val = &(ps->io_syscw);
841 numfields = strsplit (buffer, fields, 8);
848 tmp = strtoll (fields[1], &endptr, /* base = */ 10);
849 if ((errno != 0) || (endptr == fields[1]))
852 *val = (derive_t) tmp;
853 } /* while (fgets) */
858 WARNING ("processes: fclose: %s",
859 sstrerror (errno, errbuf, sizeof (errbuf)));
863 } /* procstat_t *ps_read_io */
865 int ps_read_process (int pid, procstat_t *ps, char *state)
878 long long unsigned cpu_user_counter;
879 long long unsigned cpu_system_counter;
880 long long unsigned vmem_size;
881 long long unsigned vmem_rss;
882 long long unsigned stack_size;
884 memset (ps, 0, sizeof (procstat_t));
886 ssnprintf (filename, sizeof (filename), "/proc/%i/stat", pid);
888 i = read_file_contents (filename, buffer, sizeof(buffer) - 1);
893 fields_len = strsplit (buffer, fields, 64);
896 DEBUG ("processes plugin: ps_read_process (pid = %i):"
897 " `%s' has only %i fields..",
898 (int) pid, filename, fields_len);
902 /* copy the name, strip brackets in the process */
903 name_len = strlen (fields[1]) - 2;
904 if ((fields[1][0] != '(') || (fields[1][name_len + 1] != ')'))
906 DEBUG ("No brackets found in process name: `%s'", fields[1]);
909 fields[1] = fields[1] + 1;
910 fields[1][name_len] = '\0';
911 strncpy (ps->name, fields[1], PROCSTAT_NAME_LEN);
913 ppid = atoi (fields[3]);
915 *state = fields[2][0];
924 if ( (ps->num_lwp = ps_read_tasks (pid)) == -1 )
926 /* returns -1 => kernel 2.4 */
932 /* Leave the rest at zero if this is only a zombi */
933 if (ps->num_proc == 0)
935 DEBUG ("processes plugin: This is only a zombi: pid = %i; "
936 "name = %s;", pid, ps->name);
940 cpu_user_counter = atoll (fields[13]);
941 cpu_system_counter = atoll (fields[14]);
942 vmem_size = atoll (fields[22]);
943 vmem_rss = atoll (fields[23]);
944 ps->vmem_minflt_counter = atol (fields[9]);
945 ps->vmem_majflt_counter = atol (fields[11]);
948 unsigned long long stack_start = atoll (fields[27]);
949 unsigned long long stack_ptr = atoll (fields[28]);
951 stack_size = (stack_start > stack_ptr)
952 ? stack_start - stack_ptr
953 : stack_ptr - stack_start;
956 /* Convert jiffies to useconds */
957 cpu_user_counter = cpu_user_counter * 1000000 / CONFIG_HZ;
958 cpu_system_counter = cpu_system_counter * 1000000 / CONFIG_HZ;
959 vmem_rss = vmem_rss * pagesize_g;
961 if ( (ps_read_vmem(pid, ps)) == NULL)
966 DEBUG("ps_read_process: did not get vmem data for pid %i",pid);
969 ps->cpu_user_counter = (unsigned long) cpu_user_counter;
970 ps->cpu_system_counter = (unsigned long) cpu_system_counter;
971 ps->vmem_size = (unsigned long) vmem_size;
972 ps->vmem_rss = (unsigned long) vmem_rss;
973 ps->stack_size = (unsigned long) stack_size;
975 if ( (ps_read_io (pid, ps)) == NULL)
983 DEBUG("ps_read_process: not get io data for pid %i",pid);
988 } /* int ps_read_process (...) */
990 static char *ps_get_cmdline (pid_t pid, char *name, char *buf, size_t buf_len)
1000 if ((pid < 1) || (NULL == buf) || (buf_len < 2))
1003 ssnprintf (file, sizeof (file), "/proc/%u/cmdline", pid);
1005 fd = open (file, O_RDONLY);
1008 WARNING ("processes plugin: Failed to open `%s': %s.", file,
1009 sstrerror (errno, errbuf, sizeof (errbuf)));
1021 status = read (fd, (void *)buf_ptr, len);
1026 if ((EAGAIN == errno) || (EINTR == errno))
1029 WARNING ("processes plugin: Failed to read from `%s': %s.", file,
1030 sstrerror (errno, errbuf, sizeof (errbuf)));
1050 /* cmdline not available; e.g. kernel thread, zombie */
1054 ssnprintf (buf, buf_len, "[%s]", name);
1058 assert (n <= buf_len);
1065 /* remove trailing whitespace */
1066 while ((n > 0) && (isspace (buf[n]) || ('\0' == buf[n]))) {
1071 /* arguments are separated by '\0' in /proc/<pid>/cmdline */
1078 } /* char *ps_get_cmdline (...) */
1080 static unsigned long read_fork_rate ()
1084 unsigned long result = 0;
1088 proc_stat = fopen("/proc/stat", "r");
1089 if (proc_stat == NULL) {
1091 ERROR ("processes plugin: fopen (/proc/stat) failed: %s",
1092 sstrerror (errno, errbuf, sizeof (errbuf)));
1096 while (fgets (buf, sizeof(buf), proc_stat) != NULL)
1100 numfields = strsplit(buf, fields, STATIC_ARRAY_SIZE (fields));
1104 if (strcmp ("processes", fields[0]) != 0)
1109 result = strtoul(fields[1], &endptr, 10);
1110 if ((endptr == fields[1]) || (errno != 0)) {
1111 ERROR ("processes plugin: Cannot parse fork rate: %s",
1125 static void ps_submit_fork_rate (unsigned long value)
1128 value_list_t vl = VALUE_LIST_INIT;
1130 values[0].derive = (derive_t) value;
1134 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
1135 sstrncpy (vl.plugin, "processes", sizeof (vl.plugin));
1136 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
1137 sstrncpy (vl.type, "fork_rate", sizeof (vl.type));
1138 sstrncpy (vl.type_instance, "", sizeof (vl.type_instance));
1140 plugin_dispatch_values (&vl);
1143 #endif /* KERNEL_LINUX */
1145 #if HAVE_THREAD_INFO
1146 static int mach_get_task_name (task_t t, int *pid, char *name, size_t name_max_len)
1150 struct kinfo_proc kp;
1155 mib[2] = KERN_PROC_PID;
1157 if (pid_for_task (t, pid) != KERN_SUCCESS)
1161 kp_size = sizeof (kp);
1162 if (sysctl (mib, 4, &kp, &kp_size, NULL, 0) != 0)
1165 if (name_max_len > (MAXCOMLEN + 1))
1166 name_max_len = MAXCOMLEN + 1;
1168 strncpy (name, kp.kp_proc.p_comm, name_max_len - 1);
1169 name[name_max_len - 1] = '\0';
1171 DEBUG ("pid = %i; name = %s;", *pid, name);
1173 /* We don't do the special handling for `p_comm == "LaunchCFMApp"' as
1174 * `top' does it, because it is a lot of work and only used when
1175 * debugging. -octo */
1179 #endif /* HAVE_THREAD_INFO */
1180 /* ------- end of additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
1182 /* do actual readings from kernel */
1183 static int ps_read (void)
1185 #if HAVE_THREAD_INFO
1186 kern_return_t status;
1189 processor_set_t port_pset_priv;
1192 task_array_t task_list;
1193 mach_msg_type_number_t task_list_len;
1196 char task_name[MAXCOMLEN + 1];
1199 thread_act_array_t thread_list;
1200 mach_msg_type_number_t thread_list_len;
1201 thread_basic_info_data_t thread_data;
1202 mach_msg_type_number_t thread_data_len;
1211 procstat_entry_t pse;
1216 * The Mach-concept is a little different from the traditional UNIX
1217 * concept: All the work is done in threads. Threads are contained in
1218 * `tasks'. Therefore, `task status' doesn't make much sense, since
1219 * it's actually a `thread status'.
1220 * Tasks are assigned to sets of processors, so that's where you go to
1223 for (pset = 0; pset < pset_list_len; pset++)
1225 if ((status = host_processor_set_priv (port_host_self,
1227 &port_pset_priv)) != KERN_SUCCESS)
1229 ERROR ("host_processor_set_priv failed: %s\n",
1230 mach_error_string (status));
1234 if ((status = processor_set_tasks (port_pset_priv,
1236 &task_list_len)) != KERN_SUCCESS)
1238 ERROR ("processor_set_tasks failed: %s\n",
1239 mach_error_string (status));
1240 mach_port_deallocate (port_task_self, port_pset_priv);
1244 for (task = 0; task < task_list_len; task++)
1247 if (mach_get_task_name (task_list[task],
1249 task_name, PROCSTAT_NAME_LEN) == 0)
1251 /* search for at least one match */
1252 for (ps = list_head_g; ps != NULL; ps = ps->next)
1253 /* FIXME: cmdline should be here instead of NULL */
1254 if (ps_list_match (task_name, NULL, ps) == 1)
1258 /* Collect more detailed statistics for this process */
1261 task_basic_info_data_t task_basic_info;
1262 mach_msg_type_number_t task_basic_info_len;
1263 task_events_info_data_t task_events_info;
1264 mach_msg_type_number_t task_events_info_len;
1265 task_absolutetime_info_data_t task_absolutetime_info;
1266 mach_msg_type_number_t task_absolutetime_info_len;
1268 memset (&pse, '\0', sizeof (pse));
1271 task_basic_info_len = TASK_BASIC_INFO_COUNT;
1272 status = task_info (task_list[task],
1274 (task_info_t) &task_basic_info,
1275 &task_basic_info_len);
1276 if (status != KERN_SUCCESS)
1278 ERROR ("task_info failed: %s",
1279 mach_error_string (status));
1280 continue; /* with next thread_list */
1283 task_events_info_len = TASK_EVENTS_INFO_COUNT;
1284 status = task_info (task_list[task],
1286 (task_info_t) &task_events_info,
1287 &task_events_info_len);
1288 if (status != KERN_SUCCESS)
1290 ERROR ("task_info failed: %s",
1291 mach_error_string (status));
1292 continue; /* with next thread_list */
1295 task_absolutetime_info_len = TASK_ABSOLUTETIME_INFO_COUNT;
1296 status = task_info (task_list[task],
1297 TASK_ABSOLUTETIME_INFO,
1298 (task_info_t) &task_absolutetime_info,
1299 &task_absolutetime_info_len);
1300 if (status != KERN_SUCCESS)
1302 ERROR ("task_info failed: %s",
1303 mach_error_string (status));
1304 continue; /* with next thread_list */
1308 pse.vmem_size = task_basic_info.virtual_size;
1309 pse.vmem_rss = task_basic_info.resident_size;
1310 /* Does not seem to be easily exposed */
1314 pse.vmem_minflt_counter = task_events_info.cow_faults;
1315 pse.vmem_majflt_counter = task_events_info.faults;
1317 pse.cpu_user_counter = task_absolutetime_info.total_user;
1318 pse.cpu_system_counter = task_absolutetime_info.total_system;
1321 status = task_threads (task_list[task], &thread_list,
1323 if (status != KERN_SUCCESS)
1325 /* Apple's `top' treats this case a zombie. It
1326 * makes sense to some extend: A `zombie'
1327 * thread is nonsense, since the task/process
1330 DEBUG ("task_threads failed: %s",
1331 mach_error_string (status));
1332 if (task_list[task] != port_task_self)
1333 mach_port_deallocate (port_task_self,
1335 continue; /* with next task_list */
1338 for (thread = 0; thread < thread_list_len; thread++)
1340 thread_data_len = THREAD_BASIC_INFO_COUNT;
1341 status = thread_info (thread_list[thread],
1343 (thread_info_t) &thread_data,
1345 if (status != KERN_SUCCESS)
1347 ERROR ("thread_info failed: %s",
1348 mach_error_string (status));
1349 if (task_list[task] != port_task_self)
1350 mach_port_deallocate (port_task_self,
1351 thread_list[thread]);
1352 continue; /* with next thread_list */
1358 switch (thread_data.run_state)
1360 case TH_STATE_RUNNING:
1363 case TH_STATE_STOPPED:
1364 /* What exactly is `halted'? */
1365 case TH_STATE_HALTED:
1368 case TH_STATE_WAITING:
1371 case TH_STATE_UNINTERRUPTIBLE:
1374 /* There is no `zombie' case here,
1375 * since there are no zombie-threads.
1376 * There's only zombie tasks, which are
1379 WARNING ("Unknown thread status: %i",
1380 thread_data.run_state);
1382 } /* switch (thread_data.run_state) */
1384 if (task_list[task] != port_task_self)
1386 status = mach_port_deallocate (port_task_self,
1387 thread_list[thread]);
1388 if (status != KERN_SUCCESS)
1389 ERROR ("mach_port_deallocate failed: %s",
1390 mach_error_string (status));
1392 } /* for (thread_list) */
1394 if ((status = vm_deallocate (port_task_self,
1395 (vm_address_t) thread_list,
1396 thread_list_len * sizeof (thread_act_t)))
1399 ERROR ("vm_deallocate failed: %s",
1400 mach_error_string (status));
1403 thread_list_len = 0;
1405 /* Only deallocate the task port, if it isn't our own.
1406 * Don't know what would happen in that case, but this
1407 * is what Apple's top does.. ;) */
1408 if (task_list[task] != port_task_self)
1410 status = mach_port_deallocate (port_task_self,
1412 if (status != KERN_SUCCESS)
1413 ERROR ("mach_port_deallocate failed: %s",
1414 mach_error_string (status));
1418 /* FIXME: cmdline should be here instead of NULL */
1419 ps_list_add (task_name, NULL, &pse);
1420 } /* for (task_list) */
1422 if ((status = vm_deallocate (port_task_self,
1423 (vm_address_t) task_list,
1424 task_list_len * sizeof (task_t))) != KERN_SUCCESS)
1426 ERROR ("vm_deallocate failed: %s",
1427 mach_error_string (status));
1432 if ((status = mach_port_deallocate (port_task_self, port_pset_priv))
1435 ERROR ("mach_port_deallocate failed: %s",
1436 mach_error_string (status));
1438 } /* for (pset_list) */
1440 ps_submit_state ("running", running);
1441 ps_submit_state ("sleeping", sleeping);
1442 ps_submit_state ("zombies", zombies);
1443 ps_submit_state ("stopped", stopped);
1444 ps_submit_state ("blocked", blocked);
1446 for (ps = list_head_g; ps != NULL; ps = ps->next)
1447 ps_submit_proc_list (ps);
1448 /* #endif HAVE_THREAD_INFO */
1462 char cmdline[ARG_MAX];
1466 procstat_entry_t pse;
1469 unsigned long fork_rate;
1473 running = sleeping = zombies = stopped = paging = blocked = 0;
1476 if ((proc = opendir ("/proc")) == NULL)
1479 ERROR ("Cannot open `/proc': %s",
1480 sstrerror (errno, errbuf, sizeof (errbuf)));
1484 while ((ent = readdir (proc)) != NULL)
1486 if (!isdigit (ent->d_name[0]))
1489 if ((pid = atoi (ent->d_name)) < 1)
1492 status = ps_read_process (pid, &ps, &state);
1495 DEBUG ("ps_read_process failed: %i", status);
1502 pse.num_proc = ps.num_proc;
1503 pse.num_lwp = ps.num_lwp;
1504 pse.vmem_size = ps.vmem_size;
1505 pse.vmem_rss = ps.vmem_rss;
1506 pse.vmem_data = ps.vmem_data;
1507 pse.vmem_code = ps.vmem_code;
1508 pse.stack_size = ps.stack_size;
1510 pse.vmem_minflt = 0;
1511 pse.vmem_minflt_counter = ps.vmem_minflt_counter;
1512 pse.vmem_majflt = 0;
1513 pse.vmem_majflt_counter = ps.vmem_majflt_counter;
1516 pse.cpu_user_counter = ps.cpu_user_counter;
1518 pse.cpu_system_counter = ps.cpu_system_counter;
1520 pse.io_rchar = ps.io_rchar;
1521 pse.io_wchar = ps.io_wchar;
1522 pse.io_syscr = ps.io_syscr;
1523 pse.io_syscw = ps.io_syscw;
1527 case 'R': running++; break;
1528 case 'S': sleeping++; break;
1529 case 'D': blocked++; break;
1530 case 'Z': zombies++; break;
1531 case 'T': stopped++; break;
1532 case 'W': paging++; break;
1535 ps_list_add (ps.name,
1536 ps_get_cmdline (pid, ps.name, cmdline, sizeof (cmdline)),
1542 ps_submit_state ("running", running);
1543 ps_submit_state ("sleeping", sleeping);
1544 ps_submit_state ("zombies", zombies);
1545 ps_submit_state ("stopped", stopped);
1546 ps_submit_state ("paging", paging);
1547 ps_submit_state ("blocked", blocked);
1549 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1550 ps_submit_proc_list (ps_ptr);
1552 fork_rate = read_fork_rate();
1553 if (fork_rate != ULONG_MAX)
1554 ps_submit_fork_rate(fork_rate);
1555 /* #endif KERNEL_LINUX */
1557 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD
1568 char cmdline[ARG_MAX];
1570 struct kinfo_proc *procs; /* array of processes */
1572 int count; /* returns number of processes */
1576 procstat_entry_t pse;
1580 /* Open the kvm interface, get a descriptor */
1581 kd = kvm_open (NULL, NULL, NULL, 0, errbuf);
1584 ERROR ("processes plugin: Cannot open kvm interface: %s",
1589 /* Get the list of processes. */
1590 procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, &count);
1594 ERROR ("processes plugin: Cannot get kvm processes list: %s",
1599 /* Iterate through the processes in kinfo_proc */
1600 for (i = 0; i < count; i++)
1602 /* retrieve the arguments */
1606 argv = kvm_getargv (kd, (const struct kinfo_proc *) &(procs[i]), 0);
1613 while (argv[argc] != NULL)
1616 status = strjoin (cmdline, sizeof (cmdline),
1621 WARNING ("processes plugin: Command line did "
1622 "not fit into buffer.");
1626 cmdline_ptr = &cmdline[0];
1630 pse.id = procs[i].ki_pid;
1634 pse.num_lwp = procs[i].ki_numthreads;
1636 pse.vmem_size = procs[i].ki_size;
1637 pse.vmem_rss = procs[i].ki_rssize * getpagesize();
1638 pse.vmem_data = procs[i].ki_dsize * getpagesize();
1639 pse.vmem_code = procs[i].ki_tsize * getpagesize();
1640 pse.stack_size = procs[i].ki_ssize * getpagesize();
1641 pse.vmem_minflt = 0;
1642 pse.vmem_minflt_counter = procs[i].ki_rusage.ru_minflt;
1643 pse.vmem_majflt = 0;
1644 pse.vmem_majflt_counter = procs[i].ki_rusage.ru_majflt;
1647 pse.cpu_user_counter = procs[i].ki_rusage.ru_utime.tv_sec
1649 + procs[i].ki_rusage.ru_utime.tv_usec;
1651 pse.cpu_system_counter = procs[i].ki_rusage.ru_stime.tv_sec
1653 + procs[i].ki_rusage.ru_stime.tv_usec;
1661 switch (procs[i].ki_stat)
1663 case SSTOP: stopped++; break;
1664 case SSLEEP: sleeping++; break;
1665 case SRUN: running++; break;
1666 case SIDL: idle++; break;
1667 case SWAIT: wait++; break;
1668 case SLOCK: blocked++; break;
1669 case SZOMB: zombies++; break;
1672 ps_list_add (procs[i].ki_comm, cmdline_ptr, &pse);
1677 ps_submit_state ("running", running);
1678 ps_submit_state ("sleeping", sleeping);
1679 ps_submit_state ("zombies", zombies);
1680 ps_submit_state ("stopped", stopped);
1681 ps_submit_state ("blocked", blocked);
1682 ps_submit_state ("idle", idle);
1683 ps_submit_state ("wait", wait);
1685 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1686 ps_submit_proc_list (ps_ptr);
1687 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
1689 #elif HAVE_PROCINFO_H
1702 procstat_entry_t pse;
1705 while ((nprocs = getprocs64 (procentry, sizeof(struct procentry64),
1706 /* fdsinfo = */ NULL, sizeof(struct fdsinfo64),
1707 &pindex, MAXPROCENTRY)) > 0)
1711 for (i = 0; i < nprocs; i++)
1715 char arglist[MAXARGLN+1];
1719 if (procentry[i].pi_state == SNONE) continue;
1720 /* if (procentry[i].pi_state == SZOMB) FIXME */
1722 cmdline = procentry[i].pi_comm;
1723 cargs = procentry[i].pi_comm;
1724 if ( procentry[i].pi_flags & SKPROC )
1726 if (procentry[i].pi_pid == 0)
1727 cmdline = "swapper";
1732 if (getargs(&procentry[i], sizeof(struct procentry64), arglist, MAXARGLN) >= 0)
1737 while (++n < MAXARGLN)
1739 if (arglist[n] == '\0')
1741 if (arglist[n+1] == '\0')
1750 pse.id = procentry[i].pi_pid;
1752 pse.num_lwp = procentry[i].pi_thcount;
1756 while ((nthreads = getthrds64(procentry[i].pi_pid,
1757 thrdentry, sizeof(struct thrdentry64),
1758 &thindex, MAXTHRDENTRY)) > 0)
1762 for (j=0; j< nthreads; j++)
1764 switch (thrdentry[j].ti_state)
1766 /* case TSNONE: break; */
1767 case TSIDL: blocked++; break; /* FIXME is really blocked */
1768 case TSRUN: running++; break;
1769 case TSSLEEP: sleeping++; break;
1770 case TSSWAP: paging++; break;
1771 case TSSTOP: stopped++; break;
1772 case TSZOMB: zombies++; break;
1775 if (nthreads < MAXTHRDENTRY)
1780 /* tv_usec is nanosec ??? */
1781 pse.cpu_user_counter = procentry[i].pi_ru.ru_utime.tv_sec * 1000000 +
1782 procentry[i].pi_ru.ru_utime.tv_usec / 1000;
1785 /* tv_usec is nanosec ??? */
1786 pse.cpu_system_counter = procentry[i].pi_ru.ru_stime.tv_sec * 1000000 +
1787 procentry[i].pi_ru.ru_stime.tv_usec / 1000;
1789 pse.vmem_minflt = 0;
1790 pse.vmem_minflt_counter = procentry[i].pi_minflt;
1791 pse.vmem_majflt = 0;
1792 pse.vmem_majflt_counter = procentry[i].pi_majflt;
1794 pse.vmem_size = procentry[i].pi_tsize + procentry[i].pi_dvm * pagesize;
1795 pse.vmem_rss = (procentry[i].pi_drss + procentry[i].pi_trss) * pagesize;
1801 ps_list_add (cmdline, cargs, &pse);
1802 } /* for (i = 0 .. nprocs) */
1804 if (nprocs < MAXPROCENTRY)
1806 } /* while (getprocs64() > 0) */
1807 ps_submit_state ("running", running);
1808 ps_submit_state ("sleeping", sleeping);
1809 ps_submit_state ("zombies", zombies);
1810 ps_submit_state ("stopped", stopped);
1811 ps_submit_state ("paging", paging);
1812 ps_submit_state ("blocked", blocked);
1814 for (ps = list_head_g; ps != NULL; ps = ps->next)
1815 ps_submit_proc_list (ps);
1816 #endif /* HAVE_PROCINFO_H */
1821 void module_register (void)
1823 plugin_register_config ("processes", ps_config,
1824 config_keys, config_keys_num);
1825 plugin_register_init ("processes", ps_init);
1826 plugin_register_read ("processes", ps_read);
1827 } /* void module_register */