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;
185 derive_t cswitch_vol;
186 derive_t cswitch_invol;
188 struct procstat_entry_s *next;
191 #define PROCSTAT_NAME_LEN 256
192 typedef struct procstat
194 char name[PROCSTAT_NAME_LEN];
199 unsigned long num_proc;
200 unsigned long num_lwp;
201 unsigned long vmem_size;
202 unsigned long vmem_rss;
203 unsigned long vmem_data;
204 unsigned long vmem_code;
205 unsigned long stack_size;
207 derive_t vmem_minflt_counter;
208 derive_t vmem_majflt_counter;
210 derive_t cpu_user_counter;
211 derive_t cpu_system_counter;
219 derive_t cswitch_vol;
220 derive_t cswitch_invol;
222 struct procstat *next;
223 struct procstat_entry_s *instances;
226 static procstat_t *list_head_g = NULL;
228 static _Bool report_ctx_switch = 0;
231 static mach_port_t port_host_self;
232 static mach_port_t port_task_self;
234 static processor_set_name_array_t pset_list;
235 static mach_msg_type_number_t pset_list_len;
236 /* #endif HAVE_THREAD_INFO */
239 static long pagesize_g;
240 /* #endif KERNEL_LINUX */
242 #elif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD)
244 /* #endif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD) */
246 #elif HAVE_PROCINFO_H
247 static struct procentry64 procentry[MAXPROCENTRY];
248 static struct thrdentry64 thrdentry[MAXTHRDENTRY];
251 #ifndef _AIXVERSION_610
252 int getprocs64 (void *procsinfo, int sizproc, void *fdsinfo, int sizfd, pid_t *index, int count);
253 int getthrds64( pid_t, void *, int, tid64_t *, int );
255 int getargs (void *processBuffer, int bufferLen, char *argsBuffer, int argsLen);
256 #endif /* HAVE_PROCINFO_H */
258 /* put name of process from config to list_head_g tree
259 * list_head_g is a list of 'procstat_t' structs with
260 * processes names we want to watch */
261 static void ps_list_register (const char *name, const char *regexp)
267 new = calloc (1, sizeof (*new));
270 ERROR ("processes plugin: ps_list_register: calloc failed.");
273 sstrncpy (new->name, name, sizeof (new->name));
278 DEBUG ("ProcessMatch: adding \"%s\" as criteria to process %s.", regexp, name);
279 new->re = malloc (sizeof (*new->re));
282 ERROR ("processes plugin: ps_list_register: malloc failed.");
287 status = regcomp (new->re, regexp, REG_EXTENDED | REG_NOSUB);
290 DEBUG ("ProcessMatch: compiling the regular expression \"%s\" failed.", regexp);
299 ERROR ("processes plugin: ps_list_register: "
300 "Regular expression \"%s\" found in config "
301 "file, but support for regular expressions "
302 "has been disabled at compile time.",
309 for (ptr = list_head_g; ptr != NULL; ptr = ptr->next)
311 if (strcmp (ptr->name, name) == 0)
313 WARNING ("processes plugin: You have configured more "
314 "than one `Process' or "
315 "`ProcessMatch' with the same name. "
316 "All but the first setting will be "
325 if (ptr->next == NULL)
333 } /* void ps_list_register */
335 /* try to match name against entry, returns 1 if success */
336 static int ps_list_match (const char *name, const char *cmdline, procstat_t *ps)
345 if ((str == NULL) || (str[0] == 0))
348 assert (str != NULL);
350 status = regexec (ps->re, str,
359 if (strcmp (ps->name, name) == 0)
363 } /* int ps_list_match */
365 static void ps_update_counter (_Bool init, derive_t *group_counter,
366 derive_t *curr_counter, derive_t new_counter)
368 unsigned long curr_value;
373 if (new_counter < *curr_counter)
374 curr_value = new_counter + (ULONG_MAX - *curr_counter);
376 curr_value = new_counter - *curr_counter;
378 *curr_counter = new_counter;
379 *group_counter += curr_value;
382 /* add process entry to 'instances' of process 'name' (or refresh it) */
383 static void ps_list_add (const char *name, const char *cmdline, procstat_entry_t *entry)
385 procstat_entry_t *pse;
390 for (procstat_t *ps = list_head_g; ps != NULL; ps = ps->next)
394 if ((ps_list_match (name, cmdline, ps)) == 0)
397 for (pse = ps->instances; pse != NULL; pse = pse->next)
398 if ((pse->id == entry->id) || (pse->next == NULL))
401 if ((pse == NULL) || (pse->id != entry->id))
403 procstat_entry_t *new;
405 new = calloc (1, sizeof (*new));
419 pse->num_proc = entry->num_proc;
420 pse->num_lwp = entry->num_lwp;
421 pse->vmem_size = entry->vmem_size;
422 pse->vmem_rss = entry->vmem_rss;
423 pse->vmem_data = entry->vmem_data;
424 pse->vmem_code = entry->vmem_code;
425 pse->stack_size = entry->stack_size;
426 pse->io_rchar = entry->io_rchar;
427 pse->io_wchar = entry->io_wchar;
428 pse->io_syscr = entry->io_syscr;
429 pse->io_syscw = entry->io_syscw;
430 pse->cswitch_vol = entry->cswitch_vol;
431 pse->cswitch_invol = entry->cswitch_invol;
433 ps->num_proc += pse->num_proc;
434 ps->num_lwp += pse->num_lwp;
435 ps->vmem_size += pse->vmem_size;
436 ps->vmem_rss += pse->vmem_rss;
437 ps->vmem_data += pse->vmem_data;
438 ps->vmem_code += pse->vmem_code;
439 ps->stack_size += pse->stack_size;
441 ps->io_rchar += ((pse->io_rchar == -1)?0:pse->io_rchar);
442 ps->io_wchar += ((pse->io_wchar == -1)?0:pse->io_wchar);
443 ps->io_syscr += ((pse->io_syscr == -1)?0:pse->io_syscr);
444 ps->io_syscw += ((pse->io_syscw == -1)?0:pse->io_syscw);
446 ps->cswitch_vol += ((pse->cswitch_vol == -1)?0:pse->cswitch_vol);
447 ps->cswitch_invol += ((pse->cswitch_invol == -1)?0:pse->cswitch_invol);
449 want_init = (entry->vmem_minflt_counter == 0)
450 && (entry->vmem_majflt_counter == 0);
451 ps_update_counter (want_init,
452 &ps->vmem_minflt_counter,
453 &pse->vmem_minflt_counter,
454 entry->vmem_minflt_counter);
455 ps_update_counter (want_init,
456 &ps->vmem_majflt_counter,
457 &pse->vmem_majflt_counter,
458 entry->vmem_majflt_counter);
460 want_init = (entry->cpu_user_counter == 0)
461 && (entry->cpu_system_counter == 0);
462 ps_update_counter (want_init,
463 &ps->cpu_user_counter,
464 &pse->cpu_user_counter,
465 entry->cpu_user_counter);
466 ps_update_counter (want_init,
467 &ps->cpu_system_counter,
468 &pse->cpu_system_counter,
469 entry->cpu_system_counter);
473 /* remove old entries from instances of processes in list_head_g */
474 static void ps_list_reset (void)
476 procstat_entry_t *pse;
477 procstat_entry_t *pse_prev;
479 for (procstat_t *ps = list_head_g; ps != NULL; ps = ps->next)
492 ps->cswitch_vol = -1;
493 ps->cswitch_invol = -1;
501 DEBUG ("Removing this procstat entry cause it's too old: "
502 "id = %lu; name = %s;",
505 if (pse_prev == NULL)
507 ps->instances = pse->next;
513 pse_prev->next = pse->next;
515 pse = pse_prev->next;
524 } /* while (pse != NULL) */
525 } /* for (ps = list_head_g; ps != NULL; ps = ps->next) */
528 /* put all pre-defined 'Process' names from config to list_head_g tree */
529 static int ps_config (oconfig_item_t *ci)
532 const size_t max_procname_len = 15;
533 #elif KERNEL_SOLARIS || KERNEL_FREEBSD
534 const size_t max_procname_len = MAXCOMLEN -1;
537 for (int i = 0; i < ci->children_num; ++i) {
538 oconfig_item_t *c = ci->children + i;
540 if (strcasecmp (c->key, "Process") == 0)
542 if ((c->values_num != 1)
543 || (OCONFIG_TYPE_STRING != c->values[0].type)) {
544 ERROR ("processes plugin: `Process' expects exactly "
545 "one string argument (got %i).",
550 if (c->children_num != 0) {
551 WARNING ("processes plugin: the `Process' config option "
552 "does not expect any child elements -- ignoring "
553 "content (%i elements) of the <Process '%s'> block.",
554 c->children_num, c->values[0].value.string);
557 #if KERNEL_LINUX || KERNEL_SOLARIS || KERNEL_FREEBSD
558 if (strlen (c->values[0].value.string) > max_procname_len) {
559 WARNING ("processes plugin: this platform has a %zu character limit "
560 "to process names. The `Process \"%s\"' option will "
561 "not work as expected.",
562 max_procname_len, c->values[0].value.string);
566 ps_list_register (c->values[0].value.string, NULL);
568 else if (strcasecmp (c->key, "ProcessMatch") == 0)
570 if ((c->values_num != 2)
571 || (OCONFIG_TYPE_STRING != c->values[0].type)
572 || (OCONFIG_TYPE_STRING != c->values[1].type))
574 ERROR ("processes plugin: `ProcessMatch' needs exactly "
575 "two string arguments (got %i).",
580 if (c->children_num != 0) {
581 WARNING ("processes plugin: the `ProcessMatch' config option "
582 "does not expect any child elements -- ignoring "
583 "content (%i elements) of the <ProcessMatch '%s' '%s'> "
584 "block.", c->children_num, c->values[0].value.string,
585 c->values[1].value.string);
588 ps_list_register (c->values[0].value.string,
589 c->values[1].value.string);
591 else if (strcasecmp (c->key, "CollectContextSwitch") == 0)
593 cf_util_get_boolean (c, &report_ctx_switch);
597 ERROR ("processes plugin: The `%s' configuration option is not "
598 "understood and will be ignored.", c->key);
606 static int ps_init (void)
609 kern_return_t status;
611 port_host_self = mach_host_self ();
612 port_task_self = mach_task_self ();
614 if (pset_list != NULL)
616 vm_deallocate (port_task_self,
617 (vm_address_t) pset_list,
618 pset_list_len * sizeof (processor_set_t));
623 if ((status = host_processor_sets (port_host_self,
625 &pset_list_len)) != KERN_SUCCESS)
627 ERROR ("host_processor_sets failed: %s\n",
628 mach_error_string (status));
633 /* #endif HAVE_THREAD_INFO */
636 pagesize_g = sysconf(_SC_PAGESIZE);
637 DEBUG ("pagesize_g = %li; CONFIG_HZ = %i;",
638 pagesize_g, CONFIG_HZ);
639 /* #endif KERNEL_LINUX */
641 #elif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD)
642 pagesize = getpagesize();
643 /* #endif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD) */
645 #elif HAVE_PROCINFO_H
646 pagesize = getpagesize();
647 #endif /* HAVE_PROCINFO_H */
652 /* submit global state (e.g.: qty of zombies, running, etc..) */
653 static void ps_submit_state (const char *state, double value)
655 value_list_t vl = VALUE_LIST_INIT;
657 vl.values = &(value_t) { .gauge = value };
659 sstrncpy (vl.plugin, "processes", sizeof (vl.plugin));
660 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
661 sstrncpy (vl.type, "ps_state", sizeof (vl.type));
662 sstrncpy (vl.type_instance, state, sizeof (vl.type_instance));
664 plugin_dispatch_values (&vl);
667 /* submit info about specific process (e.g.: memory taken, cpu usage, etc..) */
668 static void ps_submit_proc_list (procstat_t *ps)
670 value_list_t vl = VALUE_LIST_INIT;
674 sstrncpy (vl.plugin, "processes", sizeof (vl.plugin));
675 sstrncpy (vl.plugin_instance, ps->name, sizeof (vl.plugin_instance));
677 sstrncpy (vl.type, "ps_vm", sizeof (vl.type));
678 vl.values[0].gauge = ps->vmem_size;
680 plugin_dispatch_values (&vl);
682 sstrncpy (vl.type, "ps_rss", sizeof (vl.type));
683 vl.values[0].gauge = ps->vmem_rss;
685 plugin_dispatch_values (&vl);
687 sstrncpy (vl.type, "ps_data", sizeof (vl.type));
688 vl.values[0].gauge = ps->vmem_data;
690 plugin_dispatch_values (&vl);
692 sstrncpy (vl.type, "ps_code", sizeof (vl.type));
693 vl.values[0].gauge = ps->vmem_code;
695 plugin_dispatch_values (&vl);
697 sstrncpy (vl.type, "ps_stacksize", sizeof (vl.type));
698 vl.values[0].gauge = ps->stack_size;
700 plugin_dispatch_values (&vl);
702 sstrncpy (vl.type, "ps_cputime", sizeof (vl.type));
703 vl.values[0].derive = ps->cpu_user_counter;
704 vl.values[1].derive = ps->cpu_system_counter;
706 plugin_dispatch_values (&vl);
708 sstrncpy (vl.type, "ps_count", sizeof (vl.type));
709 vl.values[0].gauge = ps->num_proc;
710 vl.values[1].gauge = ps->num_lwp;
712 plugin_dispatch_values (&vl);
714 sstrncpy (vl.type, "ps_pagefaults", sizeof (vl.type));
715 vl.values[0].derive = ps->vmem_minflt_counter;
716 vl.values[1].derive = ps->vmem_majflt_counter;
718 plugin_dispatch_values (&vl);
720 if ( (ps->io_rchar != -1) && (ps->io_wchar != -1) )
722 sstrncpy (vl.type, "ps_disk_octets", sizeof (vl.type));
723 vl.values[0].derive = ps->io_rchar;
724 vl.values[1].derive = ps->io_wchar;
726 plugin_dispatch_values (&vl);
729 if ( (ps->io_syscr != -1) && (ps->io_syscw != -1) )
731 sstrncpy (vl.type, "ps_disk_ops", sizeof (vl.type));
732 vl.values[0].derive = ps->io_syscr;
733 vl.values[1].derive = ps->io_syscw;
735 plugin_dispatch_values (&vl);
738 if ( report_ctx_switch )
740 sstrncpy (vl.type, "contextswitch", sizeof (vl.type));
741 sstrncpy (vl.type_instance, "voluntary", sizeof (vl.type_instance));
742 vl.values[0].derive = ps->cswitch_vol;
744 plugin_dispatch_values (&vl);
746 sstrncpy (vl.type, "contextswitch", sizeof (vl.type));
747 sstrncpy (vl.type_instance, "involuntary", sizeof (vl.type_instance));
748 vl.values[0].derive = ps->cswitch_invol;
750 plugin_dispatch_values (&vl);
753 DEBUG ("name = %s; num_proc = %lu; num_lwp = %lu; "
754 "vmem_size = %lu; vmem_rss = %lu; vmem_data = %lu; "
756 "vmem_minflt_counter = %"PRIi64"; vmem_majflt_counter = %"PRIi64"; "
757 "cpu_user_counter = %"PRIi64"; cpu_system_counter = %"PRIi64"; "
758 "io_rchar = %"PRIi64"; io_wchar = %"PRIi64"; "
759 "io_syscr = %"PRIi64"; io_syscw = %"PRIi64"; "
760 "cswitch_vol = %"PRIi64"; cswitch_invol = %"PRIi64";",
761 ps->name, ps->num_proc, ps->num_lwp,
762 ps->vmem_size, ps->vmem_rss,
763 ps->vmem_data, ps->vmem_code,
764 ps->vmem_minflt_counter, ps->vmem_majflt_counter,
765 ps->cpu_user_counter, ps->cpu_system_counter,
766 ps->io_rchar, ps->io_wchar, ps->io_syscr, ps->io_syscw,
767 ps->cswitch_vol, ps->cswitch_invol);
768 } /* void ps_submit_proc_list */
770 #if KERNEL_LINUX || KERNEL_SOLARIS
771 static void ps_submit_fork_rate (derive_t value)
773 value_list_t vl = VALUE_LIST_INIT;
775 vl.values = &(value_t) { .derive = value };
777 sstrncpy(vl.plugin, "processes", sizeof (vl.plugin));
778 sstrncpy(vl.plugin_instance, "", sizeof (vl.plugin_instance));
779 sstrncpy(vl.type, "fork_rate", sizeof (vl.type));
780 sstrncpy(vl.type_instance, "", sizeof (vl.type_instance));
782 plugin_dispatch_values(&vl);
784 #endif /* KERNEL_LINUX || KERNEL_SOLARIS*/
786 /* ------- additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
788 static procstat_t *ps_read_tasks_status (long pid, procstat_t *ps)
795 derive_t cswitch_vol = 0;
796 derive_t cswitch_invol = 0;
801 ssnprintf (dirname, sizeof (dirname), "/proc/%li/task", pid);
803 if ((dh = opendir (dirname)) == NULL)
805 DEBUG ("Failed to open directory `%s'", dirname);
809 while ((ent = readdir (dh)) != NULL)
813 if (!isdigit ((int) ent->d_name[0]))
818 ssnprintf (filename, sizeof (filename), "/proc/%li/task/%s/status", pid, tpid);
819 if ((fh = fopen (filename, "r")) == NULL)
821 DEBUG ("Failed to open file `%s'", filename);
825 while (fgets (buffer, sizeof(buffer), fh) != NULL)
830 if (strncmp (buffer, "voluntary_ctxt_switches", 23) != 0
831 && strncmp (buffer, "nonvoluntary_ctxt_switches", 26) != 0)
834 numfields = strsplit (buffer, fields,
835 STATIC_ARRAY_SIZE (fields));
842 tmp = (derive_t) strtoll (fields[1], &endptr, /* base = */ 10);
843 if ((errno == 0) && (endptr != fields[1]))
845 if (strncmp (buffer, "voluntary_ctxt_switches", 23) == 0)
849 else if (strncmp (buffer, "nonvoluntary_ctxt_switches", 26) == 0)
851 cswitch_invol += tmp;
854 } /* while (fgets) */
859 WARNING ("processes: fclose: %s",
860 sstrerror (errno, errbuf, sizeof (errbuf)));
865 ps->cswitch_vol = cswitch_vol;
866 ps->cswitch_invol = cswitch_invol;
869 } /* int *ps_read_tasks_status */
871 /* Read data from /proc/pid/status */
872 static procstat_t *ps_read_status (long pid, procstat_t *ps)
877 unsigned long lib = 0;
878 unsigned long exe = 0;
879 unsigned long data = 0;
880 unsigned long threads = 0;
884 ssnprintf (filename, sizeof (filename), "/proc/%li/status", pid);
885 if ((fh = fopen (filename, "r")) == NULL)
888 while (fgets (buffer, sizeof(buffer), fh) != NULL)
893 if (strncmp (buffer, "Vm", 2) != 0
894 && strncmp (buffer, "Threads", 7) != 0)
897 numfields = strsplit (buffer, fields,
898 STATIC_ARRAY_SIZE (fields));
905 tmp = strtoul (fields[1], &endptr, /* base = */ 10);
906 if ((errno == 0) && (endptr != fields[1]))
908 if (strncmp (buffer, "VmData", 6) == 0)
912 else if (strncmp (buffer, "VmLib", 5) == 0)
916 else if (strncmp(buffer, "VmExe", 5) == 0)
920 else if (strncmp(buffer, "Threads", 7) == 0)
925 } /* while (fgets) */
930 WARNING ("processes: fclose: %s",
931 sstrerror (errno, errbuf, sizeof (errbuf)));
934 ps->vmem_data = data * 1024;
935 ps->vmem_code = (exe + lib) * 1024;
937 ps->num_lwp = threads;
940 } /* procstat_t *ps_read_vmem */
942 static procstat_t *ps_read_io (long pid, procstat_t *ps)
951 ssnprintf (filename, sizeof (filename), "/proc/%li/io", pid);
952 if ((fh = fopen (filename, "r")) == NULL)
955 while (fgets (buffer, sizeof (buffer), fh) != NULL)
957 derive_t *val = NULL;
961 if (strncasecmp (buffer, "rchar:", 6) == 0)
962 val = &(ps->io_rchar);
963 else if (strncasecmp (buffer, "wchar:", 6) == 0)
964 val = &(ps->io_wchar);
965 else if (strncasecmp (buffer, "syscr:", 6) == 0)
966 val = &(ps->io_syscr);
967 else if (strncasecmp (buffer, "syscw:", 6) == 0)
968 val = &(ps->io_syscw);
972 numfields = strsplit (buffer, fields,
973 STATIC_ARRAY_SIZE (fields));
980 tmp = strtoll (fields[1], &endptr, /* base = */ 10);
981 if ((errno != 0) || (endptr == fields[1]))
984 *val = (derive_t) tmp;
985 } /* while (fgets) */
990 WARNING ("processes: fclose: %s",
991 sstrerror (errno, errbuf, sizeof (errbuf)));
995 } /* procstat_t *ps_read_io */
997 static int ps_read_process (long pid, procstat_t *ps, char *state)
1008 size_t name_start_pos;
1009 size_t name_end_pos;
1012 derive_t cpu_user_counter;
1013 derive_t cpu_system_counter;
1014 long long unsigned vmem_size;
1015 long long unsigned vmem_rss;
1016 long long unsigned stack_size;
1020 memset (ps, 0, sizeof (procstat_t));
1022 ssnprintf (filename, sizeof (filename), "/proc/%li/stat", pid);
1024 status = read_file_contents (filename, buffer, sizeof(buffer) - 1);
1027 buffer_len = (size_t) status;
1028 buffer[buffer_len] = 0;
1030 /* The name of the process is enclosed in parens. Since the name can
1031 * contain parens itself, spaces, numbers and pretty much everything
1032 * else, use these to determine the process name. We don't use
1033 * strchr(3) and strrchr(3) to avoid pointer arithmetic which would
1034 * otherwise be required to determine name_len. */
1036 while (name_start_pos < buffer_len && buffer[name_start_pos] != '(')
1039 name_end_pos = buffer_len;
1040 while (name_end_pos > 0 && buffer[name_end_pos] != ')')
1043 /* Either '(' or ')' is not found or they are in the wrong order.
1044 * Anyway, something weird that shouldn't happen ever. */
1045 if (name_start_pos >= name_end_pos)
1047 ERROR ("processes plugin: name_start_pos = %zu >= name_end_pos = %zu",
1048 name_start_pos, name_end_pos);
1052 name_len = (name_end_pos - name_start_pos) - 1;
1053 if (name_len >= sizeof (ps->name))
1054 name_len = sizeof (ps->name) - 1;
1056 sstrncpy (ps->name, &buffer[name_start_pos + 1], name_len + 1);
1058 if ((buffer_len - name_end_pos) < 2)
1060 buffer_ptr = &buffer[name_end_pos + 2];
1062 fields_len = strsplit (buffer_ptr, fields, STATIC_ARRAY_SIZE (fields));
1063 if (fields_len < 22)
1065 DEBUG ("processes plugin: ps_read_process (pid = %li):"
1066 " `%s' has only %i fields..",
1067 pid, filename, fields_len);
1071 *state = fields[0][0];
1080 ps->num_lwp = strtoul (fields[17], /* endptr = */ NULL, /* base = */ 10);
1081 if ((ps_read_status(pid, ps)) == NULL)
1086 DEBUG("ps_read_process: did not get vmem data for pid %li", pid);
1088 if (ps->num_lwp == 0)
1093 /* Leave the rest at zero if this is only a zombi */
1094 if (ps->num_proc == 0)
1096 DEBUG ("processes plugin: This is only a zombie: pid = %li; "
1097 "name = %s;", pid, ps->name);
1101 cpu_user_counter = atoll (fields[11]);
1102 cpu_system_counter = atoll (fields[12]);
1103 vmem_size = atoll (fields[20]);
1104 vmem_rss = atoll (fields[21]);
1105 ps->vmem_minflt_counter = atol (fields[7]);
1106 ps->vmem_majflt_counter = atol (fields[9]);
1109 unsigned long long stack_start = atoll (fields[25]);
1110 unsigned long long stack_ptr = atoll (fields[26]);
1112 stack_size = (stack_start > stack_ptr)
1113 ? stack_start - stack_ptr
1114 : stack_ptr - stack_start;
1117 /* Convert jiffies to useconds */
1118 cpu_user_counter = cpu_user_counter * 1000000 / CONFIG_HZ;
1119 cpu_system_counter = cpu_system_counter * 1000000 / CONFIG_HZ;
1120 vmem_rss = vmem_rss * pagesize_g;
1122 ps->cpu_user_counter = cpu_user_counter;
1123 ps->cpu_system_counter = cpu_system_counter;
1124 ps->vmem_size = (unsigned long) vmem_size;
1125 ps->vmem_rss = (unsigned long) vmem_rss;
1126 ps->stack_size = (unsigned long) stack_size;
1128 if ( (ps_read_io (pid, ps)) == NULL)
1136 DEBUG("ps_read_process: not get io data for pid %li", pid);
1139 if ( report_ctx_switch )
1141 if ( (ps_read_tasks_status(pid, ps)) == NULL)
1143 ps->cswitch_vol = -1;
1144 ps->cswitch_invol = -1;
1146 DEBUG("ps_read_tasks_status: not get context "
1147 "switch data for pid %li", pid);
1153 } /* int ps_read_process (...) */
1155 static char *ps_get_cmdline (long pid, char *name, char *buf, size_t buf_len)
1160 char file[PATH_MAX];
1165 if ((pid < 1) || (NULL == buf) || (buf_len < 2))
1168 ssnprintf (file, sizeof (file), "/proc/%li/cmdline", pid);
1171 fd = open (file, O_RDONLY);
1174 /* ENOENT means the process exited while we were handling it.
1175 * Don't complain about this, it only fills the logs. */
1176 if (errno != ENOENT)
1177 WARNING ("processes plugin: Failed to open `%s': %s.", file,
1178 sstrerror (errno, errbuf, sizeof (errbuf)));
1190 status = read (fd, (void *)buf_ptr, len);
1195 if ((EAGAIN == errno) || (EINTR == errno))
1198 WARNING ("processes plugin: Failed to read from `%s': %s.", file,
1199 sstrerror (errno, errbuf, sizeof (errbuf)));
1219 /* cmdline not available; e.g. kernel thread, zombie */
1223 ssnprintf (buf, buf_len, "[%s]", name);
1227 assert (n <= buf_len);
1234 /* remove trailing whitespace */
1235 while ((n > 0) && (isspace (buf[n]) || ('\0' == buf[n]))) {
1240 /* arguments are separated by '\0' in /proc/<pid>/cmdline */
1247 } /* char *ps_get_cmdline (...) */
1249 static int read_fork_rate (void)
1254 _Bool value_valid = 0;
1256 proc_stat = fopen ("/proc/stat", "r");
1257 if (proc_stat == NULL)
1260 ERROR ("processes plugin: fopen (/proc/stat) failed: %s",
1261 sstrerror (errno, errbuf, sizeof (errbuf)));
1265 while (fgets (buffer, sizeof (buffer), proc_stat) != NULL)
1271 fields_num = strsplit (buffer, fields,
1272 STATIC_ARRAY_SIZE (fields));
1273 if (fields_num != 2)
1276 if (strcmp ("processes", fields[0]) != 0)
1279 status = parse_value (fields[1], &value, DS_TYPE_DERIVE);
1290 ps_submit_fork_rate (value.derive);
1293 #endif /*KERNEL_LINUX */
1296 static char *ps_get_cmdline (long pid, char *name __attribute__((unused)), /* {{{ */
1297 char *buffer, size_t buffer_size)
1299 char path[PATH_MAX];
1303 snprintf(path, sizeof (path), "/proc/%li/psinfo", pid);
1305 status = read_file_contents (path, (void *) &info, sizeof (info));
1306 if ((status < 0) || (((size_t) status) != sizeof (info)))
1308 ERROR ("processes plugin: Unexpected return value "
1309 "while reading \"%s\": "
1310 "Returned %zd but expected %zu.",
1311 path, status, buffer_size);
1315 info.pr_psargs[sizeof (info.pr_psargs) - 1] = 0;
1316 sstrncpy (buffer, info.pr_psargs, buffer_size);
1319 } /* }}} int ps_get_cmdline */
1322 * Reads process information on the Solaris OS. The information comes mainly from
1323 * /proc/PID/status, /proc/PID/psinfo and /proc/PID/usage
1324 * The values for input and ouput chars are calculated "by hand"
1325 * Added a few "solaris" specific process states as well
1327 static int ps_read_process(long pid, procstat_t *ps, char *state)
1330 char f_psinfo[64], f_usage[64];
1333 pstatus_t *myStatus;
1337 snprintf(filename, sizeof (filename), "/proc/%li/status", pid);
1338 snprintf(f_psinfo, sizeof (f_psinfo), "/proc/%li/psinfo", pid);
1339 snprintf(f_usage, sizeof (f_usage), "/proc/%li/usage", pid);
1342 buffer = calloc(1, sizeof (pstatus_t));
1343 read_file_contents(filename, buffer, sizeof (pstatus_t));
1344 myStatus = (pstatus_t *) buffer;
1346 buffer = calloc(1, sizeof (psinfo_t));
1347 read_file_contents(f_psinfo, buffer, sizeof (psinfo_t));
1348 myInfo = (psinfo_t *) buffer;
1350 buffer = calloc(1, sizeof (prusage_t));
1351 read_file_contents(f_usage, buffer, sizeof (prusage_t));
1352 myUsage = (prusage_t *) buffer;
1354 sstrncpy(ps->name, myInfo->pr_fname, sizeof (myInfo->pr_fname));
1355 ps->num_lwp = myStatus->pr_nlwp;
1356 if (myInfo->pr_wstat != 0) {
1359 *state = (char) 'Z';
1367 ps->num_lwp = myInfo->pr_nlwp;
1371 * Convert system time and user time from nanoseconds to microseconds
1372 * for compatibility with the linux module
1374 ps->cpu_system_counter = myStatus -> pr_stime.tv_nsec / 1000;
1375 ps->cpu_user_counter = myStatus -> pr_utime.tv_nsec / 1000;
1378 * Convert rssize from KB to bytes to be consistent w/ the linux module
1380 ps->vmem_rss = myInfo->pr_rssize * 1024;
1381 ps->vmem_size = myInfo->pr_size * 1024;
1382 ps->vmem_minflt_counter = myUsage->pr_minf;
1383 ps->vmem_majflt_counter = myUsage->pr_majf;
1386 * TODO: Data and code segment calculations for Solaris
1391 ps->stack_size = myStatus->pr_stksize;
1394 * Calculating input/ouput chars
1395 * Formula used is total chars / total blocks => chars/block
1396 * then convert input/output blocks to chars
1398 ulong_t tot_chars = myUsage->pr_ioch;
1399 ulong_t tot_blocks = myUsage->pr_inblk + myUsage->pr_oublk;
1400 ulong_t chars_per_block = 1;
1401 if (tot_blocks != 0)
1402 chars_per_block = tot_chars / tot_blocks;
1403 ps->io_rchar = myUsage->pr_inblk * chars_per_block;
1404 ps->io_wchar = myUsage->pr_oublk * chars_per_block;
1405 ps->io_syscr = myUsage->pr_sysc;
1406 ps->io_syscw = myUsage->pr_sysc;
1409 * TODO: context switch counters for Solaris
1411 ps->cswitch_vol = -1;
1412 ps->cswitch_invol = -1;
1416 * TODO: Find way of setting BLOCKED and PAGING status
1419 *state = (char) 'R';
1420 if (myStatus->pr_flags & PR_ASLEEP)
1421 *state = (char) 'S';
1422 else if (myStatus->pr_flags & PR_STOPPED)
1423 *state = (char) 'T';
1424 else if (myStatus->pr_flags & PR_DETACH)
1425 *state = (char) 'E';
1426 else if (myStatus->pr_flags & PR_DAEMON)
1427 *state = (char) 'A';
1428 else if (myStatus->pr_flags & PR_ISSYS)
1429 *state = (char) 'Y';
1430 else if (myStatus->pr_flags & PR_ORPHAN)
1431 *state = (char) 'O';
1441 * Reads the number of threads created since the last reboot. On Solaris these
1442 * are retrieved from kstat (module cpu, name sys, class misc, stat nthreads).
1443 * The result is the sum for all the threads created on each cpu
1445 static int read_fork_rate (void)
1447 extern kstat_ctl_t *kc;
1448 derive_t result = 0;
1453 for (kstat_t *ksp_chain = kc->kc_chain;
1455 ksp_chain = ksp_chain->ks_next)
1457 if ((strcmp (ksp_chain->ks_module, "cpu") == 0)
1458 && (strcmp (ksp_chain->ks_name, "sys") == 0)
1459 && (strcmp (ksp_chain->ks_class, "misc") == 0))
1463 kstat_read (kc, ksp_chain, NULL);
1465 tmp = get_kstat_value(ksp_chain, "nthreads");
1471 ps_submit_fork_rate (result);
1474 #endif /* KERNEL_SOLARIS */
1476 #if HAVE_THREAD_INFO
1477 static int mach_get_task_name (task_t t, int *pid, char *name, size_t name_max_len)
1481 struct kinfo_proc kp;
1486 mib[2] = KERN_PROC_PID;
1488 if (pid_for_task (t, pid) != KERN_SUCCESS)
1492 kp_size = sizeof (kp);
1493 if (sysctl (mib, 4, &kp, &kp_size, NULL, 0) != 0)
1496 if (name_max_len > (MAXCOMLEN + 1))
1497 name_max_len = MAXCOMLEN + 1;
1499 strncpy (name, kp.kp_proc.p_comm, name_max_len - 1);
1500 name[name_max_len - 1] = '\0';
1502 DEBUG ("pid = %i; name = %s;", *pid, name);
1504 /* We don't do the special handling for `p_comm == "LaunchCFMApp"' as
1505 * `top' does it, because it is a lot of work and only used when
1506 * debugging. -octo */
1510 #endif /* HAVE_THREAD_INFO */
1511 /* ------- end of additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
1513 /* do actual readings from kernel */
1514 static int ps_read (void)
1516 #if HAVE_THREAD_INFO
1517 kern_return_t status;
1519 processor_set_t port_pset_priv;
1521 task_array_t task_list;
1522 mach_msg_type_number_t task_list_len;
1525 char task_name[MAXCOMLEN + 1];
1527 thread_act_array_t thread_list;
1528 mach_msg_type_number_t thread_list_len;
1529 thread_basic_info_data_t thread_data;
1530 mach_msg_type_number_t thread_data_len;
1539 procstat_entry_t pse;
1544 * The Mach-concept is a little different from the traditional UNIX
1545 * concept: All the work is done in threads. Threads are contained in
1546 * `tasks'. Therefore, `task status' doesn't make much sense, since
1547 * it's actually a `thread status'.
1548 * Tasks are assigned to sets of processors, so that's where you go to
1551 for (mach_msg_type_number_t pset = 0; pset < pset_list_len; pset++)
1553 if ((status = host_processor_set_priv (port_host_self,
1555 &port_pset_priv)) != KERN_SUCCESS)
1557 ERROR ("host_processor_set_priv failed: %s\n",
1558 mach_error_string (status));
1562 if ((status = processor_set_tasks (port_pset_priv,
1564 &task_list_len)) != KERN_SUCCESS)
1566 ERROR ("processor_set_tasks failed: %s\n",
1567 mach_error_string (status));
1568 mach_port_deallocate (port_task_self, port_pset_priv);
1572 for (mach_msg_type_number_t task = 0; task < task_list_len; task++)
1575 if (mach_get_task_name (task_list[task],
1577 task_name, PROCSTAT_NAME_LEN) == 0)
1579 /* search for at least one match */
1580 for (ps = list_head_g; ps != NULL; ps = ps->next)
1581 /* FIXME: cmdline should be here instead of NULL */
1582 if (ps_list_match (task_name, NULL, ps) == 1)
1586 /* Collect more detailed statistics for this process */
1589 task_basic_info_data_t task_basic_info;
1590 mach_msg_type_number_t task_basic_info_len;
1591 task_events_info_data_t task_events_info;
1592 mach_msg_type_number_t task_events_info_len;
1593 task_absolutetime_info_data_t task_absolutetime_info;
1594 mach_msg_type_number_t task_absolutetime_info_len;
1596 memset (&pse, '\0', sizeof (pse));
1599 task_basic_info_len = TASK_BASIC_INFO_COUNT;
1600 status = task_info (task_list[task],
1602 (task_info_t) &task_basic_info,
1603 &task_basic_info_len);
1604 if (status != KERN_SUCCESS)
1606 ERROR ("task_info failed: %s",
1607 mach_error_string (status));
1608 continue; /* with next thread_list */
1611 task_events_info_len = TASK_EVENTS_INFO_COUNT;
1612 status = task_info (task_list[task],
1614 (task_info_t) &task_events_info,
1615 &task_events_info_len);
1616 if (status != KERN_SUCCESS)
1618 ERROR ("task_info failed: %s",
1619 mach_error_string (status));
1620 continue; /* with next thread_list */
1623 task_absolutetime_info_len = TASK_ABSOLUTETIME_INFO_COUNT;
1624 status = task_info (task_list[task],
1625 TASK_ABSOLUTETIME_INFO,
1626 (task_info_t) &task_absolutetime_info,
1627 &task_absolutetime_info_len);
1628 if (status != KERN_SUCCESS)
1630 ERROR ("task_info failed: %s",
1631 mach_error_string (status));
1632 continue; /* with next thread_list */
1636 pse.vmem_size = task_basic_info.virtual_size;
1637 pse.vmem_rss = task_basic_info.resident_size;
1638 /* Does not seem to be easily exposed */
1642 pse.vmem_minflt_counter = task_events_info.cow_faults;
1643 pse.vmem_majflt_counter = task_events_info.faults;
1645 pse.cpu_user_counter = task_absolutetime_info.total_user;
1646 pse.cpu_system_counter = task_absolutetime_info.total_system;
1648 /* context switch counters not implemented */
1649 pse.cswitch_vol = -1;
1650 pse.cswitch_invol = -1;
1653 status = task_threads (task_list[task], &thread_list,
1655 if (status != KERN_SUCCESS)
1657 /* Apple's `top' treats this case a zombie. It
1658 * makes sense to some extend: A `zombie'
1659 * thread is nonsense, since the task/process
1662 DEBUG ("task_threads failed: %s",
1663 mach_error_string (status));
1664 if (task_list[task] != port_task_self)
1665 mach_port_deallocate (port_task_self,
1667 continue; /* with next task_list */
1670 for (mach_msg_type_number_t thread = 0; thread < thread_list_len; thread++)
1672 thread_data_len = THREAD_BASIC_INFO_COUNT;
1673 status = thread_info (thread_list[thread],
1675 (thread_info_t) &thread_data,
1677 if (status != KERN_SUCCESS)
1679 ERROR ("thread_info failed: %s",
1680 mach_error_string (status));
1681 if (task_list[task] != port_task_self)
1682 mach_port_deallocate (port_task_self,
1683 thread_list[thread]);
1684 continue; /* with next thread_list */
1690 switch (thread_data.run_state)
1692 case TH_STATE_RUNNING:
1695 case TH_STATE_STOPPED:
1696 /* What exactly is `halted'? */
1697 case TH_STATE_HALTED:
1700 case TH_STATE_WAITING:
1703 case TH_STATE_UNINTERRUPTIBLE:
1706 /* There is no `zombie' case here,
1707 * since there are no zombie-threads.
1708 * There's only zombie tasks, which are
1711 WARNING ("Unknown thread status: %i",
1712 thread_data.run_state);
1714 } /* switch (thread_data.run_state) */
1716 if (task_list[task] != port_task_self)
1718 status = mach_port_deallocate (port_task_self,
1719 thread_list[thread]);
1720 if (status != KERN_SUCCESS)
1721 ERROR ("mach_port_deallocate failed: %s",
1722 mach_error_string (status));
1724 } /* for (thread_list) */
1726 if ((status = vm_deallocate (port_task_self,
1727 (vm_address_t) thread_list,
1728 thread_list_len * sizeof (thread_act_t)))
1731 ERROR ("vm_deallocate failed: %s",
1732 mach_error_string (status));
1735 thread_list_len = 0;
1737 /* Only deallocate the task port, if it isn't our own.
1738 * Don't know what would happen in that case, but this
1739 * is what Apple's top does.. ;) */
1740 if (task_list[task] != port_task_self)
1742 status = mach_port_deallocate (port_task_self,
1744 if (status != KERN_SUCCESS)
1745 ERROR ("mach_port_deallocate failed: %s",
1746 mach_error_string (status));
1750 /* FIXME: cmdline should be here instead of NULL */
1751 ps_list_add (task_name, NULL, &pse);
1752 } /* for (task_list) */
1754 if ((status = vm_deallocate (port_task_self,
1755 (vm_address_t) task_list,
1756 task_list_len * sizeof (task_t))) != KERN_SUCCESS)
1758 ERROR ("vm_deallocate failed: %s",
1759 mach_error_string (status));
1764 if ((status = mach_port_deallocate (port_task_self, port_pset_priv))
1767 ERROR ("mach_port_deallocate failed: %s",
1768 mach_error_string (status));
1770 } /* for (pset_list) */
1772 ps_submit_state ("running", running);
1773 ps_submit_state ("sleeping", sleeping);
1774 ps_submit_state ("zombies", zombies);
1775 ps_submit_state ("stopped", stopped);
1776 ps_submit_state ("blocked", blocked);
1778 for (ps = list_head_g; ps != NULL; ps = ps->next)
1779 ps_submit_proc_list (ps);
1780 /* #endif HAVE_THREAD_INFO */
1794 char cmdline[CMDLINE_BUFFER_SIZE];
1798 procstat_entry_t pse;
1801 running = sleeping = zombies = stopped = paging = blocked = 0;
1804 if ((proc = opendir ("/proc")) == NULL)
1807 ERROR ("Cannot open `/proc': %s",
1808 sstrerror (errno, errbuf, sizeof (errbuf)));
1812 while ((ent = readdir (proc)) != NULL)
1814 if (!isdigit (ent->d_name[0]))
1817 if ((pid = atol (ent->d_name)) < 1)
1820 status = ps_read_process (pid, &ps, &state);
1823 DEBUG ("ps_read_process failed: %i", status);
1827 memset (&pse, 0, sizeof (pse));
1831 pse.num_proc = ps.num_proc;
1832 pse.num_lwp = ps.num_lwp;
1833 pse.vmem_size = ps.vmem_size;
1834 pse.vmem_rss = ps.vmem_rss;
1835 pse.vmem_data = ps.vmem_data;
1836 pse.vmem_code = ps.vmem_code;
1837 pse.stack_size = ps.stack_size;
1839 pse.vmem_minflt_counter = ps.vmem_minflt_counter;
1840 pse.vmem_majflt_counter = ps.vmem_majflt_counter;
1842 pse.cpu_user_counter = ps.cpu_user_counter;
1843 pse.cpu_system_counter = ps.cpu_system_counter;
1845 pse.io_rchar = ps.io_rchar;
1846 pse.io_wchar = ps.io_wchar;
1847 pse.io_syscr = ps.io_syscr;
1848 pse.io_syscw = ps.io_syscw;
1850 pse.cswitch_vol = ps.cswitch_vol;
1851 pse.cswitch_invol = ps.cswitch_invol;
1855 case 'R': running++; break;
1856 case 'S': sleeping++; break;
1857 case 'D': blocked++; break;
1858 case 'Z': zombies++; break;
1859 case 'T': stopped++; break;
1860 case 'W': paging++; break;
1863 ps_list_add (ps.name,
1864 ps_get_cmdline (pid, ps.name, cmdline, sizeof (cmdline)),
1870 ps_submit_state ("running", running);
1871 ps_submit_state ("sleeping", sleeping);
1872 ps_submit_state ("zombies", zombies);
1873 ps_submit_state ("stopped", stopped);
1874 ps_submit_state ("paging", paging);
1875 ps_submit_state ("blocked", blocked);
1877 for (procstat_t *ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1878 ps_submit_proc_list (ps_ptr);
1881 /* #endif KERNEL_LINUX */
1883 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD
1893 char errbuf[_POSIX2_LINE_MAX];
1894 struct kinfo_proc *procs; /* array of processes */
1895 struct kinfo_proc *proc_ptr = NULL;
1896 int count; /* returns number of processes */
1898 procstat_entry_t pse;
1902 /* Open the kvm interface, get a descriptor */
1903 kd = kvm_openfiles (NULL, "/dev/null", NULL, 0, errbuf);
1906 ERROR ("processes plugin: Cannot open kvm interface: %s",
1911 /* Get the list of processes. */
1912 procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, &count);
1915 ERROR ("processes plugin: Cannot get kvm processes list: %s",
1921 /* Iterate through the processes in kinfo_proc */
1922 for (int i = 0; i < count; i++)
1924 /* Create only one process list entry per _process_, i.e.
1925 * filter out threads (duplicate PID entries). */
1926 if ((proc_ptr == NULL) || (proc_ptr->ki_pid != procs[i].ki_pid))
1928 char cmdline[CMDLINE_BUFFER_SIZE] = "";
1929 _Bool have_cmdline = 0;
1931 proc_ptr = &(procs[i]);
1932 /* Don't probe system processes and processes without arguments */
1933 if (((procs[i].ki_flag & P_SYSTEM) == 0)
1934 && (procs[i].ki_args != NULL))
1940 /* retrieve the arguments */
1941 argv = kvm_getargv (kd, proc_ptr, /* nchr = */ 0);
1943 if ((argv != NULL) && (argv[0] != NULL))
1945 while (argv[argc] != NULL)
1948 status = strjoin (cmdline, sizeof (cmdline), argv, argc, " ");
1950 WARNING ("processes plugin: Command line did not fit into buffer.");
1954 } /* if (process has argument list) */
1956 pse.id = procs[i].ki_pid;
1960 pse.num_lwp = procs[i].ki_numthreads;
1962 pse.vmem_size = procs[i].ki_size;
1963 pse.vmem_rss = procs[i].ki_rssize * pagesize;
1964 pse.vmem_data = procs[i].ki_dsize * pagesize;
1965 pse.vmem_code = procs[i].ki_tsize * pagesize;
1966 pse.stack_size = procs[i].ki_ssize * pagesize;
1967 pse.vmem_minflt_counter = procs[i].ki_rusage.ru_minflt;
1968 pse.vmem_majflt_counter = procs[i].ki_rusage.ru_majflt;
1970 pse.cpu_user_counter = 0;
1971 pse.cpu_system_counter = 0;
1973 * The u-area might be swapped out, and we can't get
1974 * at it because we have a crashdump and no swap.
1975 * If it's here fill in these fields, otherwise, just
1978 if (procs[i].ki_flag & P_INMEM)
1980 pse.cpu_user_counter = procs[i].ki_rusage.ru_utime.tv_usec
1981 + (1000000lu * procs[i].ki_rusage.ru_utime.tv_sec);
1982 pse.cpu_system_counter = procs[i].ki_rusage.ru_stime.tv_usec
1983 + (1000000lu * procs[i].ki_rusage.ru_stime.tv_sec);
1992 /* context switch counters not implemented */
1993 pse.cswitch_vol = -1;
1994 pse.cswitch_invol = -1;
1996 ps_list_add (procs[i].ki_comm, have_cmdline ? cmdline : NULL, &pse);
1998 switch (procs[i].ki_stat)
2000 case SSTOP: stopped++; break;
2001 case SSLEEP: sleeping++; break;
2002 case SRUN: running++; break;
2003 case SIDL: idle++; break;
2004 case SWAIT: wait++; break;
2005 case SLOCK: blocked++; break;
2006 case SZOMB: zombies++; break;
2008 } /* if ((proc_ptr == NULL) || (proc_ptr->ki_pid != procs[i].ki_pid)) */
2013 ps_submit_state ("running", running);
2014 ps_submit_state ("sleeping", sleeping);
2015 ps_submit_state ("zombies", zombies);
2016 ps_submit_state ("stopped", stopped);
2017 ps_submit_state ("blocked", blocked);
2018 ps_submit_state ("idle", idle);
2019 ps_submit_state ("wait", wait);
2021 for (procstat_t *ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
2022 ps_submit_proc_list (ps_ptr);
2023 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
2025 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_OPENBSD
2036 struct kinfo_proc *procs; /* array of processes */
2037 struct kinfo_proc *proc_ptr = NULL;
2038 int count; /* returns number of processes */
2040 procstat_entry_t pse;
2044 /* Open the kvm interface, get a descriptor */
2045 kd = kvm_open (NULL, NULL, NULL, 0, errbuf);
2048 ERROR ("processes plugin: Cannot open kvm interface: %s",
2053 /* Get the list of processes. */
2054 procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc), &count);
2057 ERROR ("processes plugin: Cannot get kvm processes list: %s",
2063 /* Iterate through the processes in kinfo_proc */
2064 for (int i = 0; i < count; i++)
2066 /* Create only one process list entry per _process_, i.e.
2067 * filter out threads (duplicate PID entries). */
2068 if ((proc_ptr == NULL) || (proc_ptr->p_pid != procs[i].p_pid))
2070 char cmdline[CMDLINE_BUFFER_SIZE] = "";
2071 _Bool have_cmdline = 0;
2073 proc_ptr = &(procs[i]);
2074 /* Don't probe zombie processes */
2075 if (!P_ZOMBIE(proc_ptr))
2081 /* retrieve the arguments */
2082 argv = kvm_getargv (kd, proc_ptr, /* nchr = */ 0);
2084 if ((argv != NULL) && (argv[0] != NULL))
2086 while (argv[argc] != NULL)
2089 status = strjoin (cmdline, sizeof (cmdline), argv, argc, " ");
2091 WARNING ("processes plugin: Command line did not fit into buffer.");
2095 } /* if (process has argument list) */
2097 memset (&pse, 0, sizeof (pse));
2098 pse.id = procs[i].p_pid;
2102 pse.num_lwp = 1; /* XXX: accumulate p_tid values for a single p_pid ? */
2104 pse.vmem_rss = procs[i].p_vm_rssize * pagesize;
2105 pse.vmem_data = procs[i].p_vm_dsize * pagesize;
2106 pse.vmem_code = procs[i].p_vm_tsize * pagesize;
2107 pse.stack_size = procs[i].p_vm_ssize * pagesize;
2108 pse.vmem_size = pse.stack_size + pse.vmem_code + pse.vmem_data;
2109 pse.vmem_minflt_counter = procs[i].p_uru_minflt;
2110 pse.vmem_majflt_counter = procs[i].p_uru_majflt;
2112 pse.cpu_user_counter = procs[i].p_uutime_usec +
2113 (1000000lu * procs[i].p_uutime_sec);
2114 pse.cpu_system_counter = procs[i].p_ustime_usec +
2115 (1000000lu * procs[i].p_ustime_sec);
2123 /* context switch counters not implemented */
2124 pse.cswitch_vol = -1;
2125 pse.cswitch_invol = -1;
2127 ps_list_add (procs[i].p_comm, have_cmdline ? cmdline : NULL, &pse);
2129 switch (procs[i].p_stat)
2131 case SSTOP: stopped++; break;
2132 case SSLEEP: sleeping++; break;
2133 case SRUN: running++; break;
2134 case SIDL: idle++; break;
2135 case SONPROC: onproc++; break;
2136 case SDEAD: dead++; break;
2137 case SZOMB: zombies++; break;
2139 } /* if ((proc_ptr == NULL) || (proc_ptr->p_pid != procs[i].p_pid)) */
2144 ps_submit_state ("running", running);
2145 ps_submit_state ("sleeping", sleeping);
2146 ps_submit_state ("zombies", zombies);
2147 ps_submit_state ("stopped", stopped);
2148 ps_submit_state ("onproc", onproc);
2149 ps_submit_state ("idle", idle);
2150 ps_submit_state ("dead", dead);
2152 for (procstat_t *ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
2153 ps_submit_proc_list (ps_ptr);
2154 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_OPENBSD */
2156 #elif HAVE_PROCINFO_H
2168 procstat_entry_t pse;
2171 while ((nprocs = getprocs64 (procentry, sizeof(struct procentry64),
2172 /* fdsinfo = */ NULL, sizeof(struct fdsinfo64),
2173 &pindex, MAXPROCENTRY)) > 0)
2175 for (int i = 0; i < nprocs; i++)
2179 char arglist[MAXARGLN+1];
2183 if (procentry[i].pi_state == SNONE) continue;
2184 /* if (procentry[i].pi_state == SZOMB) FIXME */
2186 cmdline = procentry[i].pi_comm;
2187 cargs = procentry[i].pi_comm;
2188 if ( procentry[i].pi_flags & SKPROC )
2190 if (procentry[i].pi_pid == 0)
2191 cmdline = "swapper";
2196 if (getargs(&procentry[i], sizeof(struct procentry64), arglist, MAXARGLN) >= 0)
2201 while (++n < MAXARGLN)
2203 if (arglist[n] == '\0')
2205 if (arglist[n+1] == '\0')
2214 pse.id = procentry[i].pi_pid;
2216 pse.num_lwp = procentry[i].pi_thcount;
2220 while ((nthreads = getthrds64(procentry[i].pi_pid,
2221 thrdentry, sizeof(struct thrdentry64),
2222 &thindex, MAXTHRDENTRY)) > 0)
2226 for (j=0; j< nthreads; j++)
2228 switch (thrdentry[j].ti_state)
2230 /* case TSNONE: break; */
2231 case TSIDL: blocked++; break; /* FIXME is really blocked */
2232 case TSRUN: running++; break;
2233 case TSSLEEP: sleeping++; break;
2234 case TSSWAP: paging++; break;
2235 case TSSTOP: stopped++; break;
2236 case TSZOMB: zombies++; break;
2239 if (nthreads < MAXTHRDENTRY)
2243 /* tv_usec is nanosec ??? */
2244 pse.cpu_user_counter = procentry[i].pi_ru.ru_utime.tv_sec * 1000000 +
2245 procentry[i].pi_ru.ru_utime.tv_usec / 1000;
2248 /* tv_usec is nanosec ??? */
2249 pse.cpu_system_counter = procentry[i].pi_ru.ru_stime.tv_sec * 1000000 +
2250 procentry[i].pi_ru.ru_stime.tv_usec / 1000;
2252 pse.vmem_minflt_counter = procentry[i].pi_minflt;
2253 pse.vmem_majflt_counter = procentry[i].pi_majflt;
2255 pse.vmem_size = procentry[i].pi_tsize + procentry[i].pi_dvm * pagesize;
2256 pse.vmem_rss = (procentry[i].pi_drss + procentry[i].pi_trss) * pagesize;
2267 pse.cswitch_vol = -1;
2268 pse.cswitch_invol = -1;
2270 ps_list_add (cmdline, cargs, &pse);
2271 } /* for (i = 0 .. nprocs) */
2273 if (nprocs < MAXPROCENTRY)
2275 } /* while (getprocs64() > 0) */
2276 ps_submit_state ("running", running);
2277 ps_submit_state ("sleeping", sleeping);
2278 ps_submit_state ("zombies", zombies);
2279 ps_submit_state ("stopped", stopped);
2280 ps_submit_state ("paging", paging);
2281 ps_submit_state ("blocked", blocked);
2283 for (procstat_t *ps = list_head_g; ps != NULL; ps = ps->next)
2284 ps_submit_proc_list (ps);
2285 /* #endif HAVE_PROCINFO_H */
2287 #elif KERNEL_SOLARIS
2289 * The Solaris section adds a few more process states and removes some
2290 * process states compared to linux. Most notably there is no "PAGING"
2291 * and "BLOCKED" state for a process. The rest is similar to the linux
2309 char cmdline[PRARGSZ];
2313 proc = opendir ("/proc");
2317 while ((ent = readdir(proc)) != NULL)
2321 procstat_entry_t pse;
2324 if (!isdigit ((int) ent->d_name[0]))
2327 pid = strtol (ent->d_name, &endptr, 10);
2328 if (*endptr != 0) /* value didn't completely parse as a number */
2331 status = ps_read_process (pid, &ps, &state);
2334 DEBUG("ps_read_process failed: %i", status);
2338 memset (&pse, 0, sizeof (pse));
2342 pse.num_proc = ps.num_proc;
2343 pse.num_lwp = ps.num_lwp;
2344 pse.vmem_size = ps.vmem_size;
2345 pse.vmem_rss = ps.vmem_rss;
2346 pse.vmem_data = ps.vmem_data;
2347 pse.vmem_code = ps.vmem_code;
2348 pse.stack_size = ps.stack_size;
2350 pse.vmem_minflt_counter = ps.vmem_minflt_counter;
2351 pse.vmem_majflt_counter = ps.vmem_majflt_counter;
2353 pse.cpu_user_counter = ps.cpu_user_counter;
2354 pse.cpu_system_counter = ps.cpu_system_counter;
2356 pse.io_rchar = ps.io_rchar;
2357 pse.io_wchar = ps.io_wchar;
2358 pse.io_syscr = ps.io_syscr;
2359 pse.io_syscw = ps.io_syscw;
2361 pse.cswitch_vol = -1;
2362 pse.cswitch_invol = -1;
2366 case 'R': running++; break;
2367 case 'S': sleeping++; break;
2368 case 'E': detached++; break;
2369 case 'Z': zombies++; break;
2370 case 'T': stopped++; break;
2371 case 'A': daemon++; break;
2372 case 'Y': system++; break;
2373 case 'O': orphan++; break;
2377 ps_list_add (ps.name,
2378 ps_get_cmdline (pid, ps.name, cmdline, sizeof (cmdline)),
2380 } /* while(readdir) */
2383 ps_submit_state ("running", running);
2384 ps_submit_state ("sleeping", sleeping);
2385 ps_submit_state ("zombies", zombies);
2386 ps_submit_state ("stopped", stopped);
2387 ps_submit_state ("detached", detached);
2388 ps_submit_state ("daemon", daemon);
2389 ps_submit_state ("system", system);
2390 ps_submit_state ("orphan", orphan);
2392 for (procstat_t *ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
2393 ps_submit_proc_list (ps_ptr);
2396 #endif /* KERNEL_SOLARIS */
2401 void module_register (void)
2403 plugin_register_complex_config ("processes", ps_config);
2404 plugin_register_init ("processes", ps_init);
2405 plugin_register_read ("processes", ps_read);
2406 } /* void module_register */