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 */
117 /* #endif KERNEL_SOLARIS */
120 # error "No applicable input method."
132 # define ARG_MAX 4096
135 typedef struct procstat_entry_s
140 unsigned long num_proc;
141 unsigned long num_lwp;
142 unsigned long vmem_size;
143 unsigned long vmem_rss;
144 unsigned long vmem_data;
145 unsigned long vmem_code;
146 unsigned long stack_size;
148 unsigned long vmem_minflt;
149 unsigned long vmem_majflt;
150 derive_t vmem_minflt_counter;
151 derive_t vmem_majflt_counter;
153 unsigned long cpu_user;
154 unsigned long cpu_system;
155 derive_t cpu_user_counter;
156 derive_t cpu_system_counter;
164 struct procstat_entry_s *next;
167 #define PROCSTAT_NAME_LEN 256
168 typedef struct procstat
170 char name[PROCSTAT_NAME_LEN];
175 unsigned long num_proc;
176 unsigned long num_lwp;
177 unsigned long vmem_size;
178 unsigned long vmem_rss;
179 unsigned long vmem_data;
180 unsigned long vmem_code;
181 unsigned long stack_size;
183 derive_t vmem_minflt_counter;
184 derive_t vmem_majflt_counter;
186 derive_t cpu_user_counter;
187 derive_t cpu_system_counter;
195 struct procstat *next;
196 struct procstat_entry_s *instances;
199 static procstat_t *list_head_g = NULL;
202 static mach_port_t port_host_self;
203 static mach_port_t port_task_self;
205 static processor_set_name_array_t pset_list;
206 static mach_msg_type_number_t pset_list_len;
207 /* #endif HAVE_THREAD_INFO */
210 static long pagesize_g;
211 /* #endif KERNEL_LINUX */
213 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD
215 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
217 #elif HAVE_PROCINFO_H
218 static struct procentry64 procentry[MAXPROCENTRY];
219 static struct thrdentry64 thrdentry[MAXTHRDENTRY];
222 #ifndef _AIXVERSION_610
223 int getprocs64 (void *procsinfo, int sizproc, void *fdsinfo, int sizfd, pid_t *index, int count);
224 int getthrds64( pid_t, void *, int, tid64_t *, int );
226 int getargs (struct procentry64 *processBuffer, int bufferLen, char *argsBuffer, int argsLen);
227 #endif /* HAVE_PROCINFO_H */
229 /* put name of process from config to list_head_g tree
230 * list_head_g is a list of 'procstat_t' structs with
231 * processes names we want to watch */
232 static void ps_list_register (const char *name, const char *regexp)
238 new = (procstat_t *) malloc (sizeof (procstat_t));
241 ERROR ("processes plugin: ps_list_register: malloc failed.");
244 memset (new, 0, sizeof (procstat_t));
245 sstrncpy (new->name, name, sizeof (new->name));
250 DEBUG ("ProcessMatch: adding \"%s\" as criteria to process %s.", regexp, name);
251 new->re = (regex_t *) malloc (sizeof (regex_t));
254 ERROR ("processes plugin: ps_list_register: malloc failed.");
259 status = regcomp (new->re, regexp, REG_EXTENDED | REG_NOSUB);
262 DEBUG ("ProcessMatch: compiling the regular expression \"%s\" failed.", regexp);
270 ERROR ("processes plugin: ps_list_register: "
271 "Regular expression \"%s\" found in config "
272 "file, but support for regular expressions "
273 "has been disabled at compile time.",
280 for (ptr = list_head_g; ptr != NULL; ptr = ptr->next)
282 if (strcmp (ptr->name, name) == 0)
284 WARNING ("processes plugin: You have configured more "
285 "than one `Process' or "
286 "`ProcessMatch' with the same name. "
287 "All but the first setting will be "
294 if (ptr->next == NULL)
302 } /* void ps_list_register */
304 /* try to match name against entry, returns 1 if success */
305 static int ps_list_match (const char *name, const char *cmdline, procstat_t *ps)
314 if ((str == NULL) || (str[0] == 0))
317 assert (str != NULL);
319 status = regexec (ps->re, str,
328 if (strcmp (ps->name, name) == 0)
332 } /* int ps_list_match */
334 /* add process entry to 'instances' of process 'name' (or refresh it) */
335 static void ps_list_add (const char *name, const char *cmdline, procstat_entry_t *entry)
338 procstat_entry_t *pse;
343 for (ps = list_head_g; ps != NULL; ps = ps->next)
345 if ((ps_list_match (name, cmdline, ps)) == 0)
348 for (pse = ps->instances; pse != NULL; pse = pse->next)
349 if ((pse->id == entry->id) || (pse->next == NULL))
352 if ((pse == NULL) || (pse->id != entry->id))
354 procstat_entry_t *new;
356 new = (procstat_entry_t *) malloc (sizeof (procstat_entry_t));
359 memset (new, 0, sizeof (procstat_entry_t));
371 pse->num_proc = entry->num_proc;
372 pse->num_lwp = entry->num_lwp;
373 pse->vmem_size = entry->vmem_size;
374 pse->vmem_rss = entry->vmem_rss;
375 pse->vmem_data = entry->vmem_data;
376 pse->vmem_code = entry->vmem_code;
377 pse->stack_size = entry->stack_size;
378 pse->io_rchar = entry->io_rchar;
379 pse->io_wchar = entry->io_wchar;
380 pse->io_syscr = entry->io_syscr;
381 pse->io_syscw = entry->io_syscw;
383 ps->num_proc += pse->num_proc;
384 ps->num_lwp += pse->num_lwp;
385 ps->vmem_size += pse->vmem_size;
386 ps->vmem_rss += pse->vmem_rss;
387 ps->vmem_data += pse->vmem_data;
388 ps->vmem_code += pse->vmem_code;
389 ps->stack_size += pse->stack_size;
391 ps->io_rchar += ((pse->io_rchar == -1)?0:pse->io_rchar);
392 ps->io_wchar += ((pse->io_wchar == -1)?0:pse->io_wchar);
393 ps->io_syscr += ((pse->io_syscr == -1)?0:pse->io_syscr);
394 ps->io_syscw += ((pse->io_syscw == -1)?0:pse->io_syscw);
396 if ((entry->vmem_minflt_counter == 0)
397 && (entry->vmem_majflt_counter == 0))
399 pse->vmem_minflt_counter += entry->vmem_minflt;
400 pse->vmem_minflt = entry->vmem_minflt;
402 pse->vmem_majflt_counter += entry->vmem_majflt;
403 pse->vmem_majflt = entry->vmem_majflt;
407 if (entry->vmem_minflt_counter < pse->vmem_minflt_counter)
409 pse->vmem_minflt = entry->vmem_minflt_counter
410 + (ULONG_MAX - pse->vmem_minflt_counter);
414 pse->vmem_minflt = entry->vmem_minflt_counter - pse->vmem_minflt_counter;
416 pse->vmem_minflt_counter = entry->vmem_minflt_counter;
418 if (entry->vmem_majflt_counter < pse->vmem_majflt_counter)
420 pse->vmem_majflt = entry->vmem_majflt_counter
421 + (ULONG_MAX - pse->vmem_majflt_counter);
425 pse->vmem_majflt = entry->vmem_majflt_counter - pse->vmem_majflt_counter;
427 pse->vmem_majflt_counter = entry->vmem_majflt_counter;
430 ps->vmem_minflt_counter += pse->vmem_minflt;
431 ps->vmem_majflt_counter += pse->vmem_majflt;
433 if ((entry->cpu_user_counter == 0)
434 && (entry->cpu_system_counter == 0))
436 pse->cpu_user_counter += entry->cpu_user;
437 pse->cpu_user = entry->cpu_user;
439 pse->cpu_system_counter += entry->cpu_system;
440 pse->cpu_system = entry->cpu_system;
444 if (entry->cpu_user_counter < pse->cpu_user_counter)
446 pse->cpu_user = entry->cpu_user_counter
447 + (ULONG_MAX - pse->cpu_user_counter);
451 pse->cpu_user = entry->cpu_user_counter - pse->cpu_user_counter;
453 pse->cpu_user_counter = entry->cpu_user_counter;
455 if (entry->cpu_system_counter < pse->cpu_system_counter)
457 pse->cpu_system = entry->cpu_system_counter
458 + (ULONG_MAX - pse->cpu_system_counter);
462 pse->cpu_system = entry->cpu_system_counter - pse->cpu_system_counter;
464 pse->cpu_system_counter = entry->cpu_system_counter;
467 ps->cpu_user_counter += pse->cpu_user;
468 ps->cpu_system_counter += pse->cpu_system;
472 /* remove old entries from instances of processes in list_head_g */
473 static void ps_list_reset (void)
476 procstat_entry_t *pse;
477 procstat_entry_t *pse_prev;
479 for (ps = list_head_g; ps != NULL; ps = ps->next)
499 DEBUG ("Removing this procstat entry cause it's too old: "
500 "id = %lu; name = %s;",
503 if (pse_prev == NULL)
505 ps->instances = pse->next;
511 pse_prev->next = pse->next;
513 pse = pse_prev->next;
522 } /* while (pse != NULL) */
523 } /* for (ps = list_head_g; ps != NULL; ps = ps->next) */
526 /* put all pre-defined 'Process' names from config to list_head_g tree */
527 static int ps_config (oconfig_item_t *ci)
531 for (i = 0; i < ci->children_num; ++i) {
532 oconfig_item_t *c = ci->children + i;
534 if (strcasecmp (c->key, "Process") == 0)
536 if ((c->values_num != 1)
537 || (OCONFIG_TYPE_STRING != c->values[0].type)) {
538 ERROR ("processes plugin: `Process' expects exactly "
539 "one string argument (got %i).",
544 if (c->children_num != 0) {
545 WARNING ("processes plugin: the `Process' config option "
546 "does not expect any child elements -- ignoring "
547 "content (%i elements) of the <Process '%s'> block.",
548 c->children_num, c->values[0].value.string);
551 ps_list_register (c->values[0].value.string, NULL);
553 else if (strcasecmp (c->key, "ProcessMatch") == 0)
555 if ((c->values_num != 2)
556 || (OCONFIG_TYPE_STRING != c->values[0].type)
557 || (OCONFIG_TYPE_STRING != c->values[1].type))
559 ERROR ("processes plugin: `ProcessMatch' needs exactly "
560 "two string arguments (got %i).",
565 if (c->children_num != 0) {
566 WARNING ("processes plugin: the `ProcessMatch' config option "
567 "does not expect any child elements -- ignoring "
568 "content (%i elements) of the <ProcessMatch '%s' '%s'> "
569 "block.", c->children_num, c->values[0].value.string,
570 c->values[1].value.string);
573 ps_list_register (c->values[0].value.string,
574 c->values[1].value.string);
578 ERROR ("processes plugin: The `%s' configuration option is not "
579 "understood and will be ignored.", c->key);
587 static int ps_init (void)
590 kern_return_t status;
592 port_host_self = mach_host_self ();
593 port_task_self = mach_task_self ();
595 if (pset_list != NULL)
597 vm_deallocate (port_task_self,
598 (vm_address_t) pset_list,
599 pset_list_len * sizeof (processor_set_t));
604 if ((status = host_processor_sets (port_host_self,
606 &pset_list_len)) != KERN_SUCCESS)
608 ERROR ("host_processor_sets failed: %s\n",
609 mach_error_string (status));
614 /* #endif HAVE_THREAD_INFO */
617 pagesize_g = sysconf(_SC_PAGESIZE);
618 DEBUG ("pagesize_g = %li; CONFIG_HZ = %i;",
619 pagesize_g, CONFIG_HZ);
620 /* #endif KERNEL_LINUX */
622 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD
623 pagesize = getpagesize();
624 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
626 #elif HAVE_PROCINFO_H
627 pagesize = getpagesize();
628 #endif /* HAVE_PROCINFO_H */
633 /* submit global state (e.g.: qty of zombies, running, etc..) */
634 static void ps_submit_state (const char *state, double value)
637 value_list_t vl = VALUE_LIST_INIT;
639 values[0].gauge = value;
643 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
644 sstrncpy (vl.plugin, "processes", sizeof (vl.plugin));
645 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
646 sstrncpy (vl.type, "ps_state", sizeof (vl.type));
647 sstrncpy (vl.type_instance, state, sizeof (vl.type_instance));
649 plugin_dispatch_values (&vl);
652 /* submit info about specific process (e.g.: memory taken, cpu usage, etc..) */
653 static void ps_submit_proc_list (procstat_t *ps)
656 value_list_t vl = VALUE_LIST_INIT;
660 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
661 sstrncpy (vl.plugin, "processes", sizeof (vl.plugin));
662 sstrncpy (vl.plugin_instance, ps->name, sizeof (vl.plugin_instance));
664 sstrncpy (vl.type, "ps_vm", sizeof (vl.type));
665 vl.values[0].gauge = ps->vmem_size;
667 plugin_dispatch_values (&vl);
669 sstrncpy (vl.type, "ps_rss", sizeof (vl.type));
670 vl.values[0].gauge = ps->vmem_rss;
672 plugin_dispatch_values (&vl);
674 sstrncpy (vl.type, "ps_data", sizeof (vl.type));
675 vl.values[0].gauge = ps->vmem_data;
677 plugin_dispatch_values (&vl);
679 sstrncpy (vl.type, "ps_code", sizeof (vl.type));
680 vl.values[0].gauge = ps->vmem_code;
682 plugin_dispatch_values (&vl);
684 sstrncpy (vl.type, "ps_stacksize", sizeof (vl.type));
685 vl.values[0].gauge = ps->stack_size;
687 plugin_dispatch_values (&vl);
689 sstrncpy (vl.type, "ps_cputime", sizeof (vl.type));
690 vl.values[0].derive = ps->cpu_user_counter;
691 vl.values[1].derive = ps->cpu_system_counter;
693 plugin_dispatch_values (&vl);
695 sstrncpy (vl.type, "ps_count", sizeof (vl.type));
696 vl.values[0].gauge = ps->num_proc;
697 vl.values[1].gauge = ps->num_lwp;
699 plugin_dispatch_values (&vl);
701 sstrncpy (vl.type, "ps_pagefaults", sizeof (vl.type));
702 vl.values[0].derive = ps->vmem_minflt_counter;
703 vl.values[1].derive = ps->vmem_majflt_counter;
705 plugin_dispatch_values (&vl);
707 if ( (ps->io_rchar != -1) && (ps->io_wchar != -1) )
709 sstrncpy (vl.type, "ps_disk_octets", sizeof (vl.type));
710 vl.values[0].derive = ps->io_rchar;
711 vl.values[1].derive = ps->io_wchar;
713 plugin_dispatch_values (&vl);
716 if ( (ps->io_syscr != -1) && (ps->io_syscw != -1) )
718 sstrncpy (vl.type, "ps_disk_ops", sizeof (vl.type));
719 vl.values[0].derive = ps->io_syscr;
720 vl.values[1].derive = ps->io_syscw;
722 plugin_dispatch_values (&vl);
725 DEBUG ("name = %s; num_proc = %lu; num_lwp = %lu; "
726 "vmem_size = %lu; vmem_rss = %lu; vmem_data = %lu; "
728 "vmem_minflt_counter = %"PRIi64"; vmem_majflt_counter = %"PRIi64"; "
729 "cpu_user_counter = %"PRIi64"; cpu_system_counter = %"PRIi64"; "
730 "io_rchar = %"PRIi64"; io_wchar = %"PRIi64"; "
731 "io_syscr = %"PRIi64"; io_syscw = %"PRIi64";",
732 ps->name, ps->num_proc, ps->num_lwp,
733 ps->vmem_size, ps->vmem_rss,
734 ps->vmem_data, ps->vmem_code,
735 ps->vmem_minflt_counter, ps->vmem_majflt_counter,
736 ps->cpu_user_counter, ps->cpu_system_counter,
737 ps->io_rchar, ps->io_wchar, ps->io_syscr, ps->io_syscw);
738 } /* void ps_submit_proc_list */
740 #if KERNEL_LINUX || KERNEL_SOLARIS
741 static void ps_submit_fork_rate (derive_t value)
744 value_list_t vl = VALUE_LIST_INIT;
746 values[0].derive = value;
750 sstrncpy(vl.host, hostname_g, sizeof (vl.host));
751 sstrncpy(vl.plugin, "processes", sizeof (vl.plugin));
752 sstrncpy(vl.plugin_instance, "", sizeof (vl.plugin_instance));
753 sstrncpy(vl.type, "fork_rate", sizeof (vl.type));
754 sstrncpy(vl.type_instance, "", sizeof (vl.type_instance));
756 plugin_dispatch_values(&vl);
758 #endif /* KERNEL_LINUX || KERNEL_SOLARIS*/
760 /* ------- additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
762 static int ps_read_tasks (int pid)
769 ssnprintf (dirname, sizeof (dirname), "/proc/%i/task", pid);
771 if ((dh = opendir (dirname)) == NULL)
773 DEBUG ("Failed to open directory `%s'", dirname);
777 while ((ent = readdir (dh)) != NULL)
779 if (!isdigit ((int) ent->d_name[0]))
786 return ((count >= 1) ? count : 1);
787 } /* int *ps_read_tasks */
789 /* Read advanced virtual memory data from /proc/pid/status */
790 static procstat_t *ps_read_vmem (int pid, procstat_t *ps)
795 unsigned long long lib = 0;
796 unsigned long long exe = 0;
797 unsigned long long data = 0;
801 ssnprintf (filename, sizeof (filename), "/proc/%i/status", pid);
802 if ((fh = fopen (filename, "r")) == NULL)
805 while (fgets (buffer, sizeof(buffer), fh) != NULL)
810 if (strncmp (buffer, "Vm", 2) != 0)
813 numfields = strsplit (buffer, fields,
814 STATIC_ARRAY_SIZE (fields));
821 tmp = strtoll (fields[1], &endptr, /* base = */ 10);
822 if ((errno == 0) && (endptr != fields[1]))
824 if (strncmp (buffer, "VmData", 6) == 0)
828 else if (strncmp (buffer, "VmLib", 5) == 0)
832 else if (strncmp(buffer, "VmExe", 5) == 0)
837 } /* while (fgets) */
842 WARNING ("processes: fclose: %s",
843 sstrerror (errno, errbuf, sizeof (errbuf)));
846 ps->vmem_data = data * 1024;
847 ps->vmem_code = (exe + lib) * 1024;
850 } /* procstat_t *ps_read_vmem */
852 static procstat_t *ps_read_io (int pid, procstat_t *ps)
861 ssnprintf (filename, sizeof (filename), "/proc/%i/io", pid);
862 if ((fh = fopen (filename, "r")) == NULL)
865 while (fgets (buffer, sizeof (buffer), fh) != NULL)
867 derive_t *val = NULL;
871 if (strncasecmp (buffer, "rchar:", 6) == 0)
872 val = &(ps->io_rchar);
873 else if (strncasecmp (buffer, "wchar:", 6) == 0)
874 val = &(ps->io_wchar);
875 else if (strncasecmp (buffer, "syscr:", 6) == 0)
876 val = &(ps->io_syscr);
877 else if (strncasecmp (buffer, "syscw:", 6) == 0)
878 val = &(ps->io_syscw);
882 numfields = strsplit (buffer, fields,
883 STATIC_ARRAY_SIZE (fields));
890 tmp = strtoll (fields[1], &endptr, /* base = */ 10);
891 if ((errno != 0) || (endptr == fields[1]))
894 *val = (derive_t) tmp;
895 } /* while (fgets) */
900 WARNING ("processes: fclose: %s",
901 sstrerror (errno, errbuf, sizeof (errbuf)));
905 } /* procstat_t *ps_read_io */
907 int ps_read_process (int pid, procstat_t *ps, char *state)
918 size_t name_start_pos;
922 derive_t cpu_user_counter;
923 derive_t cpu_system_counter;
924 long long unsigned vmem_size;
925 long long unsigned vmem_rss;
926 long long unsigned stack_size;
928 memset (ps, 0, sizeof (procstat_t));
930 ssnprintf (filename, sizeof (filename), "/proc/%i/stat", pid);
932 buffer_len = read_file_contents (filename,
933 buffer, sizeof(buffer) - 1);
936 buffer[buffer_len] = 0;
938 /* The name of the process is enclosed in parens. Since the name can
939 * contain parens itself, spaces, numbers and pretty much everything
940 * else, use these to determine the process name. We don't use
941 * strchr(3) and strrchr(3) to avoid pointer arithmetic which would
942 * otherwise be required to determine name_len. */
944 while ((buffer[name_start_pos] != '(')
945 && (name_start_pos < buffer_len))
948 name_end_pos = buffer_len;
949 while ((buffer[name_end_pos] != ')')
950 && (name_end_pos > 0))
953 /* Either '(' or ')' is not found or they are in the wrong order.
954 * Anyway, something weird that shouldn't happen ever. */
955 if (name_start_pos >= name_end_pos)
957 ERROR ("processes plugin: name_start_pos = %zu >= name_end_pos = %zu",
958 name_start_pos, name_end_pos);
962 name_len = (name_end_pos - name_start_pos) - 1;
963 if (name_len >= sizeof (ps->name))
964 name_len = sizeof (ps->name) - 1;
966 sstrncpy (ps->name, &buffer[name_start_pos + 1], name_len + 1);
968 if ((buffer_len - name_end_pos) < 2)
970 buffer_ptr = &buffer[name_end_pos + 2];
972 fields_len = strsplit (buffer_ptr, fields, STATIC_ARRAY_SIZE (fields));
975 DEBUG ("processes plugin: ps_read_process (pid = %i):"
976 " `%s' has only %i fields..",
977 (int) pid, filename, fields_len);
981 *state = fields[0][0];
990 if ( (ps->num_lwp = ps_read_tasks (pid)) == -1 )
992 /* returns -1 => kernel 2.4 */
998 /* Leave the rest at zero if this is only a zombi */
999 if (ps->num_proc == 0)
1001 DEBUG ("processes plugin: This is only a zombi: pid = %i; "
1002 "name = %s;", pid, ps->name);
1006 cpu_user_counter = atoll (fields[11]);
1007 cpu_system_counter = atoll (fields[12]);
1008 vmem_size = atoll (fields[20]);
1009 vmem_rss = atoll (fields[21]);
1010 ps->vmem_minflt_counter = atol (fields[7]);
1011 ps->vmem_majflt_counter = atol (fields[9]);
1014 unsigned long long stack_start = atoll (fields[25]);
1015 unsigned long long stack_ptr = atoll (fields[26]);
1017 stack_size = (stack_start > stack_ptr)
1018 ? stack_start - stack_ptr
1019 : stack_ptr - stack_start;
1022 /* Convert jiffies to useconds */
1023 cpu_user_counter = cpu_user_counter * 1000000 / CONFIG_HZ;
1024 cpu_system_counter = cpu_system_counter * 1000000 / CONFIG_HZ;
1025 vmem_rss = vmem_rss * pagesize_g;
1027 if ( (ps_read_vmem(pid, ps)) == NULL)
1032 DEBUG("ps_read_process: did not get vmem data for pid %i",pid);
1035 ps->cpu_user_counter = cpu_user_counter;
1036 ps->cpu_system_counter = cpu_system_counter;
1037 ps->vmem_size = (unsigned long) vmem_size;
1038 ps->vmem_rss = (unsigned long) vmem_rss;
1039 ps->stack_size = (unsigned long) stack_size;
1041 if ( (ps_read_io (pid, ps)) == NULL)
1049 DEBUG("ps_read_process: not get io data for pid %i",pid);
1054 } /* int ps_read_process (...) */
1056 static char *ps_get_cmdline (pid_t pid, char *name, char *buf, size_t buf_len)
1061 char file[PATH_MAX];
1066 if ((pid < 1) || (NULL == buf) || (buf_len < 2))
1069 ssnprintf (file, sizeof (file), "/proc/%u/cmdline",
1070 (unsigned int) pid);
1073 fd = open (file, O_RDONLY);
1076 /* ENOENT means the process exited while we were handling it.
1077 * Don't complain about this, it only fills the logs. */
1078 if (errno != ENOENT)
1079 WARNING ("processes plugin: Failed to open `%s': %s.", file,
1080 sstrerror (errno, errbuf, sizeof (errbuf)));
1092 status = read (fd, (void *)buf_ptr, len);
1097 if ((EAGAIN == errno) || (EINTR == errno))
1100 WARNING ("processes plugin: Failed to read from `%s': %s.", file,
1101 sstrerror (errno, errbuf, sizeof (errbuf)));
1121 /* cmdline not available; e.g. kernel thread, zombie */
1125 ssnprintf (buf, buf_len, "[%s]", name);
1129 assert (n <= buf_len);
1136 /* remove trailing whitespace */
1137 while ((n > 0) && (isspace (buf[n]) || ('\0' == buf[n]))) {
1142 /* arguments are separated by '\0' in /proc/<pid>/cmdline */
1149 } /* char *ps_get_cmdline (...) */
1151 static int read_fork_rate ()
1156 _Bool value_valid = 0;
1158 proc_stat = fopen ("/proc/stat", "r");
1159 if (proc_stat == NULL)
1162 ERROR ("processes plugin: fopen (/proc/stat) failed: %s",
1163 sstrerror (errno, errbuf, sizeof (errbuf)));
1167 while (fgets (buffer, sizeof (buffer), proc_stat) != NULL)
1173 fields_num = strsplit (buffer, fields,
1174 STATIC_ARRAY_SIZE (fields));
1175 if (fields_num != 2)
1178 if (strcmp ("processes", fields[0]) != 0)
1181 status = parse_value (fields[1], &value, DS_TYPE_DERIVE);
1192 ps_submit_fork_rate (value.derive);
1195 #endif /*KERNEL_LINUX */
1198 static const char *ps_get_cmdline (pid_t pid, /* {{{ */
1199 char *buffer, size_t buffer_size)
1201 char path[PATH_MAX];
1205 snprintf(path, sizeof (path), "/proc/%i/psinfo", pid);
1207 status = read_file_contents (path, (void *) &info, sizeof (info));
1208 if (status != ((int) buffer_size))
1210 ERROR ("processes plugin: Unexpected return value "
1211 "while reading \"%s\": "
1212 "Returned %i but expected %zu.",
1213 path, status, buffer_size);
1217 info.pr_psargs[sizeof (info.pr_psargs) - 1] = 0;
1218 sstrncpy (buffer, info.pr_psargs, buffer_size);
1221 } /* }}} int ps_get_cmdline */
1224 * Reads process information on the Solaris OS. The information comes mainly from
1225 * /proc/PID/status, /proc/PID/psinfo and /proc/PID/usage
1226 * The values for input and ouput chars are calculated "by hand"
1227 * Added a few "solaris" specific process states as well
1229 static int ps_read_process(int pid, procstat_t *ps, char *state)
1232 char f_psinfo[64], f_usage[64];
1235 pstatus_t *myStatus;
1239 snprintf(filename, sizeof (filename), "/proc/%i/status", pid);
1240 snprintf(f_psinfo, sizeof (f_psinfo), "/proc/%i/psinfo", pid);
1241 snprintf(f_usage, sizeof (f_usage), "/proc/%i/usage", pid);
1244 buffer = malloc(sizeof (pstatus_t));
1245 memset(buffer, 0, sizeof (pstatus_t));
1246 read_file_contents(filename, buffer, sizeof (pstatus_t));
1247 myStatus = (pstatus_t *) buffer;
1249 buffer = malloc(sizeof (psinfo_t));
1250 memset(buffer, 0, sizeof(psinfo_t));
1251 read_file_contents(f_psinfo, buffer, sizeof (psinfo_t));
1252 myInfo = (psinfo_t *) buffer;
1254 buffer = malloc(sizeof (prusage_t));
1255 memset(buffer, 0, sizeof(prusage_t));
1256 read_file_contents(f_usage, buffer, sizeof (prusage_t));
1257 myUsage = (prusage_t *) buffer;
1259 sstrncpy(ps->name, myInfo->pr_fname, sizeof (myInfo->pr_fname));
1260 ps->num_lwp = myStatus->pr_nlwp;
1261 if (myInfo->pr_wstat != 0) {
1264 *state = (char) 'Z';
1268 ps->num_lwp = myInfo->pr_nlwp;
1272 * Convert system time and user time from nanoseconds to microseconds
1273 * for compatibility with the linux module
1275 ps->cpu_system_counter = myStatus -> pr_stime.tv_nsec / 1000;
1276 ps->cpu_user_counter = myStatus -> pr_utime.tv_nsec / 1000;
1279 * Convert rssize from KB to bytes to be consistent w/ the linux module
1281 ps->vmem_rss = myInfo->pr_rssize * 1024;
1282 ps->vmem_size = myInfo->pr_size * 1024;
1283 ps->vmem_minflt_counter = myUsage->pr_minf;
1284 ps->vmem_majflt_counter = myUsage->pr_majf;
1287 * TODO: Data and code segment calculations for Solaris
1292 ps->stack_size = myStatus->pr_stksize;
1295 * Calculating input/ouput chars
1296 * Formula used is total chars / total blocks => chars/block
1297 * then convert input/output blocks to chars
1299 ulong_t tot_chars = myUsage->pr_ioch;
1300 ulong_t tot_blocks = myUsage->pr_inblk + myUsage->pr_oublk;
1301 ulong_t chars_per_block = 1;
1302 if (tot_blocks != 0)
1303 chars_per_block = tot_chars / tot_blocks;
1304 ps->io_rchar = myUsage->pr_inblk * chars_per_block;
1305 ps->io_wchar = myUsage->pr_oublk * chars_per_block;
1306 ps->io_syscr = myUsage->pr_sysc;
1307 ps->io_syscw = myUsage->pr_sysc;
1311 * TODO: Find way of setting BLOCKED and PAGING status
1314 *state = (char) 'R';
1315 if (myStatus->pr_flags & PR_ASLEEP)
1316 *state = (char) 'S';
1317 else if (myStatus->pr_flags & PR_STOPPED)
1318 *state = (char) 'T';
1319 else if (myStatus->pr_flags & PR_DETACH)
1320 *state = (char) 'E';
1321 else if (myStatus->pr_flags & PR_DAEMON)
1322 *state = (char) 'A';
1323 else if (myStatus->pr_flags & PR_ISSYS)
1324 *state = (char) 'Y';
1325 else if (myStatus->pr_flags & PR_ORPHAN)
1326 *state = (char) 'O';
1336 * Reads the number of threads created since the last reboot. On Solaris these
1337 * are retrieved from kstat (module cpu, name sys, class misc, stat nthreads).
1338 * The result is the sum for all the threads created on each cpu
1340 static int read_fork_rate()
1342 extern kstat_ctl_t *kc;
1343 kstat_t *ksp_chain = NULL;
1344 derive_t result = 0;
1349 for (ksp_chain = kc->kc_chain;
1351 ksp_chain = ksp_chain->ks_next)
1353 if ((strcmp (ksp_chain->ks_module, "cpu") == 0)
1354 && (strcmp (ksp_chain->ks_name, "sys") == 0)
1355 && (strcmp (ksp_chain->ks_class, "misc") == 0))
1359 kstat_read (kc, ksp_chain, NULL);
1361 tmp = get_kstat_value(ksp_chain, "nthreads");
1367 ps_submit_fork_rate (result);
1370 #endif /* KERNEL_SOLARIS */
1372 #if HAVE_THREAD_INFO
1373 static int mach_get_task_name (task_t t, int *pid, char *name, size_t name_max_len)
1377 struct kinfo_proc kp;
1382 mib[2] = KERN_PROC_PID;
1384 if (pid_for_task (t, pid) != KERN_SUCCESS)
1388 kp_size = sizeof (kp);
1389 if (sysctl (mib, 4, &kp, &kp_size, NULL, 0) != 0)
1392 if (name_max_len > (MAXCOMLEN + 1))
1393 name_max_len = MAXCOMLEN + 1;
1395 strncpy (name, kp.kp_proc.p_comm, name_max_len - 1);
1396 name[name_max_len - 1] = '\0';
1398 DEBUG ("pid = %i; name = %s;", *pid, name);
1400 /* We don't do the special handling for `p_comm == "LaunchCFMApp"' as
1401 * `top' does it, because it is a lot of work and only used when
1402 * debugging. -octo */
1406 #endif /* HAVE_THREAD_INFO */
1407 /* ------- end of additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
1409 /* do actual readings from kernel */
1410 static int ps_read (void)
1412 #if HAVE_THREAD_INFO
1413 kern_return_t status;
1416 processor_set_t port_pset_priv;
1419 task_array_t task_list;
1420 mach_msg_type_number_t task_list_len;
1423 char task_name[MAXCOMLEN + 1];
1426 thread_act_array_t thread_list;
1427 mach_msg_type_number_t thread_list_len;
1428 thread_basic_info_data_t thread_data;
1429 mach_msg_type_number_t thread_data_len;
1438 procstat_entry_t pse;
1443 * The Mach-concept is a little different from the traditional UNIX
1444 * concept: All the work is done in threads. Threads are contained in
1445 * `tasks'. Therefore, `task status' doesn't make much sense, since
1446 * it's actually a `thread status'.
1447 * Tasks are assigned to sets of processors, so that's where you go to
1450 for (pset = 0; pset < pset_list_len; pset++)
1452 if ((status = host_processor_set_priv (port_host_self,
1454 &port_pset_priv)) != KERN_SUCCESS)
1456 ERROR ("host_processor_set_priv failed: %s\n",
1457 mach_error_string (status));
1461 if ((status = processor_set_tasks (port_pset_priv,
1463 &task_list_len)) != KERN_SUCCESS)
1465 ERROR ("processor_set_tasks failed: %s\n",
1466 mach_error_string (status));
1467 mach_port_deallocate (port_task_self, port_pset_priv);
1471 for (task = 0; task < task_list_len; task++)
1474 if (mach_get_task_name (task_list[task],
1476 task_name, PROCSTAT_NAME_LEN) == 0)
1478 /* search for at least one match */
1479 for (ps = list_head_g; ps != NULL; ps = ps->next)
1480 /* FIXME: cmdline should be here instead of NULL */
1481 if (ps_list_match (task_name, NULL, ps) == 1)
1485 /* Collect more detailed statistics for this process */
1488 task_basic_info_data_t task_basic_info;
1489 mach_msg_type_number_t task_basic_info_len;
1490 task_events_info_data_t task_events_info;
1491 mach_msg_type_number_t task_events_info_len;
1492 task_absolutetime_info_data_t task_absolutetime_info;
1493 mach_msg_type_number_t task_absolutetime_info_len;
1495 memset (&pse, '\0', sizeof (pse));
1498 task_basic_info_len = TASK_BASIC_INFO_COUNT;
1499 status = task_info (task_list[task],
1501 (task_info_t) &task_basic_info,
1502 &task_basic_info_len);
1503 if (status != KERN_SUCCESS)
1505 ERROR ("task_info failed: %s",
1506 mach_error_string (status));
1507 continue; /* with next thread_list */
1510 task_events_info_len = TASK_EVENTS_INFO_COUNT;
1511 status = task_info (task_list[task],
1513 (task_info_t) &task_events_info,
1514 &task_events_info_len);
1515 if (status != KERN_SUCCESS)
1517 ERROR ("task_info failed: %s",
1518 mach_error_string (status));
1519 continue; /* with next thread_list */
1522 task_absolutetime_info_len = TASK_ABSOLUTETIME_INFO_COUNT;
1523 status = task_info (task_list[task],
1524 TASK_ABSOLUTETIME_INFO,
1525 (task_info_t) &task_absolutetime_info,
1526 &task_absolutetime_info_len);
1527 if (status != KERN_SUCCESS)
1529 ERROR ("task_info failed: %s",
1530 mach_error_string (status));
1531 continue; /* with next thread_list */
1535 pse.vmem_size = task_basic_info.virtual_size;
1536 pse.vmem_rss = task_basic_info.resident_size;
1537 /* Does not seem to be easily exposed */
1541 pse.vmem_minflt_counter = task_events_info.cow_faults;
1542 pse.vmem_majflt_counter = task_events_info.faults;
1544 pse.cpu_user_counter = task_absolutetime_info.total_user;
1545 pse.cpu_system_counter = task_absolutetime_info.total_system;
1548 status = task_threads (task_list[task], &thread_list,
1550 if (status != KERN_SUCCESS)
1552 /* Apple's `top' treats this case a zombie. It
1553 * makes sense to some extend: A `zombie'
1554 * thread is nonsense, since the task/process
1557 DEBUG ("task_threads failed: %s",
1558 mach_error_string (status));
1559 if (task_list[task] != port_task_self)
1560 mach_port_deallocate (port_task_self,
1562 continue; /* with next task_list */
1565 for (thread = 0; thread < thread_list_len; thread++)
1567 thread_data_len = THREAD_BASIC_INFO_COUNT;
1568 status = thread_info (thread_list[thread],
1570 (thread_info_t) &thread_data,
1572 if (status != KERN_SUCCESS)
1574 ERROR ("thread_info failed: %s",
1575 mach_error_string (status));
1576 if (task_list[task] != port_task_self)
1577 mach_port_deallocate (port_task_self,
1578 thread_list[thread]);
1579 continue; /* with next thread_list */
1585 switch (thread_data.run_state)
1587 case TH_STATE_RUNNING:
1590 case TH_STATE_STOPPED:
1591 /* What exactly is `halted'? */
1592 case TH_STATE_HALTED:
1595 case TH_STATE_WAITING:
1598 case TH_STATE_UNINTERRUPTIBLE:
1601 /* There is no `zombie' case here,
1602 * since there are no zombie-threads.
1603 * There's only zombie tasks, which are
1606 WARNING ("Unknown thread status: %i",
1607 thread_data.run_state);
1609 } /* switch (thread_data.run_state) */
1611 if (task_list[task] != port_task_self)
1613 status = mach_port_deallocate (port_task_self,
1614 thread_list[thread]);
1615 if (status != KERN_SUCCESS)
1616 ERROR ("mach_port_deallocate failed: %s",
1617 mach_error_string (status));
1619 } /* for (thread_list) */
1621 if ((status = vm_deallocate (port_task_self,
1622 (vm_address_t) thread_list,
1623 thread_list_len * sizeof (thread_act_t)))
1626 ERROR ("vm_deallocate failed: %s",
1627 mach_error_string (status));
1630 thread_list_len = 0;
1632 /* Only deallocate the task port, if it isn't our own.
1633 * Don't know what would happen in that case, but this
1634 * is what Apple's top does.. ;) */
1635 if (task_list[task] != port_task_self)
1637 status = mach_port_deallocate (port_task_self,
1639 if (status != KERN_SUCCESS)
1640 ERROR ("mach_port_deallocate failed: %s",
1641 mach_error_string (status));
1645 /* FIXME: cmdline should be here instead of NULL */
1646 ps_list_add (task_name, NULL, &pse);
1647 } /* for (task_list) */
1649 if ((status = vm_deallocate (port_task_self,
1650 (vm_address_t) task_list,
1651 task_list_len * sizeof (task_t))) != KERN_SUCCESS)
1653 ERROR ("vm_deallocate failed: %s",
1654 mach_error_string (status));
1659 if ((status = mach_port_deallocate (port_task_self, port_pset_priv))
1662 ERROR ("mach_port_deallocate failed: %s",
1663 mach_error_string (status));
1665 } /* for (pset_list) */
1667 ps_submit_state ("running", running);
1668 ps_submit_state ("sleeping", sleeping);
1669 ps_submit_state ("zombies", zombies);
1670 ps_submit_state ("stopped", stopped);
1671 ps_submit_state ("blocked", blocked);
1673 for (ps = list_head_g; ps != NULL; ps = ps->next)
1674 ps_submit_proc_list (ps);
1675 /* #endif HAVE_THREAD_INFO */
1689 char cmdline[ARG_MAX];
1693 procstat_entry_t pse;
1698 running = sleeping = zombies = stopped = paging = blocked = 0;
1701 if ((proc = opendir ("/proc")) == NULL)
1704 ERROR ("Cannot open `/proc': %s",
1705 sstrerror (errno, errbuf, sizeof (errbuf)));
1709 while ((ent = readdir (proc)) != NULL)
1711 if (!isdigit (ent->d_name[0]))
1714 if ((pid = atoi (ent->d_name)) < 1)
1717 status = ps_read_process (pid, &ps, &state);
1720 DEBUG ("ps_read_process failed: %i", status);
1727 pse.num_proc = ps.num_proc;
1728 pse.num_lwp = ps.num_lwp;
1729 pse.vmem_size = ps.vmem_size;
1730 pse.vmem_rss = ps.vmem_rss;
1731 pse.vmem_data = ps.vmem_data;
1732 pse.vmem_code = ps.vmem_code;
1733 pse.stack_size = ps.stack_size;
1735 pse.vmem_minflt = 0;
1736 pse.vmem_minflt_counter = ps.vmem_minflt_counter;
1737 pse.vmem_majflt = 0;
1738 pse.vmem_majflt_counter = ps.vmem_majflt_counter;
1741 pse.cpu_user_counter = ps.cpu_user_counter;
1743 pse.cpu_system_counter = ps.cpu_system_counter;
1745 pse.io_rchar = ps.io_rchar;
1746 pse.io_wchar = ps.io_wchar;
1747 pse.io_syscr = ps.io_syscr;
1748 pse.io_syscw = ps.io_syscw;
1752 case 'R': running++; break;
1753 case 'S': sleeping++; break;
1754 case 'D': blocked++; break;
1755 case 'Z': zombies++; break;
1756 case 'T': stopped++; break;
1757 case 'W': paging++; break;
1760 ps_list_add (ps.name,
1761 ps_get_cmdline (pid, ps.name, cmdline, sizeof (cmdline)),
1767 ps_submit_state ("running", running);
1768 ps_submit_state ("sleeping", sleeping);
1769 ps_submit_state ("zombies", zombies);
1770 ps_submit_state ("stopped", stopped);
1771 ps_submit_state ("paging", paging);
1772 ps_submit_state ("blocked", blocked);
1774 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1775 ps_submit_proc_list (ps_ptr);
1778 /* #endif KERNEL_LINUX */
1780 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD
1791 struct kinfo_proc *procs; /* array of processes */
1792 struct kinfo_proc *proc_ptr = NULL;
1793 int count; /* returns number of processes */
1797 procstat_entry_t pse;
1801 /* Open the kvm interface, get a descriptor */
1802 kd = kvm_open (NULL, NULL, NULL, 0, errbuf);
1805 ERROR ("processes plugin: Cannot open kvm interface: %s",
1810 /* Get the list of processes. */
1811 procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, &count);
1814 ERROR ("processes plugin: Cannot get kvm processes list: %s",
1820 /* Iterate through the processes in kinfo_proc */
1821 for (i = 0; i < count; i++)
1823 /* Create only one process list entry per _process_, i.e.
1824 * filter out threads (duplicate PID entries). */
1825 if ((proc_ptr == NULL) || (proc_ptr->ki_pid != procs[i].ki_pid))
1827 char cmdline[ARG_MAX] = "";
1828 _Bool have_cmdline = 0;
1830 proc_ptr = &(procs[i]);
1831 /* Don't probe system processes and processes without arguments */
1832 if (((procs[i].ki_flag & P_SYSTEM) == 0)
1833 && (procs[i].ki_args != NULL))
1839 /* retrieve the arguments */
1840 argv = kvm_getargv (kd, proc_ptr, /* nchr = */ 0);
1842 if ((argv != NULL) && (argv[0] != NULL))
1844 while (argv[argc] != NULL)
1847 status = strjoin (cmdline, sizeof (cmdline), argv, argc, " ");
1849 WARNING ("processes plugin: Command line did not fit into buffer.");
1853 } /* if (process has argument list) */
1855 pse.id = procs[i].ki_pid;
1859 pse.num_lwp = procs[i].ki_numthreads;
1861 pse.vmem_size = procs[i].ki_size;
1862 pse.vmem_rss = procs[i].ki_rssize * pagesize;
1863 pse.vmem_data = procs[i].ki_dsize * pagesize;
1864 pse.vmem_code = procs[i].ki_tsize * pagesize;
1865 pse.stack_size = procs[i].ki_ssize * pagesize;
1866 pse.vmem_minflt = 0;
1867 pse.vmem_minflt_counter = procs[i].ki_rusage.ru_minflt;
1868 pse.vmem_majflt = 0;
1869 pse.vmem_majflt_counter = procs[i].ki_rusage.ru_majflt;
1873 pse.cpu_user_counter = 0;
1874 pse.cpu_system_counter = 0;
1876 * The u-area might be swapped out, and we can't get
1877 * at it because we have a crashdump and no swap.
1878 * If it's here fill in these fields, otherwise, just
1881 if (procs[i].ki_flag & P_INMEM)
1883 pse.cpu_user_counter = procs[i].ki_rusage.ru_utime.tv_usec
1884 + (1000000lu * procs[i].ki_rusage.ru_utime.tv_sec);
1885 pse.cpu_system_counter = procs[i].ki_rusage.ru_stime.tv_usec
1886 + (1000000lu * procs[i].ki_rusage.ru_stime.tv_sec);
1895 ps_list_add (procs[i].ki_comm, have_cmdline ? cmdline : NULL, &pse);
1896 } /* if ((proc_ptr == NULL) || (proc_ptr->ki_pid != procs[i].ki_pid)) */
1898 switch (procs[i].ki_stat)
1900 case SSTOP: stopped++; break;
1901 case SSLEEP: sleeping++; break;
1902 case SRUN: running++; break;
1903 case SIDL: idle++; break;
1904 case SWAIT: wait++; break;
1905 case SLOCK: blocked++; break;
1906 case SZOMB: zombies++; break;
1912 ps_submit_state ("running", running);
1913 ps_submit_state ("sleeping", sleeping);
1914 ps_submit_state ("zombies", zombies);
1915 ps_submit_state ("stopped", stopped);
1916 ps_submit_state ("blocked", blocked);
1917 ps_submit_state ("idle", idle);
1918 ps_submit_state ("wait", wait);
1920 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1921 ps_submit_proc_list (ps_ptr);
1922 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
1924 #elif HAVE_PROCINFO_H
1937 procstat_entry_t pse;
1940 while ((nprocs = getprocs64 (procentry, sizeof(struct procentry64),
1941 /* fdsinfo = */ NULL, sizeof(struct fdsinfo64),
1942 &pindex, MAXPROCENTRY)) > 0)
1946 for (i = 0; i < nprocs; i++)
1950 char arglist[MAXARGLN+1];
1954 if (procentry[i].pi_state == SNONE) continue;
1955 /* if (procentry[i].pi_state == SZOMB) FIXME */
1957 cmdline = procentry[i].pi_comm;
1958 cargs = procentry[i].pi_comm;
1959 if ( procentry[i].pi_flags & SKPROC )
1961 if (procentry[i].pi_pid == 0)
1962 cmdline = "swapper";
1967 if (getargs(&procentry[i], sizeof(struct procentry64), arglist, MAXARGLN) >= 0)
1972 while (++n < MAXARGLN)
1974 if (arglist[n] == '\0')
1976 if (arglist[n+1] == '\0')
1985 pse.id = procentry[i].pi_pid;
1987 pse.num_lwp = procentry[i].pi_thcount;
1991 while ((nthreads = getthrds64(procentry[i].pi_pid,
1992 thrdentry, sizeof(struct thrdentry64),
1993 &thindex, MAXTHRDENTRY)) > 0)
1997 for (j=0; j< nthreads; j++)
1999 switch (thrdentry[j].ti_state)
2001 /* case TSNONE: break; */
2002 case TSIDL: blocked++; break; /* FIXME is really blocked */
2003 case TSRUN: running++; break;
2004 case TSSLEEP: sleeping++; break;
2005 case TSSWAP: paging++; break;
2006 case TSSTOP: stopped++; break;
2007 case TSZOMB: zombies++; break;
2010 if (nthreads < MAXTHRDENTRY)
2015 /* tv_usec is nanosec ??? */
2016 pse.cpu_user_counter = procentry[i].pi_ru.ru_utime.tv_sec * 1000000 +
2017 procentry[i].pi_ru.ru_utime.tv_usec / 1000;
2020 /* tv_usec is nanosec ??? */
2021 pse.cpu_system_counter = procentry[i].pi_ru.ru_stime.tv_sec * 1000000 +
2022 procentry[i].pi_ru.ru_stime.tv_usec / 1000;
2024 pse.vmem_minflt = 0;
2025 pse.vmem_minflt_counter = procentry[i].pi_minflt;
2026 pse.vmem_majflt = 0;
2027 pse.vmem_majflt_counter = procentry[i].pi_majflt;
2029 pse.vmem_size = procentry[i].pi_tsize + procentry[i].pi_dvm * pagesize;
2030 pse.vmem_rss = (procentry[i].pi_drss + procentry[i].pi_trss) * pagesize;
2041 ps_list_add (cmdline, cargs, &pse);
2042 } /* for (i = 0 .. nprocs) */
2044 if (nprocs < MAXPROCENTRY)
2046 } /* while (getprocs64() > 0) */
2047 ps_submit_state ("running", running);
2048 ps_submit_state ("sleeping", sleeping);
2049 ps_submit_state ("zombies", zombies);
2050 ps_submit_state ("stopped", stopped);
2051 ps_submit_state ("paging", paging);
2052 ps_submit_state ("blocked", blocked);
2054 for (ps = list_head_g; ps != NULL; ps = ps->next)
2055 ps_submit_proc_list (ps);
2056 /* #endif HAVE_PROCINFO_H */
2058 #elif KERNEL_SOLARIS
2060 * The Solaris section adds a few more process states and removes some
2061 * process states compared to linux. Most notably there is no "PAGING"
2062 * and "BLOCKED" state for a process. The rest is similar to the linux
2081 char cmdline[PRARGSZ];
2085 proc = opendir ("/proc");
2089 while ((ent = readdir(proc)) != NULL)
2093 procstat_entry_t pse;
2095 if (!isdigit ((int) ent->d_name[0]))
2098 if ((pid = atoi (ent->d_name)) < 1)
2101 status = ps_read_process (pid, &ps, &state);
2104 DEBUG("ps_read_process failed: %i", status);
2111 pse.num_proc = ps.num_proc;
2112 pse.num_lwp = ps.num_lwp;
2113 pse.vmem_size = ps.vmem_size;
2114 pse.vmem_rss = ps.vmem_rss;
2115 pse.vmem_data = ps.vmem_data;
2116 pse.vmem_code = ps.vmem_code;
2117 pse.stack_size = ps.stack_size;
2119 pse.vmem_minflt = 0;
2120 pse.vmem_minflt_counter = ps.vmem_minflt_counter;
2121 pse.vmem_majflt = 0;
2122 pse.vmem_majflt_counter = ps.vmem_majflt_counter;
2125 pse.cpu_user_counter = ps.cpu_user_counter;
2127 pse.cpu_system_counter = ps.cpu_system_counter;
2129 pse.io_rchar = ps.io_rchar;
2130 pse.io_wchar = ps.io_wchar;
2131 pse.io_syscr = ps.io_syscr;
2132 pse.io_syscw = ps.io_syscw;
2136 case 'R': running++; break;
2137 case 'S': sleeping++; break;
2138 case 'E': detached++; break;
2139 case 'Z': zombies++; break;
2140 case 'T': stopped++; break;
2141 case 'A': daemon++; break;
2142 case 'Y': system++; break;
2143 case 'O': orphan++; break;
2147 ps_list_add (ps.name,
2148 ps_get_cmdline ((pid_t) pid,
2149 cmdline, sizeof (cmdline)),
2151 } /* while(readdir) */
2154 ps_submit_state ("running", running);
2155 ps_submit_state ("sleeping", sleeping);
2156 ps_submit_state ("zombies", zombies);
2157 ps_submit_state ("stopped", stopped);
2158 ps_submit_state ("detached", detached);
2159 ps_submit_state ("daemon", daemon);
2160 ps_submit_state ("system", system);
2161 ps_submit_state ("orphan", orphan);
2163 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
2164 ps_submit_proc_list (ps_ptr);
2167 #endif /* KERNEL_SOLARIS */
2172 void module_register (void)
2174 plugin_register_complex_config ("processes", ps_config);
2175 plugin_register_init ("processes", ps_init);
2176 plugin_register_read ("processes", ps_read);
2177 } /* void module_register */