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 ssnprintf(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 ssnprintf(filename, sizeof(filename), "/proc/%li/task/%s/status", ps->id,
847 if ((fh = fopen(filename, "r")) == NULL) {
848 DEBUG("Failed to open file `%s'", filename);
852 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
856 if (strncmp(buffer, "voluntary_ctxt_switches", 23) != 0 &&
857 strncmp(buffer, "nonvoluntary_ctxt_switches", 26) != 0)
860 numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
867 tmp = (derive_t)strtoll(fields[1], &endptr, /* base = */ 10);
868 if ((errno == 0) && (endptr != fields[1])) {
869 if (strncmp(buffer, "voluntary_ctxt_switches", 23) == 0) {
871 } else if (strncmp(buffer, "nonvoluntary_ctxt_switches", 26) == 0) {
872 cswitch_invol += tmp;
875 } /* while (fgets) */
879 WARNING("processes: fclose: %s",
880 sstrerror(errno, errbuf, sizeof(errbuf)));
885 ps->cswitch_vol = cswitch_vol;
886 ps->cswitch_invol = cswitch_invol;
889 } /* int *ps_read_tasks_status */
891 /* Read data from /proc/pid/status */
892 static int ps_read_status(long pid, process_entry_t *ps) {
896 unsigned long lib = 0;
897 unsigned long exe = 0;
898 unsigned long data = 0;
899 unsigned long threads = 0;
903 ssnprintf(filename, sizeof(filename), "/proc/%li/status", pid);
904 if ((fh = fopen(filename, "r")) == NULL)
907 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
911 if (strncmp(buffer, "Vm", 2) != 0 && strncmp(buffer, "Threads", 7) != 0)
914 numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
921 tmp = strtoul(fields[1], &endptr, /* base = */ 10);
922 if ((errno == 0) && (endptr != fields[1])) {
923 if (strncmp(buffer, "VmData", 6) == 0) {
925 } else if (strncmp(buffer, "VmLib", 5) == 0) {
927 } else if (strncmp(buffer, "VmExe", 5) == 0) {
929 } else if (strncmp(buffer, "Threads", 7) == 0) {
933 } /* while (fgets) */
937 WARNING("processes: fclose: %s", sstrerror(errno, errbuf, sizeof(errbuf)));
940 ps->vmem_data = data * 1024;
941 ps->vmem_code = (exe + lib) * 1024;
943 ps->num_lwp = threads;
946 } /* int *ps_read_status */
948 static int ps_read_io(process_entry_t *ps) {
956 ssnprintf(filename, sizeof(filename), "/proc/%li/io", ps->id);
957 if ((fh = fopen(filename, "r")) == NULL) {
958 DEBUG("ps_read_io: Failed to open file `%s'", filename);
962 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
963 derive_t *val = NULL;
967 if (strncasecmp(buffer, "rchar:", 6) == 0)
968 val = &(ps->io_rchar);
969 else if (strncasecmp(buffer, "wchar:", 6) == 0)
970 val = &(ps->io_wchar);
971 else if (strncasecmp(buffer, "syscr:", 6) == 0)
972 val = &(ps->io_syscr);
973 else if (strncasecmp(buffer, "syscw:", 6) == 0)
974 val = &(ps->io_syscw);
975 else if (strncasecmp(buffer, "read_bytes:", 11) == 0)
976 val = &(ps->io_diskr);
977 else if (strncasecmp(buffer, "write_bytes:", 12) == 0)
978 val = &(ps->io_diskw);
982 numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
989 tmp = strtoll(fields[1], &endptr, /* base = */ 10);
990 if ((errno != 0) || (endptr == fields[1]))
993 *val = (derive_t)tmp;
994 } /* while (fgets) */
998 WARNING("processes: fclose: %s", sstrerror(errno, errbuf, sizeof(errbuf)));
1001 } /* int ps_read_io (...) */
1003 static int ps_count_fd(int pid) {
1009 ssnprintf(dirname, sizeof(dirname), "/proc/%i/fd", pid);
1011 if ((dh = opendir(dirname)) == NULL) {
1012 DEBUG("Failed to open directory `%s'", dirname);
1015 while ((ent = readdir(dh)) != NULL) {
1016 if (!isdigit((int)ent->d_name[0]))
1023 return ((count >= 1) ? count : 1);
1024 } /* int ps_count_fd (pid) */
1026 static void ps_fill_details(const procstat_t *ps, process_entry_t *entry) {
1027 if (entry->has_io == 0) {
1032 if (ps->report_ctx_switch) {
1033 if (entry->has_cswitch == 0) {
1034 ps_read_tasks_status(entry);
1035 entry->has_cswitch = 1;
1039 if (ps->report_fd_num) {
1041 if (entry->has_fd == 0 && (num_fd = ps_count_fd(entry->id)) > 0) {
1042 entry->num_fd = num_fd;
1046 } /* void ps_fill_details (...) */
1048 static int ps_read_process(long pid, process_entry_t *ps, char *state) {
1058 size_t name_start_pos;
1059 size_t name_end_pos;
1062 derive_t cpu_user_counter;
1063 derive_t cpu_system_counter;
1064 long long unsigned vmem_size;
1065 long long unsigned vmem_rss;
1066 long long unsigned stack_size;
1070 ssnprintf(filename, sizeof(filename), "/proc/%li/stat", pid);
1072 status = read_file_contents(filename, buffer, sizeof(buffer) - 1);
1075 buffer_len = (size_t)status;
1076 buffer[buffer_len] = 0;
1078 /* The name of the process is enclosed in parens. Since the name can
1079 * contain parens itself, spaces, numbers and pretty much everything
1080 * else, use these to determine the process name. We don't use
1081 * strchr(3) and strrchr(3) to avoid pointer arithmetic which would
1082 * otherwise be required to determine name_len. */
1084 while (name_start_pos < buffer_len && buffer[name_start_pos] != '(')
1087 name_end_pos = buffer_len;
1088 while (name_end_pos > 0 && buffer[name_end_pos] != ')')
1091 /* Either '(' or ')' is not found or they are in the wrong order.
1092 * Anyway, something weird that shouldn't happen ever. */
1093 if (name_start_pos >= name_end_pos) {
1094 ERROR("processes plugin: name_start_pos = %zu >= name_end_pos = %zu",
1095 name_start_pos, name_end_pos);
1099 name_len = (name_end_pos - name_start_pos) - 1;
1100 if (name_len >= sizeof(ps->name))
1101 name_len = sizeof(ps->name) - 1;
1103 sstrncpy(ps->name, &buffer[name_start_pos + 1], name_len + 1);
1105 if ((buffer_len - name_end_pos) < 2)
1107 buffer_ptr = &buffer[name_end_pos + 2];
1109 fields_len = strsplit(buffer_ptr, fields, STATIC_ARRAY_SIZE(fields));
1110 if (fields_len < 22) {
1111 DEBUG("processes plugin: ps_read_process (pid = %li):"
1112 " `%s' has only %i fields..",
1113 pid, filename, fields_len);
1117 *state = fields[0][0];
1119 if (*state == 'Z') {
1123 ps->num_lwp = strtoul(fields[17], /* endptr = */ NULL, /* base = */ 10);
1124 if ((ps_read_status(pid, ps)) != 0) {
1128 DEBUG("ps_read_process: did not get vmem data for pid %li", pid);
1130 if (ps->num_lwp == 0)
1135 /* Leave the rest at zero if this is only a zombi */
1136 if (ps->num_proc == 0) {
1137 DEBUG("processes plugin: This is only a zombie: pid = %li; "
1143 cpu_user_counter = atoll(fields[11]);
1144 cpu_system_counter = atoll(fields[12]);
1145 vmem_size = atoll(fields[20]);
1146 vmem_rss = atoll(fields[21]);
1147 ps->vmem_minflt_counter = atol(fields[7]);
1148 ps->vmem_majflt_counter = atol(fields[9]);
1151 unsigned long long stack_start = atoll(fields[25]);
1152 unsigned long long stack_ptr = atoll(fields[26]);
1154 stack_size = (stack_start > stack_ptr) ? stack_start - stack_ptr
1155 : stack_ptr - stack_start;
1158 /* Convert jiffies to useconds */
1159 cpu_user_counter = cpu_user_counter * 1000000 / CONFIG_HZ;
1160 cpu_system_counter = cpu_system_counter * 1000000 / CONFIG_HZ;
1161 vmem_rss = vmem_rss * pagesize_g;
1163 ps->cpu_user_counter = cpu_user_counter;
1164 ps->cpu_system_counter = cpu_system_counter;
1165 ps->vmem_size = (unsigned long)vmem_size;
1166 ps->vmem_rss = (unsigned long)vmem_rss;
1167 ps->stack_size = (unsigned long)stack_size;
1169 /* no data by default. May be filled by ps_fill_details () */
1177 ps->cswitch_vol = -1;
1178 ps->cswitch_invol = -1;
1182 } /* int ps_read_process (...) */
1184 static char *ps_get_cmdline(long pid, char *name, char *buf, size_t buf_len) {
1188 char file[PATH_MAX];
1193 if ((pid < 1) || (NULL == buf) || (buf_len < 2))
1196 ssnprintf(file, sizeof(file), "/proc/%li/cmdline", pid);
1199 fd = open(file, O_RDONLY);
1202 /* ENOENT means the process exited while we were handling it.
1203 * Don't complain about this, it only fills the logs. */
1204 if (errno != ENOENT)
1205 WARNING("processes plugin: Failed to open `%s': %s.", file,
1206 sstrerror(errno, errbuf, sizeof(errbuf)));
1218 status = read(fd, (void *)buf_ptr, len);
1223 if ((EAGAIN == errno) || (EINTR == errno))
1226 WARNING("processes plugin: Failed to read from `%s': %s.", file,
1227 sstrerror(errno, errbuf, sizeof(errbuf)));
1247 /* cmdline not available; e.g. kernel thread, zombie */
1251 ssnprintf(buf, buf_len, "[%s]", name);
1255 assert(n <= buf_len);
1262 /* remove trailing whitespace */
1263 while ((n > 0) && (isspace(buf[n]) || ('\0' == buf[n]))) {
1268 /* arguments are separated by '\0' in /proc/<pid>/cmdline */
1275 } /* char *ps_get_cmdline (...) */
1277 static int read_fork_rate(void) {
1281 _Bool value_valid = 0;
1283 proc_stat = fopen("/proc/stat", "r");
1284 if (proc_stat == NULL) {
1286 ERROR("processes plugin: fopen (/proc/stat) failed: %s",
1287 sstrerror(errno, errbuf, sizeof(errbuf)));
1291 while (fgets(buffer, sizeof(buffer), proc_stat) != NULL) {
1296 fields_num = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
1297 if (fields_num != 2)
1300 if (strcmp("processes", fields[0]) != 0)
1303 status = parse_value(fields[1], &value, DS_TYPE_DERIVE);
1314 ps_submit_fork_rate(value.derive);
1317 #endif /*KERNEL_LINUX */
1320 static char *ps_get_cmdline(long pid,
1321 char *name __attribute__((unused)), /* {{{ */
1322 char *buffer, size_t buffer_size) {
1323 char path[PATH_MAX];
1327 snprintf(path, sizeof(path), "/proc/%li/psinfo", pid);
1329 status = read_file_contents(path, (void *)&info, sizeof(info));
1330 if ((status < 0) || (((size_t)status) != sizeof(info))) {
1331 ERROR("processes plugin: Unexpected return value "
1332 "while reading \"%s\": "
1333 "Returned %zd but expected %zu.",
1334 path, status, buffer_size);
1338 info.pr_psargs[sizeof(info.pr_psargs) - 1] = 0;
1339 sstrncpy(buffer, info.pr_psargs, buffer_size);
1342 } /* }}} int ps_get_cmdline */
1345 * Reads process information on the Solaris OS. The information comes mainly
1347 * /proc/PID/status, /proc/PID/psinfo and /proc/PID/usage
1348 * The values for input and ouput chars are calculated "by hand"
1349 * Added a few "solaris" specific process states as well
1351 static int ps_read_process(long pid, process_entry_t *ps, char *state) {
1353 char f_psinfo[64], f_usage[64];
1356 pstatus_t *myStatus;
1360 snprintf(filename, sizeof(filename), "/proc/%li/status", pid);
1361 snprintf(f_psinfo, sizeof(f_psinfo), "/proc/%li/psinfo", pid);
1362 snprintf(f_usage, sizeof(f_usage), "/proc/%li/usage", pid);
1364 buffer = calloc(1, sizeof(pstatus_t));
1365 read_file_contents(filename, buffer, sizeof(pstatus_t));
1366 myStatus = (pstatus_t *)buffer;
1368 buffer = calloc(1, sizeof(psinfo_t));
1369 read_file_contents(f_psinfo, buffer, sizeof(psinfo_t));
1370 myInfo = (psinfo_t *)buffer;
1372 buffer = calloc(1, sizeof(prusage_t));
1373 read_file_contents(f_usage, buffer, sizeof(prusage_t));
1374 myUsage = (prusage_t *)buffer;
1376 sstrncpy(ps->name, myInfo->pr_fname, sizeof(myInfo->pr_fname));
1377 ps->num_lwp = myStatus->pr_nlwp;
1378 if (myInfo->pr_wstat != 0) {
1389 ps->num_lwp = myInfo->pr_nlwp;
1393 * Convert system time and user time from nanoseconds to microseconds
1394 * for compatibility with the linux module
1396 ps->cpu_system_counter = myStatus->pr_stime.tv_nsec / 1000;
1397 ps->cpu_user_counter = myStatus->pr_utime.tv_nsec / 1000;
1400 * Convert rssize from KB to bytes to be consistent w/ the linux module
1402 ps->vmem_rss = myInfo->pr_rssize * 1024;
1403 ps->vmem_size = myInfo->pr_size * 1024;
1404 ps->vmem_minflt_counter = myUsage->pr_minf;
1405 ps->vmem_majflt_counter = myUsage->pr_majf;
1408 * TODO: Data and code segment calculations for Solaris
1413 ps->stack_size = myStatus->pr_stksize;
1416 * TODO: File descriptor count for Solaris
1421 * Calculating input/ouput chars
1422 * Formula used is total chars / total blocks => chars/block
1423 * then convert input/output blocks to chars
1425 ulong_t tot_chars = myUsage->pr_ioch;
1426 ulong_t tot_blocks = myUsage->pr_inblk + myUsage->pr_oublk;
1427 ulong_t chars_per_block = 1;
1428 if (tot_blocks != 0)
1429 chars_per_block = tot_chars / tot_blocks;
1430 ps->io_rchar = myUsage->pr_inblk * chars_per_block;
1431 ps->io_wchar = myUsage->pr_oublk * chars_per_block;
1432 ps->io_syscr = myUsage->pr_sysc;
1433 ps->io_syscw = myUsage->pr_sysc;
1438 * TODO: context switch counters for Solaris
1440 ps->cswitch_vol = -1;
1441 ps->cswitch_invol = -1;
1444 * TODO: Find way of setting BLOCKED and PAGING status
1448 if (myStatus->pr_flags & PR_ASLEEP)
1450 else if (myStatus->pr_flags & PR_STOPPED)
1452 else if (myStatus->pr_flags & PR_DETACH)
1454 else if (myStatus->pr_flags & PR_DAEMON)
1456 else if (myStatus->pr_flags & PR_ISSYS)
1458 else if (myStatus->pr_flags & PR_ORPHAN)
1469 * Reads the number of threads created since the last reboot. On Solaris these
1470 * are retrieved from kstat (module cpu, name sys, class misc, stat nthreads).
1471 * The result is the sum for all the threads created on each cpu
1473 static int read_fork_rate(void) {
1474 extern kstat_ctl_t *kc;
1475 derive_t result = 0;
1480 for (kstat_t *ksp_chain = kc->kc_chain; ksp_chain != NULL;
1481 ksp_chain = ksp_chain->ks_next) {
1482 if ((strcmp(ksp_chain->ks_module, "cpu") == 0) &&
1483 (strcmp(ksp_chain->ks_name, "sys") == 0) &&
1484 (strcmp(ksp_chain->ks_class, "misc") == 0)) {
1487 kstat_read(kc, ksp_chain, NULL);
1489 tmp = get_kstat_value(ksp_chain, "nthreads");
1495 ps_submit_fork_rate(result);
1498 #endif /* KERNEL_SOLARIS */
1500 #if HAVE_THREAD_INFO
1501 static int mach_get_task_name(task_t t, int *pid, char *name,
1502 size_t name_max_len) {
1505 struct kinfo_proc kp;
1510 mib[2] = KERN_PROC_PID;
1512 if (pid_for_task(t, pid) != KERN_SUCCESS)
1516 kp_size = sizeof(kp);
1517 if (sysctl(mib, 4, &kp, &kp_size, NULL, 0) != 0)
1520 if (name_max_len > (MAXCOMLEN + 1))
1521 name_max_len = MAXCOMLEN + 1;
1523 strncpy(name, kp.kp_proc.p_comm, name_max_len - 1);
1524 name[name_max_len - 1] = '\0';
1526 DEBUG("pid = %i; name = %s;", *pid, name);
1528 /* We don't do the special handling for `p_comm == "LaunchCFMApp"' as
1529 * `top' does it, because it is a lot of work and only used when
1530 * debugging. -octo */
1534 #endif /* HAVE_THREAD_INFO */
1535 /* ------- end of additional functions for KERNEL_LINUX/HAVE_THREAD_INFO -------
1538 /* do actual readings from kernel */
1539 static int ps_read(void) {
1540 #if HAVE_THREAD_INFO
1541 kern_return_t status;
1543 processor_set_t port_pset_priv;
1545 task_array_t task_list;
1546 mach_msg_type_number_t task_list_len;
1549 char task_name[MAXCOMLEN + 1];
1551 thread_act_array_t thread_list;
1552 mach_msg_type_number_t thread_list_len;
1553 thread_basic_info_data_t thread_data;
1554 mach_msg_type_number_t thread_data_len;
1563 process_entry_t pse;
1568 * The Mach-concept is a little different from the traditional UNIX
1569 * concept: All the work is done in threads. Threads are contained in
1570 * `tasks'. Therefore, `task status' doesn't make much sense, since
1571 * it's actually a `thread status'.
1572 * Tasks are assigned to sets of processors, so that's where you go to
1575 for (mach_msg_type_number_t pset = 0; pset < pset_list_len; pset++) {
1576 if ((status = host_processor_set_priv(port_host_self, pset_list[pset],
1577 &port_pset_priv)) != KERN_SUCCESS) {
1578 ERROR("host_processor_set_priv failed: %s\n", mach_error_string(status));
1582 if ((status = processor_set_tasks(port_pset_priv, &task_list,
1583 &task_list_len)) != KERN_SUCCESS) {
1584 ERROR("processor_set_tasks failed: %s\n", mach_error_string(status));
1585 mach_port_deallocate(port_task_self, port_pset_priv);
1589 for (mach_msg_type_number_t task = 0; task < task_list_len; task++) {
1591 if (mach_get_task_name(task_list[task], &task_pid, task_name,
1592 PROCSTAT_NAME_LEN) == 0) {
1593 /* search for at least one match */
1594 for (ps = list_head_g; ps != NULL; ps = ps->next)
1595 /* FIXME: cmdline should be here instead of NULL */
1596 if (ps_list_match(task_name, NULL, ps) == 1)
1600 /* Collect more detailed statistics for this process */
1602 task_basic_info_data_t task_basic_info;
1603 mach_msg_type_number_t task_basic_info_len;
1604 task_events_info_data_t task_events_info;
1605 mach_msg_type_number_t task_events_info_len;
1606 task_absolutetime_info_data_t task_absolutetime_info;
1607 mach_msg_type_number_t task_absolutetime_info_len;
1609 memset(&pse, '\0', sizeof(pse));
1612 task_basic_info_len = TASK_BASIC_INFO_COUNT;
1613 status = task_info(task_list[task], TASK_BASIC_INFO,
1614 (task_info_t)&task_basic_info, &task_basic_info_len);
1615 if (status != KERN_SUCCESS) {
1616 ERROR("task_info failed: %s", mach_error_string(status));
1617 continue; /* with next thread_list */
1620 task_events_info_len = TASK_EVENTS_INFO_COUNT;
1622 task_info(task_list[task], TASK_EVENTS_INFO,
1623 (task_info_t)&task_events_info, &task_events_info_len);
1624 if (status != KERN_SUCCESS) {
1625 ERROR("task_info failed: %s", mach_error_string(status));
1626 continue; /* with next thread_list */
1629 task_absolutetime_info_len = TASK_ABSOLUTETIME_INFO_COUNT;
1630 status = task_info(task_list[task], TASK_ABSOLUTETIME_INFO,
1631 (task_info_t)&task_absolutetime_info,
1632 &task_absolutetime_info_len);
1633 if (status != KERN_SUCCESS) {
1634 ERROR("task_info failed: %s", mach_error_string(status));
1635 continue; /* with next thread_list */
1639 pse.vmem_size = task_basic_info.virtual_size;
1640 pse.vmem_rss = task_basic_info.resident_size;
1641 /* Does not seem to be easily exposed */
1652 /* File descriptor count not implemented */
1655 pse.vmem_minflt_counter = task_events_info.cow_faults;
1656 pse.vmem_majflt_counter = task_events_info.faults;
1658 pse.cpu_user_counter = task_absolutetime_info.total_user;
1659 pse.cpu_system_counter = task_absolutetime_info.total_system;
1661 /* context switch counters not implemented */
1662 pse.cswitch_vol = -1;
1663 pse.cswitch_invol = -1;
1666 status = task_threads(task_list[task], &thread_list, &thread_list_len);
1667 if (status != KERN_SUCCESS) {
1668 /* Apple's `top' treats this case a zombie. It
1669 * makes sense to some extend: A `zombie'
1670 * thread is nonsense, since the task/process
1673 DEBUG("task_threads failed: %s", mach_error_string(status));
1674 if (task_list[task] != port_task_self)
1675 mach_port_deallocate(port_task_self, task_list[task]);
1676 continue; /* with next task_list */
1679 for (mach_msg_type_number_t thread = 0; thread < thread_list_len;
1681 thread_data_len = THREAD_BASIC_INFO_COUNT;
1682 status = thread_info(thread_list[thread], THREAD_BASIC_INFO,
1683 (thread_info_t)&thread_data, &thread_data_len);
1684 if (status != KERN_SUCCESS) {
1685 ERROR("thread_info failed: %s", mach_error_string(status));
1686 if (task_list[task] != port_task_self)
1687 mach_port_deallocate(port_task_self, thread_list[thread]);
1688 continue; /* with next thread_list */
1694 switch (thread_data.run_state) {
1695 case TH_STATE_RUNNING:
1698 case TH_STATE_STOPPED:
1699 /* What exactly is `halted'? */
1700 case TH_STATE_HALTED:
1703 case TH_STATE_WAITING:
1706 case TH_STATE_UNINTERRUPTIBLE:
1709 /* There is no `zombie' case here,
1710 * since there are no zombie-threads.
1711 * There's only zombie tasks, which are
1714 WARNING("Unknown thread status: %i", thread_data.run_state);
1716 } /* switch (thread_data.run_state) */
1718 if (task_list[task] != port_task_self) {
1719 status = mach_port_deallocate(port_task_self, thread_list[thread]);
1720 if (status != KERN_SUCCESS)
1721 ERROR("mach_port_deallocate failed: %s", mach_error_string(status));
1723 } /* for (thread_list) */
1725 if ((status = vm_deallocate(port_task_self, (vm_address_t)thread_list,
1726 thread_list_len * sizeof(thread_act_t))) !=
1728 ERROR("vm_deallocate failed: %s", mach_error_string(status));
1731 thread_list_len = 0;
1733 /* Only deallocate the task port, if it isn't our own.
1734 * Don't know what would happen in that case, but this
1735 * is what Apple's top does.. ;) */
1736 if (task_list[task] != port_task_self) {
1737 status = mach_port_deallocate(port_task_self, task_list[task]);
1738 if (status != KERN_SUCCESS)
1739 ERROR("mach_port_deallocate failed: %s", mach_error_string(status));
1743 /* FIXME: cmdline should be here instead of NULL */
1744 ps_list_add(task_name, NULL, &pse);
1745 } /* for (task_list) */
1747 if ((status = vm_deallocate(port_task_self, (vm_address_t)task_list,
1748 task_list_len * sizeof(task_t))) !=
1750 ERROR("vm_deallocate failed: %s", mach_error_string(status));
1755 if ((status = mach_port_deallocate(port_task_self, port_pset_priv)) !=
1757 ERROR("mach_port_deallocate failed: %s", mach_error_string(status));
1759 } /* for (pset_list) */
1761 ps_submit_state("running", running);
1762 ps_submit_state("sleeping", sleeping);
1763 ps_submit_state("zombies", zombies);
1764 ps_submit_state("stopped", stopped);
1765 ps_submit_state("blocked", blocked);
1767 for (ps = list_head_g; ps != NULL; ps = ps->next)
1768 ps_submit_proc_list(ps);
1769 /* #endif HAVE_THREAD_INFO */
1783 char cmdline[CMDLINE_BUFFER_SIZE];
1786 process_entry_t pse;
1789 running = sleeping = zombies = stopped = paging = blocked = 0;
1792 if ((proc = opendir("/proc")) == NULL) {
1794 ERROR("Cannot open `/proc': %s", sstrerror(errno, errbuf, sizeof(errbuf)));
1798 while ((ent = readdir(proc)) != NULL) {
1799 if (!isdigit(ent->d_name[0]))
1802 if ((pid = atol(ent->d_name)) < 1)
1805 memset(&pse, 0, sizeof(pse));
1808 status = ps_read_process(pid, &pse, &state);
1810 DEBUG("ps_read_process failed: %i", status);
1835 ps_list_add(pse.name,
1836 ps_get_cmdline(pid, pse.name, cmdline, sizeof(cmdline)), &pse);
1841 ps_submit_state("running", running);
1842 ps_submit_state("sleeping", sleeping);
1843 ps_submit_state("zombies", zombies);
1844 ps_submit_state("stopped", stopped);
1845 ps_submit_state("paging", paging);
1846 ps_submit_state("blocked", blocked);
1848 for (procstat_t *ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1849 ps_submit_proc_list(ps_ptr);
1852 /* #endif KERNEL_LINUX */
1854 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD
1864 char errbuf[_POSIX2_LINE_MAX];
1865 struct kinfo_proc *procs; /* array of processes */
1866 struct kinfo_proc *proc_ptr = NULL;
1867 int count; /* returns number of processes */
1869 process_entry_t pse;
1873 /* Open the kvm interface, get a descriptor */
1874 kd = kvm_openfiles(NULL, "/dev/null", NULL, 0, errbuf);
1876 ERROR("processes plugin: Cannot open kvm interface: %s", errbuf);
1880 /* Get the list of processes. */
1881 procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, &count);
1882 if (procs == NULL) {
1883 ERROR("processes plugin: Cannot get kvm processes list: %s",
1889 /* Iterate through the processes in kinfo_proc */
1890 for (int i = 0; i < count; i++) {
1891 /* Create only one process list entry per _process_, i.e.
1892 * filter out threads (duplicate PID entries). */
1893 if ((proc_ptr == NULL) || (proc_ptr->ki_pid != procs[i].ki_pid)) {
1894 char cmdline[CMDLINE_BUFFER_SIZE] = "";
1895 _Bool have_cmdline = 0;
1897 proc_ptr = &(procs[i]);
1898 /* Don't probe system processes and processes without arguments */
1899 if (((procs[i].ki_flag & P_SYSTEM) == 0) && (procs[i].ki_args != NULL)) {
1904 /* retrieve the arguments */
1905 argv = kvm_getargv(kd, proc_ptr, /* nchr = */ 0);
1907 if ((argv != NULL) && (argv[0] != NULL)) {
1908 while (argv[argc] != NULL)
1911 status = strjoin(cmdline, sizeof(cmdline), argv, argc, " ");
1913 WARNING("processes plugin: Command line did not fit into buffer.");
1917 } /* if (process has argument list) */
1919 memset(&pse, 0, sizeof(pse));
1920 pse.id = procs[i].ki_pid;
1923 pse.num_lwp = procs[i].ki_numthreads;
1925 pse.vmem_size = procs[i].ki_size;
1926 pse.vmem_rss = procs[i].ki_rssize * pagesize;
1927 pse.vmem_data = procs[i].ki_dsize * pagesize;
1928 pse.vmem_code = procs[i].ki_tsize * pagesize;
1929 pse.stack_size = procs[i].ki_ssize * pagesize;
1930 pse.vmem_minflt_counter = procs[i].ki_rusage.ru_minflt;
1931 pse.vmem_majflt_counter = procs[i].ki_rusage.ru_majflt;
1933 pse.cpu_user_counter = 0;
1934 pse.cpu_system_counter = 0;
1936 * The u-area might be swapped out, and we can't get
1937 * at it because we have a crashdump and no swap.
1938 * If it's here fill in these fields, otherwise, just
1941 if (procs[i].ki_flag & P_INMEM) {
1942 pse.cpu_user_counter = procs[i].ki_rusage.ru_utime.tv_usec +
1943 (1000000lu * procs[i].ki_rusage.ru_utime.tv_sec);
1944 pse.cpu_system_counter =
1945 procs[i].ki_rusage.ru_stime.tv_usec +
1946 (1000000lu * procs[i].ki_rusage.ru_stime.tv_sec);
1957 /* file descriptor count not implemented */
1960 /* context switch counters not implemented */
1961 pse.cswitch_vol = -1;
1962 pse.cswitch_invol = -1;
1964 ps_list_add(procs[i].ki_comm, have_cmdline ? cmdline : NULL, &pse);
1966 switch (procs[i].ki_stat) {
1989 } /* if ((proc_ptr == NULL) || (proc_ptr->ki_pid != procs[i].ki_pid)) */
1994 ps_submit_state("running", running);
1995 ps_submit_state("sleeping", sleeping);
1996 ps_submit_state("zombies", zombies);
1997 ps_submit_state("stopped", stopped);
1998 ps_submit_state("blocked", blocked);
1999 ps_submit_state("idle", idle);
2000 ps_submit_state("wait", wait);
2002 for (procstat_t *ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
2003 ps_submit_proc_list(ps_ptr);
2004 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
2006 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_OPENBSD
2017 struct kinfo_proc *procs; /* array of processes */
2018 struct kinfo_proc *proc_ptr = NULL;
2019 int count; /* returns number of processes */
2021 process_entry_t pse;
2025 /* Open the kvm interface, get a descriptor */
2026 kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf);
2028 ERROR("processes plugin: Cannot open kvm interface: %s", errbuf);
2032 /* Get the list of processes. */
2033 procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc), &count);
2034 if (procs == NULL) {
2035 ERROR("processes plugin: Cannot get kvm processes list: %s",
2041 /* Iterate through the processes in kinfo_proc */
2042 for (int i = 0; i < count; i++) {
2043 /* Create only one process list entry per _process_, i.e.
2044 * filter out threads (duplicate PID entries). */
2045 if ((proc_ptr == NULL) || (proc_ptr->p_pid != procs[i].p_pid)) {
2046 char cmdline[CMDLINE_BUFFER_SIZE] = "";
2047 _Bool have_cmdline = 0;
2049 proc_ptr = &(procs[i]);
2050 /* Don't probe zombie processes */
2051 if (!P_ZOMBIE(proc_ptr)) {
2056 /* retrieve the arguments */
2057 argv = kvm_getargv(kd, proc_ptr, /* nchr = */ 0);
2059 if ((argv != NULL) && (argv[0] != NULL)) {
2060 while (argv[argc] != NULL)
2063 status = strjoin(cmdline, sizeof(cmdline), argv, argc, " ");
2065 WARNING("processes plugin: Command line did not fit into buffer.");
2069 } /* if (process has argument list) */
2071 memset(&pse, 0, sizeof(pse));
2072 pse.id = procs[i].p_pid;
2075 pse.num_lwp = 1; /* XXX: accumulate p_tid values for a single p_pid ? */
2077 pse.vmem_rss = procs[i].p_vm_rssize * pagesize;
2078 pse.vmem_data = procs[i].p_vm_dsize * pagesize;
2079 pse.vmem_code = procs[i].p_vm_tsize * pagesize;
2080 pse.stack_size = procs[i].p_vm_ssize * pagesize;
2081 pse.vmem_size = pse.stack_size + pse.vmem_code + pse.vmem_data;
2082 pse.vmem_minflt_counter = procs[i].p_uru_minflt;
2083 pse.vmem_majflt_counter = procs[i].p_uru_majflt;
2085 pse.cpu_user_counter =
2086 procs[i].p_uutime_usec + (1000000lu * procs[i].p_uutime_sec);
2087 pse.cpu_system_counter =
2088 procs[i].p_ustime_usec + (1000000lu * procs[i].p_ustime_sec);
2098 /* file descriptor count not implemented */
2101 /* context switch counters not implemented */
2102 pse.cswitch_vol = -1;
2103 pse.cswitch_invol = -1;
2105 ps_list_add(procs[i].p_comm, have_cmdline ? cmdline : NULL, &pse);
2107 switch (procs[i].p_stat) {
2130 } /* if ((proc_ptr == NULL) || (proc_ptr->p_pid != procs[i].p_pid)) */
2135 ps_submit_state("running", running);
2136 ps_submit_state("sleeping", sleeping);
2137 ps_submit_state("zombies", zombies);
2138 ps_submit_state("stopped", stopped);
2139 ps_submit_state("onproc", onproc);
2140 ps_submit_state("idle", idle);
2141 ps_submit_state("dead", dead);
2143 for (procstat_t *ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
2144 ps_submit_proc_list(ps_ptr);
2145 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_OPENBSD */
2147 #elif HAVE_PROCINFO_H
2159 process_entry_t pse;
2162 while ((nprocs = getprocs64(procentry, sizeof(struct procentry64),
2163 /* fdsinfo = */ NULL, sizeof(struct fdsinfo64),
2164 &pindex, MAXPROCENTRY)) > 0) {
2165 for (int i = 0; i < nprocs; i++) {
2168 char arglist[MAXARGLN + 1];
2172 if (procentry[i].pi_state == SNONE)
2174 /* if (procentry[i].pi_state == SZOMB) FIXME */
2176 cmdline = procentry[i].pi_comm;
2177 cargs = procentry[i].pi_comm;
2178 if (procentry[i].pi_flags & SKPROC) {
2179 if (procentry[i].pi_pid == 0)
2180 cmdline = "swapper";
2183 if (getargs(&procentry[i], sizeof(struct procentry64), arglist,
2188 while (++n < MAXARGLN) {
2189 if (arglist[n] == '\0') {
2190 if (arglist[n + 1] == '\0')
2199 memset(&pse, 0, sizeof(pse));
2201 pse.id = procentry[i].pi_pid;
2202 pse.num_lwp = procentry[i].pi_thcount;
2206 while ((nthreads = getthrds64(procentry[i].pi_pid, thrdentry,
2207 sizeof(struct thrdentry64), &thindex,
2208 MAXTHRDENTRY)) > 0) {
2211 for (j = 0; j < nthreads; j++) {
2212 switch (thrdentry[j].ti_state) {
2213 /* case TSNONE: break; */
2216 break; /* FIXME is really blocked */
2234 if (nthreads < MAXTHRDENTRY)
2238 /* tv_usec is nanosec ??? */
2239 pse.cpu_user_counter = procentry[i].pi_ru.ru_utime.tv_sec * 1000000 +
2240 procentry[i].pi_ru.ru_utime.tv_usec / 1000;
2242 /* tv_usec is nanosec ??? */
2243 pse.cpu_system_counter = procentry[i].pi_ru.ru_stime.tv_sec * 1000000 +
2244 procentry[i].pi_ru.ru_stime.tv_usec / 1000;
2246 pse.vmem_minflt_counter = procentry[i].pi_minflt;
2247 pse.vmem_majflt_counter = procentry[i].pi_majflt;
2249 pse.vmem_size = procentry[i].pi_tsize + procentry[i].pi_dvm * pagesize;
2250 pse.vmem_rss = (procentry[i].pi_drss + procentry[i].pi_trss) * pagesize;
2251 /* Not supported/implemented */
2265 pse.cswitch_vol = -1;
2266 pse.cswitch_invol = -1;
2268 ps_list_add(cmdline, cargs, &pse);
2269 } /* for (i = 0 .. nprocs) */
2271 if (nprocs < MAXPROCENTRY)
2273 } /* while (getprocs64() > 0) */
2274 ps_submit_state("running", running);
2275 ps_submit_state("sleeping", sleeping);
2276 ps_submit_state("zombies", zombies);
2277 ps_submit_state("stopped", stopped);
2278 ps_submit_state("paging", paging);
2279 ps_submit_state("blocked", blocked);
2281 for (procstat_t *ps = list_head_g; ps != NULL; ps = ps->next)
2282 ps_submit_proc_list(ps);
2283 /* #endif HAVE_PROCINFO_H */
2285 #elif KERNEL_SOLARIS
2287 * The Solaris section adds a few more process states and removes some
2288 * process states compared to linux. Most notably there is no "PAGING"
2289 * and "BLOCKED" state for a process. The rest is similar to the linux
2307 char cmdline[PRARGSZ];
2311 proc = opendir("/proc");
2315 while ((ent = readdir(proc)) != NULL) {
2317 process_entry_t pse;
2320 if (!isdigit((int)ent->d_name[0]))
2323 pid = strtol(ent->d_name, &endptr, 10);
2324 if (*endptr != 0) /* value didn't completely parse as a number */
2327 memset(&pse, 0, sizeof(pse));
2330 status = ps_read_process(pid, &pse, &state);
2332 DEBUG("ps_read_process failed: %i", status);
2363 ps_list_add(pse.name,
2364 ps_get_cmdline(pid, pse.name, cmdline, sizeof(cmdline)), &pse);
2365 } /* while(readdir) */
2368 ps_submit_state("running", running);
2369 ps_submit_state("sleeping", sleeping);
2370 ps_submit_state("zombies", zombies);
2371 ps_submit_state("stopped", stopped);
2372 ps_submit_state("detached", detached);
2373 ps_submit_state("daemon", daemon);
2374 ps_submit_state("system", system);
2375 ps_submit_state("orphan", orphan);
2377 for (procstat_t *ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
2378 ps_submit_proc_list(ps_ptr);
2381 #endif /* KERNEL_SOLARIS */
2388 void module_register(void) {
2389 plugin_register_complex_config("processes", ps_config);
2390 plugin_register_init("processes", ps_init);
2391 plugin_register_read("processes", ps_read);
2392 } /* void module_register */