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 *ps_rss_file = "processes/ps_rss-%s.rrd";
107 static char *ps_rss_ds_def[] =
110 "DS:byte:GAUGE:"COLLECTD_HEARTBEAT":0:9223372036854775807",
113 static int ps_rss_ds_num = 1;
115 static char *ps_cputime_file = "processes/ps_cputime-%s.rrd";
116 static char *ps_cputime_ds_def[] =
118 /* 1 second in user-mode per second ought to be enough.. */
119 "DS:user:COUNTER:"COLLECTD_HEARTBEAT":0:1000000",
120 "DS:syst:COUNTER:"COLLECTD_HEARTBEAT":0:1000000",
123 static int ps_cputime_ds_num = 2;
125 static char *ps_count_file = "processes/ps_count-%s.rrd";
126 static char *ps_count_ds_def[] =
128 "DS:processes:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
129 "DS:threads:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
132 static int ps_count_ds_num = 2;
134 static char *config_keys[] =
139 static int config_keys_num = 1;
141 typedef struct procstat
143 #define PROCSTAT_NAME_LEN 256
144 char name[PROCSTAT_NAME_LEN];
145 unsigned int num_proc;
146 unsigned int num_lwp;
147 unsigned long vmem_rss;
148 unsigned long vmem_minflt;
149 unsigned long vmem_majflt;
150 unsigned long long cpu_user;
151 unsigned long long cpu_system;
152 struct procstat *next;
155 static procstat_t *list_head_g = NULL;
158 static mach_port_t port_host_self;
159 static mach_port_t port_task_self;
161 static processor_set_name_array_t pset_list;
162 static mach_msg_type_number_t pset_list_len;
163 /* #endif HAVE_THREAD_INFO */
166 static long pagesize_g;
167 #endif /* KERNEL_LINUX */
169 static procstat_t *ps_list_append (procstat_t *list, const char *name)
174 if ((new = (procstat_t *) malloc (sizeof (procstat_t))) == NULL)
176 memset (new, 0, sizeof (procstat_t));
177 strncpy (new->name, name, PROCSTAT_NAME_LEN);
179 for (ptr = list; ptr != NULL; ptr = ptr->next)
180 if (ptr->next == NULL)
189 static void ps_list_add (procstat_t *list, procstat_t *entry)
194 while ((ptr != NULL) && (strcmp (ptr->name, entry->name) != 0))
200 ptr->num_proc += entry->num_proc;
201 ptr->num_lwp += entry->num_lwp;
202 ptr->vmem_rss += entry->vmem_rss;
203 ptr->vmem_minflt += entry->vmem_minflt;
204 ptr->vmem_majflt += entry->vmem_majflt;
205 ptr->cpu_user += entry->cpu_user;
206 ptr->cpu_system += entry->cpu_system;
209 static void ps_list_reset (procstat_t *ps)
224 static int ps_config (char *key, char *value)
226 if (strcasecmp (key, "CollectName") == 0)
230 entry = ps_list_append (list_head_g, value);
233 syslog (LOG_ERR, "processes plugin: ps_list_append failed.");
236 if (list_head_g == NULL)
247 static void ps_init (void)
250 kern_return_t status;
252 port_host_self = mach_host_self ();
253 port_task_self = mach_task_self ();
255 if (pset_list != NULL)
257 vm_deallocate (port_task_self,
258 (vm_address_t) pset_list,
259 pset_list_len * sizeof (processor_set_t));
264 if ((status = host_processor_sets (port_host_self,
266 &pset_list_len)) != KERN_SUCCESS)
268 syslog (LOG_ERR, "host_processor_sets failed: %s\n",
269 mach_error_string (status));
274 /* #endif HAVE_THREAD_INFO */
277 pagesize_g = sysconf(_SC_PAGESIZE);
278 DBG ("pagesize_g = %li; CONFIG_HZ = %i;",
279 pagesize_g, CONFIG_HZ);
280 #endif /* KERNEL_LINUX */
285 static void ps_write (char *host, char *inst, char *val)
287 rrd_update_file (host, processes_file, val,
288 processes_ds_def, processes_ds_num);
291 static void ps_rss_write (char *host, char *inst, char *val)
296 status = snprintf (filename, 256, ps_rss_file, inst);
297 if ((status < 1) || (status >= 256))
300 rrd_update_file (host, filename, val, ps_rss_ds_def, ps_rss_ds_num);
303 static void ps_cputime_write (char *host, char *inst, char *val)
308 status = snprintf (filename, 256, ps_cputime_file, inst);
309 if ((status < 1) || (status >= 256))
312 DBG ("host = %s; filename = %s; val = %s;",
313 host, filename, val);
314 rrd_update_file (host, filename, val,
315 ps_cputime_ds_def, ps_cputime_ds_num);
318 static void ps_count_write (char *host, char *inst, char *val)
323 status = snprintf (filename, 256, ps_count_file, inst);
324 if ((status < 1) || (status >= 256))
327 DBG ("host = %s; filename = %s; val = %s;",
328 host, filename, val);
329 rrd_update_file (host, filename, val,
330 ps_count_ds_def, ps_count_ds_num);
333 #if PROCESSES_HAVE_READ
334 static void ps_submit (int running,
343 if (snprintf (buf, BUFSIZE, "%u:%i:%i:%i:%i:%i:%i",
344 (unsigned int) curtime,
345 running, sleeping, zombies, stopped, paging,
349 DBG ("running = %i; sleeping = %i; zombies = %i; stopped = %i; paging = %i; blocked = %i;",
350 running, sleeping, zombies, stopped, paging, blocked);
352 plugin_submit (MODULE_NAME, "-", buf);
355 static void ps_submit_proc (procstat_t *ps)
362 snprintf (buffer, 64, "%u:%lu",
363 (unsigned int) curtime,
366 plugin_submit ("ps_rss", ps->name, buffer);
368 snprintf (buffer, 64, "%u:%u:%u",
369 (unsigned int) curtime,
370 /* Make the counter overflow */
371 (unsigned int) (ps->cpu_user & 0xFFFFFFFF),
372 (unsigned int) (ps->cpu_system & 0xFFFFFFFF));
374 plugin_submit ("ps_cputime", ps->name, buffer);
376 snprintf (buffer, 64, "%u:%u:%u",
377 (unsigned int) curtime,
378 ps->num_proc, ps->num_lwp);
380 plugin_submit ("ps_count", ps->name, buffer);
382 DBG ("name = %s; num_proc = %i; num_lwp = %i; vmem_rss = %i; "
383 "vmem_minflt = %i; vmem_majflt = %i; "
384 "cpu_user = %i; cpu_system = %i;",
385 ps->name, ps->num_proc, ps->num_lwp, ps->vmem_rss,
386 ps->vmem_minflt, ps->vmem_majflt, ps->cpu_user,
392 static int *ps_read_tasks (int pid)
395 int list_size = 1; /* size of allocated space, in elements */
396 int list_len = 0; /* number of currently used elements */
402 snprintf (dirname, 64, "/proc/%i/task", pid);
405 if ((dh = opendir (dirname)) == NULL)
407 syslog (LOG_NOTICE, "processes plugin: Failed to open directory `%s'",
412 while ((ent = readdir (dh)) != NULL)
414 if (!isdigit (ent->d_name[0]))
417 if ((list_len + 1) >= list_size)
420 int new_size = 2 * list_size;
421 /* Comes in sizes: 2, 4, 8, 16, ... */
423 new_ptr = (int *) realloc (list, (size_t) (sizeof (int) * new_size));
428 syslog (LOG_ERR, "processes plugin: "
429 "Failed to allocate more memory.");
434 list_size = new_size;
436 memset (list + list_len, 0, sizeof (int) * (list_size - list_len));
439 list[list_len] = atoi (ent->d_name);
440 if (list[list_len] != 0)
446 assert (list_len < list_size);
447 assert (list[list_len] == 0);
452 int ps_read_process (int pid, procstat_t *ps, char *state)
467 memset (ps, 0, sizeof (procstat_t));
469 snprintf (filename, 64, "/proc/%i/stat", pid);
472 if ((fh = fopen (filename, "r")) == NULL)
475 if (fgets (buffer, 1024, fh) == NULL)
483 fields_len = strsplit (buffer, fields, 64);
486 DBG ("`%s' has only %i fields..",
487 filename, fields_len);
490 else if (fields_len != 41)
492 DBG ("WARNING: (fields_len = %i) != 41", fields_len);
495 /* copy the name, strip brackets in the process */
496 name_len = strlen (fields[1]) - 2;
497 if ((fields[1][0] != '(') || (fields[1][name_len + 1] != ')'))
499 DBG ("No brackets found in process name: `%s'", fields[1]);
502 fields[1] = fields[1] + 1;
503 fields[1][name_len] = '\0';
504 strncpy (ps->name, fields[1], PROCSTAT_NAME_LEN);
506 ppid = atoi (fields[3]);
508 if ((tasks = ps_read_tasks (pid)) == NULL)
510 DBG ("ps_read_tasks (%i) failed.", pid);
517 for (i = 0; tasks[i] != 0; i++)
523 /* Leave the rest at zero if this is only an LWP */
524 if (ps->num_proc == 0)
526 DBG ("This is only an LWP: pid = %i; name = %s;",
531 ps->vmem_minflt = atol (fields[9]);
532 ps->vmem_majflt = atol (fields[11]);
533 ps->cpu_user = atoll (fields[13]);
534 ps->cpu_system = atoll (fields[14]);
535 ps->vmem_rss = atol (fields[23]);
537 /* Convert jiffies to useconds */
538 ps->cpu_user = ps->cpu_user * 1000000 / CONFIG_HZ;
539 ps->cpu_system = ps->cpu_system * 1000000 / CONFIG_HZ;
540 ps->vmem_rss = ps->vmem_rss * pagesize_g;
542 *state = fields[2][0];
546 } /* int ps_read_process (...) */
547 #endif /* KERNEL_LINUX */
549 static void ps_read (void)
552 kern_return_t status;
555 processor_set_t port_pset_priv;
558 task_array_t task_list;
559 mach_msg_type_number_t task_list_len;
562 thread_act_array_t thread_list;
563 mach_msg_type_number_t thread_list_len;
564 thread_basic_info_data_t thread_data;
565 mach_msg_type_number_t thread_data_len;
574 * The Mach-concept is a little different from the traditional UNIX
575 * concept: All the work is done in threads. Threads are contained in
576 * `tasks'. Therefore, `task status' doesn't make much sense, since
577 * it's actually a `thread status'.
578 * Tasks are assigned to sets of processors, so that's where you go to
581 for (pset = 0; pset < pset_list_len; pset++)
583 if ((status = host_processor_set_priv (port_host_self,
585 &port_pset_priv)) != KERN_SUCCESS)
587 syslog (LOG_ERR, "host_processor_set_priv failed: %s\n",
588 mach_error_string (status));
592 if ((status = processor_set_tasks (port_pset_priv,
594 &task_list_len)) != KERN_SUCCESS)
596 syslog (LOG_ERR, "processor_set_tasks failed: %s\n",
597 mach_error_string (status));
598 mach_port_deallocate (port_task_self, port_pset_priv);
602 for (task = 0; task < task_list_len; task++)
604 status = task_threads (task_list[task], &thread_list,
606 if (status != KERN_SUCCESS)
608 /* Apple's `top' treats this case a zombie. It
609 * makes sense to some extend: A `zombie'
610 * thread is nonsense, since the task/process
613 DBG ("task_threads failed: %s",
614 mach_error_string (status));
615 if (task_list[task] != port_task_self)
616 mach_port_deallocate (port_task_self,
618 continue; /* with next task_list */
621 for (thread = 0; thread < thread_list_len; thread++)
623 thread_data_len = THREAD_BASIC_INFO_COUNT;
624 status = thread_info (thread_list[thread],
626 (thread_info_t) &thread_data,
628 if (status != KERN_SUCCESS)
630 syslog (LOG_ERR, "thread_info failed: %s\n",
631 mach_error_string (status));
632 if (task_list[task] != port_task_self)
633 mach_port_deallocate (port_task_self,
634 thread_list[thread]);
635 continue; /* with next thread_list */
638 switch (thread_data.run_state)
640 case TH_STATE_RUNNING:
643 case TH_STATE_STOPPED:
644 /* What exactly is `halted'? */
645 case TH_STATE_HALTED:
648 case TH_STATE_WAITING:
651 case TH_STATE_UNINTERRUPTIBLE:
654 /* There is no `zombie' case here,
655 * since there are no zombie-threads.
656 * There's only zombie tasks, which are
660 "Unknown thread status: %s",
661 thread_data.run_state);
663 } /* switch (thread_data.run_state) */
665 if (task_list[task] != port_task_self)
667 status = mach_port_deallocate (port_task_self,
668 thread_list[thread]);
669 if (status != KERN_SUCCESS)
670 syslog (LOG_ERR, "mach_port_deallocate failed: %s",
671 mach_error_string (status));
673 } /* for (thread_list) */
675 if ((status = vm_deallocate (port_task_self,
676 (vm_address_t) thread_list,
677 thread_list_len * sizeof (thread_act_t)))
680 syslog (LOG_ERR, "vm_deallocate failed: %s",
681 mach_error_string (status));
686 /* Only deallocate the task port, if it isn't our own.
687 * Don't know what would happen in that case, but this
688 * is what Apple's top does.. ;) */
689 if (task_list[task] != port_task_self)
691 status = mach_port_deallocate (port_task_self,
693 if (status != KERN_SUCCESS)
694 syslog (LOG_ERR, "mach_port_deallocate failed: %s",
695 mach_error_string (status));
697 } /* for (task_list) */
699 if ((status = vm_deallocate (port_task_self,
700 (vm_address_t) task_list,
701 task_list_len * sizeof (task_t))) != KERN_SUCCESS)
703 syslog (LOG_ERR, "vm_deallocate failed: %s",
704 mach_error_string (status));
709 if ((status = mach_port_deallocate (port_task_self, port_pset_priv))
712 syslog (LOG_ERR, "mach_port_deallocate failed: %s",
713 mach_error_string (status));
715 } /* for (pset_list) */
717 ps_submit (running, sleeping, zombies, stopped, -1, blocked);
718 /* #endif HAVE_THREAD_INFO */
738 running = sleeping = zombies = stopped = paging = blocked = 0;
739 ps_list_reset (list_head_g);
741 if ((proc = opendir ("/proc")) == NULL)
743 syslog (LOG_ERR, "Cannot open `/proc': %s", strerror (errno));
747 while ((ent = readdir (proc)) != NULL)
749 if (!isdigit (ent->d_name[0]))
752 if ((pid = atoi (ent->d_name)) < 1)
755 status = ps_read_process (pid, &ps, &state);
758 DBG ("ps_read_process failed: %i", status);
764 case 'R': running++; break;
765 case 'S': sleeping++; break;
766 case 'D': blocked++; break;
767 case 'Z': zombies++; break;
768 case 'T': stopped++; break;
769 case 'W': paging++; break;
772 if (list_head_g != NULL)
773 ps_list_add (list_head_g, &ps);
778 ps_submit (running, sleeping, zombies, stopped, paging, blocked);
780 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
781 ps_submit_proc (ps_ptr);
782 #endif /* KERNEL_LINUX */
785 # define ps_read NULL
786 #endif /* PROCESSES_HAVE_READ */
788 void module_register (void)
790 plugin_register (MODULE_NAME, ps_init, ps_read, ps_write);
791 plugin_register ("ps_rss", NULL, NULL, ps_rss_write);
792 plugin_register ("ps_cputime", NULL, NULL, ps_cputime_write);
793 plugin_register ("ps_count", NULL, NULL, ps_count_write);
794 cf_register (MODULE_NAME, ps_config, config_keys, config_keys_num);