2 * collectd - src/processes.c
3 * Copyright (C) 2005 Lyonel Vincent
4 * Copyright (C) 2006-2017 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>
45 #include "utils_complain.h"
46 #include "utils_taskstats.h"
49 /* Include header files for the mach system, if they exist.. */
51 #if HAVE_MACH_MACH_INIT_H
52 #include <mach/mach_init.h>
54 #if HAVE_MACH_HOST_PRIV_H
55 #include <mach/host_priv.h>
57 #if HAVE_MACH_MACH_ERROR_H
58 #include <mach/mach_error.h>
60 #if HAVE_MACH_MACH_HOST_H
61 #include <mach/mach_host.h>
63 #if HAVE_MACH_MACH_PORT_H
64 #include <mach/mach_port.h>
66 #if HAVE_MACH_MACH_TYPES_H
67 #include <mach/mach_types.h>
69 #if HAVE_MACH_MESSAGE_H
70 #include <mach/message.h>
72 #if HAVE_MACH_PROCESSOR_SET_H
73 #include <mach/processor_set.h>
76 #include <mach/task.h>
78 #if HAVE_MACH_THREAD_ACT_H
79 #include <mach/thread_act.h>
81 #if HAVE_MACH_VM_REGION_H
82 #include <mach/vm_region.h>
84 #if HAVE_MACH_VM_MAP_H
85 #include <mach/vm_map.h>
87 #if HAVE_MACH_VM_PROT_H
88 #include <mach/vm_prot.h>
91 #include <sys/sysctl.h>
93 /* #endif HAVE_THREAD_INFO */
96 #if HAVE_LINUX_CONFIG_H
97 #include <linux/config.h>
100 #define CONFIG_HZ 100
102 /* #endif KERNEL_LINUX */
104 #elif HAVE_LIBKVM_GETPROCS && \
105 (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD)
107 #include <sys/param.h>
108 #include <sys/proc.h>
109 #include <sys/sysctl.h>
110 #include <sys/user.h>
111 /* #endif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD ||
112 * HAVE_STRUCT_KINFO_PROC_OPENBSD) */
114 #elif HAVE_PROCINFO_H
115 #include <procinfo.h>
116 #include <sys/types.h>
118 #define MAXPROCENTRY 32
119 #define MAXTHRDENTRY 16
120 #define MAXARGLN 1024
121 /* #endif HAVE_PROCINFO_H */
124 /* Hack: Avoid #error when building a 32-bit binary with
125 * _FILE_OFFSET_BITS=64. There is a reason for this #error, as one
126 * of the structures in <sys/procfs.h> uses an off_t, but that
127 * isn't relevant to our usage of procfs. */
128 #if !defined(_LP64) && _FILE_OFFSET_BITS == 64
130 #undef _FILE_OFFSET_BITS
136 #define _FILE_OFFSET_BITS 64
141 #include <sys/user.h>
147 /* #endif KERNEL_SOLARIS */
150 #error "No applicable input method."
161 #ifdef HAVE_SYS_CAPABILITY_H
162 #include <sys/capability.h>
165 #ifndef CMDLINE_BUFFER_SIZE
166 #if defined(ARG_MAX) && (ARG_MAX < 4096)
167 #define CMDLINE_BUFFER_SIZE ARG_MAX
169 #define CMDLINE_BUFFER_SIZE 4096
173 #define PROCSTAT_NAME_LEN 256
174 typedef struct process_entry_s {
176 char name[PROCSTAT_NAME_LEN];
178 unsigned long num_proc;
179 unsigned long num_lwp;
180 unsigned long num_fd;
181 unsigned long num_maps;
182 unsigned long vmem_size;
183 unsigned long vmem_rss;
184 unsigned long vmem_data;
185 unsigned long vmem_code;
186 unsigned long stack_size;
188 derive_t vmem_minflt_counter;
189 derive_t vmem_majflt_counter;
191 derive_t cpu_user_counter;
192 derive_t cpu_system_counter;
203 derive_t cswitch_vol;
204 derive_t cswitch_invol;
207 #if HAVE_LIBTASKSTATS
217 typedef struct procstat_entry_s {
221 derive_t vmem_minflt_counter;
222 derive_t vmem_majflt_counter;
224 derive_t cpu_user_counter;
225 derive_t cpu_system_counter;
235 derive_t cswitch_vol;
236 derive_t cswitch_invol;
238 #if HAVE_LIBTASKSTATS
239 value_to_rate_state_t delay_cpu;
240 value_to_rate_state_t delay_blkio;
241 value_to_rate_state_t delay_swapin;
242 value_to_rate_state_t delay_freepages;
245 struct procstat_entry_s *next;
248 typedef struct procstat {
249 char name[PROCSTAT_NAME_LEN];
254 unsigned long num_proc;
255 unsigned long num_lwp;
256 unsigned long num_fd;
257 unsigned long num_maps;
258 unsigned long vmem_size;
259 unsigned long vmem_rss;
260 unsigned long vmem_data;
261 unsigned long vmem_code;
262 unsigned long stack_size;
264 derive_t vmem_minflt_counter;
265 derive_t vmem_majflt_counter;
267 derive_t cpu_user_counter;
268 derive_t cpu_system_counter;
278 derive_t cswitch_vol;
279 derive_t cswitch_invol;
281 /* Linux Delay Accounting. Unit is ns/s. */
284 gauge_t delay_swapin;
285 gauge_t delay_freepages;
288 _Bool report_maps_num;
289 _Bool report_ctx_switch;
292 struct procstat *next;
293 struct procstat_entry_s *instances;
296 static procstat_t *list_head_g = NULL;
298 static _Bool want_init = 1;
299 static _Bool report_ctx_switch = 0;
300 static _Bool report_fd_num = 0;
301 static _Bool report_maps_num = 0;
302 static _Bool report_delay = 0;
305 static mach_port_t port_host_self;
306 static mach_port_t port_task_self;
308 static processor_set_name_array_t pset_list;
309 static mach_msg_type_number_t pset_list_len;
310 /* #endif HAVE_THREAD_INFO */
313 static long pagesize_g;
314 static void ps_fill_details(const procstat_t *ps, process_entry_t *entry);
315 /* #endif KERNEL_LINUX */
317 #elif HAVE_LIBKVM_GETPROCS && \
318 (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD)
320 /* #endif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD ||
321 * HAVE_STRUCT_KINFO_PROC_OPENBSD) */
323 #elif HAVE_PROCINFO_H
324 static struct procentry64 procentry[MAXPROCENTRY];
325 static struct thrdentry64 thrdentry[MAXTHRDENTRY];
328 #ifndef _AIXVERSION_610
329 int getprocs64(void *procsinfo, int sizproc, void *fdsinfo, int sizfd,
330 pid_t *index, int count);
331 int getthrds64(pid_t, void *, int, tid64_t *, int);
333 int getargs(void *processBuffer, int bufferLen, char *argsBuffer, int argsLen);
334 #endif /* HAVE_PROCINFO_H */
336 #if HAVE_LIBTASKSTATS
337 static ts_t *taskstats_handle = NULL;
340 /* put name of process from config to list_head_g tree
341 * list_head_g is a list of 'procstat_t' structs with
342 * processes names we want to watch */
343 static procstat_t *ps_list_register(const char *name, const char *regexp) {
348 new = calloc(1, sizeof(*new));
350 ERROR("processes plugin: ps_list_register: calloc failed.");
353 sstrncpy(new->name, name, sizeof(new->name));
361 new->cswitch_vol = -1;
362 new->cswitch_invol = -1;
364 new->report_fd_num = report_fd_num;
365 new->report_maps_num = report_maps_num;
366 new->report_ctx_switch = report_ctx_switch;
367 new->report_delay = report_delay;
370 if (regexp != NULL) {
371 DEBUG("ProcessMatch: adding \"%s\" as criteria to process %s.", regexp,
373 new->re = malloc(sizeof(*new->re));
374 if (new->re == NULL) {
375 ERROR("processes plugin: ps_list_register: malloc failed.");
380 status = regcomp(new->re, regexp, REG_EXTENDED | REG_NOSUB);
382 DEBUG("ProcessMatch: compiling the regular expression \"%s\" failed.",
390 if (regexp != NULL) {
391 ERROR("processes plugin: ps_list_register: "
392 "Regular expression \"%s\" found in config "
393 "file, but support for regular expressions "
394 "has been disabled at compile time.",
401 for (ptr = list_head_g; ptr != NULL; ptr = ptr->next) {
402 if (strcmp(ptr->name, name) == 0) {
403 WARNING("processes plugin: You have configured more "
404 "than one `Process' or "
405 "`ProcessMatch' with the same name. "
406 "All but the first setting will be "
415 if (ptr->next == NULL)
425 } /* void ps_list_register */
427 /* try to match name against entry, returns 1 if success */
428 static int ps_list_match(const char *name, const char *cmdline,
431 if (ps->re != NULL) {
436 if ((str == NULL) || (str[0] == 0))
441 status = regexec(ps->re, str,
449 if (strcmp(ps->name, name) == 0)
453 } /* int ps_list_match */
455 static void ps_update_counter(derive_t *group_counter, derive_t *curr_counter,
456 derive_t new_counter) {
457 unsigned long curr_value;
460 *curr_counter = new_counter;
464 if (new_counter < *curr_counter)
465 curr_value = new_counter + (ULONG_MAX - *curr_counter);
467 curr_value = new_counter - *curr_counter;
469 if (*group_counter == -1)
472 *curr_counter = new_counter;
473 *group_counter += curr_value;
476 #if HAVE_LIBTASKSTATS
477 static void ps_update_delay_one(gauge_t *out_rate_sum,
478 value_to_rate_state_t *state, uint64_t cnt,
481 int status = value_to_rate(&rate, (value_t){.counter = (counter_t)cnt},
482 DS_TYPE_COUNTER, t, state);
483 if ((status != 0) || isnan(rate)) {
487 if (isnan(*out_rate_sum)) {
488 *out_rate_sum = rate;
490 *out_rate_sum += rate;
494 static void ps_update_delay(procstat_t *out, procstat_entry_t *prev,
495 process_entry_t *curr) {
496 cdtime_t now = cdtime();
498 ps_update_delay_one(&out->delay_cpu, &prev->delay_cpu, curr->delay.cpu_ns,
500 ps_update_delay_one(&out->delay_blkio, &prev->delay_blkio,
501 curr->delay.blkio_ns, now);
502 ps_update_delay_one(&out->delay_swapin, &prev->delay_swapin,
503 curr->delay.swapin_ns, now);
504 ps_update_delay_one(&out->delay_freepages, &prev->delay_freepages,
505 curr->delay.freepages_ns, now);
509 /* add process entry to 'instances' of process 'name' (or refresh it) */
510 static void ps_list_add(const char *name, const char *cmdline,
511 process_entry_t *entry) {
512 procstat_entry_t *pse;
517 for (procstat_t *ps = list_head_g; ps != NULL; ps = ps->next) {
518 if ((ps_list_match(name, cmdline, ps)) == 0)
522 ps_fill_details(ps, entry);
525 for (pse = ps->instances; pse != NULL; pse = pse->next)
526 if ((pse->id == entry->id) || (pse->next == NULL))
529 if ((pse == NULL) || (pse->id != entry->id)) {
530 procstat_entry_t *new;
532 new = calloc(1, sizeof(*new));
547 ps->num_proc += entry->num_proc;
548 ps->num_lwp += entry->num_lwp;
549 ps->num_fd += entry->num_fd;
550 ps->num_maps += entry->num_maps;
551 ps->vmem_size += entry->vmem_size;
552 ps->vmem_rss += entry->vmem_rss;
553 ps->vmem_data += entry->vmem_data;
554 ps->vmem_code += entry->vmem_code;
555 ps->stack_size += entry->stack_size;
557 if ((entry->io_rchar != -1) && (entry->io_wchar != -1)) {
558 ps_update_counter(&ps->io_rchar, &pse->io_rchar, entry->io_rchar);
559 ps_update_counter(&ps->io_wchar, &pse->io_wchar, entry->io_wchar);
562 if ((entry->io_syscr != -1) && (entry->io_syscw != -1)) {
563 ps_update_counter(&ps->io_syscr, &pse->io_syscr, entry->io_syscr);
564 ps_update_counter(&ps->io_syscw, &pse->io_syscw, entry->io_syscw);
567 if ((entry->io_diskr != -1) && (entry->io_diskw != -1)) {
568 ps_update_counter(&ps->io_diskr, &pse->io_diskr, entry->io_diskr);
569 ps_update_counter(&ps->io_diskw, &pse->io_diskw, entry->io_diskw);
572 if ((entry->cswitch_vol != -1) && (entry->cswitch_invol != -1)) {
573 ps_update_counter(&ps->cswitch_vol, &pse->cswitch_vol,
575 ps_update_counter(&ps->cswitch_invol, &pse->cswitch_invol,
576 entry->cswitch_invol);
579 ps_update_counter(&ps->vmem_minflt_counter, &pse->vmem_minflt_counter,
580 entry->vmem_minflt_counter);
581 ps_update_counter(&ps->vmem_majflt_counter, &pse->vmem_majflt_counter,
582 entry->vmem_majflt_counter);
584 ps_update_counter(&ps->cpu_user_counter, &pse->cpu_user_counter,
585 entry->cpu_user_counter);
586 ps_update_counter(&ps->cpu_system_counter, &pse->cpu_system_counter,
587 entry->cpu_system_counter);
589 #if HAVE_LIBTASKSTATS
590 ps_update_delay(ps, pse, entry);
595 /* remove old entries from instances of processes in list_head_g */
596 static void ps_list_reset(void) {
597 procstat_entry_t *pse;
598 procstat_entry_t *pse_prev;
600 for (procstat_t *ps = list_head_g; ps != NULL; ps = ps->next) {
612 ps->delay_blkio = NAN;
613 ps->delay_swapin = NAN;
614 ps->delay_freepages = NAN;
618 while (pse != NULL) {
620 DEBUG("Removing this procstat entry cause it's too old: "
621 "id = %lu; name = %s;",
624 if (pse_prev == NULL) {
625 ps->instances = pse->next;
629 pse_prev->next = pse->next;
631 pse = pse_prev->next;
638 } /* while (pse != NULL) */
639 } /* for (ps = list_head_g; ps != NULL; ps = ps->next) */
642 static void ps_tune_instance(oconfig_item_t *ci, procstat_t *ps) {
643 for (int i = 0; i < ci->children_num; i++) {
644 oconfig_item_t *c = ci->children + i;
646 if (strcasecmp(c->key, "CollectContextSwitch") == 0)
647 cf_util_get_boolean(c, &ps->report_ctx_switch);
648 else if (strcasecmp(c->key, "CollectFileDescriptor") == 0)
649 cf_util_get_boolean(c, &ps->report_fd_num);
650 else if (strcasecmp(c->key, "CollectMemoryMaps") == 0)
651 cf_util_get_boolean(c, &ps->report_maps_num);
652 else if (strcasecmp(c->key, "CollectDelayAccounting") == 0) {
653 #if HAVE_LIBTASKSTATS
654 cf_util_get_boolean(c, &ps->report_delay);
656 WARNING("processes plugin: The plugin has been compiled without support "
657 "for the \"CollectDelayAccounting\" option.");
660 ERROR("processes plugin: Option `%s' not allowed heeere.", c->key);
662 } /* for (ci->children) */
663 } /* void ps_tune_instance */
665 /* put all pre-defined 'Process' names from config to list_head_g tree */
666 static int ps_config(oconfig_item_t *ci) {
668 const size_t max_procname_len = 15;
669 #elif KERNEL_SOLARIS || KERNEL_FREEBSD
670 const size_t max_procname_len = MAXCOMLEN - 1;
675 for (int i = 0; i < ci->children_num; ++i) {
676 oconfig_item_t *c = ci->children + i;
678 if (strcasecmp(c->key, "Process") == 0) {
679 if ((c->values_num != 1) || (OCONFIG_TYPE_STRING != c->values[0].type)) {
680 ERROR("processes plugin: `Process' expects exactly "
681 "one string argument (got %i).",
686 #if KERNEL_LINUX || KERNEL_SOLARIS || KERNEL_FREEBSD
687 if (strlen(c->values[0].value.string) > max_procname_len) {
688 WARNING("processes plugin: this platform has a %zu character limit "
689 "to process names. The `Process \"%s\"' option will "
690 "not work as expected.",
691 max_procname_len, c->values[0].value.string);
695 ps = ps_list_register(c->values[0].value.string, NULL);
697 if (c->children_num != 0 && ps != NULL)
698 ps_tune_instance(c, ps);
699 } else if (strcasecmp(c->key, "ProcessMatch") == 0) {
700 if ((c->values_num != 2) || (OCONFIG_TYPE_STRING != c->values[0].type) ||
701 (OCONFIG_TYPE_STRING != c->values[1].type)) {
702 ERROR("processes plugin: `ProcessMatch' needs exactly "
703 "two string arguments (got %i).",
708 ps = ps_list_register(c->values[0].value.string,
709 c->values[1].value.string);
711 if (c->children_num != 0 && ps != NULL)
712 ps_tune_instance(c, ps);
713 } else if (strcasecmp(c->key, "CollectContextSwitch") == 0) {
714 cf_util_get_boolean(c, &report_ctx_switch);
715 } else if (strcasecmp(c->key, "CollectFileDescriptor") == 0) {
716 cf_util_get_boolean(c, &report_fd_num);
717 } else if (strcasecmp(c->key, "CollectMemoryMaps") == 0) {
718 cf_util_get_boolean(c, &report_maps_num);
719 } else if (strcasecmp(c->key, "CollectDelayAccounting") == 0) {
720 #if HAVE_LIBTASKSTATS
721 cf_util_get_boolean(c, &report_delay);
723 WARNING("processes plugin: The plugin has been compiled without support "
724 "for the \"CollectDelayAccounting\" option.");
727 ERROR("processes plugin: The `%s' configuration option is not "
728 "understood and will be ignored.",
737 static int ps_init(void) {
739 kern_return_t status;
741 port_host_self = mach_host_self();
742 port_task_self = mach_task_self();
744 if (pset_list != NULL) {
745 vm_deallocate(port_task_self, (vm_address_t)pset_list,
746 pset_list_len * sizeof(processor_set_t));
751 if ((status = host_processor_sets(port_host_self, &pset_list,
752 &pset_list_len)) != KERN_SUCCESS) {
753 ERROR("host_processor_sets failed: %s\n", mach_error_string(status));
758 /* #endif HAVE_THREAD_INFO */
761 pagesize_g = sysconf(_SC_PAGESIZE);
762 DEBUG("pagesize_g = %li; CONFIG_HZ = %i;", pagesize_g, CONFIG_HZ);
764 #if HAVE_LIBTASKSTATS
765 if (taskstats_handle == NULL) {
766 taskstats_handle = ts_create();
767 if (taskstats_handle == NULL) {
768 WARNING("processes plugin: Creating taskstats handle failed.");
772 /* #endif KERNEL_LINUX */
774 #elif HAVE_LIBKVM_GETPROCS && \
775 (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD)
776 pagesize = getpagesize();
777 /* #endif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD ||
778 * HAVE_STRUCT_KINFO_PROC_OPENBSD) */
780 #elif HAVE_PROCINFO_H
781 pagesize = getpagesize();
782 #endif /* HAVE_PROCINFO_H */
787 /* submit global state (e.g.: qty of zombies, running, etc..) */
788 static void ps_submit_state(const char *state, double value) {
789 value_list_t vl = VALUE_LIST_INIT;
791 vl.values = &(value_t){.gauge = value};
793 sstrncpy(vl.plugin, "processes", sizeof(vl.plugin));
794 sstrncpy(vl.plugin_instance, "", sizeof(vl.plugin_instance));
795 sstrncpy(vl.type, "ps_state", sizeof(vl.type));
796 sstrncpy(vl.type_instance, state, sizeof(vl.type_instance));
798 plugin_dispatch_values(&vl);
801 /* submit info about specific process (e.g.: memory taken, cpu usage, etc..) */
802 static void ps_submit_proc_list(procstat_t *ps) {
803 value_list_t vl = VALUE_LIST_INIT;
807 sstrncpy(vl.plugin, "processes", sizeof(vl.plugin));
808 sstrncpy(vl.plugin_instance, ps->name, sizeof(vl.plugin_instance));
810 sstrncpy(vl.type, "ps_vm", sizeof(vl.type));
811 vl.values[0].gauge = ps->vmem_size;
813 plugin_dispatch_values(&vl);
815 sstrncpy(vl.type, "ps_rss", sizeof(vl.type));
816 vl.values[0].gauge = ps->vmem_rss;
818 plugin_dispatch_values(&vl);
820 sstrncpy(vl.type, "ps_data", sizeof(vl.type));
821 vl.values[0].gauge = ps->vmem_data;
823 plugin_dispatch_values(&vl);
825 sstrncpy(vl.type, "ps_code", sizeof(vl.type));
826 vl.values[0].gauge = ps->vmem_code;
828 plugin_dispatch_values(&vl);
830 sstrncpy(vl.type, "ps_stacksize", sizeof(vl.type));
831 vl.values[0].gauge = ps->stack_size;
833 plugin_dispatch_values(&vl);
835 sstrncpy(vl.type, "ps_cputime", sizeof(vl.type));
836 vl.values[0].derive = ps->cpu_user_counter;
837 vl.values[1].derive = ps->cpu_system_counter;
839 plugin_dispatch_values(&vl);
841 sstrncpy(vl.type, "ps_count", sizeof(vl.type));
842 vl.values[0].gauge = ps->num_proc;
843 vl.values[1].gauge = ps->num_lwp;
845 plugin_dispatch_values(&vl);
847 sstrncpy(vl.type, "ps_pagefaults", sizeof(vl.type));
848 vl.values[0].derive = ps->vmem_minflt_counter;
849 vl.values[1].derive = ps->vmem_majflt_counter;
851 plugin_dispatch_values(&vl);
853 if ((ps->io_rchar != -1) && (ps->io_wchar != -1)) {
854 sstrncpy(vl.type, "io_octets", sizeof(vl.type));
855 vl.values[0].derive = ps->io_rchar;
856 vl.values[1].derive = ps->io_wchar;
858 plugin_dispatch_values(&vl);
861 if ((ps->io_syscr != -1) && (ps->io_syscw != -1)) {
862 sstrncpy(vl.type, "io_ops", sizeof(vl.type));
863 vl.values[0].derive = ps->io_syscr;
864 vl.values[1].derive = ps->io_syscw;
866 plugin_dispatch_values(&vl);
869 if ((ps->io_diskr != -1) && (ps->io_diskw != -1)) {
870 sstrncpy(vl.type, "disk_octets", sizeof(vl.type));
871 vl.values[0].derive = ps->io_diskr;
872 vl.values[1].derive = ps->io_diskw;
874 plugin_dispatch_values(&vl);
877 if (ps->num_fd > 0) {
878 sstrncpy(vl.type, "file_handles", sizeof(vl.type));
879 vl.values[0].gauge = ps->num_fd;
881 plugin_dispatch_values(&vl);
884 if (ps->num_maps > 0) {
885 sstrncpy(vl.type, "file_handles", sizeof(vl.type));
886 sstrncpy(vl.type_instance, "mapped", sizeof(vl.type_instance));
887 vl.values[0].gauge = ps->num_maps;
889 plugin_dispatch_values(&vl);
892 if ((ps->cswitch_vol != -1) && (ps->cswitch_invol != -1)) {
893 sstrncpy(vl.type, "contextswitch", sizeof(vl.type));
894 sstrncpy(vl.type_instance, "voluntary", sizeof(vl.type_instance));
895 vl.values[0].derive = ps->cswitch_vol;
897 plugin_dispatch_values(&vl);
899 sstrncpy(vl.type, "contextswitch", sizeof(vl.type));
900 sstrncpy(vl.type_instance, "involuntary", sizeof(vl.type_instance));
901 vl.values[0].derive = ps->cswitch_invol;
903 plugin_dispatch_values(&vl);
906 /* The ps->delay_* metrics are in nanoseconds per second. This factor converts
907 * them to a percentage. */
908 gauge_t const delay_factor = 100.0 / 1000000000.0;
913 } delay_metrics[] = {
914 {"cpu", ps->delay_cpu},
915 {"blkio", ps->delay_blkio},
916 {"swapin", ps->delay_swapin},
917 {"freepages", ps->delay_freepages},
919 for (size_t i = 0; i < STATIC_ARRAY_SIZE(delay_metrics); i++) {
920 if (isnan(delay_metrics[i].rate_ns)) {
923 sstrncpy(vl.type, "percent", sizeof(vl.type));
924 sstrncpy(vl.type_instance, delay_metrics[i].type_instance,
925 sizeof(vl.type_instance));
926 vl.values[0].gauge = delay_metrics[i].rate_ns * delay_factor;
928 plugin_dispatch_values(&vl);
932 "name = %s; num_proc = %lu; num_lwp = %lu; num_fd = %lu; num_maps = %lu; "
933 "vmem_size = %lu; vmem_rss = %lu; vmem_data = %lu; "
935 "vmem_minflt_counter = %" PRIi64 "; vmem_majflt_counter = %" PRIi64 "; "
936 "cpu_user_counter = %" PRIi64 "; cpu_system_counter = %" PRIi64 "; "
937 "io_rchar = %" PRIi64 "; io_wchar = %" PRIi64 "; "
938 "io_syscr = %" PRIi64 "; io_syscw = %" PRIi64 "; "
939 "io_diskr = %" PRIi64 "; io_diskw = %" PRIi64 "; "
940 "cswitch_vol = %" PRIi64 "; cswitch_invol = %" PRIi64 "; "
941 "delay_cpu = %g; delay_blkio = %g; "
942 "delay_swapin = %g; delay_freepages = %g;",
943 ps->name, ps->num_proc, ps->num_lwp, ps->num_fd, ps->num_maps,
944 ps->vmem_size, ps->vmem_rss, ps->vmem_data, ps->vmem_code,
945 ps->vmem_minflt_counter, ps->vmem_majflt_counter, ps->cpu_user_counter,
946 ps->cpu_system_counter, ps->io_rchar, ps->io_wchar, ps->io_syscr,
947 ps->io_syscw, ps->io_diskr, ps->io_diskw, ps->cswitch_vol,
948 ps->cswitch_invol, ps->delay_cpu, ps->delay_blkio, ps->delay_swapin,
949 ps->delay_freepages);
951 } /* void ps_submit_proc_list */
953 #if KERNEL_LINUX || KERNEL_SOLARIS
954 static void ps_submit_fork_rate(derive_t value) {
955 value_list_t vl = VALUE_LIST_INIT;
957 vl.values = &(value_t){.derive = value};
959 sstrncpy(vl.plugin, "processes", sizeof(vl.plugin));
960 sstrncpy(vl.plugin_instance, "", sizeof(vl.plugin_instance));
961 sstrncpy(vl.type, "fork_rate", sizeof(vl.type));
962 sstrncpy(vl.type_instance, "", sizeof(vl.type_instance));
964 plugin_dispatch_values(&vl);
966 #endif /* KERNEL_LINUX || KERNEL_SOLARIS*/
968 /* ------- additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
970 static int ps_read_tasks_status(process_entry_t *ps) {
976 derive_t cswitch_vol = 0;
977 derive_t cswitch_invol = 0;
982 snprintf(dirname, sizeof(dirname), "/proc/%li/task", ps->id);
984 if ((dh = opendir(dirname)) == NULL) {
985 DEBUG("Failed to open directory `%s'", dirname);
989 while ((ent = readdir(dh)) != NULL) {
992 if (!isdigit((int)ent->d_name[0]))
997 if (snprintf(filename, sizeof(filename), "/proc/%li/task/%s/status", ps->id,
998 tpid) >= sizeof(filename)) {
999 DEBUG("Filename too long: `%s'", filename);
1003 if ((fh = fopen(filename, "r")) == NULL) {
1004 DEBUG("Failed to open file `%s'", filename);
1008 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
1012 if (strncmp(buffer, "voluntary_ctxt_switches", 23) != 0 &&
1013 strncmp(buffer, "nonvoluntary_ctxt_switches", 26) != 0)
1016 numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
1023 tmp = (derive_t)strtoll(fields[1], &endptr, /* base = */ 10);
1024 if ((errno == 0) && (endptr != fields[1])) {
1025 if (strncmp(buffer, "voluntary_ctxt_switches", 23) == 0) {
1027 } else if (strncmp(buffer, "nonvoluntary_ctxt_switches", 26) == 0) {
1028 cswitch_invol += tmp;
1031 } /* while (fgets) */
1034 WARNING("processes: fclose: %s", STRERRNO);
1039 ps->cswitch_vol = cswitch_vol;
1040 ps->cswitch_invol = cswitch_invol;
1043 } /* int *ps_read_tasks_status */
1045 /* Read data from /proc/pid/status */
1046 static int ps_read_status(long pid, process_entry_t *ps) {
1050 unsigned long lib = 0;
1051 unsigned long exe = 0;
1052 unsigned long data = 0;
1053 unsigned long threads = 0;
1057 snprintf(filename, sizeof(filename), "/proc/%li/status", pid);
1058 if ((fh = fopen(filename, "r")) == NULL)
1061 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
1065 if (strncmp(buffer, "Vm", 2) != 0 && strncmp(buffer, "Threads", 7) != 0)
1068 numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
1075 tmp = strtoul(fields[1], &endptr, /* base = */ 10);
1076 if ((errno == 0) && (endptr != fields[1])) {
1077 if (strncmp(buffer, "VmData", 6) == 0) {
1079 } else if (strncmp(buffer, "VmLib", 5) == 0) {
1081 } else if (strncmp(buffer, "VmExe", 5) == 0) {
1083 } else if (strncmp(buffer, "Threads", 7) == 0) {
1087 } /* while (fgets) */
1090 WARNING("processes: fclose: %s", STRERRNO);
1093 ps->vmem_data = data * 1024;
1094 ps->vmem_code = (exe + lib) * 1024;
1096 ps->num_lwp = threads;
1099 } /* int *ps_read_status */
1101 static int ps_read_io(process_entry_t *ps) {
1109 snprintf(filename, sizeof(filename), "/proc/%li/io", ps->id);
1110 if ((fh = fopen(filename, "r")) == NULL) {
1111 DEBUG("ps_read_io: Failed to open file `%s'", filename);
1115 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
1116 derive_t *val = NULL;
1120 if (strncasecmp(buffer, "rchar:", 6) == 0)
1121 val = &(ps->io_rchar);
1122 else if (strncasecmp(buffer, "wchar:", 6) == 0)
1123 val = &(ps->io_wchar);
1124 else if (strncasecmp(buffer, "syscr:", 6) == 0)
1125 val = &(ps->io_syscr);
1126 else if (strncasecmp(buffer, "syscw:", 6) == 0)
1127 val = &(ps->io_syscw);
1128 else if (strncasecmp(buffer, "read_bytes:", 11) == 0)
1129 val = &(ps->io_diskr);
1130 else if (strncasecmp(buffer, "write_bytes:", 12) == 0)
1131 val = &(ps->io_diskw);
1135 numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
1142 tmp = strtoll(fields[1], &endptr, /* base = */ 10);
1143 if ((errno != 0) || (endptr == fields[1]))
1146 *val = (derive_t)tmp;
1147 } /* while (fgets) */
1150 WARNING("processes: fclose: %s", STRERRNO);
1153 } /* int ps_read_io (...) */
1155 static int ps_count_maps(pid_t pid) {
1161 snprintf(filename, sizeof(filename), "/proc/%d/maps", pid);
1162 if ((fh = fopen(filename, "r")) == NULL) {
1163 DEBUG("ps_count_maps: Failed to open file `%s'", filename);
1167 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
1168 if (strchr(buffer, '\n')) {
1171 } /* while (fgets) */
1174 WARNING("processes: fclose: %s", STRERRNO);
1177 } /* int ps_count_maps (...) */
1179 static int ps_count_fd(int pid) {
1185 snprintf(dirname, sizeof(dirname), "/proc/%i/fd", pid);
1187 if ((dh = opendir(dirname)) == NULL) {
1188 DEBUG("Failed to open directory `%s'", dirname);
1191 while ((ent = readdir(dh)) != NULL) {
1192 if (!isdigit((int)ent->d_name[0]))
1199 return (count >= 1) ? count : 1;
1200 } /* int ps_count_fd (pid) */
1202 #if HAVE_LIBTASKSTATS
1203 static int ps_delay(process_entry_t *ps) {
1204 if (taskstats_handle == NULL) {
1208 int status = ts_delay_by_tgid(taskstats_handle, (uint32_t)ps->id, &ps->delay);
1209 if (status == EPERM) {
1210 static c_complain_t c;
1211 #if defined(HAVE_SYS_CAPABILITY_H) && defined(CAP_NET_ADMIN)
1212 if (check_capability(CAP_NET_ADMIN) != 0) {
1213 if (getuid() == 0) {
1216 "processes plugin: Reading Delay Accounting metric failed: %s. "
1217 "collectd is running as root, but missing the CAP_NET_ADMIN "
1218 "capability. The most common cause for this is that the init "
1219 "system is dropping capabilities.",
1224 "processes plugin: Reading Delay Accounting metric failed: %s. "
1225 "collectd is not running as root and missing the CAP_NET_ADMIN "
1226 "capability. Either run collectd as root or grant it the "
1227 "CAP_NET_ADMIN capability using \"setcap cap_net_admin=ep " PREFIX
1228 "/sbin/collectd\".",
1232 ERROR("processes plugin: ts_delay_by_tgid failed: %s. The CAP_NET_ADMIN "
1233 "capability is available (I checked), so this error is utterly "
1238 c_complain(LOG_ERR, &c,
1239 "processes plugin: Reading Delay Accounting metric failed: %s. "
1240 "Reading Delay Accounting metrics requires root privileges.",
1244 } else if (status != 0) {
1245 ERROR("processes plugin: ts_delay_by_tgid failed: %s", STRERROR(status));
1252 static int ps_delay(__attribute__((unused)) process_entry_t *unused) {
1257 static void ps_fill_details(const procstat_t *ps, process_entry_t *entry) {
1258 if (entry->has_io == 0) {
1263 if (ps->report_ctx_switch) {
1264 if (entry->has_cswitch == 0) {
1265 ps_read_tasks_status(entry);
1266 entry->has_cswitch = 1;
1270 if (ps->report_maps_num) {
1272 if (entry->has_maps == 0 && (num_maps = ps_count_maps(entry->id)) > 0) {
1273 entry->num_maps = num_maps;
1275 entry->has_maps = 1;
1278 if (ps->report_fd_num) {
1280 if (entry->has_fd == 0 && (num_fd = ps_count_fd(entry->id)) > 0) {
1281 entry->num_fd = num_fd;
1286 #if HAVE_LIBTASKSTATS
1287 if (ps->report_delay && !entry->has_delay) {
1288 if (ps_delay(entry) == 0) {
1289 entry->has_delay = 1;
1293 } /* void ps_fill_details (...) */
1295 /* ps_read_process reads process counters on Linux. */
1296 static int ps_read_process(long pid, process_entry_t *ps, char *state) {
1306 size_t name_start_pos;
1307 size_t name_end_pos;
1310 derive_t cpu_user_counter;
1311 derive_t cpu_system_counter;
1312 long long unsigned vmem_size;
1313 long long unsigned vmem_rss;
1314 long long unsigned stack_size;
1318 snprintf(filename, sizeof(filename), "/proc/%li/stat", pid);
1320 status = read_file_contents(filename, buffer, sizeof(buffer) - 1);
1323 buffer_len = (size_t)status;
1324 buffer[buffer_len] = 0;
1326 /* The name of the process is enclosed in parens. Since the name can
1327 * contain parens itself, spaces, numbers and pretty much everything
1328 * else, use these to determine the process name. We don't use
1329 * strchr(3) and strrchr(3) to avoid pointer arithmetic which would
1330 * otherwise be required to determine name_len. */
1332 while (name_start_pos < buffer_len && buffer[name_start_pos] != '(')
1335 name_end_pos = buffer_len;
1336 while (name_end_pos > 0 && buffer[name_end_pos] != ')')
1339 /* Either '(' or ')' is not found or they are in the wrong order.
1340 * Anyway, something weird that shouldn't happen ever. */
1341 if (name_start_pos >= name_end_pos) {
1342 ERROR("processes plugin: name_start_pos = %zu >= name_end_pos = %zu",
1343 name_start_pos, name_end_pos);
1347 name_len = (name_end_pos - name_start_pos) - 1;
1348 if (name_len >= sizeof(ps->name))
1349 name_len = sizeof(ps->name) - 1;
1351 sstrncpy(ps->name, &buffer[name_start_pos + 1], name_len + 1);
1353 if ((buffer_len - name_end_pos) < 2)
1355 buffer_ptr = &buffer[name_end_pos + 2];
1357 fields_len = strsplit(buffer_ptr, fields, STATIC_ARRAY_SIZE(fields));
1358 if (fields_len < 22) {
1359 DEBUG("processes plugin: ps_read_process (pid = %li):"
1360 " `%s' has only %i fields..",
1361 pid, filename, fields_len);
1365 *state = fields[0][0];
1367 if (*state == 'Z') {
1371 ps->num_lwp = strtoul(fields[17], /* endptr = */ NULL, /* base = */ 10);
1372 if ((ps_read_status(pid, ps)) != 0) {
1376 DEBUG("ps_read_process: did not get vmem data for pid %li", pid);
1378 if (ps->num_lwp == 0)
1383 /* Leave the rest at zero if this is only a zombi */
1384 if (ps->num_proc == 0) {
1385 DEBUG("processes plugin: This is only a zombie: pid = %li; "
1391 cpu_user_counter = atoll(fields[11]);
1392 cpu_system_counter = atoll(fields[12]);
1393 vmem_size = atoll(fields[20]);
1394 vmem_rss = atoll(fields[21]);
1395 ps->vmem_minflt_counter = atol(fields[7]);
1396 ps->vmem_majflt_counter = atol(fields[9]);
1399 unsigned long long stack_start = atoll(fields[25]);
1400 unsigned long long stack_ptr = atoll(fields[26]);
1402 stack_size = (stack_start > stack_ptr) ? stack_start - stack_ptr
1403 : stack_ptr - stack_start;
1406 /* Convert jiffies to useconds */
1407 cpu_user_counter = cpu_user_counter * 1000000 / CONFIG_HZ;
1408 cpu_system_counter = cpu_system_counter * 1000000 / CONFIG_HZ;
1409 vmem_rss = vmem_rss * pagesize_g;
1411 ps->cpu_user_counter = cpu_user_counter;
1412 ps->cpu_system_counter = cpu_system_counter;
1413 ps->vmem_size = (unsigned long)vmem_size;
1414 ps->vmem_rss = (unsigned long)vmem_rss;
1415 ps->stack_size = (unsigned long)stack_size;
1417 /* no data by default. May be filled by ps_fill_details () */
1425 ps->cswitch_vol = -1;
1426 ps->cswitch_invol = -1;
1430 } /* int ps_read_process (...) */
1432 static char *ps_get_cmdline(long pid, char *name, char *buf, size_t buf_len) {
1436 char file[PATH_MAX];
1441 if ((pid < 1) || (NULL == buf) || (buf_len < 2))
1444 snprintf(file, sizeof(file), "/proc/%li/cmdline", pid);
1447 fd = open(file, O_RDONLY);
1449 /* ENOENT means the process exited while we were handling it.
1450 * Don't complain about this, it only fills the logs. */
1451 if (errno != ENOENT)
1452 WARNING("processes plugin: Failed to open `%s': %s.", file, STRERRNO);
1464 status = read(fd, (void *)buf_ptr, len);
1468 if ((EAGAIN == errno) || (EINTR == errno))
1471 WARNING("processes plugin: Failed to read from `%s': %s.", file,
1492 /* cmdline not available; e.g. kernel thread, zombie */
1496 snprintf(buf, buf_len, "[%s]", name);
1500 assert(n <= buf_len);
1507 /* remove trailing whitespace */
1508 while ((n > 0) && (isspace(buf[n]) || ('\0' == buf[n]))) {
1513 /* arguments are separated by '\0' in /proc/<pid>/cmdline */
1520 } /* char *ps_get_cmdline (...) */
1522 static int read_fork_rate(void) {
1526 _Bool value_valid = 0;
1528 proc_stat = fopen("/proc/stat", "r");
1529 if (proc_stat == NULL) {
1530 ERROR("processes plugin: fopen (/proc/stat) failed: %s", STRERRNO);
1534 while (fgets(buffer, sizeof(buffer), proc_stat) != NULL) {
1539 fields_num = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
1540 if (fields_num != 2)
1543 if (strcmp("processes", fields[0]) != 0)
1546 status = parse_value(fields[1], &value, DS_TYPE_DERIVE);
1557 ps_submit_fork_rate(value.derive);
1560 #endif /*KERNEL_LINUX */
1563 static char *ps_get_cmdline(long pid,
1564 char *name __attribute__((unused)), /* {{{ */
1565 char *buffer, size_t buffer_size) {
1566 char path[PATH_MAX];
1570 snprintf(path, sizeof(path), "/proc/%li/psinfo", pid);
1572 status = read_file_contents(path, (void *)&info, sizeof(info));
1573 if ((status < 0) || (((size_t)status) != sizeof(info))) {
1574 ERROR("processes plugin: Unexpected return value "
1575 "while reading \"%s\": "
1576 "Returned %zd but expected %zu.",
1577 path, status, buffer_size);
1581 info.pr_psargs[sizeof(info.pr_psargs) - 1] = 0;
1582 sstrncpy(buffer, info.pr_psargs, buffer_size);
1585 } /* }}} int ps_get_cmdline */
1588 * Reads process information on the Solaris OS. The information comes mainly
1590 * /proc/PID/status, /proc/PID/psinfo and /proc/PID/usage
1591 * The values for input and ouput chars are calculated "by hand"
1592 * Added a few "solaris" specific process states as well
1594 static int ps_read_process(long pid, process_entry_t *ps, char *state) {
1596 char f_psinfo[64], f_usage[64];
1599 pstatus_t *myStatus;
1603 snprintf(filename, sizeof(filename), "/proc/%li/status", pid);
1604 snprintf(f_psinfo, sizeof(f_psinfo), "/proc/%li/psinfo", pid);
1605 snprintf(f_usage, sizeof(f_usage), "/proc/%li/usage", pid);
1607 buffer = calloc(1, sizeof(pstatus_t));
1608 read_file_contents(filename, buffer, sizeof(pstatus_t));
1609 myStatus = (pstatus_t *)buffer;
1611 buffer = calloc(1, sizeof(psinfo_t));
1612 read_file_contents(f_psinfo, buffer, sizeof(psinfo_t));
1613 myInfo = (psinfo_t *)buffer;
1615 buffer = calloc(1, sizeof(prusage_t));
1616 read_file_contents(f_usage, buffer, sizeof(prusage_t));
1617 myUsage = (prusage_t *)buffer;
1619 sstrncpy(ps->name, myInfo->pr_fname, sizeof(myInfo->pr_fname));
1620 ps->num_lwp = myStatus->pr_nlwp;
1621 if (myInfo->pr_wstat != 0) {
1632 ps->num_lwp = myInfo->pr_nlwp;
1636 * Convert system time and user time from nanoseconds to microseconds
1637 * for compatibility with the linux module
1639 ps->cpu_system_counter = myStatus->pr_stime.tv_nsec / 1000;
1640 ps->cpu_user_counter = myStatus->pr_utime.tv_nsec / 1000;
1643 * Convert rssize from KB to bytes to be consistent w/ the linux module
1645 ps->vmem_rss = myInfo->pr_rssize * 1024;
1646 ps->vmem_size = myInfo->pr_size * 1024;
1647 ps->vmem_minflt_counter = myUsage->pr_minf;
1648 ps->vmem_majflt_counter = myUsage->pr_majf;
1651 * TODO: Data and code segment calculations for Solaris
1656 ps->stack_size = myStatus->pr_stksize;
1659 * TODO: File descriptor count for Solaris
1663 /* Number of memory mappings */
1667 * Calculating input/ouput chars
1668 * Formula used is total chars / total blocks => chars/block
1669 * then convert input/output blocks to chars
1671 ulong_t tot_chars = myUsage->pr_ioch;
1672 ulong_t tot_blocks = myUsage->pr_inblk + myUsage->pr_oublk;
1673 ulong_t chars_per_block = 1;
1674 if (tot_blocks != 0)
1675 chars_per_block = tot_chars / tot_blocks;
1676 ps->io_rchar = myUsage->pr_inblk * chars_per_block;
1677 ps->io_wchar = myUsage->pr_oublk * chars_per_block;
1678 ps->io_syscr = myUsage->pr_sysc;
1679 ps->io_syscw = myUsage->pr_sysc;
1684 * TODO: context switch counters for Solaris
1686 ps->cswitch_vol = -1;
1687 ps->cswitch_invol = -1;
1690 * TODO: Find way of setting BLOCKED and PAGING status
1694 if (myStatus->pr_flags & PR_ASLEEP)
1696 else if (myStatus->pr_flags & PR_STOPPED)
1698 else if (myStatus->pr_flags & PR_DETACH)
1700 else if (myStatus->pr_flags & PR_DAEMON)
1702 else if (myStatus->pr_flags & PR_ISSYS)
1704 else if (myStatus->pr_flags & PR_ORPHAN)
1715 * Reads the number of threads created since the last reboot. On Solaris these
1716 * are retrieved from kstat (module cpu, name sys, class misc, stat nthreads).
1717 * The result is the sum for all the threads created on each cpu
1719 static int read_fork_rate(void) {
1720 extern kstat_ctl_t *kc;
1721 derive_t result = 0;
1726 for (kstat_t *ksp_chain = kc->kc_chain; ksp_chain != NULL;
1727 ksp_chain = ksp_chain->ks_next) {
1728 if ((strcmp(ksp_chain->ks_module, "cpu") == 0) &&
1729 (strcmp(ksp_chain->ks_name, "sys") == 0) &&
1730 (strcmp(ksp_chain->ks_class, "misc") == 0)) {
1733 kstat_read(kc, ksp_chain, NULL);
1735 tmp = get_kstat_value(ksp_chain, "nthreads");
1741 ps_submit_fork_rate(result);
1744 #endif /* KERNEL_SOLARIS */
1746 #if HAVE_THREAD_INFO
1747 static int mach_get_task_name(task_t t, int *pid, char *name,
1748 size_t name_max_len) {
1751 struct kinfo_proc kp;
1756 mib[2] = KERN_PROC_PID;
1758 if (pid_for_task(t, pid) != KERN_SUCCESS)
1762 kp_size = sizeof(kp);
1763 if (sysctl(mib, 4, &kp, &kp_size, NULL, 0) != 0)
1766 if (name_max_len > (MAXCOMLEN + 1))
1767 name_max_len = MAXCOMLEN + 1;
1769 strncpy(name, kp.kp_proc.p_comm, name_max_len - 1);
1770 name[name_max_len - 1] = '\0';
1772 DEBUG("pid = %i; name = %s;", *pid, name);
1774 /* We don't do the special handling for `p_comm == "LaunchCFMApp"' as
1775 * `top' does it, because it is a lot of work and only used when
1776 * debugging. -octo */
1780 #endif /* HAVE_THREAD_INFO */
1781 /* end of additional functions for KERNEL_LINUX/HAVE_THREAD_INFO */
1783 /* do actual readings from kernel */
1784 static int ps_read(void) {
1785 #if HAVE_THREAD_INFO
1786 kern_return_t status;
1788 processor_set_t port_pset_priv;
1790 task_array_t task_list;
1791 mach_msg_type_number_t task_list_len;
1794 char task_name[MAXCOMLEN + 1];
1796 thread_act_array_t thread_list;
1797 mach_msg_type_number_t thread_list_len;
1798 thread_basic_info_data_t thread_data;
1799 mach_msg_type_number_t thread_data_len;
1808 process_entry_t pse;
1813 * The Mach-concept is a little different from the traditional UNIX
1814 * concept: All the work is done in threads. Threads are contained in
1815 * `tasks'. Therefore, `task status' doesn't make much sense, since
1816 * it's actually a `thread status'.
1817 * Tasks are assigned to sets of processors, so that's where you go to
1820 for (mach_msg_type_number_t pset = 0; pset < pset_list_len; pset++) {
1821 if ((status = host_processor_set_priv(port_host_self, pset_list[pset],
1822 &port_pset_priv)) != KERN_SUCCESS) {
1823 ERROR("host_processor_set_priv failed: %s\n", mach_error_string(status));
1827 if ((status = processor_set_tasks(port_pset_priv, &task_list,
1828 &task_list_len)) != KERN_SUCCESS) {
1829 ERROR("processor_set_tasks failed: %s\n", mach_error_string(status));
1830 mach_port_deallocate(port_task_self, port_pset_priv);
1834 for (mach_msg_type_number_t task = 0; task < task_list_len; task++) {
1836 if (mach_get_task_name(task_list[task], &task_pid, task_name,
1837 PROCSTAT_NAME_LEN) == 0) {
1838 /* search for at least one match */
1839 for (ps = list_head_g; ps != NULL; ps = ps->next)
1840 /* FIXME: cmdline should be here instead of NULL */
1841 if (ps_list_match(task_name, NULL, ps) == 1)
1845 /* Collect more detailed statistics for this process */
1847 task_basic_info_data_t task_basic_info;
1848 mach_msg_type_number_t task_basic_info_len;
1849 task_events_info_data_t task_events_info;
1850 mach_msg_type_number_t task_events_info_len;
1851 task_absolutetime_info_data_t task_absolutetime_info;
1852 mach_msg_type_number_t task_absolutetime_info_len;
1854 memset(&pse, '\0', sizeof(pse));
1857 task_basic_info_len = TASK_BASIC_INFO_COUNT;
1858 status = task_info(task_list[task], TASK_BASIC_INFO,
1859 (task_info_t)&task_basic_info, &task_basic_info_len);
1860 if (status != KERN_SUCCESS) {
1861 ERROR("task_info failed: %s", mach_error_string(status));
1862 continue; /* with next thread_list */
1865 task_events_info_len = TASK_EVENTS_INFO_COUNT;
1867 task_info(task_list[task], TASK_EVENTS_INFO,
1868 (task_info_t)&task_events_info, &task_events_info_len);
1869 if (status != KERN_SUCCESS) {
1870 ERROR("task_info failed: %s", mach_error_string(status));
1871 continue; /* with next thread_list */
1874 task_absolutetime_info_len = TASK_ABSOLUTETIME_INFO_COUNT;
1875 status = task_info(task_list[task], TASK_ABSOLUTETIME_INFO,
1876 (task_info_t)&task_absolutetime_info,
1877 &task_absolutetime_info_len);
1878 if (status != KERN_SUCCESS) {
1879 ERROR("task_info failed: %s", mach_error_string(status));
1880 continue; /* with next thread_list */
1884 pse.vmem_size = task_basic_info.virtual_size;
1885 pse.vmem_rss = task_basic_info.resident_size;
1886 /* Does not seem to be easily exposed */
1897 /* File descriptor count not implemented */
1900 /* Number of memory mappings */
1903 pse.vmem_minflt_counter = task_events_info.cow_faults;
1904 pse.vmem_majflt_counter = task_events_info.faults;
1906 pse.cpu_user_counter = task_absolutetime_info.total_user;
1907 pse.cpu_system_counter = task_absolutetime_info.total_system;
1909 /* context switch counters not implemented */
1910 pse.cswitch_vol = -1;
1911 pse.cswitch_invol = -1;
1914 status = task_threads(task_list[task], &thread_list, &thread_list_len);
1915 if (status != KERN_SUCCESS) {
1916 /* Apple's `top' treats this case a zombie. It
1917 * makes sense to some extend: A `zombie'
1918 * thread is nonsense, since the task/process
1921 DEBUG("task_threads failed: %s", mach_error_string(status));
1922 if (task_list[task] != port_task_self)
1923 mach_port_deallocate(port_task_self, task_list[task]);
1924 continue; /* with next task_list */
1927 for (mach_msg_type_number_t thread = 0; thread < thread_list_len;
1929 thread_data_len = THREAD_BASIC_INFO_COUNT;
1930 status = thread_info(thread_list[thread], THREAD_BASIC_INFO,
1931 (thread_info_t)&thread_data, &thread_data_len);
1932 if (status != KERN_SUCCESS) {
1933 ERROR("thread_info failed: %s", mach_error_string(status));
1934 if (task_list[task] != port_task_self)
1935 mach_port_deallocate(port_task_self, thread_list[thread]);
1936 continue; /* with next thread_list */
1942 switch (thread_data.run_state) {
1943 case TH_STATE_RUNNING:
1946 case TH_STATE_STOPPED:
1947 /* What exactly is `halted'? */
1948 case TH_STATE_HALTED:
1951 case TH_STATE_WAITING:
1954 case TH_STATE_UNINTERRUPTIBLE:
1957 /* There is no `zombie' case here,
1958 * since there are no zombie-threads.
1959 * There's only zombie tasks, which are
1962 WARNING("Unknown thread status: %i", thread_data.run_state);
1964 } /* switch (thread_data.run_state) */
1966 if (task_list[task] != port_task_self) {
1967 status = mach_port_deallocate(port_task_self, thread_list[thread]);
1968 if (status != KERN_SUCCESS)
1969 ERROR("mach_port_deallocate failed: %s", mach_error_string(status));
1971 } /* for (thread_list) */
1973 if ((status = vm_deallocate(port_task_self, (vm_address_t)thread_list,
1974 thread_list_len * sizeof(thread_act_t))) !=
1976 ERROR("vm_deallocate failed: %s", mach_error_string(status));
1979 thread_list_len = 0;
1981 /* Only deallocate the task port, if it isn't our own.
1982 * Don't know what would happen in that case, but this
1983 * is what Apple's top does.. ;) */
1984 if (task_list[task] != port_task_self) {
1985 status = mach_port_deallocate(port_task_self, task_list[task]);
1986 if (status != KERN_SUCCESS)
1987 ERROR("mach_port_deallocate failed: %s", mach_error_string(status));
1991 /* FIXME: cmdline should be here instead of NULL */
1992 ps_list_add(task_name, NULL, &pse);
1993 } /* for (task_list) */
1995 if ((status = vm_deallocate(port_task_self, (vm_address_t)task_list,
1996 task_list_len * sizeof(task_t))) !=
1998 ERROR("vm_deallocate failed: %s", mach_error_string(status));
2003 if ((status = mach_port_deallocate(port_task_self, port_pset_priv)) !=
2005 ERROR("mach_port_deallocate failed: %s", mach_error_string(status));
2007 } /* for (pset_list) */
2009 ps_submit_state("running", running);
2010 ps_submit_state("sleeping", sleeping);
2011 ps_submit_state("zombies", zombies);
2012 ps_submit_state("stopped", stopped);
2013 ps_submit_state("blocked", blocked);
2015 for (ps = list_head_g; ps != NULL; ps = ps->next)
2016 ps_submit_proc_list(ps);
2017 /* #endif HAVE_THREAD_INFO */
2031 char cmdline[CMDLINE_BUFFER_SIZE];
2034 process_entry_t pse;
2037 running = sleeping = zombies = stopped = paging = blocked = 0;
2040 if ((proc = opendir("/proc")) == NULL) {
2041 ERROR("Cannot open `/proc': %s", STRERRNO);
2045 while ((ent = readdir(proc)) != NULL) {
2046 if (!isdigit(ent->d_name[0]))
2049 if ((pid = atol(ent->d_name)) < 1)
2052 memset(&pse, 0, sizeof(pse));
2055 status = ps_read_process(pid, &pse, &state);
2057 DEBUG("ps_read_process failed: %i", status);
2082 ps_list_add(pse.name,
2083 ps_get_cmdline(pid, pse.name, cmdline, sizeof(cmdline)), &pse);
2088 ps_submit_state("running", running);
2089 ps_submit_state("sleeping", sleeping);
2090 ps_submit_state("zombies", zombies);
2091 ps_submit_state("stopped", stopped);
2092 ps_submit_state("paging", paging);
2093 ps_submit_state("blocked", blocked);
2095 for (procstat_t *ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
2096 ps_submit_proc_list(ps_ptr);
2099 /* #endif KERNEL_LINUX */
2101 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD
2111 char errbuf[_POSIX2_LINE_MAX];
2112 struct kinfo_proc *procs; /* array of processes */
2113 struct kinfo_proc *proc_ptr = NULL;
2114 int count; /* returns number of processes */
2116 process_entry_t pse;
2120 /* Open the kvm interface, get a descriptor */
2121 kd = kvm_openfiles(NULL, "/dev/null", NULL, 0, errbuf);
2123 ERROR("processes plugin: Cannot open kvm interface: %s", errbuf);
2127 /* Get the list of processes. */
2128 procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, &count);
2129 if (procs == NULL) {
2130 ERROR("processes plugin: Cannot get kvm processes list: %s",
2136 /* Iterate through the processes in kinfo_proc */
2137 for (int i = 0; i < count; i++) {
2138 /* Create only one process list entry per _process_, i.e.
2139 * filter out threads (duplicate PID entries). */
2140 if ((proc_ptr == NULL) || (proc_ptr->ki_pid != procs[i].ki_pid)) {
2141 char cmdline[CMDLINE_BUFFER_SIZE] = "";
2142 _Bool have_cmdline = 0;
2144 proc_ptr = &(procs[i]);
2145 /* Don't probe system processes and processes without arguments */
2146 if (((procs[i].ki_flag & P_SYSTEM) == 0) && (procs[i].ki_args != NULL)) {
2151 /* retrieve the arguments */
2152 argv = kvm_getargv(kd, proc_ptr, /* nchr = */ 0);
2154 if ((argv != NULL) && (argv[0] != NULL)) {
2155 while (argv[argc] != NULL)
2158 status = strjoin(cmdline, sizeof(cmdline), argv, argc, " ");
2160 WARNING("processes plugin: Command line did not fit into buffer.");
2164 } /* if (process has argument list) */
2166 memset(&pse, 0, sizeof(pse));
2167 pse.id = procs[i].ki_pid;
2170 pse.num_lwp = procs[i].ki_numthreads;
2172 pse.vmem_size = procs[i].ki_size;
2173 pse.vmem_rss = procs[i].ki_rssize * pagesize;
2174 pse.vmem_data = procs[i].ki_dsize * pagesize;
2175 pse.vmem_code = procs[i].ki_tsize * pagesize;
2176 pse.stack_size = procs[i].ki_ssize * pagesize;
2177 pse.vmem_minflt_counter = procs[i].ki_rusage.ru_minflt;
2178 pse.vmem_majflt_counter = procs[i].ki_rusage.ru_majflt;
2180 pse.cpu_user_counter = 0;
2181 pse.cpu_system_counter = 0;
2183 * The u-area might be swapped out, and we can't get
2184 * at it because we have a crashdump and no swap.
2185 * If it's here fill in these fields, otherwise, just
2188 if (procs[i].ki_flag & P_INMEM) {
2189 pse.cpu_user_counter = procs[i].ki_rusage.ru_utime.tv_usec +
2190 (1000000lu * procs[i].ki_rusage.ru_utime.tv_sec);
2191 pse.cpu_system_counter =
2192 procs[i].ki_rusage.ru_stime.tv_usec +
2193 (1000000lu * procs[i].ki_rusage.ru_stime.tv_sec);
2204 /* file descriptor count not implemented */
2207 /* Number of memory mappings */
2210 /* context switch counters not implemented */
2211 pse.cswitch_vol = -1;
2212 pse.cswitch_invol = -1;
2214 ps_list_add(procs[i].ki_comm, have_cmdline ? cmdline : NULL, &pse);
2216 switch (procs[i].ki_stat) {
2239 } /* if ((proc_ptr == NULL) || (proc_ptr->ki_pid != procs[i].ki_pid)) */
2244 ps_submit_state("running", running);
2245 ps_submit_state("sleeping", sleeping);
2246 ps_submit_state("zombies", zombies);
2247 ps_submit_state("stopped", stopped);
2248 ps_submit_state("blocked", blocked);
2249 ps_submit_state("idle", idle);
2250 ps_submit_state("wait", wait);
2252 for (procstat_t *ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
2253 ps_submit_proc_list(ps_ptr);
2254 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
2256 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_OPENBSD
2267 struct kinfo_proc *procs; /* array of processes */
2268 struct kinfo_proc *proc_ptr = NULL;
2269 int count; /* returns number of processes */
2271 process_entry_t pse;
2275 /* Open the kvm interface, get a descriptor */
2276 kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf);
2278 ERROR("processes plugin: Cannot open kvm interface: %s", errbuf);
2282 /* Get the list of processes. */
2283 procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc), &count);
2284 if (procs == NULL) {
2285 ERROR("processes plugin: Cannot get kvm processes list: %s",
2291 /* Iterate through the processes in kinfo_proc */
2292 for (int i = 0; i < count; i++) {
2293 /* Create only one process list entry per _process_, i.e.
2294 * filter out threads (duplicate PID entries). */
2295 if ((proc_ptr == NULL) || (proc_ptr->p_pid != procs[i].p_pid)) {
2296 char cmdline[CMDLINE_BUFFER_SIZE] = "";
2297 _Bool have_cmdline = 0;
2299 proc_ptr = &(procs[i]);
2300 /* Don't probe zombie processes */
2301 if (!P_ZOMBIE(proc_ptr)) {
2306 /* retrieve the arguments */
2307 argv = kvm_getargv(kd, proc_ptr, /* nchr = */ 0);
2309 if ((argv != NULL) && (argv[0] != NULL)) {
2310 while (argv[argc] != NULL)
2313 status = strjoin(cmdline, sizeof(cmdline), argv, argc, " ");
2315 WARNING("processes plugin: Command line did not fit into buffer.");
2319 } /* if (process has argument list) */
2321 memset(&pse, 0, sizeof(pse));
2322 pse.id = procs[i].p_pid;
2325 pse.num_lwp = 1; /* XXX: accumulate p_tid values for a single p_pid ? */
2327 pse.vmem_rss = procs[i].p_vm_rssize * pagesize;
2328 pse.vmem_data = procs[i].p_vm_dsize * pagesize;
2329 pse.vmem_code = procs[i].p_vm_tsize * pagesize;
2330 pse.stack_size = procs[i].p_vm_ssize * pagesize;
2331 pse.vmem_size = pse.stack_size + pse.vmem_code + pse.vmem_data;
2332 pse.vmem_minflt_counter = procs[i].p_uru_minflt;
2333 pse.vmem_majflt_counter = procs[i].p_uru_majflt;
2335 pse.cpu_user_counter =
2336 procs[i].p_uutime_usec + (1000000lu * procs[i].p_uutime_sec);
2337 pse.cpu_system_counter =
2338 procs[i].p_ustime_usec + (1000000lu * procs[i].p_ustime_sec);
2348 /* file descriptor count not implemented */
2351 /* Number of memory mappings */
2354 /* context switch counters not implemented */
2355 pse.cswitch_vol = -1;
2356 pse.cswitch_invol = -1;
2358 ps_list_add(procs[i].p_comm, have_cmdline ? cmdline : NULL, &pse);
2360 switch (procs[i].p_stat) {
2383 } /* if ((proc_ptr == NULL) || (proc_ptr->p_pid != procs[i].p_pid)) */
2388 ps_submit_state("running", running);
2389 ps_submit_state("sleeping", sleeping);
2390 ps_submit_state("zombies", zombies);
2391 ps_submit_state("stopped", stopped);
2392 ps_submit_state("onproc", onproc);
2393 ps_submit_state("idle", idle);
2394 ps_submit_state("dead", dead);
2396 for (procstat_t *ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
2397 ps_submit_proc_list(ps_ptr);
2398 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_OPENBSD */
2400 #elif HAVE_PROCINFO_H
2412 process_entry_t pse;
2415 while ((nprocs = getprocs64(procentry, sizeof(struct procentry64),
2416 /* fdsinfo = */ NULL, sizeof(struct fdsinfo64),
2417 &pindex, MAXPROCENTRY)) > 0) {
2418 for (int i = 0; i < nprocs; i++) {
2421 char arglist[MAXARGLN + 1];
2425 if (procentry[i].pi_state == SNONE)
2427 /* if (procentry[i].pi_state == SZOMB) FIXME */
2429 cmdline = procentry[i].pi_comm;
2430 cargs = procentry[i].pi_comm;
2431 if (procentry[i].pi_flags & SKPROC) {
2432 if (procentry[i].pi_pid == 0)
2433 cmdline = "swapper";
2436 if (getargs(&procentry[i], sizeof(struct procentry64), arglist,
2441 while (++n < MAXARGLN) {
2442 if (arglist[n] == '\0') {
2443 if (arglist[n + 1] == '\0')
2452 memset(&pse, 0, sizeof(pse));
2454 pse.id = procentry[i].pi_pid;
2455 pse.num_lwp = procentry[i].pi_thcount;
2459 while ((nthreads = getthrds64(procentry[i].pi_pid, thrdentry,
2460 sizeof(struct thrdentry64), &thindex,
2461 MAXTHRDENTRY)) > 0) {
2464 for (j = 0; j < nthreads; j++) {
2465 switch (thrdentry[j].ti_state) {
2466 /* case TSNONE: break; */
2469 break; /* FIXME is really blocked */
2487 if (nthreads < MAXTHRDENTRY)
2491 /* tv_usec is nanosec ??? */
2492 pse.cpu_user_counter = procentry[i].pi_ru.ru_utime.tv_sec * 1000000 +
2493 procentry[i].pi_ru.ru_utime.tv_usec / 1000;
2495 /* tv_usec is nanosec ??? */
2496 pse.cpu_system_counter = procentry[i].pi_ru.ru_stime.tv_sec * 1000000 +
2497 procentry[i].pi_ru.ru_stime.tv_usec / 1000;
2499 pse.vmem_minflt_counter = procentry[i].pi_minflt;
2500 pse.vmem_majflt_counter = procentry[i].pi_majflt;
2502 pse.vmem_size = procentry[i].pi_tsize + procentry[i].pi_dvm * pagesize;
2503 pse.vmem_rss = (procentry[i].pi_drss + procentry[i].pi_trss) * pagesize;
2504 /* Not supported/implemented */
2519 pse.cswitch_vol = -1;
2520 pse.cswitch_invol = -1;
2522 ps_list_add(cmdline, cargs, &pse);
2523 } /* for (i = 0 .. nprocs) */
2525 if (nprocs < MAXPROCENTRY)
2527 } /* while (getprocs64() > 0) */
2528 ps_submit_state("running", running);
2529 ps_submit_state("sleeping", sleeping);
2530 ps_submit_state("zombies", zombies);
2531 ps_submit_state("stopped", stopped);
2532 ps_submit_state("paging", paging);
2533 ps_submit_state("blocked", blocked);
2535 for (procstat_t *ps = list_head_g; ps != NULL; ps = ps->next)
2536 ps_submit_proc_list(ps);
2537 /* #endif HAVE_PROCINFO_H */
2539 #elif KERNEL_SOLARIS
2541 * The Solaris section adds a few more process states and removes some
2542 * process states compared to linux. Most notably there is no "PAGING"
2543 * and "BLOCKED" state for a process. The rest is similar to the linux
2561 char cmdline[PRARGSZ];
2565 proc = opendir("/proc");
2569 while ((ent = readdir(proc)) != NULL) {
2571 process_entry_t pse;
2574 if (!isdigit((int)ent->d_name[0]))
2577 pid = strtol(ent->d_name, &endptr, 10);
2578 if (*endptr != 0) /* value didn't completely parse as a number */
2581 memset(&pse, 0, sizeof(pse));
2584 status = ps_read_process(pid, &pse, &state);
2586 DEBUG("ps_read_process failed: %i", status);
2617 ps_list_add(pse.name,
2618 ps_get_cmdline(pid, pse.name, cmdline, sizeof(cmdline)), &pse);
2619 } /* while(readdir) */
2622 ps_submit_state("running", running);
2623 ps_submit_state("sleeping", sleeping);
2624 ps_submit_state("zombies", zombies);
2625 ps_submit_state("stopped", stopped);
2626 ps_submit_state("detached", detached);
2627 ps_submit_state("daemon", daemon);
2628 ps_submit_state("system", system);
2629 ps_submit_state("orphan", orphan);
2631 for (procstat_t *ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
2632 ps_submit_proc_list(ps_ptr);
2635 #endif /* KERNEL_SOLARIS */
2642 void module_register(void) {
2643 plugin_register_complex_config("processes", ps_config);
2644 plugin_register_init("processes", ps_init);
2645 plugin_register_read("processes", ps_read);
2646 } /* void module_register */