2 * collectd - src/processes.c
3 * Copyright (C) 2005 Lyonel Vincent
4 * Copyright (C) 2006-2007 Florian Forster (Mach code)
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 * Lyonel Vincent <lyonel at ezix.org>
22 * Florian octo Forster <octo at verplant.org>
28 #include "configfile.h"
30 /* Include header files for the mach system, if they exist.. */
32 # if HAVE_MACH_MACH_INIT_H
33 # include <mach/mach_init.h>
35 # if HAVE_MACH_HOST_PRIV_H
36 # include <mach/host_priv.h>
38 # if HAVE_MACH_MACH_ERROR_H
39 # include <mach/mach_error.h>
41 # if HAVE_MACH_MACH_HOST_H
42 # include <mach/mach_host.h>
44 # if HAVE_MACH_MACH_PORT_H
45 # include <mach/mach_port.h>
47 # if HAVE_MACH_MACH_TYPES_H
48 # include <mach/mach_types.h>
50 # if HAVE_MACH_MESSAGE_H
51 # include <mach/message.h>
53 # if HAVE_MACH_PROCESSOR_SET_H
54 # include <mach/processor_set.h>
57 # include <mach/task.h>
59 # if HAVE_MACH_THREAD_ACT_H
60 # include <mach/thread_act.h>
62 # if HAVE_MACH_VM_REGION_H
63 # include <mach/vm_region.h>
65 # if HAVE_MACH_VM_MAP_H
66 # include <mach/vm_map.h>
68 # if HAVE_MACH_VM_PROT_H
69 # include <mach/vm_prot.h>
71 # if HAVE_SYS_SYSCTL_H
72 # include <sys/sysctl.h>
74 /* #endif HAVE_THREAD_INFO */
77 # if HAVE_LINUX_CONFIG_H
78 # include <linux/config.h>
81 # define CONFIG_HZ 100
83 /* #endif KERNEL_LINUX */
87 # include <sys/user.h>
88 # include <sys/proc.h>
89 # if HAVE_SYS_SYSCTL_H
90 # include <sys/sysctl.h>
92 /* #endif HAVE_KVM_H */
95 # error "No applicable input method."
104 static const char *config_keys[] =
110 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
112 typedef struct procstat_entry_s
117 unsigned long num_proc;
118 unsigned long num_lwp;
119 unsigned long vmem_rss;
121 unsigned long vmem_minflt;
122 unsigned long vmem_majflt;
123 unsigned long vmem_minflt_counter;
124 unsigned long vmem_majflt_counter;
126 unsigned long cpu_user;
127 unsigned long cpu_system;
128 unsigned long cpu_user_counter;
129 unsigned long cpu_system_counter;
131 struct procstat_entry_s *next;
134 #define PROCSTAT_NAME_LEN 256
135 typedef struct procstat
137 char name[PROCSTAT_NAME_LEN];
142 unsigned long num_proc;
143 unsigned long num_lwp;
144 unsigned long vmem_rss;
146 unsigned long vmem_minflt_counter;
147 unsigned long vmem_majflt_counter;
149 unsigned long cpu_user_counter;
150 unsigned long cpu_system_counter;
152 struct procstat *next;
153 struct procstat_entry_s *instances;
156 static procstat_t *list_head_g = NULL;
159 static mach_port_t port_host_self;
160 static mach_port_t port_task_self;
162 static processor_set_name_array_t pset_list;
163 static mach_msg_type_number_t pset_list_len;
164 /* #endif HAVE_THREAD_INFO */
167 static long pagesize_g;
168 #endif /* KERNEL_LINUX */
170 /* put name of process from config to list_head_g tree
171 list_head_g is a list of 'procstat_t' structs with
172 processes names we want to watch */
173 static void ps_list_register (const char *name, const char *regexp)
179 new = (procstat_t *) malloc (sizeof (procstat_t));
182 ERROR ("processes plugin: ps_list_register: malloc failed.");
185 memset (new, 0, sizeof (procstat_t));
186 sstrncpy (new->name, name, sizeof (new->name));
191 DEBUG ("ProcessMatch: adding \"%s\" as criteria to process %s.", regexp, name);
192 new->re = (regex_t *) malloc (sizeof (regex_t));
195 ERROR ("processes plugin: ps_list_register: malloc failed.");
200 status = regcomp (new->re, regexp, REG_EXTENDED | REG_NOSUB);
203 DEBUG ("ProcessMatch: compiling the regular expression \"%s\" failed.", regexp);
211 ERROR ("processes plugin: ps_list_register: "
212 "Regular expression \"%s\" found in config "
213 "file, but support for regular expressions "
214 "has been dispabled at compile time.",
221 for (ptr = list_head_g; ptr != NULL; ptr = ptr->next)
223 if (strcmp (ptr->name, name) == 0)
225 WARNING ("processes plugin: You have configured more "
226 "than one `Process' or "
227 "`ProcessMatch' with the same name. "
228 "All but the first setting will be "
235 if (ptr->next == NULL)
243 } /* void ps_list_register */
245 /* try to match name against entry, returns 1 if success */
246 static int ps_list_match (const char *name, const char *cmdline, procstat_t *ps)
248 if ((ps->re != NULL) && (regexec(ps->re, (strlen(cmdline)!=0)?cmdline:name, 0, NULL, 0) == 0))
250 if (strcmp (ps->name, name) == 0) {
256 /* add process entry to 'instances' of process 'name' (or refresh it) */
257 static void ps_list_add (const char *name, const char *cmdline, procstat_entry_t *entry)
260 procstat_entry_t *pse;
265 for (ps = list_head_g; ps != NULL; ps = ps->next)
268 if ((ps_list_match (name, cmdline, ps)) == 0)
271 for (pse = ps->instances; pse != NULL; pse = pse->next)
272 if ((pse->id == entry->id) || (pse->next == NULL))
275 if ((pse == NULL) || (pse->id != entry->id))
277 procstat_entry_t *new;
279 new = (procstat_entry_t *) malloc (sizeof (procstat_entry_t));
282 memset (new, 0, sizeof (procstat_entry_t));
294 pse->num_proc = entry->num_proc;
295 pse->num_lwp = entry->num_lwp;
296 pse->vmem_rss = entry->vmem_rss;
298 ps->num_proc += pse->num_proc;
299 ps->num_lwp += pse->num_lwp;
300 ps->vmem_rss += pse->vmem_rss;
302 if ((entry->vmem_minflt_counter == 0)
303 && (entry->vmem_majflt_counter == 0))
305 pse->vmem_minflt_counter += entry->vmem_minflt;
306 pse->vmem_minflt = entry->vmem_minflt;
308 pse->vmem_majflt_counter += entry->vmem_majflt;
309 pse->vmem_majflt = entry->vmem_majflt;
313 if (entry->vmem_minflt_counter < pse->vmem_minflt_counter)
315 pse->vmem_minflt = entry->vmem_minflt_counter
316 + (ULONG_MAX - pse->vmem_minflt_counter);
320 pse->vmem_minflt = entry->vmem_minflt_counter - pse->vmem_minflt_counter;
322 pse->vmem_minflt_counter = entry->vmem_minflt_counter;
324 if (entry->vmem_majflt_counter < pse->vmem_majflt_counter)
326 pse->vmem_majflt = entry->vmem_majflt_counter
327 + (ULONG_MAX - pse->vmem_majflt_counter);
331 pse->vmem_majflt = entry->vmem_majflt_counter - pse->vmem_majflt_counter;
333 pse->vmem_majflt_counter = entry->vmem_majflt_counter;
336 ps->vmem_minflt_counter += pse->vmem_minflt;
337 ps->vmem_majflt_counter += pse->vmem_majflt;
339 if ((entry->cpu_user_counter == 0)
340 && (entry->cpu_system_counter == 0))
342 pse->cpu_user_counter += entry->cpu_user;
343 pse->cpu_user = entry->cpu_user;
345 pse->cpu_system_counter += entry->cpu_system;
346 pse->cpu_system = entry->cpu_system;
350 if (entry->cpu_user_counter < pse->cpu_user_counter)
352 pse->cpu_user = entry->cpu_user_counter
353 + (ULONG_MAX - pse->cpu_user_counter);
357 pse->cpu_user = entry->cpu_user_counter - pse->cpu_user_counter;
359 pse->cpu_user_counter = entry->cpu_user_counter;
361 if (entry->cpu_system_counter < pse->cpu_system_counter)
363 pse->cpu_system = entry->cpu_system_counter
364 + (ULONG_MAX - pse->cpu_system_counter);
368 pse->cpu_system = entry->cpu_system_counter - pse->cpu_system_counter;
370 pse->cpu_system_counter = entry->cpu_system_counter;
373 ps->cpu_user_counter += pse->cpu_user;
374 ps->cpu_system_counter += pse->cpu_system;
378 /* remove old entries from instances of processes in list_head_g */
379 static void ps_list_reset (void)
382 procstat_entry_t *pse;
383 procstat_entry_t *pse_prev;
385 for (ps = list_head_g; ps != NULL; ps = ps->next)
397 DEBUG ("Removing this procstat entry cause it's too old: "
398 "id = %lu; name = %s;",
401 if (pse_prev == NULL)
403 ps->instances = pse->next;
409 pse_prev->next = pse->next;
411 pse = pse_prev->next;
420 } /* while (pse != NULL) */
421 } /* for (ps = list_head_g; ps != NULL; ps = ps->next) */
424 /* put all pre-defined 'Process' names from config to list_head_g tree */
425 static int ps_config (const char *key, const char *value)
431 if (strcasecmp (key, "Process") == 0)
433 ps_list_register (value, NULL);
437 if (strcasecmp (key, "ProcessMatch") == 0)
439 new_val = strdup (value);
442 fields_num = strsplit (new_val, fields, 2);
448 ps_list_register (fields[0], fields[1]);
456 static int ps_init (void)
459 kern_return_t status;
461 port_host_self = mach_host_self ();
462 port_task_self = mach_task_self ();
464 if (pset_list != NULL)
466 vm_deallocate (port_task_self,
467 (vm_address_t) pset_list,
468 pset_list_len * sizeof (processor_set_t));
473 if ((status = host_processor_sets (port_host_self,
475 &pset_list_len)) != KERN_SUCCESS)
477 ERROR ("host_processor_sets failed: %s\n",
478 mach_error_string (status));
483 /* #endif HAVE_THREAD_INFO */
486 pagesize_g = sysconf(_SC_PAGESIZE);
487 DEBUG ("pagesize_g = %li; CONFIG_HZ = %i;",
488 pagesize_g, CONFIG_HZ);
489 #endif /* KERNEL_LINUX */
494 /* submit global state (e.g.: qty of zombies, running, etc..) */
495 static void ps_submit_state (const char *state, double value)
498 value_list_t vl = VALUE_LIST_INIT;
500 values[0].gauge = value;
504 vl.time = time (NULL);
505 strcpy (vl.host, hostname_g);
506 strcpy (vl.plugin, "processes");
507 strcpy (vl.plugin_instance, "");
508 strcpy (vl.type, "ps_state");
509 sstrncpy (vl.type_instance, state, sizeof (vl.type_instance));
511 plugin_dispatch_values (&vl);
514 /* submit info about specific process (e.g.: memory taken, cpu usage, etc..) */
515 static void ps_submit_proc_list (procstat_t *ps)
518 value_list_t vl = VALUE_LIST_INIT;
522 vl.time = time (NULL);
523 strcpy (vl.host, hostname_g);
524 strcpy (vl.plugin, "processes");
525 sstrncpy (vl.plugin_instance, ps->name, sizeof (vl.plugin_instance));
527 strcpy (vl.type, "ps_rss");
528 vl.values[0].gauge = ps->vmem_rss;
530 plugin_dispatch_values (&vl);
532 strcpy (vl.type, "ps_cputime");
533 vl.values[0].counter = ps->cpu_user_counter;
534 vl.values[1].counter = ps->cpu_system_counter;
536 plugin_dispatch_values (&vl);
538 strcpy (vl.type, "ps_count");
539 vl.values[0].gauge = ps->num_proc;
540 vl.values[1].gauge = ps->num_lwp;
542 plugin_dispatch_values (&vl);
544 strcpy (vl.type, "ps_pagefaults");
545 vl.values[0].counter = ps->vmem_minflt_counter;
546 vl.values[1].counter = ps->vmem_majflt_counter;
548 plugin_dispatch_values (&vl);
550 DEBUG ("name = %s; num_proc = %lu; num_lwp = %lu; vmem_rss = %lu; "
551 "vmem_minflt_counter = %lu; vmem_majflt_counter = %lu; "
552 "cpu_user_counter = %lu; cpu_system_counter = %lu;",
553 ps->name, ps->num_proc, ps->num_lwp, ps->vmem_rss,
554 ps->vmem_minflt_counter, ps->vmem_majflt_counter,
555 ps->cpu_user_counter, ps->cpu_system_counter);
556 } /* void ps_submit_proc_list */
558 /* ------- additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
560 static int *ps_read_tasks (int pid)
563 int list_size = 1; /* size of allocated space, in elements */
564 int list_len = 0; /* number of currently used elements */
570 ssnprintf (dirname, sizeof (dirname), "/proc/%i/task", pid);
572 if ((dh = opendir (dirname)) == NULL)
574 DEBUG ("Failed to open directory `%s'", dirname);
578 while ((ent = readdir (dh)) != NULL)
580 if (!isdigit (ent->d_name[0]))
583 if ((list_len + 1) >= list_size)
586 int new_size = 2 * list_size;
587 /* Comes in sizes: 2, 4, 8, 16, ... */
589 new_ptr = (int *) realloc (list, (size_t) (sizeof (int) * new_size));
594 ERROR ("processes plugin: "
595 "Failed to allocate more memory.");
600 list_size = new_size;
602 memset (list + list_len, 0, sizeof (int) * (list_size - list_len));
605 list[list_len] = atoi (ent->d_name);
606 if (list[list_len] != 0)
615 assert (list_len < list_size);
616 assert (list[list_len] == 0);
619 } /* int *ps_read_tasks */
621 int ps_read_process (int pid, procstat_t *ps, char *state)
636 long long unsigned cpu_user_counter;
637 long long unsigned cpu_system_counter;
638 long long unsigned vmem_rss;
640 memset (ps, 0, sizeof (procstat_t));
642 ssnprintf (filename, sizeof (filename), "/proc/%i/stat", pid);
644 if ((fh = fopen (filename, "r")) == NULL)
647 if (fgets (buffer, 1024, fh) == NULL)
655 fields_len = strsplit (buffer, fields, 64);
658 DEBUG ("processes plugin: ps_read_process (pid = %i):"
659 " `%s' has only %i fields..",
660 (int) pid, filename, fields_len);
664 /* copy the name, strip brackets in the process */
665 name_len = strlen (fields[1]) - 2;
666 if ((fields[1][0] != '(') || (fields[1][name_len + 1] != ')'))
668 DEBUG ("No brackets found in process name: `%s'", fields[1]);
671 fields[1] = fields[1] + 1;
672 fields[1][name_len] = '\0';
673 strncpy (ps->name, fields[1], PROCSTAT_NAME_LEN);
675 ppid = atoi (fields[3]);
677 *state = fields[2][0];
684 else if ((tasks = ps_read_tasks (pid)) == NULL)
686 /* Kernel 2.4 or so */
694 for (i = 0; tasks[i] != 0; i++)
701 /* Leave the rest at zero if this is only a zombi */
702 if (ps->num_proc == 0)
704 DEBUG ("processes plugin: This is only a zombi: pid = %i; "
705 "name = %s;", pid, ps->name);
709 cpu_user_counter = atoll (fields[13]);
710 cpu_system_counter = atoll (fields[14]);
711 vmem_rss = atoll (fields[23]);
712 ps->vmem_minflt_counter = atol (fields[9]);
713 ps->vmem_majflt_counter = atol (fields[11]);
715 /* Convert jiffies to useconds */
716 cpu_user_counter = cpu_user_counter * 1000000 / CONFIG_HZ;
717 cpu_system_counter = cpu_system_counter * 1000000 / CONFIG_HZ;
718 vmem_rss = vmem_rss * pagesize_g;
720 ps->cpu_user_counter = (unsigned long) cpu_user_counter;
721 ps->cpu_system_counter = (unsigned long) cpu_system_counter;
722 ps->vmem_rss = (unsigned long) vmem_rss;
726 } /* int ps_read_process (...) */
727 #endif /* KERNEL_LINUX */
730 static int mach_get_task_name (task_t t, int *pid, char *name, size_t name_max_len)
734 struct kinfo_proc kp;
739 mib[2] = KERN_PROC_PID;
741 if (pid_for_task (t, pid) != KERN_SUCCESS)
745 kp_size = sizeof (kp);
746 if (sysctl (mib, 4, &kp, &kp_size, NULL, 0) != 0)
749 if (name_max_len > (MAXCOMLEN + 1))
750 name_max_len = MAXCOMLEN + 1;
752 strncpy (name, kp.kp_proc.p_comm, name_max_len - 1);
753 name[name_max_len - 1] = '\0';
755 DEBUG ("pid = %i; name = %s;", *pid, name);
757 /* We don't do the special handling for `p_comm == "LaunchCFMApp"' as
758 * `top' does it, because it is a lot of work and only used when
759 * debugging. -octo */
763 #endif /* HAVE_THREAD_INFO */
764 /* ------- end of additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
766 /* do actual readings from kernel */
767 static int ps_read (void)
770 kern_return_t status;
773 processor_set_t port_pset_priv;
776 task_array_t task_list;
777 mach_msg_type_number_t task_list_len;
780 char task_name[MAXCOMLEN + 1];
783 thread_act_array_t thread_list;
784 mach_msg_type_number_t thread_list_len;
785 thread_basic_info_data_t thread_data;
786 mach_msg_type_number_t thread_data_len;
795 procstat_entry_t pse;
800 * The Mach-concept is a little different from the traditional UNIX
801 * concept: All the work is done in threads. Threads are contained in
802 * `tasks'. Therefore, `task status' doesn't make much sense, since
803 * it's actually a `thread status'.
804 * Tasks are assigned to sets of processors, so that's where you go to
807 for (pset = 0; pset < pset_list_len; pset++)
809 if ((status = host_processor_set_priv (port_host_self,
811 &port_pset_priv)) != KERN_SUCCESS)
813 ERROR ("host_processor_set_priv failed: %s\n",
814 mach_error_string (status));
818 if ((status = processor_set_tasks (port_pset_priv,
820 &task_list_len)) != KERN_SUCCESS)
822 ERROR ("processor_set_tasks failed: %s\n",
823 mach_error_string (status));
824 mach_port_deallocate (port_task_self, port_pset_priv);
828 for (task = 0; task < task_list_len; task++)
831 if (mach_get_task_name (task_list[task],
833 task_name, PROCSTAT_NAME_LEN) == 0)
835 /* search for at least one match */
836 for (ps = list_head_g; ps != NULL; ps = ps->next)
837 if (ps_list_match(task_name, NULL, ps) == 1) //!!! cmdline should be here instead of NULL
841 /* Collect more detailed statistics for this process */
844 task_basic_info_data_t task_basic_info;
845 mach_msg_type_number_t task_basic_info_len;
846 task_events_info_data_t task_events_info;
847 mach_msg_type_number_t task_events_info_len;
848 task_absolutetime_info_data_t task_absolutetime_info;
849 mach_msg_type_number_t task_absolutetime_info_len;
851 memset (&pse, '\0', sizeof (pse));
854 task_basic_info_len = TASK_BASIC_INFO_COUNT;
855 status = task_info (task_list[task],
857 (task_info_t) &task_basic_info,
858 &task_basic_info_len);
859 if (status != KERN_SUCCESS)
861 ERROR ("task_info failed: %s",
862 mach_error_string (status));
863 continue; /* with next thread_list */
866 task_events_info_len = TASK_EVENTS_INFO_COUNT;
867 status = task_info (task_list[task],
869 (task_info_t) &task_events_info,
870 &task_events_info_len);
871 if (status != KERN_SUCCESS)
873 ERROR ("task_info failed: %s",
874 mach_error_string (status));
875 continue; /* with next thread_list */
878 task_absolutetime_info_len = TASK_ABSOLUTETIME_INFO_COUNT;
879 status = task_info (task_list[task],
880 TASK_ABSOLUTETIME_INFO,
881 (task_info_t) &task_absolutetime_info,
882 &task_absolutetime_info_len);
883 if (status != KERN_SUCCESS)
885 ERROR ("task_info failed: %s",
886 mach_error_string (status));
887 continue; /* with next thread_list */
891 pse.vmem_rss = task_basic_info.resident_size;
893 pse.vmem_minflt_counter = task_events_info.cow_faults;
894 pse.vmem_majflt_counter = task_events_info.faults;
896 pse.cpu_user_counter = task_absolutetime_info.total_user;
897 pse.cpu_system_counter = task_absolutetime_info.total_system;
900 status = task_threads (task_list[task], &thread_list,
902 if (status != KERN_SUCCESS)
904 /* Apple's `top' treats this case a zombie. It
905 * makes sense to some extend: A `zombie'
906 * thread is nonsense, since the task/process
909 DEBUG ("task_threads failed: %s",
910 mach_error_string (status));
911 if (task_list[task] != port_task_self)
912 mach_port_deallocate (port_task_self,
914 continue; /* with next task_list */
917 for (thread = 0; thread < thread_list_len; thread++)
919 thread_data_len = THREAD_BASIC_INFO_COUNT;
920 status = thread_info (thread_list[thread],
922 (thread_info_t) &thread_data,
924 if (status != KERN_SUCCESS)
926 ERROR ("thread_info failed: %s",
927 mach_error_string (status));
928 if (task_list[task] != port_task_self)
929 mach_port_deallocate (port_task_self,
930 thread_list[thread]);
931 continue; /* with next thread_list */
937 switch (thread_data.run_state)
939 case TH_STATE_RUNNING:
942 case TH_STATE_STOPPED:
943 /* What exactly is `halted'? */
944 case TH_STATE_HALTED:
947 case TH_STATE_WAITING:
950 case TH_STATE_UNINTERRUPTIBLE:
953 /* There is no `zombie' case here,
954 * since there are no zombie-threads.
955 * There's only zombie tasks, which are
958 WARNING ("Unknown thread status: %s",
959 thread_data.run_state);
961 } /* switch (thread_data.run_state) */
963 if (task_list[task] != port_task_self)
965 status = mach_port_deallocate (port_task_self,
966 thread_list[thread]);
967 if (status != KERN_SUCCESS)
968 ERROR ("mach_port_deallocate failed: %s",
969 mach_error_string (status));
971 } /* for (thread_list) */
973 if ((status = vm_deallocate (port_task_self,
974 (vm_address_t) thread_list,
975 thread_list_len * sizeof (thread_act_t)))
978 ERROR ("vm_deallocate failed: %s",
979 mach_error_string (status));
984 /* Only deallocate the task port, if it isn't our own.
985 * Don't know what would happen in that case, but this
986 * is what Apple's top does.. ;) */
987 if (task_list[task] != port_task_self)
989 status = mach_port_deallocate (port_task_self,
991 if (status != KERN_SUCCESS)
992 ERROR ("mach_port_deallocate failed: %s",
993 mach_error_string (status));
997 ps_list_add (task_name, NULL, &pse); //!!! cmdline should be here instead of NULL
998 } /* for (task_list) */
1000 if ((status = vm_deallocate (port_task_self,
1001 (vm_address_t) task_list,
1002 task_list_len * sizeof (task_t))) != KERN_SUCCESS)
1004 ERROR ("vm_deallocate failed: %s",
1005 mach_error_string (status));
1010 if ((status = mach_port_deallocate (port_task_self, port_pset_priv))
1013 ERROR ("mach_port_deallocate failed: %s",
1014 mach_error_string (status));
1016 } /* for (pset_list) */
1018 ps_submit_state ("running", running);
1019 ps_submit_state ("sleeping", sleeping);
1020 ps_submit_state ("zombies", zombies);
1021 ps_submit_state ("stopped", stopped);
1022 ps_submit_state ("blocked", blocked);
1024 for (ps = list_head_g; ps != NULL; ps = ps->next)
1025 ps_submit_proc_list (ps);
1026 /* #endif HAVE_THREAD_INFO */
1042 procstat_entry_t pse;
1047 running = sleeping = zombies = stopped = paging = blocked = 0;
1050 if ((proc = opendir ("/proc")) == NULL)
1053 ERROR ("Cannot open `/proc': %s",
1054 sstrerror (errno, errbuf, sizeof (errbuf)));
1058 while ((ent = readdir (proc)) != NULL)
1060 if (!isdigit (ent->d_name[0]))
1063 if ((pid = atoi (ent->d_name)) < 1)
1066 status = ps_read_process (pid, &ps, &state);
1069 DEBUG ("ps_read_process failed: %i", status);
1076 pse.num_proc = ps.num_proc;
1077 pse.num_lwp = ps.num_lwp;
1078 pse.vmem_rss = ps.vmem_rss;
1080 pse.vmem_minflt = 0;
1081 pse.vmem_minflt_counter = ps.vmem_minflt_counter;
1082 pse.vmem_majflt = 0;
1083 pse.vmem_majflt_counter = ps.vmem_majflt_counter;
1086 pse.cpu_user_counter = ps.cpu_user_counter;
1088 pse.cpu_system_counter = ps.cpu_system_counter;
1092 case 'R': running++; break;
1093 case 'S': sleeping++; break;
1094 case 'D': blocked++; break;
1095 case 'Z': zombies++; break;
1096 case 'T': stopped++; break;
1097 case 'W': paging++; break;
1100 ps_list_add (ps.name, NULL, &pse); //!!! cmdline should be here instead of NULL
1105 ps_submit_state ("running", running);
1106 ps_submit_state ("sleeping", sleeping);
1107 ps_submit_state ("zombies", zombies);
1108 ps_submit_state ("stopped", stopped);
1109 ps_submit_state ("paging", paging);
1110 ps_submit_state ("blocked", blocked);
1112 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1113 ps_submit_proc_list (ps_ptr);
1114 /* #endif KERNEL_LINUX */
1127 char cmdline[ARG_MAX];
1128 struct kinfo_proc *procs; /* array of processes */
1130 int count; /* returns number of processes */
1134 procstat_entry_t pse;
1138 /* Open the kvm interface, get a descriptor */
1139 if ((kd = kvm_open(NULL, NULL, NULL, 0, errbuf)) == NULL) {
1140 ERROR ("Cannot open kvm interface: %s", errbuf);
1144 /* Get the list of processes. */
1145 if ((procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, &count)) == NULL) {
1147 ERROR ("Cannot get kvm processes list: %s", kvm_geterr(kd));
1151 /* Iterate through the processes in kinfo_proc */
1152 for (i=0; i < count; i++) {
1153 // retrieve the arguments
1155 argv = kvm_getargv(kd, (const struct kinfo_proc *) &(procs[i]), 0);
1158 while (argv[j] && strlen(cmdline) <= ARG_MAX) {
1160 strncat(cmdline, " ", 1);
1161 strncat(cmdline, argv[j], strlen(argv[j]));
1166 pse.id = procs[i].ki_pid;
1170 pse.num_lwp = procs[i].ki_numthreads;
1172 pse.vmem_rss = procs[i].ki_rssize * getpagesize();
1173 pse.vmem_minflt = 0;
1174 pse.vmem_minflt_counter = procs[i].ki_rusage.ru_minflt;
1175 pse.vmem_majflt = 0;
1176 pse.vmem_majflt_counter = procs[i].ki_rusage.ru_majflt;
1179 pse.cpu_user_counter = procs[i].ki_rusage.ru_utime.tv_sec*1000 + procs[i].ki_rusage.ru_utime.tv_usec;
1181 pse.cpu_system_counter = procs[i].ki_rusage.ru_stime.tv_sec*1000 + procs[i].ki_rusage.ru_stime.tv_usec;
1183 switch (procs[i].ki_stat) {
1184 case SSTOP: stopped++; break;
1185 case SSLEEP: sleeping++; break;
1186 case SRUN: running++; break;
1187 case SIDL: idle++; break;
1188 case SWAIT: wait++; break;
1189 case SLOCK: blocked++; break;
1190 case SZOMB: zombies++; break;
1193 ps_list_add (procs[i].ki_comm, cmdline, &pse);
1196 if (kd) kvm_close(kd);
1198 ps_submit_state ("running", running);
1199 ps_submit_state ("sleeping", sleeping);
1200 ps_submit_state ("zombies", zombies);
1201 ps_submit_state ("stopped", stopped);
1202 ps_submit_state ("blocked", blocked);
1203 ps_submit_state ("idle", idle);
1204 ps_submit_state ("wait", wait);
1206 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1207 ps_submit_proc_list (ps_ptr);
1209 #endif /* HAVE_LIBKVM */
1214 void module_register (void)
1216 plugin_register_config ("processes", ps_config,
1217 config_keys, config_keys_num);
1218 plugin_register_init ("processes", ps_init);
1219 plugin_register_read ("processes", ps_read);
1220 } /* void module_register */