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."
107 static const char *config_keys[] =
113 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
115 typedef struct procstat_entry_s
120 unsigned long num_proc;
121 unsigned long num_lwp;
122 unsigned long vmem_rss;
124 unsigned long vmem_minflt;
125 unsigned long vmem_majflt;
126 unsigned long vmem_minflt_counter;
127 unsigned long vmem_majflt_counter;
129 unsigned long cpu_user;
130 unsigned long cpu_system;
131 unsigned long cpu_user_counter;
132 unsigned long cpu_system_counter;
134 struct procstat_entry_s *next;
137 #define PROCSTAT_NAME_LEN 256
138 typedef struct procstat
140 char name[PROCSTAT_NAME_LEN];
145 unsigned long num_proc;
146 unsigned long num_lwp;
147 unsigned long vmem_rss;
149 unsigned long vmem_minflt_counter;
150 unsigned long vmem_majflt_counter;
152 unsigned long cpu_user_counter;
153 unsigned long cpu_system_counter;
155 struct procstat *next;
156 struct procstat_entry_s *instances;
159 static procstat_t *list_head_g = NULL;
162 static mach_port_t port_host_self;
163 static mach_port_t port_task_self;
165 static processor_set_name_array_t pset_list;
166 static mach_msg_type_number_t pset_list_len;
167 /* #endif HAVE_THREAD_INFO */
170 static long pagesize_g;
171 /* #endif KERNEL_LINUX */
173 #elif HAVE_LIBKVM_GETPROCS
174 /* no global variables */
175 #endif /* HAVE_LIBKVM_GETPROCS */
177 /* put name of process from config to list_head_g tree
178 list_head_g is a list of 'procstat_t' structs with
179 processes names we want to watch */
180 static void ps_list_register (const char *name, const char *regexp)
186 new = (procstat_t *) malloc (sizeof (procstat_t));
189 ERROR ("processes plugin: ps_list_register: malloc failed.");
192 memset (new, 0, sizeof (procstat_t));
193 sstrncpy (new->name, name, sizeof (new->name));
198 DEBUG ("ProcessMatch: adding \"%s\" as criteria to process %s.", regexp, name);
199 new->re = (regex_t *) malloc (sizeof (regex_t));
202 ERROR ("processes plugin: ps_list_register: malloc failed.");
207 status = regcomp (new->re, regexp, REG_EXTENDED | REG_NOSUB);
210 DEBUG ("ProcessMatch: compiling the regular expression \"%s\" failed.", regexp);
218 ERROR ("processes plugin: ps_list_register: "
219 "Regular expression \"%s\" found in config "
220 "file, but support for regular expressions "
221 "has been dispabled at compile time.",
228 for (ptr = list_head_g; ptr != NULL; ptr = ptr->next)
230 if (strcmp (ptr->name, name) == 0)
232 WARNING ("processes plugin: You have configured more "
233 "than one `Process' or "
234 "`ProcessMatch' with the same name. "
235 "All but the first setting will be "
242 if (ptr->next == NULL)
250 } /* void ps_list_register */
252 /* try to match name against entry, returns 1 if success */
253 static int ps_list_match (const char *name, const char *cmdline, procstat_t *ps)
262 if ((str == NULL) || (str[0] == 0))
265 assert (str != NULL);
267 status = regexec (ps->re, str,
276 if (strcmp (ps->name, name) == 0)
280 } /* int ps_list_match */
282 /* add process entry to 'instances' of process 'name' (or refresh it) */
283 static void ps_list_add (const char *name, const char *cmdline, procstat_entry_t *entry)
286 procstat_entry_t *pse;
291 for (ps = list_head_g; ps != NULL; ps = ps->next)
293 if ((ps_list_match (name, cmdline, ps)) == 0)
296 for (pse = ps->instances; pse != NULL; pse = pse->next)
297 if ((pse->id == entry->id) || (pse->next == NULL))
300 if ((pse == NULL) || (pse->id != entry->id))
302 procstat_entry_t *new;
304 new = (procstat_entry_t *) malloc (sizeof (procstat_entry_t));
307 memset (new, 0, sizeof (procstat_entry_t));
319 pse->num_proc = entry->num_proc;
320 pse->num_lwp = entry->num_lwp;
321 pse->vmem_rss = entry->vmem_rss;
323 ps->num_proc += pse->num_proc;
324 ps->num_lwp += pse->num_lwp;
325 ps->vmem_rss += pse->vmem_rss;
327 if ((entry->vmem_minflt_counter == 0)
328 && (entry->vmem_majflt_counter == 0))
330 pse->vmem_minflt_counter += entry->vmem_minflt;
331 pse->vmem_minflt = entry->vmem_minflt;
333 pse->vmem_majflt_counter += entry->vmem_majflt;
334 pse->vmem_majflt = entry->vmem_majflt;
338 if (entry->vmem_minflt_counter < pse->vmem_minflt_counter)
340 pse->vmem_minflt = entry->vmem_minflt_counter
341 + (ULONG_MAX - pse->vmem_minflt_counter);
345 pse->vmem_minflt = entry->vmem_minflt_counter - pse->vmem_minflt_counter;
347 pse->vmem_minflt_counter = entry->vmem_minflt_counter;
349 if (entry->vmem_majflt_counter < pse->vmem_majflt_counter)
351 pse->vmem_majflt = entry->vmem_majflt_counter
352 + (ULONG_MAX - pse->vmem_majflt_counter);
356 pse->vmem_majflt = entry->vmem_majflt_counter - pse->vmem_majflt_counter;
358 pse->vmem_majflt_counter = entry->vmem_majflt_counter;
361 ps->vmem_minflt_counter += pse->vmem_minflt;
362 ps->vmem_majflt_counter += pse->vmem_majflt;
364 if ((entry->cpu_user_counter == 0)
365 && (entry->cpu_system_counter == 0))
367 pse->cpu_user_counter += entry->cpu_user;
368 pse->cpu_user = entry->cpu_user;
370 pse->cpu_system_counter += entry->cpu_system;
371 pse->cpu_system = entry->cpu_system;
375 if (entry->cpu_user_counter < pse->cpu_user_counter)
377 pse->cpu_user = entry->cpu_user_counter
378 + (ULONG_MAX - pse->cpu_user_counter);
382 pse->cpu_user = entry->cpu_user_counter - pse->cpu_user_counter;
384 pse->cpu_user_counter = entry->cpu_user_counter;
386 if (entry->cpu_system_counter < pse->cpu_system_counter)
388 pse->cpu_system = entry->cpu_system_counter
389 + (ULONG_MAX - pse->cpu_system_counter);
393 pse->cpu_system = entry->cpu_system_counter - pse->cpu_system_counter;
395 pse->cpu_system_counter = entry->cpu_system_counter;
398 ps->cpu_user_counter += pse->cpu_user;
399 ps->cpu_system_counter += pse->cpu_system;
403 /* remove old entries from instances of processes in list_head_g */
404 static void ps_list_reset (void)
407 procstat_entry_t *pse;
408 procstat_entry_t *pse_prev;
410 for (ps = list_head_g; ps != NULL; ps = ps->next)
422 DEBUG ("Removing this procstat entry cause it's too old: "
423 "id = %lu; name = %s;",
426 if (pse_prev == NULL)
428 ps->instances = pse->next;
434 pse_prev->next = pse->next;
436 pse = pse_prev->next;
445 } /* while (pse != NULL) */
446 } /* for (ps = list_head_g; ps != NULL; ps = ps->next) */
449 /* put all pre-defined 'Process' names from config to list_head_g tree */
450 static int ps_config (const char *key, const char *value)
452 if (strcasecmp (key, "Process") == 0)
454 ps_list_register (value, NULL);
456 else if (strcasecmp (key, "ProcessMatch") == 0)
462 new_val = strdup (value);
463 if (new_val == NULL) {
464 ERROR ("processes plugin: strdup failed when processing "
465 "`ProcessMatch %s'.", value);
469 fields_num = strsplit (new_val, fields,
470 STATIC_ARRAY_SIZE (fields));
473 ERROR ("processes plugin: `ProcessMatch' needs exactly "
474 "two string arguments.");
478 ps_list_register (fields[0], fields[1]);
483 ERROR ("processes plugin: The `%s' configuration option is not "
484 "understood and will be ignored.", key);
491 static int ps_init (void)
494 kern_return_t status;
496 port_host_self = mach_host_self ();
497 port_task_self = mach_task_self ();
499 if (pset_list != NULL)
501 vm_deallocate (port_task_self,
502 (vm_address_t) pset_list,
503 pset_list_len * sizeof (processor_set_t));
508 if ((status = host_processor_sets (port_host_self,
510 &pset_list_len)) != KERN_SUCCESS)
512 ERROR ("host_processor_sets failed: %s\n",
513 mach_error_string (status));
518 /* #endif HAVE_THREAD_INFO */
521 pagesize_g = sysconf(_SC_PAGESIZE);
522 DEBUG ("pagesize_g = %li; CONFIG_HZ = %i;",
523 pagesize_g, CONFIG_HZ);
524 /* #endif KERNEL_LINUX */
526 #elif HAVE_LIBKVM_GETPROCS
527 /* no initialization */
528 #endif /* HAVE_LIBKVM_GETPROCS */
533 /* submit global state (e.g.: qty of zombies, running, etc..) */
534 static void ps_submit_state (const char *state, double value)
537 value_list_t vl = VALUE_LIST_INIT;
539 values[0].gauge = value;
543 vl.time = time (NULL);
544 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
545 sstrncpy (vl.plugin, "processes", sizeof (vl.plugin));
546 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
547 sstrncpy (vl.type, "ps_state", sizeof (vl.type));
548 sstrncpy (vl.type_instance, state, sizeof (vl.type_instance));
550 plugin_dispatch_values (&vl);
553 /* submit info about specific process (e.g.: memory taken, cpu usage, etc..) */
554 static void ps_submit_proc_list (procstat_t *ps)
557 value_list_t vl = VALUE_LIST_INIT;
561 vl.time = time (NULL);
562 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
563 sstrncpy (vl.plugin, "processes", sizeof (vl.plugin));
564 sstrncpy (vl.plugin_instance, ps->name, sizeof (vl.plugin_instance));
566 sstrncpy (vl.type, "ps_rss", sizeof (vl.type));
567 vl.values[0].gauge = ps->vmem_rss;
569 plugin_dispatch_values (&vl);
571 sstrncpy (vl.type, "ps_cputime", sizeof (vl.type));
572 vl.values[0].counter = ps->cpu_user_counter;
573 vl.values[1].counter = ps->cpu_system_counter;
575 plugin_dispatch_values (&vl);
577 sstrncpy (vl.type, "ps_count", sizeof (vl.type));
578 vl.values[0].gauge = ps->num_proc;
579 vl.values[1].gauge = ps->num_lwp;
581 plugin_dispatch_values (&vl);
583 sstrncpy (vl.type, "ps_pagefaults", sizeof (vl.type));
584 vl.values[0].counter = ps->vmem_minflt_counter;
585 vl.values[1].counter = ps->vmem_majflt_counter;
587 plugin_dispatch_values (&vl);
589 DEBUG ("name = %s; num_proc = %lu; num_lwp = %lu; vmem_rss = %lu; "
590 "vmem_minflt_counter = %lu; vmem_majflt_counter = %lu; "
591 "cpu_user_counter = %lu; cpu_system_counter = %lu;",
592 ps->name, ps->num_proc, ps->num_lwp, ps->vmem_rss,
593 ps->vmem_minflt_counter, ps->vmem_majflt_counter,
594 ps->cpu_user_counter, ps->cpu_system_counter);
595 } /* void ps_submit_proc_list */
597 /* ------- additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
599 static int *ps_read_tasks (int pid)
602 int list_size = 1; /* size of allocated space, in elements */
603 int list_len = 0; /* number of currently used elements */
609 ssnprintf (dirname, sizeof (dirname), "/proc/%i/task", pid);
611 if ((dh = opendir (dirname)) == NULL)
613 DEBUG ("Failed to open directory `%s'", dirname);
617 while ((ent = readdir (dh)) != NULL)
619 if (!isdigit (ent->d_name[0]))
622 if ((list_len + 1) >= list_size)
625 int new_size = 2 * list_size;
626 /* Comes in sizes: 2, 4, 8, 16, ... */
628 new_ptr = (int *) realloc (list, (size_t) (sizeof (int) * new_size));
633 ERROR ("processes plugin: "
634 "Failed to allocate more memory.");
639 list_size = new_size;
641 memset (list + list_len, 0, sizeof (int) * (list_size - list_len));
644 list[list_len] = atoi (ent->d_name);
645 if (list[list_len] != 0)
654 assert (list_len < list_size);
655 assert (list[list_len] == 0);
658 } /* int *ps_read_tasks */
660 int ps_read_process (int pid, procstat_t *ps, char *state)
674 long long unsigned cpu_user_counter;
675 long long unsigned cpu_system_counter;
676 long long unsigned vmem_rss;
678 memset (ps, 0, sizeof (procstat_t));
680 ssnprintf (filename, sizeof (filename), "/proc/%i/stat", pid);
682 i = read_file_contents (filename, buffer, sizeof(buffer) - 1);
687 fields_len = strsplit (buffer, fields, 64);
690 DEBUG ("processes plugin: ps_read_process (pid = %i):"
691 " `%s' has only %i fields..",
692 (int) pid, filename, fields_len);
696 /* copy the name, strip brackets in the process */
697 name_len = strlen (fields[1]) - 2;
698 if ((fields[1][0] != '(') || (fields[1][name_len + 1] != ')'))
700 DEBUG ("No brackets found in process name: `%s'", fields[1]);
703 fields[1] = fields[1] + 1;
704 fields[1][name_len] = '\0';
705 strncpy (ps->name, fields[1], PROCSTAT_NAME_LEN);
707 ppid = atoi (fields[3]);
709 *state = fields[2][0];
716 else if ((tasks = ps_read_tasks (pid)) == NULL)
718 /* Kernel 2.4 or so */
726 for (i = 0; tasks[i] != 0; i++)
733 /* Leave the rest at zero if this is only a zombi */
734 if (ps->num_proc == 0)
736 DEBUG ("processes plugin: This is only a zombi: pid = %i; "
737 "name = %s;", pid, ps->name);
741 cpu_user_counter = atoll (fields[13]);
742 cpu_system_counter = atoll (fields[14]);
743 vmem_rss = atoll (fields[23]);
744 ps->vmem_minflt_counter = atol (fields[9]);
745 ps->vmem_majflt_counter = atol (fields[11]);
747 /* Convert jiffies to useconds */
748 cpu_user_counter = cpu_user_counter * 1000000 / CONFIG_HZ;
749 cpu_system_counter = cpu_system_counter * 1000000 / CONFIG_HZ;
750 vmem_rss = vmem_rss * pagesize_g;
752 ps->cpu_user_counter = (unsigned long) cpu_user_counter;
753 ps->cpu_system_counter = (unsigned long) cpu_system_counter;
754 ps->vmem_rss = (unsigned long) vmem_rss;
758 } /* int ps_read_process (...) */
760 static char *ps_get_cmdline (pid_t pid, char *name, char *buf, size_t buf_len)
770 if ((pid < 1) || (NULL == buf) || (buf_len < 2))
773 ssnprintf (file, sizeof (file), "/proc/%u/cmdline", pid);
775 fd = open (file, O_RDONLY);
778 WARNING ("processes plugin: Failed to open `%s': %s.", file,
779 sstrerror (errno, errbuf, sizeof (errbuf)));
791 status = read (fd, (void *)buf_ptr, len);
796 if ((EAGAIN == errno) || (EINTR == errno))
799 WARNING ("processes plugin: Failed to read from `%s': %s.", file,
800 sstrerror (errno, errbuf, sizeof (errbuf)));
820 /* cmdline not available; e.g. kernel thread, zombie */
824 ssnprintf (buf, buf_len, "[%s]", name);
828 assert (n <= buf_len);
835 /* remove trailing whitespace */
836 while ((n > 0) && (isspace (buf[n]) || ('\0' == buf[n]))) {
841 /* arguments are separated by '\0' in /proc/<pid>/cmdline */
848 } /* char *ps_get_cmdline (...) */
849 #endif /* KERNEL_LINUX */
852 static int mach_get_task_name (task_t t, int *pid, char *name, size_t name_max_len)
856 struct kinfo_proc kp;
861 mib[2] = KERN_PROC_PID;
863 if (pid_for_task (t, pid) != KERN_SUCCESS)
867 kp_size = sizeof (kp);
868 if (sysctl (mib, 4, &kp, &kp_size, NULL, 0) != 0)
871 if (name_max_len > (MAXCOMLEN + 1))
872 name_max_len = MAXCOMLEN + 1;
874 strncpy (name, kp.kp_proc.p_comm, name_max_len - 1);
875 name[name_max_len - 1] = '\0';
877 DEBUG ("pid = %i; name = %s;", *pid, name);
879 /* We don't do the special handling for `p_comm == "LaunchCFMApp"' as
880 * `top' does it, because it is a lot of work and only used when
881 * debugging. -octo */
885 #endif /* HAVE_THREAD_INFO */
886 /* ------- end of additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
888 /* do actual readings from kernel */
889 static int ps_read (void)
892 kern_return_t status;
895 processor_set_t port_pset_priv;
898 task_array_t task_list;
899 mach_msg_type_number_t task_list_len;
902 char task_name[MAXCOMLEN + 1];
905 thread_act_array_t thread_list;
906 mach_msg_type_number_t thread_list_len;
907 thread_basic_info_data_t thread_data;
908 mach_msg_type_number_t thread_data_len;
917 procstat_entry_t pse;
922 * The Mach-concept is a little different from the traditional UNIX
923 * concept: All the work is done in threads. Threads are contained in
924 * `tasks'. Therefore, `task status' doesn't make much sense, since
925 * it's actually a `thread status'.
926 * Tasks are assigned to sets of processors, so that's where you go to
929 for (pset = 0; pset < pset_list_len; pset++)
931 if ((status = host_processor_set_priv (port_host_self,
933 &port_pset_priv)) != KERN_SUCCESS)
935 ERROR ("host_processor_set_priv failed: %s\n",
936 mach_error_string (status));
940 if ((status = processor_set_tasks (port_pset_priv,
942 &task_list_len)) != KERN_SUCCESS)
944 ERROR ("processor_set_tasks failed: %s\n",
945 mach_error_string (status));
946 mach_port_deallocate (port_task_self, port_pset_priv);
950 for (task = 0; task < task_list_len; task++)
953 if (mach_get_task_name (task_list[task],
955 task_name, PROCSTAT_NAME_LEN) == 0)
957 /* search for at least one match */
958 for (ps = list_head_g; ps != NULL; ps = ps->next)
959 /* FIXME: cmdline should be here instead of NULL */
960 if (ps_list_match (task_name, NULL, ps) == 1)
964 /* Collect more detailed statistics for this process */
967 task_basic_info_data_t task_basic_info;
968 mach_msg_type_number_t task_basic_info_len;
969 task_events_info_data_t task_events_info;
970 mach_msg_type_number_t task_events_info_len;
971 task_absolutetime_info_data_t task_absolutetime_info;
972 mach_msg_type_number_t task_absolutetime_info_len;
974 memset (&pse, '\0', sizeof (pse));
977 task_basic_info_len = TASK_BASIC_INFO_COUNT;
978 status = task_info (task_list[task],
980 (task_info_t) &task_basic_info,
981 &task_basic_info_len);
982 if (status != KERN_SUCCESS)
984 ERROR ("task_info failed: %s",
985 mach_error_string (status));
986 continue; /* with next thread_list */
989 task_events_info_len = TASK_EVENTS_INFO_COUNT;
990 status = task_info (task_list[task],
992 (task_info_t) &task_events_info,
993 &task_events_info_len);
994 if (status != KERN_SUCCESS)
996 ERROR ("task_info failed: %s",
997 mach_error_string (status));
998 continue; /* with next thread_list */
1001 task_absolutetime_info_len = TASK_ABSOLUTETIME_INFO_COUNT;
1002 status = task_info (task_list[task],
1003 TASK_ABSOLUTETIME_INFO,
1004 (task_info_t) &task_absolutetime_info,
1005 &task_absolutetime_info_len);
1006 if (status != KERN_SUCCESS)
1008 ERROR ("task_info failed: %s",
1009 mach_error_string (status));
1010 continue; /* with next thread_list */
1014 pse.vmem_rss = task_basic_info.resident_size;
1016 pse.vmem_minflt_counter = task_events_info.cow_faults;
1017 pse.vmem_majflt_counter = task_events_info.faults;
1019 pse.cpu_user_counter = task_absolutetime_info.total_user;
1020 pse.cpu_system_counter = task_absolutetime_info.total_system;
1023 status = task_threads (task_list[task], &thread_list,
1025 if (status != KERN_SUCCESS)
1027 /* Apple's `top' treats this case a zombie. It
1028 * makes sense to some extend: A `zombie'
1029 * thread is nonsense, since the task/process
1032 DEBUG ("task_threads failed: %s",
1033 mach_error_string (status));
1034 if (task_list[task] != port_task_self)
1035 mach_port_deallocate (port_task_self,
1037 continue; /* with next task_list */
1040 for (thread = 0; thread < thread_list_len; thread++)
1042 thread_data_len = THREAD_BASIC_INFO_COUNT;
1043 status = thread_info (thread_list[thread],
1045 (thread_info_t) &thread_data,
1047 if (status != KERN_SUCCESS)
1049 ERROR ("thread_info failed: %s",
1050 mach_error_string (status));
1051 if (task_list[task] != port_task_self)
1052 mach_port_deallocate (port_task_self,
1053 thread_list[thread]);
1054 continue; /* with next thread_list */
1060 switch (thread_data.run_state)
1062 case TH_STATE_RUNNING:
1065 case TH_STATE_STOPPED:
1066 /* What exactly is `halted'? */
1067 case TH_STATE_HALTED:
1070 case TH_STATE_WAITING:
1073 case TH_STATE_UNINTERRUPTIBLE:
1076 /* There is no `zombie' case here,
1077 * since there are no zombie-threads.
1078 * There's only zombie tasks, which are
1081 WARNING ("Unknown thread status: %i",
1082 thread_data.run_state);
1084 } /* switch (thread_data.run_state) */
1086 if (task_list[task] != port_task_self)
1088 status = mach_port_deallocate (port_task_self,
1089 thread_list[thread]);
1090 if (status != KERN_SUCCESS)
1091 ERROR ("mach_port_deallocate failed: %s",
1092 mach_error_string (status));
1094 } /* for (thread_list) */
1096 if ((status = vm_deallocate (port_task_self,
1097 (vm_address_t) thread_list,
1098 thread_list_len * sizeof (thread_act_t)))
1101 ERROR ("vm_deallocate failed: %s",
1102 mach_error_string (status));
1105 thread_list_len = 0;
1107 /* Only deallocate the task port, if it isn't our own.
1108 * Don't know what would happen in that case, but this
1109 * is what Apple's top does.. ;) */
1110 if (task_list[task] != port_task_self)
1112 status = mach_port_deallocate (port_task_self,
1114 if (status != KERN_SUCCESS)
1115 ERROR ("mach_port_deallocate failed: %s",
1116 mach_error_string (status));
1120 /* FIXME: cmdline should be here instead of NULL */
1121 ps_list_add (task_name, NULL, &pse);
1122 } /* for (task_list) */
1124 if ((status = vm_deallocate (port_task_self,
1125 (vm_address_t) task_list,
1126 task_list_len * sizeof (task_t))) != KERN_SUCCESS)
1128 ERROR ("vm_deallocate failed: %s",
1129 mach_error_string (status));
1134 if ((status = mach_port_deallocate (port_task_self, port_pset_priv))
1137 ERROR ("mach_port_deallocate failed: %s",
1138 mach_error_string (status));
1140 } /* for (pset_list) */
1142 ps_submit_state ("running", running);
1143 ps_submit_state ("sleeping", sleeping);
1144 ps_submit_state ("zombies", zombies);
1145 ps_submit_state ("stopped", stopped);
1146 ps_submit_state ("blocked", blocked);
1148 for (ps = list_head_g; ps != NULL; ps = ps->next)
1149 ps_submit_proc_list (ps);
1150 /* #endif HAVE_THREAD_INFO */
1164 char cmdline[ARG_MAX];
1168 procstat_entry_t pse;
1173 running = sleeping = zombies = stopped = paging = blocked = 0;
1176 if ((proc = opendir ("/proc")) == NULL)
1179 ERROR ("Cannot open `/proc': %s",
1180 sstrerror (errno, errbuf, sizeof (errbuf)));
1184 while ((ent = readdir (proc)) != NULL)
1186 if (!isdigit (ent->d_name[0]))
1189 if ((pid = atoi (ent->d_name)) < 1)
1192 status = ps_read_process (pid, &ps, &state);
1195 DEBUG ("ps_read_process failed: %i", status);
1202 pse.num_proc = ps.num_proc;
1203 pse.num_lwp = ps.num_lwp;
1204 pse.vmem_rss = ps.vmem_rss;
1206 pse.vmem_minflt = 0;
1207 pse.vmem_minflt_counter = ps.vmem_minflt_counter;
1208 pse.vmem_majflt = 0;
1209 pse.vmem_majflt_counter = ps.vmem_majflt_counter;
1212 pse.cpu_user_counter = ps.cpu_user_counter;
1214 pse.cpu_system_counter = ps.cpu_system_counter;
1218 case 'R': running++; break;
1219 case 'S': sleeping++; break;
1220 case 'D': blocked++; break;
1221 case 'Z': zombies++; break;
1222 case 'T': stopped++; break;
1223 case 'W': paging++; break;
1226 ps_list_add (ps.name,
1227 ps_get_cmdline (pid, ps.name, cmdline, sizeof (cmdline)),
1233 ps_submit_state ("running", running);
1234 ps_submit_state ("sleeping", sleeping);
1235 ps_submit_state ("zombies", zombies);
1236 ps_submit_state ("stopped", stopped);
1237 ps_submit_state ("paging", paging);
1238 ps_submit_state ("blocked", blocked);
1240 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1241 ps_submit_proc_list (ps_ptr);
1242 /* #endif KERNEL_LINUX */
1244 #elif HAVE_LIBKVM_GETPROCS
1255 char cmdline[ARG_MAX];
1257 struct kinfo_proc *procs; /* array of processes */
1259 int count; /* returns number of processes */
1263 procstat_entry_t pse;
1267 /* Open the kvm interface, get a descriptor */
1268 kd = kvm_open (NULL, NULL, NULL, 0, errbuf);
1271 ERROR ("processes plugin: Cannot open kvm interface: %s",
1276 /* Get the list of processes. */
1277 procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, &count);
1281 ERROR ("processes plugin: Cannot get kvm processes list: %s",
1286 /* Iterate through the processes in kinfo_proc */
1287 for (i = 0; i < count; i++)
1289 /* retrieve the arguments */
1293 argv = kvm_getargv (kd, (const struct kinfo_proc *) &(procs[i]), 0);
1300 while (argv[argc] != NULL)
1303 status = strjoin (cmdline, sizeof (cmdline),
1308 WARNING ("processes plugin: Command line did "
1309 "not fit into buffer.");
1313 cmdline_ptr = &cmdline[0];
1317 pse.id = procs[i].ki_pid;
1321 pse.num_lwp = procs[i].ki_numthreads;
1323 pse.vmem_rss = procs[i].ki_rssize * getpagesize();
1324 pse.vmem_minflt = 0;
1325 pse.vmem_minflt_counter = procs[i].ki_rusage.ru_minflt;
1326 pse.vmem_majflt = 0;
1327 pse.vmem_majflt_counter = procs[i].ki_rusage.ru_majflt;
1330 pse.cpu_user_counter = procs[i].ki_rusage.ru_utime.tv_sec
1332 + procs[i].ki_rusage.ru_utime.tv_usec;
1334 pse.cpu_system_counter = procs[i].ki_rusage.ru_stime.tv_sec
1336 + procs[i].ki_rusage.ru_stime.tv_usec;
1338 switch (procs[i].ki_stat)
1340 case SSTOP: stopped++; break;
1341 case SSLEEP: sleeping++; break;
1342 case SRUN: running++; break;
1343 case SIDL: idle++; break;
1344 case SWAIT: wait++; break;
1345 case SLOCK: blocked++; break;
1346 case SZOMB: zombies++; break;
1349 ps_list_add (procs[i].ki_comm, cmdline_ptr, &pse);
1354 ps_submit_state ("running", running);
1355 ps_submit_state ("sleeping", sleeping);
1356 ps_submit_state ("zombies", zombies);
1357 ps_submit_state ("stopped", stopped);
1358 ps_submit_state ("blocked", blocked);
1359 ps_submit_state ("idle", idle);
1360 ps_submit_state ("wait", wait);
1362 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1363 ps_submit_proc_list (ps_ptr);
1364 #endif /* HAVE_LIBKVM_GETPROCS */
1369 void module_register (void)
1371 plugin_register_config ("processes", ps_config,
1372 config_keys, config_keys_num);
1373 plugin_register_init ("processes", ps_init);
1374 plugin_register_read ("processes", ps_read);
1375 } /* void module_register */