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 *config_keys[] =
120 static int config_keys_num = 1;
122 typedef struct procstat
124 #define PROCSTAT_NAME_LEN 256
125 char name[PROCSTAT_NAME_LEN];
126 unsigned int num_proc;
127 unsigned int num_lwp;
128 unsigned long vmem_rss;
129 unsigned long vmem_minflt;
130 unsigned long vmem_majflt;
131 unsigned long long cpu_user;
132 unsigned long long cpu_system;
133 struct procstat *next;
136 static procstat_t *list_head_g = NULL;
139 static mach_port_t port_host_self;
140 static mach_port_t port_task_self;
142 static processor_set_name_array_t pset_list;
143 static mach_msg_type_number_t pset_list_len;
144 /* #endif HAVE_THREAD_INFO */
147 static long pagesize_g;
148 #endif /* KERNEL_LINUX */
150 static procstat_t *ps_list_append (procstat_t *list, const char *name)
155 if ((new = (procstat_t *) malloc (sizeof (procstat_t))) == NULL)
157 memset (new, 0, sizeof (procstat_t));
158 strncpy (new->name, name, PROCSTAT_NAME_LEN);
160 for (ptr = list; ptr != NULL; ptr = ptr->next)
161 if (ptr->next == NULL)
170 static void ps_list_add (procstat_t *list, procstat_t *entry)
175 while ((ptr != NULL) && (strcmp (ptr->name, entry->name) != 0))
181 ptr->num_proc += entry->num_proc;
182 ptr->num_lwp += entry->num_lwp;
183 ptr->vmem_rss += entry->vmem_rss;
184 ptr->vmem_minflt += entry->vmem_minflt;
185 ptr->vmem_majflt += entry->vmem_majflt;
186 ptr->cpu_user += entry->cpu_user;
187 ptr->cpu_system += entry->cpu_system;
190 static void ps_list_reset (procstat_t *ps)
205 static int ps_config (char *key, char *value)
207 if (strcasecmp (key, "CollectName") == 0)
211 entry = ps_list_append (list_head_g, value);
214 syslog (LOG_ERR, "processes plugin: ps_list_append failed.");
217 if (list_head_g == NULL)
228 static void ps_init (void)
231 kern_return_t status;
233 port_host_self = mach_host_self ();
234 port_task_self = mach_task_self ();
236 if (pset_list != NULL)
238 vm_deallocate (port_task_self,
239 (vm_address_t) pset_list,
240 pset_list_len * sizeof (processor_set_t));
245 if ((status = host_processor_sets (port_host_self,
247 &pset_list_len)) != KERN_SUCCESS)
249 syslog (LOG_ERR, "host_processor_sets failed: %s\n",
250 mach_error_string (status));
255 /* #endif HAVE_THREAD_INFO */
258 pagesize_g = sysconf(_SC_PAGESIZE);
259 DBG ("pagesize_g = %li; CONFIG_HZ = %i;",
260 pagesize_g, CONFIG_HZ);
261 #endif /* KERNEL_LINUX */
266 static void ps_write (char *host, char *inst, char *val)
268 rrd_update_file (host, processes_file, val,
269 processes_ds_def, processes_ds_num);
272 static void ps_rss_write (char *host, char *inst, char *val)
277 status = snprintf (filename, 256, ps_rss_file, inst);
278 if ((status < 1) || (status >= 256))
281 rrd_update_file (host, filename, val, ps_rss_ds_def, ps_rss_ds_num);
284 #if PROCESSES_HAVE_READ
285 static void ps_submit (int running,
294 if (snprintf (buf, BUFSIZE, "%u:%i:%i:%i:%i:%i:%i",
295 (unsigned int) curtime,
296 running, sleeping, zombies, stopped, paging,
300 DBG ("running = %i; sleeping = %i; zombies = %i; stopped = %i; paging = %i; blocked = %i;",
301 running, sleeping, zombies, stopped, paging, blocked);
303 plugin_submit (MODULE_NAME, "-", buf);
306 static void ps_submit_proc (procstat_t *ps)
313 snprintf (buffer, 64, "%u:%lu",
314 (unsigned int) curtime,
317 plugin_submit ("ps_rss", ps->name, buffer);
319 DBG ("name = %s; num_proc = %i; num_lwp = %i; vmem_rss = %i; "
320 "vmem_minflt = %i; vmem_majflt = %i; "
321 "cpu_user = %i; cpu_system = %i;",
322 ps->name, ps->num_proc, ps->num_lwp, ps->vmem_rss,
323 ps->vmem_minflt, ps->vmem_majflt, ps->cpu_user,
329 static int *ps_read_tasks (int pid)
332 int list_size = 1; /* size of allocated space, in elements */
333 int list_len = 0; /* number of currently used elements */
339 snprintf (dirname, 64, "/proc/%i/task", pid);
342 if ((dh = opendir (dirname)) == NULL)
344 syslog (LOG_NOTICE, "processes plugin: Failed to open directory `%s'",
349 while ((ent = readdir (dh)) != NULL)
351 if (!isdigit (ent->d_name[0]))
354 if ((list_len + 1) >= list_size)
357 int new_size = 2 * list_size;
358 /* Comes in sizes: 2, 4, 8, 16, ... */
360 new_ptr = (int *) realloc (list, (size_t) (sizeof (int) * new_size));
365 syslog (LOG_ERR, "processes plugin: "
366 "Failed to allocate more memory.");
371 list_size = new_size;
373 memset (list + list_len, 0, sizeof (int) * (list_size - list_len));
376 list[list_len] = atoi (ent->d_name);
377 if (list[list_len] != 0)
383 assert (list_len < list_size);
384 assert (list[list_len] == 0);
389 int ps_read_process (int pid, procstat_t *ps, char *state)
404 memset (ps, 0, sizeof (procstat_t));
406 snprintf (filename, 64, "/proc/%i/stat", pid);
409 if ((fh = fopen (filename, "r")) == NULL)
412 if (fgets (buffer, 1024, fh) == NULL)
420 fields_len = strsplit (buffer, fields, 64);
423 DBG ("`%s' has only %i fields..",
424 filename, fields_len);
427 else if (fields_len != 41)
429 DBG ("WARNING: (fields_len = %i) != 41", fields_len);
432 /* copy the name, strip brackets in the process */
433 name_len = strlen (fields[1]) - 2;
434 if ((fields[1][0] != '(') || (fields[1][name_len + 1] != ')'))
436 DBG ("No brackets found in process name: `%s'", fields[1]);
439 fields[1] = fields[1] + 1;
440 fields[1][name_len] = '\0';
441 strncpy (ps->name, fields[1], PROCSTAT_NAME_LEN);
443 ppid = atoi (fields[3]);
445 if ((tasks = ps_read_tasks (pid)) == NULL)
447 DBG ("ps_read_tasks (%i) failed.", pid);
454 for (i = 0; tasks[i] != 0; i++)
460 /* Leave the rest at zero if this is only an LWP */
461 if (ps->num_proc == 0)
463 DBG ("This is only an LWP: pid = %i; name = %s;",
468 ps->vmem_minflt = atol (fields[9]);
469 ps->vmem_majflt = atol (fields[11]);
470 ps->cpu_user = atoll (fields[13]);
471 ps->cpu_system = atoll (fields[14]);
472 ps->vmem_rss = atol (fields[23]);
474 /* Convert jiffies to useconds */
475 ps->cpu_user = ps->cpu_user * 1000000 / CONFIG_HZ;
476 ps->cpu_system = ps->cpu_system * 1000000 / CONFIG_HZ;
477 ps->vmem_rss = ps->vmem_rss * pagesize_g;
479 *state = fields[2][0];
483 } /* int ps_read_process (...) */
484 #endif /* KERNEL_LINUX */
486 static void ps_read (void)
489 kern_return_t status;
492 processor_set_t port_pset_priv;
495 task_array_t task_list;
496 mach_msg_type_number_t task_list_len;
499 thread_act_array_t thread_list;
500 mach_msg_type_number_t thread_list_len;
501 thread_basic_info_data_t thread_data;
502 mach_msg_type_number_t thread_data_len;
511 * The Mach-concept is a little different from the traditional UNIX
512 * concept: All the work is done in threads. Threads are contained in
513 * `tasks'. Therefore, `task status' doesn't make much sense, since
514 * it's actually a `thread status'.
515 * Tasks are assigned to sets of processors, so that's where you go to
518 for (pset = 0; pset < pset_list_len; pset++)
520 if ((status = host_processor_set_priv (port_host_self,
522 &port_pset_priv)) != KERN_SUCCESS)
524 syslog (LOG_ERR, "host_processor_set_priv failed: %s\n",
525 mach_error_string (status));
529 if ((status = processor_set_tasks (port_pset_priv,
531 &task_list_len)) != KERN_SUCCESS)
533 syslog (LOG_ERR, "processor_set_tasks failed: %s\n",
534 mach_error_string (status));
535 mach_port_deallocate (port_task_self, port_pset_priv);
539 for (task = 0; task < task_list_len; task++)
541 status = task_threads (task_list[task], &thread_list,
543 if (status != KERN_SUCCESS)
545 /* Apple's `top' treats this case a zombie. It
546 * makes sense to some extend: A `zombie'
547 * thread is nonsense, since the task/process
550 DBG ("task_threads failed: %s",
551 mach_error_string (status));
552 if (task_list[task] != port_task_self)
553 mach_port_deallocate (port_task_self,
555 continue; /* with next task_list */
558 for (thread = 0; thread < thread_list_len; thread++)
560 thread_data_len = THREAD_BASIC_INFO_COUNT;
561 status = thread_info (thread_list[thread],
563 (thread_info_t) &thread_data,
565 if (status != KERN_SUCCESS)
567 syslog (LOG_ERR, "thread_info failed: %s\n",
568 mach_error_string (status));
569 if (task_list[task] != port_task_self)
570 mach_port_deallocate (port_task_self,
571 thread_list[thread]);
572 continue; /* with next thread_list */
575 switch (thread_data.run_state)
577 case TH_STATE_RUNNING:
580 case TH_STATE_STOPPED:
581 /* What exactly is `halted'? */
582 case TH_STATE_HALTED:
585 case TH_STATE_WAITING:
588 case TH_STATE_UNINTERRUPTIBLE:
591 /* There is no `zombie' case here,
592 * since there are no zombie-threads.
593 * There's only zombie tasks, which are
597 "Unknown thread status: %s",
598 thread_data.run_state);
600 } /* switch (thread_data.run_state) */
602 if (task_list[task] != port_task_self)
604 status = mach_port_deallocate (port_task_self,
605 thread_list[thread]);
606 if (status != KERN_SUCCESS)
607 syslog (LOG_ERR, "mach_port_deallocate failed: %s",
608 mach_error_string (status));
610 } /* for (thread_list) */
612 if ((status = vm_deallocate (port_task_self,
613 (vm_address_t) thread_list,
614 thread_list_len * sizeof (thread_act_t)))
617 syslog (LOG_ERR, "vm_deallocate failed: %s",
618 mach_error_string (status));
623 /* Only deallocate the task port, if it isn't our own.
624 * Don't know what would happen in that case, but this
625 * is what Apple's top does.. ;) */
626 if (task_list[task] != port_task_self)
628 status = mach_port_deallocate (port_task_self,
630 if (status != KERN_SUCCESS)
631 syslog (LOG_ERR, "mach_port_deallocate failed: %s",
632 mach_error_string (status));
634 } /* for (task_list) */
636 if ((status = vm_deallocate (port_task_self,
637 (vm_address_t) task_list,
638 task_list_len * sizeof (task_t))) != KERN_SUCCESS)
640 syslog (LOG_ERR, "vm_deallocate failed: %s",
641 mach_error_string (status));
646 if ((status = mach_port_deallocate (port_task_self, port_pset_priv))
649 syslog (LOG_ERR, "mach_port_deallocate failed: %s",
650 mach_error_string (status));
652 } /* for (pset_list) */
654 ps_submit (running, sleeping, zombies, stopped, -1, blocked);
655 /* #endif HAVE_THREAD_INFO */
675 running = sleeping = zombies = stopped = paging = blocked = 0;
676 ps_list_reset (list_head_g);
678 if ((proc = opendir ("/proc")) == NULL)
680 syslog (LOG_ERR, "Cannot open `/proc': %s", strerror (errno));
684 while ((ent = readdir (proc)) != NULL)
686 if (!isdigit (ent->d_name[0]))
689 if ((pid = atoi (ent->d_name)) < 1)
692 status = ps_read_process (pid, &ps, &state);
695 DBG ("ps_read_process failed: %i", status);
701 case 'R': running++; break;
702 case 'S': sleeping++; break;
703 case 'D': blocked++; break;
704 case 'Z': zombies++; break;
705 case 'T': stopped++; break;
706 case 'W': paging++; break;
709 if (list_head_g != NULL)
710 ps_list_add (list_head_g, &ps);
715 ps_submit (running, sleeping, zombies, stopped, paging, blocked);
717 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
718 ps_submit_proc (ps_ptr);
719 #endif /* KERNEL_LINUX */
722 # define ps_read NULL
723 #endif /* PROCESSES_HAVE_READ */
725 void module_register (void)
727 plugin_register (MODULE_NAME, ps_init, ps_read, ps_write);
728 plugin_register ("ps_rss", NULL, NULL, ps_rss_write);
729 cf_register (MODULE_NAME, ps_config, config_keys, config_keys_num);