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 #define BUFSIZE 256
107 static const char *config_keys[] =
108 {
109 "Process",
110 "ProcessMatch"
111 };
112 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
114 typedef struct procstat_entry_s
115 {
116 unsigned long id;
117 unsigned long age;
119 unsigned long num_proc;
120 unsigned long num_lwp;
121 unsigned long vmem_rss;
123 unsigned long vmem_minflt;
124 unsigned long vmem_majflt;
125 unsigned long vmem_minflt_counter;
126 unsigned long vmem_majflt_counter;
128 unsigned long cpu_user;
129 unsigned long cpu_system;
130 unsigned long cpu_user_counter;
131 unsigned long cpu_system_counter;
133 struct procstat_entry_s *next;
134 } procstat_entry_t;
136 #define PROCSTAT_NAME_LEN 256
137 typedef struct procstat
138 {
139 char name[PROCSTAT_NAME_LEN];
140 #if HAVE_REGEX_H
141 regex_t *re;
142 #endif
144 unsigned long num_proc;
145 unsigned long num_lwp;
146 unsigned long vmem_rss;
148 unsigned long vmem_minflt_counter;
149 unsigned long vmem_majflt_counter;
151 unsigned long cpu_user_counter;
152 unsigned long cpu_system_counter;
154 struct procstat *next;
155 struct procstat_entry_s *instances;
156 } procstat_t;
158 static procstat_t *list_head_g = NULL;
160 #if HAVE_THREAD_INFO
161 static mach_port_t port_host_self;
162 static mach_port_t port_task_self;
164 static processor_set_name_array_t pset_list;
165 static mach_msg_type_number_t pset_list_len;
166 /* #endif HAVE_THREAD_INFO */
168 #elif KERNEL_LINUX
169 static long pagesize_g;
170 /* #endif KERNEL_LINUX */
172 #elif HAVE_LIBKVM_GETPROCS
173 /* no global variables */
174 #endif /* HAVE_LIBKVM_GETPROCS */
176 /* put name of process from config to list_head_g tree
177 list_head_g is a list of 'procstat_t' structs with
178 processes names we want to watch */
179 static void ps_list_register (const char *name, const char *regexp)
180 {
181 procstat_t *new;
182 procstat_t *ptr;
183 int status;
185 new = (procstat_t *) malloc (sizeof (procstat_t));
186 if (new == NULL)
187 {
188 ERROR ("processes plugin: ps_list_register: malloc failed.");
189 return;
190 }
191 memset (new, 0, sizeof (procstat_t));
192 sstrncpy (new->name, name, sizeof (new->name));
194 #if HAVE_REGEX_H
195 if (regexp != NULL)
196 {
197 DEBUG ("ProcessMatch: adding \"%s\" as criteria to process %s.", regexp, name);
198 new->re = (regex_t *) malloc (sizeof (regex_t));
199 if (new->re == NULL)
200 {
201 ERROR ("processes plugin: ps_list_register: malloc failed.");
202 sfree (new);
203 return;
204 }
206 status = regcomp (new->re, regexp, REG_EXTENDED | REG_NOSUB);
207 if (status != 0)
208 {
209 DEBUG ("ProcessMatch: compiling the regular expression \"%s\" failed.", regexp);
210 sfree(new->re);
211 return;
212 }
213 }
214 #else
215 if (regexp != NULL)
216 {
217 ERROR ("processes plugin: ps_list_register: "
218 "Regular expression \"%s\" found in config "
219 "file, but support for regular expressions "
220 "has been dispabled at compile time.",
221 regexp);
222 sfree (new);
223 return;
224 }
225 #endif
227 for (ptr = list_head_g; ptr != NULL; ptr = ptr->next)
228 {
229 if (strcmp (ptr->name, name) == 0)
230 {
231 WARNING ("processes plugin: You have configured more "
232 "than one `Process' or "
233 "`ProcessMatch' with the same name. "
234 "All but the first setting will be "
235 "ignored.");
236 sfree (new->re);
237 sfree (new);
238 return;
239 }
241 if (ptr->next == NULL)
242 break;
243 }
245 if (ptr == NULL)
246 list_head_g = new;
247 else
248 ptr->next = new;
249 } /* void ps_list_register */
251 /* try to match name against entry, returns 1 if success */
252 static int ps_list_match (const char *name, const char *cmdline, procstat_t *ps)
253 {
254 #if HAVE_REGEX_H
255 if (ps->re != NULL)
256 {
257 int status;
258 const char *str;
260 str = cmdline;
261 if ((str == NULL) || (str[0] == 0))
262 str = name;
264 assert (str != NULL);
266 status = regexec (ps->re, str,
267 /* nmatch = */ 0,
268 /* pmatch = */ NULL,
269 /* eflags = */ 0);
270 if (status == 0)
271 return (1);
272 }
273 else
274 #endif
275 if (strcmp (ps->name, name) == 0)
276 return (1);
278 return (0);
279 } /* int ps_list_match */
281 /* add process entry to 'instances' of process 'name' (or refresh it) */
282 static void ps_list_add (const char *name, const char *cmdline, procstat_entry_t *entry)
283 {
284 procstat_t *ps;
285 procstat_entry_t *pse;
287 if (entry->id == 0)
288 return;
290 for (ps = list_head_g; ps != NULL; ps = ps->next)
291 {
292 if ((ps_list_match (name, cmdline, ps)) == 0)
293 continue;
295 for (pse = ps->instances; pse != NULL; pse = pse->next)
296 if ((pse->id == entry->id) || (pse->next == NULL))
297 break;
299 if ((pse == NULL) || (pse->id != entry->id))
300 {
301 procstat_entry_t *new;
303 new = (procstat_entry_t *) malloc (sizeof (procstat_entry_t));
304 if (new == NULL)
305 return;
306 memset (new, 0, sizeof (procstat_entry_t));
307 new->id = entry->id;
309 if (pse == NULL)
310 ps->instances = new;
311 else
312 pse->next = new;
314 pse = new;
315 }
317 pse->age = 0;
318 pse->num_proc = entry->num_proc;
319 pse->num_lwp = entry->num_lwp;
320 pse->vmem_rss = entry->vmem_rss;
322 ps->num_proc += pse->num_proc;
323 ps->num_lwp += pse->num_lwp;
324 ps->vmem_rss += pse->vmem_rss;
326 if ((entry->vmem_minflt_counter == 0)
327 && (entry->vmem_majflt_counter == 0))
328 {
329 pse->vmem_minflt_counter += entry->vmem_minflt;
330 pse->vmem_minflt = entry->vmem_minflt;
332 pse->vmem_majflt_counter += entry->vmem_majflt;
333 pse->vmem_majflt = entry->vmem_majflt;
334 }
335 else
336 {
337 if (entry->vmem_minflt_counter < pse->vmem_minflt_counter)
338 {
339 pse->vmem_minflt = entry->vmem_minflt_counter
340 + (ULONG_MAX - pse->vmem_minflt_counter);
341 }
342 else
343 {
344 pse->vmem_minflt = entry->vmem_minflt_counter - pse->vmem_minflt_counter;
345 }
346 pse->vmem_minflt_counter = entry->vmem_minflt_counter;
348 if (entry->vmem_majflt_counter < pse->vmem_majflt_counter)
349 {
350 pse->vmem_majflt = entry->vmem_majflt_counter
351 + (ULONG_MAX - pse->vmem_majflt_counter);
352 }
353 else
354 {
355 pse->vmem_majflt = entry->vmem_majflt_counter - pse->vmem_majflt_counter;
356 }
357 pse->vmem_majflt_counter = entry->vmem_majflt_counter;
358 }
360 ps->vmem_minflt_counter += pse->vmem_minflt;
361 ps->vmem_majflt_counter += pse->vmem_majflt;
363 if ((entry->cpu_user_counter == 0)
364 && (entry->cpu_system_counter == 0))
365 {
366 pse->cpu_user_counter += entry->cpu_user;
367 pse->cpu_user = entry->cpu_user;
369 pse->cpu_system_counter += entry->cpu_system;
370 pse->cpu_system = entry->cpu_system;
371 }
372 else
373 {
374 if (entry->cpu_user_counter < pse->cpu_user_counter)
375 {
376 pse->cpu_user = entry->cpu_user_counter
377 + (ULONG_MAX - pse->cpu_user_counter);
378 }
379 else
380 {
381 pse->cpu_user = entry->cpu_user_counter - pse->cpu_user_counter;
382 }
383 pse->cpu_user_counter = entry->cpu_user_counter;
385 if (entry->cpu_system_counter < pse->cpu_system_counter)
386 {
387 pse->cpu_system = entry->cpu_system_counter
388 + (ULONG_MAX - pse->cpu_system_counter);
389 }
390 else
391 {
392 pse->cpu_system = entry->cpu_system_counter - pse->cpu_system_counter;
393 }
394 pse->cpu_system_counter = entry->cpu_system_counter;
395 }
397 ps->cpu_user_counter += pse->cpu_user;
398 ps->cpu_system_counter += pse->cpu_system;
399 }
400 }
402 /* remove old entries from instances of processes in list_head_g */
403 static void ps_list_reset (void)
404 {
405 procstat_t *ps;
406 procstat_entry_t *pse;
407 procstat_entry_t *pse_prev;
409 for (ps = list_head_g; ps != NULL; ps = ps->next)
410 {
411 ps->num_proc = 0;
412 ps->num_lwp = 0;
413 ps->vmem_rss = 0;
415 pse_prev = NULL;
416 pse = ps->instances;
417 while (pse != NULL)
418 {
419 if (pse->age > 10)
420 {
421 DEBUG ("Removing this procstat entry cause it's too old: "
422 "id = %lu; name = %s;",
423 pse->id, ps->name);
425 if (pse_prev == NULL)
426 {
427 ps->instances = pse->next;
428 free (pse);
429 pse = ps->instances;
430 }
431 else
432 {
433 pse_prev->next = pse->next;
434 free (pse);
435 pse = pse_prev->next;
436 }
437 }
438 else
439 {
440 pse->age++;
441 pse_prev = pse;
442 pse = pse->next;
443 }
444 } /* while (pse != NULL) */
445 } /* for (ps = list_head_g; ps != NULL; ps = ps->next) */
446 }
448 /* put all pre-defined 'Process' names from config to list_head_g tree */
449 static int ps_config (const char *key, const char *value)
450 {
451 if (strcasecmp (key, "Process") == 0)
452 {
453 ps_list_register (value, NULL);
454 }
455 else if (strcasecmp (key, "ProcessMatch") == 0)
456 {
457 char *new_val;
458 char *fields[3];
459 int fields_num;
461 new_val = strdup (value);
462 if (new_val == NULL) {
463 ERROR ("processes plugin: strdup failed when processing "
464 "`ProcessMatch %s'.", value);
465 return (1);
466 }
468 fields_num = strsplit (new_val, fields,
469 STATIC_ARRAY_SIZE (fields));
470 if (fields_num != 2)
471 {
472 ERROR ("processes plugin: `ProcessMatch' needs exactly "
473 "two string arguments.");
474 sfree (new_val);
475 return (1);
476 }
477 ps_list_register (fields[0], fields[1]);
478 sfree (new_val);
479 }
480 else
481 {
482 ERROR ("processes plugin: The `%s' configuration option is not "
483 "understood and will be ignored.", key);
484 return (-1);
485 }
487 return (0);
488 }
490 static int ps_init (void)
491 {
492 #if HAVE_THREAD_INFO
493 kern_return_t status;
495 port_host_self = mach_host_self ();
496 port_task_self = mach_task_self ();
498 if (pset_list != NULL)
499 {
500 vm_deallocate (port_task_self,
501 (vm_address_t) pset_list,
502 pset_list_len * sizeof (processor_set_t));
503 pset_list = NULL;
504 pset_list_len = 0;
505 }
507 if ((status = host_processor_sets (port_host_self,
508 &pset_list,
509 &pset_list_len)) != KERN_SUCCESS)
510 {
511 ERROR ("host_processor_sets failed: %s\n",
512 mach_error_string (status));
513 pset_list = NULL;
514 pset_list_len = 0;
515 return (-1);
516 }
517 /* #endif HAVE_THREAD_INFO */
519 #elif KERNEL_LINUX
520 pagesize_g = sysconf(_SC_PAGESIZE);
521 DEBUG ("pagesize_g = %li; CONFIG_HZ = %i;",
522 pagesize_g, CONFIG_HZ);
523 /* #endif KERNEL_LINUX */
525 #elif HAVE_LIBKVM_GETPROCS
526 /* no initialization */
527 #endif /* HAVE_LIBKVM_GETPROCS */
529 return (0);
530 } /* int ps_init */
532 /* submit global state (e.g.: qty of zombies, running, etc..) */
533 static void ps_submit_state (const char *state, double value)
534 {
535 value_t values[1];
536 value_list_t vl = VALUE_LIST_INIT;
538 values[0].gauge = value;
540 vl.values = values;
541 vl.values_len = 1;
542 vl.time = time (NULL);
543 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
544 sstrncpy (vl.plugin, "processes", sizeof (vl.plugin));
545 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
546 sstrncpy (vl.type, "ps_state", sizeof (vl.type));
547 sstrncpy (vl.type_instance, state, sizeof (vl.type_instance));
549 plugin_dispatch_values (&vl);
550 }
552 /* submit info about specific process (e.g.: memory taken, cpu usage, etc..) */
553 static void ps_submit_proc_list (procstat_t *ps)
554 {
555 value_t values[2];
556 value_list_t vl = VALUE_LIST_INIT;
558 vl.values = values;
559 vl.values_len = 2;
560 vl.time = time (NULL);
561 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
562 sstrncpy (vl.plugin, "processes", sizeof (vl.plugin));
563 sstrncpy (vl.plugin_instance, ps->name, sizeof (vl.plugin_instance));
565 sstrncpy (vl.type, "ps_rss", sizeof (vl.type));
566 vl.values[0].gauge = ps->vmem_rss;
567 vl.values_len = 1;
568 plugin_dispatch_values (&vl);
570 sstrncpy (vl.type, "ps_cputime", sizeof (vl.type));
571 vl.values[0].counter = ps->cpu_user_counter;
572 vl.values[1].counter = ps->cpu_system_counter;
573 vl.values_len = 2;
574 plugin_dispatch_values (&vl);
576 sstrncpy (vl.type, "ps_count", sizeof (vl.type));
577 vl.values[0].gauge = ps->num_proc;
578 vl.values[1].gauge = ps->num_lwp;
579 vl.values_len = 2;
580 plugin_dispatch_values (&vl);
582 sstrncpy (vl.type, "ps_pagefaults", sizeof (vl.type));
583 vl.values[0].counter = ps->vmem_minflt_counter;
584 vl.values[1].counter = ps->vmem_majflt_counter;
585 vl.values_len = 2;
586 plugin_dispatch_values (&vl);
588 DEBUG ("name = %s; num_proc = %lu; num_lwp = %lu; vmem_rss = %lu; "
589 "vmem_minflt_counter = %lu; vmem_majflt_counter = %lu; "
590 "cpu_user_counter = %lu; cpu_system_counter = %lu;",
591 ps->name, ps->num_proc, ps->num_lwp, ps->vmem_rss,
592 ps->vmem_minflt_counter, ps->vmem_majflt_counter,
593 ps->cpu_user_counter, ps->cpu_system_counter);
594 } /* void ps_submit_proc_list */
596 /* ------- additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
597 #if KERNEL_LINUX
598 static int *ps_read_tasks (int pid)
599 {
600 int *list = NULL;
601 int list_size = 1; /* size of allocated space, in elements */
602 int list_len = 0; /* number of currently used elements */
604 char dirname[64];
605 DIR *dh;
606 struct dirent *ent;
608 ssnprintf (dirname, sizeof (dirname), "/proc/%i/task", pid);
610 if ((dh = opendir (dirname)) == NULL)
611 {
612 DEBUG ("Failed to open directory `%s'", dirname);
613 return (NULL);
614 }
616 while ((ent = readdir (dh)) != NULL)
617 {
618 if (!isdigit (ent->d_name[0]))
619 continue;
621 if ((list_len + 1) >= list_size)
622 {
623 int *new_ptr;
624 int new_size = 2 * list_size;
625 /* Comes in sizes: 2, 4, 8, 16, ... */
627 new_ptr = (int *) realloc (list, (size_t) (sizeof (int) * new_size));
628 if (new_ptr == NULL)
629 {
630 if (list != NULL)
631 free (list);
632 ERROR ("processes plugin: "
633 "Failed to allocate more memory.");
634 return (NULL);
635 }
637 list = new_ptr;
638 list_size = new_size;
640 memset (list + list_len, 0, sizeof (int) * (list_size - list_len));
641 }
643 list[list_len] = atoi (ent->d_name);
644 if (list[list_len] != 0)
645 list_len++;
646 }
648 closedir (dh);
650 if (list_len == 0)
651 return (NULL);
653 assert (list_len < list_size);
654 assert (list[list_len] == 0);
656 return (list);
657 } /* int *ps_read_tasks */
659 int ps_read_process (int pid, procstat_t *ps, char *state)
660 {
661 char filename[64];
662 char buffer[1024];
664 char *fields[64];
665 char fields_len;
667 int *tasks;
668 int i;
670 int ppid;
671 int name_len;
673 long long unsigned cpu_user_counter;
674 long long unsigned cpu_system_counter;
675 long long unsigned vmem_rss;
677 memset (ps, 0, sizeof (procstat_t));
679 ssnprintf (filename, sizeof (filename), "/proc/%i/stat", pid);
681 i = read_file_contents (filename, buffer, sizeof(buffer) - 1);
682 if (i <= 0)
683 return (-1);
684 buffer[i] = 0;
686 fields_len = strsplit (buffer, fields, 64);
687 if (fields_len < 24)
688 {
689 DEBUG ("processes plugin: ps_read_process (pid = %i):"
690 " `%s' has only %i fields..",
691 (int) pid, filename, fields_len);
692 return (-1);
693 }
695 /* copy the name, strip brackets in the process */
696 name_len = strlen (fields[1]) - 2;
697 if ((fields[1][0] != '(') || (fields[1][name_len + 1] != ')'))
698 {
699 DEBUG ("No brackets found in process name: `%s'", fields[1]);
700 return (-1);
701 }
702 fields[1] = fields[1] + 1;
703 fields[1][name_len] = '\0';
704 strncpy (ps->name, fields[1], PROCSTAT_NAME_LEN);
706 ppid = atoi (fields[3]);
708 *state = fields[2][0];
710 if (*state == 'Z')
711 {
712 ps->num_lwp = 0;
713 ps->num_proc = 0;
714 }
715 else if ((tasks = ps_read_tasks (pid)) == NULL)
716 {
717 /* Kernel 2.4 or so */
718 ps->num_lwp = 1;
719 ps->num_proc = 1;
720 }
721 else
722 {
723 ps->num_lwp = 0;
724 ps->num_proc = 1;
725 for (i = 0; tasks[i] != 0; i++)
726 ps->num_lwp++;
728 free (tasks);
729 tasks = NULL;
730 }
732 /* Leave the rest at zero if this is only a zombi */
733 if (ps->num_proc == 0)
734 {
735 DEBUG ("processes plugin: This is only a zombi: pid = %i; "
736 "name = %s;", pid, ps->name);
737 return (0);
738 }
740 cpu_user_counter = atoll (fields[13]);
741 cpu_system_counter = atoll (fields[14]);
742 vmem_rss = atoll (fields[23]);
743 ps->vmem_minflt_counter = atol (fields[9]);
744 ps->vmem_majflt_counter = atol (fields[11]);
746 /* Convert jiffies to useconds */
747 cpu_user_counter = cpu_user_counter * 1000000 / CONFIG_HZ;
748 cpu_system_counter = cpu_system_counter * 1000000 / CONFIG_HZ;
749 vmem_rss = vmem_rss * pagesize_g;
751 ps->cpu_user_counter = (unsigned long) cpu_user_counter;
752 ps->cpu_system_counter = (unsigned long) cpu_system_counter;
753 ps->vmem_rss = (unsigned long) vmem_rss;
755 /* success */
756 return (0);
757 } /* int ps_read_process (...) */
759 static char *ps_get_cmdline (pid_t pid, char *name, char *buf, size_t buf_len)
760 {
761 char *buf_ptr;
762 size_t len;
764 char file[PATH_MAX];
765 int fd;
767 size_t n;
769 if ((pid < 1) || (NULL == buf) || (buf_len < 2))
770 return NULL;
772 ssnprintf (file, sizeof (file), "/proc/%u/cmdline", pid);
774 fd = open (file, O_RDONLY);
775 if (fd < 0) {
776 char errbuf[4096];
777 WARNING ("processes plugin: Failed to open `%s': %s.", file,
778 sstrerror (errno, errbuf, sizeof (errbuf)));
779 return NULL;
780 }
782 buf_ptr = buf;
783 len = buf_len;
785 n = 0;
787 while (42) {
788 size_t status;
790 status = read (fd, (void *)buf_ptr, len);
792 if (status < 0) {
793 char errbuf[4096];
795 if ((EAGAIN == errno) || (EINTR == errno))
796 continue;
798 WARNING ("processes plugin: Failed to read from `%s': %s.", file,
799 sstrerror (errno, errbuf, sizeof (errbuf)));
800 close (fd);
801 return NULL;
802 }
804 n += status;
806 if (status == 0)
807 break;
809 buf_ptr += status;
810 len -= status;
812 if (len <= 0)
813 break;
814 }
816 close (fd);
818 if (0 == n) {
819 /* cmdline not available; e.g. kernel thread, zombie */
820 if (NULL == name)
821 return NULL;
823 ssnprintf (buf, buf_len, "[%s]", name);
824 return buf;
825 }
827 assert (n <= buf_len);
829 if (n == buf_len)
830 --n;
831 buf[n] = '\0';
833 --n;
834 /* remove trailing whitespace */
835 while ((n > 0) && (isspace (buf[n]) || ('\0' == buf[n]))) {
836 buf[n] = '\0';
837 --n;
838 }
840 /* arguments are separated by '\0' in /proc/<pid>/cmdline */
841 while (n > 0) {
842 if ('\0' == buf[n])
843 buf[n] = ' ';
844 --n;
845 }
846 return buf;
847 } /* char *ps_get_cmdline (...) */
848 #endif /* KERNEL_LINUX */
850 #if HAVE_THREAD_INFO
851 static int mach_get_task_name (task_t t, int *pid, char *name, size_t name_max_len)
852 {
853 int mib[4];
855 struct kinfo_proc kp;
856 size_t kp_size;
858 mib[0] = CTL_KERN;
859 mib[1] = KERN_PROC;
860 mib[2] = KERN_PROC_PID;
862 if (pid_for_task (t, pid) != KERN_SUCCESS)
863 return (-1);
864 mib[3] = *pid;
866 kp_size = sizeof (kp);
867 if (sysctl (mib, 4, &kp, &kp_size, NULL, 0) != 0)
868 return (-1);
870 if (name_max_len > (MAXCOMLEN + 1))
871 name_max_len = MAXCOMLEN + 1;
873 strncpy (name, kp.kp_proc.p_comm, name_max_len - 1);
874 name[name_max_len - 1] = '\0';
876 DEBUG ("pid = %i; name = %s;", *pid, name);
878 /* We don't do the special handling for `p_comm == "LaunchCFMApp"' as
879 * `top' does it, because it is a lot of work and only used when
880 * debugging. -octo */
882 return (0);
883 }
884 #endif /* HAVE_THREAD_INFO */
885 /* ------- end of additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
887 /* do actual readings from kernel */
888 static int ps_read (void)
889 {
890 #if HAVE_THREAD_INFO
891 kern_return_t status;
893 int pset;
894 processor_set_t port_pset_priv;
896 int task;
897 task_array_t task_list;
898 mach_msg_type_number_t task_list_len;
900 int task_pid;
901 char task_name[MAXCOMLEN + 1];
903 int thread;
904 thread_act_array_t thread_list;
905 mach_msg_type_number_t thread_list_len;
906 thread_basic_info_data_t thread_data;
907 mach_msg_type_number_t thread_data_len;
909 int running = 0;
910 int sleeping = 0;
911 int zombies = 0;
912 int stopped = 0;
913 int blocked = 0;
915 procstat_t *ps;
916 procstat_entry_t pse;
918 ps_list_reset ();
920 /*
921 * The Mach-concept is a little different from the traditional UNIX
922 * concept: All the work is done in threads. Threads are contained in
923 * `tasks'. Therefore, `task status' doesn't make much sense, since
924 * it's actually a `thread status'.
925 * Tasks are assigned to sets of processors, so that's where you go to
926 * get a list.
927 */
928 for (pset = 0; pset < pset_list_len; pset++)
929 {
930 if ((status = host_processor_set_priv (port_host_self,
931 pset_list[pset],
932 &port_pset_priv)) != KERN_SUCCESS)
933 {
934 ERROR ("host_processor_set_priv failed: %s\n",
935 mach_error_string (status));
936 continue;
937 }
939 if ((status = processor_set_tasks (port_pset_priv,
940 &task_list,
941 &task_list_len)) != KERN_SUCCESS)
942 {
943 ERROR ("processor_set_tasks failed: %s\n",
944 mach_error_string (status));
945 mach_port_deallocate (port_task_self, port_pset_priv);
946 continue;
947 }
949 for (task = 0; task < task_list_len; task++)
950 {
951 ps = NULL;
952 if (mach_get_task_name (task_list[task],
953 &task_pid,
954 task_name, PROCSTAT_NAME_LEN) == 0)
955 {
956 /* search for at least one match */
957 for (ps = list_head_g; ps != NULL; ps = ps->next)
958 /* FIXME: cmdline should be here instead of NULL */
959 if (ps_list_match (task_name, NULL, ps) == 1)
960 break;
961 }
963 /* Collect more detailed statistics for this process */
964 if (ps != NULL)
965 {
966 task_basic_info_data_t task_basic_info;
967 mach_msg_type_number_t task_basic_info_len;
968 task_events_info_data_t task_events_info;
969 mach_msg_type_number_t task_events_info_len;
970 task_absolutetime_info_data_t task_absolutetime_info;
971 mach_msg_type_number_t task_absolutetime_info_len;
973 memset (&pse, '\0', sizeof (pse));
974 pse.id = task_pid;
976 task_basic_info_len = TASK_BASIC_INFO_COUNT;
977 status = task_info (task_list[task],
978 TASK_BASIC_INFO,
979 (task_info_t) &task_basic_info,
980 &task_basic_info_len);
981 if (status != KERN_SUCCESS)
982 {
983 ERROR ("task_info failed: %s",
984 mach_error_string (status));
985 continue; /* with next thread_list */
986 }
988 task_events_info_len = TASK_EVENTS_INFO_COUNT;
989 status = task_info (task_list[task],
990 TASK_EVENTS_INFO,
991 (task_info_t) &task_events_info,
992 &task_events_info_len);
993 if (status != KERN_SUCCESS)
994 {
995 ERROR ("task_info failed: %s",
996 mach_error_string (status));
997 continue; /* with next thread_list */
998 }
1000 task_absolutetime_info_len = TASK_ABSOLUTETIME_INFO_COUNT;
1001 status = task_info (task_list[task],
1002 TASK_ABSOLUTETIME_INFO,
1003 (task_info_t) &task_absolutetime_info,
1004 &task_absolutetime_info_len);
1005 if (status != KERN_SUCCESS)
1006 {
1007 ERROR ("task_info failed: %s",
1008 mach_error_string (status));
1009 continue; /* with next thread_list */
1010 }
1012 pse.num_proc++;
1013 pse.vmem_rss = task_basic_info.resident_size;
1015 pse.vmem_minflt_counter = task_events_info.cow_faults;
1016 pse.vmem_majflt_counter = task_events_info.faults;
1018 pse.cpu_user_counter = task_absolutetime_info.total_user;
1019 pse.cpu_system_counter = task_absolutetime_info.total_system;
1020 }
1022 status = task_threads (task_list[task], &thread_list,
1023 &thread_list_len);
1024 if (status != KERN_SUCCESS)
1025 {
1026 /* Apple's `top' treats this case a zombie. It
1027 * makes sense to some extend: A `zombie'
1028 * thread is nonsense, since the task/process
1029 * is dead. */
1030 zombies++;
1031 DEBUG ("task_threads failed: %s",
1032 mach_error_string (status));
1033 if (task_list[task] != port_task_self)
1034 mach_port_deallocate (port_task_self,
1035 task_list[task]);
1036 continue; /* with next task_list */
1037 }
1039 for (thread = 0; thread < thread_list_len; thread++)
1040 {
1041 thread_data_len = THREAD_BASIC_INFO_COUNT;
1042 status = thread_info (thread_list[thread],
1043 THREAD_BASIC_INFO,
1044 (thread_info_t) &thread_data,
1045 &thread_data_len);
1046 if (status != KERN_SUCCESS)
1047 {
1048 ERROR ("thread_info failed: %s",
1049 mach_error_string (status));
1050 if (task_list[task] != port_task_self)
1051 mach_port_deallocate (port_task_self,
1052 thread_list[thread]);
1053 continue; /* with next thread_list */
1054 }
1056 if (ps != NULL)
1057 pse.num_lwp++;
1059 switch (thread_data.run_state)
1060 {
1061 case TH_STATE_RUNNING:
1062 running++;
1063 break;
1064 case TH_STATE_STOPPED:
1065 /* What exactly is `halted'? */
1066 case TH_STATE_HALTED:
1067 stopped++;
1068 break;
1069 case TH_STATE_WAITING:
1070 sleeping++;
1071 break;
1072 case TH_STATE_UNINTERRUPTIBLE:
1073 blocked++;
1074 break;
1075 /* There is no `zombie' case here,
1076 * since there are no zombie-threads.
1077 * There's only zombie tasks, which are
1078 * handled above. */
1079 default:
1080 WARNING ("Unknown thread status: %i",
1081 thread_data.run_state);
1082 break;
1083 } /* switch (thread_data.run_state) */
1085 if (task_list[task] != port_task_self)
1086 {
1087 status = mach_port_deallocate (port_task_self,
1088 thread_list[thread]);
1089 if (status != KERN_SUCCESS)
1090 ERROR ("mach_port_deallocate failed: %s",
1091 mach_error_string (status));
1092 }
1093 } /* for (thread_list) */
1095 if ((status = vm_deallocate (port_task_self,
1096 (vm_address_t) thread_list,
1097 thread_list_len * sizeof (thread_act_t)))
1098 != KERN_SUCCESS)
1099 {
1100 ERROR ("vm_deallocate failed: %s",
1101 mach_error_string (status));
1102 }
1103 thread_list = NULL;
1104 thread_list_len = 0;
1106 /* Only deallocate the task port, if it isn't our own.
1107 * Don't know what would happen in that case, but this
1108 * is what Apple's top does.. ;) */
1109 if (task_list[task] != port_task_self)
1110 {
1111 status = mach_port_deallocate (port_task_self,
1112 task_list[task]);
1113 if (status != KERN_SUCCESS)
1114 ERROR ("mach_port_deallocate failed: %s",
1115 mach_error_string (status));
1116 }
1118 if (ps != NULL)
1119 /* FIXME: cmdline should be here instead of NULL */
1120 ps_list_add (task_name, NULL, &pse);
1121 } /* for (task_list) */
1123 if ((status = vm_deallocate (port_task_self,
1124 (vm_address_t) task_list,
1125 task_list_len * sizeof (task_t))) != KERN_SUCCESS)
1126 {
1127 ERROR ("vm_deallocate failed: %s",
1128 mach_error_string (status));
1129 }
1130 task_list = NULL;
1131 task_list_len = 0;
1133 if ((status = mach_port_deallocate (port_task_self, port_pset_priv))
1134 != KERN_SUCCESS)
1135 {
1136 ERROR ("mach_port_deallocate failed: %s",
1137 mach_error_string (status));
1138 }
1139 } /* for (pset_list) */
1141 ps_submit_state ("running", running);
1142 ps_submit_state ("sleeping", sleeping);
1143 ps_submit_state ("zombies", zombies);
1144 ps_submit_state ("stopped", stopped);
1145 ps_submit_state ("blocked", blocked);
1147 for (ps = list_head_g; ps != NULL; ps = ps->next)
1148 ps_submit_proc_list (ps);
1149 /* #endif HAVE_THREAD_INFO */
1151 #elif KERNEL_LINUX
1152 int running = 0;
1153 int sleeping = 0;
1154 int zombies = 0;
1155 int stopped = 0;
1156 int paging = 0;
1157 int blocked = 0;
1159 struct dirent *ent;
1160 DIR *proc;
1161 int pid;
1163 char cmdline[ARG_MAX];
1165 int status;
1166 procstat_t ps;
1167 procstat_entry_t pse;
1168 char state;
1170 procstat_t *ps_ptr;
1172 running = sleeping = zombies = stopped = paging = blocked = 0;
1173 ps_list_reset ();
1175 if ((proc = opendir ("/proc")) == NULL)
1176 {
1177 char errbuf[1024];
1178 ERROR ("Cannot open `/proc': %s",
1179 sstrerror (errno, errbuf, sizeof (errbuf)));
1180 return (-1);
1181 }
1183 while ((ent = readdir (proc)) != NULL)
1184 {
1185 if (!isdigit (ent->d_name[0]))
1186 continue;
1188 if ((pid = atoi (ent->d_name)) < 1)
1189 continue;
1191 status = ps_read_process (pid, &ps, &state);
1192 if (status != 0)
1193 {
1194 DEBUG ("ps_read_process failed: %i", status);
1195 continue;
1196 }
1198 pse.id = pid;
1199 pse.age = 0;
1201 pse.num_proc = ps.num_proc;
1202 pse.num_lwp = ps.num_lwp;
1203 pse.vmem_rss = ps.vmem_rss;
1205 pse.vmem_minflt = 0;
1206 pse.vmem_minflt_counter = ps.vmem_minflt_counter;
1207 pse.vmem_majflt = 0;
1208 pse.vmem_majflt_counter = ps.vmem_majflt_counter;
1210 pse.cpu_user = 0;
1211 pse.cpu_user_counter = ps.cpu_user_counter;
1212 pse.cpu_system = 0;
1213 pse.cpu_system_counter = ps.cpu_system_counter;
1215 switch (state)
1216 {
1217 case 'R': running++; break;
1218 case 'S': sleeping++; break;
1219 case 'D': blocked++; break;
1220 case 'Z': zombies++; break;
1221 case 'T': stopped++; break;
1222 case 'W': paging++; break;
1223 }
1225 ps_list_add (ps.name,
1226 ps_get_cmdline (pid, ps.name, cmdline, sizeof (cmdline)),
1227 &pse);
1228 }
1230 closedir (proc);
1232 ps_submit_state ("running", running);
1233 ps_submit_state ("sleeping", sleeping);
1234 ps_submit_state ("zombies", zombies);
1235 ps_submit_state ("stopped", stopped);
1236 ps_submit_state ("paging", paging);
1237 ps_submit_state ("blocked", blocked);
1239 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1240 ps_submit_proc_list (ps_ptr);
1241 /* #endif KERNEL_LINUX */
1243 #elif HAVE_LIBKVM_GETPROCS
1244 int running = 0;
1245 int sleeping = 0;
1246 int zombies = 0;
1247 int stopped = 0;
1248 int blocked = 0;
1249 int idle = 0;
1250 int wait = 0;
1252 kvm_t *kd;
1253 char errbuf[1024];
1254 char cmdline[ARG_MAX];
1255 char *cmdline_ptr;
1256 struct kinfo_proc *procs; /* array of processes */
1257 char **argv;
1258 int count; /* returns number of processes */
1259 int i;
1261 procstat_t *ps_ptr;
1262 procstat_entry_t pse;
1264 ps_list_reset ();
1266 /* Open the kvm interface, get a descriptor */
1267 kd = kvm_open (NULL, NULL, NULL, 0, errbuf);
1268 if (kd == NULL)
1269 {
1270 ERROR ("processes plugin: Cannot open kvm interface: %s",
1271 errbuf);
1272 return (0);
1273 }
1275 /* Get the list of processes. */
1276 procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, &count);
1277 if (procs == NULL)
1278 {
1279 kvm_close (kd);
1280 ERROR ("processes plugin: Cannot get kvm processes list: %s",
1281 kvm_geterr(kd));
1282 return (0);
1283 }
1285 /* Iterate through the processes in kinfo_proc */
1286 for (i = 0; i < count; i++)
1287 {
1288 /* retrieve the arguments */
1289 cmdline[0] = 0;
1290 cmdline_ptr = NULL;
1292 argv = kvm_getargv (kd, (const struct kinfo_proc *) &(procs[i]), 0);
1293 if (argv != NULL)
1294 {
1295 int status;
1296 int argc;
1298 argc = 0;
1299 while (argv[argc] != NULL)
1300 argc++;
1302 status = strjoin (cmdline, sizeof (cmdline),
1303 argv, argc, " ");
1305 if (status < 0)
1306 {
1307 WARNING ("processes plugin: Command line did "
1308 "not fit into buffer.");
1309 }
1310 else
1311 {
1312 cmdline_ptr = &cmdline[0];
1313 }
1314 }
1316 pse.id = procs[i].ki_pid;
1317 pse.age = 0;
1319 pse.num_proc = 1;
1320 pse.num_lwp = procs[i].ki_numthreads;
1322 pse.vmem_rss = procs[i].ki_rssize * getpagesize();
1323 pse.vmem_minflt = 0;
1324 pse.vmem_minflt_counter = procs[i].ki_rusage.ru_minflt;
1325 pse.vmem_majflt = 0;
1326 pse.vmem_majflt_counter = procs[i].ki_rusage.ru_majflt;
1328 pse.cpu_user = 0;
1329 pse.cpu_user_counter = procs[i].ki_rusage.ru_utime.tv_sec
1330 * 1000
1331 + procs[i].ki_rusage.ru_utime.tv_usec;
1332 pse.cpu_system = 0;
1333 pse.cpu_system_counter = procs[i].ki_rusage.ru_stime.tv_sec
1334 * 1000
1335 + procs[i].ki_rusage.ru_stime.tv_usec;
1337 switch (procs[i].ki_stat)
1338 {
1339 case SSTOP: stopped++; break;
1340 case SSLEEP: sleeping++; break;
1341 case SRUN: running++; break;
1342 case SIDL: idle++; break;
1343 case SWAIT: wait++; break;
1344 case SLOCK: blocked++; break;
1345 case SZOMB: zombies++; break;
1346 }
1348 ps_list_add (procs[i].ki_comm, cmdline_ptr, &pse);
1349 }
1351 kvm_close(kd);
1353 ps_submit_state ("running", running);
1354 ps_submit_state ("sleeping", sleeping);
1355 ps_submit_state ("zombies", zombies);
1356 ps_submit_state ("stopped", stopped);
1357 ps_submit_state ("blocked", blocked);
1358 ps_submit_state ("idle", idle);
1359 ps_submit_state ("wait", wait);
1361 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1362 ps_submit_proc_list (ps_ptr);
1363 #endif /* HAVE_LIBKVM_GETPROCS */
1365 return (0);
1366 } /* int ps_read */
1368 void module_register (void)
1369 {
1370 plugin_register_config ("processes", ps_config,
1371 config_keys, config_keys_num);
1372 plugin_register_init ("processes", ps_init);
1373 plugin_register_read ("processes", ps_read);
1374 } /* void module_register */