2 * collectd - src/processes.c
3 * Copyright (C) 2005 Lyonel Vincent
4 * Copyright (C) 2006-2008 Florian octo Forster
5 * Copyright (C) 2008 Oleg King
6 * Copyright (C) 2009 Sebastian Harl
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 * Lyonel Vincent <lyonel at ezix.org>
24 * Florian octo Forster <octo at verplant.org>
25 * Oleg King <king2 at kaluga.ru>
26 * Sebastian Harl <sh at tokkee.org>
32 #include "configfile.h"
34 /* Include header files for the mach system, if they exist.. */
36 # if HAVE_MACH_MACH_INIT_H
37 # include <mach/mach_init.h>
39 # if HAVE_MACH_HOST_PRIV_H
40 # include <mach/host_priv.h>
42 # if HAVE_MACH_MACH_ERROR_H
43 # include <mach/mach_error.h>
45 # if HAVE_MACH_MACH_HOST_H
46 # include <mach/mach_host.h>
48 # if HAVE_MACH_MACH_PORT_H
49 # include <mach/mach_port.h>
51 # if HAVE_MACH_MACH_TYPES_H
52 # include <mach/mach_types.h>
54 # if HAVE_MACH_MESSAGE_H
55 # include <mach/message.h>
57 # if HAVE_MACH_PROCESSOR_SET_H
58 # include <mach/processor_set.h>
61 # include <mach/task.h>
63 # if HAVE_MACH_THREAD_ACT_H
64 # include <mach/thread_act.h>
66 # if HAVE_MACH_VM_REGION_H
67 # include <mach/vm_region.h>
69 # if HAVE_MACH_VM_MAP_H
70 # include <mach/vm_map.h>
72 # if HAVE_MACH_VM_PROT_H
73 # include <mach/vm_prot.h>
75 # if HAVE_SYS_SYSCTL_H
76 # include <sys/sysctl.h>
78 /* #endif HAVE_THREAD_INFO */
81 # if HAVE_LINUX_CONFIG_H
82 # include <linux/config.h>
85 # define CONFIG_HZ 100
87 /* #endif KERNEL_LINUX */
89 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD
91 # include <sys/param.h>
92 # include <sys/sysctl.h>
93 # include <sys/user.h>
94 # include <sys/proc.h>
95 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
98 # error "No applicable input method."
106 # define ARG_MAX 4096
111 static const char *config_keys[] =
116 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
118 typedef struct procstat_entry_s
123 unsigned long num_proc;
124 unsigned long num_lwp;
125 unsigned long vmem_size;
126 unsigned long vmem_rss;
127 unsigned long stack_size;
129 unsigned long vmem_minflt;
130 unsigned long vmem_majflt;
131 unsigned long vmem_minflt_counter;
132 unsigned long vmem_majflt_counter;
134 unsigned long cpu_user;
135 unsigned long cpu_system;
136 unsigned long cpu_user_counter;
137 unsigned long cpu_system_counter;
145 struct procstat_entry_s *next;
148 #define PROCSTAT_NAME_LEN 256
149 typedef struct procstat
151 char name[PROCSTAT_NAME_LEN];
156 unsigned long num_proc;
157 unsigned long num_lwp;
158 unsigned long vmem_size;
159 unsigned long vmem_rss;
160 unsigned long stack_size;
162 unsigned long vmem_minflt_counter;
163 unsigned long vmem_majflt_counter;
165 unsigned long cpu_user_counter;
166 unsigned long cpu_system_counter;
174 struct procstat *next;
175 struct procstat_entry_s *instances;
178 static procstat_t *list_head_g = NULL;
181 static mach_port_t port_host_self;
182 static mach_port_t port_task_self;
184 static processor_set_name_array_t pset_list;
185 static mach_msg_type_number_t pset_list_len;
186 /* #endif HAVE_THREAD_INFO */
189 static long pagesize_g;
190 /* #endif KERNEL_LINUX */
192 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD
193 /* no global variables */
194 #endif /* HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
196 /* put name of process from config to list_head_g tree
197 list_head_g is a list of 'procstat_t' structs with
198 processes names we want to watch */
199 static void ps_list_register (const char *name, const char *regexp)
205 new = (procstat_t *) malloc (sizeof (procstat_t));
208 ERROR ("processes plugin: ps_list_register: malloc failed.");
211 memset (new, 0, sizeof (procstat_t));
212 sstrncpy (new->name, name, sizeof (new->name));
217 DEBUG ("ProcessMatch: adding \"%s\" as criteria to process %s.", regexp, name);
218 new->re = (regex_t *) malloc (sizeof (regex_t));
221 ERROR ("processes plugin: ps_list_register: malloc failed.");
226 status = regcomp (new->re, regexp, REG_EXTENDED | REG_NOSUB);
229 DEBUG ("ProcessMatch: compiling the regular expression \"%s\" failed.", regexp);
237 ERROR ("processes plugin: ps_list_register: "
238 "Regular expression \"%s\" found in config "
239 "file, but support for regular expressions "
240 "has been dispabled at compile time.",
247 for (ptr = list_head_g; ptr != NULL; ptr = ptr->next)
249 if (strcmp (ptr->name, name) == 0)
251 WARNING ("processes plugin: You have configured more "
252 "than one `Process' or "
253 "`ProcessMatch' with the same name. "
254 "All but the first setting will be "
261 if (ptr->next == NULL)
269 } /* void ps_list_register */
271 /* try to match name against entry, returns 1 if success */
272 static int ps_list_match (const char *name, const char *cmdline, procstat_t *ps)
281 if ((str == NULL) || (str[0] == 0))
284 assert (str != NULL);
286 status = regexec (ps->re, str,
295 if (strcmp (ps->name, name) == 0)
299 } /* int ps_list_match */
301 /* add process entry to 'instances' of process 'name' (or refresh it) */
302 static void ps_list_add (const char *name, const char *cmdline, procstat_entry_t *entry)
305 procstat_entry_t *pse;
310 for (ps = list_head_g; ps != NULL; ps = ps->next)
312 if ((ps_list_match (name, cmdline, ps)) == 0)
315 for (pse = ps->instances; pse != NULL; pse = pse->next)
316 if ((pse->id == entry->id) || (pse->next == NULL))
319 if ((pse == NULL) || (pse->id != entry->id))
321 procstat_entry_t *new;
323 new = (procstat_entry_t *) malloc (sizeof (procstat_entry_t));
326 memset (new, 0, sizeof (procstat_entry_t));
338 pse->num_proc = entry->num_proc;
339 pse->num_lwp = entry->num_lwp;
340 pse->vmem_size = entry->vmem_size;
341 pse->vmem_rss = entry->vmem_rss;
342 pse->stack_size = entry->stack_size;
343 pse->io_rchar = entry->io_rchar;
344 pse->io_wchar = entry->io_wchar;
345 pse->io_syscr = entry->io_syscr;
346 pse->io_syscw = entry->io_syscw;
348 ps->num_proc += pse->num_proc;
349 ps->num_lwp += pse->num_lwp;
350 ps->vmem_size += pse->vmem_size;
351 ps->vmem_rss += pse->vmem_rss;
352 ps->stack_size += pse->stack_size;
354 ps->io_rchar += ((pse->io_rchar == -1)?0:pse->io_rchar);
355 ps->io_wchar += ((pse->io_wchar == -1)?0:pse->io_wchar);
356 ps->io_syscr += ((pse->io_syscr == -1)?0:pse->io_syscr);
357 ps->io_syscw += ((pse->io_syscw == -1)?0:pse->io_syscw);
359 if ((entry->vmem_minflt_counter == 0)
360 && (entry->vmem_majflt_counter == 0))
362 pse->vmem_minflt_counter += entry->vmem_minflt;
363 pse->vmem_minflt = entry->vmem_minflt;
365 pse->vmem_majflt_counter += entry->vmem_majflt;
366 pse->vmem_majflt = entry->vmem_majflt;
370 if (entry->vmem_minflt_counter < pse->vmem_minflt_counter)
372 pse->vmem_minflt = entry->vmem_minflt_counter
373 + (ULONG_MAX - pse->vmem_minflt_counter);
377 pse->vmem_minflt = entry->vmem_minflt_counter - pse->vmem_minflt_counter;
379 pse->vmem_minflt_counter = entry->vmem_minflt_counter;
381 if (entry->vmem_majflt_counter < pse->vmem_majflt_counter)
383 pse->vmem_majflt = entry->vmem_majflt_counter
384 + (ULONG_MAX - pse->vmem_majflt_counter);
388 pse->vmem_majflt = entry->vmem_majflt_counter - pse->vmem_majflt_counter;
390 pse->vmem_majflt_counter = entry->vmem_majflt_counter;
393 ps->vmem_minflt_counter += pse->vmem_minflt;
394 ps->vmem_majflt_counter += pse->vmem_majflt;
396 if ((entry->cpu_user_counter == 0)
397 && (entry->cpu_system_counter == 0))
399 pse->cpu_user_counter += entry->cpu_user;
400 pse->cpu_user = entry->cpu_user;
402 pse->cpu_system_counter += entry->cpu_system;
403 pse->cpu_system = entry->cpu_system;
407 if (entry->cpu_user_counter < pse->cpu_user_counter)
409 pse->cpu_user = entry->cpu_user_counter
410 + (ULONG_MAX - pse->cpu_user_counter);
414 pse->cpu_user = entry->cpu_user_counter - pse->cpu_user_counter;
416 pse->cpu_user_counter = entry->cpu_user_counter;
418 if (entry->cpu_system_counter < pse->cpu_system_counter)
420 pse->cpu_system = entry->cpu_system_counter
421 + (ULONG_MAX - pse->cpu_system_counter);
425 pse->cpu_system = entry->cpu_system_counter - pse->cpu_system_counter;
427 pse->cpu_system_counter = entry->cpu_system_counter;
430 ps->cpu_user_counter += pse->cpu_user;
431 ps->cpu_system_counter += pse->cpu_system;
435 /* remove old entries from instances of processes in list_head_g */
436 static void ps_list_reset (void)
439 procstat_entry_t *pse;
440 procstat_entry_t *pse_prev;
442 for (ps = list_head_g; ps != NULL; ps = ps->next)
460 DEBUG ("Removing this procstat entry cause it's too old: "
461 "id = %lu; name = %s;",
464 if (pse_prev == NULL)
466 ps->instances = pse->next;
472 pse_prev->next = pse->next;
474 pse = pse_prev->next;
483 } /* while (pse != NULL) */
484 } /* for (ps = list_head_g; ps != NULL; ps = ps->next) */
487 /* put all pre-defined 'Process' names from config to list_head_g tree */
488 static int ps_config (const char *key, const char *value)
490 if (strcasecmp (key, "Process") == 0)
492 ps_list_register (value, NULL);
494 else if (strcasecmp (key, "ProcessMatch") == 0)
500 new_val = strdup (value);
501 if (new_val == NULL) {
502 ERROR ("processes plugin: strdup failed when processing "
503 "`ProcessMatch %s'.", value);
507 fields_num = strsplit (new_val, fields,
508 STATIC_ARRAY_SIZE (fields));
511 ERROR ("processes plugin: `ProcessMatch' needs exactly "
512 "two string arguments.");
516 ps_list_register (fields[0], fields[1]);
521 ERROR ("processes plugin: The `%s' configuration option is not "
522 "understood and will be ignored.", key);
529 static int ps_init (void)
532 kern_return_t status;
534 port_host_self = mach_host_self ();
535 port_task_self = mach_task_self ();
537 if (pset_list != NULL)
539 vm_deallocate (port_task_self,
540 (vm_address_t) pset_list,
541 pset_list_len * sizeof (processor_set_t));
546 if ((status = host_processor_sets (port_host_self,
548 &pset_list_len)) != KERN_SUCCESS)
550 ERROR ("host_processor_sets failed: %s\n",
551 mach_error_string (status));
556 /* #endif HAVE_THREAD_INFO */
559 pagesize_g = sysconf(_SC_PAGESIZE);
560 DEBUG ("pagesize_g = %li; CONFIG_HZ = %i;",
561 pagesize_g, CONFIG_HZ);
562 /* #endif KERNEL_LINUX */
564 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD
565 /* no initialization */
566 #endif /* HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
571 /* submit global state (e.g.: qty of zombies, running, etc..) */
572 static void ps_submit_state (const char *state, double value)
575 value_list_t vl = VALUE_LIST_INIT;
577 values[0].gauge = value;
581 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
582 sstrncpy (vl.plugin, "processes", sizeof (vl.plugin));
583 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
584 sstrncpy (vl.type, "ps_state", sizeof (vl.type));
585 sstrncpy (vl.type_instance, state, sizeof (vl.type_instance));
587 plugin_dispatch_values (&vl);
590 /* submit info about specific process (e.g.: memory taken, cpu usage, etc..) */
591 static void ps_submit_proc_list (procstat_t *ps)
594 value_list_t vl = VALUE_LIST_INIT;
598 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
599 sstrncpy (vl.plugin, "processes", sizeof (vl.plugin));
600 sstrncpy (vl.plugin_instance, ps->name, sizeof (vl.plugin_instance));
602 sstrncpy (vl.type, "ps_vm", sizeof (vl.type));
603 vl.values[0].gauge = ps->vmem_size;
605 plugin_dispatch_values (&vl);
607 sstrncpy (vl.type, "ps_rss", sizeof (vl.type));
608 vl.values[0].gauge = ps->vmem_rss;
610 plugin_dispatch_values (&vl);
612 sstrncpy (vl.type, "ps_stacksize", sizeof (vl.type));
613 vl.values[0].gauge = ps->stack_size;
615 plugin_dispatch_values (&vl);
617 sstrncpy (vl.type, "ps_cputime", sizeof (vl.type));
618 vl.values[0].counter = ps->cpu_user_counter;
619 vl.values[1].counter = ps->cpu_system_counter;
621 plugin_dispatch_values (&vl);
623 sstrncpy (vl.type, "ps_count", sizeof (vl.type));
624 vl.values[0].gauge = ps->num_proc;
625 vl.values[1].gauge = ps->num_lwp;
627 plugin_dispatch_values (&vl);
629 sstrncpy (vl.type, "ps_pagefaults", sizeof (vl.type));
630 vl.values[0].counter = ps->vmem_minflt_counter;
631 vl.values[1].counter = ps->vmem_majflt_counter;
633 plugin_dispatch_values (&vl);
635 if ( (ps->io_rchar != -1) && (ps->io_wchar != -1) )
637 sstrncpy (vl.type, "ps_disk_octets", sizeof (vl.type));
638 vl.values[0].counter = ps->io_rchar;
639 vl.values[1].counter = ps->io_wchar;
641 plugin_dispatch_values (&vl);
644 if ( (ps->io_syscr != -1) && (ps->io_syscw != -1) )
646 sstrncpy (vl.type, "ps_disk_ops", sizeof (vl.type));
647 vl.values[0].counter = ps->io_syscr;
648 vl.values[1].counter = ps->io_syscw;
650 plugin_dispatch_values (&vl);
653 DEBUG ("name = %s; num_proc = %lu; num_lwp = %lu; vmem_rss = %lu; "
654 "vmem_minflt_counter = %lu; vmem_majflt_counter = %lu; "
655 "cpu_user_counter = %lu; cpu_system_counter = %lu; "
656 "io_rchar = %ld; io_wchar = %ld; "
657 "io_syscr = %ld; io_syscw = %ld;",
658 ps->name, ps->num_proc, ps->num_lwp, ps->vmem_rss,
659 ps->vmem_minflt_counter, ps->vmem_majflt_counter,
660 ps->cpu_user_counter, ps->cpu_system_counter,
661 ps->io_rchar, ps->io_wchar, ps->io_syscr, ps->io_syscw);
662 } /* void ps_submit_proc_list */
664 /* ------- additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
666 static int ps_read_tasks (int pid)
673 ssnprintf (dirname, sizeof (dirname), "/proc/%i/task", pid);
675 if ((dh = opendir (dirname)) == NULL)
677 DEBUG ("Failed to open directory `%s'", dirname);
681 while ((ent = readdir (dh)) != NULL)
683 if (!isdigit ((int) ent->d_name[0]))
690 return ((count >= 1) ? count : 1);
691 } /* int *ps_read_tasks */
693 static procstat_t *ps_read_io (int pid, procstat_t *ps)
702 ssnprintf (filename, sizeof (filename), "/proc/%i/io", pid);
703 if ((fh = fopen (filename, "r")) == NULL)
706 while (fgets (buffer, 1024, fh) != NULL)
710 if (strncasecmp (buffer, "rchar:", 6) == 0)
711 val = &(ps->io_rchar);
712 else if (strncasecmp (buffer, "wchar:", 6) == 0)
713 val = &(ps->io_wchar);
714 else if (strncasecmp (buffer, "syscr:", 6) == 0)
715 val = &(ps->io_syscr);
716 else if (strncasecmp (buffer, "syscw:", 6) == 0)
717 val = &(ps->io_syscw);
721 numfields = strsplit (buffer, fields, 8);
726 *val = atol (fields[1]);
732 WARNING ("processes: fclose: %s",
733 sstrerror (errno, errbuf, sizeof (errbuf)));
737 } /* procstat_t *ps_read_io */
739 int ps_read_process (int pid, procstat_t *ps, char *state)
752 long long unsigned cpu_user_counter;
753 long long unsigned cpu_system_counter;
754 long long unsigned vmem_size;
755 long long unsigned vmem_rss;
756 long long unsigned stack_size;
758 memset (ps, 0, sizeof (procstat_t));
760 ssnprintf (filename, sizeof (filename), "/proc/%i/stat", pid);
762 i = read_file_contents (filename, buffer, sizeof(buffer) - 1);
767 fields_len = strsplit (buffer, fields, 64);
770 DEBUG ("processes plugin: ps_read_process (pid = %i):"
771 " `%s' has only %i fields..",
772 (int) pid, filename, fields_len);
776 /* copy the name, strip brackets in the process */
777 name_len = strlen (fields[1]) - 2;
778 if ((fields[1][0] != '(') || (fields[1][name_len + 1] != ')'))
780 DEBUG ("No brackets found in process name: `%s'", fields[1]);
783 fields[1] = fields[1] + 1;
784 fields[1][name_len] = '\0';
785 strncpy (ps->name, fields[1], PROCSTAT_NAME_LEN);
787 ppid = atoi (fields[3]);
789 *state = fields[2][0];
798 if ( (ps->num_lwp = ps_read_tasks (pid)) == -1 )
800 /* returns -1 => kernel 2.4 */
806 /* Leave the rest at zero if this is only a zombi */
807 if (ps->num_proc == 0)
809 DEBUG ("processes plugin: This is only a zombi: pid = %i; "
810 "name = %s;", pid, ps->name);
814 cpu_user_counter = atoll (fields[13]);
815 cpu_system_counter = atoll (fields[14]);
816 vmem_size = atoll (fields[22]);
817 vmem_rss = atoll (fields[23]);
818 ps->vmem_minflt_counter = atol (fields[9]);
819 ps->vmem_majflt_counter = atol (fields[11]);
822 unsigned long long stack_start = atoll (fields[27]);
823 unsigned long long stack_ptr = atoll (fields[28]);
825 stack_size = (stack_start > stack_ptr)
826 ? stack_start - stack_ptr
827 : stack_ptr - stack_start;
830 /* Convert jiffies to useconds */
831 cpu_user_counter = cpu_user_counter * 1000000 / CONFIG_HZ;
832 cpu_system_counter = cpu_system_counter * 1000000 / CONFIG_HZ;
833 vmem_rss = vmem_rss * pagesize_g;
835 ps->cpu_user_counter = (unsigned long) cpu_user_counter;
836 ps->cpu_system_counter = (unsigned long) cpu_system_counter;
837 ps->vmem_size = (unsigned long) vmem_size;
838 ps->vmem_rss = (unsigned long) vmem_rss;
839 ps->stack_size = (unsigned long) stack_size;
841 if ( (ps_read_io (pid, ps)) == NULL)
849 DEBUG("ps_read_process: not get io data for pid %i",pid);
854 } /* int ps_read_process (...) */
856 static char *ps_get_cmdline (pid_t pid, char *name, char *buf, size_t buf_len)
866 if ((pid < 1) || (NULL == buf) || (buf_len < 2))
869 ssnprintf (file, sizeof (file), "/proc/%u/cmdline", pid);
871 fd = open (file, O_RDONLY);
874 WARNING ("processes plugin: Failed to open `%s': %s.", file,
875 sstrerror (errno, errbuf, sizeof (errbuf)));
887 status = read (fd, (void *)buf_ptr, len);
892 if ((EAGAIN == errno) || (EINTR == errno))
895 WARNING ("processes plugin: Failed to read from `%s': %s.", file,
896 sstrerror (errno, errbuf, sizeof (errbuf)));
916 /* cmdline not available; e.g. kernel thread, zombie */
920 ssnprintf (buf, buf_len, "[%s]", name);
924 assert (n <= buf_len);
931 /* remove trailing whitespace */
932 while ((n > 0) && (isspace (buf[n]) || ('\0' == buf[n]))) {
937 /* arguments are separated by '\0' in /proc/<pid>/cmdline */
944 } /* char *ps_get_cmdline (...) */
946 static unsigned long read_fork_rate ()
950 unsigned long result = 0;
954 proc_stat = fopen("/proc/stat", "r");
955 if (proc_stat == NULL) {
957 ERROR ("processes plugin: fopen (/proc/stat) failed: %s",
958 sstrerror (errno, errbuf, sizeof (errbuf)));
962 while (fgets (buf, sizeof(buf), proc_stat) != NULL)
966 numfields = strsplit(buf, fields, STATIC_ARRAY_SIZE (fields));
970 if (strcmp ("processes", fields[0]) != 0)
975 result = strtoul(fields[1], &endptr, 10);
976 if ((endptr == fields[1]) || (errno != 0)) {
977 ERROR ("processes plugin: Cannot parse fork rate: %s",
991 static void ps_submit_fork_rate (unsigned long value)
994 value_list_t vl = VALUE_LIST_INIT;
996 values[0].derive = (derive_t) value;
1000 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
1001 sstrncpy (vl.plugin, "processes", sizeof (vl.plugin));
1002 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
1003 sstrncpy (vl.type, "fork_rate", sizeof (vl.type));
1004 sstrncpy (vl.type_instance, "", sizeof (vl.type_instance));
1006 plugin_dispatch_values (&vl);
1009 #endif /* KERNEL_LINUX */
1011 #if HAVE_THREAD_INFO
1012 static int mach_get_task_name (task_t t, int *pid, char *name, size_t name_max_len)
1016 struct kinfo_proc kp;
1021 mib[2] = KERN_PROC_PID;
1023 if (pid_for_task (t, pid) != KERN_SUCCESS)
1027 kp_size = sizeof (kp);
1028 if (sysctl (mib, 4, &kp, &kp_size, NULL, 0) != 0)
1031 if (name_max_len > (MAXCOMLEN + 1))
1032 name_max_len = MAXCOMLEN + 1;
1034 strncpy (name, kp.kp_proc.p_comm, name_max_len - 1);
1035 name[name_max_len - 1] = '\0';
1037 DEBUG ("pid = %i; name = %s;", *pid, name);
1039 /* We don't do the special handling for `p_comm == "LaunchCFMApp"' as
1040 * `top' does it, because it is a lot of work and only used when
1041 * debugging. -octo */
1045 #endif /* HAVE_THREAD_INFO */
1046 /* ------- end of additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
1048 /* do actual readings from kernel */
1049 static int ps_read (void)
1051 #if HAVE_THREAD_INFO
1052 kern_return_t status;
1055 processor_set_t port_pset_priv;
1058 task_array_t task_list;
1059 mach_msg_type_number_t task_list_len;
1062 char task_name[MAXCOMLEN + 1];
1065 thread_act_array_t thread_list;
1066 mach_msg_type_number_t thread_list_len;
1067 thread_basic_info_data_t thread_data;
1068 mach_msg_type_number_t thread_data_len;
1077 procstat_entry_t pse;
1082 * The Mach-concept is a little different from the traditional UNIX
1083 * concept: All the work is done in threads. Threads are contained in
1084 * `tasks'. Therefore, `task status' doesn't make much sense, since
1085 * it's actually a `thread status'.
1086 * Tasks are assigned to sets of processors, so that's where you go to
1089 for (pset = 0; pset < pset_list_len; pset++)
1091 if ((status = host_processor_set_priv (port_host_self,
1093 &port_pset_priv)) != KERN_SUCCESS)
1095 ERROR ("host_processor_set_priv failed: %s\n",
1096 mach_error_string (status));
1100 if ((status = processor_set_tasks (port_pset_priv,
1102 &task_list_len)) != KERN_SUCCESS)
1104 ERROR ("processor_set_tasks failed: %s\n",
1105 mach_error_string (status));
1106 mach_port_deallocate (port_task_self, port_pset_priv);
1110 for (task = 0; task < task_list_len; task++)
1113 if (mach_get_task_name (task_list[task],
1115 task_name, PROCSTAT_NAME_LEN) == 0)
1117 /* search for at least one match */
1118 for (ps = list_head_g; ps != NULL; ps = ps->next)
1119 /* FIXME: cmdline should be here instead of NULL */
1120 if (ps_list_match (task_name, NULL, ps) == 1)
1124 /* Collect more detailed statistics for this process */
1127 task_basic_info_data_t task_basic_info;
1128 mach_msg_type_number_t task_basic_info_len;
1129 task_events_info_data_t task_events_info;
1130 mach_msg_type_number_t task_events_info_len;
1131 task_absolutetime_info_data_t task_absolutetime_info;
1132 mach_msg_type_number_t task_absolutetime_info_len;
1134 memset (&pse, '\0', sizeof (pse));
1137 task_basic_info_len = TASK_BASIC_INFO_COUNT;
1138 status = task_info (task_list[task],
1140 (task_info_t) &task_basic_info,
1141 &task_basic_info_len);
1142 if (status != KERN_SUCCESS)
1144 ERROR ("task_info failed: %s",
1145 mach_error_string (status));
1146 continue; /* with next thread_list */
1149 task_events_info_len = TASK_EVENTS_INFO_COUNT;
1150 status = task_info (task_list[task],
1152 (task_info_t) &task_events_info,
1153 &task_events_info_len);
1154 if (status != KERN_SUCCESS)
1156 ERROR ("task_info failed: %s",
1157 mach_error_string (status));
1158 continue; /* with next thread_list */
1161 task_absolutetime_info_len = TASK_ABSOLUTETIME_INFO_COUNT;
1162 status = task_info (task_list[task],
1163 TASK_ABSOLUTETIME_INFO,
1164 (task_info_t) &task_absolutetime_info,
1165 &task_absolutetime_info_len);
1166 if (status != KERN_SUCCESS)
1168 ERROR ("task_info failed: %s",
1169 mach_error_string (status));
1170 continue; /* with next thread_list */
1174 pse.vmem_rss = task_basic_info.resident_size;
1176 pse.vmem_minflt_counter = task_events_info.cow_faults;
1177 pse.vmem_majflt_counter = task_events_info.faults;
1179 pse.cpu_user_counter = task_absolutetime_info.total_user;
1180 pse.cpu_system_counter = task_absolutetime_info.total_system;
1183 status = task_threads (task_list[task], &thread_list,
1185 if (status != KERN_SUCCESS)
1187 /* Apple's `top' treats this case a zombie. It
1188 * makes sense to some extend: A `zombie'
1189 * thread is nonsense, since the task/process
1192 DEBUG ("task_threads failed: %s",
1193 mach_error_string (status));
1194 if (task_list[task] != port_task_self)
1195 mach_port_deallocate (port_task_self,
1197 continue; /* with next task_list */
1200 for (thread = 0; thread < thread_list_len; thread++)
1202 thread_data_len = THREAD_BASIC_INFO_COUNT;
1203 status = thread_info (thread_list[thread],
1205 (thread_info_t) &thread_data,
1207 if (status != KERN_SUCCESS)
1209 ERROR ("thread_info failed: %s",
1210 mach_error_string (status));
1211 if (task_list[task] != port_task_self)
1212 mach_port_deallocate (port_task_self,
1213 thread_list[thread]);
1214 continue; /* with next thread_list */
1220 switch (thread_data.run_state)
1222 case TH_STATE_RUNNING:
1225 case TH_STATE_STOPPED:
1226 /* What exactly is `halted'? */
1227 case TH_STATE_HALTED:
1230 case TH_STATE_WAITING:
1233 case TH_STATE_UNINTERRUPTIBLE:
1236 /* There is no `zombie' case here,
1237 * since there are no zombie-threads.
1238 * There's only zombie tasks, which are
1241 WARNING ("Unknown thread status: %i",
1242 thread_data.run_state);
1244 } /* switch (thread_data.run_state) */
1246 if (task_list[task] != port_task_self)
1248 status = mach_port_deallocate (port_task_self,
1249 thread_list[thread]);
1250 if (status != KERN_SUCCESS)
1251 ERROR ("mach_port_deallocate failed: %s",
1252 mach_error_string (status));
1254 } /* for (thread_list) */
1256 if ((status = vm_deallocate (port_task_self,
1257 (vm_address_t) thread_list,
1258 thread_list_len * sizeof (thread_act_t)))
1261 ERROR ("vm_deallocate failed: %s",
1262 mach_error_string (status));
1265 thread_list_len = 0;
1267 /* Only deallocate the task port, if it isn't our own.
1268 * Don't know what would happen in that case, but this
1269 * is what Apple's top does.. ;) */
1270 if (task_list[task] != port_task_self)
1272 status = mach_port_deallocate (port_task_self,
1274 if (status != KERN_SUCCESS)
1275 ERROR ("mach_port_deallocate failed: %s",
1276 mach_error_string (status));
1280 /* FIXME: cmdline should be here instead of NULL */
1281 ps_list_add (task_name, NULL, &pse);
1282 } /* for (task_list) */
1284 if ((status = vm_deallocate (port_task_self,
1285 (vm_address_t) task_list,
1286 task_list_len * sizeof (task_t))) != KERN_SUCCESS)
1288 ERROR ("vm_deallocate failed: %s",
1289 mach_error_string (status));
1294 if ((status = mach_port_deallocate (port_task_self, port_pset_priv))
1297 ERROR ("mach_port_deallocate failed: %s",
1298 mach_error_string (status));
1300 } /* for (pset_list) */
1302 ps_submit_state ("running", running);
1303 ps_submit_state ("sleeping", sleeping);
1304 ps_submit_state ("zombies", zombies);
1305 ps_submit_state ("stopped", stopped);
1306 ps_submit_state ("blocked", blocked);
1308 for (ps = list_head_g; ps != NULL; ps = ps->next)
1309 ps_submit_proc_list (ps);
1310 /* #endif HAVE_THREAD_INFO */
1324 char cmdline[ARG_MAX];
1328 procstat_entry_t pse;
1331 unsigned long fork_rate;
1335 running = sleeping = zombies = stopped = paging = blocked = 0;
1338 if ((proc = opendir ("/proc")) == NULL)
1341 ERROR ("Cannot open `/proc': %s",
1342 sstrerror (errno, errbuf, sizeof (errbuf)));
1346 while ((ent = readdir (proc)) != NULL)
1348 if (!isdigit (ent->d_name[0]))
1351 if ((pid = atoi (ent->d_name)) < 1)
1354 status = ps_read_process (pid, &ps, &state);
1357 DEBUG ("ps_read_process failed: %i", status);
1364 pse.num_proc = ps.num_proc;
1365 pse.num_lwp = ps.num_lwp;
1366 pse.vmem_size = ps.vmem_size;
1367 pse.vmem_rss = ps.vmem_rss;
1368 pse.stack_size = ps.stack_size;
1370 pse.vmem_minflt = 0;
1371 pse.vmem_minflt_counter = ps.vmem_minflt_counter;
1372 pse.vmem_majflt = 0;
1373 pse.vmem_majflt_counter = ps.vmem_majflt_counter;
1376 pse.cpu_user_counter = ps.cpu_user_counter;
1378 pse.cpu_system_counter = ps.cpu_system_counter;
1380 pse.io_rchar = ps.io_rchar;
1381 pse.io_wchar = ps.io_wchar;
1382 pse.io_syscr = ps.io_syscr;
1383 pse.io_syscw = ps.io_syscw;
1387 case 'R': running++; break;
1388 case 'S': sleeping++; break;
1389 case 'D': blocked++; break;
1390 case 'Z': zombies++; break;
1391 case 'T': stopped++; break;
1392 case 'W': paging++; break;
1395 ps_list_add (ps.name,
1396 ps_get_cmdline (pid, ps.name, cmdline, sizeof (cmdline)),
1402 ps_submit_state ("running", running);
1403 ps_submit_state ("sleeping", sleeping);
1404 ps_submit_state ("zombies", zombies);
1405 ps_submit_state ("stopped", stopped);
1406 ps_submit_state ("paging", paging);
1407 ps_submit_state ("blocked", blocked);
1409 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1410 ps_submit_proc_list (ps_ptr);
1412 fork_rate = read_fork_rate();
1413 if (fork_rate != ULONG_MAX)
1414 ps_submit_fork_rate(fork_rate);
1415 /* #endif KERNEL_LINUX */
1417 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD
1428 char cmdline[ARG_MAX];
1430 struct kinfo_proc *procs; /* array of processes */
1432 int count; /* returns number of processes */
1436 procstat_entry_t pse;
1440 /* Open the kvm interface, get a descriptor */
1441 kd = kvm_open (NULL, NULL, NULL, 0, errbuf);
1444 ERROR ("processes plugin: Cannot open kvm interface: %s",
1449 /* Get the list of processes. */
1450 procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, &count);
1454 ERROR ("processes plugin: Cannot get kvm processes list: %s",
1459 /* Iterate through the processes in kinfo_proc */
1460 for (i = 0; i < count; i++)
1462 /* retrieve the arguments */
1466 argv = kvm_getargv (kd, (const struct kinfo_proc *) &(procs[i]), 0);
1473 while (argv[argc] != NULL)
1476 status = strjoin (cmdline, sizeof (cmdline),
1481 WARNING ("processes plugin: Command line did "
1482 "not fit into buffer.");
1486 cmdline_ptr = &cmdline[0];
1490 pse.id = procs[i].ki_pid;
1494 pse.num_lwp = procs[i].ki_numthreads;
1496 pse.vmem_size = procs[i].ki_size;
1497 pse.vmem_rss = procs[i].ki_rssize * getpagesize();
1498 pse.stack_size = procs[i].ki_ssize * getpagesize();
1499 pse.vmem_minflt = 0;
1500 pse.vmem_minflt_counter = procs[i].ki_rusage.ru_minflt;
1501 pse.vmem_majflt = 0;
1502 pse.vmem_majflt_counter = procs[i].ki_rusage.ru_majflt;
1505 pse.cpu_user_counter = procs[i].ki_rusage.ru_utime.tv_sec
1507 + procs[i].ki_rusage.ru_utime.tv_usec;
1509 pse.cpu_system_counter = procs[i].ki_rusage.ru_stime.tv_sec
1511 + procs[i].ki_rusage.ru_stime.tv_usec;
1519 switch (procs[i].ki_stat)
1521 case SSTOP: stopped++; break;
1522 case SSLEEP: sleeping++; break;
1523 case SRUN: running++; break;
1524 case SIDL: idle++; break;
1525 case SWAIT: wait++; break;
1526 case SLOCK: blocked++; break;
1527 case SZOMB: zombies++; break;
1530 ps_list_add (procs[i].ki_comm, cmdline_ptr, &pse);
1535 ps_submit_state ("running", running);
1536 ps_submit_state ("sleeping", sleeping);
1537 ps_submit_state ("zombies", zombies);
1538 ps_submit_state ("stopped", stopped);
1539 ps_submit_state ("blocked", blocked);
1540 ps_submit_state ("idle", idle);
1541 ps_submit_state ("wait", wait);
1543 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1544 ps_submit_proc_list (ps_ptr);
1545 #endif /* HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
1550 void module_register (void)
1552 plugin_register_config ("processes", ps_config,
1553 config_keys, config_keys_num);
1554 plugin_register_init ("processes", ps_init);
1555 plugin_register_read ("processes", ps_read);
1556 } /* void module_register */