2 * collectd - src/processes.c
3 * Copyright (C) 2005 Lyonel Vincent
4 * Copyright (C) 2006 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 "utils_debug.h"
29 #include "configfile.h"
31 /* Include header files for the mach system, if they exist.. */
33 # if HAVE_MACH_MACH_INIT_H
34 # include <mach/mach_init.h>
36 # if HAVE_MACH_HOST_PRIV_H
37 # include <mach/host_priv.h>
39 # if HAVE_MACH_MACH_ERROR_H
40 # include <mach/mach_error.h>
42 # if HAVE_MACH_MACH_HOST_H
43 # include <mach/mach_host.h>
45 # if HAVE_MACH_MACH_PORT_H
46 # include <mach/mach_port.h>
48 # if HAVE_MACH_MACH_TYPES_H
49 # include <mach/mach_types.h>
51 # if HAVE_MACH_MESSAGE_H
52 # include <mach/message.h>
54 # if HAVE_MACH_PROCESSOR_SET_H
55 # include <mach/processor_set.h>
58 # include <mach/task.h>
60 # if HAVE_MACH_THREAD_ACT_H
61 # include <mach/thread_act.h>
63 # if HAVE_MACH_VM_REGION_H
64 # include <mach/vm_region.h>
66 # if HAVE_MACH_VM_MAP_H
67 # include <mach/vm_map.h>
69 # if HAVE_MACH_VM_PROT_H
70 # include <mach/vm_prot.h>
72 # if HAVE_SYS_SYSCTL_H
73 # include <sys/sysctl.h>
75 /* #endif HAVE_THREAD_INFO */
78 # if HAVE_LINUX_CONFIG_H
79 # include <linux/config.h>
82 # define CONFIG_HZ 100
84 #endif /* KERNEL_LINUX */
86 #define MODULE_NAME "processes"
88 #if HAVE_THREAD_INFO || KERNEL_LINUX
89 # define PROCESSES_HAVE_READ 1
91 # define PROCESSES_HAVE_READ 0
96 static char *processes_file = "processes.rrd";
97 static char *processes_ds_def[] =
99 "DS:running:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
100 "DS:sleeping:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
101 "DS:zombies:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
102 "DS:stopped:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
103 "DS:paging:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
104 "DS:blocked:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
107 static int processes_ds_num = 6;
109 static char *ps_rss_file = "processes/ps_rss-%s.rrd";
110 static char *ps_rss_ds_def[] =
113 "DS:byte:GAUGE:"COLLECTD_HEARTBEAT":0:9223372036854775807",
116 static int ps_rss_ds_num = 1;
118 static char *ps_cputime_file = "processes/ps_cputime-%s.rrd";
119 static char *ps_cputime_ds_def[] =
121 /* 1 second in user-mode per second ought to be enough.. */
122 "DS:user:COUNTER:"COLLECTD_HEARTBEAT":0:1000000",
123 "DS:syst:COUNTER:"COLLECTD_HEARTBEAT":0:1000000",
126 static int ps_cputime_ds_num = 2;
128 static char *ps_count_file = "processes/ps_count-%s.rrd";
129 static char *ps_count_ds_def[] =
131 "DS:processes:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
132 "DS:threads:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
135 static int ps_count_ds_num = 2;
137 static char *ps_pagefaults_file = "processes/ps_pagefaults-%s.rrd";
138 static char *ps_pagefaults_ds_def[] =
141 "DS:minflt:COUNTER:"COLLECTD_HEARTBEAT":0:9223372036854775807",
142 "DS:majflt:COUNTER:"COLLECTD_HEARTBEAT":0:9223372036854775807",
145 static int ps_pagefaults_ds_num = 2;
147 static char *config_keys[] =
152 static int config_keys_num = 1;
154 typedef struct procstat_entry_s
159 unsigned long num_proc;
160 unsigned long num_lwp;
161 unsigned long vmem_rss;
163 unsigned long vmem_minflt;
164 unsigned long vmem_majflt;
165 unsigned long vmem_minflt_counter;
166 unsigned long vmem_majflt_counter;
168 unsigned long cpu_user;
169 unsigned long cpu_system;
170 unsigned long cpu_user_counter;
171 unsigned long cpu_system_counter;
173 struct procstat_entry_s *next;
176 #define PROCSTAT_NAME_LEN 256
177 typedef struct procstat
179 char name[PROCSTAT_NAME_LEN];
181 unsigned long num_proc;
182 unsigned long num_lwp;
183 unsigned long vmem_rss;
185 unsigned long vmem_minflt_counter;
186 unsigned long vmem_majflt_counter;
188 unsigned long cpu_user_counter;
189 unsigned long cpu_system_counter;
191 struct procstat *next;
192 struct procstat_entry_s *instances;
195 static procstat_t *list_head_g = NULL;
198 static mach_port_t port_host_self;
199 static mach_port_t port_task_self;
201 static processor_set_name_array_t pset_list;
202 static mach_msg_type_number_t pset_list_len;
203 /* #endif HAVE_THREAD_INFO */
206 static long pagesize_g;
207 #endif /* KERNEL_LINUX */
209 #if HAVE_THREAD_INFO | KERNEL_LINUX
210 static void ps_list_register (const char *name)
215 if ((new = (procstat_t *) malloc (sizeof (procstat_t))) == NULL)
217 memset (new, 0, sizeof (procstat_t));
218 strncpy (new->name, name, PROCSTAT_NAME_LEN);
220 for (ptr = list_head_g; ptr != NULL; ptr = ptr->next)
222 if (strcmp (ptr->name, name) == 0)
224 if (ptr->next == NULL)
234 static procstat_t *ps_list_search (const char *name)
238 for (ptr = list_head_g; ptr != NULL; ptr = ptr->next)
239 if (strcmp (ptr->name, name) == 0)
245 static void ps_list_add (const char *name, procstat_entry_t *entry)
248 procstat_entry_t *pse;
253 if ((ps = ps_list_search (name)) == NULL)
256 for (pse = ps->instances; pse != NULL; pse = pse->next)
257 if ((pse->id == entry->id) || (pse->next == NULL))
260 if ((pse == NULL) || (pse->id != entry->id))
262 procstat_entry_t *new;
264 new = (procstat_entry_t *) malloc (sizeof (procstat_entry_t));
267 memset (new, 0, sizeof (procstat_entry_t));
279 pse->num_proc = entry->num_proc;
280 pse->num_lwp = entry->num_lwp;
281 pse->vmem_rss = entry->vmem_rss;
283 ps->num_proc += pse->num_proc;
284 ps->num_lwp += pse->num_lwp;
285 ps->vmem_rss += pse->vmem_rss;
287 if ((entry->vmem_minflt_counter == 0)
288 && (entry->vmem_majflt_counter == 0))
290 pse->vmem_minflt_counter += entry->vmem_minflt;
291 pse->vmem_minflt = entry->vmem_minflt;
293 pse->vmem_majflt_counter += entry->vmem_majflt;
294 pse->vmem_majflt = entry->vmem_majflt;
298 if (entry->vmem_minflt_counter < pse->vmem_minflt_counter)
300 pse->vmem_minflt = entry->vmem_minflt_counter
301 + (ULONG_MAX - pse->vmem_minflt_counter);
305 pse->vmem_minflt = entry->vmem_minflt_counter - pse->vmem_minflt_counter;
307 pse->vmem_minflt_counter = entry->vmem_minflt_counter;
309 if (entry->vmem_majflt_counter < pse->vmem_majflt_counter)
311 pse->vmem_majflt = entry->vmem_majflt_counter
312 + (ULONG_MAX - pse->vmem_majflt_counter);
316 pse->vmem_majflt = entry->vmem_majflt_counter - pse->vmem_majflt_counter;
318 pse->vmem_majflt_counter = entry->vmem_majflt_counter;
321 ps->vmem_minflt_counter += pse->vmem_minflt;
322 ps->vmem_majflt_counter += pse->vmem_majflt;
324 if ((entry->cpu_user_counter == 0)
325 && (entry->cpu_system_counter == 0))
327 pse->cpu_user_counter += entry->cpu_user;
328 pse->cpu_user = entry->cpu_user;
330 pse->cpu_system_counter += entry->cpu_system;
331 pse->cpu_system = entry->cpu_system;
335 if (entry->cpu_user_counter < pse->cpu_user_counter)
337 pse->cpu_user = entry->cpu_user_counter
338 + (ULONG_MAX - pse->cpu_user_counter);
342 pse->cpu_user = entry->cpu_user_counter - pse->cpu_user_counter;
344 pse->cpu_user_counter = entry->cpu_user_counter;
346 if (entry->cpu_system_counter < pse->cpu_system_counter)
348 pse->cpu_system = entry->cpu_system_counter
349 + (ULONG_MAX - pse->cpu_system_counter);
353 pse->cpu_system = entry->cpu_system_counter - pse->cpu_system_counter;
355 pse->cpu_system_counter = entry->cpu_system_counter;
358 ps->cpu_user_counter += pse->cpu_user;
359 ps->cpu_system_counter += pse->cpu_system;
362 static void ps_list_reset (void)
365 procstat_entry_t *pse;
366 procstat_entry_t *pse_prev;
368 for (ps = list_head_g; ps != NULL; ps = ps->next)
380 DBG ("Removing this procstat entry cause it's too old: "
381 "id = %lu; name = %s;",
384 if (pse_prev == NULL)
386 ps->instances = pse->next;
392 pse_prev->next = pse->next;
394 pse = pse_prev->next;
403 } /* while (pse != NULL) */
404 } /* for (ps = list_head_g; ps != NULL; ps = ps->next) */
406 #endif /* HAVE_THREAD_INFO | KERNEL_LINUX */
408 static int ps_config (char *key, char *value)
410 if (strcasecmp (key, "Process") == 0)
412 ps_list_register (value);
422 static void ps_init (void)
425 kern_return_t status;
427 port_host_self = mach_host_self ();
428 port_task_self = mach_task_self ();
430 if (pset_list != NULL)
432 vm_deallocate (port_task_self,
433 (vm_address_t) pset_list,
434 pset_list_len * sizeof (processor_set_t));
439 if ((status = host_processor_sets (port_host_self,
441 &pset_list_len)) != KERN_SUCCESS)
443 syslog (LOG_ERR, "host_processor_sets failed: %s\n",
444 mach_error_string (status));
449 /* #endif HAVE_THREAD_INFO */
452 pagesize_g = sysconf(_SC_PAGESIZE);
453 DBG ("pagesize_g = %li; CONFIG_HZ = %i;",
454 pagesize_g, CONFIG_HZ);
455 #endif /* KERNEL_LINUX */
460 static void ps_write (char *host, char *inst, char *val)
462 rrd_update_file (host, processes_file, val,
463 processes_ds_def, processes_ds_num);
466 static void ps_rss_write (char *host, char *inst, char *val)
471 status = snprintf (filename, 256, ps_rss_file, inst);
472 if ((status < 1) || (status >= 256))
475 rrd_update_file (host, filename, val, ps_rss_ds_def, ps_rss_ds_num);
478 static void ps_cputime_write (char *host, char *inst, char *val)
483 status = snprintf (filename, 256, ps_cputime_file, inst);
484 if ((status < 1) || (status >= 256))
487 DBG ("host = %s; filename = %s; val = %s;",
488 host, filename, val);
489 rrd_update_file (host, filename, val,
490 ps_cputime_ds_def, ps_cputime_ds_num);
493 static void ps_count_write (char *host, char *inst, char *val)
498 status = snprintf (filename, 256, ps_count_file, inst);
499 if ((status < 1) || (status >= 256))
502 DBG ("host = %s; filename = %s; val = %s;",
503 host, filename, val);
504 rrd_update_file (host, filename, val,
505 ps_count_ds_def, ps_count_ds_num);
508 static void ps_pagefaults_write (char *host, char *inst, char *val)
513 status = snprintf (filename, 256, ps_pagefaults_file, inst);
514 if ((status < 1) || (status >= 256))
517 DBG ("host = %s; filename = %s; val = %s;",
518 host, filename, val);
519 rrd_update_file (host, filename, val,
520 ps_pagefaults_ds_def, ps_pagefaults_ds_num);
523 #if PROCESSES_HAVE_READ
524 static void ps_submit (int running,
533 if (snprintf (buf, BUFSIZE, "%u:%i:%i:%i:%i:%i:%i",
534 (unsigned int) curtime,
535 running, sleeping, zombies, stopped, paging,
539 DBG ("running = %i; sleeping = %i; zombies = %i; stopped = %i; paging = %i; blocked = %i;",
540 running, sleeping, zombies, stopped, paging, blocked);
542 plugin_submit (MODULE_NAME, "-", buf);
545 static void ps_submit_proc_list (procstat_t *ps)
552 snprintf (buffer, 64, "%u:%lu",
553 (unsigned int) curtime,
556 plugin_submit ("ps_rss", ps->name, buffer);
558 snprintf (buffer, 64, "%u:%u:%u",
559 (unsigned int) curtime,
560 /* Make the counter overflow */
561 (unsigned int) (ps->cpu_user_counter & 0xFFFFFFFF),
562 (unsigned int) (ps->cpu_system_counter & 0xFFFFFFFF));
564 plugin_submit ("ps_cputime", ps->name, buffer);
566 snprintf (buffer, 64, "%u:%lu:%lu",
567 (unsigned int) curtime,
568 ps->num_proc, ps->num_lwp);
570 plugin_submit ("ps_count", ps->name, buffer);
572 snprintf (buffer, 64, "%u:%lu:%lu",
573 (unsigned int) curtime,
574 ps->vmem_minflt_counter, ps->vmem_majflt_counter);
576 plugin_submit ("ps_pagefaults", ps->name, buffer);
578 DBG ("name = %s; num_proc = %lu; num_lwp = %lu; vmem_rss = %lu; "
579 "vmem_minflt_counter = %lu; vmem_majflt_counter = %lu; "
580 "cpu_user_counter = %lu; cpu_system_counter = %lu;",
581 ps->name, ps->num_proc, ps->num_lwp, ps->vmem_rss,
582 ps->vmem_minflt_counter, ps->vmem_majflt_counter, ps->cpu_user_counter,
583 ps->cpu_system_counter);
588 static int *ps_read_tasks (int pid)
591 int list_size = 1; /* size of allocated space, in elements */
592 int list_len = 0; /* number of currently used elements */
598 snprintf (dirname, 64, "/proc/%i/task", pid);
601 if ((dh = opendir (dirname)) == NULL)
603 syslog (LOG_NOTICE, "processes plugin: Failed to open directory `%s'",
608 while ((ent = readdir (dh)) != NULL)
610 if (!isdigit (ent->d_name[0]))
613 if ((list_len + 1) >= list_size)
616 int new_size = 2 * list_size;
617 /* Comes in sizes: 2, 4, 8, 16, ... */
619 new_ptr = (int *) realloc (list, (size_t) (sizeof (int) * new_size));
624 syslog (LOG_ERR, "processes plugin: "
625 "Failed to allocate more memory.");
630 list_size = new_size;
632 memset (list + list_len, 0, sizeof (int) * (list_size - list_len));
635 list[list_len] = atoi (ent->d_name);
636 if (list[list_len] != 0)
642 assert (list_len < list_size);
643 assert (list[list_len] == 0);
648 int ps_read_process (int pid, procstat_t *ps, char *state)
663 long long unsigned cpu_user_counter;
664 long long unsigned cpu_system_counter;
665 long long unsigned vmem_rss;
667 memset (ps, 0, sizeof (procstat_t));
669 snprintf (filename, 64, "/proc/%i/stat", pid);
672 if ((fh = fopen (filename, "r")) == NULL)
675 if (fgets (buffer, 1024, fh) == NULL)
683 fields_len = strsplit (buffer, fields, 64);
686 DBG ("`%s' has only %i fields..",
687 filename, fields_len);
690 else if (fields_len != 41)
692 DBG ("WARNING: (fields_len = %i) != 41", fields_len);
695 /* copy the name, strip brackets in the process */
696 name_len = strlen (fields[1]) - 2;
697 if ((fields[1][0] != '(') || (fields[1][name_len + 1] != ')'))
699 DBG ("No brackets found in process name: `%s'", fields[1]);
702 fields[1] = fields[1] + 1;
703 fields[1][name_len] = '\0';
704 strncpy (ps->name, fields[1], PROCSTAT_NAME_LEN);
706 ppid = atoi (fields[3]);
708 if ((tasks = ps_read_tasks (pid)) == NULL)
710 /* This happends for zombied, e.g. */
711 DBG ("ps_read_tasks (%i) failed.", pid);
721 for (i = 0; tasks[i] != 0; i++)
728 /* Leave the rest at zero if this is only an LWP */
729 if (ps->num_proc == 0)
731 DBG ("This is only an LWP: pid = %i; name = %s;",
736 cpu_user_counter = atoll (fields[13]);
737 cpu_system_counter = atoll (fields[14]);
738 vmem_rss = atoll (fields[23]);
739 ps->vmem_minflt_counter = atol (fields[9]);
740 ps->vmem_majflt_counter = atol (fields[11]);
742 /* Convert jiffies to useconds */
743 cpu_user_counter = cpu_user_counter * 1000000 / CONFIG_HZ;
744 cpu_system_counter = cpu_system_counter * 1000000 / CONFIG_HZ;
745 vmem_rss = vmem_rss * pagesize_g;
747 ps->cpu_user_counter = (unsigned long) cpu_user_counter;
748 ps->cpu_system_counter = (unsigned long) cpu_system_counter;
749 ps->vmem_rss = (unsigned long) vmem_rss;
751 *state = fields[2][0];
755 } /* int ps_read_process (...) */
756 #endif /* KERNEL_LINUX */
759 static int mach_get_task_name (task_t t, int *pid, char *name, size_t name_max_len)
763 struct kinfo_proc kp;
768 mib[2] = KERN_PROC_PID;
770 if (pid_for_task (t, pid) != KERN_SUCCESS)
774 kp_size = sizeof (kp);
775 if (sysctl (mib, 4, &kp, &kp_size, NULL, 0) != 0)
778 if (name_max_len > (MAXCOMLEN + 1))
779 name_max_len = MAXCOMLEN + 1;
781 strncpy (name, kp.kp_proc.p_comm, name_max_len - 1);
782 name[name_max_len - 1] = '\0';
784 DBG ("pid = %i; name = %s;", *pid, name);
786 /* We don't do the special handling for `p_comm == "LaunchCFMApp"' as
787 * `top' does it, because it is a lot of work and only used when
788 * debugging. -octo */
792 #endif /* HAVE_THREAD_INFO */
794 static void ps_read (void)
797 kern_return_t status;
800 processor_set_t port_pset_priv;
803 task_array_t task_list;
804 mach_msg_type_number_t task_list_len;
807 char task_name[MAXCOMLEN + 1];
810 thread_act_array_t thread_list;
811 mach_msg_type_number_t thread_list_len;
812 thread_basic_info_data_t thread_data;
813 mach_msg_type_number_t thread_data_len;
822 procstat_entry_t pse;
827 * The Mach-concept is a little different from the traditional UNIX
828 * concept: All the work is done in threads. Threads are contained in
829 * `tasks'. Therefore, `task status' doesn't make much sense, since
830 * it's actually a `thread status'.
831 * Tasks are assigned to sets of processors, so that's where you go to
834 for (pset = 0; pset < pset_list_len; pset++)
836 if ((status = host_processor_set_priv (port_host_self,
838 &port_pset_priv)) != KERN_SUCCESS)
840 syslog (LOG_ERR, "host_processor_set_priv failed: %s\n",
841 mach_error_string (status));
845 if ((status = processor_set_tasks (port_pset_priv,
847 &task_list_len)) != KERN_SUCCESS)
849 syslog (LOG_ERR, "processor_set_tasks failed: %s\n",
850 mach_error_string (status));
851 mach_port_deallocate (port_task_self, port_pset_priv);
855 for (task = 0; task < task_list_len; task++)
858 if (mach_get_task_name (task_list[task],
860 task_name, PROCSTAT_NAME_LEN) == 0)
861 ps = ps_list_search (task_name);
863 /* Collect more detailed statistics for this process */
866 task_basic_info_data_t task_basic_info;
867 mach_msg_type_number_t task_basic_info_len;
868 task_events_info_data_t task_events_info;
869 mach_msg_type_number_t task_events_info_len;
870 task_absolutetime_info_data_t task_absolutetime_info;
871 mach_msg_type_number_t task_absolutetime_info_len;
873 memset (&pse, '\0', sizeof (pse));
876 task_basic_info_len = TASK_BASIC_INFO_COUNT;
877 status = task_info (task_list[task],
879 (task_info_t) &task_basic_info,
880 &task_basic_info_len);
881 if (status != KERN_SUCCESS)
883 syslog (LOG_ERR, "task_info failed: %s",
884 mach_error_string (status));
885 continue; /* with next thread_list */
888 task_events_info_len = TASK_EVENTS_INFO_COUNT;
889 status = task_info (task_list[task],
891 (task_info_t) &task_events_info,
892 &task_events_info_len);
893 if (status != KERN_SUCCESS)
895 syslog (LOG_ERR, "task_info failed: %s",
896 mach_error_string (status));
897 continue; /* with next thread_list */
900 task_absolutetime_info_len = TASK_ABSOLUTETIME_INFO_COUNT;
901 status = task_info (task_list[task],
902 TASK_ABSOLUTETIME_INFO,
903 (task_info_t) &task_absolutetime_info,
904 &task_absolutetime_info_len);
905 if (status != KERN_SUCCESS)
907 syslog (LOG_ERR, "task_info failed: %s",
908 mach_error_string (status));
909 continue; /* with next thread_list */
913 pse.vmem_rss = task_basic_info.resident_size;
915 pse.vmem_minflt_counter = task_events_info.cow_faults;
916 pse.vmem_majflt_counter = task_events_info.faults;
918 pse.cpu_user_counter = task_absolutetime_info.total_user;
919 pse.cpu_system_counter = task_absolutetime_info.total_system;
922 status = task_threads (task_list[task], &thread_list,
924 if (status != KERN_SUCCESS)
926 /* Apple's `top' treats this case a zombie. It
927 * makes sense to some extend: A `zombie'
928 * thread is nonsense, since the task/process
931 DBG ("task_threads failed: %s",
932 mach_error_string (status));
933 if (task_list[task] != port_task_self)
934 mach_port_deallocate (port_task_self,
936 continue; /* with next task_list */
939 for (thread = 0; thread < thread_list_len; thread++)
941 thread_data_len = THREAD_BASIC_INFO_COUNT;
942 status = thread_info (thread_list[thread],
944 (thread_info_t) &thread_data,
946 if (status != KERN_SUCCESS)
948 syslog (LOG_ERR, "thread_info failed: %s",
949 mach_error_string (status));
950 if (task_list[task] != port_task_self)
951 mach_port_deallocate (port_task_self,
952 thread_list[thread]);
953 continue; /* with next thread_list */
959 switch (thread_data.run_state)
961 case TH_STATE_RUNNING:
964 case TH_STATE_STOPPED:
965 /* What exactly is `halted'? */
966 case TH_STATE_HALTED:
969 case TH_STATE_WAITING:
972 case TH_STATE_UNINTERRUPTIBLE:
975 /* There is no `zombie' case here,
976 * since there are no zombie-threads.
977 * There's only zombie tasks, which are
981 "Unknown thread status: %s",
982 thread_data.run_state);
984 } /* switch (thread_data.run_state) */
986 if (task_list[task] != port_task_self)
988 status = mach_port_deallocate (port_task_self,
989 thread_list[thread]);
990 if (status != KERN_SUCCESS)
991 syslog (LOG_ERR, "mach_port_deallocate failed: %s",
992 mach_error_string (status));
994 } /* for (thread_list) */
996 if ((status = vm_deallocate (port_task_self,
997 (vm_address_t) thread_list,
998 thread_list_len * sizeof (thread_act_t)))
1001 syslog (LOG_ERR, "vm_deallocate failed: %s",
1002 mach_error_string (status));
1005 thread_list_len = 0;
1007 /* Only deallocate the task port, if it isn't our own.
1008 * Don't know what would happen in that case, but this
1009 * is what Apple's top does.. ;) */
1010 if (task_list[task] != port_task_self)
1012 status = mach_port_deallocate (port_task_self,
1014 if (status != KERN_SUCCESS)
1015 syslog (LOG_ERR, "mach_port_deallocate failed: %s",
1016 mach_error_string (status));
1020 ps_list_add (task_name, &pse);
1021 } /* for (task_list) */
1023 if ((status = vm_deallocate (port_task_self,
1024 (vm_address_t) task_list,
1025 task_list_len * sizeof (task_t))) != KERN_SUCCESS)
1027 syslog (LOG_ERR, "vm_deallocate failed: %s",
1028 mach_error_string (status));
1033 if ((status = mach_port_deallocate (port_task_self, port_pset_priv))
1036 syslog (LOG_ERR, "mach_port_deallocate failed: %s",
1037 mach_error_string (status));
1039 } /* for (pset_list) */
1041 ps_submit (running, sleeping, zombies, stopped, -1, blocked);
1043 for (ps = list_head_g; ps != NULL; ps = ps->next)
1044 ps_submit_proc_list (ps);
1045 /* #endif HAVE_THREAD_INFO */
1061 procstat_entry_t pse;
1066 running = sleeping = zombies = stopped = paging = blocked = 0;
1069 if ((proc = opendir ("/proc")) == NULL)
1071 syslog (LOG_ERR, "Cannot open `/proc': %s", strerror (errno));
1075 while ((ent = readdir (proc)) != NULL)
1077 if (!isdigit (ent->d_name[0]))
1080 if ((pid = atoi (ent->d_name)) < 1)
1083 status = ps_read_process (pid, &ps, &state);
1086 DBG ("ps_read_process failed: %i", status);
1093 pse.num_proc = ps.num_proc;
1094 pse.num_lwp = ps.num_lwp;
1095 pse.vmem_rss = ps.vmem_rss;
1097 pse.vmem_minflt = 0;
1098 pse.vmem_minflt_counter = ps.vmem_minflt_counter;
1099 pse.vmem_majflt = 0;
1100 pse.vmem_majflt_counter = ps.vmem_majflt_counter;
1103 pse.cpu_user_counter = ps.cpu_user_counter;
1105 pse.cpu_system_counter = ps.cpu_system_counter;
1109 case 'R': running++; break;
1110 case 'S': sleeping++; break;
1111 case 'D': blocked++; break;
1112 case 'Z': zombies++; break;
1113 case 'T': stopped++; break;
1114 case 'W': paging++; break;
1117 ps_list_add (ps.name, &pse);
1122 ps_submit (running, sleeping, zombies, stopped, paging, blocked);
1124 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1125 ps_submit_proc_list (ps_ptr);
1126 #endif /* KERNEL_LINUX */
1129 # define ps_read NULL
1130 #endif /* PROCESSES_HAVE_READ */
1132 void module_register (void)
1134 plugin_register (MODULE_NAME, ps_init, ps_read, ps_write);
1135 plugin_register ("ps_rss", NULL, NULL, ps_rss_write);
1136 plugin_register ("ps_cputime", NULL, NULL, ps_cputime_write);
1137 plugin_register ("ps_count", NULL, NULL, ps_count_write);
1138 plugin_register ("ps_pagefaults", NULL, NULL, ps_pagefaults_write);
1139 cf_register (MODULE_NAME, ps_config, config_keys, config_keys_num);