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[] =
112 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
114 typedef struct procstat_entry_s
119 unsigned long num_proc;
120 unsigned long num_lwp;
121 unsigned long vmem_size;
122 unsigned long vmem_rss;
123 unsigned long stack_size;
125 unsigned long vmem_minflt;
126 unsigned long vmem_majflt;
127 unsigned long vmem_minflt_counter;
128 unsigned long vmem_majflt_counter;
130 unsigned long cpu_user;
131 unsigned long cpu_system;
132 unsigned long cpu_user_counter;
133 unsigned long cpu_system_counter;
135 struct procstat_entry_s *next;
138 #define PROCSTAT_NAME_LEN 256
139 typedef struct procstat
141 char name[PROCSTAT_NAME_LEN];
146 unsigned long num_proc;
147 unsigned long num_lwp;
148 unsigned long vmem_size;
149 unsigned long vmem_rss;
150 unsigned long stack_size;
152 unsigned long vmem_minflt_counter;
153 unsigned long vmem_majflt_counter;
155 unsigned long cpu_user_counter;
156 unsigned long cpu_system_counter;
158 struct procstat *next;
159 struct procstat_entry_s *instances;
162 static procstat_t *list_head_g = NULL;
165 static mach_port_t port_host_self;
166 static mach_port_t port_task_self;
168 static processor_set_name_array_t pset_list;
169 static mach_msg_type_number_t pset_list_len;
170 /* #endif HAVE_THREAD_INFO */
173 static long pagesize_g;
174 /* #endif KERNEL_LINUX */
176 #elif HAVE_LIBKVM_GETPROCS
177 /* no global variables */
178 #endif /* HAVE_LIBKVM_GETPROCS */
180 /* put name of process from config to list_head_g tree
181 list_head_g is a list of 'procstat_t' structs with
182 processes names we want to watch */
183 static void ps_list_register (const char *name, const char *regexp)
189 new = (procstat_t *) malloc (sizeof (procstat_t));
192 ERROR ("processes plugin: ps_list_register: malloc failed.");
195 memset (new, 0, sizeof (procstat_t));
196 sstrncpy (new->name, name, sizeof (new->name));
201 DEBUG ("ProcessMatch: adding \"%s\" as criteria to process %s.", regexp, name);
202 new->re = (regex_t *) malloc (sizeof (regex_t));
205 ERROR ("processes plugin: ps_list_register: malloc failed.");
210 status = regcomp (new->re, regexp, REG_EXTENDED | REG_NOSUB);
213 DEBUG ("ProcessMatch: compiling the regular expression \"%s\" failed.", regexp);
221 ERROR ("processes plugin: ps_list_register: "
222 "Regular expression \"%s\" found in config "
223 "file, but support for regular expressions "
224 "has been dispabled at compile time.",
231 for (ptr = list_head_g; ptr != NULL; ptr = ptr->next)
233 if (strcmp (ptr->name, name) == 0)
235 WARNING ("processes plugin: You have configured more "
236 "than one `Process' or "
237 "`ProcessMatch' with the same name. "
238 "All but the first setting will be "
245 if (ptr->next == NULL)
253 } /* void ps_list_register */
255 /* try to match name against entry, returns 1 if success */
256 static int ps_list_match (const char *name, const char *cmdline, procstat_t *ps)
265 if ((str == NULL) || (str[0] == 0))
268 assert (str != NULL);
270 status = regexec (ps->re, str,
279 if (strcmp (ps->name, name) == 0)
283 } /* int ps_list_match */
285 /* add process entry to 'instances' of process 'name' (or refresh it) */
286 static void ps_list_add (const char *name, const char *cmdline, procstat_entry_t *entry)
289 procstat_entry_t *pse;
294 for (ps = list_head_g; ps != NULL; ps = ps->next)
296 if ((ps_list_match (name, cmdline, ps)) == 0)
299 for (pse = ps->instances; pse != NULL; pse = pse->next)
300 if ((pse->id == entry->id) || (pse->next == NULL))
303 if ((pse == NULL) || (pse->id != entry->id))
305 procstat_entry_t *new;
307 new = (procstat_entry_t *) malloc (sizeof (procstat_entry_t));
310 memset (new, 0, sizeof (procstat_entry_t));
322 pse->num_proc = entry->num_proc;
323 pse->num_lwp = entry->num_lwp;
324 pse->vmem_size = entry->vmem_size;
325 pse->vmem_rss = entry->vmem_rss;
326 pse->stack_size = entry->stack_size;
328 ps->num_proc += pse->num_proc;
329 ps->num_lwp += pse->num_lwp;
330 ps->vmem_size += pse->vmem_size;
331 ps->vmem_rss += pse->vmem_rss;
332 ps->stack_size += pse->stack_size;
334 if ((entry->vmem_minflt_counter == 0)
335 && (entry->vmem_majflt_counter == 0))
337 pse->vmem_minflt_counter += entry->vmem_minflt;
338 pse->vmem_minflt = entry->vmem_minflt;
340 pse->vmem_majflt_counter += entry->vmem_majflt;
341 pse->vmem_majflt = entry->vmem_majflt;
345 if (entry->vmem_minflt_counter < pse->vmem_minflt_counter)
347 pse->vmem_minflt = entry->vmem_minflt_counter
348 + (ULONG_MAX - pse->vmem_minflt_counter);
352 pse->vmem_minflt = entry->vmem_minflt_counter - pse->vmem_minflt_counter;
354 pse->vmem_minflt_counter = entry->vmem_minflt_counter;
356 if (entry->vmem_majflt_counter < pse->vmem_majflt_counter)
358 pse->vmem_majflt = entry->vmem_majflt_counter
359 + (ULONG_MAX - pse->vmem_majflt_counter);
363 pse->vmem_majflt = entry->vmem_majflt_counter - pse->vmem_majflt_counter;
365 pse->vmem_majflt_counter = entry->vmem_majflt_counter;
368 ps->vmem_minflt_counter += pse->vmem_minflt;
369 ps->vmem_majflt_counter += pse->vmem_majflt;
371 if ((entry->cpu_user_counter == 0)
372 && (entry->cpu_system_counter == 0))
374 pse->cpu_user_counter += entry->cpu_user;
375 pse->cpu_user = entry->cpu_user;
377 pse->cpu_system_counter += entry->cpu_system;
378 pse->cpu_system = entry->cpu_system;
382 if (entry->cpu_user_counter < pse->cpu_user_counter)
384 pse->cpu_user = entry->cpu_user_counter
385 + (ULONG_MAX - pse->cpu_user_counter);
389 pse->cpu_user = entry->cpu_user_counter - pse->cpu_user_counter;
391 pse->cpu_user_counter = entry->cpu_user_counter;
393 if (entry->cpu_system_counter < pse->cpu_system_counter)
395 pse->cpu_system = entry->cpu_system_counter
396 + (ULONG_MAX - pse->cpu_system_counter);
400 pse->cpu_system = entry->cpu_system_counter - pse->cpu_system_counter;
402 pse->cpu_system_counter = entry->cpu_system_counter;
405 ps->cpu_user_counter += pse->cpu_user;
406 ps->cpu_system_counter += pse->cpu_system;
410 /* remove old entries from instances of processes in list_head_g */
411 static void ps_list_reset (void)
414 procstat_entry_t *pse;
415 procstat_entry_t *pse_prev;
417 for (ps = list_head_g; ps != NULL; ps = ps->next)
431 DEBUG ("Removing this procstat entry cause it's too old: "
432 "id = %lu; name = %s;",
435 if (pse_prev == NULL)
437 ps->instances = pse->next;
443 pse_prev->next = pse->next;
445 pse = pse_prev->next;
454 } /* while (pse != NULL) */
455 } /* for (ps = list_head_g; ps != NULL; ps = ps->next) */
458 /* put all pre-defined 'Process' names from config to list_head_g tree */
459 static int ps_config (const char *key, const char *value)
461 if (strcasecmp (key, "Process") == 0)
463 ps_list_register (value, NULL);
465 else if (strcasecmp (key, "ProcessMatch") == 0)
471 new_val = strdup (value);
472 if (new_val == NULL) {
473 ERROR ("processes plugin: strdup failed when processing "
474 "`ProcessMatch %s'.", value);
478 fields_num = strsplit (new_val, fields,
479 STATIC_ARRAY_SIZE (fields));
482 ERROR ("processes plugin: `ProcessMatch' needs exactly "
483 "two string arguments.");
487 ps_list_register (fields[0], fields[1]);
492 ERROR ("processes plugin: The `%s' configuration option is not "
493 "understood and will be ignored.", key);
500 static int ps_init (void)
503 kern_return_t status;
505 port_host_self = mach_host_self ();
506 port_task_self = mach_task_self ();
508 if (pset_list != NULL)
510 vm_deallocate (port_task_self,
511 (vm_address_t) pset_list,
512 pset_list_len * sizeof (processor_set_t));
517 if ((status = host_processor_sets (port_host_self,
519 &pset_list_len)) != KERN_SUCCESS)
521 ERROR ("host_processor_sets failed: %s\n",
522 mach_error_string (status));
527 /* #endif HAVE_THREAD_INFO */
530 pagesize_g = sysconf(_SC_PAGESIZE);
531 DEBUG ("pagesize_g = %li; CONFIG_HZ = %i;",
532 pagesize_g, CONFIG_HZ);
533 /* #endif KERNEL_LINUX */
535 #elif HAVE_LIBKVM_GETPROCS
536 /* no initialization */
537 #endif /* HAVE_LIBKVM_GETPROCS */
542 /* submit global state (e.g.: qty of zombies, running, etc..) */
543 static void ps_submit_state (const char *state, double value)
546 value_list_t vl = VALUE_LIST_INIT;
548 values[0].gauge = value;
552 vl.time = time (NULL);
553 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
554 sstrncpy (vl.plugin, "processes", sizeof (vl.plugin));
555 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
556 sstrncpy (vl.type, "ps_state", sizeof (vl.type));
557 sstrncpy (vl.type_instance, state, sizeof (vl.type_instance));
559 plugin_dispatch_values (&vl);
562 /* submit info about specific process (e.g.: memory taken, cpu usage, etc..) */
563 static void ps_submit_proc_list (procstat_t *ps)
566 value_list_t vl = VALUE_LIST_INIT;
570 vl.time = time (NULL);
571 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
572 sstrncpy (vl.plugin, "processes", sizeof (vl.plugin));
573 sstrncpy (vl.plugin_instance, ps->name, sizeof (vl.plugin_instance));
575 sstrncpy (vl.type, "ps_vm", sizeof (vl.type));
576 vl.values[0].gauge = ps->vmem_size;
578 plugin_dispatch_values (&vl);
580 sstrncpy (vl.type, "ps_rss", sizeof (vl.type));
581 vl.values[0].gauge = ps->vmem_rss;
583 plugin_dispatch_values (&vl);
585 sstrncpy (vl.type, "ps_stacksize", sizeof (vl.type));
586 vl.values[0].gauge = ps->stack_size;
588 plugin_dispatch_values (&vl);
590 sstrncpy (vl.type, "ps_cputime", sizeof (vl.type));
591 vl.values[0].counter = ps->cpu_user_counter;
592 vl.values[1].counter = ps->cpu_system_counter;
594 plugin_dispatch_values (&vl);
596 sstrncpy (vl.type, "ps_count", sizeof (vl.type));
597 vl.values[0].gauge = ps->num_proc;
598 vl.values[1].gauge = ps->num_lwp;
600 plugin_dispatch_values (&vl);
602 sstrncpy (vl.type, "ps_pagefaults", sizeof (vl.type));
603 vl.values[0].counter = ps->vmem_minflt_counter;
604 vl.values[1].counter = ps->vmem_majflt_counter;
606 plugin_dispatch_values (&vl);
608 DEBUG ("name = %s; num_proc = %lu; num_lwp = %lu; vmem_rss = %lu; "
609 "vmem_minflt_counter = %lu; vmem_majflt_counter = %lu; "
610 "cpu_user_counter = %lu; cpu_system_counter = %lu;",
611 ps->name, ps->num_proc, ps->num_lwp, ps->vmem_rss,
612 ps->vmem_minflt_counter, ps->vmem_majflt_counter,
613 ps->cpu_user_counter, ps->cpu_system_counter);
614 } /* void ps_submit_proc_list */
616 /* ------- additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
618 static int *ps_read_tasks (int pid)
621 int list_size = 1; /* size of allocated space, in elements */
622 int list_len = 0; /* number of currently used elements */
628 ssnprintf (dirname, sizeof (dirname), "/proc/%i/task", pid);
630 if ((dh = opendir (dirname)) == NULL)
632 DEBUG ("Failed to open directory `%s'", dirname);
636 while ((ent = readdir (dh)) != NULL)
638 if (!isdigit (ent->d_name[0]))
641 if ((list_len + 1) >= list_size)
644 int new_size = 2 * list_size;
645 /* Comes in sizes: 2, 4, 8, 16, ... */
647 new_ptr = (int *) realloc (list, (size_t) (sizeof (int) * new_size));
652 ERROR ("processes plugin: "
653 "Failed to allocate more memory.");
658 list_size = new_size;
660 memset (list + list_len, 0, sizeof (int) * (list_size - list_len));
663 list[list_len] = atoi (ent->d_name);
664 if (list[list_len] != 0)
673 assert (list_len < list_size);
674 assert (list[list_len] == 0);
677 } /* int *ps_read_tasks */
679 int ps_read_process (int pid, procstat_t *ps, char *state)
693 long long unsigned cpu_user_counter;
694 long long unsigned cpu_system_counter;
695 long long unsigned vmem_size;
696 long long unsigned vmem_rss;
697 long long unsigned stack_size;
699 memset (ps, 0, sizeof (procstat_t));
701 ssnprintf (filename, sizeof (filename), "/proc/%i/stat", pid);
703 i = read_file_contents (filename, buffer, sizeof(buffer) - 1);
708 fields_len = strsplit (buffer, fields, 64);
711 DEBUG ("processes plugin: ps_read_process (pid = %i):"
712 " `%s' has only %i fields..",
713 (int) pid, filename, fields_len);
717 /* copy the name, strip brackets in the process */
718 name_len = strlen (fields[1]) - 2;
719 if ((fields[1][0] != '(') || (fields[1][name_len + 1] != ')'))
721 DEBUG ("No brackets found in process name: `%s'", fields[1]);
724 fields[1] = fields[1] + 1;
725 fields[1][name_len] = '\0';
726 strncpy (ps->name, fields[1], PROCSTAT_NAME_LEN);
728 ppid = atoi (fields[3]);
730 *state = fields[2][0];
737 else if ((tasks = ps_read_tasks (pid)) == NULL)
739 /* Kernel 2.4 or so */
747 for (i = 0; tasks[i] != 0; i++)
754 /* Leave the rest at zero if this is only a zombi */
755 if (ps->num_proc == 0)
757 DEBUG ("processes plugin: This is only a zombi: pid = %i; "
758 "name = %s;", pid, ps->name);
762 cpu_user_counter = atoll (fields[13]);
763 cpu_system_counter = atoll (fields[14]);
764 vmem_size = atoll (fields[22]);
765 vmem_rss = atoll (fields[23]);
766 ps->vmem_minflt_counter = atol (fields[9]);
767 ps->vmem_majflt_counter = atol (fields[11]);
770 unsigned long long stack_start = atoll (fields[27]);
771 unsigned long long stack_ptr = atoll (fields[28]);
773 stack_size = (stack_start > stack_ptr)
774 ? stack_start - stack_ptr
775 : stack_ptr - stack_start;
778 /* Convert jiffies to useconds */
779 cpu_user_counter = cpu_user_counter * 1000000 / CONFIG_HZ;
780 cpu_system_counter = cpu_system_counter * 1000000 / CONFIG_HZ;
781 vmem_rss = vmem_rss * pagesize_g;
783 ps->cpu_user_counter = (unsigned long) cpu_user_counter;
784 ps->cpu_system_counter = (unsigned long) cpu_system_counter;
785 ps->vmem_size = (unsigned long) vmem_size;
786 ps->vmem_rss = (unsigned long) vmem_rss;
787 ps->stack_size = (unsigned long) stack_size;
791 } /* int ps_read_process (...) */
793 static char *ps_get_cmdline (pid_t pid, char *name, char *buf, size_t buf_len)
803 if ((pid < 1) || (NULL == buf) || (buf_len < 2))
806 ssnprintf (file, sizeof (file), "/proc/%u/cmdline", pid);
808 fd = open (file, O_RDONLY);
811 WARNING ("processes plugin: Failed to open `%s': %s.", file,
812 sstrerror (errno, errbuf, sizeof (errbuf)));
824 status = read (fd, (void *)buf_ptr, len);
829 if ((EAGAIN == errno) || (EINTR == errno))
832 WARNING ("processes plugin: Failed to read from `%s': %s.", file,
833 sstrerror (errno, errbuf, sizeof (errbuf)));
853 /* cmdline not available; e.g. kernel thread, zombie */
857 ssnprintf (buf, buf_len, "[%s]", name);
861 assert (n <= buf_len);
868 /* remove trailing whitespace */
869 while ((n > 0) && (isspace (buf[n]) || ('\0' == buf[n]))) {
874 /* arguments are separated by '\0' in /proc/<pid>/cmdline */
881 } /* char *ps_get_cmdline (...) */
882 #endif /* KERNEL_LINUX */
885 static int mach_get_task_name (task_t t, int *pid, char *name, size_t name_max_len)
889 struct kinfo_proc kp;
894 mib[2] = KERN_PROC_PID;
896 if (pid_for_task (t, pid) != KERN_SUCCESS)
900 kp_size = sizeof (kp);
901 if (sysctl (mib, 4, &kp, &kp_size, NULL, 0) != 0)
904 if (name_max_len > (MAXCOMLEN + 1))
905 name_max_len = MAXCOMLEN + 1;
907 strncpy (name, kp.kp_proc.p_comm, name_max_len - 1);
908 name[name_max_len - 1] = '\0';
910 DEBUG ("pid = %i; name = %s;", *pid, name);
912 /* We don't do the special handling for `p_comm == "LaunchCFMApp"' as
913 * `top' does it, because it is a lot of work and only used when
914 * debugging. -octo */
918 #endif /* HAVE_THREAD_INFO */
919 /* ------- end of additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
921 /* do actual readings from kernel */
922 static int ps_read (void)
925 kern_return_t status;
928 processor_set_t port_pset_priv;
931 task_array_t task_list;
932 mach_msg_type_number_t task_list_len;
935 char task_name[MAXCOMLEN + 1];
938 thread_act_array_t thread_list;
939 mach_msg_type_number_t thread_list_len;
940 thread_basic_info_data_t thread_data;
941 mach_msg_type_number_t thread_data_len;
950 procstat_entry_t pse;
955 * The Mach-concept is a little different from the traditional UNIX
956 * concept: All the work is done in threads. Threads are contained in
957 * `tasks'. Therefore, `task status' doesn't make much sense, since
958 * it's actually a `thread status'.
959 * Tasks are assigned to sets of processors, so that's where you go to
962 for (pset = 0; pset < pset_list_len; pset++)
964 if ((status = host_processor_set_priv (port_host_self,
966 &port_pset_priv)) != KERN_SUCCESS)
968 ERROR ("host_processor_set_priv failed: %s\n",
969 mach_error_string (status));
973 if ((status = processor_set_tasks (port_pset_priv,
975 &task_list_len)) != KERN_SUCCESS)
977 ERROR ("processor_set_tasks failed: %s\n",
978 mach_error_string (status));
979 mach_port_deallocate (port_task_self, port_pset_priv);
983 for (task = 0; task < task_list_len; task++)
986 if (mach_get_task_name (task_list[task],
988 task_name, PROCSTAT_NAME_LEN) == 0)
990 /* search for at least one match */
991 for (ps = list_head_g; ps != NULL; ps = ps->next)
992 /* FIXME: cmdline should be here instead of NULL */
993 if (ps_list_match (task_name, NULL, ps) == 1)
997 /* Collect more detailed statistics for this process */
1000 task_basic_info_data_t task_basic_info;
1001 mach_msg_type_number_t task_basic_info_len;
1002 task_events_info_data_t task_events_info;
1003 mach_msg_type_number_t task_events_info_len;
1004 task_absolutetime_info_data_t task_absolutetime_info;
1005 mach_msg_type_number_t task_absolutetime_info_len;
1007 memset (&pse, '\0', sizeof (pse));
1010 task_basic_info_len = TASK_BASIC_INFO_COUNT;
1011 status = task_info (task_list[task],
1013 (task_info_t) &task_basic_info,
1014 &task_basic_info_len);
1015 if (status != KERN_SUCCESS)
1017 ERROR ("task_info failed: %s",
1018 mach_error_string (status));
1019 continue; /* with next thread_list */
1022 task_events_info_len = TASK_EVENTS_INFO_COUNT;
1023 status = task_info (task_list[task],
1025 (task_info_t) &task_events_info,
1026 &task_events_info_len);
1027 if (status != KERN_SUCCESS)
1029 ERROR ("task_info failed: %s",
1030 mach_error_string (status));
1031 continue; /* with next thread_list */
1034 task_absolutetime_info_len = TASK_ABSOLUTETIME_INFO_COUNT;
1035 status = task_info (task_list[task],
1036 TASK_ABSOLUTETIME_INFO,
1037 (task_info_t) &task_absolutetime_info,
1038 &task_absolutetime_info_len);
1039 if (status != KERN_SUCCESS)
1041 ERROR ("task_info failed: %s",
1042 mach_error_string (status));
1043 continue; /* with next thread_list */
1047 pse.vmem_rss = task_basic_info.resident_size;
1049 pse.vmem_minflt_counter = task_events_info.cow_faults;
1050 pse.vmem_majflt_counter = task_events_info.faults;
1052 pse.cpu_user_counter = task_absolutetime_info.total_user;
1053 pse.cpu_system_counter = task_absolutetime_info.total_system;
1056 status = task_threads (task_list[task], &thread_list,
1058 if (status != KERN_SUCCESS)
1060 /* Apple's `top' treats this case a zombie. It
1061 * makes sense to some extend: A `zombie'
1062 * thread is nonsense, since the task/process
1065 DEBUG ("task_threads failed: %s",
1066 mach_error_string (status));
1067 if (task_list[task] != port_task_self)
1068 mach_port_deallocate (port_task_self,
1070 continue; /* with next task_list */
1073 for (thread = 0; thread < thread_list_len; thread++)
1075 thread_data_len = THREAD_BASIC_INFO_COUNT;
1076 status = thread_info (thread_list[thread],
1078 (thread_info_t) &thread_data,
1080 if (status != KERN_SUCCESS)
1082 ERROR ("thread_info failed: %s",
1083 mach_error_string (status));
1084 if (task_list[task] != port_task_self)
1085 mach_port_deallocate (port_task_self,
1086 thread_list[thread]);
1087 continue; /* with next thread_list */
1093 switch (thread_data.run_state)
1095 case TH_STATE_RUNNING:
1098 case TH_STATE_STOPPED:
1099 /* What exactly is `halted'? */
1100 case TH_STATE_HALTED:
1103 case TH_STATE_WAITING:
1106 case TH_STATE_UNINTERRUPTIBLE:
1109 /* There is no `zombie' case here,
1110 * since there are no zombie-threads.
1111 * There's only zombie tasks, which are
1114 WARNING ("Unknown thread status: %i",
1115 thread_data.run_state);
1117 } /* switch (thread_data.run_state) */
1119 if (task_list[task] != port_task_self)
1121 status = mach_port_deallocate (port_task_self,
1122 thread_list[thread]);
1123 if (status != KERN_SUCCESS)
1124 ERROR ("mach_port_deallocate failed: %s",
1125 mach_error_string (status));
1127 } /* for (thread_list) */
1129 if ((status = vm_deallocate (port_task_self,
1130 (vm_address_t) thread_list,
1131 thread_list_len * sizeof (thread_act_t)))
1134 ERROR ("vm_deallocate failed: %s",
1135 mach_error_string (status));
1138 thread_list_len = 0;
1140 /* Only deallocate the task port, if it isn't our own.
1141 * Don't know what would happen in that case, but this
1142 * is what Apple's top does.. ;) */
1143 if (task_list[task] != port_task_self)
1145 status = mach_port_deallocate (port_task_self,
1147 if (status != KERN_SUCCESS)
1148 ERROR ("mach_port_deallocate failed: %s",
1149 mach_error_string (status));
1153 /* FIXME: cmdline should be here instead of NULL */
1154 ps_list_add (task_name, NULL, &pse);
1155 } /* for (task_list) */
1157 if ((status = vm_deallocate (port_task_self,
1158 (vm_address_t) task_list,
1159 task_list_len * sizeof (task_t))) != KERN_SUCCESS)
1161 ERROR ("vm_deallocate failed: %s",
1162 mach_error_string (status));
1167 if ((status = mach_port_deallocate (port_task_self, port_pset_priv))
1170 ERROR ("mach_port_deallocate failed: %s",
1171 mach_error_string (status));
1173 } /* for (pset_list) */
1175 ps_submit_state ("running", running);
1176 ps_submit_state ("sleeping", sleeping);
1177 ps_submit_state ("zombies", zombies);
1178 ps_submit_state ("stopped", stopped);
1179 ps_submit_state ("blocked", blocked);
1181 for (ps = list_head_g; ps != NULL; ps = ps->next)
1182 ps_submit_proc_list (ps);
1183 /* #endif HAVE_THREAD_INFO */
1197 char cmdline[ARG_MAX];
1201 procstat_entry_t pse;
1206 running = sleeping = zombies = stopped = paging = blocked = 0;
1209 if ((proc = opendir ("/proc")) == NULL)
1212 ERROR ("Cannot open `/proc': %s",
1213 sstrerror (errno, errbuf, sizeof (errbuf)));
1217 while ((ent = readdir (proc)) != NULL)
1219 if (!isdigit (ent->d_name[0]))
1222 if ((pid = atoi (ent->d_name)) < 1)
1225 status = ps_read_process (pid, &ps, &state);
1228 DEBUG ("ps_read_process failed: %i", status);
1235 pse.num_proc = ps.num_proc;
1236 pse.num_lwp = ps.num_lwp;
1237 pse.vmem_size = ps.vmem_size;
1238 pse.vmem_rss = ps.vmem_rss;
1239 pse.stack_size = ps.stack_size;
1241 pse.vmem_minflt = 0;
1242 pse.vmem_minflt_counter = ps.vmem_minflt_counter;
1243 pse.vmem_majflt = 0;
1244 pse.vmem_majflt_counter = ps.vmem_majflt_counter;
1247 pse.cpu_user_counter = ps.cpu_user_counter;
1249 pse.cpu_system_counter = ps.cpu_system_counter;
1253 case 'R': running++; break;
1254 case 'S': sleeping++; break;
1255 case 'D': blocked++; break;
1256 case 'Z': zombies++; break;
1257 case 'T': stopped++; break;
1258 case 'W': paging++; break;
1261 ps_list_add (ps.name,
1262 ps_get_cmdline (pid, ps.name, cmdline, sizeof (cmdline)),
1268 ps_submit_state ("running", running);
1269 ps_submit_state ("sleeping", sleeping);
1270 ps_submit_state ("zombies", zombies);
1271 ps_submit_state ("stopped", stopped);
1272 ps_submit_state ("paging", paging);
1273 ps_submit_state ("blocked", blocked);
1275 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1276 ps_submit_proc_list (ps_ptr);
1277 /* #endif KERNEL_LINUX */
1279 #elif HAVE_LIBKVM_GETPROCS
1290 char cmdline[ARG_MAX];
1292 struct kinfo_proc *procs; /* array of processes */
1294 int count; /* returns number of processes */
1298 procstat_entry_t pse;
1302 /* Open the kvm interface, get a descriptor */
1303 kd = kvm_open (NULL, NULL, NULL, 0, errbuf);
1306 ERROR ("processes plugin: Cannot open kvm interface: %s",
1311 /* Get the list of processes. */
1312 procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, &count);
1316 ERROR ("processes plugin: Cannot get kvm processes list: %s",
1321 /* Iterate through the processes in kinfo_proc */
1322 for (i = 0; i < count; i++)
1324 /* retrieve the arguments */
1328 argv = kvm_getargv (kd, (const struct kinfo_proc *) &(procs[i]), 0);
1335 while (argv[argc] != NULL)
1338 status = strjoin (cmdline, sizeof (cmdline),
1343 WARNING ("processes plugin: Command line did "
1344 "not fit into buffer.");
1348 cmdline_ptr = &cmdline[0];
1352 pse.id = procs[i].ki_pid;
1356 pse.num_lwp = procs[i].ki_numthreads;
1358 pse.vmem_rss = procs[i].ki_rssize * getpagesize();
1359 pse.vmem_minflt = 0;
1360 pse.vmem_minflt_counter = procs[i].ki_rusage.ru_minflt;
1361 pse.vmem_majflt = 0;
1362 pse.vmem_majflt_counter = procs[i].ki_rusage.ru_majflt;
1365 pse.cpu_user_counter = procs[i].ki_rusage.ru_utime.tv_sec
1367 + procs[i].ki_rusage.ru_utime.tv_usec;
1369 pse.cpu_system_counter = procs[i].ki_rusage.ru_stime.tv_sec
1371 + procs[i].ki_rusage.ru_stime.tv_usec;
1373 switch (procs[i].ki_stat)
1375 case SSTOP: stopped++; break;
1376 case SSLEEP: sleeping++; break;
1377 case SRUN: running++; break;
1378 case SIDL: idle++; break;
1379 case SWAIT: wait++; break;
1380 case SLOCK: blocked++; break;
1381 case SZOMB: zombies++; break;
1384 ps_list_add (procs[i].ki_comm, cmdline_ptr, &pse);
1389 ps_submit_state ("running", running);
1390 ps_submit_state ("sleeping", sleeping);
1391 ps_submit_state ("zombies", zombies);
1392 ps_submit_state ("stopped", stopped);
1393 ps_submit_state ("blocked", blocked);
1394 ps_submit_state ("idle", idle);
1395 ps_submit_state ("wait", wait);
1397 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1398 ps_submit_proc_list (ps_ptr);
1399 #endif /* HAVE_LIBKVM_GETPROCS */
1404 void module_register (void)
1406 plugin_register_config ("processes", ps_config,
1407 config_keys, config_keys_num);
1408 plugin_register_init ("processes", ps_init);
1409 plugin_register_read ("processes", ps_read);
1410 } /* void module_register */