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 static long pagesize_g;
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 */
239 pagesize_g = sysconf(_SC_PAGESIZE);
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 void ps_submit_proc (procstat_t *ps)
277 DBG ("name = %s; num_proc = %i; num_lwp = %i; vmem_rss = %i; "
278 "vmem_minflt = %i; vmem_majflt = %i; "
279 "cpu_user = %i; cpu_system = %i;",
280 ps->name, ps->num_proc, ps->num_lwp, ps->vmem_rss,
281 ps->vmem_minflt, ps->vmem_majflt, ps->cpu_user,
286 static int *ps_read_tasks (int pid)
289 int list_size = 1; /* size of allocated space, in elements */
290 int list_len = 0; /* number of currently used elements */
296 snprintf (dirname, 64, "/proc/%i/task", pid);
299 if ((dh = opendir (dirname)) == NULL)
301 syslog (LOG_NOTICE, "processes plugin: Failed to open directory `%s'",
306 while ((ent = readdir (dh)) != NULL)
308 if (!isdigit (ent->d_name[0]))
311 if ((list_len + 1) >= list_size)
314 int new_size = 2 * list_size;
315 /* Comes in sizes: 2, 4, 8, 16, ... */
317 new_ptr = (int *) realloc (list, (size_t) (sizeof (int) * new_size));
322 syslog (LOG_ERR, "processes plugin: "
323 "Failed to allocate more memory.");
328 list_size = new_size;
330 memset (list + list_len, 0, sizeof (int) * (list_size - list_len));
333 list[list_len] = atoi (ent->d_name);
334 if (list[list_len] != 0)
340 assert (list_len < list_size);
341 assert (list[list_len] == 0);
346 int ps_read_process (int pid, procstat_t *ps, char *state)
361 memset (ps, 0, sizeof (procstat_t));
363 snprintf (filename, 64, "/proc/%i/stat", pid);
366 if ((fh = fopen (filename, "r")) == NULL)
369 if (fgets (buffer, 1024, fh) == NULL)
377 fields_len = strsplit (buffer, fields, 64);
380 DBG ("`%s' has only %i fields..",
381 filename, fields_len);
384 else if (fields_len != 41)
386 DBG ("WARNING: (fields_len = %i) != 41", fields_len);
389 /* copy the name, strip brackets in the process */
390 name_len = strlen (fields[1]) - 2;
391 if ((fields[1][0] != '(') || (fields[1][name_len + 1] != ')'))
393 DBG ("No brackets found in process name: `%s'", fields[1]);
396 fields[1] = fields[1] + 1;
397 fields[1][name_len] = '\0';
398 strncpy (ps->name, fields[1], PROCSTAT_NAME_LEN);
400 ppid = atoi (fields[3]);
402 if ((tasks = ps_read_tasks (pid)) == NULL)
404 DBG ("ps_read_tasks (%i) failed.", pid);
411 for (i = 0; tasks[i] != 0; i++)
417 /* Leave the rest at zero if this is only an LWP */
418 if (ps->num_proc == 0)
420 DBG ("This is only an LWP: pid = %i; name = %s;",
425 ps->vmem_minflt = atoi (fields[9]);
426 ps->vmem_majflt = atoi (fields[11]);
427 ps->cpu_user = atoi (fields[13]);
428 ps->cpu_system = atoi (fields[14]);
429 ps->vmem_rss = atoi (fields[23]) * pagesize_g;
431 *state = fields[2][0];
435 } /* int ps_read_process (...) */
436 #endif /* KERNEL_LINUX */
438 static void ps_read (void)
441 kern_return_t status;
444 processor_set_t port_pset_priv;
447 task_array_t task_list;
448 mach_msg_type_number_t task_list_len;
451 thread_act_array_t thread_list;
452 mach_msg_type_number_t thread_list_len;
453 thread_basic_info_data_t thread_data;
454 mach_msg_type_number_t thread_data_len;
463 * The Mach-concept is a little different from the traditional UNIX
464 * concept: All the work is done in threads. Threads are contained in
465 * `tasks'. Therefore, `task status' doesn't make much sense, since
466 * it's actually a `thread status'.
467 * Tasks are assigned to sets of processors, so that's where you go to
470 for (pset = 0; pset < pset_list_len; pset++)
472 if ((status = host_processor_set_priv (port_host_self,
474 &port_pset_priv)) != KERN_SUCCESS)
476 syslog (LOG_ERR, "host_processor_set_priv failed: %s\n",
477 mach_error_string (status));
481 if ((status = processor_set_tasks (port_pset_priv,
483 &task_list_len)) != KERN_SUCCESS)
485 syslog (LOG_ERR, "processor_set_tasks failed: %s\n",
486 mach_error_string (status));
487 mach_port_deallocate (port_task_self, port_pset_priv);
491 for (task = 0; task < task_list_len; task++)
493 status = task_threads (task_list[task], &thread_list,
495 if (status != KERN_SUCCESS)
497 /* Apple's `top' treats this case a zombie. It
498 * makes sense to some extend: A `zombie'
499 * thread is nonsense, since the task/process
502 DBG ("task_threads failed: %s",
503 mach_error_string (status));
504 if (task_list[task] != port_task_self)
505 mach_port_deallocate (port_task_self,
507 continue; /* with next task_list */
510 for (thread = 0; thread < thread_list_len; thread++)
512 thread_data_len = THREAD_BASIC_INFO_COUNT;
513 status = thread_info (thread_list[thread],
515 (thread_info_t) &thread_data,
517 if (status != KERN_SUCCESS)
519 syslog (LOG_ERR, "thread_info failed: %s\n",
520 mach_error_string (status));
521 if (task_list[task] != port_task_self)
522 mach_port_deallocate (port_task_self,
523 thread_list[thread]);
524 continue; /* with next thread_list */
527 switch (thread_data.run_state)
529 case TH_STATE_RUNNING:
532 case TH_STATE_STOPPED:
533 /* What exactly is `halted'? */
534 case TH_STATE_HALTED:
537 case TH_STATE_WAITING:
540 case TH_STATE_UNINTERRUPTIBLE:
543 /* There is no `zombie' case here,
544 * since there are no zombie-threads.
545 * There's only zombie tasks, which are
549 "Unknown thread status: %s",
550 thread_data.run_state);
552 } /* switch (thread_data.run_state) */
554 if (task_list[task] != port_task_self)
556 status = mach_port_deallocate (port_task_self,
557 thread_list[thread]);
558 if (status != KERN_SUCCESS)
559 syslog (LOG_ERR, "mach_port_deallocate failed: %s",
560 mach_error_string (status));
562 } /* for (thread_list) */
564 if ((status = vm_deallocate (port_task_self,
565 (vm_address_t) thread_list,
566 thread_list_len * sizeof (thread_act_t)))
569 syslog (LOG_ERR, "vm_deallocate failed: %s",
570 mach_error_string (status));
575 /* Only deallocate the task port, if it isn't our own.
576 * Don't know what would happen in that case, but this
577 * is what Apple's top does.. ;) */
578 if (task_list[task] != port_task_self)
580 status = mach_port_deallocate (port_task_self,
582 if (status != KERN_SUCCESS)
583 syslog (LOG_ERR, "mach_port_deallocate failed: %s",
584 mach_error_string (status));
586 } /* for (task_list) */
588 if ((status = vm_deallocate (port_task_self,
589 (vm_address_t) task_list,
590 task_list_len * sizeof (task_t))) != KERN_SUCCESS)
592 syslog (LOG_ERR, "vm_deallocate failed: %s",
593 mach_error_string (status));
598 if ((status = mach_port_deallocate (port_task_self, port_pset_priv))
601 syslog (LOG_ERR, "mach_port_deallocate failed: %s",
602 mach_error_string (status));
604 } /* for (pset_list) */
606 ps_submit (running, sleeping, zombies, stopped, -1, blocked);
607 /* #endif HAVE_THREAD_INFO */
627 running = sleeping = zombies = stopped = paging = blocked = 0;
628 ps_list_reset (list_head_g);
630 if ((proc = opendir ("/proc")) == NULL)
632 syslog (LOG_ERR, "Cannot open `/proc': %s", strerror (errno));
636 while ((ent = readdir (proc)) != NULL)
638 if (!isdigit (ent->d_name[0]))
641 if ((pid = atoi (ent->d_name)) < 1)
644 status = ps_read_process (pid, &ps, &state);
647 DBG ("ps_read_process failed: %i", status);
653 case 'R': running++; break;
654 case 'S': sleeping++; break;
655 case 'D': blocked++; break;
656 case 'Z': zombies++; break;
657 case 'T': stopped++; break;
658 case 'W': paging++; break;
661 if (list_head_g != NULL)
662 ps_list_add (list_head_g, &ps);
667 ps_submit (running, sleeping, zombies, stopped, paging, blocked);
669 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
670 ps_submit_proc (ps_ptr);
671 #endif /* KERNEL_LINUX */
674 # define ps_read NULL
675 #endif /* PROCESSES_HAVE_READ */
677 void module_register (void)
679 plugin_register (MODULE_NAME, ps_init, ps_read, ps_write);
680 cf_register (MODULE_NAME, ps_config, config_keys, config_keys_num);