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 plugin_submit (MODULE_NAME, "-", buf);
220 static void ps_read (void)
223 kern_return_t status;
226 processor_set_t port_pset_priv;
229 task_array_t task_list;
230 mach_msg_type_number_t task_list_len;
233 thread_act_array_t thread_list;
234 mach_msg_type_number_t thread_list_len;
235 thread_basic_info_data_t thread_data;
236 mach_msg_type_number_t thread_data_len;
245 * The Mach-concept is a little different from the traditional UNIX
246 * concept: All the work is done in threads. Threads are contained in
247 * `tasks'. Therefore, `task status' doesn't make much sense, since
248 * it's actually a `thread status'.
249 * Tasks are assigned to sets of processors, so that's where you go to
252 for (pset = 0; pset < pset_list_len; pset++)
254 if ((status = host_processor_set_priv (port_host_self,
256 &port_pset_priv)) != KERN_SUCCESS)
258 syslog (LOG_ERR, "host_processor_set_priv failed: %s\n",
259 mach_error_string (status));
263 if ((status = processor_set_tasks (port_pset_priv,
265 &task_list_len)) != KERN_SUCCESS)
267 syslog (LOG_ERR, "processor_set_tasks failed: %s\n",
268 mach_error_string (status));
269 mach_port_deallocate (port_task_self, port_pset_priv);
273 for (task = 0; task < task_list_len; task++)
275 status = task_threads (task_list[task], &thread_list,
277 if (status != KERN_SUCCESS)
279 /* Apple's `top' treats this case a zombie. It
280 * makes sense to some extend: A `zombie'
281 * thread is nonsense, since the task/process
284 DBG ("task_threads failed: %s",
285 mach_error_string (status));
286 if (task_list[task] != port_task_self)
287 mach_port_deallocate (port_task_self,
289 continue; /* with next task_list */
292 for (thread = 0; thread < thread_list_len; thread++)
294 thread_data_len = THREAD_BASIC_INFO_COUNT;
295 status = thread_info (thread_list[thread],
297 (thread_info_t) &thread_data,
299 if (status != KERN_SUCCESS)
301 syslog (LOG_ERR, "thread_info failed: %s\n",
302 mach_error_string (status));
303 if (task_list[task] != port_task_self)
304 mach_port_deallocate (port_task_self,
305 thread_list[thread]);
306 continue; /* with next thread_list */
309 switch (thread_data.run_state)
311 case TH_STATE_RUNNING:
314 case TH_STATE_STOPPED:
315 /* What exactly is `halted'? */
316 case TH_STATE_HALTED:
319 case TH_STATE_WAITING:
322 case TH_STATE_UNINTERRUPTIBLE:
325 /* There is no `zombie' case here,
326 * since there are no zombie-threads.
327 * There's only zombie tasks, which are
331 "Unknown thread status: %s",
332 thread_data.run_state);
334 } /* switch (thread_data.run_state) */
336 if (task_list[task] != port_task_self)
338 status = mach_port_deallocate (port_task_self,
339 thread_list[thread]);
340 if (status != KERN_SUCCESS)
341 syslog (LOG_ERR, "mach_port_deallocate failed: %s",
342 mach_error_string (status));
344 } /* for (thread_list) */
346 if ((status = vm_deallocate (port_task_self,
347 (vm_address_t) thread_list,
348 thread_list_len * sizeof (thread_act_t)))
351 syslog (LOG_ERR, "vm_deallocate failed: %s",
352 mach_error_string (status));
357 /* Only deallocate the task port, if it isn't our own.
358 * Don't know what would happen in that case, but this
359 * is what Apple's top does.. ;) */
360 if (task_list[task] != port_task_self)
362 status = mach_port_deallocate (port_task_self,
364 if (status != KERN_SUCCESS)
365 syslog (LOG_ERR, "mach_port_deallocate failed: %s",
366 mach_error_string (status));
368 } /* for (task_list) */
370 if ((status = vm_deallocate (port_task_self,
371 (vm_address_t) task_list,
372 task_list_len * sizeof (task_t))) != KERN_SUCCESS)
374 syslog (LOG_ERR, "vm_deallocate failed: %s",
375 mach_error_string (status));
380 if ((status = mach_port_deallocate (port_task_self, port_pset_priv))
383 syslog (LOG_ERR, "mach_port_deallocate failed: %s",
384 mach_error_string (status));
386 } /* for (pset_list) */
388 ps_submit (running, sleeping, zombies, stopped, -1, blocked);
389 /* #endif HAVE_THREAD_INFO */
400 char filename[20]; /* need 17 bytes */
401 char *fields[BUFSIZE];
407 running = sleeping = zombies = stopped = paging = blocked = 0;
409 if ((proc = opendir ("/proc")) == NULL)
411 syslog (LOG_ERR, "Cannot open `/proc': %s", strerror (errno));
415 while ((ent = readdir (proc)) != NULL)
417 if (!isdigit (ent->d_name[0]))
420 if (snprintf (filename, 20, "/proc/%s/stat", ent->d_name) >= 20)
423 if ((fh = fopen (filename, "r")) == NULL)
425 syslog (LOG_ERR, "Cannot open `%s': %s", filename, strerror (errno));
429 if (fgets (buf, BUFSIZE, fh) == NULL)
437 if (strsplit (buf, fields, BUFSIZE) < 3)
440 switch (fields[2][0])
442 case 'R': running++; break;
443 case 'S': sleeping++; break;
444 case 'D': blocked++; break;
445 case 'Z': zombies++; break;
446 case 'T': stopped++; break;
447 case 'W': paging++; break;
453 ps_submit (running, sleeping, zombies, stopped, paging, blocked);
454 #endif /* KERNEL_LINUX */
457 # define ps_read NULL
458 #endif /* PROCESSES_HAVE_READ */
460 void module_register (void)
462 plugin_register (MODULE_NAME, ps_init, ps_read, ps_write);