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 **/
27 #include "collectd.h"
28 #include "common.h"
29 #include "plugin.h"
30 #include "configfile.h"
32 /* Include header files for the mach system, if they exist.. */
33 #if HAVE_THREAD_INFO
34 # if HAVE_MACH_MACH_INIT_H
35 # include <mach/mach_init.h>
36 # endif
37 # if HAVE_MACH_HOST_PRIV_H
38 # include <mach/host_priv.h>
39 # endif
40 # if HAVE_MACH_MACH_ERROR_H
41 # include <mach/mach_error.h>
42 # endif
43 # if HAVE_MACH_MACH_HOST_H
44 # include <mach/mach_host.h>
45 # endif
46 # if HAVE_MACH_MACH_PORT_H
47 # include <mach/mach_port.h>
48 # endif
49 # if HAVE_MACH_MACH_TYPES_H
50 # include <mach/mach_types.h>
51 # endif
52 # if HAVE_MACH_MESSAGE_H
53 # include <mach/message.h>
54 # endif
55 # if HAVE_MACH_PROCESSOR_SET_H
56 # include <mach/processor_set.h>
57 # endif
58 # if HAVE_MACH_TASK_H
59 # include <mach/task.h>
60 # endif
61 # if HAVE_MACH_THREAD_ACT_H
62 # include <mach/thread_act.h>
63 # endif
64 # if HAVE_MACH_VM_REGION_H
65 # include <mach/vm_region.h>
66 # endif
67 # if HAVE_MACH_VM_MAP_H
68 # include <mach/vm_map.h>
69 # endif
70 # if HAVE_MACH_VM_PROT_H
71 # include <mach/vm_prot.h>
72 # endif
73 # if HAVE_SYS_SYSCTL_H
74 # include <sys/sysctl.h>
75 # endif
76 /* #endif HAVE_THREAD_INFO */
78 #elif KERNEL_LINUX
79 # if HAVE_LINUX_CONFIG_H
80 # include <linux/config.h>
81 # endif
82 # ifndef CONFIG_HZ
83 # define CONFIG_HZ 100
84 # endif
85 /* #endif KERNEL_LINUX */
87 #elif HAVE_LIBKVM_GETPROCS
88 # include <kvm.h>
89 # include <sys/user.h>
90 # include <sys/proc.h>
91 # if HAVE_SYS_SYSCTL_H
92 # include <sys/sysctl.h>
93 # endif
94 /* #endif HAVE_LIBKVM_GETPROCS */
96 #else
97 # error "No applicable input method."
98 #endif
100 #if HAVE_REGEX_H
101 # include <regex.h>
102 #endif
104 #define BUFSIZE 256
106 static const char *config_keys[] =
107 {
108 "Process",
109 "ProcessMatch",
110 NULL
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 return (1);
464 fields_num = strsplit (new_val, fields,
465 STATIC_ARRAY_SIZE (fields));
466 if (fields_num != 2)
467 {
468 sfree (new_val);
469 return (1);
470 }
471 ps_list_register (fields[0], fields[1]);
472 sfree (new_val);
473 }
474 else
475 {
476 return (-1);
477 }
479 return (0);
480 }
482 static int ps_init (void)
483 {
484 #if HAVE_THREAD_INFO
485 kern_return_t status;
487 port_host_self = mach_host_self ();
488 port_task_self = mach_task_self ();
490 if (pset_list != NULL)
491 {
492 vm_deallocate (port_task_self,
493 (vm_address_t) pset_list,
494 pset_list_len * sizeof (processor_set_t));
495 pset_list = NULL;
496 pset_list_len = 0;
497 }
499 if ((status = host_processor_sets (port_host_self,
500 &pset_list,
501 &pset_list_len)) != KERN_SUCCESS)
502 {
503 ERROR ("host_processor_sets failed: %s\n",
504 mach_error_string (status));
505 pset_list = NULL;
506 pset_list_len = 0;
507 return (-1);
508 }
509 /* #endif HAVE_THREAD_INFO */
511 #elif KERNEL_LINUX
512 pagesize_g = sysconf(_SC_PAGESIZE);
513 DEBUG ("pagesize_g = %li; CONFIG_HZ = %i;",
514 pagesize_g, CONFIG_HZ);
515 /* #endif KERNEL_LINUX */
517 #elif HAVE_LIBKVM_GETPROCS
518 /* no initialization */
519 #endif /* HAVE_LIBKVM_GETPROCS */
521 return (0);
522 } /* int ps_init */
524 /* submit global state (e.g.: qty of zombies, running, etc..) */
525 static void ps_submit_state (const char *state, double value)
526 {
527 value_t values[1];
528 value_list_t vl = VALUE_LIST_INIT;
530 values[0].gauge = value;
532 vl.values = values;
533 vl.values_len = 1;
534 vl.time = time (NULL);
535 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
536 sstrncpy (vl.plugin, "processes", sizeof (vl.plugin));
537 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
538 sstrncpy (vl.type, "ps_state", sizeof (vl.type));
539 sstrncpy (vl.type_instance, state, sizeof (vl.type_instance));
541 plugin_dispatch_values (&vl);
542 }
544 /* submit info about specific process (e.g.: memory taken, cpu usage, etc..) */
545 static void ps_submit_proc_list (procstat_t *ps)
546 {
547 value_t values[2];
548 value_list_t vl = VALUE_LIST_INIT;
550 vl.values = values;
551 vl.values_len = 2;
552 vl.time = time (NULL);
553 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
554 sstrncpy (vl.plugin, "processes", sizeof (vl.plugin));
555 sstrncpy (vl.plugin_instance, ps->name, sizeof (vl.plugin_instance));
557 sstrncpy (vl.type, "ps_rss", sizeof (vl.type));
558 vl.values[0].gauge = ps->vmem_rss;
559 vl.values_len = 1;
560 plugin_dispatch_values (&vl);
562 sstrncpy (vl.type, "ps_cputime", sizeof (vl.type));
563 vl.values[0].counter = ps->cpu_user_counter;
564 vl.values[1].counter = ps->cpu_system_counter;
565 vl.values_len = 2;
566 plugin_dispatch_values (&vl);
568 sstrncpy (vl.type, "ps_count", sizeof (vl.type));
569 vl.values[0].gauge = ps->num_proc;
570 vl.values[1].gauge = ps->num_lwp;
571 vl.values_len = 2;
572 plugin_dispatch_values (&vl);
574 sstrncpy (vl.type, "ps_pagefaults", sizeof (vl.type));
575 vl.values[0].counter = ps->vmem_minflt_counter;
576 vl.values[1].counter = ps->vmem_majflt_counter;
577 vl.values_len = 2;
578 plugin_dispatch_values (&vl);
580 DEBUG ("name = %s; num_proc = %lu; num_lwp = %lu; vmem_rss = %lu; "
581 "vmem_minflt_counter = %lu; vmem_majflt_counter = %lu; "
582 "cpu_user_counter = %lu; cpu_system_counter = %lu;",
583 ps->name, ps->num_proc, ps->num_lwp, ps->vmem_rss,
584 ps->vmem_minflt_counter, ps->vmem_majflt_counter,
585 ps->cpu_user_counter, ps->cpu_system_counter);
586 } /* void ps_submit_proc_list */
588 /* ------- additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
589 #if KERNEL_LINUX
590 static int *ps_read_tasks (int pid)
591 {
592 int *list = NULL;
593 int list_size = 1; /* size of allocated space, in elements */
594 int list_len = 0; /* number of currently used elements */
596 char dirname[64];
597 DIR *dh;
598 struct dirent *ent;
600 ssnprintf (dirname, sizeof (dirname), "/proc/%i/task", pid);
602 if ((dh = opendir (dirname)) == NULL)
603 {
604 DEBUG ("Failed to open directory `%s'", dirname);
605 return (NULL);
606 }
608 while ((ent = readdir (dh)) != NULL)
609 {
610 if (!isdigit (ent->d_name[0]))
611 continue;
613 if ((list_len + 1) >= list_size)
614 {
615 int *new_ptr;
616 int new_size = 2 * list_size;
617 /* Comes in sizes: 2, 4, 8, 16, ... */
619 new_ptr = (int *) realloc (list, (size_t) (sizeof (int) * new_size));
620 if (new_ptr == NULL)
621 {
622 if (list != NULL)
623 free (list);
624 ERROR ("processes plugin: "
625 "Failed to allocate more memory.");
626 return (NULL);
627 }
629 list = new_ptr;
630 list_size = new_size;
632 memset (list + list_len, 0, sizeof (int) * (list_size - list_len));
633 }
635 list[list_len] = atoi (ent->d_name);
636 if (list[list_len] != 0)
637 list_len++;
638 }
640 closedir (dh);
642 if (list_len == 0)
643 return (NULL);
645 assert (list_len < list_size);
646 assert (list[list_len] == 0);
648 return (list);
649 } /* int *ps_read_tasks */
651 int ps_read_process (int pid, procstat_t *ps, char *state)
652 {
653 char filename[64];
654 char buffer[1024];
656 char *fields[64];
657 char fields_len;
659 int *tasks;
660 int i;
662 int ppid;
663 int name_len;
665 long long unsigned cpu_user_counter;
666 long long unsigned cpu_system_counter;
667 long long unsigned vmem_rss;
669 memset (ps, 0, sizeof (procstat_t));
671 ssnprintf (filename, sizeof (filename), "/proc/%i/stat", pid);
673 i = read_file_contents (filename, buffer, sizeof(buffer) - 1);
674 if (i <= 0)
675 return (-1);
676 buffer[i] = 0;
678 fields_len = strsplit (buffer, fields, 64);
679 if (fields_len < 24)
680 {
681 DEBUG ("processes plugin: ps_read_process (pid = %i):"
682 " `%s' has only %i fields..",
683 (int) pid, filename, fields_len);
684 return (-1);
685 }
687 /* copy the name, strip brackets in the process */
688 name_len = strlen (fields[1]) - 2;
689 if ((fields[1][0] != '(') || (fields[1][name_len + 1] != ')'))
690 {
691 DEBUG ("No brackets found in process name: `%s'", fields[1]);
692 return (-1);
693 }
694 fields[1] = fields[1] + 1;
695 fields[1][name_len] = '\0';
696 strncpy (ps->name, fields[1], PROCSTAT_NAME_LEN);
698 ppid = atoi (fields[3]);
700 *state = fields[2][0];
702 if (*state == 'Z')
703 {
704 ps->num_lwp = 0;
705 ps->num_proc = 0;
706 }
707 else if ((tasks = ps_read_tasks (pid)) == NULL)
708 {
709 /* Kernel 2.4 or so */
710 ps->num_lwp = 1;
711 ps->num_proc = 1;
712 }
713 else
714 {
715 ps->num_lwp = 0;
716 ps->num_proc = 1;
717 for (i = 0; tasks[i] != 0; i++)
718 ps->num_lwp++;
720 free (tasks);
721 tasks = NULL;
722 }
724 /* Leave the rest at zero if this is only a zombi */
725 if (ps->num_proc == 0)
726 {
727 DEBUG ("processes plugin: This is only a zombi: pid = %i; "
728 "name = %s;", pid, ps->name);
729 return (0);
730 }
732 cpu_user_counter = atoll (fields[13]);
733 cpu_system_counter = atoll (fields[14]);
734 vmem_rss = atoll (fields[23]);
735 ps->vmem_minflt_counter = atol (fields[9]);
736 ps->vmem_majflt_counter = atol (fields[11]);
738 /* Convert jiffies to useconds */
739 cpu_user_counter = cpu_user_counter * 1000000 / CONFIG_HZ;
740 cpu_system_counter = cpu_system_counter * 1000000 / CONFIG_HZ;
741 vmem_rss = vmem_rss * pagesize_g;
743 ps->cpu_user_counter = (unsigned long) cpu_user_counter;
744 ps->cpu_system_counter = (unsigned long) cpu_system_counter;
745 ps->vmem_rss = (unsigned long) vmem_rss;
747 /* success */
748 return (0);
749 } /* int ps_read_process (...) */
750 #endif /* KERNEL_LINUX */
752 #if HAVE_THREAD_INFO
753 static int mach_get_task_name (task_t t, int *pid, char *name, size_t name_max_len)
754 {
755 int mib[4];
757 struct kinfo_proc kp;
758 size_t kp_size;
760 mib[0] = CTL_KERN;
761 mib[1] = KERN_PROC;
762 mib[2] = KERN_PROC_PID;
764 if (pid_for_task (t, pid) != KERN_SUCCESS)
765 return (-1);
766 mib[3] = *pid;
768 kp_size = sizeof (kp);
769 if (sysctl (mib, 4, &kp, &kp_size, NULL, 0) != 0)
770 return (-1);
772 if (name_max_len > (MAXCOMLEN + 1))
773 name_max_len = MAXCOMLEN + 1;
775 strncpy (name, kp.kp_proc.p_comm, name_max_len - 1);
776 name[name_max_len - 1] = '\0';
778 DEBUG ("pid = %i; name = %s;", *pid, name);
780 /* We don't do the special handling for `p_comm == "LaunchCFMApp"' as
781 * `top' does it, because it is a lot of work and only used when
782 * debugging. -octo */
784 return (0);
785 }
786 #endif /* HAVE_THREAD_INFO */
787 /* ------- end of additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
789 /* do actual readings from kernel */
790 static int ps_read (void)
791 {
792 #if HAVE_THREAD_INFO
793 kern_return_t status;
795 int pset;
796 processor_set_t port_pset_priv;
798 int task;
799 task_array_t task_list;
800 mach_msg_type_number_t task_list_len;
802 int task_pid;
803 char task_name[MAXCOMLEN + 1];
805 int thread;
806 thread_act_array_t thread_list;
807 mach_msg_type_number_t thread_list_len;
808 thread_basic_info_data_t thread_data;
809 mach_msg_type_number_t thread_data_len;
811 int running = 0;
812 int sleeping = 0;
813 int zombies = 0;
814 int stopped = 0;
815 int blocked = 0;
817 procstat_t *ps;
818 procstat_entry_t pse;
820 ps_list_reset ();
822 /*
823 * The Mach-concept is a little different from the traditional UNIX
824 * concept: All the work is done in threads. Threads are contained in
825 * `tasks'. Therefore, `task status' doesn't make much sense, since
826 * it's actually a `thread status'.
827 * Tasks are assigned to sets of processors, so that's where you go to
828 * get a list.
829 */
830 for (pset = 0; pset < pset_list_len; pset++)
831 {
832 if ((status = host_processor_set_priv (port_host_self,
833 pset_list[pset],
834 &port_pset_priv)) != KERN_SUCCESS)
835 {
836 ERROR ("host_processor_set_priv failed: %s\n",
837 mach_error_string (status));
838 continue;
839 }
841 if ((status = processor_set_tasks (port_pset_priv,
842 &task_list,
843 &task_list_len)) != KERN_SUCCESS)
844 {
845 ERROR ("processor_set_tasks failed: %s\n",
846 mach_error_string (status));
847 mach_port_deallocate (port_task_self, port_pset_priv);
848 continue;
849 }
851 for (task = 0; task < task_list_len; task++)
852 {
853 ps = NULL;
854 if (mach_get_task_name (task_list[task],
855 &task_pid,
856 task_name, PROCSTAT_NAME_LEN) == 0)
857 {
858 /* search for at least one match */
859 for (ps = list_head_g; ps != NULL; ps = ps->next)
860 /* FIXME: cmdline should be here instead of NULL */
861 if (ps_list_match (task_name, NULL, ps) == 1)
862 break;
863 }
865 /* Collect more detailed statistics for this process */
866 if (ps != NULL)
867 {
868 task_basic_info_data_t task_basic_info;
869 mach_msg_type_number_t task_basic_info_len;
870 task_events_info_data_t task_events_info;
871 mach_msg_type_number_t task_events_info_len;
872 task_absolutetime_info_data_t task_absolutetime_info;
873 mach_msg_type_number_t task_absolutetime_info_len;
875 memset (&pse, '\0', sizeof (pse));
876 pse.id = task_pid;
878 task_basic_info_len = TASK_BASIC_INFO_COUNT;
879 status = task_info (task_list[task],
880 TASK_BASIC_INFO,
881 (task_info_t) &task_basic_info,
882 &task_basic_info_len);
883 if (status != KERN_SUCCESS)
884 {
885 ERROR ("task_info failed: %s",
886 mach_error_string (status));
887 continue; /* with next thread_list */
888 }
890 task_events_info_len = TASK_EVENTS_INFO_COUNT;
891 status = task_info (task_list[task],
892 TASK_EVENTS_INFO,
893 (task_info_t) &task_events_info,
894 &task_events_info_len);
895 if (status != KERN_SUCCESS)
896 {
897 ERROR ("task_info failed: %s",
898 mach_error_string (status));
899 continue; /* with next thread_list */
900 }
902 task_absolutetime_info_len = TASK_ABSOLUTETIME_INFO_COUNT;
903 status = task_info (task_list[task],
904 TASK_ABSOLUTETIME_INFO,
905 (task_info_t) &task_absolutetime_info,
906 &task_absolutetime_info_len);
907 if (status != KERN_SUCCESS)
908 {
909 ERROR ("task_info failed: %s",
910 mach_error_string (status));
911 continue; /* with next thread_list */
912 }
914 pse.num_proc++;
915 pse.vmem_rss = task_basic_info.resident_size;
917 pse.vmem_minflt_counter = task_events_info.cow_faults;
918 pse.vmem_majflt_counter = task_events_info.faults;
920 pse.cpu_user_counter = task_absolutetime_info.total_user;
921 pse.cpu_system_counter = task_absolutetime_info.total_system;
922 }
924 status = task_threads (task_list[task], &thread_list,
925 &thread_list_len);
926 if (status != KERN_SUCCESS)
927 {
928 /* Apple's `top' treats this case a zombie. It
929 * makes sense to some extend: A `zombie'
930 * thread is nonsense, since the task/process
931 * is dead. */
932 zombies++;
933 DEBUG ("task_threads failed: %s",
934 mach_error_string (status));
935 if (task_list[task] != port_task_self)
936 mach_port_deallocate (port_task_self,
937 task_list[task]);
938 continue; /* with next task_list */
939 }
941 for (thread = 0; thread < thread_list_len; thread++)
942 {
943 thread_data_len = THREAD_BASIC_INFO_COUNT;
944 status = thread_info (thread_list[thread],
945 THREAD_BASIC_INFO,
946 (thread_info_t) &thread_data,
947 &thread_data_len);
948 if (status != KERN_SUCCESS)
949 {
950 ERROR ("thread_info failed: %s",
951 mach_error_string (status));
952 if (task_list[task] != port_task_self)
953 mach_port_deallocate (port_task_self,
954 thread_list[thread]);
955 continue; /* with next thread_list */
956 }
958 if (ps != NULL)
959 pse.num_lwp++;
961 switch (thread_data.run_state)
962 {
963 case TH_STATE_RUNNING:
964 running++;
965 break;
966 case TH_STATE_STOPPED:
967 /* What exactly is `halted'? */
968 case TH_STATE_HALTED:
969 stopped++;
970 break;
971 case TH_STATE_WAITING:
972 sleeping++;
973 break;
974 case TH_STATE_UNINTERRUPTIBLE:
975 blocked++;
976 break;
977 /* There is no `zombie' case here,
978 * since there are no zombie-threads.
979 * There's only zombie tasks, which are
980 * handled above. */
981 default:
982 WARNING ("Unknown thread status: %s",
983 thread_data.run_state);
984 break;
985 } /* switch (thread_data.run_state) */
987 if (task_list[task] != port_task_self)
988 {
989 status = mach_port_deallocate (port_task_self,
990 thread_list[thread]);
991 if (status != KERN_SUCCESS)
992 ERROR ("mach_port_deallocate failed: %s",
993 mach_error_string (status));
994 }
995 } /* for (thread_list) */
997 if ((status = vm_deallocate (port_task_self,
998 (vm_address_t) thread_list,
999 thread_list_len * sizeof (thread_act_t)))
1000 != KERN_SUCCESS)
1001 {
1002 ERROR ("vm_deallocate failed: %s",
1003 mach_error_string (status));
1004 }
1005 thread_list = NULL;
1006 thread_list_len = 0;
1008 /* Only deallocate the task port, if it isn't our own.
1009 * Don't know what would happen in that case, but this
1010 * is what Apple's top does.. ;) */
1011 if (task_list[task] != port_task_self)
1012 {
1013 status = mach_port_deallocate (port_task_self,
1014 task_list[task]);
1015 if (status != KERN_SUCCESS)
1016 ERROR ("mach_port_deallocate failed: %s",
1017 mach_error_string (status));
1018 }
1020 if (ps != NULL)
1021 /* FIXME: cmdline should be here instead of NULL */
1022 ps_list_add (task_name, NULL, &pse);
1023 } /* for (task_list) */
1025 if ((status = vm_deallocate (port_task_self,
1026 (vm_address_t) task_list,
1027 task_list_len * sizeof (task_t))) != KERN_SUCCESS)
1028 {
1029 ERROR ("vm_deallocate failed: %s",
1030 mach_error_string (status));
1031 }
1032 task_list = NULL;
1033 task_list_len = 0;
1035 if ((status = mach_port_deallocate (port_task_self, port_pset_priv))
1036 != KERN_SUCCESS)
1037 {
1038 ERROR ("mach_port_deallocate failed: %s",
1039 mach_error_string (status));
1040 }
1041 } /* for (pset_list) */
1043 ps_submit_state ("running", running);
1044 ps_submit_state ("sleeping", sleeping);
1045 ps_submit_state ("zombies", zombies);
1046 ps_submit_state ("stopped", stopped);
1047 ps_submit_state ("blocked", blocked);
1049 for (ps = list_head_g; ps != NULL; ps = ps->next)
1050 ps_submit_proc_list (ps);
1051 /* #endif HAVE_THREAD_INFO */
1053 #elif KERNEL_LINUX
1054 int running = 0;
1055 int sleeping = 0;
1056 int zombies = 0;
1057 int stopped = 0;
1058 int paging = 0;
1059 int blocked = 0;
1061 struct dirent *ent;
1062 DIR *proc;
1063 int pid;
1065 int status;
1066 procstat_t ps;
1067 procstat_entry_t pse;
1068 char state;
1070 procstat_t *ps_ptr;
1072 running = sleeping = zombies = stopped = paging = blocked = 0;
1073 ps_list_reset ();
1075 if ((proc = opendir ("/proc")) == NULL)
1076 {
1077 char errbuf[1024];
1078 ERROR ("Cannot open `/proc': %s",
1079 sstrerror (errno, errbuf, sizeof (errbuf)));
1080 return (-1);
1081 }
1083 while ((ent = readdir (proc)) != NULL)
1084 {
1085 if (!isdigit (ent->d_name[0]))
1086 continue;
1088 if ((pid = atoi (ent->d_name)) < 1)
1089 continue;
1091 status = ps_read_process (pid, &ps, &state);
1092 if (status != 0)
1093 {
1094 DEBUG ("ps_read_process failed: %i", status);
1095 continue;
1096 }
1098 pse.id = pid;
1099 pse.age = 0;
1101 pse.num_proc = ps.num_proc;
1102 pse.num_lwp = ps.num_lwp;
1103 pse.vmem_rss = ps.vmem_rss;
1105 pse.vmem_minflt = 0;
1106 pse.vmem_minflt_counter = ps.vmem_minflt_counter;
1107 pse.vmem_majflt = 0;
1108 pse.vmem_majflt_counter = ps.vmem_majflt_counter;
1110 pse.cpu_user = 0;
1111 pse.cpu_user_counter = ps.cpu_user_counter;
1112 pse.cpu_system = 0;
1113 pse.cpu_system_counter = ps.cpu_system_counter;
1115 switch (state)
1116 {
1117 case 'R': running++; break;
1118 case 'S': sleeping++; break;
1119 case 'D': blocked++; break;
1120 case 'Z': zombies++; break;
1121 case 'T': stopped++; break;
1122 case 'W': paging++; break;
1123 }
1125 /* FIXME: cmdline should be here instead of NULL */
1126 ps_list_add (ps.name, NULL, &pse);
1127 }
1129 closedir (proc);
1131 ps_submit_state ("running", running);
1132 ps_submit_state ("sleeping", sleeping);
1133 ps_submit_state ("zombies", zombies);
1134 ps_submit_state ("stopped", stopped);
1135 ps_submit_state ("paging", paging);
1136 ps_submit_state ("blocked", blocked);
1138 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1139 ps_submit_proc_list (ps_ptr);
1140 /* #endif KERNEL_LINUX */
1142 #elif HAVE_LIBKVM_GETPROCS
1143 int running = 0;
1144 int sleeping = 0;
1145 int zombies = 0;
1146 int stopped = 0;
1147 int blocked = 0;
1148 int idle = 0;
1149 int wait = 0;
1151 kvm_t *kd;
1152 char errbuf[1024];
1153 char cmdline[ARG_MAX];
1154 char *cmdline_ptr;
1155 struct kinfo_proc *procs; /* array of processes */
1156 char **argv;
1157 int count; /* returns number of processes */
1158 int i;
1160 procstat_t *ps_ptr;
1161 procstat_entry_t pse;
1163 ps_list_reset ();
1165 /* Open the kvm interface, get a descriptor */
1166 kd = kvm_open (NULL, NULL, NULL, 0, errbuf);
1167 if (kd == NULL)
1168 {
1169 ERROR ("processes plugin: Cannot open kvm interface: %s",
1170 errbuf);
1171 return (0);
1172 }
1174 /* Get the list of processes. */
1175 procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, &count);
1176 if (procs == NULL)
1177 {
1178 kvm_close (kd);
1179 ERROR ("processes plugin: Cannot get kvm processes list: %s",
1180 kvm_geterr(kd));
1181 return (0);
1182 }
1184 /* Iterate through the processes in kinfo_proc */
1185 for (i = 0; i < count; i++)
1186 {
1187 /* retrieve the arguments */
1188 cmdline[0] = 0;
1189 cmdline_ptr = NULL;
1191 argv = kvm_getargv (kd, (const struct kinfo_proc *) &(procs[i]), 0);
1192 if (argv != NULL)
1193 {
1194 int status;
1195 int argc;
1197 argc = 0;
1198 while (argv[argc] != NULL)
1199 argc++;
1201 status = strjoin (cmdline, sizeof (cmdline),
1202 argv, argc, " ");
1204 if (status < 0)
1205 {
1206 WARNING ("processes plugin: Command line did "
1207 "not fit into buffer.");
1208 }
1209 else
1210 {
1211 cmdline_ptr = &cmdline[0];
1212 }
1213 }
1215 pse.id = procs[i].ki_pid;
1216 pse.age = 0;
1218 pse.num_proc = 1;
1219 pse.num_lwp = procs[i].ki_numthreads;
1221 pse.vmem_rss = procs[i].ki_rssize * getpagesize();
1222 pse.vmem_minflt = 0;
1223 pse.vmem_minflt_counter = procs[i].ki_rusage.ru_minflt;
1224 pse.vmem_majflt = 0;
1225 pse.vmem_majflt_counter = procs[i].ki_rusage.ru_majflt;
1227 pse.cpu_user = 0;
1228 pse.cpu_user_counter = procs[i].ki_rusage.ru_utime.tv_sec
1229 * 1000
1230 + procs[i].ki_rusage.ru_utime.tv_usec;
1231 pse.cpu_system = 0;
1232 pse.cpu_system_counter = procs[i].ki_rusage.ru_stime.tv_sec
1233 * 1000
1234 + procs[i].ki_rusage.ru_stime.tv_usec;
1236 switch (procs[i].ki_stat)
1237 {
1238 case SSTOP: stopped++; break;
1239 case SSLEEP: sleeping++; break;
1240 case SRUN: running++; break;
1241 case SIDL: idle++; break;
1242 case SWAIT: wait++; break;
1243 case SLOCK: blocked++; break;
1244 case SZOMB: zombies++; break;
1245 }
1247 ps_list_add (procs[i].ki_comm, cmdline_ptr, &pse);
1248 }
1250 kvm_close(kd);
1252 ps_submit_state ("running", running);
1253 ps_submit_state ("sleeping", sleeping);
1254 ps_submit_state ("zombies", zombies);
1255 ps_submit_state ("stopped", stopped);
1256 ps_submit_state ("blocked", blocked);
1257 ps_submit_state ("idle", idle);
1258 ps_submit_state ("wait", wait);
1260 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1261 ps_submit_proc_list (ps_ptr);
1262 #endif /* HAVE_LIBKVM_GETPROCS */
1264 return (0);
1265 } /* int ps_read */
1267 void module_register (void)
1268 {
1269 plugin_register_config ("processes", ps_config,
1270 config_keys, config_keys_num);
1271 plugin_register_init ("processes", ps_init);
1272 plugin_register_read ("processes", ps_read);
1273 } /* void module_register */