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
87 # if defined(HAVE_SYSCONF) && HAVE_SYSCONF && defined(_SC_ARG_MAX)
88 # define ARG_MAX sysconf(_SC_ARG_MAX)
93 /* #endif KERNEL_LINUX */
95 #elif HAVE_LIBKVM_GETPROCS
97 # include <sys/user.h>
98 # include <sys/proc.h>
99 # if HAVE_SYS_SYSCTL_H
100 # include <sys/sysctl.h>
102 /* #endif HAVE_LIBKVM_GETPROCS */
105 # error "No applicable input method."
114 static const char *config_keys[] =
119 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
121 typedef struct procstat_entry_s
126 unsigned long num_proc;
127 unsigned long num_lwp;
128 unsigned long vmem_size;
129 unsigned long vmem_rss;
130 unsigned long stack_size;
132 unsigned long vmem_minflt;
133 unsigned long vmem_majflt;
134 unsigned long vmem_minflt_counter;
135 unsigned long vmem_majflt_counter;
137 unsigned long cpu_user;
138 unsigned long cpu_system;
139 unsigned long cpu_user_counter;
140 unsigned long cpu_system_counter;
142 struct procstat_entry_s *next;
145 #define PROCSTAT_NAME_LEN 256
146 typedef struct procstat
148 char name[PROCSTAT_NAME_LEN];
153 unsigned long num_proc;
154 unsigned long num_lwp;
155 unsigned long vmem_size;
156 unsigned long vmem_rss;
157 unsigned long stack_size;
159 unsigned long vmem_minflt_counter;
160 unsigned long vmem_majflt_counter;
162 unsigned long cpu_user_counter;
163 unsigned long cpu_system_counter;
165 struct procstat *next;
166 struct procstat_entry_s *instances;
169 static procstat_t *list_head_g = NULL;
172 static mach_port_t port_host_self;
173 static mach_port_t port_task_self;
175 static processor_set_name_array_t pset_list;
176 static mach_msg_type_number_t pset_list_len;
177 /* #endif HAVE_THREAD_INFO */
180 static long pagesize_g;
181 /* #endif KERNEL_LINUX */
183 #elif HAVE_LIBKVM_GETPROCS
184 /* no global variables */
185 #endif /* HAVE_LIBKVM_GETPROCS */
187 /* put name of process from config to list_head_g tree
188 list_head_g is a list of 'procstat_t' structs with
189 processes names we want to watch */
190 static void ps_list_register (const char *name, const char *regexp)
196 new = (procstat_t *) malloc (sizeof (procstat_t));
199 ERROR ("processes plugin: ps_list_register: malloc failed.");
202 memset (new, 0, sizeof (procstat_t));
203 sstrncpy (new->name, name, sizeof (new->name));
208 DEBUG ("ProcessMatch: adding \"%s\" as criteria to process %s.", regexp, name);
209 new->re = (regex_t *) malloc (sizeof (regex_t));
212 ERROR ("processes plugin: ps_list_register: malloc failed.");
217 status = regcomp (new->re, regexp, REG_EXTENDED | REG_NOSUB);
220 DEBUG ("ProcessMatch: compiling the regular expression \"%s\" failed.", regexp);
228 ERROR ("processes plugin: ps_list_register: "
229 "Regular expression \"%s\" found in config "
230 "file, but support for regular expressions "
231 "has been dispabled at compile time.",
238 for (ptr = list_head_g; ptr != NULL; ptr = ptr->next)
240 if (strcmp (ptr->name, name) == 0)
242 WARNING ("processes plugin: You have configured more "
243 "than one `Process' or "
244 "`ProcessMatch' with the same name. "
245 "All but the first setting will be "
252 if (ptr->next == NULL)
260 } /* void ps_list_register */
262 /* try to match name against entry, returns 1 if success */
263 static int ps_list_match (const char *name, const char *cmdline, procstat_t *ps)
272 if ((str == NULL) || (str[0] == 0))
275 assert (str != NULL);
277 status = regexec (ps->re, str,
286 if (strcmp (ps->name, name) == 0)
290 } /* int ps_list_match */
292 /* add process entry to 'instances' of process 'name' (or refresh it) */
293 static void ps_list_add (const char *name, const char *cmdline, procstat_entry_t *entry)
296 procstat_entry_t *pse;
301 for (ps = list_head_g; ps != NULL; ps = ps->next)
303 if ((ps_list_match (name, cmdline, ps)) == 0)
306 for (pse = ps->instances; pse != NULL; pse = pse->next)
307 if ((pse->id == entry->id) || (pse->next == NULL))
310 if ((pse == NULL) || (pse->id != entry->id))
312 procstat_entry_t *new;
314 new = (procstat_entry_t *) malloc (sizeof (procstat_entry_t));
317 memset (new, 0, sizeof (procstat_entry_t));
329 pse->num_proc = entry->num_proc;
330 pse->num_lwp = entry->num_lwp;
331 pse->vmem_size = entry->vmem_size;
332 pse->vmem_rss = entry->vmem_rss;
333 pse->stack_size = entry->stack_size;
335 ps->num_proc += pse->num_proc;
336 ps->num_lwp += pse->num_lwp;
337 ps->vmem_size += pse->vmem_size;
338 ps->vmem_rss += pse->vmem_rss;
339 ps->stack_size += pse->stack_size;
341 if ((entry->vmem_minflt_counter == 0)
342 && (entry->vmem_majflt_counter == 0))
344 pse->vmem_minflt_counter += entry->vmem_minflt;
345 pse->vmem_minflt = entry->vmem_minflt;
347 pse->vmem_majflt_counter += entry->vmem_majflt;
348 pse->vmem_majflt = entry->vmem_majflt;
352 if (entry->vmem_minflt_counter < pse->vmem_minflt_counter)
354 pse->vmem_minflt = entry->vmem_minflt_counter
355 + (ULONG_MAX - pse->vmem_minflt_counter);
359 pse->vmem_minflt = entry->vmem_minflt_counter - pse->vmem_minflt_counter;
361 pse->vmem_minflt_counter = entry->vmem_minflt_counter;
363 if (entry->vmem_majflt_counter < pse->vmem_majflt_counter)
365 pse->vmem_majflt = entry->vmem_majflt_counter
366 + (ULONG_MAX - pse->vmem_majflt_counter);
370 pse->vmem_majflt = entry->vmem_majflt_counter - pse->vmem_majflt_counter;
372 pse->vmem_majflt_counter = entry->vmem_majflt_counter;
375 ps->vmem_minflt_counter += pse->vmem_minflt;
376 ps->vmem_majflt_counter += pse->vmem_majflt;
378 if ((entry->cpu_user_counter == 0)
379 && (entry->cpu_system_counter == 0))
381 pse->cpu_user_counter += entry->cpu_user;
382 pse->cpu_user = entry->cpu_user;
384 pse->cpu_system_counter += entry->cpu_system;
385 pse->cpu_system = entry->cpu_system;
389 if (entry->cpu_user_counter < pse->cpu_user_counter)
391 pse->cpu_user = entry->cpu_user_counter
392 + (ULONG_MAX - pse->cpu_user_counter);
396 pse->cpu_user = entry->cpu_user_counter - pse->cpu_user_counter;
398 pse->cpu_user_counter = entry->cpu_user_counter;
400 if (entry->cpu_system_counter < pse->cpu_system_counter)
402 pse->cpu_system = entry->cpu_system_counter
403 + (ULONG_MAX - pse->cpu_system_counter);
407 pse->cpu_system = entry->cpu_system_counter - pse->cpu_system_counter;
409 pse->cpu_system_counter = entry->cpu_system_counter;
412 ps->cpu_user_counter += pse->cpu_user;
413 ps->cpu_system_counter += pse->cpu_system;
417 /* remove old entries from instances of processes in list_head_g */
418 static void ps_list_reset (void)
421 procstat_entry_t *pse;
422 procstat_entry_t *pse_prev;
424 for (ps = list_head_g; ps != NULL; ps = ps->next)
438 DEBUG ("Removing this procstat entry cause it's too old: "
439 "id = %lu; name = %s;",
442 if (pse_prev == NULL)
444 ps->instances = pse->next;
450 pse_prev->next = pse->next;
452 pse = pse_prev->next;
461 } /* while (pse != NULL) */
462 } /* for (ps = list_head_g; ps != NULL; ps = ps->next) */
465 /* put all pre-defined 'Process' names from config to list_head_g tree */
466 static int ps_config (const char *key, const char *value)
468 if (strcasecmp (key, "Process") == 0)
470 ps_list_register (value, NULL);
472 else if (strcasecmp (key, "ProcessMatch") == 0)
478 new_val = strdup (value);
479 if (new_val == NULL) {
480 ERROR ("processes plugin: strdup failed when processing "
481 "`ProcessMatch %s'.", value);
485 fields_num = strsplit (new_val, fields,
486 STATIC_ARRAY_SIZE (fields));
489 ERROR ("processes plugin: `ProcessMatch' needs exactly "
490 "two string arguments.");
494 ps_list_register (fields[0], fields[1]);
499 ERROR ("processes plugin: The `%s' configuration option is not "
500 "understood and will be ignored.", key);
507 static int ps_init (void)
510 kern_return_t status;
512 port_host_self = mach_host_self ();
513 port_task_self = mach_task_self ();
515 if (pset_list != NULL)
517 vm_deallocate (port_task_self,
518 (vm_address_t) pset_list,
519 pset_list_len * sizeof (processor_set_t));
524 if ((status = host_processor_sets (port_host_self,
526 &pset_list_len)) != KERN_SUCCESS)
528 ERROR ("host_processor_sets failed: %s\n",
529 mach_error_string (status));
534 /* #endif HAVE_THREAD_INFO */
537 pagesize_g = sysconf(_SC_PAGESIZE);
538 DEBUG ("pagesize_g = %li; CONFIG_HZ = %i;",
539 pagesize_g, CONFIG_HZ);
540 /* #endif KERNEL_LINUX */
542 #elif HAVE_LIBKVM_GETPROCS
543 /* no initialization */
544 #endif /* HAVE_LIBKVM_GETPROCS */
549 /* submit global state (e.g.: qty of zombies, running, etc..) */
550 static void ps_submit_state (const char *state, double value)
553 value_list_t vl = VALUE_LIST_INIT;
555 values[0].gauge = value;
559 vl.time = time (NULL);
560 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
561 sstrncpy (vl.plugin, "processes", sizeof (vl.plugin));
562 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
563 sstrncpy (vl.type, "ps_state", sizeof (vl.type));
564 sstrncpy (vl.type_instance, state, sizeof (vl.type_instance));
566 plugin_dispatch_values (&vl);
569 /* submit info about specific process (e.g.: memory taken, cpu usage, etc..) */
570 static void ps_submit_proc_list (procstat_t *ps)
573 value_list_t vl = VALUE_LIST_INIT;
577 vl.time = time (NULL);
578 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
579 sstrncpy (vl.plugin, "processes", sizeof (vl.plugin));
580 sstrncpy (vl.plugin_instance, ps->name, sizeof (vl.plugin_instance));
582 sstrncpy (vl.type, "ps_vm", sizeof (vl.type));
583 vl.values[0].gauge = ps->vmem_size;
585 plugin_dispatch_values (&vl);
587 sstrncpy (vl.type, "ps_rss", sizeof (vl.type));
588 vl.values[0].gauge = ps->vmem_rss;
590 plugin_dispatch_values (&vl);
592 sstrncpy (vl.type, "ps_stacksize", sizeof (vl.type));
593 vl.values[0].gauge = ps->stack_size;
595 plugin_dispatch_values (&vl);
597 sstrncpy (vl.type, "ps_cputime", sizeof (vl.type));
598 vl.values[0].counter = ps->cpu_user_counter;
599 vl.values[1].counter = ps->cpu_system_counter;
601 plugin_dispatch_values (&vl);
603 sstrncpy (vl.type, "ps_count", sizeof (vl.type));
604 vl.values[0].gauge = ps->num_proc;
605 vl.values[1].gauge = ps->num_lwp;
607 plugin_dispatch_values (&vl);
609 sstrncpy (vl.type, "ps_pagefaults", sizeof (vl.type));
610 vl.values[0].counter = ps->vmem_minflt_counter;
611 vl.values[1].counter = ps->vmem_majflt_counter;
613 plugin_dispatch_values (&vl);
615 DEBUG ("name = %s; num_proc = %lu; num_lwp = %lu; vmem_rss = %lu; "
616 "vmem_minflt_counter = %lu; vmem_majflt_counter = %lu; "
617 "cpu_user_counter = %lu; cpu_system_counter = %lu;",
618 ps->name, ps->num_proc, ps->num_lwp, ps->vmem_rss,
619 ps->vmem_minflt_counter, ps->vmem_majflt_counter,
620 ps->cpu_user_counter, ps->cpu_system_counter);
621 } /* void ps_submit_proc_list */
623 /* ------- additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
625 static int *ps_read_tasks (int pid)
628 int list_size = 1; /* size of allocated space, in elements */
629 int list_len = 0; /* number of currently used elements */
635 ssnprintf (dirname, sizeof (dirname), "/proc/%i/task", pid);
637 if ((dh = opendir (dirname)) == NULL)
639 DEBUG ("Failed to open directory `%s'", dirname);
643 while ((ent = readdir (dh)) != NULL)
645 if (!isdigit (ent->d_name[0]))
648 if ((list_len + 1) >= list_size)
651 int new_size = 2 * list_size;
652 /* Comes in sizes: 2, 4, 8, 16, ... */
654 new_ptr = (int *) realloc (list, (size_t) (sizeof (int) * new_size));
659 ERROR ("processes plugin: "
660 "Failed to allocate more memory.");
665 list_size = new_size;
667 memset (list + list_len, 0, sizeof (int) * (list_size - list_len));
670 list[list_len] = atoi (ent->d_name);
671 if (list[list_len] != 0)
680 assert (list_len < list_size);
681 assert (list[list_len] == 0);
684 } /* int *ps_read_tasks */
686 int ps_read_process (int pid, procstat_t *ps, char *state)
700 long long unsigned cpu_user_counter;
701 long long unsigned cpu_system_counter;
702 long long unsigned vmem_size;
703 long long unsigned vmem_rss;
704 long long unsigned stack_size;
706 memset (ps, 0, sizeof (procstat_t));
708 ssnprintf (filename, sizeof (filename), "/proc/%i/stat", pid);
710 i = read_file_contents (filename, buffer, sizeof(buffer) - 1);
715 fields_len = strsplit (buffer, fields, 64);
718 DEBUG ("processes plugin: ps_read_process (pid = %i):"
719 " `%s' has only %i fields..",
720 (int) pid, filename, fields_len);
724 /* copy the name, strip brackets in the process */
725 name_len = strlen (fields[1]) - 2;
726 if ((fields[1][0] != '(') || (fields[1][name_len + 1] != ')'))
728 DEBUG ("No brackets found in process name: `%s'", fields[1]);
731 fields[1] = fields[1] + 1;
732 fields[1][name_len] = '\0';
733 strncpy (ps->name, fields[1], PROCSTAT_NAME_LEN);
735 ppid = atoi (fields[3]);
737 *state = fields[2][0];
744 else if ((tasks = ps_read_tasks (pid)) == NULL)
746 /* Kernel 2.4 or so */
754 for (i = 0; tasks[i] != 0; i++)
761 /* Leave the rest at zero if this is only a zombi */
762 if (ps->num_proc == 0)
764 DEBUG ("processes plugin: This is only a zombi: pid = %i; "
765 "name = %s;", pid, ps->name);
769 cpu_user_counter = atoll (fields[13]);
770 cpu_system_counter = atoll (fields[14]);
771 vmem_size = atoll (fields[22]);
772 vmem_rss = atoll (fields[23]);
773 ps->vmem_minflt_counter = atol (fields[9]);
774 ps->vmem_majflt_counter = atol (fields[11]);
777 unsigned long long stack_start = atoll (fields[27]);
778 unsigned long long stack_ptr = atoll (fields[28]);
780 stack_size = (stack_start > stack_ptr)
781 ? stack_start - stack_ptr
782 : stack_ptr - stack_start;
785 /* Convert jiffies to useconds */
786 cpu_user_counter = cpu_user_counter * 1000000 / CONFIG_HZ;
787 cpu_system_counter = cpu_system_counter * 1000000 / CONFIG_HZ;
788 vmem_rss = vmem_rss * pagesize_g;
790 ps->cpu_user_counter = (unsigned long) cpu_user_counter;
791 ps->cpu_system_counter = (unsigned long) cpu_system_counter;
792 ps->vmem_size = (unsigned long) vmem_size;
793 ps->vmem_rss = (unsigned long) vmem_rss;
794 ps->stack_size = (unsigned long) stack_size;
798 } /* int ps_read_process (...) */
800 static char *ps_get_cmdline (pid_t pid, char *name, char *buf, size_t buf_len)
810 if ((pid < 1) || (NULL == buf) || (buf_len < 2))
813 ssnprintf (file, sizeof (file), "/proc/%u/cmdline", pid);
815 fd = open (file, O_RDONLY);
818 WARNING ("processes plugin: Failed to open `%s': %s.", file,
819 sstrerror (errno, errbuf, sizeof (errbuf)));
831 status = read (fd, (void *)buf_ptr, len);
836 if ((EAGAIN == errno) || (EINTR == errno))
839 WARNING ("processes plugin: Failed to read from `%s': %s.", file,
840 sstrerror (errno, errbuf, sizeof (errbuf)));
860 /* cmdline not available; e.g. kernel thread, zombie */
864 ssnprintf (buf, buf_len, "[%s]", name);
868 assert (n <= buf_len);
875 /* remove trailing whitespace */
876 while ((n > 0) && (isspace (buf[n]) || ('\0' == buf[n]))) {
881 /* arguments are separated by '\0' in /proc/<pid>/cmdline */
888 } /* char *ps_get_cmdline (...) */
889 #endif /* KERNEL_LINUX */
892 static int mach_get_task_name (task_t t, int *pid, char *name, size_t name_max_len)
896 struct kinfo_proc kp;
901 mib[2] = KERN_PROC_PID;
903 if (pid_for_task (t, pid) != KERN_SUCCESS)
907 kp_size = sizeof (kp);
908 if (sysctl (mib, 4, &kp, &kp_size, NULL, 0) != 0)
911 if (name_max_len > (MAXCOMLEN + 1))
912 name_max_len = MAXCOMLEN + 1;
914 strncpy (name, kp.kp_proc.p_comm, name_max_len - 1);
915 name[name_max_len - 1] = '\0';
917 DEBUG ("pid = %i; name = %s;", *pid, name);
919 /* We don't do the special handling for `p_comm == "LaunchCFMApp"' as
920 * `top' does it, because it is a lot of work and only used when
921 * debugging. -octo */
925 #endif /* HAVE_THREAD_INFO */
926 /* ------- end of additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
928 /* do actual readings from kernel */
929 static int ps_read (void)
932 kern_return_t status;
935 processor_set_t port_pset_priv;
938 task_array_t task_list;
939 mach_msg_type_number_t task_list_len;
942 char task_name[MAXCOMLEN + 1];
945 thread_act_array_t thread_list;
946 mach_msg_type_number_t thread_list_len;
947 thread_basic_info_data_t thread_data;
948 mach_msg_type_number_t thread_data_len;
957 procstat_entry_t pse;
962 * The Mach-concept is a little different from the traditional UNIX
963 * concept: All the work is done in threads. Threads are contained in
964 * `tasks'. Therefore, `task status' doesn't make much sense, since
965 * it's actually a `thread status'.
966 * Tasks are assigned to sets of processors, so that's where you go to
969 for (pset = 0; pset < pset_list_len; pset++)
971 if ((status = host_processor_set_priv (port_host_self,
973 &port_pset_priv)) != KERN_SUCCESS)
975 ERROR ("host_processor_set_priv failed: %s\n",
976 mach_error_string (status));
980 if ((status = processor_set_tasks (port_pset_priv,
982 &task_list_len)) != KERN_SUCCESS)
984 ERROR ("processor_set_tasks failed: %s\n",
985 mach_error_string (status));
986 mach_port_deallocate (port_task_self, port_pset_priv);
990 for (task = 0; task < task_list_len; task++)
993 if (mach_get_task_name (task_list[task],
995 task_name, PROCSTAT_NAME_LEN) == 0)
997 /* search for at least one match */
998 for (ps = list_head_g; ps != NULL; ps = ps->next)
999 /* FIXME: cmdline should be here instead of NULL */
1000 if (ps_list_match (task_name, NULL, ps) == 1)
1004 /* Collect more detailed statistics for this process */
1007 task_basic_info_data_t task_basic_info;
1008 mach_msg_type_number_t task_basic_info_len;
1009 task_events_info_data_t task_events_info;
1010 mach_msg_type_number_t task_events_info_len;
1011 task_absolutetime_info_data_t task_absolutetime_info;
1012 mach_msg_type_number_t task_absolutetime_info_len;
1014 memset (&pse, '\0', sizeof (pse));
1017 task_basic_info_len = TASK_BASIC_INFO_COUNT;
1018 status = task_info (task_list[task],
1020 (task_info_t) &task_basic_info,
1021 &task_basic_info_len);
1022 if (status != KERN_SUCCESS)
1024 ERROR ("task_info failed: %s",
1025 mach_error_string (status));
1026 continue; /* with next thread_list */
1029 task_events_info_len = TASK_EVENTS_INFO_COUNT;
1030 status = task_info (task_list[task],
1032 (task_info_t) &task_events_info,
1033 &task_events_info_len);
1034 if (status != KERN_SUCCESS)
1036 ERROR ("task_info failed: %s",
1037 mach_error_string (status));
1038 continue; /* with next thread_list */
1041 task_absolutetime_info_len = TASK_ABSOLUTETIME_INFO_COUNT;
1042 status = task_info (task_list[task],
1043 TASK_ABSOLUTETIME_INFO,
1044 (task_info_t) &task_absolutetime_info,
1045 &task_absolutetime_info_len);
1046 if (status != KERN_SUCCESS)
1048 ERROR ("task_info failed: %s",
1049 mach_error_string (status));
1050 continue; /* with next thread_list */
1054 pse.vmem_rss = task_basic_info.resident_size;
1056 pse.vmem_minflt_counter = task_events_info.cow_faults;
1057 pse.vmem_majflt_counter = task_events_info.faults;
1059 pse.cpu_user_counter = task_absolutetime_info.total_user;
1060 pse.cpu_system_counter = task_absolutetime_info.total_system;
1063 status = task_threads (task_list[task], &thread_list,
1065 if (status != KERN_SUCCESS)
1067 /* Apple's `top' treats this case a zombie. It
1068 * makes sense to some extend: A `zombie'
1069 * thread is nonsense, since the task/process
1072 DEBUG ("task_threads failed: %s",
1073 mach_error_string (status));
1074 if (task_list[task] != port_task_self)
1075 mach_port_deallocate (port_task_self,
1077 continue; /* with next task_list */
1080 for (thread = 0; thread < thread_list_len; thread++)
1082 thread_data_len = THREAD_BASIC_INFO_COUNT;
1083 status = thread_info (thread_list[thread],
1085 (thread_info_t) &thread_data,
1087 if (status != KERN_SUCCESS)
1089 ERROR ("thread_info failed: %s",
1090 mach_error_string (status));
1091 if (task_list[task] != port_task_self)
1092 mach_port_deallocate (port_task_self,
1093 thread_list[thread]);
1094 continue; /* with next thread_list */
1100 switch (thread_data.run_state)
1102 case TH_STATE_RUNNING:
1105 case TH_STATE_STOPPED:
1106 /* What exactly is `halted'? */
1107 case TH_STATE_HALTED:
1110 case TH_STATE_WAITING:
1113 case TH_STATE_UNINTERRUPTIBLE:
1116 /* There is no `zombie' case here,
1117 * since there are no zombie-threads.
1118 * There's only zombie tasks, which are
1121 WARNING ("Unknown thread status: %i",
1122 thread_data.run_state);
1124 } /* switch (thread_data.run_state) */
1126 if (task_list[task] != port_task_self)
1128 status = mach_port_deallocate (port_task_self,
1129 thread_list[thread]);
1130 if (status != KERN_SUCCESS)
1131 ERROR ("mach_port_deallocate failed: %s",
1132 mach_error_string (status));
1134 } /* for (thread_list) */
1136 if ((status = vm_deallocate (port_task_self,
1137 (vm_address_t) thread_list,
1138 thread_list_len * sizeof (thread_act_t)))
1141 ERROR ("vm_deallocate failed: %s",
1142 mach_error_string (status));
1145 thread_list_len = 0;
1147 /* Only deallocate the task port, if it isn't our own.
1148 * Don't know what would happen in that case, but this
1149 * is what Apple's top does.. ;) */
1150 if (task_list[task] != port_task_self)
1152 status = mach_port_deallocate (port_task_self,
1154 if (status != KERN_SUCCESS)
1155 ERROR ("mach_port_deallocate failed: %s",
1156 mach_error_string (status));
1160 /* FIXME: cmdline should be here instead of NULL */
1161 ps_list_add (task_name, NULL, &pse);
1162 } /* for (task_list) */
1164 if ((status = vm_deallocate (port_task_self,
1165 (vm_address_t) task_list,
1166 task_list_len * sizeof (task_t))) != KERN_SUCCESS)
1168 ERROR ("vm_deallocate failed: %s",
1169 mach_error_string (status));
1174 if ((status = mach_port_deallocate (port_task_self, port_pset_priv))
1177 ERROR ("mach_port_deallocate failed: %s",
1178 mach_error_string (status));
1180 } /* for (pset_list) */
1182 ps_submit_state ("running", running);
1183 ps_submit_state ("sleeping", sleeping);
1184 ps_submit_state ("zombies", zombies);
1185 ps_submit_state ("stopped", stopped);
1186 ps_submit_state ("blocked", blocked);
1188 for (ps = list_head_g; ps != NULL; ps = ps->next)
1189 ps_submit_proc_list (ps);
1190 /* #endif HAVE_THREAD_INFO */
1204 char cmdline[ARG_MAX];
1208 procstat_entry_t pse;
1213 running = sleeping = zombies = stopped = paging = blocked = 0;
1216 if ((proc = opendir ("/proc")) == NULL)
1219 ERROR ("Cannot open `/proc': %s",
1220 sstrerror (errno, errbuf, sizeof (errbuf)));
1224 while ((ent = readdir (proc)) != NULL)
1226 if (!isdigit (ent->d_name[0]))
1229 if ((pid = atoi (ent->d_name)) < 1)
1232 status = ps_read_process (pid, &ps, &state);
1235 DEBUG ("ps_read_process failed: %i", status);
1242 pse.num_proc = ps.num_proc;
1243 pse.num_lwp = ps.num_lwp;
1244 pse.vmem_size = ps.vmem_size;
1245 pse.vmem_rss = ps.vmem_rss;
1246 pse.stack_size = ps.stack_size;
1248 pse.vmem_minflt = 0;
1249 pse.vmem_minflt_counter = ps.vmem_minflt_counter;
1250 pse.vmem_majflt = 0;
1251 pse.vmem_majflt_counter = ps.vmem_majflt_counter;
1254 pse.cpu_user_counter = ps.cpu_user_counter;
1256 pse.cpu_system_counter = ps.cpu_system_counter;
1260 case 'R': running++; break;
1261 case 'S': sleeping++; break;
1262 case 'D': blocked++; break;
1263 case 'Z': zombies++; break;
1264 case 'T': stopped++; break;
1265 case 'W': paging++; break;
1268 ps_list_add (ps.name,
1269 ps_get_cmdline (pid, ps.name, cmdline, sizeof (cmdline)),
1275 ps_submit_state ("running", running);
1276 ps_submit_state ("sleeping", sleeping);
1277 ps_submit_state ("zombies", zombies);
1278 ps_submit_state ("stopped", stopped);
1279 ps_submit_state ("paging", paging);
1280 ps_submit_state ("blocked", blocked);
1282 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1283 ps_submit_proc_list (ps_ptr);
1284 /* #endif KERNEL_LINUX */
1286 #elif HAVE_LIBKVM_GETPROCS
1297 char cmdline[ARG_MAX];
1299 struct kinfo_proc *procs; /* array of processes */
1301 int count; /* returns number of processes */
1305 procstat_entry_t pse;
1309 /* Open the kvm interface, get a descriptor */
1310 kd = kvm_open (NULL, NULL, NULL, 0, errbuf);
1313 ERROR ("processes plugin: Cannot open kvm interface: %s",
1318 /* Get the list of processes. */
1319 procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, &count);
1323 ERROR ("processes plugin: Cannot get kvm processes list: %s",
1328 /* Iterate through the processes in kinfo_proc */
1329 for (i = 0; i < count; i++)
1331 /* retrieve the arguments */
1335 argv = kvm_getargv (kd, (const struct kinfo_proc *) &(procs[i]), 0);
1342 while (argv[argc] != NULL)
1345 status = strjoin (cmdline, sizeof (cmdline),
1350 WARNING ("processes plugin: Command line did "
1351 "not fit into buffer.");
1355 cmdline_ptr = &cmdline[0];
1359 pse.id = procs[i].ki_pid;
1363 pse.num_lwp = procs[i].ki_numthreads;
1365 pse.vmem_rss = procs[i].ki_rssize * getpagesize();
1366 pse.vmem_minflt = 0;
1367 pse.vmem_minflt_counter = procs[i].ki_rusage.ru_minflt;
1368 pse.vmem_majflt = 0;
1369 pse.vmem_majflt_counter = procs[i].ki_rusage.ru_majflt;
1372 pse.cpu_user_counter = procs[i].ki_rusage.ru_utime.tv_sec
1374 + procs[i].ki_rusage.ru_utime.tv_usec;
1376 pse.cpu_system_counter = procs[i].ki_rusage.ru_stime.tv_sec
1378 + procs[i].ki_rusage.ru_stime.tv_usec;
1380 switch (procs[i].ki_stat)
1382 case SSTOP: stopped++; break;
1383 case SSLEEP: sleeping++; break;
1384 case SRUN: running++; break;
1385 case SIDL: idle++; break;
1386 case SWAIT: wait++; break;
1387 case SLOCK: blocked++; break;
1388 case SZOMB: zombies++; break;
1391 ps_list_add (procs[i].ki_comm, cmdline_ptr, &pse);
1396 ps_submit_state ("running", running);
1397 ps_submit_state ("sleeping", sleeping);
1398 ps_submit_state ("zombies", zombies);
1399 ps_submit_state ("stopped", stopped);
1400 ps_submit_state ("blocked", blocked);
1401 ps_submit_state ("idle", idle);
1402 ps_submit_state ("wait", wait);
1404 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1405 ps_submit_proc_list (ps_ptr);
1406 #endif /* HAVE_LIBKVM_GETPROCS */
1411 void module_register (void)
1413 plugin_register_config ("processes", ps_config,
1414 config_keys, config_keys_num);
1415 plugin_register_init ("processes", ps_init);
1416 plugin_register_read ("processes", ps_read);
1417 } /* void module_register */