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>
42 /* Include header files for the mach system, if they exist.. */
44 #if HAVE_MACH_MACH_INIT_H
45 #include <mach/mach_init.h>
47 #if HAVE_MACH_HOST_PRIV_H
48 #include <mach/host_priv.h>
50 #if HAVE_MACH_MACH_ERROR_H
51 #include <mach/mach_error.h>
53 #if HAVE_MACH_MACH_HOST_H
54 #include <mach/mach_host.h>
56 #if HAVE_MACH_MACH_PORT_H
57 #include <mach/mach_port.h>
59 #if HAVE_MACH_MACH_TYPES_H
60 #include <mach/mach_types.h>
62 #if HAVE_MACH_MESSAGE_H
63 #include <mach/message.h>
65 #if HAVE_MACH_PROCESSOR_SET_H
66 #include <mach/processor_set.h>
69 #include <mach/task.h>
71 #if HAVE_MACH_THREAD_ACT_H
72 #include <mach/thread_act.h>
74 #if HAVE_MACH_VM_REGION_H
75 #include <mach/vm_region.h>
77 #if HAVE_MACH_VM_MAP_H
78 #include <mach/vm_map.h>
80 #if HAVE_MACH_VM_PROT_H
81 #include <mach/vm_prot.h>
84 #include <sys/sysctl.h>
86 /* #endif HAVE_THREAD_INFO */
89 #if HAVE_LINUX_CONFIG_H
90 #include <linux/config.h>
95 /* #endif KERNEL_LINUX */
97 #elif HAVE_LIBKVM_GETPROCS && \
98 (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD)
100 #include <sys/param.h>
101 #include <sys/proc.h>
102 #include <sys/sysctl.h>
103 #include <sys/user.h>
104 /* #endif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD ||
105 * HAVE_STRUCT_KINFO_PROC_OPENBSD) */
107 #elif HAVE_PROCINFO_H
108 #include <procinfo.h>
109 #include <sys/types.h>
111 #define MAXPROCENTRY 32
112 #define MAXTHRDENTRY 16
113 #define MAXARGLN 1024
114 /* #endif HAVE_PROCINFO_H */
117 /* Hack: Avoid #error when building a 32-bit binary with
118 * _FILE_OFFSET_BITS=64. There is a reason for this #error, as one
119 * of the structures in <sys/procfs.h> uses an off_t, but that
120 * isn't relevant to our usage of procfs. */
121 #if !defined(_LP64) && _FILE_OFFSET_BITS == 64
123 #undef _FILE_OFFSET_BITS
129 #define _FILE_OFFSET_BITS 64
134 #include <sys/user.h>
140 /* #endif KERNEL_SOLARIS */
143 #error "No applicable input method."
154 #ifndef CMDLINE_BUFFER_SIZE
155 #if defined(ARG_MAX) && (ARG_MAX < 4096)
156 #define CMDLINE_BUFFER_SIZE ARG_MAX
158 #define CMDLINE_BUFFER_SIZE 4096
162 #define PROCSTAT_NAME_LEN 256
163 typedef struct process_entry_s {
165 char name[PROCSTAT_NAME_LEN];
167 unsigned long num_proc;
168 unsigned long num_lwp;
169 unsigned long vmem_size;
170 unsigned long vmem_rss;
171 unsigned long vmem_data;
172 unsigned long vmem_code;
173 unsigned long stack_size;
175 derive_t vmem_minflt_counter;
176 derive_t vmem_majflt_counter;
178 derive_t cpu_user_counter;
179 derive_t cpu_system_counter;
188 derive_t cswitch_vol;
189 derive_t cswitch_invol;
193 typedef struct procstat_entry_s {
197 derive_t vmem_minflt_counter;
198 derive_t vmem_majflt_counter;
200 derive_t cpu_user_counter;
201 derive_t cpu_system_counter;
209 derive_t cswitch_vol;
210 derive_t cswitch_invol;
212 struct procstat_entry_s *next;
215 typedef struct procstat {
216 char name[PROCSTAT_NAME_LEN];
221 unsigned long num_proc;
222 unsigned long num_lwp;
223 unsigned long vmem_size;
224 unsigned long vmem_rss;
225 unsigned long vmem_data;
226 unsigned long vmem_code;
227 unsigned long stack_size;
229 derive_t vmem_minflt_counter;
230 derive_t vmem_majflt_counter;
232 derive_t cpu_user_counter;
233 derive_t cpu_system_counter;
241 derive_t cswitch_vol;
242 derive_t cswitch_invol;
244 struct procstat *next;
245 struct procstat_entry_s *instances;
248 static procstat_t *list_head_g = NULL;
250 static _Bool want_init = 1;
251 static _Bool report_ctx_switch = 0;
254 static mach_port_t port_host_self;
255 static mach_port_t port_task_self;
257 static processor_set_name_array_t pset_list;
258 static mach_msg_type_number_t pset_list_len;
259 /* #endif HAVE_THREAD_INFO */
262 static long pagesize_g;
263 static void ps_fill_details(const procstat_t *ps, process_entry_t *entry);
264 /* #endif KERNEL_LINUX */
266 #elif HAVE_LIBKVM_GETPROCS && \
267 (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD)
269 /* #endif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD ||
270 * HAVE_STRUCT_KINFO_PROC_OPENBSD) */
272 #elif HAVE_PROCINFO_H
273 static struct procentry64 procentry[MAXPROCENTRY];
274 static struct thrdentry64 thrdentry[MAXTHRDENTRY];
277 #ifndef _AIXVERSION_610
278 int getprocs64(void *procsinfo, int sizproc, void *fdsinfo, int sizfd,
279 pid_t *index, int count);
280 int getthrds64(pid_t, void *, int, tid64_t *, int);
282 int getargs(void *processBuffer, int bufferLen, char *argsBuffer, int argsLen);
283 #endif /* HAVE_PROCINFO_H */
285 /* put name of process from config to list_head_g tree
286 * list_head_g is a list of 'procstat_t' structs with
287 * processes names we want to watch */
288 static void ps_list_register(const char *name, const char *regexp) {
293 new = calloc(1, sizeof(*new));
295 ERROR("processes plugin: ps_list_register: calloc failed.");
298 sstrncpy(new->name, name, sizeof(new->name));
304 new->cswitch_vol = -1;
305 new->cswitch_invol = -1;
308 if (regexp != NULL) {
309 DEBUG("ProcessMatch: adding \"%s\" as criteria to process %s.", regexp,
311 new->re = malloc(sizeof(*new->re));
312 if (new->re == NULL) {
313 ERROR("processes plugin: ps_list_register: malloc failed.");
318 status = regcomp(new->re, regexp, REG_EXTENDED | REG_NOSUB);
320 DEBUG("ProcessMatch: compiling the regular expression \"%s\" failed.",
328 if (regexp != NULL) {
329 ERROR("processes plugin: ps_list_register: "
330 "Regular expression \"%s\" found in config "
331 "file, but support for regular expressions "
332 "has been disabled at compile time.",
339 for (ptr = list_head_g; ptr != NULL; ptr = ptr->next) {
340 if (strcmp(ptr->name, name) == 0) {
341 WARNING("processes plugin: You have configured more "
342 "than one `Process' or "
343 "`ProcessMatch' with the same name. "
344 "All but the first setting will be "
353 if (ptr->next == NULL)
361 } /* void ps_list_register */
363 /* try to match name against entry, returns 1 if success */
364 static int ps_list_match(const char *name, const char *cmdline,
367 if (ps->re != NULL) {
372 if ((str == NULL) || (str[0] == 0))
377 status = regexec(ps->re, str,
385 if (strcmp(ps->name, name) == 0)
389 } /* int ps_list_match */
391 static void ps_update_counter(derive_t *group_counter, derive_t *curr_counter,
392 derive_t new_counter) {
393 unsigned long curr_value;
396 *curr_counter = new_counter;
400 if (new_counter < *curr_counter)
401 curr_value = new_counter + (ULONG_MAX - *curr_counter);
403 curr_value = new_counter - *curr_counter;
405 if (*group_counter == -1)
408 *curr_counter = new_counter;
409 *group_counter += curr_value;
412 /* add process entry to 'instances' of process 'name' (or refresh it) */
413 static void ps_list_add(const char *name, const char *cmdline,
414 process_entry_t *entry) {
415 procstat_entry_t *pse;
420 for (procstat_t *ps = list_head_g; ps != NULL; ps = ps->next) {
421 if ((ps_list_match(name, cmdline, ps)) == 0)
425 ps_fill_details(ps, entry);
428 for (pse = ps->instances; pse != NULL; pse = pse->next)
429 if ((pse->id == entry->id) || (pse->next == NULL))
432 if ((pse == NULL) || (pse->id != entry->id)) {
433 procstat_entry_t *new;
435 new = calloc(1, sizeof(*new));
450 ps->num_proc += entry->num_proc;
451 ps->num_lwp += entry->num_lwp;
452 ps->vmem_size += entry->vmem_size;
453 ps->vmem_rss += entry->vmem_rss;
454 ps->vmem_data += entry->vmem_data;
455 ps->vmem_code += entry->vmem_code;
456 ps->stack_size += entry->stack_size;
458 if ((entry->io_rchar != -1) && (entry->io_wchar != -1)) {
459 ps_update_counter(&ps->io_rchar, &pse->io_rchar, entry->io_rchar);
460 ps_update_counter(&ps->io_wchar, &pse->io_wchar, entry->io_wchar);
463 if ((entry->io_syscr != -1) && (entry->io_syscw != -1)) {
464 ps_update_counter(&ps->io_syscr, &pse->io_syscr, entry->io_syscr);
465 ps_update_counter(&ps->io_syscw, &pse->io_syscw, entry->io_syscw);
468 if ((entry->cswitch_vol != -1) && (entry->cswitch_vol != -1)) {
469 ps_update_counter(&ps->cswitch_vol, &pse->cswitch_vol,
471 ps_update_counter(&ps->cswitch_invol, &pse->cswitch_invol,
472 entry->cswitch_invol);
475 ps_update_counter(&ps->vmem_minflt_counter, &pse->vmem_minflt_counter,
476 entry->vmem_minflt_counter);
477 ps_update_counter(&ps->vmem_majflt_counter, &pse->vmem_majflt_counter,
478 entry->vmem_majflt_counter);
480 ps_update_counter(&ps->cpu_user_counter, &pse->cpu_user_counter,
481 entry->cpu_user_counter);
482 ps_update_counter(&ps->cpu_system_counter, &pse->cpu_system_counter,
483 entry->cpu_system_counter);
487 /* remove old entries from instances of processes in list_head_g */
488 static void ps_list_reset(void) {
489 procstat_entry_t *pse;
490 procstat_entry_t *pse_prev;
492 for (procstat_t *ps = list_head_g; ps != NULL; ps = ps->next) {
503 while (pse != NULL) {
505 DEBUG("Removing this procstat entry cause it's too old: "
506 "id = %lu; name = %s;",
509 if (pse_prev == NULL) {
510 ps->instances = pse->next;
514 pse_prev->next = pse->next;
516 pse = pse_prev->next;
523 } /* while (pse != NULL) */
524 } /* for (ps = list_head_g; ps != NULL; ps = ps->next) */
527 /* put all pre-defined 'Process' names from config to list_head_g tree */
528 static int ps_config(oconfig_item_t *ci) {
530 const size_t max_procname_len = 15;
531 #elif KERNEL_SOLARIS || KERNEL_FREEBSD
532 const size_t max_procname_len = MAXCOMLEN - 1;
535 for (int i = 0; i < ci->children_num; ++i) {
536 oconfig_item_t *c = ci->children + i;
538 if (strcasecmp(c->key, "Process") == 0) {
539 if ((c->values_num != 1) || (OCONFIG_TYPE_STRING != c->values[0].type)) {
540 ERROR("processes plugin: `Process' expects exactly "
541 "one string argument (got %i).",
546 if (c->children_num != 0) {
547 WARNING("processes plugin: the `Process' config option "
548 "does not expect any child elements -- ignoring "
549 "content (%i elements) of the <Process '%s'> block.",
550 c->children_num, c->values[0].value.string);
553 #if KERNEL_LINUX || KERNEL_SOLARIS || KERNEL_FREEBSD
554 if (strlen(c->values[0].value.string) > max_procname_len) {
555 WARNING("processes plugin: this platform has a %zu character limit "
556 "to process names. The `Process \"%s\"' option will "
557 "not work as expected.",
558 max_procname_len, c->values[0].value.string);
562 ps_list_register(c->values[0].value.string, NULL);
563 } else if (strcasecmp(c->key, "ProcessMatch") == 0) {
564 if ((c->values_num != 2) || (OCONFIG_TYPE_STRING != c->values[0].type) ||
565 (OCONFIG_TYPE_STRING != c->values[1].type)) {
566 ERROR("processes plugin: `ProcessMatch' needs exactly "
567 "two string arguments (got %i).",
572 if (c->children_num != 0) {
573 WARNING("processes plugin: the `ProcessMatch' config option "
574 "does not expect any child elements -- ignoring "
575 "content (%i elements) of the <ProcessMatch '%s' '%s'> "
577 c->children_num, c->values[0].value.string,
578 c->values[1].value.string);
581 ps_list_register(c->values[0].value.string, c->values[1].value.string);
582 } else if (strcasecmp(c->key, "CollectContextSwitch") == 0) {
583 cf_util_get_boolean(c, &report_ctx_switch);
585 ERROR("processes plugin: The `%s' configuration option is not "
586 "understood and will be ignored.",
595 static int ps_init(void) {
597 kern_return_t status;
599 port_host_self = mach_host_self();
600 port_task_self = mach_task_self();
602 if (pset_list != NULL) {
603 vm_deallocate(port_task_self, (vm_address_t)pset_list,
604 pset_list_len * sizeof(processor_set_t));
609 if ((status = host_processor_sets(port_host_self, &pset_list,
610 &pset_list_len)) != KERN_SUCCESS) {
611 ERROR("host_processor_sets failed: %s\n", mach_error_string(status));
616 /* #endif HAVE_THREAD_INFO */
619 pagesize_g = sysconf(_SC_PAGESIZE);
620 DEBUG("pagesize_g = %li; CONFIG_HZ = %i;", pagesize_g, CONFIG_HZ);
621 /* #endif KERNEL_LINUX */
623 #elif HAVE_LIBKVM_GETPROCS && \
624 (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD)
625 pagesize = getpagesize();
626 /* #endif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD ||
627 * HAVE_STRUCT_KINFO_PROC_OPENBSD) */
629 #elif HAVE_PROCINFO_H
630 pagesize = getpagesize();
631 #endif /* HAVE_PROCINFO_H */
636 /* submit global state (e.g.: qty of zombies, running, etc..) */
637 static void ps_submit_state(const char *state, double value) {
638 value_list_t vl = VALUE_LIST_INIT;
640 vl.values = &(value_t){.gauge = value};
642 sstrncpy(vl.plugin, "processes", sizeof(vl.plugin));
643 sstrncpy(vl.plugin_instance, "", sizeof(vl.plugin_instance));
644 sstrncpy(vl.type, "ps_state", sizeof(vl.type));
645 sstrncpy(vl.type_instance, state, sizeof(vl.type_instance));
647 plugin_dispatch_values(&vl);
650 /* submit info about specific process (e.g.: memory taken, cpu usage, etc..) */
651 static void ps_submit_proc_list(procstat_t *ps) {
652 value_list_t vl = VALUE_LIST_INIT;
656 sstrncpy(vl.plugin, "processes", sizeof(vl.plugin));
657 sstrncpy(vl.plugin_instance, ps->name, sizeof(vl.plugin_instance));
659 sstrncpy(vl.type, "ps_vm", sizeof(vl.type));
660 vl.values[0].gauge = ps->vmem_size;
662 plugin_dispatch_values(&vl);
664 sstrncpy(vl.type, "ps_rss", sizeof(vl.type));
665 vl.values[0].gauge = ps->vmem_rss;
667 plugin_dispatch_values(&vl);
669 sstrncpy(vl.type, "ps_data", sizeof(vl.type));
670 vl.values[0].gauge = ps->vmem_data;
672 plugin_dispatch_values(&vl);
674 sstrncpy(vl.type, "ps_code", sizeof(vl.type));
675 vl.values[0].gauge = ps->vmem_code;
677 plugin_dispatch_values(&vl);
679 sstrncpy(vl.type, "ps_stacksize", sizeof(vl.type));
680 vl.values[0].gauge = ps->stack_size;
682 plugin_dispatch_values(&vl);
684 sstrncpy(vl.type, "ps_cputime", sizeof(vl.type));
685 vl.values[0].derive = ps->cpu_user_counter;
686 vl.values[1].derive = ps->cpu_system_counter;
688 plugin_dispatch_values(&vl);
690 sstrncpy(vl.type, "ps_count", sizeof(vl.type));
691 vl.values[0].gauge = ps->num_proc;
692 vl.values[1].gauge = ps->num_lwp;
694 plugin_dispatch_values(&vl);
696 sstrncpy(vl.type, "ps_pagefaults", sizeof(vl.type));
697 vl.values[0].derive = ps->vmem_minflt_counter;
698 vl.values[1].derive = ps->vmem_majflt_counter;
700 plugin_dispatch_values(&vl);
702 if ((ps->io_rchar != -1) && (ps->io_wchar != -1)) {
703 sstrncpy(vl.type, "ps_disk_octets", sizeof(vl.type));
704 vl.values[0].derive = ps->io_rchar;
705 vl.values[1].derive = ps->io_wchar;
707 plugin_dispatch_values(&vl);
710 if ((ps->io_syscr != -1) && (ps->io_syscw != -1)) {
711 sstrncpy(vl.type, "ps_disk_ops", sizeof(vl.type));
712 vl.values[0].derive = ps->io_syscr;
713 vl.values[1].derive = ps->io_syscw;
715 plugin_dispatch_values(&vl);
718 if ((ps->cswitch_vol != -1) && (ps->cswitch_invol != -1)) {
719 sstrncpy(vl.type, "contextswitch", sizeof(vl.type));
720 sstrncpy(vl.type_instance, "voluntary", sizeof(vl.type_instance));
721 vl.values[0].derive = ps->cswitch_vol;
723 plugin_dispatch_values(&vl);
725 sstrncpy(vl.type, "contextswitch", sizeof(vl.type));
726 sstrncpy(vl.type_instance, "involuntary", sizeof(vl.type_instance));
727 vl.values[0].derive = ps->cswitch_invol;
729 plugin_dispatch_values(&vl);
732 DEBUG("name = %s; num_proc = %lu; num_lwp = %lu; "
733 "vmem_size = %lu; vmem_rss = %lu; vmem_data = %lu; "
735 "vmem_minflt_counter = %" PRIi64 "; vmem_majflt_counter = %" PRIi64 "; "
736 "cpu_user_counter = %" PRIi64 "; cpu_system_counter = %" PRIi64 "; "
737 "io_rchar = %" PRIi64 "; io_wchar = %" PRIi64 "; "
738 "io_syscr = %" PRIi64 "; io_syscw = %" PRIi64 "; "
739 "cswitch_vol = %" PRIi64 "; cswitch_invol = %" PRIi64 ";",
740 ps->name, ps->num_proc, ps->num_lwp, ps->vmem_size, ps->vmem_rss,
741 ps->vmem_data, ps->vmem_code, ps->vmem_minflt_counter,
742 ps->vmem_majflt_counter, ps->cpu_user_counter, ps->cpu_system_counter,
743 ps->io_rchar, ps->io_wchar, ps->io_syscr, ps->io_syscw, ps->cswitch_vol,
745 } /* void ps_submit_proc_list */
747 #if KERNEL_LINUX || KERNEL_SOLARIS
748 static void ps_submit_fork_rate(derive_t value) {
749 value_list_t vl = VALUE_LIST_INIT;
751 vl.values = &(value_t){.derive = value};
753 sstrncpy(vl.plugin, "processes", sizeof(vl.plugin));
754 sstrncpy(vl.plugin_instance, "", sizeof(vl.plugin_instance));
755 sstrncpy(vl.type, "fork_rate", sizeof(vl.type));
756 sstrncpy(vl.type_instance, "", sizeof(vl.type_instance));
758 plugin_dispatch_values(&vl);
760 #endif /* KERNEL_LINUX || KERNEL_SOLARIS*/
762 /* ------- additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
764 static int ps_read_tasks_status(process_entry_t *ps) {
770 derive_t cswitch_vol = 0;
771 derive_t cswitch_invol = 0;
776 ssnprintf(dirname, sizeof(dirname), "/proc/%li/task", ps->id);
778 if ((dh = opendir(dirname)) == NULL) {
779 DEBUG("Failed to open directory `%s'", dirname);
783 while ((ent = readdir(dh)) != NULL) {
786 if (!isdigit((int)ent->d_name[0]))
791 ssnprintf(filename, sizeof(filename), "/proc/%li/task/%s/status", ps->id,
793 if ((fh = fopen(filename, "r")) == NULL) {
794 DEBUG("Failed to open file `%s'", filename);
798 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
802 if (strncmp(buffer, "voluntary_ctxt_switches", 23) != 0 &&
803 strncmp(buffer, "nonvoluntary_ctxt_switches", 26) != 0)
806 numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
813 tmp = (derive_t)strtoll(fields[1], &endptr, /* base = */ 10);
814 if ((errno == 0) && (endptr != fields[1])) {
815 if (strncmp(buffer, "voluntary_ctxt_switches", 23) == 0) {
817 } else if (strncmp(buffer, "nonvoluntary_ctxt_switches", 26) == 0) {
818 cswitch_invol += tmp;
821 } /* while (fgets) */
825 WARNING("processes: fclose: %s",
826 sstrerror(errno, errbuf, sizeof(errbuf)));
831 ps->cswitch_vol = cswitch_vol;
832 ps->cswitch_invol = cswitch_invol;
835 } /* int *ps_read_tasks_status */
837 /* Read data from /proc/pid/status */
838 static int ps_read_status(long pid, process_entry_t *ps) {
842 unsigned long lib = 0;
843 unsigned long exe = 0;
844 unsigned long data = 0;
845 unsigned long threads = 0;
849 ssnprintf(filename, sizeof(filename), "/proc/%li/status", pid);
850 if ((fh = fopen(filename, "r")) == NULL)
853 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
857 if (strncmp(buffer, "Vm", 2) != 0 && strncmp(buffer, "Threads", 7) != 0)
860 numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
867 tmp = strtoul(fields[1], &endptr, /* base = */ 10);
868 if ((errno == 0) && (endptr != fields[1])) {
869 if (strncmp(buffer, "VmData", 6) == 0) {
871 } else if (strncmp(buffer, "VmLib", 5) == 0) {
873 } else if (strncmp(buffer, "VmExe", 5) == 0) {
875 } else if (strncmp(buffer, "Threads", 7) == 0) {
879 } /* while (fgets) */
883 WARNING("processes: fclose: %s", sstrerror(errno, errbuf, sizeof(errbuf)));
886 ps->vmem_data = data * 1024;
887 ps->vmem_code = (exe + lib) * 1024;
889 ps->num_lwp = threads;
892 } /* int *ps_read_status */
894 static int ps_read_io(process_entry_t *ps) {
902 ssnprintf(filename, sizeof(filename), "/proc/%li/io", ps->id);
903 if ((fh = fopen(filename, "r")) == NULL)
906 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
907 derive_t *val = NULL;
911 if (strncasecmp(buffer, "rchar:", 6) == 0)
912 val = &(ps->io_rchar);
913 else if (strncasecmp(buffer, "wchar:", 6) == 0)
914 val = &(ps->io_wchar);
915 else if (strncasecmp(buffer, "syscr:", 6) == 0)
916 val = &(ps->io_syscr);
917 else if (strncasecmp(buffer, "syscw:", 6) == 0)
918 val = &(ps->io_syscw);
922 numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
929 tmp = strtoll(fields[1], &endptr, /* base = */ 10);
930 if ((errno != 0) || (endptr == fields[1]))
933 *val = (derive_t)tmp;
934 } /* while (fgets) */
938 WARNING("processes: fclose: %s", sstrerror(errno, errbuf, sizeof(errbuf)));
941 } /* int ps_read_io (...) */
943 static void ps_fill_details(const procstat_t *ps, process_entry_t *entry) {
944 if (entry->has_io == 0 && ps_read_io(entry) != 0) {
946 entry->io_rchar = -1;
947 entry->io_wchar = -1;
948 entry->io_syscr = -1;
949 entry->io_syscw = -1;
951 DEBUG("ps_read_io: not get io data for pid %li", entry->id);
955 if (report_ctx_switch) {
956 if (entry->has_cswitch == 0 && ps_read_tasks_status(entry) != 0) {
957 entry->cswitch_vol = -1;
958 entry->cswitch_invol = -1;
960 DEBUG("ps_read_tasks_status: not get context "
961 "switch data for pid %li",
964 entry->has_cswitch = 1;
966 } /* void ps_fill_details (...) */
968 static int ps_read_process(long pid, process_entry_t *ps, char *state) {
978 size_t name_start_pos;
982 derive_t cpu_user_counter;
983 derive_t cpu_system_counter;
984 long long unsigned vmem_size;
985 long long unsigned vmem_rss;
986 long long unsigned stack_size;
990 ssnprintf(filename, sizeof(filename), "/proc/%li/stat", pid);
992 status = read_file_contents(filename, buffer, sizeof(buffer) - 1);
995 buffer_len = (size_t)status;
996 buffer[buffer_len] = 0;
998 /* The name of the process is enclosed in parens. Since the name can
999 * contain parens itself, spaces, numbers and pretty much everything
1000 * else, use these to determine the process name. We don't use
1001 * strchr(3) and strrchr(3) to avoid pointer arithmetic which would
1002 * otherwise be required to determine name_len. */
1004 while (name_start_pos < buffer_len && buffer[name_start_pos] != '(')
1007 name_end_pos = buffer_len;
1008 while (name_end_pos > 0 && buffer[name_end_pos] != ')')
1011 /* Either '(' or ')' is not found or they are in the wrong order.
1012 * Anyway, something weird that shouldn't happen ever. */
1013 if (name_start_pos >= name_end_pos) {
1014 ERROR("processes plugin: name_start_pos = %zu >= name_end_pos = %zu",
1015 name_start_pos, name_end_pos);
1019 name_len = (name_end_pos - name_start_pos) - 1;
1020 if (name_len >= sizeof(ps->name))
1021 name_len = sizeof(ps->name) - 1;
1023 sstrncpy(ps->name, &buffer[name_start_pos + 1], name_len + 1);
1025 if ((buffer_len - name_end_pos) < 2)
1027 buffer_ptr = &buffer[name_end_pos + 2];
1029 fields_len = strsplit(buffer_ptr, fields, STATIC_ARRAY_SIZE(fields));
1030 if (fields_len < 22) {
1031 DEBUG("processes plugin: ps_read_process (pid = %li):"
1032 " `%s' has only %i fields..",
1033 pid, filename, fields_len);
1037 *state = fields[0][0];
1039 if (*state == 'Z') {
1043 ps->num_lwp = strtoul(fields[17], /* endptr = */ NULL, /* base = */ 10);
1044 if ((ps_read_status(pid, ps)) != 0) {
1048 DEBUG("ps_read_process: did not get vmem data for pid %li", pid);
1050 if (ps->num_lwp == 0)
1055 /* Leave the rest at zero if this is only a zombi */
1056 if (ps->num_proc == 0) {
1057 DEBUG("processes plugin: This is only a zombie: pid = %li; "
1063 cpu_user_counter = atoll(fields[11]);
1064 cpu_system_counter = atoll(fields[12]);
1065 vmem_size = atoll(fields[20]);
1066 vmem_rss = atoll(fields[21]);
1067 ps->vmem_minflt_counter = atol(fields[7]);
1068 ps->vmem_majflt_counter = atol(fields[9]);
1071 unsigned long long stack_start = atoll(fields[25]);
1072 unsigned long long stack_ptr = atoll(fields[26]);
1074 stack_size = (stack_start > stack_ptr) ? stack_start - stack_ptr
1075 : stack_ptr - stack_start;
1078 /* Convert jiffies to useconds */
1079 cpu_user_counter = cpu_user_counter * 1000000 / CONFIG_HZ;
1080 cpu_system_counter = cpu_system_counter * 1000000 / CONFIG_HZ;
1081 vmem_rss = vmem_rss * pagesize_g;
1083 ps->cpu_user_counter = cpu_user_counter;
1084 ps->cpu_system_counter = cpu_system_counter;
1085 ps->vmem_size = (unsigned long)vmem_size;
1086 ps->vmem_rss = (unsigned long)vmem_rss;
1087 ps->stack_size = (unsigned long)stack_size;
1091 } /* int ps_read_process (...) */
1093 static char *ps_get_cmdline(long pid, char *name, char *buf, size_t buf_len) {
1097 char file[PATH_MAX];
1102 if ((pid < 1) || (NULL == buf) || (buf_len < 2))
1105 ssnprintf(file, sizeof(file), "/proc/%li/cmdline", pid);
1108 fd = open(file, O_RDONLY);
1111 /* ENOENT means the process exited while we were handling it.
1112 * Don't complain about this, it only fills the logs. */
1113 if (errno != ENOENT)
1114 WARNING("processes plugin: Failed to open `%s': %s.", file,
1115 sstrerror(errno, errbuf, sizeof(errbuf)));
1127 status = read(fd, (void *)buf_ptr, len);
1132 if ((EAGAIN == errno) || (EINTR == errno))
1135 WARNING("processes plugin: Failed to read from `%s': %s.", file,
1136 sstrerror(errno, errbuf, sizeof(errbuf)));
1156 /* cmdline not available; e.g. kernel thread, zombie */
1160 ssnprintf(buf, buf_len, "[%s]", name);
1164 assert(n <= buf_len);
1171 /* remove trailing whitespace */
1172 while ((n > 0) && (isspace(buf[n]) || ('\0' == buf[n]))) {
1177 /* arguments are separated by '\0' in /proc/<pid>/cmdline */
1184 } /* char *ps_get_cmdline (...) */
1186 static int read_fork_rate(void) {
1190 _Bool value_valid = 0;
1192 proc_stat = fopen("/proc/stat", "r");
1193 if (proc_stat == NULL) {
1195 ERROR("processes plugin: fopen (/proc/stat) failed: %s",
1196 sstrerror(errno, errbuf, sizeof(errbuf)));
1200 while (fgets(buffer, sizeof(buffer), proc_stat) != NULL) {
1205 fields_num = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
1206 if (fields_num != 2)
1209 if (strcmp("processes", fields[0]) != 0)
1212 status = parse_value(fields[1], &value, DS_TYPE_DERIVE);
1223 ps_submit_fork_rate(value.derive);
1226 #endif /*KERNEL_LINUX */
1229 static char *ps_get_cmdline(long pid,
1230 char *name __attribute__((unused)), /* {{{ */
1231 char *buffer, size_t buffer_size) {
1232 char path[PATH_MAX];
1236 snprintf(path, sizeof(path), "/proc/%li/psinfo", pid);
1238 status = read_file_contents(path, (void *)&info, sizeof(info));
1239 if ((status < 0) || (((size_t)status) != sizeof(info))) {
1240 ERROR("processes plugin: Unexpected return value "
1241 "while reading \"%s\": "
1242 "Returned %zd but expected %zu.",
1243 path, status, buffer_size);
1247 info.pr_psargs[sizeof(info.pr_psargs) - 1] = 0;
1248 sstrncpy(buffer, info.pr_psargs, buffer_size);
1251 } /* }}} int ps_get_cmdline */
1254 * Reads process information on the Solaris OS. The information comes mainly
1256 * /proc/PID/status, /proc/PID/psinfo and /proc/PID/usage
1257 * The values for input and ouput chars are calculated "by hand"
1258 * Added a few "solaris" specific process states as well
1260 static int ps_read_process(long pid, process_entry_t *ps, char *state) {
1262 char f_psinfo[64], f_usage[64];
1265 pstatus_t *myStatus;
1269 snprintf(filename, sizeof(filename), "/proc/%li/status", pid);
1270 snprintf(f_psinfo, sizeof(f_psinfo), "/proc/%li/psinfo", pid);
1271 snprintf(f_usage, sizeof(f_usage), "/proc/%li/usage", pid);
1273 buffer = calloc(1, sizeof(pstatus_t));
1274 read_file_contents(filename, buffer, sizeof(pstatus_t));
1275 myStatus = (pstatus_t *)buffer;
1277 buffer = calloc(1, sizeof(psinfo_t));
1278 read_file_contents(f_psinfo, buffer, sizeof(psinfo_t));
1279 myInfo = (psinfo_t *)buffer;
1281 buffer = calloc(1, sizeof(prusage_t));
1282 read_file_contents(f_usage, buffer, sizeof(prusage_t));
1283 myUsage = (prusage_t *)buffer;
1285 sstrncpy(ps->name, myInfo->pr_fname, sizeof(myInfo->pr_fname));
1286 ps->num_lwp = myStatus->pr_nlwp;
1287 if (myInfo->pr_wstat != 0) {
1298 ps->num_lwp = myInfo->pr_nlwp;
1302 * Convert system time and user time from nanoseconds to microseconds
1303 * for compatibility with the linux module
1305 ps->cpu_system_counter = myStatus->pr_stime.tv_nsec / 1000;
1306 ps->cpu_user_counter = myStatus->pr_utime.tv_nsec / 1000;
1309 * Convert rssize from KB to bytes to be consistent w/ the linux module
1311 ps->vmem_rss = myInfo->pr_rssize * 1024;
1312 ps->vmem_size = myInfo->pr_size * 1024;
1313 ps->vmem_minflt_counter = myUsage->pr_minf;
1314 ps->vmem_majflt_counter = myUsage->pr_majf;
1317 * TODO: Data and code segment calculations for Solaris
1322 ps->stack_size = myStatus->pr_stksize;
1325 * Calculating input/ouput chars
1326 * Formula used is total chars / total blocks => chars/block
1327 * then convert input/output blocks to chars
1329 ulong_t tot_chars = myUsage->pr_ioch;
1330 ulong_t tot_blocks = myUsage->pr_inblk + myUsage->pr_oublk;
1331 ulong_t chars_per_block = 1;
1332 if (tot_blocks != 0)
1333 chars_per_block = tot_chars / tot_blocks;
1334 ps->io_rchar = myUsage->pr_inblk * chars_per_block;
1335 ps->io_wchar = myUsage->pr_oublk * chars_per_block;
1336 ps->io_syscr = myUsage->pr_sysc;
1337 ps->io_syscw = myUsage->pr_sysc;
1340 * TODO: context switch counters for Solaris
1342 ps->cswitch_vol = -1;
1343 ps->cswitch_invol = -1;
1346 * TODO: Find way of setting BLOCKED and PAGING status
1350 if (myStatus->pr_flags & PR_ASLEEP)
1352 else if (myStatus->pr_flags & PR_STOPPED)
1354 else if (myStatus->pr_flags & PR_DETACH)
1356 else if (myStatus->pr_flags & PR_DAEMON)
1358 else if (myStatus->pr_flags & PR_ISSYS)
1360 else if (myStatus->pr_flags & PR_ORPHAN)
1371 * Reads the number of threads created since the last reboot. On Solaris these
1372 * are retrieved from kstat (module cpu, name sys, class misc, stat nthreads).
1373 * The result is the sum for all the threads created on each cpu
1375 static int read_fork_rate(void) {
1376 extern kstat_ctl_t *kc;
1377 derive_t result = 0;
1382 for (kstat_t *ksp_chain = kc->kc_chain; ksp_chain != NULL;
1383 ksp_chain = ksp_chain->ks_next) {
1384 if ((strcmp(ksp_chain->ks_module, "cpu") == 0) &&
1385 (strcmp(ksp_chain->ks_name, "sys") == 0) &&
1386 (strcmp(ksp_chain->ks_class, "misc") == 0)) {
1389 kstat_read(kc, ksp_chain, NULL);
1391 tmp = get_kstat_value(ksp_chain, "nthreads");
1397 ps_submit_fork_rate(result);
1400 #endif /* KERNEL_SOLARIS */
1402 #if HAVE_THREAD_INFO
1403 static int mach_get_task_name(task_t t, int *pid, char *name,
1404 size_t name_max_len) {
1407 struct kinfo_proc kp;
1412 mib[2] = KERN_PROC_PID;
1414 if (pid_for_task(t, pid) != KERN_SUCCESS)
1418 kp_size = sizeof(kp);
1419 if (sysctl(mib, 4, &kp, &kp_size, NULL, 0) != 0)
1422 if (name_max_len > (MAXCOMLEN + 1))
1423 name_max_len = MAXCOMLEN + 1;
1425 strncpy(name, kp.kp_proc.p_comm, name_max_len - 1);
1426 name[name_max_len - 1] = '\0';
1428 DEBUG("pid = %i; name = %s;", *pid, name);
1430 /* We don't do the special handling for `p_comm == "LaunchCFMApp"' as
1431 * `top' does it, because it is a lot of work and only used when
1432 * debugging. -octo */
1436 #endif /* HAVE_THREAD_INFO */
1437 /* ------- end of additional functions for KERNEL_LINUX/HAVE_THREAD_INFO -------
1440 /* do actual readings from kernel */
1441 static int ps_read(void) {
1442 #if HAVE_THREAD_INFO
1443 kern_return_t status;
1445 processor_set_t port_pset_priv;
1447 task_array_t task_list;
1448 mach_msg_type_number_t task_list_len;
1451 char task_name[MAXCOMLEN + 1];
1453 thread_act_array_t thread_list;
1454 mach_msg_type_number_t thread_list_len;
1455 thread_basic_info_data_t thread_data;
1456 mach_msg_type_number_t thread_data_len;
1465 process_entry_t pse;
1470 * The Mach-concept is a little different from the traditional UNIX
1471 * concept: All the work is done in threads. Threads are contained in
1472 * `tasks'. Therefore, `task status' doesn't make much sense, since
1473 * it's actually a `thread status'.
1474 * Tasks are assigned to sets of processors, so that's where you go to
1477 for (mach_msg_type_number_t pset = 0; pset < pset_list_len; pset++) {
1478 if ((status = host_processor_set_priv(port_host_self, pset_list[pset],
1479 &port_pset_priv)) != KERN_SUCCESS) {
1480 ERROR("host_processor_set_priv failed: %s\n", mach_error_string(status));
1484 if ((status = processor_set_tasks(port_pset_priv, &task_list,
1485 &task_list_len)) != KERN_SUCCESS) {
1486 ERROR("processor_set_tasks failed: %s\n", mach_error_string(status));
1487 mach_port_deallocate(port_task_self, port_pset_priv);
1491 for (mach_msg_type_number_t task = 0; task < task_list_len; task++) {
1493 if (mach_get_task_name(task_list[task], &task_pid, task_name,
1494 PROCSTAT_NAME_LEN) == 0) {
1495 /* search for at least one match */
1496 for (ps = list_head_g; ps != NULL; ps = ps->next)
1497 /* FIXME: cmdline should be here instead of NULL */
1498 if (ps_list_match(task_name, NULL, ps) == 1)
1502 /* Collect more detailed statistics for this process */
1504 task_basic_info_data_t task_basic_info;
1505 mach_msg_type_number_t task_basic_info_len;
1506 task_events_info_data_t task_events_info;
1507 mach_msg_type_number_t task_events_info_len;
1508 task_absolutetime_info_data_t task_absolutetime_info;
1509 mach_msg_type_number_t task_absolutetime_info_len;
1511 memset(&pse, '\0', sizeof(pse));
1514 task_basic_info_len = TASK_BASIC_INFO_COUNT;
1515 status = task_info(task_list[task], TASK_BASIC_INFO,
1516 (task_info_t)&task_basic_info, &task_basic_info_len);
1517 if (status != KERN_SUCCESS) {
1518 ERROR("task_info failed: %s", mach_error_string(status));
1519 continue; /* with next thread_list */
1522 task_events_info_len = TASK_EVENTS_INFO_COUNT;
1524 task_info(task_list[task], TASK_EVENTS_INFO,
1525 (task_info_t)&task_events_info, &task_events_info_len);
1526 if (status != KERN_SUCCESS) {
1527 ERROR("task_info failed: %s", mach_error_string(status));
1528 continue; /* with next thread_list */
1531 task_absolutetime_info_len = TASK_ABSOLUTETIME_INFO_COUNT;
1532 status = task_info(task_list[task], TASK_ABSOLUTETIME_INFO,
1533 (task_info_t)&task_absolutetime_info,
1534 &task_absolutetime_info_len);
1535 if (status != KERN_SUCCESS) {
1536 ERROR("task_info failed: %s", mach_error_string(status));
1537 continue; /* with next thread_list */
1541 pse.vmem_size = task_basic_info.virtual_size;
1542 pse.vmem_rss = task_basic_info.resident_size;
1543 /* Does not seem to be easily exposed */
1547 pse.vmem_minflt_counter = task_events_info.cow_faults;
1548 pse.vmem_majflt_counter = task_events_info.faults;
1550 pse.cpu_user_counter = task_absolutetime_info.total_user;
1551 pse.cpu_system_counter = task_absolutetime_info.total_system;
1553 /* context switch counters not implemented */
1554 pse.cswitch_vol = -1;
1555 pse.cswitch_invol = -1;
1558 status = task_threads(task_list[task], &thread_list, &thread_list_len);
1559 if (status != KERN_SUCCESS) {
1560 /* Apple's `top' treats this case a zombie. It
1561 * makes sense to some extend: A `zombie'
1562 * thread is nonsense, since the task/process
1565 DEBUG("task_threads failed: %s", mach_error_string(status));
1566 if (task_list[task] != port_task_self)
1567 mach_port_deallocate(port_task_self, task_list[task]);
1568 continue; /* with next task_list */
1571 for (mach_msg_type_number_t thread = 0; thread < thread_list_len;
1573 thread_data_len = THREAD_BASIC_INFO_COUNT;
1574 status = thread_info(thread_list[thread], THREAD_BASIC_INFO,
1575 (thread_info_t)&thread_data, &thread_data_len);
1576 if (status != KERN_SUCCESS) {
1577 ERROR("thread_info failed: %s", mach_error_string(status));
1578 if (task_list[task] != port_task_self)
1579 mach_port_deallocate(port_task_self, thread_list[thread]);
1580 continue; /* with next thread_list */
1586 switch (thread_data.run_state) {
1587 case TH_STATE_RUNNING:
1590 case TH_STATE_STOPPED:
1591 /* What exactly is `halted'? */
1592 case TH_STATE_HALTED:
1595 case TH_STATE_WAITING:
1598 case TH_STATE_UNINTERRUPTIBLE:
1601 /* There is no `zombie' case here,
1602 * since there are no zombie-threads.
1603 * There's only zombie tasks, which are
1606 WARNING("Unknown thread status: %i", thread_data.run_state);
1608 } /* switch (thread_data.run_state) */
1610 if (task_list[task] != port_task_self) {
1611 status = mach_port_deallocate(port_task_self, thread_list[thread]);
1612 if (status != KERN_SUCCESS)
1613 ERROR("mach_port_deallocate failed: %s", mach_error_string(status));
1615 } /* for (thread_list) */
1617 if ((status = vm_deallocate(port_task_self, (vm_address_t)thread_list,
1618 thread_list_len * sizeof(thread_act_t))) !=
1620 ERROR("vm_deallocate failed: %s", mach_error_string(status));
1623 thread_list_len = 0;
1625 /* Only deallocate the task port, if it isn't our own.
1626 * Don't know what would happen in that case, but this
1627 * is what Apple's top does.. ;) */
1628 if (task_list[task] != port_task_self) {
1629 status = mach_port_deallocate(port_task_self, task_list[task]);
1630 if (status != KERN_SUCCESS)
1631 ERROR("mach_port_deallocate failed: %s", mach_error_string(status));
1635 /* FIXME: cmdline should be here instead of NULL */
1636 ps_list_add(task_name, NULL, &pse);
1637 } /* for (task_list) */
1639 if ((status = vm_deallocate(port_task_self, (vm_address_t)task_list,
1640 task_list_len * sizeof(task_t))) !=
1642 ERROR("vm_deallocate failed: %s", mach_error_string(status));
1647 if ((status = mach_port_deallocate(port_task_self, port_pset_priv)) !=
1649 ERROR("mach_port_deallocate failed: %s", mach_error_string(status));
1651 } /* for (pset_list) */
1653 ps_submit_state("running", running);
1654 ps_submit_state("sleeping", sleeping);
1655 ps_submit_state("zombies", zombies);
1656 ps_submit_state("stopped", stopped);
1657 ps_submit_state("blocked", blocked);
1659 for (ps = list_head_g; ps != NULL; ps = ps->next)
1660 ps_submit_proc_list(ps);
1661 /* #endif HAVE_THREAD_INFO */
1675 char cmdline[CMDLINE_BUFFER_SIZE];
1678 process_entry_t pse;
1681 running = sleeping = zombies = stopped = paging = blocked = 0;
1684 if ((proc = opendir("/proc")) == NULL) {
1686 ERROR("Cannot open `/proc': %s", sstrerror(errno, errbuf, sizeof(errbuf)));
1690 while ((ent = readdir(proc)) != NULL) {
1691 if (!isdigit(ent->d_name[0]))
1694 if ((pid = atol(ent->d_name)) < 1)
1697 memset(&pse, 0, sizeof(pse));
1700 status = ps_read_process(pid, &pse, &state);
1702 DEBUG("ps_read_process failed: %i", status);
1727 ps_list_add(pse.name,
1728 ps_get_cmdline(pid, pse.name, cmdline, sizeof(cmdline)), &pse);
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("paging", paging);
1738 ps_submit_state("blocked", blocked);
1740 for (procstat_t *ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1741 ps_submit_proc_list(ps_ptr);
1744 /* #endif KERNEL_LINUX */
1746 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD
1756 char errbuf[_POSIX2_LINE_MAX];
1757 struct kinfo_proc *procs; /* array of processes */
1758 struct kinfo_proc *proc_ptr = NULL;
1759 int count; /* returns number of processes */
1761 process_entry_t pse;
1765 /* Open the kvm interface, get a descriptor */
1766 kd = kvm_openfiles(NULL, "/dev/null", NULL, 0, errbuf);
1768 ERROR("processes plugin: Cannot open kvm interface: %s", errbuf);
1772 /* Get the list of processes. */
1773 procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, &count);
1774 if (procs == NULL) {
1775 ERROR("processes plugin: Cannot get kvm processes list: %s",
1781 /* Iterate through the processes in kinfo_proc */
1782 for (int i = 0; i < count; i++) {
1783 /* Create only one process list entry per _process_, i.e.
1784 * filter out threads (duplicate PID entries). */
1785 if ((proc_ptr == NULL) || (proc_ptr->ki_pid != procs[i].ki_pid)) {
1786 char cmdline[CMDLINE_BUFFER_SIZE] = "";
1787 _Bool have_cmdline = 0;
1789 proc_ptr = &(procs[i]);
1790 /* Don't probe system processes and processes without arguments */
1791 if (((procs[i].ki_flag & P_SYSTEM) == 0) && (procs[i].ki_args != NULL)) {
1796 /* retrieve the arguments */
1797 argv = kvm_getargv(kd, proc_ptr, /* nchr = */ 0);
1799 if ((argv != NULL) && (argv[0] != NULL)) {
1800 while (argv[argc] != NULL)
1803 status = strjoin(cmdline, sizeof(cmdline), argv, argc, " ");
1805 WARNING("processes plugin: Command line did not fit into buffer.");
1809 } /* if (process has argument list) */
1811 memset(&pse, 0, sizeof(pse));
1812 pse.id = procs[i].ki_pid;
1815 pse.num_lwp = procs[i].ki_numthreads;
1817 pse.vmem_size = procs[i].ki_size;
1818 pse.vmem_rss = procs[i].ki_rssize * pagesize;
1819 pse.vmem_data = procs[i].ki_dsize * pagesize;
1820 pse.vmem_code = procs[i].ki_tsize * pagesize;
1821 pse.stack_size = procs[i].ki_ssize * pagesize;
1822 pse.vmem_minflt_counter = procs[i].ki_rusage.ru_minflt;
1823 pse.vmem_majflt_counter = procs[i].ki_rusage.ru_majflt;
1825 pse.cpu_user_counter = 0;
1826 pse.cpu_system_counter = 0;
1828 * The u-area might be swapped out, and we can't get
1829 * at it because we have a crashdump and no swap.
1830 * If it's here fill in these fields, otherwise, just
1833 if (procs[i].ki_flag & P_INMEM) {
1834 pse.cpu_user_counter = procs[i].ki_rusage.ru_utime.tv_usec +
1835 (1000000lu * procs[i].ki_rusage.ru_utime.tv_sec);
1836 pse.cpu_system_counter =
1837 procs[i].ki_rusage.ru_stime.tv_usec +
1838 (1000000lu * procs[i].ki_rusage.ru_stime.tv_sec);
1847 /* context switch counters not implemented */
1848 pse.cswitch_vol = -1;
1849 pse.cswitch_invol = -1;
1851 ps_list_add(procs[i].ki_comm, have_cmdline ? cmdline : NULL, &pse);
1853 switch (procs[i].ki_stat) {
1876 } /* if ((proc_ptr == NULL) || (proc_ptr->ki_pid != procs[i].ki_pid)) */
1881 ps_submit_state("running", running);
1882 ps_submit_state("sleeping", sleeping);
1883 ps_submit_state("zombies", zombies);
1884 ps_submit_state("stopped", stopped);
1885 ps_submit_state("blocked", blocked);
1886 ps_submit_state("idle", idle);
1887 ps_submit_state("wait", wait);
1889 for (procstat_t *ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1890 ps_submit_proc_list(ps_ptr);
1891 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
1893 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_OPENBSD
1904 struct kinfo_proc *procs; /* array of processes */
1905 struct kinfo_proc *proc_ptr = NULL;
1906 int count; /* returns number of processes */
1908 process_entry_t pse;
1912 /* Open the kvm interface, get a descriptor */
1913 kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf);
1915 ERROR("processes plugin: Cannot open kvm interface: %s", errbuf);
1919 /* Get the list of processes. */
1920 procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc), &count);
1921 if (procs == NULL) {
1922 ERROR("processes plugin: Cannot get kvm processes list: %s",
1928 /* Iterate through the processes in kinfo_proc */
1929 for (int i = 0; i < count; i++) {
1930 /* Create only one process list entry per _process_, i.e.
1931 * filter out threads (duplicate PID entries). */
1932 if ((proc_ptr == NULL) || (proc_ptr->p_pid != procs[i].p_pid)) {
1933 char cmdline[CMDLINE_BUFFER_SIZE] = "";
1934 _Bool have_cmdline = 0;
1936 proc_ptr = &(procs[i]);
1937 /* Don't probe zombie processes */
1938 if (!P_ZOMBIE(proc_ptr)) {
1943 /* retrieve the arguments */
1944 argv = kvm_getargv(kd, proc_ptr, /* nchr = */ 0);
1946 if ((argv != NULL) && (argv[0] != NULL)) {
1947 while (argv[argc] != NULL)
1950 status = strjoin(cmdline, sizeof(cmdline), argv, argc, " ");
1952 WARNING("processes plugin: Command line did not fit into buffer.");
1956 } /* if (process has argument list) */
1958 memset(&pse, 0, sizeof(pse));
1959 pse.id = procs[i].p_pid;
1962 pse.num_lwp = 1; /* XXX: accumulate p_tid values for a single p_pid ? */
1964 pse.vmem_rss = procs[i].p_vm_rssize * pagesize;
1965 pse.vmem_data = procs[i].p_vm_dsize * pagesize;
1966 pse.vmem_code = procs[i].p_vm_tsize * pagesize;
1967 pse.stack_size = procs[i].p_vm_ssize * pagesize;
1968 pse.vmem_size = pse.stack_size + pse.vmem_code + pse.vmem_data;
1969 pse.vmem_minflt_counter = procs[i].p_uru_minflt;
1970 pse.vmem_majflt_counter = procs[i].p_uru_majflt;
1972 pse.cpu_user_counter =
1973 procs[i].p_uutime_usec + (1000000lu * procs[i].p_uutime_sec);
1974 pse.cpu_system_counter =
1975 procs[i].p_ustime_usec + (1000000lu * procs[i].p_ustime_sec);
1983 /* context switch counters not implemented */
1984 pse.cswitch_vol = -1;
1985 pse.cswitch_invol = -1;
1987 ps_list_add(procs[i].p_comm, have_cmdline ? cmdline : NULL, &pse);
1989 switch (procs[i].p_stat) {
2012 } /* if ((proc_ptr == NULL) || (proc_ptr->p_pid != procs[i].p_pid)) */
2017 ps_submit_state("running", running);
2018 ps_submit_state("sleeping", sleeping);
2019 ps_submit_state("zombies", zombies);
2020 ps_submit_state("stopped", stopped);
2021 ps_submit_state("onproc", onproc);
2022 ps_submit_state("idle", idle);
2023 ps_submit_state("dead", dead);
2025 for (procstat_t *ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
2026 ps_submit_proc_list(ps_ptr);
2027 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_OPENBSD */
2029 #elif HAVE_PROCINFO_H
2041 process_entry_t pse;
2044 while ((nprocs = getprocs64(procentry, sizeof(struct procentry64),
2045 /* fdsinfo = */ NULL, sizeof(struct fdsinfo64),
2046 &pindex, MAXPROCENTRY)) > 0) {
2047 for (int i = 0; i < nprocs; i++) {
2050 char arglist[MAXARGLN + 1];
2054 if (procentry[i].pi_state == SNONE)
2056 /* if (procentry[i].pi_state == SZOMB) FIXME */
2058 cmdline = procentry[i].pi_comm;
2059 cargs = procentry[i].pi_comm;
2060 if (procentry[i].pi_flags & SKPROC) {
2061 if (procentry[i].pi_pid == 0)
2062 cmdline = "swapper";
2065 if (getargs(&procentry[i], sizeof(struct procentry64), arglist,
2070 while (++n < MAXARGLN) {
2071 if (arglist[n] == '\0') {
2072 if (arglist[n + 1] == '\0')
2081 memset(&pse, 0, sizeof(pse));
2083 pse.id = procentry[i].pi_pid;
2084 pse.num_lwp = procentry[i].pi_thcount;
2088 while ((nthreads = getthrds64(procentry[i].pi_pid, thrdentry,
2089 sizeof(struct thrdentry64), &thindex,
2090 MAXTHRDENTRY)) > 0) {
2093 for (j = 0; j < nthreads; j++) {
2094 switch (thrdentry[j].ti_state) {
2095 /* case TSNONE: break; */
2098 break; /* FIXME is really blocked */
2116 if (nthreads < MAXTHRDENTRY)
2120 /* tv_usec is nanosec ??? */
2121 pse.cpu_user_counter = procentry[i].pi_ru.ru_utime.tv_sec * 1000000 +
2122 procentry[i].pi_ru.ru_utime.tv_usec / 1000;
2124 /* tv_usec is nanosec ??? */
2125 pse.cpu_system_counter = procentry[i].pi_ru.ru_stime.tv_sec * 1000000 +
2126 procentry[i].pi_ru.ru_stime.tv_usec / 1000;
2128 pse.vmem_minflt_counter = procentry[i].pi_minflt;
2129 pse.vmem_majflt_counter = procentry[i].pi_majflt;
2131 pse.vmem_size = procentry[i].pi_tsize + procentry[i].pi_dvm * pagesize;
2132 pse.vmem_rss = (procentry[i].pi_drss + procentry[i].pi_trss) * pagesize;
2143 pse.cswitch_vol = -1;
2144 pse.cswitch_invol = -1;
2146 ps_list_add(cmdline, cargs, &pse);
2147 } /* for (i = 0 .. nprocs) */
2149 if (nprocs < MAXPROCENTRY)
2151 } /* while (getprocs64() > 0) */
2152 ps_submit_state("running", running);
2153 ps_submit_state("sleeping", sleeping);
2154 ps_submit_state("zombies", zombies);
2155 ps_submit_state("stopped", stopped);
2156 ps_submit_state("paging", paging);
2157 ps_submit_state("blocked", blocked);
2159 for (procstat_t *ps = list_head_g; ps != NULL; ps = ps->next)
2160 ps_submit_proc_list(ps);
2161 /* #endif HAVE_PROCINFO_H */
2163 #elif KERNEL_SOLARIS
2165 * The Solaris section adds a few more process states and removes some
2166 * process states compared to linux. Most notably there is no "PAGING"
2167 * and "BLOCKED" state for a process. The rest is similar to the linux
2185 char cmdline[PRARGSZ];
2189 proc = opendir("/proc");
2193 while ((ent = readdir(proc)) != NULL) {
2195 process_entry_t pse;
2198 if (!isdigit((int)ent->d_name[0]))
2201 pid = strtol(ent->d_name, &endptr, 10);
2202 if (*endptr != 0) /* value didn't completely parse as a number */
2205 memset(&pse, 0, sizeof(pse));
2208 status = ps_read_process(pid, &pse, &state);
2210 DEBUG("ps_read_process failed: %i", status);
2241 ps_list_add(pse.name,
2242 ps_get_cmdline(pid, pse.name, cmdline, sizeof(cmdline)), &pse);
2243 } /* while(readdir) */
2246 ps_submit_state("running", running);
2247 ps_submit_state("sleeping", sleeping);
2248 ps_submit_state("zombies", zombies);
2249 ps_submit_state("stopped", stopped);
2250 ps_submit_state("detached", detached);
2251 ps_submit_state("daemon", daemon);
2252 ps_submit_state("system", system);
2253 ps_submit_state("orphan", orphan);
2255 for (procstat_t *ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
2256 ps_submit_proc_list(ps_ptr);
2259 #endif /* KERNEL_SOLARIS */
2266 void module_register(void) {
2267 plugin_register_complex_config("processes", ps_config);
2268 plugin_register_init("processes", ps_init);
2269 plugin_register_read("processes", ps_read);
2270 } /* void module_register */