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.. */
32 #if HAVE_MACH_MACH_INIT_H
33 # include <mach/mach_init.h>
35 #if HAVE_MACH_HOST_PRIV_H
36 # include <mach/host_priv.h>
38 #if HAVE_MACH_MACH_ERROR_H
39 # include <mach/mach_error.h>
41 #if HAVE_MACH_MACH_HOST_H
42 # include <mach/mach_host.h>
44 #if HAVE_MACH_MACH_PORT_H
45 # include <mach/mach_port.h>
47 #if HAVE_MACH_MACH_TYPES_H
48 # include <mach/mach_types.h>
50 #if HAVE_MACH_MESSAGE_H
51 # include <mach/message.h>
53 #if HAVE_MACH_PROCESSOR_SET_H
54 # include <mach/processor_set.h>
57 # include <mach/task.h>
59 #if HAVE_MACH_THREAD_ACT_H
60 # include <mach/thread_act.h>
62 #if HAVE_MACH_VM_REGION_H
63 # include <mach/vm_region.h>
65 #if HAVE_MACH_VM_MAP_H
66 # include <mach/vm_map.h>
68 #if HAVE_MACH_VM_PROT_H
69 # include <mach/vm_prot.h>
72 #define MODULE_NAME "processes"
74 #if HAVE_THREAD_INFO || KERNEL_LINUX
75 # define PROCESSES_HAVE_READ 1
77 # define PROCESSES_HAVE_READ 0
82 static char *ps_file = "processes.rrd";
84 static char *ds_def[] =
86 "DS:running:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
87 "DS:sleeping:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
88 "DS:zombies:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
89 "DS:stopped:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
90 "DS:paging:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
91 "DS:blocked:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
94 static int ds_num = 6;
96 static char *config_keys[] =
101 static int config_keys_num = 1;
103 typedef struct procstat
105 #define PROCSTAT_NAME_LEN 256
106 char name[PROCSTAT_NAME_LEN];
107 unsigned int num_proc;
108 unsigned int num_lwp;
109 unsigned int vmem_rss;
110 unsigned int vmem_minflt;
111 unsigned int vmem_majflt;
112 unsigned int cpu_user;
113 unsigned int cpu_system;
114 struct procstat *next;
117 static procstat_t *list_head_g = NULL;
120 static mach_port_t port_host_self;
121 static mach_port_t port_task_self;
123 static processor_set_name_array_t pset_list;
124 static mach_msg_type_number_t pset_list_len;
125 /* #endif HAVE_THREAD_INFO */
128 /* No global variables */
129 #endif /* KERNEL_LINUX */
131 static procstat_t *ps_list_append (procstat_t *list, const char *name)
136 if ((new = (procstat_t *) malloc (sizeof (procstat_t))) == NULL)
138 memset (new, 0, sizeof (procstat_t));
139 strncpy (new->name, name, PROCSTAT_NAME_LEN);
141 for (ptr = list; ptr != NULL; ptr = ptr->next)
142 if (ptr->next == NULL)
151 static void ps_list_add (procstat_t *list, procstat_t *entry)
156 while ((ptr != NULL) && (strcmp (ptr->name, entry->name) != 0))
162 ptr->num_proc += entry->num_proc;
163 ptr->num_lwp += entry->num_lwp;
164 ptr->vmem_rss += entry->vmem_rss;
165 ptr->vmem_minflt += entry->vmem_minflt;
166 ptr->vmem_majflt += entry->vmem_majflt;
167 ptr->cpu_user += entry->cpu_user;
168 ptr->cpu_system += entry->cpu_system;
171 static void ps_list_reset (procstat_t *ps)
186 static int ps_config (char *key, char *value)
188 if (strcasecmp (key, "CollectName") == 0)
192 entry = ps_list_append (list_head_g, value);
195 syslog (LOG_ERR, "processes plugin: ps_list_append failed.");
198 if (list_head_g != NULL)
209 static void ps_init (void)
212 kern_return_t status;
214 port_host_self = mach_host_self ();
215 port_task_self = mach_task_self ();
217 if (pset_list != NULL)
219 vm_deallocate (port_task_self,
220 (vm_address_t) pset_list,
221 pset_list_len * sizeof (processor_set_t));
226 if ((status = host_processor_sets (port_host_self,
228 &pset_list_len)) != KERN_SUCCESS)
230 syslog (LOG_ERR, "host_processor_sets failed: %s\n",
231 mach_error_string (status));
236 /* #endif HAVE_THREAD_INFO */
240 #endif /* KERNEL_LINUX */
245 static void ps_write (char *host, char *inst, char *val)
247 rrd_update_file (host, ps_file, val, ds_def, ds_num);
250 #if PROCESSES_HAVE_READ
251 static void ps_submit (int running,
260 if (snprintf (buf, BUFSIZE, "%u:%i:%i:%i:%i:%i:%i",
261 (unsigned int) curtime,
262 running, sleeping, zombies, stopped, paging,
266 DBG ("running = %i; sleeping = %i; zombies = %i; stopped = %i; paging = %i; blocked = %i;",
267 running, sleeping, zombies, stopped, paging, blocked);
269 plugin_submit (MODULE_NAME, "-", buf);
272 static int *ps_read_tasks (int pid)
278 static void ps_read (void)
281 kern_return_t status;
284 processor_set_t port_pset_priv;
287 task_array_t task_list;
288 mach_msg_type_number_t task_list_len;
291 thread_act_array_t thread_list;
292 mach_msg_type_number_t thread_list_len;
293 thread_basic_info_data_t thread_data;
294 mach_msg_type_number_t thread_data_len;
303 * The Mach-concept is a little different from the traditional UNIX
304 * concept: All the work is done in threads. Threads are contained in
305 * `tasks'. Therefore, `task status' doesn't make much sense, since
306 * it's actually a `thread status'.
307 * Tasks are assigned to sets of processors, so that's where you go to
310 for (pset = 0; pset < pset_list_len; pset++)
312 if ((status = host_processor_set_priv (port_host_self,
314 &port_pset_priv)) != KERN_SUCCESS)
316 syslog (LOG_ERR, "host_processor_set_priv failed: %s\n",
317 mach_error_string (status));
321 if ((status = processor_set_tasks (port_pset_priv,
323 &task_list_len)) != KERN_SUCCESS)
325 syslog (LOG_ERR, "processor_set_tasks failed: %s\n",
326 mach_error_string (status));
327 mach_port_deallocate (port_task_self, port_pset_priv);
331 for (task = 0; task < task_list_len; task++)
333 status = task_threads (task_list[task], &thread_list,
335 if (status != KERN_SUCCESS)
337 /* Apple's `top' treats this case a zombie. It
338 * makes sense to some extend: A `zombie'
339 * thread is nonsense, since the task/process
342 DBG ("task_threads failed: %s",
343 mach_error_string (status));
344 if (task_list[task] != port_task_self)
345 mach_port_deallocate (port_task_self,
347 continue; /* with next task_list */
350 for (thread = 0; thread < thread_list_len; thread++)
352 thread_data_len = THREAD_BASIC_INFO_COUNT;
353 status = thread_info (thread_list[thread],
355 (thread_info_t) &thread_data,
357 if (status != KERN_SUCCESS)
359 syslog (LOG_ERR, "thread_info failed: %s\n",
360 mach_error_string (status));
361 if (task_list[task] != port_task_self)
362 mach_port_deallocate (port_task_self,
363 thread_list[thread]);
364 continue; /* with next thread_list */
367 switch (thread_data.run_state)
369 case TH_STATE_RUNNING:
372 case TH_STATE_STOPPED:
373 /* What exactly is `halted'? */
374 case TH_STATE_HALTED:
377 case TH_STATE_WAITING:
380 case TH_STATE_UNINTERRUPTIBLE:
383 /* There is no `zombie' case here,
384 * since there are no zombie-threads.
385 * There's only zombie tasks, which are
389 "Unknown thread status: %s",
390 thread_data.run_state);
392 } /* switch (thread_data.run_state) */
394 if (task_list[task] != port_task_self)
396 status = mach_port_deallocate (port_task_self,
397 thread_list[thread]);
398 if (status != KERN_SUCCESS)
399 syslog (LOG_ERR, "mach_port_deallocate failed: %s",
400 mach_error_string (status));
402 } /* for (thread_list) */
404 if ((status = vm_deallocate (port_task_self,
405 (vm_address_t) thread_list,
406 thread_list_len * sizeof (thread_act_t)))
409 syslog (LOG_ERR, "vm_deallocate failed: %s",
410 mach_error_string (status));
415 /* Only deallocate the task port, if it isn't our own.
416 * Don't know what would happen in that case, but this
417 * is what Apple's top does.. ;) */
418 if (task_list[task] != port_task_self)
420 status = mach_port_deallocate (port_task_self,
422 if (status != KERN_SUCCESS)
423 syslog (LOG_ERR, "mach_port_deallocate failed: %s",
424 mach_error_string (status));
426 } /* for (task_list) */
428 if ((status = vm_deallocate (port_task_self,
429 (vm_address_t) task_list,
430 task_list_len * sizeof (task_t))) != KERN_SUCCESS)
432 syslog (LOG_ERR, "vm_deallocate failed: %s",
433 mach_error_string (status));
438 if ((status = mach_port_deallocate (port_task_self, port_pset_priv))
441 syslog (LOG_ERR, "mach_port_deallocate failed: %s",
442 mach_error_string (status));
444 } /* for (pset_list) */
446 ps_submit (running, sleeping, zombies, stopped, -1, blocked);
447 /* #endif HAVE_THREAD_INFO */
458 char filename[20]; /* need 17 bytes */
459 char *fields[BUFSIZE];
465 running = sleeping = zombies = stopped = paging = blocked = 0;
467 if ((proc = opendir ("/proc")) == NULL)
469 syslog (LOG_ERR, "Cannot open `/proc': %s", strerror (errno));
473 while ((ent = readdir (proc)) != NULL)
475 if (!isdigit (ent->d_name[0]))
478 if (snprintf (filename, 20, "/proc/%s/stat", ent->d_name) >= 20)
481 if ((fh = fopen (filename, "r")) == NULL)
483 syslog (LOG_NOTICE, "Cannot open `%s': %s", filename,
488 if (fgets (buf, BUFSIZE, fh) == NULL)
490 syslog (LOG_NOTICE, "Unable to read from `%s': %s",
491 filename, strerror (errno));
498 if (strsplit (buf, fields, BUFSIZE) < 3)
500 DBG ("Line has less than three fields.");
504 switch (fields[2][0])
506 case 'R': running++; break;
507 case 'S': sleeping++; break;
508 case 'D': blocked++; break;
509 case 'Z': zombies++; break;
510 case 'T': stopped++; break;
511 case 'W': paging++; break;
517 ps_submit (running, sleeping, zombies, stopped, paging, blocked);
518 #endif /* KERNEL_LINUX */
521 # define ps_read NULL
522 #endif /* PROCESSES_HAVE_READ */
524 void module_register (void)
526 plugin_register (MODULE_NAME, ps_init, ps_read, ps_write);
527 cf_register (MODULE_NAME, ps_config, config_keys, config_keys_num);