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_KVM_H
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_KVM_H */
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 /* put name of process from config to list_head_g tree
173 list_head_g is a list of 'procstat_t' structs with
174 processes names we want to watch */
175 static void ps_list_register (const char *name, const char *regexp)
176 {
177 procstat_t *new;
178 procstat_t *ptr;
179 int status;
181 new = (procstat_t *) malloc (sizeof (procstat_t));
182 if (new == NULL)
183 {
184 ERROR ("processes plugin: ps_list_register: malloc failed.");
185 return;
186 }
187 memset (new, 0, sizeof (procstat_t));
188 sstrncpy (new->name, name, sizeof (new->name));
190 #if HAVE_REGEX_H
191 if (regexp != NULL)
192 {
193 DEBUG ("ProcessMatch: adding \"%s\" as criteria to process %s.", regexp, name);
194 new->re = (regex_t *) malloc (sizeof (regex_t));
195 if (new->re == NULL)
196 {
197 ERROR ("processes plugin: ps_list_register: malloc failed.");
198 sfree (new);
199 return;
200 }
202 status = regcomp (new->re, regexp, REG_EXTENDED | REG_NOSUB);
203 if (status != 0)
204 {
205 DEBUG ("ProcessMatch: compiling the regular expression \"%s\" failed.", regexp);
206 sfree(new->re);
207 return;
208 }
209 }
210 #else
211 if (regexp != NULL)
212 {
213 ERROR ("processes plugin: ps_list_register: "
214 "Regular expression \"%s\" found in config "
215 "file, but support for regular expressions "
216 "has been dispabled at compile time.",
217 regexp);
218 sfree (new);
219 return;
220 }
221 #endif
223 for (ptr = list_head_g; ptr != NULL; ptr = ptr->next)
224 {
225 if (strcmp (ptr->name, name) == 0)
226 {
227 WARNING ("processes plugin: You have configured more "
228 "than one `Process' or "
229 "`ProcessMatch' with the same name. "
230 "All but the first setting will be "
231 "ignored.");
232 sfree (new->re);
233 sfree (new);
234 return;
235 }
237 if (ptr->next == NULL)
238 break;
239 }
241 if (ptr == NULL)
242 list_head_g = new;
243 else
244 ptr->next = new;
245 } /* void ps_list_register */
247 /* try to match name against entry, returns 1 if success */
248 static int ps_list_match (const char *name, const char *cmdline, procstat_t *ps)
249 {
250 #if HAVE_REGEX_H
251 if (ps->re != NULL)
252 {
253 int status;
254 const char *str;
256 str = cmdline;
257 if ((str == NULL) || (str[0] == 0))
258 str = name;
260 assert (str != NULL);
262 status = regexec (ps->re, str,
263 /* nmatch = */ 0,
264 /* pmatch = */ NULL,
265 /* eflags = */ 0);
266 if (status == 0)
267 return (1);
268 }
269 else
270 #endif
271 if (strcmp (ps->name, name) == 0)
272 return (1);
274 return (0);
275 } /* int ps_list_match */
277 /* add process entry to 'instances' of process 'name' (or refresh it) */
278 static void ps_list_add (const char *name, const char *cmdline, procstat_entry_t *entry)
279 {
280 procstat_t *ps;
281 procstat_entry_t *pse;
283 if (entry->id == 0)
284 return;
286 for (ps = list_head_g; ps != NULL; ps = ps->next)
287 {
289 if ((ps_list_match (name, cmdline, ps)) == 0)
290 continue;
292 for (pse = ps->instances; pse != NULL; pse = pse->next)
293 if ((pse->id == entry->id) || (pse->next == NULL))
294 break;
296 if ((pse == NULL) || (pse->id != entry->id))
297 {
298 procstat_entry_t *new;
300 new = (procstat_entry_t *) malloc (sizeof (procstat_entry_t));
301 if (new == NULL)
302 return;
303 memset (new, 0, sizeof (procstat_entry_t));
304 new->id = entry->id;
306 if (pse == NULL)
307 ps->instances = new;
308 else
309 pse->next = new;
311 pse = new;
312 }
314 pse->age = 0;
315 pse->num_proc = entry->num_proc;
316 pse->num_lwp = entry->num_lwp;
317 pse->vmem_rss = entry->vmem_rss;
319 ps->num_proc += pse->num_proc;
320 ps->num_lwp += pse->num_lwp;
321 ps->vmem_rss += pse->vmem_rss;
323 if ((entry->vmem_minflt_counter == 0)
324 && (entry->vmem_majflt_counter == 0))
325 {
326 pse->vmem_minflt_counter += entry->vmem_minflt;
327 pse->vmem_minflt = entry->vmem_minflt;
329 pse->vmem_majflt_counter += entry->vmem_majflt;
330 pse->vmem_majflt = entry->vmem_majflt;
331 }
332 else
333 {
334 if (entry->vmem_minflt_counter < pse->vmem_minflt_counter)
335 {
336 pse->vmem_minflt = entry->vmem_minflt_counter
337 + (ULONG_MAX - pse->vmem_minflt_counter);
338 }
339 else
340 {
341 pse->vmem_minflt = entry->vmem_minflt_counter - pse->vmem_minflt_counter;
342 }
343 pse->vmem_minflt_counter = entry->vmem_minflt_counter;
345 if (entry->vmem_majflt_counter < pse->vmem_majflt_counter)
346 {
347 pse->vmem_majflt = entry->vmem_majflt_counter
348 + (ULONG_MAX - pse->vmem_majflt_counter);
349 }
350 else
351 {
352 pse->vmem_majflt = entry->vmem_majflt_counter - pse->vmem_majflt_counter;
353 }
354 pse->vmem_majflt_counter = entry->vmem_majflt_counter;
355 }
357 ps->vmem_minflt_counter += pse->vmem_minflt;
358 ps->vmem_majflt_counter += pse->vmem_majflt;
360 if ((entry->cpu_user_counter == 0)
361 && (entry->cpu_system_counter == 0))
362 {
363 pse->cpu_user_counter += entry->cpu_user;
364 pse->cpu_user = entry->cpu_user;
366 pse->cpu_system_counter += entry->cpu_system;
367 pse->cpu_system = entry->cpu_system;
368 }
369 else
370 {
371 if (entry->cpu_user_counter < pse->cpu_user_counter)
372 {
373 pse->cpu_user = entry->cpu_user_counter
374 + (ULONG_MAX - pse->cpu_user_counter);
375 }
376 else
377 {
378 pse->cpu_user = entry->cpu_user_counter - pse->cpu_user_counter;
379 }
380 pse->cpu_user_counter = entry->cpu_user_counter;
382 if (entry->cpu_system_counter < pse->cpu_system_counter)
383 {
384 pse->cpu_system = entry->cpu_system_counter
385 + (ULONG_MAX - pse->cpu_system_counter);
386 }
387 else
388 {
389 pse->cpu_system = entry->cpu_system_counter - pse->cpu_system_counter;
390 }
391 pse->cpu_system_counter = entry->cpu_system_counter;
392 }
394 ps->cpu_user_counter += pse->cpu_user;
395 ps->cpu_system_counter += pse->cpu_system;
396 }
397 }
399 /* remove old entries from instances of processes in list_head_g */
400 static void ps_list_reset (void)
401 {
402 procstat_t *ps;
403 procstat_entry_t *pse;
404 procstat_entry_t *pse_prev;
406 for (ps = list_head_g; ps != NULL; ps = ps->next)
407 {
408 ps->num_proc = 0;
409 ps->num_lwp = 0;
410 ps->vmem_rss = 0;
412 pse_prev = NULL;
413 pse = ps->instances;
414 while (pse != NULL)
415 {
416 if (pse->age > 10)
417 {
418 DEBUG ("Removing this procstat entry cause it's too old: "
419 "id = %lu; name = %s;",
420 pse->id, ps->name);
422 if (pse_prev == NULL)
423 {
424 ps->instances = pse->next;
425 free (pse);
426 pse = ps->instances;
427 }
428 else
429 {
430 pse_prev->next = pse->next;
431 free (pse);
432 pse = pse_prev->next;
433 }
434 }
435 else
436 {
437 pse->age++;
438 pse_prev = pse;
439 pse = pse->next;
440 }
441 } /* while (pse != NULL) */
442 } /* for (ps = list_head_g; ps != NULL; ps = ps->next) */
443 }
445 /* put all pre-defined 'Process' names from config to list_head_g tree */
446 static int ps_config (const char *key, const char *value)
447 {
448 if (strcasecmp (key, "Process") == 0)
449 {
450 ps_list_register (value, NULL);
451 }
452 else if (strcasecmp (key, "ProcessMatch") == 0)
453 {
454 char *new_val;
455 char *fields[3];
456 int fields_num;
458 new_val = strdup (value);
459 if (new_val == NULL)
460 return (1);
461 fields_num = strsplit (new_val, fields,
462 STATIC_ARRAY_SIZE (fields));
463 if (fields_num != 2)
464 {
465 sfree (new_val);
466 return (1);
467 }
468 ps_list_register (fields[0], fields[1]);
469 sfree (new_val);
470 }
471 else
472 {
473 return (-1);
474 }
476 return (0);
477 }
479 static int ps_init (void)
480 {
481 #if HAVE_THREAD_INFO
482 kern_return_t status;
484 port_host_self = mach_host_self ();
485 port_task_self = mach_task_self ();
487 if (pset_list != NULL)
488 {
489 vm_deallocate (port_task_self,
490 (vm_address_t) pset_list,
491 pset_list_len * sizeof (processor_set_t));
492 pset_list = NULL;
493 pset_list_len = 0;
494 }
496 if ((status = host_processor_sets (port_host_self,
497 &pset_list,
498 &pset_list_len)) != KERN_SUCCESS)
499 {
500 ERROR ("host_processor_sets failed: %s\n",
501 mach_error_string (status));
502 pset_list = NULL;
503 pset_list_len = 0;
504 return (-1);
505 }
506 /* #endif HAVE_THREAD_INFO */
508 #elif KERNEL_LINUX
509 pagesize_g = sysconf(_SC_PAGESIZE);
510 DEBUG ("pagesize_g = %li; CONFIG_HZ = %i;",
511 pagesize_g, CONFIG_HZ);
512 #endif /* KERNEL_LINUX */
514 return (0);
515 } /* int ps_init */
517 /* submit global state (e.g.: qty of zombies, running, etc..) */
518 static void ps_submit_state (const char *state, double value)
519 {
520 value_t values[1];
521 value_list_t vl = VALUE_LIST_INIT;
523 values[0].gauge = value;
525 vl.values = values;
526 vl.values_len = 1;
527 vl.time = time (NULL);
528 strcpy (vl.host, hostname_g);
529 strcpy (vl.plugin, "processes");
530 strcpy (vl.plugin_instance, "");
531 strcpy (vl.type, "ps_state");
532 sstrncpy (vl.type_instance, state, sizeof (vl.type_instance));
534 plugin_dispatch_values (&vl);
535 }
537 /* submit info about specific process (e.g.: memory taken, cpu usage, etc..) */
538 static void ps_submit_proc_list (procstat_t *ps)
539 {
540 value_t values[2];
541 value_list_t vl = VALUE_LIST_INIT;
543 vl.values = values;
544 vl.values_len = 2;
545 vl.time = time (NULL);
546 strcpy (vl.host, hostname_g);
547 strcpy (vl.plugin, "processes");
548 sstrncpy (vl.plugin_instance, ps->name, sizeof (vl.plugin_instance));
550 strcpy (vl.type, "ps_rss");
551 vl.values[0].gauge = ps->vmem_rss;
552 vl.values_len = 1;
553 plugin_dispatch_values (&vl);
555 strcpy (vl.type, "ps_cputime");
556 vl.values[0].counter = ps->cpu_user_counter;
557 vl.values[1].counter = ps->cpu_system_counter;
558 vl.values_len = 2;
559 plugin_dispatch_values (&vl);
561 strcpy (vl.type, "ps_count");
562 vl.values[0].gauge = ps->num_proc;
563 vl.values[1].gauge = ps->num_lwp;
564 vl.values_len = 2;
565 plugin_dispatch_values (&vl);
567 strcpy (vl.type, "ps_pagefaults");
568 vl.values[0].counter = ps->vmem_minflt_counter;
569 vl.values[1].counter = ps->vmem_majflt_counter;
570 vl.values_len = 2;
571 plugin_dispatch_values (&vl);
573 DEBUG ("name = %s; num_proc = %lu; num_lwp = %lu; vmem_rss = %lu; "
574 "vmem_minflt_counter = %lu; vmem_majflt_counter = %lu; "
575 "cpu_user_counter = %lu; cpu_system_counter = %lu;",
576 ps->name, ps->num_proc, ps->num_lwp, ps->vmem_rss,
577 ps->vmem_minflt_counter, ps->vmem_majflt_counter,
578 ps->cpu_user_counter, ps->cpu_system_counter);
579 } /* void ps_submit_proc_list */
581 /* ------- additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
582 #if KERNEL_LINUX
583 static int *ps_read_tasks (int pid)
584 {
585 int *list = NULL;
586 int list_size = 1; /* size of allocated space, in elements */
587 int list_len = 0; /* number of currently used elements */
589 char dirname[64];
590 DIR *dh;
591 struct dirent *ent;
593 ssnprintf (dirname, sizeof (dirname), "/proc/%i/task", pid);
595 if ((dh = opendir (dirname)) == NULL)
596 {
597 DEBUG ("Failed to open directory `%s'", dirname);
598 return (NULL);
599 }
601 while ((ent = readdir (dh)) != NULL)
602 {
603 if (!isdigit (ent->d_name[0]))
604 continue;
606 if ((list_len + 1) >= list_size)
607 {
608 int *new_ptr;
609 int new_size = 2 * list_size;
610 /* Comes in sizes: 2, 4, 8, 16, ... */
612 new_ptr = (int *) realloc (list, (size_t) (sizeof (int) * new_size));
613 if (new_ptr == NULL)
614 {
615 if (list != NULL)
616 free (list);
617 ERROR ("processes plugin: "
618 "Failed to allocate more memory.");
619 return (NULL);
620 }
622 list = new_ptr;
623 list_size = new_size;
625 memset (list + list_len, 0, sizeof (int) * (list_size - list_len));
626 }
628 list[list_len] = atoi (ent->d_name);
629 if (list[list_len] != 0)
630 list_len++;
631 }
633 closedir (dh);
635 if (list_len == 0)
636 return (NULL);
638 assert (list_len < list_size);
639 assert (list[list_len] == 0);
641 return (list);
642 } /* int *ps_read_tasks */
644 int ps_read_process (int pid, procstat_t *ps, char *state)
645 {
646 char filename[64];
647 char buffer[1024];
648 FILE *fh;
650 char *fields[64];
651 char fields_len;
653 int *tasks;
654 int i;
656 int ppid;
657 int name_len;
659 long long unsigned cpu_user_counter;
660 long long unsigned cpu_system_counter;
661 long long unsigned vmem_rss;
663 memset (ps, 0, sizeof (procstat_t));
665 ssnprintf (filename, sizeof (filename), "/proc/%i/stat", pid);
667 if ((fh = fopen (filename, "r")) == NULL)
668 return (-1);
670 if (fgets (buffer, 1024, fh) == NULL)
671 {
672 fclose (fh);
673 return (-1);
674 }
676 fclose (fh);
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 if (ps_list_match(task_name, NULL, ps) == 1) //!!! cmdline should be here instead of NULL
861 break;
862 }
864 /* Collect more detailed statistics for this process */
865 if (ps != NULL)
866 {
867 task_basic_info_data_t task_basic_info;
868 mach_msg_type_number_t task_basic_info_len;
869 task_events_info_data_t task_events_info;
870 mach_msg_type_number_t task_events_info_len;
871 task_absolutetime_info_data_t task_absolutetime_info;
872 mach_msg_type_number_t task_absolutetime_info_len;
874 memset (&pse, '\0', sizeof (pse));
875 pse.id = task_pid;
877 task_basic_info_len = TASK_BASIC_INFO_COUNT;
878 status = task_info (task_list[task],
879 TASK_BASIC_INFO,
880 (task_info_t) &task_basic_info,
881 &task_basic_info_len);
882 if (status != KERN_SUCCESS)
883 {
884 ERROR ("task_info failed: %s",
885 mach_error_string (status));
886 continue; /* with next thread_list */
887 }
889 task_events_info_len = TASK_EVENTS_INFO_COUNT;
890 status = task_info (task_list[task],
891 TASK_EVENTS_INFO,
892 (task_info_t) &task_events_info,
893 &task_events_info_len);
894 if (status != KERN_SUCCESS)
895 {
896 ERROR ("task_info failed: %s",
897 mach_error_string (status));
898 continue; /* with next thread_list */
899 }
901 task_absolutetime_info_len = TASK_ABSOLUTETIME_INFO_COUNT;
902 status = task_info (task_list[task],
903 TASK_ABSOLUTETIME_INFO,
904 (task_info_t) &task_absolutetime_info,
905 &task_absolutetime_info_len);
906 if (status != KERN_SUCCESS)
907 {
908 ERROR ("task_info failed: %s",
909 mach_error_string (status));
910 continue; /* with next thread_list */
911 }
913 pse.num_proc++;
914 pse.vmem_rss = task_basic_info.resident_size;
916 pse.vmem_minflt_counter = task_events_info.cow_faults;
917 pse.vmem_majflt_counter = task_events_info.faults;
919 pse.cpu_user_counter = task_absolutetime_info.total_user;
920 pse.cpu_system_counter = task_absolutetime_info.total_system;
921 }
923 status = task_threads (task_list[task], &thread_list,
924 &thread_list_len);
925 if (status != KERN_SUCCESS)
926 {
927 /* Apple's `top' treats this case a zombie. It
928 * makes sense to some extend: A `zombie'
929 * thread is nonsense, since the task/process
930 * is dead. */
931 zombies++;
932 DEBUG ("task_threads failed: %s",
933 mach_error_string (status));
934 if (task_list[task] != port_task_self)
935 mach_port_deallocate (port_task_self,
936 task_list[task]);
937 continue; /* with next task_list */
938 }
940 for (thread = 0; thread < thread_list_len; thread++)
941 {
942 thread_data_len = THREAD_BASIC_INFO_COUNT;
943 status = thread_info (thread_list[thread],
944 THREAD_BASIC_INFO,
945 (thread_info_t) &thread_data,
946 &thread_data_len);
947 if (status != KERN_SUCCESS)
948 {
949 ERROR ("thread_info failed: %s",
950 mach_error_string (status));
951 if (task_list[task] != port_task_self)
952 mach_port_deallocate (port_task_self,
953 thread_list[thread]);
954 continue; /* with next thread_list */
955 }
957 if (ps != NULL)
958 pse.num_lwp++;
960 switch (thread_data.run_state)
961 {
962 case TH_STATE_RUNNING:
963 running++;
964 break;
965 case TH_STATE_STOPPED:
966 /* What exactly is `halted'? */
967 case TH_STATE_HALTED:
968 stopped++;
969 break;
970 case TH_STATE_WAITING:
971 sleeping++;
972 break;
973 case TH_STATE_UNINTERRUPTIBLE:
974 blocked++;
975 break;
976 /* There is no `zombie' case here,
977 * since there are no zombie-threads.
978 * There's only zombie tasks, which are
979 * handled above. */
980 default:
981 WARNING ("Unknown thread status: %s",
982 thread_data.run_state);
983 break;
984 } /* switch (thread_data.run_state) */
986 if (task_list[task] != port_task_self)
987 {
988 status = mach_port_deallocate (port_task_self,
989 thread_list[thread]);
990 if (status != KERN_SUCCESS)
991 ERROR ("mach_port_deallocate failed: %s",
992 mach_error_string (status));
993 }
994 } /* for (thread_list) */
996 if ((status = vm_deallocate (port_task_self,
997 (vm_address_t) thread_list,
998 thread_list_len * sizeof (thread_act_t)))
999 != KERN_SUCCESS)
1000 {
1001 ERROR ("vm_deallocate failed: %s",
1002 mach_error_string (status));
1003 }
1004 thread_list = NULL;
1005 thread_list_len = 0;
1007 /* Only deallocate the task port, if it isn't our own.
1008 * Don't know what would happen in that case, but this
1009 * is what Apple's top does.. ;) */
1010 if (task_list[task] != port_task_self)
1011 {
1012 status = mach_port_deallocate (port_task_self,
1013 task_list[task]);
1014 if (status != KERN_SUCCESS)
1015 ERROR ("mach_port_deallocate failed: %s",
1016 mach_error_string (status));
1017 }
1019 if (ps != NULL)
1020 ps_list_add (task_name, NULL, &pse); //!!! cmdline should be here instead of NULL
1021 } /* for (task_list) */
1023 if ((status = vm_deallocate (port_task_self,
1024 (vm_address_t) task_list,
1025 task_list_len * sizeof (task_t))) != KERN_SUCCESS)
1026 {
1027 ERROR ("vm_deallocate failed: %s",
1028 mach_error_string (status));
1029 }
1030 task_list = NULL;
1031 task_list_len = 0;
1033 if ((status = mach_port_deallocate (port_task_self, port_pset_priv))
1034 != KERN_SUCCESS)
1035 {
1036 ERROR ("mach_port_deallocate failed: %s",
1037 mach_error_string (status));
1038 }
1039 } /* for (pset_list) */
1041 ps_submit_state ("running", running);
1042 ps_submit_state ("sleeping", sleeping);
1043 ps_submit_state ("zombies", zombies);
1044 ps_submit_state ("stopped", stopped);
1045 ps_submit_state ("blocked", blocked);
1047 for (ps = list_head_g; ps != NULL; ps = ps->next)
1048 ps_submit_proc_list (ps);
1049 /* #endif HAVE_THREAD_INFO */
1051 #elif KERNEL_LINUX
1052 int running = 0;
1053 int sleeping = 0;
1054 int zombies = 0;
1055 int stopped = 0;
1056 int paging = 0;
1057 int blocked = 0;
1059 struct dirent *ent;
1060 DIR *proc;
1061 int pid;
1063 int status;
1064 procstat_t ps;
1065 procstat_entry_t pse;
1066 char state;
1068 procstat_t *ps_ptr;
1070 running = sleeping = zombies = stopped = paging = blocked = 0;
1071 ps_list_reset ();
1073 if ((proc = opendir ("/proc")) == NULL)
1074 {
1075 char errbuf[1024];
1076 ERROR ("Cannot open `/proc': %s",
1077 sstrerror (errno, errbuf, sizeof (errbuf)));
1078 return (-1);
1079 }
1081 while ((ent = readdir (proc)) != NULL)
1082 {
1083 if (!isdigit (ent->d_name[0]))
1084 continue;
1086 if ((pid = atoi (ent->d_name)) < 1)
1087 continue;
1089 status = ps_read_process (pid, &ps, &state);
1090 if (status != 0)
1091 {
1092 DEBUG ("ps_read_process failed: %i", status);
1093 continue;
1094 }
1096 pse.id = pid;
1097 pse.age = 0;
1099 pse.num_proc = ps.num_proc;
1100 pse.num_lwp = ps.num_lwp;
1101 pse.vmem_rss = ps.vmem_rss;
1103 pse.vmem_minflt = 0;
1104 pse.vmem_minflt_counter = ps.vmem_minflt_counter;
1105 pse.vmem_majflt = 0;
1106 pse.vmem_majflt_counter = ps.vmem_majflt_counter;
1108 pse.cpu_user = 0;
1109 pse.cpu_user_counter = ps.cpu_user_counter;
1110 pse.cpu_system = 0;
1111 pse.cpu_system_counter = ps.cpu_system_counter;
1113 switch (state)
1114 {
1115 case 'R': running++; break;
1116 case 'S': sleeping++; break;
1117 case 'D': blocked++; break;
1118 case 'Z': zombies++; break;
1119 case 'T': stopped++; break;
1120 case 'W': paging++; break;
1121 }
1123 ps_list_add (ps.name, NULL, &pse); //!!! cmdline should be here instead of NULL
1124 }
1126 closedir (proc);
1128 ps_submit_state ("running", running);
1129 ps_submit_state ("sleeping", sleeping);
1130 ps_submit_state ("zombies", zombies);
1131 ps_submit_state ("stopped", stopped);
1132 ps_submit_state ("paging", paging);
1133 ps_submit_state ("blocked", blocked);
1135 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1136 ps_submit_proc_list (ps_ptr);
1137 /* #endif KERNEL_LINUX */
1139 #elif HAVE_LIBKVM
1140 int running = 0;
1141 int sleeping = 0;
1142 int zombies = 0;
1143 int stopped = 0;
1144 int blocked = 0;
1145 int idle = 0;
1146 int wait = 0;
1148 kvm_t *kd;
1149 char errbuf[1024];
1150 char cmdline[ARG_MAX];
1151 struct kinfo_proc *procs; /* array of processes */
1152 char ** argv;
1153 int count; /* returns number of processes */
1154 int i, j;
1156 procstat_t *ps_ptr;
1157 procstat_entry_t pse;
1159 ps_list_reset ();
1161 /* Open the kvm interface, get a descriptor */
1162 if ((kd = kvm_open(NULL, NULL, NULL, 0, errbuf)) == NULL) {
1163 ERROR ("Cannot open kvm interface: %s", errbuf);
1164 return (0);
1165 }
1167 /* Get the list of processes. */
1168 if ((procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, &count)) == NULL) {
1169 kvm_close(kd);
1170 ERROR ("Cannot get kvm processes list: %s", kvm_geterr(kd));
1171 return (0);
1172 }
1174 /* Iterate through the processes in kinfo_proc */
1175 for (i=0; i < count; i++) {
1176 // retrieve the arguments
1177 *cmdline = '\0';
1178 argv = kvm_getargv(kd, (const struct kinfo_proc *) &(procs[i]), 0);
1179 if (argv) {
1180 j = 0;
1181 while (argv[j] && strlen(cmdline) <= ARG_MAX) {
1182 if (j)
1183 strncat(cmdline, " ", 1);
1184 strncat(cmdline, argv[j], strlen(argv[j]));
1185 j++;
1186 }
1187 }
1189 pse.id = procs[i].ki_pid;
1190 pse.age = 0;
1192 pse.num_proc = 1;
1193 pse.num_lwp = procs[i].ki_numthreads;
1195 pse.vmem_rss = procs[i].ki_rssize * getpagesize();
1196 pse.vmem_minflt = 0;
1197 pse.vmem_minflt_counter = procs[i].ki_rusage.ru_minflt;
1198 pse.vmem_majflt = 0;
1199 pse.vmem_majflt_counter = procs[i].ki_rusage.ru_majflt;
1201 pse.cpu_user = 0;
1202 pse.cpu_user_counter = procs[i].ki_rusage.ru_utime.tv_sec*1000 + procs[i].ki_rusage.ru_utime.tv_usec;
1203 pse.cpu_system = 0;
1204 pse.cpu_system_counter = procs[i].ki_rusage.ru_stime.tv_sec*1000 + procs[i].ki_rusage.ru_stime.tv_usec;
1206 switch (procs[i].ki_stat) {
1207 case SSTOP: stopped++; break;
1208 case SSLEEP: sleeping++; break;
1209 case SRUN: running++; break;
1210 case SIDL: idle++; break;
1211 case SWAIT: wait++; break;
1212 case SLOCK: blocked++; break;
1213 case SZOMB: zombies++; break;
1214 }
1216 ps_list_add (procs[i].ki_comm, cmdline, &pse);
1217 }
1219 if (kd) kvm_close(kd);
1221 ps_submit_state ("running", running);
1222 ps_submit_state ("sleeping", sleeping);
1223 ps_submit_state ("zombies", zombies);
1224 ps_submit_state ("stopped", stopped);
1225 ps_submit_state ("blocked", blocked);
1226 ps_submit_state ("idle", idle);
1227 ps_submit_state ("wait", wait);
1229 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1230 ps_submit_proc_list (ps_ptr);
1232 #endif /* HAVE_LIBKVM */
1234 return (0);
1235 } /* int ps_read */
1237 void module_register (void)
1238 {
1239 plugin_register_config ("processes", ps_config,
1240 config_keys, config_keys_num);
1241 plugin_register_init ("processes", ps_init);
1242 plugin_register_read ("processes", ps_read);
1243 } /* void module_register */