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>
35 * Pavel Rochnyack <pavel2000 at ngs.ru>
36 * Wilfried Goesgens <dothebart at citadel.org>
44 /* Include header files for the mach system, if they exist.. */
46 #if HAVE_MACH_MACH_INIT_H
47 #include <mach/mach_init.h>
49 #if HAVE_MACH_HOST_PRIV_H
50 #include <mach/host_priv.h>
52 #if HAVE_MACH_MACH_ERROR_H
53 #include <mach/mach_error.h>
55 #if HAVE_MACH_MACH_HOST_H
56 #include <mach/mach_host.h>
58 #if HAVE_MACH_MACH_PORT_H
59 #include <mach/mach_port.h>
61 #if HAVE_MACH_MACH_TYPES_H
62 #include <mach/mach_types.h>
64 #if HAVE_MACH_MESSAGE_H
65 #include <mach/message.h>
67 #if HAVE_MACH_PROCESSOR_SET_H
68 #include <mach/processor_set.h>
71 #include <mach/task.h>
73 #if HAVE_MACH_THREAD_ACT_H
74 #include <mach/thread_act.h>
76 #if HAVE_MACH_VM_REGION_H
77 #include <mach/vm_region.h>
79 #if HAVE_MACH_VM_MAP_H
80 #include <mach/vm_map.h>
82 #if HAVE_MACH_VM_PROT_H
83 #include <mach/vm_prot.h>
86 #include <sys/sysctl.h>
88 /* #endif HAVE_THREAD_INFO */
91 #if HAVE_LINUX_CONFIG_H
92 #include <linux/config.h>
97 /* #endif KERNEL_LINUX */
99 #elif HAVE_LIBKVM_GETPROCS && \
100 (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD)
102 #include <sys/param.h>
103 #include <sys/proc.h>
104 #include <sys/sysctl.h>
105 #include <sys/user.h>
106 /* #endif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD ||
107 * HAVE_STRUCT_KINFO_PROC_OPENBSD) */
109 #elif HAVE_PROCINFO_H
110 #include <procinfo.h>
111 #include <sys/types.h>
113 #define MAXPROCENTRY 32
114 #define MAXTHRDENTRY 16
115 #define MAXARGLN 1024
116 /* #endif HAVE_PROCINFO_H */
119 /* Hack: Avoid #error when building a 32-bit binary with
120 * _FILE_OFFSET_BITS=64. There is a reason for this #error, as one
121 * of the structures in <sys/procfs.h> uses an off_t, but that
122 * isn't relevant to our usage of procfs. */
123 #if !defined(_LP64) && _FILE_OFFSET_BITS == 64
125 #undef _FILE_OFFSET_BITS
131 #define _FILE_OFFSET_BITS 64
136 #include <sys/user.h>
142 /* #endif KERNEL_SOLARIS */
145 #error "No applicable input method."
156 #ifndef CMDLINE_BUFFER_SIZE
157 #if defined(ARG_MAX) && (ARG_MAX < 4096)
158 #define CMDLINE_BUFFER_SIZE ARG_MAX
160 #define CMDLINE_BUFFER_SIZE 4096
164 #define PROCSTAT_NAME_LEN 256
165 typedef struct process_entry_s {
167 char name[PROCSTAT_NAME_LEN];
169 unsigned long num_proc;
170 unsigned long num_lwp;
171 unsigned long num_fd;
172 unsigned long num_maps;
173 unsigned long vmem_size;
174 unsigned long vmem_rss;
175 unsigned long vmem_data;
176 unsigned long vmem_code;
177 unsigned long stack_size;
179 derive_t vmem_minflt_counter;
180 derive_t vmem_majflt_counter;
182 derive_t cpu_user_counter;
183 derive_t cpu_system_counter;
194 derive_t cswitch_vol;
195 derive_t cswitch_invol;
203 typedef struct procstat_entry_s {
207 derive_t vmem_minflt_counter;
208 derive_t vmem_majflt_counter;
210 derive_t cpu_user_counter;
211 derive_t cpu_system_counter;
221 derive_t cswitch_vol;
222 derive_t cswitch_invol;
224 struct procstat_entry_s *next;
227 typedef struct procstat {
228 char name[PROCSTAT_NAME_LEN];
233 unsigned long num_proc;
234 unsigned long num_lwp;
235 unsigned long num_fd;
236 unsigned long num_maps;
237 unsigned long vmem_size;
238 unsigned long vmem_rss;
239 unsigned long vmem_data;
240 unsigned long vmem_code;
241 unsigned long stack_size;
243 derive_t vmem_minflt_counter;
244 derive_t vmem_majflt_counter;
246 derive_t cpu_user_counter;
247 derive_t cpu_system_counter;
257 derive_t cswitch_vol;
258 derive_t cswitch_invol;
261 _Bool report_maps_num;
262 _Bool report_ctx_switch;
264 struct procstat *next;
265 struct procstat_entry_s *instances;
268 static procstat_t *list_head_g = NULL;
270 static _Bool want_init = 1;
271 static _Bool report_ctx_switch = 0;
272 static _Bool report_fd_num = 0;
273 static _Bool report_maps_num = 0;
276 static mach_port_t port_host_self;
277 static mach_port_t port_task_self;
279 static processor_set_name_array_t pset_list;
280 static mach_msg_type_number_t pset_list_len;
281 /* #endif HAVE_THREAD_INFO */
284 static long pagesize_g;
285 static void ps_fill_details(const procstat_t *ps, process_entry_t *entry);
286 /* #endif KERNEL_LINUX */
288 #elif HAVE_LIBKVM_GETPROCS && \
289 (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD)
291 /* #endif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD ||
292 * HAVE_STRUCT_KINFO_PROC_OPENBSD) */
294 #elif HAVE_PROCINFO_H
295 static struct procentry64 procentry[MAXPROCENTRY];
296 static struct thrdentry64 thrdentry[MAXTHRDENTRY];
299 #ifndef _AIXVERSION_610
300 int getprocs64(void *procsinfo, int sizproc, void *fdsinfo, int sizfd,
301 pid_t *index, int count);
302 int getthrds64(pid_t, void *, int, tid64_t *, int);
304 int getargs(void *processBuffer, int bufferLen, char *argsBuffer, int argsLen);
305 #endif /* HAVE_PROCINFO_H */
307 /* put name of process from config to list_head_g tree
308 * list_head_g is a list of 'procstat_t' structs with
309 * processes names we want to watch */
310 static procstat_t *ps_list_register(const char *name, const char *regexp) {
315 new = calloc(1, sizeof(*new));
317 ERROR("processes plugin: ps_list_register: calloc failed.");
320 sstrncpy(new->name, name, sizeof(new->name));
328 new->cswitch_vol = -1;
329 new->cswitch_invol = -1;
331 new->report_fd_num = report_fd_num;
332 new->report_maps_num = report_maps_num;
333 new->report_ctx_switch = report_ctx_switch;
336 if (regexp != NULL) {
337 DEBUG("ProcessMatch: adding \"%s\" as criteria to process %s.", regexp,
339 new->re = malloc(sizeof(*new->re));
340 if (new->re == NULL) {
341 ERROR("processes plugin: ps_list_register: malloc failed.");
346 status = regcomp(new->re, regexp, REG_EXTENDED | REG_NOSUB);
348 DEBUG("ProcessMatch: compiling the regular expression \"%s\" failed.",
356 if (regexp != NULL) {
357 ERROR("processes plugin: ps_list_register: "
358 "Regular expression \"%s\" found in config "
359 "file, but support for regular expressions "
360 "has been disabled at compile time.",
367 for (ptr = list_head_g; ptr != NULL; ptr = ptr->next) {
368 if (strcmp(ptr->name, name) == 0) {
369 WARNING("processes plugin: You have configured more "
370 "than one `Process' or "
371 "`ProcessMatch' with the same name. "
372 "All but the first setting will be "
381 if (ptr->next == NULL)
391 } /* void ps_list_register */
393 /* try to match name against entry, returns 1 if success */
394 static int ps_list_match(const char *name, const char *cmdline,
397 if (ps->re != NULL) {
402 if ((str == NULL) || (str[0] == 0))
407 status = regexec(ps->re, str,
415 if (strcmp(ps->name, name) == 0)
419 } /* int ps_list_match */
421 static void ps_update_counter(derive_t *group_counter, derive_t *curr_counter,
422 derive_t new_counter) {
423 unsigned long curr_value;
426 *curr_counter = new_counter;
430 if (new_counter < *curr_counter)
431 curr_value = new_counter + (ULONG_MAX - *curr_counter);
433 curr_value = new_counter - *curr_counter;
435 if (*group_counter == -1)
438 *curr_counter = new_counter;
439 *group_counter += curr_value;
442 /* add process entry to 'instances' of process 'name' (or refresh it) */
443 static void ps_list_add(const char *name, const char *cmdline,
444 process_entry_t *entry) {
445 procstat_entry_t *pse;
450 for (procstat_t *ps = list_head_g; ps != NULL; ps = ps->next) {
451 if ((ps_list_match(name, cmdline, ps)) == 0)
455 ps_fill_details(ps, entry);
458 for (pse = ps->instances; pse != NULL; pse = pse->next)
459 if ((pse->id == entry->id) || (pse->next == NULL))
462 if ((pse == NULL) || (pse->id != entry->id)) {
463 procstat_entry_t *new;
465 new = calloc(1, sizeof(*new));
480 ps->num_proc += entry->num_proc;
481 ps->num_lwp += entry->num_lwp;
482 ps->num_fd += entry->num_fd;
483 ps->num_maps += entry->num_maps;
484 ps->vmem_size += entry->vmem_size;
485 ps->vmem_rss += entry->vmem_rss;
486 ps->vmem_data += entry->vmem_data;
487 ps->vmem_code += entry->vmem_code;
488 ps->stack_size += entry->stack_size;
490 if ((entry->io_rchar != -1) && (entry->io_wchar != -1)) {
491 ps_update_counter(&ps->io_rchar, &pse->io_rchar, entry->io_rchar);
492 ps_update_counter(&ps->io_wchar, &pse->io_wchar, entry->io_wchar);
495 if ((entry->io_syscr != -1) && (entry->io_syscw != -1)) {
496 ps_update_counter(&ps->io_syscr, &pse->io_syscr, entry->io_syscr);
497 ps_update_counter(&ps->io_syscw, &pse->io_syscw, entry->io_syscw);
500 if ((entry->io_diskr != -1) && (entry->io_diskw != -1)) {
501 ps_update_counter(&ps->io_diskr, &pse->io_diskr, entry->io_diskr);
502 ps_update_counter(&ps->io_diskw, &pse->io_diskw, entry->io_diskw);
505 if ((entry->cswitch_vol != -1) && (entry->cswitch_vol != -1)) {
506 ps_update_counter(&ps->cswitch_vol, &pse->cswitch_vol,
508 ps_update_counter(&ps->cswitch_invol, &pse->cswitch_invol,
509 entry->cswitch_invol);
512 ps_update_counter(&ps->vmem_minflt_counter, &pse->vmem_minflt_counter,
513 entry->vmem_minflt_counter);
514 ps_update_counter(&ps->vmem_majflt_counter, &pse->vmem_majflt_counter,
515 entry->vmem_majflt_counter);
517 ps_update_counter(&ps->cpu_user_counter, &pse->cpu_user_counter,
518 entry->cpu_user_counter);
519 ps_update_counter(&ps->cpu_system_counter, &pse->cpu_system_counter,
520 entry->cpu_system_counter);
524 /* remove old entries from instances of processes in list_head_g */
525 static void ps_list_reset(void) {
526 procstat_entry_t *pse;
527 procstat_entry_t *pse_prev;
529 for (procstat_t *ps = list_head_g; ps != NULL; ps = ps->next) {
542 while (pse != NULL) {
544 DEBUG("Removing this procstat entry cause it's too old: "
545 "id = %lu; name = %s;",
548 if (pse_prev == NULL) {
549 ps->instances = pse->next;
553 pse_prev->next = pse->next;
555 pse = pse_prev->next;
562 } /* while (pse != NULL) */
563 } /* for (ps = list_head_g; ps != NULL; ps = ps->next) */
566 static void ps_tune_instance(oconfig_item_t *ci, procstat_t *ps) {
567 for (int i = 0; i < ci->children_num; i++) {
568 oconfig_item_t *c = ci->children + i;
570 if (strcasecmp(c->key, "CollectContextSwitch") == 0)
571 cf_util_get_boolean(c, &ps->report_ctx_switch);
572 else if (strcasecmp(c->key, "CollectFileDescriptor") == 0)
573 cf_util_get_boolean(c, &ps->report_fd_num);
574 else if (strcasecmp(c->key, "CollectMemoryMaps") == 0)
575 cf_util_get_boolean(c, &ps->report_maps_num);
577 ERROR("processes plugin: Option `%s' not allowed here.", c->key);
579 } /* for (ci->children) */
580 } /* void ps_tune_instance */
582 /* put all pre-defined 'Process' names from config to list_head_g tree */
583 static int ps_config(oconfig_item_t *ci) {
585 const size_t max_procname_len = 15;
586 #elif KERNEL_SOLARIS || KERNEL_FREEBSD
587 const size_t max_procname_len = MAXCOMLEN - 1;
592 for (int i = 0; i < ci->children_num; ++i) {
593 oconfig_item_t *c = ci->children + i;
595 if (strcasecmp(c->key, "Process") == 0) {
596 if ((c->values_num != 1) || (OCONFIG_TYPE_STRING != c->values[0].type)) {
597 ERROR("processes plugin: `Process' expects exactly "
598 "one string argument (got %i).",
603 #if KERNEL_LINUX || KERNEL_SOLARIS || KERNEL_FREEBSD
604 if (strlen(c->values[0].value.string) > max_procname_len) {
605 WARNING("processes plugin: this platform has a %zu character limit "
606 "to process names. The `Process \"%s\"' option will "
607 "not work as expected.",
608 max_procname_len, c->values[0].value.string);
612 ps = ps_list_register(c->values[0].value.string, NULL);
614 if (c->children_num != 0 && ps != NULL)
615 ps_tune_instance(c, ps);
616 } else if (strcasecmp(c->key, "ProcessMatch") == 0) {
617 if ((c->values_num != 2) || (OCONFIG_TYPE_STRING != c->values[0].type) ||
618 (OCONFIG_TYPE_STRING != c->values[1].type)) {
619 ERROR("processes plugin: `ProcessMatch' needs exactly "
620 "two string arguments (got %i).",
625 ps = ps_list_register(c->values[0].value.string,
626 c->values[1].value.string);
628 if (c->children_num != 0 && ps != NULL)
629 ps_tune_instance(c, ps);
630 } else if (strcasecmp(c->key, "CollectContextSwitch") == 0) {
631 cf_util_get_boolean(c, &report_ctx_switch);
632 } else if (strcasecmp(c->key, "CollectFileDescriptor") == 0) {
633 cf_util_get_boolean(c, &report_fd_num);
634 } else if (strcasecmp(c->key, "CollectMemoryMaps") == 0) {
635 cf_util_get_boolean(c, &report_maps_num);
637 ERROR("processes plugin: The `%s' configuration option is not "
638 "understood and will be ignored.",
647 static int ps_init(void) {
649 kern_return_t status;
651 port_host_self = mach_host_self();
652 port_task_self = mach_task_self();
654 if (pset_list != NULL) {
655 vm_deallocate(port_task_self, (vm_address_t)pset_list,
656 pset_list_len * sizeof(processor_set_t));
661 if ((status = host_processor_sets(port_host_self, &pset_list,
662 &pset_list_len)) != KERN_SUCCESS) {
663 ERROR("host_processor_sets failed: %s\n", mach_error_string(status));
668 /* #endif HAVE_THREAD_INFO */
671 pagesize_g = sysconf(_SC_PAGESIZE);
672 DEBUG("pagesize_g = %li; CONFIG_HZ = %i;", pagesize_g, CONFIG_HZ);
673 /* #endif KERNEL_LINUX */
675 #elif HAVE_LIBKVM_GETPROCS && \
676 (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD)
677 pagesize = getpagesize();
678 /* #endif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD ||
679 * HAVE_STRUCT_KINFO_PROC_OPENBSD) */
681 #elif HAVE_PROCINFO_H
682 pagesize = getpagesize();
683 #endif /* HAVE_PROCINFO_H */
688 /* submit global state (e.g.: qty of zombies, running, etc..) */
689 static void ps_submit_state(const char *state, double value) {
690 value_list_t vl = VALUE_LIST_INIT;
692 vl.values = &(value_t){.gauge = value};
694 sstrncpy(vl.plugin, "processes", sizeof(vl.plugin));
695 sstrncpy(vl.plugin_instance, "", sizeof(vl.plugin_instance));
696 sstrncpy(vl.type, "ps_state", sizeof(vl.type));
697 sstrncpy(vl.type_instance, state, sizeof(vl.type_instance));
699 plugin_dispatch_values(&vl);
702 /* submit info about specific process (e.g.: memory taken, cpu usage, etc..) */
703 static void ps_submit_proc_list(procstat_t *ps) {
704 value_list_t vl = VALUE_LIST_INIT;
708 sstrncpy(vl.plugin, "processes", sizeof(vl.plugin));
709 sstrncpy(vl.plugin_instance, ps->name, sizeof(vl.plugin_instance));
711 sstrncpy(vl.type, "ps_vm", sizeof(vl.type));
712 vl.values[0].gauge = ps->vmem_size;
714 plugin_dispatch_values(&vl);
716 sstrncpy(vl.type, "ps_rss", sizeof(vl.type));
717 vl.values[0].gauge = ps->vmem_rss;
719 plugin_dispatch_values(&vl);
721 sstrncpy(vl.type, "ps_data", sizeof(vl.type));
722 vl.values[0].gauge = ps->vmem_data;
724 plugin_dispatch_values(&vl);
726 sstrncpy(vl.type, "ps_code", sizeof(vl.type));
727 vl.values[0].gauge = ps->vmem_code;
729 plugin_dispatch_values(&vl);
731 sstrncpy(vl.type, "ps_stacksize", sizeof(vl.type));
732 vl.values[0].gauge = ps->stack_size;
734 plugin_dispatch_values(&vl);
736 sstrncpy(vl.type, "ps_cputime", sizeof(vl.type));
737 vl.values[0].derive = ps->cpu_user_counter;
738 vl.values[1].derive = ps->cpu_system_counter;
740 plugin_dispatch_values(&vl);
742 sstrncpy(vl.type, "ps_count", sizeof(vl.type));
743 vl.values[0].gauge = ps->num_proc;
744 vl.values[1].gauge = ps->num_lwp;
746 plugin_dispatch_values(&vl);
748 sstrncpy(vl.type, "ps_pagefaults", sizeof(vl.type));
749 vl.values[0].derive = ps->vmem_minflt_counter;
750 vl.values[1].derive = ps->vmem_majflt_counter;
752 plugin_dispatch_values(&vl);
754 if ((ps->io_rchar != -1) && (ps->io_wchar != -1)) {
755 sstrncpy(vl.type, "io_octets", sizeof(vl.type));
756 vl.values[0].derive = ps->io_rchar;
757 vl.values[1].derive = ps->io_wchar;
759 plugin_dispatch_values(&vl);
762 if ((ps->io_syscr != -1) && (ps->io_syscw != -1)) {
763 sstrncpy(vl.type, "io_ops", sizeof(vl.type));
764 vl.values[0].derive = ps->io_syscr;
765 vl.values[1].derive = ps->io_syscw;
767 plugin_dispatch_values(&vl);
770 if ((ps->io_diskr != -1) && (ps->io_diskw != -1)) {
771 sstrncpy(vl.type, "disk_octets", sizeof(vl.type));
772 vl.values[0].derive = ps->io_diskr;
773 vl.values[1].derive = ps->io_diskw;
775 plugin_dispatch_values(&vl);
778 if (ps->num_fd > 0) {
779 sstrncpy(vl.type, "file_handles", sizeof(vl.type));
780 vl.values[0].gauge = ps->num_fd;
782 plugin_dispatch_values(&vl);
785 if (ps->num_maps > 0) {
786 sstrncpy(vl.type, "file_handles", sizeof(vl.type));
787 sstrncpy(vl.type_instance, "mapped", sizeof(vl.type_instance));
788 vl.values[0].gauge = ps->num_maps;
790 plugin_dispatch_values(&vl);
793 if ((ps->cswitch_vol != -1) && (ps->cswitch_invol != -1)) {
794 sstrncpy(vl.type, "contextswitch", sizeof(vl.type));
795 sstrncpy(vl.type_instance, "voluntary", sizeof(vl.type_instance));
796 vl.values[0].derive = ps->cswitch_vol;
798 plugin_dispatch_values(&vl);
800 sstrncpy(vl.type, "contextswitch", sizeof(vl.type));
801 sstrncpy(vl.type_instance, "involuntary", sizeof(vl.type_instance));
802 vl.values[0].derive = ps->cswitch_invol;
804 plugin_dispatch_values(&vl);
807 DEBUG("name = %s; num_proc = %lu; num_lwp = %lu; num_fd = %lu; num_maps = %lu; "
808 "vmem_size = %lu; vmem_rss = %lu; vmem_data = %lu; "
810 "vmem_minflt_counter = %" PRIi64 "; vmem_majflt_counter = %" PRIi64 "; "
811 "cpu_user_counter = %" PRIi64 "; cpu_system_counter = %" PRIi64 "; "
812 "io_rchar = %" PRIi64 "; io_wchar = %" PRIi64 "; "
813 "io_syscr = %" PRIi64 "; io_syscw = %" PRIi64 "; "
814 "io_diskr = %" PRIi64 "; io_diskw = %" PRIi64 "; "
815 "cswitch_vol = %" PRIi64 "; cswitch_invol = %" PRIi64 ";",
816 ps->name, ps->num_proc, ps->num_lwp, ps->num_fd, ps->num_maps, ps->vmem_size,
817 ps->vmem_rss, ps->vmem_data, ps->vmem_code, ps->vmem_minflt_counter,
818 ps->vmem_majflt_counter, ps->cpu_user_counter, ps->cpu_system_counter,
819 ps->io_rchar, ps->io_wchar, ps->io_syscr, ps->io_syscw, ps->io_diskr,
820 ps->io_diskw, ps->cswitch_vol, ps->cswitch_invol);
822 } /* void ps_submit_proc_list */
824 #if KERNEL_LINUX || KERNEL_SOLARIS
825 static void ps_submit_fork_rate(derive_t value) {
826 value_list_t vl = VALUE_LIST_INIT;
828 vl.values = &(value_t){.derive = value};
830 sstrncpy(vl.plugin, "processes", sizeof(vl.plugin));
831 sstrncpy(vl.plugin_instance, "", sizeof(vl.plugin_instance));
832 sstrncpy(vl.type, "fork_rate", sizeof(vl.type));
833 sstrncpy(vl.type_instance, "", sizeof(vl.type_instance));
835 plugin_dispatch_values(&vl);
837 #endif /* KERNEL_LINUX || KERNEL_SOLARIS*/
839 /* ------- additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
841 static int ps_read_tasks_status(process_entry_t *ps) {
847 derive_t cswitch_vol = 0;
848 derive_t cswitch_invol = 0;
853 snprintf(dirname, sizeof(dirname), "/proc/%li/task", ps->id);
855 if ((dh = opendir(dirname)) == NULL) {
856 DEBUG("Failed to open directory `%s'", dirname);
860 while ((ent = readdir(dh)) != NULL) {
863 if (!isdigit((int)ent->d_name[0]))
868 if (snprintf(filename, sizeof(filename), "/proc/%li/task/%s/status", ps->id,
869 tpid) >= sizeof(filename)) {
870 DEBUG("Filename too long: `%s'", filename);
874 if ((fh = fopen(filename, "r")) == NULL) {
875 DEBUG("Failed to open file `%s'", filename);
879 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
883 if (strncmp(buffer, "voluntary_ctxt_switches", 23) != 0 &&
884 strncmp(buffer, "nonvoluntary_ctxt_switches", 26) != 0)
887 numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
894 tmp = (derive_t)strtoll(fields[1], &endptr, /* base = */ 10);
895 if ((errno == 0) && (endptr != fields[1])) {
896 if (strncmp(buffer, "voluntary_ctxt_switches", 23) == 0) {
898 } else if (strncmp(buffer, "nonvoluntary_ctxt_switches", 26) == 0) {
899 cswitch_invol += tmp;
902 } /* while (fgets) */
906 WARNING("processes: fclose: %s",
907 sstrerror(errno, errbuf, sizeof(errbuf)));
912 ps->cswitch_vol = cswitch_vol;
913 ps->cswitch_invol = cswitch_invol;
916 } /* int *ps_read_tasks_status */
918 /* Read data from /proc/pid/status */
919 static int ps_read_status(long pid, process_entry_t *ps) {
923 unsigned long lib = 0;
924 unsigned long exe = 0;
925 unsigned long data = 0;
926 unsigned long threads = 0;
930 snprintf(filename, sizeof(filename), "/proc/%li/status", pid);
931 if ((fh = fopen(filename, "r")) == NULL)
934 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
938 if (strncmp(buffer, "Vm", 2) != 0 && strncmp(buffer, "Threads", 7) != 0)
941 numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
948 tmp = strtoul(fields[1], &endptr, /* base = */ 10);
949 if ((errno == 0) && (endptr != fields[1])) {
950 if (strncmp(buffer, "VmData", 6) == 0) {
952 } else if (strncmp(buffer, "VmLib", 5) == 0) {
954 } else if (strncmp(buffer, "VmExe", 5) == 0) {
956 } else if (strncmp(buffer, "Threads", 7) == 0) {
960 } /* while (fgets) */
964 WARNING("processes: fclose: %s", sstrerror(errno, errbuf, sizeof(errbuf)));
967 ps->vmem_data = data * 1024;
968 ps->vmem_code = (exe + lib) * 1024;
970 ps->num_lwp = threads;
973 } /* int *ps_read_status */
975 static int ps_read_io(process_entry_t *ps) {
983 snprintf(filename, sizeof(filename), "/proc/%li/io", ps->id);
984 if ((fh = fopen(filename, "r")) == NULL) {
985 DEBUG("ps_read_io: Failed to open file `%s'", filename);
989 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
990 derive_t *val = NULL;
994 if (strncasecmp(buffer, "rchar:", 6) == 0)
995 val = &(ps->io_rchar);
996 else if (strncasecmp(buffer, "wchar:", 6) == 0)
997 val = &(ps->io_wchar);
998 else if (strncasecmp(buffer, "syscr:", 6) == 0)
999 val = &(ps->io_syscr);
1000 else if (strncasecmp(buffer, "syscw:", 6) == 0)
1001 val = &(ps->io_syscw);
1002 else if (strncasecmp(buffer, "read_bytes:", 11) == 0)
1003 val = &(ps->io_diskr);
1004 else if (strncasecmp(buffer, "write_bytes:", 12) == 0)
1005 val = &(ps->io_diskw);
1009 numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
1016 tmp = strtoll(fields[1], &endptr, /* base = */ 10);
1017 if ((errno != 0) || (endptr == fields[1]))
1020 *val = (derive_t)tmp;
1021 } /* while (fgets) */
1025 WARNING("processes: fclose: %s", sstrerror(errno, errbuf, sizeof(errbuf)));
1028 } /* int ps_read_io (...) */
1030 static int ps_count_maps(pid_t pid) {
1036 snprintf(filename, sizeof(filename), "/proc/%d/maps", pid);
1037 if ((fh = fopen(filename, "r")) == NULL) {
1038 DEBUG("ps_count_maps: Failed to open file `%s'", filename);
1042 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
1043 if (strchr(buffer, '\n')) {
1046 } /* while (fgets) */
1050 WARNING("processes: fclose: %s", sstrerror(errno, errbuf, sizeof(errbuf)));
1053 } /* int ps_count_maps (...) */
1055 static int ps_count_fd(int pid) {
1061 snprintf(dirname, sizeof(dirname), "/proc/%i/fd", pid);
1063 if ((dh = opendir(dirname)) == NULL) {
1064 DEBUG("Failed to open directory `%s'", dirname);
1067 while ((ent = readdir(dh)) != NULL) {
1068 if (!isdigit((int)ent->d_name[0]))
1075 return (count >= 1) ? count : 1;
1076 } /* int ps_count_fd (pid) */
1078 static void ps_fill_details(const procstat_t *ps, process_entry_t *entry) {
1079 if (entry->has_io == 0) {
1084 if (ps->report_ctx_switch) {
1085 if (entry->has_cswitch == 0) {
1086 ps_read_tasks_status(entry);
1087 entry->has_cswitch = 1;
1091 if (ps->report_maps_num) {
1093 if (entry->has_maps == 0 && (num_maps = ps_count_maps(entry->id)) > 0) {
1094 entry->num_maps = num_maps;
1096 entry->has_maps = 1;
1099 if (ps->report_fd_num) {
1101 if (entry->has_fd == 0 && (num_fd = ps_count_fd(entry->id)) > 0) {
1102 entry->num_fd = num_fd;
1106 } /* void ps_fill_details (...) */
1108 static int ps_read_process(long pid, process_entry_t *ps, char *state) {
1118 size_t name_start_pos;
1119 size_t name_end_pos;
1122 derive_t cpu_user_counter;
1123 derive_t cpu_system_counter;
1124 long long unsigned vmem_size;
1125 long long unsigned vmem_rss;
1126 long long unsigned stack_size;
1130 snprintf(filename, sizeof(filename), "/proc/%li/stat", pid);
1132 status = read_file_contents(filename, buffer, sizeof(buffer) - 1);
1135 buffer_len = (size_t)status;
1136 buffer[buffer_len] = 0;
1138 /* The name of the process is enclosed in parens. Since the name can
1139 * contain parens itself, spaces, numbers and pretty much everything
1140 * else, use these to determine the process name. We don't use
1141 * strchr(3) and strrchr(3) to avoid pointer arithmetic which would
1142 * otherwise be required to determine name_len. */
1144 while (name_start_pos < buffer_len && buffer[name_start_pos] != '(')
1147 name_end_pos = buffer_len;
1148 while (name_end_pos > 0 && buffer[name_end_pos] != ')')
1151 /* Either '(' or ')' is not found or they are in the wrong order.
1152 * Anyway, something weird that shouldn't happen ever. */
1153 if (name_start_pos >= name_end_pos) {
1154 ERROR("processes plugin: name_start_pos = %zu >= name_end_pos = %zu",
1155 name_start_pos, name_end_pos);
1159 name_len = (name_end_pos - name_start_pos) - 1;
1160 if (name_len >= sizeof(ps->name))
1161 name_len = sizeof(ps->name) - 1;
1163 sstrncpy(ps->name, &buffer[name_start_pos + 1], name_len + 1);
1165 if ((buffer_len - name_end_pos) < 2)
1167 buffer_ptr = &buffer[name_end_pos + 2];
1169 fields_len = strsplit(buffer_ptr, fields, STATIC_ARRAY_SIZE(fields));
1170 if (fields_len < 22) {
1171 DEBUG("processes plugin: ps_read_process (pid = %li):"
1172 " `%s' has only %i fields..",
1173 pid, filename, fields_len);
1177 *state = fields[0][0];
1179 if (*state == 'Z') {
1183 ps->num_lwp = strtoul(fields[17], /* endptr = */ NULL, /* base = */ 10);
1184 if ((ps_read_status(pid, ps)) != 0) {
1188 DEBUG("ps_read_process: did not get vmem data for pid %li", pid);
1190 if (ps->num_lwp == 0)
1195 /* Leave the rest at zero if this is only a zombi */
1196 if (ps->num_proc == 0) {
1197 DEBUG("processes plugin: This is only a zombie: pid = %li; "
1203 cpu_user_counter = atoll(fields[11]);
1204 cpu_system_counter = atoll(fields[12]);
1205 vmem_size = atoll(fields[20]);
1206 vmem_rss = atoll(fields[21]);
1207 ps->vmem_minflt_counter = atol(fields[7]);
1208 ps->vmem_majflt_counter = atol(fields[9]);
1211 unsigned long long stack_start = atoll(fields[25]);
1212 unsigned long long stack_ptr = atoll(fields[26]);
1214 stack_size = (stack_start > stack_ptr) ? stack_start - stack_ptr
1215 : stack_ptr - stack_start;
1218 /* Convert jiffies to useconds */
1219 cpu_user_counter = cpu_user_counter * 1000000 / CONFIG_HZ;
1220 cpu_system_counter = cpu_system_counter * 1000000 / CONFIG_HZ;
1221 vmem_rss = vmem_rss * pagesize_g;
1223 ps->cpu_user_counter = cpu_user_counter;
1224 ps->cpu_system_counter = cpu_system_counter;
1225 ps->vmem_size = (unsigned long)vmem_size;
1226 ps->vmem_rss = (unsigned long)vmem_rss;
1227 ps->stack_size = (unsigned long)stack_size;
1229 /* no data by default. May be filled by ps_fill_details () */
1237 ps->cswitch_vol = -1;
1238 ps->cswitch_invol = -1;
1242 } /* int ps_read_process (...) */
1244 static char *ps_get_cmdline(long pid, char *name, char *buf, size_t buf_len) {
1248 char file[PATH_MAX];
1253 if ((pid < 1) || (NULL == buf) || (buf_len < 2))
1256 snprintf(file, sizeof(file), "/proc/%li/cmdline", pid);
1259 fd = open(file, O_RDONLY);
1262 /* ENOENT means the process exited while we were handling it.
1263 * Don't complain about this, it only fills the logs. */
1264 if (errno != ENOENT)
1265 WARNING("processes plugin: Failed to open `%s': %s.", file,
1266 sstrerror(errno, errbuf, sizeof(errbuf)));
1278 status = read(fd, (void *)buf_ptr, len);
1283 if ((EAGAIN == errno) || (EINTR == errno))
1286 WARNING("processes plugin: Failed to read from `%s': %s.", file,
1287 sstrerror(errno, errbuf, sizeof(errbuf)));
1307 /* cmdline not available; e.g. kernel thread, zombie */
1311 snprintf(buf, buf_len, "[%s]", name);
1315 assert(n <= buf_len);
1322 /* remove trailing whitespace */
1323 while ((n > 0) && (isspace(buf[n]) || ('\0' == buf[n]))) {
1328 /* arguments are separated by '\0' in /proc/<pid>/cmdline */
1335 } /* char *ps_get_cmdline (...) */
1337 static int read_fork_rate(void) {
1341 _Bool value_valid = 0;
1343 proc_stat = fopen("/proc/stat", "r");
1344 if (proc_stat == NULL) {
1346 ERROR("processes plugin: fopen (/proc/stat) failed: %s",
1347 sstrerror(errno, errbuf, sizeof(errbuf)));
1351 while (fgets(buffer, sizeof(buffer), proc_stat) != NULL) {
1356 fields_num = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
1357 if (fields_num != 2)
1360 if (strcmp("processes", fields[0]) != 0)
1363 status = parse_value(fields[1], &value, DS_TYPE_DERIVE);
1374 ps_submit_fork_rate(value.derive);
1377 #endif /*KERNEL_LINUX */
1380 static char *ps_get_cmdline(long pid,
1381 char *name __attribute__((unused)), /* {{{ */
1382 char *buffer, size_t buffer_size) {
1383 char path[PATH_MAX];
1387 snprintf(path, sizeof(path), "/proc/%li/psinfo", pid);
1389 status = read_file_contents(path, (void *)&info, sizeof(info));
1390 if ((status < 0) || (((size_t)status) != sizeof(info))) {
1391 ERROR("processes plugin: Unexpected return value "
1392 "while reading \"%s\": "
1393 "Returned %zd but expected %zu.",
1394 path, status, buffer_size);
1398 info.pr_psargs[sizeof(info.pr_psargs) - 1] = 0;
1399 sstrncpy(buffer, info.pr_psargs, buffer_size);
1402 } /* }}} int ps_get_cmdline */
1405 * Reads process information on the Solaris OS. The information comes mainly
1407 * /proc/PID/status, /proc/PID/psinfo and /proc/PID/usage
1408 * The values for input and ouput chars are calculated "by hand"
1409 * Added a few "solaris" specific process states as well
1411 static int ps_read_process(long pid, process_entry_t *ps, char *state) {
1413 char f_psinfo[64], f_usage[64];
1416 pstatus_t *myStatus;
1420 snprintf(filename, sizeof(filename), "/proc/%li/status", pid);
1421 snprintf(f_psinfo, sizeof(f_psinfo), "/proc/%li/psinfo", pid);
1422 snprintf(f_usage, sizeof(f_usage), "/proc/%li/usage", pid);
1424 buffer = calloc(1, sizeof(pstatus_t));
1425 read_file_contents(filename, buffer, sizeof(pstatus_t));
1426 myStatus = (pstatus_t *)buffer;
1428 buffer = calloc(1, sizeof(psinfo_t));
1429 read_file_contents(f_psinfo, buffer, sizeof(psinfo_t));
1430 myInfo = (psinfo_t *)buffer;
1432 buffer = calloc(1, sizeof(prusage_t));
1433 read_file_contents(f_usage, buffer, sizeof(prusage_t));
1434 myUsage = (prusage_t *)buffer;
1436 sstrncpy(ps->name, myInfo->pr_fname, sizeof(myInfo->pr_fname));
1437 ps->num_lwp = myStatus->pr_nlwp;
1438 if (myInfo->pr_wstat != 0) {
1449 ps->num_lwp = myInfo->pr_nlwp;
1453 * Convert system time and user time from nanoseconds to microseconds
1454 * for compatibility with the linux module
1456 ps->cpu_system_counter = myStatus->pr_stime.tv_nsec / 1000;
1457 ps->cpu_user_counter = myStatus->pr_utime.tv_nsec / 1000;
1460 * Convert rssize from KB to bytes to be consistent w/ the linux module
1462 ps->vmem_rss = myInfo->pr_rssize * 1024;
1463 ps->vmem_size = myInfo->pr_size * 1024;
1464 ps->vmem_minflt_counter = myUsage->pr_minf;
1465 ps->vmem_majflt_counter = myUsage->pr_majf;
1468 * TODO: Data and code segment calculations for Solaris
1473 ps->stack_size = myStatus->pr_stksize;
1476 * TODO: File descriptor count for Solaris
1480 /* Number of memory mappings */
1484 * Calculating input/ouput chars
1485 * Formula used is total chars / total blocks => chars/block
1486 * then convert input/output blocks to chars
1488 ulong_t tot_chars = myUsage->pr_ioch;
1489 ulong_t tot_blocks = myUsage->pr_inblk + myUsage->pr_oublk;
1490 ulong_t chars_per_block = 1;
1491 if (tot_blocks != 0)
1492 chars_per_block = tot_chars / tot_blocks;
1493 ps->io_rchar = myUsage->pr_inblk * chars_per_block;
1494 ps->io_wchar = myUsage->pr_oublk * chars_per_block;
1495 ps->io_syscr = myUsage->pr_sysc;
1496 ps->io_syscw = myUsage->pr_sysc;
1501 * TODO: context switch counters for Solaris
1503 ps->cswitch_vol = -1;
1504 ps->cswitch_invol = -1;
1507 * TODO: Find way of setting BLOCKED and PAGING status
1511 if (myStatus->pr_flags & PR_ASLEEP)
1513 else if (myStatus->pr_flags & PR_STOPPED)
1515 else if (myStatus->pr_flags & PR_DETACH)
1517 else if (myStatus->pr_flags & PR_DAEMON)
1519 else if (myStatus->pr_flags & PR_ISSYS)
1521 else if (myStatus->pr_flags & PR_ORPHAN)
1532 * Reads the number of threads created since the last reboot. On Solaris these
1533 * are retrieved from kstat (module cpu, name sys, class misc, stat nthreads).
1534 * The result is the sum for all the threads created on each cpu
1536 static int read_fork_rate(void) {
1537 extern kstat_ctl_t *kc;
1538 derive_t result = 0;
1543 for (kstat_t *ksp_chain = kc->kc_chain; ksp_chain != NULL;
1544 ksp_chain = ksp_chain->ks_next) {
1545 if ((strcmp(ksp_chain->ks_module, "cpu") == 0) &&
1546 (strcmp(ksp_chain->ks_name, "sys") == 0) &&
1547 (strcmp(ksp_chain->ks_class, "misc") == 0)) {
1550 kstat_read(kc, ksp_chain, NULL);
1552 tmp = get_kstat_value(ksp_chain, "nthreads");
1558 ps_submit_fork_rate(result);
1561 #endif /* KERNEL_SOLARIS */
1563 #if HAVE_THREAD_INFO
1564 static int mach_get_task_name(task_t t, int *pid, char *name,
1565 size_t name_max_len) {
1568 struct kinfo_proc kp;
1573 mib[2] = KERN_PROC_PID;
1575 if (pid_for_task(t, pid) != KERN_SUCCESS)
1579 kp_size = sizeof(kp);
1580 if (sysctl(mib, 4, &kp, &kp_size, NULL, 0) != 0)
1583 if (name_max_len > (MAXCOMLEN + 1))
1584 name_max_len = MAXCOMLEN + 1;
1586 strncpy(name, kp.kp_proc.p_comm, name_max_len - 1);
1587 name[name_max_len - 1] = '\0';
1589 DEBUG("pid = %i; name = %s;", *pid, name);
1591 /* We don't do the special handling for `p_comm == "LaunchCFMApp"' as
1592 * `top' does it, because it is a lot of work and only used when
1593 * debugging. -octo */
1597 #endif /* HAVE_THREAD_INFO */
1598 /* ------- end of additional functions for KERNEL_LINUX/HAVE_THREAD_INFO -------
1601 /* do actual readings from kernel */
1602 static int ps_read(void) {
1603 #if HAVE_THREAD_INFO
1604 kern_return_t status;
1606 processor_set_t port_pset_priv;
1608 task_array_t task_list;
1609 mach_msg_type_number_t task_list_len;
1612 char task_name[MAXCOMLEN + 1];
1614 thread_act_array_t thread_list;
1615 mach_msg_type_number_t thread_list_len;
1616 thread_basic_info_data_t thread_data;
1617 mach_msg_type_number_t thread_data_len;
1626 process_entry_t pse;
1631 * The Mach-concept is a little different from the traditional UNIX
1632 * concept: All the work is done in threads. Threads are contained in
1633 * `tasks'. Therefore, `task status' doesn't make much sense, since
1634 * it's actually a `thread status'.
1635 * Tasks are assigned to sets of processors, so that's where you go to
1638 for (mach_msg_type_number_t pset = 0; pset < pset_list_len; pset++) {
1639 if ((status = host_processor_set_priv(port_host_self, pset_list[pset],
1640 &port_pset_priv)) != KERN_SUCCESS) {
1641 ERROR("host_processor_set_priv failed: %s\n", mach_error_string(status));
1645 if ((status = processor_set_tasks(port_pset_priv, &task_list,
1646 &task_list_len)) != KERN_SUCCESS) {
1647 ERROR("processor_set_tasks failed: %s\n", mach_error_string(status));
1648 mach_port_deallocate(port_task_self, port_pset_priv);
1652 for (mach_msg_type_number_t task = 0; task < task_list_len; task++) {
1654 if (mach_get_task_name(task_list[task], &task_pid, task_name,
1655 PROCSTAT_NAME_LEN) == 0) {
1656 /* search for at least one match */
1657 for (ps = list_head_g; ps != NULL; ps = ps->next)
1658 /* FIXME: cmdline should be here instead of NULL */
1659 if (ps_list_match(task_name, NULL, ps) == 1)
1663 /* Collect more detailed statistics for this process */
1665 task_basic_info_data_t task_basic_info;
1666 mach_msg_type_number_t task_basic_info_len;
1667 task_events_info_data_t task_events_info;
1668 mach_msg_type_number_t task_events_info_len;
1669 task_absolutetime_info_data_t task_absolutetime_info;
1670 mach_msg_type_number_t task_absolutetime_info_len;
1672 memset(&pse, '\0', sizeof(pse));
1675 task_basic_info_len = TASK_BASIC_INFO_COUNT;
1676 status = task_info(task_list[task], TASK_BASIC_INFO,
1677 (task_info_t)&task_basic_info, &task_basic_info_len);
1678 if (status != KERN_SUCCESS) {
1679 ERROR("task_info failed: %s", mach_error_string(status));
1680 continue; /* with next thread_list */
1683 task_events_info_len = TASK_EVENTS_INFO_COUNT;
1685 task_info(task_list[task], TASK_EVENTS_INFO,
1686 (task_info_t)&task_events_info, &task_events_info_len);
1687 if (status != KERN_SUCCESS) {
1688 ERROR("task_info failed: %s", mach_error_string(status));
1689 continue; /* with next thread_list */
1692 task_absolutetime_info_len = TASK_ABSOLUTETIME_INFO_COUNT;
1693 status = task_info(task_list[task], TASK_ABSOLUTETIME_INFO,
1694 (task_info_t)&task_absolutetime_info,
1695 &task_absolutetime_info_len);
1696 if (status != KERN_SUCCESS) {
1697 ERROR("task_info failed: %s", mach_error_string(status));
1698 continue; /* with next thread_list */
1702 pse.vmem_size = task_basic_info.virtual_size;
1703 pse.vmem_rss = task_basic_info.resident_size;
1704 /* Does not seem to be easily exposed */
1715 /* File descriptor count not implemented */
1718 /* Number of memory mappings */
1721 pse.vmem_minflt_counter = task_events_info.cow_faults;
1722 pse.vmem_majflt_counter = task_events_info.faults;
1724 pse.cpu_user_counter = task_absolutetime_info.total_user;
1725 pse.cpu_system_counter = task_absolutetime_info.total_system;
1727 /* context switch counters not implemented */
1728 pse.cswitch_vol = -1;
1729 pse.cswitch_invol = -1;
1732 status = task_threads(task_list[task], &thread_list, &thread_list_len);
1733 if (status != KERN_SUCCESS) {
1734 /* Apple's `top' treats this case a zombie. It
1735 * makes sense to some extend: A `zombie'
1736 * thread is nonsense, since the task/process
1739 DEBUG("task_threads failed: %s", mach_error_string(status));
1740 if (task_list[task] != port_task_self)
1741 mach_port_deallocate(port_task_self, task_list[task]);
1742 continue; /* with next task_list */
1745 for (mach_msg_type_number_t thread = 0; thread < thread_list_len;
1747 thread_data_len = THREAD_BASIC_INFO_COUNT;
1748 status = thread_info(thread_list[thread], THREAD_BASIC_INFO,
1749 (thread_info_t)&thread_data, &thread_data_len);
1750 if (status != KERN_SUCCESS) {
1751 ERROR("thread_info failed: %s", mach_error_string(status));
1752 if (task_list[task] != port_task_self)
1753 mach_port_deallocate(port_task_self, thread_list[thread]);
1754 continue; /* with next thread_list */
1760 switch (thread_data.run_state) {
1761 case TH_STATE_RUNNING:
1764 case TH_STATE_STOPPED:
1765 /* What exactly is `halted'? */
1766 case TH_STATE_HALTED:
1769 case TH_STATE_WAITING:
1772 case TH_STATE_UNINTERRUPTIBLE:
1775 /* There is no `zombie' case here,
1776 * since there are no zombie-threads.
1777 * There's only zombie tasks, which are
1780 WARNING("Unknown thread status: %i", thread_data.run_state);
1782 } /* switch (thread_data.run_state) */
1784 if (task_list[task] != port_task_self) {
1785 status = mach_port_deallocate(port_task_self, thread_list[thread]);
1786 if (status != KERN_SUCCESS)
1787 ERROR("mach_port_deallocate failed: %s", mach_error_string(status));
1789 } /* for (thread_list) */
1791 if ((status = vm_deallocate(port_task_self, (vm_address_t)thread_list,
1792 thread_list_len * sizeof(thread_act_t))) !=
1794 ERROR("vm_deallocate failed: %s", mach_error_string(status));
1797 thread_list_len = 0;
1799 /* Only deallocate the task port, if it isn't our own.
1800 * Don't know what would happen in that case, but this
1801 * is what Apple's top does.. ;) */
1802 if (task_list[task] != port_task_self) {
1803 status = mach_port_deallocate(port_task_self, task_list[task]);
1804 if (status != KERN_SUCCESS)
1805 ERROR("mach_port_deallocate failed: %s", mach_error_string(status));
1809 /* FIXME: cmdline should be here instead of NULL */
1810 ps_list_add(task_name, NULL, &pse);
1811 } /* for (task_list) */
1813 if ((status = vm_deallocate(port_task_self, (vm_address_t)task_list,
1814 task_list_len * sizeof(task_t))) !=
1816 ERROR("vm_deallocate failed: %s", mach_error_string(status));
1821 if ((status = mach_port_deallocate(port_task_self, port_pset_priv)) !=
1823 ERROR("mach_port_deallocate failed: %s", mach_error_string(status));
1825 } /* for (pset_list) */
1827 ps_submit_state("running", running);
1828 ps_submit_state("sleeping", sleeping);
1829 ps_submit_state("zombies", zombies);
1830 ps_submit_state("stopped", stopped);
1831 ps_submit_state("blocked", blocked);
1833 for (ps = list_head_g; ps != NULL; ps = ps->next)
1834 ps_submit_proc_list(ps);
1835 /* #endif HAVE_THREAD_INFO */
1849 char cmdline[CMDLINE_BUFFER_SIZE];
1852 process_entry_t pse;
1855 running = sleeping = zombies = stopped = paging = blocked = 0;
1858 if ((proc = opendir("/proc")) == NULL) {
1860 ERROR("Cannot open `/proc': %s", sstrerror(errno, errbuf, sizeof(errbuf)));
1864 while ((ent = readdir(proc)) != NULL) {
1865 if (!isdigit(ent->d_name[0]))
1868 if ((pid = atol(ent->d_name)) < 1)
1871 memset(&pse, 0, sizeof(pse));
1874 status = ps_read_process(pid, &pse, &state);
1876 DEBUG("ps_read_process failed: %i", status);
1901 ps_list_add(pse.name,
1902 ps_get_cmdline(pid, pse.name, cmdline, sizeof(cmdline)), &pse);
1907 ps_submit_state("running", running);
1908 ps_submit_state("sleeping", sleeping);
1909 ps_submit_state("zombies", zombies);
1910 ps_submit_state("stopped", stopped);
1911 ps_submit_state("paging", paging);
1912 ps_submit_state("blocked", blocked);
1914 for (procstat_t *ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1915 ps_submit_proc_list(ps_ptr);
1918 /* #endif KERNEL_LINUX */
1920 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD
1930 char errbuf[_POSIX2_LINE_MAX];
1931 struct kinfo_proc *procs; /* array of processes */
1932 struct kinfo_proc *proc_ptr = NULL;
1933 int count; /* returns number of processes */
1935 process_entry_t pse;
1939 /* Open the kvm interface, get a descriptor */
1940 kd = kvm_openfiles(NULL, "/dev/null", NULL, 0, errbuf);
1942 ERROR("processes plugin: Cannot open kvm interface: %s", errbuf);
1946 /* Get the list of processes. */
1947 procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, &count);
1948 if (procs == NULL) {
1949 ERROR("processes plugin: Cannot get kvm processes list: %s",
1955 /* Iterate through the processes in kinfo_proc */
1956 for (int i = 0; i < count; i++) {
1957 /* Create only one process list entry per _process_, i.e.
1958 * filter out threads (duplicate PID entries). */
1959 if ((proc_ptr == NULL) || (proc_ptr->ki_pid != procs[i].ki_pid)) {
1960 char cmdline[CMDLINE_BUFFER_SIZE] = "";
1961 _Bool have_cmdline = 0;
1963 proc_ptr = &(procs[i]);
1964 /* Don't probe system processes and processes without arguments */
1965 if (((procs[i].ki_flag & P_SYSTEM) == 0) && (procs[i].ki_args != NULL)) {
1970 /* retrieve the arguments */
1971 argv = kvm_getargv(kd, proc_ptr, /* nchr = */ 0);
1973 if ((argv != NULL) && (argv[0] != NULL)) {
1974 while (argv[argc] != NULL)
1977 status = strjoin(cmdline, sizeof(cmdline), argv, argc, " ");
1979 WARNING("processes plugin: Command line did not fit into buffer.");
1983 } /* if (process has argument list) */
1985 memset(&pse, 0, sizeof(pse));
1986 pse.id = procs[i].ki_pid;
1989 pse.num_lwp = procs[i].ki_numthreads;
1991 pse.vmem_size = procs[i].ki_size;
1992 pse.vmem_rss = procs[i].ki_rssize * pagesize;
1993 pse.vmem_data = procs[i].ki_dsize * pagesize;
1994 pse.vmem_code = procs[i].ki_tsize * pagesize;
1995 pse.stack_size = procs[i].ki_ssize * pagesize;
1996 pse.vmem_minflt_counter = procs[i].ki_rusage.ru_minflt;
1997 pse.vmem_majflt_counter = procs[i].ki_rusage.ru_majflt;
1999 pse.cpu_user_counter = 0;
2000 pse.cpu_system_counter = 0;
2002 * The u-area might be swapped out, and we can't get
2003 * at it because we have a crashdump and no swap.
2004 * If it's here fill in these fields, otherwise, just
2007 if (procs[i].ki_flag & P_INMEM) {
2008 pse.cpu_user_counter = procs[i].ki_rusage.ru_utime.tv_usec +
2009 (1000000lu * procs[i].ki_rusage.ru_utime.tv_sec);
2010 pse.cpu_system_counter =
2011 procs[i].ki_rusage.ru_stime.tv_usec +
2012 (1000000lu * procs[i].ki_rusage.ru_stime.tv_sec);
2023 /* file descriptor count not implemented */
2026 /* Number of memory mappings */
2029 /* context switch counters not implemented */
2030 pse.cswitch_vol = -1;
2031 pse.cswitch_invol = -1;
2033 ps_list_add(procs[i].ki_comm, have_cmdline ? cmdline : NULL, &pse);
2035 switch (procs[i].ki_stat) {
2058 } /* if ((proc_ptr == NULL) || (proc_ptr->ki_pid != procs[i].ki_pid)) */
2063 ps_submit_state("running", running);
2064 ps_submit_state("sleeping", sleeping);
2065 ps_submit_state("zombies", zombies);
2066 ps_submit_state("stopped", stopped);
2067 ps_submit_state("blocked", blocked);
2068 ps_submit_state("idle", idle);
2069 ps_submit_state("wait", wait);
2071 for (procstat_t *ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
2072 ps_submit_proc_list(ps_ptr);
2073 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
2075 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_OPENBSD
2086 struct kinfo_proc *procs; /* array of processes */
2087 struct kinfo_proc *proc_ptr = NULL;
2088 int count; /* returns number of processes */
2090 process_entry_t pse;
2094 /* Open the kvm interface, get a descriptor */
2095 kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf);
2097 ERROR("processes plugin: Cannot open kvm interface: %s", errbuf);
2101 /* Get the list of processes. */
2102 procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc), &count);
2103 if (procs == NULL) {
2104 ERROR("processes plugin: Cannot get kvm processes list: %s",
2110 /* Iterate through the processes in kinfo_proc */
2111 for (int i = 0; i < count; i++) {
2112 /* Create only one process list entry per _process_, i.e.
2113 * filter out threads (duplicate PID entries). */
2114 if ((proc_ptr == NULL) || (proc_ptr->p_pid != procs[i].p_pid)) {
2115 char cmdline[CMDLINE_BUFFER_SIZE] = "";
2116 _Bool have_cmdline = 0;
2118 proc_ptr = &(procs[i]);
2119 /* Don't probe zombie processes */
2120 if (!P_ZOMBIE(proc_ptr)) {
2125 /* retrieve the arguments */
2126 argv = kvm_getargv(kd, proc_ptr, /* nchr = */ 0);
2128 if ((argv != NULL) && (argv[0] != NULL)) {
2129 while (argv[argc] != NULL)
2132 status = strjoin(cmdline, sizeof(cmdline), argv, argc, " ");
2134 WARNING("processes plugin: Command line did not fit into buffer.");
2138 } /* if (process has argument list) */
2140 memset(&pse, 0, sizeof(pse));
2141 pse.id = procs[i].p_pid;
2144 pse.num_lwp = 1; /* XXX: accumulate p_tid values for a single p_pid ? */
2146 pse.vmem_rss = procs[i].p_vm_rssize * pagesize;
2147 pse.vmem_data = procs[i].p_vm_dsize * pagesize;
2148 pse.vmem_code = procs[i].p_vm_tsize * pagesize;
2149 pse.stack_size = procs[i].p_vm_ssize * pagesize;
2150 pse.vmem_size = pse.stack_size + pse.vmem_code + pse.vmem_data;
2151 pse.vmem_minflt_counter = procs[i].p_uru_minflt;
2152 pse.vmem_majflt_counter = procs[i].p_uru_majflt;
2154 pse.cpu_user_counter =
2155 procs[i].p_uutime_usec + (1000000lu * procs[i].p_uutime_sec);
2156 pse.cpu_system_counter =
2157 procs[i].p_ustime_usec + (1000000lu * procs[i].p_ustime_sec);
2167 /* file descriptor count not implemented */
2170 /* Number of memory mappings */
2173 /* context switch counters not implemented */
2174 pse.cswitch_vol = -1;
2175 pse.cswitch_invol = -1;
2177 ps_list_add(procs[i].p_comm, have_cmdline ? cmdline : NULL, &pse);
2179 switch (procs[i].p_stat) {
2202 } /* if ((proc_ptr == NULL) || (proc_ptr->p_pid != procs[i].p_pid)) */
2207 ps_submit_state("running", running);
2208 ps_submit_state("sleeping", sleeping);
2209 ps_submit_state("zombies", zombies);
2210 ps_submit_state("stopped", stopped);
2211 ps_submit_state("onproc", onproc);
2212 ps_submit_state("idle", idle);
2213 ps_submit_state("dead", dead);
2215 for (procstat_t *ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
2216 ps_submit_proc_list(ps_ptr);
2217 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_OPENBSD */
2219 #elif HAVE_PROCINFO_H
2231 process_entry_t pse;
2234 while ((nprocs = getprocs64(procentry, sizeof(struct procentry64),
2235 /* fdsinfo = */ NULL, sizeof(struct fdsinfo64),
2236 &pindex, MAXPROCENTRY)) > 0) {
2237 for (int i = 0; i < nprocs; i++) {
2240 char arglist[MAXARGLN + 1];
2244 if (procentry[i].pi_state == SNONE)
2246 /* if (procentry[i].pi_state == SZOMB) FIXME */
2248 cmdline = procentry[i].pi_comm;
2249 cargs = procentry[i].pi_comm;
2250 if (procentry[i].pi_flags & SKPROC) {
2251 if (procentry[i].pi_pid == 0)
2252 cmdline = "swapper";
2255 if (getargs(&procentry[i], sizeof(struct procentry64), arglist,
2260 while (++n < MAXARGLN) {
2261 if (arglist[n] == '\0') {
2262 if (arglist[n + 1] == '\0')
2271 memset(&pse, 0, sizeof(pse));
2273 pse.id = procentry[i].pi_pid;
2274 pse.num_lwp = procentry[i].pi_thcount;
2278 while ((nthreads = getthrds64(procentry[i].pi_pid, thrdentry,
2279 sizeof(struct thrdentry64), &thindex,
2280 MAXTHRDENTRY)) > 0) {
2283 for (j = 0; j < nthreads; j++) {
2284 switch (thrdentry[j].ti_state) {
2285 /* case TSNONE: break; */
2288 break; /* FIXME is really blocked */
2306 if (nthreads < MAXTHRDENTRY)
2310 /* tv_usec is nanosec ??? */
2311 pse.cpu_user_counter = procentry[i].pi_ru.ru_utime.tv_sec * 1000000 +
2312 procentry[i].pi_ru.ru_utime.tv_usec / 1000;
2314 /* tv_usec is nanosec ??? */
2315 pse.cpu_system_counter = procentry[i].pi_ru.ru_stime.tv_sec * 1000000 +
2316 procentry[i].pi_ru.ru_stime.tv_usec / 1000;
2318 pse.vmem_minflt_counter = procentry[i].pi_minflt;
2319 pse.vmem_majflt_counter = procentry[i].pi_majflt;
2321 pse.vmem_size = procentry[i].pi_tsize + procentry[i].pi_dvm * pagesize;
2322 pse.vmem_rss = (procentry[i].pi_drss + procentry[i].pi_trss) * pagesize;
2323 /* Not supported/implemented */
2338 pse.cswitch_vol = -1;
2339 pse.cswitch_invol = -1;
2341 ps_list_add(cmdline, cargs, &pse);
2342 } /* for (i = 0 .. nprocs) */
2344 if (nprocs < MAXPROCENTRY)
2346 } /* while (getprocs64() > 0) */
2347 ps_submit_state("running", running);
2348 ps_submit_state("sleeping", sleeping);
2349 ps_submit_state("zombies", zombies);
2350 ps_submit_state("stopped", stopped);
2351 ps_submit_state("paging", paging);
2352 ps_submit_state("blocked", blocked);
2354 for (procstat_t *ps = list_head_g; ps != NULL; ps = ps->next)
2355 ps_submit_proc_list(ps);
2356 /* #endif HAVE_PROCINFO_H */
2358 #elif KERNEL_SOLARIS
2360 * The Solaris section adds a few more process states and removes some
2361 * process states compared to linux. Most notably there is no "PAGING"
2362 * and "BLOCKED" state for a process. The rest is similar to the linux
2380 char cmdline[PRARGSZ];
2384 proc = opendir("/proc");
2388 while ((ent = readdir(proc)) != NULL) {
2390 process_entry_t pse;
2393 if (!isdigit((int)ent->d_name[0]))
2396 pid = strtol(ent->d_name, &endptr, 10);
2397 if (*endptr != 0) /* value didn't completely parse as a number */
2400 memset(&pse, 0, sizeof(pse));
2403 status = ps_read_process(pid, &pse, &state);
2405 DEBUG("ps_read_process failed: %i", status);
2436 ps_list_add(pse.name,
2437 ps_get_cmdline(pid, pse.name, cmdline, sizeof(cmdline)), &pse);
2438 } /* while(readdir) */
2441 ps_submit_state("running", running);
2442 ps_submit_state("sleeping", sleeping);
2443 ps_submit_state("zombies", zombies);
2444 ps_submit_state("stopped", stopped);
2445 ps_submit_state("detached", detached);
2446 ps_submit_state("daemon", daemon);
2447 ps_submit_state("system", system);
2448 ps_submit_state("orphan", orphan);
2450 for (procstat_t *ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
2451 ps_submit_proc_list(ps_ptr);
2454 #endif /* KERNEL_SOLARIS */
2461 void module_register(void) {
2462 plugin_register_complex_config("processes", ps_config);
2463 plugin_register_init("processes", ps_init);
2464 plugin_register_read("processes", ps_read);
2465 } /* void module_register */