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 ssnprintf(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 ssnprintf(filename, sizeof(filename), "/proc/%li/task/%s/status", ps->id,
825 if ((fh = fopen(filename, "r")) == NULL) {
826 DEBUG("Failed to open file `%s'", filename);
830 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
834 if (strncmp(buffer, "voluntary_ctxt_switches", 23) != 0 &&
835 strncmp(buffer, "nonvoluntary_ctxt_switches", 26) != 0)
838 numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
845 tmp = (derive_t)strtoll(fields[1], &endptr, /* base = */ 10);
846 if ((errno == 0) && (endptr != fields[1])) {
847 if (strncmp(buffer, "voluntary_ctxt_switches", 23) == 0) {
849 } else if (strncmp(buffer, "nonvoluntary_ctxt_switches", 26) == 0) {
850 cswitch_invol += tmp;
853 } /* while (fgets) */
857 WARNING("processes: fclose: %s",
858 sstrerror(errno, errbuf, sizeof(errbuf)));
863 ps->cswitch_vol = cswitch_vol;
864 ps->cswitch_invol = cswitch_invol;
867 } /* int *ps_read_tasks_status */
869 /* Read data from /proc/pid/status */
870 static int ps_read_status(long pid, process_entry_t *ps) {
874 unsigned long lib = 0;
875 unsigned long exe = 0;
876 unsigned long data = 0;
877 unsigned long threads = 0;
881 ssnprintf(filename, sizeof(filename), "/proc/%li/status", pid);
882 if ((fh = fopen(filename, "r")) == NULL)
885 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
889 if (strncmp(buffer, "Vm", 2) != 0 && strncmp(buffer, "Threads", 7) != 0)
892 numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
899 tmp = strtoul(fields[1], &endptr, /* base = */ 10);
900 if ((errno == 0) && (endptr != fields[1])) {
901 if (strncmp(buffer, "VmData", 6) == 0) {
903 } else if (strncmp(buffer, "VmLib", 5) == 0) {
905 } else if (strncmp(buffer, "VmExe", 5) == 0) {
907 } else if (strncmp(buffer, "Threads", 7) == 0) {
911 } /* while (fgets) */
915 WARNING("processes: fclose: %s", sstrerror(errno, errbuf, sizeof(errbuf)));
918 ps->vmem_data = data * 1024;
919 ps->vmem_code = (exe + lib) * 1024;
921 ps->num_lwp = threads;
924 } /* int *ps_read_status */
926 static int ps_read_io(process_entry_t *ps) {
934 ssnprintf(filename, sizeof(filename), "/proc/%li/io", ps->id);
935 if ((fh = fopen(filename, "r")) == NULL) {
936 DEBUG("ps_read_io: Failed to open file `%s'", filename);
940 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
941 derive_t *val = NULL;
945 if (strncasecmp(buffer, "rchar:", 6) == 0)
946 val = &(ps->io_rchar);
947 else if (strncasecmp(buffer, "wchar:", 6) == 0)
948 val = &(ps->io_wchar);
949 else if (strncasecmp(buffer, "syscr:", 6) == 0)
950 val = &(ps->io_syscr);
951 else if (strncasecmp(buffer, "syscw:", 6) == 0)
952 val = &(ps->io_syscw);
956 numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
963 tmp = strtoll(fields[1], &endptr, /* base = */ 10);
964 if ((errno != 0) || (endptr == fields[1]))
967 *val = (derive_t)tmp;
968 } /* while (fgets) */
972 WARNING("processes: fclose: %s", sstrerror(errno, errbuf, sizeof(errbuf)));
975 } /* int ps_read_io (...) */
977 static int ps_count_fd(int pid) {
983 ssnprintf(dirname, sizeof(dirname), "/proc/%i/fd", pid);
985 if ((dh = opendir(dirname)) == NULL) {
986 DEBUG("Failed to open directory `%s'", dirname);
989 while ((ent = readdir(dh)) != NULL) {
990 if (!isdigit((int)ent->d_name[0]))
997 return (count >= 1) ? count : 1;
998 } /* int ps_count_fd (pid) */
1000 static void ps_fill_details(const procstat_t *ps, process_entry_t *entry) {
1001 if (entry->has_io == 0) {
1006 if (ps->report_ctx_switch) {
1007 if (entry->has_cswitch == 0) {
1008 ps_read_tasks_status(entry);
1009 entry->has_cswitch = 1;
1013 if (ps->report_fd_num) {
1015 if (entry->has_fd == 0 && (num_fd = ps_count_fd(entry->id)) > 0) {
1016 entry->num_fd = num_fd;
1020 } /* void ps_fill_details (...) */
1022 static int ps_read_process(long pid, process_entry_t *ps, char *state) {
1032 size_t name_start_pos;
1033 size_t name_end_pos;
1036 derive_t cpu_user_counter;
1037 derive_t cpu_system_counter;
1038 long long unsigned vmem_size;
1039 long long unsigned vmem_rss;
1040 long long unsigned stack_size;
1044 ssnprintf(filename, sizeof(filename), "/proc/%li/stat", pid);
1046 status = read_file_contents(filename, buffer, sizeof(buffer) - 1);
1049 buffer_len = (size_t)status;
1050 buffer[buffer_len] = 0;
1052 /* The name of the process is enclosed in parens. Since the name can
1053 * contain parens itself, spaces, numbers and pretty much everything
1054 * else, use these to determine the process name. We don't use
1055 * strchr(3) and strrchr(3) to avoid pointer arithmetic which would
1056 * otherwise be required to determine name_len. */
1058 while (name_start_pos < buffer_len && buffer[name_start_pos] != '(')
1061 name_end_pos = buffer_len;
1062 while (name_end_pos > 0 && buffer[name_end_pos] != ')')
1065 /* Either '(' or ')' is not found or they are in the wrong order.
1066 * Anyway, something weird that shouldn't happen ever. */
1067 if (name_start_pos >= name_end_pos) {
1068 ERROR("processes plugin: name_start_pos = %zu >= name_end_pos = %zu",
1069 name_start_pos, name_end_pos);
1073 name_len = (name_end_pos - name_start_pos) - 1;
1074 if (name_len >= sizeof(ps->name))
1075 name_len = sizeof(ps->name) - 1;
1077 sstrncpy(ps->name, &buffer[name_start_pos + 1], name_len + 1);
1079 if ((buffer_len - name_end_pos) < 2)
1081 buffer_ptr = &buffer[name_end_pos + 2];
1083 fields_len = strsplit(buffer_ptr, fields, STATIC_ARRAY_SIZE(fields));
1084 if (fields_len < 22) {
1085 DEBUG("processes plugin: ps_read_process (pid = %li):"
1086 " `%s' has only %i fields..",
1087 pid, filename, fields_len);
1091 *state = fields[0][0];
1093 if (*state == 'Z') {
1097 ps->num_lwp = strtoul(fields[17], /* endptr = */ NULL, /* base = */ 10);
1098 if ((ps_read_status(pid, ps)) != 0) {
1102 DEBUG("ps_read_process: did not get vmem data for pid %li", pid);
1104 if (ps->num_lwp == 0)
1109 /* Leave the rest at zero if this is only a zombi */
1110 if (ps->num_proc == 0) {
1111 DEBUG("processes plugin: This is only a zombie: pid = %li; "
1117 cpu_user_counter = atoll(fields[11]);
1118 cpu_system_counter = atoll(fields[12]);
1119 vmem_size = atoll(fields[20]);
1120 vmem_rss = atoll(fields[21]);
1121 ps->vmem_minflt_counter = atol(fields[7]);
1122 ps->vmem_majflt_counter = atol(fields[9]);
1125 unsigned long long stack_start = atoll(fields[25]);
1126 unsigned long long stack_ptr = atoll(fields[26]);
1128 stack_size = (stack_start > stack_ptr) ? stack_start - stack_ptr
1129 : stack_ptr - stack_start;
1132 /* Convert jiffies to useconds */
1133 cpu_user_counter = cpu_user_counter * 1000000 / CONFIG_HZ;
1134 cpu_system_counter = cpu_system_counter * 1000000 / CONFIG_HZ;
1135 vmem_rss = vmem_rss * pagesize_g;
1137 ps->cpu_user_counter = cpu_user_counter;
1138 ps->cpu_system_counter = cpu_system_counter;
1139 ps->vmem_size = (unsigned long)vmem_size;
1140 ps->vmem_rss = (unsigned long)vmem_rss;
1141 ps->stack_size = (unsigned long)stack_size;
1143 /* no data by default. May be filled by ps_fill_details () */
1149 ps->cswitch_vol = -1;
1150 ps->cswitch_invol = -1;
1154 } /* int ps_read_process (...) */
1156 static char *ps_get_cmdline(long pid, char *name, char *buf, size_t buf_len) {
1160 char file[PATH_MAX];
1165 if ((pid < 1) || (NULL == buf) || (buf_len < 2))
1168 ssnprintf(file, sizeof(file), "/proc/%li/cmdline", pid);
1171 fd = open(file, O_RDONLY);
1174 /* ENOENT means the process exited while we were handling it.
1175 * Don't complain about this, it only fills the logs. */
1176 if (errno != ENOENT)
1177 WARNING("processes plugin: Failed to open `%s': %s.", file,
1178 sstrerror(errno, errbuf, sizeof(errbuf)));
1190 status = read(fd, (void *)buf_ptr, len);
1195 if ((EAGAIN == errno) || (EINTR == errno))
1198 WARNING("processes plugin: Failed to read from `%s': %s.", file,
1199 sstrerror(errno, errbuf, sizeof(errbuf)));
1219 /* cmdline not available; e.g. kernel thread, zombie */
1223 ssnprintf(buf, buf_len, "[%s]", name);
1227 assert(n <= buf_len);
1234 /* remove trailing whitespace */
1235 while ((n > 0) && (isspace(buf[n]) || ('\0' == buf[n]))) {
1240 /* arguments are separated by '\0' in /proc/<pid>/cmdline */
1247 } /* char *ps_get_cmdline (...) */
1249 static int read_fork_rate(void) {
1253 _Bool value_valid = 0;
1255 proc_stat = fopen("/proc/stat", "r");
1256 if (proc_stat == NULL) {
1258 ERROR("processes plugin: fopen (/proc/stat) failed: %s",
1259 sstrerror(errno, errbuf, sizeof(errbuf)));
1263 while (fgets(buffer, sizeof(buffer), proc_stat) != NULL) {
1268 fields_num = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
1269 if (fields_num != 2)
1272 if (strcmp("processes", fields[0]) != 0)
1275 status = parse_value(fields[1], &value, DS_TYPE_DERIVE);
1286 ps_submit_fork_rate(value.derive);
1289 #endif /*KERNEL_LINUX */
1292 static char *ps_get_cmdline(long pid,
1293 char *name __attribute__((unused)), /* {{{ */
1294 char *buffer, size_t buffer_size) {
1295 char path[PATH_MAX];
1299 snprintf(path, sizeof(path), "/proc/%li/psinfo", pid);
1301 status = read_file_contents(path, (void *)&info, sizeof(info));
1302 if ((status < 0) || (((size_t)status) != sizeof(info))) {
1303 ERROR("processes plugin: Unexpected return value "
1304 "while reading \"%s\": "
1305 "Returned %zd but expected %zu.",
1306 path, status, buffer_size);
1310 info.pr_psargs[sizeof(info.pr_psargs) - 1] = 0;
1311 sstrncpy(buffer, info.pr_psargs, buffer_size);
1314 } /* }}} int ps_get_cmdline */
1317 * Reads process information on the Solaris OS. The information comes mainly
1319 * /proc/PID/status, /proc/PID/psinfo and /proc/PID/usage
1320 * The values for input and ouput chars are calculated "by hand"
1321 * Added a few "solaris" specific process states as well
1323 static int ps_read_process(long pid, process_entry_t *ps, char *state) {
1325 char f_psinfo[64], f_usage[64];
1328 pstatus_t *myStatus;
1332 snprintf(filename, sizeof(filename), "/proc/%li/status", pid);
1333 snprintf(f_psinfo, sizeof(f_psinfo), "/proc/%li/psinfo", pid);
1334 snprintf(f_usage, sizeof(f_usage), "/proc/%li/usage", pid);
1336 buffer = calloc(1, sizeof(pstatus_t));
1337 read_file_contents(filename, buffer, sizeof(pstatus_t));
1338 myStatus = (pstatus_t *)buffer;
1340 buffer = calloc(1, sizeof(psinfo_t));
1341 read_file_contents(f_psinfo, buffer, sizeof(psinfo_t));
1342 myInfo = (psinfo_t *)buffer;
1344 buffer = calloc(1, sizeof(prusage_t));
1345 read_file_contents(f_usage, buffer, sizeof(prusage_t));
1346 myUsage = (prusage_t *)buffer;
1348 sstrncpy(ps->name, myInfo->pr_fname, sizeof(myInfo->pr_fname));
1349 ps->num_lwp = myStatus->pr_nlwp;
1350 if (myInfo->pr_wstat != 0) {
1361 ps->num_lwp = myInfo->pr_nlwp;
1365 * Convert system time and user time from nanoseconds to microseconds
1366 * for compatibility with the linux module
1368 ps->cpu_system_counter = myStatus->pr_stime.tv_nsec / 1000;
1369 ps->cpu_user_counter = myStatus->pr_utime.tv_nsec / 1000;
1372 * Convert rssize from KB to bytes to be consistent w/ the linux module
1374 ps->vmem_rss = myInfo->pr_rssize * 1024;
1375 ps->vmem_size = myInfo->pr_size * 1024;
1376 ps->vmem_minflt_counter = myUsage->pr_minf;
1377 ps->vmem_majflt_counter = myUsage->pr_majf;
1380 * TODO: Data and code segment calculations for Solaris
1385 ps->stack_size = myStatus->pr_stksize;
1388 * TODO: File descriptor count for Solaris
1393 * Calculating input/ouput chars
1394 * Formula used is total chars / total blocks => chars/block
1395 * then convert input/output blocks to chars
1397 ulong_t tot_chars = myUsage->pr_ioch;
1398 ulong_t tot_blocks = myUsage->pr_inblk + myUsage->pr_oublk;
1399 ulong_t chars_per_block = 1;
1400 if (tot_blocks != 0)
1401 chars_per_block = tot_chars / tot_blocks;
1402 ps->io_rchar = myUsage->pr_inblk * chars_per_block;
1403 ps->io_wchar = myUsage->pr_oublk * chars_per_block;
1404 ps->io_syscr = myUsage->pr_sysc;
1405 ps->io_syscw = myUsage->pr_sysc;
1408 * TODO: context switch counters for Solaris
1410 ps->cswitch_vol = -1;
1411 ps->cswitch_invol = -1;
1414 * TODO: Find way of setting BLOCKED and PAGING status
1418 if (myStatus->pr_flags & PR_ASLEEP)
1420 else if (myStatus->pr_flags & PR_STOPPED)
1422 else if (myStatus->pr_flags & PR_DETACH)
1424 else if (myStatus->pr_flags & PR_DAEMON)
1426 else if (myStatus->pr_flags & PR_ISSYS)
1428 else if (myStatus->pr_flags & PR_ORPHAN)
1439 * Reads the number of threads created since the last reboot. On Solaris these
1440 * are retrieved from kstat (module cpu, name sys, class misc, stat nthreads).
1441 * The result is the sum for all the threads created on each cpu
1443 static int read_fork_rate(void) {
1444 extern kstat_ctl_t *kc;
1445 derive_t result = 0;
1450 for (kstat_t *ksp_chain = kc->kc_chain; ksp_chain != NULL;
1451 ksp_chain = ksp_chain->ks_next) {
1452 if ((strcmp(ksp_chain->ks_module, "cpu") == 0) &&
1453 (strcmp(ksp_chain->ks_name, "sys") == 0) &&
1454 (strcmp(ksp_chain->ks_class, "misc") == 0)) {
1457 kstat_read(kc, ksp_chain, NULL);
1459 tmp = get_kstat_value(ksp_chain, "nthreads");
1465 ps_submit_fork_rate(result);
1468 #endif /* KERNEL_SOLARIS */
1470 #if HAVE_THREAD_INFO
1471 static int mach_get_task_name(task_t t, int *pid, char *name,
1472 size_t name_max_len) {
1475 struct kinfo_proc kp;
1480 mib[2] = KERN_PROC_PID;
1482 if (pid_for_task(t, pid) != KERN_SUCCESS)
1486 kp_size = sizeof(kp);
1487 if (sysctl(mib, 4, &kp, &kp_size, NULL, 0) != 0)
1490 if (name_max_len > (MAXCOMLEN + 1))
1491 name_max_len = MAXCOMLEN + 1;
1493 strncpy(name, kp.kp_proc.p_comm, name_max_len - 1);
1494 name[name_max_len - 1] = '\0';
1496 DEBUG("pid = %i; name = %s;", *pid, name);
1498 /* We don't do the special handling for `p_comm == "LaunchCFMApp"' as
1499 * `top' does it, because it is a lot of work and only used when
1500 * debugging. -octo */
1504 #endif /* HAVE_THREAD_INFO */
1505 /* ------- end of additional functions for KERNEL_LINUX/HAVE_THREAD_INFO -------
1508 /* do actual readings from kernel */
1509 static int ps_read(void) {
1510 #if HAVE_THREAD_INFO
1511 kern_return_t status;
1513 processor_set_t port_pset_priv;
1515 task_array_t task_list;
1516 mach_msg_type_number_t task_list_len;
1519 char task_name[MAXCOMLEN + 1];
1521 thread_act_array_t thread_list;
1522 mach_msg_type_number_t thread_list_len;
1523 thread_basic_info_data_t thread_data;
1524 mach_msg_type_number_t thread_data_len;
1533 process_entry_t pse;
1538 * The Mach-concept is a little different from the traditional UNIX
1539 * concept: All the work is done in threads. Threads are contained in
1540 * `tasks'. Therefore, `task status' doesn't make much sense, since
1541 * it's actually a `thread status'.
1542 * Tasks are assigned to sets of processors, so that's where you go to
1545 for (mach_msg_type_number_t pset = 0; pset < pset_list_len; pset++) {
1546 if ((status = host_processor_set_priv(port_host_self, pset_list[pset],
1547 &port_pset_priv)) != KERN_SUCCESS) {
1548 ERROR("host_processor_set_priv failed: %s\n", mach_error_string(status));
1552 if ((status = processor_set_tasks(port_pset_priv, &task_list,
1553 &task_list_len)) != KERN_SUCCESS) {
1554 ERROR("processor_set_tasks failed: %s\n", mach_error_string(status));
1555 mach_port_deallocate(port_task_self, port_pset_priv);
1559 for (mach_msg_type_number_t task = 0; task < task_list_len; task++) {
1561 if (mach_get_task_name(task_list[task], &task_pid, task_name,
1562 PROCSTAT_NAME_LEN) == 0) {
1563 /* search for at least one match */
1564 for (ps = list_head_g; ps != NULL; ps = ps->next)
1565 /* FIXME: cmdline should be here instead of NULL */
1566 if (ps_list_match(task_name, NULL, ps) == 1)
1570 /* Collect more detailed statistics for this process */
1572 task_basic_info_data_t task_basic_info;
1573 mach_msg_type_number_t task_basic_info_len;
1574 task_events_info_data_t task_events_info;
1575 mach_msg_type_number_t task_events_info_len;
1576 task_absolutetime_info_data_t task_absolutetime_info;
1577 mach_msg_type_number_t task_absolutetime_info_len;
1579 memset(&pse, '\0', sizeof(pse));
1582 task_basic_info_len = TASK_BASIC_INFO_COUNT;
1583 status = task_info(task_list[task], TASK_BASIC_INFO,
1584 (task_info_t)&task_basic_info, &task_basic_info_len);
1585 if (status != KERN_SUCCESS) {
1586 ERROR("task_info failed: %s", mach_error_string(status));
1587 continue; /* with next thread_list */
1590 task_events_info_len = TASK_EVENTS_INFO_COUNT;
1592 task_info(task_list[task], TASK_EVENTS_INFO,
1593 (task_info_t)&task_events_info, &task_events_info_len);
1594 if (status != KERN_SUCCESS) {
1595 ERROR("task_info failed: %s", mach_error_string(status));
1596 continue; /* with next thread_list */
1599 task_absolutetime_info_len = TASK_ABSOLUTETIME_INFO_COUNT;
1600 status = task_info(task_list[task], TASK_ABSOLUTETIME_INFO,
1601 (task_info_t)&task_absolutetime_info,
1602 &task_absolutetime_info_len);
1603 if (status != KERN_SUCCESS) {
1604 ERROR("task_info failed: %s", mach_error_string(status));
1605 continue; /* with next thread_list */
1609 pse.vmem_size = task_basic_info.virtual_size;
1610 pse.vmem_rss = task_basic_info.resident_size;
1611 /* Does not seem to be easily exposed */
1620 /* File descriptor count not implemented */
1623 pse.vmem_minflt_counter = task_events_info.cow_faults;
1624 pse.vmem_majflt_counter = task_events_info.faults;
1626 pse.cpu_user_counter = task_absolutetime_info.total_user;
1627 pse.cpu_system_counter = task_absolutetime_info.total_system;
1629 /* context switch counters not implemented */
1630 pse.cswitch_vol = -1;
1631 pse.cswitch_invol = -1;
1634 status = task_threads(task_list[task], &thread_list, &thread_list_len);
1635 if (status != KERN_SUCCESS) {
1636 /* Apple's `top' treats this case a zombie. It
1637 * makes sense to some extend: A `zombie'
1638 * thread is nonsense, since the task/process
1641 DEBUG("task_threads failed: %s", mach_error_string(status));
1642 if (task_list[task] != port_task_self)
1643 mach_port_deallocate(port_task_self, task_list[task]);
1644 continue; /* with next task_list */
1647 for (mach_msg_type_number_t thread = 0; thread < thread_list_len;
1649 thread_data_len = THREAD_BASIC_INFO_COUNT;
1650 status = thread_info(thread_list[thread], THREAD_BASIC_INFO,
1651 (thread_info_t)&thread_data, &thread_data_len);
1652 if (status != KERN_SUCCESS) {
1653 ERROR("thread_info failed: %s", mach_error_string(status));
1654 if (task_list[task] != port_task_self)
1655 mach_port_deallocate(port_task_self, thread_list[thread]);
1656 continue; /* with next thread_list */
1662 switch (thread_data.run_state) {
1663 case TH_STATE_RUNNING:
1666 case TH_STATE_STOPPED:
1667 /* What exactly is `halted'? */
1668 case TH_STATE_HALTED:
1671 case TH_STATE_WAITING:
1674 case TH_STATE_UNINTERRUPTIBLE:
1677 /* There is no `zombie' case here,
1678 * since there are no zombie-threads.
1679 * There's only zombie tasks, which are
1682 WARNING("Unknown thread status: %i", thread_data.run_state);
1684 } /* switch (thread_data.run_state) */
1686 if (task_list[task] != port_task_self) {
1687 status = mach_port_deallocate(port_task_self, thread_list[thread]);
1688 if (status != KERN_SUCCESS)
1689 ERROR("mach_port_deallocate failed: %s", mach_error_string(status));
1691 } /* for (thread_list) */
1693 if ((status = vm_deallocate(port_task_self, (vm_address_t)thread_list,
1694 thread_list_len * sizeof(thread_act_t))) !=
1696 ERROR("vm_deallocate failed: %s", mach_error_string(status));
1699 thread_list_len = 0;
1701 /* Only deallocate the task port, if it isn't our own.
1702 * Don't know what would happen in that case, but this
1703 * is what Apple's top does.. ;) */
1704 if (task_list[task] != port_task_self) {
1705 status = mach_port_deallocate(port_task_self, task_list[task]);
1706 if (status != KERN_SUCCESS)
1707 ERROR("mach_port_deallocate failed: %s", mach_error_string(status));
1711 /* FIXME: cmdline should be here instead of NULL */
1712 ps_list_add(task_name, NULL, &pse);
1713 } /* for (task_list) */
1715 if ((status = vm_deallocate(port_task_self, (vm_address_t)task_list,
1716 task_list_len * sizeof(task_t))) !=
1718 ERROR("vm_deallocate failed: %s", mach_error_string(status));
1723 if ((status = mach_port_deallocate(port_task_self, port_pset_priv)) !=
1725 ERROR("mach_port_deallocate failed: %s", mach_error_string(status));
1727 } /* for (pset_list) */
1729 ps_submit_state("running", running);
1730 ps_submit_state("sleeping", sleeping);
1731 ps_submit_state("zombies", zombies);
1732 ps_submit_state("stopped", stopped);
1733 ps_submit_state("blocked", blocked);
1735 for (ps = list_head_g; ps != NULL; ps = ps->next)
1736 ps_submit_proc_list(ps);
1737 /* #endif HAVE_THREAD_INFO */
1751 char cmdline[CMDLINE_BUFFER_SIZE];
1754 process_entry_t pse;
1757 running = sleeping = zombies = stopped = paging = blocked = 0;
1760 if ((proc = opendir("/proc")) == NULL) {
1762 ERROR("Cannot open `/proc': %s", sstrerror(errno, errbuf, sizeof(errbuf)));
1766 while ((ent = readdir(proc)) != NULL) {
1767 if (!isdigit(ent->d_name[0]))
1770 if ((pid = atol(ent->d_name)) < 1)
1773 memset(&pse, 0, sizeof(pse));
1776 status = ps_read_process(pid, &pse, &state);
1778 DEBUG("ps_read_process failed: %i", status);
1803 ps_list_add(pse.name,
1804 ps_get_cmdline(pid, pse.name, cmdline, sizeof(cmdline)), &pse);
1809 ps_submit_state("running", running);
1810 ps_submit_state("sleeping", sleeping);
1811 ps_submit_state("zombies", zombies);
1812 ps_submit_state("stopped", stopped);
1813 ps_submit_state("paging", paging);
1814 ps_submit_state("blocked", blocked);
1816 for (procstat_t *ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1817 ps_submit_proc_list(ps_ptr);
1820 /* #endif KERNEL_LINUX */
1822 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD
1832 char errbuf[_POSIX2_LINE_MAX];
1833 struct kinfo_proc *procs; /* array of processes */
1834 struct kinfo_proc *proc_ptr = NULL;
1835 int count; /* returns number of processes */
1837 process_entry_t pse;
1841 /* Open the kvm interface, get a descriptor */
1842 kd = kvm_openfiles(NULL, "/dev/null", NULL, 0, errbuf);
1844 ERROR("processes plugin: Cannot open kvm interface: %s", errbuf);
1848 /* Get the list of processes. */
1849 procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, &count);
1850 if (procs == NULL) {
1851 ERROR("processes plugin: Cannot get kvm processes list: %s",
1857 /* Iterate through the processes in kinfo_proc */
1858 for (int i = 0; i < count; i++) {
1859 /* Create only one process list entry per _process_, i.e.
1860 * filter out threads (duplicate PID entries). */
1861 if ((proc_ptr == NULL) || (proc_ptr->ki_pid != procs[i].ki_pid)) {
1862 char cmdline[CMDLINE_BUFFER_SIZE] = "";
1863 _Bool have_cmdline = 0;
1865 proc_ptr = &(procs[i]);
1866 /* Don't probe system processes and processes without arguments */
1867 if (((procs[i].ki_flag & P_SYSTEM) == 0) && (procs[i].ki_args != NULL)) {
1872 /* retrieve the arguments */
1873 argv = kvm_getargv(kd, proc_ptr, /* nchr = */ 0);
1875 if ((argv != NULL) && (argv[0] != NULL)) {
1876 while (argv[argc] != NULL)
1879 status = strjoin(cmdline, sizeof(cmdline), argv, argc, " ");
1881 WARNING("processes plugin: Command line did not fit into buffer.");
1885 } /* if (process has argument list) */
1887 memset(&pse, 0, sizeof(pse));
1888 pse.id = procs[i].ki_pid;
1891 pse.num_lwp = procs[i].ki_numthreads;
1893 pse.vmem_size = procs[i].ki_size;
1894 pse.vmem_rss = procs[i].ki_rssize * pagesize;
1895 pse.vmem_data = procs[i].ki_dsize * pagesize;
1896 pse.vmem_code = procs[i].ki_tsize * pagesize;
1897 pse.stack_size = procs[i].ki_ssize * pagesize;
1898 pse.vmem_minflt_counter = procs[i].ki_rusage.ru_minflt;
1899 pse.vmem_majflt_counter = procs[i].ki_rusage.ru_majflt;
1901 pse.cpu_user_counter = 0;
1902 pse.cpu_system_counter = 0;
1904 * The u-area might be swapped out, and we can't get
1905 * at it because we have a crashdump and no swap.
1906 * If it's here fill in these fields, otherwise, just
1909 if (procs[i].ki_flag & P_INMEM) {
1910 pse.cpu_user_counter = procs[i].ki_rusage.ru_utime.tv_usec +
1911 (1000000lu * procs[i].ki_rusage.ru_utime.tv_sec);
1912 pse.cpu_system_counter =
1913 procs[i].ki_rusage.ru_stime.tv_usec +
1914 (1000000lu * procs[i].ki_rusage.ru_stime.tv_sec);
1923 /* file descriptor count not implemented */
1926 /* context switch counters not implemented */
1927 pse.cswitch_vol = -1;
1928 pse.cswitch_invol = -1;
1930 ps_list_add(procs[i].ki_comm, have_cmdline ? cmdline : NULL, &pse);
1932 switch (procs[i].ki_stat) {
1955 } /* if ((proc_ptr == NULL) || (proc_ptr->ki_pid != procs[i].ki_pid)) */
1960 ps_submit_state("running", running);
1961 ps_submit_state("sleeping", sleeping);
1962 ps_submit_state("zombies", zombies);
1963 ps_submit_state("stopped", stopped);
1964 ps_submit_state("blocked", blocked);
1965 ps_submit_state("idle", idle);
1966 ps_submit_state("wait", wait);
1968 for (procstat_t *ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1969 ps_submit_proc_list(ps_ptr);
1970 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
1972 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_OPENBSD
1983 struct kinfo_proc *procs; /* array of processes */
1984 struct kinfo_proc *proc_ptr = NULL;
1985 int count; /* returns number of processes */
1987 process_entry_t pse;
1991 /* Open the kvm interface, get a descriptor */
1992 kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf);
1994 ERROR("processes plugin: Cannot open kvm interface: %s", errbuf);
1998 /* Get the list of processes. */
1999 procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc), &count);
2000 if (procs == NULL) {
2001 ERROR("processes plugin: Cannot get kvm processes list: %s",
2007 /* Iterate through the processes in kinfo_proc */
2008 for (int i = 0; i < count; i++) {
2009 /* Create only one process list entry per _process_, i.e.
2010 * filter out threads (duplicate PID entries). */
2011 if ((proc_ptr == NULL) || (proc_ptr->p_pid != procs[i].p_pid)) {
2012 char cmdline[CMDLINE_BUFFER_SIZE] = "";
2013 _Bool have_cmdline = 0;
2015 proc_ptr = &(procs[i]);
2016 /* Don't probe zombie processes */
2017 if (!P_ZOMBIE(proc_ptr)) {
2022 /* retrieve the arguments */
2023 argv = kvm_getargv(kd, proc_ptr, /* nchr = */ 0);
2025 if ((argv != NULL) && (argv[0] != NULL)) {
2026 while (argv[argc] != NULL)
2029 status = strjoin(cmdline, sizeof(cmdline), argv, argc, " ");
2031 WARNING("processes plugin: Command line did not fit into buffer.");
2035 } /* if (process has argument list) */
2037 memset(&pse, 0, sizeof(pse));
2038 pse.id = procs[i].p_pid;
2041 pse.num_lwp = 1; /* XXX: accumulate p_tid values for a single p_pid ? */
2043 pse.vmem_rss = procs[i].p_vm_rssize * pagesize;
2044 pse.vmem_data = procs[i].p_vm_dsize * pagesize;
2045 pse.vmem_code = procs[i].p_vm_tsize * pagesize;
2046 pse.stack_size = procs[i].p_vm_ssize * pagesize;
2047 pse.vmem_size = pse.stack_size + pse.vmem_code + pse.vmem_data;
2048 pse.vmem_minflt_counter = procs[i].p_uru_minflt;
2049 pse.vmem_majflt_counter = procs[i].p_uru_majflt;
2051 pse.cpu_user_counter =
2052 procs[i].p_uutime_usec + (1000000lu * procs[i].p_uutime_sec);
2053 pse.cpu_system_counter =
2054 procs[i].p_ustime_usec + (1000000lu * procs[i].p_ustime_sec);
2062 /* file descriptor count not implemented */
2065 /* context switch counters not implemented */
2066 pse.cswitch_vol = -1;
2067 pse.cswitch_invol = -1;
2069 ps_list_add(procs[i].p_comm, have_cmdline ? cmdline : NULL, &pse);
2071 switch (procs[i].p_stat) {
2094 } /* if ((proc_ptr == NULL) || (proc_ptr->p_pid != procs[i].p_pid)) */
2099 ps_submit_state("running", running);
2100 ps_submit_state("sleeping", sleeping);
2101 ps_submit_state("zombies", zombies);
2102 ps_submit_state("stopped", stopped);
2103 ps_submit_state("onproc", onproc);
2104 ps_submit_state("idle", idle);
2105 ps_submit_state("dead", dead);
2107 for (procstat_t *ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
2108 ps_submit_proc_list(ps_ptr);
2109 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_OPENBSD */
2111 #elif HAVE_PROCINFO_H
2123 process_entry_t pse;
2126 while ((nprocs = getprocs64(procentry, sizeof(struct procentry64),
2127 /* fdsinfo = */ NULL, sizeof(struct fdsinfo64),
2128 &pindex, MAXPROCENTRY)) > 0) {
2129 for (int i = 0; i < nprocs; i++) {
2132 char arglist[MAXARGLN + 1];
2136 if (procentry[i].pi_state == SNONE)
2138 /* if (procentry[i].pi_state == SZOMB) FIXME */
2140 cmdline = procentry[i].pi_comm;
2141 cargs = procentry[i].pi_comm;
2142 if (procentry[i].pi_flags & SKPROC) {
2143 if (procentry[i].pi_pid == 0)
2144 cmdline = "swapper";
2147 if (getargs(&procentry[i], sizeof(struct procentry64), arglist,
2152 while (++n < MAXARGLN) {
2153 if (arglist[n] == '\0') {
2154 if (arglist[n + 1] == '\0')
2163 memset(&pse, 0, sizeof(pse));
2165 pse.id = procentry[i].pi_pid;
2166 pse.num_lwp = procentry[i].pi_thcount;
2170 while ((nthreads = getthrds64(procentry[i].pi_pid, thrdentry,
2171 sizeof(struct thrdentry64), &thindex,
2172 MAXTHRDENTRY)) > 0) {
2175 for (j = 0; j < nthreads; j++) {
2176 switch (thrdentry[j].ti_state) {
2177 /* case TSNONE: break; */
2180 break; /* FIXME is really blocked */
2198 if (nthreads < MAXTHRDENTRY)
2202 /* tv_usec is nanosec ??? */
2203 pse.cpu_user_counter = procentry[i].pi_ru.ru_utime.tv_sec * 1000000 +
2204 procentry[i].pi_ru.ru_utime.tv_usec / 1000;
2206 /* tv_usec is nanosec ??? */
2207 pse.cpu_system_counter = procentry[i].pi_ru.ru_stime.tv_sec * 1000000 +
2208 procentry[i].pi_ru.ru_stime.tv_usec / 1000;
2210 pse.vmem_minflt_counter = procentry[i].pi_minflt;
2211 pse.vmem_majflt_counter = procentry[i].pi_majflt;
2213 pse.vmem_size = procentry[i].pi_tsize + procentry[i].pi_dvm * pagesize;
2214 pse.vmem_rss = (procentry[i].pi_drss + procentry[i].pi_trss) * pagesize;
2215 /* Not supported/implemented */
2227 pse.cswitch_vol = -1;
2228 pse.cswitch_invol = -1;
2230 ps_list_add(cmdline, cargs, &pse);
2231 } /* for (i = 0 .. nprocs) */
2233 if (nprocs < MAXPROCENTRY)
2235 } /* while (getprocs64() > 0) */
2236 ps_submit_state("running", running);
2237 ps_submit_state("sleeping", sleeping);
2238 ps_submit_state("zombies", zombies);
2239 ps_submit_state("stopped", stopped);
2240 ps_submit_state("paging", paging);
2241 ps_submit_state("blocked", blocked);
2243 for (procstat_t *ps = list_head_g; ps != NULL; ps = ps->next)
2244 ps_submit_proc_list(ps);
2245 /* #endif HAVE_PROCINFO_H */
2247 #elif KERNEL_SOLARIS
2249 * The Solaris section adds a few more process states and removes some
2250 * process states compared to linux. Most notably there is no "PAGING"
2251 * and "BLOCKED" state for a process. The rest is similar to the linux
2269 char cmdline[PRARGSZ];
2273 proc = opendir("/proc");
2277 while ((ent = readdir(proc)) != NULL) {
2279 process_entry_t pse;
2282 if (!isdigit((int)ent->d_name[0]))
2285 pid = strtol(ent->d_name, &endptr, 10);
2286 if (*endptr != 0) /* value didn't completely parse as a number */
2289 memset(&pse, 0, sizeof(pse));
2292 status = ps_read_process(pid, &pse, &state);
2294 DEBUG("ps_read_process failed: %i", status);
2325 ps_list_add(pse.name,
2326 ps_get_cmdline(pid, pse.name, cmdline, sizeof(cmdline)), &pse);
2327 } /* while(readdir) */
2330 ps_submit_state("running", running);
2331 ps_submit_state("sleeping", sleeping);
2332 ps_submit_state("zombies", zombies);
2333 ps_submit_state("stopped", stopped);
2334 ps_submit_state("detached", detached);
2335 ps_submit_state("daemon", daemon);
2336 ps_submit_state("system", system);
2337 ps_submit_state("orphan", orphan);
2339 for (procstat_t *ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
2340 ps_submit_proc_list(ps_ptr);
2343 #endif /* KERNEL_SOLARIS */
2350 void module_register(void) {
2351 plugin_register_complex_config("processes", ps_config);
2352 plugin_register_init("processes", ps_init);
2353 plugin_register_read("processes", ps_read);
2354 } /* void module_register */