b022b461e84d07280666ae6020b93b5073dc84ff
1 /**
2 * collectd - src/processes.c
3 * Copyright (C) 2005 Lyonel Vincent
4 * Copyright (C) 2006-2007 Florian Forster (Mach code)
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 * Authors:
21 * Lyonel Vincent <lyonel at ezix.org>
22 * Florian octo Forster <octo at verplant.org>
23 **/
25 #include "collectd.h"
26 #include "common.h"
27 #include "plugin.h"
28 #include "configfile.h"
30 /* Include header files for the mach system, if they exist.. */
31 #if HAVE_THREAD_INFO
32 # if HAVE_MACH_MACH_INIT_H
33 # include <mach/mach_init.h>
34 # endif
35 # if HAVE_MACH_HOST_PRIV_H
36 # include <mach/host_priv.h>
37 # endif
38 # if HAVE_MACH_MACH_ERROR_H
39 # include <mach/mach_error.h>
40 # endif
41 # if HAVE_MACH_MACH_HOST_H
42 # include <mach/mach_host.h>
43 # endif
44 # if HAVE_MACH_MACH_PORT_H
45 # include <mach/mach_port.h>
46 # endif
47 # if HAVE_MACH_MACH_TYPES_H
48 # include <mach/mach_types.h>
49 # endif
50 # if HAVE_MACH_MESSAGE_H
51 # include <mach/message.h>
52 # endif
53 # if HAVE_MACH_PROCESSOR_SET_H
54 # include <mach/processor_set.h>
55 # endif
56 # if HAVE_MACH_TASK_H
57 # include <mach/task.h>
58 # endif
59 # if HAVE_MACH_THREAD_ACT_H
60 # include <mach/thread_act.h>
61 # endif
62 # if HAVE_MACH_VM_REGION_H
63 # include <mach/vm_region.h>
64 # endif
65 # if HAVE_MACH_VM_MAP_H
66 # include <mach/vm_map.h>
67 # endif
68 # if HAVE_MACH_VM_PROT_H
69 # include <mach/vm_prot.h>
70 # endif
71 # if HAVE_SYS_SYSCTL_H
72 # include <sys/sysctl.h>
73 # endif
74 /* #endif HAVE_THREAD_INFO */
76 #elif KERNEL_LINUX
77 # if HAVE_LINUX_CONFIG_H
78 # include <linux/config.h>
79 # endif
80 # ifndef CONFIG_HZ
81 # define CONFIG_HZ 100
82 # endif
83 /* #endif KERNEL_LINUX */
85 #elif HAVE_KVM_H
86 # include <kvm.h>
87 # include <sys/user.h>
88 # include <sys/proc.h>
89 # if HAVE_SYS_SYSCTL_H
90 # include <sys/sysctl.h>
91 # endif
92 /* #endif HAVE_KVM_H */
94 #else
95 # error "No applicable input method."
96 #endif
98 #if HAVE_REGEX_H
99 # include <regex.h>
100 #endif
102 #define BUFSIZE 256
104 static const char *config_keys[] =
105 {
106 "Process",
107 "ProcessMatch",
108 NULL
109 };
110 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
112 typedef struct procstat_entry_s
113 {
114 unsigned long id;
115 unsigned long age;
117 unsigned long num_proc;
118 unsigned long num_lwp;
119 unsigned long vmem_rss;
121 unsigned long vmem_minflt;
122 unsigned long vmem_majflt;
123 unsigned long vmem_minflt_counter;
124 unsigned long vmem_majflt_counter;
126 unsigned long cpu_user;
127 unsigned long cpu_system;
128 unsigned long cpu_user_counter;
129 unsigned long cpu_system_counter;
131 struct procstat_entry_s *next;
132 } procstat_entry_t;
134 #define PROCSTAT_NAME_LEN 256
135 typedef struct procstat
136 {
137 char name[PROCSTAT_NAME_LEN];
138 #if HAVE_REGEX_H
139 regex_t *re;
140 #endif
142 unsigned long num_proc;
143 unsigned long num_lwp;
144 unsigned long vmem_rss;
146 unsigned long vmem_minflt_counter;
147 unsigned long vmem_majflt_counter;
149 unsigned long cpu_user_counter;
150 unsigned long cpu_system_counter;
152 struct procstat *next;
153 struct procstat_entry_s *instances;
154 } procstat_t;
156 static procstat_t *list_head_g = NULL;
158 #if HAVE_THREAD_INFO
159 static mach_port_t port_host_self;
160 static mach_port_t port_task_self;
162 static processor_set_name_array_t pset_list;
163 static mach_msg_type_number_t pset_list_len;
164 /* #endif HAVE_THREAD_INFO */
166 #elif KERNEL_LINUX
167 static long pagesize_g;
168 #endif /* KERNEL_LINUX */
170 /* put name of process from config to list_head_g tree
171 list_head_g is a list of 'procstat_t' structs with
172 processes names we want to watch */
173 static void ps_list_register (const char *name, const char *regexp)
174 {
175 procstat_t *new;
176 procstat_t *ptr;
177 int status;
179 new = (procstat_t *) malloc (sizeof (procstat_t));
180 if (new == NULL)
181 {
182 ERROR ("processes plugin: ps_list_register: malloc failed.");
183 return;
184 }
185 memset (new, 0, sizeof (procstat_t));
186 sstrncpy (new->name, name, sizeof (new->name));
188 #if HAVE_REGEX_H
189 if (regexp != NULL)
190 {
191 DEBUG ("ProcessMatch: adding \"%s\" as criteria to process %s.", regexp, name);
192 new->re = (regex_t *) malloc (sizeof (regex_t));
193 if (new->re == NULL)
194 {
195 ERROR ("processes plugin: ps_list_register: malloc failed.");
196 sfree (new);
197 return;
198 }
200 status = regcomp (new->re, regexp, REG_EXTENDED | REG_NOSUB);
201 if (status != 0)
202 {
203 DEBUG ("ProcessMatch: compiling the regular expression \"%s\" failed.", regexp);
204 sfree(new->re);
205 return;
206 }
207 }
208 #else
209 if (regexp != NULL)
210 {
211 ERROR ("processes plugin: ps_list_register: "
212 "Regular expression \"%s\" found in config "
213 "file, but support for regular expressions "
214 "has been dispabled at compile time.",
215 regexp);
216 sfree (new);
217 return;
218 }
219 #endif
221 for (ptr = list_head_g; ptr != NULL; ptr = ptr->next)
222 {
223 if (strcmp (ptr->name, name) == 0)
224 {
225 WARNING ("processes plugin: You have configured more "
226 "than one `Process' or "
227 "`ProcessMatch' with the same name. "
228 "All but the first setting will be "
229 "ignored.");
230 sfree (new->re);
231 sfree (new);
232 return;
233 }
235 if (ptr->next == NULL)
236 break;
237 }
239 if (ptr == NULL)
240 list_head_g = new;
241 else
242 ptr->next = new;
243 } /* void ps_list_register */
245 /* try to match name against entry, returns 1 if success */
246 static int ps_list_match (const char *name, const char *cmdline, procstat_t *ps)
247 {
248 if ((ps->re != NULL) && (regexec(ps->re, (strlen(cmdline)!=0)?cmdline:name, 0, NULL, 0) == 0))
249 return (1);
250 if (strcmp (ps->name, name) == 0) {
251 return (1);
252 }
253 return (0);
254 }
256 /* add process entry to 'instances' of process 'name' (or refresh it) */
257 static void ps_list_add (const char *name, const char *cmdline, procstat_entry_t *entry)
258 {
259 procstat_t *ps;
260 procstat_entry_t *pse;
262 if (entry->id == 0)
263 return;
265 for (ps = list_head_g; ps != NULL; ps = ps->next)
266 {
268 if ((ps_list_match (name, cmdline, ps)) == 0)
269 continue;
271 for (pse = ps->instances; pse != NULL; pse = pse->next)
272 if ((pse->id == entry->id) || (pse->next == NULL))
273 break;
275 if ((pse == NULL) || (pse->id != entry->id))
276 {
277 procstat_entry_t *new;
279 new = (procstat_entry_t *) malloc (sizeof (procstat_entry_t));
280 if (new == NULL)
281 return;
282 memset (new, 0, sizeof (procstat_entry_t));
283 new->id = entry->id;
285 if (pse == NULL)
286 ps->instances = new;
287 else
288 pse->next = new;
290 pse = new;
291 }
293 pse->age = 0;
294 pse->num_proc = entry->num_proc;
295 pse->num_lwp = entry->num_lwp;
296 pse->vmem_rss = entry->vmem_rss;
298 ps->num_proc += pse->num_proc;
299 ps->num_lwp += pse->num_lwp;
300 ps->vmem_rss += pse->vmem_rss;
302 if ((entry->vmem_minflt_counter == 0)
303 && (entry->vmem_majflt_counter == 0))
304 {
305 pse->vmem_minflt_counter += entry->vmem_minflt;
306 pse->vmem_minflt = entry->vmem_minflt;
308 pse->vmem_majflt_counter += entry->vmem_majflt;
309 pse->vmem_majflt = entry->vmem_majflt;
310 }
311 else
312 {
313 if (entry->vmem_minflt_counter < pse->vmem_minflt_counter)
314 {
315 pse->vmem_minflt = entry->vmem_minflt_counter
316 + (ULONG_MAX - pse->vmem_minflt_counter);
317 }
318 else
319 {
320 pse->vmem_minflt = entry->vmem_minflt_counter - pse->vmem_minflt_counter;
321 }
322 pse->vmem_minflt_counter = entry->vmem_minflt_counter;
324 if (entry->vmem_majflt_counter < pse->vmem_majflt_counter)
325 {
326 pse->vmem_majflt = entry->vmem_majflt_counter
327 + (ULONG_MAX - pse->vmem_majflt_counter);
328 }
329 else
330 {
331 pse->vmem_majflt = entry->vmem_majflt_counter - pse->vmem_majflt_counter;
332 }
333 pse->vmem_majflt_counter = entry->vmem_majflt_counter;
334 }
336 ps->vmem_minflt_counter += pse->vmem_minflt;
337 ps->vmem_majflt_counter += pse->vmem_majflt;
339 if ((entry->cpu_user_counter == 0)
340 && (entry->cpu_system_counter == 0))
341 {
342 pse->cpu_user_counter += entry->cpu_user;
343 pse->cpu_user = entry->cpu_user;
345 pse->cpu_system_counter += entry->cpu_system;
346 pse->cpu_system = entry->cpu_system;
347 }
348 else
349 {
350 if (entry->cpu_user_counter < pse->cpu_user_counter)
351 {
352 pse->cpu_user = entry->cpu_user_counter
353 + (ULONG_MAX - pse->cpu_user_counter);
354 }
355 else
356 {
357 pse->cpu_user = entry->cpu_user_counter - pse->cpu_user_counter;
358 }
359 pse->cpu_user_counter = entry->cpu_user_counter;
361 if (entry->cpu_system_counter < pse->cpu_system_counter)
362 {
363 pse->cpu_system = entry->cpu_system_counter
364 + (ULONG_MAX - pse->cpu_system_counter);
365 }
366 else
367 {
368 pse->cpu_system = entry->cpu_system_counter - pse->cpu_system_counter;
369 }
370 pse->cpu_system_counter = entry->cpu_system_counter;
371 }
373 ps->cpu_user_counter += pse->cpu_user;
374 ps->cpu_system_counter += pse->cpu_system;
375 }
376 }
378 /* remove old entries from instances of processes in list_head_g */
379 static void ps_list_reset (void)
380 {
381 procstat_t *ps;
382 procstat_entry_t *pse;
383 procstat_entry_t *pse_prev;
385 for (ps = list_head_g; ps != NULL; ps = ps->next)
386 {
387 ps->num_proc = 0;
388 ps->num_lwp = 0;
389 ps->vmem_rss = 0;
391 pse_prev = NULL;
392 pse = ps->instances;
393 while (pse != NULL)
394 {
395 if (pse->age > 10)
396 {
397 DEBUG ("Removing this procstat entry cause it's too old: "
398 "id = %lu; name = %s;",
399 pse->id, ps->name);
401 if (pse_prev == NULL)
402 {
403 ps->instances = pse->next;
404 free (pse);
405 pse = ps->instances;
406 }
407 else
408 {
409 pse_prev->next = pse->next;
410 free (pse);
411 pse = pse_prev->next;
412 }
413 }
414 else
415 {
416 pse->age++;
417 pse_prev = pse;
418 pse = pse->next;
419 }
420 } /* while (pse != NULL) */
421 } /* for (ps = list_head_g; ps != NULL; ps = ps->next) */
422 }
424 /* put all pre-defined 'Process' names from config to list_head_g tree */
425 static int ps_config (const char *key, const char *value)
426 {
427 char *new_val;
428 char *fields[2];
429 int fields_num;
431 if (strcasecmp (key, "Process") == 0)
432 {
433 ps_list_register (value, NULL);
434 return (0);
435 }
437 if (strcasecmp (key, "ProcessMatch") == 0)
438 {
439 new_val = strdup (value);
440 if (new_val == NULL)
441 return (-1);
442 fields_num = strsplit (new_val, fields, 2);
443 if (fields_num != 2)
444 {
445 sfree (new_val);
446 return (-1);
447 }
448 ps_list_register (fields[0], fields[1]);
449 sfree (new_val);
450 return (0);
451 }
453 return (-1);
454 }
456 static int ps_init (void)
457 {
458 #if HAVE_THREAD_INFO
459 kern_return_t status;
461 port_host_self = mach_host_self ();
462 port_task_self = mach_task_self ();
464 if (pset_list != NULL)
465 {
466 vm_deallocate (port_task_self,
467 (vm_address_t) pset_list,
468 pset_list_len * sizeof (processor_set_t));
469 pset_list = NULL;
470 pset_list_len = 0;
471 }
473 if ((status = host_processor_sets (port_host_self,
474 &pset_list,
475 &pset_list_len)) != KERN_SUCCESS)
476 {
477 ERROR ("host_processor_sets failed: %s\n",
478 mach_error_string (status));
479 pset_list = NULL;
480 pset_list_len = 0;
481 return (-1);
482 }
483 /* #endif HAVE_THREAD_INFO */
485 #elif KERNEL_LINUX
486 pagesize_g = sysconf(_SC_PAGESIZE);
487 DEBUG ("pagesize_g = %li; CONFIG_HZ = %i;",
488 pagesize_g, CONFIG_HZ);
489 #endif /* KERNEL_LINUX */
491 return (0);
492 } /* int ps_init */
494 /* submit global state (e.g.: qty of zombies, running, etc..) */
495 static void ps_submit_state (const char *state, double value)
496 {
497 value_t values[1];
498 value_list_t vl = VALUE_LIST_INIT;
500 values[0].gauge = value;
502 vl.values = values;
503 vl.values_len = 1;
504 vl.time = time (NULL);
505 strcpy (vl.host, hostname_g);
506 strcpy (vl.plugin, "processes");
507 strcpy (vl.plugin_instance, "");
508 strcpy (vl.type, "ps_state");
509 sstrncpy (vl.type_instance, state, sizeof (vl.type_instance));
511 plugin_dispatch_values (&vl);
512 }
514 /* submit info about specific process (e.g.: memory taken, cpu usage, etc..) */
515 static void ps_submit_proc_list (procstat_t *ps)
516 {
517 value_t values[2];
518 value_list_t vl = VALUE_LIST_INIT;
520 vl.values = values;
521 vl.values_len = 2;
522 vl.time = time (NULL);
523 strcpy (vl.host, hostname_g);
524 strcpy (vl.plugin, "processes");
525 sstrncpy (vl.plugin_instance, ps->name, sizeof (vl.plugin_instance));
527 strcpy (vl.type, "ps_rss");
528 vl.values[0].gauge = ps->vmem_rss;
529 vl.values_len = 1;
530 plugin_dispatch_values (&vl);
532 strcpy (vl.type, "ps_cputime");
533 vl.values[0].counter = ps->cpu_user_counter;
534 vl.values[1].counter = ps->cpu_system_counter;
535 vl.values_len = 2;
536 plugin_dispatch_values (&vl);
538 strcpy (vl.type, "ps_count");
539 vl.values[0].gauge = ps->num_proc;
540 vl.values[1].gauge = ps->num_lwp;
541 vl.values_len = 2;
542 plugin_dispatch_values (&vl);
544 strcpy (vl.type, "ps_pagefaults");
545 vl.values[0].counter = ps->vmem_minflt_counter;
546 vl.values[1].counter = ps->vmem_majflt_counter;
547 vl.values_len = 2;
548 plugin_dispatch_values (&vl);
550 DEBUG ("name = %s; num_proc = %lu; num_lwp = %lu; vmem_rss = %lu; "
551 "vmem_minflt_counter = %lu; vmem_majflt_counter = %lu; "
552 "cpu_user_counter = %lu; cpu_system_counter = %lu;",
553 ps->name, ps->num_proc, ps->num_lwp, ps->vmem_rss,
554 ps->vmem_minflt_counter, ps->vmem_majflt_counter,
555 ps->cpu_user_counter, ps->cpu_system_counter);
556 } /* void ps_submit_proc_list */
558 /* ------- additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
559 #if KERNEL_LINUX
560 static int *ps_read_tasks (int pid)
561 {
562 int *list = NULL;
563 int list_size = 1; /* size of allocated space, in elements */
564 int list_len = 0; /* number of currently used elements */
566 char dirname[64];
567 DIR *dh;
568 struct dirent *ent;
570 ssnprintf (dirname, sizeof (dirname), "/proc/%i/task", pid);
572 if ((dh = opendir (dirname)) == NULL)
573 {
574 DEBUG ("Failed to open directory `%s'", dirname);
575 return (NULL);
576 }
578 while ((ent = readdir (dh)) != NULL)
579 {
580 if (!isdigit (ent->d_name[0]))
581 continue;
583 if ((list_len + 1) >= list_size)
584 {
585 int *new_ptr;
586 int new_size = 2 * list_size;
587 /* Comes in sizes: 2, 4, 8, 16, ... */
589 new_ptr = (int *) realloc (list, (size_t) (sizeof (int) * new_size));
590 if (new_ptr == NULL)
591 {
592 if (list != NULL)
593 free (list);
594 ERROR ("processes plugin: "
595 "Failed to allocate more memory.");
596 return (NULL);
597 }
599 list = new_ptr;
600 list_size = new_size;
602 memset (list + list_len, 0, sizeof (int) * (list_size - list_len));
603 }
605 list[list_len] = atoi (ent->d_name);
606 if (list[list_len] != 0)
607 list_len++;
608 }
610 closedir (dh);
612 if (list_len == 0)
613 return (NULL);
615 assert (list_len < list_size);
616 assert (list[list_len] == 0);
618 return (list);
619 } /* int *ps_read_tasks */
621 int ps_read_process (int pid, procstat_t *ps, char *state)
622 {
623 char filename[64];
624 char buffer[1024];
625 FILE *fh;
627 char *fields[64];
628 char fields_len;
630 int *tasks;
631 int i;
633 int ppid;
634 int name_len;
636 long long unsigned cpu_user_counter;
637 long long unsigned cpu_system_counter;
638 long long unsigned vmem_rss;
640 memset (ps, 0, sizeof (procstat_t));
642 ssnprintf (filename, sizeof (filename), "/proc/%i/stat", pid);
644 if ((fh = fopen (filename, "r")) == NULL)
645 return (-1);
647 if (fgets (buffer, 1024, fh) == NULL)
648 {
649 fclose (fh);
650 return (-1);
651 }
653 fclose (fh);
655 fields_len = strsplit (buffer, fields, 64);
656 if (fields_len < 24)
657 {
658 DEBUG ("processes plugin: ps_read_process (pid = %i):"
659 " `%s' has only %i fields..",
660 (int) pid, filename, fields_len);
661 return (-1);
662 }
664 /* copy the name, strip brackets in the process */
665 name_len = strlen (fields[1]) - 2;
666 if ((fields[1][0] != '(') || (fields[1][name_len + 1] != ')'))
667 {
668 DEBUG ("No brackets found in process name: `%s'", fields[1]);
669 return (-1);
670 }
671 fields[1] = fields[1] + 1;
672 fields[1][name_len] = '\0';
673 strncpy (ps->name, fields[1], PROCSTAT_NAME_LEN);
675 ppid = atoi (fields[3]);
677 *state = fields[2][0];
679 if (*state == 'Z')
680 {
681 ps->num_lwp = 0;
682 ps->num_proc = 0;
683 }
684 else if ((tasks = ps_read_tasks (pid)) == NULL)
685 {
686 /* Kernel 2.4 or so */
687 ps->num_lwp = 1;
688 ps->num_proc = 1;
689 }
690 else
691 {
692 ps->num_lwp = 0;
693 ps->num_proc = 1;
694 for (i = 0; tasks[i] != 0; i++)
695 ps->num_lwp++;
697 free (tasks);
698 tasks = NULL;
699 }
701 /* Leave the rest at zero if this is only a zombi */
702 if (ps->num_proc == 0)
703 {
704 DEBUG ("processes plugin: This is only a zombi: pid = %i; "
705 "name = %s;", pid, ps->name);
706 return (0);
707 }
709 cpu_user_counter = atoll (fields[13]);
710 cpu_system_counter = atoll (fields[14]);
711 vmem_rss = atoll (fields[23]);
712 ps->vmem_minflt_counter = atol (fields[9]);
713 ps->vmem_majflt_counter = atol (fields[11]);
715 /* Convert jiffies to useconds */
716 cpu_user_counter = cpu_user_counter * 1000000 / CONFIG_HZ;
717 cpu_system_counter = cpu_system_counter * 1000000 / CONFIG_HZ;
718 vmem_rss = vmem_rss * pagesize_g;
720 ps->cpu_user_counter = (unsigned long) cpu_user_counter;
721 ps->cpu_system_counter = (unsigned long) cpu_system_counter;
722 ps->vmem_rss = (unsigned long) vmem_rss;
724 /* success */
725 return (0);
726 } /* int ps_read_process (...) */
727 #endif /* KERNEL_LINUX */
729 #if HAVE_THREAD_INFO
730 static int mach_get_task_name (task_t t, int *pid, char *name, size_t name_max_len)
731 {
732 int mib[4];
734 struct kinfo_proc kp;
735 size_t kp_size;
737 mib[0] = CTL_KERN;
738 mib[1] = KERN_PROC;
739 mib[2] = KERN_PROC_PID;
741 if (pid_for_task (t, pid) != KERN_SUCCESS)
742 return (-1);
743 mib[3] = *pid;
745 kp_size = sizeof (kp);
746 if (sysctl (mib, 4, &kp, &kp_size, NULL, 0) != 0)
747 return (-1);
749 if (name_max_len > (MAXCOMLEN + 1))
750 name_max_len = MAXCOMLEN + 1;
752 strncpy (name, kp.kp_proc.p_comm, name_max_len - 1);
753 name[name_max_len - 1] = '\0';
755 DEBUG ("pid = %i; name = %s;", *pid, name);
757 /* We don't do the special handling for `p_comm == "LaunchCFMApp"' as
758 * `top' does it, because it is a lot of work and only used when
759 * debugging. -octo */
761 return (0);
762 }
763 #endif /* HAVE_THREAD_INFO */
764 /* ------- end of additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
766 /* do actual readings from kernel */
767 static int ps_read (void)
768 {
769 #if HAVE_THREAD_INFO
770 kern_return_t status;
772 int pset;
773 processor_set_t port_pset_priv;
775 int task;
776 task_array_t task_list;
777 mach_msg_type_number_t task_list_len;
779 int task_pid;
780 char task_name[MAXCOMLEN + 1];
782 int thread;
783 thread_act_array_t thread_list;
784 mach_msg_type_number_t thread_list_len;
785 thread_basic_info_data_t thread_data;
786 mach_msg_type_number_t thread_data_len;
788 int running = 0;
789 int sleeping = 0;
790 int zombies = 0;
791 int stopped = 0;
792 int blocked = 0;
794 procstat_t *ps;
795 procstat_entry_t pse;
797 ps_list_reset ();
799 /*
800 * The Mach-concept is a little different from the traditional UNIX
801 * concept: All the work is done in threads. Threads are contained in
802 * `tasks'. Therefore, `task status' doesn't make much sense, since
803 * it's actually a `thread status'.
804 * Tasks are assigned to sets of processors, so that's where you go to
805 * get a list.
806 */
807 for (pset = 0; pset < pset_list_len; pset++)
808 {
809 if ((status = host_processor_set_priv (port_host_self,
810 pset_list[pset],
811 &port_pset_priv)) != KERN_SUCCESS)
812 {
813 ERROR ("host_processor_set_priv failed: %s\n",
814 mach_error_string (status));
815 continue;
816 }
818 if ((status = processor_set_tasks (port_pset_priv,
819 &task_list,
820 &task_list_len)) != KERN_SUCCESS)
821 {
822 ERROR ("processor_set_tasks failed: %s\n",
823 mach_error_string (status));
824 mach_port_deallocate (port_task_self, port_pset_priv);
825 continue;
826 }
828 for (task = 0; task < task_list_len; task++)
829 {
830 ps = NULL;
831 if (mach_get_task_name (task_list[task],
832 &task_pid,
833 task_name, PROCSTAT_NAME_LEN) == 0)
834 {
835 /* search for at least one match */
836 for (ps = list_head_g; ps != NULL; ps = ps->next)
837 if (ps_list_match(task_name, NULL, ps) == 1) //!!! cmdline should be here instead of NULL
838 break;
839 }
841 /* Collect more detailed statistics for this process */
842 if (ps != NULL)
843 {
844 task_basic_info_data_t task_basic_info;
845 mach_msg_type_number_t task_basic_info_len;
846 task_events_info_data_t task_events_info;
847 mach_msg_type_number_t task_events_info_len;
848 task_absolutetime_info_data_t task_absolutetime_info;
849 mach_msg_type_number_t task_absolutetime_info_len;
851 memset (&pse, '\0', sizeof (pse));
852 pse.id = task_pid;
854 task_basic_info_len = TASK_BASIC_INFO_COUNT;
855 status = task_info (task_list[task],
856 TASK_BASIC_INFO,
857 (task_info_t) &task_basic_info,
858 &task_basic_info_len);
859 if (status != KERN_SUCCESS)
860 {
861 ERROR ("task_info failed: %s",
862 mach_error_string (status));
863 continue; /* with next thread_list */
864 }
866 task_events_info_len = TASK_EVENTS_INFO_COUNT;
867 status = task_info (task_list[task],
868 TASK_EVENTS_INFO,
869 (task_info_t) &task_events_info,
870 &task_events_info_len);
871 if (status != KERN_SUCCESS)
872 {
873 ERROR ("task_info failed: %s",
874 mach_error_string (status));
875 continue; /* with next thread_list */
876 }
878 task_absolutetime_info_len = TASK_ABSOLUTETIME_INFO_COUNT;
879 status = task_info (task_list[task],
880 TASK_ABSOLUTETIME_INFO,
881 (task_info_t) &task_absolutetime_info,
882 &task_absolutetime_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 pse.num_proc++;
891 pse.vmem_rss = task_basic_info.resident_size;
893 pse.vmem_minflt_counter = task_events_info.cow_faults;
894 pse.vmem_majflt_counter = task_events_info.faults;
896 pse.cpu_user_counter = task_absolutetime_info.total_user;
897 pse.cpu_system_counter = task_absolutetime_info.total_system;
898 }
900 status = task_threads (task_list[task], &thread_list,
901 &thread_list_len);
902 if (status != KERN_SUCCESS)
903 {
904 /* Apple's `top' treats this case a zombie. It
905 * makes sense to some extend: A `zombie'
906 * thread is nonsense, since the task/process
907 * is dead. */
908 zombies++;
909 DEBUG ("task_threads failed: %s",
910 mach_error_string (status));
911 if (task_list[task] != port_task_self)
912 mach_port_deallocate (port_task_self,
913 task_list[task]);
914 continue; /* with next task_list */
915 }
917 for (thread = 0; thread < thread_list_len; thread++)
918 {
919 thread_data_len = THREAD_BASIC_INFO_COUNT;
920 status = thread_info (thread_list[thread],
921 THREAD_BASIC_INFO,
922 (thread_info_t) &thread_data,
923 &thread_data_len);
924 if (status != KERN_SUCCESS)
925 {
926 ERROR ("thread_info failed: %s",
927 mach_error_string (status));
928 if (task_list[task] != port_task_self)
929 mach_port_deallocate (port_task_self,
930 thread_list[thread]);
931 continue; /* with next thread_list */
932 }
934 if (ps != NULL)
935 pse.num_lwp++;
937 switch (thread_data.run_state)
938 {
939 case TH_STATE_RUNNING:
940 running++;
941 break;
942 case TH_STATE_STOPPED:
943 /* What exactly is `halted'? */
944 case TH_STATE_HALTED:
945 stopped++;
946 break;
947 case TH_STATE_WAITING:
948 sleeping++;
949 break;
950 case TH_STATE_UNINTERRUPTIBLE:
951 blocked++;
952 break;
953 /* There is no `zombie' case here,
954 * since there are no zombie-threads.
955 * There's only zombie tasks, which are
956 * handled above. */
957 default:
958 WARNING ("Unknown thread status: %s",
959 thread_data.run_state);
960 break;
961 } /* switch (thread_data.run_state) */
963 if (task_list[task] != port_task_self)
964 {
965 status = mach_port_deallocate (port_task_self,
966 thread_list[thread]);
967 if (status != KERN_SUCCESS)
968 ERROR ("mach_port_deallocate failed: %s",
969 mach_error_string (status));
970 }
971 } /* for (thread_list) */
973 if ((status = vm_deallocate (port_task_self,
974 (vm_address_t) thread_list,
975 thread_list_len * sizeof (thread_act_t)))
976 != KERN_SUCCESS)
977 {
978 ERROR ("vm_deallocate failed: %s",
979 mach_error_string (status));
980 }
981 thread_list = NULL;
982 thread_list_len = 0;
984 /* Only deallocate the task port, if it isn't our own.
985 * Don't know what would happen in that case, but this
986 * is what Apple's top does.. ;) */
987 if (task_list[task] != port_task_self)
988 {
989 status = mach_port_deallocate (port_task_self,
990 task_list[task]);
991 if (status != KERN_SUCCESS)
992 ERROR ("mach_port_deallocate failed: %s",
993 mach_error_string (status));
994 }
996 if (ps != NULL)
997 ps_list_add (task_name, NULL, &pse); //!!! cmdline should be here instead of NULL
998 } /* for (task_list) */
1000 if ((status = vm_deallocate (port_task_self,
1001 (vm_address_t) task_list,
1002 task_list_len * sizeof (task_t))) != KERN_SUCCESS)
1003 {
1004 ERROR ("vm_deallocate failed: %s",
1005 mach_error_string (status));
1006 }
1007 task_list = NULL;
1008 task_list_len = 0;
1010 if ((status = mach_port_deallocate (port_task_self, port_pset_priv))
1011 != KERN_SUCCESS)
1012 {
1013 ERROR ("mach_port_deallocate failed: %s",
1014 mach_error_string (status));
1015 }
1016 } /* for (pset_list) */
1018 ps_submit_state ("running", running);
1019 ps_submit_state ("sleeping", sleeping);
1020 ps_submit_state ("zombies", zombies);
1021 ps_submit_state ("stopped", stopped);
1022 ps_submit_state ("blocked", blocked);
1024 for (ps = list_head_g; ps != NULL; ps = ps->next)
1025 ps_submit_proc_list (ps);
1026 /* #endif HAVE_THREAD_INFO */
1028 #elif KERNEL_LINUX
1029 int running = 0;
1030 int sleeping = 0;
1031 int zombies = 0;
1032 int stopped = 0;
1033 int paging = 0;
1034 int blocked = 0;
1036 struct dirent *ent;
1037 DIR *proc;
1038 int pid;
1040 int status;
1041 procstat_t ps;
1042 procstat_entry_t pse;
1043 char state;
1045 procstat_t *ps_ptr;
1047 running = sleeping = zombies = stopped = paging = blocked = 0;
1048 ps_list_reset ();
1050 if ((proc = opendir ("/proc")) == NULL)
1051 {
1052 char errbuf[1024];
1053 ERROR ("Cannot open `/proc': %s",
1054 sstrerror (errno, errbuf, sizeof (errbuf)));
1055 return (-1);
1056 }
1058 while ((ent = readdir (proc)) != NULL)
1059 {
1060 if (!isdigit (ent->d_name[0]))
1061 continue;
1063 if ((pid = atoi (ent->d_name)) < 1)
1064 continue;
1066 status = ps_read_process (pid, &ps, &state);
1067 if (status != 0)
1068 {
1069 DEBUG ("ps_read_process failed: %i", status);
1070 continue;
1071 }
1073 pse.id = pid;
1074 pse.age = 0;
1076 pse.num_proc = ps.num_proc;
1077 pse.num_lwp = ps.num_lwp;
1078 pse.vmem_rss = ps.vmem_rss;
1080 pse.vmem_minflt = 0;
1081 pse.vmem_minflt_counter = ps.vmem_minflt_counter;
1082 pse.vmem_majflt = 0;
1083 pse.vmem_majflt_counter = ps.vmem_majflt_counter;
1085 pse.cpu_user = 0;
1086 pse.cpu_user_counter = ps.cpu_user_counter;
1087 pse.cpu_system = 0;
1088 pse.cpu_system_counter = ps.cpu_system_counter;
1090 switch (state)
1091 {
1092 case 'R': running++; break;
1093 case 'S': sleeping++; break;
1094 case 'D': blocked++; break;
1095 case 'Z': zombies++; break;
1096 case 'T': stopped++; break;
1097 case 'W': paging++; break;
1098 }
1100 ps_list_add (ps.name, NULL, &pse); //!!! cmdline should be here instead of NULL
1101 }
1103 closedir (proc);
1105 ps_submit_state ("running", running);
1106 ps_submit_state ("sleeping", sleeping);
1107 ps_submit_state ("zombies", zombies);
1108 ps_submit_state ("stopped", stopped);
1109 ps_submit_state ("paging", paging);
1110 ps_submit_state ("blocked", blocked);
1112 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1113 ps_submit_proc_list (ps_ptr);
1114 /* #endif KERNEL_LINUX */
1116 #elif HAVE_LIBKVM
1117 int running = 0;
1118 int sleeping = 0;
1119 int zombies = 0;
1120 int stopped = 0;
1121 int blocked = 0;
1122 int idle = 0;
1123 int wait = 0;
1125 kvm_t *kd;
1126 char errbuf[1024];
1127 char cmdline[ARG_MAX];
1128 struct kinfo_proc *procs; /* array of processes */
1129 char ** argv;
1130 int count; /* returns number of processes */
1131 int i, j;
1133 procstat_t *ps_ptr;
1134 procstat_entry_t pse;
1136 ps_list_reset ();
1138 /* Open the kvm interface, get a descriptor */
1139 if ((kd = kvm_open(NULL, NULL, NULL, 0, errbuf)) == NULL) {
1140 ERROR ("Cannot open kvm interface: %s", errbuf);
1141 return (0);
1142 }
1144 /* Get the list of processes. */
1145 if ((procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, &count)) == NULL) {
1146 kvm_close(kd);
1147 ERROR ("Cannot get kvm processes list: %s", kvm_geterr(kd));
1148 return (0);
1149 }
1151 /* Iterate through the processes in kinfo_proc */
1152 for (i=0; i < count; i++) {
1153 // retrieve the arguments
1154 *cmdline = '\0';
1155 argv = kvm_getargv(kd, (const struct kinfo_proc *) &(procs[i]), 0);
1156 if (argv) {
1157 j = 0;
1158 while (argv[j] && strlen(cmdline) <= ARG_MAX) {
1159 if (j)
1160 strncat(cmdline, " ", 1);
1161 strncat(cmdline, argv[j], strlen(argv[j]));
1162 j++;
1163 }
1164 }
1166 pse.id = procs[i].ki_pid;
1167 pse.age = 0;
1169 pse.num_proc = 1;
1170 pse.num_lwp = procs[i].ki_numthreads;
1172 pse.vmem_rss = procs[i].ki_rssize * getpagesize();
1173 pse.vmem_minflt = 0;
1174 pse.vmem_minflt_counter = procs[i].ki_rusage.ru_minflt;
1175 pse.vmem_majflt = 0;
1176 pse.vmem_majflt_counter = procs[i].ki_rusage.ru_majflt;
1178 pse.cpu_user = 0;
1179 pse.cpu_user_counter = procs[i].ki_rusage.ru_utime.tv_sec*1000 + procs[i].ki_rusage.ru_utime.tv_usec;
1180 pse.cpu_system = 0;
1181 pse.cpu_system_counter = procs[i].ki_rusage.ru_stime.tv_sec*1000 + procs[i].ki_rusage.ru_stime.tv_usec;
1183 switch (procs[i].ki_stat) {
1184 case SSTOP: stopped++; break;
1185 case SSLEEP: sleeping++; break;
1186 case SRUN: running++; break;
1187 case SIDL: idle++; break;
1188 case SWAIT: wait++; break;
1189 case SLOCK: blocked++; break;
1190 case SZOMB: zombies++; break;
1191 }
1193 ps_list_add (procs[i].ki_comm, cmdline, &pse);
1194 }
1196 if (kd) kvm_close(kd);
1198 ps_submit_state ("running", running);
1199 ps_submit_state ("sleeping", sleeping);
1200 ps_submit_state ("zombies", zombies);
1201 ps_submit_state ("stopped", stopped);
1202 ps_submit_state ("blocked", blocked);
1203 ps_submit_state ("idle", idle);
1204 ps_submit_state ("wait", wait);
1206 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1207 ps_submit_proc_list (ps_ptr);
1209 #endif /* HAVE_LIBKVM */
1211 return (0);
1212 } /* int ps_read */
1214 void module_register (void)
1215 {
1216 plugin_register_config ("processes", ps_config,
1217 config_keys, config_keys_num);
1218 plugin_register_init ("processes", ps_init);
1219 plugin_register_read ("processes", ps_read);
1220 } /* void module_register */