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 #else
86 # error "No applicable input method."
87 #endif
89 #define BUFSIZE 256
91 static const char *config_keys[] =
92 {
93 "Process",
94 NULL
95 };
96 static int config_keys_num = 1;
98 typedef struct procstat_entry_s
99 {
100 unsigned long id;
101 unsigned long age;
103 unsigned long num_proc;
104 unsigned long num_lwp;
105 unsigned long vmem_rss;
107 unsigned long vmem_minflt;
108 unsigned long vmem_majflt;
109 unsigned long vmem_minflt_counter;
110 unsigned long vmem_majflt_counter;
112 unsigned long cpu_user;
113 unsigned long cpu_system;
114 unsigned long cpu_user_counter;
115 unsigned long cpu_system_counter;
117 struct procstat_entry_s *next;
118 } procstat_entry_t;
120 #define PROCSTAT_NAME_LEN 256
121 typedef struct procstat
122 {
123 char name[PROCSTAT_NAME_LEN];
125 unsigned long num_proc;
126 unsigned long num_lwp;
127 unsigned long vmem_rss;
129 unsigned long vmem_minflt_counter;
130 unsigned long vmem_majflt_counter;
132 unsigned long cpu_user_counter;
133 unsigned long cpu_system_counter;
135 struct procstat *next;
136 struct procstat_entry_s *instances;
137 } procstat_t;
139 static procstat_t *list_head_g = NULL;
141 #if HAVE_THREAD_INFO
142 static mach_port_t port_host_self;
143 static mach_port_t port_task_self;
145 static processor_set_name_array_t pset_list;
146 static mach_msg_type_number_t pset_list_len;
147 /* #endif HAVE_THREAD_INFO */
149 #elif KERNEL_LINUX
150 static long pagesize_g;
151 #endif /* KERNEL_LINUX */
153 static void ps_list_register (const char *name)
154 {
155 procstat_t *new;
156 procstat_t *ptr;
158 if ((new = (procstat_t *) malloc (sizeof (procstat_t))) == NULL)
159 return;
160 memset (new, 0, sizeof (procstat_t));
161 strncpy (new->name, name, PROCSTAT_NAME_LEN);
163 for (ptr = list_head_g; ptr != NULL; ptr = ptr->next)
164 {
165 if (strcmp (ptr->name, name) == 0)
166 return;
167 if (ptr->next == NULL)
168 break;
169 }
171 if (ptr == NULL)
172 list_head_g = new;
173 else
174 ptr->next = new;
175 }
177 static procstat_t *ps_list_search (const char *name)
178 {
179 procstat_t *ptr;
181 for (ptr = list_head_g; ptr != NULL; ptr = ptr->next)
182 if (strcmp (ptr->name, name) == 0)
183 break;
185 return (ptr);
186 }
188 static void ps_list_add (const char *name, procstat_entry_t *entry)
189 {
190 procstat_t *ps;
191 procstat_entry_t *pse;
193 if (entry->id == 0)
194 return;
196 if ((ps = ps_list_search (name)) == NULL)
197 return;
199 for (pse = ps->instances; pse != NULL; pse = pse->next)
200 if ((pse->id == entry->id) || (pse->next == NULL))
201 break;
203 if ((pse == NULL) || (pse->id != entry->id))
204 {
205 procstat_entry_t *new;
207 new = (procstat_entry_t *) malloc (sizeof (procstat_entry_t));
208 if (new == NULL)
209 return;
210 memset (new, 0, sizeof (procstat_entry_t));
211 new->id = entry->id;
213 if (pse == NULL)
214 ps->instances = new;
215 else
216 pse->next = new;
218 pse = new;
219 }
221 pse->age = 0;
222 pse->num_proc = entry->num_proc;
223 pse->num_lwp = entry->num_lwp;
224 pse->vmem_rss = entry->vmem_rss;
226 ps->num_proc += pse->num_proc;
227 ps->num_lwp += pse->num_lwp;
228 ps->vmem_rss += pse->vmem_rss;
230 if ((entry->vmem_minflt_counter == 0)
231 && (entry->vmem_majflt_counter == 0))
232 {
233 pse->vmem_minflt_counter += entry->vmem_minflt;
234 pse->vmem_minflt = entry->vmem_minflt;
236 pse->vmem_majflt_counter += entry->vmem_majflt;
237 pse->vmem_majflt = entry->vmem_majflt;
238 }
239 else
240 {
241 if (entry->vmem_minflt_counter < pse->vmem_minflt_counter)
242 {
243 pse->vmem_minflt = entry->vmem_minflt_counter
244 + (ULONG_MAX - pse->vmem_minflt_counter);
245 }
246 else
247 {
248 pse->vmem_minflt = entry->vmem_minflt_counter - pse->vmem_minflt_counter;
249 }
250 pse->vmem_minflt_counter = entry->vmem_minflt_counter;
252 if (entry->vmem_majflt_counter < pse->vmem_majflt_counter)
253 {
254 pse->vmem_majflt = entry->vmem_majflt_counter
255 + (ULONG_MAX - pse->vmem_majflt_counter);
256 }
257 else
258 {
259 pse->vmem_majflt = entry->vmem_majflt_counter - pse->vmem_majflt_counter;
260 }
261 pse->vmem_majflt_counter = entry->vmem_majflt_counter;
262 }
264 ps->vmem_minflt_counter += pse->vmem_minflt;
265 ps->vmem_majflt_counter += pse->vmem_majflt;
267 if ((entry->cpu_user_counter == 0)
268 && (entry->cpu_system_counter == 0))
269 {
270 pse->cpu_user_counter += entry->cpu_user;
271 pse->cpu_user = entry->cpu_user;
273 pse->cpu_system_counter += entry->cpu_system;
274 pse->cpu_system = entry->cpu_system;
275 }
276 else
277 {
278 if (entry->cpu_user_counter < pse->cpu_user_counter)
279 {
280 pse->cpu_user = entry->cpu_user_counter
281 + (ULONG_MAX - pse->cpu_user_counter);
282 }
283 else
284 {
285 pse->cpu_user = entry->cpu_user_counter - pse->cpu_user_counter;
286 }
287 pse->cpu_user_counter = entry->cpu_user_counter;
289 if (entry->cpu_system_counter < pse->cpu_system_counter)
290 {
291 pse->cpu_system = entry->cpu_system_counter
292 + (ULONG_MAX - pse->cpu_system_counter);
293 }
294 else
295 {
296 pse->cpu_system = entry->cpu_system_counter - pse->cpu_system_counter;
297 }
298 pse->cpu_system_counter = entry->cpu_system_counter;
299 }
301 ps->cpu_user_counter += pse->cpu_user;
302 ps->cpu_system_counter += pse->cpu_system;
303 }
305 static void ps_list_reset (void)
306 {
307 procstat_t *ps;
308 procstat_entry_t *pse;
309 procstat_entry_t *pse_prev;
311 for (ps = list_head_g; ps != NULL; ps = ps->next)
312 {
313 ps->num_proc = 0;
314 ps->num_lwp = 0;
315 ps->vmem_rss = 0;
317 pse_prev = NULL;
318 pse = ps->instances;
319 while (pse != NULL)
320 {
321 if (pse->age > 10)
322 {
323 DEBUG ("Removing this procstat entry cause it's too old: "
324 "id = %lu; name = %s;",
325 pse->id, ps->name);
327 if (pse_prev == NULL)
328 {
329 ps->instances = pse->next;
330 free (pse);
331 pse = ps->instances;
332 }
333 else
334 {
335 pse_prev->next = pse->next;
336 free (pse);
337 pse = pse_prev->next;
338 }
339 }
340 else
341 {
342 pse->age++;
343 pse_prev = pse;
344 pse = pse->next;
345 }
346 } /* while (pse != NULL) */
347 } /* for (ps = list_head_g; ps != NULL; ps = ps->next) */
348 }
350 static int ps_config (const char *key, const char *value)
351 {
352 if (strcasecmp (key, "Process") == 0)
353 {
354 ps_list_register (value);
355 }
356 else
357 {
358 return (-1);
359 }
361 return (0);
362 }
364 static int ps_init (void)
365 {
366 #if HAVE_THREAD_INFO
367 kern_return_t status;
369 port_host_self = mach_host_self ();
370 port_task_self = mach_task_self ();
372 if (pset_list != NULL)
373 {
374 vm_deallocate (port_task_self,
375 (vm_address_t) pset_list,
376 pset_list_len * sizeof (processor_set_t));
377 pset_list = NULL;
378 pset_list_len = 0;
379 }
381 if ((status = host_processor_sets (port_host_self,
382 &pset_list,
383 &pset_list_len)) != KERN_SUCCESS)
384 {
385 ERROR ("host_processor_sets failed: %s\n",
386 mach_error_string (status));
387 pset_list = NULL;
388 pset_list_len = 0;
389 return (-1);
390 }
391 /* #endif HAVE_THREAD_INFO */
393 #elif KERNEL_LINUX
394 pagesize_g = sysconf(_SC_PAGESIZE);
395 DEBUG ("pagesize_g = %li; CONFIG_HZ = %i;",
396 pagesize_g, CONFIG_HZ);
397 #endif /* KERNEL_LINUX */
399 return (0);
400 } /* int ps_init */
402 static void ps_submit_state (const char *state, double value)
403 {
404 value_t values[1];
405 value_list_t vl = VALUE_LIST_INIT;
407 values[0].gauge = value;
409 vl.values = values;
410 vl.values_len = 1;
411 vl.time = time (NULL);
412 strcpy (vl.host, hostname_g);
413 strcpy (vl.plugin, "processes");
414 strcpy (vl.plugin_instance, "");
415 strncpy (vl.type_instance, state, sizeof (vl.type_instance));
417 plugin_dispatch_values ("ps_state", &vl);
418 }
420 static void ps_submit_proc_list (procstat_t *ps)
421 {
422 value_t values[2];
423 value_list_t vl = VALUE_LIST_INIT;
425 vl.values = values;
426 vl.values_len = 2;
427 vl.time = time (NULL);
428 strcpy (vl.host, hostname_g);
429 strcpy (vl.plugin, "processes");
430 strncpy (vl.plugin_instance, ps->name, sizeof (vl.plugin_instance));
432 vl.values[0].gauge = ps->vmem_rss;
433 vl.values_len = 1;
434 plugin_dispatch_values ("ps_rss", &vl);
436 vl.values[0].counter = ps->cpu_user_counter;
437 vl.values[1].counter = ps->cpu_system_counter;
438 vl.values_len = 2;
439 plugin_dispatch_values ("ps_cputime", &vl);
441 vl.values[0].gauge = ps->num_proc;
442 vl.values[1].gauge = ps->num_lwp;
443 vl.values_len = 2;
444 plugin_dispatch_values ("ps_count", &vl);
446 vl.values[0].counter = ps->vmem_minflt_counter;
447 vl.values[1].counter = ps->vmem_majflt_counter;
448 vl.values_len = 2;
449 plugin_dispatch_values ("ps_pagefaults", &vl);
451 DEBUG ("name = %s; num_proc = %lu; num_lwp = %lu; vmem_rss = %lu; "
452 "vmem_minflt_counter = %lu; vmem_majflt_counter = %lu; "
453 "cpu_user_counter = %lu; cpu_system_counter = %lu;",
454 ps->name, ps->num_proc, ps->num_lwp, ps->vmem_rss,
455 ps->vmem_minflt_counter, ps->vmem_majflt_counter,
456 ps->cpu_user_counter, ps->cpu_system_counter);
457 } /* void ps_submit_proc_list */
459 #if KERNEL_LINUX
460 static int *ps_read_tasks (int pid)
461 {
462 int *list = NULL;
463 int list_size = 1; /* size of allocated space, in elements */
464 int list_len = 0; /* number of currently used elements */
466 char dirname[64];
467 DIR *dh;
468 struct dirent *ent;
470 snprintf (dirname, 64, "/proc/%i/task", pid);
471 dirname[63] = '\0';
473 if ((dh = opendir (dirname)) == NULL)
474 {
475 DEBUG ("Failed to open directory `%s'", dirname);
476 return (NULL);
477 }
479 while ((ent = readdir (dh)) != NULL)
480 {
481 if (!isdigit (ent->d_name[0]))
482 continue;
484 if ((list_len + 1) >= list_size)
485 {
486 int *new_ptr;
487 int new_size = 2 * list_size;
488 /* Comes in sizes: 2, 4, 8, 16, ... */
490 new_ptr = (int *) realloc (list, (size_t) (sizeof (int) * new_size));
491 if (new_ptr == NULL)
492 {
493 if (list != NULL)
494 free (list);
495 ERROR ("processes plugin: "
496 "Failed to allocate more memory.");
497 return (NULL);
498 }
500 list = new_ptr;
501 list_size = new_size;
503 memset (list + list_len, 0, sizeof (int) * (list_size - list_len));
504 }
506 list[list_len] = atoi (ent->d_name);
507 if (list[list_len] != 0)
508 list_len++;
509 }
511 closedir (dh);
513 if (list_len == 0)
514 return (NULL);
516 assert (list_len < list_size);
517 assert (list[list_len] == 0);
519 return (list);
520 } /* int *ps_read_tasks */
522 int ps_read_process (int pid, procstat_t *ps, char *state)
523 {
524 char filename[64];
525 char buffer[1024];
526 FILE *fh;
528 char *fields[64];
529 char fields_len;
531 int *tasks;
532 int i;
534 int ppid;
535 int name_len;
537 long long unsigned cpu_user_counter;
538 long long unsigned cpu_system_counter;
539 long long unsigned vmem_rss;
541 memset (ps, 0, sizeof (procstat_t));
543 snprintf (filename, 64, "/proc/%i/stat", pid);
544 filename[63] = '\0';
546 if ((fh = fopen (filename, "r")) == NULL)
547 return (-1);
549 if (fgets (buffer, 1024, fh) == NULL)
550 {
551 fclose (fh);
552 return (-1);
553 }
555 fclose (fh);
557 fields_len = strsplit (buffer, fields, 64);
558 if (fields_len < 24)
559 {
560 DEBUG ("processes plugin: ps_read_process (pid = %i):"
561 " `%s' has only %i fields..",
562 (int) pid, filename, fields_len);
563 return (-1);
564 }
566 /* copy the name, strip brackets in the process */
567 name_len = strlen (fields[1]) - 2;
568 if ((fields[1][0] != '(') || (fields[1][name_len + 1] != ')'))
569 {
570 DEBUG ("No brackets found in process name: `%s'", fields[1]);
571 return (-1);
572 }
573 fields[1] = fields[1] + 1;
574 fields[1][name_len] = '\0';
575 strncpy (ps->name, fields[1], PROCSTAT_NAME_LEN);
577 ppid = atoi (fields[3]);
579 *state = fields[2][0];
581 if (*state == 'Z')
582 {
583 ps->num_lwp = 0;
584 ps->num_proc = 0;
585 }
586 else if ((tasks = ps_read_tasks (pid)) == NULL)
587 {
588 /* Kernel 2.4 or so */
589 ps->num_lwp = 1;
590 ps->num_proc = 1;
591 }
592 else
593 {
594 ps->num_lwp = 0;
595 ps->num_proc = 1;
596 for (i = 0; tasks[i] != 0; i++)
597 ps->num_lwp++;
599 free (tasks);
600 tasks = NULL;
601 }
603 /* Leave the rest at zero if this is only a zombi */
604 if (ps->num_proc == 0)
605 {
606 DEBUG ("processes plugin: This is only a zombi: pid = %i; "
607 "name = %s;", pid, ps->name);
608 return (0);
609 }
611 cpu_user_counter = atoll (fields[13]);
612 cpu_system_counter = atoll (fields[14]);
613 vmem_rss = atoll (fields[23]);
614 ps->vmem_minflt_counter = atol (fields[9]);
615 ps->vmem_majflt_counter = atol (fields[11]);
617 /* Convert jiffies to useconds */
618 cpu_user_counter = cpu_user_counter * 1000000 / CONFIG_HZ;
619 cpu_system_counter = cpu_system_counter * 1000000 / CONFIG_HZ;
620 vmem_rss = vmem_rss * pagesize_g;
622 ps->cpu_user_counter = (unsigned long) cpu_user_counter;
623 ps->cpu_system_counter = (unsigned long) cpu_system_counter;
624 ps->vmem_rss = (unsigned long) vmem_rss;
626 /* success */
627 return (0);
628 } /* int ps_read_process (...) */
629 #endif /* KERNEL_LINUX */
631 #if HAVE_THREAD_INFO
632 static int mach_get_task_name (task_t t, int *pid, char *name, size_t name_max_len)
633 {
634 int mib[4];
636 struct kinfo_proc kp;
637 size_t kp_size;
639 mib[0] = CTL_KERN;
640 mib[1] = KERN_PROC;
641 mib[2] = KERN_PROC_PID;
643 if (pid_for_task (t, pid) != KERN_SUCCESS)
644 return (-1);
645 mib[3] = *pid;
647 kp_size = sizeof (kp);
648 if (sysctl (mib, 4, &kp, &kp_size, NULL, 0) != 0)
649 return (-1);
651 if (name_max_len > (MAXCOMLEN + 1))
652 name_max_len = MAXCOMLEN + 1;
654 strncpy (name, kp.kp_proc.p_comm, name_max_len - 1);
655 name[name_max_len - 1] = '\0';
657 DEBUG ("pid = %i; name = %s;", *pid, name);
659 /* We don't do the special handling for `p_comm == "LaunchCFMApp"' as
660 * `top' does it, because it is a lot of work and only used when
661 * debugging. -octo */
663 return (0);
664 }
665 #endif /* HAVE_THREAD_INFO */
667 static int ps_read (void)
668 {
669 #if HAVE_THREAD_INFO
670 kern_return_t status;
672 int pset;
673 processor_set_t port_pset_priv;
675 int task;
676 task_array_t task_list;
677 mach_msg_type_number_t task_list_len;
679 int task_pid;
680 char task_name[MAXCOMLEN + 1];
682 int thread;
683 thread_act_array_t thread_list;
684 mach_msg_type_number_t thread_list_len;
685 thread_basic_info_data_t thread_data;
686 mach_msg_type_number_t thread_data_len;
688 int running = 0;
689 int sleeping = 0;
690 int zombies = 0;
691 int stopped = 0;
692 int blocked = 0;
694 procstat_t *ps;
695 procstat_entry_t pse;
697 ps_list_reset ();
699 /*
700 * The Mach-concept is a little different from the traditional UNIX
701 * concept: All the work is done in threads. Threads are contained in
702 * `tasks'. Therefore, `task status' doesn't make much sense, since
703 * it's actually a `thread status'.
704 * Tasks are assigned to sets of processors, so that's where you go to
705 * get a list.
706 */
707 for (pset = 0; pset < pset_list_len; pset++)
708 {
709 if ((status = host_processor_set_priv (port_host_self,
710 pset_list[pset],
711 &port_pset_priv)) != KERN_SUCCESS)
712 {
713 ERROR ("host_processor_set_priv failed: %s\n",
714 mach_error_string (status));
715 continue;
716 }
718 if ((status = processor_set_tasks (port_pset_priv,
719 &task_list,
720 &task_list_len)) != KERN_SUCCESS)
721 {
722 ERROR ("processor_set_tasks failed: %s\n",
723 mach_error_string (status));
724 mach_port_deallocate (port_task_self, port_pset_priv);
725 continue;
726 }
728 for (task = 0; task < task_list_len; task++)
729 {
730 ps = NULL;
731 if (mach_get_task_name (task_list[task],
732 &task_pid,
733 task_name, PROCSTAT_NAME_LEN) == 0)
734 ps = ps_list_search (task_name);
736 /* Collect more detailed statistics for this process */
737 if (ps != NULL)
738 {
739 task_basic_info_data_t task_basic_info;
740 mach_msg_type_number_t task_basic_info_len;
741 task_events_info_data_t task_events_info;
742 mach_msg_type_number_t task_events_info_len;
743 task_absolutetime_info_data_t task_absolutetime_info;
744 mach_msg_type_number_t task_absolutetime_info_len;
746 memset (&pse, '\0', sizeof (pse));
747 pse.id = task_pid;
749 task_basic_info_len = TASK_BASIC_INFO_COUNT;
750 status = task_info (task_list[task],
751 TASK_BASIC_INFO,
752 (task_info_t) &task_basic_info,
753 &task_basic_info_len);
754 if (status != KERN_SUCCESS)
755 {
756 ERROR ("task_info failed: %s",
757 mach_error_string (status));
758 continue; /* with next thread_list */
759 }
761 task_events_info_len = TASK_EVENTS_INFO_COUNT;
762 status = task_info (task_list[task],
763 TASK_EVENTS_INFO,
764 (task_info_t) &task_events_info,
765 &task_events_info_len);
766 if (status != KERN_SUCCESS)
767 {
768 ERROR ("task_info failed: %s",
769 mach_error_string (status));
770 continue; /* with next thread_list */
771 }
773 task_absolutetime_info_len = TASK_ABSOLUTETIME_INFO_COUNT;
774 status = task_info (task_list[task],
775 TASK_ABSOLUTETIME_INFO,
776 (task_info_t) &task_absolutetime_info,
777 &task_absolutetime_info_len);
778 if (status != KERN_SUCCESS)
779 {
780 ERROR ("task_info failed: %s",
781 mach_error_string (status));
782 continue; /* with next thread_list */
783 }
785 pse.num_proc++;
786 pse.vmem_rss = task_basic_info.resident_size;
788 pse.vmem_minflt_counter = task_events_info.cow_faults;
789 pse.vmem_majflt_counter = task_events_info.faults;
791 pse.cpu_user_counter = task_absolutetime_info.total_user;
792 pse.cpu_system_counter = task_absolutetime_info.total_system;
793 }
795 status = task_threads (task_list[task], &thread_list,
796 &thread_list_len);
797 if (status != KERN_SUCCESS)
798 {
799 /* Apple's `top' treats this case a zombie. It
800 * makes sense to some extend: A `zombie'
801 * thread is nonsense, since the task/process
802 * is dead. */
803 zombies++;
804 DEBUG ("task_threads failed: %s",
805 mach_error_string (status));
806 if (task_list[task] != port_task_self)
807 mach_port_deallocate (port_task_self,
808 task_list[task]);
809 continue; /* with next task_list */
810 }
812 for (thread = 0; thread < thread_list_len; thread++)
813 {
814 thread_data_len = THREAD_BASIC_INFO_COUNT;
815 status = thread_info (thread_list[thread],
816 THREAD_BASIC_INFO,
817 (thread_info_t) &thread_data,
818 &thread_data_len);
819 if (status != KERN_SUCCESS)
820 {
821 ERROR ("thread_info failed: %s",
822 mach_error_string (status));
823 if (task_list[task] != port_task_self)
824 mach_port_deallocate (port_task_self,
825 thread_list[thread]);
826 continue; /* with next thread_list */
827 }
829 if (ps != NULL)
830 pse.num_lwp++;
832 switch (thread_data.run_state)
833 {
834 case TH_STATE_RUNNING:
835 running++;
836 break;
837 case TH_STATE_STOPPED:
838 /* What exactly is `halted'? */
839 case TH_STATE_HALTED:
840 stopped++;
841 break;
842 case TH_STATE_WAITING:
843 sleeping++;
844 break;
845 case TH_STATE_UNINTERRUPTIBLE:
846 blocked++;
847 break;
848 /* There is no `zombie' case here,
849 * since there are no zombie-threads.
850 * There's only zombie tasks, which are
851 * handled above. */
852 default:
853 WARNING ("Unknown thread status: %s",
854 thread_data.run_state);
855 break;
856 } /* switch (thread_data.run_state) */
858 if (task_list[task] != port_task_self)
859 {
860 status = mach_port_deallocate (port_task_self,
861 thread_list[thread]);
862 if (status != KERN_SUCCESS)
863 ERROR ("mach_port_deallocate failed: %s",
864 mach_error_string (status));
865 }
866 } /* for (thread_list) */
868 if ((status = vm_deallocate (port_task_self,
869 (vm_address_t) thread_list,
870 thread_list_len * sizeof (thread_act_t)))
871 != KERN_SUCCESS)
872 {
873 ERROR ("vm_deallocate failed: %s",
874 mach_error_string (status));
875 }
876 thread_list = NULL;
877 thread_list_len = 0;
879 /* Only deallocate the task port, if it isn't our own.
880 * Don't know what would happen in that case, but this
881 * is what Apple's top does.. ;) */
882 if (task_list[task] != port_task_self)
883 {
884 status = mach_port_deallocate (port_task_self,
885 task_list[task]);
886 if (status != KERN_SUCCESS)
887 ERROR ("mach_port_deallocate failed: %s",
888 mach_error_string (status));
889 }
891 if (ps != NULL)
892 ps_list_add (task_name, &pse);
893 } /* for (task_list) */
895 if ((status = vm_deallocate (port_task_self,
896 (vm_address_t) task_list,
897 task_list_len * sizeof (task_t))) != KERN_SUCCESS)
898 {
899 ERROR ("vm_deallocate failed: %s",
900 mach_error_string (status));
901 }
902 task_list = NULL;
903 task_list_len = 0;
905 if ((status = mach_port_deallocate (port_task_self, port_pset_priv))
906 != KERN_SUCCESS)
907 {
908 ERROR ("mach_port_deallocate failed: %s",
909 mach_error_string (status));
910 }
911 } /* for (pset_list) */
913 ps_submit_state ("running", running);
914 ps_submit_state ("sleeping", sleeping);
915 ps_submit_state ("zombies", zombies);
916 ps_submit_state ("stopped", stopped);
917 ps_submit_state ("blocked", blocked);
919 for (ps = list_head_g; ps != NULL; ps = ps->next)
920 ps_submit_proc_list (ps);
921 /* #endif HAVE_THREAD_INFO */
923 #elif KERNEL_LINUX
924 int running = 0;
925 int sleeping = 0;
926 int zombies = 0;
927 int stopped = 0;
928 int paging = 0;
929 int blocked = 0;
931 struct dirent *ent;
932 DIR *proc;
933 int pid;
935 int status;
936 procstat_t ps;
937 procstat_entry_t pse;
938 char state;
940 procstat_t *ps_ptr;
942 running = sleeping = zombies = stopped = paging = blocked = 0;
943 ps_list_reset ();
945 if ((proc = opendir ("/proc")) == NULL)
946 {
947 char errbuf[1024];
948 ERROR ("Cannot open `/proc': %s",
949 sstrerror (errno, errbuf, sizeof (errbuf)));
950 return (-1);
951 }
953 while ((ent = readdir (proc)) != NULL)
954 {
955 if (!isdigit (ent->d_name[0]))
956 continue;
958 if ((pid = atoi (ent->d_name)) < 1)
959 continue;
961 status = ps_read_process (pid, &ps, &state);
962 if (status != 0)
963 {
964 DEBUG ("ps_read_process failed: %i", status);
965 continue;
966 }
968 pse.id = pid;
969 pse.age = 0;
971 pse.num_proc = ps.num_proc;
972 pse.num_lwp = ps.num_lwp;
973 pse.vmem_rss = ps.vmem_rss;
975 pse.vmem_minflt = 0;
976 pse.vmem_minflt_counter = ps.vmem_minflt_counter;
977 pse.vmem_majflt = 0;
978 pse.vmem_majflt_counter = ps.vmem_majflt_counter;
980 pse.cpu_user = 0;
981 pse.cpu_user_counter = ps.cpu_user_counter;
982 pse.cpu_system = 0;
983 pse.cpu_system_counter = ps.cpu_system_counter;
985 switch (state)
986 {
987 case 'R': running++; break;
988 case 'S': sleeping++; break;
989 case 'D': blocked++; break;
990 case 'Z': zombies++; break;
991 case 'T': stopped++; break;
992 case 'W': paging++; break;
993 }
995 ps_list_add (ps.name, &pse);
996 }
998 closedir (proc);
1000 ps_submit_state ("running", running);
1001 ps_submit_state ("sleeping", sleeping);
1002 ps_submit_state ("zombies", zombies);
1003 ps_submit_state ("stopped", stopped);
1004 ps_submit_state ("paging", paging);
1005 ps_submit_state ("blocked", blocked);
1007 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1008 ps_submit_proc_list (ps_ptr);
1009 #endif /* KERNEL_LINUX */
1011 return (0);
1012 } /* int ps_read */
1014 void module_register (void)
1015 {
1016 plugin_register_config ("processes", ps_config,
1017 config_keys, config_keys_num);
1018 plugin_register_init ("processes", ps_init);
1019 plugin_register_read ("processes", ps_read);
1020 } /* void module_register */