1 /**
2 * collectd - src/processes.c
3 * Copyright (C) 2005 Lyonel Vincent
4 * Copyright (C) 2006-2008 Florian Forster (Mach code)
5 * Copyright (C) 2008 Oleg King
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 *
21 * Authors:
22 * Lyonel Vincent <lyonel at ezix.org>
23 * Florian octo Forster <octo at verplant.org>
24 * Oleg King <king2 at kaluga.ru>
25 * Sebastian Harl <sh at tokkee.org>
26 **/
28 #include "collectd.h"
29 #include "common.h"
30 #include "plugin.h"
31 #include "configfile.h"
33 /* Include header files for the mach system, if they exist.. */
34 #if HAVE_THREAD_INFO
35 # if HAVE_MACH_MACH_INIT_H
36 # include <mach/mach_init.h>
37 # endif
38 # if HAVE_MACH_HOST_PRIV_H
39 # include <mach/host_priv.h>
40 # endif
41 # if HAVE_MACH_MACH_ERROR_H
42 # include <mach/mach_error.h>
43 # endif
44 # if HAVE_MACH_MACH_HOST_H
45 # include <mach/mach_host.h>
46 # endif
47 # if HAVE_MACH_MACH_PORT_H
48 # include <mach/mach_port.h>
49 # endif
50 # if HAVE_MACH_MACH_TYPES_H
51 # include <mach/mach_types.h>
52 # endif
53 # if HAVE_MACH_MESSAGE_H
54 # include <mach/message.h>
55 # endif
56 # if HAVE_MACH_PROCESSOR_SET_H
57 # include <mach/processor_set.h>
58 # endif
59 # if HAVE_MACH_TASK_H
60 # include <mach/task.h>
61 # endif
62 # if HAVE_MACH_THREAD_ACT_H
63 # include <mach/thread_act.h>
64 # endif
65 # if HAVE_MACH_VM_REGION_H
66 # include <mach/vm_region.h>
67 # endif
68 # if HAVE_MACH_VM_MAP_H
69 # include <mach/vm_map.h>
70 # endif
71 # if HAVE_MACH_VM_PROT_H
72 # include <mach/vm_prot.h>
73 # endif
74 # if HAVE_SYS_SYSCTL_H
75 # include <sys/sysctl.h>
76 # endif
77 /* #endif HAVE_THREAD_INFO */
79 #elif KERNEL_LINUX
80 # if HAVE_LINUX_CONFIG_H
81 # include <linux/config.h>
82 # endif
83 # ifndef CONFIG_HZ
84 # define CONFIG_HZ 100
85 # endif
86 /* #endif KERNEL_LINUX */
88 #elif HAVE_LIBKVM_GETPROCS
89 # include <kvm.h>
90 # include <sys/user.h>
91 # include <sys/proc.h>
92 # if HAVE_SYS_SYSCTL_H
93 # include <sys/sysctl.h>
94 # endif
95 /* #endif HAVE_LIBKVM_GETPROCS */
97 #else
98 # error "No applicable input method."
99 #endif
101 #if HAVE_REGEX_H
102 # include <regex.h>
103 #endif
105 #ifndef ARG_MAX
106 # define ARG_MAX 4096
107 #endif
109 #define BUFSIZE 256
111 static const char *config_keys[] =
112 {
113 "Process",
114 "ProcessMatch"
115 };
116 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
118 typedef struct procstat_entry_s
119 {
120 unsigned long id;
121 unsigned long age;
123 unsigned long num_proc;
124 unsigned long num_lwp;
125 unsigned long vmem_size;
126 unsigned long vmem_rss;
127 unsigned long stack_size;
129 unsigned long vmem_minflt;
130 unsigned long vmem_majflt;
131 unsigned long vmem_minflt_counter;
132 unsigned long vmem_majflt_counter;
134 unsigned long cpu_user;
135 unsigned long cpu_system;
136 unsigned long cpu_user_counter;
137 unsigned long cpu_system_counter;
139 struct procstat_entry_s *next;
140 } procstat_entry_t;
142 #define PROCSTAT_NAME_LEN 256
143 typedef struct procstat
144 {
145 char name[PROCSTAT_NAME_LEN];
146 #if HAVE_REGEX_H
147 regex_t *re;
148 #endif
150 unsigned long num_proc;
151 unsigned long num_lwp;
152 unsigned long vmem_size;
153 unsigned long vmem_rss;
154 unsigned long stack_size;
156 unsigned long vmem_minflt_counter;
157 unsigned long vmem_majflt_counter;
159 unsigned long cpu_user_counter;
160 unsigned long cpu_system_counter;
162 struct procstat *next;
163 struct procstat_entry_s *instances;
164 } procstat_t;
166 static procstat_t *list_head_g = NULL;
168 #if HAVE_THREAD_INFO
169 static mach_port_t port_host_self;
170 static mach_port_t port_task_self;
172 static processor_set_name_array_t pset_list;
173 static mach_msg_type_number_t pset_list_len;
174 /* #endif HAVE_THREAD_INFO */
176 #elif KERNEL_LINUX
177 static long pagesize_g;
178 /* #endif KERNEL_LINUX */
180 #elif HAVE_LIBKVM_GETPROCS
181 /* no global variables */
182 #endif /* HAVE_LIBKVM_GETPROCS */
184 /* put name of process from config to list_head_g tree
185 list_head_g is a list of 'procstat_t' structs with
186 processes names we want to watch */
187 static void ps_list_register (const char *name, const char *regexp)
188 {
189 procstat_t *new;
190 procstat_t *ptr;
191 int status;
193 new = (procstat_t *) malloc (sizeof (procstat_t));
194 if (new == NULL)
195 {
196 ERROR ("processes plugin: ps_list_register: malloc failed.");
197 return;
198 }
199 memset (new, 0, sizeof (procstat_t));
200 sstrncpy (new->name, name, sizeof (new->name));
202 #if HAVE_REGEX_H
203 if (regexp != NULL)
204 {
205 DEBUG ("ProcessMatch: adding \"%s\" as criteria to process %s.", regexp, name);
206 new->re = (regex_t *) malloc (sizeof (regex_t));
207 if (new->re == NULL)
208 {
209 ERROR ("processes plugin: ps_list_register: malloc failed.");
210 sfree (new);
211 return;
212 }
214 status = regcomp (new->re, regexp, REG_EXTENDED | REG_NOSUB);
215 if (status != 0)
216 {
217 DEBUG ("ProcessMatch: compiling the regular expression \"%s\" failed.", regexp);
218 sfree(new->re);
219 return;
220 }
221 }
222 #else
223 if (regexp != NULL)
224 {
225 ERROR ("processes plugin: ps_list_register: "
226 "Regular expression \"%s\" found in config "
227 "file, but support for regular expressions "
228 "has been dispabled at compile time.",
229 regexp);
230 sfree (new);
231 return;
232 }
233 #endif
235 for (ptr = list_head_g; ptr != NULL; ptr = ptr->next)
236 {
237 if (strcmp (ptr->name, name) == 0)
238 {
239 WARNING ("processes plugin: You have configured more "
240 "than one `Process' or "
241 "`ProcessMatch' with the same name. "
242 "All but the first setting will be "
243 "ignored.");
244 sfree (new->re);
245 sfree (new);
246 return;
247 }
249 if (ptr->next == NULL)
250 break;
251 }
253 if (ptr == NULL)
254 list_head_g = new;
255 else
256 ptr->next = new;
257 } /* void ps_list_register */
259 /* try to match name against entry, returns 1 if success */
260 static int ps_list_match (const char *name, const char *cmdline, procstat_t *ps)
261 {
262 #if HAVE_REGEX_H
263 if (ps->re != NULL)
264 {
265 int status;
266 const char *str;
268 str = cmdline;
269 if ((str == NULL) || (str[0] == 0))
270 str = name;
272 assert (str != NULL);
274 status = regexec (ps->re, str,
275 /* nmatch = */ 0,
276 /* pmatch = */ NULL,
277 /* eflags = */ 0);
278 if (status == 0)
279 return (1);
280 }
281 else
282 #endif
283 if (strcmp (ps->name, name) == 0)
284 return (1);
286 return (0);
287 } /* int ps_list_match */
289 /* add process entry to 'instances' of process 'name' (or refresh it) */
290 static void ps_list_add (const char *name, const char *cmdline, procstat_entry_t *entry)
291 {
292 procstat_t *ps;
293 procstat_entry_t *pse;
295 if (entry->id == 0)
296 return;
298 for (ps = list_head_g; ps != NULL; ps = ps->next)
299 {
300 if ((ps_list_match (name, cmdline, ps)) == 0)
301 continue;
303 for (pse = ps->instances; pse != NULL; pse = pse->next)
304 if ((pse->id == entry->id) || (pse->next == NULL))
305 break;
307 if ((pse == NULL) || (pse->id != entry->id))
308 {
309 procstat_entry_t *new;
311 new = (procstat_entry_t *) malloc (sizeof (procstat_entry_t));
312 if (new == NULL)
313 return;
314 memset (new, 0, sizeof (procstat_entry_t));
315 new->id = entry->id;
317 if (pse == NULL)
318 ps->instances = new;
319 else
320 pse->next = new;
322 pse = new;
323 }
325 pse->age = 0;
326 pse->num_proc = entry->num_proc;
327 pse->num_lwp = entry->num_lwp;
328 pse->vmem_size = entry->vmem_size;
329 pse->vmem_rss = entry->vmem_rss;
330 pse->stack_size = entry->stack_size;
332 ps->num_proc += pse->num_proc;
333 ps->num_lwp += pse->num_lwp;
334 ps->vmem_size += pse->vmem_size;
335 ps->vmem_rss += pse->vmem_rss;
336 ps->stack_size += pse->stack_size;
338 if ((entry->vmem_minflt_counter == 0)
339 && (entry->vmem_majflt_counter == 0))
340 {
341 pse->vmem_minflt_counter += entry->vmem_minflt;
342 pse->vmem_minflt = entry->vmem_minflt;
344 pse->vmem_majflt_counter += entry->vmem_majflt;
345 pse->vmem_majflt = entry->vmem_majflt;
346 }
347 else
348 {
349 if (entry->vmem_minflt_counter < pse->vmem_minflt_counter)
350 {
351 pse->vmem_minflt = entry->vmem_minflt_counter
352 + (ULONG_MAX - pse->vmem_minflt_counter);
353 }
354 else
355 {
356 pse->vmem_minflt = entry->vmem_minflt_counter - pse->vmem_minflt_counter;
357 }
358 pse->vmem_minflt_counter = entry->vmem_minflt_counter;
360 if (entry->vmem_majflt_counter < pse->vmem_majflt_counter)
361 {
362 pse->vmem_majflt = entry->vmem_majflt_counter
363 + (ULONG_MAX - pse->vmem_majflt_counter);
364 }
365 else
366 {
367 pse->vmem_majflt = entry->vmem_majflt_counter - pse->vmem_majflt_counter;
368 }
369 pse->vmem_majflt_counter = entry->vmem_majflt_counter;
370 }
372 ps->vmem_minflt_counter += pse->vmem_minflt;
373 ps->vmem_majflt_counter += pse->vmem_majflt;
375 if ((entry->cpu_user_counter == 0)
376 && (entry->cpu_system_counter == 0))
377 {
378 pse->cpu_user_counter += entry->cpu_user;
379 pse->cpu_user = entry->cpu_user;
381 pse->cpu_system_counter += entry->cpu_system;
382 pse->cpu_system = entry->cpu_system;
383 }
384 else
385 {
386 if (entry->cpu_user_counter < pse->cpu_user_counter)
387 {
388 pse->cpu_user = entry->cpu_user_counter
389 + (ULONG_MAX - pse->cpu_user_counter);
390 }
391 else
392 {
393 pse->cpu_user = entry->cpu_user_counter - pse->cpu_user_counter;
394 }
395 pse->cpu_user_counter = entry->cpu_user_counter;
397 if (entry->cpu_system_counter < pse->cpu_system_counter)
398 {
399 pse->cpu_system = entry->cpu_system_counter
400 + (ULONG_MAX - pse->cpu_system_counter);
401 }
402 else
403 {
404 pse->cpu_system = entry->cpu_system_counter - pse->cpu_system_counter;
405 }
406 pse->cpu_system_counter = entry->cpu_system_counter;
407 }
409 ps->cpu_user_counter += pse->cpu_user;
410 ps->cpu_system_counter += pse->cpu_system;
411 }
412 }
414 /* remove old entries from instances of processes in list_head_g */
415 static void ps_list_reset (void)
416 {
417 procstat_t *ps;
418 procstat_entry_t *pse;
419 procstat_entry_t *pse_prev;
421 for (ps = list_head_g; ps != NULL; ps = ps->next)
422 {
423 ps->num_proc = 0;
424 ps->num_lwp = 0;
425 ps->vmem_size = 0;
426 ps->vmem_rss = 0;
427 ps->stack_size = 0;
429 pse_prev = NULL;
430 pse = ps->instances;
431 while (pse != NULL)
432 {
433 if (pse->age > 10)
434 {
435 DEBUG ("Removing this procstat entry cause it's too old: "
436 "id = %lu; name = %s;",
437 pse->id, ps->name);
439 if (pse_prev == NULL)
440 {
441 ps->instances = pse->next;
442 free (pse);
443 pse = ps->instances;
444 }
445 else
446 {
447 pse_prev->next = pse->next;
448 free (pse);
449 pse = pse_prev->next;
450 }
451 }
452 else
453 {
454 pse->age++;
455 pse_prev = pse;
456 pse = pse->next;
457 }
458 } /* while (pse != NULL) */
459 } /* for (ps = list_head_g; ps != NULL; ps = ps->next) */
460 }
462 /* put all pre-defined 'Process' names from config to list_head_g tree */
463 static int ps_config (const char *key, const char *value)
464 {
465 if (strcasecmp (key, "Process") == 0)
466 {
467 ps_list_register (value, NULL);
468 }
469 else if (strcasecmp (key, "ProcessMatch") == 0)
470 {
471 char *new_val;
472 char *fields[3];
473 int fields_num;
475 new_val = strdup (value);
476 if (new_val == NULL) {
477 ERROR ("processes plugin: strdup failed when processing "
478 "`ProcessMatch %s'.", value);
479 return (1);
480 }
482 fields_num = strsplit (new_val, fields,
483 STATIC_ARRAY_SIZE (fields));
484 if (fields_num != 2)
485 {
486 ERROR ("processes plugin: `ProcessMatch' needs exactly "
487 "two string arguments.");
488 sfree (new_val);
489 return (1);
490 }
491 ps_list_register (fields[0], fields[1]);
492 sfree (new_val);
493 }
494 else
495 {
496 ERROR ("processes plugin: The `%s' configuration option is not "
497 "understood and will be ignored.", key);
498 return (-1);
499 }
501 return (0);
502 }
504 static int ps_init (void)
505 {
506 #if HAVE_THREAD_INFO
507 kern_return_t status;
509 port_host_self = mach_host_self ();
510 port_task_self = mach_task_self ();
512 if (pset_list != NULL)
513 {
514 vm_deallocate (port_task_self,
515 (vm_address_t) pset_list,
516 pset_list_len * sizeof (processor_set_t));
517 pset_list = NULL;
518 pset_list_len = 0;
519 }
521 if ((status = host_processor_sets (port_host_self,
522 &pset_list,
523 &pset_list_len)) != KERN_SUCCESS)
524 {
525 ERROR ("host_processor_sets failed: %s\n",
526 mach_error_string (status));
527 pset_list = NULL;
528 pset_list_len = 0;
529 return (-1);
530 }
531 /* #endif HAVE_THREAD_INFO */
533 #elif KERNEL_LINUX
534 pagesize_g = sysconf(_SC_PAGESIZE);
535 DEBUG ("pagesize_g = %li; CONFIG_HZ = %i;",
536 pagesize_g, CONFIG_HZ);
537 /* #endif KERNEL_LINUX */
539 #elif HAVE_LIBKVM_GETPROCS
540 /* no initialization */
541 #endif /* HAVE_LIBKVM_GETPROCS */
543 return (0);
544 } /* int ps_init */
546 /* submit global state (e.g.: qty of zombies, running, etc..) */
547 static void ps_submit_state (const char *state, double value)
548 {
549 value_t values[1];
550 value_list_t vl = VALUE_LIST_INIT;
552 values[0].gauge = value;
554 vl.values = values;
555 vl.values_len = 1;
556 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
557 sstrncpy (vl.plugin, "processes", sizeof (vl.plugin));
558 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
559 sstrncpy (vl.type, "ps_state", sizeof (vl.type));
560 sstrncpy (vl.type_instance, state, sizeof (vl.type_instance));
562 plugin_dispatch_values (&vl);
563 }
565 /* submit info about specific process (e.g.: memory taken, cpu usage, etc..) */
566 static void ps_submit_proc_list (procstat_t *ps)
567 {
568 value_t values[2];
569 value_list_t vl = VALUE_LIST_INIT;
571 vl.values = values;
572 vl.values_len = 2;
573 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
574 sstrncpy (vl.plugin, "processes", sizeof (vl.plugin));
575 sstrncpy (vl.plugin_instance, ps->name, sizeof (vl.plugin_instance));
577 sstrncpy (vl.type, "ps_vm", sizeof (vl.type));
578 vl.values[0].gauge = ps->vmem_size;
579 vl.values_len = 1;
580 plugin_dispatch_values (&vl);
582 sstrncpy (vl.type, "ps_rss", sizeof (vl.type));
583 vl.values[0].gauge = ps->vmem_rss;
584 vl.values_len = 1;
585 plugin_dispatch_values (&vl);
587 sstrncpy (vl.type, "ps_stacksize", sizeof (vl.type));
588 vl.values[0].gauge = ps->stack_size;
589 vl.values_len = 1;
590 plugin_dispatch_values (&vl);
592 sstrncpy (vl.type, "ps_cputime", sizeof (vl.type));
593 vl.values[0].counter = ps->cpu_user_counter;
594 vl.values[1].counter = ps->cpu_system_counter;
595 vl.values_len = 2;
596 plugin_dispatch_values (&vl);
598 sstrncpy (vl.type, "ps_count", sizeof (vl.type));
599 vl.values[0].gauge = ps->num_proc;
600 vl.values[1].gauge = ps->num_lwp;
601 vl.values_len = 2;
602 plugin_dispatch_values (&vl);
604 sstrncpy (vl.type, "ps_pagefaults", sizeof (vl.type));
605 vl.values[0].counter = ps->vmem_minflt_counter;
606 vl.values[1].counter = ps->vmem_majflt_counter;
607 vl.values_len = 2;
608 plugin_dispatch_values (&vl);
610 DEBUG ("name = %s; num_proc = %lu; num_lwp = %lu; vmem_rss = %lu; "
611 "vmem_minflt_counter = %lu; vmem_majflt_counter = %lu; "
612 "cpu_user_counter = %lu; cpu_system_counter = %lu;",
613 ps->name, ps->num_proc, ps->num_lwp, ps->vmem_rss,
614 ps->vmem_minflt_counter, ps->vmem_majflt_counter,
615 ps->cpu_user_counter, ps->cpu_system_counter);
616 } /* void ps_submit_proc_list */
618 /* ------- additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
619 #if KERNEL_LINUX
620 static int *ps_read_tasks (int pid)
621 {
622 int *list = NULL;
623 int list_size = 1; /* size of allocated space, in elements */
624 int list_len = 0; /* number of currently used elements */
626 char dirname[64];
627 DIR *dh;
628 struct dirent *ent;
630 ssnprintf (dirname, sizeof (dirname), "/proc/%i/task", pid);
632 if ((dh = opendir (dirname)) == NULL)
633 {
634 DEBUG ("Failed to open directory `%s'", dirname);
635 return (NULL);
636 }
638 while ((ent = readdir (dh)) != NULL)
639 {
640 if (!isdigit (ent->d_name[0]))
641 continue;
643 if ((list_len + 1) >= list_size)
644 {
645 int *new_ptr;
646 int new_size = 2 * list_size;
647 /* Comes in sizes: 2, 4, 8, 16, ... */
649 new_ptr = (int *) realloc (list, (size_t) (sizeof (int) * new_size));
650 if (new_ptr == NULL)
651 {
652 if (list != NULL)
653 free (list);
654 ERROR ("processes plugin: "
655 "Failed to allocate more memory.");
656 return (NULL);
657 }
659 list = new_ptr;
660 list_size = new_size;
662 memset (list + list_len, 0, sizeof (int) * (list_size - list_len));
663 }
665 list[list_len] = atoi (ent->d_name);
666 if (list[list_len] != 0)
667 list_len++;
668 }
670 closedir (dh);
672 if (list_len == 0)
673 return (NULL);
675 assert (list_len < list_size);
676 assert (list[list_len] == 0);
678 return (list);
679 } /* int *ps_read_tasks */
681 int ps_read_process (int pid, procstat_t *ps, char *state)
682 {
683 char filename[64];
684 char buffer[1024];
686 char *fields[64];
687 char fields_len;
689 int *tasks;
690 int i;
692 int ppid;
693 int name_len;
695 long long unsigned cpu_user_counter;
696 long long unsigned cpu_system_counter;
697 long long unsigned vmem_size;
698 long long unsigned vmem_rss;
699 long long unsigned stack_size;
701 memset (ps, 0, sizeof (procstat_t));
703 ssnprintf (filename, sizeof (filename), "/proc/%i/stat", pid);
705 i = read_file_contents (filename, buffer, sizeof(buffer) - 1);
706 if (i <= 0)
707 return (-1);
708 buffer[i] = 0;
710 fields_len = strsplit (buffer, fields, 64);
711 if (fields_len < 24)
712 {
713 DEBUG ("processes plugin: ps_read_process (pid = %i):"
714 " `%s' has only %i fields..",
715 (int) pid, filename, fields_len);
716 return (-1);
717 }
719 /* copy the name, strip brackets in the process */
720 name_len = strlen (fields[1]) - 2;
721 if ((fields[1][0] != '(') || (fields[1][name_len + 1] != ')'))
722 {
723 DEBUG ("No brackets found in process name: `%s'", fields[1]);
724 return (-1);
725 }
726 fields[1] = fields[1] + 1;
727 fields[1][name_len] = '\0';
728 strncpy (ps->name, fields[1], PROCSTAT_NAME_LEN);
730 ppid = atoi (fields[3]);
732 *state = fields[2][0];
734 if (*state == 'Z')
735 {
736 ps->num_lwp = 0;
737 ps->num_proc = 0;
738 }
739 else if ((tasks = ps_read_tasks (pid)) == NULL)
740 {
741 /* Kernel 2.4 or so */
742 ps->num_lwp = 1;
743 ps->num_proc = 1;
744 }
745 else
746 {
747 ps->num_lwp = 0;
748 ps->num_proc = 1;
749 for (i = 0; tasks[i] != 0; i++)
750 ps->num_lwp++;
752 free (tasks);
753 tasks = NULL;
754 }
756 /* Leave the rest at zero if this is only a zombi */
757 if (ps->num_proc == 0)
758 {
759 DEBUG ("processes plugin: This is only a zombi: pid = %i; "
760 "name = %s;", pid, ps->name);
761 return (0);
762 }
764 cpu_user_counter = atoll (fields[13]);
765 cpu_system_counter = atoll (fields[14]);
766 vmem_size = atoll (fields[22]);
767 vmem_rss = atoll (fields[23]);
768 ps->vmem_minflt_counter = atol (fields[9]);
769 ps->vmem_majflt_counter = atol (fields[11]);
771 {
772 unsigned long long stack_start = atoll (fields[27]);
773 unsigned long long stack_ptr = atoll (fields[28]);
775 stack_size = (stack_start > stack_ptr)
776 ? stack_start - stack_ptr
777 : stack_ptr - stack_start;
778 }
780 /* Convert jiffies to useconds */
781 cpu_user_counter = cpu_user_counter * 1000000 / CONFIG_HZ;
782 cpu_system_counter = cpu_system_counter * 1000000 / CONFIG_HZ;
783 vmem_rss = vmem_rss * pagesize_g;
785 ps->cpu_user_counter = (unsigned long) cpu_user_counter;
786 ps->cpu_system_counter = (unsigned long) cpu_system_counter;
787 ps->vmem_size = (unsigned long) vmem_size;
788 ps->vmem_rss = (unsigned long) vmem_rss;
789 ps->stack_size = (unsigned long) stack_size;
791 /* success */
792 return (0);
793 } /* int ps_read_process (...) */
795 static char *ps_get_cmdline (pid_t pid, char *name, char *buf, size_t buf_len)
796 {
797 char *buf_ptr;
798 size_t len;
800 char file[PATH_MAX];
801 int fd;
803 size_t n;
805 if ((pid < 1) || (NULL == buf) || (buf_len < 2))
806 return NULL;
808 ssnprintf (file, sizeof (file), "/proc/%u/cmdline", pid);
810 fd = open (file, O_RDONLY);
811 if (fd < 0) {
812 char errbuf[4096];
813 WARNING ("processes plugin: Failed to open `%s': %s.", file,
814 sstrerror (errno, errbuf, sizeof (errbuf)));
815 return NULL;
816 }
818 buf_ptr = buf;
819 len = buf_len;
821 n = 0;
823 while (42) {
824 ssize_t status;
826 status = read (fd, (void *)buf_ptr, len);
828 if (status < 0) {
829 char errbuf[4096];
831 if ((EAGAIN == errno) || (EINTR == errno))
832 continue;
834 WARNING ("processes plugin: Failed to read from `%s': %s.", file,
835 sstrerror (errno, errbuf, sizeof (errbuf)));
836 close (fd);
837 return NULL;
838 }
840 n += status;
842 if (status == 0)
843 break;
845 buf_ptr += status;
846 len -= status;
848 if (len <= 0)
849 break;
850 }
852 close (fd);
854 if (0 == n) {
855 /* cmdline not available; e.g. kernel thread, zombie */
856 if (NULL == name)
857 return NULL;
859 ssnprintf (buf, buf_len, "[%s]", name);
860 return buf;
861 }
863 assert (n <= buf_len);
865 if (n == buf_len)
866 --n;
867 buf[n] = '\0';
869 --n;
870 /* remove trailing whitespace */
871 while ((n > 0) && (isspace (buf[n]) || ('\0' == buf[n]))) {
872 buf[n] = '\0';
873 --n;
874 }
876 /* arguments are separated by '\0' in /proc/<pid>/cmdline */
877 while (n > 0) {
878 if ('\0' == buf[n])
879 buf[n] = ' ';
880 --n;
881 }
882 return buf;
883 } /* char *ps_get_cmdline (...) */
884 #endif /* KERNEL_LINUX */
886 #if HAVE_THREAD_INFO
887 static int mach_get_task_name (task_t t, int *pid, char *name, size_t name_max_len)
888 {
889 int mib[4];
891 struct kinfo_proc kp;
892 size_t kp_size;
894 mib[0] = CTL_KERN;
895 mib[1] = KERN_PROC;
896 mib[2] = KERN_PROC_PID;
898 if (pid_for_task (t, pid) != KERN_SUCCESS)
899 return (-1);
900 mib[3] = *pid;
902 kp_size = sizeof (kp);
903 if (sysctl (mib, 4, &kp, &kp_size, NULL, 0) != 0)
904 return (-1);
906 if (name_max_len > (MAXCOMLEN + 1))
907 name_max_len = MAXCOMLEN + 1;
909 strncpy (name, kp.kp_proc.p_comm, name_max_len - 1);
910 name[name_max_len - 1] = '\0';
912 DEBUG ("pid = %i; name = %s;", *pid, name);
914 /* We don't do the special handling for `p_comm == "LaunchCFMApp"' as
915 * `top' does it, because it is a lot of work and only used when
916 * debugging. -octo */
918 return (0);
919 }
920 #endif /* HAVE_THREAD_INFO */
921 /* ------- end of additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
923 /* do actual readings from kernel */
924 static int ps_read (void)
925 {
926 #if HAVE_THREAD_INFO
927 kern_return_t status;
929 int pset;
930 processor_set_t port_pset_priv;
932 int task;
933 task_array_t task_list;
934 mach_msg_type_number_t task_list_len;
936 int task_pid;
937 char task_name[MAXCOMLEN + 1];
939 int thread;
940 thread_act_array_t thread_list;
941 mach_msg_type_number_t thread_list_len;
942 thread_basic_info_data_t thread_data;
943 mach_msg_type_number_t thread_data_len;
945 int running = 0;
946 int sleeping = 0;
947 int zombies = 0;
948 int stopped = 0;
949 int blocked = 0;
951 procstat_t *ps;
952 procstat_entry_t pse;
954 ps_list_reset ();
956 /*
957 * The Mach-concept is a little different from the traditional UNIX
958 * concept: All the work is done in threads. Threads are contained in
959 * `tasks'. Therefore, `task status' doesn't make much sense, since
960 * it's actually a `thread status'.
961 * Tasks are assigned to sets of processors, so that's where you go to
962 * get a list.
963 */
964 for (pset = 0; pset < pset_list_len; pset++)
965 {
966 if ((status = host_processor_set_priv (port_host_self,
967 pset_list[pset],
968 &port_pset_priv)) != KERN_SUCCESS)
969 {
970 ERROR ("host_processor_set_priv failed: %s\n",
971 mach_error_string (status));
972 continue;
973 }
975 if ((status = processor_set_tasks (port_pset_priv,
976 &task_list,
977 &task_list_len)) != KERN_SUCCESS)
978 {
979 ERROR ("processor_set_tasks failed: %s\n",
980 mach_error_string (status));
981 mach_port_deallocate (port_task_self, port_pset_priv);
982 continue;
983 }
985 for (task = 0; task < task_list_len; task++)
986 {
987 ps = NULL;
988 if (mach_get_task_name (task_list[task],
989 &task_pid,
990 task_name, PROCSTAT_NAME_LEN) == 0)
991 {
992 /* search for at least one match */
993 for (ps = list_head_g; ps != NULL; ps = ps->next)
994 /* FIXME: cmdline should be here instead of NULL */
995 if (ps_list_match (task_name, NULL, ps) == 1)
996 break;
997 }
999 /* Collect more detailed statistics for this process */
1000 if (ps != NULL)
1001 {
1002 task_basic_info_data_t task_basic_info;
1003 mach_msg_type_number_t task_basic_info_len;
1004 task_events_info_data_t task_events_info;
1005 mach_msg_type_number_t task_events_info_len;
1006 task_absolutetime_info_data_t task_absolutetime_info;
1007 mach_msg_type_number_t task_absolutetime_info_len;
1009 memset (&pse, '\0', sizeof (pse));
1010 pse.id = task_pid;
1012 task_basic_info_len = TASK_BASIC_INFO_COUNT;
1013 status = task_info (task_list[task],
1014 TASK_BASIC_INFO,
1015 (task_info_t) &task_basic_info,
1016 &task_basic_info_len);
1017 if (status != KERN_SUCCESS)
1018 {
1019 ERROR ("task_info failed: %s",
1020 mach_error_string (status));
1021 continue; /* with next thread_list */
1022 }
1024 task_events_info_len = TASK_EVENTS_INFO_COUNT;
1025 status = task_info (task_list[task],
1026 TASK_EVENTS_INFO,
1027 (task_info_t) &task_events_info,
1028 &task_events_info_len);
1029 if (status != KERN_SUCCESS)
1030 {
1031 ERROR ("task_info failed: %s",
1032 mach_error_string (status));
1033 continue; /* with next thread_list */
1034 }
1036 task_absolutetime_info_len = TASK_ABSOLUTETIME_INFO_COUNT;
1037 status = task_info (task_list[task],
1038 TASK_ABSOLUTETIME_INFO,
1039 (task_info_t) &task_absolutetime_info,
1040 &task_absolutetime_info_len);
1041 if (status != KERN_SUCCESS)
1042 {
1043 ERROR ("task_info failed: %s",
1044 mach_error_string (status));
1045 continue; /* with next thread_list */
1046 }
1048 pse.num_proc++;
1049 pse.vmem_rss = task_basic_info.resident_size;
1051 pse.vmem_minflt_counter = task_events_info.cow_faults;
1052 pse.vmem_majflt_counter = task_events_info.faults;
1054 pse.cpu_user_counter = task_absolutetime_info.total_user;
1055 pse.cpu_system_counter = task_absolutetime_info.total_system;
1056 }
1058 status = task_threads (task_list[task], &thread_list,
1059 &thread_list_len);
1060 if (status != KERN_SUCCESS)
1061 {
1062 /* Apple's `top' treats this case a zombie. It
1063 * makes sense to some extend: A `zombie'
1064 * thread is nonsense, since the task/process
1065 * is dead. */
1066 zombies++;
1067 DEBUG ("task_threads failed: %s",
1068 mach_error_string (status));
1069 if (task_list[task] != port_task_self)
1070 mach_port_deallocate (port_task_self,
1071 task_list[task]);
1072 continue; /* with next task_list */
1073 }
1075 for (thread = 0; thread < thread_list_len; thread++)
1076 {
1077 thread_data_len = THREAD_BASIC_INFO_COUNT;
1078 status = thread_info (thread_list[thread],
1079 THREAD_BASIC_INFO,
1080 (thread_info_t) &thread_data,
1081 &thread_data_len);
1082 if (status != KERN_SUCCESS)
1083 {
1084 ERROR ("thread_info failed: %s",
1085 mach_error_string (status));
1086 if (task_list[task] != port_task_self)
1087 mach_port_deallocate (port_task_self,
1088 thread_list[thread]);
1089 continue; /* with next thread_list */
1090 }
1092 if (ps != NULL)
1093 pse.num_lwp++;
1095 switch (thread_data.run_state)
1096 {
1097 case TH_STATE_RUNNING:
1098 running++;
1099 break;
1100 case TH_STATE_STOPPED:
1101 /* What exactly is `halted'? */
1102 case TH_STATE_HALTED:
1103 stopped++;
1104 break;
1105 case TH_STATE_WAITING:
1106 sleeping++;
1107 break;
1108 case TH_STATE_UNINTERRUPTIBLE:
1109 blocked++;
1110 break;
1111 /* There is no `zombie' case here,
1112 * since there are no zombie-threads.
1113 * There's only zombie tasks, which are
1114 * handled above. */
1115 default:
1116 WARNING ("Unknown thread status: %i",
1117 thread_data.run_state);
1118 break;
1119 } /* switch (thread_data.run_state) */
1121 if (task_list[task] != port_task_self)
1122 {
1123 status = mach_port_deallocate (port_task_self,
1124 thread_list[thread]);
1125 if (status != KERN_SUCCESS)
1126 ERROR ("mach_port_deallocate failed: %s",
1127 mach_error_string (status));
1128 }
1129 } /* for (thread_list) */
1131 if ((status = vm_deallocate (port_task_self,
1132 (vm_address_t) thread_list,
1133 thread_list_len * sizeof (thread_act_t)))
1134 != KERN_SUCCESS)
1135 {
1136 ERROR ("vm_deallocate failed: %s",
1137 mach_error_string (status));
1138 }
1139 thread_list = NULL;
1140 thread_list_len = 0;
1142 /* Only deallocate the task port, if it isn't our own.
1143 * Don't know what would happen in that case, but this
1144 * is what Apple's top does.. ;) */
1145 if (task_list[task] != port_task_self)
1146 {
1147 status = mach_port_deallocate (port_task_self,
1148 task_list[task]);
1149 if (status != KERN_SUCCESS)
1150 ERROR ("mach_port_deallocate failed: %s",
1151 mach_error_string (status));
1152 }
1154 if (ps != NULL)
1155 /* FIXME: cmdline should be here instead of NULL */
1156 ps_list_add (task_name, NULL, &pse);
1157 } /* for (task_list) */
1159 if ((status = vm_deallocate (port_task_self,
1160 (vm_address_t) task_list,
1161 task_list_len * sizeof (task_t))) != KERN_SUCCESS)
1162 {
1163 ERROR ("vm_deallocate failed: %s",
1164 mach_error_string (status));
1165 }
1166 task_list = NULL;
1167 task_list_len = 0;
1169 if ((status = mach_port_deallocate (port_task_self, port_pset_priv))
1170 != KERN_SUCCESS)
1171 {
1172 ERROR ("mach_port_deallocate failed: %s",
1173 mach_error_string (status));
1174 }
1175 } /* for (pset_list) */
1177 ps_submit_state ("running", running);
1178 ps_submit_state ("sleeping", sleeping);
1179 ps_submit_state ("zombies", zombies);
1180 ps_submit_state ("stopped", stopped);
1181 ps_submit_state ("blocked", blocked);
1183 for (ps = list_head_g; ps != NULL; ps = ps->next)
1184 ps_submit_proc_list (ps);
1185 /* #endif HAVE_THREAD_INFO */
1187 #elif KERNEL_LINUX
1188 int running = 0;
1189 int sleeping = 0;
1190 int zombies = 0;
1191 int stopped = 0;
1192 int paging = 0;
1193 int blocked = 0;
1195 struct dirent *ent;
1196 DIR *proc;
1197 int pid;
1199 char cmdline[ARG_MAX];
1201 int status;
1202 procstat_t ps;
1203 procstat_entry_t pse;
1204 char state;
1206 procstat_t *ps_ptr;
1208 running = sleeping = zombies = stopped = paging = blocked = 0;
1209 ps_list_reset ();
1211 if ((proc = opendir ("/proc")) == NULL)
1212 {
1213 char errbuf[1024];
1214 ERROR ("Cannot open `/proc': %s",
1215 sstrerror (errno, errbuf, sizeof (errbuf)));
1216 return (-1);
1217 }
1219 while ((ent = readdir (proc)) != NULL)
1220 {
1221 if (!isdigit (ent->d_name[0]))
1222 continue;
1224 if ((pid = atoi (ent->d_name)) < 1)
1225 continue;
1227 status = ps_read_process (pid, &ps, &state);
1228 if (status != 0)
1229 {
1230 DEBUG ("ps_read_process failed: %i", status);
1231 continue;
1232 }
1234 pse.id = pid;
1235 pse.age = 0;
1237 pse.num_proc = ps.num_proc;
1238 pse.num_lwp = ps.num_lwp;
1239 pse.vmem_size = ps.vmem_size;
1240 pse.vmem_rss = ps.vmem_rss;
1241 pse.stack_size = ps.stack_size;
1243 pse.vmem_minflt = 0;
1244 pse.vmem_minflt_counter = ps.vmem_minflt_counter;
1245 pse.vmem_majflt = 0;
1246 pse.vmem_majflt_counter = ps.vmem_majflt_counter;
1248 pse.cpu_user = 0;
1249 pse.cpu_user_counter = ps.cpu_user_counter;
1250 pse.cpu_system = 0;
1251 pse.cpu_system_counter = ps.cpu_system_counter;
1253 switch (state)
1254 {
1255 case 'R': running++; break;
1256 case 'S': sleeping++; break;
1257 case 'D': blocked++; break;
1258 case 'Z': zombies++; break;
1259 case 'T': stopped++; break;
1260 case 'W': paging++; break;
1261 }
1263 ps_list_add (ps.name,
1264 ps_get_cmdline (pid, ps.name, cmdline, sizeof (cmdline)),
1265 &pse);
1266 }
1268 closedir (proc);
1270 ps_submit_state ("running", running);
1271 ps_submit_state ("sleeping", sleeping);
1272 ps_submit_state ("zombies", zombies);
1273 ps_submit_state ("stopped", stopped);
1274 ps_submit_state ("paging", paging);
1275 ps_submit_state ("blocked", blocked);
1277 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1278 ps_submit_proc_list (ps_ptr);
1279 /* #endif KERNEL_LINUX */
1281 #elif HAVE_LIBKVM_GETPROCS
1282 int running = 0;
1283 int sleeping = 0;
1284 int zombies = 0;
1285 int stopped = 0;
1286 int blocked = 0;
1287 int idle = 0;
1288 int wait = 0;
1290 kvm_t *kd;
1291 char errbuf[1024];
1292 char cmdline[ARG_MAX];
1293 char *cmdline_ptr;
1294 struct kinfo_proc *procs; /* array of processes */
1295 char **argv;
1296 int count; /* returns number of processes */
1297 int i;
1299 procstat_t *ps_ptr;
1300 procstat_entry_t pse;
1302 ps_list_reset ();
1304 /* Open the kvm interface, get a descriptor */
1305 kd = kvm_open (NULL, NULL, NULL, 0, errbuf);
1306 if (kd == NULL)
1307 {
1308 ERROR ("processes plugin: Cannot open kvm interface: %s",
1309 errbuf);
1310 return (0);
1311 }
1313 /* Get the list of processes. */
1314 procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, &count);
1315 if (procs == NULL)
1316 {
1317 kvm_close (kd);
1318 ERROR ("processes plugin: Cannot get kvm processes list: %s",
1319 kvm_geterr(kd));
1320 return (0);
1321 }
1323 /* Iterate through the processes in kinfo_proc */
1324 for (i = 0; i < count; i++)
1325 {
1326 /* retrieve the arguments */
1327 cmdline[0] = 0;
1328 cmdline_ptr = NULL;
1330 argv = kvm_getargv (kd, (const struct kinfo_proc *) &(procs[i]), 0);
1331 if (argv != NULL)
1332 {
1333 int status;
1334 int argc;
1336 argc = 0;
1337 while (argv[argc] != NULL)
1338 argc++;
1340 status = strjoin (cmdline, sizeof (cmdline),
1341 argv, argc, " ");
1343 if (status < 0)
1344 {
1345 WARNING ("processes plugin: Command line did "
1346 "not fit into buffer.");
1347 }
1348 else
1349 {
1350 cmdline_ptr = &cmdline[0];
1351 }
1352 }
1354 pse.id = procs[i].ki_pid;
1355 pse.age = 0;
1357 pse.num_proc = 1;
1358 pse.num_lwp = procs[i].ki_numthreads;
1360 pse.vmem_rss = procs[i].ki_rssize * getpagesize();
1361 pse.vmem_minflt = 0;
1362 pse.vmem_minflt_counter = procs[i].ki_rusage.ru_minflt;
1363 pse.vmem_majflt = 0;
1364 pse.vmem_majflt_counter = procs[i].ki_rusage.ru_majflt;
1366 pse.cpu_user = 0;
1367 pse.cpu_user_counter = procs[i].ki_rusage.ru_utime.tv_sec
1368 * 1000
1369 + procs[i].ki_rusage.ru_utime.tv_usec;
1370 pse.cpu_system = 0;
1371 pse.cpu_system_counter = procs[i].ki_rusage.ru_stime.tv_sec
1372 * 1000
1373 + procs[i].ki_rusage.ru_stime.tv_usec;
1375 switch (procs[i].ki_stat)
1376 {
1377 case SSTOP: stopped++; break;
1378 case SSLEEP: sleeping++; break;
1379 case SRUN: running++; break;
1380 case SIDL: idle++; break;
1381 case SWAIT: wait++; break;
1382 case SLOCK: blocked++; break;
1383 case SZOMB: zombies++; break;
1384 }
1386 ps_list_add (procs[i].ki_comm, cmdline_ptr, &pse);
1387 }
1389 kvm_close(kd);
1391 ps_submit_state ("running", running);
1392 ps_submit_state ("sleeping", sleeping);
1393 ps_submit_state ("zombies", zombies);
1394 ps_submit_state ("stopped", stopped);
1395 ps_submit_state ("blocked", blocked);
1396 ps_submit_state ("idle", idle);
1397 ps_submit_state ("wait", wait);
1399 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1400 ps_submit_proc_list (ps_ptr);
1401 #endif /* HAVE_LIBKVM_GETPROCS */
1403 return (0);
1404 } /* int ps_read */
1406 void module_register (void)
1407 {
1408 plugin_register_config ("processes", ps_config,
1409 config_keys, config_keys_num);
1410 plugin_register_init ("processes", ps_init);
1411 plugin_register_read ("processes", ps_read);
1412 } /* void module_register */