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;
203 struct procstat_entry_s *next;
206 typedef struct procstat {
207 char name[PROCSTAT_NAME_LEN];
212 unsigned long num_proc;
213 unsigned long num_lwp;
214 unsigned long vmem_size;
215 unsigned long vmem_rss;
216 unsigned long vmem_data;
217 unsigned long vmem_code;
218 unsigned long stack_size;
220 derive_t vmem_minflt_counter;
221 derive_t vmem_majflt_counter;
223 derive_t cpu_user_counter;
224 derive_t cpu_system_counter;
232 derive_t cswitch_vol;
233 derive_t cswitch_invol;
235 struct procstat *next;
236 struct procstat_entry_s *instances;
239 static procstat_t *list_head_g = NULL;
241 static _Bool want_init = 1;
242 static _Bool report_ctx_switch = 0;
245 static mach_port_t port_host_self;
246 static mach_port_t port_task_self;
248 static processor_set_name_array_t pset_list;
249 static mach_msg_type_number_t pset_list_len;
250 /* #endif HAVE_THREAD_INFO */
253 static long pagesize_g;
254 static void ps_fill_details(const procstat_t *ps, process_entry_t *entry);
255 /* #endif KERNEL_LINUX */
257 #elif HAVE_LIBKVM_GETPROCS && \
258 (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD)
260 /* #endif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD ||
261 * HAVE_STRUCT_KINFO_PROC_OPENBSD) */
263 #elif HAVE_PROCINFO_H
264 static struct procentry64 procentry[MAXPROCENTRY];
265 static struct thrdentry64 thrdentry[MAXTHRDENTRY];
268 #ifndef _AIXVERSION_610
269 int getprocs64(void *procsinfo, int sizproc, void *fdsinfo, int sizfd,
270 pid_t *index, int count);
271 int getthrds64(pid_t, void *, int, tid64_t *, int);
273 int getargs(void *processBuffer, int bufferLen, char *argsBuffer, int argsLen);
274 #endif /* HAVE_PROCINFO_H */
276 /* put name of process from config to list_head_g tree
277 * list_head_g is a list of 'procstat_t' structs with
278 * processes names we want to watch */
279 static void ps_list_register(const char *name, const char *regexp) {
284 new = calloc(1, sizeof(*new));
286 ERROR("processes plugin: ps_list_register: calloc failed.");
289 sstrncpy(new->name, name, sizeof(new->name));
292 if (regexp != NULL) {
293 DEBUG("ProcessMatch: adding \"%s\" as criteria to process %s.", regexp,
295 new->re = malloc(sizeof(*new->re));
296 if (new->re == NULL) {
297 ERROR("processes plugin: ps_list_register: malloc failed.");
302 status = regcomp(new->re, regexp, REG_EXTENDED | REG_NOSUB);
304 DEBUG("ProcessMatch: compiling the regular expression \"%s\" failed.",
312 if (regexp != NULL) {
313 ERROR("processes plugin: ps_list_register: "
314 "Regular expression \"%s\" found in config "
315 "file, but support for regular expressions "
316 "has been disabled at compile time.",
323 for (ptr = list_head_g; ptr != NULL; ptr = ptr->next) {
324 if (strcmp(ptr->name, name) == 0) {
325 WARNING("processes plugin: You have configured more "
326 "than one `Process' or "
327 "`ProcessMatch' with the same name. "
328 "All but the first setting will be "
337 if (ptr->next == NULL)
345 } /* void ps_list_register */
347 /* try to match name against entry, returns 1 if success */
348 static int ps_list_match(const char *name, const char *cmdline,
351 if (ps->re != NULL) {
356 if ((str == NULL) || (str[0] == 0))
361 status = regexec(ps->re, str,
369 if (strcmp(ps->name, name) == 0)
373 } /* int ps_list_match */
375 static void ps_update_counter(derive_t *group_counter, derive_t *curr_counter,
376 derive_t new_counter) {
377 unsigned long curr_value;
380 *curr_counter = new_counter;
384 if (new_counter < *curr_counter)
385 curr_value = new_counter + (ULONG_MAX - *curr_counter);
387 curr_value = new_counter - *curr_counter;
389 *curr_counter = new_counter;
390 *group_counter += curr_value;
393 /* add process entry to 'instances' of process 'name' (or refresh it) */
394 static void ps_list_add(const char *name, const char *cmdline,
395 process_entry_t *entry) {
396 procstat_entry_t *pse;
401 for (procstat_t *ps = list_head_g; ps != NULL; ps = ps->next) {
402 if ((ps_list_match(name, cmdline, ps)) == 0)
406 ps_fill_details(ps, entry);
409 for (pse = ps->instances; pse != NULL; pse = pse->next)
410 if ((pse->id == entry->id) || (pse->next == NULL))
413 if ((pse == NULL) || (pse->id != entry->id)) {
414 procstat_entry_t *new;
416 new = calloc(1, sizeof(*new));
431 ps->num_proc += entry->num_proc;
432 ps->num_lwp += entry->num_lwp;
433 ps->vmem_size += entry->vmem_size;
434 ps->vmem_rss += entry->vmem_rss;
435 ps->vmem_data += entry->vmem_data;
436 ps->vmem_code += entry->vmem_code;
437 ps->stack_size += entry->stack_size;
439 ps->io_rchar += ((entry->io_rchar == -1) ? 0 : entry->io_rchar);
440 ps->io_wchar += ((entry->io_wchar == -1) ? 0 : entry->io_wchar);
441 ps->io_syscr += ((entry->io_syscr == -1) ? 0 : entry->io_syscr);
442 ps->io_syscw += ((entry->io_syscw == -1) ? 0 : entry->io_syscw);
444 ps->cswitch_vol += ((entry->cswitch_vol == -1) ? 0 : entry->cswitch_vol);
446 ((entry->cswitch_invol == -1) ? 0 : entry->cswitch_invol);
448 ps_update_counter(&ps->vmem_minflt_counter, &pse->vmem_minflt_counter,
449 entry->vmem_minflt_counter);
450 ps_update_counter(&ps->vmem_majflt_counter, &pse->vmem_majflt_counter,
451 entry->vmem_majflt_counter);
453 ps_update_counter(&ps->cpu_user_counter, &pse->cpu_user_counter,
454 entry->cpu_user_counter);
455 ps_update_counter(&ps->cpu_system_counter, &pse->cpu_system_counter,
456 entry->cpu_system_counter);
460 /* remove old entries from instances of processes in list_head_g */
461 static void ps_list_reset(void) {
462 procstat_entry_t *pse;
463 procstat_entry_t *pse_prev;
465 for (procstat_t *ps = list_head_g; ps != NULL; ps = ps->next) {
477 ps->cswitch_vol = -1;
478 ps->cswitch_invol = -1;
482 while (pse != NULL) {
484 DEBUG("Removing this procstat entry cause it's too old: "
485 "id = %lu; name = %s;",
488 if (pse_prev == NULL) {
489 ps->instances = pse->next;
493 pse_prev->next = pse->next;
495 pse = pse_prev->next;
502 } /* while (pse != NULL) */
503 } /* for (ps = list_head_g; ps != NULL; ps = ps->next) */
506 /* put all pre-defined 'Process' names from config to list_head_g tree */
507 static int ps_config(oconfig_item_t *ci) {
509 const size_t max_procname_len = 15;
510 #elif KERNEL_SOLARIS || KERNEL_FREEBSD
511 const size_t max_procname_len = MAXCOMLEN - 1;
514 for (int i = 0; i < ci->children_num; ++i) {
515 oconfig_item_t *c = ci->children + i;
517 if (strcasecmp(c->key, "Process") == 0) {
518 if ((c->values_num != 1) || (OCONFIG_TYPE_STRING != c->values[0].type)) {
519 ERROR("processes plugin: `Process' expects exactly "
520 "one string argument (got %i).",
525 if (c->children_num != 0) {
526 WARNING("processes plugin: the `Process' config option "
527 "does not expect any child elements -- ignoring "
528 "content (%i elements) of the <Process '%s'> block.",
529 c->children_num, c->values[0].value.string);
532 #if KERNEL_LINUX || KERNEL_SOLARIS || KERNEL_FREEBSD
533 if (strlen(c->values[0].value.string) > max_procname_len) {
534 WARNING("processes plugin: this platform has a %zu character limit "
535 "to process names. The `Process \"%s\"' option will "
536 "not work as expected.",
537 max_procname_len, c->values[0].value.string);
541 ps_list_register(c->values[0].value.string, NULL);
542 } else if (strcasecmp(c->key, "ProcessMatch") == 0) {
543 if ((c->values_num != 2) || (OCONFIG_TYPE_STRING != c->values[0].type) ||
544 (OCONFIG_TYPE_STRING != c->values[1].type)) {
545 ERROR("processes plugin: `ProcessMatch' needs exactly "
546 "two string arguments (got %i).",
551 if (c->children_num != 0) {
552 WARNING("processes plugin: the `ProcessMatch' config option "
553 "does not expect any child elements -- ignoring "
554 "content (%i elements) of the <ProcessMatch '%s' '%s'> "
556 c->children_num, c->values[0].value.string,
557 c->values[1].value.string);
560 ps_list_register(c->values[0].value.string, c->values[1].value.string);
561 } else if (strcasecmp(c->key, "CollectContextSwitch") == 0) {
562 cf_util_get_boolean(c, &report_ctx_switch);
564 ERROR("processes plugin: The `%s' configuration option is not "
565 "understood and will be ignored.",
574 static int ps_init(void) {
576 kern_return_t status;
578 port_host_self = mach_host_self();
579 port_task_self = mach_task_self();
581 if (pset_list != NULL) {
582 vm_deallocate(port_task_self, (vm_address_t)pset_list,
583 pset_list_len * sizeof(processor_set_t));
588 if ((status = host_processor_sets(port_host_self, &pset_list,
589 &pset_list_len)) != KERN_SUCCESS) {
590 ERROR("host_processor_sets failed: %s\n", mach_error_string(status));
595 /* #endif HAVE_THREAD_INFO */
598 pagesize_g = sysconf(_SC_PAGESIZE);
599 DEBUG("pagesize_g = %li; CONFIG_HZ = %i;", pagesize_g, CONFIG_HZ);
600 /* #endif KERNEL_LINUX */
602 #elif HAVE_LIBKVM_GETPROCS && \
603 (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD)
604 pagesize = getpagesize();
605 /* #endif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD ||
606 * HAVE_STRUCT_KINFO_PROC_OPENBSD) */
608 #elif HAVE_PROCINFO_H
609 pagesize = getpagesize();
610 #endif /* HAVE_PROCINFO_H */
615 /* submit global state (e.g.: qty of zombies, running, etc..) */
616 static void ps_submit_state(const char *state, double value) {
617 value_list_t vl = VALUE_LIST_INIT;
619 vl.values = &(value_t){.gauge = value};
621 sstrncpy(vl.plugin, "processes", sizeof(vl.plugin));
622 sstrncpy(vl.plugin_instance, "", sizeof(vl.plugin_instance));
623 sstrncpy(vl.type, "ps_state", sizeof(vl.type));
624 sstrncpy(vl.type_instance, state, sizeof(vl.type_instance));
626 plugin_dispatch_values(&vl);
629 /* submit info about specific process (e.g.: memory taken, cpu usage, etc..) */
630 static void ps_submit_proc_list(procstat_t *ps) {
631 value_list_t vl = VALUE_LIST_INIT;
635 sstrncpy(vl.plugin, "processes", sizeof(vl.plugin));
636 sstrncpy(vl.plugin_instance, ps->name, sizeof(vl.plugin_instance));
638 sstrncpy(vl.type, "ps_vm", sizeof(vl.type));
639 vl.values[0].gauge = ps->vmem_size;
641 plugin_dispatch_values(&vl);
643 sstrncpy(vl.type, "ps_rss", sizeof(vl.type));
644 vl.values[0].gauge = ps->vmem_rss;
646 plugin_dispatch_values(&vl);
648 sstrncpy(vl.type, "ps_data", sizeof(vl.type));
649 vl.values[0].gauge = ps->vmem_data;
651 plugin_dispatch_values(&vl);
653 sstrncpy(vl.type, "ps_code", sizeof(vl.type));
654 vl.values[0].gauge = ps->vmem_code;
656 plugin_dispatch_values(&vl);
658 sstrncpy(vl.type, "ps_stacksize", sizeof(vl.type));
659 vl.values[0].gauge = ps->stack_size;
661 plugin_dispatch_values(&vl);
663 sstrncpy(vl.type, "ps_cputime", sizeof(vl.type));
664 vl.values[0].derive = ps->cpu_user_counter;
665 vl.values[1].derive = ps->cpu_system_counter;
667 plugin_dispatch_values(&vl);
669 sstrncpy(vl.type, "ps_count", sizeof(vl.type));
670 vl.values[0].gauge = ps->num_proc;
671 vl.values[1].gauge = ps->num_lwp;
673 plugin_dispatch_values(&vl);
675 sstrncpy(vl.type, "ps_pagefaults", sizeof(vl.type));
676 vl.values[0].derive = ps->vmem_minflt_counter;
677 vl.values[1].derive = ps->vmem_majflt_counter;
679 plugin_dispatch_values(&vl);
681 if ((ps->io_rchar != -1) && (ps->io_wchar != -1)) {
682 sstrncpy(vl.type, "ps_disk_octets", sizeof(vl.type));
683 vl.values[0].derive = ps->io_rchar;
684 vl.values[1].derive = ps->io_wchar;
686 plugin_dispatch_values(&vl);
689 if ((ps->io_syscr != -1) && (ps->io_syscw != -1)) {
690 sstrncpy(vl.type, "ps_disk_ops", sizeof(vl.type));
691 vl.values[0].derive = ps->io_syscr;
692 vl.values[1].derive = ps->io_syscw;
694 plugin_dispatch_values(&vl);
697 if (report_ctx_switch) {
698 sstrncpy(vl.type, "contextswitch", sizeof(vl.type));
699 sstrncpy(vl.type_instance, "voluntary", sizeof(vl.type_instance));
700 vl.values[0].derive = ps->cswitch_vol;
702 plugin_dispatch_values(&vl);
704 sstrncpy(vl.type, "contextswitch", sizeof(vl.type));
705 sstrncpy(vl.type_instance, "involuntary", sizeof(vl.type_instance));
706 vl.values[0].derive = ps->cswitch_invol;
708 plugin_dispatch_values(&vl);
711 DEBUG("name = %s; num_proc = %lu; num_lwp = %lu; "
712 "vmem_size = %lu; vmem_rss = %lu; vmem_data = %lu; "
714 "vmem_minflt_counter = %" PRIi64 "; vmem_majflt_counter = %" PRIi64 "; "
715 "cpu_user_counter = %" PRIi64 "; cpu_system_counter = %" PRIi64 "; "
716 "io_rchar = %" PRIi64 "; io_wchar = %" PRIi64 "; "
717 "io_syscr = %" PRIi64 "; io_syscw = %" PRIi64 "; "
718 "cswitch_vol = %" PRIi64 "; cswitch_invol = %" PRIi64 ";",
719 ps->name, ps->num_proc, ps->num_lwp, ps->vmem_size, ps->vmem_rss,
720 ps->vmem_data, ps->vmem_code, ps->vmem_minflt_counter,
721 ps->vmem_majflt_counter, ps->cpu_user_counter, ps->cpu_system_counter,
722 ps->io_rchar, ps->io_wchar, ps->io_syscr, ps->io_syscw, ps->cswitch_vol,
724 } /* void ps_submit_proc_list */
726 #if KERNEL_LINUX || KERNEL_SOLARIS
727 static void ps_submit_fork_rate(derive_t value) {
728 value_list_t vl = VALUE_LIST_INIT;
730 vl.values = &(value_t){.derive = value};
732 sstrncpy(vl.plugin, "processes", sizeof(vl.plugin));
733 sstrncpy(vl.plugin_instance, "", sizeof(vl.plugin_instance));
734 sstrncpy(vl.type, "fork_rate", sizeof(vl.type));
735 sstrncpy(vl.type_instance, "", sizeof(vl.type_instance));
737 plugin_dispatch_values(&vl);
739 #endif /* KERNEL_LINUX || KERNEL_SOLARIS*/
741 /* ------- additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
743 static int ps_read_tasks_status(process_entry_t *ps) {
749 derive_t cswitch_vol = 0;
750 derive_t cswitch_invol = 0;
755 ssnprintf(dirname, sizeof(dirname), "/proc/%li/task", ps->id);
757 if ((dh = opendir(dirname)) == NULL) {
758 DEBUG("Failed to open directory `%s'", dirname);
762 while ((ent = readdir(dh)) != NULL) {
765 if (!isdigit((int)ent->d_name[0]))
770 ssnprintf(filename, sizeof(filename), "/proc/%li/task/%s/status", ps->id,
772 if ((fh = fopen(filename, "r")) == NULL) {
773 DEBUG("Failed to open file `%s'", filename);
777 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
781 if (strncmp(buffer, "voluntary_ctxt_switches", 23) != 0 &&
782 strncmp(buffer, "nonvoluntary_ctxt_switches", 26) != 0)
785 numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
792 tmp = (derive_t)strtoll(fields[1], &endptr, /* base = */ 10);
793 if ((errno == 0) && (endptr != fields[1])) {
794 if (strncmp(buffer, "voluntary_ctxt_switches", 23) == 0) {
796 } else if (strncmp(buffer, "nonvoluntary_ctxt_switches", 26) == 0) {
797 cswitch_invol += tmp;
800 } /* while (fgets) */
804 WARNING("processes: fclose: %s",
805 sstrerror(errno, errbuf, sizeof(errbuf)));
810 ps->cswitch_vol = cswitch_vol;
811 ps->cswitch_invol = cswitch_invol;
814 } /* int *ps_read_tasks_status */
816 /* Read data from /proc/pid/status */
817 static int ps_read_status(long pid, process_entry_t *ps) {
821 unsigned long lib = 0;
822 unsigned long exe = 0;
823 unsigned long data = 0;
824 unsigned long threads = 0;
828 ssnprintf(filename, sizeof(filename), "/proc/%li/status", pid);
829 if ((fh = fopen(filename, "r")) == NULL)
832 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
836 if (strncmp(buffer, "Vm", 2) != 0 && strncmp(buffer, "Threads", 7) != 0)
839 numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
846 tmp = strtoul(fields[1], &endptr, /* base = */ 10);
847 if ((errno == 0) && (endptr != fields[1])) {
848 if (strncmp(buffer, "VmData", 6) == 0) {
850 } else if (strncmp(buffer, "VmLib", 5) == 0) {
852 } else if (strncmp(buffer, "VmExe", 5) == 0) {
854 } else if (strncmp(buffer, "Threads", 7) == 0) {
858 } /* while (fgets) */
862 WARNING("processes: fclose: %s", sstrerror(errno, errbuf, sizeof(errbuf)));
865 ps->vmem_data = data * 1024;
866 ps->vmem_code = (exe + lib) * 1024;
868 ps->num_lwp = threads;
871 } /* int *ps_read_status */
873 static int ps_read_io(process_entry_t *ps) {
881 ssnprintf(filename, sizeof(filename), "/proc/%li/io", ps->id);
882 if ((fh = fopen(filename, "r")) == NULL)
885 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
886 derive_t *val = NULL;
890 if (strncasecmp(buffer, "rchar:", 6) == 0)
891 val = &(ps->io_rchar);
892 else if (strncasecmp(buffer, "wchar:", 6) == 0)
893 val = &(ps->io_wchar);
894 else if (strncasecmp(buffer, "syscr:", 6) == 0)
895 val = &(ps->io_syscr);
896 else if (strncasecmp(buffer, "syscw:", 6) == 0)
897 val = &(ps->io_syscw);
901 numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
908 tmp = strtoll(fields[1], &endptr, /* base = */ 10);
909 if ((errno != 0) || (endptr == fields[1]))
912 *val = (derive_t)tmp;
913 } /* while (fgets) */
917 WARNING("processes: fclose: %s", sstrerror(errno, errbuf, sizeof(errbuf)));
920 } /* int ps_read_io (...) */
922 static void ps_fill_details(const procstat_t *ps, process_entry_t *entry) {
923 if (entry->has_io == 0 && ps_read_io(entry) != 0) {
925 entry->io_rchar = -1;
926 entry->io_wchar = -1;
927 entry->io_syscr = -1;
928 entry->io_syscw = -1;
930 DEBUG("ps_read_io: not get io data for pid %li", entry->id);
934 if (report_ctx_switch) {
935 if (entry->has_cswitch == 0 && ps_read_tasks_status(entry) != 0) {
936 entry->cswitch_vol = -1;
937 entry->cswitch_invol = -1;
939 DEBUG("ps_read_tasks_status: not get context "
940 "switch data for pid %li",
943 entry->has_cswitch = 1;
945 } /* void ps_fill_details (...) */
947 static int ps_read_process(long pid, process_entry_t *ps, char *state) {
957 size_t name_start_pos;
961 derive_t cpu_user_counter;
962 derive_t cpu_system_counter;
963 long long unsigned vmem_size;
964 long long unsigned vmem_rss;
965 long long unsigned stack_size;
969 ssnprintf(filename, sizeof(filename), "/proc/%li/stat", pid);
971 status = read_file_contents(filename, buffer, sizeof(buffer) - 1);
974 buffer_len = (size_t)status;
975 buffer[buffer_len] = 0;
977 /* The name of the process is enclosed in parens. Since the name can
978 * contain parens itself, spaces, numbers and pretty much everything
979 * else, use these to determine the process name. We don't use
980 * strchr(3) and strrchr(3) to avoid pointer arithmetic which would
981 * otherwise be required to determine name_len. */
983 while (name_start_pos < buffer_len && buffer[name_start_pos] != '(')
986 name_end_pos = buffer_len;
987 while (name_end_pos > 0 && buffer[name_end_pos] != ')')
990 /* Either '(' or ')' is not found or they are in the wrong order.
991 * Anyway, something weird that shouldn't happen ever. */
992 if (name_start_pos >= name_end_pos) {
993 ERROR("processes plugin: name_start_pos = %zu >= name_end_pos = %zu",
994 name_start_pos, name_end_pos);
998 name_len = (name_end_pos - name_start_pos) - 1;
999 if (name_len >= sizeof(ps->name))
1000 name_len = sizeof(ps->name) - 1;
1002 sstrncpy(ps->name, &buffer[name_start_pos + 1], name_len + 1);
1004 if ((buffer_len - name_end_pos) < 2)
1006 buffer_ptr = &buffer[name_end_pos + 2];
1008 fields_len = strsplit(buffer_ptr, fields, STATIC_ARRAY_SIZE(fields));
1009 if (fields_len < 22) {
1010 DEBUG("processes plugin: ps_read_process (pid = %li):"
1011 " `%s' has only %i fields..",
1012 pid, filename, fields_len);
1016 *state = fields[0][0];
1018 if (*state == 'Z') {
1022 ps->num_lwp = strtoul(fields[17], /* endptr = */ NULL, /* base = */ 10);
1023 if ((ps_read_status(pid, ps)) != 0) {
1027 DEBUG("ps_read_process: did not get vmem data for pid %li", pid);
1029 if (ps->num_lwp == 0)
1034 /* Leave the rest at zero if this is only a zombi */
1035 if (ps->num_proc == 0) {
1036 DEBUG("processes plugin: This is only a zombie: pid = %li; "
1042 cpu_user_counter = atoll(fields[11]);
1043 cpu_system_counter = atoll(fields[12]);
1044 vmem_size = atoll(fields[20]);
1045 vmem_rss = atoll(fields[21]);
1046 ps->vmem_minflt_counter = atol(fields[7]);
1047 ps->vmem_majflt_counter = atol(fields[9]);
1050 unsigned long long stack_start = atoll(fields[25]);
1051 unsigned long long stack_ptr = atoll(fields[26]);
1053 stack_size = (stack_start > stack_ptr) ? stack_start - stack_ptr
1054 : stack_ptr - stack_start;
1057 /* Convert jiffies to useconds */
1058 cpu_user_counter = cpu_user_counter * 1000000 / CONFIG_HZ;
1059 cpu_system_counter = cpu_system_counter * 1000000 / CONFIG_HZ;
1060 vmem_rss = vmem_rss * pagesize_g;
1062 ps->cpu_user_counter = cpu_user_counter;
1063 ps->cpu_system_counter = cpu_system_counter;
1064 ps->vmem_size = (unsigned long)vmem_size;
1065 ps->vmem_rss = (unsigned long)vmem_rss;
1066 ps->stack_size = (unsigned long)stack_size;
1070 } /* int ps_read_process (...) */
1072 static char *ps_get_cmdline(long pid, char *name, char *buf, size_t buf_len) {
1076 char file[PATH_MAX];
1081 if ((pid < 1) || (NULL == buf) || (buf_len < 2))
1084 ssnprintf(file, sizeof(file), "/proc/%li/cmdline", pid);
1087 fd = open(file, O_RDONLY);
1090 /* ENOENT means the process exited while we were handling it.
1091 * Don't complain about this, it only fills the logs. */
1092 if (errno != ENOENT)
1093 WARNING("processes plugin: Failed to open `%s': %s.", file,
1094 sstrerror(errno, errbuf, sizeof(errbuf)));
1106 status = read(fd, (void *)buf_ptr, len);
1111 if ((EAGAIN == errno) || (EINTR == errno))
1114 WARNING("processes plugin: Failed to read from `%s': %s.", file,
1115 sstrerror(errno, errbuf, sizeof(errbuf)));
1135 /* cmdline not available; e.g. kernel thread, zombie */
1139 ssnprintf(buf, buf_len, "[%s]", name);
1143 assert(n <= buf_len);
1150 /* remove trailing whitespace */
1151 while ((n > 0) && (isspace(buf[n]) || ('\0' == buf[n]))) {
1156 /* arguments are separated by '\0' in /proc/<pid>/cmdline */
1163 } /* char *ps_get_cmdline (...) */
1165 static int read_fork_rate(void) {
1169 _Bool value_valid = 0;
1171 proc_stat = fopen("/proc/stat", "r");
1172 if (proc_stat == NULL) {
1174 ERROR("processes plugin: fopen (/proc/stat) failed: %s",
1175 sstrerror(errno, errbuf, sizeof(errbuf)));
1179 while (fgets(buffer, sizeof(buffer), proc_stat) != NULL) {
1184 fields_num = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
1185 if (fields_num != 2)
1188 if (strcmp("processes", fields[0]) != 0)
1191 status = parse_value(fields[1], &value, DS_TYPE_DERIVE);
1202 ps_submit_fork_rate(value.derive);
1205 #endif /*KERNEL_LINUX */
1208 static char *ps_get_cmdline(long pid,
1209 char *name __attribute__((unused)), /* {{{ */
1210 char *buffer, size_t buffer_size) {
1211 char path[PATH_MAX];
1215 snprintf(path, sizeof(path), "/proc/%li/psinfo", pid);
1217 status = read_file_contents(path, (void *)&info, sizeof(info));
1218 if ((status < 0) || (((size_t)status) != sizeof(info))) {
1219 ERROR("processes plugin: Unexpected return value "
1220 "while reading \"%s\": "
1221 "Returned %zd but expected %zu.",
1222 path, status, buffer_size);
1226 info.pr_psargs[sizeof(info.pr_psargs) - 1] = 0;
1227 sstrncpy(buffer, info.pr_psargs, buffer_size);
1230 } /* }}} int ps_get_cmdline */
1233 * Reads process information on the Solaris OS. The information comes mainly
1235 * /proc/PID/status, /proc/PID/psinfo and /proc/PID/usage
1236 * The values for input and ouput chars are calculated "by hand"
1237 * Added a few "solaris" specific process states as well
1239 static int ps_read_process(long pid, process_entry_t *ps, char *state) {
1241 char f_psinfo[64], f_usage[64];
1244 pstatus_t *myStatus;
1248 snprintf(filename, sizeof(filename), "/proc/%li/status", pid);
1249 snprintf(f_psinfo, sizeof(f_psinfo), "/proc/%li/psinfo", pid);
1250 snprintf(f_usage, sizeof(f_usage), "/proc/%li/usage", pid);
1252 buffer = calloc(1, sizeof(pstatus_t));
1253 read_file_contents(filename, buffer, sizeof(pstatus_t));
1254 myStatus = (pstatus_t *)buffer;
1256 buffer = calloc(1, sizeof(psinfo_t));
1257 read_file_contents(f_psinfo, buffer, sizeof(psinfo_t));
1258 myInfo = (psinfo_t *)buffer;
1260 buffer = calloc(1, sizeof(prusage_t));
1261 read_file_contents(f_usage, buffer, sizeof(prusage_t));
1262 myUsage = (prusage_t *)buffer;
1264 sstrncpy(ps->name, myInfo->pr_fname, sizeof(myInfo->pr_fname));
1265 ps->num_lwp = myStatus->pr_nlwp;
1266 if (myInfo->pr_wstat != 0) {
1277 ps->num_lwp = myInfo->pr_nlwp;
1281 * Convert system time and user time from nanoseconds to microseconds
1282 * for compatibility with the linux module
1284 ps->cpu_system_counter = myStatus->pr_stime.tv_nsec / 1000;
1285 ps->cpu_user_counter = myStatus->pr_utime.tv_nsec / 1000;
1288 * Convert rssize from KB to bytes to be consistent w/ the linux module
1290 ps->vmem_rss = myInfo->pr_rssize * 1024;
1291 ps->vmem_size = myInfo->pr_size * 1024;
1292 ps->vmem_minflt_counter = myUsage->pr_minf;
1293 ps->vmem_majflt_counter = myUsage->pr_majf;
1296 * TODO: Data and code segment calculations for Solaris
1301 ps->stack_size = myStatus->pr_stksize;
1304 * Calculating input/ouput chars
1305 * Formula used is total chars / total blocks => chars/block
1306 * then convert input/output blocks to chars
1308 ulong_t tot_chars = myUsage->pr_ioch;
1309 ulong_t tot_blocks = myUsage->pr_inblk + myUsage->pr_oublk;
1310 ulong_t chars_per_block = 1;
1311 if (tot_blocks != 0)
1312 chars_per_block = tot_chars / tot_blocks;
1313 ps->io_rchar = myUsage->pr_inblk * chars_per_block;
1314 ps->io_wchar = myUsage->pr_oublk * chars_per_block;
1315 ps->io_syscr = myUsage->pr_sysc;
1316 ps->io_syscw = myUsage->pr_sysc;
1319 * TODO: context switch counters for Solaris
1321 ps->cswitch_vol = -1;
1322 ps->cswitch_invol = -1;
1325 * TODO: Find way of setting BLOCKED and PAGING status
1329 if (myStatus->pr_flags & PR_ASLEEP)
1331 else if (myStatus->pr_flags & PR_STOPPED)
1333 else if (myStatus->pr_flags & PR_DETACH)
1335 else if (myStatus->pr_flags & PR_DAEMON)
1337 else if (myStatus->pr_flags & PR_ISSYS)
1339 else if (myStatus->pr_flags & PR_ORPHAN)
1350 * Reads the number of threads created since the last reboot. On Solaris these
1351 * are retrieved from kstat (module cpu, name sys, class misc, stat nthreads).
1352 * The result is the sum for all the threads created on each cpu
1354 static int read_fork_rate(void) {
1355 extern kstat_ctl_t *kc;
1356 derive_t result = 0;
1361 for (kstat_t *ksp_chain = kc->kc_chain; ksp_chain != NULL;
1362 ksp_chain = ksp_chain->ks_next) {
1363 if ((strcmp(ksp_chain->ks_module, "cpu") == 0) &&
1364 (strcmp(ksp_chain->ks_name, "sys") == 0) &&
1365 (strcmp(ksp_chain->ks_class, "misc") == 0)) {
1368 kstat_read(kc, ksp_chain, NULL);
1370 tmp = get_kstat_value(ksp_chain, "nthreads");
1376 ps_submit_fork_rate(result);
1379 #endif /* KERNEL_SOLARIS */
1381 #if HAVE_THREAD_INFO
1382 static int mach_get_task_name(task_t t, int *pid, char *name,
1383 size_t name_max_len) {
1386 struct kinfo_proc kp;
1391 mib[2] = KERN_PROC_PID;
1393 if (pid_for_task(t, pid) != KERN_SUCCESS)
1397 kp_size = sizeof(kp);
1398 if (sysctl(mib, 4, &kp, &kp_size, NULL, 0) != 0)
1401 if (name_max_len > (MAXCOMLEN + 1))
1402 name_max_len = MAXCOMLEN + 1;
1404 strncpy(name, kp.kp_proc.p_comm, name_max_len - 1);
1405 name[name_max_len - 1] = '\0';
1407 DEBUG("pid = %i; name = %s;", *pid, name);
1409 /* We don't do the special handling for `p_comm == "LaunchCFMApp"' as
1410 * `top' does it, because it is a lot of work and only used when
1411 * debugging. -octo */
1415 #endif /* HAVE_THREAD_INFO */
1416 /* ------- end of additional functions for KERNEL_LINUX/HAVE_THREAD_INFO -------
1419 /* do actual readings from kernel */
1420 static int ps_read(void) {
1421 #if HAVE_THREAD_INFO
1422 kern_return_t status;
1424 processor_set_t port_pset_priv;
1426 task_array_t task_list;
1427 mach_msg_type_number_t task_list_len;
1430 char task_name[MAXCOMLEN + 1];
1432 thread_act_array_t thread_list;
1433 mach_msg_type_number_t thread_list_len;
1434 thread_basic_info_data_t thread_data;
1435 mach_msg_type_number_t thread_data_len;
1444 process_entry_t pse;
1449 * The Mach-concept is a little different from the traditional UNIX
1450 * concept: All the work is done in threads. Threads are contained in
1451 * `tasks'. Therefore, `task status' doesn't make much sense, since
1452 * it's actually a `thread status'.
1453 * Tasks are assigned to sets of processors, so that's where you go to
1456 for (mach_msg_type_number_t pset = 0; pset < pset_list_len; pset++) {
1457 if ((status = host_processor_set_priv(port_host_self, pset_list[pset],
1458 &port_pset_priv)) != KERN_SUCCESS) {
1459 ERROR("host_processor_set_priv failed: %s\n", mach_error_string(status));
1463 if ((status = processor_set_tasks(port_pset_priv, &task_list,
1464 &task_list_len)) != KERN_SUCCESS) {
1465 ERROR("processor_set_tasks failed: %s\n", mach_error_string(status));
1466 mach_port_deallocate(port_task_self, port_pset_priv);
1470 for (mach_msg_type_number_t task = 0; task < task_list_len; task++) {
1472 if (mach_get_task_name(task_list[task], &task_pid, task_name,
1473 PROCSTAT_NAME_LEN) == 0) {
1474 /* search for at least one match */
1475 for (ps = list_head_g; ps != NULL; ps = ps->next)
1476 /* FIXME: cmdline should be here instead of NULL */
1477 if (ps_list_match(task_name, NULL, ps) == 1)
1481 /* Collect more detailed statistics for this process */
1483 task_basic_info_data_t task_basic_info;
1484 mach_msg_type_number_t task_basic_info_len;
1485 task_events_info_data_t task_events_info;
1486 mach_msg_type_number_t task_events_info_len;
1487 task_absolutetime_info_data_t task_absolutetime_info;
1488 mach_msg_type_number_t task_absolutetime_info_len;
1490 memset(&pse, '\0', sizeof(pse));
1493 task_basic_info_len = TASK_BASIC_INFO_COUNT;
1494 status = task_info(task_list[task], TASK_BASIC_INFO,
1495 (task_info_t)&task_basic_info, &task_basic_info_len);
1496 if (status != KERN_SUCCESS) {
1497 ERROR("task_info failed: %s", mach_error_string(status));
1498 continue; /* with next thread_list */
1501 task_events_info_len = TASK_EVENTS_INFO_COUNT;
1503 task_info(task_list[task], TASK_EVENTS_INFO,
1504 (task_info_t)&task_events_info, &task_events_info_len);
1505 if (status != KERN_SUCCESS) {
1506 ERROR("task_info failed: %s", mach_error_string(status));
1507 continue; /* with next thread_list */
1510 task_absolutetime_info_len = TASK_ABSOLUTETIME_INFO_COUNT;
1511 status = task_info(task_list[task], TASK_ABSOLUTETIME_INFO,
1512 (task_info_t)&task_absolutetime_info,
1513 &task_absolutetime_info_len);
1514 if (status != KERN_SUCCESS) {
1515 ERROR("task_info failed: %s", mach_error_string(status));
1516 continue; /* with next thread_list */
1520 pse.vmem_size = task_basic_info.virtual_size;
1521 pse.vmem_rss = task_basic_info.resident_size;
1522 /* Does not seem to be easily exposed */
1526 pse.vmem_minflt_counter = task_events_info.cow_faults;
1527 pse.vmem_majflt_counter = task_events_info.faults;
1529 pse.cpu_user_counter = task_absolutetime_info.total_user;
1530 pse.cpu_system_counter = task_absolutetime_info.total_system;
1532 /* context switch counters not implemented */
1533 pse.cswitch_vol = -1;
1534 pse.cswitch_invol = -1;
1537 status = task_threads(task_list[task], &thread_list, &thread_list_len);
1538 if (status != KERN_SUCCESS) {
1539 /* Apple's `top' treats this case a zombie. It
1540 * makes sense to some extend: A `zombie'
1541 * thread is nonsense, since the task/process
1544 DEBUG("task_threads failed: %s", mach_error_string(status));
1545 if (task_list[task] != port_task_self)
1546 mach_port_deallocate(port_task_self, task_list[task]);
1547 continue; /* with next task_list */
1550 for (mach_msg_type_number_t thread = 0; thread < thread_list_len;
1552 thread_data_len = THREAD_BASIC_INFO_COUNT;
1553 status = thread_info(thread_list[thread], THREAD_BASIC_INFO,
1554 (thread_info_t)&thread_data, &thread_data_len);
1555 if (status != KERN_SUCCESS) {
1556 ERROR("thread_info failed: %s", mach_error_string(status));
1557 if (task_list[task] != port_task_self)
1558 mach_port_deallocate(port_task_self, thread_list[thread]);
1559 continue; /* with next thread_list */
1565 switch (thread_data.run_state) {
1566 case TH_STATE_RUNNING:
1569 case TH_STATE_STOPPED:
1570 /* What exactly is `halted'? */
1571 case TH_STATE_HALTED:
1574 case TH_STATE_WAITING:
1577 case TH_STATE_UNINTERRUPTIBLE:
1580 /* There is no `zombie' case here,
1581 * since there are no zombie-threads.
1582 * There's only zombie tasks, which are
1585 WARNING("Unknown thread status: %i", thread_data.run_state);
1587 } /* switch (thread_data.run_state) */
1589 if (task_list[task] != port_task_self) {
1590 status = mach_port_deallocate(port_task_self, thread_list[thread]);
1591 if (status != KERN_SUCCESS)
1592 ERROR("mach_port_deallocate failed: %s", mach_error_string(status));
1594 } /* for (thread_list) */
1596 if ((status = vm_deallocate(port_task_self, (vm_address_t)thread_list,
1597 thread_list_len * sizeof(thread_act_t))) !=
1599 ERROR("vm_deallocate failed: %s", mach_error_string(status));
1602 thread_list_len = 0;
1604 /* Only deallocate the task port, if it isn't our own.
1605 * Don't know what would happen in that case, but this
1606 * is what Apple's top does.. ;) */
1607 if (task_list[task] != port_task_self) {
1608 status = mach_port_deallocate(port_task_self, task_list[task]);
1609 if (status != KERN_SUCCESS)
1610 ERROR("mach_port_deallocate failed: %s", mach_error_string(status));
1614 /* FIXME: cmdline should be here instead of NULL */
1615 ps_list_add(task_name, NULL, &pse);
1616 } /* for (task_list) */
1618 if ((status = vm_deallocate(port_task_self, (vm_address_t)task_list,
1619 task_list_len * sizeof(task_t))) !=
1621 ERROR("vm_deallocate failed: %s", mach_error_string(status));
1626 if ((status = mach_port_deallocate(port_task_self, port_pset_priv)) !=
1628 ERROR("mach_port_deallocate failed: %s", mach_error_string(status));
1630 } /* for (pset_list) */
1632 ps_submit_state("running", running);
1633 ps_submit_state("sleeping", sleeping);
1634 ps_submit_state("zombies", zombies);
1635 ps_submit_state("stopped", stopped);
1636 ps_submit_state("blocked", blocked);
1638 for (ps = list_head_g; ps != NULL; ps = ps->next)
1639 ps_submit_proc_list(ps);
1640 /* #endif HAVE_THREAD_INFO */
1654 char cmdline[CMDLINE_BUFFER_SIZE];
1657 process_entry_t pse;
1660 running = sleeping = zombies = stopped = paging = blocked = 0;
1663 if ((proc = opendir("/proc")) == NULL) {
1665 ERROR("Cannot open `/proc': %s", sstrerror(errno, errbuf, sizeof(errbuf)));
1669 while ((ent = readdir(proc)) != NULL) {
1670 if (!isdigit(ent->d_name[0]))
1673 if ((pid = atol(ent->d_name)) < 1)
1676 memset(&pse, 0, sizeof(pse));
1679 status = ps_read_process(pid, &pse, &state);
1681 DEBUG("ps_read_process failed: %i", status);
1706 ps_list_add(pse.name,
1707 ps_get_cmdline(pid, pse.name, cmdline, sizeof(cmdline)), &pse);
1712 ps_submit_state("running", running);
1713 ps_submit_state("sleeping", sleeping);
1714 ps_submit_state("zombies", zombies);
1715 ps_submit_state("stopped", stopped);
1716 ps_submit_state("paging", paging);
1717 ps_submit_state("blocked", blocked);
1719 for (procstat_t *ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1720 ps_submit_proc_list(ps_ptr);
1723 /* #endif KERNEL_LINUX */
1725 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD
1735 char errbuf[_POSIX2_LINE_MAX];
1736 struct kinfo_proc *procs; /* array of processes */
1737 struct kinfo_proc *proc_ptr = NULL;
1738 int count; /* returns number of processes */
1740 process_entry_t pse;
1744 /* Open the kvm interface, get a descriptor */
1745 kd = kvm_openfiles(NULL, "/dev/null", NULL, 0, errbuf);
1747 ERROR("processes plugin: Cannot open kvm interface: %s", errbuf);
1751 /* Get the list of processes. */
1752 procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, &count);
1753 if (procs == NULL) {
1754 ERROR("processes plugin: Cannot get kvm processes list: %s",
1760 /* Iterate through the processes in kinfo_proc */
1761 for (int i = 0; i < count; i++) {
1762 /* Create only one process list entry per _process_, i.e.
1763 * filter out threads (duplicate PID entries). */
1764 if ((proc_ptr == NULL) || (proc_ptr->ki_pid != procs[i].ki_pid)) {
1765 char cmdline[CMDLINE_BUFFER_SIZE] = "";
1766 _Bool have_cmdline = 0;
1768 proc_ptr = &(procs[i]);
1769 /* Don't probe system processes and processes without arguments */
1770 if (((procs[i].ki_flag & P_SYSTEM) == 0) && (procs[i].ki_args != NULL)) {
1775 /* retrieve the arguments */
1776 argv = kvm_getargv(kd, proc_ptr, /* nchr = */ 0);
1778 if ((argv != NULL) && (argv[0] != NULL)) {
1779 while (argv[argc] != NULL)
1782 status = strjoin(cmdline, sizeof(cmdline), argv, argc, " ");
1784 WARNING("processes plugin: Command line did not fit into buffer.");
1788 } /* if (process has argument list) */
1790 memset(&pse, 0, sizeof(pse));
1791 pse.id = procs[i].ki_pid;
1794 pse.num_lwp = procs[i].ki_numthreads;
1796 pse.vmem_size = procs[i].ki_size;
1797 pse.vmem_rss = procs[i].ki_rssize * pagesize;
1798 pse.vmem_data = procs[i].ki_dsize * pagesize;
1799 pse.vmem_code = procs[i].ki_tsize * pagesize;
1800 pse.stack_size = procs[i].ki_ssize * pagesize;
1801 pse.vmem_minflt_counter = procs[i].ki_rusage.ru_minflt;
1802 pse.vmem_majflt_counter = procs[i].ki_rusage.ru_majflt;
1804 pse.cpu_user_counter = 0;
1805 pse.cpu_system_counter = 0;
1807 * The u-area might be swapped out, and we can't get
1808 * at it because we have a crashdump and no swap.
1809 * If it's here fill in these fields, otherwise, just
1812 if (procs[i].ki_flag & P_INMEM) {
1813 pse.cpu_user_counter = procs[i].ki_rusage.ru_utime.tv_usec +
1814 (1000000lu * procs[i].ki_rusage.ru_utime.tv_sec);
1815 pse.cpu_system_counter =
1816 procs[i].ki_rusage.ru_stime.tv_usec +
1817 (1000000lu * procs[i].ki_rusage.ru_stime.tv_sec);
1826 /* context switch counters not implemented */
1827 pse.cswitch_vol = -1;
1828 pse.cswitch_invol = -1;
1830 ps_list_add(procs[i].ki_comm, have_cmdline ? cmdline : NULL, &pse);
1832 switch (procs[i].ki_stat) {
1855 } /* if ((proc_ptr == NULL) || (proc_ptr->ki_pid != procs[i].ki_pid)) */
1860 ps_submit_state("running", running);
1861 ps_submit_state("sleeping", sleeping);
1862 ps_submit_state("zombies", zombies);
1863 ps_submit_state("stopped", stopped);
1864 ps_submit_state("blocked", blocked);
1865 ps_submit_state("idle", idle);
1866 ps_submit_state("wait", wait);
1868 for (procstat_t *ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1869 ps_submit_proc_list(ps_ptr);
1870 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
1872 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_OPENBSD
1883 struct kinfo_proc *procs; /* array of processes */
1884 struct kinfo_proc *proc_ptr = NULL;
1885 int count; /* returns number of processes */
1887 process_entry_t pse;
1891 /* Open the kvm interface, get a descriptor */
1892 kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf);
1894 ERROR("processes plugin: Cannot open kvm interface: %s", errbuf);
1898 /* Get the list of processes. */
1899 procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc), &count);
1900 if (procs == NULL) {
1901 ERROR("processes plugin: Cannot get kvm processes list: %s",
1907 /* Iterate through the processes in kinfo_proc */
1908 for (int i = 0; i < count; i++) {
1909 /* Create only one process list entry per _process_, i.e.
1910 * filter out threads (duplicate PID entries). */
1911 if ((proc_ptr == NULL) || (proc_ptr->p_pid != procs[i].p_pid)) {
1912 char cmdline[CMDLINE_BUFFER_SIZE] = "";
1913 _Bool have_cmdline = 0;
1915 proc_ptr = &(procs[i]);
1916 /* Don't probe zombie processes */
1917 if (!P_ZOMBIE(proc_ptr)) {
1922 /* retrieve the arguments */
1923 argv = kvm_getargv(kd, proc_ptr, /* nchr = */ 0);
1925 if ((argv != NULL) && (argv[0] != NULL)) {
1926 while (argv[argc] != NULL)
1929 status = strjoin(cmdline, sizeof(cmdline), argv, argc, " ");
1931 WARNING("processes plugin: Command line did not fit into buffer.");
1935 } /* if (process has argument list) */
1937 memset(&pse, 0, sizeof(pse));
1938 pse.id = procs[i].p_pid;
1941 pse.num_lwp = 1; /* XXX: accumulate p_tid values for a single p_pid ? */
1943 pse.vmem_rss = procs[i].p_vm_rssize * pagesize;
1944 pse.vmem_data = procs[i].p_vm_dsize * pagesize;
1945 pse.vmem_code = procs[i].p_vm_tsize * pagesize;
1946 pse.stack_size = procs[i].p_vm_ssize * pagesize;
1947 pse.vmem_size = pse.stack_size + pse.vmem_code + pse.vmem_data;
1948 pse.vmem_minflt_counter = procs[i].p_uru_minflt;
1949 pse.vmem_majflt_counter = procs[i].p_uru_majflt;
1951 pse.cpu_user_counter =
1952 procs[i].p_uutime_usec + (1000000lu * procs[i].p_uutime_sec);
1953 pse.cpu_system_counter =
1954 procs[i].p_ustime_usec + (1000000lu * procs[i].p_ustime_sec);
1962 /* context switch counters not implemented */
1963 pse.cswitch_vol = -1;
1964 pse.cswitch_invol = -1;
1966 ps_list_add(procs[i].p_comm, have_cmdline ? cmdline : NULL, &pse);
1968 switch (procs[i].p_stat) {
1991 } /* if ((proc_ptr == NULL) || (proc_ptr->p_pid != procs[i].p_pid)) */
1996 ps_submit_state("running", running);
1997 ps_submit_state("sleeping", sleeping);
1998 ps_submit_state("zombies", zombies);
1999 ps_submit_state("stopped", stopped);
2000 ps_submit_state("onproc", onproc);
2001 ps_submit_state("idle", idle);
2002 ps_submit_state("dead", dead);
2004 for (procstat_t *ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
2005 ps_submit_proc_list(ps_ptr);
2006 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_OPENBSD */
2008 #elif HAVE_PROCINFO_H
2020 process_entry_t pse;
2023 while ((nprocs = getprocs64(procentry, sizeof(struct procentry64),
2024 /* fdsinfo = */ NULL, sizeof(struct fdsinfo64),
2025 &pindex, MAXPROCENTRY)) > 0) {
2026 for (int i = 0; i < nprocs; i++) {
2029 char arglist[MAXARGLN + 1];
2033 if (procentry[i].pi_state == SNONE)
2035 /* if (procentry[i].pi_state == SZOMB) FIXME */
2037 cmdline = procentry[i].pi_comm;
2038 cargs = procentry[i].pi_comm;
2039 if (procentry[i].pi_flags & SKPROC) {
2040 if (procentry[i].pi_pid == 0)
2041 cmdline = "swapper";
2044 if (getargs(&procentry[i], sizeof(struct procentry64), arglist,
2049 while (++n < MAXARGLN) {
2050 if (arglist[n] == '\0') {
2051 if (arglist[n + 1] == '\0')
2060 memset(&pse, 0, sizeof(pse));
2062 pse.id = procentry[i].pi_pid;
2063 pse.num_lwp = procentry[i].pi_thcount;
2067 while ((nthreads = getthrds64(procentry[i].pi_pid, thrdentry,
2068 sizeof(struct thrdentry64), &thindex,
2069 MAXTHRDENTRY)) > 0) {
2072 for (j = 0; j < nthreads; j++) {
2073 switch (thrdentry[j].ti_state) {
2074 /* case TSNONE: break; */
2077 break; /* FIXME is really blocked */
2095 if (nthreads < MAXTHRDENTRY)
2099 /* tv_usec is nanosec ??? */
2100 pse.cpu_user_counter = procentry[i].pi_ru.ru_utime.tv_sec * 1000000 +
2101 procentry[i].pi_ru.ru_utime.tv_usec / 1000;
2103 /* tv_usec is nanosec ??? */
2104 pse.cpu_system_counter = procentry[i].pi_ru.ru_stime.tv_sec * 1000000 +
2105 procentry[i].pi_ru.ru_stime.tv_usec / 1000;
2107 pse.vmem_minflt_counter = procentry[i].pi_minflt;
2108 pse.vmem_majflt_counter = procentry[i].pi_majflt;
2110 pse.vmem_size = procentry[i].pi_tsize + procentry[i].pi_dvm * pagesize;
2111 pse.vmem_rss = (procentry[i].pi_drss + procentry[i].pi_trss) * pagesize;
2122 pse.cswitch_vol = -1;
2123 pse.cswitch_invol = -1;
2125 ps_list_add(cmdline, cargs, &pse);
2126 } /* for (i = 0 .. nprocs) */
2128 if (nprocs < MAXPROCENTRY)
2130 } /* while (getprocs64() > 0) */
2131 ps_submit_state("running", running);
2132 ps_submit_state("sleeping", sleeping);
2133 ps_submit_state("zombies", zombies);
2134 ps_submit_state("stopped", stopped);
2135 ps_submit_state("paging", paging);
2136 ps_submit_state("blocked", blocked);
2138 for (procstat_t *ps = list_head_g; ps != NULL; ps = ps->next)
2139 ps_submit_proc_list(ps);
2140 /* #endif HAVE_PROCINFO_H */
2142 #elif KERNEL_SOLARIS
2144 * The Solaris section adds a few more process states and removes some
2145 * process states compared to linux. Most notably there is no "PAGING"
2146 * and "BLOCKED" state for a process. The rest is similar to the linux
2164 char cmdline[PRARGSZ];
2168 proc = opendir("/proc");
2172 while ((ent = readdir(proc)) != NULL) {
2174 process_entry_t pse;
2177 if (!isdigit((int)ent->d_name[0]))
2180 pid = strtol(ent->d_name, &endptr, 10);
2181 if (*endptr != 0) /* value didn't completely parse as a number */
2184 memset(&pse, 0, sizeof(pse));
2187 status = ps_read_process(pid, &pse, &state);
2189 DEBUG("ps_read_process failed: %i", status);
2220 ps_list_add(pse.name,
2221 ps_get_cmdline(pid, pse.name, cmdline, sizeof(cmdline)), &pse);
2222 } /* while(readdir) */
2225 ps_submit_state("running", running);
2226 ps_submit_state("sleeping", sleeping);
2227 ps_submit_state("zombies", zombies);
2228 ps_submit_state("stopped", stopped);
2229 ps_submit_state("detached", detached);
2230 ps_submit_state("daemon", daemon);
2231 ps_submit_state("system", system);
2232 ps_submit_state("orphan", orphan);
2234 for (procstat_t *ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
2235 ps_submit_proc_list(ps_ptr);
2238 #endif /* KERNEL_SOLARIS */
2245 void module_register(void) {
2246 plugin_register_complex_config("processes", ps_config);
2247 plugin_register_init("processes", ps_init);
2248 plugin_register_read("processes", ps_read);
2249 } /* void module_register */