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;
190 derive_t cswitch_vol;
191 derive_t cswitch_invol;
197 typedef struct procstat_entry_s {
201 derive_t vmem_minflt_counter;
202 derive_t vmem_majflt_counter;
204 derive_t cpu_user_counter;
205 derive_t cpu_system_counter;
213 derive_t cswitch_vol;
214 derive_t cswitch_invol;
216 struct procstat_entry_s *next;
219 typedef struct procstat {
220 char name[PROCSTAT_NAME_LEN];
225 unsigned long num_proc;
226 unsigned long num_lwp;
227 unsigned long num_fd;
228 unsigned long vmem_size;
229 unsigned long vmem_rss;
230 unsigned long vmem_data;
231 unsigned long vmem_code;
232 unsigned long stack_size;
234 derive_t vmem_minflt_counter;
235 derive_t vmem_majflt_counter;
237 derive_t cpu_user_counter;
238 derive_t cpu_system_counter;
246 derive_t cswitch_vol;
247 derive_t cswitch_invol;
250 _Bool report_ctx_switch;
252 struct procstat *next;
253 struct procstat_entry_s *instances;
256 static procstat_t *list_head_g = NULL;
258 static _Bool want_init = 1;
259 static _Bool report_ctx_switch = 0;
260 static _Bool report_fd_num = 0;
263 static mach_port_t port_host_self;
264 static mach_port_t port_task_self;
266 static processor_set_name_array_t pset_list;
267 static mach_msg_type_number_t pset_list_len;
268 /* #endif HAVE_THREAD_INFO */
271 static long pagesize_g;
272 static void ps_fill_details(const procstat_t *ps, process_entry_t *entry);
273 /* #endif KERNEL_LINUX */
275 #elif HAVE_LIBKVM_GETPROCS && \
276 (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD)
278 /* #endif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD ||
279 * HAVE_STRUCT_KINFO_PROC_OPENBSD) */
281 #elif HAVE_PROCINFO_H
282 static struct procentry64 procentry[MAXPROCENTRY];
283 static struct thrdentry64 thrdentry[MAXTHRDENTRY];
286 #ifndef _AIXVERSION_610
287 int getprocs64(void *procsinfo, int sizproc, void *fdsinfo, int sizfd,
288 pid_t *index, int count);
289 int getthrds64(pid_t, void *, int, tid64_t *, int);
291 int getargs(void *processBuffer, int bufferLen, char *argsBuffer, int argsLen);
292 #endif /* HAVE_PROCINFO_H */
294 /* put name of process from config to list_head_g tree
295 * list_head_g is a list of 'procstat_t' structs with
296 * processes names we want to watch */
297 static procstat_t *ps_list_register(const char *name, const char *regexp) {
302 new = calloc(1, sizeof(*new));
304 ERROR("processes plugin: ps_list_register: calloc failed.");
307 sstrncpy(new->name, name, sizeof(new->name));
313 new->cswitch_vol = -1;
314 new->cswitch_invol = -1;
316 new->report_fd_num = report_fd_num;
317 new->report_ctx_switch = report_ctx_switch;
320 if (regexp != NULL) {
321 DEBUG("ProcessMatch: adding \"%s\" as criteria to process %s.", regexp,
323 new->re = malloc(sizeof(*new->re));
324 if (new->re == NULL) {
325 ERROR("processes plugin: ps_list_register: malloc failed.");
330 status = regcomp(new->re, regexp, REG_EXTENDED | REG_NOSUB);
332 DEBUG("ProcessMatch: compiling the regular expression \"%s\" failed.",
340 if (regexp != NULL) {
341 ERROR("processes plugin: ps_list_register: "
342 "Regular expression \"%s\" found in config "
343 "file, but support for regular expressions "
344 "has been disabled at compile time.",
351 for (ptr = list_head_g; ptr != NULL; ptr = ptr->next) {
352 if (strcmp(ptr->name, name) == 0) {
353 WARNING("processes plugin: You have configured more "
354 "than one `Process' or "
355 "`ProcessMatch' with the same name. "
356 "All but the first setting will be "
365 if (ptr->next == NULL)
375 } /* void ps_list_register */
377 /* try to match name against entry, returns 1 if success */
378 static int ps_list_match(const char *name, const char *cmdline,
381 if (ps->re != NULL) {
386 if ((str == NULL) || (str[0] == 0))
391 status = regexec(ps->re, str,
399 if (strcmp(ps->name, name) == 0)
403 } /* int ps_list_match */
405 static void ps_update_counter(derive_t *group_counter, derive_t *curr_counter,
406 derive_t new_counter) {
407 unsigned long curr_value;
410 *curr_counter = new_counter;
414 if (new_counter < *curr_counter)
415 curr_value = new_counter + (ULONG_MAX - *curr_counter);
417 curr_value = new_counter - *curr_counter;
419 if (*group_counter == -1)
422 *curr_counter = new_counter;
423 *group_counter += curr_value;
426 /* add process entry to 'instances' of process 'name' (or refresh it) */
427 static void ps_list_add(const char *name, const char *cmdline,
428 process_entry_t *entry) {
429 procstat_entry_t *pse;
434 for (procstat_t *ps = list_head_g; ps != NULL; ps = ps->next) {
435 if ((ps_list_match(name, cmdline, ps)) == 0)
439 ps_fill_details(ps, entry);
442 for (pse = ps->instances; pse != NULL; pse = pse->next)
443 if ((pse->id == entry->id) || (pse->next == NULL))
446 if ((pse == NULL) || (pse->id != entry->id)) {
447 procstat_entry_t *new;
449 new = calloc(1, sizeof(*new));
464 ps->num_proc += entry->num_proc;
465 ps->num_lwp += entry->num_lwp;
466 ps->num_fd += entry->num_fd;
467 ps->vmem_size += entry->vmem_size;
468 ps->vmem_rss += entry->vmem_rss;
469 ps->vmem_data += entry->vmem_data;
470 ps->vmem_code += entry->vmem_code;
471 ps->stack_size += entry->stack_size;
473 if ((entry->io_rchar != -1) && (entry->io_wchar != -1)) {
474 ps_update_counter(&ps->io_rchar, &pse->io_rchar, entry->io_rchar);
475 ps_update_counter(&ps->io_wchar, &pse->io_wchar, entry->io_wchar);
478 if ((entry->io_syscr != -1) && (entry->io_syscw != -1)) {
479 ps_update_counter(&ps->io_syscr, &pse->io_syscr, entry->io_syscr);
480 ps_update_counter(&ps->io_syscw, &pse->io_syscw, entry->io_syscw);
483 if ((entry->cswitch_vol != -1) && (entry->cswitch_vol != -1)) {
484 ps_update_counter(&ps->cswitch_vol, &pse->cswitch_vol,
486 ps_update_counter(&ps->cswitch_invol, &pse->cswitch_invol,
487 entry->cswitch_invol);
490 ps_update_counter(&ps->vmem_minflt_counter, &pse->vmem_minflt_counter,
491 entry->vmem_minflt_counter);
492 ps_update_counter(&ps->vmem_majflt_counter, &pse->vmem_majflt_counter,
493 entry->vmem_majflt_counter);
495 ps_update_counter(&ps->cpu_user_counter, &pse->cpu_user_counter,
496 entry->cpu_user_counter);
497 ps_update_counter(&ps->cpu_system_counter, &pse->cpu_system_counter,
498 entry->cpu_system_counter);
502 /* remove old entries from instances of processes in list_head_g */
503 static void ps_list_reset(void) {
504 procstat_entry_t *pse;
505 procstat_entry_t *pse_prev;
507 for (procstat_t *ps = list_head_g; ps != NULL; ps = ps->next) {
519 while (pse != NULL) {
521 DEBUG("Removing this procstat entry cause it's too old: "
522 "id = %lu; name = %s;",
525 if (pse_prev == NULL) {
526 ps->instances = pse->next;
530 pse_prev->next = pse->next;
532 pse = pse_prev->next;
539 } /* while (pse != NULL) */
540 } /* for (ps = list_head_g; ps != NULL; ps = ps->next) */
543 static void ps_tune_instance(oconfig_item_t *ci, procstat_t *ps) {
544 for (int i = 0; i < ci->children_num; i++) {
545 oconfig_item_t *c = ci->children + i;
547 if (strcasecmp(c->key, "CollectContextSwitch") == 0)
548 cf_util_get_boolean(c, &ps->report_ctx_switch);
549 else if (strcasecmp(c->key, "CollectFileDescriptor") == 0)
550 cf_util_get_boolean(c, &ps->report_fd_num);
552 ERROR("processes plugin: Option `%s' not allowed here.", c->key);
554 } /* for (ci->children) */
555 } /* void ps_tune_instance */
557 /* put all pre-defined 'Process' names from config to list_head_g tree */
558 static int ps_config(oconfig_item_t *ci) {
560 const size_t max_procname_len = 15;
561 #elif KERNEL_SOLARIS || KERNEL_FREEBSD
562 const size_t max_procname_len = MAXCOMLEN - 1;
567 for (int i = 0; i < ci->children_num; ++i) {
568 oconfig_item_t *c = ci->children + i;
570 if (strcasecmp(c->key, "Process") == 0) {
571 if ((c->values_num != 1) || (OCONFIG_TYPE_STRING != c->values[0].type)) {
572 ERROR("processes plugin: `Process' expects exactly "
573 "one string argument (got %i).",
578 #if KERNEL_LINUX || KERNEL_SOLARIS || KERNEL_FREEBSD
579 if (strlen(c->values[0].value.string) > max_procname_len) {
580 WARNING("processes plugin: this platform has a %zu character limit "
581 "to process names. The `Process \"%s\"' option will "
582 "not work as expected.",
583 max_procname_len, c->values[0].value.string);
587 ps = ps_list_register(c->values[0].value.string, NULL);
589 if (c->children_num != 0 && ps != NULL)
590 ps_tune_instance(c, ps);
591 } else if (strcasecmp(c->key, "ProcessMatch") == 0) {
592 if ((c->values_num != 2) || (OCONFIG_TYPE_STRING != c->values[0].type) ||
593 (OCONFIG_TYPE_STRING != c->values[1].type)) {
594 ERROR("processes plugin: `ProcessMatch' needs exactly "
595 "two string arguments (got %i).",
600 ps = ps_list_register(c->values[0].value.string,
601 c->values[1].value.string);
603 if (c->children_num != 0 && ps != NULL)
604 ps_tune_instance(c, ps);
605 } else if (strcasecmp(c->key, "CollectContextSwitch") == 0) {
606 cf_util_get_boolean(c, &report_ctx_switch);
607 } else if (strcasecmp(c->key, "CollectFileDescriptor") == 0) {
608 cf_util_get_boolean(c, &report_fd_num);
610 ERROR("processes plugin: The `%s' configuration option is not "
611 "understood and will be ignored.",
620 static int ps_init(void) {
622 kern_return_t status;
624 port_host_self = mach_host_self();
625 port_task_self = mach_task_self();
627 if (pset_list != NULL) {
628 vm_deallocate(port_task_self, (vm_address_t)pset_list,
629 pset_list_len * sizeof(processor_set_t));
634 if ((status = host_processor_sets(port_host_self, &pset_list,
635 &pset_list_len)) != KERN_SUCCESS) {
636 ERROR("host_processor_sets failed: %s\n", mach_error_string(status));
641 /* #endif HAVE_THREAD_INFO */
644 pagesize_g = sysconf(_SC_PAGESIZE);
645 DEBUG("pagesize_g = %li; CONFIG_HZ = %i;", pagesize_g, CONFIG_HZ);
646 /* #endif KERNEL_LINUX */
648 #elif HAVE_LIBKVM_GETPROCS && \
649 (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD)
650 pagesize = getpagesize();
651 /* #endif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD ||
652 * HAVE_STRUCT_KINFO_PROC_OPENBSD) */
654 #elif HAVE_PROCINFO_H
655 pagesize = getpagesize();
656 #endif /* HAVE_PROCINFO_H */
661 /* submit global state (e.g.: qty of zombies, running, etc..) */
662 static void ps_submit_state(const char *state, double value) {
663 value_list_t vl = VALUE_LIST_INIT;
665 vl.values = &(value_t){.gauge = value};
667 sstrncpy(vl.plugin, "processes", sizeof(vl.plugin));
668 sstrncpy(vl.plugin_instance, "", sizeof(vl.plugin_instance));
669 sstrncpy(vl.type, "ps_state", sizeof(vl.type));
670 sstrncpy(vl.type_instance, state, sizeof(vl.type_instance));
672 plugin_dispatch_values(&vl);
675 /* submit info about specific process (e.g.: memory taken, cpu usage, etc..) */
676 static void ps_submit_proc_list(procstat_t *ps) {
677 value_list_t vl = VALUE_LIST_INIT;
681 sstrncpy(vl.plugin, "processes", sizeof(vl.plugin));
682 sstrncpy(vl.plugin_instance, ps->name, sizeof(vl.plugin_instance));
684 sstrncpy(vl.type, "ps_vm", sizeof(vl.type));
685 vl.values[0].gauge = ps->vmem_size;
687 plugin_dispatch_values(&vl);
689 sstrncpy(vl.type, "ps_rss", sizeof(vl.type));
690 vl.values[0].gauge = ps->vmem_rss;
692 plugin_dispatch_values(&vl);
694 sstrncpy(vl.type, "ps_data", sizeof(vl.type));
695 vl.values[0].gauge = ps->vmem_data;
697 plugin_dispatch_values(&vl);
699 sstrncpy(vl.type, "ps_code", sizeof(vl.type));
700 vl.values[0].gauge = ps->vmem_code;
702 plugin_dispatch_values(&vl);
704 sstrncpy(vl.type, "ps_stacksize", sizeof(vl.type));
705 vl.values[0].gauge = ps->stack_size;
707 plugin_dispatch_values(&vl);
709 sstrncpy(vl.type, "ps_cputime", sizeof(vl.type));
710 vl.values[0].derive = ps->cpu_user_counter;
711 vl.values[1].derive = ps->cpu_system_counter;
713 plugin_dispatch_values(&vl);
715 sstrncpy(vl.type, "ps_count", sizeof(vl.type));
716 vl.values[0].gauge = ps->num_proc;
717 vl.values[1].gauge = ps->num_lwp;
719 plugin_dispatch_values(&vl);
721 sstrncpy(vl.type, "ps_pagefaults", sizeof(vl.type));
722 vl.values[0].derive = ps->vmem_minflt_counter;
723 vl.values[1].derive = ps->vmem_majflt_counter;
725 plugin_dispatch_values(&vl);
727 if ((ps->io_rchar != -1) && (ps->io_wchar != -1)) {
728 sstrncpy(vl.type, "ps_disk_octets", sizeof(vl.type));
729 vl.values[0].derive = ps->io_rchar;
730 vl.values[1].derive = ps->io_wchar;
732 plugin_dispatch_values(&vl);
735 if ((ps->io_syscr != -1) && (ps->io_syscw != -1)) {
736 sstrncpy(vl.type, "ps_disk_ops", sizeof(vl.type));
737 vl.values[0].derive = ps->io_syscr;
738 vl.values[1].derive = ps->io_syscw;
740 plugin_dispatch_values(&vl);
743 if (ps->num_fd > 0) {
744 sstrncpy(vl.type, "file_handles", sizeof(vl.type));
745 vl.values[0].gauge = ps->num_fd;
747 plugin_dispatch_values(&vl);
750 if ((ps->cswitch_vol != -1) && (ps->cswitch_invol != -1)) {
751 sstrncpy(vl.type, "contextswitch", sizeof(vl.type));
752 sstrncpy(vl.type_instance, "voluntary", sizeof(vl.type_instance));
753 vl.values[0].derive = ps->cswitch_vol;
755 plugin_dispatch_values(&vl);
757 sstrncpy(vl.type, "contextswitch", sizeof(vl.type));
758 sstrncpy(vl.type_instance, "involuntary", sizeof(vl.type_instance));
759 vl.values[0].derive = ps->cswitch_invol;
761 plugin_dispatch_values(&vl);
764 DEBUG("name = %s; num_proc = %lu; num_lwp = %lu; num_fd = %lu; "
765 "vmem_size = %lu; vmem_rss = %lu; vmem_data = %lu; "
767 "vmem_minflt_counter = %" PRIi64 "; vmem_majflt_counter = %" PRIi64 "; "
768 "cpu_user_counter = %" PRIi64 "; cpu_system_counter = %" PRIi64 "; "
769 "io_rchar = %" PRIi64 "; io_wchar = %" PRIi64 "; "
770 "io_syscr = %" PRIi64 "; io_syscw = %" PRIi64 "; "
771 "cswitch_vol = %" PRIi64 "; cswitch_invol = %" PRIi64 ";",
772 ps->name, ps->num_proc, ps->num_lwp, ps->num_fd, ps->vmem_size,
773 ps->vmem_rss, ps->vmem_data, ps->vmem_code, ps->vmem_minflt_counter,
774 ps->vmem_majflt_counter, ps->cpu_user_counter, ps->cpu_system_counter,
775 ps->io_rchar, ps->io_wchar, ps->io_syscr, ps->io_syscw, ps->cswitch_vol,
777 } /* void ps_submit_proc_list */
779 #if KERNEL_LINUX || KERNEL_SOLARIS
780 static void ps_submit_fork_rate(derive_t value) {
781 value_list_t vl = VALUE_LIST_INIT;
783 vl.values = &(value_t){.derive = value};
785 sstrncpy(vl.plugin, "processes", sizeof(vl.plugin));
786 sstrncpy(vl.plugin_instance, "", sizeof(vl.plugin_instance));
787 sstrncpy(vl.type, "fork_rate", sizeof(vl.type));
788 sstrncpy(vl.type_instance, "", sizeof(vl.type_instance));
790 plugin_dispatch_values(&vl);
792 #endif /* KERNEL_LINUX || KERNEL_SOLARIS*/
794 /* ------- additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
796 static int ps_read_tasks_status(process_entry_t *ps) {
802 derive_t cswitch_vol = 0;
803 derive_t cswitch_invol = 0;
808 snprintf(dirname, sizeof(dirname), "/proc/%li/task", ps->id);
810 if ((dh = opendir(dirname)) == NULL) {
811 DEBUG("Failed to open directory `%s'", dirname);
815 while ((ent = readdir(dh)) != NULL) {
818 if (!isdigit((int)ent->d_name[0]))
823 if (snprintf(filename, sizeof(filename), "/proc/%li/task/%s/status", ps->id,
824 tpid) >= sizeof(filename)) {
825 DEBUG("Filename too long: `%s'", filename);
829 if ((fh = fopen(filename, "r")) == NULL) {
830 DEBUG("Failed to open file `%s'", filename);
834 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
838 if (strncmp(buffer, "voluntary_ctxt_switches", 23) != 0 &&
839 strncmp(buffer, "nonvoluntary_ctxt_switches", 26) != 0)
842 numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
849 tmp = (derive_t)strtoll(fields[1], &endptr, /* base = */ 10);
850 if ((errno == 0) && (endptr != fields[1])) {
851 if (strncmp(buffer, "voluntary_ctxt_switches", 23) == 0) {
853 } else if (strncmp(buffer, "nonvoluntary_ctxt_switches", 26) == 0) {
854 cswitch_invol += tmp;
857 } /* while (fgets) */
861 WARNING("processes: fclose: %s",
862 sstrerror(errno, errbuf, sizeof(errbuf)));
867 ps->cswitch_vol = cswitch_vol;
868 ps->cswitch_invol = cswitch_invol;
871 } /* int *ps_read_tasks_status */
873 /* Read data from /proc/pid/status */
874 static int ps_read_status(long pid, process_entry_t *ps) {
878 unsigned long lib = 0;
879 unsigned long exe = 0;
880 unsigned long data = 0;
881 unsigned long threads = 0;
885 snprintf(filename, sizeof(filename), "/proc/%li/status", pid);
886 if ((fh = fopen(filename, "r")) == NULL)
889 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
893 if (strncmp(buffer, "Vm", 2) != 0 && strncmp(buffer, "Threads", 7) != 0)
896 numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
903 tmp = strtoul(fields[1], &endptr, /* base = */ 10);
904 if ((errno == 0) && (endptr != fields[1])) {
905 if (strncmp(buffer, "VmData", 6) == 0) {
907 } else if (strncmp(buffer, "VmLib", 5) == 0) {
909 } else if (strncmp(buffer, "VmExe", 5) == 0) {
911 } else if (strncmp(buffer, "Threads", 7) == 0) {
915 } /* while (fgets) */
919 WARNING("processes: fclose: %s", sstrerror(errno, errbuf, sizeof(errbuf)));
922 ps->vmem_data = data * 1024;
923 ps->vmem_code = (exe + lib) * 1024;
925 ps->num_lwp = threads;
928 } /* int *ps_read_status */
930 static int ps_read_io(process_entry_t *ps) {
938 snprintf(filename, sizeof(filename), "/proc/%li/io", ps->id);
939 if ((fh = fopen(filename, "r")) == NULL) {
940 DEBUG("ps_read_io: Failed to open file `%s'", filename);
944 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
945 derive_t *val = NULL;
949 if (strncasecmp(buffer, "rchar:", 6) == 0)
950 val = &(ps->io_rchar);
951 else if (strncasecmp(buffer, "wchar:", 6) == 0)
952 val = &(ps->io_wchar);
953 else if (strncasecmp(buffer, "syscr:", 6) == 0)
954 val = &(ps->io_syscr);
955 else if (strncasecmp(buffer, "syscw:", 6) == 0)
956 val = &(ps->io_syscw);
960 numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
967 tmp = strtoll(fields[1], &endptr, /* base = */ 10);
968 if ((errno != 0) || (endptr == fields[1]))
971 *val = (derive_t)tmp;
972 } /* while (fgets) */
976 WARNING("processes: fclose: %s", sstrerror(errno, errbuf, sizeof(errbuf)));
979 } /* int ps_read_io (...) */
981 static int ps_count_fd(int pid) {
987 snprintf(dirname, sizeof(dirname), "/proc/%i/fd", pid);
989 if ((dh = opendir(dirname)) == NULL) {
990 DEBUG("Failed to open directory `%s'", dirname);
993 while ((ent = readdir(dh)) != NULL) {
994 if (!isdigit((int)ent->d_name[0]))
1001 return (count >= 1) ? count : 1;
1002 } /* int ps_count_fd (pid) */
1004 static void ps_fill_details(const procstat_t *ps, process_entry_t *entry) {
1005 if (entry->has_io == 0) {
1010 if (ps->report_ctx_switch) {
1011 if (entry->has_cswitch == 0) {
1012 ps_read_tasks_status(entry);
1013 entry->has_cswitch = 1;
1017 if (ps->report_fd_num) {
1019 if (entry->has_fd == 0 && (num_fd = ps_count_fd(entry->id)) > 0) {
1020 entry->num_fd = num_fd;
1024 } /* void ps_fill_details (...) */
1026 static int ps_read_process(long pid, process_entry_t *ps, char *state) {
1036 size_t name_start_pos;
1037 size_t name_end_pos;
1040 derive_t cpu_user_counter;
1041 derive_t cpu_system_counter;
1042 long long unsigned vmem_size;
1043 long long unsigned vmem_rss;
1044 long long unsigned stack_size;
1048 snprintf(filename, sizeof(filename), "/proc/%li/stat", pid);
1050 status = read_file_contents(filename, buffer, sizeof(buffer) - 1);
1053 buffer_len = (size_t)status;
1054 buffer[buffer_len] = 0;
1056 /* The name of the process is enclosed in parens. Since the name can
1057 * contain parens itself, spaces, numbers and pretty much everything
1058 * else, use these to determine the process name. We don't use
1059 * strchr(3) and strrchr(3) to avoid pointer arithmetic which would
1060 * otherwise be required to determine name_len. */
1062 while (name_start_pos < buffer_len && buffer[name_start_pos] != '(')
1065 name_end_pos = buffer_len;
1066 while (name_end_pos > 0 && buffer[name_end_pos] != ')')
1069 /* Either '(' or ')' is not found or they are in the wrong order.
1070 * Anyway, something weird that shouldn't happen ever. */
1071 if (name_start_pos >= name_end_pos) {
1072 ERROR("processes plugin: name_start_pos = %zu >= name_end_pos = %zu",
1073 name_start_pos, name_end_pos);
1077 name_len = (name_end_pos - name_start_pos) - 1;
1078 if (name_len >= sizeof(ps->name))
1079 name_len = sizeof(ps->name) - 1;
1081 sstrncpy(ps->name, &buffer[name_start_pos + 1], name_len + 1);
1083 if ((buffer_len - name_end_pos) < 2)
1085 buffer_ptr = &buffer[name_end_pos + 2];
1087 fields_len = strsplit(buffer_ptr, fields, STATIC_ARRAY_SIZE(fields));
1088 if (fields_len < 22) {
1089 DEBUG("processes plugin: ps_read_process (pid = %li):"
1090 " `%s' has only %i fields..",
1091 pid, filename, fields_len);
1095 *state = fields[0][0];
1097 if (*state == 'Z') {
1101 ps->num_lwp = strtoul(fields[17], /* endptr = */ NULL, /* base = */ 10);
1102 if ((ps_read_status(pid, ps)) != 0) {
1106 DEBUG("ps_read_process: did not get vmem data for pid %li", pid);
1108 if (ps->num_lwp == 0)
1113 /* Leave the rest at zero if this is only a zombi */
1114 if (ps->num_proc == 0) {
1115 DEBUG("processes plugin: This is only a zombie: pid = %li; "
1121 cpu_user_counter = atoll(fields[11]);
1122 cpu_system_counter = atoll(fields[12]);
1123 vmem_size = atoll(fields[20]);
1124 vmem_rss = atoll(fields[21]);
1125 ps->vmem_minflt_counter = atol(fields[7]);
1126 ps->vmem_majflt_counter = atol(fields[9]);
1129 unsigned long long stack_start = atoll(fields[25]);
1130 unsigned long long stack_ptr = atoll(fields[26]);
1132 stack_size = (stack_start > stack_ptr) ? stack_start - stack_ptr
1133 : stack_ptr - stack_start;
1136 /* Convert jiffies to useconds */
1137 cpu_user_counter = cpu_user_counter * 1000000 / CONFIG_HZ;
1138 cpu_system_counter = cpu_system_counter * 1000000 / CONFIG_HZ;
1139 vmem_rss = vmem_rss * pagesize_g;
1141 ps->cpu_user_counter = cpu_user_counter;
1142 ps->cpu_system_counter = cpu_system_counter;
1143 ps->vmem_size = (unsigned long)vmem_size;
1144 ps->vmem_rss = (unsigned long)vmem_rss;
1145 ps->stack_size = (unsigned long)stack_size;
1147 /* no data by default. May be filled by ps_fill_details () */
1153 ps->cswitch_vol = -1;
1154 ps->cswitch_invol = -1;
1158 } /* int ps_read_process (...) */
1160 static char *ps_get_cmdline(long pid, char *name, char *buf, size_t buf_len) {
1164 char file[PATH_MAX];
1169 if ((pid < 1) || (NULL == buf) || (buf_len < 2))
1172 snprintf(file, sizeof(file), "/proc/%li/cmdline", pid);
1175 fd = open(file, O_RDONLY);
1178 /* ENOENT means the process exited while we were handling it.
1179 * Don't complain about this, it only fills the logs. */
1180 if (errno != ENOENT)
1181 WARNING("processes plugin: Failed to open `%s': %s.", file,
1182 sstrerror(errno, errbuf, sizeof(errbuf)));
1194 status = read(fd, (void *)buf_ptr, len);
1199 if ((EAGAIN == errno) || (EINTR == errno))
1202 WARNING("processes plugin: Failed to read from `%s': %s.", file,
1203 sstrerror(errno, errbuf, sizeof(errbuf)));
1223 /* cmdline not available; e.g. kernel thread, zombie */
1227 snprintf(buf, buf_len, "[%s]", name);
1231 assert(n <= buf_len);
1238 /* remove trailing whitespace */
1239 while ((n > 0) && (isspace(buf[n]) || ('\0' == buf[n]))) {
1244 /* arguments are separated by '\0' in /proc/<pid>/cmdline */
1251 } /* char *ps_get_cmdline (...) */
1253 static int read_fork_rate(void) {
1257 _Bool value_valid = 0;
1259 proc_stat = fopen("/proc/stat", "r");
1260 if (proc_stat == NULL) {
1262 ERROR("processes plugin: fopen (/proc/stat) failed: %s",
1263 sstrerror(errno, errbuf, sizeof(errbuf)));
1267 while (fgets(buffer, sizeof(buffer), proc_stat) != NULL) {
1272 fields_num = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
1273 if (fields_num != 2)
1276 if (strcmp("processes", fields[0]) != 0)
1279 status = parse_value(fields[1], &value, DS_TYPE_DERIVE);
1290 ps_submit_fork_rate(value.derive);
1293 #endif /*KERNEL_LINUX */
1296 static char *ps_get_cmdline(long pid,
1297 char *name __attribute__((unused)), /* {{{ */
1298 char *buffer, size_t buffer_size) {
1299 char path[PATH_MAX];
1303 snprintf(path, sizeof(path), "/proc/%li/psinfo", pid);
1305 status = read_file_contents(path, (void *)&info, sizeof(info));
1306 if ((status < 0) || (((size_t)status) != sizeof(info))) {
1307 ERROR("processes plugin: Unexpected return value "
1308 "while reading \"%s\": "
1309 "Returned %zd but expected %zu.",
1310 path, status, buffer_size);
1314 info.pr_psargs[sizeof(info.pr_psargs) - 1] = 0;
1315 sstrncpy(buffer, info.pr_psargs, buffer_size);
1318 } /* }}} int ps_get_cmdline */
1321 * Reads process information on the Solaris OS. The information comes mainly
1323 * /proc/PID/status, /proc/PID/psinfo and /proc/PID/usage
1324 * The values for input and ouput chars are calculated "by hand"
1325 * Added a few "solaris" specific process states as well
1327 static int ps_read_process(long pid, process_entry_t *ps, char *state) {
1329 char f_psinfo[64], f_usage[64];
1332 pstatus_t *myStatus;
1336 snprintf(filename, sizeof(filename), "/proc/%li/status", pid);
1337 snprintf(f_psinfo, sizeof(f_psinfo), "/proc/%li/psinfo", pid);
1338 snprintf(f_usage, sizeof(f_usage), "/proc/%li/usage", pid);
1340 buffer = calloc(1, sizeof(pstatus_t));
1341 read_file_contents(filename, buffer, sizeof(pstatus_t));
1342 myStatus = (pstatus_t *)buffer;
1344 buffer = calloc(1, sizeof(psinfo_t));
1345 read_file_contents(f_psinfo, buffer, sizeof(psinfo_t));
1346 myInfo = (psinfo_t *)buffer;
1348 buffer = calloc(1, sizeof(prusage_t));
1349 read_file_contents(f_usage, buffer, sizeof(prusage_t));
1350 myUsage = (prusage_t *)buffer;
1352 sstrncpy(ps->name, myInfo->pr_fname, sizeof(myInfo->pr_fname));
1353 ps->num_lwp = myStatus->pr_nlwp;
1354 if (myInfo->pr_wstat != 0) {
1365 ps->num_lwp = myInfo->pr_nlwp;
1369 * Convert system time and user time from nanoseconds to microseconds
1370 * for compatibility with the linux module
1372 ps->cpu_system_counter = myStatus->pr_stime.tv_nsec / 1000;
1373 ps->cpu_user_counter = myStatus->pr_utime.tv_nsec / 1000;
1376 * Convert rssize from KB to bytes to be consistent w/ the linux module
1378 ps->vmem_rss = myInfo->pr_rssize * 1024;
1379 ps->vmem_size = myInfo->pr_size * 1024;
1380 ps->vmem_minflt_counter = myUsage->pr_minf;
1381 ps->vmem_majflt_counter = myUsage->pr_majf;
1384 * TODO: Data and code segment calculations for Solaris
1389 ps->stack_size = myStatus->pr_stksize;
1392 * TODO: File descriptor count for Solaris
1397 * Calculating input/ouput chars
1398 * Formula used is total chars / total blocks => chars/block
1399 * then convert input/output blocks to chars
1401 ulong_t tot_chars = myUsage->pr_ioch;
1402 ulong_t tot_blocks = myUsage->pr_inblk + myUsage->pr_oublk;
1403 ulong_t chars_per_block = 1;
1404 if (tot_blocks != 0)
1405 chars_per_block = tot_chars / tot_blocks;
1406 ps->io_rchar = myUsage->pr_inblk * chars_per_block;
1407 ps->io_wchar = myUsage->pr_oublk * chars_per_block;
1408 ps->io_syscr = myUsage->pr_sysc;
1409 ps->io_syscw = myUsage->pr_sysc;
1412 * TODO: context switch counters for Solaris
1414 ps->cswitch_vol = -1;
1415 ps->cswitch_invol = -1;
1418 * TODO: Find way of setting BLOCKED and PAGING status
1422 if (myStatus->pr_flags & PR_ASLEEP)
1424 else if (myStatus->pr_flags & PR_STOPPED)
1426 else if (myStatus->pr_flags & PR_DETACH)
1428 else if (myStatus->pr_flags & PR_DAEMON)
1430 else if (myStatus->pr_flags & PR_ISSYS)
1432 else if (myStatus->pr_flags & PR_ORPHAN)
1443 * Reads the number of threads created since the last reboot. On Solaris these
1444 * are retrieved from kstat (module cpu, name sys, class misc, stat nthreads).
1445 * The result is the sum for all the threads created on each cpu
1447 static int read_fork_rate(void) {
1448 extern kstat_ctl_t *kc;
1449 derive_t result = 0;
1454 for (kstat_t *ksp_chain = kc->kc_chain; ksp_chain != NULL;
1455 ksp_chain = ksp_chain->ks_next) {
1456 if ((strcmp(ksp_chain->ks_module, "cpu") == 0) &&
1457 (strcmp(ksp_chain->ks_name, "sys") == 0) &&
1458 (strcmp(ksp_chain->ks_class, "misc") == 0)) {
1461 kstat_read(kc, ksp_chain, NULL);
1463 tmp = get_kstat_value(ksp_chain, "nthreads");
1469 ps_submit_fork_rate(result);
1472 #endif /* KERNEL_SOLARIS */
1474 #if HAVE_THREAD_INFO
1475 static int mach_get_task_name(task_t t, int *pid, char *name,
1476 size_t name_max_len) {
1479 struct kinfo_proc kp;
1484 mib[2] = KERN_PROC_PID;
1486 if (pid_for_task(t, pid) != KERN_SUCCESS)
1490 kp_size = sizeof(kp);
1491 if (sysctl(mib, 4, &kp, &kp_size, NULL, 0) != 0)
1494 if (name_max_len > (MAXCOMLEN + 1))
1495 name_max_len = MAXCOMLEN + 1;
1497 strncpy(name, kp.kp_proc.p_comm, name_max_len - 1);
1498 name[name_max_len - 1] = '\0';
1500 DEBUG("pid = %i; name = %s;", *pid, name);
1502 /* We don't do the special handling for `p_comm == "LaunchCFMApp"' as
1503 * `top' does it, because it is a lot of work and only used when
1504 * debugging. -octo */
1508 #endif /* HAVE_THREAD_INFO */
1509 /* ------- end of additional functions for KERNEL_LINUX/HAVE_THREAD_INFO -------
1512 /* do actual readings from kernel */
1513 static int ps_read(void) {
1514 #if HAVE_THREAD_INFO
1515 kern_return_t status;
1517 processor_set_t port_pset_priv;
1519 task_array_t task_list;
1520 mach_msg_type_number_t task_list_len;
1523 char task_name[MAXCOMLEN + 1];
1525 thread_act_array_t thread_list;
1526 mach_msg_type_number_t thread_list_len;
1527 thread_basic_info_data_t thread_data;
1528 mach_msg_type_number_t thread_data_len;
1537 process_entry_t pse;
1542 * The Mach-concept is a little different from the traditional UNIX
1543 * concept: All the work is done in threads. Threads are contained in
1544 * `tasks'. Therefore, `task status' doesn't make much sense, since
1545 * it's actually a `thread status'.
1546 * Tasks are assigned to sets of processors, so that's where you go to
1549 for (mach_msg_type_number_t pset = 0; pset < pset_list_len; pset++) {
1550 if ((status = host_processor_set_priv(port_host_self, pset_list[pset],
1551 &port_pset_priv)) != KERN_SUCCESS) {
1552 ERROR("host_processor_set_priv failed: %s\n", mach_error_string(status));
1556 if ((status = processor_set_tasks(port_pset_priv, &task_list,
1557 &task_list_len)) != KERN_SUCCESS) {
1558 ERROR("processor_set_tasks failed: %s\n", mach_error_string(status));
1559 mach_port_deallocate(port_task_self, port_pset_priv);
1563 for (mach_msg_type_number_t task = 0; task < task_list_len; task++) {
1565 if (mach_get_task_name(task_list[task], &task_pid, task_name,
1566 PROCSTAT_NAME_LEN) == 0) {
1567 /* search for at least one match */
1568 for (ps = list_head_g; ps != NULL; ps = ps->next)
1569 /* FIXME: cmdline should be here instead of NULL */
1570 if (ps_list_match(task_name, NULL, ps) == 1)
1574 /* Collect more detailed statistics for this process */
1576 task_basic_info_data_t task_basic_info;
1577 mach_msg_type_number_t task_basic_info_len;
1578 task_events_info_data_t task_events_info;
1579 mach_msg_type_number_t task_events_info_len;
1580 task_absolutetime_info_data_t task_absolutetime_info;
1581 mach_msg_type_number_t task_absolutetime_info_len;
1583 memset(&pse, '\0', sizeof(pse));
1586 task_basic_info_len = TASK_BASIC_INFO_COUNT;
1587 status = task_info(task_list[task], TASK_BASIC_INFO,
1588 (task_info_t)&task_basic_info, &task_basic_info_len);
1589 if (status != KERN_SUCCESS) {
1590 ERROR("task_info failed: %s", mach_error_string(status));
1591 continue; /* with next thread_list */
1594 task_events_info_len = TASK_EVENTS_INFO_COUNT;
1596 task_info(task_list[task], TASK_EVENTS_INFO,
1597 (task_info_t)&task_events_info, &task_events_info_len);
1598 if (status != KERN_SUCCESS) {
1599 ERROR("task_info failed: %s", mach_error_string(status));
1600 continue; /* with next thread_list */
1603 task_absolutetime_info_len = TASK_ABSOLUTETIME_INFO_COUNT;
1604 status = task_info(task_list[task], TASK_ABSOLUTETIME_INFO,
1605 (task_info_t)&task_absolutetime_info,
1606 &task_absolutetime_info_len);
1607 if (status != KERN_SUCCESS) {
1608 ERROR("task_info failed: %s", mach_error_string(status));
1609 continue; /* with next thread_list */
1613 pse.vmem_size = task_basic_info.virtual_size;
1614 pse.vmem_rss = task_basic_info.resident_size;
1615 /* Does not seem to be easily exposed */
1624 /* File descriptor count not implemented */
1627 pse.vmem_minflt_counter = task_events_info.cow_faults;
1628 pse.vmem_majflt_counter = task_events_info.faults;
1630 pse.cpu_user_counter = task_absolutetime_info.total_user;
1631 pse.cpu_system_counter = task_absolutetime_info.total_system;
1633 /* context switch counters not implemented */
1634 pse.cswitch_vol = -1;
1635 pse.cswitch_invol = -1;
1638 status = task_threads(task_list[task], &thread_list, &thread_list_len);
1639 if (status != KERN_SUCCESS) {
1640 /* Apple's `top' treats this case a zombie. It
1641 * makes sense to some extend: A `zombie'
1642 * thread is nonsense, since the task/process
1645 DEBUG("task_threads failed: %s", mach_error_string(status));
1646 if (task_list[task] != port_task_self)
1647 mach_port_deallocate(port_task_self, task_list[task]);
1648 continue; /* with next task_list */
1651 for (mach_msg_type_number_t thread = 0; thread < thread_list_len;
1653 thread_data_len = THREAD_BASIC_INFO_COUNT;
1654 status = thread_info(thread_list[thread], THREAD_BASIC_INFO,
1655 (thread_info_t)&thread_data, &thread_data_len);
1656 if (status != KERN_SUCCESS) {
1657 ERROR("thread_info failed: %s", mach_error_string(status));
1658 if (task_list[task] != port_task_self)
1659 mach_port_deallocate(port_task_self, thread_list[thread]);
1660 continue; /* with next thread_list */
1666 switch (thread_data.run_state) {
1667 case TH_STATE_RUNNING:
1670 case TH_STATE_STOPPED:
1671 /* What exactly is `halted'? */
1672 case TH_STATE_HALTED:
1675 case TH_STATE_WAITING:
1678 case TH_STATE_UNINTERRUPTIBLE:
1681 /* There is no `zombie' case here,
1682 * since there are no zombie-threads.
1683 * There's only zombie tasks, which are
1686 WARNING("Unknown thread status: %i", thread_data.run_state);
1688 } /* switch (thread_data.run_state) */
1690 if (task_list[task] != port_task_self) {
1691 status = mach_port_deallocate(port_task_self, thread_list[thread]);
1692 if (status != KERN_SUCCESS)
1693 ERROR("mach_port_deallocate failed: %s", mach_error_string(status));
1695 } /* for (thread_list) */
1697 if ((status = vm_deallocate(port_task_self, (vm_address_t)thread_list,
1698 thread_list_len * sizeof(thread_act_t))) !=
1700 ERROR("vm_deallocate failed: %s", mach_error_string(status));
1703 thread_list_len = 0;
1705 /* Only deallocate the task port, if it isn't our own.
1706 * Don't know what would happen in that case, but this
1707 * is what Apple's top does.. ;) */
1708 if (task_list[task] != port_task_self) {
1709 status = mach_port_deallocate(port_task_self, task_list[task]);
1710 if (status != KERN_SUCCESS)
1711 ERROR("mach_port_deallocate failed: %s", mach_error_string(status));
1715 /* FIXME: cmdline should be here instead of NULL */
1716 ps_list_add(task_name, NULL, &pse);
1717 } /* for (task_list) */
1719 if ((status = vm_deallocate(port_task_self, (vm_address_t)task_list,
1720 task_list_len * sizeof(task_t))) !=
1722 ERROR("vm_deallocate failed: %s", mach_error_string(status));
1727 if ((status = mach_port_deallocate(port_task_self, port_pset_priv)) !=
1729 ERROR("mach_port_deallocate failed: %s", mach_error_string(status));
1731 } /* for (pset_list) */
1733 ps_submit_state("running", running);
1734 ps_submit_state("sleeping", sleeping);
1735 ps_submit_state("zombies", zombies);
1736 ps_submit_state("stopped", stopped);
1737 ps_submit_state("blocked", blocked);
1739 for (ps = list_head_g; ps != NULL; ps = ps->next)
1740 ps_submit_proc_list(ps);
1741 /* #endif HAVE_THREAD_INFO */
1755 char cmdline[CMDLINE_BUFFER_SIZE];
1758 process_entry_t pse;
1761 running = sleeping = zombies = stopped = paging = blocked = 0;
1764 if ((proc = opendir("/proc")) == NULL) {
1766 ERROR("Cannot open `/proc': %s", sstrerror(errno, errbuf, sizeof(errbuf)));
1770 while ((ent = readdir(proc)) != NULL) {
1771 if (!isdigit(ent->d_name[0]))
1774 if ((pid = atol(ent->d_name)) < 1)
1777 memset(&pse, 0, sizeof(pse));
1780 status = ps_read_process(pid, &pse, &state);
1782 DEBUG("ps_read_process failed: %i", status);
1807 ps_list_add(pse.name,
1808 ps_get_cmdline(pid, pse.name, cmdline, sizeof(cmdline)), &pse);
1813 ps_submit_state("running", running);
1814 ps_submit_state("sleeping", sleeping);
1815 ps_submit_state("zombies", zombies);
1816 ps_submit_state("stopped", stopped);
1817 ps_submit_state("paging", paging);
1818 ps_submit_state("blocked", blocked);
1820 for (procstat_t *ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1821 ps_submit_proc_list(ps_ptr);
1824 /* #endif KERNEL_LINUX */
1826 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD
1836 char errbuf[_POSIX2_LINE_MAX];
1837 struct kinfo_proc *procs; /* array of processes */
1838 struct kinfo_proc *proc_ptr = NULL;
1839 int count; /* returns number of processes */
1841 process_entry_t pse;
1845 /* Open the kvm interface, get a descriptor */
1846 kd = kvm_openfiles(NULL, "/dev/null", NULL, 0, errbuf);
1848 ERROR("processes plugin: Cannot open kvm interface: %s", errbuf);
1852 /* Get the list of processes. */
1853 procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, &count);
1854 if (procs == NULL) {
1855 ERROR("processes plugin: Cannot get kvm processes list: %s",
1861 /* Iterate through the processes in kinfo_proc */
1862 for (int i = 0; i < count; i++) {
1863 /* Create only one process list entry per _process_, i.e.
1864 * filter out threads (duplicate PID entries). */
1865 if ((proc_ptr == NULL) || (proc_ptr->ki_pid != procs[i].ki_pid)) {
1866 char cmdline[CMDLINE_BUFFER_SIZE] = "";
1867 _Bool have_cmdline = 0;
1869 proc_ptr = &(procs[i]);
1870 /* Don't probe system processes and processes without arguments */
1871 if (((procs[i].ki_flag & P_SYSTEM) == 0) && (procs[i].ki_args != NULL)) {
1876 /* retrieve the arguments */
1877 argv = kvm_getargv(kd, proc_ptr, /* nchr = */ 0);
1879 if ((argv != NULL) && (argv[0] != NULL)) {
1880 while (argv[argc] != NULL)
1883 status = strjoin(cmdline, sizeof(cmdline), argv, argc, " ");
1885 WARNING("processes plugin: Command line did not fit into buffer.");
1889 } /* if (process has argument list) */
1891 memset(&pse, 0, sizeof(pse));
1892 pse.id = procs[i].ki_pid;
1895 pse.num_lwp = procs[i].ki_numthreads;
1897 pse.vmem_size = procs[i].ki_size;
1898 pse.vmem_rss = procs[i].ki_rssize * pagesize;
1899 pse.vmem_data = procs[i].ki_dsize * pagesize;
1900 pse.vmem_code = procs[i].ki_tsize * pagesize;
1901 pse.stack_size = procs[i].ki_ssize * pagesize;
1902 pse.vmem_minflt_counter = procs[i].ki_rusage.ru_minflt;
1903 pse.vmem_majflt_counter = procs[i].ki_rusage.ru_majflt;
1905 pse.cpu_user_counter = 0;
1906 pse.cpu_system_counter = 0;
1908 * The u-area might be swapped out, and we can't get
1909 * at it because we have a crashdump and no swap.
1910 * If it's here fill in these fields, otherwise, just
1913 if (procs[i].ki_flag & P_INMEM) {
1914 pse.cpu_user_counter = procs[i].ki_rusage.ru_utime.tv_usec +
1915 (1000000lu * procs[i].ki_rusage.ru_utime.tv_sec);
1916 pse.cpu_system_counter =
1917 procs[i].ki_rusage.ru_stime.tv_usec +
1918 (1000000lu * procs[i].ki_rusage.ru_stime.tv_sec);
1927 /* file descriptor count not implemented */
1930 /* context switch counters not implemented */
1931 pse.cswitch_vol = -1;
1932 pse.cswitch_invol = -1;
1934 ps_list_add(procs[i].ki_comm, have_cmdline ? cmdline : NULL, &pse);
1936 switch (procs[i].ki_stat) {
1959 } /* if ((proc_ptr == NULL) || (proc_ptr->ki_pid != procs[i].ki_pid)) */
1964 ps_submit_state("running", running);
1965 ps_submit_state("sleeping", sleeping);
1966 ps_submit_state("zombies", zombies);
1967 ps_submit_state("stopped", stopped);
1968 ps_submit_state("blocked", blocked);
1969 ps_submit_state("idle", idle);
1970 ps_submit_state("wait", wait);
1972 for (procstat_t *ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1973 ps_submit_proc_list(ps_ptr);
1974 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
1976 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_OPENBSD
1987 struct kinfo_proc *procs; /* array of processes */
1988 struct kinfo_proc *proc_ptr = NULL;
1989 int count; /* returns number of processes */
1991 process_entry_t pse;
1995 /* Open the kvm interface, get a descriptor */
1996 kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf);
1998 ERROR("processes plugin: Cannot open kvm interface: %s", errbuf);
2002 /* Get the list of processes. */
2003 procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc), &count);
2004 if (procs == NULL) {
2005 ERROR("processes plugin: Cannot get kvm processes list: %s",
2011 /* Iterate through the processes in kinfo_proc */
2012 for (int i = 0; i < count; i++) {
2013 /* Create only one process list entry per _process_, i.e.
2014 * filter out threads (duplicate PID entries). */
2015 if ((proc_ptr == NULL) || (proc_ptr->p_pid != procs[i].p_pid)) {
2016 char cmdline[CMDLINE_BUFFER_SIZE] = "";
2017 _Bool have_cmdline = 0;
2019 proc_ptr = &(procs[i]);
2020 /* Don't probe zombie processes */
2021 if (!P_ZOMBIE(proc_ptr)) {
2026 /* retrieve the arguments */
2027 argv = kvm_getargv(kd, proc_ptr, /* nchr = */ 0);
2029 if ((argv != NULL) && (argv[0] != NULL)) {
2030 while (argv[argc] != NULL)
2033 status = strjoin(cmdline, sizeof(cmdline), argv, argc, " ");
2035 WARNING("processes plugin: Command line did not fit into buffer.");
2039 } /* if (process has argument list) */
2041 memset(&pse, 0, sizeof(pse));
2042 pse.id = procs[i].p_pid;
2045 pse.num_lwp = 1; /* XXX: accumulate p_tid values for a single p_pid ? */
2047 pse.vmem_rss = procs[i].p_vm_rssize * pagesize;
2048 pse.vmem_data = procs[i].p_vm_dsize * pagesize;
2049 pse.vmem_code = procs[i].p_vm_tsize * pagesize;
2050 pse.stack_size = procs[i].p_vm_ssize * pagesize;
2051 pse.vmem_size = pse.stack_size + pse.vmem_code + pse.vmem_data;
2052 pse.vmem_minflt_counter = procs[i].p_uru_minflt;
2053 pse.vmem_majflt_counter = procs[i].p_uru_majflt;
2055 pse.cpu_user_counter =
2056 procs[i].p_uutime_usec + (1000000lu * procs[i].p_uutime_sec);
2057 pse.cpu_system_counter =
2058 procs[i].p_ustime_usec + (1000000lu * procs[i].p_ustime_sec);
2066 /* file descriptor count not implemented */
2069 /* context switch counters not implemented */
2070 pse.cswitch_vol = -1;
2071 pse.cswitch_invol = -1;
2073 ps_list_add(procs[i].p_comm, have_cmdline ? cmdline : NULL, &pse);
2075 switch (procs[i].p_stat) {
2098 } /* if ((proc_ptr == NULL) || (proc_ptr->p_pid != procs[i].p_pid)) */
2103 ps_submit_state("running", running);
2104 ps_submit_state("sleeping", sleeping);
2105 ps_submit_state("zombies", zombies);
2106 ps_submit_state("stopped", stopped);
2107 ps_submit_state("onproc", onproc);
2108 ps_submit_state("idle", idle);
2109 ps_submit_state("dead", dead);
2111 for (procstat_t *ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
2112 ps_submit_proc_list(ps_ptr);
2113 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_OPENBSD */
2115 #elif HAVE_PROCINFO_H
2127 process_entry_t pse;
2130 while ((nprocs = getprocs64(procentry, sizeof(struct procentry64),
2131 /* fdsinfo = */ NULL, sizeof(struct fdsinfo64),
2132 &pindex, MAXPROCENTRY)) > 0) {
2133 for (int i = 0; i < nprocs; i++) {
2136 char arglist[MAXARGLN + 1];
2140 if (procentry[i].pi_state == SNONE)
2142 /* if (procentry[i].pi_state == SZOMB) FIXME */
2144 cmdline = procentry[i].pi_comm;
2145 cargs = procentry[i].pi_comm;
2146 if (procentry[i].pi_flags & SKPROC) {
2147 if (procentry[i].pi_pid == 0)
2148 cmdline = "swapper";
2151 if (getargs(&procentry[i], sizeof(struct procentry64), arglist,
2156 while (++n < MAXARGLN) {
2157 if (arglist[n] == '\0') {
2158 if (arglist[n + 1] == '\0')
2167 memset(&pse, 0, sizeof(pse));
2169 pse.id = procentry[i].pi_pid;
2170 pse.num_lwp = procentry[i].pi_thcount;
2174 while ((nthreads = getthrds64(procentry[i].pi_pid, thrdentry,
2175 sizeof(struct thrdentry64), &thindex,
2176 MAXTHRDENTRY)) > 0) {
2179 for (j = 0; j < nthreads; j++) {
2180 switch (thrdentry[j].ti_state) {
2181 /* case TSNONE: break; */
2184 break; /* FIXME is really blocked */
2202 if (nthreads < MAXTHRDENTRY)
2206 /* tv_usec is nanosec ??? */
2207 pse.cpu_user_counter = procentry[i].pi_ru.ru_utime.tv_sec * 1000000 +
2208 procentry[i].pi_ru.ru_utime.tv_usec / 1000;
2210 /* tv_usec is nanosec ??? */
2211 pse.cpu_system_counter = procentry[i].pi_ru.ru_stime.tv_sec * 1000000 +
2212 procentry[i].pi_ru.ru_stime.tv_usec / 1000;
2214 pse.vmem_minflt_counter = procentry[i].pi_minflt;
2215 pse.vmem_majflt_counter = procentry[i].pi_majflt;
2217 pse.vmem_size = procentry[i].pi_tsize + procentry[i].pi_dvm * pagesize;
2218 pse.vmem_rss = (procentry[i].pi_drss + procentry[i].pi_trss) * pagesize;
2219 /* Not supported/implemented */
2231 pse.cswitch_vol = -1;
2232 pse.cswitch_invol = -1;
2234 ps_list_add(cmdline, cargs, &pse);
2235 } /* for (i = 0 .. nprocs) */
2237 if (nprocs < MAXPROCENTRY)
2239 } /* while (getprocs64() > 0) */
2240 ps_submit_state("running", running);
2241 ps_submit_state("sleeping", sleeping);
2242 ps_submit_state("zombies", zombies);
2243 ps_submit_state("stopped", stopped);
2244 ps_submit_state("paging", paging);
2245 ps_submit_state("blocked", blocked);
2247 for (procstat_t *ps = list_head_g; ps != NULL; ps = ps->next)
2248 ps_submit_proc_list(ps);
2249 /* #endif HAVE_PROCINFO_H */
2251 #elif KERNEL_SOLARIS
2253 * The Solaris section adds a few more process states and removes some
2254 * process states compared to linux. Most notably there is no "PAGING"
2255 * and "BLOCKED" state for a process. The rest is similar to the linux
2273 char cmdline[PRARGSZ];
2277 proc = opendir("/proc");
2281 while ((ent = readdir(proc)) != NULL) {
2283 process_entry_t pse;
2286 if (!isdigit((int)ent->d_name[0]))
2289 pid = strtol(ent->d_name, &endptr, 10);
2290 if (*endptr != 0) /* value didn't completely parse as a number */
2293 memset(&pse, 0, sizeof(pse));
2296 status = ps_read_process(pid, &pse, &state);
2298 DEBUG("ps_read_process failed: %i", status);
2329 ps_list_add(pse.name,
2330 ps_get_cmdline(pid, pse.name, cmdline, sizeof(cmdline)), &pse);
2331 } /* while(readdir) */
2334 ps_submit_state("running", running);
2335 ps_submit_state("sleeping", sleeping);
2336 ps_submit_state("zombies", zombies);
2337 ps_submit_state("stopped", stopped);
2338 ps_submit_state("detached", detached);
2339 ps_submit_state("daemon", daemon);
2340 ps_submit_state("system", system);
2341 ps_submit_state("orphan", orphan);
2343 for (procstat_t *ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
2344 ps_submit_proc_list(ps_ptr);
2347 #endif /* KERNEL_SOLARIS */
2354 void module_register(void) {
2355 plugin_register_complex_config("processes", ps_config);
2356 plugin_register_init("processes", ps_init);
2357 plugin_register_read("processes", ps_read);
2358 } /* void module_register */