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)
250 if ((ps->re != NULL) && (regexec(ps->re, (strlen(cmdline)!=0)?cmdline:name, 0, NULL, 0) == 0))
252 if (strcmp (ps->name, name) == 0) {
258 /* add process entry to 'instances' of process 'name' (or refresh it) */
259 static void ps_list_add (const char *name, const char *cmdline, procstat_entry_t *entry)
262 procstat_entry_t *pse;
267 for (ps = list_head_g; ps != NULL; ps = ps->next)
270 if ((ps_list_match (name, cmdline, ps)) == 0)
273 for (pse = ps->instances; pse != NULL; pse = pse->next)
274 if ((pse->id == entry->id) || (pse->next == NULL))
277 if ((pse == NULL) || (pse->id != entry->id))
279 procstat_entry_t *new;
281 new = (procstat_entry_t *) malloc (sizeof (procstat_entry_t));
284 memset (new, 0, sizeof (procstat_entry_t));
296 pse->num_proc = entry->num_proc;
297 pse->num_lwp = entry->num_lwp;
298 pse->vmem_rss = entry->vmem_rss;
300 ps->num_proc += pse->num_proc;
301 ps->num_lwp += pse->num_lwp;
302 ps->vmem_rss += pse->vmem_rss;
304 if ((entry->vmem_minflt_counter == 0)
305 && (entry->vmem_majflt_counter == 0))
307 pse->vmem_minflt_counter += entry->vmem_minflt;
308 pse->vmem_minflt = entry->vmem_minflt;
310 pse->vmem_majflt_counter += entry->vmem_majflt;
311 pse->vmem_majflt = entry->vmem_majflt;
315 if (entry->vmem_minflt_counter < pse->vmem_minflt_counter)
317 pse->vmem_minflt = entry->vmem_minflt_counter
318 + (ULONG_MAX - pse->vmem_minflt_counter);
322 pse->vmem_minflt = entry->vmem_minflt_counter - pse->vmem_minflt_counter;
324 pse->vmem_minflt_counter = entry->vmem_minflt_counter;
326 if (entry->vmem_majflt_counter < pse->vmem_majflt_counter)
328 pse->vmem_majflt = entry->vmem_majflt_counter
329 + (ULONG_MAX - pse->vmem_majflt_counter);
333 pse->vmem_majflt = entry->vmem_majflt_counter - pse->vmem_majflt_counter;
335 pse->vmem_majflt_counter = entry->vmem_majflt_counter;
338 ps->vmem_minflt_counter += pse->vmem_minflt;
339 ps->vmem_majflt_counter += pse->vmem_majflt;
341 if ((entry->cpu_user_counter == 0)
342 && (entry->cpu_system_counter == 0))
344 pse->cpu_user_counter += entry->cpu_user;
345 pse->cpu_user = entry->cpu_user;
347 pse->cpu_system_counter += entry->cpu_system;
348 pse->cpu_system = entry->cpu_system;
352 if (entry->cpu_user_counter < pse->cpu_user_counter)
354 pse->cpu_user = entry->cpu_user_counter
355 + (ULONG_MAX - pse->cpu_user_counter);
359 pse->cpu_user = entry->cpu_user_counter - pse->cpu_user_counter;
361 pse->cpu_user_counter = entry->cpu_user_counter;
363 if (entry->cpu_system_counter < pse->cpu_system_counter)
365 pse->cpu_system = entry->cpu_system_counter
366 + (ULONG_MAX - pse->cpu_system_counter);
370 pse->cpu_system = entry->cpu_system_counter - pse->cpu_system_counter;
372 pse->cpu_system_counter = entry->cpu_system_counter;
375 ps->cpu_user_counter += pse->cpu_user;
376 ps->cpu_system_counter += pse->cpu_system;
380 /* remove old entries from instances of processes in list_head_g */
381 static void ps_list_reset (void)
384 procstat_entry_t *pse;
385 procstat_entry_t *pse_prev;
387 for (ps = list_head_g; ps != NULL; ps = ps->next)
399 DEBUG ("Removing this procstat entry cause it's too old: "
400 "id = %lu; name = %s;",
403 if (pse_prev == NULL)
405 ps->instances = pse->next;
411 pse_prev->next = pse->next;
413 pse = pse_prev->next;
422 } /* while (pse != NULL) */
423 } /* for (ps = list_head_g; ps != NULL; ps = ps->next) */
426 /* put all pre-defined 'Process' names from config to list_head_g tree */
427 static int ps_config (const char *key, const char *value)
429 if (strcasecmp (key, "Process") == 0)
431 ps_list_register (value, NULL);
433 else if (strcasecmp (key, "ProcessMatch") == 0)
439 new_val = strdup (value);
442 fields_num = strsplit (new_val, fields,
443 STATIC_ARRAY_SIZE (fields));
449 ps_list_register (fields[0], fields[1]);
460 static int ps_init (void)
463 kern_return_t status;
465 port_host_self = mach_host_self ();
466 port_task_self = mach_task_self ();
468 if (pset_list != NULL)
470 vm_deallocate (port_task_self,
471 (vm_address_t) pset_list,
472 pset_list_len * sizeof (processor_set_t));
477 if ((status = host_processor_sets (port_host_self,
479 &pset_list_len)) != KERN_SUCCESS)
481 ERROR ("host_processor_sets failed: %s\n",
482 mach_error_string (status));
487 /* #endif HAVE_THREAD_INFO */
490 pagesize_g = sysconf(_SC_PAGESIZE);
491 DEBUG ("pagesize_g = %li; CONFIG_HZ = %i;",
492 pagesize_g, CONFIG_HZ);
493 #endif /* KERNEL_LINUX */
498 /* submit global state (e.g.: qty of zombies, running, etc..) */
499 static void ps_submit_state (const char *state, double value)
502 value_list_t vl = VALUE_LIST_INIT;
504 values[0].gauge = value;
508 vl.time = time (NULL);
509 strcpy (vl.host, hostname_g);
510 strcpy (vl.plugin, "processes");
511 strcpy (vl.plugin_instance, "");
512 strcpy (vl.type, "ps_state");
513 sstrncpy (vl.type_instance, state, sizeof (vl.type_instance));
515 plugin_dispatch_values (&vl);
518 /* submit info about specific process (e.g.: memory taken, cpu usage, etc..) */
519 static void ps_submit_proc_list (procstat_t *ps)
522 value_list_t vl = VALUE_LIST_INIT;
526 vl.time = time (NULL);
527 strcpy (vl.host, hostname_g);
528 strcpy (vl.plugin, "processes");
529 sstrncpy (vl.plugin_instance, ps->name, sizeof (vl.plugin_instance));
531 strcpy (vl.type, "ps_rss");
532 vl.values[0].gauge = ps->vmem_rss;
534 plugin_dispatch_values (&vl);
536 strcpy (vl.type, "ps_cputime");
537 vl.values[0].counter = ps->cpu_user_counter;
538 vl.values[1].counter = ps->cpu_system_counter;
540 plugin_dispatch_values (&vl);
542 strcpy (vl.type, "ps_count");
543 vl.values[0].gauge = ps->num_proc;
544 vl.values[1].gauge = ps->num_lwp;
546 plugin_dispatch_values (&vl);
548 strcpy (vl.type, "ps_pagefaults");
549 vl.values[0].counter = ps->vmem_minflt_counter;
550 vl.values[1].counter = ps->vmem_majflt_counter;
552 plugin_dispatch_values (&vl);
554 DEBUG ("name = %s; num_proc = %lu; num_lwp = %lu; vmem_rss = %lu; "
555 "vmem_minflt_counter = %lu; vmem_majflt_counter = %lu; "
556 "cpu_user_counter = %lu; cpu_system_counter = %lu;",
557 ps->name, ps->num_proc, ps->num_lwp, ps->vmem_rss,
558 ps->vmem_minflt_counter, ps->vmem_majflt_counter,
559 ps->cpu_user_counter, ps->cpu_system_counter);
560 } /* void ps_submit_proc_list */
562 /* ------- additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
564 static int *ps_read_tasks (int pid)
567 int list_size = 1; /* size of allocated space, in elements */
568 int list_len = 0; /* number of currently used elements */
574 ssnprintf (dirname, sizeof (dirname), "/proc/%i/task", pid);
576 if ((dh = opendir (dirname)) == NULL)
578 DEBUG ("Failed to open directory `%s'", dirname);
582 while ((ent = readdir (dh)) != NULL)
584 if (!isdigit (ent->d_name[0]))
587 if ((list_len + 1) >= list_size)
590 int new_size = 2 * list_size;
591 /* Comes in sizes: 2, 4, 8, 16, ... */
593 new_ptr = (int *) realloc (list, (size_t) (sizeof (int) * new_size));
598 ERROR ("processes plugin: "
599 "Failed to allocate more memory.");
604 list_size = new_size;
606 memset (list + list_len, 0, sizeof (int) * (list_size - list_len));
609 list[list_len] = atoi (ent->d_name);
610 if (list[list_len] != 0)
619 assert (list_len < list_size);
620 assert (list[list_len] == 0);
623 } /* int *ps_read_tasks */
625 int ps_read_process (int pid, procstat_t *ps, char *state)
640 long long unsigned cpu_user_counter;
641 long long unsigned cpu_system_counter;
642 long long unsigned vmem_rss;
644 memset (ps, 0, sizeof (procstat_t));
646 ssnprintf (filename, sizeof (filename), "/proc/%i/stat", pid);
648 if ((fh = fopen (filename, "r")) == NULL)
651 if (fgets (buffer, 1024, fh) == NULL)
659 fields_len = strsplit (buffer, fields, 64);
662 DEBUG ("processes plugin: ps_read_process (pid = %i):"
663 " `%s' has only %i fields..",
664 (int) pid, filename, fields_len);
668 /* copy the name, strip brackets in the process */
669 name_len = strlen (fields[1]) - 2;
670 if ((fields[1][0] != '(') || (fields[1][name_len + 1] != ')'))
672 DEBUG ("No brackets found in process name: `%s'", fields[1]);
675 fields[1] = fields[1] + 1;
676 fields[1][name_len] = '\0';
677 strncpy (ps->name, fields[1], PROCSTAT_NAME_LEN);
679 ppid = atoi (fields[3]);
681 *state = fields[2][0];
688 else if ((tasks = ps_read_tasks (pid)) == NULL)
690 /* Kernel 2.4 or so */
698 for (i = 0; tasks[i] != 0; i++)
705 /* Leave the rest at zero if this is only a zombi */
706 if (ps->num_proc == 0)
708 DEBUG ("processes plugin: This is only a zombi: pid = %i; "
709 "name = %s;", pid, ps->name);
713 cpu_user_counter = atoll (fields[13]);
714 cpu_system_counter = atoll (fields[14]);
715 vmem_rss = atoll (fields[23]);
716 ps->vmem_minflt_counter = atol (fields[9]);
717 ps->vmem_majflt_counter = atol (fields[11]);
719 /* Convert jiffies to useconds */
720 cpu_user_counter = cpu_user_counter * 1000000 / CONFIG_HZ;
721 cpu_system_counter = cpu_system_counter * 1000000 / CONFIG_HZ;
722 vmem_rss = vmem_rss * pagesize_g;
724 ps->cpu_user_counter = (unsigned long) cpu_user_counter;
725 ps->cpu_system_counter = (unsigned long) cpu_system_counter;
726 ps->vmem_rss = (unsigned long) vmem_rss;
730 } /* int ps_read_process (...) */
731 #endif /* KERNEL_LINUX */
734 static int mach_get_task_name (task_t t, int *pid, char *name, size_t name_max_len)
738 struct kinfo_proc kp;
743 mib[2] = KERN_PROC_PID;
745 if (pid_for_task (t, pid) != KERN_SUCCESS)
749 kp_size = sizeof (kp);
750 if (sysctl (mib, 4, &kp, &kp_size, NULL, 0) != 0)
753 if (name_max_len > (MAXCOMLEN + 1))
754 name_max_len = MAXCOMLEN + 1;
756 strncpy (name, kp.kp_proc.p_comm, name_max_len - 1);
757 name[name_max_len - 1] = '\0';
759 DEBUG ("pid = %i; name = %s;", *pid, name);
761 /* We don't do the special handling for `p_comm == "LaunchCFMApp"' as
762 * `top' does it, because it is a lot of work and only used when
763 * debugging. -octo */
767 #endif /* HAVE_THREAD_INFO */
768 /* ------- end of additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
770 /* do actual readings from kernel */
771 static int ps_read (void)
774 kern_return_t status;
777 processor_set_t port_pset_priv;
780 task_array_t task_list;
781 mach_msg_type_number_t task_list_len;
784 char task_name[MAXCOMLEN + 1];
787 thread_act_array_t thread_list;
788 mach_msg_type_number_t thread_list_len;
789 thread_basic_info_data_t thread_data;
790 mach_msg_type_number_t thread_data_len;
799 procstat_entry_t pse;
804 * The Mach-concept is a little different from the traditional UNIX
805 * concept: All the work is done in threads. Threads are contained in
806 * `tasks'. Therefore, `task status' doesn't make much sense, since
807 * it's actually a `thread status'.
808 * Tasks are assigned to sets of processors, so that's where you go to
811 for (pset = 0; pset < pset_list_len; pset++)
813 if ((status = host_processor_set_priv (port_host_self,
815 &port_pset_priv)) != KERN_SUCCESS)
817 ERROR ("host_processor_set_priv failed: %s\n",
818 mach_error_string (status));
822 if ((status = processor_set_tasks (port_pset_priv,
824 &task_list_len)) != KERN_SUCCESS)
826 ERROR ("processor_set_tasks failed: %s\n",
827 mach_error_string (status));
828 mach_port_deallocate (port_task_self, port_pset_priv);
832 for (task = 0; task < task_list_len; task++)
835 if (mach_get_task_name (task_list[task],
837 task_name, PROCSTAT_NAME_LEN) == 0)
839 /* search for at least one match */
840 for (ps = list_head_g; ps != NULL; ps = ps->next)
841 if (ps_list_match(task_name, NULL, ps) == 1) //!!! cmdline should be here instead of NULL
845 /* Collect more detailed statistics for this process */
848 task_basic_info_data_t task_basic_info;
849 mach_msg_type_number_t task_basic_info_len;
850 task_events_info_data_t task_events_info;
851 mach_msg_type_number_t task_events_info_len;
852 task_absolutetime_info_data_t task_absolutetime_info;
853 mach_msg_type_number_t task_absolutetime_info_len;
855 memset (&pse, '\0', sizeof (pse));
858 task_basic_info_len = TASK_BASIC_INFO_COUNT;
859 status = task_info (task_list[task],
861 (task_info_t) &task_basic_info,
862 &task_basic_info_len);
863 if (status != KERN_SUCCESS)
865 ERROR ("task_info failed: %s",
866 mach_error_string (status));
867 continue; /* with next thread_list */
870 task_events_info_len = TASK_EVENTS_INFO_COUNT;
871 status = task_info (task_list[task],
873 (task_info_t) &task_events_info,
874 &task_events_info_len);
875 if (status != KERN_SUCCESS)
877 ERROR ("task_info failed: %s",
878 mach_error_string (status));
879 continue; /* with next thread_list */
882 task_absolutetime_info_len = TASK_ABSOLUTETIME_INFO_COUNT;
883 status = task_info (task_list[task],
884 TASK_ABSOLUTETIME_INFO,
885 (task_info_t) &task_absolutetime_info,
886 &task_absolutetime_info_len);
887 if (status != KERN_SUCCESS)
889 ERROR ("task_info failed: %s",
890 mach_error_string (status));
891 continue; /* with next thread_list */
895 pse.vmem_rss = task_basic_info.resident_size;
897 pse.vmem_minflt_counter = task_events_info.cow_faults;
898 pse.vmem_majflt_counter = task_events_info.faults;
900 pse.cpu_user_counter = task_absolutetime_info.total_user;
901 pse.cpu_system_counter = task_absolutetime_info.total_system;
904 status = task_threads (task_list[task], &thread_list,
906 if (status != KERN_SUCCESS)
908 /* Apple's `top' treats this case a zombie. It
909 * makes sense to some extend: A `zombie'
910 * thread is nonsense, since the task/process
913 DEBUG ("task_threads failed: %s",
914 mach_error_string (status));
915 if (task_list[task] != port_task_self)
916 mach_port_deallocate (port_task_self,
918 continue; /* with next task_list */
921 for (thread = 0; thread < thread_list_len; thread++)
923 thread_data_len = THREAD_BASIC_INFO_COUNT;
924 status = thread_info (thread_list[thread],
926 (thread_info_t) &thread_data,
928 if (status != KERN_SUCCESS)
930 ERROR ("thread_info failed: %s",
931 mach_error_string (status));
932 if (task_list[task] != port_task_self)
933 mach_port_deallocate (port_task_self,
934 thread_list[thread]);
935 continue; /* with next thread_list */
941 switch (thread_data.run_state)
943 case TH_STATE_RUNNING:
946 case TH_STATE_STOPPED:
947 /* What exactly is `halted'? */
948 case TH_STATE_HALTED:
951 case TH_STATE_WAITING:
954 case TH_STATE_UNINTERRUPTIBLE:
957 /* There is no `zombie' case here,
958 * since there are no zombie-threads.
959 * There's only zombie tasks, which are
962 WARNING ("Unknown thread status: %s",
963 thread_data.run_state);
965 } /* switch (thread_data.run_state) */
967 if (task_list[task] != port_task_self)
969 status = mach_port_deallocate (port_task_self,
970 thread_list[thread]);
971 if (status != KERN_SUCCESS)
972 ERROR ("mach_port_deallocate failed: %s",
973 mach_error_string (status));
975 } /* for (thread_list) */
977 if ((status = vm_deallocate (port_task_self,
978 (vm_address_t) thread_list,
979 thread_list_len * sizeof (thread_act_t)))
982 ERROR ("vm_deallocate failed: %s",
983 mach_error_string (status));
988 /* Only deallocate the task port, if it isn't our own.
989 * Don't know what would happen in that case, but this
990 * is what Apple's top does.. ;) */
991 if (task_list[task] != port_task_self)
993 status = mach_port_deallocate (port_task_self,
995 if (status != KERN_SUCCESS)
996 ERROR ("mach_port_deallocate failed: %s",
997 mach_error_string (status));
1001 ps_list_add (task_name, NULL, &pse); //!!! cmdline should be here instead of NULL
1002 } /* for (task_list) */
1004 if ((status = vm_deallocate (port_task_self,
1005 (vm_address_t) task_list,
1006 task_list_len * sizeof (task_t))) != KERN_SUCCESS)
1008 ERROR ("vm_deallocate failed: %s",
1009 mach_error_string (status));
1014 if ((status = mach_port_deallocate (port_task_self, port_pset_priv))
1017 ERROR ("mach_port_deallocate failed: %s",
1018 mach_error_string (status));
1020 } /* for (pset_list) */
1022 ps_submit_state ("running", running);
1023 ps_submit_state ("sleeping", sleeping);
1024 ps_submit_state ("zombies", zombies);
1025 ps_submit_state ("stopped", stopped);
1026 ps_submit_state ("blocked", blocked);
1028 for (ps = list_head_g; ps != NULL; ps = ps->next)
1029 ps_submit_proc_list (ps);
1030 /* #endif HAVE_THREAD_INFO */
1046 procstat_entry_t pse;
1051 running = sleeping = zombies = stopped = paging = blocked = 0;
1054 if ((proc = opendir ("/proc")) == NULL)
1057 ERROR ("Cannot open `/proc': %s",
1058 sstrerror (errno, errbuf, sizeof (errbuf)));
1062 while ((ent = readdir (proc)) != NULL)
1064 if (!isdigit (ent->d_name[0]))
1067 if ((pid = atoi (ent->d_name)) < 1)
1070 status = ps_read_process (pid, &ps, &state);
1073 DEBUG ("ps_read_process failed: %i", status);
1080 pse.num_proc = ps.num_proc;
1081 pse.num_lwp = ps.num_lwp;
1082 pse.vmem_rss = ps.vmem_rss;
1084 pse.vmem_minflt = 0;
1085 pse.vmem_minflt_counter = ps.vmem_minflt_counter;
1086 pse.vmem_majflt = 0;
1087 pse.vmem_majflt_counter = ps.vmem_majflt_counter;
1090 pse.cpu_user_counter = ps.cpu_user_counter;
1092 pse.cpu_system_counter = ps.cpu_system_counter;
1096 case 'R': running++; break;
1097 case 'S': sleeping++; break;
1098 case 'D': blocked++; break;
1099 case 'Z': zombies++; break;
1100 case 'T': stopped++; break;
1101 case 'W': paging++; break;
1104 ps_list_add (ps.name, NULL, &pse); //!!! cmdline should be here instead of NULL
1109 ps_submit_state ("running", running);
1110 ps_submit_state ("sleeping", sleeping);
1111 ps_submit_state ("zombies", zombies);
1112 ps_submit_state ("stopped", stopped);
1113 ps_submit_state ("paging", paging);
1114 ps_submit_state ("blocked", blocked);
1116 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1117 ps_submit_proc_list (ps_ptr);
1118 /* #endif KERNEL_LINUX */
1131 char cmdline[ARG_MAX];
1132 struct kinfo_proc *procs; /* array of processes */
1134 int count; /* returns number of processes */
1138 procstat_entry_t pse;
1142 /* Open the kvm interface, get a descriptor */
1143 if ((kd = kvm_open(NULL, NULL, NULL, 0, errbuf)) == NULL) {
1144 ERROR ("Cannot open kvm interface: %s", errbuf);
1148 /* Get the list of processes. */
1149 if ((procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, &count)) == NULL) {
1151 ERROR ("Cannot get kvm processes list: %s", kvm_geterr(kd));
1155 /* Iterate through the processes in kinfo_proc */
1156 for (i=0; i < count; i++) {
1157 // retrieve the arguments
1159 argv = kvm_getargv(kd, (const struct kinfo_proc *) &(procs[i]), 0);
1162 while (argv[j] && strlen(cmdline) <= ARG_MAX) {
1164 strncat(cmdline, " ", 1);
1165 strncat(cmdline, argv[j], strlen(argv[j]));
1170 pse.id = procs[i].ki_pid;
1174 pse.num_lwp = procs[i].ki_numthreads;
1176 pse.vmem_rss = procs[i].ki_rssize * getpagesize();
1177 pse.vmem_minflt = 0;
1178 pse.vmem_minflt_counter = procs[i].ki_rusage.ru_minflt;
1179 pse.vmem_majflt = 0;
1180 pse.vmem_majflt_counter = procs[i].ki_rusage.ru_majflt;
1183 pse.cpu_user_counter = procs[i].ki_rusage.ru_utime.tv_sec*1000 + procs[i].ki_rusage.ru_utime.tv_usec;
1185 pse.cpu_system_counter = procs[i].ki_rusage.ru_stime.tv_sec*1000 + procs[i].ki_rusage.ru_stime.tv_usec;
1187 switch (procs[i].ki_stat) {
1188 case SSTOP: stopped++; break;
1189 case SSLEEP: sleeping++; break;
1190 case SRUN: running++; break;
1191 case SIDL: idle++; break;
1192 case SWAIT: wait++; break;
1193 case SLOCK: blocked++; break;
1194 case SZOMB: zombies++; break;
1197 ps_list_add (procs[i].ki_comm, cmdline, &pse);
1200 if (kd) kvm_close(kd);
1202 ps_submit_state ("running", running);
1203 ps_submit_state ("sleeping", sleeping);
1204 ps_submit_state ("zombies", zombies);
1205 ps_submit_state ("stopped", stopped);
1206 ps_submit_state ("blocked", blocked);
1207 ps_submit_state ("idle", idle);
1208 ps_submit_state ("wait", wait);
1210 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1211 ps_submit_proc_list (ps_ptr);
1213 #endif /* HAVE_LIBKVM */
1218 void module_register (void)
1220 plugin_register_config ("processes", ps_config,
1221 config_keys, config_keys_num);
1222 plugin_register_init ("processes", ps_init);
1223 plugin_register_read ("processes", ps_read);
1224 } /* void module_register */