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
134 # define MAXCOMLEN 16
137 /* #endif KERNEL_SOLARIS */
140 # error "No applicable input method."
151 #ifndef CMDLINE_BUFFER_SIZE
152 # if defined(ARG_MAX) && (ARG_MAX < 4096)
153 # define CMDLINE_BUFFER_SIZE ARG_MAX
155 # define CMDLINE_BUFFER_SIZE 4096
159 typedef struct procstat_entry_s
164 unsigned long num_proc;
165 unsigned long num_lwp;
166 unsigned long vmem_size;
167 unsigned long vmem_rss;
168 unsigned long vmem_data;
169 unsigned long vmem_code;
170 unsigned long stack_size;
172 unsigned long vmem_minflt;
173 unsigned long vmem_majflt;
174 derive_t vmem_minflt_counter;
175 derive_t vmem_majflt_counter;
177 unsigned long cpu_user;
178 unsigned long cpu_system;
179 derive_t cpu_user_counter;
180 derive_t cpu_system_counter;
188 struct procstat_entry_s *next;
191 #define PROCSTAT_NAME_LEN 256
192 typedef struct procstat
194 char name[PROCSTAT_NAME_LEN];
199 unsigned long num_proc;
200 unsigned long num_lwp;
201 unsigned long vmem_size;
202 unsigned long vmem_rss;
203 unsigned long vmem_data;
204 unsigned long vmem_code;
205 unsigned long stack_size;
207 derive_t vmem_minflt_counter;
208 derive_t vmem_majflt_counter;
210 derive_t cpu_user_counter;
211 derive_t cpu_system_counter;
219 struct procstat *next;
220 struct procstat_entry_s *instances;
223 static procstat_t *list_head_g = NULL;
226 static mach_port_t port_host_self;
227 static mach_port_t port_task_self;
229 static processor_set_name_array_t pset_list;
230 static mach_msg_type_number_t pset_list_len;
231 /* #endif HAVE_THREAD_INFO */
234 static long pagesize_g;
235 /* #endif KERNEL_LINUX */
237 #elif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD)
239 /* #endif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD) */
241 #elif HAVE_PROCINFO_H
242 static struct procentry64 procentry[MAXPROCENTRY];
243 static struct thrdentry64 thrdentry[MAXTHRDENTRY];
246 #ifndef _AIXVERSION_610
247 int getprocs64 (void *procsinfo, int sizproc, void *fdsinfo, int sizfd, pid_t *index, int count);
248 int getthrds64( pid_t, void *, int, tid64_t *, int );
250 int getargs (void *processBuffer, int bufferLen, char *argsBuffer, int argsLen);
251 #endif /* HAVE_PROCINFO_H */
253 /* put name of process from config to list_head_g tree
254 * list_head_g is a list of 'procstat_t' structs with
255 * processes names we want to watch */
256 static void ps_list_register (const char *name, const char *regexp)
262 new = (procstat_t *) malloc (sizeof (procstat_t));
265 ERROR ("processes plugin: ps_list_register: malloc failed.");
268 memset (new, 0, sizeof (procstat_t));
269 sstrncpy (new->name, name, sizeof (new->name));
274 DEBUG ("ProcessMatch: adding \"%s\" as criteria to process %s.", regexp, name);
275 new->re = (regex_t *) malloc (sizeof (regex_t));
278 ERROR ("processes plugin: ps_list_register: malloc failed.");
283 status = regcomp (new->re, regexp, REG_EXTENDED | REG_NOSUB);
286 DEBUG ("ProcessMatch: compiling the regular expression \"%s\" failed.", regexp);
295 ERROR ("processes plugin: ps_list_register: "
296 "Regular expression \"%s\" found in config "
297 "file, but support for regular expressions "
298 "has been disabled at compile time.",
305 for (ptr = list_head_g; ptr != NULL; ptr = ptr->next)
307 if (strcmp (ptr->name, name) == 0)
309 WARNING ("processes plugin: You have configured more "
310 "than one `Process' or "
311 "`ProcessMatch' with the same name. "
312 "All but the first setting will be "
321 if (ptr->next == NULL)
329 } /* void ps_list_register */
331 /* try to match name against entry, returns 1 if success */
332 static int ps_list_match (const char *name, const char *cmdline, procstat_t *ps)
341 if ((str == NULL) || (str[0] == 0))
344 assert (str != NULL);
346 status = regexec (ps->re, str,
355 if (strcmp (ps->name, name) == 0)
359 } /* int ps_list_match */
361 /* add process entry to 'instances' of process 'name' (or refresh it) */
362 static void ps_list_add (const char *name, const char *cmdline, procstat_entry_t *entry)
365 procstat_entry_t *pse;
370 for (ps = list_head_g; ps != NULL; ps = ps->next)
372 if ((ps_list_match (name, cmdline, ps)) == 0)
375 for (pse = ps->instances; pse != NULL; pse = pse->next)
376 if ((pse->id == entry->id) || (pse->next == NULL))
379 if ((pse == NULL) || (pse->id != entry->id))
381 procstat_entry_t *new;
383 new = (procstat_entry_t *) malloc (sizeof (procstat_entry_t));
386 memset (new, 0, sizeof (procstat_entry_t));
398 pse->num_proc = entry->num_proc;
399 pse->num_lwp = entry->num_lwp;
400 pse->vmem_size = entry->vmem_size;
401 pse->vmem_rss = entry->vmem_rss;
402 pse->vmem_data = entry->vmem_data;
403 pse->vmem_code = entry->vmem_code;
404 pse->stack_size = entry->stack_size;
405 pse->io_rchar = entry->io_rchar;
406 pse->io_wchar = entry->io_wchar;
407 pse->io_syscr = entry->io_syscr;
408 pse->io_syscw = entry->io_syscw;
410 ps->num_proc += pse->num_proc;
411 ps->num_lwp += pse->num_lwp;
412 ps->vmem_size += pse->vmem_size;
413 ps->vmem_rss += pse->vmem_rss;
414 ps->vmem_data += pse->vmem_data;
415 ps->vmem_code += pse->vmem_code;
416 ps->stack_size += pse->stack_size;
418 ps->io_rchar += ((pse->io_rchar == -1)?0:pse->io_rchar);
419 ps->io_wchar += ((pse->io_wchar == -1)?0:pse->io_wchar);
420 ps->io_syscr += ((pse->io_syscr == -1)?0:pse->io_syscr);
421 ps->io_syscw += ((pse->io_syscw == -1)?0:pse->io_syscw);
423 if ((entry->vmem_minflt_counter == 0)
424 && (entry->vmem_majflt_counter == 0))
426 pse->vmem_minflt_counter += entry->vmem_minflt;
427 pse->vmem_minflt = entry->vmem_minflt;
429 pse->vmem_majflt_counter += entry->vmem_majflt;
430 pse->vmem_majflt = entry->vmem_majflt;
434 if (entry->vmem_minflt_counter < pse->vmem_minflt_counter)
436 pse->vmem_minflt = entry->vmem_minflt_counter
437 + (ULONG_MAX - pse->vmem_minflt_counter);
441 pse->vmem_minflt = entry->vmem_minflt_counter - pse->vmem_minflt_counter;
443 pse->vmem_minflt_counter = entry->vmem_minflt_counter;
445 if (entry->vmem_majflt_counter < pse->vmem_majflt_counter)
447 pse->vmem_majflt = entry->vmem_majflt_counter
448 + (ULONG_MAX - pse->vmem_majflt_counter);
452 pse->vmem_majflt = entry->vmem_majflt_counter - pse->vmem_majflt_counter;
454 pse->vmem_majflt_counter = entry->vmem_majflt_counter;
457 ps->vmem_minflt_counter += pse->vmem_minflt;
458 ps->vmem_majflt_counter += pse->vmem_majflt;
460 if ((entry->cpu_user_counter == 0)
461 && (entry->cpu_system_counter == 0))
463 pse->cpu_user_counter += entry->cpu_user;
464 pse->cpu_user = entry->cpu_user;
466 pse->cpu_system_counter += entry->cpu_system;
467 pse->cpu_system = entry->cpu_system;
471 if (entry->cpu_user_counter < pse->cpu_user_counter)
473 pse->cpu_user = entry->cpu_user_counter
474 + (ULONG_MAX - pse->cpu_user_counter);
478 pse->cpu_user = entry->cpu_user_counter - pse->cpu_user_counter;
480 pse->cpu_user_counter = entry->cpu_user_counter;
482 if (entry->cpu_system_counter < pse->cpu_system_counter)
484 pse->cpu_system = entry->cpu_system_counter
485 + (ULONG_MAX - pse->cpu_system_counter);
489 pse->cpu_system = entry->cpu_system_counter - pse->cpu_system_counter;
491 pse->cpu_system_counter = entry->cpu_system_counter;
494 ps->cpu_user_counter += pse->cpu_user;
495 ps->cpu_system_counter += pse->cpu_system;
499 /* remove old entries from instances of processes in list_head_g */
500 static void ps_list_reset (void)
503 procstat_entry_t *pse;
504 procstat_entry_t *pse_prev;
506 for (ps = list_head_g; ps != NULL; ps = ps->next)
526 DEBUG ("Removing this procstat entry cause it's too old: "
527 "id = %lu; name = %s;",
530 if (pse_prev == NULL)
532 ps->instances = pse->next;
538 pse_prev->next = pse->next;
540 pse = pse_prev->next;
549 } /* while (pse != NULL) */
550 } /* for (ps = list_head_g; ps != NULL; ps = ps->next) */
553 /* put all pre-defined 'Process' names from config to list_head_g tree */
554 static int ps_config (oconfig_item_t *ci)
559 const size_t max_procname_len = 15;
560 #elif KERNEL_SOLARIS || KERNEL_FREEBSD
561 const size_t max_procname_len = MAXCOMLEN -1;
564 for (i = 0; i < ci->children_num; ++i) {
565 oconfig_item_t *c = ci->children + i;
567 if (strcasecmp (c->key, "Process") == 0)
569 if ((c->values_num != 1)
570 || (OCONFIG_TYPE_STRING != c->values[0].type)) {
571 ERROR ("processes plugin: `Process' expects exactly "
572 "one string argument (got %i).",
577 if (c->children_num != 0) {
578 WARNING ("processes plugin: the `Process' config option "
579 "does not expect any child elements -- ignoring "
580 "content (%i elements) of the <Process '%s'> block.",
581 c->children_num, c->values[0].value.string);
584 #if KERNEL_LINUX || KERNEL_SOLARIS || KERNEL_FREEBSD
585 if (strlen (c->values[0].value.string) > max_procname_len) {
586 WARNING ("processes plugin: this platform has a %zu character limit "
587 "to process names. The `Process \"%s\"' option will "
588 "not work as expected.",
589 max_procname_len, c->values[0].value.string);
593 ps_list_register (c->values[0].value.string, NULL);
595 else if (strcasecmp (c->key, "ProcessMatch") == 0)
597 if ((c->values_num != 2)
598 || (OCONFIG_TYPE_STRING != c->values[0].type)
599 || (OCONFIG_TYPE_STRING != c->values[1].type))
601 ERROR ("processes plugin: `ProcessMatch' needs exactly "
602 "two string arguments (got %i).",
607 if (c->children_num != 0) {
608 WARNING ("processes plugin: the `ProcessMatch' config option "
609 "does not expect any child elements -- ignoring "
610 "content (%i elements) of the <ProcessMatch '%s' '%s'> "
611 "block.", c->children_num, c->values[0].value.string,
612 c->values[1].value.string);
615 ps_list_register (c->values[0].value.string,
616 c->values[1].value.string);
620 ERROR ("processes plugin: The `%s' configuration option is not "
621 "understood and will be ignored.", c->key);
629 static int ps_init (void)
632 kern_return_t status;
634 port_host_self = mach_host_self ();
635 port_task_self = mach_task_self ();
637 if (pset_list != NULL)
639 vm_deallocate (port_task_self,
640 (vm_address_t) pset_list,
641 pset_list_len * sizeof (processor_set_t));
646 if ((status = host_processor_sets (port_host_self,
648 &pset_list_len)) != KERN_SUCCESS)
650 ERROR ("host_processor_sets failed: %s\n",
651 mach_error_string (status));
656 /* #endif HAVE_THREAD_INFO */
659 pagesize_g = sysconf(_SC_PAGESIZE);
660 DEBUG ("pagesize_g = %li; CONFIG_HZ = %i;",
661 pagesize_g, CONFIG_HZ);
662 /* #endif KERNEL_LINUX */
664 #elif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD)
665 pagesize = getpagesize();
666 /* #endif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD) */
668 #elif HAVE_PROCINFO_H
669 pagesize = getpagesize();
670 #endif /* HAVE_PROCINFO_H */
675 /* submit global state (e.g.: qty of zombies, running, etc..) */
676 static void ps_submit_state (const char *state, double value)
679 value_list_t vl = VALUE_LIST_INIT;
681 values[0].gauge = value;
685 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
686 sstrncpy (vl.plugin, "processes", sizeof (vl.plugin));
687 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
688 sstrncpy (vl.type, "ps_state", sizeof (vl.type));
689 sstrncpy (vl.type_instance, state, sizeof (vl.type_instance));
691 plugin_dispatch_values (&vl);
694 /* submit info about specific process (e.g.: memory taken, cpu usage, etc..) */
695 static void ps_submit_proc_list (procstat_t *ps)
698 value_list_t vl = VALUE_LIST_INIT;
702 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
703 sstrncpy (vl.plugin, "processes", sizeof (vl.plugin));
704 sstrncpy (vl.plugin_instance, ps->name, sizeof (vl.plugin_instance));
706 sstrncpy (vl.type, "ps_vm", sizeof (vl.type));
707 vl.values[0].gauge = ps->vmem_size;
709 plugin_dispatch_values (&vl);
711 sstrncpy (vl.type, "ps_rss", sizeof (vl.type));
712 vl.values[0].gauge = ps->vmem_rss;
714 plugin_dispatch_values (&vl);
716 sstrncpy (vl.type, "ps_data", sizeof (vl.type));
717 vl.values[0].gauge = ps->vmem_data;
719 plugin_dispatch_values (&vl);
721 sstrncpy (vl.type, "ps_code", sizeof (vl.type));
722 vl.values[0].gauge = ps->vmem_code;
724 plugin_dispatch_values (&vl);
726 sstrncpy (vl.type, "ps_stacksize", sizeof (vl.type));
727 vl.values[0].gauge = ps->stack_size;
729 plugin_dispatch_values (&vl);
731 sstrncpy (vl.type, "ps_cputime", sizeof (vl.type));
732 vl.values[0].derive = ps->cpu_user_counter;
733 vl.values[1].derive = ps->cpu_system_counter;
735 plugin_dispatch_values (&vl);
737 sstrncpy (vl.type, "ps_count", sizeof (vl.type));
738 vl.values[0].gauge = ps->num_proc;
739 vl.values[1].gauge = ps->num_lwp;
741 plugin_dispatch_values (&vl);
743 sstrncpy (vl.type, "ps_pagefaults", sizeof (vl.type));
744 vl.values[0].derive = ps->vmem_minflt_counter;
745 vl.values[1].derive = ps->vmem_majflt_counter;
747 plugin_dispatch_values (&vl);
749 if ( (ps->io_rchar != -1) && (ps->io_wchar != -1) )
751 sstrncpy (vl.type, "ps_disk_octets", sizeof (vl.type));
752 vl.values[0].derive = ps->io_rchar;
753 vl.values[1].derive = ps->io_wchar;
755 plugin_dispatch_values (&vl);
758 if ( (ps->io_syscr != -1) && (ps->io_syscw != -1) )
760 sstrncpy (vl.type, "ps_disk_ops", sizeof (vl.type));
761 vl.values[0].derive = ps->io_syscr;
762 vl.values[1].derive = ps->io_syscw;
764 plugin_dispatch_values (&vl);
767 DEBUG ("name = %s; num_proc = %lu; num_lwp = %lu; "
768 "vmem_size = %lu; vmem_rss = %lu; vmem_data = %lu; "
770 "vmem_minflt_counter = %"PRIi64"; vmem_majflt_counter = %"PRIi64"; "
771 "cpu_user_counter = %"PRIi64"; cpu_system_counter = %"PRIi64"; "
772 "io_rchar = %"PRIi64"; io_wchar = %"PRIi64"; "
773 "io_syscr = %"PRIi64"; io_syscw = %"PRIi64";",
774 ps->name, ps->num_proc, ps->num_lwp,
775 ps->vmem_size, ps->vmem_rss,
776 ps->vmem_data, ps->vmem_code,
777 ps->vmem_minflt_counter, ps->vmem_majflt_counter,
778 ps->cpu_user_counter, ps->cpu_system_counter,
779 ps->io_rchar, ps->io_wchar, ps->io_syscr, ps->io_syscw);
780 } /* void ps_submit_proc_list */
782 #if KERNEL_LINUX || KERNEL_SOLARIS
783 static void ps_submit_fork_rate (derive_t value)
786 value_list_t vl = VALUE_LIST_INIT;
788 values[0].derive = value;
792 sstrncpy(vl.host, hostname_g, sizeof (vl.host));
793 sstrncpy(vl.plugin, "processes", sizeof (vl.plugin));
794 sstrncpy(vl.plugin_instance, "", sizeof (vl.plugin_instance));
795 sstrncpy(vl.type, "fork_rate", sizeof (vl.type));
796 sstrncpy(vl.type_instance, "", sizeof (vl.type_instance));
798 plugin_dispatch_values(&vl);
800 #endif /* KERNEL_LINUX || KERNEL_SOLARIS*/
802 /* ------- additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
804 static int ps_read_tasks (long pid)
811 ssnprintf (dirname, sizeof (dirname), "/proc/%li/task", pid);
813 if ((dh = opendir (dirname)) == NULL)
815 DEBUG ("Failed to open directory `%s'", dirname);
819 while ((ent = readdir (dh)) != NULL)
821 if (!isdigit ((int) ent->d_name[0]))
828 return ((count >= 1) ? count : 1);
829 } /* int *ps_read_tasks */
831 /* Read advanced virtual memory data from /proc/pid/status */
832 static procstat_t *ps_read_vmem (long pid, procstat_t *ps)
837 unsigned long long lib = 0;
838 unsigned long long exe = 0;
839 unsigned long long data = 0;
843 ssnprintf (filename, sizeof (filename), "/proc/%li/status", pid);
844 if ((fh = fopen (filename, "r")) == NULL)
847 while (fgets (buffer, sizeof(buffer), fh) != NULL)
852 if (strncmp (buffer, "Vm", 2) != 0)
855 numfields = strsplit (buffer, fields,
856 STATIC_ARRAY_SIZE (fields));
863 tmp = strtoll (fields[1], &endptr, /* base = */ 10);
864 if ((errno == 0) && (endptr != fields[1]))
866 if (strncmp (buffer, "VmData", 6) == 0)
870 else if (strncmp (buffer, "VmLib", 5) == 0)
874 else if (strncmp(buffer, "VmExe", 5) == 0)
879 } /* while (fgets) */
884 WARNING ("processes: fclose: %s",
885 sstrerror (errno, errbuf, sizeof (errbuf)));
888 ps->vmem_data = data * 1024;
889 ps->vmem_code = (exe + lib) * 1024;
892 } /* procstat_t *ps_read_vmem */
894 static procstat_t *ps_read_io (long pid, procstat_t *ps)
903 ssnprintf (filename, sizeof (filename), "/proc/%li/io", pid);
904 if ((fh = fopen (filename, "r")) == NULL)
907 while (fgets (buffer, sizeof (buffer), fh) != NULL)
909 derive_t *val = NULL;
913 if (strncasecmp (buffer, "rchar:", 6) == 0)
914 val = &(ps->io_rchar);
915 else if (strncasecmp (buffer, "wchar:", 6) == 0)
916 val = &(ps->io_wchar);
917 else if (strncasecmp (buffer, "syscr:", 6) == 0)
918 val = &(ps->io_syscr);
919 else if (strncasecmp (buffer, "syscw:", 6) == 0)
920 val = &(ps->io_syscw);
924 numfields = strsplit (buffer, fields,
925 STATIC_ARRAY_SIZE (fields));
932 tmp = strtoll (fields[1], &endptr, /* base = */ 10);
933 if ((errno != 0) || (endptr == fields[1]))
936 *val = (derive_t) tmp;
937 } /* while (fgets) */
942 WARNING ("processes: fclose: %s",
943 sstrerror (errno, errbuf, sizeof (errbuf)));
947 } /* procstat_t *ps_read_io */
949 static int ps_read_process (long pid, procstat_t *ps, char *state)
960 size_t name_start_pos;
964 derive_t cpu_user_counter;
965 derive_t cpu_system_counter;
966 long long unsigned vmem_size;
967 long long unsigned vmem_rss;
968 long long unsigned stack_size;
970 memset (ps, 0, sizeof (procstat_t));
972 ssnprintf (filename, sizeof (filename), "/proc/%li/stat", pid);
974 buffer_len = read_file_contents (filename,
975 buffer, sizeof(buffer) - 1);
978 buffer[buffer_len] = 0;
980 /* The name of the process is enclosed in parens. Since the name can
981 * contain parens itself, spaces, numbers and pretty much everything
982 * else, use these to determine the process name. We don't use
983 * strchr(3) and strrchr(3) to avoid pointer arithmetic which would
984 * otherwise be required to determine name_len. */
986 while ((buffer[name_start_pos] != '(')
987 && (name_start_pos < buffer_len))
990 name_end_pos = buffer_len;
991 while ((buffer[name_end_pos] != ')')
992 && (name_end_pos > 0))
995 /* Either '(' or ')' is not found or they are in the wrong order.
996 * Anyway, something weird that shouldn't happen ever. */
997 if (name_start_pos >= name_end_pos)
999 ERROR ("processes plugin: name_start_pos = %zu >= name_end_pos = %zu",
1000 name_start_pos, name_end_pos);
1004 name_len = (name_end_pos - name_start_pos) - 1;
1005 if (name_len >= sizeof (ps->name))
1006 name_len = sizeof (ps->name) - 1;
1008 sstrncpy (ps->name, &buffer[name_start_pos + 1], name_len + 1);
1010 if ((buffer_len - name_end_pos) < 2)
1012 buffer_ptr = &buffer[name_end_pos + 2];
1014 fields_len = strsplit (buffer_ptr, fields, STATIC_ARRAY_SIZE (fields));
1015 if (fields_len < 22)
1017 DEBUG ("processes plugin: ps_read_process (pid = %li):"
1018 " `%s' has only %i fields..",
1019 pid, filename, fields_len);
1023 *state = fields[0][0];
1032 if ( (ps->num_lwp = ps_read_tasks (pid)) == -1 )
1034 /* returns -1 => kernel 2.4 */
1040 /* Leave the rest at zero if this is only a zombi */
1041 if (ps->num_proc == 0)
1043 DEBUG ("processes plugin: This is only a zombie: pid = %li; "
1044 "name = %s;", pid, ps->name);
1048 cpu_user_counter = atoll (fields[11]);
1049 cpu_system_counter = atoll (fields[12]);
1050 vmem_size = atoll (fields[20]);
1051 vmem_rss = atoll (fields[21]);
1052 ps->vmem_minflt_counter = atol (fields[7]);
1053 ps->vmem_majflt_counter = atol (fields[9]);
1056 unsigned long long stack_start = atoll (fields[25]);
1057 unsigned long long stack_ptr = atoll (fields[26]);
1059 stack_size = (stack_start > stack_ptr)
1060 ? stack_start - stack_ptr
1061 : stack_ptr - stack_start;
1064 /* Convert jiffies to useconds */
1065 cpu_user_counter = cpu_user_counter * 1000000 / CONFIG_HZ;
1066 cpu_system_counter = cpu_system_counter * 1000000 / CONFIG_HZ;
1067 vmem_rss = vmem_rss * pagesize_g;
1069 if ( (ps_read_vmem(pid, ps)) == NULL)
1074 DEBUG("ps_read_process: did not get vmem data for pid %li", pid);
1077 ps->cpu_user_counter = cpu_user_counter;
1078 ps->cpu_system_counter = cpu_system_counter;
1079 ps->vmem_size = (unsigned long) vmem_size;
1080 ps->vmem_rss = (unsigned long) vmem_rss;
1081 ps->stack_size = (unsigned long) stack_size;
1083 if ( (ps_read_io (pid, ps)) == NULL)
1091 DEBUG("ps_read_process: not get io data for pid %li", pid);
1096 } /* int ps_read_process (...) */
1098 static char *ps_get_cmdline (long pid, char *name, char *buf, size_t buf_len)
1103 char file[PATH_MAX];
1108 if ((pid < 1) || (NULL == buf) || (buf_len < 2))
1111 ssnprintf (file, sizeof (file), "/proc/%li/cmdline", pid);
1114 fd = open (file, O_RDONLY);
1117 /* ENOENT means the process exited while we were handling it.
1118 * Don't complain about this, it only fills the logs. */
1119 if (errno != ENOENT)
1120 WARNING ("processes plugin: Failed to open `%s': %s.", file,
1121 sstrerror (errno, errbuf, sizeof (errbuf)));
1133 status = read (fd, (void *)buf_ptr, len);
1138 if ((EAGAIN == errno) || (EINTR == errno))
1141 WARNING ("processes plugin: Failed to read from `%s': %s.", file,
1142 sstrerror (errno, errbuf, sizeof (errbuf)));
1162 /* cmdline not available; e.g. kernel thread, zombie */
1166 ssnprintf (buf, buf_len, "[%s]", name);
1170 assert (n <= buf_len);
1177 /* remove trailing whitespace */
1178 while ((n > 0) && (isspace (buf[n]) || ('\0' == buf[n]))) {
1183 /* arguments are separated by '\0' in /proc/<pid>/cmdline */
1190 } /* char *ps_get_cmdline (...) */
1192 static int read_fork_rate (void)
1197 _Bool value_valid = 0;
1199 proc_stat = fopen ("/proc/stat", "r");
1200 if (proc_stat == NULL)
1203 ERROR ("processes plugin: fopen (/proc/stat) failed: %s",
1204 sstrerror (errno, errbuf, sizeof (errbuf)));
1208 while (fgets (buffer, sizeof (buffer), proc_stat) != NULL)
1214 fields_num = strsplit (buffer, fields,
1215 STATIC_ARRAY_SIZE (fields));
1216 if (fields_num != 2)
1219 if (strcmp ("processes", fields[0]) != 0)
1222 status = parse_value (fields[1], &value, DS_TYPE_DERIVE);
1233 ps_submit_fork_rate (value.derive);
1236 #endif /*KERNEL_LINUX */
1239 static char *ps_get_cmdline (long pid, char *name __attribute__((unused)), /* {{{ */
1240 char *buffer, size_t buffer_size)
1242 char path[PATH_MAX];
1246 snprintf(path, sizeof (path), "/proc/%li/psinfo", pid);
1248 status = read_file_contents (path, (void *) &info, sizeof (info));
1249 if (status != sizeof (info))
1251 ERROR ("processes plugin: Unexpected return value "
1252 "while reading \"%s\": "
1253 "Returned %i but expected %zu.",
1254 path, status, buffer_size);
1258 info.pr_psargs[sizeof (info.pr_psargs) - 1] = 0;
1259 sstrncpy (buffer, info.pr_psargs, buffer_size);
1262 } /* }}} int ps_get_cmdline */
1265 * Reads process information on the Solaris OS. The information comes mainly from
1266 * /proc/PID/status, /proc/PID/psinfo and /proc/PID/usage
1267 * The values for input and ouput chars are calculated "by hand"
1268 * Added a few "solaris" specific process states as well
1270 static int ps_read_process(long pid, procstat_t *ps, char *state)
1273 char f_psinfo[64], f_usage[64];
1276 pstatus_t *myStatus;
1280 snprintf(filename, sizeof (filename), "/proc/%li/status", pid);
1281 snprintf(f_psinfo, sizeof (f_psinfo), "/proc/%li/psinfo", pid);
1282 snprintf(f_usage, sizeof (f_usage), "/proc/%li/usage", pid);
1285 buffer = malloc(sizeof (pstatus_t));
1286 memset(buffer, 0, sizeof (pstatus_t));
1287 read_file_contents(filename, buffer, sizeof (pstatus_t));
1288 myStatus = (pstatus_t *) buffer;
1290 buffer = malloc(sizeof (psinfo_t));
1291 memset(buffer, 0, sizeof(psinfo_t));
1292 read_file_contents(f_psinfo, buffer, sizeof (psinfo_t));
1293 myInfo = (psinfo_t *) buffer;
1295 buffer = malloc(sizeof (prusage_t));
1296 memset(buffer, 0, sizeof(prusage_t));
1297 read_file_contents(f_usage, buffer, sizeof (prusage_t));
1298 myUsage = (prusage_t *) buffer;
1300 sstrncpy(ps->name, myInfo->pr_fname, sizeof (myInfo->pr_fname));
1301 ps->num_lwp = myStatus->pr_nlwp;
1302 if (myInfo->pr_wstat != 0) {
1305 *state = (char) 'Z';
1313 ps->num_lwp = myInfo->pr_nlwp;
1317 * Convert system time and user time from nanoseconds to microseconds
1318 * for compatibility with the linux module
1320 ps->cpu_system_counter = myStatus -> pr_stime.tv_nsec / 1000;
1321 ps->cpu_user_counter = myStatus -> pr_utime.tv_nsec / 1000;
1324 * Convert rssize from KB to bytes to be consistent w/ the linux module
1326 ps->vmem_rss = myInfo->pr_rssize * 1024;
1327 ps->vmem_size = myInfo->pr_size * 1024;
1328 ps->vmem_minflt_counter = myUsage->pr_minf;
1329 ps->vmem_majflt_counter = myUsage->pr_majf;
1332 * TODO: Data and code segment calculations for Solaris
1337 ps->stack_size = myStatus->pr_stksize;
1340 * Calculating input/ouput chars
1341 * Formula used is total chars / total blocks => chars/block
1342 * then convert input/output blocks to chars
1344 ulong_t tot_chars = myUsage->pr_ioch;
1345 ulong_t tot_blocks = myUsage->pr_inblk + myUsage->pr_oublk;
1346 ulong_t chars_per_block = 1;
1347 if (tot_blocks != 0)
1348 chars_per_block = tot_chars / tot_blocks;
1349 ps->io_rchar = myUsage->pr_inblk * chars_per_block;
1350 ps->io_wchar = myUsage->pr_oublk * chars_per_block;
1351 ps->io_syscr = myUsage->pr_sysc;
1352 ps->io_syscw = myUsage->pr_sysc;
1356 * TODO: Find way of setting BLOCKED and PAGING status
1359 *state = (char) 'R';
1360 if (myStatus->pr_flags & PR_ASLEEP)
1361 *state = (char) 'S';
1362 else if (myStatus->pr_flags & PR_STOPPED)
1363 *state = (char) 'T';
1364 else if (myStatus->pr_flags & PR_DETACH)
1365 *state = (char) 'E';
1366 else if (myStatus->pr_flags & PR_DAEMON)
1367 *state = (char) 'A';
1368 else if (myStatus->pr_flags & PR_ISSYS)
1369 *state = (char) 'Y';
1370 else if (myStatus->pr_flags & PR_ORPHAN)
1371 *state = (char) 'O';
1381 * Reads the number of threads created since the last reboot. On Solaris these
1382 * are retrieved from kstat (module cpu, name sys, class misc, stat nthreads).
1383 * The result is the sum for all the threads created on each cpu
1385 static int read_fork_rate()
1387 extern kstat_ctl_t *kc;
1388 kstat_t *ksp_chain = NULL;
1389 derive_t result = 0;
1394 for (ksp_chain = kc->kc_chain;
1396 ksp_chain = ksp_chain->ks_next)
1398 if ((strcmp (ksp_chain->ks_module, "cpu") == 0)
1399 && (strcmp (ksp_chain->ks_name, "sys") == 0)
1400 && (strcmp (ksp_chain->ks_class, "misc") == 0))
1404 kstat_read (kc, ksp_chain, NULL);
1406 tmp = get_kstat_value(ksp_chain, "nthreads");
1412 ps_submit_fork_rate (result);
1415 #endif /* KERNEL_SOLARIS */
1417 #if HAVE_THREAD_INFO
1418 static int mach_get_task_name (task_t t, int *pid, char *name, size_t name_max_len)
1422 struct kinfo_proc kp;
1427 mib[2] = KERN_PROC_PID;
1429 if (pid_for_task (t, pid) != KERN_SUCCESS)
1433 kp_size = sizeof (kp);
1434 if (sysctl (mib, 4, &kp, &kp_size, NULL, 0) != 0)
1437 if (name_max_len > (MAXCOMLEN + 1))
1438 name_max_len = MAXCOMLEN + 1;
1440 strncpy (name, kp.kp_proc.p_comm, name_max_len - 1);
1441 name[name_max_len - 1] = '\0';
1443 DEBUG ("pid = %i; name = %s;", *pid, name);
1445 /* We don't do the special handling for `p_comm == "LaunchCFMApp"' as
1446 * `top' does it, because it is a lot of work and only used when
1447 * debugging. -octo */
1451 #endif /* HAVE_THREAD_INFO */
1452 /* ------- end of additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
1454 /* do actual readings from kernel */
1455 static int ps_read (void)
1457 #if HAVE_THREAD_INFO
1458 kern_return_t status;
1461 processor_set_t port_pset_priv;
1464 task_array_t task_list;
1465 mach_msg_type_number_t task_list_len;
1468 char task_name[MAXCOMLEN + 1];
1471 thread_act_array_t thread_list;
1472 mach_msg_type_number_t thread_list_len;
1473 thread_basic_info_data_t thread_data;
1474 mach_msg_type_number_t thread_data_len;
1483 procstat_entry_t pse;
1488 * The Mach-concept is a little different from the traditional UNIX
1489 * concept: All the work is done in threads. Threads are contained in
1490 * `tasks'. Therefore, `task status' doesn't make much sense, since
1491 * it's actually a `thread status'.
1492 * Tasks are assigned to sets of processors, so that's where you go to
1495 for (pset = 0; pset < pset_list_len; pset++)
1497 if ((status = host_processor_set_priv (port_host_self,
1499 &port_pset_priv)) != KERN_SUCCESS)
1501 ERROR ("host_processor_set_priv failed: %s\n",
1502 mach_error_string (status));
1506 if ((status = processor_set_tasks (port_pset_priv,
1508 &task_list_len)) != KERN_SUCCESS)
1510 ERROR ("processor_set_tasks failed: %s\n",
1511 mach_error_string (status));
1512 mach_port_deallocate (port_task_self, port_pset_priv);
1516 for (task = 0; task < task_list_len; task++)
1519 if (mach_get_task_name (task_list[task],
1521 task_name, PROCSTAT_NAME_LEN) == 0)
1523 /* search for at least one match */
1524 for (ps = list_head_g; ps != NULL; ps = ps->next)
1525 /* FIXME: cmdline should be here instead of NULL */
1526 if (ps_list_match (task_name, NULL, ps) == 1)
1530 /* Collect more detailed statistics for this process */
1533 task_basic_info_data_t task_basic_info;
1534 mach_msg_type_number_t task_basic_info_len;
1535 task_events_info_data_t task_events_info;
1536 mach_msg_type_number_t task_events_info_len;
1537 task_absolutetime_info_data_t task_absolutetime_info;
1538 mach_msg_type_number_t task_absolutetime_info_len;
1540 memset (&pse, '\0', sizeof (pse));
1543 task_basic_info_len = TASK_BASIC_INFO_COUNT;
1544 status = task_info (task_list[task],
1546 (task_info_t) &task_basic_info,
1547 &task_basic_info_len);
1548 if (status != KERN_SUCCESS)
1550 ERROR ("task_info failed: %s",
1551 mach_error_string (status));
1552 continue; /* with next thread_list */
1555 task_events_info_len = TASK_EVENTS_INFO_COUNT;
1556 status = task_info (task_list[task],
1558 (task_info_t) &task_events_info,
1559 &task_events_info_len);
1560 if (status != KERN_SUCCESS)
1562 ERROR ("task_info failed: %s",
1563 mach_error_string (status));
1564 continue; /* with next thread_list */
1567 task_absolutetime_info_len = TASK_ABSOLUTETIME_INFO_COUNT;
1568 status = task_info (task_list[task],
1569 TASK_ABSOLUTETIME_INFO,
1570 (task_info_t) &task_absolutetime_info,
1571 &task_absolutetime_info_len);
1572 if (status != KERN_SUCCESS)
1574 ERROR ("task_info failed: %s",
1575 mach_error_string (status));
1576 continue; /* with next thread_list */
1580 pse.vmem_size = task_basic_info.virtual_size;
1581 pse.vmem_rss = task_basic_info.resident_size;
1582 /* Does not seem to be easily exposed */
1586 pse.vmem_minflt_counter = task_events_info.cow_faults;
1587 pse.vmem_majflt_counter = task_events_info.faults;
1589 pse.cpu_user_counter = task_absolutetime_info.total_user;
1590 pse.cpu_system_counter = task_absolutetime_info.total_system;
1593 status = task_threads (task_list[task], &thread_list,
1595 if (status != KERN_SUCCESS)
1597 /* Apple's `top' treats this case a zombie. It
1598 * makes sense to some extend: A `zombie'
1599 * thread is nonsense, since the task/process
1602 DEBUG ("task_threads failed: %s",
1603 mach_error_string (status));
1604 if (task_list[task] != port_task_self)
1605 mach_port_deallocate (port_task_self,
1607 continue; /* with next task_list */
1610 for (thread = 0; thread < thread_list_len; thread++)
1612 thread_data_len = THREAD_BASIC_INFO_COUNT;
1613 status = thread_info (thread_list[thread],
1615 (thread_info_t) &thread_data,
1617 if (status != KERN_SUCCESS)
1619 ERROR ("thread_info failed: %s",
1620 mach_error_string (status));
1621 if (task_list[task] != port_task_self)
1622 mach_port_deallocate (port_task_self,
1623 thread_list[thread]);
1624 continue; /* with next thread_list */
1630 switch (thread_data.run_state)
1632 case TH_STATE_RUNNING:
1635 case TH_STATE_STOPPED:
1636 /* What exactly is `halted'? */
1637 case TH_STATE_HALTED:
1640 case TH_STATE_WAITING:
1643 case TH_STATE_UNINTERRUPTIBLE:
1646 /* There is no `zombie' case here,
1647 * since there are no zombie-threads.
1648 * There's only zombie tasks, which are
1651 WARNING ("Unknown thread status: %i",
1652 thread_data.run_state);
1654 } /* switch (thread_data.run_state) */
1656 if (task_list[task] != port_task_self)
1658 status = mach_port_deallocate (port_task_self,
1659 thread_list[thread]);
1660 if (status != KERN_SUCCESS)
1661 ERROR ("mach_port_deallocate failed: %s",
1662 mach_error_string (status));
1664 } /* for (thread_list) */
1666 if ((status = vm_deallocate (port_task_self,
1667 (vm_address_t) thread_list,
1668 thread_list_len * sizeof (thread_act_t)))
1671 ERROR ("vm_deallocate failed: %s",
1672 mach_error_string (status));
1675 thread_list_len = 0;
1677 /* Only deallocate the task port, if it isn't our own.
1678 * Don't know what would happen in that case, but this
1679 * is what Apple's top does.. ;) */
1680 if (task_list[task] != port_task_self)
1682 status = mach_port_deallocate (port_task_self,
1684 if (status != KERN_SUCCESS)
1685 ERROR ("mach_port_deallocate failed: %s",
1686 mach_error_string (status));
1690 /* FIXME: cmdline should be here instead of NULL */
1691 ps_list_add (task_name, NULL, &pse);
1692 } /* for (task_list) */
1694 if ((status = vm_deallocate (port_task_self,
1695 (vm_address_t) task_list,
1696 task_list_len * sizeof (task_t))) != KERN_SUCCESS)
1698 ERROR ("vm_deallocate failed: %s",
1699 mach_error_string (status));
1704 if ((status = mach_port_deallocate (port_task_self, port_pset_priv))
1707 ERROR ("mach_port_deallocate failed: %s",
1708 mach_error_string (status));
1710 } /* for (pset_list) */
1712 ps_submit_state ("running", running);
1713 ps_submit_state ("sleeping", sleeping);
1714 ps_submit_state ("zombies", zombies);
1715 ps_submit_state ("stopped", stopped);
1716 ps_submit_state ("blocked", blocked);
1718 for (ps = list_head_g; ps != NULL; ps = ps->next)
1719 ps_submit_proc_list (ps);
1720 /* #endif HAVE_THREAD_INFO */
1734 char cmdline[CMDLINE_BUFFER_SIZE];
1738 procstat_entry_t pse;
1743 running = sleeping = zombies = stopped = paging = blocked = 0;
1746 if ((proc = opendir ("/proc")) == NULL)
1749 ERROR ("Cannot open `/proc': %s",
1750 sstrerror (errno, errbuf, sizeof (errbuf)));
1754 while ((ent = readdir (proc)) != NULL)
1756 if (!isdigit (ent->d_name[0]))
1759 if ((pid = atol (ent->d_name)) < 1)
1762 status = ps_read_process (pid, &ps, &state);
1765 DEBUG ("ps_read_process failed: %i", status);
1769 memset (&pse, 0, sizeof (pse));
1773 pse.num_proc = ps.num_proc;
1774 pse.num_lwp = ps.num_lwp;
1775 pse.vmem_size = ps.vmem_size;
1776 pse.vmem_rss = ps.vmem_rss;
1777 pse.vmem_data = ps.vmem_data;
1778 pse.vmem_code = ps.vmem_code;
1779 pse.stack_size = ps.stack_size;
1781 pse.vmem_minflt = 0;
1782 pse.vmem_minflt_counter = ps.vmem_minflt_counter;
1783 pse.vmem_majflt = 0;
1784 pse.vmem_majflt_counter = ps.vmem_majflt_counter;
1787 pse.cpu_user_counter = ps.cpu_user_counter;
1789 pse.cpu_system_counter = ps.cpu_system_counter;
1791 pse.io_rchar = ps.io_rchar;
1792 pse.io_wchar = ps.io_wchar;
1793 pse.io_syscr = ps.io_syscr;
1794 pse.io_syscw = ps.io_syscw;
1798 case 'R': running++; break;
1799 case 'S': sleeping++; break;
1800 case 'D': blocked++; break;
1801 case 'Z': zombies++; break;
1802 case 'T': stopped++; break;
1803 case 'W': paging++; break;
1806 ps_list_add (ps.name,
1807 ps_get_cmdline (pid, ps.name, cmdline, sizeof (cmdline)),
1813 ps_submit_state ("running", running);
1814 ps_submit_state ("sleeping", sleeping);
1815 ps_submit_state ("zombies", zombies);
1816 ps_submit_state ("stopped", stopped);
1817 ps_submit_state ("paging", paging);
1818 ps_submit_state ("blocked", blocked);
1820 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1821 ps_submit_proc_list (ps_ptr);
1824 /* #endif KERNEL_LINUX */
1826 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD
1836 char errbuf[_POSIX2_LINE_MAX];
1837 struct kinfo_proc *procs; /* array of processes */
1838 struct kinfo_proc *proc_ptr = NULL;
1839 int count; /* returns number of processes */
1843 procstat_entry_t pse;
1847 /* Open the kvm interface, get a descriptor */
1848 kd = kvm_openfiles (NULL, "/dev/null", NULL, 0, errbuf);
1851 ERROR ("processes plugin: Cannot open kvm interface: %s",
1856 /* Get the list of processes. */
1857 procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, &count);
1860 ERROR ("processes plugin: Cannot get kvm processes list: %s",
1866 /* Iterate through the processes in kinfo_proc */
1867 for (i = 0; i < count; i++)
1869 /* Create only one process list entry per _process_, i.e.
1870 * filter out threads (duplicate PID entries). */
1871 if ((proc_ptr == NULL) || (proc_ptr->ki_pid != procs[i].ki_pid))
1873 char cmdline[CMDLINE_BUFFER_SIZE] = "";
1874 _Bool have_cmdline = 0;
1876 proc_ptr = &(procs[i]);
1877 /* Don't probe system processes and processes without arguments */
1878 if (((procs[i].ki_flag & P_SYSTEM) == 0)
1879 && (procs[i].ki_args != NULL))
1885 /* retrieve the arguments */
1886 argv = kvm_getargv (kd, proc_ptr, /* nchr = */ 0);
1888 if ((argv != NULL) && (argv[0] != NULL))
1890 while (argv[argc] != NULL)
1893 status = strjoin (cmdline, sizeof (cmdline), argv, argc, " ");
1895 WARNING ("processes plugin: Command line did not fit into buffer.");
1899 } /* if (process has argument list) */
1901 pse.id = procs[i].ki_pid;
1905 pse.num_lwp = procs[i].ki_numthreads;
1907 pse.vmem_size = procs[i].ki_size;
1908 pse.vmem_rss = procs[i].ki_rssize * pagesize;
1909 pse.vmem_data = procs[i].ki_dsize * pagesize;
1910 pse.vmem_code = procs[i].ki_tsize * pagesize;
1911 pse.stack_size = procs[i].ki_ssize * pagesize;
1912 pse.vmem_minflt = 0;
1913 pse.vmem_minflt_counter = procs[i].ki_rusage.ru_minflt;
1914 pse.vmem_majflt = 0;
1915 pse.vmem_majflt_counter = procs[i].ki_rusage.ru_majflt;
1919 pse.cpu_user_counter = 0;
1920 pse.cpu_system_counter = 0;
1922 * The u-area might be swapped out, and we can't get
1923 * at it because we have a crashdump and no swap.
1924 * If it's here fill in these fields, otherwise, just
1927 if (procs[i].ki_flag & P_INMEM)
1929 pse.cpu_user_counter = procs[i].ki_rusage.ru_utime.tv_usec
1930 + (1000000lu * procs[i].ki_rusage.ru_utime.tv_sec);
1931 pse.cpu_system_counter = procs[i].ki_rusage.ru_stime.tv_usec
1932 + (1000000lu * procs[i].ki_rusage.ru_stime.tv_sec);
1941 ps_list_add (procs[i].ki_comm, have_cmdline ? cmdline : NULL, &pse);
1943 switch (procs[i].ki_stat)
1945 case SSTOP: stopped++; break;
1946 case SSLEEP: sleeping++; break;
1947 case SRUN: running++; break;
1948 case SIDL: idle++; break;
1949 case SWAIT: wait++; break;
1950 case SLOCK: blocked++; break;
1951 case SZOMB: zombies++; break;
1953 } /* if ((proc_ptr == NULL) || (proc_ptr->ki_pid != procs[i].ki_pid)) */
1958 ps_submit_state ("running", running);
1959 ps_submit_state ("sleeping", sleeping);
1960 ps_submit_state ("zombies", zombies);
1961 ps_submit_state ("stopped", stopped);
1962 ps_submit_state ("blocked", blocked);
1963 ps_submit_state ("idle", idle);
1964 ps_submit_state ("wait", wait);
1966 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1967 ps_submit_proc_list (ps_ptr);
1968 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
1970 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_OPENBSD
1981 struct kinfo_proc *procs; /* array of processes */
1982 struct kinfo_proc *proc_ptr = NULL;
1983 int count; /* returns number of processes */
1987 procstat_entry_t pse;
1991 /* Open the kvm interface, get a descriptor */
1992 kd = kvm_open (NULL, NULL, NULL, 0, errbuf);
1995 ERROR ("processes plugin: Cannot open kvm interface: %s",
2000 /* Get the list of processes. */
2001 procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc), &count);
2004 ERROR ("processes plugin: Cannot get kvm processes list: %s",
2010 /* Iterate through the processes in kinfo_proc */
2011 for (i = 0; i < count; i++)
2013 /* Create only one process list entry per _process_, i.e.
2014 * filter out threads (duplicate PID entries). */
2015 if ((proc_ptr == NULL) || (proc_ptr->p_pid != procs[i].p_pid))
2017 char cmdline[CMDLINE_BUFFER_SIZE] = "";
2018 _Bool have_cmdline = 0;
2020 proc_ptr = &(procs[i]);
2021 /* Don't probe zombie processes */
2022 if (!P_ZOMBIE(proc_ptr))
2028 /* retrieve the arguments */
2029 argv = kvm_getargv (kd, proc_ptr, /* nchr = */ 0);
2031 if ((argv != NULL) && (argv[0] != NULL))
2033 while (argv[argc] != NULL)
2036 status = strjoin (cmdline, sizeof (cmdline), argv, argc, " ");
2038 WARNING ("processes plugin: Command line did not fit into buffer.");
2042 } /* if (process has argument list) */
2044 memset (&pse, 0, sizeof (pse));
2045 pse.id = procs[i].p_pid;
2049 pse.num_lwp = 1; /* XXX: accumulate p_tid values for a single p_pid ? */
2051 pse.vmem_rss = procs[i].p_vm_rssize * pagesize;
2052 pse.vmem_data = procs[i].p_vm_dsize * pagesize;
2053 pse.vmem_code = procs[i].p_vm_tsize * pagesize;
2054 pse.stack_size = procs[i].p_vm_ssize * pagesize;
2055 pse.vmem_size = pse.stack_size + pse.vmem_code + pse.vmem_data;
2056 pse.vmem_minflt = 0;
2057 pse.vmem_minflt_counter = procs[i].p_uru_minflt;
2058 pse.vmem_majflt = 0;
2059 pse.vmem_majflt_counter = procs[i].p_uru_majflt;
2063 pse.cpu_user_counter = procs[i].p_uutime_usec +
2064 (1000000lu * procs[i].p_uutime_sec);
2065 pse.cpu_system_counter = procs[i].p_ustime_usec +
2066 (1000000lu * procs[i].p_ustime_sec);
2074 ps_list_add (procs[i].p_comm, have_cmdline ? cmdline : NULL, &pse);
2076 switch (procs[i].p_stat)
2078 case SSTOP: stopped++; break;
2079 case SSLEEP: sleeping++; break;
2080 case SRUN: running++; break;
2081 case SIDL: idle++; break;
2082 case SONPROC: onproc++; break;
2083 case SDEAD: dead++; break;
2084 case SZOMB: zombies++; break;
2086 } /* if ((proc_ptr == NULL) || (proc_ptr->p_pid != procs[i].p_pid)) */
2091 ps_submit_state ("running", running);
2092 ps_submit_state ("sleeping", sleeping);
2093 ps_submit_state ("zombies", zombies);
2094 ps_submit_state ("stopped", stopped);
2095 ps_submit_state ("onproc", onproc);
2096 ps_submit_state ("idle", idle);
2097 ps_submit_state ("dead", dead);
2099 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
2100 ps_submit_proc_list (ps_ptr);
2101 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_OPENBSD */
2103 #elif HAVE_PROCINFO_H
2116 procstat_entry_t pse;
2119 while ((nprocs = getprocs64 (procentry, sizeof(struct procentry64),
2120 /* fdsinfo = */ NULL, sizeof(struct fdsinfo64),
2121 &pindex, MAXPROCENTRY)) > 0)
2125 for (i = 0; i < nprocs; i++)
2129 char arglist[MAXARGLN+1];
2133 if (procentry[i].pi_state == SNONE) continue;
2134 /* if (procentry[i].pi_state == SZOMB) FIXME */
2136 cmdline = procentry[i].pi_comm;
2137 cargs = procentry[i].pi_comm;
2138 if ( procentry[i].pi_flags & SKPROC )
2140 if (procentry[i].pi_pid == 0)
2141 cmdline = "swapper";
2146 if (getargs(&procentry[i], sizeof(struct procentry64), arglist, MAXARGLN) >= 0)
2151 while (++n < MAXARGLN)
2153 if (arglist[n] == '\0')
2155 if (arglist[n+1] == '\0')
2164 pse.id = procentry[i].pi_pid;
2166 pse.num_lwp = procentry[i].pi_thcount;
2170 while ((nthreads = getthrds64(procentry[i].pi_pid,
2171 thrdentry, sizeof(struct thrdentry64),
2172 &thindex, MAXTHRDENTRY)) > 0)
2176 for (j=0; j< nthreads; j++)
2178 switch (thrdentry[j].ti_state)
2180 /* case TSNONE: break; */
2181 case TSIDL: blocked++; break; /* FIXME is really blocked */
2182 case TSRUN: running++; break;
2183 case TSSLEEP: sleeping++; break;
2184 case TSSWAP: paging++; break;
2185 case TSSTOP: stopped++; break;
2186 case TSZOMB: zombies++; break;
2189 if (nthreads < MAXTHRDENTRY)
2194 /* tv_usec is nanosec ??? */
2195 pse.cpu_user_counter = procentry[i].pi_ru.ru_utime.tv_sec * 1000000 +
2196 procentry[i].pi_ru.ru_utime.tv_usec / 1000;
2199 /* tv_usec is nanosec ??? */
2200 pse.cpu_system_counter = procentry[i].pi_ru.ru_stime.tv_sec * 1000000 +
2201 procentry[i].pi_ru.ru_stime.tv_usec / 1000;
2203 pse.vmem_minflt = 0;
2204 pse.vmem_minflt_counter = procentry[i].pi_minflt;
2205 pse.vmem_majflt = 0;
2206 pse.vmem_majflt_counter = procentry[i].pi_majflt;
2208 pse.vmem_size = procentry[i].pi_tsize + procentry[i].pi_dvm * pagesize;
2209 pse.vmem_rss = (procentry[i].pi_drss + procentry[i].pi_trss) * pagesize;
2220 ps_list_add (cmdline, cargs, &pse);
2221 } /* for (i = 0 .. nprocs) */
2223 if (nprocs < MAXPROCENTRY)
2225 } /* while (getprocs64() > 0) */
2226 ps_submit_state ("running", running);
2227 ps_submit_state ("sleeping", sleeping);
2228 ps_submit_state ("zombies", zombies);
2229 ps_submit_state ("stopped", stopped);
2230 ps_submit_state ("paging", paging);
2231 ps_submit_state ("blocked", blocked);
2233 for (ps = list_head_g; ps != NULL; ps = ps->next)
2234 ps_submit_proc_list (ps);
2235 /* #endif HAVE_PROCINFO_H */
2237 #elif KERNEL_SOLARIS
2239 * The Solaris section adds a few more process states and removes some
2240 * process states compared to linux. Most notably there is no "PAGING"
2241 * and "BLOCKED" state for a process. The rest is similar to the linux
2260 char cmdline[PRARGSZ];
2264 proc = opendir ("/proc");
2268 while ((ent = readdir(proc)) != NULL)
2272 procstat_entry_t pse;
2275 if (!isdigit ((int) ent->d_name[0]))
2278 pid = strtol (ent->d_name, &endptr, 10);
2279 if (*endptr != 0) /* value didn't completely parse as a number */
2282 status = ps_read_process (pid, &ps, &state);
2285 DEBUG("ps_read_process failed: %i", status);
2289 memset (&pse, 0, sizeof (pse));
2293 pse.num_proc = ps.num_proc;
2294 pse.num_lwp = ps.num_lwp;
2295 pse.vmem_size = ps.vmem_size;
2296 pse.vmem_rss = ps.vmem_rss;
2297 pse.vmem_data = ps.vmem_data;
2298 pse.vmem_code = ps.vmem_code;
2299 pse.stack_size = ps.stack_size;
2301 pse.vmem_minflt = 0;
2302 pse.vmem_minflt_counter = ps.vmem_minflt_counter;
2303 pse.vmem_majflt = 0;
2304 pse.vmem_majflt_counter = ps.vmem_majflt_counter;
2307 pse.cpu_user_counter = ps.cpu_user_counter;
2309 pse.cpu_system_counter = ps.cpu_system_counter;
2311 pse.io_rchar = ps.io_rchar;
2312 pse.io_wchar = ps.io_wchar;
2313 pse.io_syscr = ps.io_syscr;
2314 pse.io_syscw = ps.io_syscw;
2318 case 'R': running++; break;
2319 case 'S': sleeping++; break;
2320 case 'E': detached++; break;
2321 case 'Z': zombies++; break;
2322 case 'T': stopped++; break;
2323 case 'A': daemon++; break;
2324 case 'Y': system++; break;
2325 case 'O': orphan++; break;
2329 ps_list_add (ps.name,
2330 ps_get_cmdline (pid, ps.name, cmdline, sizeof (cmdline)),
2332 } /* while(readdir) */
2335 ps_submit_state ("running", running);
2336 ps_submit_state ("sleeping", sleeping);
2337 ps_submit_state ("zombies", zombies);
2338 ps_submit_state ("stopped", stopped);
2339 ps_submit_state ("detached", detached);
2340 ps_submit_state ("daemon", daemon);
2341 ps_submit_state ("system", system);
2342 ps_submit_state ("orphan", orphan);
2344 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
2345 ps_submit_proc_list (ps_ptr);
2348 #endif /* KERNEL_SOLARIS */
2353 void module_register (void)
2355 plugin_register_complex_config ("processes", ps_config);
2356 plugin_register_init ("processes", ps_init);
2357 plugin_register_read ("processes", ps_read);
2358 } /* void module_register */