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 "utils_debug.h"
29 #include "configfile.h"
31 /* Include header files for the mach system, if they exist.. */
32 #if HAVE_THREAD_INFO
33 # if HAVE_MACH_MACH_INIT_H
34 # include <mach/mach_init.h>
35 # endif
36 # if HAVE_MACH_HOST_PRIV_H
37 # include <mach/host_priv.h>
38 # endif
39 # if HAVE_MACH_MACH_ERROR_H
40 # include <mach/mach_error.h>
41 # endif
42 # if HAVE_MACH_MACH_HOST_H
43 # include <mach/mach_host.h>
44 # endif
45 # if HAVE_MACH_MACH_PORT_H
46 # include <mach/mach_port.h>
47 # endif
48 # if HAVE_MACH_MACH_TYPES_H
49 # include <mach/mach_types.h>
50 # endif
51 # if HAVE_MACH_MESSAGE_H
52 # include <mach/message.h>
53 # endif
54 # if HAVE_MACH_PROCESSOR_SET_H
55 # include <mach/processor_set.h>
56 # endif
57 # if HAVE_MACH_TASK_H
58 # include <mach/task.h>
59 # endif
60 # if HAVE_MACH_THREAD_ACT_H
61 # include <mach/thread_act.h>
62 # endif
63 # if HAVE_MACH_VM_REGION_H
64 # include <mach/vm_region.h>
65 # endif
66 # if HAVE_MACH_VM_MAP_H
67 # include <mach/vm_map.h>
68 # endif
69 # if HAVE_MACH_VM_PROT_H
70 # include <mach/vm_prot.h>
71 # endif
72 # if HAVE_SYS_SYSCTL_H
73 # include <sys/sysctl.h>
74 # endif
75 /* #endif HAVE_THREAD_INFO */
77 #elif KERNEL_LINUX
78 # if HAVE_LINUX_CONFIG_H
79 # include <linux/config.h>
80 # endif
81 # ifndef CONFIG_HZ
82 # define CONFIG_HZ 100
83 # endif
84 #endif /* KERNEL_LINUX */
86 #define MODULE_NAME "processes"
88 #if HAVE_THREAD_INFO || KERNEL_LINUX
89 # define PROCESSES_HAVE_READ 1
90 #else
91 # define PROCESSES_HAVE_READ 0
92 #endif
94 #define BUFSIZE 256
96 static data_source_t state_dsrc[1] =
97 {
98 {"value", DS_TYPE_GAUGE, 0.0, 65535.0}
99 };
101 static data_set_t state_ds =
102 {
103 "ps_state", 1, state_dsrc
104 };
106 static data_source_t rss_dsrc[1] =
107 {
108 /* max = 2^63 - 1 */
109 {"value", DS_TYPE_GAUGE, 0.0, 9223372036854775807.0}
110 };
112 static data_set_t rss_ds =
113 {
114 "ps_rss", 1, rss_dsrc
115 };
117 static data_source_t time_dsrc[2] =
118 {
119 /* 1 second in user-mode per second ought to be enough.. */
120 {"user", DS_TYPE_COUNTER, 0.0, 1000000.0},
121 {"syst", DS_TYPE_COUNTER, 0.0, 1000000.0}
122 };
124 static data_set_t time_ds =
125 {
126 "ps_cputime", 2, time_dsrc
127 };
129 static data_source_t count_dsrc[2] =
130 {
131 /* 1 second in user-mode per second ought to be enough.. */
132 {"processes", DS_TYPE_GAUGE, 0.0, 1000000.0},
133 {"threads", DS_TYPE_GAUGE, 0.0, 1000000.0}
134 };
136 static data_set_t count_ds =
137 {
138 "ps_count", 2, count_dsrc
139 };
141 static data_source_t pagefaults_dsrc[2] =
142 {
143 /* max = 2^63 - 1 */
144 {"minflt", DS_TYPE_COUNTER, 0.0, 9223372036854775807.0},
145 {"majflt", DS_TYPE_COUNTER, 0.0, 9223372036854775807.0}
146 };
148 static data_set_t pagefaults_ds =
149 {
150 "ps_pagefaults", 2, pagefaults_dsrc
151 };
153 #if PROCESSES_HAVE_READ
154 static const char *config_keys[] =
155 {
156 "Process",
157 NULL
158 };
159 static int config_keys_num = 1;
161 typedef struct procstat_entry_s
162 {
163 unsigned long id;
164 unsigned long age;
166 unsigned long num_proc;
167 unsigned long num_lwp;
168 unsigned long vmem_rss;
170 unsigned long vmem_minflt;
171 unsigned long vmem_majflt;
172 unsigned long vmem_minflt_counter;
173 unsigned long vmem_majflt_counter;
175 unsigned long cpu_user;
176 unsigned long cpu_system;
177 unsigned long cpu_user_counter;
178 unsigned long cpu_system_counter;
180 struct procstat_entry_s *next;
181 } procstat_entry_t;
183 #define PROCSTAT_NAME_LEN 256
184 typedef struct procstat
185 {
186 char name[PROCSTAT_NAME_LEN];
188 unsigned long num_proc;
189 unsigned long num_lwp;
190 unsigned long vmem_rss;
192 unsigned long vmem_minflt_counter;
193 unsigned long vmem_majflt_counter;
195 unsigned long cpu_user_counter;
196 unsigned long cpu_system_counter;
198 struct procstat *next;
199 struct procstat_entry_s *instances;
200 } procstat_t;
202 static procstat_t *list_head_g = NULL;
204 #if HAVE_THREAD_INFO
205 static mach_port_t port_host_self;
206 static mach_port_t port_task_self;
208 static processor_set_name_array_t pset_list;
209 static mach_msg_type_number_t pset_list_len;
210 /* #endif HAVE_THREAD_INFO */
212 #elif KERNEL_LINUX
213 static long pagesize_g;
214 #endif /* KERNEL_LINUX */
216 #if HAVE_THREAD_INFO | KERNEL_LINUX
217 static void ps_list_register (const char *name)
218 {
219 procstat_t *new;
220 procstat_t *ptr;
222 if ((new = (procstat_t *) malloc (sizeof (procstat_t))) == NULL)
223 return;
224 memset (new, 0, sizeof (procstat_t));
225 strncpy (new->name, name, PROCSTAT_NAME_LEN);
227 for (ptr = list_head_g; ptr != NULL; ptr = ptr->next)
228 {
229 if (strcmp (ptr->name, name) == 0)
230 return;
231 if (ptr->next == NULL)
232 break;
233 }
235 if (ptr == NULL)
236 list_head_g = new;
237 else
238 ptr->next = new;
239 }
241 static procstat_t *ps_list_search (const char *name)
242 {
243 procstat_t *ptr;
245 for (ptr = list_head_g; ptr != NULL; ptr = ptr->next)
246 if (strcmp (ptr->name, name) == 0)
247 break;
249 return (ptr);
250 }
252 static void ps_list_add (const char *name, procstat_entry_t *entry)
253 {
254 procstat_t *ps;
255 procstat_entry_t *pse;
257 if (entry->id == 0)
258 return;
260 if ((ps = ps_list_search (name)) == NULL)
261 return;
263 for (pse = ps->instances; pse != NULL; pse = pse->next)
264 if ((pse->id == entry->id) || (pse->next == NULL))
265 break;
267 if ((pse == NULL) || (pse->id != entry->id))
268 {
269 procstat_entry_t *new;
271 new = (procstat_entry_t *) malloc (sizeof (procstat_entry_t));
272 if (new == NULL)
273 return;
274 memset (new, 0, sizeof (procstat_entry_t));
275 new->id = entry->id;
277 if (pse == NULL)
278 ps->instances = new;
279 else
280 pse->next = new;
282 pse = new;
283 }
285 pse->age = 0;
286 pse->num_proc = entry->num_proc;
287 pse->num_lwp = entry->num_lwp;
288 pse->vmem_rss = entry->vmem_rss;
290 ps->num_proc += pse->num_proc;
291 ps->num_lwp += pse->num_lwp;
292 ps->vmem_rss += pse->vmem_rss;
294 if ((entry->vmem_minflt_counter == 0)
295 && (entry->vmem_majflt_counter == 0))
296 {
297 pse->vmem_minflt_counter += entry->vmem_minflt;
298 pse->vmem_minflt = entry->vmem_minflt;
300 pse->vmem_majflt_counter += entry->vmem_majflt;
301 pse->vmem_majflt = entry->vmem_majflt;
302 }
303 else
304 {
305 if (entry->vmem_minflt_counter < pse->vmem_minflt_counter)
306 {
307 pse->vmem_minflt = entry->vmem_minflt_counter
308 + (ULONG_MAX - pse->vmem_minflt_counter);
309 }
310 else
311 {
312 pse->vmem_minflt = entry->vmem_minflt_counter - pse->vmem_minflt_counter;
313 }
314 pse->vmem_minflt_counter = entry->vmem_minflt_counter;
316 if (entry->vmem_majflt_counter < pse->vmem_majflt_counter)
317 {
318 pse->vmem_majflt = entry->vmem_majflt_counter
319 + (ULONG_MAX - pse->vmem_majflt_counter);
320 }
321 else
322 {
323 pse->vmem_majflt = entry->vmem_majflt_counter - pse->vmem_majflt_counter;
324 }
325 pse->vmem_majflt_counter = entry->vmem_majflt_counter;
326 }
328 ps->vmem_minflt_counter += pse->vmem_minflt;
329 ps->vmem_majflt_counter += pse->vmem_majflt;
331 if ((entry->cpu_user_counter == 0)
332 && (entry->cpu_system_counter == 0))
333 {
334 pse->cpu_user_counter += entry->cpu_user;
335 pse->cpu_user = entry->cpu_user;
337 pse->cpu_system_counter += entry->cpu_system;
338 pse->cpu_system = entry->cpu_system;
339 }
340 else
341 {
342 if (entry->cpu_user_counter < pse->cpu_user_counter)
343 {
344 pse->cpu_user = entry->cpu_user_counter
345 + (ULONG_MAX - pse->cpu_user_counter);
346 }
347 else
348 {
349 pse->cpu_user = entry->cpu_user_counter - pse->cpu_user_counter;
350 }
351 pse->cpu_user_counter = entry->cpu_user_counter;
353 if (entry->cpu_system_counter < pse->cpu_system_counter)
354 {
355 pse->cpu_system = entry->cpu_system_counter
356 + (ULONG_MAX - pse->cpu_system_counter);
357 }
358 else
359 {
360 pse->cpu_system = entry->cpu_system_counter - pse->cpu_system_counter;
361 }
362 pse->cpu_system_counter = entry->cpu_system_counter;
363 }
365 ps->cpu_user_counter += pse->cpu_user;
366 ps->cpu_system_counter += pse->cpu_system;
367 }
369 static void ps_list_reset (void)
370 {
371 procstat_t *ps;
372 procstat_entry_t *pse;
373 procstat_entry_t *pse_prev;
375 for (ps = list_head_g; ps != NULL; ps = ps->next)
376 {
377 ps->num_proc = 0;
378 ps->num_lwp = 0;
379 ps->vmem_rss = 0;
381 pse_prev = NULL;
382 pse = ps->instances;
383 while (pse != NULL)
384 {
385 if (pse->age > 10)
386 {
387 DBG ("Removing this procstat entry cause it's too old: "
388 "id = %lu; name = %s;",
389 pse->id, ps->name);
391 if (pse_prev == NULL)
392 {
393 ps->instances = pse->next;
394 free (pse);
395 pse = ps->instances;
396 }
397 else
398 {
399 pse_prev->next = pse->next;
400 free (pse);
401 pse = pse_prev->next;
402 }
403 }
404 else
405 {
406 pse->age++;
407 pse_prev = pse;
408 pse = pse->next;
409 }
410 } /* while (pse != NULL) */
411 } /* for (ps = list_head_g; ps != NULL; ps = ps->next) */
412 }
413 #endif /* HAVE_THREAD_INFO | KERNEL_LINUX */
415 static int ps_config (const char *key, const char *value)
416 {
417 if (strcasecmp (key, "Process") == 0)
418 {
419 ps_list_register (value);
420 }
421 else
422 {
423 return (-1);
424 }
426 return (0);
427 }
429 static int ps_init (void)
430 {
431 #if HAVE_THREAD_INFO
432 kern_return_t status;
434 port_host_self = mach_host_self ();
435 port_task_self = mach_task_self ();
437 if (pset_list != NULL)
438 {
439 vm_deallocate (port_task_self,
440 (vm_address_t) pset_list,
441 pset_list_len * sizeof (processor_set_t));
442 pset_list = NULL;
443 pset_list_len = 0;
444 }
446 if ((status = host_processor_sets (port_host_self,
447 &pset_list,
448 &pset_list_len)) != KERN_SUCCESS)
449 {
450 syslog (LOG_ERR, "host_processor_sets failed: %s\n",
451 mach_error_string (status));
452 pset_list = NULL;
453 pset_list_len = 0;
454 return (-1);
455 }
456 /* #endif HAVE_THREAD_INFO */
458 #elif KERNEL_LINUX
459 pagesize_g = sysconf(_SC_PAGESIZE);
460 DBG ("pagesize_g = %li; CONFIG_HZ = %i;",
461 pagesize_g, CONFIG_HZ);
462 #endif /* KERNEL_LINUX */
464 return (0);
465 } /* int ps_init */
467 static void ps_submit_state (const char *state, double value)
468 {
469 value_t values[1];
470 value_list_t vl = VALUE_LIST_INIT;
472 values[0].gauge = value;
474 vl.values = values;
475 vl.values_len = 1;
476 vl.time = time (NULL);
477 strcpy (vl.host, hostname);
478 strcpy (vl.plugin, "processes");
479 strcpy (vl.plugin_instance, "");
480 strncpy (vl.type_instance, state, sizeof (vl.type_instance));
482 plugin_dispatch_values ("ps_state", &vl);
483 }
485 static void ps_submit_proc_list (procstat_t *ps)
486 {
487 value_t values[2];
488 value_list_t vl = VALUE_LIST_INIT;
490 vl.values = values;
491 vl.values_len = 2;
492 vl.time = time (NULL);
493 strcpy (vl.host, hostname);
494 strcpy (vl.plugin, "processes");
495 strncpy (vl.plugin_instance, ps->name, sizeof (vl.plugin_instance));
497 vl.values[0].gauge = ps->vmem_rss;
498 vl.values_len = 1;
499 plugin_dispatch_values ("ps_rss", &vl);
501 vl.values[0].counter = ps->cpu_user_counter;
502 vl.values[1].counter = ps->cpu_system_counter;
503 vl.values_len = 2;
504 plugin_dispatch_values ("ps_cputime", &vl);
506 vl.values[0].gauge = ps->num_proc;
507 vl.values[1].gauge = ps->num_lwp;
508 vl.values_len = 2;
509 plugin_dispatch_values ("ps_count", &vl);
511 vl.values[0].counter = ps->vmem_minflt_counter;
512 vl.values[1].counter = ps->vmem_majflt_counter;
513 vl.values_len = 2;
514 plugin_dispatch_values ("ps_pagefaults", &vl);
516 DBG ("name = %s; num_proc = %lu; num_lwp = %lu; vmem_rss = %lu; "
517 "vmem_minflt_counter = %lu; vmem_majflt_counter = %lu; "
518 "cpu_user_counter = %lu; cpu_system_counter = %lu;",
519 ps->name, ps->num_proc, ps->num_lwp, ps->vmem_rss,
520 ps->vmem_minflt_counter, ps->vmem_majflt_counter,
521 ps->cpu_user_counter, ps->cpu_system_counter);
522 } /* void ps_submit_proc_list */
524 #if KERNEL_LINUX
525 static int *ps_read_tasks (int pid)
526 {
527 int *list = NULL;
528 int list_size = 1; /* size of allocated space, in elements */
529 int list_len = 0; /* number of currently used elements */
531 char dirname[64];
532 DIR *dh;
533 struct dirent *ent;
535 snprintf (dirname, 64, "/proc/%i/task", pid);
536 dirname[63] = '\0';
538 if ((dh = opendir (dirname)) == NULL)
539 {
540 DBG ("Failed to open directory `%s'", dirname);
541 return (NULL);
542 }
544 while ((ent = readdir (dh)) != NULL)
545 {
546 if (!isdigit (ent->d_name[0]))
547 continue;
549 if ((list_len + 1) >= list_size)
550 {
551 int *new_ptr;
552 int new_size = 2 * list_size;
553 /* Comes in sizes: 2, 4, 8, 16, ... */
555 new_ptr = (int *) realloc (list, (size_t) (sizeof (int) * new_size));
556 if (new_ptr == NULL)
557 {
558 if (list != NULL)
559 free (list);
560 syslog (LOG_ERR, "processes plugin: "
561 "Failed to allocate more memory.");
562 return (NULL);
563 }
565 list = new_ptr;
566 list_size = new_size;
568 memset (list + list_len, 0, sizeof (int) * (list_size - list_len));
569 }
571 list[list_len] = atoi (ent->d_name);
572 if (list[list_len] != 0)
573 list_len++;
574 }
576 closedir (dh);
578 assert (list_len < list_size);
579 assert (list[list_len] == 0);
581 return (list);
582 }
584 int ps_read_process (int pid, procstat_t *ps, char *state)
585 {
586 char filename[64];
587 char buffer[1024];
588 FILE *fh;
590 char *fields[64];
591 char fields_len;
593 int *tasks;
594 int i;
596 int ppid;
597 int name_len;
599 long long unsigned cpu_user_counter;
600 long long unsigned cpu_system_counter;
601 long long unsigned vmem_rss;
603 memset (ps, 0, sizeof (procstat_t));
605 snprintf (filename, 64, "/proc/%i/stat", pid);
606 filename[63] = '\0';
608 if ((fh = fopen (filename, "r")) == NULL)
609 return (-1);
611 if (fgets (buffer, 1024, fh) == NULL)
612 {
613 fclose (fh);
614 return (-1);
615 }
617 fclose (fh);
619 fields_len = strsplit (buffer, fields, 64);
620 if (fields_len < 24)
621 {
622 DBG ("`%s' has only %i fields..",
623 filename, fields_len);
624 return (-1);
625 }
626 else if (fields_len != 41)
627 {
628 DBG ("WARNING: (fields_len = %i) != 41", fields_len);
629 }
631 /* copy the name, strip brackets in the process */
632 name_len = strlen (fields[1]) - 2;
633 if ((fields[1][0] != '(') || (fields[1][name_len + 1] != ')'))
634 {
635 DBG ("No brackets found in process name: `%s'", fields[1]);
636 return (-1);
637 }
638 fields[1] = fields[1] + 1;
639 fields[1][name_len] = '\0';
640 strncpy (ps->name, fields[1], PROCSTAT_NAME_LEN);
642 ppid = atoi (fields[3]);
644 if ((tasks = ps_read_tasks (pid)) == NULL)
645 {
646 /* This happends for zombied, e.g. */
647 DBG ("ps_read_tasks (%i) failed.", pid);
648 *state = 'Z';
649 ps->num_lwp = 0;
650 ps->num_proc = 0;
651 }
652 else
653 {
654 *state = '\0';
655 ps->num_lwp = 0;
656 ps->num_proc = 1;
657 for (i = 0; tasks[i] != 0; i++)
658 ps->num_lwp++;
660 free (tasks);
661 tasks = NULL;
662 }
664 /* Leave the rest at zero if this is only an LWP */
665 if (ps->num_proc == 0)
666 {
667 DBG ("This is only an LWP: pid = %i; name = %s;",
668 pid, ps->name);
669 return (0);
670 }
672 cpu_user_counter = atoll (fields[13]);
673 cpu_system_counter = atoll (fields[14]);
674 vmem_rss = atoll (fields[23]);
675 ps->vmem_minflt_counter = atol (fields[9]);
676 ps->vmem_majflt_counter = atol (fields[11]);
678 /* Convert jiffies to useconds */
679 cpu_user_counter = cpu_user_counter * 1000000 / CONFIG_HZ;
680 cpu_system_counter = cpu_system_counter * 1000000 / CONFIG_HZ;
681 vmem_rss = vmem_rss * pagesize_g;
683 ps->cpu_user_counter = (unsigned long) cpu_user_counter;
684 ps->cpu_system_counter = (unsigned long) cpu_system_counter;
685 ps->vmem_rss = (unsigned long) vmem_rss;
687 *state = fields[2][0];
689 /* success */
690 return (0);
691 } /* int ps_read_process (...) */
692 #endif /* KERNEL_LINUX */
694 #if HAVE_THREAD_INFO
695 static int mach_get_task_name (task_t t, int *pid, char *name, size_t name_max_len)
696 {
697 int mib[4];
699 struct kinfo_proc kp;
700 size_t kp_size;
702 mib[0] = CTL_KERN;
703 mib[1] = KERN_PROC;
704 mib[2] = KERN_PROC_PID;
706 if (pid_for_task (t, pid) != KERN_SUCCESS)
707 return (-1);
708 mib[3] = *pid;
710 kp_size = sizeof (kp);
711 if (sysctl (mib, 4, &kp, &kp_size, NULL, 0) != 0)
712 return (-1);
714 if (name_max_len > (MAXCOMLEN + 1))
715 name_max_len = MAXCOMLEN + 1;
717 strncpy (name, kp.kp_proc.p_comm, name_max_len - 1);
718 name[name_max_len - 1] = '\0';
720 DBG ("pid = %i; name = %s;", *pid, name);
722 /* We don't do the special handling for `p_comm == "LaunchCFMApp"' as
723 * `top' does it, because it is a lot of work and only used when
724 * debugging. -octo */
726 return (0);
727 }
728 #endif /* HAVE_THREAD_INFO */
730 static int ps_read (void)
731 {
732 #if HAVE_THREAD_INFO
733 kern_return_t status;
735 int pset;
736 processor_set_t port_pset_priv;
738 int task;
739 task_array_t task_list;
740 mach_msg_type_number_t task_list_len;
742 int task_pid;
743 char task_name[MAXCOMLEN + 1];
745 int thread;
746 thread_act_array_t thread_list;
747 mach_msg_type_number_t thread_list_len;
748 thread_basic_info_data_t thread_data;
749 mach_msg_type_number_t thread_data_len;
751 int running = 0;
752 int sleeping = 0;
753 int zombies = 0;
754 int stopped = 0;
755 int blocked = 0;
757 procstat_t *ps;
758 procstat_entry_t pse;
760 ps_list_reset ();
762 /*
763 * The Mach-concept is a little different from the traditional UNIX
764 * concept: All the work is done in threads. Threads are contained in
765 * `tasks'. Therefore, `task status' doesn't make much sense, since
766 * it's actually a `thread status'.
767 * Tasks are assigned to sets of processors, so that's where you go to
768 * get a list.
769 */
770 for (pset = 0; pset < pset_list_len; pset++)
771 {
772 if ((status = host_processor_set_priv (port_host_self,
773 pset_list[pset],
774 &port_pset_priv)) != KERN_SUCCESS)
775 {
776 syslog (LOG_ERR, "host_processor_set_priv failed: %s\n",
777 mach_error_string (status));
778 continue;
779 }
781 if ((status = processor_set_tasks (port_pset_priv,
782 &task_list,
783 &task_list_len)) != KERN_SUCCESS)
784 {
785 syslog (LOG_ERR, "processor_set_tasks failed: %s\n",
786 mach_error_string (status));
787 mach_port_deallocate (port_task_self, port_pset_priv);
788 continue;
789 }
791 for (task = 0; task < task_list_len; task++)
792 {
793 ps = NULL;
794 if (mach_get_task_name (task_list[task],
795 &task_pid,
796 task_name, PROCSTAT_NAME_LEN) == 0)
797 ps = ps_list_search (task_name);
799 /* Collect more detailed statistics for this process */
800 if (ps != NULL)
801 {
802 task_basic_info_data_t task_basic_info;
803 mach_msg_type_number_t task_basic_info_len;
804 task_events_info_data_t task_events_info;
805 mach_msg_type_number_t task_events_info_len;
806 task_absolutetime_info_data_t task_absolutetime_info;
807 mach_msg_type_number_t task_absolutetime_info_len;
809 memset (&pse, '\0', sizeof (pse));
810 pse.id = task_pid;
812 task_basic_info_len = TASK_BASIC_INFO_COUNT;
813 status = task_info (task_list[task],
814 TASK_BASIC_INFO,
815 (task_info_t) &task_basic_info,
816 &task_basic_info_len);
817 if (status != KERN_SUCCESS)
818 {
819 syslog (LOG_ERR, "task_info failed: %s",
820 mach_error_string (status));
821 continue; /* with next thread_list */
822 }
824 task_events_info_len = TASK_EVENTS_INFO_COUNT;
825 status = task_info (task_list[task],
826 TASK_EVENTS_INFO,
827 (task_info_t) &task_events_info,
828 &task_events_info_len);
829 if (status != KERN_SUCCESS)
830 {
831 syslog (LOG_ERR, "task_info failed: %s",
832 mach_error_string (status));
833 continue; /* with next thread_list */
834 }
836 task_absolutetime_info_len = TASK_ABSOLUTETIME_INFO_COUNT;
837 status = task_info (task_list[task],
838 TASK_ABSOLUTETIME_INFO,
839 (task_info_t) &task_absolutetime_info,
840 &task_absolutetime_info_len);
841 if (status != KERN_SUCCESS)
842 {
843 syslog (LOG_ERR, "task_info failed: %s",
844 mach_error_string (status));
845 continue; /* with next thread_list */
846 }
848 pse.num_proc++;
849 pse.vmem_rss = task_basic_info.resident_size;
851 pse.vmem_minflt_counter = task_events_info.cow_faults;
852 pse.vmem_majflt_counter = task_events_info.faults;
854 pse.cpu_user_counter = task_absolutetime_info.total_user;
855 pse.cpu_system_counter = task_absolutetime_info.total_system;
856 }
858 status = task_threads (task_list[task], &thread_list,
859 &thread_list_len);
860 if (status != KERN_SUCCESS)
861 {
862 /* Apple's `top' treats this case a zombie. It
863 * makes sense to some extend: A `zombie'
864 * thread is nonsense, since the task/process
865 * is dead. */
866 zombies++;
867 DBG ("task_threads failed: %s",
868 mach_error_string (status));
869 if (task_list[task] != port_task_self)
870 mach_port_deallocate (port_task_self,
871 task_list[task]);
872 continue; /* with next task_list */
873 }
875 for (thread = 0; thread < thread_list_len; thread++)
876 {
877 thread_data_len = THREAD_BASIC_INFO_COUNT;
878 status = thread_info (thread_list[thread],
879 THREAD_BASIC_INFO,
880 (thread_info_t) &thread_data,
881 &thread_data_len);
882 if (status != KERN_SUCCESS)
883 {
884 syslog (LOG_ERR, "thread_info failed: %s",
885 mach_error_string (status));
886 if (task_list[task] != port_task_self)
887 mach_port_deallocate (port_task_self,
888 thread_list[thread]);
889 continue; /* with next thread_list */
890 }
892 if (ps != NULL)
893 pse.num_lwp++;
895 switch (thread_data.run_state)
896 {
897 case TH_STATE_RUNNING:
898 running++;
899 break;
900 case TH_STATE_STOPPED:
901 /* What exactly is `halted'? */
902 case TH_STATE_HALTED:
903 stopped++;
904 break;
905 case TH_STATE_WAITING:
906 sleeping++;
907 break;
908 case TH_STATE_UNINTERRUPTIBLE:
909 blocked++;
910 break;
911 /* There is no `zombie' case here,
912 * since there are no zombie-threads.
913 * There's only zombie tasks, which are
914 * handled above. */
915 default:
916 syslog (LOG_WARNING,
917 "Unknown thread status: %s",
918 thread_data.run_state);
919 break;
920 } /* switch (thread_data.run_state) */
922 if (task_list[task] != port_task_self)
923 {
924 status = mach_port_deallocate (port_task_self,
925 thread_list[thread]);
926 if (status != KERN_SUCCESS)
927 syslog (LOG_ERR, "mach_port_deallocate failed: %s",
928 mach_error_string (status));
929 }
930 } /* for (thread_list) */
932 if ((status = vm_deallocate (port_task_self,
933 (vm_address_t) thread_list,
934 thread_list_len * sizeof (thread_act_t)))
935 != KERN_SUCCESS)
936 {
937 syslog (LOG_ERR, "vm_deallocate failed: %s",
938 mach_error_string (status));
939 }
940 thread_list = NULL;
941 thread_list_len = 0;
943 /* Only deallocate the task port, if it isn't our own.
944 * Don't know what would happen in that case, but this
945 * is what Apple's top does.. ;) */
946 if (task_list[task] != port_task_self)
947 {
948 status = mach_port_deallocate (port_task_self,
949 task_list[task]);
950 if (status != KERN_SUCCESS)
951 syslog (LOG_ERR, "mach_port_deallocate failed: %s",
952 mach_error_string (status));
953 }
955 if (ps != NULL)
956 ps_list_add (task_name, &pse);
957 } /* for (task_list) */
959 if ((status = vm_deallocate (port_task_self,
960 (vm_address_t) task_list,
961 task_list_len * sizeof (task_t))) != KERN_SUCCESS)
962 {
963 syslog (LOG_ERR, "vm_deallocate failed: %s",
964 mach_error_string (status));
965 }
966 task_list = NULL;
967 task_list_len = 0;
969 if ((status = mach_port_deallocate (port_task_self, port_pset_priv))
970 != KERN_SUCCESS)
971 {
972 syslog (LOG_ERR, "mach_port_deallocate failed: %s",
973 mach_error_string (status));
974 }
975 } /* for (pset_list) */
977 ps_submit_state ("running", running);
978 ps_submit_state ("sleeping", sleeping);
979 ps_submit_state ("zombies", zombies);
980 ps_submit_state ("stopped", stopped);
981 ps_submit_state ("blocked", blocked);
983 for (ps = list_head_g; ps != NULL; ps = ps->next)
984 ps_submit_proc_list (ps);
985 /* #endif HAVE_THREAD_INFO */
987 #elif KERNEL_LINUX
988 int running = 0;
989 int sleeping = 0;
990 int zombies = 0;
991 int stopped = 0;
992 int paging = 0;
993 int blocked = 0;
995 struct dirent *ent;
996 DIR *proc;
997 int pid;
999 int status;
1000 procstat_t ps;
1001 procstat_entry_t pse;
1002 char state;
1004 procstat_t *ps_ptr;
1006 running = sleeping = zombies = stopped = paging = blocked = 0;
1007 ps_list_reset ();
1009 if ((proc = opendir ("/proc")) == NULL)
1010 {
1011 syslog (LOG_ERR, "Cannot open `/proc': %s", strerror (errno));
1012 return (-1);
1013 }
1015 while ((ent = readdir (proc)) != NULL)
1016 {
1017 if (!isdigit (ent->d_name[0]))
1018 continue;
1020 if ((pid = atoi (ent->d_name)) < 1)
1021 continue;
1023 status = ps_read_process (pid, &ps, &state);
1024 if (status != 0)
1025 {
1026 DBG ("ps_read_process failed: %i", status);
1027 continue;
1028 }
1030 pse.id = pid;
1031 pse.age = 0;
1033 pse.num_proc = ps.num_proc;
1034 pse.num_lwp = ps.num_lwp;
1035 pse.vmem_rss = ps.vmem_rss;
1037 pse.vmem_minflt = 0;
1038 pse.vmem_minflt_counter = ps.vmem_minflt_counter;
1039 pse.vmem_majflt = 0;
1040 pse.vmem_majflt_counter = ps.vmem_majflt_counter;
1042 pse.cpu_user = 0;
1043 pse.cpu_user_counter = ps.cpu_user_counter;
1044 pse.cpu_system = 0;
1045 pse.cpu_system_counter = ps.cpu_system_counter;
1047 switch (state)
1048 {
1049 case 'R': running++; break;
1050 case 'S': sleeping++; break;
1051 case 'D': blocked++; break;
1052 case 'Z': zombies++; break;
1053 case 'T': stopped++; break;
1054 case 'W': paging++; break;
1055 }
1057 ps_list_add (ps.name, &pse);
1058 }
1060 closedir (proc);
1062 ps_submit_state ("running", running);
1063 ps_submit_state ("sleeping", sleeping);
1064 ps_submit_state ("zombies", zombies);
1065 ps_submit_state ("stopped", stopped);
1066 ps_submit_state ("paging", paging);
1067 ps_submit_state ("blocked", blocked);
1069 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1070 ps_submit_proc_list (ps_ptr);
1071 #endif /* KERNEL_LINUX */
1073 return (0);
1074 } /* int ps_read */
1075 #endif /* PROCESSES_HAVE_READ */
1077 void module_register (void)
1078 {
1079 plugin_register_data_set (&state_ds);
1080 plugin_register_data_set (&rss_ds);
1081 plugin_register_data_set (&time_ds);
1082 plugin_register_data_set (&count_ds );
1083 plugin_register_data_set (&pagefaults_ds );
1085 #if PROCESSES_HAVE_READ
1086 plugin_register_config ("processes", ps_config,
1087 config_keys, config_keys_num);
1088 plugin_register_init ("processes", ps_init);
1089 plugin_register_read ("processes", ps_read);
1090 #endif /* PROCESSES_HAVE_READ */
1091 }