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"
30 /* Include header files for the mach system, if they exist.. */
31 #if HAVE_MACH_MACH_INIT_H
32 # include <mach/mach_init.h>
34 #if HAVE_MACH_HOST_PRIV_H
35 # include <mach/host_priv.h>
37 #if HAVE_MACH_MACH_ERROR_H
38 # include <mach/mach_error.h>
40 #if HAVE_MACH_MACH_HOST_H
41 # include <mach/mach_host.h>
43 #if HAVE_MACH_MACH_PORT_H
44 # include <mach/mach_port.h>
46 #if HAVE_MACH_MACH_TYPES_H
47 # include <mach/mach_types.h>
49 #if HAVE_MACH_MESSAGE_H
50 # include <mach/message.h>
52 #if HAVE_MACH_PROCESSOR_SET_H
53 # include <mach/processor_set.h>
56 # include <mach/task.h>
58 #if HAVE_MACH_THREAD_ACT_H
59 # include <mach/thread_act.h>
61 #if HAVE_MACH_VM_REGION_H
62 # include <mach/vm_region.h>
64 #if HAVE_MACH_VM_MAP_H
65 # include <mach/vm_map.h>
67 #if HAVE_MACH_VM_PROT_H
68 # include <mach/vm_prot.h>
71 #define MODULE_NAME "processes"
73 #if HAVE_THREAD_INFO || KERNEL_LINUX
74 # define PROCESSES_HAVE_READ 1
76 # define PROCESSES_HAVE_READ 0
81 static char *ps_file = "processes.rrd";
83 static char *ds_def[] =
85 "DS:running:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
86 "DS:sleeping:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
87 "DS:zombies:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
88 "DS:stopped:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
89 "DS:paging:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
90 "DS:blocked:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
93 static int ds_num = 6;
96 static mach_port_t port_host_self;
97 static mach_port_t port_task_self;
99 static processor_set_name_array_t pset_list;
100 static mach_msg_type_number_t pset_list_len;
101 /* #endif HAVE_THREAD_INFO */
104 /* No global variables */
105 #endif /* KERNEL_LINUX */
107 static void ps_init (void)
110 kern_return_t status;
112 port_host_self = mach_host_self ();
113 port_task_self = mach_task_self ();
115 if (pset_list != NULL)
117 vm_deallocate (port_task_self,
118 (vm_address_t) pset_list,
119 pset_list_len * sizeof (processor_set_t));
124 if ((status = host_processor_sets (port_host_self,
126 &pset_list_len)) != KERN_SUCCESS)
128 syslog (LOG_ERR, "host_processor_sets failed: %s\n",
129 mach_error_string (status));
134 /* #endif HAVE_THREAD_INFO */
138 #endif /* KERNEL_LINUX */
143 static void ps_write (char *host, char *inst, char *val)
145 rrd_update_file (host, ps_file, val, ds_def, ds_num);
148 #if PROCESSES_HAVE_READ
149 static void ps_submit (int running,
158 if (snprintf (buf, BUFSIZE, "%u:%i:%i:%i:%i:%i:%i",
159 (unsigned int) curtime,
160 running, sleeping, zombies, stopped, paging,
164 DBG ("running = %i; sleeping = %i; zombies = %i; stopped = %i; paging = %i; blocked = %i;",
165 running, sleeping, zombies, stopped, paging, blocked);
167 plugin_submit (MODULE_NAME, "-", buf);
170 static void ps_read (void)
173 kern_return_t status;
176 processor_set_t port_pset_priv;
179 task_array_t task_list;
180 mach_msg_type_number_t task_list_len;
183 thread_act_array_t thread_list;
184 mach_msg_type_number_t thread_list_len;
185 thread_basic_info_data_t thread_data;
186 mach_msg_type_number_t thread_data_len;
195 * The Mach-concept is a little different from the traditional UNIX
196 * concept: All the work is done in threads. Threads are contained in
197 * `tasks'. Therefore, `task status' doesn't make much sense, since
198 * it's actually a `thread status'.
199 * Tasks are assigned to sets of processors, so that's where you go to
202 for (pset = 0; pset < pset_list_len; pset++)
204 if ((status = host_processor_set_priv (port_host_self,
206 &port_pset_priv)) != KERN_SUCCESS)
208 syslog (LOG_ERR, "host_processor_set_priv failed: %s\n",
209 mach_error_string (status));
213 if ((status = processor_set_tasks (port_pset_priv,
215 &task_list_len)) != KERN_SUCCESS)
217 syslog (LOG_ERR, "processor_set_tasks failed: %s\n",
218 mach_error_string (status));
219 mach_port_deallocate (port_task_self, port_pset_priv);
223 for (task = 0; task < task_list_len; task++)
225 status = task_threads (task_list[task], &thread_list,
227 if (status != KERN_SUCCESS)
229 /* Apple's `top' treats this case a zombie. It
230 * makes sense to some extend: A `zombie'
231 * thread is nonsense, since the task/process
234 DBG ("task_threads failed: %s",
235 mach_error_string (status));
236 if (task_list[task] != port_task_self)
237 mach_port_deallocate (port_task_self,
239 continue; /* with next task_list */
242 for (thread = 0; thread < thread_list_len; thread++)
244 thread_data_len = THREAD_BASIC_INFO_COUNT;
245 status = thread_info (thread_list[thread],
247 (thread_info_t) &thread_data,
249 if (status != KERN_SUCCESS)
251 syslog (LOG_ERR, "thread_info failed: %s\n",
252 mach_error_string (status));
253 if (task_list[task] != port_task_self)
254 mach_port_deallocate (port_task_self,
255 thread_list[thread]);
256 continue; /* with next thread_list */
259 switch (thread_data.run_state)
261 case TH_STATE_RUNNING:
264 case TH_STATE_STOPPED:
265 /* What exactly is `halted'? */
266 case TH_STATE_HALTED:
269 case TH_STATE_WAITING:
272 case TH_STATE_UNINTERRUPTIBLE:
275 /* There is no `zombie' case here,
276 * since there are no zombie-threads.
277 * There's only zombie tasks, which are
281 "Unknown thread status: %s",
282 thread_data.run_state);
284 } /* switch (thread_data.run_state) */
286 if (task_list[task] != port_task_self)
288 status = mach_port_deallocate (port_task_self,
289 thread_list[thread]);
290 if (status != KERN_SUCCESS)
291 syslog (LOG_ERR, "mach_port_deallocate failed: %s",
292 mach_error_string (status));
294 } /* for (thread_list) */
296 if ((status = vm_deallocate (port_task_self,
297 (vm_address_t) thread_list,
298 thread_list_len * sizeof (thread_act_t)))
301 syslog (LOG_ERR, "vm_deallocate failed: %s",
302 mach_error_string (status));
307 /* Only deallocate the task port, if it isn't our own.
308 * Don't know what would happen in that case, but this
309 * is what Apple's top does.. ;) */
310 if (task_list[task] != port_task_self)
312 status = mach_port_deallocate (port_task_self,
314 if (status != KERN_SUCCESS)
315 syslog (LOG_ERR, "mach_port_deallocate failed: %s",
316 mach_error_string (status));
318 } /* for (task_list) */
320 if ((status = vm_deallocate (port_task_self,
321 (vm_address_t) task_list,
322 task_list_len * sizeof (task_t))) != KERN_SUCCESS)
324 syslog (LOG_ERR, "vm_deallocate failed: %s",
325 mach_error_string (status));
330 if ((status = mach_port_deallocate (port_task_self, port_pset_priv))
333 syslog (LOG_ERR, "mach_port_deallocate failed: %s",
334 mach_error_string (status));
336 } /* for (pset_list) */
338 ps_submit (running, sleeping, zombies, stopped, -1, blocked);
339 /* #endif HAVE_THREAD_INFO */
350 char filename[20]; /* need 17 bytes */
351 char *fields[BUFSIZE];
357 running = sleeping = zombies = stopped = paging = blocked = 0;
359 if ((proc = opendir ("/proc")) == NULL)
361 syslog (LOG_ERR, "Cannot open `/proc': %s", strerror (errno));
365 while ((ent = readdir (proc)) != NULL)
367 if (!isdigit (ent->d_name[0]))
370 if (snprintf (filename, 20, "/proc/%s/stat", ent->d_name) >= 20)
373 if ((fh = fopen (filename, "r")) == NULL)
375 syslog (LOG_NOTICE, "Cannot open `%s': %s", filename,
380 if (fgets (buf, BUFSIZE, fh) == NULL)
382 syslog (LOG_NOTICE, "Unable to read from `%s': %s",
383 filename, strerror (errno));
390 if (strsplit (buf, fields, BUFSIZE) < 3)
392 DBG ("Line has less than three fields.");
396 switch (fields[2][0])
398 case 'R': running++; break;
399 case 'S': sleeping++; break;
400 case 'D': blocked++; break;
401 case 'Z': zombies++; break;
402 case 'T': stopped++; break;
403 case 'W': paging++; break;
409 ps_submit (running, sleeping, zombies, stopped, paging, blocked);
410 #endif /* KERNEL_LINUX */
413 # define ps_read NULL
414 #endif /* PROCESSES_HAVE_READ */
416 void module_register (void)
418 plugin_register (MODULE_NAME, ps_init, ps_read, ps_write);