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.. */
33 # if HAVE_MACH_MACH_INIT_H
34 # include <mach/mach_init.h>
36 # if HAVE_MACH_HOST_PRIV_H
37 # include <mach/host_priv.h>
39 # if HAVE_MACH_MACH_ERROR_H
40 # include <mach/mach_error.h>
42 # if HAVE_MACH_MACH_HOST_H
43 # include <mach/mach_host.h>
45 # if HAVE_MACH_MACH_PORT_H
46 # include <mach/mach_port.h>
48 # if HAVE_MACH_MACH_TYPES_H
49 # include <mach/mach_types.h>
51 # if HAVE_MACH_MESSAGE_H
52 # include <mach/message.h>
54 # if HAVE_MACH_PROCESSOR_SET_H
55 # include <mach/processor_set.h>
58 # include <mach/task.h>
60 # if HAVE_MACH_THREAD_ACT_H
61 # include <mach/thread_act.h>
63 # if HAVE_MACH_VM_REGION_H
64 # include <mach/vm_region.h>
66 # if HAVE_MACH_VM_MAP_H
67 # include <mach/vm_map.h>
69 # if HAVE_MACH_VM_PROT_H
70 # include <mach/vm_prot.h>
72 /* #endif HAVE_THREAD_INFO */
75 # if HAVE_LINUX_CONFIG_H
76 # include <linux/config.h>
79 # define CONFIG_HZ 100
81 #endif /* KERNEL_LINUX */
83 #define MODULE_NAME "processes"
85 #if HAVE_THREAD_INFO || KERNEL_LINUX
86 # define PROCESSES_HAVE_READ 1
88 # define PROCESSES_HAVE_READ 0
93 static char *processes_file = "processes.rrd";
94 static char *processes_ds_def[] =
96 "DS:running:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
97 "DS:sleeping:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
98 "DS:zombies:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
99 "DS:stopped:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
100 "DS:paging:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
101 "DS:blocked:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
104 static int processes_ds_num = 6;
106 static char *config_keys[] =
111 static int config_keys_num = 1;
113 typedef struct procstat
115 #define PROCSTAT_NAME_LEN 256
116 char name[PROCSTAT_NAME_LEN];
117 unsigned int num_proc;
118 unsigned int num_lwp;
119 unsigned long vmem_rss;
120 unsigned long vmem_minflt;
121 unsigned long vmem_majflt;
122 unsigned long long cpu_user;
123 unsigned long long cpu_system;
124 struct procstat *next;
127 static procstat_t *list_head_g = NULL;
130 static mach_port_t port_host_self;
131 static mach_port_t port_task_self;
133 static processor_set_name_array_t pset_list;
134 static mach_msg_type_number_t pset_list_len;
135 /* #endif HAVE_THREAD_INFO */
138 static long pagesize_g;
139 #endif /* KERNEL_LINUX */
141 static procstat_t *ps_list_append (procstat_t *list, const char *name)
146 if ((new = (procstat_t *) malloc (sizeof (procstat_t))) == NULL)
148 memset (new, 0, sizeof (procstat_t));
149 strncpy (new->name, name, PROCSTAT_NAME_LEN);
151 for (ptr = list; ptr != NULL; ptr = ptr->next)
152 if (ptr->next == NULL)
161 static void ps_list_add (procstat_t *list, procstat_t *entry)
166 while ((ptr != NULL) && (strcmp (ptr->name, entry->name) != 0))
172 ptr->num_proc += entry->num_proc;
173 ptr->num_lwp += entry->num_lwp;
174 ptr->vmem_rss += entry->vmem_rss;
175 ptr->vmem_minflt += entry->vmem_minflt;
176 ptr->vmem_majflt += entry->vmem_majflt;
177 ptr->cpu_user += entry->cpu_user;
178 ptr->cpu_system += entry->cpu_system;
181 static void ps_list_reset (procstat_t *ps)
196 static int ps_config (char *key, char *value)
198 if (strcasecmp (key, "CollectName") == 0)
202 entry = ps_list_append (list_head_g, value);
205 syslog (LOG_ERR, "processes plugin: ps_list_append failed.");
208 if (list_head_g == NULL)
219 static void ps_init (void)
222 kern_return_t status;
224 port_host_self = mach_host_self ();
225 port_task_self = mach_task_self ();
227 if (pset_list != NULL)
229 vm_deallocate (port_task_self,
230 (vm_address_t) pset_list,
231 pset_list_len * sizeof (processor_set_t));
236 if ((status = host_processor_sets (port_host_self,
238 &pset_list_len)) != KERN_SUCCESS)
240 syslog (LOG_ERR, "host_processor_sets failed: %s\n",
241 mach_error_string (status));
246 /* #endif HAVE_THREAD_INFO */
249 pagesize_g = sysconf(_SC_PAGESIZE);
250 #endif /* KERNEL_LINUX */
255 static void ps_write (char *host, char *inst, char *val)
257 rrd_update_file (host, processes_file, val,
258 processes_ds_def, processes_ds_num);
261 #if PROCESSES_HAVE_READ
262 static void ps_submit (int running,
271 if (snprintf (buf, BUFSIZE, "%u:%i:%i:%i:%i:%i:%i",
272 (unsigned int) curtime,
273 running, sleeping, zombies, stopped, paging,
277 DBG ("running = %i; sleeping = %i; zombies = %i; stopped = %i; paging = %i; blocked = %i;",
278 running, sleeping, zombies, stopped, paging, blocked);
280 plugin_submit (MODULE_NAME, "-", buf);
283 static void ps_submit_proc (procstat_t *ps)
288 DBG ("name = %s; num_proc = %i; num_lwp = %i; vmem_rss = %i; "
289 "vmem_minflt = %i; vmem_majflt = %i; "
290 "cpu_user = %i; cpu_system = %i;",
291 ps->name, ps->num_proc, ps->num_lwp, ps->vmem_rss,
292 ps->vmem_minflt, ps->vmem_majflt, ps->cpu_user,
297 static int *ps_read_tasks (int pid)
300 int list_size = 1; /* size of allocated space, in elements */
301 int list_len = 0; /* number of currently used elements */
307 snprintf (dirname, 64, "/proc/%i/task", pid);
310 if ((dh = opendir (dirname)) == NULL)
312 syslog (LOG_NOTICE, "processes plugin: Failed to open directory `%s'",
317 while ((ent = readdir (dh)) != NULL)
319 if (!isdigit (ent->d_name[0]))
322 if ((list_len + 1) >= list_size)
325 int new_size = 2 * list_size;
326 /* Comes in sizes: 2, 4, 8, 16, ... */
328 new_ptr = (int *) realloc (list, (size_t) (sizeof (int) * new_size));
333 syslog (LOG_ERR, "processes plugin: "
334 "Failed to allocate more memory.");
339 list_size = new_size;
341 memset (list + list_len, 0, sizeof (int) * (list_size - list_len));
344 list[list_len] = atoi (ent->d_name);
345 if (list[list_len] != 0)
351 assert (list_len < list_size);
352 assert (list[list_len] == 0);
357 int ps_read_process (int pid, procstat_t *ps, char *state)
372 memset (ps, 0, sizeof (procstat_t));
374 snprintf (filename, 64, "/proc/%i/stat", pid);
377 if ((fh = fopen (filename, "r")) == NULL)
380 if (fgets (buffer, 1024, fh) == NULL)
388 fields_len = strsplit (buffer, fields, 64);
391 DBG ("`%s' has only %i fields..",
392 filename, fields_len);
395 else if (fields_len != 41)
397 DBG ("WARNING: (fields_len = %i) != 41", fields_len);
400 /* copy the name, strip brackets in the process */
401 name_len = strlen (fields[1]) - 2;
402 if ((fields[1][0] != '(') || (fields[1][name_len + 1] != ')'))
404 DBG ("No brackets found in process name: `%s'", fields[1]);
407 fields[1] = fields[1] + 1;
408 fields[1][name_len] = '\0';
409 strncpy (ps->name, fields[1], PROCSTAT_NAME_LEN);
411 ppid = atoi (fields[3]);
413 if ((tasks = ps_read_tasks (pid)) == NULL)
415 DBG ("ps_read_tasks (%i) failed.", pid);
422 for (i = 0; tasks[i] != 0; i++)
428 /* Leave the rest at zero if this is only an LWP */
429 if (ps->num_proc == 0)
431 DBG ("This is only an LWP: pid = %i; name = %s;",
436 ps->vmem_minflt = atol (fields[9]);
437 ps->vmem_majflt = atol (fields[11]);
438 ps->cpu_user = atoll (fields[13]);
439 ps->cpu_system = atoll (fields[14]);
440 ps->vmem_rss = atol (fields[23]);
442 /* Convert jiffies to useconds */
443 ps->cpu_user = ps->cpu_user * 1000000 / CONFIG_HZ;
444 ps->cpu_system = ps->cpu_system * 1000000 / CONFIG_HZ;
445 ps->vmem_rss = ps->vmem_rss * pagesize_g;
447 *state = fields[2][0];
451 } /* int ps_read_process (...) */
452 #endif /* KERNEL_LINUX */
454 static void ps_read (void)
457 kern_return_t status;
460 processor_set_t port_pset_priv;
463 task_array_t task_list;
464 mach_msg_type_number_t task_list_len;
467 thread_act_array_t thread_list;
468 mach_msg_type_number_t thread_list_len;
469 thread_basic_info_data_t thread_data;
470 mach_msg_type_number_t thread_data_len;
479 * The Mach-concept is a little different from the traditional UNIX
480 * concept: All the work is done in threads. Threads are contained in
481 * `tasks'. Therefore, `task status' doesn't make much sense, since
482 * it's actually a `thread status'.
483 * Tasks are assigned to sets of processors, so that's where you go to
486 for (pset = 0; pset < pset_list_len; pset++)
488 if ((status = host_processor_set_priv (port_host_self,
490 &port_pset_priv)) != KERN_SUCCESS)
492 syslog (LOG_ERR, "host_processor_set_priv failed: %s\n",
493 mach_error_string (status));
497 if ((status = processor_set_tasks (port_pset_priv,
499 &task_list_len)) != KERN_SUCCESS)
501 syslog (LOG_ERR, "processor_set_tasks failed: %s\n",
502 mach_error_string (status));
503 mach_port_deallocate (port_task_self, port_pset_priv);
507 for (task = 0; task < task_list_len; task++)
509 status = task_threads (task_list[task], &thread_list,
511 if (status != KERN_SUCCESS)
513 /* Apple's `top' treats this case a zombie. It
514 * makes sense to some extend: A `zombie'
515 * thread is nonsense, since the task/process
518 DBG ("task_threads failed: %s",
519 mach_error_string (status));
520 if (task_list[task] != port_task_self)
521 mach_port_deallocate (port_task_self,
523 continue; /* with next task_list */
526 for (thread = 0; thread < thread_list_len; thread++)
528 thread_data_len = THREAD_BASIC_INFO_COUNT;
529 status = thread_info (thread_list[thread],
531 (thread_info_t) &thread_data,
533 if (status != KERN_SUCCESS)
535 syslog (LOG_ERR, "thread_info failed: %s\n",
536 mach_error_string (status));
537 if (task_list[task] != port_task_self)
538 mach_port_deallocate (port_task_self,
539 thread_list[thread]);
540 continue; /* with next thread_list */
543 switch (thread_data.run_state)
545 case TH_STATE_RUNNING:
548 case TH_STATE_STOPPED:
549 /* What exactly is `halted'? */
550 case TH_STATE_HALTED:
553 case TH_STATE_WAITING:
556 case TH_STATE_UNINTERRUPTIBLE:
559 /* There is no `zombie' case here,
560 * since there are no zombie-threads.
561 * There's only zombie tasks, which are
565 "Unknown thread status: %s",
566 thread_data.run_state);
568 } /* switch (thread_data.run_state) */
570 if (task_list[task] != port_task_self)
572 status = mach_port_deallocate (port_task_self,
573 thread_list[thread]);
574 if (status != KERN_SUCCESS)
575 syslog (LOG_ERR, "mach_port_deallocate failed: %s",
576 mach_error_string (status));
578 } /* for (thread_list) */
580 if ((status = vm_deallocate (port_task_self,
581 (vm_address_t) thread_list,
582 thread_list_len * sizeof (thread_act_t)))
585 syslog (LOG_ERR, "vm_deallocate failed: %s",
586 mach_error_string (status));
591 /* Only deallocate the task port, if it isn't our own.
592 * Don't know what would happen in that case, but this
593 * is what Apple's top does.. ;) */
594 if (task_list[task] != port_task_self)
596 status = mach_port_deallocate (port_task_self,
598 if (status != KERN_SUCCESS)
599 syslog (LOG_ERR, "mach_port_deallocate failed: %s",
600 mach_error_string (status));
602 } /* for (task_list) */
604 if ((status = vm_deallocate (port_task_self,
605 (vm_address_t) task_list,
606 task_list_len * sizeof (task_t))) != KERN_SUCCESS)
608 syslog (LOG_ERR, "vm_deallocate failed: %s",
609 mach_error_string (status));
614 if ((status = mach_port_deallocate (port_task_self, port_pset_priv))
617 syslog (LOG_ERR, "mach_port_deallocate failed: %s",
618 mach_error_string (status));
620 } /* for (pset_list) */
622 ps_submit (running, sleeping, zombies, stopped, -1, blocked);
623 /* #endif HAVE_THREAD_INFO */
643 running = sleeping = zombies = stopped = paging = blocked = 0;
644 ps_list_reset (list_head_g);
646 if ((proc = opendir ("/proc")) == NULL)
648 syslog (LOG_ERR, "Cannot open `/proc': %s", strerror (errno));
652 while ((ent = readdir (proc)) != NULL)
654 if (!isdigit (ent->d_name[0]))
657 if ((pid = atoi (ent->d_name)) < 1)
660 status = ps_read_process (pid, &ps, &state);
663 DBG ("ps_read_process failed: %i", status);
669 case 'R': running++; break;
670 case 'S': sleeping++; break;
671 case 'D': blocked++; break;
672 case 'Z': zombies++; break;
673 case 'T': stopped++; break;
674 case 'W': paging++; break;
677 if (list_head_g != NULL)
678 ps_list_add (list_head_g, &ps);
683 ps_submit (running, sleeping, zombies, stopped, paging, blocked);
685 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
686 ps_submit_proc (ps_ptr);
687 #endif /* KERNEL_LINUX */
690 # define ps_read NULL
691 #endif /* PROCESSES_HAVE_READ */
693 void module_register (void)
695 plugin_register (MODULE_NAME, ps_init, ps_read, ps_write);
696 cf_register (MODULE_NAME, ps_config, config_keys, config_keys_num);