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 */
86 # error "No applicable input method."
91 static const char *config_keys[] =
96 static int config_keys_num = 1;
98 typedef struct procstat_entry_s
103 unsigned long num_proc;
104 unsigned long num_lwp;
105 unsigned long vmem_rss;
107 unsigned long vmem_minflt;
108 unsigned long vmem_majflt;
109 unsigned long vmem_minflt_counter;
110 unsigned long vmem_majflt_counter;
112 unsigned long cpu_user;
113 unsigned long cpu_system;
114 unsigned long cpu_user_counter;
115 unsigned long cpu_system_counter;
117 struct procstat_entry_s *next;
120 #define PROCSTAT_NAME_LEN 256
121 typedef struct procstat
123 char name[PROCSTAT_NAME_LEN];
125 unsigned long num_proc;
126 unsigned long num_lwp;
127 unsigned long vmem_rss;
129 unsigned long vmem_minflt_counter;
130 unsigned long vmem_majflt_counter;
132 unsigned long cpu_user_counter;
133 unsigned long cpu_system_counter;
135 struct procstat *next;
136 struct procstat_entry_s *instances;
139 static procstat_t *list_head_g = NULL;
142 static mach_port_t port_host_self;
143 static mach_port_t port_task_self;
145 static processor_set_name_array_t pset_list;
146 static mach_msg_type_number_t pset_list_len;
147 /* #endif HAVE_THREAD_INFO */
150 static long pagesize_g;
151 #endif /* KERNEL_LINUX */
153 static void ps_list_register (const char *name)
158 if ((new = (procstat_t *) malloc (sizeof (procstat_t))) == NULL)
160 memset (new, 0, sizeof (procstat_t));
161 strncpy (new->name, name, PROCSTAT_NAME_LEN);
163 for (ptr = list_head_g; ptr != NULL; ptr = ptr->next)
165 if (strcmp (ptr->name, name) == 0)
167 if (ptr->next == NULL)
177 static procstat_t *ps_list_search (const char *name)
181 for (ptr = list_head_g; ptr != NULL; ptr = ptr->next)
182 if (strcmp (ptr->name, name) == 0)
188 static void ps_list_add (const char *name, procstat_entry_t *entry)
191 procstat_entry_t *pse;
196 if ((ps = ps_list_search (name)) == NULL)
199 for (pse = ps->instances; pse != NULL; pse = pse->next)
200 if ((pse->id == entry->id) || (pse->next == NULL))
203 if ((pse == NULL) || (pse->id != entry->id))
205 procstat_entry_t *new;
207 new = (procstat_entry_t *) malloc (sizeof (procstat_entry_t));
210 memset (new, 0, sizeof (procstat_entry_t));
222 pse->num_proc = entry->num_proc;
223 pse->num_lwp = entry->num_lwp;
224 pse->vmem_rss = entry->vmem_rss;
226 ps->num_proc += pse->num_proc;
227 ps->num_lwp += pse->num_lwp;
228 ps->vmem_rss += pse->vmem_rss;
230 if ((entry->vmem_minflt_counter == 0)
231 && (entry->vmem_majflt_counter == 0))
233 pse->vmem_minflt_counter += entry->vmem_minflt;
234 pse->vmem_minflt = entry->vmem_minflt;
236 pse->vmem_majflt_counter += entry->vmem_majflt;
237 pse->vmem_majflt = entry->vmem_majflt;
241 if (entry->vmem_minflt_counter < pse->vmem_minflt_counter)
243 pse->vmem_minflt = entry->vmem_minflt_counter
244 + (ULONG_MAX - pse->vmem_minflt_counter);
248 pse->vmem_minflt = entry->vmem_minflt_counter - pse->vmem_minflt_counter;
250 pse->vmem_minflt_counter = entry->vmem_minflt_counter;
252 if (entry->vmem_majflt_counter < pse->vmem_majflt_counter)
254 pse->vmem_majflt = entry->vmem_majflt_counter
255 + (ULONG_MAX - pse->vmem_majflt_counter);
259 pse->vmem_majflt = entry->vmem_majflt_counter - pse->vmem_majflt_counter;
261 pse->vmem_majflt_counter = entry->vmem_majflt_counter;
264 ps->vmem_minflt_counter += pse->vmem_minflt;
265 ps->vmem_majflt_counter += pse->vmem_majflt;
267 if ((entry->cpu_user_counter == 0)
268 && (entry->cpu_system_counter == 0))
270 pse->cpu_user_counter += entry->cpu_user;
271 pse->cpu_user = entry->cpu_user;
273 pse->cpu_system_counter += entry->cpu_system;
274 pse->cpu_system = entry->cpu_system;
278 if (entry->cpu_user_counter < pse->cpu_user_counter)
280 pse->cpu_user = entry->cpu_user_counter
281 + (ULONG_MAX - pse->cpu_user_counter);
285 pse->cpu_user = entry->cpu_user_counter - pse->cpu_user_counter;
287 pse->cpu_user_counter = entry->cpu_user_counter;
289 if (entry->cpu_system_counter < pse->cpu_system_counter)
291 pse->cpu_system = entry->cpu_system_counter
292 + (ULONG_MAX - pse->cpu_system_counter);
296 pse->cpu_system = entry->cpu_system_counter - pse->cpu_system_counter;
298 pse->cpu_system_counter = entry->cpu_system_counter;
301 ps->cpu_user_counter += pse->cpu_user;
302 ps->cpu_system_counter += pse->cpu_system;
305 static void ps_list_reset (void)
308 procstat_entry_t *pse;
309 procstat_entry_t *pse_prev;
311 for (ps = list_head_g; ps != NULL; ps = ps->next)
323 DEBUG ("Removing this procstat entry cause it's too old: "
324 "id = %lu; name = %s;",
327 if (pse_prev == NULL)
329 ps->instances = pse->next;
335 pse_prev->next = pse->next;
337 pse = pse_prev->next;
346 } /* while (pse != NULL) */
347 } /* for (ps = list_head_g; ps != NULL; ps = ps->next) */
350 static int ps_config (const char *key, const char *value)
352 if (strcasecmp (key, "Process") == 0)
354 ps_list_register (value);
364 static int ps_init (void)
367 kern_return_t status;
369 port_host_self = mach_host_self ();
370 port_task_self = mach_task_self ();
372 if (pset_list != NULL)
374 vm_deallocate (port_task_self,
375 (vm_address_t) pset_list,
376 pset_list_len * sizeof (processor_set_t));
381 if ((status = host_processor_sets (port_host_self,
383 &pset_list_len)) != KERN_SUCCESS)
385 ERROR ("host_processor_sets failed: %s\n",
386 mach_error_string (status));
391 /* #endif HAVE_THREAD_INFO */
394 pagesize_g = sysconf(_SC_PAGESIZE);
395 DEBUG ("pagesize_g = %li; CONFIG_HZ = %i;",
396 pagesize_g, CONFIG_HZ);
397 #endif /* KERNEL_LINUX */
402 static void ps_submit_state (const char *state, double value)
405 value_list_t vl = VALUE_LIST_INIT;
407 values[0].gauge = value;
411 vl.time = time (NULL);
412 strcpy (vl.host, hostname_g);
413 strcpy (vl.plugin, "processes");
414 strcpy (vl.plugin_instance, "");
415 strcpy (vl.type, "ps_state");
416 strncpy (vl.type_instance, state, sizeof (vl.type_instance));
418 plugin_dispatch_values (&vl);
421 static void ps_submit_proc_list (procstat_t *ps)
424 value_list_t vl = VALUE_LIST_INIT;
428 vl.time = time (NULL);
429 strcpy (vl.host, hostname_g);
430 strcpy (vl.plugin, "processes");
431 strncpy (vl.plugin_instance, ps->name, sizeof (vl.plugin_instance));
433 strcpy (vl.type, "ps_rss");
434 vl.values[0].gauge = ps->vmem_rss;
436 plugin_dispatch_values (&vl);
438 strcpy (vl.type, "ps_cputime");
439 vl.values[0].counter = ps->cpu_user_counter;
440 vl.values[1].counter = ps->cpu_system_counter;
442 plugin_dispatch_values (&vl);
444 strcpy (vl.type, "ps_count");
445 vl.values[0].gauge = ps->num_proc;
446 vl.values[1].gauge = ps->num_lwp;
448 plugin_dispatch_values (&vl);
450 strcpy (vl.type, "ps_pagefaults");
451 vl.values[0].counter = ps->vmem_minflt_counter;
452 vl.values[1].counter = ps->vmem_majflt_counter;
454 plugin_dispatch_values (&vl);
456 DEBUG ("name = %s; num_proc = %lu; num_lwp = %lu; vmem_rss = %lu; "
457 "vmem_minflt_counter = %lu; vmem_majflt_counter = %lu; "
458 "cpu_user_counter = %lu; cpu_system_counter = %lu;",
459 ps->name, ps->num_proc, ps->num_lwp, ps->vmem_rss,
460 ps->vmem_minflt_counter, ps->vmem_majflt_counter,
461 ps->cpu_user_counter, ps->cpu_system_counter);
462 } /* void ps_submit_proc_list */
465 static int *ps_read_tasks (int pid)
468 int list_size = 1; /* size of allocated space, in elements */
469 int list_len = 0; /* number of currently used elements */
475 snprintf (dirname, 64, "/proc/%i/task", pid);
478 if ((dh = opendir (dirname)) == NULL)
480 DEBUG ("Failed to open directory `%s'", dirname);
484 while ((ent = readdir (dh)) != NULL)
486 if (!isdigit (ent->d_name[0]))
489 if ((list_len + 1) >= list_size)
492 int new_size = 2 * list_size;
493 /* Comes in sizes: 2, 4, 8, 16, ... */
495 new_ptr = (int *) realloc (list, (size_t) (sizeof (int) * new_size));
500 ERROR ("processes plugin: "
501 "Failed to allocate more memory.");
506 list_size = new_size;
508 memset (list + list_len, 0, sizeof (int) * (list_size - list_len));
511 list[list_len] = atoi (ent->d_name);
512 if (list[list_len] != 0)
521 assert (list_len < list_size);
522 assert (list[list_len] == 0);
525 } /* int *ps_read_tasks */
527 int ps_read_process (int pid, procstat_t *ps, char *state)
542 long long unsigned cpu_user_counter;
543 long long unsigned cpu_system_counter;
544 long long unsigned vmem_rss;
546 memset (ps, 0, sizeof (procstat_t));
548 snprintf (filename, 64, "/proc/%i/stat", pid);
551 if ((fh = fopen (filename, "r")) == NULL)
554 if (fgets (buffer, 1024, fh) == NULL)
562 fields_len = strsplit (buffer, fields, 64);
565 DEBUG ("processes plugin: ps_read_process (pid = %i):"
566 " `%s' has only %i fields..",
567 (int) pid, filename, fields_len);
571 /* copy the name, strip brackets in the process */
572 name_len = strlen (fields[1]) - 2;
573 if ((fields[1][0] != '(') || (fields[1][name_len + 1] != ')'))
575 DEBUG ("No brackets found in process name: `%s'", fields[1]);
578 fields[1] = fields[1] + 1;
579 fields[1][name_len] = '\0';
580 strncpy (ps->name, fields[1], PROCSTAT_NAME_LEN);
582 ppid = atoi (fields[3]);
584 *state = fields[2][0];
591 else if ((tasks = ps_read_tasks (pid)) == NULL)
593 /* Kernel 2.4 or so */
601 for (i = 0; tasks[i] != 0; i++)
608 /* Leave the rest at zero if this is only a zombi */
609 if (ps->num_proc == 0)
611 DEBUG ("processes plugin: This is only a zombi: pid = %i; "
612 "name = %s;", pid, ps->name);
616 cpu_user_counter = atoll (fields[13]);
617 cpu_system_counter = atoll (fields[14]);
618 vmem_rss = atoll (fields[23]);
619 ps->vmem_minflt_counter = atol (fields[9]);
620 ps->vmem_majflt_counter = atol (fields[11]);
622 /* Convert jiffies to useconds */
623 cpu_user_counter = cpu_user_counter * 1000000 / CONFIG_HZ;
624 cpu_system_counter = cpu_system_counter * 1000000 / CONFIG_HZ;
625 vmem_rss = vmem_rss * pagesize_g;
627 ps->cpu_user_counter = (unsigned long) cpu_user_counter;
628 ps->cpu_system_counter = (unsigned long) cpu_system_counter;
629 ps->vmem_rss = (unsigned long) vmem_rss;
633 } /* int ps_read_process (...) */
634 #endif /* KERNEL_LINUX */
637 static int mach_get_task_name (task_t t, int *pid, char *name, size_t name_max_len)
641 struct kinfo_proc kp;
646 mib[2] = KERN_PROC_PID;
648 if (pid_for_task (t, pid) != KERN_SUCCESS)
652 kp_size = sizeof (kp);
653 if (sysctl (mib, 4, &kp, &kp_size, NULL, 0) != 0)
656 if (name_max_len > (MAXCOMLEN + 1))
657 name_max_len = MAXCOMLEN + 1;
659 strncpy (name, kp.kp_proc.p_comm, name_max_len - 1);
660 name[name_max_len - 1] = '\0';
662 DEBUG ("pid = %i; name = %s;", *pid, name);
664 /* We don't do the special handling for `p_comm == "LaunchCFMApp"' as
665 * `top' does it, because it is a lot of work and only used when
666 * debugging. -octo */
670 #endif /* HAVE_THREAD_INFO */
672 static int ps_read (void)
675 kern_return_t status;
678 processor_set_t port_pset_priv;
681 task_array_t task_list;
682 mach_msg_type_number_t task_list_len;
685 char task_name[MAXCOMLEN + 1];
688 thread_act_array_t thread_list;
689 mach_msg_type_number_t thread_list_len;
690 thread_basic_info_data_t thread_data;
691 mach_msg_type_number_t thread_data_len;
700 procstat_entry_t pse;
705 * The Mach-concept is a little different from the traditional UNIX
706 * concept: All the work is done in threads. Threads are contained in
707 * `tasks'. Therefore, `task status' doesn't make much sense, since
708 * it's actually a `thread status'.
709 * Tasks are assigned to sets of processors, so that's where you go to
712 for (pset = 0; pset < pset_list_len; pset++)
714 if ((status = host_processor_set_priv (port_host_self,
716 &port_pset_priv)) != KERN_SUCCESS)
718 ERROR ("host_processor_set_priv failed: %s\n",
719 mach_error_string (status));
723 if ((status = processor_set_tasks (port_pset_priv,
725 &task_list_len)) != KERN_SUCCESS)
727 ERROR ("processor_set_tasks failed: %s\n",
728 mach_error_string (status));
729 mach_port_deallocate (port_task_self, port_pset_priv);
733 for (task = 0; task < task_list_len; task++)
736 if (mach_get_task_name (task_list[task],
738 task_name, PROCSTAT_NAME_LEN) == 0)
739 ps = ps_list_search (task_name);
741 /* Collect more detailed statistics for this process */
744 task_basic_info_data_t task_basic_info;
745 mach_msg_type_number_t task_basic_info_len;
746 task_events_info_data_t task_events_info;
747 mach_msg_type_number_t task_events_info_len;
748 task_absolutetime_info_data_t task_absolutetime_info;
749 mach_msg_type_number_t task_absolutetime_info_len;
751 memset (&pse, '\0', sizeof (pse));
754 task_basic_info_len = TASK_BASIC_INFO_COUNT;
755 status = task_info (task_list[task],
757 (task_info_t) &task_basic_info,
758 &task_basic_info_len);
759 if (status != KERN_SUCCESS)
761 ERROR ("task_info failed: %s",
762 mach_error_string (status));
763 continue; /* with next thread_list */
766 task_events_info_len = TASK_EVENTS_INFO_COUNT;
767 status = task_info (task_list[task],
769 (task_info_t) &task_events_info,
770 &task_events_info_len);
771 if (status != KERN_SUCCESS)
773 ERROR ("task_info failed: %s",
774 mach_error_string (status));
775 continue; /* with next thread_list */
778 task_absolutetime_info_len = TASK_ABSOLUTETIME_INFO_COUNT;
779 status = task_info (task_list[task],
780 TASK_ABSOLUTETIME_INFO,
781 (task_info_t) &task_absolutetime_info,
782 &task_absolutetime_info_len);
783 if (status != KERN_SUCCESS)
785 ERROR ("task_info failed: %s",
786 mach_error_string (status));
787 continue; /* with next thread_list */
791 pse.vmem_rss = task_basic_info.resident_size;
793 pse.vmem_minflt_counter = task_events_info.cow_faults;
794 pse.vmem_majflt_counter = task_events_info.faults;
796 pse.cpu_user_counter = task_absolutetime_info.total_user;
797 pse.cpu_system_counter = task_absolutetime_info.total_system;
800 status = task_threads (task_list[task], &thread_list,
802 if (status != KERN_SUCCESS)
804 /* Apple's `top' treats this case a zombie. It
805 * makes sense to some extend: A `zombie'
806 * thread is nonsense, since the task/process
809 DEBUG ("task_threads failed: %s",
810 mach_error_string (status));
811 if (task_list[task] != port_task_self)
812 mach_port_deallocate (port_task_self,
814 continue; /* with next task_list */
817 for (thread = 0; thread < thread_list_len; thread++)
819 thread_data_len = THREAD_BASIC_INFO_COUNT;
820 status = thread_info (thread_list[thread],
822 (thread_info_t) &thread_data,
824 if (status != KERN_SUCCESS)
826 ERROR ("thread_info failed: %s",
827 mach_error_string (status));
828 if (task_list[task] != port_task_self)
829 mach_port_deallocate (port_task_self,
830 thread_list[thread]);
831 continue; /* with next thread_list */
837 switch (thread_data.run_state)
839 case TH_STATE_RUNNING:
842 case TH_STATE_STOPPED:
843 /* What exactly is `halted'? */
844 case TH_STATE_HALTED:
847 case TH_STATE_WAITING:
850 case TH_STATE_UNINTERRUPTIBLE:
853 /* There is no `zombie' case here,
854 * since there are no zombie-threads.
855 * There's only zombie tasks, which are
858 WARNING ("Unknown thread status: %s",
859 thread_data.run_state);
861 } /* switch (thread_data.run_state) */
863 if (task_list[task] != port_task_self)
865 status = mach_port_deallocate (port_task_self,
866 thread_list[thread]);
867 if (status != KERN_SUCCESS)
868 ERROR ("mach_port_deallocate failed: %s",
869 mach_error_string (status));
871 } /* for (thread_list) */
873 if ((status = vm_deallocate (port_task_self,
874 (vm_address_t) thread_list,
875 thread_list_len * sizeof (thread_act_t)))
878 ERROR ("vm_deallocate failed: %s",
879 mach_error_string (status));
884 /* Only deallocate the task port, if it isn't our own.
885 * Don't know what would happen in that case, but this
886 * is what Apple's top does.. ;) */
887 if (task_list[task] != port_task_self)
889 status = mach_port_deallocate (port_task_self,
891 if (status != KERN_SUCCESS)
892 ERROR ("mach_port_deallocate failed: %s",
893 mach_error_string (status));
897 ps_list_add (task_name, &pse);
898 } /* for (task_list) */
900 if ((status = vm_deallocate (port_task_self,
901 (vm_address_t) task_list,
902 task_list_len * sizeof (task_t))) != KERN_SUCCESS)
904 ERROR ("vm_deallocate failed: %s",
905 mach_error_string (status));
910 if ((status = mach_port_deallocate (port_task_self, port_pset_priv))
913 ERROR ("mach_port_deallocate failed: %s",
914 mach_error_string (status));
916 } /* for (pset_list) */
918 ps_submit_state ("running", running);
919 ps_submit_state ("sleeping", sleeping);
920 ps_submit_state ("zombies", zombies);
921 ps_submit_state ("stopped", stopped);
922 ps_submit_state ("blocked", blocked);
924 for (ps = list_head_g; ps != NULL; ps = ps->next)
925 ps_submit_proc_list (ps);
926 /* #endif HAVE_THREAD_INFO */
942 procstat_entry_t pse;
947 running = sleeping = zombies = stopped = paging = blocked = 0;
950 if ((proc = opendir ("/proc")) == NULL)
953 ERROR ("Cannot open `/proc': %s",
954 sstrerror (errno, errbuf, sizeof (errbuf)));
958 while ((ent = readdir (proc)) != NULL)
960 if (!isdigit (ent->d_name[0]))
963 if ((pid = atoi (ent->d_name)) < 1)
966 status = ps_read_process (pid, &ps, &state);
969 DEBUG ("ps_read_process failed: %i", status);
976 pse.num_proc = ps.num_proc;
977 pse.num_lwp = ps.num_lwp;
978 pse.vmem_rss = ps.vmem_rss;
981 pse.vmem_minflt_counter = ps.vmem_minflt_counter;
983 pse.vmem_majflt_counter = ps.vmem_majflt_counter;
986 pse.cpu_user_counter = ps.cpu_user_counter;
988 pse.cpu_system_counter = ps.cpu_system_counter;
992 case 'R': running++; break;
993 case 'S': sleeping++; break;
994 case 'D': blocked++; break;
995 case 'Z': zombies++; break;
996 case 'T': stopped++; break;
997 case 'W': paging++; break;
1000 ps_list_add (ps.name, &pse);
1005 ps_submit_state ("running", running);
1006 ps_submit_state ("sleeping", sleeping);
1007 ps_submit_state ("zombies", zombies);
1008 ps_submit_state ("stopped", stopped);
1009 ps_submit_state ("paging", paging);
1010 ps_submit_state ("blocked", blocked);
1012 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1013 ps_submit_proc_list (ps_ptr);
1014 #endif /* KERNEL_LINUX */
1019 void module_register (void)
1021 plugin_register_config ("processes", ps_config,
1022 config_keys, config_keys_num);
1023 plugin_register_init ("processes", ps_init);
1024 plugin_register_read ("processes", ps_read);
1025 } /* void module_register */