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>
41 #include "configfile.h"
43 /* Include header files for the mach system, if they exist.. */
45 # if HAVE_MACH_MACH_INIT_H
46 # include <mach/mach_init.h>
48 # if HAVE_MACH_HOST_PRIV_H
49 # include <mach/host_priv.h>
51 # if HAVE_MACH_MACH_ERROR_H
52 # include <mach/mach_error.h>
54 # if HAVE_MACH_MACH_HOST_H
55 # include <mach/mach_host.h>
57 # if HAVE_MACH_MACH_PORT_H
58 # include <mach/mach_port.h>
60 # if HAVE_MACH_MACH_TYPES_H
61 # include <mach/mach_types.h>
63 # if HAVE_MACH_MESSAGE_H
64 # include <mach/message.h>
66 # if HAVE_MACH_PROCESSOR_SET_H
67 # include <mach/processor_set.h>
70 # include <mach/task.h>
72 # if HAVE_MACH_THREAD_ACT_H
73 # include <mach/thread_act.h>
75 # if HAVE_MACH_VM_REGION_H
76 # include <mach/vm_region.h>
78 # if HAVE_MACH_VM_MAP_H
79 # include <mach/vm_map.h>
81 # if HAVE_MACH_VM_PROT_H
82 # include <mach/vm_prot.h>
84 # if HAVE_SYS_SYSCTL_H
85 # include <sys/sysctl.h>
87 /* #endif HAVE_THREAD_INFO */
90 # if HAVE_LINUX_CONFIG_H
91 # include <linux/config.h>
94 # define CONFIG_HZ 100
96 /* #endif KERNEL_LINUX */
98 #elif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD)
100 # include <sys/param.h>
101 # include <sys/sysctl.h>
102 # include <sys/user.h>
103 # include <sys/proc.h>
104 /* #endif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD) */
106 #elif HAVE_PROCINFO_H
107 # include <procinfo.h>
108 # include <sys/types.h>
110 #define MAXPROCENTRY 32
111 #define MAXTHRDENTRY 16
112 #define MAXARGLN 1024
113 /* #endif HAVE_PROCINFO_H */
116 /* Hack: Avoid #error when building a 32-bit binary with
117 * _FILE_OFFSET_BITS=64. There is a reason for this #error, as one
118 * of the structures in <sys/procfs.h> uses an off_t, but that
119 * isn't relevant to our usage of procfs. */
120 #if !defined(_LP64) && _FILE_OFFSET_BITS == 64
122 # undef _FILE_OFFSET_BITS
128 # define _FILE_OFFSET_BITS 64
132 # include <sys/user.h>
136 # define MAXCOMLEN 16
139 /* #endif KERNEL_SOLARIS */
142 # error "No applicable input method."
153 #ifndef CMDLINE_BUFFER_SIZE
154 # if defined(ARG_MAX) && (ARG_MAX < 4096)
155 # define CMDLINE_BUFFER_SIZE ARG_MAX
157 # define CMDLINE_BUFFER_SIZE 4096
161 typedef struct procstat_entry_s
166 unsigned long num_proc;
167 unsigned long num_lwp;
168 unsigned long vmem_size;
169 unsigned long vmem_rss;
170 unsigned long vmem_data;
171 unsigned long vmem_code;
172 unsigned long stack_size;
174 unsigned long vmem_minflt;
175 unsigned long vmem_majflt;
176 derive_t vmem_minflt_counter;
177 derive_t vmem_majflt_counter;
179 unsigned long cpu_user;
180 unsigned long cpu_system;
181 derive_t cpu_user_counter;
182 derive_t cpu_system_counter;
190 derive_t cswitch_vol;
191 derive_t cswitch_invol;
193 struct procstat_entry_s *next;
196 #define PROCSTAT_NAME_LEN 256
197 typedef struct procstat
199 char name[PROCSTAT_NAME_LEN];
204 unsigned long num_proc;
205 unsigned long num_lwp;
206 unsigned long vmem_size;
207 unsigned long vmem_rss;
208 unsigned long vmem_data;
209 unsigned long vmem_code;
210 unsigned long stack_size;
212 derive_t vmem_minflt_counter;
213 derive_t vmem_majflt_counter;
215 derive_t cpu_user_counter;
216 derive_t cpu_system_counter;
224 derive_t cswitch_vol;
225 derive_t cswitch_invol;
227 struct procstat *next;
228 struct procstat_entry_s *instances;
231 static procstat_t *list_head_g = NULL;
233 static _Bool report_ctx_switch = 0;
236 static mach_port_t port_host_self;
237 static mach_port_t port_task_self;
239 static processor_set_name_array_t pset_list;
240 static mach_msg_type_number_t pset_list_len;
241 /* #endif HAVE_THREAD_INFO */
244 static long pagesize_g;
245 /* #endif KERNEL_LINUX */
247 #elif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD)
249 /* #endif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD) */
251 #elif HAVE_PROCINFO_H
252 static struct procentry64 procentry[MAXPROCENTRY];
253 static struct thrdentry64 thrdentry[MAXTHRDENTRY];
256 #ifndef _AIXVERSION_610
257 int getprocs64 (void *procsinfo, int sizproc, void *fdsinfo, int sizfd, pid_t *index, int count);
258 int getthrds64( pid_t, void *, int, tid64_t *, int );
260 int getargs (void *processBuffer, int bufferLen, char *argsBuffer, int argsLen);
261 #endif /* HAVE_PROCINFO_H */
263 /* put name of process from config to list_head_g tree
264 * list_head_g is a list of 'procstat_t' structs with
265 * processes names we want to watch */
266 static void ps_list_register (const char *name, const char *regexp)
272 new = calloc (1, sizeof (*new));
275 ERROR ("processes plugin: ps_list_register: calloc failed.");
278 sstrncpy (new->name, name, sizeof (new->name));
283 DEBUG ("ProcessMatch: adding \"%s\" as criteria to process %s.", regexp, name);
284 new->re = malloc (sizeof (*new->re));
287 ERROR ("processes plugin: ps_list_register: malloc failed.");
292 status = regcomp (new->re, regexp, REG_EXTENDED | REG_NOSUB);
295 DEBUG ("ProcessMatch: compiling the regular expression \"%s\" failed.", regexp);
304 ERROR ("processes plugin: ps_list_register: "
305 "Regular expression \"%s\" found in config "
306 "file, but support for regular expressions "
307 "has been disabled at compile time.",
314 for (ptr = list_head_g; ptr != NULL; ptr = ptr->next)
316 if (strcmp (ptr->name, name) == 0)
318 WARNING ("processes plugin: You have configured more "
319 "than one `Process' or "
320 "`ProcessMatch' with the same name. "
321 "All but the first setting will be "
330 if (ptr->next == NULL)
338 } /* void ps_list_register */
340 /* try to match name against entry, returns 1 if success */
341 static int ps_list_match (const char *name, const char *cmdline, procstat_t *ps)
350 if ((str == NULL) || (str[0] == 0))
353 assert (str != NULL);
355 status = regexec (ps->re, str,
364 if (strcmp (ps->name, name) == 0)
368 } /* int ps_list_match */
370 static void ps_update_counter (_Bool init, derive_t *group_counter,
371 derive_t *curr_counter, unsigned long *curr_value,
372 derive_t new_counter, unsigned long new_value)
376 *curr_value = new_value;
377 *curr_counter += new_value;
378 *group_counter += new_value;
382 if (new_counter < *curr_counter)
383 *curr_value = new_counter + (ULONG_MAX - *curr_counter);
385 *curr_value = new_counter - *curr_counter;
387 *curr_counter = new_counter;
388 *group_counter += *curr_value;
391 /* add process entry to 'instances' of process 'name' (or refresh it) */
392 static void ps_list_add (const char *name, const char *cmdline, procstat_entry_t *entry)
394 procstat_entry_t *pse;
399 for (procstat_t *ps = list_head_g; ps != NULL; ps = ps->next)
403 if ((ps_list_match (name, cmdline, ps)) == 0)
406 for (pse = ps->instances; pse != NULL; pse = pse->next)
407 if ((pse->id == entry->id) || (pse->next == NULL))
410 if ((pse == NULL) || (pse->id != entry->id))
412 procstat_entry_t *new;
414 new = calloc (1, sizeof (*new));
428 pse->num_proc = entry->num_proc;
429 pse->num_lwp = entry->num_lwp;
430 pse->vmem_size = entry->vmem_size;
431 pse->vmem_rss = entry->vmem_rss;
432 pse->vmem_data = entry->vmem_data;
433 pse->vmem_code = entry->vmem_code;
434 pse->stack_size = entry->stack_size;
435 pse->io_rchar = entry->io_rchar;
436 pse->io_wchar = entry->io_wchar;
437 pse->io_syscr = entry->io_syscr;
438 pse->io_syscw = entry->io_syscw;
439 pse->cswitch_vol = entry->cswitch_vol;
440 pse->cswitch_invol = entry->cswitch_invol;
442 ps->num_proc += pse->num_proc;
443 ps->num_lwp += pse->num_lwp;
444 ps->vmem_size += pse->vmem_size;
445 ps->vmem_rss += pse->vmem_rss;
446 ps->vmem_data += pse->vmem_data;
447 ps->vmem_code += pse->vmem_code;
448 ps->stack_size += pse->stack_size;
450 ps->io_rchar += ((pse->io_rchar == -1)?0:pse->io_rchar);
451 ps->io_wchar += ((pse->io_wchar == -1)?0:pse->io_wchar);
452 ps->io_syscr += ((pse->io_syscr == -1)?0:pse->io_syscr);
453 ps->io_syscw += ((pse->io_syscw == -1)?0:pse->io_syscw);
455 ps->cswitch_vol += ((pse->cswitch_vol == -1)?0:pse->cswitch_vol);
456 ps->cswitch_invol += ((pse->cswitch_invol == -1)?0:pse->cswitch_invol);
458 want_init = (entry->vmem_minflt_counter == 0)
459 && (entry->vmem_majflt_counter == 0);
460 ps_update_counter (want_init,
461 &ps->vmem_minflt_counter,
462 &pse->vmem_minflt_counter, &pse->vmem_minflt,
463 entry->vmem_minflt_counter, entry->vmem_minflt);
464 ps_update_counter (want_init,
465 &ps->vmem_majflt_counter,
466 &pse->vmem_majflt_counter, &pse->vmem_majflt,
467 entry->vmem_majflt_counter, entry->vmem_majflt);
469 want_init = (entry->cpu_user_counter == 0)
470 && (entry->cpu_system_counter == 0);
471 ps_update_counter (want_init,
472 &ps->cpu_user_counter,
473 &pse->cpu_user_counter, &pse->cpu_user,
474 entry->cpu_user_counter, entry->cpu_user);
475 ps_update_counter (want_init,
476 &ps->cpu_system_counter,
477 &pse->cpu_system_counter, &pse->cpu_system,
478 entry->cpu_system_counter, entry->cpu_system);
482 /* remove old entries from instances of processes in list_head_g */
483 static void ps_list_reset (void)
485 procstat_entry_t *pse;
486 procstat_entry_t *pse_prev;
488 for (procstat_t *ps = list_head_g; ps != NULL; ps = ps->next)
501 ps->cswitch_vol = -1;
502 ps->cswitch_invol = -1;
510 DEBUG ("Removing this procstat entry cause it's too old: "
511 "id = %lu; name = %s;",
514 if (pse_prev == NULL)
516 ps->instances = pse->next;
522 pse_prev->next = pse->next;
524 pse = pse_prev->next;
533 } /* while (pse != NULL) */
534 } /* for (ps = list_head_g; ps != NULL; ps = ps->next) */
537 /* put all pre-defined 'Process' names from config to list_head_g tree */
538 static int ps_config (oconfig_item_t *ci)
541 const size_t max_procname_len = 15;
542 #elif KERNEL_SOLARIS || KERNEL_FREEBSD
543 const size_t max_procname_len = MAXCOMLEN -1;
546 for (int i = 0; i < ci->children_num; ++i) {
547 oconfig_item_t *c = ci->children + i;
549 if (strcasecmp (c->key, "Process") == 0)
551 if ((c->values_num != 1)
552 || (OCONFIG_TYPE_STRING != c->values[0].type)) {
553 ERROR ("processes plugin: `Process' expects exactly "
554 "one string argument (got %i).",
559 if (c->children_num != 0) {
560 WARNING ("processes plugin: the `Process' config option "
561 "does not expect any child elements -- ignoring "
562 "content (%i elements) of the <Process '%s'> block.",
563 c->children_num, c->values[0].value.string);
566 #if KERNEL_LINUX || KERNEL_SOLARIS || KERNEL_FREEBSD
567 if (strlen (c->values[0].value.string) > max_procname_len) {
568 WARNING ("processes plugin: this platform has a %zu character limit "
569 "to process names. The `Process \"%s\"' option will "
570 "not work as expected.",
571 max_procname_len, c->values[0].value.string);
575 ps_list_register (c->values[0].value.string, NULL);
577 else if (strcasecmp (c->key, "ProcessMatch") == 0)
579 if ((c->values_num != 2)
580 || (OCONFIG_TYPE_STRING != c->values[0].type)
581 || (OCONFIG_TYPE_STRING != c->values[1].type))
583 ERROR ("processes plugin: `ProcessMatch' needs exactly "
584 "two string arguments (got %i).",
589 if (c->children_num != 0) {
590 WARNING ("processes plugin: the `ProcessMatch' config option "
591 "does not expect any child elements -- ignoring "
592 "content (%i elements) of the <ProcessMatch '%s' '%s'> "
593 "block.", c->children_num, c->values[0].value.string,
594 c->values[1].value.string);
597 ps_list_register (c->values[0].value.string,
598 c->values[1].value.string);
600 else if (strcasecmp (c->key, "CollectContextSwitch") == 0)
602 cf_util_get_boolean (c, &report_ctx_switch);
606 ERROR ("processes plugin: The `%s' configuration option is not "
607 "understood and will be ignored.", c->key);
615 static int ps_init (void)
618 kern_return_t status;
620 port_host_self = mach_host_self ();
621 port_task_self = mach_task_self ();
623 if (pset_list != NULL)
625 vm_deallocate (port_task_self,
626 (vm_address_t) pset_list,
627 pset_list_len * sizeof (processor_set_t));
632 if ((status = host_processor_sets (port_host_self,
634 &pset_list_len)) != KERN_SUCCESS)
636 ERROR ("host_processor_sets failed: %s\n",
637 mach_error_string (status));
642 /* #endif HAVE_THREAD_INFO */
645 pagesize_g = sysconf(_SC_PAGESIZE);
646 DEBUG ("pagesize_g = %li; CONFIG_HZ = %i;",
647 pagesize_g, CONFIG_HZ);
648 /* #endif KERNEL_LINUX */
650 #elif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD)
651 pagesize = getpagesize();
652 /* #endif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD) */
654 #elif HAVE_PROCINFO_H
655 pagesize = getpagesize();
656 #endif /* HAVE_PROCINFO_H */
661 /* submit global state (e.g.: qty of zombies, running, etc..) */
662 static void ps_submit_state (const char *state, double value)
665 value_list_t vl = VALUE_LIST_INIT;
667 values[0].gauge = value;
671 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
672 sstrncpy (vl.plugin, "processes", sizeof (vl.plugin));
673 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
674 sstrncpy (vl.type, "ps_state", sizeof (vl.type));
675 sstrncpy (vl.type_instance, state, sizeof (vl.type_instance));
677 plugin_dispatch_values (&vl);
680 /* submit info about specific process (e.g.: memory taken, cpu usage, etc..) */
681 static void ps_submit_proc_list (procstat_t *ps)
684 value_list_t vl = VALUE_LIST_INIT;
688 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
689 sstrncpy (vl.plugin, "processes", sizeof (vl.plugin));
690 sstrncpy (vl.plugin_instance, ps->name, sizeof (vl.plugin_instance));
692 sstrncpy (vl.type, "ps_vm", sizeof (vl.type));
693 vl.values[0].gauge = ps->vmem_size;
695 plugin_dispatch_values (&vl);
697 sstrncpy (vl.type, "ps_rss", sizeof (vl.type));
698 vl.values[0].gauge = ps->vmem_rss;
700 plugin_dispatch_values (&vl);
702 sstrncpy (vl.type, "ps_data", sizeof (vl.type));
703 vl.values[0].gauge = ps->vmem_data;
705 plugin_dispatch_values (&vl);
707 sstrncpy (vl.type, "ps_code", sizeof (vl.type));
708 vl.values[0].gauge = ps->vmem_code;
710 plugin_dispatch_values (&vl);
712 sstrncpy (vl.type, "ps_stacksize", sizeof (vl.type));
713 vl.values[0].gauge = ps->stack_size;
715 plugin_dispatch_values (&vl);
717 sstrncpy (vl.type, "ps_cputime", sizeof (vl.type));
718 vl.values[0].derive = ps->cpu_user_counter;
719 vl.values[1].derive = ps->cpu_system_counter;
721 plugin_dispatch_values (&vl);
723 sstrncpy (vl.type, "ps_count", sizeof (vl.type));
724 vl.values[0].gauge = ps->num_proc;
725 vl.values[1].gauge = ps->num_lwp;
727 plugin_dispatch_values (&vl);
729 sstrncpy (vl.type, "ps_pagefaults", sizeof (vl.type));
730 vl.values[0].derive = ps->vmem_minflt_counter;
731 vl.values[1].derive = ps->vmem_majflt_counter;
733 plugin_dispatch_values (&vl);
735 if ( (ps->io_rchar != -1) && (ps->io_wchar != -1) )
737 sstrncpy (vl.type, "ps_disk_octets", sizeof (vl.type));
738 vl.values[0].derive = ps->io_rchar;
739 vl.values[1].derive = ps->io_wchar;
741 plugin_dispatch_values (&vl);
744 if ( (ps->io_syscr != -1) && (ps->io_syscw != -1) )
746 sstrncpy (vl.type, "ps_disk_ops", sizeof (vl.type));
747 vl.values[0].derive = ps->io_syscr;
748 vl.values[1].derive = ps->io_syscw;
750 plugin_dispatch_values (&vl);
753 if ( report_ctx_switch )
755 sstrncpy (vl.type, "contextswitch", sizeof (vl.type));
756 sstrncpy (vl.type_instance, "voluntary", sizeof (vl.type_instance));
757 vl.values[0].derive = ps->cswitch_vol;
759 plugin_dispatch_values (&vl);
761 sstrncpy (vl.type, "contextswitch", sizeof (vl.type));
762 sstrncpy (vl.type_instance, "involuntary", sizeof (vl.type_instance));
763 vl.values[0].derive = ps->cswitch_invol;
765 plugin_dispatch_values (&vl);
768 DEBUG ("name = %s; num_proc = %lu; num_lwp = %lu; "
769 "vmem_size = %lu; vmem_rss = %lu; vmem_data = %lu; "
771 "vmem_minflt_counter = %"PRIi64"; vmem_majflt_counter = %"PRIi64"; "
772 "cpu_user_counter = %"PRIi64"; cpu_system_counter = %"PRIi64"; "
773 "io_rchar = %"PRIi64"; io_wchar = %"PRIi64"; "
774 "io_syscr = %"PRIi64"; io_syscw = %"PRIi64"; "
775 "cswitch_vol = %"PRIi64"; cswitch_invol = %"PRIi64";",
776 ps->name, ps->num_proc, ps->num_lwp,
777 ps->vmem_size, ps->vmem_rss,
778 ps->vmem_data, ps->vmem_code,
779 ps->vmem_minflt_counter, ps->vmem_majflt_counter,
780 ps->cpu_user_counter, ps->cpu_system_counter,
781 ps->io_rchar, ps->io_wchar, ps->io_syscr, ps->io_syscw,
782 ps->cswitch_vol, ps->cswitch_invol);
783 } /* void ps_submit_proc_list */
785 #if KERNEL_LINUX || KERNEL_SOLARIS
786 static void ps_submit_fork_rate (derive_t value)
789 value_list_t vl = VALUE_LIST_INIT;
791 values[0].derive = value;
795 sstrncpy(vl.host, hostname_g, sizeof (vl.host));
796 sstrncpy(vl.plugin, "processes", sizeof (vl.plugin));
797 sstrncpy(vl.plugin_instance, "", sizeof (vl.plugin_instance));
798 sstrncpy(vl.type, "fork_rate", sizeof (vl.type));
799 sstrncpy(vl.type_instance, "", sizeof (vl.type_instance));
801 plugin_dispatch_values(&vl);
803 #endif /* KERNEL_LINUX || KERNEL_SOLARIS*/
805 /* ------- additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
807 static procstat_t *ps_read_tasks_status (long pid, procstat_t *ps)
814 derive_t cswitch_vol = 0;
815 derive_t cswitch_invol = 0;
820 ssnprintf (dirname, sizeof (dirname), "/proc/%li/task", pid);
822 if ((dh = opendir (dirname)) == NULL)
824 DEBUG ("Failed to open directory `%s'", dirname);
828 while ((ent = readdir (dh)) != NULL)
832 if (!isdigit ((int) ent->d_name[0]))
837 ssnprintf (filename, sizeof (filename), "/proc/%li/task/%s/status", pid, tpid);
838 if ((fh = fopen (filename, "r")) == NULL)
840 DEBUG ("Failed to open file `%s'", filename);
844 while (fgets (buffer, sizeof(buffer), fh) != NULL)
849 if (strncmp (buffer, "voluntary_ctxt_switches", 23) != 0
850 && strncmp (buffer, "nonvoluntary_ctxt_switches", 26) != 0)
853 numfields = strsplit (buffer, fields,
854 STATIC_ARRAY_SIZE (fields));
861 tmp = (derive_t) strtoll (fields[1], &endptr, /* base = */ 10);
862 if ((errno == 0) && (endptr != fields[1]))
864 if (strncmp (buffer, "voluntary_ctxt_switches", 23) == 0)
868 else if (strncmp (buffer, "nonvoluntary_ctxt_switches", 26) == 0)
870 cswitch_invol += tmp;
873 } /* while (fgets) */
878 WARNING ("processes: fclose: %s",
879 sstrerror (errno, errbuf, sizeof (errbuf)));
884 ps->cswitch_vol = cswitch_vol;
885 ps->cswitch_invol = cswitch_invol;
888 } /* int *ps_read_tasks_status */
890 /* Read data from /proc/pid/status */
891 static procstat_t *ps_read_status (long pid, procstat_t *ps)
896 unsigned long lib = 0;
897 unsigned long exe = 0;
898 unsigned long data = 0;
899 unsigned long threads = 0;
903 ssnprintf (filename, sizeof (filename), "/proc/%li/status", pid);
904 if ((fh = fopen (filename, "r")) == NULL)
907 while (fgets (buffer, sizeof(buffer), fh) != NULL)
912 if (strncmp (buffer, "Vm", 2) != 0
913 && strncmp (buffer, "Threads", 7) != 0)
916 numfields = strsplit (buffer, fields,
917 STATIC_ARRAY_SIZE (fields));
924 tmp = strtoul (fields[1], &endptr, /* base = */ 10);
925 if ((errno == 0) && (endptr != fields[1]))
927 if (strncmp (buffer, "VmData", 6) == 0)
931 else if (strncmp (buffer, "VmLib", 5) == 0)
935 else if (strncmp(buffer, "VmExe", 5) == 0)
939 else if (strncmp(buffer, "Threads", 7) == 0)
944 } /* while (fgets) */
949 WARNING ("processes: fclose: %s",
950 sstrerror (errno, errbuf, sizeof (errbuf)));
953 ps->vmem_data = data * 1024;
954 ps->vmem_code = (exe + lib) * 1024;
956 ps->num_lwp = threads;
959 } /* procstat_t *ps_read_vmem */
961 static procstat_t *ps_read_io (long pid, procstat_t *ps)
970 ssnprintf (filename, sizeof (filename), "/proc/%li/io", pid);
971 if ((fh = fopen (filename, "r")) == NULL)
974 while (fgets (buffer, sizeof (buffer), fh) != NULL)
976 derive_t *val = NULL;
980 if (strncasecmp (buffer, "rchar:", 6) == 0)
981 val = &(ps->io_rchar);
982 else if (strncasecmp (buffer, "wchar:", 6) == 0)
983 val = &(ps->io_wchar);
984 else if (strncasecmp (buffer, "syscr:", 6) == 0)
985 val = &(ps->io_syscr);
986 else if (strncasecmp (buffer, "syscw:", 6) == 0)
987 val = &(ps->io_syscw);
991 numfields = strsplit (buffer, fields,
992 STATIC_ARRAY_SIZE (fields));
999 tmp = strtoll (fields[1], &endptr, /* base = */ 10);
1000 if ((errno != 0) || (endptr == fields[1]))
1003 *val = (derive_t) tmp;
1004 } /* while (fgets) */
1009 WARNING ("processes: fclose: %s",
1010 sstrerror (errno, errbuf, sizeof (errbuf)));
1014 } /* procstat_t *ps_read_io */
1016 static int ps_read_process (long pid, procstat_t *ps, char *state)
1027 size_t name_start_pos;
1028 size_t name_end_pos;
1031 derive_t cpu_user_counter;
1032 derive_t cpu_system_counter;
1033 long long unsigned vmem_size;
1034 long long unsigned vmem_rss;
1035 long long unsigned stack_size;
1039 memset (ps, 0, sizeof (procstat_t));
1041 ssnprintf (filename, sizeof (filename), "/proc/%li/stat", pid);
1043 status = read_file_contents (filename, buffer, sizeof(buffer) - 1);
1046 buffer_len = (size_t) status;
1047 buffer[buffer_len] = 0;
1049 /* The name of the process is enclosed in parens. Since the name can
1050 * contain parens itself, spaces, numbers and pretty much everything
1051 * else, use these to determine the process name. We don't use
1052 * strchr(3) and strrchr(3) to avoid pointer arithmetic which would
1053 * otherwise be required to determine name_len. */
1055 while (name_start_pos < buffer_len && buffer[name_start_pos] != '(')
1058 name_end_pos = buffer_len;
1059 while (name_end_pos > 0 && buffer[name_end_pos] != ')')
1062 /* Either '(' or ')' is not found or they are in the wrong order.
1063 * Anyway, something weird that shouldn't happen ever. */
1064 if (name_start_pos >= name_end_pos)
1066 ERROR ("processes plugin: name_start_pos = %zu >= name_end_pos = %zu",
1067 name_start_pos, name_end_pos);
1071 name_len = (name_end_pos - name_start_pos) - 1;
1072 if (name_len >= sizeof (ps->name))
1073 name_len = sizeof (ps->name) - 1;
1075 sstrncpy (ps->name, &buffer[name_start_pos + 1], name_len + 1);
1077 if ((buffer_len - name_end_pos) < 2)
1079 buffer_ptr = &buffer[name_end_pos + 2];
1081 fields_len = strsplit (buffer_ptr, fields, STATIC_ARRAY_SIZE (fields));
1082 if (fields_len < 22)
1084 DEBUG ("processes plugin: ps_read_process (pid = %li):"
1085 " `%s' has only %i fields..",
1086 pid, filename, fields_len);
1090 *state = fields[0][0];
1099 ps->num_lwp = strtoul (fields[17], /* endptr = */ NULL, /* base = */ 10);
1100 if ((ps_read_status(pid, ps)) == NULL)
1105 DEBUG("ps_read_process: did not get vmem data for pid %li", pid);
1107 if (ps->num_lwp == 0)
1112 /* Leave the rest at zero if this is only a zombi */
1113 if (ps->num_proc == 0)
1115 DEBUG ("processes plugin: This is only a zombie: pid = %li; "
1116 "name = %s;", pid, ps->name);
1120 cpu_user_counter = atoll (fields[11]);
1121 cpu_system_counter = atoll (fields[12]);
1122 vmem_size = atoll (fields[20]);
1123 vmem_rss = atoll (fields[21]);
1124 ps->vmem_minflt_counter = atol (fields[7]);
1125 ps->vmem_majflt_counter = atol (fields[9]);
1128 unsigned long long stack_start = atoll (fields[25]);
1129 unsigned long long stack_ptr = atoll (fields[26]);
1131 stack_size = (stack_start > stack_ptr)
1132 ? stack_start - stack_ptr
1133 : stack_ptr - stack_start;
1136 /* Convert jiffies to useconds */
1137 cpu_user_counter = cpu_user_counter * 1000000 / CONFIG_HZ;
1138 cpu_system_counter = cpu_system_counter * 1000000 / CONFIG_HZ;
1139 vmem_rss = vmem_rss * pagesize_g;
1141 ps->cpu_user_counter = cpu_user_counter;
1142 ps->cpu_system_counter = cpu_system_counter;
1143 ps->vmem_size = (unsigned long) vmem_size;
1144 ps->vmem_rss = (unsigned long) vmem_rss;
1145 ps->stack_size = (unsigned long) stack_size;
1147 if ( (ps_read_io (pid, ps)) == NULL)
1155 DEBUG("ps_read_process: not get io data for pid %li", pid);
1158 if ( report_ctx_switch )
1160 if ( (ps_read_tasks_status(pid, ps)) == NULL)
1162 ps->cswitch_vol = -1;
1163 ps->cswitch_invol = -1;
1165 DEBUG("ps_read_tasks_status: not get context "
1166 "switch data for pid %li", pid);
1172 } /* int ps_read_process (...) */
1174 static char *ps_get_cmdline (long pid, char *name, char *buf, size_t buf_len)
1179 char file[PATH_MAX];
1184 if ((pid < 1) || (NULL == buf) || (buf_len < 2))
1187 ssnprintf (file, sizeof (file), "/proc/%li/cmdline", pid);
1190 fd = open (file, O_RDONLY);
1193 /* ENOENT means the process exited while we were handling it.
1194 * Don't complain about this, it only fills the logs. */
1195 if (errno != ENOENT)
1196 WARNING ("processes plugin: Failed to open `%s': %s.", file,
1197 sstrerror (errno, errbuf, sizeof (errbuf)));
1209 status = read (fd, (void *)buf_ptr, len);
1214 if ((EAGAIN == errno) || (EINTR == errno))
1217 WARNING ("processes plugin: Failed to read from `%s': %s.", file,
1218 sstrerror (errno, errbuf, sizeof (errbuf)));
1238 /* cmdline not available; e.g. kernel thread, zombie */
1242 ssnprintf (buf, buf_len, "[%s]", name);
1246 assert (n <= buf_len);
1253 /* remove trailing whitespace */
1254 while ((n > 0) && (isspace (buf[n]) || ('\0' == buf[n]))) {
1259 /* arguments are separated by '\0' in /proc/<pid>/cmdline */
1266 } /* char *ps_get_cmdline (...) */
1268 static int read_fork_rate (void)
1273 _Bool value_valid = 0;
1275 proc_stat = fopen ("/proc/stat", "r");
1276 if (proc_stat == NULL)
1279 ERROR ("processes plugin: fopen (/proc/stat) failed: %s",
1280 sstrerror (errno, errbuf, sizeof (errbuf)));
1284 while (fgets (buffer, sizeof (buffer), proc_stat) != NULL)
1290 fields_num = strsplit (buffer, fields,
1291 STATIC_ARRAY_SIZE (fields));
1292 if (fields_num != 2)
1295 if (strcmp ("processes", fields[0]) != 0)
1298 status = parse_value (fields[1], &value, DS_TYPE_DERIVE);
1309 ps_submit_fork_rate (value.derive);
1312 #endif /*KERNEL_LINUX */
1315 static char *ps_get_cmdline (long pid, char *name __attribute__((unused)), /* {{{ */
1316 char *buffer, size_t buffer_size)
1318 char path[PATH_MAX];
1322 snprintf(path, sizeof (path), "/proc/%li/psinfo", pid);
1324 status = read_file_contents (path, (void *) &info, sizeof (info));
1325 if ((status < 0) || (((size_t) status) != sizeof (info)))
1327 ERROR ("processes plugin: Unexpected return value "
1328 "while reading \"%s\": "
1329 "Returned %zd but expected %zu.",
1330 path, status, buffer_size);
1334 info.pr_psargs[sizeof (info.pr_psargs) - 1] = 0;
1335 sstrncpy (buffer, info.pr_psargs, buffer_size);
1338 } /* }}} int ps_get_cmdline */
1341 * Reads process information on the Solaris OS. The information comes mainly from
1342 * /proc/PID/status, /proc/PID/psinfo and /proc/PID/usage
1343 * The values for input and ouput chars are calculated "by hand"
1344 * Added a few "solaris" specific process states as well
1346 static int ps_read_process(long pid, procstat_t *ps, char *state)
1349 char f_psinfo[64], f_usage[64];
1352 pstatus_t *myStatus;
1356 snprintf(filename, sizeof (filename), "/proc/%li/status", pid);
1357 snprintf(f_psinfo, sizeof (f_psinfo), "/proc/%li/psinfo", pid);
1358 snprintf(f_usage, sizeof (f_usage), "/proc/%li/usage", pid);
1361 buffer = calloc(1, sizeof (pstatus_t));
1362 read_file_contents(filename, buffer, sizeof (pstatus_t));
1363 myStatus = (pstatus_t *) buffer;
1365 buffer = calloc(1, sizeof (psinfo_t));
1366 read_file_contents(f_psinfo, buffer, sizeof (psinfo_t));
1367 myInfo = (psinfo_t *) buffer;
1369 buffer = calloc(1, sizeof (prusage_t));
1370 read_file_contents(f_usage, buffer, sizeof (prusage_t));
1371 myUsage = (prusage_t *) buffer;
1373 sstrncpy(ps->name, myInfo->pr_fname, sizeof (myInfo->pr_fname));
1374 ps->num_lwp = myStatus->pr_nlwp;
1375 if (myInfo->pr_wstat != 0) {
1378 *state = (char) 'Z';
1386 ps->num_lwp = myInfo->pr_nlwp;
1390 * Convert system time and user time from nanoseconds to microseconds
1391 * for compatibility with the linux module
1393 ps->cpu_system_counter = myStatus -> pr_stime.tv_nsec / 1000;
1394 ps->cpu_user_counter = myStatus -> pr_utime.tv_nsec / 1000;
1397 * Convert rssize from KB to bytes to be consistent w/ the linux module
1399 ps->vmem_rss = myInfo->pr_rssize * 1024;
1400 ps->vmem_size = myInfo->pr_size * 1024;
1401 ps->vmem_minflt_counter = myUsage->pr_minf;
1402 ps->vmem_majflt_counter = myUsage->pr_majf;
1405 * TODO: Data and code segment calculations for Solaris
1410 ps->stack_size = myStatus->pr_stksize;
1413 * Calculating input/ouput chars
1414 * Formula used is total chars / total blocks => chars/block
1415 * then convert input/output blocks to chars
1417 ulong_t tot_chars = myUsage->pr_ioch;
1418 ulong_t tot_blocks = myUsage->pr_inblk + myUsage->pr_oublk;
1419 ulong_t chars_per_block = 1;
1420 if (tot_blocks != 0)
1421 chars_per_block = tot_chars / tot_blocks;
1422 ps->io_rchar = myUsage->pr_inblk * chars_per_block;
1423 ps->io_wchar = myUsage->pr_oublk * chars_per_block;
1424 ps->io_syscr = myUsage->pr_sysc;
1425 ps->io_syscw = myUsage->pr_sysc;
1428 * TODO: context switch counters for Solaris
1430 ps->cswitch_vol = -1;
1431 ps->cswitch_invol = -1;
1435 * TODO: Find way of setting BLOCKED and PAGING status
1438 *state = (char) 'R';
1439 if (myStatus->pr_flags & PR_ASLEEP)
1440 *state = (char) 'S';
1441 else if (myStatus->pr_flags & PR_STOPPED)
1442 *state = (char) 'T';
1443 else if (myStatus->pr_flags & PR_DETACH)
1444 *state = (char) 'E';
1445 else if (myStatus->pr_flags & PR_DAEMON)
1446 *state = (char) 'A';
1447 else if (myStatus->pr_flags & PR_ISSYS)
1448 *state = (char) 'Y';
1449 else if (myStatus->pr_flags & PR_ORPHAN)
1450 *state = (char) 'O';
1460 * Reads the number of threads created since the last reboot. On Solaris these
1461 * are retrieved from kstat (module cpu, name sys, class misc, stat nthreads).
1462 * The result is the sum for all the threads created on each cpu
1464 static int read_fork_rate (void)
1466 extern kstat_ctl_t *kc;
1467 derive_t result = 0;
1472 for (kstat_t *ksp_chain = kc->kc_chain;
1474 ksp_chain = ksp_chain->ks_next)
1476 if ((strcmp (ksp_chain->ks_module, "cpu") == 0)
1477 && (strcmp (ksp_chain->ks_name, "sys") == 0)
1478 && (strcmp (ksp_chain->ks_class, "misc") == 0))
1482 kstat_read (kc, ksp_chain, NULL);
1484 tmp = get_kstat_value(ksp_chain, "nthreads");
1490 ps_submit_fork_rate (result);
1493 #endif /* KERNEL_SOLARIS */
1495 #if HAVE_THREAD_INFO
1496 static int mach_get_task_name (task_t t, int *pid, char *name, size_t name_max_len)
1500 struct kinfo_proc kp;
1505 mib[2] = KERN_PROC_PID;
1507 if (pid_for_task (t, pid) != KERN_SUCCESS)
1511 kp_size = sizeof (kp);
1512 if (sysctl (mib, 4, &kp, &kp_size, NULL, 0) != 0)
1515 if (name_max_len > (MAXCOMLEN + 1))
1516 name_max_len = MAXCOMLEN + 1;
1518 strncpy (name, kp.kp_proc.p_comm, name_max_len - 1);
1519 name[name_max_len - 1] = '\0';
1521 DEBUG ("pid = %i; name = %s;", *pid, name);
1523 /* We don't do the special handling for `p_comm == "LaunchCFMApp"' as
1524 * `top' does it, because it is a lot of work and only used when
1525 * debugging. -octo */
1529 #endif /* HAVE_THREAD_INFO */
1530 /* ------- end of additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
1532 /* do actual readings from kernel */
1533 static int ps_read (void)
1535 #if HAVE_THREAD_INFO
1536 kern_return_t status;
1538 processor_set_t port_pset_priv;
1540 task_array_t task_list;
1541 mach_msg_type_number_t task_list_len;
1544 char task_name[MAXCOMLEN + 1];
1546 thread_act_array_t thread_list;
1547 mach_msg_type_number_t thread_list_len;
1548 thread_basic_info_data_t thread_data;
1549 mach_msg_type_number_t thread_data_len;
1558 procstat_entry_t pse;
1563 * The Mach-concept is a little different from the traditional UNIX
1564 * concept: All the work is done in threads. Threads are contained in
1565 * `tasks'. Therefore, `task status' doesn't make much sense, since
1566 * it's actually a `thread status'.
1567 * Tasks are assigned to sets of processors, so that's where you go to
1570 for (int pset = 0; pset < pset_list_len; pset++)
1572 if ((status = host_processor_set_priv (port_host_self,
1574 &port_pset_priv)) != KERN_SUCCESS)
1576 ERROR ("host_processor_set_priv failed: %s\n",
1577 mach_error_string (status));
1581 if ((status = processor_set_tasks (port_pset_priv,
1583 &task_list_len)) != KERN_SUCCESS)
1585 ERROR ("processor_set_tasks failed: %s\n",
1586 mach_error_string (status));
1587 mach_port_deallocate (port_task_self, port_pset_priv);
1591 for (int task = 0; task < task_list_len; task++)
1594 if (mach_get_task_name (task_list[task],
1596 task_name, PROCSTAT_NAME_LEN) == 0)
1598 /* search for at least one match */
1599 for (ps = list_head_g; ps != NULL; ps = ps->next)
1600 /* FIXME: cmdline should be here instead of NULL */
1601 if (ps_list_match (task_name, NULL, ps) == 1)
1605 /* Collect more detailed statistics for this process */
1608 task_basic_info_data_t task_basic_info;
1609 mach_msg_type_number_t task_basic_info_len;
1610 task_events_info_data_t task_events_info;
1611 mach_msg_type_number_t task_events_info_len;
1612 task_absolutetime_info_data_t task_absolutetime_info;
1613 mach_msg_type_number_t task_absolutetime_info_len;
1615 memset (&pse, '\0', sizeof (pse));
1618 task_basic_info_len = TASK_BASIC_INFO_COUNT;
1619 status = task_info (task_list[task],
1621 (task_info_t) &task_basic_info,
1622 &task_basic_info_len);
1623 if (status != KERN_SUCCESS)
1625 ERROR ("task_info failed: %s",
1626 mach_error_string (status));
1627 continue; /* with next thread_list */
1630 task_events_info_len = TASK_EVENTS_INFO_COUNT;
1631 status = task_info (task_list[task],
1633 (task_info_t) &task_events_info,
1634 &task_events_info_len);
1635 if (status != KERN_SUCCESS)
1637 ERROR ("task_info failed: %s",
1638 mach_error_string (status));
1639 continue; /* with next thread_list */
1642 task_absolutetime_info_len = TASK_ABSOLUTETIME_INFO_COUNT;
1643 status = task_info (task_list[task],
1644 TASK_ABSOLUTETIME_INFO,
1645 (task_info_t) &task_absolutetime_info,
1646 &task_absolutetime_info_len);
1647 if (status != KERN_SUCCESS)
1649 ERROR ("task_info failed: %s",
1650 mach_error_string (status));
1651 continue; /* with next thread_list */
1655 pse.vmem_size = task_basic_info.virtual_size;
1656 pse.vmem_rss = task_basic_info.resident_size;
1657 /* Does not seem to be easily exposed */
1661 pse.vmem_minflt_counter = task_events_info.cow_faults;
1662 pse.vmem_majflt_counter = task_events_info.faults;
1664 pse.cpu_user_counter = task_absolutetime_info.total_user;
1665 pse.cpu_system_counter = task_absolutetime_info.total_system;
1667 /* context switch counters not implemented */
1668 pse.cswitch_vol = -1;
1669 pse.cswitch_invol = -1;
1672 status = task_threads (task_list[task], &thread_list,
1674 if (status != KERN_SUCCESS)
1676 /* Apple's `top' treats this case a zombie. It
1677 * makes sense to some extend: A `zombie'
1678 * thread is nonsense, since the task/process
1681 DEBUG ("task_threads failed: %s",
1682 mach_error_string (status));
1683 if (task_list[task] != port_task_self)
1684 mach_port_deallocate (port_task_self,
1686 continue; /* with next task_list */
1689 for (int thread = 0; thread < thread_list_len; thread++)
1691 thread_data_len = THREAD_BASIC_INFO_COUNT;
1692 status = thread_info (thread_list[thread],
1694 (thread_info_t) &thread_data,
1696 if (status != KERN_SUCCESS)
1698 ERROR ("thread_info failed: %s",
1699 mach_error_string (status));
1700 if (task_list[task] != port_task_self)
1701 mach_port_deallocate (port_task_self,
1702 thread_list[thread]);
1703 continue; /* with next thread_list */
1709 switch (thread_data.run_state)
1711 case TH_STATE_RUNNING:
1714 case TH_STATE_STOPPED:
1715 /* What exactly is `halted'? */
1716 case TH_STATE_HALTED:
1719 case TH_STATE_WAITING:
1722 case TH_STATE_UNINTERRUPTIBLE:
1725 /* There is no `zombie' case here,
1726 * since there are no zombie-threads.
1727 * There's only zombie tasks, which are
1730 WARNING ("Unknown thread status: %i",
1731 thread_data.run_state);
1733 } /* switch (thread_data.run_state) */
1735 if (task_list[task] != port_task_self)
1737 status = mach_port_deallocate (port_task_self,
1738 thread_list[thread]);
1739 if (status != KERN_SUCCESS)
1740 ERROR ("mach_port_deallocate failed: %s",
1741 mach_error_string (status));
1743 } /* for (thread_list) */
1745 if ((status = vm_deallocate (port_task_self,
1746 (vm_address_t) thread_list,
1747 thread_list_len * sizeof (thread_act_t)))
1750 ERROR ("vm_deallocate failed: %s",
1751 mach_error_string (status));
1754 thread_list_len = 0;
1756 /* Only deallocate the task port, if it isn't our own.
1757 * Don't know what would happen in that case, but this
1758 * is what Apple's top does.. ;) */
1759 if (task_list[task] != port_task_self)
1761 status = mach_port_deallocate (port_task_self,
1763 if (status != KERN_SUCCESS)
1764 ERROR ("mach_port_deallocate failed: %s",
1765 mach_error_string (status));
1769 /* FIXME: cmdline should be here instead of NULL */
1770 ps_list_add (task_name, NULL, &pse);
1771 } /* for (task_list) */
1773 if ((status = vm_deallocate (port_task_self,
1774 (vm_address_t) task_list,
1775 task_list_len * sizeof (task_t))) != KERN_SUCCESS)
1777 ERROR ("vm_deallocate failed: %s",
1778 mach_error_string (status));
1783 if ((status = mach_port_deallocate (port_task_self, port_pset_priv))
1786 ERROR ("mach_port_deallocate failed: %s",
1787 mach_error_string (status));
1789 } /* for (pset_list) */
1791 ps_submit_state ("running", running);
1792 ps_submit_state ("sleeping", sleeping);
1793 ps_submit_state ("zombies", zombies);
1794 ps_submit_state ("stopped", stopped);
1795 ps_submit_state ("blocked", blocked);
1797 for (ps = list_head_g; ps != NULL; ps = ps->next)
1798 ps_submit_proc_list (ps);
1799 /* #endif HAVE_THREAD_INFO */
1813 char cmdline[CMDLINE_BUFFER_SIZE];
1817 procstat_entry_t pse;
1820 running = sleeping = zombies = stopped = paging = blocked = 0;
1823 if ((proc = opendir ("/proc")) == NULL)
1826 ERROR ("Cannot open `/proc': %s",
1827 sstrerror (errno, errbuf, sizeof (errbuf)));
1831 while ((ent = readdir (proc)) != NULL)
1833 if (!isdigit (ent->d_name[0]))
1836 if ((pid = atol (ent->d_name)) < 1)
1839 status = ps_read_process (pid, &ps, &state);
1842 DEBUG ("ps_read_process failed: %i", status);
1846 memset (&pse, 0, sizeof (pse));
1850 pse.num_proc = ps.num_proc;
1851 pse.num_lwp = ps.num_lwp;
1852 pse.vmem_size = ps.vmem_size;
1853 pse.vmem_rss = ps.vmem_rss;
1854 pse.vmem_data = ps.vmem_data;
1855 pse.vmem_code = ps.vmem_code;
1856 pse.stack_size = ps.stack_size;
1858 pse.vmem_minflt = 0;
1859 pse.vmem_minflt_counter = ps.vmem_minflt_counter;
1860 pse.vmem_majflt = 0;
1861 pse.vmem_majflt_counter = ps.vmem_majflt_counter;
1864 pse.cpu_user_counter = ps.cpu_user_counter;
1866 pse.cpu_system_counter = ps.cpu_system_counter;
1868 pse.io_rchar = ps.io_rchar;
1869 pse.io_wchar = ps.io_wchar;
1870 pse.io_syscr = ps.io_syscr;
1871 pse.io_syscw = ps.io_syscw;
1873 pse.cswitch_vol = ps.cswitch_vol;
1874 pse.cswitch_invol = ps.cswitch_invol;
1878 case 'R': running++; break;
1879 case 'S': sleeping++; break;
1880 case 'D': blocked++; break;
1881 case 'Z': zombies++; break;
1882 case 'T': stopped++; break;
1883 case 'W': paging++; break;
1886 ps_list_add (ps.name,
1887 ps_get_cmdline (pid, ps.name, cmdline, sizeof (cmdline)),
1893 ps_submit_state ("running", running);
1894 ps_submit_state ("sleeping", sleeping);
1895 ps_submit_state ("zombies", zombies);
1896 ps_submit_state ("stopped", stopped);
1897 ps_submit_state ("paging", paging);
1898 ps_submit_state ("blocked", blocked);
1900 for (procstat_t *ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1901 ps_submit_proc_list (ps_ptr);
1904 /* #endif KERNEL_LINUX */
1906 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD
1916 char errbuf[_POSIX2_LINE_MAX];
1917 struct kinfo_proc *procs; /* array of processes */
1918 struct kinfo_proc *proc_ptr = NULL;
1919 int count; /* returns number of processes */
1921 procstat_entry_t pse;
1925 /* Open the kvm interface, get a descriptor */
1926 kd = kvm_openfiles (NULL, "/dev/null", NULL, 0, errbuf);
1929 ERROR ("processes plugin: Cannot open kvm interface: %s",
1934 /* Get the list of processes. */
1935 procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, &count);
1938 ERROR ("processes plugin: Cannot get kvm processes list: %s",
1944 /* Iterate through the processes in kinfo_proc */
1945 for (int i = 0; i < count; i++)
1947 /* Create only one process list entry per _process_, i.e.
1948 * filter out threads (duplicate PID entries). */
1949 if ((proc_ptr == NULL) || (proc_ptr->ki_pid != procs[i].ki_pid))
1951 char cmdline[CMDLINE_BUFFER_SIZE] = "";
1952 _Bool have_cmdline = 0;
1954 proc_ptr = &(procs[i]);
1955 /* Don't probe system processes and processes without arguments */
1956 if (((procs[i].ki_flag & P_SYSTEM) == 0)
1957 && (procs[i].ki_args != NULL))
1963 /* retrieve the arguments */
1964 argv = kvm_getargv (kd, proc_ptr, /* nchr = */ 0);
1966 if ((argv != NULL) && (argv[0] != NULL))
1968 while (argv[argc] != NULL)
1971 status = strjoin (cmdline, sizeof (cmdline), argv, argc, " ");
1973 WARNING ("processes plugin: Command line did not fit into buffer.");
1977 } /* if (process has argument list) */
1979 pse.id = procs[i].ki_pid;
1983 pse.num_lwp = procs[i].ki_numthreads;
1985 pse.vmem_size = procs[i].ki_size;
1986 pse.vmem_rss = procs[i].ki_rssize * pagesize;
1987 pse.vmem_data = procs[i].ki_dsize * pagesize;
1988 pse.vmem_code = procs[i].ki_tsize * pagesize;
1989 pse.stack_size = procs[i].ki_ssize * pagesize;
1990 pse.vmem_minflt = 0;
1991 pse.vmem_minflt_counter = procs[i].ki_rusage.ru_minflt;
1992 pse.vmem_majflt = 0;
1993 pse.vmem_majflt_counter = procs[i].ki_rusage.ru_majflt;
1997 pse.cpu_user_counter = 0;
1998 pse.cpu_system_counter = 0;
2000 * The u-area might be swapped out, and we can't get
2001 * at it because we have a crashdump and no swap.
2002 * If it's here fill in these fields, otherwise, just
2005 if (procs[i].ki_flag & P_INMEM)
2007 pse.cpu_user_counter = procs[i].ki_rusage.ru_utime.tv_usec
2008 + (1000000lu * procs[i].ki_rusage.ru_utime.tv_sec);
2009 pse.cpu_system_counter = procs[i].ki_rusage.ru_stime.tv_usec
2010 + (1000000lu * procs[i].ki_rusage.ru_stime.tv_sec);
2019 /* context switch counters not implemented */
2020 pse.cswitch_vol = -1;
2021 pse.cswitch_invol = -1;
2023 ps_list_add (procs[i].ki_comm, have_cmdline ? cmdline : NULL, &pse);
2025 switch (procs[i].ki_stat)
2027 case SSTOP: stopped++; break;
2028 case SSLEEP: sleeping++; break;
2029 case SRUN: running++; break;
2030 case SIDL: idle++; break;
2031 case SWAIT: wait++; break;
2032 case SLOCK: blocked++; break;
2033 case SZOMB: zombies++; break;
2035 } /* if ((proc_ptr == NULL) || (proc_ptr->ki_pid != procs[i].ki_pid)) */
2040 ps_submit_state ("running", running);
2041 ps_submit_state ("sleeping", sleeping);
2042 ps_submit_state ("zombies", zombies);
2043 ps_submit_state ("stopped", stopped);
2044 ps_submit_state ("blocked", blocked);
2045 ps_submit_state ("idle", idle);
2046 ps_submit_state ("wait", wait);
2048 for (procstat_t *ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
2049 ps_submit_proc_list (ps_ptr);
2050 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
2052 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_OPENBSD
2063 struct kinfo_proc *procs; /* array of processes */
2064 struct kinfo_proc *proc_ptr = NULL;
2065 int count; /* returns number of processes */
2067 procstat_entry_t pse;
2071 /* Open the kvm interface, get a descriptor */
2072 kd = kvm_open (NULL, NULL, NULL, 0, errbuf);
2075 ERROR ("processes plugin: Cannot open kvm interface: %s",
2080 /* Get the list of processes. */
2081 procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc), &count);
2084 ERROR ("processes plugin: Cannot get kvm processes list: %s",
2090 /* Iterate through the processes in kinfo_proc */
2091 for (int i = 0; i < count; i++)
2093 /* Create only one process list entry per _process_, i.e.
2094 * filter out threads (duplicate PID entries). */
2095 if ((proc_ptr == NULL) || (proc_ptr->p_pid != procs[i].p_pid))
2097 char cmdline[CMDLINE_BUFFER_SIZE] = "";
2098 _Bool have_cmdline = 0;
2100 proc_ptr = &(procs[i]);
2101 /* Don't probe zombie processes */
2102 if (!P_ZOMBIE(proc_ptr))
2108 /* retrieve the arguments */
2109 argv = kvm_getargv (kd, proc_ptr, /* nchr = */ 0);
2111 if ((argv != NULL) && (argv[0] != NULL))
2113 while (argv[argc] != NULL)
2116 status = strjoin (cmdline, sizeof (cmdline), argv, argc, " ");
2118 WARNING ("processes plugin: Command line did not fit into buffer.");
2122 } /* if (process has argument list) */
2124 memset (&pse, 0, sizeof (pse));
2125 pse.id = procs[i].p_pid;
2129 pse.num_lwp = 1; /* XXX: accumulate p_tid values for a single p_pid ? */
2131 pse.vmem_rss = procs[i].p_vm_rssize * pagesize;
2132 pse.vmem_data = procs[i].p_vm_dsize * pagesize;
2133 pse.vmem_code = procs[i].p_vm_tsize * pagesize;
2134 pse.stack_size = procs[i].p_vm_ssize * pagesize;
2135 pse.vmem_size = pse.stack_size + pse.vmem_code + pse.vmem_data;
2136 pse.vmem_minflt = 0;
2137 pse.vmem_minflt_counter = procs[i].p_uru_minflt;
2138 pse.vmem_majflt = 0;
2139 pse.vmem_majflt_counter = procs[i].p_uru_majflt;
2143 pse.cpu_user_counter = procs[i].p_uutime_usec +
2144 (1000000lu * procs[i].p_uutime_sec);
2145 pse.cpu_system_counter = procs[i].p_ustime_usec +
2146 (1000000lu * procs[i].p_ustime_sec);
2154 /* context switch counters not implemented */
2155 pse.cswitch_vol = -1;
2156 pse.cswitch_invol = -1;
2158 ps_list_add (procs[i].p_comm, have_cmdline ? cmdline : NULL, &pse);
2160 switch (procs[i].p_stat)
2162 case SSTOP: stopped++; break;
2163 case SSLEEP: sleeping++; break;
2164 case SRUN: running++; break;
2165 case SIDL: idle++; break;
2166 case SONPROC: onproc++; break;
2167 case SDEAD: dead++; break;
2168 case SZOMB: zombies++; break;
2170 } /* if ((proc_ptr == NULL) || (proc_ptr->p_pid != procs[i].p_pid)) */
2175 ps_submit_state ("running", running);
2176 ps_submit_state ("sleeping", sleeping);
2177 ps_submit_state ("zombies", zombies);
2178 ps_submit_state ("stopped", stopped);
2179 ps_submit_state ("onproc", onproc);
2180 ps_submit_state ("idle", idle);
2181 ps_submit_state ("dead", dead);
2183 for (procstat_t *ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
2184 ps_submit_proc_list (ps_ptr);
2185 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_OPENBSD */
2187 #elif HAVE_PROCINFO_H
2199 procstat_entry_t pse;
2202 while ((nprocs = getprocs64 (procentry, sizeof(struct procentry64),
2203 /* fdsinfo = */ NULL, sizeof(struct fdsinfo64),
2204 &pindex, MAXPROCENTRY)) > 0)
2206 for (int i = 0; i < nprocs; i++)
2210 char arglist[MAXARGLN+1];
2214 if (procentry[i].pi_state == SNONE) continue;
2215 /* if (procentry[i].pi_state == SZOMB) FIXME */
2217 cmdline = procentry[i].pi_comm;
2218 cargs = procentry[i].pi_comm;
2219 if ( procentry[i].pi_flags & SKPROC )
2221 if (procentry[i].pi_pid == 0)
2222 cmdline = "swapper";
2227 if (getargs(&procentry[i], sizeof(struct procentry64), arglist, MAXARGLN) >= 0)
2232 while (++n < MAXARGLN)
2234 if (arglist[n] == '\0')
2236 if (arglist[n+1] == '\0')
2245 pse.id = procentry[i].pi_pid;
2247 pse.num_lwp = procentry[i].pi_thcount;
2251 while ((nthreads = getthrds64(procentry[i].pi_pid,
2252 thrdentry, sizeof(struct thrdentry64),
2253 &thindex, MAXTHRDENTRY)) > 0)
2257 for (j=0; j< nthreads; j++)
2259 switch (thrdentry[j].ti_state)
2261 /* case TSNONE: break; */
2262 case TSIDL: blocked++; break; /* FIXME is really blocked */
2263 case TSRUN: running++; break;
2264 case TSSLEEP: sleeping++; break;
2265 case TSSWAP: paging++; break;
2266 case TSSTOP: stopped++; break;
2267 case TSZOMB: zombies++; break;
2270 if (nthreads < MAXTHRDENTRY)
2275 /* tv_usec is nanosec ??? */
2276 pse.cpu_user_counter = procentry[i].pi_ru.ru_utime.tv_sec * 1000000 +
2277 procentry[i].pi_ru.ru_utime.tv_usec / 1000;
2280 /* tv_usec is nanosec ??? */
2281 pse.cpu_system_counter = procentry[i].pi_ru.ru_stime.tv_sec * 1000000 +
2282 procentry[i].pi_ru.ru_stime.tv_usec / 1000;
2284 pse.vmem_minflt = 0;
2285 pse.vmem_minflt_counter = procentry[i].pi_minflt;
2286 pse.vmem_majflt = 0;
2287 pse.vmem_majflt_counter = procentry[i].pi_majflt;
2289 pse.vmem_size = procentry[i].pi_tsize + procentry[i].pi_dvm * pagesize;
2290 pse.vmem_rss = (procentry[i].pi_drss + procentry[i].pi_trss) * pagesize;
2301 pse.cswitch_vol = -1;
2302 pse.cswitch_invol = -1;
2304 ps_list_add (cmdline, cargs, &pse);
2305 } /* for (i = 0 .. nprocs) */
2307 if (nprocs < MAXPROCENTRY)
2309 } /* while (getprocs64() > 0) */
2310 ps_submit_state ("running", running);
2311 ps_submit_state ("sleeping", sleeping);
2312 ps_submit_state ("zombies", zombies);
2313 ps_submit_state ("stopped", stopped);
2314 ps_submit_state ("paging", paging);
2315 ps_submit_state ("blocked", blocked);
2317 for (procstat_t *ps = list_head_g; ps != NULL; ps = ps->next)
2318 ps_submit_proc_list (ps);
2319 /* #endif HAVE_PROCINFO_H */
2321 #elif KERNEL_SOLARIS
2323 * The Solaris section adds a few more process states and removes some
2324 * process states compared to linux. Most notably there is no "PAGING"
2325 * and "BLOCKED" state for a process. The rest is similar to the linux
2343 char cmdline[PRARGSZ];
2347 proc = opendir ("/proc");
2351 while ((ent = readdir(proc)) != NULL)
2355 procstat_entry_t pse;
2358 if (!isdigit ((int) ent->d_name[0]))
2361 pid = strtol (ent->d_name, &endptr, 10);
2362 if (*endptr != 0) /* value didn't completely parse as a number */
2365 status = ps_read_process (pid, &ps, &state);
2368 DEBUG("ps_read_process failed: %i", status);
2372 memset (&pse, 0, sizeof (pse));
2376 pse.num_proc = ps.num_proc;
2377 pse.num_lwp = ps.num_lwp;
2378 pse.vmem_size = ps.vmem_size;
2379 pse.vmem_rss = ps.vmem_rss;
2380 pse.vmem_data = ps.vmem_data;
2381 pse.vmem_code = ps.vmem_code;
2382 pse.stack_size = ps.stack_size;
2384 pse.vmem_minflt = 0;
2385 pse.vmem_minflt_counter = ps.vmem_minflt_counter;
2386 pse.vmem_majflt = 0;
2387 pse.vmem_majflt_counter = ps.vmem_majflt_counter;
2390 pse.cpu_user_counter = ps.cpu_user_counter;
2392 pse.cpu_system_counter = ps.cpu_system_counter;
2394 pse.io_rchar = ps.io_rchar;
2395 pse.io_wchar = ps.io_wchar;
2396 pse.io_syscr = ps.io_syscr;
2397 pse.io_syscw = ps.io_syscw;
2399 pse.cswitch_vol = -1;
2400 pse.cswitch_invol = -1;
2404 case 'R': running++; break;
2405 case 'S': sleeping++; break;
2406 case 'E': detached++; break;
2407 case 'Z': zombies++; break;
2408 case 'T': stopped++; break;
2409 case 'A': daemon++; break;
2410 case 'Y': system++; break;
2411 case 'O': orphan++; break;
2415 ps_list_add (ps.name,
2416 ps_get_cmdline (pid, ps.name, cmdline, sizeof (cmdline)),
2418 } /* while(readdir) */
2421 ps_submit_state ("running", running);
2422 ps_submit_state ("sleeping", sleeping);
2423 ps_submit_state ("zombies", zombies);
2424 ps_submit_state ("stopped", stopped);
2425 ps_submit_state ("detached", detached);
2426 ps_submit_state ("daemon", daemon);
2427 ps_submit_state ("system", system);
2428 ps_submit_state ("orphan", orphan);
2430 for (procstat_t *ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
2431 ps_submit_proc_list (ps_ptr);
2434 #endif /* KERNEL_SOLARIS */
2439 void module_register (void)
2441 plugin_register_complex_config ("processes", ps_config);
2442 plugin_register_init ("processes", ps_init);
2443 plugin_register_read ("processes", ps_read);
2444 } /* void module_register */