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;
95 typedef struct procstat
97 #define PROCSTAT_NAME_LEN 256
98 char name[PROCSTAT_NAME_LEN];
99 unsigned int num_proc;
100 unsigned int num_lwp;
101 unsigned int vmem_rss;
102 unsigned int vmem_minflt;
103 unsigned int vmem_majflt;
104 unsigned int cpu_user;
105 unsigned int cpu_system;
106 struct procstat *next;
110 static mach_port_t port_host_self;
111 static mach_port_t port_task_self;
113 static processor_set_name_array_t pset_list;
114 static mach_msg_type_number_t pset_list_len;
115 /* #endif HAVE_THREAD_INFO */
118 /* No global variables */
119 #endif /* KERNEL_LINUX */
121 static void ps_list_add (procstat_t *list, procstat_t *entry)
126 while ((ptr != NULL) && (strcmp (ptr->name, entry->name) != 0))
132 ptr->num_proc += entry->num_proc;
133 ptr->num_lwp += entry->num_lwp;
134 ptr->vmem_rss += entry->vmem_rss;
135 ptr->vmem_minflt += entry->vmem_minflt;
136 ptr->vmem_maxflt += entry->vmem_maxflt;
137 ptr->cpu_user += entry->cpu_user;
138 ptr->cpu_system += entry->cpu_system;
141 static void ps_list_reset (procstat_t *ps)
156 static int ps_config (char *key, char *value)
160 static void ps_init (void)
163 kern_return_t status;
165 port_host_self = mach_host_self ();
166 port_task_self = mach_task_self ();
168 if (pset_list != NULL)
170 vm_deallocate (port_task_self,
171 (vm_address_t) pset_list,
172 pset_list_len * sizeof (processor_set_t));
177 if ((status = host_processor_sets (port_host_self,
179 &pset_list_len)) != KERN_SUCCESS)
181 syslog (LOG_ERR, "host_processor_sets failed: %s\n",
182 mach_error_string (status));
187 /* #endif HAVE_THREAD_INFO */
191 #endif /* KERNEL_LINUX */
196 static void ps_write (char *host, char *inst, char *val)
198 rrd_update_file (host, ps_file, val, ds_def, ds_num);
201 #if PROCESSES_HAVE_READ
202 static void ps_submit (int running,
211 if (snprintf (buf, BUFSIZE, "%u:%i:%i:%i:%i:%i:%i",
212 (unsigned int) curtime,
213 running, sleeping, zombies, stopped, paging,
217 DBG ("running = %i; sleeping = %i; zombies = %i; stopped = %i; paging = %i; blocked = %i;",
218 running, sleeping, zombies, stopped, paging, blocked);
220 plugin_submit (MODULE_NAME, "-", buf);
223 static void ps_read (void)
226 kern_return_t status;
229 processor_set_t port_pset_priv;
232 task_array_t task_list;
233 mach_msg_type_number_t task_list_len;
236 thread_act_array_t thread_list;
237 mach_msg_type_number_t thread_list_len;
238 thread_basic_info_data_t thread_data;
239 mach_msg_type_number_t thread_data_len;
248 * The Mach-concept is a little different from the traditional UNIX
249 * concept: All the work is done in threads. Threads are contained in
250 * `tasks'. Therefore, `task status' doesn't make much sense, since
251 * it's actually a `thread status'.
252 * Tasks are assigned to sets of processors, so that's where you go to
255 for (pset = 0; pset < pset_list_len; pset++)
257 if ((status = host_processor_set_priv (port_host_self,
259 &port_pset_priv)) != KERN_SUCCESS)
261 syslog (LOG_ERR, "host_processor_set_priv failed: %s\n",
262 mach_error_string (status));
266 if ((status = processor_set_tasks (port_pset_priv,
268 &task_list_len)) != KERN_SUCCESS)
270 syslog (LOG_ERR, "processor_set_tasks failed: %s\n",
271 mach_error_string (status));
272 mach_port_deallocate (port_task_self, port_pset_priv);
276 for (task = 0; task < task_list_len; task++)
278 status = task_threads (task_list[task], &thread_list,
280 if (status != KERN_SUCCESS)
282 /* Apple's `top' treats this case a zombie. It
283 * makes sense to some extend: A `zombie'
284 * thread is nonsense, since the task/process
287 DBG ("task_threads failed: %s",
288 mach_error_string (status));
289 if (task_list[task] != port_task_self)
290 mach_port_deallocate (port_task_self,
292 continue; /* with next task_list */
295 for (thread = 0; thread < thread_list_len; thread++)
297 thread_data_len = THREAD_BASIC_INFO_COUNT;
298 status = thread_info (thread_list[thread],
300 (thread_info_t) &thread_data,
302 if (status != KERN_SUCCESS)
304 syslog (LOG_ERR, "thread_info failed: %s\n",
305 mach_error_string (status));
306 if (task_list[task] != port_task_self)
307 mach_port_deallocate (port_task_self,
308 thread_list[thread]);
309 continue; /* with next thread_list */
312 switch (thread_data.run_state)
314 case TH_STATE_RUNNING:
317 case TH_STATE_STOPPED:
318 /* What exactly is `halted'? */
319 case TH_STATE_HALTED:
322 case TH_STATE_WAITING:
325 case TH_STATE_UNINTERRUPTIBLE:
328 /* There is no `zombie' case here,
329 * since there are no zombie-threads.
330 * There's only zombie tasks, which are
334 "Unknown thread status: %s",
335 thread_data.run_state);
337 } /* switch (thread_data.run_state) */
339 if (task_list[task] != port_task_self)
341 status = mach_port_deallocate (port_task_self,
342 thread_list[thread]);
343 if (status != KERN_SUCCESS)
344 syslog (LOG_ERR, "mach_port_deallocate failed: %s",
345 mach_error_string (status));
347 } /* for (thread_list) */
349 if ((status = vm_deallocate (port_task_self,
350 (vm_address_t) thread_list,
351 thread_list_len * sizeof (thread_act_t)))
354 syslog (LOG_ERR, "vm_deallocate failed: %s",
355 mach_error_string (status));
360 /* Only deallocate the task port, if it isn't our own.
361 * Don't know what would happen in that case, but this
362 * is what Apple's top does.. ;) */
363 if (task_list[task] != port_task_self)
365 status = mach_port_deallocate (port_task_self,
367 if (status != KERN_SUCCESS)
368 syslog (LOG_ERR, "mach_port_deallocate failed: %s",
369 mach_error_string (status));
371 } /* for (task_list) */
373 if ((status = vm_deallocate (port_task_self,
374 (vm_address_t) task_list,
375 task_list_len * sizeof (task_t))) != KERN_SUCCESS)
377 syslog (LOG_ERR, "vm_deallocate failed: %s",
378 mach_error_string (status));
383 if ((status = mach_port_deallocate (port_task_self, port_pset_priv))
386 syslog (LOG_ERR, "mach_port_deallocate failed: %s",
387 mach_error_string (status));
389 } /* for (pset_list) */
391 ps_submit (running, sleeping, zombies, stopped, -1, blocked);
392 /* #endif HAVE_THREAD_INFO */
403 char filename[20]; /* need 17 bytes */
404 char *fields[BUFSIZE];
410 running = sleeping = zombies = stopped = paging = blocked = 0;
412 if ((proc = opendir ("/proc")) == NULL)
414 syslog (LOG_ERR, "Cannot open `/proc': %s", strerror (errno));
418 while ((ent = readdir (proc)) != NULL)
420 if (!isdigit (ent->d_name[0]))
423 if (snprintf (filename, 20, "/proc/%s/stat", ent->d_name) >= 20)
426 if ((fh = fopen (filename, "r")) == NULL)
428 syslog (LOG_NOTICE, "Cannot open `%s': %s", filename,
433 if (fgets (buf, BUFSIZE, fh) == NULL)
435 syslog (LOG_NOTICE, "Unable to read from `%s': %s",
436 filename, strerror (errno));
443 if (strsplit (buf, fields, BUFSIZE) < 3)
445 DBG ("Line has less than three fields.");
449 switch (fields[2][0])
451 case 'R': running++; break;
452 case 'S': sleeping++; break;
453 case 'D': blocked++; break;
454 case 'Z': zombies++; break;
455 case 'T': stopped++; break;
456 case 'W': paging++; break;
462 ps_submit (running, sleeping, zombies, stopped, paging, blocked);
463 #endif /* KERNEL_LINUX */
466 # define ps_read NULL
467 #endif /* PROCESSES_HAVE_READ */
469 void module_register (void)
471 plugin_register (MODULE_NAME, ps_init, ps_read, ps_write);