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>
30 #include "configfile.h"
32 /* Include header files for the mach system, if they exist.. */
34 # if HAVE_MACH_MACH_INIT_H
35 # include <mach/mach_init.h>
37 # if HAVE_MACH_HOST_PRIV_H
38 # include <mach/host_priv.h>
40 # if HAVE_MACH_MACH_ERROR_H
41 # include <mach/mach_error.h>
43 # if HAVE_MACH_MACH_HOST_H
44 # include <mach/mach_host.h>
46 # if HAVE_MACH_MACH_PORT_H
47 # include <mach/mach_port.h>
49 # if HAVE_MACH_MACH_TYPES_H
50 # include <mach/mach_types.h>
52 # if HAVE_MACH_MESSAGE_H
53 # include <mach/message.h>
55 # if HAVE_MACH_PROCESSOR_SET_H
56 # include <mach/processor_set.h>
59 # include <mach/task.h>
61 # if HAVE_MACH_THREAD_ACT_H
62 # include <mach/thread_act.h>
64 # if HAVE_MACH_VM_REGION_H
65 # include <mach/vm_region.h>
67 # if HAVE_MACH_VM_MAP_H
68 # include <mach/vm_map.h>
70 # if HAVE_MACH_VM_PROT_H
71 # include <mach/vm_prot.h>
73 # if HAVE_SYS_SYSCTL_H
74 # include <sys/sysctl.h>
76 /* #endif HAVE_THREAD_INFO */
79 # if HAVE_LINUX_CONFIG_H
80 # include <linux/config.h>
83 # define CONFIG_HZ 100
85 /* #endif KERNEL_LINUX */
87 #elif HAVE_LIBKVM_GETPROCS
89 # include <sys/user.h>
90 # include <sys/proc.h>
91 # if HAVE_SYS_SYSCTL_H
92 # include <sys/sysctl.h>
94 /* #endif HAVE_LIBKVM_GETPROCS */
97 # error "No applicable input method."
106 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_rss;
123 unsigned long vmem_minflt;
124 unsigned long vmem_majflt;
125 unsigned long vmem_minflt_counter;
126 unsigned long vmem_majflt_counter;
128 unsigned long cpu_user;
129 unsigned long cpu_system;
130 unsigned long cpu_user_counter;
131 unsigned long cpu_system_counter;
133 struct procstat_entry_s *next;
136 #define PROCSTAT_NAME_LEN 256
137 typedef struct procstat
139 char name[PROCSTAT_NAME_LEN];
144 unsigned long num_proc;
145 unsigned long num_lwp;
146 unsigned long vmem_rss;
148 unsigned long vmem_minflt_counter;
149 unsigned long vmem_majflt_counter;
151 unsigned long cpu_user_counter;
152 unsigned long cpu_system_counter;
154 struct procstat *next;
155 struct procstat_entry_s *instances;
158 static procstat_t *list_head_g = NULL;
161 static mach_port_t port_host_self;
162 static mach_port_t port_task_self;
164 static processor_set_name_array_t pset_list;
165 static mach_msg_type_number_t pset_list_len;
166 /* #endif HAVE_THREAD_INFO */
169 static long pagesize_g;
170 /* #endif KERNEL_LINUX */
172 #elif HAVE_LIBKVM_GETPROCS
173 /* no global variables */
174 #endif /* HAVE_LIBKVM_GETPROCS */
176 /* put name of process from config to list_head_g tree
177 list_head_g is a list of 'procstat_t' structs with
178 processes names we want to watch */
179 static void ps_list_register (const char *name, const char *regexp)
185 new = (procstat_t *) malloc (sizeof (procstat_t));
188 ERROR ("processes plugin: ps_list_register: malloc failed.");
191 memset (new, 0, sizeof (procstat_t));
192 sstrncpy (new->name, name, sizeof (new->name));
197 DEBUG ("ProcessMatch: adding \"%s\" as criteria to process %s.", regexp, name);
198 new->re = (regex_t *) malloc (sizeof (regex_t));
201 ERROR ("processes plugin: ps_list_register: malloc failed.");
206 status = regcomp (new->re, regexp, REG_EXTENDED | REG_NOSUB);
209 DEBUG ("ProcessMatch: compiling the regular expression \"%s\" failed.", regexp);
217 ERROR ("processes plugin: ps_list_register: "
218 "Regular expression \"%s\" found in config "
219 "file, but support for regular expressions "
220 "has been dispabled at compile time.",
227 for (ptr = list_head_g; ptr != NULL; ptr = ptr->next)
229 if (strcmp (ptr->name, name) == 0)
231 WARNING ("processes plugin: You have configured more "
232 "than one `Process' or "
233 "`ProcessMatch' with the same name. "
234 "All but the first setting will be "
241 if (ptr->next == NULL)
249 } /* void ps_list_register */
251 /* try to match name against entry, returns 1 if success */
252 static int ps_list_match (const char *name, const char *cmdline, procstat_t *ps)
261 if ((str == NULL) || (str[0] == 0))
264 assert (str != NULL);
266 status = regexec (ps->re, str,
275 if (strcmp (ps->name, name) == 0)
279 } /* int ps_list_match */
281 /* add process entry to 'instances' of process 'name' (or refresh it) */
282 static void ps_list_add (const char *name, const char *cmdline, procstat_entry_t *entry)
285 procstat_entry_t *pse;
290 for (ps = list_head_g; ps != NULL; ps = ps->next)
292 if ((ps_list_match (name, cmdline, ps)) == 0)
295 for (pse = ps->instances; pse != NULL; pse = pse->next)
296 if ((pse->id == entry->id) || (pse->next == NULL))
299 if ((pse == NULL) || (pse->id != entry->id))
301 procstat_entry_t *new;
303 new = (procstat_entry_t *) malloc (sizeof (procstat_entry_t));
306 memset (new, 0, sizeof (procstat_entry_t));
318 pse->num_proc = entry->num_proc;
319 pse->num_lwp = entry->num_lwp;
320 pse->vmem_rss = entry->vmem_rss;
322 ps->num_proc += pse->num_proc;
323 ps->num_lwp += pse->num_lwp;
324 ps->vmem_rss += pse->vmem_rss;
326 if ((entry->vmem_minflt_counter == 0)
327 && (entry->vmem_majflt_counter == 0))
329 pse->vmem_minflt_counter += entry->vmem_minflt;
330 pse->vmem_minflt = entry->vmem_minflt;
332 pse->vmem_majflt_counter += entry->vmem_majflt;
333 pse->vmem_majflt = entry->vmem_majflt;
337 if (entry->vmem_minflt_counter < pse->vmem_minflt_counter)
339 pse->vmem_minflt = entry->vmem_minflt_counter
340 + (ULONG_MAX - pse->vmem_minflt_counter);
344 pse->vmem_minflt = entry->vmem_minflt_counter - pse->vmem_minflt_counter;
346 pse->vmem_minflt_counter = entry->vmem_minflt_counter;
348 if (entry->vmem_majflt_counter < pse->vmem_majflt_counter)
350 pse->vmem_majflt = entry->vmem_majflt_counter
351 + (ULONG_MAX - pse->vmem_majflt_counter);
355 pse->vmem_majflt = entry->vmem_majflt_counter - pse->vmem_majflt_counter;
357 pse->vmem_majflt_counter = entry->vmem_majflt_counter;
360 ps->vmem_minflt_counter += pse->vmem_minflt;
361 ps->vmem_majflt_counter += pse->vmem_majflt;
363 if ((entry->cpu_user_counter == 0)
364 && (entry->cpu_system_counter == 0))
366 pse->cpu_user_counter += entry->cpu_user;
367 pse->cpu_user = entry->cpu_user;
369 pse->cpu_system_counter += entry->cpu_system;
370 pse->cpu_system = entry->cpu_system;
374 if (entry->cpu_user_counter < pse->cpu_user_counter)
376 pse->cpu_user = entry->cpu_user_counter
377 + (ULONG_MAX - pse->cpu_user_counter);
381 pse->cpu_user = entry->cpu_user_counter - pse->cpu_user_counter;
383 pse->cpu_user_counter = entry->cpu_user_counter;
385 if (entry->cpu_system_counter < pse->cpu_system_counter)
387 pse->cpu_system = entry->cpu_system_counter
388 + (ULONG_MAX - pse->cpu_system_counter);
392 pse->cpu_system = entry->cpu_system_counter - pse->cpu_system_counter;
394 pse->cpu_system_counter = entry->cpu_system_counter;
397 ps->cpu_user_counter += pse->cpu_user;
398 ps->cpu_system_counter += pse->cpu_system;
402 /* remove old entries from instances of processes in list_head_g */
403 static void ps_list_reset (void)
406 procstat_entry_t *pse;
407 procstat_entry_t *pse_prev;
409 for (ps = list_head_g; ps != NULL; ps = ps->next)
421 DEBUG ("Removing this procstat entry cause it's too old: "
422 "id = %lu; name = %s;",
425 if (pse_prev == NULL)
427 ps->instances = pse->next;
433 pse_prev->next = pse->next;
435 pse = pse_prev->next;
444 } /* while (pse != NULL) */
445 } /* for (ps = list_head_g; ps != NULL; ps = ps->next) */
448 /* put all pre-defined 'Process' names from config to list_head_g tree */
449 static int ps_config (const char *key, const char *value)
451 if (strcasecmp (key, "Process") == 0)
453 ps_list_register (value, NULL);
455 else if (strcasecmp (key, "ProcessMatch") == 0)
461 new_val = strdup (value);
464 fields_num = strsplit (new_val, fields,
465 STATIC_ARRAY_SIZE (fields));
471 ps_list_register (fields[0], fields[1]);
482 static int ps_init (void)
485 kern_return_t status;
487 port_host_self = mach_host_self ();
488 port_task_self = mach_task_self ();
490 if (pset_list != NULL)
492 vm_deallocate (port_task_self,
493 (vm_address_t) pset_list,
494 pset_list_len * sizeof (processor_set_t));
499 if ((status = host_processor_sets (port_host_self,
501 &pset_list_len)) != KERN_SUCCESS)
503 ERROR ("host_processor_sets failed: %s\n",
504 mach_error_string (status));
509 /* #endif HAVE_THREAD_INFO */
512 pagesize_g = sysconf(_SC_PAGESIZE);
513 DEBUG ("pagesize_g = %li; CONFIG_HZ = %i;",
514 pagesize_g, CONFIG_HZ);
515 /* #endif KERNEL_LINUX */
517 #elif HAVE_LIBKVM_GETPROCS
518 /* no initialization */
519 #endif /* HAVE_LIBKVM_GETPROCS */
524 /* submit global state (e.g.: qty of zombies, running, etc..) */
525 static void ps_submit_state (const char *state, double value)
528 value_list_t vl = VALUE_LIST_INIT;
530 values[0].gauge = value;
534 vl.time = time (NULL);
535 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
536 sstrncpy (vl.plugin, "processes", sizeof (vl.plugin));
537 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
538 sstrncpy (vl.type, "ps_state", sizeof (vl.type));
539 sstrncpy (vl.type_instance, state, sizeof (vl.type_instance));
541 plugin_dispatch_values (&vl);
544 /* submit info about specific process (e.g.: memory taken, cpu usage, etc..) */
545 static void ps_submit_proc_list (procstat_t *ps)
548 value_list_t vl = VALUE_LIST_INIT;
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, ps->name, sizeof (vl.plugin_instance));
557 sstrncpy (vl.type, "ps_rss", sizeof (vl.type));
558 vl.values[0].gauge = ps->vmem_rss;
560 plugin_dispatch_values (&vl);
562 sstrncpy (vl.type, "ps_cputime", sizeof (vl.type));
563 vl.values[0].counter = ps->cpu_user_counter;
564 vl.values[1].counter = ps->cpu_system_counter;
566 plugin_dispatch_values (&vl);
568 sstrncpy (vl.type, "ps_count", sizeof (vl.type));
569 vl.values[0].gauge = ps->num_proc;
570 vl.values[1].gauge = ps->num_lwp;
572 plugin_dispatch_values (&vl);
574 sstrncpy (vl.type, "ps_pagefaults", sizeof (vl.type));
575 vl.values[0].counter = ps->vmem_minflt_counter;
576 vl.values[1].counter = ps->vmem_majflt_counter;
578 plugin_dispatch_values (&vl);
580 DEBUG ("name = %s; num_proc = %lu; num_lwp = %lu; vmem_rss = %lu; "
581 "vmem_minflt_counter = %lu; vmem_majflt_counter = %lu; "
582 "cpu_user_counter = %lu; cpu_system_counter = %lu;",
583 ps->name, ps->num_proc, ps->num_lwp, ps->vmem_rss,
584 ps->vmem_minflt_counter, ps->vmem_majflt_counter,
585 ps->cpu_user_counter, ps->cpu_system_counter);
586 } /* void ps_submit_proc_list */
588 /* ------- additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
590 static int *ps_read_tasks (int pid)
593 int list_size = 1; /* size of allocated space, in elements */
594 int list_len = 0; /* number of currently used elements */
600 ssnprintf (dirname, sizeof (dirname), "/proc/%i/task", pid);
602 if ((dh = opendir (dirname)) == NULL)
604 DEBUG ("Failed to open directory `%s'", dirname);
608 while ((ent = readdir (dh)) != NULL)
610 if (!isdigit (ent->d_name[0]))
613 if ((list_len + 1) >= list_size)
616 int new_size = 2 * list_size;
617 /* Comes in sizes: 2, 4, 8, 16, ... */
619 new_ptr = (int *) realloc (list, (size_t) (sizeof (int) * new_size));
624 ERROR ("processes plugin: "
625 "Failed to allocate more memory.");
630 list_size = new_size;
632 memset (list + list_len, 0, sizeof (int) * (list_size - list_len));
635 list[list_len] = atoi (ent->d_name);
636 if (list[list_len] != 0)
645 assert (list_len < list_size);
646 assert (list[list_len] == 0);
649 } /* int *ps_read_tasks */
651 int ps_read_process (int pid, procstat_t *ps, char *state)
665 long long unsigned cpu_user_counter;
666 long long unsigned cpu_system_counter;
667 long long unsigned vmem_rss;
669 memset (ps, 0, sizeof (procstat_t));
671 ssnprintf (filename, sizeof (filename), "/proc/%i/stat", pid);
673 i = read_file_contents (filename, buffer, sizeof(buffer) - 1);
678 fields_len = strsplit (buffer, fields, 64);
681 DEBUG ("processes plugin: ps_read_process (pid = %i):"
682 " `%s' has only %i fields..",
683 (int) pid, filename, fields_len);
687 /* copy the name, strip brackets in the process */
688 name_len = strlen (fields[1]) - 2;
689 if ((fields[1][0] != '(') || (fields[1][name_len + 1] != ')'))
691 DEBUG ("No brackets found in process name: `%s'", fields[1]);
694 fields[1] = fields[1] + 1;
695 fields[1][name_len] = '\0';
696 strncpy (ps->name, fields[1], PROCSTAT_NAME_LEN);
698 ppid = atoi (fields[3]);
700 *state = fields[2][0];
707 else if ((tasks = ps_read_tasks (pid)) == NULL)
709 /* Kernel 2.4 or so */
717 for (i = 0; tasks[i] != 0; i++)
724 /* Leave the rest at zero if this is only a zombi */
725 if (ps->num_proc == 0)
727 DEBUG ("processes plugin: This is only a zombi: pid = %i; "
728 "name = %s;", pid, ps->name);
732 cpu_user_counter = atoll (fields[13]);
733 cpu_system_counter = atoll (fields[14]);
734 vmem_rss = atoll (fields[23]);
735 ps->vmem_minflt_counter = atol (fields[9]);
736 ps->vmem_majflt_counter = atol (fields[11]);
738 /* Convert jiffies to useconds */
739 cpu_user_counter = cpu_user_counter * 1000000 / CONFIG_HZ;
740 cpu_system_counter = cpu_system_counter * 1000000 / CONFIG_HZ;
741 vmem_rss = vmem_rss * pagesize_g;
743 ps->cpu_user_counter = (unsigned long) cpu_user_counter;
744 ps->cpu_system_counter = (unsigned long) cpu_system_counter;
745 ps->vmem_rss = (unsigned long) vmem_rss;
749 } /* int ps_read_process (...) */
750 #endif /* KERNEL_LINUX */
753 static int mach_get_task_name (task_t t, int *pid, char *name, size_t name_max_len)
757 struct kinfo_proc kp;
762 mib[2] = KERN_PROC_PID;
764 if (pid_for_task (t, pid) != KERN_SUCCESS)
768 kp_size = sizeof (kp);
769 if (sysctl (mib, 4, &kp, &kp_size, NULL, 0) != 0)
772 if (name_max_len > (MAXCOMLEN + 1))
773 name_max_len = MAXCOMLEN + 1;
775 strncpy (name, kp.kp_proc.p_comm, name_max_len - 1);
776 name[name_max_len - 1] = '\0';
778 DEBUG ("pid = %i; name = %s;", *pid, name);
780 /* We don't do the special handling for `p_comm == "LaunchCFMApp"' as
781 * `top' does it, because it is a lot of work and only used when
782 * debugging. -octo */
786 #endif /* HAVE_THREAD_INFO */
787 /* ------- end of additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
789 /* do actual readings from kernel */
790 static int ps_read (void)
793 kern_return_t status;
796 processor_set_t port_pset_priv;
799 task_array_t task_list;
800 mach_msg_type_number_t task_list_len;
803 char task_name[MAXCOMLEN + 1];
806 thread_act_array_t thread_list;
807 mach_msg_type_number_t thread_list_len;
808 thread_basic_info_data_t thread_data;
809 mach_msg_type_number_t thread_data_len;
818 procstat_entry_t pse;
823 * The Mach-concept is a little different from the traditional UNIX
824 * concept: All the work is done in threads. Threads are contained in
825 * `tasks'. Therefore, `task status' doesn't make much sense, since
826 * it's actually a `thread status'.
827 * Tasks are assigned to sets of processors, so that's where you go to
830 for (pset = 0; pset < pset_list_len; pset++)
832 if ((status = host_processor_set_priv (port_host_self,
834 &port_pset_priv)) != KERN_SUCCESS)
836 ERROR ("host_processor_set_priv failed: %s\n",
837 mach_error_string (status));
841 if ((status = processor_set_tasks (port_pset_priv,
843 &task_list_len)) != KERN_SUCCESS)
845 ERROR ("processor_set_tasks failed: %s\n",
846 mach_error_string (status));
847 mach_port_deallocate (port_task_self, port_pset_priv);
851 for (task = 0; task < task_list_len; task++)
854 if (mach_get_task_name (task_list[task],
856 task_name, PROCSTAT_NAME_LEN) == 0)
858 /* search for at least one match */
859 for (ps = list_head_g; ps != NULL; ps = ps->next)
860 /* FIXME: cmdline should be here instead of NULL */
861 if (ps_list_match (task_name, NULL, ps) == 1)
865 /* Collect more detailed statistics for this process */
868 task_basic_info_data_t task_basic_info;
869 mach_msg_type_number_t task_basic_info_len;
870 task_events_info_data_t task_events_info;
871 mach_msg_type_number_t task_events_info_len;
872 task_absolutetime_info_data_t task_absolutetime_info;
873 mach_msg_type_number_t task_absolutetime_info_len;
875 memset (&pse, '\0', sizeof (pse));
878 task_basic_info_len = TASK_BASIC_INFO_COUNT;
879 status = task_info (task_list[task],
881 (task_info_t) &task_basic_info,
882 &task_basic_info_len);
883 if (status != KERN_SUCCESS)
885 ERROR ("task_info failed: %s",
886 mach_error_string (status));
887 continue; /* with next thread_list */
890 task_events_info_len = TASK_EVENTS_INFO_COUNT;
891 status = task_info (task_list[task],
893 (task_info_t) &task_events_info,
894 &task_events_info_len);
895 if (status != KERN_SUCCESS)
897 ERROR ("task_info failed: %s",
898 mach_error_string (status));
899 continue; /* with next thread_list */
902 task_absolutetime_info_len = TASK_ABSOLUTETIME_INFO_COUNT;
903 status = task_info (task_list[task],
904 TASK_ABSOLUTETIME_INFO,
905 (task_info_t) &task_absolutetime_info,
906 &task_absolutetime_info_len);
907 if (status != KERN_SUCCESS)
909 ERROR ("task_info failed: %s",
910 mach_error_string (status));
911 continue; /* with next thread_list */
915 pse.vmem_rss = task_basic_info.resident_size;
917 pse.vmem_minflt_counter = task_events_info.cow_faults;
918 pse.vmem_majflt_counter = task_events_info.faults;
920 pse.cpu_user_counter = task_absolutetime_info.total_user;
921 pse.cpu_system_counter = task_absolutetime_info.total_system;
924 status = task_threads (task_list[task], &thread_list,
926 if (status != KERN_SUCCESS)
928 /* Apple's `top' treats this case a zombie. It
929 * makes sense to some extend: A `zombie'
930 * thread is nonsense, since the task/process
933 DEBUG ("task_threads failed: %s",
934 mach_error_string (status));
935 if (task_list[task] != port_task_self)
936 mach_port_deallocate (port_task_self,
938 continue; /* with next task_list */
941 for (thread = 0; thread < thread_list_len; thread++)
943 thread_data_len = THREAD_BASIC_INFO_COUNT;
944 status = thread_info (thread_list[thread],
946 (thread_info_t) &thread_data,
948 if (status != KERN_SUCCESS)
950 ERROR ("thread_info failed: %s",
951 mach_error_string (status));
952 if (task_list[task] != port_task_self)
953 mach_port_deallocate (port_task_self,
954 thread_list[thread]);
955 continue; /* with next thread_list */
961 switch (thread_data.run_state)
963 case TH_STATE_RUNNING:
966 case TH_STATE_STOPPED:
967 /* What exactly is `halted'? */
968 case TH_STATE_HALTED:
971 case TH_STATE_WAITING:
974 case TH_STATE_UNINTERRUPTIBLE:
977 /* There is no `zombie' case here,
978 * since there are no zombie-threads.
979 * There's only zombie tasks, which are
982 WARNING ("Unknown thread status: %i",
983 thread_data.run_state);
985 } /* switch (thread_data.run_state) */
987 if (task_list[task] != port_task_self)
989 status = mach_port_deallocate (port_task_self,
990 thread_list[thread]);
991 if (status != KERN_SUCCESS)
992 ERROR ("mach_port_deallocate failed: %s",
993 mach_error_string (status));
995 } /* for (thread_list) */
997 if ((status = vm_deallocate (port_task_self,
998 (vm_address_t) thread_list,
999 thread_list_len * sizeof (thread_act_t)))
1002 ERROR ("vm_deallocate failed: %s",
1003 mach_error_string (status));
1006 thread_list_len = 0;
1008 /* Only deallocate the task port, if it isn't our own.
1009 * Don't know what would happen in that case, but this
1010 * is what Apple's top does.. ;) */
1011 if (task_list[task] != port_task_self)
1013 status = mach_port_deallocate (port_task_self,
1015 if (status != KERN_SUCCESS)
1016 ERROR ("mach_port_deallocate failed: %s",
1017 mach_error_string (status));
1021 /* FIXME: cmdline should be here instead of NULL */
1022 ps_list_add (task_name, NULL, &pse);
1023 } /* for (task_list) */
1025 if ((status = vm_deallocate (port_task_self,
1026 (vm_address_t) task_list,
1027 task_list_len * sizeof (task_t))) != KERN_SUCCESS)
1029 ERROR ("vm_deallocate failed: %s",
1030 mach_error_string (status));
1035 if ((status = mach_port_deallocate (port_task_self, port_pset_priv))
1038 ERROR ("mach_port_deallocate failed: %s",
1039 mach_error_string (status));
1041 } /* for (pset_list) */
1043 ps_submit_state ("running", running);
1044 ps_submit_state ("sleeping", sleeping);
1045 ps_submit_state ("zombies", zombies);
1046 ps_submit_state ("stopped", stopped);
1047 ps_submit_state ("blocked", blocked);
1049 for (ps = list_head_g; ps != NULL; ps = ps->next)
1050 ps_submit_proc_list (ps);
1051 /* #endif HAVE_THREAD_INFO */
1067 procstat_entry_t pse;
1072 running = sleeping = zombies = stopped = paging = blocked = 0;
1075 if ((proc = opendir ("/proc")) == NULL)
1078 ERROR ("Cannot open `/proc': %s",
1079 sstrerror (errno, errbuf, sizeof (errbuf)));
1083 while ((ent = readdir (proc)) != NULL)
1085 if (!isdigit (ent->d_name[0]))
1088 if ((pid = atoi (ent->d_name)) < 1)
1091 status = ps_read_process (pid, &ps, &state);
1094 DEBUG ("ps_read_process failed: %i", status);
1101 pse.num_proc = ps.num_proc;
1102 pse.num_lwp = ps.num_lwp;
1103 pse.vmem_rss = ps.vmem_rss;
1105 pse.vmem_minflt = 0;
1106 pse.vmem_minflt_counter = ps.vmem_minflt_counter;
1107 pse.vmem_majflt = 0;
1108 pse.vmem_majflt_counter = ps.vmem_majflt_counter;
1111 pse.cpu_user_counter = ps.cpu_user_counter;
1113 pse.cpu_system_counter = ps.cpu_system_counter;
1117 case 'R': running++; break;
1118 case 'S': sleeping++; break;
1119 case 'D': blocked++; break;
1120 case 'Z': zombies++; break;
1121 case 'T': stopped++; break;
1122 case 'W': paging++; break;
1125 /* FIXME: cmdline should be here instead of NULL */
1126 ps_list_add (ps.name, NULL, &pse);
1131 ps_submit_state ("running", running);
1132 ps_submit_state ("sleeping", sleeping);
1133 ps_submit_state ("zombies", zombies);
1134 ps_submit_state ("stopped", stopped);
1135 ps_submit_state ("paging", paging);
1136 ps_submit_state ("blocked", blocked);
1138 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1139 ps_submit_proc_list (ps_ptr);
1140 /* #endif KERNEL_LINUX */
1142 #elif HAVE_LIBKVM_GETPROCS
1153 char cmdline[ARG_MAX];
1155 struct kinfo_proc *procs; /* array of processes */
1157 int count; /* returns number of processes */
1161 procstat_entry_t pse;
1165 /* Open the kvm interface, get a descriptor */
1166 kd = kvm_open (NULL, NULL, NULL, 0, errbuf);
1169 ERROR ("processes plugin: Cannot open kvm interface: %s",
1174 /* Get the list of processes. */
1175 procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, &count);
1179 ERROR ("processes plugin: Cannot get kvm processes list: %s",
1184 /* Iterate through the processes in kinfo_proc */
1185 for (i = 0; i < count; i++)
1187 /* retrieve the arguments */
1191 argv = kvm_getargv (kd, (const struct kinfo_proc *) &(procs[i]), 0);
1198 while (argv[argc] != NULL)
1201 status = strjoin (cmdline, sizeof (cmdline),
1206 WARNING ("processes plugin: Command line did "
1207 "not fit into buffer.");
1211 cmdline_ptr = &cmdline[0];
1215 pse.id = procs[i].ki_pid;
1219 pse.num_lwp = procs[i].ki_numthreads;
1221 pse.vmem_rss = procs[i].ki_rssize * getpagesize();
1222 pse.vmem_minflt = 0;
1223 pse.vmem_minflt_counter = procs[i].ki_rusage.ru_minflt;
1224 pse.vmem_majflt = 0;
1225 pse.vmem_majflt_counter = procs[i].ki_rusage.ru_majflt;
1228 pse.cpu_user_counter = procs[i].ki_rusage.ru_utime.tv_sec
1230 + procs[i].ki_rusage.ru_utime.tv_usec;
1232 pse.cpu_system_counter = procs[i].ki_rusage.ru_stime.tv_sec
1234 + procs[i].ki_rusage.ru_stime.tv_usec;
1236 switch (procs[i].ki_stat)
1238 case SSTOP: stopped++; break;
1239 case SSLEEP: sleeping++; break;
1240 case SRUN: running++; break;
1241 case SIDL: idle++; break;
1242 case SWAIT: wait++; break;
1243 case SLOCK: blocked++; break;
1244 case SZOMB: zombies++; break;
1247 ps_list_add (procs[i].ki_comm, cmdline_ptr, &pse);
1252 ps_submit_state ("running", running);
1253 ps_submit_state ("sleeping", sleeping);
1254 ps_submit_state ("zombies", zombies);
1255 ps_submit_state ("stopped", stopped);
1256 ps_submit_state ("blocked", blocked);
1257 ps_submit_state ("idle", idle);
1258 ps_submit_state ("wait", wait);
1260 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1261 ps_submit_proc_list (ps_ptr);
1262 #endif /* HAVE_LIBKVM_GETPROCS */
1267 void module_register (void)
1269 plugin_register_config ("processes", ps_config,
1270 config_keys, config_keys_num);
1271 plugin_register_init ("processes", ps_init);
1272 plugin_register_read ("processes", ps_read);
1273 } /* void module_register */