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>
40 #include "configfile.h"
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>
83 # if HAVE_SYS_SYSCTL_H
84 # include <sys/sysctl.h>
86 /* #endif HAVE_THREAD_INFO */
89 # if HAVE_LINUX_CONFIG_H
90 # include <linux/config.h>
93 # define CONFIG_HZ 100
95 /* #endif KERNEL_LINUX */
97 #elif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD)
99 # include <sys/param.h>
100 # include <sys/sysctl.h>
101 # include <sys/user.h>
102 # include <sys/proc.h>
103 /* #endif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD) */
105 #elif HAVE_PROCINFO_H
106 # include <procinfo.h>
107 # include <sys/types.h>
109 #define MAXPROCENTRY 32
110 #define MAXTHRDENTRY 16
111 #define MAXARGLN 1024
112 /* #endif HAVE_PROCINFO_H */
115 /* Hack: Avoid #error when building a 32-bit binary with
116 * _FILE_OFFSET_BITS=64. There is a reason for this #error, as one
117 * of the structures in <sys/procfs.h> uses an off_t, but that
118 * isn't relevant to our usage of procfs. */
119 #if !defined(_LP64) && _FILE_OFFSET_BITS == 64
121 # undef _FILE_OFFSET_BITS
127 # define _FILE_OFFSET_BITS 64
131 # include <sys/user.h>
133 /* #endif KERNEL_SOLARIS */
136 # error "No applicable input method."
147 #ifndef CMDLINE_BUFFER_SIZE
148 # if defined(ARG_MAX) && (ARG_MAX < 4096)
149 # define CMDLINE_BUFFER_SIZE ARG_MAX
151 # define CMDLINE_BUFFER_SIZE 4096
155 typedef struct procstat_entry_s
160 unsigned long num_proc;
161 unsigned long num_lwp;
162 unsigned long vmem_size;
163 unsigned long vmem_rss;
164 unsigned long vmem_data;
165 unsigned long vmem_code;
166 unsigned long stack_size;
168 unsigned long vmem_minflt;
169 unsigned long vmem_majflt;
170 derive_t vmem_minflt_counter;
171 derive_t vmem_majflt_counter;
173 unsigned long cpu_user;
174 unsigned long cpu_system;
175 derive_t cpu_user_counter;
176 derive_t cpu_system_counter;
184 struct procstat_entry_s *next;
187 #define PROCSTAT_NAME_LEN 256
188 typedef struct procstat
190 char name[PROCSTAT_NAME_LEN];
195 unsigned long num_proc;
196 unsigned long num_lwp;
197 unsigned long vmem_size;
198 unsigned long vmem_rss;
199 unsigned long vmem_data;
200 unsigned long vmem_code;
201 unsigned long stack_size;
203 derive_t vmem_minflt_counter;
204 derive_t vmem_majflt_counter;
206 derive_t cpu_user_counter;
207 derive_t cpu_system_counter;
215 struct procstat *next;
216 struct procstat_entry_s *instances;
219 static procstat_t *list_head_g = NULL;
222 static mach_port_t port_host_self;
223 static mach_port_t port_task_self;
225 static processor_set_name_array_t pset_list;
226 static mach_msg_type_number_t pset_list_len;
227 /* #endif HAVE_THREAD_INFO */
230 static long pagesize_g;
231 /* #endif KERNEL_LINUX */
233 #elif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD)
235 /* #endif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD) */
237 #elif HAVE_PROCINFO_H
238 static struct procentry64 procentry[MAXPROCENTRY];
239 static struct thrdentry64 thrdentry[MAXTHRDENTRY];
242 #ifndef _AIXVERSION_610
243 int getprocs64 (void *procsinfo, int sizproc, void *fdsinfo, int sizfd, pid_t *index, int count);
244 int getthrds64( pid_t, void *, int, tid64_t *, int );
246 int getargs (void *processBuffer, int bufferLen, char *argsBuffer, int argsLen);
247 #endif /* HAVE_PROCINFO_H */
249 /* put name of process from config to list_head_g tree
250 * list_head_g is a list of 'procstat_t' structs with
251 * processes names we want to watch */
252 static void ps_list_register (const char *name, const char *regexp)
258 new = (procstat_t *) malloc (sizeof (procstat_t));
261 ERROR ("processes plugin: ps_list_register: malloc failed.");
264 memset (new, 0, sizeof (procstat_t));
265 sstrncpy (new->name, name, sizeof (new->name));
270 DEBUG ("ProcessMatch: adding \"%s\" as criteria to process %s.", regexp, name);
271 new->re = (regex_t *) malloc (sizeof (regex_t));
274 ERROR ("processes plugin: ps_list_register: malloc failed.");
279 status = regcomp (new->re, regexp, REG_EXTENDED | REG_NOSUB);
282 DEBUG ("ProcessMatch: compiling the regular expression \"%s\" failed.", regexp);
291 ERROR ("processes plugin: ps_list_register: "
292 "Regular expression \"%s\" found in config "
293 "file, but support for regular expressions "
294 "has been disabled at compile time.",
301 for (ptr = list_head_g; ptr != NULL; ptr = ptr->next)
303 if (strcmp (ptr->name, name) == 0)
305 WARNING ("processes plugin: You have configured more "
306 "than one `Process' or "
307 "`ProcessMatch' with the same name. "
308 "All but the first setting will be "
317 if (ptr->next == NULL)
325 } /* void ps_list_register */
327 /* try to match name against entry, returns 1 if success */
328 static int ps_list_match (const char *name, const char *cmdline, procstat_t *ps)
337 if ((str == NULL) || (str[0] == 0))
340 assert (str != NULL);
342 status = regexec (ps->re, str,
351 if (strcmp (ps->name, name) == 0)
355 } /* int ps_list_match */
357 /* add process entry to 'instances' of process 'name' (or refresh it) */
358 static void ps_list_add (const char *name, const char *cmdline, procstat_entry_t *entry)
361 procstat_entry_t *pse;
366 for (ps = list_head_g; ps != NULL; ps = ps->next)
368 if ((ps_list_match (name, cmdline, ps)) == 0)
371 for (pse = ps->instances; pse != NULL; pse = pse->next)
372 if ((pse->id == entry->id) || (pse->next == NULL))
375 if ((pse == NULL) || (pse->id != entry->id))
377 procstat_entry_t *new;
379 new = (procstat_entry_t *) malloc (sizeof (procstat_entry_t));
382 memset (new, 0, sizeof (procstat_entry_t));
394 pse->num_proc = entry->num_proc;
395 pse->num_lwp = entry->num_lwp;
396 pse->vmem_size = entry->vmem_size;
397 pse->vmem_rss = entry->vmem_rss;
398 pse->vmem_data = entry->vmem_data;
399 pse->vmem_code = entry->vmem_code;
400 pse->stack_size = entry->stack_size;
401 pse->io_rchar = entry->io_rchar;
402 pse->io_wchar = entry->io_wchar;
403 pse->io_syscr = entry->io_syscr;
404 pse->io_syscw = entry->io_syscw;
406 ps->num_proc += pse->num_proc;
407 ps->num_lwp += pse->num_lwp;
408 ps->vmem_size += pse->vmem_size;
409 ps->vmem_rss += pse->vmem_rss;
410 ps->vmem_data += pse->vmem_data;
411 ps->vmem_code += pse->vmem_code;
412 ps->stack_size += pse->stack_size;
414 ps->io_rchar += ((pse->io_rchar == -1)?0:pse->io_rchar);
415 ps->io_wchar += ((pse->io_wchar == -1)?0:pse->io_wchar);
416 ps->io_syscr += ((pse->io_syscr == -1)?0:pse->io_syscr);
417 ps->io_syscw += ((pse->io_syscw == -1)?0:pse->io_syscw);
419 if ((entry->vmem_minflt_counter == 0)
420 && (entry->vmem_majflt_counter == 0))
422 pse->vmem_minflt_counter += entry->vmem_minflt;
423 pse->vmem_minflt = entry->vmem_minflt;
425 pse->vmem_majflt_counter += entry->vmem_majflt;
426 pse->vmem_majflt = entry->vmem_majflt;
430 if (entry->vmem_minflt_counter < pse->vmem_minflt_counter)
432 pse->vmem_minflt = entry->vmem_minflt_counter
433 + (ULONG_MAX - pse->vmem_minflt_counter);
437 pse->vmem_minflt = entry->vmem_minflt_counter - pse->vmem_minflt_counter;
439 pse->vmem_minflt_counter = entry->vmem_minflt_counter;
441 if (entry->vmem_majflt_counter < pse->vmem_majflt_counter)
443 pse->vmem_majflt = entry->vmem_majflt_counter
444 + (ULONG_MAX - pse->vmem_majflt_counter);
448 pse->vmem_majflt = entry->vmem_majflt_counter - pse->vmem_majflt_counter;
450 pse->vmem_majflt_counter = entry->vmem_majflt_counter;
453 ps->vmem_minflt_counter += pse->vmem_minflt;
454 ps->vmem_majflt_counter += pse->vmem_majflt;
456 if ((entry->cpu_user_counter == 0)
457 && (entry->cpu_system_counter == 0))
459 pse->cpu_user_counter += entry->cpu_user;
460 pse->cpu_user = entry->cpu_user;
462 pse->cpu_system_counter += entry->cpu_system;
463 pse->cpu_system = entry->cpu_system;
467 if (entry->cpu_user_counter < pse->cpu_user_counter)
469 pse->cpu_user = entry->cpu_user_counter
470 + (ULONG_MAX - pse->cpu_user_counter);
474 pse->cpu_user = entry->cpu_user_counter - pse->cpu_user_counter;
476 pse->cpu_user_counter = entry->cpu_user_counter;
478 if (entry->cpu_system_counter < pse->cpu_system_counter)
480 pse->cpu_system = entry->cpu_system_counter
481 + (ULONG_MAX - pse->cpu_system_counter);
485 pse->cpu_system = entry->cpu_system_counter - pse->cpu_system_counter;
487 pse->cpu_system_counter = entry->cpu_system_counter;
490 ps->cpu_user_counter += pse->cpu_user;
491 ps->cpu_system_counter += pse->cpu_system;
495 /* remove old entries from instances of processes in list_head_g */
496 static void ps_list_reset (void)
499 procstat_entry_t *pse;
500 procstat_entry_t *pse_prev;
502 for (ps = list_head_g; ps != NULL; ps = ps->next)
522 DEBUG ("Removing this procstat entry cause it's too old: "
523 "id = %lu; name = %s;",
526 if (pse_prev == NULL)
528 ps->instances = pse->next;
534 pse_prev->next = pse->next;
536 pse = pse_prev->next;
545 } /* while (pse != NULL) */
546 } /* for (ps = list_head_g; ps != NULL; ps = ps->next) */
549 /* put all pre-defined 'Process' names from config to list_head_g tree */
550 static int ps_config (oconfig_item_t *ci)
555 const size_t max_procname_len = 15;
556 #elif KERNEL_SOLARIS || KERNEL_FREEBSD
557 const size_t max_procname_len = MAXCOMLEN -1;
560 for (i = 0; i < ci->children_num; ++i) {
561 oconfig_item_t *c = ci->children + i;
563 if (strcasecmp (c->key, "Process") == 0)
565 if ((c->values_num != 1)
566 || (OCONFIG_TYPE_STRING != c->values[0].type)) {
567 ERROR ("processes plugin: `Process' expects exactly "
568 "one string argument (got %i).",
573 if (c->children_num != 0) {
574 WARNING ("processes plugin: the `Process' config option "
575 "does not expect any child elements -- ignoring "
576 "content (%i elements) of the <Process '%s'> block.",
577 c->children_num, c->values[0].value.string);
580 #if KERNEL_LINUX || KERNEL_SOLARIS || KERNEL_FREEBSD
581 if (strlen (c->values[0].value.string) > max_procname_len) {
582 WARNING ("processes plugin: this platform has a %zu character limit "
583 "to process names. The `Process \"%s\"' option will "
584 "not work as expected.",
585 max_procname_len, c->values[0].value.string);
589 ps_list_register (c->values[0].value.string, NULL);
591 else if (strcasecmp (c->key, "ProcessMatch") == 0)
593 if ((c->values_num != 2)
594 || (OCONFIG_TYPE_STRING != c->values[0].type)
595 || (OCONFIG_TYPE_STRING != c->values[1].type))
597 ERROR ("processes plugin: `ProcessMatch' needs exactly "
598 "two string arguments (got %i).",
603 if (c->children_num != 0) {
604 WARNING ("processes plugin: the `ProcessMatch' config option "
605 "does not expect any child elements -- ignoring "
606 "content (%i elements) of the <ProcessMatch '%s' '%s'> "
607 "block.", c->children_num, c->values[0].value.string,
608 c->values[1].value.string);
611 ps_list_register (c->values[0].value.string,
612 c->values[1].value.string);
616 ERROR ("processes plugin: The `%s' configuration option is not "
617 "understood and will be ignored.", c->key);
625 static int ps_init (void)
628 kern_return_t status;
630 port_host_self = mach_host_self ();
631 port_task_self = mach_task_self ();
633 if (pset_list != NULL)
635 vm_deallocate (port_task_self,
636 (vm_address_t) pset_list,
637 pset_list_len * sizeof (processor_set_t));
642 if ((status = host_processor_sets (port_host_self,
644 &pset_list_len)) != KERN_SUCCESS)
646 ERROR ("host_processor_sets failed: %s\n",
647 mach_error_string (status));
652 /* #endif HAVE_THREAD_INFO */
655 pagesize_g = sysconf(_SC_PAGESIZE);
656 DEBUG ("pagesize_g = %li; CONFIG_HZ = %i;",
657 pagesize_g, CONFIG_HZ);
658 /* #endif KERNEL_LINUX */
660 #elif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD)
661 pagesize = getpagesize();
662 /* #endif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD) */
664 #elif HAVE_PROCINFO_H
665 pagesize = getpagesize();
666 #endif /* HAVE_PROCINFO_H */
671 /* submit global state (e.g.: qty of zombies, running, etc..) */
672 static void ps_submit_state (const char *state, double value)
675 value_list_t vl = VALUE_LIST_INIT;
677 values[0].gauge = value;
681 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
682 sstrncpy (vl.plugin, "processes", sizeof (vl.plugin));
683 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
684 sstrncpy (vl.type, "ps_state", sizeof (vl.type));
685 sstrncpy (vl.type_instance, state, sizeof (vl.type_instance));
687 plugin_dispatch_values (&vl);
690 /* submit info about specific process (e.g.: memory taken, cpu usage, etc..) */
691 static void ps_submit_proc_list (procstat_t *ps)
694 value_list_t vl = VALUE_LIST_INIT;
698 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
699 sstrncpy (vl.plugin, "processes", sizeof (vl.plugin));
700 sstrncpy (vl.plugin_instance, ps->name, sizeof (vl.plugin_instance));
702 sstrncpy (vl.type, "ps_vm", sizeof (vl.type));
703 vl.values[0].gauge = ps->vmem_size;
705 plugin_dispatch_values (&vl);
707 sstrncpy (vl.type, "ps_rss", sizeof (vl.type));
708 vl.values[0].gauge = ps->vmem_rss;
710 plugin_dispatch_values (&vl);
712 sstrncpy (vl.type, "ps_data", sizeof (vl.type));
713 vl.values[0].gauge = ps->vmem_data;
715 plugin_dispatch_values (&vl);
717 sstrncpy (vl.type, "ps_code", sizeof (vl.type));
718 vl.values[0].gauge = ps->vmem_code;
720 plugin_dispatch_values (&vl);
722 sstrncpy (vl.type, "ps_stacksize", sizeof (vl.type));
723 vl.values[0].gauge = ps->stack_size;
725 plugin_dispatch_values (&vl);
727 sstrncpy (vl.type, "ps_cputime", sizeof (vl.type));
728 vl.values[0].derive = ps->cpu_user_counter;
729 vl.values[1].derive = ps->cpu_system_counter;
731 plugin_dispatch_values (&vl);
733 sstrncpy (vl.type, "ps_count", sizeof (vl.type));
734 vl.values[0].gauge = ps->num_proc;
735 vl.values[1].gauge = ps->num_lwp;
737 plugin_dispatch_values (&vl);
739 sstrncpy (vl.type, "ps_pagefaults", sizeof (vl.type));
740 vl.values[0].derive = ps->vmem_minflt_counter;
741 vl.values[1].derive = ps->vmem_majflt_counter;
743 plugin_dispatch_values (&vl);
745 if ( (ps->io_rchar != -1) && (ps->io_wchar != -1) )
747 sstrncpy (vl.type, "ps_disk_octets", sizeof (vl.type));
748 vl.values[0].derive = ps->io_rchar;
749 vl.values[1].derive = ps->io_wchar;
751 plugin_dispatch_values (&vl);
754 if ( (ps->io_syscr != -1) && (ps->io_syscw != -1) )
756 sstrncpy (vl.type, "ps_disk_ops", sizeof (vl.type));
757 vl.values[0].derive = ps->io_syscr;
758 vl.values[1].derive = ps->io_syscw;
760 plugin_dispatch_values (&vl);
763 DEBUG ("name = %s; num_proc = %lu; num_lwp = %lu; "
764 "vmem_size = %lu; vmem_rss = %lu; vmem_data = %lu; "
766 "vmem_minflt_counter = %"PRIi64"; vmem_majflt_counter = %"PRIi64"; "
767 "cpu_user_counter = %"PRIi64"; cpu_system_counter = %"PRIi64"; "
768 "io_rchar = %"PRIi64"; io_wchar = %"PRIi64"; "
769 "io_syscr = %"PRIi64"; io_syscw = %"PRIi64";",
770 ps->name, ps->num_proc, ps->num_lwp,
771 ps->vmem_size, ps->vmem_rss,
772 ps->vmem_data, ps->vmem_code,
773 ps->vmem_minflt_counter, ps->vmem_majflt_counter,
774 ps->cpu_user_counter, ps->cpu_system_counter,
775 ps->io_rchar, ps->io_wchar, ps->io_syscr, ps->io_syscw);
776 } /* void ps_submit_proc_list */
778 #if KERNEL_LINUX || KERNEL_SOLARIS
779 static void ps_submit_fork_rate (derive_t value)
782 value_list_t vl = VALUE_LIST_INIT;
784 values[0].derive = value;
788 sstrncpy(vl.host, hostname_g, sizeof (vl.host));
789 sstrncpy(vl.plugin, "processes", sizeof (vl.plugin));
790 sstrncpy(vl.plugin_instance, "", sizeof (vl.plugin_instance));
791 sstrncpy(vl.type, "fork_rate", sizeof (vl.type));
792 sstrncpy(vl.type_instance, "", sizeof (vl.type_instance));
794 plugin_dispatch_values(&vl);
796 #endif /* KERNEL_LINUX || KERNEL_SOLARIS*/
798 /* ------- additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
800 static int ps_read_tasks (long pid)
807 ssnprintf (dirname, sizeof (dirname), "/proc/%li/task", pid);
809 if ((dh = opendir (dirname)) == NULL)
811 DEBUG ("Failed to open directory `%s'", dirname);
815 while ((ent = readdir (dh)) != NULL)
817 if (!isdigit ((int) ent->d_name[0]))
824 return ((count >= 1) ? count : 1);
825 } /* int *ps_read_tasks */
827 /* Read advanced virtual memory data from /proc/pid/status */
828 static procstat_t *ps_read_vmem (long pid, procstat_t *ps)
833 unsigned long long lib = 0;
834 unsigned long long exe = 0;
835 unsigned long long data = 0;
839 ssnprintf (filename, sizeof (filename), "/proc/%li/status", pid);
840 if ((fh = fopen (filename, "r")) == NULL)
843 while (fgets (buffer, sizeof(buffer), fh) != NULL)
848 if (strncmp (buffer, "Vm", 2) != 0)
851 numfields = strsplit (buffer, fields,
852 STATIC_ARRAY_SIZE (fields));
859 tmp = strtoll (fields[1], &endptr, /* base = */ 10);
860 if ((errno == 0) && (endptr != fields[1]))
862 if (strncmp (buffer, "VmData", 6) == 0)
866 else if (strncmp (buffer, "VmLib", 5) == 0)
870 else if (strncmp(buffer, "VmExe", 5) == 0)
875 } /* while (fgets) */
880 WARNING ("processes: fclose: %s",
881 sstrerror (errno, errbuf, sizeof (errbuf)));
884 ps->vmem_data = data * 1024;
885 ps->vmem_code = (exe + lib) * 1024;
888 } /* procstat_t *ps_read_vmem */
890 static procstat_t *ps_read_io (long pid, procstat_t *ps)
899 ssnprintf (filename, sizeof (filename), "/proc/%li/io", pid);
900 if ((fh = fopen (filename, "r")) == NULL)
903 while (fgets (buffer, sizeof (buffer), fh) != NULL)
905 derive_t *val = NULL;
909 if (strncasecmp (buffer, "rchar:", 6) == 0)
910 val = &(ps->io_rchar);
911 else if (strncasecmp (buffer, "wchar:", 6) == 0)
912 val = &(ps->io_wchar);
913 else if (strncasecmp (buffer, "syscr:", 6) == 0)
914 val = &(ps->io_syscr);
915 else if (strncasecmp (buffer, "syscw:", 6) == 0)
916 val = &(ps->io_syscw);
920 numfields = strsplit (buffer, fields,
921 STATIC_ARRAY_SIZE (fields));
928 tmp = strtoll (fields[1], &endptr, /* base = */ 10);
929 if ((errno != 0) || (endptr == fields[1]))
932 *val = (derive_t) tmp;
933 } /* while (fgets) */
938 WARNING ("processes: fclose: %s",
939 sstrerror (errno, errbuf, sizeof (errbuf)));
943 } /* procstat_t *ps_read_io */
945 static int ps_read_process (long pid, procstat_t *ps, char *state)
956 size_t name_start_pos;
960 derive_t cpu_user_counter;
961 derive_t cpu_system_counter;
962 long long unsigned vmem_size;
963 long long unsigned vmem_rss;
964 long long unsigned stack_size;
966 memset (ps, 0, sizeof (procstat_t));
968 ssnprintf (filename, sizeof (filename), "/proc/%li/stat", pid);
970 buffer_len = read_file_contents (filename,
971 buffer, sizeof(buffer) - 1);
974 buffer[buffer_len] = 0;
976 /* The name of the process is enclosed in parens. Since the name can
977 * contain parens itself, spaces, numbers and pretty much everything
978 * else, use these to determine the process name. We don't use
979 * strchr(3) and strrchr(3) to avoid pointer arithmetic which would
980 * otherwise be required to determine name_len. */
982 while ((buffer[name_start_pos] != '(')
983 && (name_start_pos < buffer_len))
986 name_end_pos = buffer_len;
987 while ((buffer[name_end_pos] != ')')
988 && (name_end_pos > 0))
991 /* Either '(' or ')' is not found or they are in the wrong order.
992 * Anyway, something weird that shouldn't happen ever. */
993 if (name_start_pos >= name_end_pos)
995 ERROR ("processes plugin: name_start_pos = %zu >= name_end_pos = %zu",
996 name_start_pos, name_end_pos);
1000 name_len = (name_end_pos - name_start_pos) - 1;
1001 if (name_len >= sizeof (ps->name))
1002 name_len = sizeof (ps->name) - 1;
1004 sstrncpy (ps->name, &buffer[name_start_pos + 1], name_len + 1);
1006 if ((buffer_len - name_end_pos) < 2)
1008 buffer_ptr = &buffer[name_end_pos + 2];
1010 fields_len = strsplit (buffer_ptr, fields, STATIC_ARRAY_SIZE (fields));
1011 if (fields_len < 22)
1013 DEBUG ("processes plugin: ps_read_process (pid = %li):"
1014 " `%s' has only %i fields..",
1015 pid, filename, fields_len);
1019 *state = fields[0][0];
1028 if ( (ps->num_lwp = ps_read_tasks (pid)) == -1 )
1030 /* returns -1 => kernel 2.4 */
1036 /* Leave the rest at zero if this is only a zombi */
1037 if (ps->num_proc == 0)
1039 DEBUG ("processes plugin: This is only a zombie: pid = %li; "
1040 "name = %s;", pid, ps->name);
1044 cpu_user_counter = atoll (fields[11]);
1045 cpu_system_counter = atoll (fields[12]);
1046 vmem_size = atoll (fields[20]);
1047 vmem_rss = atoll (fields[21]);
1048 ps->vmem_minflt_counter = atol (fields[7]);
1049 ps->vmem_majflt_counter = atol (fields[9]);
1052 unsigned long long stack_start = atoll (fields[25]);
1053 unsigned long long stack_ptr = atoll (fields[26]);
1055 stack_size = (stack_start > stack_ptr)
1056 ? stack_start - stack_ptr
1057 : stack_ptr - stack_start;
1060 /* Convert jiffies to useconds */
1061 cpu_user_counter = cpu_user_counter * 1000000 / CONFIG_HZ;
1062 cpu_system_counter = cpu_system_counter * 1000000 / CONFIG_HZ;
1063 vmem_rss = vmem_rss * pagesize_g;
1065 if ( (ps_read_vmem(pid, ps)) == NULL)
1070 DEBUG("ps_read_process: did not get vmem data for pid %li", pid);
1073 ps->cpu_user_counter = cpu_user_counter;
1074 ps->cpu_system_counter = cpu_system_counter;
1075 ps->vmem_size = (unsigned long) vmem_size;
1076 ps->vmem_rss = (unsigned long) vmem_rss;
1077 ps->stack_size = (unsigned long) stack_size;
1079 if ( (ps_read_io (pid, ps)) == NULL)
1087 DEBUG("ps_read_process: not get io data for pid %li", pid);
1092 } /* int ps_read_process (...) */
1094 static char *ps_get_cmdline (long pid, char *name, char *buf, size_t buf_len)
1099 char file[PATH_MAX];
1104 if ((pid < 1) || (NULL == buf) || (buf_len < 2))
1107 ssnprintf (file, sizeof (file), "/proc/%li/cmdline", pid);
1110 fd = open (file, O_RDONLY);
1113 /* ENOENT means the process exited while we were handling it.
1114 * Don't complain about this, it only fills the logs. */
1115 if (errno != ENOENT)
1116 WARNING ("processes plugin: Failed to open `%s': %s.", file,
1117 sstrerror (errno, errbuf, sizeof (errbuf)));
1129 status = read (fd, (void *)buf_ptr, len);
1134 if ((EAGAIN == errno) || (EINTR == errno))
1137 WARNING ("processes plugin: Failed to read from `%s': %s.", file,
1138 sstrerror (errno, errbuf, sizeof (errbuf)));
1158 /* cmdline not available; e.g. kernel thread, zombie */
1162 ssnprintf (buf, buf_len, "[%s]", name);
1166 assert (n <= buf_len);
1173 /* remove trailing whitespace */
1174 while ((n > 0) && (isspace (buf[n]) || ('\0' == buf[n]))) {
1179 /* arguments are separated by '\0' in /proc/<pid>/cmdline */
1186 } /* char *ps_get_cmdline (...) */
1188 static int read_fork_rate (void)
1193 _Bool value_valid = 0;
1195 proc_stat = fopen ("/proc/stat", "r");
1196 if (proc_stat == NULL)
1199 ERROR ("processes plugin: fopen (/proc/stat) failed: %s",
1200 sstrerror (errno, errbuf, sizeof (errbuf)));
1204 while (fgets (buffer, sizeof (buffer), proc_stat) != NULL)
1210 fields_num = strsplit (buffer, fields,
1211 STATIC_ARRAY_SIZE (fields));
1212 if (fields_num != 2)
1215 if (strcmp ("processes", fields[0]) != 0)
1218 status = parse_value (fields[1], &value, DS_TYPE_DERIVE);
1229 ps_submit_fork_rate (value.derive);
1232 #endif /*KERNEL_LINUX */
1235 static char *ps_get_cmdline (long pid, char *name __attribute__((unused)), /* {{{ */
1236 char *buffer, size_t buffer_size)
1238 char path[PATH_MAX];
1242 snprintf(path, sizeof (path), "/proc/%li/psinfo", pid);
1244 status = read_file_contents (path, (void *) &info, sizeof (info));
1245 if (status != sizeof (info))
1247 ERROR ("processes plugin: Unexpected return value "
1248 "while reading \"%s\": "
1249 "Returned %i but expected %zu.",
1250 path, status, buffer_size);
1254 info.pr_psargs[sizeof (info.pr_psargs) - 1] = 0;
1255 sstrncpy (buffer, info.pr_psargs, buffer_size);
1258 } /* }}} int ps_get_cmdline */
1261 * Reads process information on the Solaris OS. The information comes mainly from
1262 * /proc/PID/status, /proc/PID/psinfo and /proc/PID/usage
1263 * The values for input and ouput chars are calculated "by hand"
1264 * Added a few "solaris" specific process states as well
1266 static int ps_read_process(long pid, procstat_t *ps, char *state)
1269 char f_psinfo[64], f_usage[64];
1272 pstatus_t *myStatus;
1276 snprintf(filename, sizeof (filename), "/proc/%li/status", pid);
1277 snprintf(f_psinfo, sizeof (f_psinfo), "/proc/%li/psinfo", pid);
1278 snprintf(f_usage, sizeof (f_usage), "/proc/%li/usage", pid);
1281 buffer = malloc(sizeof (pstatus_t));
1282 memset(buffer, 0, sizeof (pstatus_t));
1283 read_file_contents(filename, buffer, sizeof (pstatus_t));
1284 myStatus = (pstatus_t *) buffer;
1286 buffer = malloc(sizeof (psinfo_t));
1287 memset(buffer, 0, sizeof(psinfo_t));
1288 read_file_contents(f_psinfo, buffer, sizeof (psinfo_t));
1289 myInfo = (psinfo_t *) buffer;
1291 buffer = malloc(sizeof (prusage_t));
1292 memset(buffer, 0, sizeof(prusage_t));
1293 read_file_contents(f_usage, buffer, sizeof (prusage_t));
1294 myUsage = (prusage_t *) buffer;
1296 sstrncpy(ps->name, myInfo->pr_fname, sizeof (myInfo->pr_fname));
1297 ps->num_lwp = myStatus->pr_nlwp;
1298 if (myInfo->pr_wstat != 0) {
1301 *state = (char) 'Z';
1309 ps->num_lwp = myInfo->pr_nlwp;
1313 * Convert system time and user time from nanoseconds to microseconds
1314 * for compatibility with the linux module
1316 ps->cpu_system_counter = myStatus -> pr_stime.tv_nsec / 1000;
1317 ps->cpu_user_counter = myStatus -> pr_utime.tv_nsec / 1000;
1320 * Convert rssize from KB to bytes to be consistent w/ the linux module
1322 ps->vmem_rss = myInfo->pr_rssize * 1024;
1323 ps->vmem_size = myInfo->pr_size * 1024;
1324 ps->vmem_minflt_counter = myUsage->pr_minf;
1325 ps->vmem_majflt_counter = myUsage->pr_majf;
1328 * TODO: Data and code segment calculations for Solaris
1333 ps->stack_size = myStatus->pr_stksize;
1336 * Calculating input/ouput chars
1337 * Formula used is total chars / total blocks => chars/block
1338 * then convert input/output blocks to chars
1340 ulong_t tot_chars = myUsage->pr_ioch;
1341 ulong_t tot_blocks = myUsage->pr_inblk + myUsage->pr_oublk;
1342 ulong_t chars_per_block = 1;
1343 if (tot_blocks != 0)
1344 chars_per_block = tot_chars / tot_blocks;
1345 ps->io_rchar = myUsage->pr_inblk * chars_per_block;
1346 ps->io_wchar = myUsage->pr_oublk * chars_per_block;
1347 ps->io_syscr = myUsage->pr_sysc;
1348 ps->io_syscw = myUsage->pr_sysc;
1352 * TODO: Find way of setting BLOCKED and PAGING status
1355 *state = (char) 'R';
1356 if (myStatus->pr_flags & PR_ASLEEP)
1357 *state = (char) 'S';
1358 else if (myStatus->pr_flags & PR_STOPPED)
1359 *state = (char) 'T';
1360 else if (myStatus->pr_flags & PR_DETACH)
1361 *state = (char) 'E';
1362 else if (myStatus->pr_flags & PR_DAEMON)
1363 *state = (char) 'A';
1364 else if (myStatus->pr_flags & PR_ISSYS)
1365 *state = (char) 'Y';
1366 else if (myStatus->pr_flags & PR_ORPHAN)
1367 *state = (char) 'O';
1377 * Reads the number of threads created since the last reboot. On Solaris these
1378 * are retrieved from kstat (module cpu, name sys, class misc, stat nthreads).
1379 * The result is the sum for all the threads created on each cpu
1381 static int read_fork_rate()
1383 extern kstat_ctl_t *kc;
1384 kstat_t *ksp_chain = NULL;
1385 derive_t result = 0;
1390 for (ksp_chain = kc->kc_chain;
1392 ksp_chain = ksp_chain->ks_next)
1394 if ((strcmp (ksp_chain->ks_module, "cpu") == 0)
1395 && (strcmp (ksp_chain->ks_name, "sys") == 0)
1396 && (strcmp (ksp_chain->ks_class, "misc") == 0))
1400 kstat_read (kc, ksp_chain, NULL);
1402 tmp = get_kstat_value(ksp_chain, "nthreads");
1408 ps_submit_fork_rate (result);
1411 #endif /* KERNEL_SOLARIS */
1413 #if HAVE_THREAD_INFO
1414 static int mach_get_task_name (task_t t, int *pid, char *name, size_t name_max_len)
1418 struct kinfo_proc kp;
1423 mib[2] = KERN_PROC_PID;
1425 if (pid_for_task (t, pid) != KERN_SUCCESS)
1429 kp_size = sizeof (kp);
1430 if (sysctl (mib, 4, &kp, &kp_size, NULL, 0) != 0)
1433 if (name_max_len > (MAXCOMLEN + 1))
1434 name_max_len = MAXCOMLEN + 1;
1436 strncpy (name, kp.kp_proc.p_comm, name_max_len - 1);
1437 name[name_max_len - 1] = '\0';
1439 DEBUG ("pid = %i; name = %s;", *pid, name);
1441 /* We don't do the special handling for `p_comm == "LaunchCFMApp"' as
1442 * `top' does it, because it is a lot of work and only used when
1443 * debugging. -octo */
1447 #endif /* HAVE_THREAD_INFO */
1448 /* ------- end of additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
1450 /* do actual readings from kernel */
1451 static int ps_read (void)
1453 #if HAVE_THREAD_INFO
1454 kern_return_t status;
1457 processor_set_t port_pset_priv;
1460 task_array_t task_list;
1461 mach_msg_type_number_t task_list_len;
1464 char task_name[MAXCOMLEN + 1];
1467 thread_act_array_t thread_list;
1468 mach_msg_type_number_t thread_list_len;
1469 thread_basic_info_data_t thread_data;
1470 mach_msg_type_number_t thread_data_len;
1479 procstat_entry_t pse;
1484 * The Mach-concept is a little different from the traditional UNIX
1485 * concept: All the work is done in threads. Threads are contained in
1486 * `tasks'. Therefore, `task status' doesn't make much sense, since
1487 * it's actually a `thread status'.
1488 * Tasks are assigned to sets of processors, so that's where you go to
1491 for (pset = 0; pset < pset_list_len; pset++)
1493 if ((status = host_processor_set_priv (port_host_self,
1495 &port_pset_priv)) != KERN_SUCCESS)
1497 ERROR ("host_processor_set_priv failed: %s\n",
1498 mach_error_string (status));
1502 if ((status = processor_set_tasks (port_pset_priv,
1504 &task_list_len)) != KERN_SUCCESS)
1506 ERROR ("processor_set_tasks failed: %s\n",
1507 mach_error_string (status));
1508 mach_port_deallocate (port_task_self, port_pset_priv);
1512 for (task = 0; task < task_list_len; task++)
1515 if (mach_get_task_name (task_list[task],
1517 task_name, PROCSTAT_NAME_LEN) == 0)
1519 /* search for at least one match */
1520 for (ps = list_head_g; ps != NULL; ps = ps->next)
1521 /* FIXME: cmdline should be here instead of NULL */
1522 if (ps_list_match (task_name, NULL, ps) == 1)
1526 /* Collect more detailed statistics for this process */
1529 task_basic_info_data_t task_basic_info;
1530 mach_msg_type_number_t task_basic_info_len;
1531 task_events_info_data_t task_events_info;
1532 mach_msg_type_number_t task_events_info_len;
1533 task_absolutetime_info_data_t task_absolutetime_info;
1534 mach_msg_type_number_t task_absolutetime_info_len;
1536 memset (&pse, '\0', sizeof (pse));
1539 task_basic_info_len = TASK_BASIC_INFO_COUNT;
1540 status = task_info (task_list[task],
1542 (task_info_t) &task_basic_info,
1543 &task_basic_info_len);
1544 if (status != KERN_SUCCESS)
1546 ERROR ("task_info failed: %s",
1547 mach_error_string (status));
1548 continue; /* with next thread_list */
1551 task_events_info_len = TASK_EVENTS_INFO_COUNT;
1552 status = task_info (task_list[task],
1554 (task_info_t) &task_events_info,
1555 &task_events_info_len);
1556 if (status != KERN_SUCCESS)
1558 ERROR ("task_info failed: %s",
1559 mach_error_string (status));
1560 continue; /* with next thread_list */
1563 task_absolutetime_info_len = TASK_ABSOLUTETIME_INFO_COUNT;
1564 status = task_info (task_list[task],
1565 TASK_ABSOLUTETIME_INFO,
1566 (task_info_t) &task_absolutetime_info,
1567 &task_absolutetime_info_len);
1568 if (status != KERN_SUCCESS)
1570 ERROR ("task_info failed: %s",
1571 mach_error_string (status));
1572 continue; /* with next thread_list */
1576 pse.vmem_size = task_basic_info.virtual_size;
1577 pse.vmem_rss = task_basic_info.resident_size;
1578 /* Does not seem to be easily exposed */
1582 pse.vmem_minflt_counter = task_events_info.cow_faults;
1583 pse.vmem_majflt_counter = task_events_info.faults;
1585 pse.cpu_user_counter = task_absolutetime_info.total_user;
1586 pse.cpu_system_counter = task_absolutetime_info.total_system;
1589 status = task_threads (task_list[task], &thread_list,
1591 if (status != KERN_SUCCESS)
1593 /* Apple's `top' treats this case a zombie. It
1594 * makes sense to some extend: A `zombie'
1595 * thread is nonsense, since the task/process
1598 DEBUG ("task_threads failed: %s",
1599 mach_error_string (status));
1600 if (task_list[task] != port_task_self)
1601 mach_port_deallocate (port_task_self,
1603 continue; /* with next task_list */
1606 for (thread = 0; thread < thread_list_len; thread++)
1608 thread_data_len = THREAD_BASIC_INFO_COUNT;
1609 status = thread_info (thread_list[thread],
1611 (thread_info_t) &thread_data,
1613 if (status != KERN_SUCCESS)
1615 ERROR ("thread_info failed: %s",
1616 mach_error_string (status));
1617 if (task_list[task] != port_task_self)
1618 mach_port_deallocate (port_task_self,
1619 thread_list[thread]);
1620 continue; /* with next thread_list */
1626 switch (thread_data.run_state)
1628 case TH_STATE_RUNNING:
1631 case TH_STATE_STOPPED:
1632 /* What exactly is `halted'? */
1633 case TH_STATE_HALTED:
1636 case TH_STATE_WAITING:
1639 case TH_STATE_UNINTERRUPTIBLE:
1642 /* There is no `zombie' case here,
1643 * since there are no zombie-threads.
1644 * There's only zombie tasks, which are
1647 WARNING ("Unknown thread status: %i",
1648 thread_data.run_state);
1650 } /* switch (thread_data.run_state) */
1652 if (task_list[task] != port_task_self)
1654 status = mach_port_deallocate (port_task_self,
1655 thread_list[thread]);
1656 if (status != KERN_SUCCESS)
1657 ERROR ("mach_port_deallocate failed: %s",
1658 mach_error_string (status));
1660 } /* for (thread_list) */
1662 if ((status = vm_deallocate (port_task_self,
1663 (vm_address_t) thread_list,
1664 thread_list_len * sizeof (thread_act_t)))
1667 ERROR ("vm_deallocate failed: %s",
1668 mach_error_string (status));
1671 thread_list_len = 0;
1673 /* Only deallocate the task port, if it isn't our own.
1674 * Don't know what would happen in that case, but this
1675 * is what Apple's top does.. ;) */
1676 if (task_list[task] != port_task_self)
1678 status = mach_port_deallocate (port_task_self,
1680 if (status != KERN_SUCCESS)
1681 ERROR ("mach_port_deallocate failed: %s",
1682 mach_error_string (status));
1686 /* FIXME: cmdline should be here instead of NULL */
1687 ps_list_add (task_name, NULL, &pse);
1688 } /* for (task_list) */
1690 if ((status = vm_deallocate (port_task_self,
1691 (vm_address_t) task_list,
1692 task_list_len * sizeof (task_t))) != KERN_SUCCESS)
1694 ERROR ("vm_deallocate failed: %s",
1695 mach_error_string (status));
1700 if ((status = mach_port_deallocate (port_task_self, port_pset_priv))
1703 ERROR ("mach_port_deallocate failed: %s",
1704 mach_error_string (status));
1706 } /* for (pset_list) */
1708 ps_submit_state ("running", running);
1709 ps_submit_state ("sleeping", sleeping);
1710 ps_submit_state ("zombies", zombies);
1711 ps_submit_state ("stopped", stopped);
1712 ps_submit_state ("blocked", blocked);
1714 for (ps = list_head_g; ps != NULL; ps = ps->next)
1715 ps_submit_proc_list (ps);
1716 /* #endif HAVE_THREAD_INFO */
1730 char cmdline[CMDLINE_BUFFER_SIZE];
1734 procstat_entry_t pse;
1739 running = sleeping = zombies = stopped = paging = blocked = 0;
1742 if ((proc = opendir ("/proc")) == NULL)
1745 ERROR ("Cannot open `/proc': %s",
1746 sstrerror (errno, errbuf, sizeof (errbuf)));
1750 while ((ent = readdir (proc)) != NULL)
1752 if (!isdigit (ent->d_name[0]))
1755 if ((pid = atol (ent->d_name)) < 1)
1758 status = ps_read_process (pid, &ps, &state);
1761 DEBUG ("ps_read_process failed: %i", status);
1765 memset (&pse, 0, sizeof (pse));
1769 pse.num_proc = ps.num_proc;
1770 pse.num_lwp = ps.num_lwp;
1771 pse.vmem_size = ps.vmem_size;
1772 pse.vmem_rss = ps.vmem_rss;
1773 pse.vmem_data = ps.vmem_data;
1774 pse.vmem_code = ps.vmem_code;
1775 pse.stack_size = ps.stack_size;
1777 pse.vmem_minflt = 0;
1778 pse.vmem_minflt_counter = ps.vmem_minflt_counter;
1779 pse.vmem_majflt = 0;
1780 pse.vmem_majflt_counter = ps.vmem_majflt_counter;
1783 pse.cpu_user_counter = ps.cpu_user_counter;
1785 pse.cpu_system_counter = ps.cpu_system_counter;
1787 pse.io_rchar = ps.io_rchar;
1788 pse.io_wchar = ps.io_wchar;
1789 pse.io_syscr = ps.io_syscr;
1790 pse.io_syscw = ps.io_syscw;
1794 case 'R': running++; break;
1795 case 'S': sleeping++; break;
1796 case 'D': blocked++; break;
1797 case 'Z': zombies++; break;
1798 case 'T': stopped++; break;
1799 case 'W': paging++; break;
1802 ps_list_add (ps.name,
1803 ps_get_cmdline (pid, ps.name, cmdline, sizeof (cmdline)),
1809 ps_submit_state ("running", running);
1810 ps_submit_state ("sleeping", sleeping);
1811 ps_submit_state ("zombies", zombies);
1812 ps_submit_state ("stopped", stopped);
1813 ps_submit_state ("paging", paging);
1814 ps_submit_state ("blocked", blocked);
1816 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1817 ps_submit_proc_list (ps_ptr);
1820 /* #endif KERNEL_LINUX */
1822 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD
1832 char errbuf[_POSIX2_LINE_MAX];
1833 struct kinfo_proc *procs; /* array of processes */
1834 struct kinfo_proc *proc_ptr = NULL;
1835 int count; /* returns number of processes */
1839 procstat_entry_t pse;
1843 /* Open the kvm interface, get a descriptor */
1844 kd = kvm_openfiles (NULL, "/dev/null", NULL, 0, errbuf);
1847 ERROR ("processes plugin: Cannot open kvm interface: %s",
1852 /* Get the list of processes. */
1853 procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, &count);
1856 ERROR ("processes plugin: Cannot get kvm processes list: %s",
1862 /* Iterate through the processes in kinfo_proc */
1863 for (i = 0; i < count; i++)
1865 /* Create only one process list entry per _process_, i.e.
1866 * filter out threads (duplicate PID entries). */
1867 if ((proc_ptr == NULL) || (proc_ptr->ki_pid != procs[i].ki_pid))
1869 char cmdline[CMDLINE_BUFFER_SIZE] = "";
1870 _Bool have_cmdline = 0;
1872 proc_ptr = &(procs[i]);
1873 /* Don't probe system processes and processes without arguments */
1874 if (((procs[i].ki_flag & P_SYSTEM) == 0)
1875 && (procs[i].ki_args != NULL))
1881 /* retrieve the arguments */
1882 argv = kvm_getargv (kd, proc_ptr, /* nchr = */ 0);
1884 if ((argv != NULL) && (argv[0] != NULL))
1886 while (argv[argc] != NULL)
1889 status = strjoin (cmdline, sizeof (cmdline), argv, argc, " ");
1891 WARNING ("processes plugin: Command line did not fit into buffer.");
1895 } /* if (process has argument list) */
1897 pse.id = procs[i].ki_pid;
1901 pse.num_lwp = procs[i].ki_numthreads;
1903 pse.vmem_size = procs[i].ki_size;
1904 pse.vmem_rss = procs[i].ki_rssize * pagesize;
1905 pse.vmem_data = procs[i].ki_dsize * pagesize;
1906 pse.vmem_code = procs[i].ki_tsize * pagesize;
1907 pse.stack_size = procs[i].ki_ssize * pagesize;
1908 pse.vmem_minflt = 0;
1909 pse.vmem_minflt_counter = procs[i].ki_rusage.ru_minflt;
1910 pse.vmem_majflt = 0;
1911 pse.vmem_majflt_counter = procs[i].ki_rusage.ru_majflt;
1915 pse.cpu_user_counter = 0;
1916 pse.cpu_system_counter = 0;
1918 * The u-area might be swapped out, and we can't get
1919 * at it because we have a crashdump and no swap.
1920 * If it's here fill in these fields, otherwise, just
1923 if (procs[i].ki_flag & P_INMEM)
1925 pse.cpu_user_counter = procs[i].ki_rusage.ru_utime.tv_usec
1926 + (1000000lu * procs[i].ki_rusage.ru_utime.tv_sec);
1927 pse.cpu_system_counter = procs[i].ki_rusage.ru_stime.tv_usec
1928 + (1000000lu * procs[i].ki_rusage.ru_stime.tv_sec);
1937 ps_list_add (procs[i].ki_comm, have_cmdline ? cmdline : NULL, &pse);
1939 switch (procs[i].ki_stat)
1941 case SSTOP: stopped++; break;
1942 case SSLEEP: sleeping++; break;
1943 case SRUN: running++; break;
1944 case SIDL: idle++; break;
1945 case SWAIT: wait++; break;
1946 case SLOCK: blocked++; break;
1947 case SZOMB: zombies++; break;
1949 } /* if ((proc_ptr == NULL) || (proc_ptr->ki_pid != procs[i].ki_pid)) */
1954 ps_submit_state ("running", running);
1955 ps_submit_state ("sleeping", sleeping);
1956 ps_submit_state ("zombies", zombies);
1957 ps_submit_state ("stopped", stopped);
1958 ps_submit_state ("blocked", blocked);
1959 ps_submit_state ("idle", idle);
1960 ps_submit_state ("wait", wait);
1962 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1963 ps_submit_proc_list (ps_ptr);
1964 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
1966 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_OPENBSD
1977 struct kinfo_proc *procs; /* array of processes */
1978 struct kinfo_proc *proc_ptr = NULL;
1979 int count; /* returns number of processes */
1983 procstat_entry_t pse;
1987 /* Open the kvm interface, get a descriptor */
1988 kd = kvm_open (NULL, NULL, NULL, 0, errbuf);
1991 ERROR ("processes plugin: Cannot open kvm interface: %s",
1996 /* Get the list of processes. */
1997 procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc), &count);
2000 ERROR ("processes plugin: Cannot get kvm processes list: %s",
2006 /* Iterate through the processes in kinfo_proc */
2007 for (i = 0; i < count; i++)
2009 /* Create only one process list entry per _process_, i.e.
2010 * filter out threads (duplicate PID entries). */
2011 if ((proc_ptr == NULL) || (proc_ptr->p_pid != procs[i].p_pid))
2013 char cmdline[CMDLINE_BUFFER_SIZE] = "";
2014 _Bool have_cmdline = 0;
2016 proc_ptr = &(procs[i]);
2017 /* Don't probe zombie processes */
2018 if (!P_ZOMBIE(proc_ptr))
2024 /* retrieve the arguments */
2025 argv = kvm_getargv (kd, proc_ptr, /* nchr = */ 0);
2027 if ((argv != NULL) && (argv[0] != NULL))
2029 while (argv[argc] != NULL)
2032 status = strjoin (cmdline, sizeof (cmdline), argv, argc, " ");
2034 WARNING ("processes plugin: Command line did not fit into buffer.");
2038 } /* if (process has argument list) */
2040 memset (&pse, 0, sizeof (pse));
2041 pse.id = procs[i].p_pid;
2045 pse.num_lwp = 1; /* XXX: accumulate p_tid values for a single p_pid ? */
2047 pse.vmem_rss = procs[i].p_vm_rssize * pagesize;
2048 pse.vmem_data = procs[i].p_vm_dsize * pagesize;
2049 pse.vmem_code = procs[i].p_vm_tsize * pagesize;
2050 pse.stack_size = procs[i].p_vm_ssize * pagesize;
2051 pse.vmem_size = pse.stack_size + pse.vmem_code + pse.vmem_data;
2052 pse.vmem_minflt = 0;
2053 pse.vmem_minflt_counter = procs[i].p_uru_minflt;
2054 pse.vmem_majflt = 0;
2055 pse.vmem_majflt_counter = procs[i].p_uru_majflt;
2059 pse.cpu_user_counter = procs[i].p_uutime_usec +
2060 (1000000lu * procs[i].p_uutime_sec);
2061 pse.cpu_system_counter = procs[i].p_ustime_usec +
2062 (1000000lu * procs[i].p_ustime_sec);
2070 ps_list_add (procs[i].p_comm, have_cmdline ? cmdline : NULL, &pse);
2072 switch (procs[i].p_stat)
2074 case SSTOP: stopped++; break;
2075 case SSLEEP: sleeping++; break;
2076 case SRUN: running++; break;
2077 case SIDL: idle++; break;
2078 case SONPROC: onproc++; break;
2079 case SDEAD: dead++; break;
2080 case SZOMB: zombies++; break;
2082 } /* if ((proc_ptr == NULL) || (proc_ptr->p_pid != procs[i].p_pid)) */
2087 ps_submit_state ("running", running);
2088 ps_submit_state ("sleeping", sleeping);
2089 ps_submit_state ("zombies", zombies);
2090 ps_submit_state ("stopped", stopped);
2091 ps_submit_state ("onproc", onproc);
2092 ps_submit_state ("idle", idle);
2093 ps_submit_state ("dead", dead);
2095 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
2096 ps_submit_proc_list (ps_ptr);
2097 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_OPENBSD */
2099 #elif HAVE_PROCINFO_H
2112 procstat_entry_t pse;
2115 while ((nprocs = getprocs64 (procentry, sizeof(struct procentry64),
2116 /* fdsinfo = */ NULL, sizeof(struct fdsinfo64),
2117 &pindex, MAXPROCENTRY)) > 0)
2121 for (i = 0; i < nprocs; i++)
2125 char arglist[MAXARGLN+1];
2129 if (procentry[i].pi_state == SNONE) continue;
2130 /* if (procentry[i].pi_state == SZOMB) FIXME */
2132 cmdline = procentry[i].pi_comm;
2133 cargs = procentry[i].pi_comm;
2134 if ( procentry[i].pi_flags & SKPROC )
2136 if (procentry[i].pi_pid == 0)
2137 cmdline = "swapper";
2142 if (getargs(&procentry[i], sizeof(struct procentry64), arglist, MAXARGLN) >= 0)
2147 while (++n < MAXARGLN)
2149 if (arglist[n] == '\0')
2151 if (arglist[n+1] == '\0')
2160 pse.id = procentry[i].pi_pid;
2162 pse.num_lwp = procentry[i].pi_thcount;
2166 while ((nthreads = getthrds64(procentry[i].pi_pid,
2167 thrdentry, sizeof(struct thrdentry64),
2168 &thindex, MAXTHRDENTRY)) > 0)
2172 for (j=0; j< nthreads; j++)
2174 switch (thrdentry[j].ti_state)
2176 /* case TSNONE: break; */
2177 case TSIDL: blocked++; break; /* FIXME is really blocked */
2178 case TSRUN: running++; break;
2179 case TSSLEEP: sleeping++; break;
2180 case TSSWAP: paging++; break;
2181 case TSSTOP: stopped++; break;
2182 case TSZOMB: zombies++; break;
2185 if (nthreads < MAXTHRDENTRY)
2190 /* tv_usec is nanosec ??? */
2191 pse.cpu_user_counter = procentry[i].pi_ru.ru_utime.tv_sec * 1000000 +
2192 procentry[i].pi_ru.ru_utime.tv_usec / 1000;
2195 /* tv_usec is nanosec ??? */
2196 pse.cpu_system_counter = procentry[i].pi_ru.ru_stime.tv_sec * 1000000 +
2197 procentry[i].pi_ru.ru_stime.tv_usec / 1000;
2199 pse.vmem_minflt = 0;
2200 pse.vmem_minflt_counter = procentry[i].pi_minflt;
2201 pse.vmem_majflt = 0;
2202 pse.vmem_majflt_counter = procentry[i].pi_majflt;
2204 pse.vmem_size = procentry[i].pi_tsize + procentry[i].pi_dvm * pagesize;
2205 pse.vmem_rss = (procentry[i].pi_drss + procentry[i].pi_trss) * pagesize;
2216 ps_list_add (cmdline, cargs, &pse);
2217 } /* for (i = 0 .. nprocs) */
2219 if (nprocs < MAXPROCENTRY)
2221 } /* while (getprocs64() > 0) */
2222 ps_submit_state ("running", running);
2223 ps_submit_state ("sleeping", sleeping);
2224 ps_submit_state ("zombies", zombies);
2225 ps_submit_state ("stopped", stopped);
2226 ps_submit_state ("paging", paging);
2227 ps_submit_state ("blocked", blocked);
2229 for (ps = list_head_g; ps != NULL; ps = ps->next)
2230 ps_submit_proc_list (ps);
2231 /* #endif HAVE_PROCINFO_H */
2233 #elif KERNEL_SOLARIS
2235 * The Solaris section adds a few more process states and removes some
2236 * process states compared to linux. Most notably there is no "PAGING"
2237 * and "BLOCKED" state for a process. The rest is similar to the linux
2256 char cmdline[PRARGSZ];
2260 proc = opendir ("/proc");
2264 while ((ent = readdir(proc)) != NULL)
2268 procstat_entry_t pse;
2271 if (!isdigit ((int) ent->d_name[0]))
2274 pid = strtol (ent->d_name, &endptr, 10);
2275 if (*endptr != 0) /* value didn't completely parse as a number */
2278 status = ps_read_process (pid, &ps, &state);
2281 DEBUG("ps_read_process failed: %i", status);
2285 memset (&pse, 0, sizeof (pse));
2289 pse.num_proc = ps.num_proc;
2290 pse.num_lwp = ps.num_lwp;
2291 pse.vmem_size = ps.vmem_size;
2292 pse.vmem_rss = ps.vmem_rss;
2293 pse.vmem_data = ps.vmem_data;
2294 pse.vmem_code = ps.vmem_code;
2295 pse.stack_size = ps.stack_size;
2297 pse.vmem_minflt = 0;
2298 pse.vmem_minflt_counter = ps.vmem_minflt_counter;
2299 pse.vmem_majflt = 0;
2300 pse.vmem_majflt_counter = ps.vmem_majflt_counter;
2303 pse.cpu_user_counter = ps.cpu_user_counter;
2305 pse.cpu_system_counter = ps.cpu_system_counter;
2307 pse.io_rchar = ps.io_rchar;
2308 pse.io_wchar = ps.io_wchar;
2309 pse.io_syscr = ps.io_syscr;
2310 pse.io_syscw = ps.io_syscw;
2314 case 'R': running++; break;
2315 case 'S': sleeping++; break;
2316 case 'E': detached++; break;
2317 case 'Z': zombies++; break;
2318 case 'T': stopped++; break;
2319 case 'A': daemon++; break;
2320 case 'Y': system++; break;
2321 case 'O': orphan++; break;
2325 ps_list_add (ps.name,
2326 ps_get_cmdline (pid, ps.name, cmdline, sizeof (cmdline)),
2328 } /* while(readdir) */
2331 ps_submit_state ("running", running);
2332 ps_submit_state ("sleeping", sleeping);
2333 ps_submit_state ("zombies", zombies);
2334 ps_submit_state ("stopped", stopped);
2335 ps_submit_state ("detached", detached);
2336 ps_submit_state ("daemon", daemon);
2337 ps_submit_state ("system", system);
2338 ps_submit_state ("orphan", orphan);
2340 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
2341 ps_submit_proc_list (ps_ptr);
2344 #endif /* KERNEL_SOLARIS */
2349 void module_register (void)
2351 plugin_register_complex_config ("processes", ps_config);
2352 plugin_register_init ("processes", ps_init);
2353 plugin_register_read ("processes", ps_read);
2354 } /* void module_register */