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 = 2;
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)
178 if ((new = (procstat_t *) malloc (sizeof (procstat_t))) == NULL)
180 memset (new, 0, sizeof (procstat_t));
181 sstrncpy (new->name, name, sizeof (new->name));
186 DEBUG ("ProcessMatch: adding \"%s\" as criteria to process %s.", regexp, name);
187 if ((new->re = (regex_t *) malloc (sizeof (regex_t))) != NULL)
189 if (regcomp(new->re, regexp, REG_EXTENDED|REG_NOSUB) != 0)
191 DEBUG ("ProcessMatch: compiling the regular expression \"%s\" failed.", regexp);
195 DEBUG("ProcessMatch: malloc failed when allocating memory for regexp!");
198 DEBUG("ProcessMatch: regexp '%s' met in config file, but regexps are not supported!", regexp);
202 for (ptr = list_head_g; ptr != NULL; ptr = ptr->next)
204 if (strcmp (ptr->name, name) == 0)
206 if (ptr->next == NULL)
216 /* try to match name against entry, returns 1 if success */
217 static int ps_list_match (const char *name, const char *cmdline, procstat_t *ps)
219 if ((ps->re != NULL) && (regexec(ps->re, (strlen(cmdline)!=0)?cmdline:name, 0, NULL, 0) == 0))
221 if (strcmp (ps->name, name) == 0) {
227 /* add process entry to 'instances' of process 'name' (or refresh it) */
228 static void ps_list_add (const char *name, const char *cmdline, procstat_entry_t *entry)
231 procstat_entry_t *pse;
236 for (ps = list_head_g; ps != NULL; ps = ps->next)
239 if ((ps_list_match (name, cmdline, ps)) == 0)
242 for (pse = ps->instances; pse != NULL; pse = pse->next)
243 if ((pse->id == entry->id) || (pse->next == NULL))
246 if ((pse == NULL) || (pse->id != entry->id))
248 procstat_entry_t *new;
250 new = (procstat_entry_t *) malloc (sizeof (procstat_entry_t));
253 memset (new, 0, sizeof (procstat_entry_t));
265 pse->num_proc = entry->num_proc;
266 pse->num_lwp = entry->num_lwp;
267 pse->vmem_rss = entry->vmem_rss;
269 ps->num_proc += pse->num_proc;
270 ps->num_lwp += pse->num_lwp;
271 ps->vmem_rss += pse->vmem_rss;
273 if ((entry->vmem_minflt_counter == 0)
274 && (entry->vmem_majflt_counter == 0))
276 pse->vmem_minflt_counter += entry->vmem_minflt;
277 pse->vmem_minflt = entry->vmem_minflt;
279 pse->vmem_majflt_counter += entry->vmem_majflt;
280 pse->vmem_majflt = entry->vmem_majflt;
284 if (entry->vmem_minflt_counter < pse->vmem_minflt_counter)
286 pse->vmem_minflt = entry->vmem_minflt_counter
287 + (ULONG_MAX - pse->vmem_minflt_counter);
291 pse->vmem_minflt = entry->vmem_minflt_counter - pse->vmem_minflt_counter;
293 pse->vmem_minflt_counter = entry->vmem_minflt_counter;
295 if (entry->vmem_majflt_counter < pse->vmem_majflt_counter)
297 pse->vmem_majflt = entry->vmem_majflt_counter
298 + (ULONG_MAX - pse->vmem_majflt_counter);
302 pse->vmem_majflt = entry->vmem_majflt_counter - pse->vmem_majflt_counter;
304 pse->vmem_majflt_counter = entry->vmem_majflt_counter;
307 ps->vmem_minflt_counter += pse->vmem_minflt;
308 ps->vmem_majflt_counter += pse->vmem_majflt;
310 if ((entry->cpu_user_counter == 0)
311 && (entry->cpu_system_counter == 0))
313 pse->cpu_user_counter += entry->cpu_user;
314 pse->cpu_user = entry->cpu_user;
316 pse->cpu_system_counter += entry->cpu_system;
317 pse->cpu_system = entry->cpu_system;
321 if (entry->cpu_user_counter < pse->cpu_user_counter)
323 pse->cpu_user = entry->cpu_user_counter
324 + (ULONG_MAX - pse->cpu_user_counter);
328 pse->cpu_user = entry->cpu_user_counter - pse->cpu_user_counter;
330 pse->cpu_user_counter = entry->cpu_user_counter;
332 if (entry->cpu_system_counter < pse->cpu_system_counter)
334 pse->cpu_system = entry->cpu_system_counter
335 + (ULONG_MAX - pse->cpu_system_counter);
339 pse->cpu_system = entry->cpu_system_counter - pse->cpu_system_counter;
341 pse->cpu_system_counter = entry->cpu_system_counter;
344 ps->cpu_user_counter += pse->cpu_user;
345 ps->cpu_system_counter += pse->cpu_system;
349 /* remove old entries from instances of processes in list_head_g */
350 static void ps_list_reset (void)
353 procstat_entry_t *pse;
354 procstat_entry_t *pse_prev;
356 for (ps = list_head_g; ps != NULL; ps = ps->next)
368 DEBUG ("Removing this procstat entry cause it's too old: "
369 "id = %lu; name = %s;",
372 if (pse_prev == NULL)
374 ps->instances = pse->next;
380 pse_prev->next = pse->next;
382 pse = pse_prev->next;
391 } /* while (pse != NULL) */
392 } /* for (ps = list_head_g; ps != NULL; ps = ps->next) */
395 /* put all pre-defined 'Process' names from config to list_head_g tree */
396 static int ps_config (const char *key, const char *value)
402 if (strcasecmp (key, "Process") == 0)
404 ps_list_register (value, NULL);
408 if (strcasecmp (key, "ProcessMatch") == 0)
410 new_val = strdup (value);
413 fields_num = strsplit (new_val, fields, 2);
419 ps_list_register (fields[0], fields[1]);
427 static int ps_init (void)
430 kern_return_t status;
432 port_host_self = mach_host_self ();
433 port_task_self = mach_task_self ();
435 if (pset_list != NULL)
437 vm_deallocate (port_task_self,
438 (vm_address_t) pset_list,
439 pset_list_len * sizeof (processor_set_t));
444 if ((status = host_processor_sets (port_host_self,
446 &pset_list_len)) != KERN_SUCCESS)
448 ERROR ("host_processor_sets failed: %s\n",
449 mach_error_string (status));
454 /* #endif HAVE_THREAD_INFO */
457 pagesize_g = sysconf(_SC_PAGESIZE);
458 DEBUG ("pagesize_g = %li; CONFIG_HZ = %i;",
459 pagesize_g, CONFIG_HZ);
460 #endif /* KERNEL_LINUX */
465 /* submit global state (e.g.: qty of zombies, running, etc..) */
466 static void ps_submit_state (const char *state, double value)
469 value_list_t vl = VALUE_LIST_INIT;
471 values[0].gauge = value;
475 vl.time = time (NULL);
476 strcpy (vl.host, hostname_g);
477 strcpy (vl.plugin, "processes");
478 strcpy (vl.plugin_instance, "");
479 strcpy (vl.type, "ps_state");
480 sstrncpy (vl.type_instance, state, sizeof (vl.type_instance));
482 plugin_dispatch_values (&vl);
485 /* submit info about specific process (e.g.: memory taken, cpu usage, etc..) */
486 static void ps_submit_proc_list (procstat_t *ps)
489 value_list_t vl = VALUE_LIST_INIT;
493 vl.time = time (NULL);
494 strcpy (vl.host, hostname_g);
495 strcpy (vl.plugin, "processes");
496 sstrncpy (vl.plugin_instance, ps->name, sizeof (vl.plugin_instance));
498 strcpy (vl.type, "ps_rss");
499 vl.values[0].gauge = ps->vmem_rss;
501 plugin_dispatch_values (&vl);
503 strcpy (vl.type, "ps_cputime");
504 vl.values[0].counter = ps->cpu_user_counter;
505 vl.values[1].counter = ps->cpu_system_counter;
507 plugin_dispatch_values (&vl);
509 strcpy (vl.type, "ps_count");
510 vl.values[0].gauge = ps->num_proc;
511 vl.values[1].gauge = ps->num_lwp;
513 plugin_dispatch_values (&vl);
515 strcpy (vl.type, "ps_pagefaults");
516 vl.values[0].counter = ps->vmem_minflt_counter;
517 vl.values[1].counter = ps->vmem_majflt_counter;
519 plugin_dispatch_values (&vl);
521 DEBUG ("name = %s; num_proc = %lu; num_lwp = %lu; vmem_rss = %lu; "
522 "vmem_minflt_counter = %lu; vmem_majflt_counter = %lu; "
523 "cpu_user_counter = %lu; cpu_system_counter = %lu;",
524 ps->name, ps->num_proc, ps->num_lwp, ps->vmem_rss,
525 ps->vmem_minflt_counter, ps->vmem_majflt_counter,
526 ps->cpu_user_counter, ps->cpu_system_counter);
527 } /* void ps_submit_proc_list */
529 /* ------- additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
531 static int *ps_read_tasks (int pid)
534 int list_size = 1; /* size of allocated space, in elements */
535 int list_len = 0; /* number of currently used elements */
541 ssnprintf (dirname, sizeof (dirname), "/proc/%i/task", pid);
543 if ((dh = opendir (dirname)) == NULL)
545 DEBUG ("Failed to open directory `%s'", dirname);
549 while ((ent = readdir (dh)) != NULL)
551 if (!isdigit (ent->d_name[0]))
554 if ((list_len + 1) >= list_size)
557 int new_size = 2 * list_size;
558 /* Comes in sizes: 2, 4, 8, 16, ... */
560 new_ptr = (int *) realloc (list, (size_t) (sizeof (int) * new_size));
565 ERROR ("processes plugin: "
566 "Failed to allocate more memory.");
571 list_size = new_size;
573 memset (list + list_len, 0, sizeof (int) * (list_size - list_len));
576 list[list_len] = atoi (ent->d_name);
577 if (list[list_len] != 0)
586 assert (list_len < list_size);
587 assert (list[list_len] == 0);
590 } /* int *ps_read_tasks */
592 int ps_read_process (int pid, procstat_t *ps, char *state)
607 long long unsigned cpu_user_counter;
608 long long unsigned cpu_system_counter;
609 long long unsigned vmem_rss;
611 memset (ps, 0, sizeof (procstat_t));
613 ssnprintf (filename, sizeof (filename), "/proc/%i/stat", pid);
615 if ((fh = fopen (filename, "r")) == NULL)
618 if (fgets (buffer, 1024, fh) == NULL)
626 fields_len = strsplit (buffer, fields, 64);
629 DEBUG ("processes plugin: ps_read_process (pid = %i):"
630 " `%s' has only %i fields..",
631 (int) pid, filename, fields_len);
635 /* copy the name, strip brackets in the process */
636 name_len = strlen (fields[1]) - 2;
637 if ((fields[1][0] != '(') || (fields[1][name_len + 1] != ')'))
639 DEBUG ("No brackets found in process name: `%s'", fields[1]);
642 fields[1] = fields[1] + 1;
643 fields[1][name_len] = '\0';
644 strncpy (ps->name, fields[1], PROCSTAT_NAME_LEN);
646 ppid = atoi (fields[3]);
648 *state = fields[2][0];
655 else if ((tasks = ps_read_tasks (pid)) == NULL)
657 /* Kernel 2.4 or so */
665 for (i = 0; tasks[i] != 0; i++)
672 /* Leave the rest at zero if this is only a zombi */
673 if (ps->num_proc == 0)
675 DEBUG ("processes plugin: This is only a zombi: pid = %i; "
676 "name = %s;", pid, ps->name);
680 cpu_user_counter = atoll (fields[13]);
681 cpu_system_counter = atoll (fields[14]);
682 vmem_rss = atoll (fields[23]);
683 ps->vmem_minflt_counter = atol (fields[9]);
684 ps->vmem_majflt_counter = atol (fields[11]);
686 /* Convert jiffies to useconds */
687 cpu_user_counter = cpu_user_counter * 1000000 / CONFIG_HZ;
688 cpu_system_counter = cpu_system_counter * 1000000 / CONFIG_HZ;
689 vmem_rss = vmem_rss * pagesize_g;
691 ps->cpu_user_counter = (unsigned long) cpu_user_counter;
692 ps->cpu_system_counter = (unsigned long) cpu_system_counter;
693 ps->vmem_rss = (unsigned long) vmem_rss;
697 } /* int ps_read_process (...) */
698 #endif /* KERNEL_LINUX */
701 static int mach_get_task_name (task_t t, int *pid, char *name, size_t name_max_len)
705 struct kinfo_proc kp;
710 mib[2] = KERN_PROC_PID;
712 if (pid_for_task (t, pid) != KERN_SUCCESS)
716 kp_size = sizeof (kp);
717 if (sysctl (mib, 4, &kp, &kp_size, NULL, 0) != 0)
720 if (name_max_len > (MAXCOMLEN + 1))
721 name_max_len = MAXCOMLEN + 1;
723 strncpy (name, kp.kp_proc.p_comm, name_max_len - 1);
724 name[name_max_len - 1] = '\0';
726 DEBUG ("pid = %i; name = %s;", *pid, name);
728 /* We don't do the special handling for `p_comm == "LaunchCFMApp"' as
729 * `top' does it, because it is a lot of work and only used when
730 * debugging. -octo */
734 #endif /* HAVE_THREAD_INFO */
735 /* ------- end of additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
737 /* do actual readings from kernel */
738 static int ps_read (void)
741 kern_return_t status;
744 processor_set_t port_pset_priv;
747 task_array_t task_list;
748 mach_msg_type_number_t task_list_len;
751 char task_name[MAXCOMLEN + 1];
754 thread_act_array_t thread_list;
755 mach_msg_type_number_t thread_list_len;
756 thread_basic_info_data_t thread_data;
757 mach_msg_type_number_t thread_data_len;
766 procstat_entry_t pse;
771 * The Mach-concept is a little different from the traditional UNIX
772 * concept: All the work is done in threads. Threads are contained in
773 * `tasks'. Therefore, `task status' doesn't make much sense, since
774 * it's actually a `thread status'.
775 * Tasks are assigned to sets of processors, so that's where you go to
778 for (pset = 0; pset < pset_list_len; pset++)
780 if ((status = host_processor_set_priv (port_host_self,
782 &port_pset_priv)) != KERN_SUCCESS)
784 ERROR ("host_processor_set_priv failed: %s\n",
785 mach_error_string (status));
789 if ((status = processor_set_tasks (port_pset_priv,
791 &task_list_len)) != KERN_SUCCESS)
793 ERROR ("processor_set_tasks failed: %s\n",
794 mach_error_string (status));
795 mach_port_deallocate (port_task_self, port_pset_priv);
799 for (task = 0; task < task_list_len; task++)
802 if (mach_get_task_name (task_list[task],
804 task_name, PROCSTAT_NAME_LEN) == 0)
806 /* search for at least one match */
807 for (ps = list_head_g; ps != NULL; ps = ps->next)
808 if (ps_list_match(task_name, NULL, ps) == 1) //!!! cmdline should be here instead of NULL
812 /* Collect more detailed statistics for this process */
815 task_basic_info_data_t task_basic_info;
816 mach_msg_type_number_t task_basic_info_len;
817 task_events_info_data_t task_events_info;
818 mach_msg_type_number_t task_events_info_len;
819 task_absolutetime_info_data_t task_absolutetime_info;
820 mach_msg_type_number_t task_absolutetime_info_len;
822 memset (&pse, '\0', sizeof (pse));
825 task_basic_info_len = TASK_BASIC_INFO_COUNT;
826 status = task_info (task_list[task],
828 (task_info_t) &task_basic_info,
829 &task_basic_info_len);
830 if (status != KERN_SUCCESS)
832 ERROR ("task_info failed: %s",
833 mach_error_string (status));
834 continue; /* with next thread_list */
837 task_events_info_len = TASK_EVENTS_INFO_COUNT;
838 status = task_info (task_list[task],
840 (task_info_t) &task_events_info,
841 &task_events_info_len);
842 if (status != KERN_SUCCESS)
844 ERROR ("task_info failed: %s",
845 mach_error_string (status));
846 continue; /* with next thread_list */
849 task_absolutetime_info_len = TASK_ABSOLUTETIME_INFO_COUNT;
850 status = task_info (task_list[task],
851 TASK_ABSOLUTETIME_INFO,
852 (task_info_t) &task_absolutetime_info,
853 &task_absolutetime_info_len);
854 if (status != KERN_SUCCESS)
856 ERROR ("task_info failed: %s",
857 mach_error_string (status));
858 continue; /* with next thread_list */
862 pse.vmem_rss = task_basic_info.resident_size;
864 pse.vmem_minflt_counter = task_events_info.cow_faults;
865 pse.vmem_majflt_counter = task_events_info.faults;
867 pse.cpu_user_counter = task_absolutetime_info.total_user;
868 pse.cpu_system_counter = task_absolutetime_info.total_system;
871 status = task_threads (task_list[task], &thread_list,
873 if (status != KERN_SUCCESS)
875 /* Apple's `top' treats this case a zombie. It
876 * makes sense to some extend: A `zombie'
877 * thread is nonsense, since the task/process
880 DEBUG ("task_threads failed: %s",
881 mach_error_string (status));
882 if (task_list[task] != port_task_self)
883 mach_port_deallocate (port_task_self,
885 continue; /* with next task_list */
888 for (thread = 0; thread < thread_list_len; thread++)
890 thread_data_len = THREAD_BASIC_INFO_COUNT;
891 status = thread_info (thread_list[thread],
893 (thread_info_t) &thread_data,
895 if (status != KERN_SUCCESS)
897 ERROR ("thread_info failed: %s",
898 mach_error_string (status));
899 if (task_list[task] != port_task_self)
900 mach_port_deallocate (port_task_self,
901 thread_list[thread]);
902 continue; /* with next thread_list */
908 switch (thread_data.run_state)
910 case TH_STATE_RUNNING:
913 case TH_STATE_STOPPED:
914 /* What exactly is `halted'? */
915 case TH_STATE_HALTED:
918 case TH_STATE_WAITING:
921 case TH_STATE_UNINTERRUPTIBLE:
924 /* There is no `zombie' case here,
925 * since there are no zombie-threads.
926 * There's only zombie tasks, which are
929 WARNING ("Unknown thread status: %s",
930 thread_data.run_state);
932 } /* switch (thread_data.run_state) */
934 if (task_list[task] != port_task_self)
936 status = mach_port_deallocate (port_task_self,
937 thread_list[thread]);
938 if (status != KERN_SUCCESS)
939 ERROR ("mach_port_deallocate failed: %s",
940 mach_error_string (status));
942 } /* for (thread_list) */
944 if ((status = vm_deallocate (port_task_self,
945 (vm_address_t) thread_list,
946 thread_list_len * sizeof (thread_act_t)))
949 ERROR ("vm_deallocate failed: %s",
950 mach_error_string (status));
955 /* Only deallocate the task port, if it isn't our own.
956 * Don't know what would happen in that case, but this
957 * is what Apple's top does.. ;) */
958 if (task_list[task] != port_task_self)
960 status = mach_port_deallocate (port_task_self,
962 if (status != KERN_SUCCESS)
963 ERROR ("mach_port_deallocate failed: %s",
964 mach_error_string (status));
968 ps_list_add (task_name, NULL, &pse); //!!! cmdline should be here instead of NULL
969 } /* for (task_list) */
971 if ((status = vm_deallocate (port_task_self,
972 (vm_address_t) task_list,
973 task_list_len * sizeof (task_t))) != KERN_SUCCESS)
975 ERROR ("vm_deallocate failed: %s",
976 mach_error_string (status));
981 if ((status = mach_port_deallocate (port_task_self, port_pset_priv))
984 ERROR ("mach_port_deallocate failed: %s",
985 mach_error_string (status));
987 } /* for (pset_list) */
989 ps_submit_state ("running", running);
990 ps_submit_state ("sleeping", sleeping);
991 ps_submit_state ("zombies", zombies);
992 ps_submit_state ("stopped", stopped);
993 ps_submit_state ("blocked", blocked);
995 for (ps = list_head_g; ps != NULL; ps = ps->next)
996 ps_submit_proc_list (ps);
997 /* #endif HAVE_THREAD_INFO */
1013 procstat_entry_t pse;
1018 running = sleeping = zombies = stopped = paging = blocked = 0;
1021 if ((proc = opendir ("/proc")) == NULL)
1024 ERROR ("Cannot open `/proc': %s",
1025 sstrerror (errno, errbuf, sizeof (errbuf)));
1029 while ((ent = readdir (proc)) != NULL)
1031 if (!isdigit (ent->d_name[0]))
1034 if ((pid = atoi (ent->d_name)) < 1)
1037 status = ps_read_process (pid, &ps, &state);
1040 DEBUG ("ps_read_process failed: %i", status);
1047 pse.num_proc = ps.num_proc;
1048 pse.num_lwp = ps.num_lwp;
1049 pse.vmem_rss = ps.vmem_rss;
1051 pse.vmem_minflt = 0;
1052 pse.vmem_minflt_counter = ps.vmem_minflt_counter;
1053 pse.vmem_majflt = 0;
1054 pse.vmem_majflt_counter = ps.vmem_majflt_counter;
1057 pse.cpu_user_counter = ps.cpu_user_counter;
1059 pse.cpu_system_counter = ps.cpu_system_counter;
1063 case 'R': running++; break;
1064 case 'S': sleeping++; break;
1065 case 'D': blocked++; break;
1066 case 'Z': zombies++; break;
1067 case 'T': stopped++; break;
1068 case 'W': paging++; break;
1071 ps_list_add (ps.name, NULL, &pse); //!!! cmdline should be here instead of NULL
1076 ps_submit_state ("running", running);
1077 ps_submit_state ("sleeping", sleeping);
1078 ps_submit_state ("zombies", zombies);
1079 ps_submit_state ("stopped", stopped);
1080 ps_submit_state ("paging", paging);
1081 ps_submit_state ("blocked", blocked);
1083 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1084 ps_submit_proc_list (ps_ptr);
1085 /* #endif KERNEL_LINUX */
1098 char cmdline[ARG_MAX];
1099 struct kinfo_proc *procs; /* array of processes */
1101 int count; /* returns number of processes */
1105 procstat_entry_t pse;
1109 /* Open the kvm interface, get a descriptor */
1110 if ((kd = kvm_open(NULL, NULL, NULL, 0, errbuf)) == NULL) {
1111 ERROR ("Cannot open kvm interface: %s", errbuf);
1115 /* Get the list of processes. */
1116 if ((procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, &count)) == NULL) {
1118 ERROR ("Cannot get kvm processes list: %s", kvm_geterr(kd));
1122 /* Iterate through the processes in kinfo_proc */
1123 for (i=0; i < count; i++) {
1124 // retrieve the arguments
1126 argv = kvm_getargv(kd, (const struct kinfo_proc *) &(procs[i]), 0);
1129 while (argv[j] && strlen(cmdline) <= ARG_MAX) {
1131 strncat(cmdline, " ", 1);
1132 strncat(cmdline, argv[j], strlen(argv[j]));
1137 pse.id = procs[i].ki_pid;
1141 pse.num_lwp = procs[i].ki_numthreads;
1143 pse.vmem_rss = procs[i].ki_rssize * getpagesize();
1144 pse.vmem_minflt = 0;
1145 pse.vmem_minflt_counter = procs[i].ki_rusage.ru_minflt;
1146 pse.vmem_majflt = 0;
1147 pse.vmem_majflt_counter = procs[i].ki_rusage.ru_majflt;
1150 pse.cpu_user_counter = procs[i].ki_rusage.ru_utime.tv_sec*1000 + procs[i].ki_rusage.ru_utime.tv_usec;
1152 pse.cpu_system_counter = procs[i].ki_rusage.ru_stime.tv_sec*1000 + procs[i].ki_rusage.ru_stime.tv_usec;
1154 switch (procs[i].ki_stat) {
1155 case SSTOP: stopped++; break;
1156 case SSLEEP: sleeping++; break;
1157 case SRUN: running++; break;
1158 case SIDL: idle++; break;
1159 case SWAIT: wait++; break;
1160 case SLOCK: blocked++; break;
1161 case SZOMB: zombies++; break;
1164 ps_list_add (procs[i].ki_comm, cmdline, &pse);
1167 if (kd) kvm_close(kd);
1169 ps_submit_state ("running", running);
1170 ps_submit_state ("sleeping", sleeping);
1171 ps_submit_state ("zombies", zombies);
1172 ps_submit_state ("stopped", stopped);
1173 ps_submit_state ("blocked", blocked);
1174 ps_submit_state ("idle", idle);
1175 ps_submit_state ("wait", wait);
1177 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1178 ps_submit_proc_list (ps_ptr);
1180 #endif /* HAVE_LIBKVM */
1185 void module_register (void)
1187 plugin_register_config ("processes", ps_config,
1188 config_keys, config_keys_num);
1189 plugin_register_init ("processes", ps_init);
1190 plugin_register_read ("processes", ps_read);
1191 } /* void module_register */