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>
43 /* Include header files for the mach system, if they exist.. */
45 #if HAVE_MACH_MACH_INIT_H
46 #include <mach/mach_init.h>
48 #if HAVE_MACH_HOST_PRIV_H
49 #include <mach/host_priv.h>
51 #if HAVE_MACH_MACH_ERROR_H
52 #include <mach/mach_error.h>
54 #if HAVE_MACH_MACH_HOST_H
55 #include <mach/mach_host.h>
57 #if HAVE_MACH_MACH_PORT_H
58 #include <mach/mach_port.h>
60 #if HAVE_MACH_MACH_TYPES_H
61 #include <mach/mach_types.h>
63 #if HAVE_MACH_MESSAGE_H
64 #include <mach/message.h>
66 #if HAVE_MACH_PROCESSOR_SET_H
67 #include <mach/processor_set.h>
70 #include <mach/task.h>
72 #if HAVE_MACH_THREAD_ACT_H
73 #include <mach/thread_act.h>
75 #if HAVE_MACH_VM_REGION_H
76 #include <mach/vm_region.h>
78 #if HAVE_MACH_VM_MAP_H
79 #include <mach/vm_map.h>
81 #if HAVE_MACH_VM_PROT_H
82 #include <mach/vm_prot.h>
85 #include <sys/sysctl.h>
87 /* #endif HAVE_THREAD_INFO */
90 #if HAVE_LINUX_CONFIG_H
91 #include <linux/config.h>
96 /* #endif KERNEL_LINUX */
98 #elif HAVE_LIBKVM_GETPROCS && \
99 (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD)
101 #include <sys/param.h>
102 #include <sys/proc.h>
103 #include <sys/sysctl.h>
104 #include <sys/user.h>
105 /* #endif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD ||
106 * HAVE_STRUCT_KINFO_PROC_OPENBSD) */
108 #elif HAVE_PROCINFO_H
109 #include <procinfo.h>
110 #include <sys/types.h>
112 #define MAXPROCENTRY 32
113 #define MAXTHRDENTRY 16
114 #define MAXARGLN 1024
115 /* #endif HAVE_PROCINFO_H */
118 /* Hack: Avoid #error when building a 32-bit binary with
119 * _FILE_OFFSET_BITS=64. There is a reason for this #error, as one
120 * of the structures in <sys/procfs.h> uses an off_t, but that
121 * isn't relevant to our usage of procfs. */
122 #if !defined(_LP64) && _FILE_OFFSET_BITS == 64
124 #undef _FILE_OFFSET_BITS
130 #define _FILE_OFFSET_BITS 64
135 #include <sys/user.h>
141 /* #endif KERNEL_SOLARIS */
144 #error "No applicable input method."
155 #ifndef CMDLINE_BUFFER_SIZE
156 #if defined(ARG_MAX) && (ARG_MAX < 4096)
157 #define CMDLINE_BUFFER_SIZE ARG_MAX
159 #define CMDLINE_BUFFER_SIZE 4096
163 #define PROCSTAT_NAME_LEN 256
164 typedef struct process_entry_s {
166 char name[PROCSTAT_NAME_LEN];
168 unsigned long num_proc;
169 unsigned long num_lwp;
170 unsigned long num_fd;
171 unsigned long vmem_size;
172 unsigned long vmem_rss;
173 unsigned long vmem_data;
174 unsigned long vmem_code;
175 unsigned long stack_size;
177 derive_t vmem_minflt_counter;
178 derive_t vmem_majflt_counter;
180 derive_t cpu_user_counter;
181 derive_t cpu_system_counter;
192 derive_t cswitch_vol;
193 derive_t cswitch_invol;
199 typedef struct procstat_entry_s {
203 derive_t vmem_minflt_counter;
204 derive_t vmem_majflt_counter;
206 derive_t cpu_user_counter;
207 derive_t cpu_system_counter;
217 derive_t cswitch_vol;
218 derive_t cswitch_invol;
220 struct procstat_entry_s *next;
223 typedef struct procstat {
224 char name[PROCSTAT_NAME_LEN];
229 unsigned long num_proc;
230 unsigned long num_lwp;
231 unsigned long num_fd;
232 unsigned long vmem_size;
233 unsigned long vmem_rss;
234 unsigned long vmem_data;
235 unsigned long vmem_code;
236 unsigned long stack_size;
238 derive_t vmem_minflt_counter;
239 derive_t vmem_majflt_counter;
241 derive_t cpu_user_counter;
242 derive_t cpu_system_counter;
252 derive_t cswitch_vol;
253 derive_t cswitch_invol;
256 _Bool report_ctx_switch;
258 struct procstat *next;
259 struct procstat_entry_s *instances;
262 static procstat_t *list_head_g = NULL;
264 static _Bool want_init = 1;
265 static _Bool report_ctx_switch = 0;
266 static _Bool report_fd_num = 0;
269 static mach_port_t port_host_self;
270 static mach_port_t port_task_self;
272 static processor_set_name_array_t pset_list;
273 static mach_msg_type_number_t pset_list_len;
274 /* #endif HAVE_THREAD_INFO */
277 static long pagesize_g;
278 static void ps_fill_details(const procstat_t *ps, process_entry_t *entry);
279 /* #endif KERNEL_LINUX */
281 #elif HAVE_LIBKVM_GETPROCS && \
282 (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD)
284 /* #endif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD ||
285 * HAVE_STRUCT_KINFO_PROC_OPENBSD) */
287 #elif HAVE_PROCINFO_H
288 static struct procentry64 procentry[MAXPROCENTRY];
289 static struct thrdentry64 thrdentry[MAXTHRDENTRY];
292 #ifndef _AIXVERSION_610
293 int getprocs64(void *procsinfo, int sizproc, void *fdsinfo, int sizfd,
294 pid_t *index, int count);
295 int getthrds64(pid_t, void *, int, tid64_t *, int);
297 int getargs(void *processBuffer, int bufferLen, char *argsBuffer, int argsLen);
298 #endif /* HAVE_PROCINFO_H */
300 /* put name of process from config to list_head_g tree
301 * list_head_g is a list of 'procstat_t' structs with
302 * processes names we want to watch */
303 static procstat_t *ps_list_register(const char *name, const char *regexp) {
308 new = calloc(1, sizeof(*new));
310 ERROR("processes plugin: ps_list_register: calloc failed.");
313 sstrncpy(new->name, name, sizeof(new->name));
321 new->cswitch_vol = -1;
322 new->cswitch_invol = -1;
324 new->report_fd_num = report_fd_num;
325 new->report_ctx_switch = report_ctx_switch;
328 if (regexp != NULL) {
329 DEBUG("ProcessMatch: adding \"%s\" as criteria to process %s.", regexp,
331 new->re = malloc(sizeof(*new->re));
332 if (new->re == NULL) {
333 ERROR("processes plugin: ps_list_register: malloc failed.");
338 status = regcomp(new->re, regexp, REG_EXTENDED | REG_NOSUB);
340 DEBUG("ProcessMatch: compiling the regular expression \"%s\" failed.",
348 if (regexp != NULL) {
349 ERROR("processes plugin: ps_list_register: "
350 "Regular expression \"%s\" found in config "
351 "file, but support for regular expressions "
352 "has been disabled at compile time.",
359 for (ptr = list_head_g; ptr != NULL; ptr = ptr->next) {
360 if (strcmp(ptr->name, name) == 0) {
361 WARNING("processes plugin: You have configured more "
362 "than one `Process' or "
363 "`ProcessMatch' with the same name. "
364 "All but the first setting will be "
373 if (ptr->next == NULL)
383 } /* void ps_list_register */
385 /* try to match name against entry, returns 1 if success */
386 static int ps_list_match(const char *name, const char *cmdline,
389 if (ps->re != NULL) {
394 if ((str == NULL) || (str[0] == 0))
399 status = regexec(ps->re, str,
407 if (strcmp(ps->name, name) == 0)
411 } /* int ps_list_match */
413 static void ps_update_counter(derive_t *group_counter, derive_t *curr_counter,
414 derive_t new_counter) {
415 unsigned long curr_value;
418 *curr_counter = new_counter;
422 if (new_counter < *curr_counter)
423 curr_value = new_counter + (ULONG_MAX - *curr_counter);
425 curr_value = new_counter - *curr_counter;
427 if (*group_counter == -1)
430 *curr_counter = new_counter;
431 *group_counter += curr_value;
434 /* add process entry to 'instances' of process 'name' (or refresh it) */
435 static void ps_list_add(const char *name, const char *cmdline,
436 process_entry_t *entry) {
437 procstat_entry_t *pse;
442 for (procstat_t *ps = list_head_g; ps != NULL; ps = ps->next) {
443 if ((ps_list_match(name, cmdline, ps)) == 0)
447 ps_fill_details(ps, entry);
450 for (pse = ps->instances; pse != NULL; pse = pse->next)
451 if ((pse->id == entry->id) || (pse->next == NULL))
454 if ((pse == NULL) || (pse->id != entry->id)) {
455 procstat_entry_t *new;
457 new = calloc(1, sizeof(*new));
472 ps->num_proc += entry->num_proc;
473 ps->num_lwp += entry->num_lwp;
474 ps->num_fd += entry->num_fd;
475 ps->vmem_size += entry->vmem_size;
476 ps->vmem_rss += entry->vmem_rss;
477 ps->vmem_data += entry->vmem_data;
478 ps->vmem_code += entry->vmem_code;
479 ps->stack_size += entry->stack_size;
481 if ((entry->io_rchar != -1) && (entry->io_wchar != -1)) {
482 ps_update_counter(&ps->io_rchar, &pse->io_rchar, entry->io_rchar);
483 ps_update_counter(&ps->io_wchar, &pse->io_wchar, entry->io_wchar);
486 if ((entry->io_syscr != -1) && (entry->io_syscw != -1)) {
487 ps_update_counter(&ps->io_syscr, &pse->io_syscr, entry->io_syscr);
488 ps_update_counter(&ps->io_syscw, &pse->io_syscw, entry->io_syscw);
491 if ((entry->io_diskr != -1) && (entry->io_diskw != -1)) {
492 ps_update_counter(&ps->io_diskr, &pse->io_diskr, entry->io_diskr);
493 ps_update_counter(&ps->io_diskw, &pse->io_diskw, entry->io_diskw);
496 if ((entry->cswitch_vol != -1) && (entry->cswitch_vol != -1)) {
497 ps_update_counter(&ps->cswitch_vol, &pse->cswitch_vol,
499 ps_update_counter(&ps->cswitch_invol, &pse->cswitch_invol,
500 entry->cswitch_invol);
503 ps_update_counter(&ps->vmem_minflt_counter, &pse->vmem_minflt_counter,
504 entry->vmem_minflt_counter);
505 ps_update_counter(&ps->vmem_majflt_counter, &pse->vmem_majflt_counter,
506 entry->vmem_majflt_counter);
508 ps_update_counter(&ps->cpu_user_counter, &pse->cpu_user_counter,
509 entry->cpu_user_counter);
510 ps_update_counter(&ps->cpu_system_counter, &pse->cpu_system_counter,
511 entry->cpu_system_counter);
515 /* remove old entries from instances of processes in list_head_g */
516 static void ps_list_reset(void) {
517 procstat_entry_t *pse;
518 procstat_entry_t *pse_prev;
520 for (procstat_t *ps = list_head_g; ps != NULL; ps = ps->next) {
532 while (pse != NULL) {
534 DEBUG("Removing this procstat entry cause it's too old: "
535 "id = %lu; name = %s;",
538 if (pse_prev == NULL) {
539 ps->instances = pse->next;
543 pse_prev->next = pse->next;
545 pse = pse_prev->next;
552 } /* while (pse != NULL) */
553 } /* for (ps = list_head_g; ps != NULL; ps = ps->next) */
556 static void ps_tune_instance(oconfig_item_t *ci, procstat_t *ps) {
557 for (int i = 0; i < ci->children_num; i++) {
558 oconfig_item_t *c = ci->children + i;
560 if (strcasecmp(c->key, "CollectContextSwitch") == 0)
561 cf_util_get_boolean(c, &ps->report_ctx_switch);
562 else if (strcasecmp(c->key, "CollectFileDescriptor") == 0)
563 cf_util_get_boolean(c, &ps->report_fd_num);
565 ERROR("processes plugin: Option `%s' not allowed here.", c->key);
567 } /* for (ci->children) */
568 } /* void ps_tune_instance */
570 /* put all pre-defined 'Process' names from config to list_head_g tree */
571 static int ps_config(oconfig_item_t *ci) {
573 const size_t max_procname_len = 15;
574 #elif KERNEL_SOLARIS || KERNEL_FREEBSD
575 const size_t max_procname_len = MAXCOMLEN - 1;
580 for (int i = 0; i < ci->children_num; ++i) {
581 oconfig_item_t *c = ci->children + i;
583 if (strcasecmp(c->key, "Process") == 0) {
584 if ((c->values_num != 1) || (OCONFIG_TYPE_STRING != c->values[0].type)) {
585 ERROR("processes plugin: `Process' expects exactly "
586 "one string argument (got %i).",
591 #if KERNEL_LINUX || KERNEL_SOLARIS || KERNEL_FREEBSD
592 if (strlen(c->values[0].value.string) > max_procname_len) {
593 WARNING("processes plugin: this platform has a %zu character limit "
594 "to process names. The `Process \"%s\"' option will "
595 "not work as expected.",
596 max_procname_len, c->values[0].value.string);
600 ps = ps_list_register(c->values[0].value.string, NULL);
602 if (c->children_num != 0 && ps != NULL)
603 ps_tune_instance(c, ps);
604 } else if (strcasecmp(c->key, "ProcessMatch") == 0) {
605 if ((c->values_num != 2) || (OCONFIG_TYPE_STRING != c->values[0].type) ||
606 (OCONFIG_TYPE_STRING != c->values[1].type)) {
607 ERROR("processes plugin: `ProcessMatch' needs exactly "
608 "two string arguments (got %i).",
613 ps = ps_list_register(c->values[0].value.string,
614 c->values[1].value.string);
616 if (c->children_num != 0 && ps != NULL)
617 ps_tune_instance(c, ps);
618 } else if (strcasecmp(c->key, "CollectContextSwitch") == 0) {
619 cf_util_get_boolean(c, &report_ctx_switch);
620 } else if (strcasecmp(c->key, "CollectFileDescriptor") == 0) {
621 cf_util_get_boolean(c, &report_fd_num);
623 ERROR("processes plugin: The `%s' configuration option is not "
624 "understood and will be ignored.",
633 static int ps_init(void) {
635 kern_return_t status;
637 port_host_self = mach_host_self();
638 port_task_self = mach_task_self();
640 if (pset_list != NULL) {
641 vm_deallocate(port_task_self, (vm_address_t)pset_list,
642 pset_list_len * sizeof(processor_set_t));
647 if ((status = host_processor_sets(port_host_self, &pset_list,
648 &pset_list_len)) != KERN_SUCCESS) {
649 ERROR("host_processor_sets failed: %s\n", mach_error_string(status));
654 /* #endif HAVE_THREAD_INFO */
657 pagesize_g = sysconf(_SC_PAGESIZE);
658 DEBUG("pagesize_g = %li; CONFIG_HZ = %i;", pagesize_g, CONFIG_HZ);
659 /* #endif KERNEL_LINUX */
661 #elif HAVE_LIBKVM_GETPROCS && \
662 (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD)
663 pagesize = getpagesize();
664 /* #endif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD ||
665 * HAVE_STRUCT_KINFO_PROC_OPENBSD) */
667 #elif HAVE_PROCINFO_H
668 pagesize = getpagesize();
669 #endif /* HAVE_PROCINFO_H */
674 /* submit global state (e.g.: qty of zombies, running, etc..) */
675 static void ps_submit_state(const char *state, double value) {
676 value_list_t vl = VALUE_LIST_INIT;
678 vl.values = &(value_t){.gauge = value};
680 sstrncpy(vl.plugin, "processes", sizeof(vl.plugin));
681 sstrncpy(vl.plugin_instance, "", sizeof(vl.plugin_instance));
682 sstrncpy(vl.type, "ps_state", sizeof(vl.type));
683 sstrncpy(vl.type_instance, state, sizeof(vl.type_instance));
685 plugin_dispatch_values(&vl);
688 /* submit info about specific process (e.g.: memory taken, cpu usage, etc..) */
689 static void ps_submit_proc_list(procstat_t *ps) {
690 value_list_t vl = VALUE_LIST_INIT;
694 sstrncpy(vl.plugin, "processes", sizeof(vl.plugin));
695 sstrncpy(vl.plugin_instance, ps->name, sizeof(vl.plugin_instance));
697 sstrncpy(vl.type, "ps_vm", sizeof(vl.type));
698 vl.values[0].gauge = ps->vmem_size;
700 plugin_dispatch_values(&vl);
702 sstrncpy(vl.type, "ps_rss", sizeof(vl.type));
703 vl.values[0].gauge = ps->vmem_rss;
705 plugin_dispatch_values(&vl);
707 sstrncpy(vl.type, "ps_data", sizeof(vl.type));
708 vl.values[0].gauge = ps->vmem_data;
710 plugin_dispatch_values(&vl);
712 sstrncpy(vl.type, "ps_code", sizeof(vl.type));
713 vl.values[0].gauge = ps->vmem_code;
715 plugin_dispatch_values(&vl);
717 sstrncpy(vl.type, "ps_stacksize", sizeof(vl.type));
718 vl.values[0].gauge = ps->stack_size;
720 plugin_dispatch_values(&vl);
722 sstrncpy(vl.type, "ps_cputime", sizeof(vl.type));
723 vl.values[0].derive = ps->cpu_user_counter;
724 vl.values[1].derive = ps->cpu_system_counter;
726 plugin_dispatch_values(&vl);
728 sstrncpy(vl.type, "ps_count", sizeof(vl.type));
729 vl.values[0].gauge = ps->num_proc;
730 vl.values[1].gauge = ps->num_lwp;
732 plugin_dispatch_values(&vl);
734 sstrncpy(vl.type, "ps_pagefaults", sizeof(vl.type));
735 vl.values[0].derive = ps->vmem_minflt_counter;
736 vl.values[1].derive = ps->vmem_majflt_counter;
738 plugin_dispatch_values(&vl);
740 if ((ps->io_rchar != -1) && (ps->io_wchar != -1)) {
741 sstrncpy(vl.type, "io_octets", sizeof(vl.type));
742 vl.values[0].derive = ps->io_rchar;
743 vl.values[1].derive = ps->io_wchar;
745 plugin_dispatch_values(&vl);
748 if ((ps->io_syscr != -1) && (ps->io_syscw != -1)) {
749 sstrncpy(vl.type, "io_ops", sizeof(vl.type));
750 vl.values[0].derive = ps->io_syscr;
751 vl.values[1].derive = ps->io_syscw;
753 plugin_dispatch_values(&vl);
756 if ((ps->io_diskr != -1) && (ps->io_diskw != -1)) {
757 sstrncpy(vl.type, "disk_octets", sizeof(vl.type));
758 vl.values[0].derive = ps->io_diskr;
759 vl.values[1].derive = ps->io_diskw;
761 plugin_dispatch_values(&vl);
764 if (ps->num_fd > 0) {
765 sstrncpy(vl.type, "file_handles", sizeof(vl.type));
766 vl.values[0].gauge = ps->num_fd;
768 plugin_dispatch_values(&vl);
771 if ((ps->cswitch_vol != -1) && (ps->cswitch_invol != -1)) {
772 sstrncpy(vl.type, "contextswitch", sizeof(vl.type));
773 sstrncpy(vl.type_instance, "voluntary", sizeof(vl.type_instance));
774 vl.values[0].derive = ps->cswitch_vol;
776 plugin_dispatch_values(&vl);
778 sstrncpy(vl.type, "contextswitch", sizeof(vl.type));
779 sstrncpy(vl.type_instance, "involuntary", sizeof(vl.type_instance));
780 vl.values[0].derive = ps->cswitch_invol;
782 plugin_dispatch_values(&vl);
785 DEBUG("name = %s; num_proc = %lu; num_lwp = %lu; num_fd = %lu; "
786 "vmem_size = %lu; vmem_rss = %lu; vmem_data = %lu; "
788 "vmem_minflt_counter = %" PRIi64 "; vmem_majflt_counter = %" PRIi64 "; "
789 "cpu_user_counter = %" PRIi64 "; cpu_system_counter = %" PRIi64 "; "
790 "io_rchar = %" PRIi64 "; io_wchar = %" PRIi64 "; "
791 "io_syscr = %" PRIi64 "; io_syscw = %" PRIi64 "; "
792 "io_diskr = %" PRIi64 "; io_diskw = %" PRIi64 "; "
793 "cswitch_vol = %" PRIi64 "; cswitch_invol = %" PRIi64 ";",
794 ps->name, ps->num_proc, ps->num_lwp, ps->num_fd, ps->vmem_size,
795 ps->vmem_rss, ps->vmem_data, ps->vmem_code, ps->vmem_minflt_counter,
796 ps->vmem_majflt_counter, ps->cpu_user_counter, ps->cpu_system_counter,
797 ps->io_rchar, ps->io_wchar, ps->io_syscr, ps->io_syscw, ps->io_diskr,
798 ps->io_diskw, ps->cswitch_vol, ps->cswitch_invol);
799 } /* void ps_submit_proc_list */
801 #if KERNEL_LINUX || KERNEL_SOLARIS
802 static void ps_submit_fork_rate(derive_t value) {
803 value_list_t vl = VALUE_LIST_INIT;
805 vl.values = &(value_t){.derive = value};
807 sstrncpy(vl.plugin, "processes", sizeof(vl.plugin));
808 sstrncpy(vl.plugin_instance, "", sizeof(vl.plugin_instance));
809 sstrncpy(vl.type, "fork_rate", sizeof(vl.type));
810 sstrncpy(vl.type_instance, "", sizeof(vl.type_instance));
812 plugin_dispatch_values(&vl);
814 #endif /* KERNEL_LINUX || KERNEL_SOLARIS*/
816 /* ------- additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
818 static int ps_read_tasks_status(process_entry_t *ps) {
824 derive_t cswitch_vol = 0;
825 derive_t cswitch_invol = 0;
830 snprintf(dirname, sizeof(dirname), "/proc/%li/task", ps->id);
832 if ((dh = opendir(dirname)) == NULL) {
833 DEBUG("Failed to open directory `%s'", dirname);
837 while ((ent = readdir(dh)) != NULL) {
840 if (!isdigit((int)ent->d_name[0]))
845 if (snprintf(filename, sizeof(filename), "/proc/%li/task/%s/status", ps->id,
846 tpid) >= sizeof(filename)) {
847 DEBUG("Filename too long: `%s'", filename);
851 if ((fh = fopen(filename, "r")) == NULL) {
852 DEBUG("Failed to open file `%s'", filename);
856 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
860 if (strncmp(buffer, "voluntary_ctxt_switches", 23) != 0 &&
861 strncmp(buffer, "nonvoluntary_ctxt_switches", 26) != 0)
864 numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
871 tmp = (derive_t)strtoll(fields[1], &endptr, /* base = */ 10);
872 if ((errno == 0) && (endptr != fields[1])) {
873 if (strncmp(buffer, "voluntary_ctxt_switches", 23) == 0) {
875 } else if (strncmp(buffer, "nonvoluntary_ctxt_switches", 26) == 0) {
876 cswitch_invol += tmp;
879 } /* while (fgets) */
883 WARNING("processes: fclose: %s",
884 sstrerror(errno, errbuf, sizeof(errbuf)));
889 ps->cswitch_vol = cswitch_vol;
890 ps->cswitch_invol = cswitch_invol;
893 } /* int *ps_read_tasks_status */
895 /* Read data from /proc/pid/status */
896 static int ps_read_status(long pid, process_entry_t *ps) {
900 unsigned long lib = 0;
901 unsigned long exe = 0;
902 unsigned long data = 0;
903 unsigned long threads = 0;
907 snprintf(filename, sizeof(filename), "/proc/%li/status", pid);
908 if ((fh = fopen(filename, "r")) == NULL)
911 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
915 if (strncmp(buffer, "Vm", 2) != 0 && strncmp(buffer, "Threads", 7) != 0)
918 numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
925 tmp = strtoul(fields[1], &endptr, /* base = */ 10);
926 if ((errno == 0) && (endptr != fields[1])) {
927 if (strncmp(buffer, "VmData", 6) == 0) {
929 } else if (strncmp(buffer, "VmLib", 5) == 0) {
931 } else if (strncmp(buffer, "VmExe", 5) == 0) {
933 } else if (strncmp(buffer, "Threads", 7) == 0) {
937 } /* while (fgets) */
941 WARNING("processes: fclose: %s", sstrerror(errno, errbuf, sizeof(errbuf)));
944 ps->vmem_data = data * 1024;
945 ps->vmem_code = (exe + lib) * 1024;
947 ps->num_lwp = threads;
950 } /* int *ps_read_status */
952 static int ps_read_io(process_entry_t *ps) {
960 snprintf(filename, sizeof(filename), "/proc/%li/io", ps->id);
961 if ((fh = fopen(filename, "r")) == NULL) {
962 DEBUG("ps_read_io: Failed to open file `%s'", filename);
966 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
967 derive_t *val = NULL;
971 if (strncasecmp(buffer, "rchar:", 6) == 0)
972 val = &(ps->io_rchar);
973 else if (strncasecmp(buffer, "wchar:", 6) == 0)
974 val = &(ps->io_wchar);
975 else if (strncasecmp(buffer, "syscr:", 6) == 0)
976 val = &(ps->io_syscr);
977 else if (strncasecmp(buffer, "syscw:", 6) == 0)
978 val = &(ps->io_syscw);
979 else if (strncasecmp(buffer, "read_bytes:", 11) == 0)
980 val = &(ps->io_diskr);
981 else if (strncasecmp(buffer, "write_bytes:", 12) == 0)
982 val = &(ps->io_diskw);
986 numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
993 tmp = strtoll(fields[1], &endptr, /* base = */ 10);
994 if ((errno != 0) || (endptr == fields[1]))
997 *val = (derive_t)tmp;
998 } /* while (fgets) */
1002 WARNING("processes: fclose: %s", sstrerror(errno, errbuf, sizeof(errbuf)));
1005 } /* int ps_read_io (...) */
1007 static int ps_count_fd(int pid) {
1013 snprintf(dirname, sizeof(dirname), "/proc/%i/fd", pid);
1015 if ((dh = opendir(dirname)) == NULL) {
1016 DEBUG("Failed to open directory `%s'", dirname);
1019 while ((ent = readdir(dh)) != NULL) {
1020 if (!isdigit((int)ent->d_name[0]))
1027 return (count >= 1) ? count : 1;
1028 } /* int ps_count_fd (pid) */
1030 static void ps_fill_details(const procstat_t *ps, process_entry_t *entry) {
1031 if (entry->has_io == 0) {
1036 if (ps->report_ctx_switch) {
1037 if (entry->has_cswitch == 0) {
1038 ps_read_tasks_status(entry);
1039 entry->has_cswitch = 1;
1043 if (ps->report_fd_num) {
1045 if (entry->has_fd == 0 && (num_fd = ps_count_fd(entry->id)) > 0) {
1046 entry->num_fd = num_fd;
1050 } /* void ps_fill_details (...) */
1052 static int ps_read_process(long pid, process_entry_t *ps, char *state) {
1062 size_t name_start_pos;
1063 size_t name_end_pos;
1066 derive_t cpu_user_counter;
1067 derive_t cpu_system_counter;
1068 long long unsigned vmem_size;
1069 long long unsigned vmem_rss;
1070 long long unsigned stack_size;
1074 snprintf(filename, sizeof(filename), "/proc/%li/stat", pid);
1076 status = read_file_contents(filename, buffer, sizeof(buffer) - 1);
1079 buffer_len = (size_t)status;
1080 buffer[buffer_len] = 0;
1082 /* The name of the process is enclosed in parens. Since the name can
1083 * contain parens itself, spaces, numbers and pretty much everything
1084 * else, use these to determine the process name. We don't use
1085 * strchr(3) and strrchr(3) to avoid pointer arithmetic which would
1086 * otherwise be required to determine name_len. */
1088 while (name_start_pos < buffer_len && buffer[name_start_pos] != '(')
1091 name_end_pos = buffer_len;
1092 while (name_end_pos > 0 && buffer[name_end_pos] != ')')
1095 /* Either '(' or ')' is not found or they are in the wrong order.
1096 * Anyway, something weird that shouldn't happen ever. */
1097 if (name_start_pos >= name_end_pos) {
1098 ERROR("processes plugin: name_start_pos = %zu >= name_end_pos = %zu",
1099 name_start_pos, name_end_pos);
1103 name_len = (name_end_pos - name_start_pos) - 1;
1104 if (name_len >= sizeof(ps->name))
1105 name_len = sizeof(ps->name) - 1;
1107 sstrncpy(ps->name, &buffer[name_start_pos + 1], name_len + 1);
1109 if ((buffer_len - name_end_pos) < 2)
1111 buffer_ptr = &buffer[name_end_pos + 2];
1113 fields_len = strsplit(buffer_ptr, fields, STATIC_ARRAY_SIZE(fields));
1114 if (fields_len < 22) {
1115 DEBUG("processes plugin: ps_read_process (pid = %li):"
1116 " `%s' has only %i fields..",
1117 pid, filename, fields_len);
1121 *state = fields[0][0];
1123 if (*state == 'Z') {
1127 ps->num_lwp = strtoul(fields[17], /* endptr = */ NULL, /* base = */ 10);
1128 if ((ps_read_status(pid, ps)) != 0) {
1132 DEBUG("ps_read_process: did not get vmem data for pid %li", pid);
1134 if (ps->num_lwp == 0)
1139 /* Leave the rest at zero if this is only a zombi */
1140 if (ps->num_proc == 0) {
1141 DEBUG("processes plugin: This is only a zombie: pid = %li; "
1147 cpu_user_counter = atoll(fields[11]);
1148 cpu_system_counter = atoll(fields[12]);
1149 vmem_size = atoll(fields[20]);
1150 vmem_rss = atoll(fields[21]);
1151 ps->vmem_minflt_counter = atol(fields[7]);
1152 ps->vmem_majflt_counter = atol(fields[9]);
1155 unsigned long long stack_start = atoll(fields[25]);
1156 unsigned long long stack_ptr = atoll(fields[26]);
1158 stack_size = (stack_start > stack_ptr) ? stack_start - stack_ptr
1159 : stack_ptr - stack_start;
1162 /* Convert jiffies to useconds */
1163 cpu_user_counter = cpu_user_counter * 1000000 / CONFIG_HZ;
1164 cpu_system_counter = cpu_system_counter * 1000000 / CONFIG_HZ;
1165 vmem_rss = vmem_rss * pagesize_g;
1167 ps->cpu_user_counter = cpu_user_counter;
1168 ps->cpu_system_counter = cpu_system_counter;
1169 ps->vmem_size = (unsigned long)vmem_size;
1170 ps->vmem_rss = (unsigned long)vmem_rss;
1171 ps->stack_size = (unsigned long)stack_size;
1173 /* no data by default. May be filled by ps_fill_details () */
1181 ps->cswitch_vol = -1;
1182 ps->cswitch_invol = -1;
1186 } /* int ps_read_process (...) */
1188 static char *ps_get_cmdline(long pid, char *name, char *buf, size_t buf_len) {
1192 char file[PATH_MAX];
1197 if ((pid < 1) || (NULL == buf) || (buf_len < 2))
1200 snprintf(file, sizeof(file), "/proc/%li/cmdline", pid);
1203 fd = open(file, O_RDONLY);
1206 /* ENOENT means the process exited while we were handling it.
1207 * Don't complain about this, it only fills the logs. */
1208 if (errno != ENOENT)
1209 WARNING("processes plugin: Failed to open `%s': %s.", file,
1210 sstrerror(errno, errbuf, sizeof(errbuf)));
1222 status = read(fd, (void *)buf_ptr, len);
1227 if ((EAGAIN == errno) || (EINTR == errno))
1230 WARNING("processes plugin: Failed to read from `%s': %s.", file,
1231 sstrerror(errno, errbuf, sizeof(errbuf)));
1251 /* cmdline not available; e.g. kernel thread, zombie */
1255 snprintf(buf, buf_len, "[%s]", name);
1259 assert(n <= buf_len);
1266 /* remove trailing whitespace */
1267 while ((n > 0) && (isspace(buf[n]) || ('\0' == buf[n]))) {
1272 /* arguments are separated by '\0' in /proc/<pid>/cmdline */
1279 } /* char *ps_get_cmdline (...) */
1281 static int read_fork_rate(void) {
1285 _Bool value_valid = 0;
1287 proc_stat = fopen("/proc/stat", "r");
1288 if (proc_stat == NULL) {
1290 ERROR("processes plugin: fopen (/proc/stat) failed: %s",
1291 sstrerror(errno, errbuf, sizeof(errbuf)));
1295 while (fgets(buffer, sizeof(buffer), proc_stat) != NULL) {
1300 fields_num = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
1301 if (fields_num != 2)
1304 if (strcmp("processes", fields[0]) != 0)
1307 status = parse_value(fields[1], &value, DS_TYPE_DERIVE);
1318 ps_submit_fork_rate(value.derive);
1321 #endif /*KERNEL_LINUX */
1324 static char *ps_get_cmdline(long pid,
1325 char *name __attribute__((unused)), /* {{{ */
1326 char *buffer, size_t buffer_size) {
1327 char path[PATH_MAX];
1331 snprintf(path, sizeof(path), "/proc/%li/psinfo", pid);
1333 status = read_file_contents(path, (void *)&info, sizeof(info));
1334 if ((status < 0) || (((size_t)status) != sizeof(info))) {
1335 ERROR("processes plugin: Unexpected return value "
1336 "while reading \"%s\": "
1337 "Returned %zd but expected %zu.",
1338 path, status, buffer_size);
1342 info.pr_psargs[sizeof(info.pr_psargs) - 1] = 0;
1343 sstrncpy(buffer, info.pr_psargs, buffer_size);
1346 } /* }}} int ps_get_cmdline */
1349 * Reads process information on the Solaris OS. The information comes mainly
1351 * /proc/PID/status, /proc/PID/psinfo and /proc/PID/usage
1352 * The values for input and ouput chars are calculated "by hand"
1353 * Added a few "solaris" specific process states as well
1355 static int ps_read_process(long pid, process_entry_t *ps, char *state) {
1357 char f_psinfo[64], f_usage[64];
1360 pstatus_t *myStatus;
1364 snprintf(filename, sizeof(filename), "/proc/%li/status", pid);
1365 snprintf(f_psinfo, sizeof(f_psinfo), "/proc/%li/psinfo", pid);
1366 snprintf(f_usage, sizeof(f_usage), "/proc/%li/usage", pid);
1368 buffer = calloc(1, sizeof(pstatus_t));
1369 read_file_contents(filename, buffer, sizeof(pstatus_t));
1370 myStatus = (pstatus_t *)buffer;
1372 buffer = calloc(1, sizeof(psinfo_t));
1373 read_file_contents(f_psinfo, buffer, sizeof(psinfo_t));
1374 myInfo = (psinfo_t *)buffer;
1376 buffer = calloc(1, sizeof(prusage_t));
1377 read_file_contents(f_usage, buffer, sizeof(prusage_t));
1378 myUsage = (prusage_t *)buffer;
1380 sstrncpy(ps->name, myInfo->pr_fname, sizeof(myInfo->pr_fname));
1381 ps->num_lwp = myStatus->pr_nlwp;
1382 if (myInfo->pr_wstat != 0) {
1393 ps->num_lwp = myInfo->pr_nlwp;
1397 * Convert system time and user time from nanoseconds to microseconds
1398 * for compatibility with the linux module
1400 ps->cpu_system_counter = myStatus->pr_stime.tv_nsec / 1000;
1401 ps->cpu_user_counter = myStatus->pr_utime.tv_nsec / 1000;
1404 * Convert rssize from KB to bytes to be consistent w/ the linux module
1406 ps->vmem_rss = myInfo->pr_rssize * 1024;
1407 ps->vmem_size = myInfo->pr_size * 1024;
1408 ps->vmem_minflt_counter = myUsage->pr_minf;
1409 ps->vmem_majflt_counter = myUsage->pr_majf;
1412 * TODO: Data and code segment calculations for Solaris
1417 ps->stack_size = myStatus->pr_stksize;
1420 * TODO: File descriptor count for Solaris
1425 * Calculating input/ouput chars
1426 * Formula used is total chars / total blocks => chars/block
1427 * then convert input/output blocks to chars
1429 ulong_t tot_chars = myUsage->pr_ioch;
1430 ulong_t tot_blocks = myUsage->pr_inblk + myUsage->pr_oublk;
1431 ulong_t chars_per_block = 1;
1432 if (tot_blocks != 0)
1433 chars_per_block = tot_chars / tot_blocks;
1434 ps->io_rchar = myUsage->pr_inblk * chars_per_block;
1435 ps->io_wchar = myUsage->pr_oublk * chars_per_block;
1436 ps->io_syscr = myUsage->pr_sysc;
1437 ps->io_syscw = myUsage->pr_sysc;
1442 * TODO: context switch counters for Solaris
1444 ps->cswitch_vol = -1;
1445 ps->cswitch_invol = -1;
1448 * TODO: Find way of setting BLOCKED and PAGING status
1452 if (myStatus->pr_flags & PR_ASLEEP)
1454 else if (myStatus->pr_flags & PR_STOPPED)
1456 else if (myStatus->pr_flags & PR_DETACH)
1458 else if (myStatus->pr_flags & PR_DAEMON)
1460 else if (myStatus->pr_flags & PR_ISSYS)
1462 else if (myStatus->pr_flags & PR_ORPHAN)
1473 * Reads the number of threads created since the last reboot. On Solaris these
1474 * are retrieved from kstat (module cpu, name sys, class misc, stat nthreads).
1475 * The result is the sum for all the threads created on each cpu
1477 static int read_fork_rate(void) {
1478 extern kstat_ctl_t *kc;
1479 derive_t result = 0;
1484 for (kstat_t *ksp_chain = kc->kc_chain; ksp_chain != NULL;
1485 ksp_chain = ksp_chain->ks_next) {
1486 if ((strcmp(ksp_chain->ks_module, "cpu") == 0) &&
1487 (strcmp(ksp_chain->ks_name, "sys") == 0) &&
1488 (strcmp(ksp_chain->ks_class, "misc") == 0)) {
1491 kstat_read(kc, ksp_chain, NULL);
1493 tmp = get_kstat_value(ksp_chain, "nthreads");
1499 ps_submit_fork_rate(result);
1502 #endif /* KERNEL_SOLARIS */
1504 #if HAVE_THREAD_INFO
1505 static int mach_get_task_name(task_t t, int *pid, char *name,
1506 size_t name_max_len) {
1509 struct kinfo_proc kp;
1514 mib[2] = KERN_PROC_PID;
1516 if (pid_for_task(t, pid) != KERN_SUCCESS)
1520 kp_size = sizeof(kp);
1521 if (sysctl(mib, 4, &kp, &kp_size, NULL, 0) != 0)
1524 if (name_max_len > (MAXCOMLEN + 1))
1525 name_max_len = MAXCOMLEN + 1;
1527 strncpy(name, kp.kp_proc.p_comm, name_max_len - 1);
1528 name[name_max_len - 1] = '\0';
1530 DEBUG("pid = %i; name = %s;", *pid, name);
1532 /* We don't do the special handling for `p_comm == "LaunchCFMApp"' as
1533 * `top' does it, because it is a lot of work and only used when
1534 * debugging. -octo */
1538 #endif /* HAVE_THREAD_INFO */
1539 /* ------- end of additional functions for KERNEL_LINUX/HAVE_THREAD_INFO -------
1542 /* do actual readings from kernel */
1543 static int ps_read(void) {
1544 #if HAVE_THREAD_INFO
1545 kern_return_t status;
1547 processor_set_t port_pset_priv;
1549 task_array_t task_list;
1550 mach_msg_type_number_t task_list_len;
1553 char task_name[MAXCOMLEN + 1];
1555 thread_act_array_t thread_list;
1556 mach_msg_type_number_t thread_list_len;
1557 thread_basic_info_data_t thread_data;
1558 mach_msg_type_number_t thread_data_len;
1567 process_entry_t pse;
1572 * The Mach-concept is a little different from the traditional UNIX
1573 * concept: All the work is done in threads. Threads are contained in
1574 * `tasks'. Therefore, `task status' doesn't make much sense, since
1575 * it's actually a `thread status'.
1576 * Tasks are assigned to sets of processors, so that's where you go to
1579 for (mach_msg_type_number_t pset = 0; pset < pset_list_len; pset++) {
1580 if ((status = host_processor_set_priv(port_host_self, pset_list[pset],
1581 &port_pset_priv)) != KERN_SUCCESS) {
1582 ERROR("host_processor_set_priv failed: %s\n", mach_error_string(status));
1586 if ((status = processor_set_tasks(port_pset_priv, &task_list,
1587 &task_list_len)) != KERN_SUCCESS) {
1588 ERROR("processor_set_tasks failed: %s\n", mach_error_string(status));
1589 mach_port_deallocate(port_task_self, port_pset_priv);
1593 for (mach_msg_type_number_t task = 0; task < task_list_len; task++) {
1595 if (mach_get_task_name(task_list[task], &task_pid, task_name,
1596 PROCSTAT_NAME_LEN) == 0) {
1597 /* search for at least one match */
1598 for (ps = list_head_g; ps != NULL; ps = ps->next)
1599 /* FIXME: cmdline should be here instead of NULL */
1600 if (ps_list_match(task_name, NULL, ps) == 1)
1604 /* Collect more detailed statistics for this process */
1606 task_basic_info_data_t task_basic_info;
1607 mach_msg_type_number_t task_basic_info_len;
1608 task_events_info_data_t task_events_info;
1609 mach_msg_type_number_t task_events_info_len;
1610 task_absolutetime_info_data_t task_absolutetime_info;
1611 mach_msg_type_number_t task_absolutetime_info_len;
1613 memset(&pse, '\0', sizeof(pse));
1616 task_basic_info_len = TASK_BASIC_INFO_COUNT;
1617 status = task_info(task_list[task], TASK_BASIC_INFO,
1618 (task_info_t)&task_basic_info, &task_basic_info_len);
1619 if (status != KERN_SUCCESS) {
1620 ERROR("task_info failed: %s", mach_error_string(status));
1621 continue; /* with next thread_list */
1624 task_events_info_len = TASK_EVENTS_INFO_COUNT;
1626 task_info(task_list[task], TASK_EVENTS_INFO,
1627 (task_info_t)&task_events_info, &task_events_info_len);
1628 if (status != KERN_SUCCESS) {
1629 ERROR("task_info failed: %s", mach_error_string(status));
1630 continue; /* with next thread_list */
1633 task_absolutetime_info_len = TASK_ABSOLUTETIME_INFO_COUNT;
1634 status = task_info(task_list[task], TASK_ABSOLUTETIME_INFO,
1635 (task_info_t)&task_absolutetime_info,
1636 &task_absolutetime_info_len);
1637 if (status != KERN_SUCCESS) {
1638 ERROR("task_info failed: %s", mach_error_string(status));
1639 continue; /* with next thread_list */
1643 pse.vmem_size = task_basic_info.virtual_size;
1644 pse.vmem_rss = task_basic_info.resident_size;
1645 /* Does not seem to be easily exposed */
1656 /* File descriptor count not implemented */
1659 pse.vmem_minflt_counter = task_events_info.cow_faults;
1660 pse.vmem_majflt_counter = task_events_info.faults;
1662 pse.cpu_user_counter = task_absolutetime_info.total_user;
1663 pse.cpu_system_counter = task_absolutetime_info.total_system;
1665 /* context switch counters not implemented */
1666 pse.cswitch_vol = -1;
1667 pse.cswitch_invol = -1;
1670 status = task_threads(task_list[task], &thread_list, &thread_list_len);
1671 if (status != KERN_SUCCESS) {
1672 /* Apple's `top' treats this case a zombie. It
1673 * makes sense to some extend: A `zombie'
1674 * thread is nonsense, since the task/process
1677 DEBUG("task_threads failed: %s", mach_error_string(status));
1678 if (task_list[task] != port_task_self)
1679 mach_port_deallocate(port_task_self, task_list[task]);
1680 continue; /* with next task_list */
1683 for (mach_msg_type_number_t thread = 0; thread < thread_list_len;
1685 thread_data_len = THREAD_BASIC_INFO_COUNT;
1686 status = thread_info(thread_list[thread], THREAD_BASIC_INFO,
1687 (thread_info_t)&thread_data, &thread_data_len);
1688 if (status != KERN_SUCCESS) {
1689 ERROR("thread_info failed: %s", mach_error_string(status));
1690 if (task_list[task] != port_task_self)
1691 mach_port_deallocate(port_task_self, thread_list[thread]);
1692 continue; /* with next thread_list */
1698 switch (thread_data.run_state) {
1699 case TH_STATE_RUNNING:
1702 case TH_STATE_STOPPED:
1703 /* What exactly is `halted'? */
1704 case TH_STATE_HALTED:
1707 case TH_STATE_WAITING:
1710 case TH_STATE_UNINTERRUPTIBLE:
1713 /* There is no `zombie' case here,
1714 * since there are no zombie-threads.
1715 * There's only zombie tasks, which are
1718 WARNING("Unknown thread status: %i", thread_data.run_state);
1720 } /* switch (thread_data.run_state) */
1722 if (task_list[task] != port_task_self) {
1723 status = mach_port_deallocate(port_task_self, thread_list[thread]);
1724 if (status != KERN_SUCCESS)
1725 ERROR("mach_port_deallocate failed: %s", mach_error_string(status));
1727 } /* for (thread_list) */
1729 if ((status = vm_deallocate(port_task_self, (vm_address_t)thread_list,
1730 thread_list_len * sizeof(thread_act_t))) !=
1732 ERROR("vm_deallocate failed: %s", mach_error_string(status));
1735 thread_list_len = 0;
1737 /* Only deallocate the task port, if it isn't our own.
1738 * Don't know what would happen in that case, but this
1739 * is what Apple's top does.. ;) */
1740 if (task_list[task] != port_task_self) {
1741 status = mach_port_deallocate(port_task_self, task_list[task]);
1742 if (status != KERN_SUCCESS)
1743 ERROR("mach_port_deallocate failed: %s", mach_error_string(status));
1747 /* FIXME: cmdline should be here instead of NULL */
1748 ps_list_add(task_name, NULL, &pse);
1749 } /* for (task_list) */
1751 if ((status = vm_deallocate(port_task_self, (vm_address_t)task_list,
1752 task_list_len * sizeof(task_t))) !=
1754 ERROR("vm_deallocate failed: %s", mach_error_string(status));
1759 if ((status = mach_port_deallocate(port_task_self, port_pset_priv)) !=
1761 ERROR("mach_port_deallocate failed: %s", mach_error_string(status));
1763 } /* for (pset_list) */
1765 ps_submit_state("running", running);
1766 ps_submit_state("sleeping", sleeping);
1767 ps_submit_state("zombies", zombies);
1768 ps_submit_state("stopped", stopped);
1769 ps_submit_state("blocked", blocked);
1771 for (ps = list_head_g; ps != NULL; ps = ps->next)
1772 ps_submit_proc_list(ps);
1773 /* #endif HAVE_THREAD_INFO */
1787 char cmdline[CMDLINE_BUFFER_SIZE];
1790 process_entry_t pse;
1793 running = sleeping = zombies = stopped = paging = blocked = 0;
1796 if ((proc = opendir("/proc")) == NULL) {
1798 ERROR("Cannot open `/proc': %s", sstrerror(errno, errbuf, sizeof(errbuf)));
1802 while ((ent = readdir(proc)) != NULL) {
1803 if (!isdigit(ent->d_name[0]))
1806 if ((pid = atol(ent->d_name)) < 1)
1809 memset(&pse, 0, sizeof(pse));
1812 status = ps_read_process(pid, &pse, &state);
1814 DEBUG("ps_read_process failed: %i", status);
1839 ps_list_add(pse.name,
1840 ps_get_cmdline(pid, pse.name, cmdline, sizeof(cmdline)), &pse);
1845 ps_submit_state("running", running);
1846 ps_submit_state("sleeping", sleeping);
1847 ps_submit_state("zombies", zombies);
1848 ps_submit_state("stopped", stopped);
1849 ps_submit_state("paging", paging);
1850 ps_submit_state("blocked", blocked);
1852 for (procstat_t *ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1853 ps_submit_proc_list(ps_ptr);
1856 /* #endif KERNEL_LINUX */
1858 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD
1868 char errbuf[_POSIX2_LINE_MAX];
1869 struct kinfo_proc *procs; /* array of processes */
1870 struct kinfo_proc *proc_ptr = NULL;
1871 int count; /* returns number of processes */
1873 process_entry_t pse;
1877 /* Open the kvm interface, get a descriptor */
1878 kd = kvm_openfiles(NULL, "/dev/null", NULL, 0, errbuf);
1880 ERROR("processes plugin: Cannot open kvm interface: %s", errbuf);
1884 /* Get the list of processes. */
1885 procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, &count);
1886 if (procs == NULL) {
1887 ERROR("processes plugin: Cannot get kvm processes list: %s",
1893 /* Iterate through the processes in kinfo_proc */
1894 for (int i = 0; i < count; i++) {
1895 /* Create only one process list entry per _process_, i.e.
1896 * filter out threads (duplicate PID entries). */
1897 if ((proc_ptr == NULL) || (proc_ptr->ki_pid != procs[i].ki_pid)) {
1898 char cmdline[CMDLINE_BUFFER_SIZE] = "";
1899 _Bool have_cmdline = 0;
1901 proc_ptr = &(procs[i]);
1902 /* Don't probe system processes and processes without arguments */
1903 if (((procs[i].ki_flag & P_SYSTEM) == 0) && (procs[i].ki_args != NULL)) {
1908 /* retrieve the arguments */
1909 argv = kvm_getargv(kd, proc_ptr, /* nchr = */ 0);
1911 if ((argv != NULL) && (argv[0] != NULL)) {
1912 while (argv[argc] != NULL)
1915 status = strjoin(cmdline, sizeof(cmdline), argv, argc, " ");
1917 WARNING("processes plugin: Command line did not fit into buffer.");
1921 } /* if (process has argument list) */
1923 memset(&pse, 0, sizeof(pse));
1924 pse.id = procs[i].ki_pid;
1927 pse.num_lwp = procs[i].ki_numthreads;
1929 pse.vmem_size = procs[i].ki_size;
1930 pse.vmem_rss = procs[i].ki_rssize * pagesize;
1931 pse.vmem_data = procs[i].ki_dsize * pagesize;
1932 pse.vmem_code = procs[i].ki_tsize * pagesize;
1933 pse.stack_size = procs[i].ki_ssize * pagesize;
1934 pse.vmem_minflt_counter = procs[i].ki_rusage.ru_minflt;
1935 pse.vmem_majflt_counter = procs[i].ki_rusage.ru_majflt;
1937 pse.cpu_user_counter = 0;
1938 pse.cpu_system_counter = 0;
1940 * The u-area might be swapped out, and we can't get
1941 * at it because we have a crashdump and no swap.
1942 * If it's here fill in these fields, otherwise, just
1945 if (procs[i].ki_flag & P_INMEM) {
1946 pse.cpu_user_counter = procs[i].ki_rusage.ru_utime.tv_usec +
1947 (1000000lu * procs[i].ki_rusage.ru_utime.tv_sec);
1948 pse.cpu_system_counter =
1949 procs[i].ki_rusage.ru_stime.tv_usec +
1950 (1000000lu * procs[i].ki_rusage.ru_stime.tv_sec);
1961 /* file descriptor count not implemented */
1964 /* context switch counters not implemented */
1965 pse.cswitch_vol = -1;
1966 pse.cswitch_invol = -1;
1968 ps_list_add(procs[i].ki_comm, have_cmdline ? cmdline : NULL, &pse);
1970 switch (procs[i].ki_stat) {
1993 } /* if ((proc_ptr == NULL) || (proc_ptr->ki_pid != procs[i].ki_pid)) */
1998 ps_submit_state("running", running);
1999 ps_submit_state("sleeping", sleeping);
2000 ps_submit_state("zombies", zombies);
2001 ps_submit_state("stopped", stopped);
2002 ps_submit_state("blocked", blocked);
2003 ps_submit_state("idle", idle);
2004 ps_submit_state("wait", wait);
2006 for (procstat_t *ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
2007 ps_submit_proc_list(ps_ptr);
2008 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
2010 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_OPENBSD
2021 struct kinfo_proc *procs; /* array of processes */
2022 struct kinfo_proc *proc_ptr = NULL;
2023 int count; /* returns number of processes */
2025 process_entry_t pse;
2029 /* Open the kvm interface, get a descriptor */
2030 kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf);
2032 ERROR("processes plugin: Cannot open kvm interface: %s", errbuf);
2036 /* Get the list of processes. */
2037 procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc), &count);
2038 if (procs == NULL) {
2039 ERROR("processes plugin: Cannot get kvm processes list: %s",
2045 /* Iterate through the processes in kinfo_proc */
2046 for (int i = 0; i < count; i++) {
2047 /* Create only one process list entry per _process_, i.e.
2048 * filter out threads (duplicate PID entries). */
2049 if ((proc_ptr == NULL) || (proc_ptr->p_pid != procs[i].p_pid)) {
2050 char cmdline[CMDLINE_BUFFER_SIZE] = "";
2051 _Bool have_cmdline = 0;
2053 proc_ptr = &(procs[i]);
2054 /* Don't probe zombie processes */
2055 if (!P_ZOMBIE(proc_ptr)) {
2060 /* retrieve the arguments */
2061 argv = kvm_getargv(kd, proc_ptr, /* nchr = */ 0);
2063 if ((argv != NULL) && (argv[0] != NULL)) {
2064 while (argv[argc] != NULL)
2067 status = strjoin(cmdline, sizeof(cmdline), argv, argc, " ");
2069 WARNING("processes plugin: Command line did not fit into buffer.");
2073 } /* if (process has argument list) */
2075 memset(&pse, 0, sizeof(pse));
2076 pse.id = procs[i].p_pid;
2079 pse.num_lwp = 1; /* XXX: accumulate p_tid values for a single p_pid ? */
2081 pse.vmem_rss = procs[i].p_vm_rssize * pagesize;
2082 pse.vmem_data = procs[i].p_vm_dsize * pagesize;
2083 pse.vmem_code = procs[i].p_vm_tsize * pagesize;
2084 pse.stack_size = procs[i].p_vm_ssize * pagesize;
2085 pse.vmem_size = pse.stack_size + pse.vmem_code + pse.vmem_data;
2086 pse.vmem_minflt_counter = procs[i].p_uru_minflt;
2087 pse.vmem_majflt_counter = procs[i].p_uru_majflt;
2089 pse.cpu_user_counter =
2090 procs[i].p_uutime_usec + (1000000lu * procs[i].p_uutime_sec);
2091 pse.cpu_system_counter =
2092 procs[i].p_ustime_usec + (1000000lu * procs[i].p_ustime_sec);
2102 /* file descriptor count not implemented */
2105 /* context switch counters not implemented */
2106 pse.cswitch_vol = -1;
2107 pse.cswitch_invol = -1;
2109 ps_list_add(procs[i].p_comm, have_cmdline ? cmdline : NULL, &pse);
2111 switch (procs[i].p_stat) {
2134 } /* if ((proc_ptr == NULL) || (proc_ptr->p_pid != procs[i].p_pid)) */
2139 ps_submit_state("running", running);
2140 ps_submit_state("sleeping", sleeping);
2141 ps_submit_state("zombies", zombies);
2142 ps_submit_state("stopped", stopped);
2143 ps_submit_state("onproc", onproc);
2144 ps_submit_state("idle", idle);
2145 ps_submit_state("dead", dead);
2147 for (procstat_t *ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
2148 ps_submit_proc_list(ps_ptr);
2149 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_OPENBSD */
2151 #elif HAVE_PROCINFO_H
2163 process_entry_t pse;
2166 while ((nprocs = getprocs64(procentry, sizeof(struct procentry64),
2167 /* fdsinfo = */ NULL, sizeof(struct fdsinfo64),
2168 &pindex, MAXPROCENTRY)) > 0) {
2169 for (int i = 0; i < nprocs; i++) {
2172 char arglist[MAXARGLN + 1];
2176 if (procentry[i].pi_state == SNONE)
2178 /* if (procentry[i].pi_state == SZOMB) FIXME */
2180 cmdline = procentry[i].pi_comm;
2181 cargs = procentry[i].pi_comm;
2182 if (procentry[i].pi_flags & SKPROC) {
2183 if (procentry[i].pi_pid == 0)
2184 cmdline = "swapper";
2187 if (getargs(&procentry[i], sizeof(struct procentry64), arglist,
2192 while (++n < MAXARGLN) {
2193 if (arglist[n] == '\0') {
2194 if (arglist[n + 1] == '\0')
2203 memset(&pse, 0, sizeof(pse));
2205 pse.id = procentry[i].pi_pid;
2206 pse.num_lwp = procentry[i].pi_thcount;
2210 while ((nthreads = getthrds64(procentry[i].pi_pid, thrdentry,
2211 sizeof(struct thrdentry64), &thindex,
2212 MAXTHRDENTRY)) > 0) {
2215 for (j = 0; j < nthreads; j++) {
2216 switch (thrdentry[j].ti_state) {
2217 /* case TSNONE: break; */
2220 break; /* FIXME is really blocked */
2238 if (nthreads < MAXTHRDENTRY)
2242 /* tv_usec is nanosec ??? */
2243 pse.cpu_user_counter = procentry[i].pi_ru.ru_utime.tv_sec * 1000000 +
2244 procentry[i].pi_ru.ru_utime.tv_usec / 1000;
2246 /* tv_usec is nanosec ??? */
2247 pse.cpu_system_counter = procentry[i].pi_ru.ru_stime.tv_sec * 1000000 +
2248 procentry[i].pi_ru.ru_stime.tv_usec / 1000;
2250 pse.vmem_minflt_counter = procentry[i].pi_minflt;
2251 pse.vmem_majflt_counter = procentry[i].pi_majflt;
2253 pse.vmem_size = procentry[i].pi_tsize + procentry[i].pi_dvm * pagesize;
2254 pse.vmem_rss = (procentry[i].pi_drss + procentry[i].pi_trss) * pagesize;
2255 /* Not supported/implemented */
2269 pse.cswitch_vol = -1;
2270 pse.cswitch_invol = -1;
2272 ps_list_add(cmdline, cargs, &pse);
2273 } /* for (i = 0 .. nprocs) */
2275 if (nprocs < MAXPROCENTRY)
2277 } /* while (getprocs64() > 0) */
2278 ps_submit_state("running", running);
2279 ps_submit_state("sleeping", sleeping);
2280 ps_submit_state("zombies", zombies);
2281 ps_submit_state("stopped", stopped);
2282 ps_submit_state("paging", paging);
2283 ps_submit_state("blocked", blocked);
2285 for (procstat_t *ps = list_head_g; ps != NULL; ps = ps->next)
2286 ps_submit_proc_list(ps);
2287 /* #endif HAVE_PROCINFO_H */
2289 #elif KERNEL_SOLARIS
2291 * The Solaris section adds a few more process states and removes some
2292 * process states compared to linux. Most notably there is no "PAGING"
2293 * and "BLOCKED" state for a process. The rest is similar to the linux
2311 char cmdline[PRARGSZ];
2315 proc = opendir("/proc");
2319 while ((ent = readdir(proc)) != NULL) {
2321 process_entry_t pse;
2324 if (!isdigit((int)ent->d_name[0]))
2327 pid = strtol(ent->d_name, &endptr, 10);
2328 if (*endptr != 0) /* value didn't completely parse as a number */
2331 memset(&pse, 0, sizeof(pse));
2334 status = ps_read_process(pid, &pse, &state);
2336 DEBUG("ps_read_process failed: %i", status);
2367 ps_list_add(pse.name,
2368 ps_get_cmdline(pid, pse.name, cmdline, sizeof(cmdline)), &pse);
2369 } /* while(readdir) */
2372 ps_submit_state("running", running);
2373 ps_submit_state("sleeping", sleeping);
2374 ps_submit_state("zombies", zombies);
2375 ps_submit_state("stopped", stopped);
2376 ps_submit_state("detached", detached);
2377 ps_submit_state("daemon", daemon);
2378 ps_submit_state("system", system);
2379 ps_submit_state("orphan", orphan);
2381 for (procstat_t *ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
2382 ps_submit_proc_list(ps_ptr);
2385 #endif /* KERNEL_SOLARIS */
2392 void module_register(void) {
2393 plugin_register_complex_config("processes", ps_config);
2394 plugin_register_init("processes", ps_init);
2395 plugin_register_read("processes", ps_read);
2396 } /* void module_register */