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 verplant.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
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 */
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."
148 # define ARG_MAX 4096
151 typedef struct procstat_entry_s
156 unsigned long num_proc;
157 unsigned long num_lwp;
158 unsigned long vmem_size;
159 unsigned long vmem_rss;
160 unsigned long vmem_data;
161 unsigned long vmem_code;
162 unsigned long stack_size;
164 unsigned long vmem_minflt;
165 unsigned long vmem_majflt;
166 derive_t vmem_minflt_counter;
167 derive_t vmem_majflt_counter;
169 unsigned long cpu_user;
170 unsigned long cpu_system;
171 derive_t cpu_user_counter;
172 derive_t cpu_system_counter;
180 struct procstat_entry_s *next;
183 #define PROCSTAT_NAME_LEN 256
184 typedef struct procstat
186 char name[PROCSTAT_NAME_LEN];
191 unsigned long num_proc;
192 unsigned long num_lwp;
193 unsigned long vmem_size;
194 unsigned long vmem_rss;
195 unsigned long vmem_data;
196 unsigned long vmem_code;
197 unsigned long stack_size;
199 derive_t vmem_minflt_counter;
200 derive_t vmem_majflt_counter;
202 derive_t cpu_user_counter;
203 derive_t cpu_system_counter;
211 struct procstat *next;
212 struct procstat_entry_s *instances;
215 static procstat_t *list_head_g = NULL;
218 static mach_port_t port_host_self;
219 static mach_port_t port_task_self;
221 static processor_set_name_array_t pset_list;
222 static mach_msg_type_number_t pset_list_len;
223 /* #endif HAVE_THREAD_INFO */
226 static long pagesize_g;
227 /* #endif KERNEL_LINUX */
229 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD
231 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
233 #elif HAVE_PROCINFO_H
234 static struct procentry64 procentry[MAXPROCENTRY];
235 static struct thrdentry64 thrdentry[MAXTHRDENTRY];
238 #ifndef _AIXVERSION_610
239 int getprocs64 (void *procsinfo, int sizproc, void *fdsinfo, int sizfd, pid_t *index, int count);
240 int getthrds64( pid_t, void *, int, tid64_t *, int );
242 int getargs (void *processBuffer, int bufferLen, char *argsBuffer, int argsLen);
243 #endif /* HAVE_PROCINFO_H */
245 /* put name of process from config to list_head_g tree
246 * list_head_g is a list of 'procstat_t' structs with
247 * processes names we want to watch */
248 static void ps_list_register (const char *name, const char *regexp)
254 new = (procstat_t *) malloc (sizeof (procstat_t));
257 ERROR ("processes plugin: ps_list_register: malloc failed.");
260 memset (new, 0, sizeof (procstat_t));
261 sstrncpy (new->name, name, sizeof (new->name));
266 DEBUG ("ProcessMatch: adding \"%s\" as criteria to process %s.", regexp, name);
267 new->re = (regex_t *) malloc (sizeof (regex_t));
270 ERROR ("processes plugin: ps_list_register: malloc failed.");
275 status = regcomp (new->re, regexp, REG_EXTENDED | REG_NOSUB);
278 DEBUG ("ProcessMatch: compiling the regular expression \"%s\" failed.", regexp);
287 ERROR ("processes plugin: ps_list_register: "
288 "Regular expression \"%s\" found in config "
289 "file, but support for regular expressions "
290 "has been disabled at compile time.",
297 for (ptr = list_head_g; ptr != NULL; ptr = ptr->next)
299 if (strcmp (ptr->name, name) == 0)
301 WARNING ("processes plugin: You have configured more "
302 "than one `Process' or "
303 "`ProcessMatch' with the same name. "
304 "All but the first setting will be "
313 if (ptr->next == NULL)
321 } /* void ps_list_register */
323 /* try to match name against entry, returns 1 if success */
324 static int ps_list_match (const char *name, const char *cmdline, procstat_t *ps)
333 if ((str == NULL) || (str[0] == 0))
336 assert (str != NULL);
338 status = regexec (ps->re, str,
347 if (strcmp (ps->name, name) == 0)
351 } /* int ps_list_match */
353 /* add process entry to 'instances' of process 'name' (or refresh it) */
354 static void ps_list_add (const char *name, const char *cmdline, procstat_entry_t *entry)
357 procstat_entry_t *pse;
362 for (ps = list_head_g; ps != NULL; ps = ps->next)
364 if ((ps_list_match (name, cmdline, ps)) == 0)
367 for (pse = ps->instances; pse != NULL; pse = pse->next)
368 if ((pse->id == entry->id) || (pse->next == NULL))
371 if ((pse == NULL) || (pse->id != entry->id))
373 procstat_entry_t *new;
375 new = (procstat_entry_t *) malloc (sizeof (procstat_entry_t));
378 memset (new, 0, sizeof (procstat_entry_t));
390 pse->num_proc = entry->num_proc;
391 pse->num_lwp = entry->num_lwp;
392 pse->vmem_size = entry->vmem_size;
393 pse->vmem_rss = entry->vmem_rss;
394 pse->vmem_data = entry->vmem_data;
395 pse->vmem_code = entry->vmem_code;
396 pse->stack_size = entry->stack_size;
397 pse->io_rchar = entry->io_rchar;
398 pse->io_wchar = entry->io_wchar;
399 pse->io_syscr = entry->io_syscr;
400 pse->io_syscw = entry->io_syscw;
402 ps->num_proc += pse->num_proc;
403 ps->num_lwp += pse->num_lwp;
404 ps->vmem_size += pse->vmem_size;
405 ps->vmem_rss += pse->vmem_rss;
406 ps->vmem_data += pse->vmem_data;
407 ps->vmem_code += pse->vmem_code;
408 ps->stack_size += pse->stack_size;
410 ps->io_rchar += ((pse->io_rchar == -1)?0:pse->io_rchar);
411 ps->io_wchar += ((pse->io_wchar == -1)?0:pse->io_wchar);
412 ps->io_syscr += ((pse->io_syscr == -1)?0:pse->io_syscr);
413 ps->io_syscw += ((pse->io_syscw == -1)?0:pse->io_syscw);
415 if ((entry->vmem_minflt_counter == 0)
416 && (entry->vmem_majflt_counter == 0))
418 pse->vmem_minflt_counter += entry->vmem_minflt;
419 pse->vmem_minflt = entry->vmem_minflt;
421 pse->vmem_majflt_counter += entry->vmem_majflt;
422 pse->vmem_majflt = entry->vmem_majflt;
426 if (entry->vmem_minflt_counter < pse->vmem_minflt_counter)
428 pse->vmem_minflt = entry->vmem_minflt_counter
429 + (ULONG_MAX - pse->vmem_minflt_counter);
433 pse->vmem_minflt = entry->vmem_minflt_counter - pse->vmem_minflt_counter;
435 pse->vmem_minflt_counter = entry->vmem_minflt_counter;
437 if (entry->vmem_majflt_counter < pse->vmem_majflt_counter)
439 pse->vmem_majflt = entry->vmem_majflt_counter
440 + (ULONG_MAX - pse->vmem_majflt_counter);
444 pse->vmem_majflt = entry->vmem_majflt_counter - pse->vmem_majflt_counter;
446 pse->vmem_majflt_counter = entry->vmem_majflt_counter;
449 ps->vmem_minflt_counter += pse->vmem_minflt;
450 ps->vmem_majflt_counter += pse->vmem_majflt;
452 if ((entry->cpu_user_counter == 0)
453 && (entry->cpu_system_counter == 0))
455 pse->cpu_user_counter += entry->cpu_user;
456 pse->cpu_user = entry->cpu_user;
458 pse->cpu_system_counter += entry->cpu_system;
459 pse->cpu_system = entry->cpu_system;
463 if (entry->cpu_user_counter < pse->cpu_user_counter)
465 pse->cpu_user = entry->cpu_user_counter
466 + (ULONG_MAX - pse->cpu_user_counter);
470 pse->cpu_user = entry->cpu_user_counter - pse->cpu_user_counter;
472 pse->cpu_user_counter = entry->cpu_user_counter;
474 if (entry->cpu_system_counter < pse->cpu_system_counter)
476 pse->cpu_system = entry->cpu_system_counter
477 + (ULONG_MAX - pse->cpu_system_counter);
481 pse->cpu_system = entry->cpu_system_counter - pse->cpu_system_counter;
483 pse->cpu_system_counter = entry->cpu_system_counter;
486 ps->cpu_user_counter += pse->cpu_user;
487 ps->cpu_system_counter += pse->cpu_system;
491 /* remove old entries from instances of processes in list_head_g */
492 static void ps_list_reset (void)
495 procstat_entry_t *pse;
496 procstat_entry_t *pse_prev;
498 for (ps = list_head_g; ps != NULL; ps = ps->next)
518 DEBUG ("Removing this procstat entry cause it's too old: "
519 "id = %lu; name = %s;",
522 if (pse_prev == NULL)
524 ps->instances = pse->next;
530 pse_prev->next = pse->next;
532 pse = pse_prev->next;
541 } /* while (pse != NULL) */
542 } /* for (ps = list_head_g; ps != NULL; ps = ps->next) */
545 /* put all pre-defined 'Process' names from config to list_head_g tree */
546 static int ps_config (oconfig_item_t *ci)
551 const size_t max_procname_len = 15;
552 #elif KERNEL_SOLARIS || KERNEL_FREEBSD
553 const size_t max_procname_len = MAXCOMLEN -1;
556 for (i = 0; i < ci->children_num; ++i) {
557 oconfig_item_t *c = ci->children + i;
559 if (strcasecmp (c->key, "Process") == 0)
561 if ((c->values_num != 1)
562 || (OCONFIG_TYPE_STRING != c->values[0].type)) {
563 ERROR ("processes plugin: `Process' expects exactly "
564 "one string argument (got %i).",
569 if (c->children_num != 0) {
570 WARNING ("processes plugin: the `Process' config option "
571 "does not expect any child elements -- ignoring "
572 "content (%i elements) of the <Process '%s'> block.",
573 c->children_num, c->values[0].value.string);
576 #if KERNEL_LINUX || KERNEL_SOLARIS || KERNEL_FREEBSD
577 if (strlen (c->values[0].value.string) > max_procname_len) {
578 WARNING ("processes plugin: this platform has a %zu character limit "
579 "to process names. The `Process \"%s\"' option will "
580 "not work as expected.",
581 max_procname_len, c->values[0].value.string);
585 ps_list_register (c->values[0].value.string, NULL);
587 else if (strcasecmp (c->key, "ProcessMatch") == 0)
589 if ((c->values_num != 2)
590 || (OCONFIG_TYPE_STRING != c->values[0].type)
591 || (OCONFIG_TYPE_STRING != c->values[1].type))
593 ERROR ("processes plugin: `ProcessMatch' needs exactly "
594 "two string arguments (got %i).",
599 if (c->children_num != 0) {
600 WARNING ("processes plugin: the `ProcessMatch' config option "
601 "does not expect any child elements -- ignoring "
602 "content (%i elements) of the <ProcessMatch '%s' '%s'> "
603 "block.", c->children_num, c->values[0].value.string,
604 c->values[1].value.string);
607 ps_list_register (c->values[0].value.string,
608 c->values[1].value.string);
612 ERROR ("processes plugin: The `%s' configuration option is not "
613 "understood and will be ignored.", c->key);
621 static int ps_init (void)
624 kern_return_t status;
626 port_host_self = mach_host_self ();
627 port_task_self = mach_task_self ();
629 if (pset_list != NULL)
631 vm_deallocate (port_task_self,
632 (vm_address_t) pset_list,
633 pset_list_len * sizeof (processor_set_t));
638 if ((status = host_processor_sets (port_host_self,
640 &pset_list_len)) != KERN_SUCCESS)
642 ERROR ("host_processor_sets failed: %s\n",
643 mach_error_string (status));
648 /* #endif HAVE_THREAD_INFO */
651 pagesize_g = sysconf(_SC_PAGESIZE);
652 DEBUG ("pagesize_g = %li; CONFIG_HZ = %i;",
653 pagesize_g, CONFIG_HZ);
654 /* #endif KERNEL_LINUX */
656 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD
657 pagesize = getpagesize();
658 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
660 #elif HAVE_PROCINFO_H
661 pagesize = getpagesize();
662 #endif /* HAVE_PROCINFO_H */
667 /* submit global state (e.g.: qty of zombies, running, etc..) */
668 static void ps_submit_state (const char *state, double value)
671 value_list_t vl = VALUE_LIST_INIT;
673 values[0].gauge = value;
677 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
678 sstrncpy (vl.plugin, "processes", sizeof (vl.plugin));
679 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
680 sstrncpy (vl.type, "ps_state", sizeof (vl.type));
681 sstrncpy (vl.type_instance, state, sizeof (vl.type_instance));
683 plugin_dispatch_values (&vl);
686 /* submit info about specific process (e.g.: memory taken, cpu usage, etc..) */
687 static void ps_submit_proc_list (procstat_t *ps)
690 value_list_t vl = VALUE_LIST_INIT;
694 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
695 sstrncpy (vl.plugin, "processes", sizeof (vl.plugin));
696 sstrncpy (vl.plugin_instance, ps->name, sizeof (vl.plugin_instance));
698 sstrncpy (vl.type, "ps_vm", sizeof (vl.type));
699 vl.values[0].gauge = ps->vmem_size;
701 plugin_dispatch_values (&vl);
703 sstrncpy (vl.type, "ps_rss", sizeof (vl.type));
704 vl.values[0].gauge = ps->vmem_rss;
706 plugin_dispatch_values (&vl);
708 sstrncpy (vl.type, "ps_data", sizeof (vl.type));
709 vl.values[0].gauge = ps->vmem_data;
711 plugin_dispatch_values (&vl);
713 sstrncpy (vl.type, "ps_code", sizeof (vl.type));
714 vl.values[0].gauge = ps->vmem_code;
716 plugin_dispatch_values (&vl);
718 sstrncpy (vl.type, "ps_stacksize", sizeof (vl.type));
719 vl.values[0].gauge = ps->stack_size;
721 plugin_dispatch_values (&vl);
723 sstrncpy (vl.type, "ps_cputime", sizeof (vl.type));
724 vl.values[0].derive = ps->cpu_user_counter;
725 vl.values[1].derive = ps->cpu_system_counter;
727 plugin_dispatch_values (&vl);
729 sstrncpy (vl.type, "ps_count", sizeof (vl.type));
730 vl.values[0].gauge = ps->num_proc;
731 vl.values[1].gauge = ps->num_lwp;
733 plugin_dispatch_values (&vl);
735 sstrncpy (vl.type, "ps_pagefaults", sizeof (vl.type));
736 vl.values[0].derive = ps->vmem_minflt_counter;
737 vl.values[1].derive = ps->vmem_majflt_counter;
739 plugin_dispatch_values (&vl);
741 if ( (ps->io_rchar != -1) && (ps->io_wchar != -1) )
743 sstrncpy (vl.type, "ps_disk_octets", sizeof (vl.type));
744 vl.values[0].derive = ps->io_rchar;
745 vl.values[1].derive = ps->io_wchar;
747 plugin_dispatch_values (&vl);
750 if ( (ps->io_syscr != -1) && (ps->io_syscw != -1) )
752 sstrncpy (vl.type, "ps_disk_ops", sizeof (vl.type));
753 vl.values[0].derive = ps->io_syscr;
754 vl.values[1].derive = ps->io_syscw;
756 plugin_dispatch_values (&vl);
759 DEBUG ("name = %s; num_proc = %lu; num_lwp = %lu; "
760 "vmem_size = %lu; vmem_rss = %lu; vmem_data = %lu; "
762 "vmem_minflt_counter = %"PRIi64"; vmem_majflt_counter = %"PRIi64"; "
763 "cpu_user_counter = %"PRIi64"; cpu_system_counter = %"PRIi64"; "
764 "io_rchar = %"PRIi64"; io_wchar = %"PRIi64"; "
765 "io_syscr = %"PRIi64"; io_syscw = %"PRIi64";",
766 ps->name, ps->num_proc, ps->num_lwp,
767 ps->vmem_size, ps->vmem_rss,
768 ps->vmem_data, ps->vmem_code,
769 ps->vmem_minflt_counter, ps->vmem_majflt_counter,
770 ps->cpu_user_counter, ps->cpu_system_counter,
771 ps->io_rchar, ps->io_wchar, ps->io_syscr, ps->io_syscw);
772 } /* void ps_submit_proc_list */
774 #if KERNEL_LINUX || KERNEL_SOLARIS
775 static void ps_submit_fork_rate (derive_t value)
778 value_list_t vl = VALUE_LIST_INIT;
780 values[0].derive = value;
784 sstrncpy(vl.host, hostname_g, sizeof (vl.host));
785 sstrncpy(vl.plugin, "processes", sizeof (vl.plugin));
786 sstrncpy(vl.plugin_instance, "", sizeof (vl.plugin_instance));
787 sstrncpy(vl.type, "fork_rate", sizeof (vl.type));
788 sstrncpy(vl.type_instance, "", sizeof (vl.type_instance));
790 plugin_dispatch_values(&vl);
792 #endif /* KERNEL_LINUX || KERNEL_SOLARIS*/
794 /* ------- additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
796 static int ps_read_tasks (long pid)
803 ssnprintf (dirname, sizeof (dirname), "/proc/%li/task", pid);
805 if ((dh = opendir (dirname)) == NULL)
807 DEBUG ("Failed to open directory `%s'", dirname);
811 while ((ent = readdir (dh)) != NULL)
813 if (!isdigit ((int) ent->d_name[0]))
820 return ((count >= 1) ? count : 1);
821 } /* int *ps_read_tasks */
823 /* Read advanced virtual memory data from /proc/pid/status */
824 static procstat_t *ps_read_vmem (long pid, procstat_t *ps)
829 unsigned long long lib = 0;
830 unsigned long long exe = 0;
831 unsigned long long data = 0;
835 ssnprintf (filename, sizeof (filename), "/proc/%li/status", pid);
836 if ((fh = fopen (filename, "r")) == NULL)
839 while (fgets (buffer, sizeof(buffer), fh) != NULL)
844 if (strncmp (buffer, "Vm", 2) != 0)
847 numfields = strsplit (buffer, fields,
848 STATIC_ARRAY_SIZE (fields));
855 tmp = strtoll (fields[1], &endptr, /* base = */ 10);
856 if ((errno == 0) && (endptr != fields[1]))
858 if (strncmp (buffer, "VmData", 6) == 0)
862 else if (strncmp (buffer, "VmLib", 5) == 0)
866 else if (strncmp(buffer, "VmExe", 5) == 0)
871 } /* while (fgets) */
876 WARNING ("processes: fclose: %s",
877 sstrerror (errno, errbuf, sizeof (errbuf)));
880 ps->vmem_data = data * 1024;
881 ps->vmem_code = (exe + lib) * 1024;
884 } /* procstat_t *ps_read_vmem */
886 static procstat_t *ps_read_io (long pid, procstat_t *ps)
895 ssnprintf (filename, sizeof (filename), "/proc/%li/io", pid);
896 if ((fh = fopen (filename, "r")) == NULL)
899 while (fgets (buffer, sizeof (buffer), fh) != NULL)
901 derive_t *val = NULL;
905 if (strncasecmp (buffer, "rchar:", 6) == 0)
906 val = &(ps->io_rchar);
907 else if (strncasecmp (buffer, "wchar:", 6) == 0)
908 val = &(ps->io_wchar);
909 else if (strncasecmp (buffer, "syscr:", 6) == 0)
910 val = &(ps->io_syscr);
911 else if (strncasecmp (buffer, "syscw:", 6) == 0)
912 val = &(ps->io_syscw);
916 numfields = strsplit (buffer, fields,
917 STATIC_ARRAY_SIZE (fields));
924 tmp = strtoll (fields[1], &endptr, /* base = */ 10);
925 if ((errno != 0) || (endptr == fields[1]))
928 *val = (derive_t) tmp;
929 } /* while (fgets) */
934 WARNING ("processes: fclose: %s",
935 sstrerror (errno, errbuf, sizeof (errbuf)));
939 } /* procstat_t *ps_read_io */
941 static int ps_read_process (long pid, procstat_t *ps, char *state)
952 size_t name_start_pos;
956 derive_t cpu_user_counter;
957 derive_t cpu_system_counter;
958 long long unsigned vmem_size;
959 long long unsigned vmem_rss;
960 long long unsigned stack_size;
962 memset (ps, 0, sizeof (procstat_t));
964 ssnprintf (filename, sizeof (filename), "/proc/%li/stat", pid);
966 buffer_len = read_file_contents (filename,
967 buffer, sizeof(buffer) - 1);
970 buffer[buffer_len] = 0;
972 /* The name of the process is enclosed in parens. Since the name can
973 * contain parens itself, spaces, numbers and pretty much everything
974 * else, use these to determine the process name. We don't use
975 * strchr(3) and strrchr(3) to avoid pointer arithmetic which would
976 * otherwise be required to determine name_len. */
978 while ((buffer[name_start_pos] != '(')
979 && (name_start_pos < buffer_len))
982 name_end_pos = buffer_len;
983 while ((buffer[name_end_pos] != ')')
984 && (name_end_pos > 0))
987 /* Either '(' or ')' is not found or they are in the wrong order.
988 * Anyway, something weird that shouldn't happen ever. */
989 if (name_start_pos >= name_end_pos)
991 ERROR ("processes plugin: name_start_pos = %zu >= name_end_pos = %zu",
992 name_start_pos, name_end_pos);
996 name_len = (name_end_pos - name_start_pos) - 1;
997 if (name_len >= sizeof (ps->name))
998 name_len = sizeof (ps->name) - 1;
1000 sstrncpy (ps->name, &buffer[name_start_pos + 1], name_len + 1);
1002 if ((buffer_len - name_end_pos) < 2)
1004 buffer_ptr = &buffer[name_end_pos + 2];
1006 fields_len = strsplit (buffer_ptr, fields, STATIC_ARRAY_SIZE (fields));
1007 if (fields_len < 22)
1009 DEBUG ("processes plugin: ps_read_process (pid = %li):"
1010 " `%s' has only %i fields..",
1011 pid, filename, fields_len);
1015 *state = fields[0][0];
1024 if ( (ps->num_lwp = ps_read_tasks (pid)) == -1 )
1026 /* returns -1 => kernel 2.4 */
1032 /* Leave the rest at zero if this is only a zombi */
1033 if (ps->num_proc == 0)
1035 DEBUG ("processes plugin: This is only a zombie: pid = %li; "
1036 "name = %s;", pid, ps->name);
1040 cpu_user_counter = atoll (fields[11]);
1041 cpu_system_counter = atoll (fields[12]);
1042 vmem_size = atoll (fields[20]);
1043 vmem_rss = atoll (fields[21]);
1044 ps->vmem_minflt_counter = atol (fields[7]);
1045 ps->vmem_majflt_counter = atol (fields[9]);
1048 unsigned long long stack_start = atoll (fields[25]);
1049 unsigned long long stack_ptr = atoll (fields[26]);
1051 stack_size = (stack_start > stack_ptr)
1052 ? stack_start - stack_ptr
1053 : stack_ptr - stack_start;
1056 /* Convert jiffies to useconds */
1057 cpu_user_counter = cpu_user_counter * 1000000 / CONFIG_HZ;
1058 cpu_system_counter = cpu_system_counter * 1000000 / CONFIG_HZ;
1059 vmem_rss = vmem_rss * pagesize_g;
1061 if ( (ps_read_vmem(pid, ps)) == NULL)
1066 DEBUG("ps_read_process: did not get vmem data for pid %li", pid);
1069 ps->cpu_user_counter = cpu_user_counter;
1070 ps->cpu_system_counter = cpu_system_counter;
1071 ps->vmem_size = (unsigned long) vmem_size;
1072 ps->vmem_rss = (unsigned long) vmem_rss;
1073 ps->stack_size = (unsigned long) stack_size;
1075 if ( (ps_read_io (pid, ps)) == NULL)
1083 DEBUG("ps_read_process: not get io data for pid %li", pid);
1088 } /* int ps_read_process (...) */
1090 static char *ps_get_cmdline (long pid, char *name, char *buf, size_t buf_len)
1095 char file[PATH_MAX];
1100 if ((pid < 1) || (NULL == buf) || (buf_len < 2))
1103 ssnprintf (file, sizeof (file), "/proc/%li/cmdline", pid);
1106 fd = open (file, O_RDONLY);
1109 /* ENOENT means the process exited while we were handling it.
1110 * Don't complain about this, it only fills the logs. */
1111 if (errno != ENOENT)
1112 WARNING ("processes plugin: Failed to open `%s': %s.", file,
1113 sstrerror (errno, errbuf, sizeof (errbuf)));
1125 status = read (fd, (void *)buf_ptr, len);
1130 if ((EAGAIN == errno) || (EINTR == errno))
1133 WARNING ("processes plugin: Failed to read from `%s': %s.", file,
1134 sstrerror (errno, errbuf, sizeof (errbuf)));
1154 /* cmdline not available; e.g. kernel thread, zombie */
1158 ssnprintf (buf, buf_len, "[%s]", name);
1162 assert (n <= buf_len);
1169 /* remove trailing whitespace */
1170 while ((n > 0) && (isspace (buf[n]) || ('\0' == buf[n]))) {
1175 /* arguments are separated by '\0' in /proc/<pid>/cmdline */
1182 } /* char *ps_get_cmdline (...) */
1184 static int read_fork_rate (void)
1189 _Bool value_valid = 0;
1191 proc_stat = fopen ("/proc/stat", "r");
1192 if (proc_stat == NULL)
1195 ERROR ("processes plugin: fopen (/proc/stat) failed: %s",
1196 sstrerror (errno, errbuf, sizeof (errbuf)));
1200 while (fgets (buffer, sizeof (buffer), proc_stat) != NULL)
1206 fields_num = strsplit (buffer, fields,
1207 STATIC_ARRAY_SIZE (fields));
1208 if (fields_num != 2)
1211 if (strcmp ("processes", fields[0]) != 0)
1214 status = parse_value (fields[1], &value, DS_TYPE_DERIVE);
1225 ps_submit_fork_rate (value.derive);
1228 #endif /*KERNEL_LINUX */
1231 static char *ps_get_cmdline (long pid, char *name __attribute__((unused)), /* {{{ */
1232 char *buffer, size_t buffer_size)
1234 char path[PATH_MAX];
1238 snprintf(path, sizeof (path), "/proc/%li/psinfo", pid);
1240 status = read_file_contents (path, (void *) &info, sizeof (info));
1241 if (status != sizeof (info))
1243 ERROR ("processes plugin: Unexpected return value "
1244 "while reading \"%s\": "
1245 "Returned %i but expected %zu.",
1246 path, status, buffer_size);
1250 info.pr_psargs[sizeof (info.pr_psargs) - 1] = 0;
1251 sstrncpy (buffer, info.pr_psargs, buffer_size);
1254 } /* }}} int ps_get_cmdline */
1257 * Reads process information on the Solaris OS. The information comes mainly from
1258 * /proc/PID/status, /proc/PID/psinfo and /proc/PID/usage
1259 * The values for input and ouput chars are calculated "by hand"
1260 * Added a few "solaris" specific process states as well
1262 static int ps_read_process(long pid, procstat_t *ps, char *state)
1265 char f_psinfo[64], f_usage[64];
1268 pstatus_t *myStatus;
1272 snprintf(filename, sizeof (filename), "/proc/%li/status", pid);
1273 snprintf(f_psinfo, sizeof (f_psinfo), "/proc/%li/psinfo", pid);
1274 snprintf(f_usage, sizeof (f_usage), "/proc/%li/usage", pid);
1277 buffer = malloc(sizeof (pstatus_t));
1278 memset(buffer, 0, sizeof (pstatus_t));
1279 read_file_contents(filename, buffer, sizeof (pstatus_t));
1280 myStatus = (pstatus_t *) buffer;
1282 buffer = malloc(sizeof (psinfo_t));
1283 memset(buffer, 0, sizeof(psinfo_t));
1284 read_file_contents(f_psinfo, buffer, sizeof (psinfo_t));
1285 myInfo = (psinfo_t *) buffer;
1287 buffer = malloc(sizeof (prusage_t));
1288 memset(buffer, 0, sizeof(prusage_t));
1289 read_file_contents(f_usage, buffer, sizeof (prusage_t));
1290 myUsage = (prusage_t *) buffer;
1292 sstrncpy(ps->name, myInfo->pr_fname, sizeof (myInfo->pr_fname));
1293 ps->num_lwp = myStatus->pr_nlwp;
1294 if (myInfo->pr_wstat != 0) {
1297 *state = (char) 'Z';
1305 ps->num_lwp = myInfo->pr_nlwp;
1309 * Convert system time and user time from nanoseconds to microseconds
1310 * for compatibility with the linux module
1312 ps->cpu_system_counter = myStatus -> pr_stime.tv_nsec / 1000;
1313 ps->cpu_user_counter = myStatus -> pr_utime.tv_nsec / 1000;
1316 * Convert rssize from KB to bytes to be consistent w/ the linux module
1318 ps->vmem_rss = myInfo->pr_rssize * 1024;
1319 ps->vmem_size = myInfo->pr_size * 1024;
1320 ps->vmem_minflt_counter = myUsage->pr_minf;
1321 ps->vmem_majflt_counter = myUsage->pr_majf;
1324 * TODO: Data and code segment calculations for Solaris
1329 ps->stack_size = myStatus->pr_stksize;
1332 * Calculating input/ouput chars
1333 * Formula used is total chars / total blocks => chars/block
1334 * then convert input/output blocks to chars
1336 ulong_t tot_chars = myUsage->pr_ioch;
1337 ulong_t tot_blocks = myUsage->pr_inblk + myUsage->pr_oublk;
1338 ulong_t chars_per_block = 1;
1339 if (tot_blocks != 0)
1340 chars_per_block = tot_chars / tot_blocks;
1341 ps->io_rchar = myUsage->pr_inblk * chars_per_block;
1342 ps->io_wchar = myUsage->pr_oublk * chars_per_block;
1343 ps->io_syscr = myUsage->pr_sysc;
1344 ps->io_syscw = myUsage->pr_sysc;
1348 * TODO: Find way of setting BLOCKED and PAGING status
1351 *state = (char) 'R';
1352 if (myStatus->pr_flags & PR_ASLEEP)
1353 *state = (char) 'S';
1354 else if (myStatus->pr_flags & PR_STOPPED)
1355 *state = (char) 'T';
1356 else if (myStatus->pr_flags & PR_DETACH)
1357 *state = (char) 'E';
1358 else if (myStatus->pr_flags & PR_DAEMON)
1359 *state = (char) 'A';
1360 else if (myStatus->pr_flags & PR_ISSYS)
1361 *state = (char) 'Y';
1362 else if (myStatus->pr_flags & PR_ORPHAN)
1363 *state = (char) 'O';
1373 * Reads the number of threads created since the last reboot. On Solaris these
1374 * are retrieved from kstat (module cpu, name sys, class misc, stat nthreads).
1375 * The result is the sum for all the threads created on each cpu
1377 static int read_fork_rate()
1379 extern kstat_ctl_t *kc;
1380 kstat_t *ksp_chain = NULL;
1381 derive_t result = 0;
1386 for (ksp_chain = kc->kc_chain;
1388 ksp_chain = ksp_chain->ks_next)
1390 if ((strcmp (ksp_chain->ks_module, "cpu") == 0)
1391 && (strcmp (ksp_chain->ks_name, "sys") == 0)
1392 && (strcmp (ksp_chain->ks_class, "misc") == 0))
1396 kstat_read (kc, ksp_chain, NULL);
1398 tmp = get_kstat_value(ksp_chain, "nthreads");
1404 ps_submit_fork_rate (result);
1407 #endif /* KERNEL_SOLARIS */
1409 #if HAVE_THREAD_INFO
1410 static int mach_get_task_name (task_t t, int *pid, char *name, size_t name_max_len)
1414 struct kinfo_proc kp;
1419 mib[2] = KERN_PROC_PID;
1421 if (pid_for_task (t, pid) != KERN_SUCCESS)
1425 kp_size = sizeof (kp);
1426 if (sysctl (mib, 4, &kp, &kp_size, NULL, 0) != 0)
1429 if (name_max_len > (MAXCOMLEN + 1))
1430 name_max_len = MAXCOMLEN + 1;
1432 strncpy (name, kp.kp_proc.p_comm, name_max_len - 1);
1433 name[name_max_len - 1] = '\0';
1435 DEBUG ("pid = %i; name = %s;", *pid, name);
1437 /* We don't do the special handling for `p_comm == "LaunchCFMApp"' as
1438 * `top' does it, because it is a lot of work and only used when
1439 * debugging. -octo */
1443 #endif /* HAVE_THREAD_INFO */
1444 /* ------- end of additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
1446 /* do actual readings from kernel */
1447 static int ps_read (void)
1449 #if HAVE_THREAD_INFO
1450 kern_return_t status;
1453 processor_set_t port_pset_priv;
1456 task_array_t task_list;
1457 mach_msg_type_number_t task_list_len;
1460 char task_name[MAXCOMLEN + 1];
1463 thread_act_array_t thread_list;
1464 mach_msg_type_number_t thread_list_len;
1465 thread_basic_info_data_t thread_data;
1466 mach_msg_type_number_t thread_data_len;
1475 procstat_entry_t pse;
1480 * The Mach-concept is a little different from the traditional UNIX
1481 * concept: All the work is done in threads. Threads are contained in
1482 * `tasks'. Therefore, `task status' doesn't make much sense, since
1483 * it's actually a `thread status'.
1484 * Tasks are assigned to sets of processors, so that's where you go to
1487 for (pset = 0; pset < pset_list_len; pset++)
1489 if ((status = host_processor_set_priv (port_host_self,
1491 &port_pset_priv)) != KERN_SUCCESS)
1493 ERROR ("host_processor_set_priv failed: %s\n",
1494 mach_error_string (status));
1498 if ((status = processor_set_tasks (port_pset_priv,
1500 &task_list_len)) != KERN_SUCCESS)
1502 ERROR ("processor_set_tasks failed: %s\n",
1503 mach_error_string (status));
1504 mach_port_deallocate (port_task_self, port_pset_priv);
1508 for (task = 0; task < task_list_len; task++)
1511 if (mach_get_task_name (task_list[task],
1513 task_name, PROCSTAT_NAME_LEN) == 0)
1515 /* search for at least one match */
1516 for (ps = list_head_g; ps != NULL; ps = ps->next)
1517 /* FIXME: cmdline should be here instead of NULL */
1518 if (ps_list_match (task_name, NULL, ps) == 1)
1522 /* Collect more detailed statistics for this process */
1525 task_basic_info_data_t task_basic_info;
1526 mach_msg_type_number_t task_basic_info_len;
1527 task_events_info_data_t task_events_info;
1528 mach_msg_type_number_t task_events_info_len;
1529 task_absolutetime_info_data_t task_absolutetime_info;
1530 mach_msg_type_number_t task_absolutetime_info_len;
1532 memset (&pse, '\0', sizeof (pse));
1535 task_basic_info_len = TASK_BASIC_INFO_COUNT;
1536 status = task_info (task_list[task],
1538 (task_info_t) &task_basic_info,
1539 &task_basic_info_len);
1540 if (status != KERN_SUCCESS)
1542 ERROR ("task_info failed: %s",
1543 mach_error_string (status));
1544 continue; /* with next thread_list */
1547 task_events_info_len = TASK_EVENTS_INFO_COUNT;
1548 status = task_info (task_list[task],
1550 (task_info_t) &task_events_info,
1551 &task_events_info_len);
1552 if (status != KERN_SUCCESS)
1554 ERROR ("task_info failed: %s",
1555 mach_error_string (status));
1556 continue; /* with next thread_list */
1559 task_absolutetime_info_len = TASK_ABSOLUTETIME_INFO_COUNT;
1560 status = task_info (task_list[task],
1561 TASK_ABSOLUTETIME_INFO,
1562 (task_info_t) &task_absolutetime_info,
1563 &task_absolutetime_info_len);
1564 if (status != KERN_SUCCESS)
1566 ERROR ("task_info failed: %s",
1567 mach_error_string (status));
1568 continue; /* with next thread_list */
1572 pse.vmem_size = task_basic_info.virtual_size;
1573 pse.vmem_rss = task_basic_info.resident_size;
1574 /* Does not seem to be easily exposed */
1578 pse.vmem_minflt_counter = task_events_info.cow_faults;
1579 pse.vmem_majflt_counter = task_events_info.faults;
1581 pse.cpu_user_counter = task_absolutetime_info.total_user;
1582 pse.cpu_system_counter = task_absolutetime_info.total_system;
1585 status = task_threads (task_list[task], &thread_list,
1587 if (status != KERN_SUCCESS)
1589 /* Apple's `top' treats this case a zombie. It
1590 * makes sense to some extend: A `zombie'
1591 * thread is nonsense, since the task/process
1594 DEBUG ("task_threads failed: %s",
1595 mach_error_string (status));
1596 if (task_list[task] != port_task_self)
1597 mach_port_deallocate (port_task_self,
1599 continue; /* with next task_list */
1602 for (thread = 0; thread < thread_list_len; thread++)
1604 thread_data_len = THREAD_BASIC_INFO_COUNT;
1605 status = thread_info (thread_list[thread],
1607 (thread_info_t) &thread_data,
1609 if (status != KERN_SUCCESS)
1611 ERROR ("thread_info failed: %s",
1612 mach_error_string (status));
1613 if (task_list[task] != port_task_self)
1614 mach_port_deallocate (port_task_self,
1615 thread_list[thread]);
1616 continue; /* with next thread_list */
1622 switch (thread_data.run_state)
1624 case TH_STATE_RUNNING:
1627 case TH_STATE_STOPPED:
1628 /* What exactly is `halted'? */
1629 case TH_STATE_HALTED:
1632 case TH_STATE_WAITING:
1635 case TH_STATE_UNINTERRUPTIBLE:
1638 /* There is no `zombie' case here,
1639 * since there are no zombie-threads.
1640 * There's only zombie tasks, which are
1643 WARNING ("Unknown thread status: %i",
1644 thread_data.run_state);
1646 } /* switch (thread_data.run_state) */
1648 if (task_list[task] != port_task_self)
1650 status = mach_port_deallocate (port_task_self,
1651 thread_list[thread]);
1652 if (status != KERN_SUCCESS)
1653 ERROR ("mach_port_deallocate failed: %s",
1654 mach_error_string (status));
1656 } /* for (thread_list) */
1658 if ((status = vm_deallocate (port_task_self,
1659 (vm_address_t) thread_list,
1660 thread_list_len * sizeof (thread_act_t)))
1663 ERROR ("vm_deallocate failed: %s",
1664 mach_error_string (status));
1667 thread_list_len = 0;
1669 /* Only deallocate the task port, if it isn't our own.
1670 * Don't know what would happen in that case, but this
1671 * is what Apple's top does.. ;) */
1672 if (task_list[task] != port_task_self)
1674 status = mach_port_deallocate (port_task_self,
1676 if (status != KERN_SUCCESS)
1677 ERROR ("mach_port_deallocate failed: %s",
1678 mach_error_string (status));
1682 /* FIXME: cmdline should be here instead of NULL */
1683 ps_list_add (task_name, NULL, &pse);
1684 } /* for (task_list) */
1686 if ((status = vm_deallocate (port_task_self,
1687 (vm_address_t) task_list,
1688 task_list_len * sizeof (task_t))) != KERN_SUCCESS)
1690 ERROR ("vm_deallocate failed: %s",
1691 mach_error_string (status));
1696 if ((status = mach_port_deallocate (port_task_self, port_pset_priv))
1699 ERROR ("mach_port_deallocate failed: %s",
1700 mach_error_string (status));
1702 } /* for (pset_list) */
1704 ps_submit_state ("running", running);
1705 ps_submit_state ("sleeping", sleeping);
1706 ps_submit_state ("zombies", zombies);
1707 ps_submit_state ("stopped", stopped);
1708 ps_submit_state ("blocked", blocked);
1710 for (ps = list_head_g; ps != NULL; ps = ps->next)
1711 ps_submit_proc_list (ps);
1712 /* #endif HAVE_THREAD_INFO */
1726 char cmdline[ARG_MAX];
1730 procstat_entry_t pse;
1735 running = sleeping = zombies = stopped = paging = blocked = 0;
1738 if ((proc = opendir ("/proc")) == NULL)
1741 ERROR ("Cannot open `/proc': %s",
1742 sstrerror (errno, errbuf, sizeof (errbuf)));
1746 while ((ent = readdir (proc)) != NULL)
1748 if (!isdigit (ent->d_name[0]))
1751 if ((pid = atol (ent->d_name)) < 1)
1754 status = ps_read_process (pid, &ps, &state);
1757 DEBUG ("ps_read_process failed: %i", status);
1764 pse.num_proc = ps.num_proc;
1765 pse.num_lwp = ps.num_lwp;
1766 pse.vmem_size = ps.vmem_size;
1767 pse.vmem_rss = ps.vmem_rss;
1768 pse.vmem_data = ps.vmem_data;
1769 pse.vmem_code = ps.vmem_code;
1770 pse.stack_size = ps.stack_size;
1772 pse.vmem_minflt = 0;
1773 pse.vmem_minflt_counter = ps.vmem_minflt_counter;
1774 pse.vmem_majflt = 0;
1775 pse.vmem_majflt_counter = ps.vmem_majflt_counter;
1778 pse.cpu_user_counter = ps.cpu_user_counter;
1780 pse.cpu_system_counter = ps.cpu_system_counter;
1782 pse.io_rchar = ps.io_rchar;
1783 pse.io_wchar = ps.io_wchar;
1784 pse.io_syscr = ps.io_syscr;
1785 pse.io_syscw = ps.io_syscw;
1789 case 'R': running++; break;
1790 case 'S': sleeping++; break;
1791 case 'D': blocked++; break;
1792 case 'Z': zombies++; break;
1793 case 'T': stopped++; break;
1794 case 'W': paging++; break;
1797 ps_list_add (ps.name,
1798 ps_get_cmdline (pid, ps.name, cmdline, sizeof (cmdline)),
1804 ps_submit_state ("running", running);
1805 ps_submit_state ("sleeping", sleeping);
1806 ps_submit_state ("zombies", zombies);
1807 ps_submit_state ("stopped", stopped);
1808 ps_submit_state ("paging", paging);
1809 ps_submit_state ("blocked", blocked);
1811 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1812 ps_submit_proc_list (ps_ptr);
1815 /* #endif KERNEL_LINUX */
1817 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD
1827 char errbuf[_POSIX2_LINE_MAX];
1828 struct kinfo_proc *procs; /* array of processes */
1829 struct kinfo_proc *proc_ptr = NULL;
1830 int count; /* returns number of processes */
1834 procstat_entry_t pse;
1838 /* Open the kvm interface, get a descriptor */
1839 kd = kvm_openfiles (NULL, "/dev/null", NULL, 0, errbuf);
1842 ERROR ("processes plugin: Cannot open kvm interface: %s",
1847 /* Get the list of processes. */
1848 procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, &count);
1851 ERROR ("processes plugin: Cannot get kvm processes list: %s",
1857 /* Iterate through the processes in kinfo_proc */
1858 for (i = 0; i < count; i++)
1860 /* Create only one process list entry per _process_, i.e.
1861 * filter out threads (duplicate PID entries). */
1862 if ((proc_ptr == NULL) || (proc_ptr->ki_pid != procs[i].ki_pid))
1864 char cmdline[ARG_MAX] = "";
1865 _Bool have_cmdline = 0;
1867 proc_ptr = &(procs[i]);
1868 /* Don't probe system processes and processes without arguments */
1869 if (((procs[i].ki_flag & P_SYSTEM) == 0)
1870 && (procs[i].ki_args != NULL))
1876 /* retrieve the arguments */
1877 argv = kvm_getargv (kd, proc_ptr, /* nchr = */ 0);
1879 if ((argv != NULL) && (argv[0] != NULL))
1881 while (argv[argc] != NULL)
1884 status = strjoin (cmdline, sizeof (cmdline), argv, argc, " ");
1886 WARNING ("processes plugin: Command line did not fit into buffer.");
1890 } /* if (process has argument list) */
1892 pse.id = procs[i].ki_pid;
1896 pse.num_lwp = procs[i].ki_numthreads;
1898 pse.vmem_size = procs[i].ki_size;
1899 pse.vmem_rss = procs[i].ki_rssize * pagesize;
1900 pse.vmem_data = procs[i].ki_dsize * pagesize;
1901 pse.vmem_code = procs[i].ki_tsize * pagesize;
1902 pse.stack_size = procs[i].ki_ssize * pagesize;
1903 pse.vmem_minflt = 0;
1904 pse.vmem_minflt_counter = procs[i].ki_rusage.ru_minflt;
1905 pse.vmem_majflt = 0;
1906 pse.vmem_majflt_counter = procs[i].ki_rusage.ru_majflt;
1910 pse.cpu_user_counter = 0;
1911 pse.cpu_system_counter = 0;
1913 * The u-area might be swapped out, and we can't get
1914 * at it because we have a crashdump and no swap.
1915 * If it's here fill in these fields, otherwise, just
1918 if (procs[i].ki_flag & P_INMEM)
1920 pse.cpu_user_counter = procs[i].ki_rusage.ru_utime.tv_usec
1921 + (1000000lu * procs[i].ki_rusage.ru_utime.tv_sec);
1922 pse.cpu_system_counter = procs[i].ki_rusage.ru_stime.tv_usec
1923 + (1000000lu * procs[i].ki_rusage.ru_stime.tv_sec);
1932 ps_list_add (procs[i].ki_comm, have_cmdline ? cmdline : NULL, &pse);
1934 switch (procs[i].ki_stat)
1936 case SSTOP: stopped++; break;
1937 case SSLEEP: sleeping++; break;
1938 case SRUN: running++; break;
1939 case SIDL: idle++; break;
1940 case SWAIT: wait++; break;
1941 case SLOCK: blocked++; break;
1942 case SZOMB: zombies++; break;
1944 } /* if ((proc_ptr == NULL) || (proc_ptr->ki_pid != procs[i].ki_pid)) */
1949 ps_submit_state ("running", running);
1950 ps_submit_state ("sleeping", sleeping);
1951 ps_submit_state ("zombies", zombies);
1952 ps_submit_state ("stopped", stopped);
1953 ps_submit_state ("blocked", blocked);
1954 ps_submit_state ("idle", idle);
1955 ps_submit_state ("wait", wait);
1957 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1958 ps_submit_proc_list (ps_ptr);
1959 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
1961 #elif HAVE_PROCINFO_H
1974 procstat_entry_t pse;
1977 while ((nprocs = getprocs64 (procentry, sizeof(struct procentry64),
1978 /* fdsinfo = */ NULL, sizeof(struct fdsinfo64),
1979 &pindex, MAXPROCENTRY)) > 0)
1983 for (i = 0; i < nprocs; i++)
1987 char arglist[MAXARGLN+1];
1991 if (procentry[i].pi_state == SNONE) continue;
1992 /* if (procentry[i].pi_state == SZOMB) FIXME */
1994 cmdline = procentry[i].pi_comm;
1995 cargs = procentry[i].pi_comm;
1996 if ( procentry[i].pi_flags & SKPROC )
1998 if (procentry[i].pi_pid == 0)
1999 cmdline = "swapper";
2004 if (getargs(&procentry[i], sizeof(struct procentry64), arglist, MAXARGLN) >= 0)
2009 while (++n < MAXARGLN)
2011 if (arglist[n] == '\0')
2013 if (arglist[n+1] == '\0')
2022 pse.id = procentry[i].pi_pid;
2024 pse.num_lwp = procentry[i].pi_thcount;
2028 while ((nthreads = getthrds64(procentry[i].pi_pid,
2029 thrdentry, sizeof(struct thrdentry64),
2030 &thindex, MAXTHRDENTRY)) > 0)
2034 for (j=0; j< nthreads; j++)
2036 switch (thrdentry[j].ti_state)
2038 /* case TSNONE: break; */
2039 case TSIDL: blocked++; break; /* FIXME is really blocked */
2040 case TSRUN: running++; break;
2041 case TSSLEEP: sleeping++; break;
2042 case TSSWAP: paging++; break;
2043 case TSSTOP: stopped++; break;
2044 case TSZOMB: zombies++; break;
2047 if (nthreads < MAXTHRDENTRY)
2052 /* tv_usec is nanosec ??? */
2053 pse.cpu_user_counter = procentry[i].pi_ru.ru_utime.tv_sec * 1000000 +
2054 procentry[i].pi_ru.ru_utime.tv_usec / 1000;
2057 /* tv_usec is nanosec ??? */
2058 pse.cpu_system_counter = procentry[i].pi_ru.ru_stime.tv_sec * 1000000 +
2059 procentry[i].pi_ru.ru_stime.tv_usec / 1000;
2061 pse.vmem_minflt = 0;
2062 pse.vmem_minflt_counter = procentry[i].pi_minflt;
2063 pse.vmem_majflt = 0;
2064 pse.vmem_majflt_counter = procentry[i].pi_majflt;
2066 pse.vmem_size = procentry[i].pi_tsize + procentry[i].pi_dvm * pagesize;
2067 pse.vmem_rss = (procentry[i].pi_drss + procentry[i].pi_trss) * pagesize;
2078 ps_list_add (cmdline, cargs, &pse);
2079 } /* for (i = 0 .. nprocs) */
2081 if (nprocs < MAXPROCENTRY)
2083 } /* while (getprocs64() > 0) */
2084 ps_submit_state ("running", running);
2085 ps_submit_state ("sleeping", sleeping);
2086 ps_submit_state ("zombies", zombies);
2087 ps_submit_state ("stopped", stopped);
2088 ps_submit_state ("paging", paging);
2089 ps_submit_state ("blocked", blocked);
2091 for (ps = list_head_g; ps != NULL; ps = ps->next)
2092 ps_submit_proc_list (ps);
2093 /* #endif HAVE_PROCINFO_H */
2095 #elif KERNEL_SOLARIS
2097 * The Solaris section adds a few more process states and removes some
2098 * process states compared to linux. Most notably there is no "PAGING"
2099 * and "BLOCKED" state for a process. The rest is similar to the linux
2118 char cmdline[PRARGSZ];
2122 proc = opendir ("/proc");
2126 while ((ent = readdir(proc)) != NULL)
2130 procstat_entry_t pse;
2133 if (!isdigit ((int) ent->d_name[0]))
2136 pid = strtol (ent->d_name, &endptr, 10);
2137 if (*endptr != 0) /* value didn't completely parse as a number */
2140 status = ps_read_process (pid, &ps, &state);
2143 DEBUG("ps_read_process failed: %i", status);
2150 pse.num_proc = ps.num_proc;
2151 pse.num_lwp = ps.num_lwp;
2152 pse.vmem_size = ps.vmem_size;
2153 pse.vmem_rss = ps.vmem_rss;
2154 pse.vmem_data = ps.vmem_data;
2155 pse.vmem_code = ps.vmem_code;
2156 pse.stack_size = ps.stack_size;
2158 pse.vmem_minflt = 0;
2159 pse.vmem_minflt_counter = ps.vmem_minflt_counter;
2160 pse.vmem_majflt = 0;
2161 pse.vmem_majflt_counter = ps.vmem_majflt_counter;
2164 pse.cpu_user_counter = ps.cpu_user_counter;
2166 pse.cpu_system_counter = ps.cpu_system_counter;
2168 pse.io_rchar = ps.io_rchar;
2169 pse.io_wchar = ps.io_wchar;
2170 pse.io_syscr = ps.io_syscr;
2171 pse.io_syscw = ps.io_syscw;
2175 case 'R': running++; break;
2176 case 'S': sleeping++; break;
2177 case 'E': detached++; break;
2178 case 'Z': zombies++; break;
2179 case 'T': stopped++; break;
2180 case 'A': daemon++; break;
2181 case 'Y': system++; break;
2182 case 'O': orphan++; break;
2186 ps_list_add (ps.name,
2187 ps_get_cmdline (pid, ps.name, cmdline, sizeof (cmdline)),
2189 } /* while(readdir) */
2192 ps_submit_state ("running", running);
2193 ps_submit_state ("sleeping", sleeping);
2194 ps_submit_state ("zombies", zombies);
2195 ps_submit_state ("stopped", stopped);
2196 ps_submit_state ("detached", detached);
2197 ps_submit_state ("daemon", daemon);
2198 ps_submit_state ("system", system);
2199 ps_submit_state ("orphan", orphan);
2201 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
2202 ps_submit_proc_list (ps_ptr);
2205 #endif /* KERNEL_SOLARIS */
2210 void module_register (void)
2212 plugin_register_complex_config ("processes", ps_config);
2213 plugin_register_init ("processes", ps_init);
2214 plugin_register_read ("processes", ps_read);
2215 } /* void module_register */