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 */
89 # include <sys/user.h>
90 # include <sys/proc.h>
91 # if HAVE_SYS_SYSCTL_H
92 # include <sys/sysctl.h>
94 /* #endif HAVE_KVM_H */
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 /* put name of process from config to list_head_g tree
173 list_head_g is a list of 'procstat_t' structs with
174 processes names we want to watch */
175 static void ps_list_register (const char *name, const char *regexp)
181 new = (procstat_t *) malloc (sizeof (procstat_t));
184 ERROR ("processes plugin: ps_list_register: malloc failed.");
187 memset (new, 0, sizeof (procstat_t));
188 sstrncpy (new->name, name, sizeof (new->name));
193 DEBUG ("ProcessMatch: adding \"%s\" as criteria to process %s.", regexp, name);
194 new->re = (regex_t *) malloc (sizeof (regex_t));
197 ERROR ("processes plugin: ps_list_register: malloc failed.");
202 status = regcomp (new->re, regexp, REG_EXTENDED | REG_NOSUB);
205 DEBUG ("ProcessMatch: compiling the regular expression \"%s\" failed.", regexp);
213 ERROR ("processes plugin: ps_list_register: "
214 "Regular expression \"%s\" found in config "
215 "file, but support for regular expressions "
216 "has been dispabled at compile time.",
223 for (ptr = list_head_g; ptr != NULL; ptr = ptr->next)
225 if (strcmp (ptr->name, name) == 0)
227 WARNING ("processes plugin: You have configured more "
228 "than one `Process' or "
229 "`ProcessMatch' with the same name. "
230 "All but the first setting will be "
237 if (ptr->next == NULL)
245 } /* void ps_list_register */
247 /* try to match name against entry, returns 1 if success */
248 static int ps_list_match (const char *name, const char *cmdline, procstat_t *ps)
257 if ((str == NULL) || (str[0] == 0))
260 assert (str != NULL);
262 status = regexec (ps->re, str,
271 if (strcmp (ps->name, name) == 0)
275 } /* int ps_list_match */
277 /* add process entry to 'instances' of process 'name' (or refresh it) */
278 static void ps_list_add (const char *name, const char *cmdline, procstat_entry_t *entry)
281 procstat_entry_t *pse;
286 for (ps = list_head_g; ps != NULL; ps = ps->next)
288 if ((ps_list_match (name, cmdline, ps)) == 0)
291 for (pse = ps->instances; pse != NULL; pse = pse->next)
292 if ((pse->id == entry->id) || (pse->next == NULL))
295 if ((pse == NULL) || (pse->id != entry->id))
297 procstat_entry_t *new;
299 new = (procstat_entry_t *) malloc (sizeof (procstat_entry_t));
302 memset (new, 0, sizeof (procstat_entry_t));
314 pse->num_proc = entry->num_proc;
315 pse->num_lwp = entry->num_lwp;
316 pse->vmem_rss = entry->vmem_rss;
318 ps->num_proc += pse->num_proc;
319 ps->num_lwp += pse->num_lwp;
320 ps->vmem_rss += pse->vmem_rss;
322 if ((entry->vmem_minflt_counter == 0)
323 && (entry->vmem_majflt_counter == 0))
325 pse->vmem_minflt_counter += entry->vmem_minflt;
326 pse->vmem_minflt = entry->vmem_minflt;
328 pse->vmem_majflt_counter += entry->vmem_majflt;
329 pse->vmem_majflt = entry->vmem_majflt;
333 if (entry->vmem_minflt_counter < pse->vmem_minflt_counter)
335 pse->vmem_minflt = entry->vmem_minflt_counter
336 + (ULONG_MAX - pse->vmem_minflt_counter);
340 pse->vmem_minflt = entry->vmem_minflt_counter - pse->vmem_minflt_counter;
342 pse->vmem_minflt_counter = entry->vmem_minflt_counter;
344 if (entry->vmem_majflt_counter < pse->vmem_majflt_counter)
346 pse->vmem_majflt = entry->vmem_majflt_counter
347 + (ULONG_MAX - pse->vmem_majflt_counter);
351 pse->vmem_majflt = entry->vmem_majflt_counter - pse->vmem_majflt_counter;
353 pse->vmem_majflt_counter = entry->vmem_majflt_counter;
356 ps->vmem_minflt_counter += pse->vmem_minflt;
357 ps->vmem_majflt_counter += pse->vmem_majflt;
359 if ((entry->cpu_user_counter == 0)
360 && (entry->cpu_system_counter == 0))
362 pse->cpu_user_counter += entry->cpu_user;
363 pse->cpu_user = entry->cpu_user;
365 pse->cpu_system_counter += entry->cpu_system;
366 pse->cpu_system = entry->cpu_system;
370 if (entry->cpu_user_counter < pse->cpu_user_counter)
372 pse->cpu_user = entry->cpu_user_counter
373 + (ULONG_MAX - pse->cpu_user_counter);
377 pse->cpu_user = entry->cpu_user_counter - pse->cpu_user_counter;
379 pse->cpu_user_counter = entry->cpu_user_counter;
381 if (entry->cpu_system_counter < pse->cpu_system_counter)
383 pse->cpu_system = entry->cpu_system_counter
384 + (ULONG_MAX - pse->cpu_system_counter);
388 pse->cpu_system = entry->cpu_system_counter - pse->cpu_system_counter;
390 pse->cpu_system_counter = entry->cpu_system_counter;
393 ps->cpu_user_counter += pse->cpu_user;
394 ps->cpu_system_counter += pse->cpu_system;
398 /* remove old entries from instances of processes in list_head_g */
399 static void ps_list_reset (void)
402 procstat_entry_t *pse;
403 procstat_entry_t *pse_prev;
405 for (ps = list_head_g; ps != NULL; ps = ps->next)
417 DEBUG ("Removing this procstat entry cause it's too old: "
418 "id = %lu; name = %s;",
421 if (pse_prev == NULL)
423 ps->instances = pse->next;
429 pse_prev->next = pse->next;
431 pse = pse_prev->next;
440 } /* while (pse != NULL) */
441 } /* for (ps = list_head_g; ps != NULL; ps = ps->next) */
444 /* put all pre-defined 'Process' names from config to list_head_g tree */
445 static int ps_config (const char *key, const char *value)
447 if (strcasecmp (key, "Process") == 0)
449 ps_list_register (value, NULL);
451 else if (strcasecmp (key, "ProcessMatch") == 0)
457 new_val = strdup (value);
460 fields_num = strsplit (new_val, fields,
461 STATIC_ARRAY_SIZE (fields));
467 ps_list_register (fields[0], fields[1]);
478 static int ps_init (void)
481 kern_return_t status;
483 port_host_self = mach_host_self ();
484 port_task_self = mach_task_self ();
486 if (pset_list != NULL)
488 vm_deallocate (port_task_self,
489 (vm_address_t) pset_list,
490 pset_list_len * sizeof (processor_set_t));
495 if ((status = host_processor_sets (port_host_self,
497 &pset_list_len)) != KERN_SUCCESS)
499 ERROR ("host_processor_sets failed: %s\n",
500 mach_error_string (status));
505 /* #endif HAVE_THREAD_INFO */
508 pagesize_g = sysconf(_SC_PAGESIZE);
509 DEBUG ("pagesize_g = %li; CONFIG_HZ = %i;",
510 pagesize_g, CONFIG_HZ);
511 #endif /* KERNEL_LINUX */
516 /* submit global state (e.g.: qty of zombies, running, etc..) */
517 static void ps_submit_state (const char *state, double value)
520 value_list_t vl = VALUE_LIST_INIT;
522 values[0].gauge = value;
526 vl.time = time (NULL);
527 strcpy (vl.host, hostname_g);
528 strcpy (vl.plugin, "processes");
529 strcpy (vl.plugin_instance, "");
530 strcpy (vl.type, "ps_state");
531 sstrncpy (vl.type_instance, state, sizeof (vl.type_instance));
533 plugin_dispatch_values (&vl);
536 /* submit info about specific process (e.g.: memory taken, cpu usage, etc..) */
537 static void ps_submit_proc_list (procstat_t *ps)
540 value_list_t vl = VALUE_LIST_INIT;
544 vl.time = time (NULL);
545 strcpy (vl.host, hostname_g);
546 strcpy (vl.plugin, "processes");
547 sstrncpy (vl.plugin_instance, ps->name, sizeof (vl.plugin_instance));
549 strcpy (vl.type, "ps_rss");
550 vl.values[0].gauge = ps->vmem_rss;
552 plugin_dispatch_values (&vl);
554 strcpy (vl.type, "ps_cputime");
555 vl.values[0].counter = ps->cpu_user_counter;
556 vl.values[1].counter = ps->cpu_system_counter;
558 plugin_dispatch_values (&vl);
560 strcpy (vl.type, "ps_count");
561 vl.values[0].gauge = ps->num_proc;
562 vl.values[1].gauge = ps->num_lwp;
564 plugin_dispatch_values (&vl);
566 strcpy (vl.type, "ps_pagefaults");
567 vl.values[0].counter = ps->vmem_minflt_counter;
568 vl.values[1].counter = ps->vmem_majflt_counter;
570 plugin_dispatch_values (&vl);
572 DEBUG ("name = %s; num_proc = %lu; num_lwp = %lu; vmem_rss = %lu; "
573 "vmem_minflt_counter = %lu; vmem_majflt_counter = %lu; "
574 "cpu_user_counter = %lu; cpu_system_counter = %lu;",
575 ps->name, ps->num_proc, ps->num_lwp, ps->vmem_rss,
576 ps->vmem_minflt_counter, ps->vmem_majflt_counter,
577 ps->cpu_user_counter, ps->cpu_system_counter);
578 } /* void ps_submit_proc_list */
580 /* ------- additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
582 static int *ps_read_tasks (int pid)
585 int list_size = 1; /* size of allocated space, in elements */
586 int list_len = 0; /* number of currently used elements */
592 ssnprintf (dirname, sizeof (dirname), "/proc/%i/task", pid);
594 if ((dh = opendir (dirname)) == NULL)
596 DEBUG ("Failed to open directory `%s'", dirname);
600 while ((ent = readdir (dh)) != NULL)
602 if (!isdigit (ent->d_name[0]))
605 if ((list_len + 1) >= list_size)
608 int new_size = 2 * list_size;
609 /* Comes in sizes: 2, 4, 8, 16, ... */
611 new_ptr = (int *) realloc (list, (size_t) (sizeof (int) * new_size));
616 ERROR ("processes plugin: "
617 "Failed to allocate more memory.");
622 list_size = new_size;
624 memset (list + list_len, 0, sizeof (int) * (list_size - list_len));
627 list[list_len] = atoi (ent->d_name);
628 if (list[list_len] != 0)
637 assert (list_len < list_size);
638 assert (list[list_len] == 0);
641 } /* int *ps_read_tasks */
643 int ps_read_process (int pid, procstat_t *ps, char *state)
658 long long unsigned cpu_user_counter;
659 long long unsigned cpu_system_counter;
660 long long unsigned vmem_rss;
662 memset (ps, 0, sizeof (procstat_t));
664 ssnprintf (filename, sizeof (filename), "/proc/%i/stat", pid);
666 if ((fh = fopen (filename, "r")) == NULL)
669 if (fgets (buffer, 1024, fh) == NULL)
677 fields_len = strsplit (buffer, fields, 64);
680 DEBUG ("processes plugin: ps_read_process (pid = %i):"
681 " `%s' has only %i fields..",
682 (int) pid, filename, fields_len);
686 /* copy the name, strip brackets in the process */
687 name_len = strlen (fields[1]) - 2;
688 if ((fields[1][0] != '(') || (fields[1][name_len + 1] != ')'))
690 DEBUG ("No brackets found in process name: `%s'", fields[1]);
693 fields[1] = fields[1] + 1;
694 fields[1][name_len] = '\0';
695 strncpy (ps->name, fields[1], PROCSTAT_NAME_LEN);
697 ppid = atoi (fields[3]);
699 *state = fields[2][0];
706 else if ((tasks = ps_read_tasks (pid)) == NULL)
708 /* Kernel 2.4 or so */
716 for (i = 0; tasks[i] != 0; i++)
723 /* Leave the rest at zero if this is only a zombi */
724 if (ps->num_proc == 0)
726 DEBUG ("processes plugin: This is only a zombi: pid = %i; "
727 "name = %s;", pid, ps->name);
731 cpu_user_counter = atoll (fields[13]);
732 cpu_system_counter = atoll (fields[14]);
733 vmem_rss = atoll (fields[23]);
734 ps->vmem_minflt_counter = atol (fields[9]);
735 ps->vmem_majflt_counter = atol (fields[11]);
737 /* Convert jiffies to useconds */
738 cpu_user_counter = cpu_user_counter * 1000000 / CONFIG_HZ;
739 cpu_system_counter = cpu_system_counter * 1000000 / CONFIG_HZ;
740 vmem_rss = vmem_rss * pagesize_g;
742 ps->cpu_user_counter = (unsigned long) cpu_user_counter;
743 ps->cpu_system_counter = (unsigned long) cpu_system_counter;
744 ps->vmem_rss = (unsigned long) vmem_rss;
748 } /* int ps_read_process (...) */
749 #endif /* KERNEL_LINUX */
752 static int mach_get_task_name (task_t t, int *pid, char *name, size_t name_max_len)
756 struct kinfo_proc kp;
761 mib[2] = KERN_PROC_PID;
763 if (pid_for_task (t, pid) != KERN_SUCCESS)
767 kp_size = sizeof (kp);
768 if (sysctl (mib, 4, &kp, &kp_size, NULL, 0) != 0)
771 if (name_max_len > (MAXCOMLEN + 1))
772 name_max_len = MAXCOMLEN + 1;
774 strncpy (name, kp.kp_proc.p_comm, name_max_len - 1);
775 name[name_max_len - 1] = '\0';
777 DEBUG ("pid = %i; name = %s;", *pid, name);
779 /* We don't do the special handling for `p_comm == "LaunchCFMApp"' as
780 * `top' does it, because it is a lot of work and only used when
781 * debugging. -octo */
785 #endif /* HAVE_THREAD_INFO */
786 /* ------- end of additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
788 /* do actual readings from kernel */
789 static int ps_read (void)
792 kern_return_t status;
795 processor_set_t port_pset_priv;
798 task_array_t task_list;
799 mach_msg_type_number_t task_list_len;
802 char task_name[MAXCOMLEN + 1];
805 thread_act_array_t thread_list;
806 mach_msg_type_number_t thread_list_len;
807 thread_basic_info_data_t thread_data;
808 mach_msg_type_number_t thread_data_len;
817 procstat_entry_t pse;
822 * The Mach-concept is a little different from the traditional UNIX
823 * concept: All the work is done in threads. Threads are contained in
824 * `tasks'. Therefore, `task status' doesn't make much sense, since
825 * it's actually a `thread status'.
826 * Tasks are assigned to sets of processors, so that's where you go to
829 for (pset = 0; pset < pset_list_len; pset++)
831 if ((status = host_processor_set_priv (port_host_self,
833 &port_pset_priv)) != KERN_SUCCESS)
835 ERROR ("host_processor_set_priv failed: %s\n",
836 mach_error_string (status));
840 if ((status = processor_set_tasks (port_pset_priv,
842 &task_list_len)) != KERN_SUCCESS)
844 ERROR ("processor_set_tasks failed: %s\n",
845 mach_error_string (status));
846 mach_port_deallocate (port_task_self, port_pset_priv);
850 for (task = 0; task < task_list_len; task++)
853 if (mach_get_task_name (task_list[task],
855 task_name, PROCSTAT_NAME_LEN) == 0)
857 /* search for at least one match */
858 for (ps = list_head_g; ps != NULL; ps = ps->next)
859 /* FIXME: cmdline should be here instead of NULL */
860 if (ps_list_match (task_name, NULL, ps) == 1)
864 /* Collect more detailed statistics for this process */
867 task_basic_info_data_t task_basic_info;
868 mach_msg_type_number_t task_basic_info_len;
869 task_events_info_data_t task_events_info;
870 mach_msg_type_number_t task_events_info_len;
871 task_absolutetime_info_data_t task_absolutetime_info;
872 mach_msg_type_number_t task_absolutetime_info_len;
874 memset (&pse, '\0', sizeof (pse));
877 task_basic_info_len = TASK_BASIC_INFO_COUNT;
878 status = task_info (task_list[task],
880 (task_info_t) &task_basic_info,
881 &task_basic_info_len);
882 if (status != KERN_SUCCESS)
884 ERROR ("task_info failed: %s",
885 mach_error_string (status));
886 continue; /* with next thread_list */
889 task_events_info_len = TASK_EVENTS_INFO_COUNT;
890 status = task_info (task_list[task],
892 (task_info_t) &task_events_info,
893 &task_events_info_len);
894 if (status != KERN_SUCCESS)
896 ERROR ("task_info failed: %s",
897 mach_error_string (status));
898 continue; /* with next thread_list */
901 task_absolutetime_info_len = TASK_ABSOLUTETIME_INFO_COUNT;
902 status = task_info (task_list[task],
903 TASK_ABSOLUTETIME_INFO,
904 (task_info_t) &task_absolutetime_info,
905 &task_absolutetime_info_len);
906 if (status != KERN_SUCCESS)
908 ERROR ("task_info failed: %s",
909 mach_error_string (status));
910 continue; /* with next thread_list */
914 pse.vmem_rss = task_basic_info.resident_size;
916 pse.vmem_minflt_counter = task_events_info.cow_faults;
917 pse.vmem_majflt_counter = task_events_info.faults;
919 pse.cpu_user_counter = task_absolutetime_info.total_user;
920 pse.cpu_system_counter = task_absolutetime_info.total_system;
923 status = task_threads (task_list[task], &thread_list,
925 if (status != KERN_SUCCESS)
927 /* Apple's `top' treats this case a zombie. It
928 * makes sense to some extend: A `zombie'
929 * thread is nonsense, since the task/process
932 DEBUG ("task_threads failed: %s",
933 mach_error_string (status));
934 if (task_list[task] != port_task_self)
935 mach_port_deallocate (port_task_self,
937 continue; /* with next task_list */
940 for (thread = 0; thread < thread_list_len; thread++)
942 thread_data_len = THREAD_BASIC_INFO_COUNT;
943 status = thread_info (thread_list[thread],
945 (thread_info_t) &thread_data,
947 if (status != KERN_SUCCESS)
949 ERROR ("thread_info failed: %s",
950 mach_error_string (status));
951 if (task_list[task] != port_task_self)
952 mach_port_deallocate (port_task_self,
953 thread_list[thread]);
954 continue; /* with next thread_list */
960 switch (thread_data.run_state)
962 case TH_STATE_RUNNING:
965 case TH_STATE_STOPPED:
966 /* What exactly is `halted'? */
967 case TH_STATE_HALTED:
970 case TH_STATE_WAITING:
973 case TH_STATE_UNINTERRUPTIBLE:
976 /* There is no `zombie' case here,
977 * since there are no zombie-threads.
978 * There's only zombie tasks, which are
981 WARNING ("Unknown thread status: %s",
982 thread_data.run_state);
984 } /* switch (thread_data.run_state) */
986 if (task_list[task] != port_task_self)
988 status = mach_port_deallocate (port_task_self,
989 thread_list[thread]);
990 if (status != KERN_SUCCESS)
991 ERROR ("mach_port_deallocate failed: %s",
992 mach_error_string (status));
994 } /* for (thread_list) */
996 if ((status = vm_deallocate (port_task_self,
997 (vm_address_t) thread_list,
998 thread_list_len * sizeof (thread_act_t)))
1001 ERROR ("vm_deallocate failed: %s",
1002 mach_error_string (status));
1005 thread_list_len = 0;
1007 /* Only deallocate the task port, if it isn't our own.
1008 * Don't know what would happen in that case, but this
1009 * is what Apple's top does.. ;) */
1010 if (task_list[task] != port_task_self)
1012 status = mach_port_deallocate (port_task_self,
1014 if (status != KERN_SUCCESS)
1015 ERROR ("mach_port_deallocate failed: %s",
1016 mach_error_string (status));
1020 /* FIXME: cmdline should be here instead of NULL */
1021 ps_list_add (task_name, NULL, &pse);
1022 } /* for (task_list) */
1024 if ((status = vm_deallocate (port_task_self,
1025 (vm_address_t) task_list,
1026 task_list_len * sizeof (task_t))) != KERN_SUCCESS)
1028 ERROR ("vm_deallocate failed: %s",
1029 mach_error_string (status));
1034 if ((status = mach_port_deallocate (port_task_self, port_pset_priv))
1037 ERROR ("mach_port_deallocate failed: %s",
1038 mach_error_string (status));
1040 } /* for (pset_list) */
1042 ps_submit_state ("running", running);
1043 ps_submit_state ("sleeping", sleeping);
1044 ps_submit_state ("zombies", zombies);
1045 ps_submit_state ("stopped", stopped);
1046 ps_submit_state ("blocked", blocked);
1048 for (ps = list_head_g; ps != NULL; ps = ps->next)
1049 ps_submit_proc_list (ps);
1050 /* #endif HAVE_THREAD_INFO */
1066 procstat_entry_t pse;
1071 running = sleeping = zombies = stopped = paging = blocked = 0;
1074 if ((proc = opendir ("/proc")) == NULL)
1077 ERROR ("Cannot open `/proc': %s",
1078 sstrerror (errno, errbuf, sizeof (errbuf)));
1082 while ((ent = readdir (proc)) != NULL)
1084 if (!isdigit (ent->d_name[0]))
1087 if ((pid = atoi (ent->d_name)) < 1)
1090 status = ps_read_process (pid, &ps, &state);
1093 DEBUG ("ps_read_process failed: %i", status);
1100 pse.num_proc = ps.num_proc;
1101 pse.num_lwp = ps.num_lwp;
1102 pse.vmem_rss = ps.vmem_rss;
1104 pse.vmem_minflt = 0;
1105 pse.vmem_minflt_counter = ps.vmem_minflt_counter;
1106 pse.vmem_majflt = 0;
1107 pse.vmem_majflt_counter = ps.vmem_majflt_counter;
1110 pse.cpu_user_counter = ps.cpu_user_counter;
1112 pse.cpu_system_counter = ps.cpu_system_counter;
1116 case 'R': running++; break;
1117 case 'S': sleeping++; break;
1118 case 'D': blocked++; break;
1119 case 'Z': zombies++; break;
1120 case 'T': stopped++; break;
1121 case 'W': paging++; break;
1124 /* FIXME: cmdline should be here instead of NULL */
1125 ps_list_add (ps.name, NULL, &pse);
1130 ps_submit_state ("running", running);
1131 ps_submit_state ("sleeping", sleeping);
1132 ps_submit_state ("zombies", zombies);
1133 ps_submit_state ("stopped", stopped);
1134 ps_submit_state ("paging", paging);
1135 ps_submit_state ("blocked", blocked);
1137 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1138 ps_submit_proc_list (ps_ptr);
1139 /* #endif KERNEL_LINUX */
1152 char cmdline[ARG_MAX];
1154 struct kinfo_proc *procs; /* array of processes */
1156 int count; /* returns number of processes */
1160 procstat_entry_t pse;
1164 /* Open the kvm interface, get a descriptor */
1165 kd = kvm_open (NULL, NULL, NULL, 0, errbuf);
1168 ERROR ("processes plugin: Cannot open kvm interface: %s",
1173 /* Get the list of processes. */
1174 procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, &count);
1178 ERROR ("processes plugin: Cannot get kvm processes list: %s",
1183 /* Iterate through the processes in kinfo_proc */
1184 for (i = 0; i < count; i++)
1186 /* retrieve the arguments */
1190 argv = kvm_getargv (kd, (const struct kinfo_proc *) &(procs[i]), 0);
1197 while (argv[argc] != NULL)
1200 status = strjoin (cmdline, sizeof (cmdline),
1205 WARNING ("processes plugin: Command line did "
1206 "not fit into buffer.");
1210 cmdline_ptr = &cmdline[0];
1214 pse.id = procs[i].ki_pid;
1218 pse.num_lwp = procs[i].ki_numthreads;
1220 pse.vmem_rss = procs[i].ki_rssize * getpagesize();
1221 pse.vmem_minflt = 0;
1222 pse.vmem_minflt_counter = procs[i].ki_rusage.ru_minflt;
1223 pse.vmem_majflt = 0;
1224 pse.vmem_majflt_counter = procs[i].ki_rusage.ru_majflt;
1227 pse.cpu_user_counter = procs[i].ki_rusage.ru_utime.tv_sec
1229 + procs[i].ki_rusage.ru_utime.tv_usec;
1231 pse.cpu_system_counter = procs[i].ki_rusage.ru_stime.tv_sec
1233 + procs[i].ki_rusage.ru_stime.tv_usec;
1235 switch (procs[i].ki_stat)
1237 case SSTOP: stopped++; break;
1238 case SSLEEP: sleeping++; break;
1239 case SRUN: running++; break;
1240 case SIDL: idle++; break;
1241 case SWAIT: wait++; break;
1242 case SLOCK: blocked++; break;
1243 case SZOMB: zombies++; break;
1246 ps_list_add (procs[i].ki_comm, cmdline_ptr, &pse);
1251 ps_submit_state ("running", running);
1252 ps_submit_state ("sleeping", sleeping);
1253 ps_submit_state ("zombies", zombies);
1254 ps_submit_state ("stopped", stopped);
1255 ps_submit_state ("blocked", blocked);
1256 ps_submit_state ("idle", idle);
1257 ps_submit_state ("wait", wait);
1259 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1260 ps_submit_proc_list (ps_ptr);
1261 #endif /* HAVE_LIBKVM */
1266 void module_register (void)
1268 plugin_register_config ("processes", ps_config,
1269 config_keys, config_keys_num);
1270 plugin_register_init ("processes", ps_init);
1271 plugin_register_read ("processes", ps_read);
1272 } /* void module_register */