2 * collectd - src/processes.c
3 * Copyright (C) 2005 Lyonel Vincent
4 * Copyright (C) 2006-2008 Florian Forster (Mach code)
5 * Copyright (C) 2008 Oleg King
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 * Lyonel Vincent <lyonel at ezix.org>
23 * Florian octo Forster <octo at verplant.org>
24 * Oleg King <king2 at kaluga.ru>
25 * Sebastian Harl <sh at tokkee.org>
31 #include "configfile.h"
33 /* Include header files for the mach system, if they exist.. */
35 # if HAVE_MACH_MACH_INIT_H
36 # include <mach/mach_init.h>
38 # if HAVE_MACH_HOST_PRIV_H
39 # include <mach/host_priv.h>
41 # if HAVE_MACH_MACH_ERROR_H
42 # include <mach/mach_error.h>
44 # if HAVE_MACH_MACH_HOST_H
45 # include <mach/mach_host.h>
47 # if HAVE_MACH_MACH_PORT_H
48 # include <mach/mach_port.h>
50 # if HAVE_MACH_MACH_TYPES_H
51 # include <mach/mach_types.h>
53 # if HAVE_MACH_MESSAGE_H
54 # include <mach/message.h>
56 # if HAVE_MACH_PROCESSOR_SET_H
57 # include <mach/processor_set.h>
60 # include <mach/task.h>
62 # if HAVE_MACH_THREAD_ACT_H
63 # include <mach/thread_act.h>
65 # if HAVE_MACH_VM_REGION_H
66 # include <mach/vm_region.h>
68 # if HAVE_MACH_VM_MAP_H
69 # include <mach/vm_map.h>
71 # if HAVE_MACH_VM_PROT_H
72 # include <mach/vm_prot.h>
74 # if HAVE_SYS_SYSCTL_H
75 # include <sys/sysctl.h>
77 /* #endif HAVE_THREAD_INFO */
80 # if HAVE_LINUX_CONFIG_H
81 # include <linux/config.h>
84 # define CONFIG_HZ 100
86 /* #endif KERNEL_LINUX */
88 #elif HAVE_LIBKVM_GETPROCS
90 # include <sys/user.h>
91 # include <sys/proc.h>
92 # if HAVE_SYS_SYSCTL_H
93 # include <sys/sysctl.h>
95 /* #endif HAVE_LIBKVM_GETPROCS */
98 # error "No applicable input method."
106 # define ARG_MAX 4096
111 static const char *config_keys[] =
116 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
118 typedef struct procstat_entry_s
123 unsigned long num_proc;
124 unsigned long num_lwp;
125 unsigned long vmem_size;
126 unsigned long vmem_rss;
127 unsigned long stack_size;
129 unsigned long vmem_minflt;
130 unsigned long vmem_majflt;
131 unsigned long vmem_minflt_counter;
132 unsigned long vmem_majflt_counter;
134 unsigned long cpu_user;
135 unsigned long cpu_system;
136 unsigned long cpu_user_counter;
137 unsigned long cpu_system_counter;
139 struct procstat_entry_s *next;
142 #define PROCSTAT_NAME_LEN 256
143 typedef struct procstat
145 char name[PROCSTAT_NAME_LEN];
150 unsigned long num_proc;
151 unsigned long num_lwp;
152 unsigned long vmem_size;
153 unsigned long vmem_rss;
154 unsigned long stack_size;
156 unsigned long vmem_minflt_counter;
157 unsigned long vmem_majflt_counter;
159 unsigned long cpu_user_counter;
160 unsigned long cpu_system_counter;
162 struct procstat *next;
163 struct procstat_entry_s *instances;
166 static procstat_t *list_head_g = NULL;
169 static mach_port_t port_host_self;
170 static mach_port_t port_task_self;
172 static processor_set_name_array_t pset_list;
173 static mach_msg_type_number_t pset_list_len;
174 /* #endif HAVE_THREAD_INFO */
177 static long pagesize_g;
178 /* #endif KERNEL_LINUX */
180 #elif HAVE_LIBKVM_GETPROCS
181 /* no global variables */
182 #endif /* HAVE_LIBKVM_GETPROCS */
184 /* put name of process from config to list_head_g tree
185 list_head_g is a list of 'procstat_t' structs with
186 processes names we want to watch */
187 static void ps_list_register (const char *name, const char *regexp)
193 new = (procstat_t *) malloc (sizeof (procstat_t));
196 ERROR ("processes plugin: ps_list_register: malloc failed.");
199 memset (new, 0, sizeof (procstat_t));
200 sstrncpy (new->name, name, sizeof (new->name));
205 DEBUG ("ProcessMatch: adding \"%s\" as criteria to process %s.", regexp, name);
206 new->re = (regex_t *) malloc (sizeof (regex_t));
209 ERROR ("processes plugin: ps_list_register: malloc failed.");
214 status = regcomp (new->re, regexp, REG_EXTENDED | REG_NOSUB);
217 DEBUG ("ProcessMatch: compiling the regular expression \"%s\" failed.", regexp);
225 ERROR ("processes plugin: ps_list_register: "
226 "Regular expression \"%s\" found in config "
227 "file, but support for regular expressions "
228 "has been dispabled at compile time.",
235 for (ptr = list_head_g; ptr != NULL; ptr = ptr->next)
237 if (strcmp (ptr->name, name) == 0)
239 WARNING ("processes plugin: You have configured more "
240 "than one `Process' or "
241 "`ProcessMatch' with the same name. "
242 "All but the first setting will be "
249 if (ptr->next == NULL)
257 } /* void ps_list_register */
259 /* try to match name against entry, returns 1 if success */
260 static int ps_list_match (const char *name, const char *cmdline, procstat_t *ps)
269 if ((str == NULL) || (str[0] == 0))
272 assert (str != NULL);
274 status = regexec (ps->re, str,
283 if (strcmp (ps->name, name) == 0)
287 } /* int ps_list_match */
289 /* add process entry to 'instances' of process 'name' (or refresh it) */
290 static void ps_list_add (const char *name, const char *cmdline, procstat_entry_t *entry)
293 procstat_entry_t *pse;
298 for (ps = list_head_g; ps != NULL; ps = ps->next)
300 if ((ps_list_match (name, cmdline, ps)) == 0)
303 for (pse = ps->instances; pse != NULL; pse = pse->next)
304 if ((pse->id == entry->id) || (pse->next == NULL))
307 if ((pse == NULL) || (pse->id != entry->id))
309 procstat_entry_t *new;
311 new = (procstat_entry_t *) malloc (sizeof (procstat_entry_t));
314 memset (new, 0, sizeof (procstat_entry_t));
326 pse->num_proc = entry->num_proc;
327 pse->num_lwp = entry->num_lwp;
328 pse->vmem_size = entry->vmem_size;
329 pse->vmem_rss = entry->vmem_rss;
330 pse->stack_size = entry->stack_size;
332 ps->num_proc += pse->num_proc;
333 ps->num_lwp += pse->num_lwp;
334 ps->vmem_size += pse->vmem_size;
335 ps->vmem_rss += pse->vmem_rss;
336 ps->stack_size += pse->stack_size;
338 if ((entry->vmem_minflt_counter == 0)
339 && (entry->vmem_majflt_counter == 0))
341 pse->vmem_minflt_counter += entry->vmem_minflt;
342 pse->vmem_minflt = entry->vmem_minflt;
344 pse->vmem_majflt_counter += entry->vmem_majflt;
345 pse->vmem_majflt = entry->vmem_majflt;
349 if (entry->vmem_minflt_counter < pse->vmem_minflt_counter)
351 pse->vmem_minflt = entry->vmem_minflt_counter
352 + (ULONG_MAX - pse->vmem_minflt_counter);
356 pse->vmem_minflt = entry->vmem_minflt_counter - pse->vmem_minflt_counter;
358 pse->vmem_minflt_counter = entry->vmem_minflt_counter;
360 if (entry->vmem_majflt_counter < pse->vmem_majflt_counter)
362 pse->vmem_majflt = entry->vmem_majflt_counter
363 + (ULONG_MAX - pse->vmem_majflt_counter);
367 pse->vmem_majflt = entry->vmem_majflt_counter - pse->vmem_majflt_counter;
369 pse->vmem_majflt_counter = entry->vmem_majflt_counter;
372 ps->vmem_minflt_counter += pse->vmem_minflt;
373 ps->vmem_majflt_counter += pse->vmem_majflt;
375 if ((entry->cpu_user_counter == 0)
376 && (entry->cpu_system_counter == 0))
378 pse->cpu_user_counter += entry->cpu_user;
379 pse->cpu_user = entry->cpu_user;
381 pse->cpu_system_counter += entry->cpu_system;
382 pse->cpu_system = entry->cpu_system;
386 if (entry->cpu_user_counter < pse->cpu_user_counter)
388 pse->cpu_user = entry->cpu_user_counter
389 + (ULONG_MAX - pse->cpu_user_counter);
393 pse->cpu_user = entry->cpu_user_counter - pse->cpu_user_counter;
395 pse->cpu_user_counter = entry->cpu_user_counter;
397 if (entry->cpu_system_counter < pse->cpu_system_counter)
399 pse->cpu_system = entry->cpu_system_counter
400 + (ULONG_MAX - pse->cpu_system_counter);
404 pse->cpu_system = entry->cpu_system_counter - pse->cpu_system_counter;
406 pse->cpu_system_counter = entry->cpu_system_counter;
409 ps->cpu_user_counter += pse->cpu_user;
410 ps->cpu_system_counter += pse->cpu_system;
414 /* remove old entries from instances of processes in list_head_g */
415 static void ps_list_reset (void)
418 procstat_entry_t *pse;
419 procstat_entry_t *pse_prev;
421 for (ps = list_head_g; ps != NULL; ps = ps->next)
435 DEBUG ("Removing this procstat entry cause it's too old: "
436 "id = %lu; name = %s;",
439 if (pse_prev == NULL)
441 ps->instances = pse->next;
447 pse_prev->next = pse->next;
449 pse = pse_prev->next;
458 } /* while (pse != NULL) */
459 } /* for (ps = list_head_g; ps != NULL; ps = ps->next) */
462 /* put all pre-defined 'Process' names from config to list_head_g tree */
463 static int ps_config (const char *key, const char *value)
465 if (strcasecmp (key, "Process") == 0)
467 ps_list_register (value, NULL);
469 else if (strcasecmp (key, "ProcessMatch") == 0)
475 new_val = strdup (value);
476 if (new_val == NULL) {
477 ERROR ("processes plugin: strdup failed when processing "
478 "`ProcessMatch %s'.", value);
482 fields_num = strsplit (new_val, fields,
483 STATIC_ARRAY_SIZE (fields));
486 ERROR ("processes plugin: `ProcessMatch' needs exactly "
487 "two string arguments.");
491 ps_list_register (fields[0], fields[1]);
496 ERROR ("processes plugin: The `%s' configuration option is not "
497 "understood and will be ignored.", key);
504 static int ps_init (void)
507 kern_return_t status;
509 port_host_self = mach_host_self ();
510 port_task_self = mach_task_self ();
512 if (pset_list != NULL)
514 vm_deallocate (port_task_self,
515 (vm_address_t) pset_list,
516 pset_list_len * sizeof (processor_set_t));
521 if ((status = host_processor_sets (port_host_self,
523 &pset_list_len)) != KERN_SUCCESS)
525 ERROR ("host_processor_sets failed: %s\n",
526 mach_error_string (status));
531 /* #endif HAVE_THREAD_INFO */
534 pagesize_g = sysconf(_SC_PAGESIZE);
535 DEBUG ("pagesize_g = %li; CONFIG_HZ = %i;",
536 pagesize_g, CONFIG_HZ);
537 /* #endif KERNEL_LINUX */
539 #elif HAVE_LIBKVM_GETPROCS
540 /* no initialization */
541 #endif /* HAVE_LIBKVM_GETPROCS */
546 /* submit global state (e.g.: qty of zombies, running, etc..) */
547 static void ps_submit_state (const char *state, double value)
550 value_list_t vl = VALUE_LIST_INIT;
552 values[0].gauge = value;
556 vl.time = time (NULL);
557 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
558 sstrncpy (vl.plugin, "processes", sizeof (vl.plugin));
559 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
560 sstrncpy (vl.type, "ps_state", sizeof (vl.type));
561 sstrncpy (vl.type_instance, state, sizeof (vl.type_instance));
563 plugin_dispatch_values (&vl);
566 /* submit info about specific process (e.g.: memory taken, cpu usage, etc..) */
567 static void ps_submit_proc_list (procstat_t *ps)
570 value_list_t vl = VALUE_LIST_INIT;
574 vl.time = time (NULL);
575 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
576 sstrncpy (vl.plugin, "processes", sizeof (vl.plugin));
577 sstrncpy (vl.plugin_instance, ps->name, sizeof (vl.plugin_instance));
579 sstrncpy (vl.type, "ps_vm", sizeof (vl.type));
580 vl.values[0].gauge = ps->vmem_size;
582 plugin_dispatch_values (&vl);
584 sstrncpy (vl.type, "ps_rss", sizeof (vl.type));
585 vl.values[0].gauge = ps->vmem_rss;
587 plugin_dispatch_values (&vl);
589 sstrncpy (vl.type, "ps_stacksize", sizeof (vl.type));
590 vl.values[0].gauge = ps->stack_size;
592 plugin_dispatch_values (&vl);
594 sstrncpy (vl.type, "ps_cputime", sizeof (vl.type));
595 vl.values[0].counter = ps->cpu_user_counter;
596 vl.values[1].counter = ps->cpu_system_counter;
598 plugin_dispatch_values (&vl);
600 sstrncpy (vl.type, "ps_count", sizeof (vl.type));
601 vl.values[0].gauge = ps->num_proc;
602 vl.values[1].gauge = ps->num_lwp;
604 plugin_dispatch_values (&vl);
606 sstrncpy (vl.type, "ps_pagefaults", sizeof (vl.type));
607 vl.values[0].counter = ps->vmem_minflt_counter;
608 vl.values[1].counter = ps->vmem_majflt_counter;
610 plugin_dispatch_values (&vl);
612 DEBUG ("name = %s; num_proc = %lu; num_lwp = %lu; vmem_rss = %lu; "
613 "vmem_minflt_counter = %lu; vmem_majflt_counter = %lu; "
614 "cpu_user_counter = %lu; cpu_system_counter = %lu;",
615 ps->name, ps->num_proc, ps->num_lwp, ps->vmem_rss,
616 ps->vmem_minflt_counter, ps->vmem_majflt_counter,
617 ps->cpu_user_counter, ps->cpu_system_counter);
618 } /* void ps_submit_proc_list */
620 /* ------- additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
622 static int *ps_read_tasks (int pid)
625 int list_size = 1; /* size of allocated space, in elements */
626 int list_len = 0; /* number of currently used elements */
632 ssnprintf (dirname, sizeof (dirname), "/proc/%i/task", pid);
634 if ((dh = opendir (dirname)) == NULL)
636 DEBUG ("Failed to open directory `%s'", dirname);
640 while ((ent = readdir (dh)) != NULL)
642 if (!isdigit (ent->d_name[0]))
645 if ((list_len + 1) >= list_size)
648 int new_size = 2 * list_size;
649 /* Comes in sizes: 2, 4, 8, 16, ... */
651 new_ptr = (int *) realloc (list, (size_t) (sizeof (int) * new_size));
656 ERROR ("processes plugin: "
657 "Failed to allocate more memory.");
662 list_size = new_size;
664 memset (list + list_len, 0, sizeof (int) * (list_size - list_len));
667 list[list_len] = atoi (ent->d_name);
668 if (list[list_len] != 0)
677 assert (list_len < list_size);
678 assert (list[list_len] == 0);
681 } /* int *ps_read_tasks */
683 int ps_read_process (int pid, procstat_t *ps, char *state)
697 long long unsigned cpu_user_counter;
698 long long unsigned cpu_system_counter;
699 long long unsigned vmem_size;
700 long long unsigned vmem_rss;
701 long long unsigned stack_size;
703 memset (ps, 0, sizeof (procstat_t));
705 ssnprintf (filename, sizeof (filename), "/proc/%i/stat", pid);
707 i = read_file_contents (filename, buffer, sizeof(buffer) - 1);
712 fields_len = strsplit (buffer, fields, 64);
715 DEBUG ("processes plugin: ps_read_process (pid = %i):"
716 " `%s' has only %i fields..",
717 (int) pid, filename, fields_len);
721 /* copy the name, strip brackets in the process */
722 name_len = strlen (fields[1]) - 2;
723 if ((fields[1][0] != '(') || (fields[1][name_len + 1] != ')'))
725 DEBUG ("No brackets found in process name: `%s'", fields[1]);
728 fields[1] = fields[1] + 1;
729 fields[1][name_len] = '\0';
730 strncpy (ps->name, fields[1], PROCSTAT_NAME_LEN);
732 ppid = atoi (fields[3]);
734 *state = fields[2][0];
741 else if ((tasks = ps_read_tasks (pid)) == NULL)
743 /* Kernel 2.4 or so */
751 for (i = 0; tasks[i] != 0; i++)
758 /* Leave the rest at zero if this is only a zombi */
759 if (ps->num_proc == 0)
761 DEBUG ("processes plugin: This is only a zombi: pid = %i; "
762 "name = %s;", pid, ps->name);
766 cpu_user_counter = atoll (fields[13]);
767 cpu_system_counter = atoll (fields[14]);
768 vmem_size = atoll (fields[22]);
769 vmem_rss = atoll (fields[23]);
770 ps->vmem_minflt_counter = atol (fields[9]);
771 ps->vmem_majflt_counter = atol (fields[11]);
774 unsigned long long stack_start = atoll (fields[27]);
775 unsigned long long stack_ptr = atoll (fields[28]);
777 stack_size = (stack_start > stack_ptr)
778 ? stack_start - stack_ptr
779 : stack_ptr - stack_start;
782 /* Convert jiffies to useconds */
783 cpu_user_counter = cpu_user_counter * 1000000 / CONFIG_HZ;
784 cpu_system_counter = cpu_system_counter * 1000000 / CONFIG_HZ;
785 vmem_rss = vmem_rss * pagesize_g;
787 ps->cpu_user_counter = (unsigned long) cpu_user_counter;
788 ps->cpu_system_counter = (unsigned long) cpu_system_counter;
789 ps->vmem_size = (unsigned long) vmem_size;
790 ps->vmem_rss = (unsigned long) vmem_rss;
791 ps->stack_size = (unsigned long) stack_size;
795 } /* int ps_read_process (...) */
797 static char *ps_get_cmdline (pid_t pid, char *name, char *buf, size_t buf_len)
807 if ((pid < 1) || (NULL == buf) || (buf_len < 2))
810 ssnprintf (file, sizeof (file), "/proc/%u/cmdline", pid);
812 fd = open (file, O_RDONLY);
815 WARNING ("processes plugin: Failed to open `%s': %s.", file,
816 sstrerror (errno, errbuf, sizeof (errbuf)));
828 status = read (fd, (void *)buf_ptr, len);
833 if ((EAGAIN == errno) || (EINTR == errno))
836 WARNING ("processes plugin: Failed to read from `%s': %s.", file,
837 sstrerror (errno, errbuf, sizeof (errbuf)));
857 /* cmdline not available; e.g. kernel thread, zombie */
861 ssnprintf (buf, buf_len, "[%s]", name);
865 assert (n <= buf_len);
872 /* remove trailing whitespace */
873 while ((n > 0) && (isspace (buf[n]) || ('\0' == buf[n]))) {
878 /* arguments are separated by '\0' in /proc/<pid>/cmdline */
885 } /* char *ps_get_cmdline (...) */
886 #endif /* KERNEL_LINUX */
889 static int mach_get_task_name (task_t t, int *pid, char *name, size_t name_max_len)
893 struct kinfo_proc kp;
898 mib[2] = KERN_PROC_PID;
900 if (pid_for_task (t, pid) != KERN_SUCCESS)
904 kp_size = sizeof (kp);
905 if (sysctl (mib, 4, &kp, &kp_size, NULL, 0) != 0)
908 if (name_max_len > (MAXCOMLEN + 1))
909 name_max_len = MAXCOMLEN + 1;
911 strncpy (name, kp.kp_proc.p_comm, name_max_len - 1);
912 name[name_max_len - 1] = '\0';
914 DEBUG ("pid = %i; name = %s;", *pid, name);
916 /* We don't do the special handling for `p_comm == "LaunchCFMApp"' as
917 * `top' does it, because it is a lot of work and only used when
918 * debugging. -octo */
922 #endif /* HAVE_THREAD_INFO */
923 /* ------- end of additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
925 /* do actual readings from kernel */
926 static int ps_read (void)
929 kern_return_t status;
932 processor_set_t port_pset_priv;
935 task_array_t task_list;
936 mach_msg_type_number_t task_list_len;
939 char task_name[MAXCOMLEN + 1];
942 thread_act_array_t thread_list;
943 mach_msg_type_number_t thread_list_len;
944 thread_basic_info_data_t thread_data;
945 mach_msg_type_number_t thread_data_len;
954 procstat_entry_t pse;
959 * The Mach-concept is a little different from the traditional UNIX
960 * concept: All the work is done in threads. Threads are contained in
961 * `tasks'. Therefore, `task status' doesn't make much sense, since
962 * it's actually a `thread status'.
963 * Tasks are assigned to sets of processors, so that's where you go to
966 for (pset = 0; pset < pset_list_len; pset++)
968 if ((status = host_processor_set_priv (port_host_self,
970 &port_pset_priv)) != KERN_SUCCESS)
972 ERROR ("host_processor_set_priv failed: %s\n",
973 mach_error_string (status));
977 if ((status = processor_set_tasks (port_pset_priv,
979 &task_list_len)) != KERN_SUCCESS)
981 ERROR ("processor_set_tasks failed: %s\n",
982 mach_error_string (status));
983 mach_port_deallocate (port_task_self, port_pset_priv);
987 for (task = 0; task < task_list_len; task++)
990 if (mach_get_task_name (task_list[task],
992 task_name, PROCSTAT_NAME_LEN) == 0)
994 /* search for at least one match */
995 for (ps = list_head_g; ps != NULL; ps = ps->next)
996 /* FIXME: cmdline should be here instead of NULL */
997 if (ps_list_match (task_name, NULL, ps) == 1)
1001 /* Collect more detailed statistics for this process */
1004 task_basic_info_data_t task_basic_info;
1005 mach_msg_type_number_t task_basic_info_len;
1006 task_events_info_data_t task_events_info;
1007 mach_msg_type_number_t task_events_info_len;
1008 task_absolutetime_info_data_t task_absolutetime_info;
1009 mach_msg_type_number_t task_absolutetime_info_len;
1011 memset (&pse, '\0', sizeof (pse));
1014 task_basic_info_len = TASK_BASIC_INFO_COUNT;
1015 status = task_info (task_list[task],
1017 (task_info_t) &task_basic_info,
1018 &task_basic_info_len);
1019 if (status != KERN_SUCCESS)
1021 ERROR ("task_info failed: %s",
1022 mach_error_string (status));
1023 continue; /* with next thread_list */
1026 task_events_info_len = TASK_EVENTS_INFO_COUNT;
1027 status = task_info (task_list[task],
1029 (task_info_t) &task_events_info,
1030 &task_events_info_len);
1031 if (status != KERN_SUCCESS)
1033 ERROR ("task_info failed: %s",
1034 mach_error_string (status));
1035 continue; /* with next thread_list */
1038 task_absolutetime_info_len = TASK_ABSOLUTETIME_INFO_COUNT;
1039 status = task_info (task_list[task],
1040 TASK_ABSOLUTETIME_INFO,
1041 (task_info_t) &task_absolutetime_info,
1042 &task_absolutetime_info_len);
1043 if (status != KERN_SUCCESS)
1045 ERROR ("task_info failed: %s",
1046 mach_error_string (status));
1047 continue; /* with next thread_list */
1051 pse.vmem_rss = task_basic_info.resident_size;
1053 pse.vmem_minflt_counter = task_events_info.cow_faults;
1054 pse.vmem_majflt_counter = task_events_info.faults;
1056 pse.cpu_user_counter = task_absolutetime_info.total_user;
1057 pse.cpu_system_counter = task_absolutetime_info.total_system;
1060 status = task_threads (task_list[task], &thread_list,
1062 if (status != KERN_SUCCESS)
1064 /* Apple's `top' treats this case a zombie. It
1065 * makes sense to some extend: A `zombie'
1066 * thread is nonsense, since the task/process
1069 DEBUG ("task_threads failed: %s",
1070 mach_error_string (status));
1071 if (task_list[task] != port_task_self)
1072 mach_port_deallocate (port_task_self,
1074 continue; /* with next task_list */
1077 for (thread = 0; thread < thread_list_len; thread++)
1079 thread_data_len = THREAD_BASIC_INFO_COUNT;
1080 status = thread_info (thread_list[thread],
1082 (thread_info_t) &thread_data,
1084 if (status != KERN_SUCCESS)
1086 ERROR ("thread_info failed: %s",
1087 mach_error_string (status));
1088 if (task_list[task] != port_task_self)
1089 mach_port_deallocate (port_task_self,
1090 thread_list[thread]);
1091 continue; /* with next thread_list */
1097 switch (thread_data.run_state)
1099 case TH_STATE_RUNNING:
1102 case TH_STATE_STOPPED:
1103 /* What exactly is `halted'? */
1104 case TH_STATE_HALTED:
1107 case TH_STATE_WAITING:
1110 case TH_STATE_UNINTERRUPTIBLE:
1113 /* There is no `zombie' case here,
1114 * since there are no zombie-threads.
1115 * There's only zombie tasks, which are
1118 WARNING ("Unknown thread status: %i",
1119 thread_data.run_state);
1121 } /* switch (thread_data.run_state) */
1123 if (task_list[task] != port_task_self)
1125 status = mach_port_deallocate (port_task_self,
1126 thread_list[thread]);
1127 if (status != KERN_SUCCESS)
1128 ERROR ("mach_port_deallocate failed: %s",
1129 mach_error_string (status));
1131 } /* for (thread_list) */
1133 if ((status = vm_deallocate (port_task_self,
1134 (vm_address_t) thread_list,
1135 thread_list_len * sizeof (thread_act_t)))
1138 ERROR ("vm_deallocate failed: %s",
1139 mach_error_string (status));
1142 thread_list_len = 0;
1144 /* Only deallocate the task port, if it isn't our own.
1145 * Don't know what would happen in that case, but this
1146 * is what Apple's top does.. ;) */
1147 if (task_list[task] != port_task_self)
1149 status = mach_port_deallocate (port_task_self,
1151 if (status != KERN_SUCCESS)
1152 ERROR ("mach_port_deallocate failed: %s",
1153 mach_error_string (status));
1157 /* FIXME: cmdline should be here instead of NULL */
1158 ps_list_add (task_name, NULL, &pse);
1159 } /* for (task_list) */
1161 if ((status = vm_deallocate (port_task_self,
1162 (vm_address_t) task_list,
1163 task_list_len * sizeof (task_t))) != KERN_SUCCESS)
1165 ERROR ("vm_deallocate failed: %s",
1166 mach_error_string (status));
1171 if ((status = mach_port_deallocate (port_task_self, port_pset_priv))
1174 ERROR ("mach_port_deallocate failed: %s",
1175 mach_error_string (status));
1177 } /* for (pset_list) */
1179 ps_submit_state ("running", running);
1180 ps_submit_state ("sleeping", sleeping);
1181 ps_submit_state ("zombies", zombies);
1182 ps_submit_state ("stopped", stopped);
1183 ps_submit_state ("blocked", blocked);
1185 for (ps = list_head_g; ps != NULL; ps = ps->next)
1186 ps_submit_proc_list (ps);
1187 /* #endif HAVE_THREAD_INFO */
1201 char cmdline[ARG_MAX];
1205 procstat_entry_t pse;
1210 running = sleeping = zombies = stopped = paging = blocked = 0;
1213 if ((proc = opendir ("/proc")) == NULL)
1216 ERROR ("Cannot open `/proc': %s",
1217 sstrerror (errno, errbuf, sizeof (errbuf)));
1221 while ((ent = readdir (proc)) != NULL)
1223 if (!isdigit (ent->d_name[0]))
1226 if ((pid = atoi (ent->d_name)) < 1)
1229 status = ps_read_process (pid, &ps, &state);
1232 DEBUG ("ps_read_process failed: %i", status);
1239 pse.num_proc = ps.num_proc;
1240 pse.num_lwp = ps.num_lwp;
1241 pse.vmem_size = ps.vmem_size;
1242 pse.vmem_rss = ps.vmem_rss;
1243 pse.stack_size = ps.stack_size;
1245 pse.vmem_minflt = 0;
1246 pse.vmem_minflt_counter = ps.vmem_minflt_counter;
1247 pse.vmem_majflt = 0;
1248 pse.vmem_majflt_counter = ps.vmem_majflt_counter;
1251 pse.cpu_user_counter = ps.cpu_user_counter;
1253 pse.cpu_system_counter = ps.cpu_system_counter;
1257 case 'R': running++; break;
1258 case 'S': sleeping++; break;
1259 case 'D': blocked++; break;
1260 case 'Z': zombies++; break;
1261 case 'T': stopped++; break;
1262 case 'W': paging++; break;
1265 ps_list_add (ps.name,
1266 ps_get_cmdline (pid, ps.name, cmdline, sizeof (cmdline)),
1272 ps_submit_state ("running", running);
1273 ps_submit_state ("sleeping", sleeping);
1274 ps_submit_state ("zombies", zombies);
1275 ps_submit_state ("stopped", stopped);
1276 ps_submit_state ("paging", paging);
1277 ps_submit_state ("blocked", blocked);
1279 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1280 ps_submit_proc_list (ps_ptr);
1281 /* #endif KERNEL_LINUX */
1283 #elif HAVE_LIBKVM_GETPROCS
1294 char cmdline[ARG_MAX];
1296 struct kinfo_proc *procs; /* array of processes */
1298 int count; /* returns number of processes */
1302 procstat_entry_t pse;
1306 /* Open the kvm interface, get a descriptor */
1307 kd = kvm_open (NULL, NULL, NULL, 0, errbuf);
1310 ERROR ("processes plugin: Cannot open kvm interface: %s",
1315 /* Get the list of processes. */
1316 procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, &count);
1320 ERROR ("processes plugin: Cannot get kvm processes list: %s",
1325 /* Iterate through the processes in kinfo_proc */
1326 for (i = 0; i < count; i++)
1328 /* retrieve the arguments */
1332 argv = kvm_getargv (kd, (const struct kinfo_proc *) &(procs[i]), 0);
1339 while (argv[argc] != NULL)
1342 status = strjoin (cmdline, sizeof (cmdline),
1347 WARNING ("processes plugin: Command line did "
1348 "not fit into buffer.");
1352 cmdline_ptr = &cmdline[0];
1356 pse.id = procs[i].ki_pid;
1360 pse.num_lwp = procs[i].ki_numthreads;
1362 pse.vmem_rss = procs[i].ki_rssize * getpagesize();
1363 pse.vmem_minflt = 0;
1364 pse.vmem_minflt_counter = procs[i].ki_rusage.ru_minflt;
1365 pse.vmem_majflt = 0;
1366 pse.vmem_majflt_counter = procs[i].ki_rusage.ru_majflt;
1369 pse.cpu_user_counter = procs[i].ki_rusage.ru_utime.tv_sec
1371 + procs[i].ki_rusage.ru_utime.tv_usec;
1373 pse.cpu_system_counter = procs[i].ki_rusage.ru_stime.tv_sec
1375 + procs[i].ki_rusage.ru_stime.tv_usec;
1377 switch (procs[i].ki_stat)
1379 case SSTOP: stopped++; break;
1380 case SSLEEP: sleeping++; break;
1381 case SRUN: running++; break;
1382 case SIDL: idle++; break;
1383 case SWAIT: wait++; break;
1384 case SLOCK: blocked++; break;
1385 case SZOMB: zombies++; break;
1388 ps_list_add (procs[i].ki_comm, cmdline_ptr, &pse);
1393 ps_submit_state ("running", running);
1394 ps_submit_state ("sleeping", sleeping);
1395 ps_submit_state ("zombies", zombies);
1396 ps_submit_state ("stopped", stopped);
1397 ps_submit_state ("blocked", blocked);
1398 ps_submit_state ("idle", idle);
1399 ps_submit_state ("wait", wait);
1401 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1402 ps_submit_proc_list (ps_ptr);
1403 #endif /* HAVE_LIBKVM_GETPROCS */
1408 void module_register (void)
1410 plugin_register_config ("processes", ps_config,
1411 config_keys, config_keys_num);
1412 plugin_register_init ("processes", ps_init);
1413 plugin_register_read ("processes", ps_read);
1414 } /* void module_register */