2 * collectd - src/processes.c
3 * Copyright (C) 2005 Lyonel Vincent
4 * Copyright (C) 2006-2010 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
10 * Copyright (C) 2012 Cosmin Ioiart
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27 * Lyonel Vincent <lyonel at ezix.org>
28 * Florian octo Forster <octo at collectd.org>
29 * Oleg King <king2 at kaluga.ru>
30 * Sebastian Harl <sh at tokkee.org>
31 * Andrés J. Díaz <ajdiaz at connectical.com>
33 * Clément Stenac <clement.stenac at diwi.org>
34 * Cosmin Ioiart <cioiart at gmail.com>
42 /* Include header files for the mach system, if they exist.. */
44 # if HAVE_MACH_MACH_INIT_H
45 # include <mach/mach_init.h>
47 # if HAVE_MACH_HOST_PRIV_H
48 # include <mach/host_priv.h>
50 # if HAVE_MACH_MACH_ERROR_H
51 # include <mach/mach_error.h>
53 # if HAVE_MACH_MACH_HOST_H
54 # include <mach/mach_host.h>
56 # if HAVE_MACH_MACH_PORT_H
57 # include <mach/mach_port.h>
59 # if HAVE_MACH_MACH_TYPES_H
60 # include <mach/mach_types.h>
62 # if HAVE_MACH_MESSAGE_H
63 # include <mach/message.h>
65 # if HAVE_MACH_PROCESSOR_SET_H
66 # include <mach/processor_set.h>
69 # include <mach/task.h>
71 # if HAVE_MACH_THREAD_ACT_H
72 # include <mach/thread_act.h>
74 # if HAVE_MACH_VM_REGION_H
75 # include <mach/vm_region.h>
77 # if HAVE_MACH_VM_MAP_H
78 # include <mach/vm_map.h>
80 # if HAVE_MACH_VM_PROT_H
81 # include <mach/vm_prot.h>
83 # if HAVE_SYS_SYSCTL_H
84 # include <sys/sysctl.h>
86 /* #endif HAVE_THREAD_INFO */
89 # if HAVE_LINUX_CONFIG_H
90 # include <linux/config.h>
93 # define CONFIG_HZ 100
95 /* #endif KERNEL_LINUX */
97 #elif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD)
99 # include <sys/param.h>
100 # include <sys/sysctl.h>
101 # include <sys/user.h>
102 # include <sys/proc.h>
103 /* #endif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD) */
105 #elif HAVE_PROCINFO_H
106 # include <procinfo.h>
107 # include <sys/types.h>
109 #define MAXPROCENTRY 32
110 #define MAXTHRDENTRY 16
111 #define MAXARGLN 1024
112 /* #endif HAVE_PROCINFO_H */
115 /* Hack: Avoid #error when building a 32-bit binary with
116 * _FILE_OFFSET_BITS=64. There is a reason for this #error, as one
117 * of the structures in <sys/procfs.h> uses an off_t, but that
118 * isn't relevant to our usage of procfs. */
119 #if !defined(_LP64) && _FILE_OFFSET_BITS == 64
121 # undef _FILE_OFFSET_BITS
127 # define _FILE_OFFSET_BITS 64
131 # include <sys/user.h>
135 # define MAXCOMLEN 16
138 /* #endif KERNEL_SOLARIS */
141 # error "No applicable input method."
152 #ifndef CMDLINE_BUFFER_SIZE
153 # if defined(ARG_MAX) && (ARG_MAX < 4096)
154 # define CMDLINE_BUFFER_SIZE ARG_MAX
156 # define CMDLINE_BUFFER_SIZE 4096
160 typedef struct procstat_entry_s
165 unsigned long num_proc;
166 unsigned long num_lwp;
167 unsigned long vmem_size;
168 unsigned long vmem_rss;
169 unsigned long vmem_data;
170 unsigned long vmem_code;
171 unsigned long stack_size;
173 derive_t vmem_minflt_counter;
174 derive_t vmem_majflt_counter;
176 derive_t cpu_user_counter;
177 derive_t cpu_system_counter;
186 derive_t cswitch_vol;
187 derive_t cswitch_invol;
190 struct procstat_entry_s *next;
193 #define PROCSTAT_NAME_LEN 256
194 typedef struct procstat
196 char name[PROCSTAT_NAME_LEN];
201 unsigned long num_proc;
202 unsigned long num_lwp;
203 unsigned long vmem_size;
204 unsigned long vmem_rss;
205 unsigned long vmem_data;
206 unsigned long vmem_code;
207 unsigned long stack_size;
209 derive_t vmem_minflt_counter;
210 derive_t vmem_majflt_counter;
212 derive_t cpu_user_counter;
213 derive_t cpu_system_counter;
221 derive_t cswitch_vol;
222 derive_t cswitch_invol;
224 struct procstat *next;
225 struct procstat_entry_s *instances;
228 static procstat_t *list_head_g = NULL;
230 static _Bool want_init = 1;
231 static _Bool report_ctx_switch = 0;
234 static mach_port_t port_host_self;
235 static mach_port_t port_task_self;
237 static processor_set_name_array_t pset_list;
238 static mach_msg_type_number_t pset_list_len;
239 /* #endif HAVE_THREAD_INFO */
242 static long pagesize_g;
243 static void ps_fill_details (const procstat_t *ps, procstat_entry_t *entry);
244 /* #endif KERNEL_LINUX */
246 #elif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD)
248 /* #endif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD) */
250 #elif HAVE_PROCINFO_H
251 static struct procentry64 procentry[MAXPROCENTRY];
252 static struct thrdentry64 thrdentry[MAXTHRDENTRY];
255 #ifndef _AIXVERSION_610
256 int getprocs64 (void *procsinfo, int sizproc, void *fdsinfo, int sizfd, pid_t *index, int count);
257 int getthrds64( pid_t, void *, int, tid64_t *, int );
259 int getargs (void *processBuffer, int bufferLen, char *argsBuffer, int argsLen);
260 #endif /* HAVE_PROCINFO_H */
262 /* put name of process from config to list_head_g tree
263 * list_head_g is a list of 'procstat_t' structs with
264 * processes names we want to watch */
265 static void ps_list_register (const char *name, const char *regexp)
271 new = calloc (1, sizeof (*new));
274 ERROR ("processes plugin: ps_list_register: calloc failed.");
277 sstrncpy (new->name, name, sizeof (new->name));
282 DEBUG ("ProcessMatch: adding \"%s\" as criteria to process %s.", regexp, name);
283 new->re = malloc (sizeof (*new->re));
286 ERROR ("processes plugin: ps_list_register: malloc failed.");
291 status = regcomp (new->re, regexp, REG_EXTENDED | REG_NOSUB);
294 DEBUG ("ProcessMatch: compiling the regular expression \"%s\" failed.", regexp);
303 ERROR ("processes plugin: ps_list_register: "
304 "Regular expression \"%s\" found in config "
305 "file, but support for regular expressions "
306 "has been disabled at compile time.",
313 for (ptr = list_head_g; ptr != NULL; ptr = ptr->next)
315 if (strcmp (ptr->name, name) == 0)
317 WARNING ("processes plugin: You have configured more "
318 "than one `Process' or "
319 "`ProcessMatch' with the same name. "
320 "All but the first setting will be "
329 if (ptr->next == NULL)
337 } /* void ps_list_register */
339 /* try to match name against entry, returns 1 if success */
340 static int ps_list_match (const char *name, const char *cmdline, procstat_t *ps)
349 if ((str == NULL) || (str[0] == 0))
352 assert (str != NULL);
354 status = regexec (ps->re, str,
363 if (strcmp (ps->name, name) == 0)
367 } /* int ps_list_match */
369 static void ps_update_counter (derive_t *group_counter,
370 derive_t *curr_counter, derive_t new_counter)
372 unsigned long curr_value;
376 *curr_counter = new_counter;
380 if (new_counter < *curr_counter)
381 curr_value = new_counter + (ULONG_MAX - *curr_counter);
383 curr_value = new_counter - *curr_counter;
385 *curr_counter = new_counter;
386 *group_counter += curr_value;
389 /* add process entry to 'instances' of process 'name' (or refresh it) */
390 static void ps_list_add (const char *name, const char *cmdline, procstat_entry_t *entry)
392 procstat_entry_t *pse;
397 for (procstat_t *ps = list_head_g; ps != NULL; ps = ps->next)
399 if ((ps_list_match (name, cmdline, ps)) == 0)
403 ps_fill_details(ps, entry);
406 for (pse = ps->instances; pse != NULL; pse = pse->next)
407 if ((pse->id == entry->id) || (pse->next == NULL))
410 if ((pse == NULL) || (pse->id != entry->id))
412 procstat_entry_t *new;
414 new = calloc (1, sizeof (*new));
428 pse->num_proc = entry->num_proc;
429 pse->num_lwp = entry->num_lwp;
430 pse->vmem_size = entry->vmem_size;
431 pse->vmem_rss = entry->vmem_rss;
432 pse->vmem_data = entry->vmem_data;
433 pse->vmem_code = entry->vmem_code;
434 pse->stack_size = entry->stack_size;
435 pse->io_rchar = entry->io_rchar;
436 pse->io_wchar = entry->io_wchar;
437 pse->io_syscr = entry->io_syscr;
438 pse->io_syscw = entry->io_syscw;
439 pse->cswitch_vol = entry->cswitch_vol;
440 pse->cswitch_invol = entry->cswitch_invol;
442 ps->num_proc += pse->num_proc;
443 ps->num_lwp += pse->num_lwp;
444 ps->vmem_size += pse->vmem_size;
445 ps->vmem_rss += pse->vmem_rss;
446 ps->vmem_data += pse->vmem_data;
447 ps->vmem_code += pse->vmem_code;
448 ps->stack_size += pse->stack_size;
450 ps->io_rchar += ((pse->io_rchar == -1)?0:pse->io_rchar);
451 ps->io_wchar += ((pse->io_wchar == -1)?0:pse->io_wchar);
452 ps->io_syscr += ((pse->io_syscr == -1)?0:pse->io_syscr);
453 ps->io_syscw += ((pse->io_syscw == -1)?0:pse->io_syscw);
455 ps->cswitch_vol += ((pse->cswitch_vol == -1)?0:pse->cswitch_vol);
456 ps->cswitch_invol += ((pse->cswitch_invol == -1)?0:pse->cswitch_invol);
459 &ps->vmem_minflt_counter,
460 &pse->vmem_minflt_counter,
461 entry->vmem_minflt_counter);
463 &ps->vmem_majflt_counter,
464 &pse->vmem_majflt_counter,
465 entry->vmem_majflt_counter);
468 &ps->cpu_user_counter,
469 &pse->cpu_user_counter,
470 entry->cpu_user_counter);
472 &ps->cpu_system_counter,
473 &pse->cpu_system_counter,
474 entry->cpu_system_counter);
478 /* remove old entries from instances of processes in list_head_g */
479 static void ps_list_reset (void)
481 procstat_entry_t *pse;
482 procstat_entry_t *pse_prev;
484 for (procstat_t *ps = list_head_g; ps != NULL; ps = ps->next)
497 ps->cswitch_vol = -1;
498 ps->cswitch_invol = -1;
506 DEBUG ("Removing this procstat entry cause it's too old: "
507 "id = %lu; name = %s;",
510 if (pse_prev == NULL)
512 ps->instances = pse->next;
518 pse_prev->next = pse->next;
520 pse = pse_prev->next;
529 } /* while (pse != NULL) */
530 } /* for (ps = list_head_g; ps != NULL; ps = ps->next) */
533 /* put all pre-defined 'Process' names from config to list_head_g tree */
534 static int ps_config (oconfig_item_t *ci)
537 const size_t max_procname_len = 15;
538 #elif KERNEL_SOLARIS || KERNEL_FREEBSD
539 const size_t max_procname_len = MAXCOMLEN -1;
542 for (int i = 0; i < ci->children_num; ++i) {
543 oconfig_item_t *c = ci->children + i;
545 if (strcasecmp (c->key, "Process") == 0)
547 if ((c->values_num != 1)
548 || (OCONFIG_TYPE_STRING != c->values[0].type)) {
549 ERROR ("processes plugin: `Process' expects exactly "
550 "one string argument (got %i).",
555 if (c->children_num != 0) {
556 WARNING ("processes plugin: the `Process' config option "
557 "does not expect any child elements -- ignoring "
558 "content (%i elements) of the <Process '%s'> block.",
559 c->children_num, c->values[0].value.string);
562 #if KERNEL_LINUX || KERNEL_SOLARIS || KERNEL_FREEBSD
563 if (strlen (c->values[0].value.string) > max_procname_len) {
564 WARNING ("processes plugin: this platform has a %zu character limit "
565 "to process names. The `Process \"%s\"' option will "
566 "not work as expected.",
567 max_procname_len, c->values[0].value.string);
571 ps_list_register (c->values[0].value.string, NULL);
573 else if (strcasecmp (c->key, "ProcessMatch") == 0)
575 if ((c->values_num != 2)
576 || (OCONFIG_TYPE_STRING != c->values[0].type)
577 || (OCONFIG_TYPE_STRING != c->values[1].type))
579 ERROR ("processes plugin: `ProcessMatch' needs exactly "
580 "two string arguments (got %i).",
585 if (c->children_num != 0) {
586 WARNING ("processes plugin: the `ProcessMatch' config option "
587 "does not expect any child elements -- ignoring "
588 "content (%i elements) of the <ProcessMatch '%s' '%s'> "
589 "block.", c->children_num, c->values[0].value.string,
590 c->values[1].value.string);
593 ps_list_register (c->values[0].value.string,
594 c->values[1].value.string);
596 else if (strcasecmp (c->key, "CollectContextSwitch") == 0)
598 cf_util_get_boolean (c, &report_ctx_switch);
602 ERROR ("processes plugin: The `%s' configuration option is not "
603 "understood and will be ignored.", c->key);
611 static int ps_init (void)
614 kern_return_t status;
616 port_host_self = mach_host_self ();
617 port_task_self = mach_task_self ();
619 if (pset_list != NULL)
621 vm_deallocate (port_task_self,
622 (vm_address_t) pset_list,
623 pset_list_len * sizeof (processor_set_t));
628 if ((status = host_processor_sets (port_host_self,
630 &pset_list_len)) != KERN_SUCCESS)
632 ERROR ("host_processor_sets failed: %s\n",
633 mach_error_string (status));
638 /* #endif HAVE_THREAD_INFO */
641 pagesize_g = sysconf(_SC_PAGESIZE);
642 DEBUG ("pagesize_g = %li; CONFIG_HZ = %i;",
643 pagesize_g, CONFIG_HZ);
644 /* #endif KERNEL_LINUX */
646 #elif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD)
647 pagesize = getpagesize();
648 /* #endif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD) */
650 #elif HAVE_PROCINFO_H
651 pagesize = getpagesize();
652 #endif /* HAVE_PROCINFO_H */
657 /* submit global state (e.g.: qty of zombies, running, etc..) */
658 static void ps_submit_state (const char *state, double value)
660 value_list_t vl = VALUE_LIST_INIT;
662 vl.values = &(value_t) { .gauge = value };
664 sstrncpy (vl.plugin, "processes", sizeof (vl.plugin));
665 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
666 sstrncpy (vl.type, "ps_state", sizeof (vl.type));
667 sstrncpy (vl.type_instance, state, sizeof (vl.type_instance));
669 plugin_dispatch_values (&vl);
672 /* submit info about specific process (e.g.: memory taken, cpu usage, etc..) */
673 static void ps_submit_proc_list (procstat_t *ps)
675 value_list_t vl = VALUE_LIST_INIT;
679 sstrncpy (vl.plugin, "processes", sizeof (vl.plugin));
680 sstrncpy (vl.plugin_instance, ps->name, sizeof (vl.plugin_instance));
682 sstrncpy (vl.type, "ps_vm", sizeof (vl.type));
683 vl.values[0].gauge = ps->vmem_size;
685 plugin_dispatch_values (&vl);
687 sstrncpy (vl.type, "ps_rss", sizeof (vl.type));
688 vl.values[0].gauge = ps->vmem_rss;
690 plugin_dispatch_values (&vl);
692 sstrncpy (vl.type, "ps_data", sizeof (vl.type));
693 vl.values[0].gauge = ps->vmem_data;
695 plugin_dispatch_values (&vl);
697 sstrncpy (vl.type, "ps_code", sizeof (vl.type));
698 vl.values[0].gauge = ps->vmem_code;
700 plugin_dispatch_values (&vl);
702 sstrncpy (vl.type, "ps_stacksize", sizeof (vl.type));
703 vl.values[0].gauge = ps->stack_size;
705 plugin_dispatch_values (&vl);
707 sstrncpy (vl.type, "ps_cputime", sizeof (vl.type));
708 vl.values[0].derive = ps->cpu_user_counter;
709 vl.values[1].derive = ps->cpu_system_counter;
711 plugin_dispatch_values (&vl);
713 sstrncpy (vl.type, "ps_count", sizeof (vl.type));
714 vl.values[0].gauge = ps->num_proc;
715 vl.values[1].gauge = ps->num_lwp;
717 plugin_dispatch_values (&vl);
719 sstrncpy (vl.type, "ps_pagefaults", sizeof (vl.type));
720 vl.values[0].derive = ps->vmem_minflt_counter;
721 vl.values[1].derive = ps->vmem_majflt_counter;
723 plugin_dispatch_values (&vl);
725 if ( (ps->io_rchar != -1) && (ps->io_wchar != -1) )
727 sstrncpy (vl.type, "ps_disk_octets", sizeof (vl.type));
728 vl.values[0].derive = ps->io_rchar;
729 vl.values[1].derive = ps->io_wchar;
731 plugin_dispatch_values (&vl);
734 if ( (ps->io_syscr != -1) && (ps->io_syscw != -1) )
736 sstrncpy (vl.type, "ps_disk_ops", sizeof (vl.type));
737 vl.values[0].derive = ps->io_syscr;
738 vl.values[1].derive = ps->io_syscw;
740 plugin_dispatch_values (&vl);
743 if ( report_ctx_switch )
745 sstrncpy (vl.type, "contextswitch", sizeof (vl.type));
746 sstrncpy (vl.type_instance, "voluntary", sizeof (vl.type_instance));
747 vl.values[0].derive = ps->cswitch_vol;
749 plugin_dispatch_values (&vl);
751 sstrncpy (vl.type, "contextswitch", sizeof (vl.type));
752 sstrncpy (vl.type_instance, "involuntary", sizeof (vl.type_instance));
753 vl.values[0].derive = ps->cswitch_invol;
755 plugin_dispatch_values (&vl);
758 DEBUG ("name = %s; num_proc = %lu; num_lwp = %lu; "
759 "vmem_size = %lu; vmem_rss = %lu; vmem_data = %lu; "
761 "vmem_minflt_counter = %"PRIi64"; vmem_majflt_counter = %"PRIi64"; "
762 "cpu_user_counter = %"PRIi64"; cpu_system_counter = %"PRIi64"; "
763 "io_rchar = %"PRIi64"; io_wchar = %"PRIi64"; "
764 "io_syscr = %"PRIi64"; io_syscw = %"PRIi64"; "
765 "cswitch_vol = %"PRIi64"; cswitch_invol = %"PRIi64";",
766 ps->name, ps->num_proc, ps->num_lwp,
767 ps->vmem_size, ps->vmem_rss,
768 ps->vmem_data, ps->vmem_code,
769 ps->vmem_minflt_counter, ps->vmem_majflt_counter,
770 ps->cpu_user_counter, ps->cpu_system_counter,
771 ps->io_rchar, ps->io_wchar, ps->io_syscr, ps->io_syscw,
772 ps->cswitch_vol, ps->cswitch_invol);
773 } /* void ps_submit_proc_list */
775 #if KERNEL_LINUX || KERNEL_SOLARIS
776 static void ps_submit_fork_rate (derive_t value)
778 value_list_t vl = VALUE_LIST_INIT;
780 vl.values = &(value_t) { .derive = value };
782 sstrncpy(vl.plugin, "processes", sizeof (vl.plugin));
783 sstrncpy(vl.plugin_instance, "", sizeof (vl.plugin_instance));
784 sstrncpy(vl.type, "fork_rate", sizeof (vl.type));
785 sstrncpy(vl.type_instance, "", sizeof (vl.type_instance));
787 plugin_dispatch_values(&vl);
789 #endif /* KERNEL_LINUX || KERNEL_SOLARIS*/
791 /* ------- additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
793 static int ps_read_tasks_status (procstat_entry_t *ps)
800 derive_t cswitch_vol = 0;
801 derive_t cswitch_invol = 0;
806 ssnprintf (dirname, sizeof (dirname), "/proc/%li/task", ps->id);
808 if ((dh = opendir (dirname)) == NULL)
810 DEBUG ("Failed to open directory `%s'", dirname);
814 while ((ent = readdir (dh)) != NULL)
818 if (!isdigit ((int) ent->d_name[0]))
823 ssnprintf (filename, sizeof (filename), "/proc/%li/task/%s/status", ps->id, tpid);
824 if ((fh = fopen (filename, "r")) == NULL)
826 DEBUG ("Failed to open file `%s'", filename);
830 while (fgets (buffer, sizeof(buffer), fh) != NULL)
835 if (strncmp (buffer, "voluntary_ctxt_switches", 23) != 0
836 && strncmp (buffer, "nonvoluntary_ctxt_switches", 26) != 0)
839 numfields = strsplit (buffer, fields,
840 STATIC_ARRAY_SIZE (fields));
847 tmp = (derive_t) strtoll (fields[1], &endptr, /* base = */ 10);
848 if ((errno == 0) && (endptr != fields[1]))
850 if (strncmp (buffer, "voluntary_ctxt_switches", 23) == 0)
854 else if (strncmp (buffer, "nonvoluntary_ctxt_switches", 26) == 0)
856 cswitch_invol += tmp;
859 } /* while (fgets) */
864 WARNING ("processes: fclose: %s",
865 sstrerror (errno, errbuf, sizeof (errbuf)));
870 ps->cswitch_vol = cswitch_vol;
871 ps->cswitch_invol = cswitch_invol;
874 } /* int *ps_read_tasks_status */
876 /* Read data from /proc/pid/status */
877 static procstat_t *ps_read_status (long pid, procstat_t *ps)
882 unsigned long lib = 0;
883 unsigned long exe = 0;
884 unsigned long data = 0;
885 unsigned long threads = 0;
889 ssnprintf (filename, sizeof (filename), "/proc/%li/status", pid);
890 if ((fh = fopen (filename, "r")) == NULL)
893 while (fgets (buffer, sizeof(buffer), fh) != NULL)
898 if (strncmp (buffer, "Vm", 2) != 0
899 && strncmp (buffer, "Threads", 7) != 0)
902 numfields = strsplit (buffer, fields,
903 STATIC_ARRAY_SIZE (fields));
910 tmp = strtoul (fields[1], &endptr, /* base = */ 10);
911 if ((errno == 0) && (endptr != fields[1]))
913 if (strncmp (buffer, "VmData", 6) == 0)
917 else if (strncmp (buffer, "VmLib", 5) == 0)
921 else if (strncmp(buffer, "VmExe", 5) == 0)
925 else if (strncmp(buffer, "Threads", 7) == 0)
930 } /* while (fgets) */
935 WARNING ("processes: fclose: %s",
936 sstrerror (errno, errbuf, sizeof (errbuf)));
939 ps->vmem_data = data * 1024;
940 ps->vmem_code = (exe + lib) * 1024;
942 ps->num_lwp = threads;
945 } /* procstat_t *ps_read_vmem */
947 static int ps_read_io (procstat_entry_t *ps)
956 ssnprintf (filename, sizeof (filename), "/proc/%li/io", ps->id);
957 if ((fh = fopen (filename, "r")) == NULL)
960 while (fgets (buffer, sizeof (buffer), fh) != NULL)
962 derive_t *val = NULL;
966 if (strncasecmp (buffer, "rchar:", 6) == 0)
967 val = &(ps->io_rchar);
968 else if (strncasecmp (buffer, "wchar:", 6) == 0)
969 val = &(ps->io_wchar);
970 else if (strncasecmp (buffer, "syscr:", 6) == 0)
971 val = &(ps->io_syscr);
972 else if (strncasecmp (buffer, "syscw:", 6) == 0)
973 val = &(ps->io_syscw);
977 numfields = strsplit (buffer, fields,
978 STATIC_ARRAY_SIZE (fields));
985 tmp = strtoll (fields[1], &endptr, /* base = */ 10);
986 if ((errno != 0) || (endptr == fields[1]))
989 *val = (derive_t) tmp;
990 } /* while (fgets) */
995 WARNING ("processes: fclose: %s",
996 sstrerror (errno, errbuf, sizeof (errbuf)));
999 } /* int ps_read_io (...) */
1001 static void ps_fill_details (const procstat_t *ps, procstat_entry_t *entry)
1003 if ( entry->has_io == 0 && ps_read_io (entry) != 0 )
1006 entry->io_rchar = -1;
1007 entry->io_wchar = -1;
1008 entry->io_syscr = -1;
1009 entry->io_syscw = -1;
1011 DEBUG("ps_read_process: not get io data for pid %li", entry->id);
1015 if ( report_ctx_switch )
1017 if ( entry->has_cswitch == 0 && ps_read_tasks_status(entry) != 0 )
1019 entry->cswitch_vol = -1;
1020 entry->cswitch_invol = -1;
1022 DEBUG("ps_read_tasks_status: not get context "
1023 "switch data for pid %li", entry->id);
1025 entry->has_cswitch = 1;
1027 } /* void ps_fill_details (...) */
1029 static int ps_read_process (long pid, procstat_t *ps, char *state)
1040 size_t name_start_pos;
1041 size_t name_end_pos;
1044 derive_t cpu_user_counter;
1045 derive_t cpu_system_counter;
1046 long long unsigned vmem_size;
1047 long long unsigned vmem_rss;
1048 long long unsigned stack_size;
1052 memset (ps, 0, sizeof (procstat_t));
1054 ssnprintf (filename, sizeof (filename), "/proc/%li/stat", pid);
1056 status = read_file_contents (filename, buffer, sizeof(buffer) - 1);
1059 buffer_len = (size_t) status;
1060 buffer[buffer_len] = 0;
1062 /* The name of the process is enclosed in parens. Since the name can
1063 * contain parens itself, spaces, numbers and pretty much everything
1064 * else, use these to determine the process name. We don't use
1065 * strchr(3) and strrchr(3) to avoid pointer arithmetic which would
1066 * otherwise be required to determine name_len. */
1068 while (name_start_pos < buffer_len && buffer[name_start_pos] != '(')
1071 name_end_pos = buffer_len;
1072 while (name_end_pos > 0 && buffer[name_end_pos] != ')')
1075 /* Either '(' or ')' is not found or they are in the wrong order.
1076 * Anyway, something weird that shouldn't happen ever. */
1077 if (name_start_pos >= name_end_pos)
1079 ERROR ("processes plugin: name_start_pos = %zu >= name_end_pos = %zu",
1080 name_start_pos, name_end_pos);
1084 name_len = (name_end_pos - name_start_pos) - 1;
1085 if (name_len >= sizeof (ps->name))
1086 name_len = sizeof (ps->name) - 1;
1088 sstrncpy (ps->name, &buffer[name_start_pos + 1], name_len + 1);
1090 if ((buffer_len - name_end_pos) < 2)
1092 buffer_ptr = &buffer[name_end_pos + 2];
1094 fields_len = strsplit (buffer_ptr, fields, STATIC_ARRAY_SIZE (fields));
1095 if (fields_len < 22)
1097 DEBUG ("processes plugin: ps_read_process (pid = %li):"
1098 " `%s' has only %i fields..",
1099 pid, filename, fields_len);
1103 *state = fields[0][0];
1112 ps->num_lwp = strtoul (fields[17], /* endptr = */ NULL, /* base = */ 10);
1113 if ((ps_read_status(pid, ps)) == NULL)
1118 DEBUG("ps_read_process: did not get vmem data for pid %li", pid);
1120 if (ps->num_lwp == 0)
1125 /* Leave the rest at zero if this is only a zombi */
1126 if (ps->num_proc == 0)
1128 DEBUG ("processes plugin: This is only a zombie: pid = %li; "
1129 "name = %s;", pid, ps->name);
1133 cpu_user_counter = atoll (fields[11]);
1134 cpu_system_counter = atoll (fields[12]);
1135 vmem_size = atoll (fields[20]);
1136 vmem_rss = atoll (fields[21]);
1137 ps->vmem_minflt_counter = atol (fields[7]);
1138 ps->vmem_majflt_counter = atol (fields[9]);
1141 unsigned long long stack_start = atoll (fields[25]);
1142 unsigned long long stack_ptr = atoll (fields[26]);
1144 stack_size = (stack_start > stack_ptr)
1145 ? stack_start - stack_ptr
1146 : stack_ptr - stack_start;
1149 /* Convert jiffies to useconds */
1150 cpu_user_counter = cpu_user_counter * 1000000 / CONFIG_HZ;
1151 cpu_system_counter = cpu_system_counter * 1000000 / CONFIG_HZ;
1152 vmem_rss = vmem_rss * pagesize_g;
1154 ps->cpu_user_counter = cpu_user_counter;
1155 ps->cpu_system_counter = cpu_system_counter;
1156 ps->vmem_size = (unsigned long) vmem_size;
1157 ps->vmem_rss = (unsigned long) vmem_rss;
1158 ps->stack_size = (unsigned long) stack_size;
1162 } /* int ps_read_process (...) */
1164 static char *ps_get_cmdline (long pid, char *name, char *buf, size_t buf_len)
1169 char file[PATH_MAX];
1174 if ((pid < 1) || (NULL == buf) || (buf_len < 2))
1177 ssnprintf (file, sizeof (file), "/proc/%li/cmdline", pid);
1180 fd = open (file, O_RDONLY);
1183 /* ENOENT means the process exited while we were handling it.
1184 * Don't complain about this, it only fills the logs. */
1185 if (errno != ENOENT)
1186 WARNING ("processes plugin: Failed to open `%s': %s.", file,
1187 sstrerror (errno, errbuf, sizeof (errbuf)));
1199 status = read (fd, (void *)buf_ptr, len);
1204 if ((EAGAIN == errno) || (EINTR == errno))
1207 WARNING ("processes plugin: Failed to read from `%s': %s.", file,
1208 sstrerror (errno, errbuf, sizeof (errbuf)));
1228 /* cmdline not available; e.g. kernel thread, zombie */
1232 ssnprintf (buf, buf_len, "[%s]", name);
1236 assert (n <= buf_len);
1243 /* remove trailing whitespace */
1244 while ((n > 0) && (isspace (buf[n]) || ('\0' == buf[n]))) {
1249 /* arguments are separated by '\0' in /proc/<pid>/cmdline */
1256 } /* char *ps_get_cmdline (...) */
1258 static int read_fork_rate (void)
1263 _Bool value_valid = 0;
1265 proc_stat = fopen ("/proc/stat", "r");
1266 if (proc_stat == NULL)
1269 ERROR ("processes plugin: fopen (/proc/stat) failed: %s",
1270 sstrerror (errno, errbuf, sizeof (errbuf)));
1274 while (fgets (buffer, sizeof (buffer), proc_stat) != NULL)
1280 fields_num = strsplit (buffer, fields,
1281 STATIC_ARRAY_SIZE (fields));
1282 if (fields_num != 2)
1285 if (strcmp ("processes", fields[0]) != 0)
1288 status = parse_value (fields[1], &value, DS_TYPE_DERIVE);
1299 ps_submit_fork_rate (value.derive);
1302 #endif /*KERNEL_LINUX */
1305 static char *ps_get_cmdline (long pid, char *name __attribute__((unused)), /* {{{ */
1306 char *buffer, size_t buffer_size)
1308 char path[PATH_MAX];
1312 snprintf(path, sizeof (path), "/proc/%li/psinfo", pid);
1314 status = read_file_contents (path, (void *) &info, sizeof (info));
1315 if ((status < 0) || (((size_t) status) != sizeof (info)))
1317 ERROR ("processes plugin: Unexpected return value "
1318 "while reading \"%s\": "
1319 "Returned %zd but expected %zu.",
1320 path, status, buffer_size);
1324 info.pr_psargs[sizeof (info.pr_psargs) - 1] = 0;
1325 sstrncpy (buffer, info.pr_psargs, buffer_size);
1328 } /* }}} int ps_get_cmdline */
1331 * Reads process information on the Solaris OS. The information comes mainly from
1332 * /proc/PID/status, /proc/PID/psinfo and /proc/PID/usage
1333 * The values for input and ouput chars are calculated "by hand"
1334 * Added a few "solaris" specific process states as well
1336 static int ps_read_process(long pid, procstat_t *ps, char *state)
1339 char f_psinfo[64], f_usage[64];
1342 pstatus_t *myStatus;
1346 snprintf(filename, sizeof (filename), "/proc/%li/status", pid);
1347 snprintf(f_psinfo, sizeof (f_psinfo), "/proc/%li/psinfo", pid);
1348 snprintf(f_usage, sizeof (f_usage), "/proc/%li/usage", pid);
1351 buffer = calloc(1, sizeof (pstatus_t));
1352 read_file_contents(filename, buffer, sizeof (pstatus_t));
1353 myStatus = (pstatus_t *) buffer;
1355 buffer = calloc(1, sizeof (psinfo_t));
1356 read_file_contents(f_psinfo, buffer, sizeof (psinfo_t));
1357 myInfo = (psinfo_t *) buffer;
1359 buffer = calloc(1, sizeof (prusage_t));
1360 read_file_contents(f_usage, buffer, sizeof (prusage_t));
1361 myUsage = (prusage_t *) buffer;
1363 sstrncpy(ps->name, myInfo->pr_fname, sizeof (myInfo->pr_fname));
1364 ps->num_lwp = myStatus->pr_nlwp;
1365 if (myInfo->pr_wstat != 0) {
1368 *state = (char) 'Z';
1376 ps->num_lwp = myInfo->pr_nlwp;
1380 * Convert system time and user time from nanoseconds to microseconds
1381 * for compatibility with the linux module
1383 ps->cpu_system_counter = myStatus -> pr_stime.tv_nsec / 1000;
1384 ps->cpu_user_counter = myStatus -> pr_utime.tv_nsec / 1000;
1387 * Convert rssize from KB to bytes to be consistent w/ the linux module
1389 ps->vmem_rss = myInfo->pr_rssize * 1024;
1390 ps->vmem_size = myInfo->pr_size * 1024;
1391 ps->vmem_minflt_counter = myUsage->pr_minf;
1392 ps->vmem_majflt_counter = myUsage->pr_majf;
1395 * TODO: Data and code segment calculations for Solaris
1400 ps->stack_size = myStatus->pr_stksize;
1403 * Calculating input/ouput chars
1404 * Formula used is total chars / total blocks => chars/block
1405 * then convert input/output blocks to chars
1407 ulong_t tot_chars = myUsage->pr_ioch;
1408 ulong_t tot_blocks = myUsage->pr_inblk + myUsage->pr_oublk;
1409 ulong_t chars_per_block = 1;
1410 if (tot_blocks != 0)
1411 chars_per_block = tot_chars / tot_blocks;
1412 ps->io_rchar = myUsage->pr_inblk * chars_per_block;
1413 ps->io_wchar = myUsage->pr_oublk * chars_per_block;
1414 ps->io_syscr = myUsage->pr_sysc;
1415 ps->io_syscw = myUsage->pr_sysc;
1418 * TODO: context switch counters for Solaris
1420 ps->cswitch_vol = -1;
1421 ps->cswitch_invol = -1;
1425 * TODO: Find way of setting BLOCKED and PAGING status
1428 *state = (char) 'R';
1429 if (myStatus->pr_flags & PR_ASLEEP)
1430 *state = (char) 'S';
1431 else if (myStatus->pr_flags & PR_STOPPED)
1432 *state = (char) 'T';
1433 else if (myStatus->pr_flags & PR_DETACH)
1434 *state = (char) 'E';
1435 else if (myStatus->pr_flags & PR_DAEMON)
1436 *state = (char) 'A';
1437 else if (myStatus->pr_flags & PR_ISSYS)
1438 *state = (char) 'Y';
1439 else if (myStatus->pr_flags & PR_ORPHAN)
1440 *state = (char) 'O';
1450 * Reads the number of threads created since the last reboot. On Solaris these
1451 * are retrieved from kstat (module cpu, name sys, class misc, stat nthreads).
1452 * The result is the sum for all the threads created on each cpu
1454 static int read_fork_rate (void)
1456 extern kstat_ctl_t *kc;
1457 derive_t result = 0;
1462 for (kstat_t *ksp_chain = kc->kc_chain;
1464 ksp_chain = ksp_chain->ks_next)
1466 if ((strcmp (ksp_chain->ks_module, "cpu") == 0)
1467 && (strcmp (ksp_chain->ks_name, "sys") == 0)
1468 && (strcmp (ksp_chain->ks_class, "misc") == 0))
1472 kstat_read (kc, ksp_chain, NULL);
1474 tmp = get_kstat_value(ksp_chain, "nthreads");
1480 ps_submit_fork_rate (result);
1483 #endif /* KERNEL_SOLARIS */
1485 #if HAVE_THREAD_INFO
1486 static int mach_get_task_name (task_t t, int *pid, char *name, size_t name_max_len)
1490 struct kinfo_proc kp;
1495 mib[2] = KERN_PROC_PID;
1497 if (pid_for_task (t, pid) != KERN_SUCCESS)
1501 kp_size = sizeof (kp);
1502 if (sysctl (mib, 4, &kp, &kp_size, NULL, 0) != 0)
1505 if (name_max_len > (MAXCOMLEN + 1))
1506 name_max_len = MAXCOMLEN + 1;
1508 strncpy (name, kp.kp_proc.p_comm, name_max_len - 1);
1509 name[name_max_len - 1] = '\0';
1511 DEBUG ("pid = %i; name = %s;", *pid, name);
1513 /* We don't do the special handling for `p_comm == "LaunchCFMApp"' as
1514 * `top' does it, because it is a lot of work and only used when
1515 * debugging. -octo */
1519 #endif /* HAVE_THREAD_INFO */
1520 /* ------- end of additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
1522 /* do actual readings from kernel */
1523 static int ps_read (void)
1525 #if HAVE_THREAD_INFO
1526 kern_return_t status;
1528 processor_set_t port_pset_priv;
1530 task_array_t task_list;
1531 mach_msg_type_number_t task_list_len;
1534 char task_name[MAXCOMLEN + 1];
1536 thread_act_array_t thread_list;
1537 mach_msg_type_number_t thread_list_len;
1538 thread_basic_info_data_t thread_data;
1539 mach_msg_type_number_t thread_data_len;
1548 procstat_entry_t pse;
1553 * The Mach-concept is a little different from the traditional UNIX
1554 * concept: All the work is done in threads. Threads are contained in
1555 * `tasks'. Therefore, `task status' doesn't make much sense, since
1556 * it's actually a `thread status'.
1557 * Tasks are assigned to sets of processors, so that's where you go to
1560 for (mach_msg_type_number_t pset = 0; pset < pset_list_len; pset++)
1562 if ((status = host_processor_set_priv (port_host_self,
1564 &port_pset_priv)) != KERN_SUCCESS)
1566 ERROR ("host_processor_set_priv failed: %s\n",
1567 mach_error_string (status));
1571 if ((status = processor_set_tasks (port_pset_priv,
1573 &task_list_len)) != KERN_SUCCESS)
1575 ERROR ("processor_set_tasks failed: %s\n",
1576 mach_error_string (status));
1577 mach_port_deallocate (port_task_self, port_pset_priv);
1581 for (mach_msg_type_number_t task = 0; task < task_list_len; task++)
1584 if (mach_get_task_name (task_list[task],
1586 task_name, PROCSTAT_NAME_LEN) == 0)
1588 /* search for at least one match */
1589 for (ps = list_head_g; ps != NULL; ps = ps->next)
1590 /* FIXME: cmdline should be here instead of NULL */
1591 if (ps_list_match (task_name, NULL, ps) == 1)
1595 /* Collect more detailed statistics for this process */
1598 task_basic_info_data_t task_basic_info;
1599 mach_msg_type_number_t task_basic_info_len;
1600 task_events_info_data_t task_events_info;
1601 mach_msg_type_number_t task_events_info_len;
1602 task_absolutetime_info_data_t task_absolutetime_info;
1603 mach_msg_type_number_t task_absolutetime_info_len;
1605 memset (&pse, '\0', sizeof (pse));
1608 task_basic_info_len = TASK_BASIC_INFO_COUNT;
1609 status = task_info (task_list[task],
1611 (task_info_t) &task_basic_info,
1612 &task_basic_info_len);
1613 if (status != KERN_SUCCESS)
1615 ERROR ("task_info failed: %s",
1616 mach_error_string (status));
1617 continue; /* with next thread_list */
1620 task_events_info_len = TASK_EVENTS_INFO_COUNT;
1621 status = task_info (task_list[task],
1623 (task_info_t) &task_events_info,
1624 &task_events_info_len);
1625 if (status != KERN_SUCCESS)
1627 ERROR ("task_info failed: %s",
1628 mach_error_string (status));
1629 continue; /* with next thread_list */
1632 task_absolutetime_info_len = TASK_ABSOLUTETIME_INFO_COUNT;
1633 status = task_info (task_list[task],
1634 TASK_ABSOLUTETIME_INFO,
1635 (task_info_t) &task_absolutetime_info,
1636 &task_absolutetime_info_len);
1637 if (status != KERN_SUCCESS)
1639 ERROR ("task_info failed: %s",
1640 mach_error_string (status));
1641 continue; /* with next thread_list */
1645 pse.vmem_size = task_basic_info.virtual_size;
1646 pse.vmem_rss = task_basic_info.resident_size;
1647 /* Does not seem to be easily exposed */
1651 pse.vmem_minflt_counter = task_events_info.cow_faults;
1652 pse.vmem_majflt_counter = task_events_info.faults;
1654 pse.cpu_user_counter = task_absolutetime_info.total_user;
1655 pse.cpu_system_counter = task_absolutetime_info.total_system;
1657 /* context switch counters not implemented */
1658 pse.cswitch_vol = -1;
1659 pse.cswitch_invol = -1;
1662 status = task_threads (task_list[task], &thread_list,
1664 if (status != KERN_SUCCESS)
1666 /* Apple's `top' treats this case a zombie. It
1667 * makes sense to some extend: A `zombie'
1668 * thread is nonsense, since the task/process
1671 DEBUG ("task_threads failed: %s",
1672 mach_error_string (status));
1673 if (task_list[task] != port_task_self)
1674 mach_port_deallocate (port_task_self,
1676 continue; /* with next task_list */
1679 for (mach_msg_type_number_t thread = 0; thread < thread_list_len; thread++)
1681 thread_data_len = THREAD_BASIC_INFO_COUNT;
1682 status = thread_info (thread_list[thread],
1684 (thread_info_t) &thread_data,
1686 if (status != KERN_SUCCESS)
1688 ERROR ("thread_info failed: %s",
1689 mach_error_string (status));
1690 if (task_list[task] != port_task_self)
1691 mach_port_deallocate (port_task_self,
1692 thread_list[thread]);
1693 continue; /* with next thread_list */
1699 switch (thread_data.run_state)
1701 case TH_STATE_RUNNING:
1704 case TH_STATE_STOPPED:
1705 /* What exactly is `halted'? */
1706 case TH_STATE_HALTED:
1709 case TH_STATE_WAITING:
1712 case TH_STATE_UNINTERRUPTIBLE:
1715 /* There is no `zombie' case here,
1716 * since there are no zombie-threads.
1717 * There's only zombie tasks, which are
1720 WARNING ("Unknown thread status: %i",
1721 thread_data.run_state);
1723 } /* switch (thread_data.run_state) */
1725 if (task_list[task] != port_task_self)
1727 status = mach_port_deallocate (port_task_self,
1728 thread_list[thread]);
1729 if (status != KERN_SUCCESS)
1730 ERROR ("mach_port_deallocate failed: %s",
1731 mach_error_string (status));
1733 } /* for (thread_list) */
1735 if ((status = vm_deallocate (port_task_self,
1736 (vm_address_t) thread_list,
1737 thread_list_len * sizeof (thread_act_t)))
1740 ERROR ("vm_deallocate failed: %s",
1741 mach_error_string (status));
1744 thread_list_len = 0;
1746 /* Only deallocate the task port, if it isn't our own.
1747 * Don't know what would happen in that case, but this
1748 * is what Apple's top does.. ;) */
1749 if (task_list[task] != port_task_self)
1751 status = mach_port_deallocate (port_task_self,
1753 if (status != KERN_SUCCESS)
1754 ERROR ("mach_port_deallocate failed: %s",
1755 mach_error_string (status));
1759 /* FIXME: cmdline should be here instead of NULL */
1760 ps_list_add (task_name, NULL, &pse);
1761 } /* for (task_list) */
1763 if ((status = vm_deallocate (port_task_self,
1764 (vm_address_t) task_list,
1765 task_list_len * sizeof (task_t))) != KERN_SUCCESS)
1767 ERROR ("vm_deallocate failed: %s",
1768 mach_error_string (status));
1773 if ((status = mach_port_deallocate (port_task_self, port_pset_priv))
1776 ERROR ("mach_port_deallocate failed: %s",
1777 mach_error_string (status));
1779 } /* for (pset_list) */
1781 ps_submit_state ("running", running);
1782 ps_submit_state ("sleeping", sleeping);
1783 ps_submit_state ("zombies", zombies);
1784 ps_submit_state ("stopped", stopped);
1785 ps_submit_state ("blocked", blocked);
1787 for (ps = list_head_g; ps != NULL; ps = ps->next)
1788 ps_submit_proc_list (ps);
1789 /* #endif HAVE_THREAD_INFO */
1803 char cmdline[CMDLINE_BUFFER_SIZE];
1807 procstat_entry_t pse;
1810 running = sleeping = zombies = stopped = paging = blocked = 0;
1813 if ((proc = opendir ("/proc")) == NULL)
1816 ERROR ("Cannot open `/proc': %s",
1817 sstrerror (errno, errbuf, sizeof (errbuf)));
1821 while ((ent = readdir (proc)) != NULL)
1823 if (!isdigit (ent->d_name[0]))
1826 if ((pid = atol (ent->d_name)) < 1)
1829 status = ps_read_process (pid, &ps, &state);
1832 DEBUG ("ps_read_process failed: %i", status);
1836 memset (&pse, 0, sizeof (pse));
1840 pse.num_proc = ps.num_proc;
1841 pse.num_lwp = ps.num_lwp;
1842 pse.vmem_size = ps.vmem_size;
1843 pse.vmem_rss = ps.vmem_rss;
1844 pse.vmem_data = ps.vmem_data;
1845 pse.vmem_code = ps.vmem_code;
1846 pse.stack_size = ps.stack_size;
1848 pse.vmem_minflt_counter = ps.vmem_minflt_counter;
1849 pse.vmem_majflt_counter = ps.vmem_majflt_counter;
1851 pse.cpu_user_counter = ps.cpu_user_counter;
1852 pse.cpu_system_counter = ps.cpu_system_counter;
1854 pse.io_rchar = ps.io_rchar;
1855 pse.io_wchar = ps.io_wchar;
1856 pse.io_syscr = ps.io_syscr;
1857 pse.io_syscw = ps.io_syscw;
1859 pse.cswitch_vol = ps.cswitch_vol;
1860 pse.cswitch_invol = ps.cswitch_invol;
1864 case 'R': running++; break;
1865 case 'S': sleeping++; break;
1866 case 'D': blocked++; break;
1867 case 'Z': zombies++; break;
1868 case 'T': stopped++; break;
1869 case 'W': paging++; break;
1872 ps_list_add (ps.name,
1873 ps_get_cmdline (pid, ps.name, cmdline, sizeof (cmdline)),
1879 ps_submit_state ("running", running);
1880 ps_submit_state ("sleeping", sleeping);
1881 ps_submit_state ("zombies", zombies);
1882 ps_submit_state ("stopped", stopped);
1883 ps_submit_state ("paging", paging);
1884 ps_submit_state ("blocked", blocked);
1886 for (procstat_t *ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1887 ps_submit_proc_list (ps_ptr);
1890 /* #endif KERNEL_LINUX */
1892 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD
1902 char errbuf[_POSIX2_LINE_MAX];
1903 struct kinfo_proc *procs; /* array of processes */
1904 struct kinfo_proc *proc_ptr = NULL;
1905 int count; /* returns number of processes */
1907 procstat_entry_t pse;
1911 /* Open the kvm interface, get a descriptor */
1912 kd = kvm_openfiles (NULL, "/dev/null", NULL, 0, errbuf);
1915 ERROR ("processes plugin: Cannot open kvm interface: %s",
1920 /* Get the list of processes. */
1921 procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, &count);
1924 ERROR ("processes plugin: Cannot get kvm processes list: %s",
1930 /* Iterate through the processes in kinfo_proc */
1931 for (int i = 0; i < count; i++)
1933 /* Create only one process list entry per _process_, i.e.
1934 * filter out threads (duplicate PID entries). */
1935 if ((proc_ptr == NULL) || (proc_ptr->ki_pid != procs[i].ki_pid))
1937 char cmdline[CMDLINE_BUFFER_SIZE] = "";
1938 _Bool have_cmdline = 0;
1940 proc_ptr = &(procs[i]);
1941 /* Don't probe system processes and processes without arguments */
1942 if (((procs[i].ki_flag & P_SYSTEM) == 0)
1943 && (procs[i].ki_args != NULL))
1949 /* retrieve the arguments */
1950 argv = kvm_getargv (kd, proc_ptr, /* nchr = */ 0);
1952 if ((argv != NULL) && (argv[0] != NULL))
1954 while (argv[argc] != NULL)
1957 status = strjoin (cmdline, sizeof (cmdline), argv, argc, " ");
1959 WARNING ("processes plugin: Command line did not fit into buffer.");
1963 } /* if (process has argument list) */
1965 pse.id = procs[i].ki_pid;
1969 pse.num_lwp = procs[i].ki_numthreads;
1971 pse.vmem_size = procs[i].ki_size;
1972 pse.vmem_rss = procs[i].ki_rssize * pagesize;
1973 pse.vmem_data = procs[i].ki_dsize * pagesize;
1974 pse.vmem_code = procs[i].ki_tsize * pagesize;
1975 pse.stack_size = procs[i].ki_ssize * pagesize;
1976 pse.vmem_minflt_counter = procs[i].ki_rusage.ru_minflt;
1977 pse.vmem_majflt_counter = procs[i].ki_rusage.ru_majflt;
1979 pse.cpu_user_counter = 0;
1980 pse.cpu_system_counter = 0;
1982 * The u-area might be swapped out, and we can't get
1983 * at it because we have a crashdump and no swap.
1984 * If it's here fill in these fields, otherwise, just
1987 if (procs[i].ki_flag & P_INMEM)
1989 pse.cpu_user_counter = procs[i].ki_rusage.ru_utime.tv_usec
1990 + (1000000lu * procs[i].ki_rusage.ru_utime.tv_sec);
1991 pse.cpu_system_counter = procs[i].ki_rusage.ru_stime.tv_usec
1992 + (1000000lu * procs[i].ki_rusage.ru_stime.tv_sec);
2001 /* context switch counters not implemented */
2002 pse.cswitch_vol = -1;
2003 pse.cswitch_invol = -1;
2005 ps_list_add (procs[i].ki_comm, have_cmdline ? cmdline : NULL, &pse);
2007 switch (procs[i].ki_stat)
2009 case SSTOP: stopped++; break;
2010 case SSLEEP: sleeping++; break;
2011 case SRUN: running++; break;
2012 case SIDL: idle++; break;
2013 case SWAIT: wait++; break;
2014 case SLOCK: blocked++; break;
2015 case SZOMB: zombies++; break;
2017 } /* if ((proc_ptr == NULL) || (proc_ptr->ki_pid != procs[i].ki_pid)) */
2022 ps_submit_state ("running", running);
2023 ps_submit_state ("sleeping", sleeping);
2024 ps_submit_state ("zombies", zombies);
2025 ps_submit_state ("stopped", stopped);
2026 ps_submit_state ("blocked", blocked);
2027 ps_submit_state ("idle", idle);
2028 ps_submit_state ("wait", wait);
2030 for (procstat_t *ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
2031 ps_submit_proc_list (ps_ptr);
2032 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
2034 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_OPENBSD
2045 struct kinfo_proc *procs; /* array of processes */
2046 struct kinfo_proc *proc_ptr = NULL;
2047 int count; /* returns number of processes */
2049 procstat_entry_t pse;
2053 /* Open the kvm interface, get a descriptor */
2054 kd = kvm_open (NULL, NULL, NULL, 0, errbuf);
2057 ERROR ("processes plugin: Cannot open kvm interface: %s",
2062 /* Get the list of processes. */
2063 procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc), &count);
2066 ERROR ("processes plugin: Cannot get kvm processes list: %s",
2072 /* Iterate through the processes in kinfo_proc */
2073 for (int i = 0; i < count; i++)
2075 /* Create only one process list entry per _process_, i.e.
2076 * filter out threads (duplicate PID entries). */
2077 if ((proc_ptr == NULL) || (proc_ptr->p_pid != procs[i].p_pid))
2079 char cmdline[CMDLINE_BUFFER_SIZE] = "";
2080 _Bool have_cmdline = 0;
2082 proc_ptr = &(procs[i]);
2083 /* Don't probe zombie processes */
2084 if (!P_ZOMBIE(proc_ptr))
2090 /* retrieve the arguments */
2091 argv = kvm_getargv (kd, proc_ptr, /* nchr = */ 0);
2093 if ((argv != NULL) && (argv[0] != NULL))
2095 while (argv[argc] != NULL)
2098 status = strjoin (cmdline, sizeof (cmdline), argv, argc, " ");
2100 WARNING ("processes plugin: Command line did not fit into buffer.");
2104 } /* if (process has argument list) */
2106 memset (&pse, 0, sizeof (pse));
2107 pse.id = procs[i].p_pid;
2111 pse.num_lwp = 1; /* XXX: accumulate p_tid values for a single p_pid ? */
2113 pse.vmem_rss = procs[i].p_vm_rssize * pagesize;
2114 pse.vmem_data = procs[i].p_vm_dsize * pagesize;
2115 pse.vmem_code = procs[i].p_vm_tsize * pagesize;
2116 pse.stack_size = procs[i].p_vm_ssize * pagesize;
2117 pse.vmem_size = pse.stack_size + pse.vmem_code + pse.vmem_data;
2118 pse.vmem_minflt_counter = procs[i].p_uru_minflt;
2119 pse.vmem_majflt_counter = procs[i].p_uru_majflt;
2121 pse.cpu_user_counter = procs[i].p_uutime_usec +
2122 (1000000lu * procs[i].p_uutime_sec);
2123 pse.cpu_system_counter = procs[i].p_ustime_usec +
2124 (1000000lu * procs[i].p_ustime_sec);
2132 /* context switch counters not implemented */
2133 pse.cswitch_vol = -1;
2134 pse.cswitch_invol = -1;
2136 ps_list_add (procs[i].p_comm, have_cmdline ? cmdline : NULL, &pse);
2138 switch (procs[i].p_stat)
2140 case SSTOP: stopped++; break;
2141 case SSLEEP: sleeping++; break;
2142 case SRUN: running++; break;
2143 case SIDL: idle++; break;
2144 case SONPROC: onproc++; break;
2145 case SDEAD: dead++; break;
2146 case SZOMB: zombies++; break;
2148 } /* if ((proc_ptr == NULL) || (proc_ptr->p_pid != procs[i].p_pid)) */
2153 ps_submit_state ("running", running);
2154 ps_submit_state ("sleeping", sleeping);
2155 ps_submit_state ("zombies", zombies);
2156 ps_submit_state ("stopped", stopped);
2157 ps_submit_state ("onproc", onproc);
2158 ps_submit_state ("idle", idle);
2159 ps_submit_state ("dead", dead);
2161 for (procstat_t *ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
2162 ps_submit_proc_list (ps_ptr);
2163 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_OPENBSD */
2165 #elif HAVE_PROCINFO_H
2177 procstat_entry_t pse;
2180 while ((nprocs = getprocs64 (procentry, sizeof(struct procentry64),
2181 /* fdsinfo = */ NULL, sizeof(struct fdsinfo64),
2182 &pindex, MAXPROCENTRY)) > 0)
2184 for (int i = 0; i < nprocs; i++)
2188 char arglist[MAXARGLN+1];
2192 if (procentry[i].pi_state == SNONE) continue;
2193 /* if (procentry[i].pi_state == SZOMB) FIXME */
2195 cmdline = procentry[i].pi_comm;
2196 cargs = procentry[i].pi_comm;
2197 if ( procentry[i].pi_flags & SKPROC )
2199 if (procentry[i].pi_pid == 0)
2200 cmdline = "swapper";
2205 if (getargs(&procentry[i], sizeof(struct procentry64), arglist, MAXARGLN) >= 0)
2210 while (++n < MAXARGLN)
2212 if (arglist[n] == '\0')
2214 if (arglist[n+1] == '\0')
2223 pse.id = procentry[i].pi_pid;
2225 pse.num_lwp = procentry[i].pi_thcount;
2229 while ((nthreads = getthrds64(procentry[i].pi_pid,
2230 thrdentry, sizeof(struct thrdentry64),
2231 &thindex, MAXTHRDENTRY)) > 0)
2235 for (j=0; j< nthreads; j++)
2237 switch (thrdentry[j].ti_state)
2239 /* case TSNONE: break; */
2240 case TSIDL: blocked++; break; /* FIXME is really blocked */
2241 case TSRUN: running++; break;
2242 case TSSLEEP: sleeping++; break;
2243 case TSSWAP: paging++; break;
2244 case TSSTOP: stopped++; break;
2245 case TSZOMB: zombies++; break;
2248 if (nthreads < MAXTHRDENTRY)
2252 /* tv_usec is nanosec ??? */
2253 pse.cpu_user_counter = procentry[i].pi_ru.ru_utime.tv_sec * 1000000 +
2254 procentry[i].pi_ru.ru_utime.tv_usec / 1000;
2257 /* tv_usec is nanosec ??? */
2258 pse.cpu_system_counter = procentry[i].pi_ru.ru_stime.tv_sec * 1000000 +
2259 procentry[i].pi_ru.ru_stime.tv_usec / 1000;
2261 pse.vmem_minflt_counter = procentry[i].pi_minflt;
2262 pse.vmem_majflt_counter = procentry[i].pi_majflt;
2264 pse.vmem_size = procentry[i].pi_tsize + procentry[i].pi_dvm * pagesize;
2265 pse.vmem_rss = (procentry[i].pi_drss + procentry[i].pi_trss) * pagesize;
2276 pse.cswitch_vol = -1;
2277 pse.cswitch_invol = -1;
2279 ps_list_add (cmdline, cargs, &pse);
2280 } /* for (i = 0 .. nprocs) */
2282 if (nprocs < MAXPROCENTRY)
2284 } /* while (getprocs64() > 0) */
2285 ps_submit_state ("running", running);
2286 ps_submit_state ("sleeping", sleeping);
2287 ps_submit_state ("zombies", zombies);
2288 ps_submit_state ("stopped", stopped);
2289 ps_submit_state ("paging", paging);
2290 ps_submit_state ("blocked", blocked);
2292 for (procstat_t *ps = list_head_g; ps != NULL; ps = ps->next)
2293 ps_submit_proc_list (ps);
2294 /* #endif HAVE_PROCINFO_H */
2296 #elif KERNEL_SOLARIS
2298 * The Solaris section adds a few more process states and removes some
2299 * process states compared to linux. Most notably there is no "PAGING"
2300 * and "BLOCKED" state for a process. The rest is similar to the linux
2318 char cmdline[PRARGSZ];
2322 proc = opendir ("/proc");
2326 while ((ent = readdir(proc)) != NULL)
2330 procstat_entry_t pse;
2333 if (!isdigit ((int) ent->d_name[0]))
2336 pid = strtol (ent->d_name, &endptr, 10);
2337 if (*endptr != 0) /* value didn't completely parse as a number */
2340 status = ps_read_process (pid, &ps, &state);
2343 DEBUG("ps_read_process failed: %i", status);
2347 memset (&pse, 0, sizeof (pse));
2351 pse.num_proc = ps.num_proc;
2352 pse.num_lwp = ps.num_lwp;
2353 pse.vmem_size = ps.vmem_size;
2354 pse.vmem_rss = ps.vmem_rss;
2355 pse.vmem_data = ps.vmem_data;
2356 pse.vmem_code = ps.vmem_code;
2357 pse.stack_size = ps.stack_size;
2359 pse.vmem_minflt_counter = ps.vmem_minflt_counter;
2360 pse.vmem_majflt_counter = ps.vmem_majflt_counter;
2362 pse.cpu_user_counter = ps.cpu_user_counter;
2363 pse.cpu_system_counter = ps.cpu_system_counter;
2365 pse.io_rchar = ps.io_rchar;
2366 pse.io_wchar = ps.io_wchar;
2367 pse.io_syscr = ps.io_syscr;
2368 pse.io_syscw = ps.io_syscw;
2370 pse.cswitch_vol = -1;
2371 pse.cswitch_invol = -1;
2375 case 'R': running++; break;
2376 case 'S': sleeping++; break;
2377 case 'E': detached++; break;
2378 case 'Z': zombies++; break;
2379 case 'T': stopped++; break;
2380 case 'A': daemon++; break;
2381 case 'Y': system++; break;
2382 case 'O': orphan++; break;
2386 ps_list_add (ps.name,
2387 ps_get_cmdline (pid, ps.name, cmdline, sizeof (cmdline)),
2389 } /* while(readdir) */
2392 ps_submit_state ("running", running);
2393 ps_submit_state ("sleeping", sleeping);
2394 ps_submit_state ("zombies", zombies);
2395 ps_submit_state ("stopped", stopped);
2396 ps_submit_state ("detached", detached);
2397 ps_submit_state ("daemon", daemon);
2398 ps_submit_state ("system", system);
2399 ps_submit_state ("orphan", orphan);
2401 for (procstat_t *ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
2402 ps_submit_proc_list (ps_ptr);
2405 #endif /* KERNEL_SOLARIS */
2412 void module_register (void)
2414 plugin_register_complex_config ("processes", ps_config);
2415 plugin_register_init ("processes", ps_init);
2416 plugin_register_read ("processes", ps_read);
2417 } /* void module_register */