2 * collectd - src/processes.c
3 * Copyright (C) 2005 Lyonel Vincent
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Lyonel Vincent <lyonel at ezix.org>
21 * Florian octo Forster <octo at verplant.org>
27 #include "utils_debug.h"
29 /* Include header files for the mach system, if they exist.. */
30 #if HAVE_MACH_MACH_INIT_H
31 # include <mach/mach_init.h>
33 #if HAVE_MACH_HOST_PRIV_H
34 # include <mach/host_priv.h>
36 #if HAVE_MACH_MACH_ERROR_H
37 # include <mach/mach_error.h>
39 #if HAVE_MACH_MACH_HOST_H
40 # include <mach/mach_host.h>
42 #if HAVE_MACH_MACH_PORT_H
43 # include <mach/mach_port.h>
45 #if HAVE_MACH_MACH_TYPES_H
46 # include <mach/mach_types.h>
48 #if HAVE_MACH_MESSAGE_H
49 # include <mach/message.h>
51 #if HAVE_MACH_PROCESSOR_SET_H
52 # include <mach/processor_set.h>
55 # include <mach/task.h>
57 #if HAVE_MACH_THREAD_ACT_H
58 # include <mach/thread_act.h>
60 #if HAVE_MACH_VM_REGION_H
61 # include <mach/vm_region.h>
63 #if HAVE_MACH_VM_MAP_H
64 # include <mach/vm_map.h>
66 #if HAVE_MACH_VM_PROT_H
67 # include <mach/vm_prot.h>
70 #define MODULE_NAME "processes"
72 #if HAVE_THREAD_INFO || defined(KERNEL_LINUX)
73 # define PROCESSES_HAVE_READ 1
75 # define PROCESSES_HAVE_READ 0
80 static char *ps_file = "processes.rrd";
82 static char *ds_def[] =
84 "DS:running:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
85 "DS:sleeping:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
86 "DS:zombies:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
87 "DS:stopped:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
88 "DS:paging:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
89 "DS:blocked:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
92 static int ds_num = 6;
95 static mach_port_t port_host_self;
96 static mach_port_t port_task_self;
98 static processor_set_name_array_t pset_list;
99 static mach_msg_type_number_t pset_list_len;
100 /* #endif HAVE_THREAD_INFO */
103 /* No global variables */
104 #endif /* KERNEL_LINUX */
106 static void ps_init (void)
109 kern_return_t status;
111 port_host_self = mach_host_self ();
112 port_task_self = mach_task_self ();
114 if (pset_list != NULL)
116 vm_deallocate (port_task_self,
117 (vm_address_t) pset_list,
118 pset_list_len * sizeof (processor_set_t));
123 if ((status = host_processor_sets (port_host_self,
125 &pset_list_len)) != KERN_SUCCESS)
127 syslog (LOG_ERR, "host_processor_sets failed: %s\n",
128 mach_error_string (status));
133 /* #endif HAVE_THREAD_INFO */
137 #endif /* KERNEL_LINUX */
142 static void ps_write (char *host, char *inst, char *val)
144 rrd_update_file (host, ps_file, val, ds_def, ds_num);
147 #if PROCESSES_HAVE_READ
148 static void ps_submit (int running,
157 if (snprintf (buf, BUFSIZE, "%u:%i:%i:%i:%i:%i:%i",
158 (unsigned int) curtime,
159 running, sleeping, zombies, stopped, paging,
163 plugin_submit (MODULE_NAME, "-", buf);
166 static void ps_read (void)
169 kern_return_t status;
172 processor_set_t port_pset_priv;
175 task_array_t task_list;
176 mach_msg_type_number_t task_list_len;
179 thread_act_array_t thread_list;
180 mach_msg_type_number_t thread_list_len;
181 thread_basic_info_data_t thread_data;
182 mach_msg_type_number_t thread_data_len;
191 * The Mach-concept is a little different from the traditional UNIX
192 * concept: All the work is done in threads. Threads are contained in
193 * `tasks'. Therefore, `task status' doesn't make much sense, since
194 * it's actually a `thread status'.
195 * Tasks are assigned to sets of processors, so that's where you go to
198 for (pset = 0; pset < pset_list_len; pset++)
200 if ((status = host_processor_set_priv (port_host_self,
202 &port_pset_priv)) != KERN_SUCCESS)
204 syslog (LOG_ERR, "host_processor_set_priv failed: %s\n",
205 mach_error_string (status));
209 if ((status = processor_set_tasks (port_pset_priv,
211 &task_list_len)) != KERN_SUCCESS)
213 syslog (LOG_ERR, "processor_set_tasks failed: %s\n",
214 mach_error_string (status));
215 mach_port_deallocate (port_task_self, port_pset_priv);
219 for (task = 0; task < task_list_len; task++)
221 status = task_threads (task_list[task], &thread_list,
223 if (status != KERN_SUCCESS)
225 /* Apple's `top' treats this case a zombie. It
226 * makes sense to some extend: A `zombie'
227 * thread is nonsense, since the task/process
230 DBG ("task_threads failed: %s",
231 mach_error_string (status));
232 if (task_list[task] != port_task_self)
233 mach_port_deallocate (port_task_self,
235 continue; /* with next task_list */
238 for (thread = 0; thread < thread_list_len; thread++)
240 thread_data_len = THREAD_BASIC_INFO_COUNT;
241 status = thread_info (thread_list[thread],
243 (thread_info_t) &thread_data,
245 if (status != KERN_SUCCESS)
247 syslog (LOG_ERR, "thread_info failed: %s\n",
248 mach_error_string (status));
249 if (task_list[task] != port_task_self)
250 mach_port_deallocate (port_task_self,
251 thread_list[thread]);
252 continue; /* with next thread_list */
255 switch (thread_data.run_state)
257 case TH_STATE_RUNNING:
260 case TH_STATE_STOPPED:
261 /* What exactly is `halted'? */
262 case TH_STATE_HALTED:
265 case TH_STATE_WAITING:
268 case TH_STATE_UNINTERRUPTIBLE:
271 /* There is no `zombie' case here,
272 * since there are no zombie-threads.
273 * There's only zombie tasks, which are
277 "Unknown thread status: %s",
278 thread_data.run_state);
280 } /* switch (thread_data.run_state) */
282 if (task_list[task] != port_task_self)
284 status = mach_port_deallocate (port_task_self,
285 thread_list[thread]);
286 if (status != KERN_SUCCESS)
287 syslog (LOG_ERR, "mach_port_deallocate failed: %s",
288 mach_error_string (status));
290 } /* for (thread_list) */
292 if ((status = vm_deallocate (port_task_self,
293 (vm_address_t) thread_list,
294 thread_list_len * sizeof (thread_act_t)))
297 syslog (LOG_ERR, "vm_deallocate failed: %s",
298 mach_error_string (status));
303 /* Only deallocate the task port, if it isn't our own.
304 * Don't know what would happen in that case, but this
305 * is what Apple's top does.. ;) */
306 if (task_list[task] != port_task_self)
308 status = mach_port_deallocate (port_task_self,
310 if (status != KERN_SUCCESS)
311 syslog (LOG_ERR, "mach_port_deallocate failed: %s",
312 mach_error_string (status));
314 } /* for (task_list) */
316 if ((status = vm_deallocate (port_task_self,
317 (vm_address_t) task_list,
318 task_list_len * sizeof (task_t))) != KERN_SUCCESS)
320 syslog (LOG_ERR, "vm_deallocate failed: %s",
321 mach_error_string (status));
326 if ((status = mach_port_deallocate (port_task_self, port_pset_priv))
329 syslog (LOG_ERR, "mach_port_deallocate failed: %s",
330 mach_error_string (status));
332 } /* for (pset_list) */
333 /* #endif HAVE_THREAD_INFO */
344 char filename[20]; /* need 17 bytes */
345 char *fields[BUFSIZE];
351 running = sleeping = zombies = stopped = paging = blocked = 0;
353 if ((proc = opendir ("/proc")) == NULL)
355 syslog (LOG_ERR, "Cannot open `/proc': %s", strerror (errno));
359 while ((ent = readdir (proc)) != NULL)
361 if (!isdigit (ent->d_name[0]))
364 if (snprintf (filename, 20, "/proc/%s/stat", ent->d_name) >= 20)
367 if ((fh = fopen (filename, "r")) == NULL)
369 syslog (LOG_ERR, "Cannot open `%s': %s", filename, strerror (errno));
373 if (fgets (buf, BUFSIZE, fh) == NULL)
381 if (strsplit (buf, fields, BUFSIZE) < 3)
384 switch (fields[2][0])
386 case 'R': running++; break;
387 case 'S': sleeping++; break;
388 case 'D': blocked++; break;
389 case 'Z': zombies++; break;
390 case 'T': stopped++; break;
391 case 'W': paging++; break;
397 ps_submit (running, sleeping, zombies, stopped, paging, blocked);
398 #endif /* KERNEL_LINUX */
401 # define ps_read NULL
402 #endif /* PROCESSES_HAVE_READ */
404 void module_register (void)
406 plugin_register (MODULE_NAME, ps_init, ps_read, ps_write);