7d918d4fd5645a952a5fadcc2cd559cae21df95d
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 #define MODULE_NAME "processes"
87 #if HAVE_THREAD_INFO || KERNEL_LINUX
88 # define PROCESSES_HAVE_READ 1
89 #else
90 # define PROCESSES_HAVE_READ 0
91 #endif
93 #define BUFSIZE 256
95 static data_source_t state_dsrc[1] =
96 {
97 {"value", DS_TYPE_GAUGE, 0.0, 65535.0}
98 };
100 static data_set_t state_ds =
101 {
102 "ps_state", 1, state_dsrc
103 };
105 static data_source_t rss_dsrc[1] =
106 {
107 /* max = 2^63 - 1 */
108 {"value", DS_TYPE_GAUGE, 0.0, 9223372036854775807.0}
109 };
111 static data_set_t rss_ds =
112 {
113 "ps_rss", 1, rss_dsrc
114 };
116 static data_source_t time_dsrc[2] =
117 {
118 /* 1 second in user-mode per second ought to be enough.. */
119 {"user", DS_TYPE_COUNTER, 0.0, 1000000.0},
120 {"syst", DS_TYPE_COUNTER, 0.0, 1000000.0}
121 };
123 static data_set_t time_ds =
124 {
125 "ps_cputime", 2, time_dsrc
126 };
128 static data_source_t count_dsrc[2] =
129 {
130 /* 1 second in user-mode per second ought to be enough.. */
131 {"processes", DS_TYPE_GAUGE, 0.0, 1000000.0},
132 {"threads", DS_TYPE_GAUGE, 0.0, 1000000.0}
133 };
135 static data_set_t count_ds =
136 {
137 "ps_count", 2, count_dsrc
138 };
140 static data_source_t pagefaults_dsrc[2] =
141 {
142 /* max = 2^63 - 1 */
143 {"minflt", DS_TYPE_COUNTER, 0.0, 9223372036854775807.0},
144 {"majflt", DS_TYPE_COUNTER, 0.0, 9223372036854775807.0}
145 };
147 static data_set_t pagefaults_ds =
148 {
149 "ps_pagefaults", 2, pagefaults_dsrc
150 };
152 #if PROCESSES_HAVE_READ
153 #if HAVE_THREAD_INFO | KERNEL_LINUX
154 static const char *config_keys[] =
155 {
156 "Process",
157 NULL
158 };
159 static int config_keys_num = 1;
160 #endif
162 typedef struct procstat_entry_s
163 {
164 unsigned long id;
165 unsigned long age;
167 unsigned long num_proc;
168 unsigned long num_lwp;
169 unsigned long vmem_rss;
171 unsigned long vmem_minflt;
172 unsigned long vmem_majflt;
173 unsigned long vmem_minflt_counter;
174 unsigned long vmem_majflt_counter;
176 unsigned long cpu_user;
177 unsigned long cpu_system;
178 unsigned long cpu_user_counter;
179 unsigned long cpu_system_counter;
181 struct procstat_entry_s *next;
182 } procstat_entry_t;
184 #define PROCSTAT_NAME_LEN 256
185 typedef struct procstat
186 {
187 char name[PROCSTAT_NAME_LEN];
189 unsigned long num_proc;
190 unsigned long num_lwp;
191 unsigned long vmem_rss;
193 unsigned long vmem_minflt_counter;
194 unsigned long vmem_majflt_counter;
196 unsigned long cpu_user_counter;
197 unsigned long cpu_system_counter;
199 struct procstat *next;
200 struct procstat_entry_s *instances;
201 } procstat_t;
203 #if HAVE_THREAD_INFO | KERNEL_LINUX
204 static procstat_t *list_head_g = NULL;
205 #endif
207 #if HAVE_THREAD_INFO
208 static mach_port_t port_host_self;
209 static mach_port_t port_task_self;
211 static processor_set_name_array_t pset_list;
212 static mach_msg_type_number_t pset_list_len;
213 /* #endif HAVE_THREAD_INFO */
215 #elif KERNEL_LINUX
216 static long pagesize_g;
217 #endif /* KERNEL_LINUX */
219 #if HAVE_THREAD_INFO | KERNEL_LINUX
220 static void ps_list_register (const char *name)
221 {
222 procstat_t *new;
223 procstat_t *ptr;
225 if ((new = (procstat_t *) malloc (sizeof (procstat_t))) == NULL)
226 return;
227 memset (new, 0, sizeof (procstat_t));
228 strncpy (new->name, name, PROCSTAT_NAME_LEN);
230 for (ptr = list_head_g; ptr != NULL; ptr = ptr->next)
231 {
232 if (strcmp (ptr->name, name) == 0)
233 return;
234 if (ptr->next == NULL)
235 break;
236 }
238 if (ptr == NULL)
239 list_head_g = new;
240 else
241 ptr->next = new;
242 }
244 static procstat_t *ps_list_search (const char *name)
245 {
246 procstat_t *ptr;
248 for (ptr = list_head_g; ptr != NULL; ptr = ptr->next)
249 if (strcmp (ptr->name, name) == 0)
250 break;
252 return (ptr);
253 }
255 static void ps_list_add (const char *name, procstat_entry_t *entry)
256 {
257 procstat_t *ps;
258 procstat_entry_t *pse;
260 if (entry->id == 0)
261 return;
263 if ((ps = ps_list_search (name)) == NULL)
264 return;
266 for (pse = ps->instances; pse != NULL; pse = pse->next)
267 if ((pse->id == entry->id) || (pse->next == NULL))
268 break;
270 if ((pse == NULL) || (pse->id != entry->id))
271 {
272 procstat_entry_t *new;
274 new = (procstat_entry_t *) malloc (sizeof (procstat_entry_t));
275 if (new == NULL)
276 return;
277 memset (new, 0, sizeof (procstat_entry_t));
278 new->id = entry->id;
280 if (pse == NULL)
281 ps->instances = new;
282 else
283 pse->next = new;
285 pse = new;
286 }
288 pse->age = 0;
289 pse->num_proc = entry->num_proc;
290 pse->num_lwp = entry->num_lwp;
291 pse->vmem_rss = entry->vmem_rss;
293 ps->num_proc += pse->num_proc;
294 ps->num_lwp += pse->num_lwp;
295 ps->vmem_rss += pse->vmem_rss;
297 if ((entry->vmem_minflt_counter == 0)
298 && (entry->vmem_majflt_counter == 0))
299 {
300 pse->vmem_minflt_counter += entry->vmem_minflt;
301 pse->vmem_minflt = entry->vmem_minflt;
303 pse->vmem_majflt_counter += entry->vmem_majflt;
304 pse->vmem_majflt = entry->vmem_majflt;
305 }
306 else
307 {
308 if (entry->vmem_minflt_counter < pse->vmem_minflt_counter)
309 {
310 pse->vmem_minflt = entry->vmem_minflt_counter
311 + (ULONG_MAX - pse->vmem_minflt_counter);
312 }
313 else
314 {
315 pse->vmem_minflt = entry->vmem_minflt_counter - pse->vmem_minflt_counter;
316 }
317 pse->vmem_minflt_counter = entry->vmem_minflt_counter;
319 if (entry->vmem_majflt_counter < pse->vmem_majflt_counter)
320 {
321 pse->vmem_majflt = entry->vmem_majflt_counter
322 + (ULONG_MAX - pse->vmem_majflt_counter);
323 }
324 else
325 {
326 pse->vmem_majflt = entry->vmem_majflt_counter - pse->vmem_majflt_counter;
327 }
328 pse->vmem_majflt_counter = entry->vmem_majflt_counter;
329 }
331 ps->vmem_minflt_counter += pse->vmem_minflt;
332 ps->vmem_majflt_counter += pse->vmem_majflt;
334 if ((entry->cpu_user_counter == 0)
335 && (entry->cpu_system_counter == 0))
336 {
337 pse->cpu_user_counter += entry->cpu_user;
338 pse->cpu_user = entry->cpu_user;
340 pse->cpu_system_counter += entry->cpu_system;
341 pse->cpu_system = entry->cpu_system;
342 }
343 else
344 {
345 if (entry->cpu_user_counter < pse->cpu_user_counter)
346 {
347 pse->cpu_user = entry->cpu_user_counter
348 + (ULONG_MAX - pse->cpu_user_counter);
349 }
350 else
351 {
352 pse->cpu_user = entry->cpu_user_counter - pse->cpu_user_counter;
353 }
354 pse->cpu_user_counter = entry->cpu_user_counter;
356 if (entry->cpu_system_counter < pse->cpu_system_counter)
357 {
358 pse->cpu_system = entry->cpu_system_counter
359 + (ULONG_MAX - pse->cpu_system_counter);
360 }
361 else
362 {
363 pse->cpu_system = entry->cpu_system_counter - pse->cpu_system_counter;
364 }
365 pse->cpu_system_counter = entry->cpu_system_counter;
366 }
368 ps->cpu_user_counter += pse->cpu_user;
369 ps->cpu_system_counter += pse->cpu_system;
370 }
372 static void ps_list_reset (void)
373 {
374 procstat_t *ps;
375 procstat_entry_t *pse;
376 procstat_entry_t *pse_prev;
378 for (ps = list_head_g; ps != NULL; ps = ps->next)
379 {
380 ps->num_proc = 0;
381 ps->num_lwp = 0;
382 ps->vmem_rss = 0;
384 pse_prev = NULL;
385 pse = ps->instances;
386 while (pse != NULL)
387 {
388 if (pse->age > 10)
389 {
390 DEBUG ("Removing this procstat entry cause it's too old: "
391 "id = %lu; name = %s;",
392 pse->id, ps->name);
394 if (pse_prev == NULL)
395 {
396 ps->instances = pse->next;
397 free (pse);
398 pse = ps->instances;
399 }
400 else
401 {
402 pse_prev->next = pse->next;
403 free (pse);
404 pse = pse_prev->next;
405 }
406 }
407 else
408 {
409 pse->age++;
410 pse_prev = pse;
411 pse = pse->next;
412 }
413 } /* while (pse != NULL) */
414 } /* for (ps = list_head_g; ps != NULL; ps = ps->next) */
415 }
417 static int ps_config (const char *key, const char *value)
418 {
419 if (strcasecmp (key, "Process") == 0)
420 {
421 ps_list_register (value);
422 }
423 else
424 {
425 return (-1);
426 }
428 return (0);
429 }
430 #endif /* HAVE_THREAD_INFO | KERNEL_LINUX */
432 static int ps_init (void)
433 {
434 #if HAVE_THREAD_INFO
435 kern_return_t status;
437 port_host_self = mach_host_self ();
438 port_task_self = mach_task_self ();
440 if (pset_list != NULL)
441 {
442 vm_deallocate (port_task_self,
443 (vm_address_t) pset_list,
444 pset_list_len * sizeof (processor_set_t));
445 pset_list = NULL;
446 pset_list_len = 0;
447 }
449 if ((status = host_processor_sets (port_host_self,
450 &pset_list,
451 &pset_list_len)) != KERN_SUCCESS)
452 {
453 ERROR ("host_processor_sets failed: %s\n",
454 mach_error_string (status));
455 pset_list = NULL;
456 pset_list_len = 0;
457 return (-1);
458 }
459 /* #endif HAVE_THREAD_INFO */
461 #elif KERNEL_LINUX
462 pagesize_g = sysconf(_SC_PAGESIZE);
463 DEBUG ("pagesize_g = %li; CONFIG_HZ = %i;",
464 pagesize_g, CONFIG_HZ);
465 #endif /* KERNEL_LINUX */
467 return (0);
468 } /* int ps_init */
470 static void ps_submit_state (const char *state, double value)
471 {
472 value_t values[1];
473 value_list_t vl = VALUE_LIST_INIT;
475 values[0].gauge = value;
477 vl.values = values;
478 vl.values_len = 1;
479 vl.time = time (NULL);
480 strcpy (vl.host, hostname_g);
481 strcpy (vl.plugin, "processes");
482 strcpy (vl.plugin_instance, "");
483 strncpy (vl.type_instance, state, sizeof (vl.type_instance));
485 plugin_dispatch_values ("ps_state", &vl);
486 }
488 static void ps_submit_proc_list (procstat_t *ps)
489 {
490 value_t values[2];
491 value_list_t vl = VALUE_LIST_INIT;
493 vl.values = values;
494 vl.values_len = 2;
495 vl.time = time (NULL);
496 strcpy (vl.host, hostname_g);
497 strcpy (vl.plugin, "processes");
498 strncpy (vl.plugin_instance, ps->name, sizeof (vl.plugin_instance));
500 vl.values[0].gauge = ps->vmem_rss;
501 vl.values_len = 1;
502 plugin_dispatch_values ("ps_rss", &vl);
504 vl.values[0].counter = ps->cpu_user_counter;
505 vl.values[1].counter = ps->cpu_system_counter;
506 vl.values_len = 2;
507 plugin_dispatch_values ("ps_cputime", &vl);
509 vl.values[0].gauge = ps->num_proc;
510 vl.values[1].gauge = ps->num_lwp;
511 vl.values_len = 2;
512 plugin_dispatch_values ("ps_count", &vl);
514 vl.values[0].counter = ps->vmem_minflt_counter;
515 vl.values[1].counter = ps->vmem_majflt_counter;
516 vl.values_len = 2;
517 plugin_dispatch_values ("ps_pagefaults", &vl);
519 DEBUG ("name = %s; num_proc = %lu; num_lwp = %lu; vmem_rss = %lu; "
520 "vmem_minflt_counter = %lu; vmem_majflt_counter = %lu; "
521 "cpu_user_counter = %lu; cpu_system_counter = %lu;",
522 ps->name, ps->num_proc, ps->num_lwp, ps->vmem_rss,
523 ps->vmem_minflt_counter, ps->vmem_majflt_counter,
524 ps->cpu_user_counter, ps->cpu_system_counter);
525 } /* void ps_submit_proc_list */
527 #if KERNEL_LINUX
528 static int *ps_read_tasks (int pid)
529 {
530 int *list = NULL;
531 int list_size = 1; /* size of allocated space, in elements */
532 int list_len = 0; /* number of currently used elements */
534 char dirname[64];
535 DIR *dh;
536 struct dirent *ent;
538 snprintf (dirname, 64, "/proc/%i/task", pid);
539 dirname[63] = '\0';
541 if ((dh = opendir (dirname)) == NULL)
542 {
543 DEBUG ("Failed to open directory `%s'", dirname);
544 return (NULL);
545 }
547 while ((ent = readdir (dh)) != NULL)
548 {
549 if (!isdigit (ent->d_name[0]))
550 continue;
552 if ((list_len + 1) >= list_size)
553 {
554 int *new_ptr;
555 int new_size = 2 * list_size;
556 /* Comes in sizes: 2, 4, 8, 16, ... */
558 new_ptr = (int *) realloc (list, (size_t) (sizeof (int) * new_size));
559 if (new_ptr == NULL)
560 {
561 if (list != NULL)
562 free (list);
563 ERROR ("processes plugin: "
564 "Failed to allocate more memory.");
565 return (NULL);
566 }
568 list = new_ptr;
569 list_size = new_size;
571 memset (list + list_len, 0, sizeof (int) * (list_size - list_len));
572 }
574 list[list_len] = atoi (ent->d_name);
575 if (list[list_len] != 0)
576 list_len++;
577 }
579 closedir (dh);
581 assert (list_len < list_size);
582 assert (list[list_len] == 0);
584 return (list);
585 }
587 int ps_read_process (int pid, procstat_t *ps, char *state)
588 {
589 char filename[64];
590 char buffer[1024];
591 FILE *fh;
593 char *fields[64];
594 char fields_len;
596 int *tasks;
597 int i;
599 int ppid;
600 int name_len;
602 long long unsigned cpu_user_counter;
603 long long unsigned cpu_system_counter;
604 long long unsigned vmem_rss;
606 memset (ps, 0, sizeof (procstat_t));
608 snprintf (filename, 64, "/proc/%i/stat", pid);
609 filename[63] = '\0';
611 if ((fh = fopen (filename, "r")) == NULL)
612 return (-1);
614 if (fgets (buffer, 1024, fh) == NULL)
615 {
616 fclose (fh);
617 return (-1);
618 }
620 fclose (fh);
622 fields_len = strsplit (buffer, fields, 64);
623 if (fields_len < 24)
624 {
625 DEBUG ("`%s' has only %i fields..",
626 filename, fields_len);
627 return (-1);
628 }
629 else if (fields_len != 41)
630 {
631 DEBUG ("WARNING: (fields_len = %i) != 41", fields_len);
632 }
634 /* copy the name, strip brackets in the process */
635 name_len = strlen (fields[1]) - 2;
636 if ((fields[1][0] != '(') || (fields[1][name_len + 1] != ')'))
637 {
638 DEBUG ("No brackets found in process name: `%s'", fields[1]);
639 return (-1);
640 }
641 fields[1] = fields[1] + 1;
642 fields[1][name_len] = '\0';
643 strncpy (ps->name, fields[1], PROCSTAT_NAME_LEN);
645 ppid = atoi (fields[3]);
647 if ((tasks = ps_read_tasks (pid)) == NULL)
648 {
649 /* This happends for zombied, e.g. */
650 DEBUG ("ps_read_tasks (%i) failed.", pid);
651 *state = 'Z';
652 ps->num_lwp = 0;
653 ps->num_proc = 0;
654 }
655 else
656 {
657 *state = '\0';
658 ps->num_lwp = 0;
659 ps->num_proc = 1;
660 for (i = 0; tasks[i] != 0; i++)
661 ps->num_lwp++;
663 free (tasks);
664 tasks = NULL;
665 }
667 /* Leave the rest at zero if this is only an LWP */
668 if (ps->num_proc == 0)
669 {
670 DEBUG ("This is only an LWP: pid = %i; name = %s;",
671 pid, ps->name);
672 return (0);
673 }
675 cpu_user_counter = atoll (fields[13]);
676 cpu_system_counter = atoll (fields[14]);
677 vmem_rss = atoll (fields[23]);
678 ps->vmem_minflt_counter = atol (fields[9]);
679 ps->vmem_majflt_counter = atol (fields[11]);
681 /* Convert jiffies to useconds */
682 cpu_user_counter = cpu_user_counter * 1000000 / CONFIG_HZ;
683 cpu_system_counter = cpu_system_counter * 1000000 / CONFIG_HZ;
684 vmem_rss = vmem_rss * pagesize_g;
686 ps->cpu_user_counter = (unsigned long) cpu_user_counter;
687 ps->cpu_system_counter = (unsigned long) cpu_system_counter;
688 ps->vmem_rss = (unsigned long) vmem_rss;
690 *state = fields[2][0];
692 /* success */
693 return (0);
694 } /* int ps_read_process (...) */
695 #endif /* KERNEL_LINUX */
697 #if HAVE_THREAD_INFO
698 static int mach_get_task_name (task_t t, int *pid, char *name, size_t name_max_len)
699 {
700 int mib[4];
702 struct kinfo_proc kp;
703 size_t kp_size;
705 mib[0] = CTL_KERN;
706 mib[1] = KERN_PROC;
707 mib[2] = KERN_PROC_PID;
709 if (pid_for_task (t, pid) != KERN_SUCCESS)
710 return (-1);
711 mib[3] = *pid;
713 kp_size = sizeof (kp);
714 if (sysctl (mib, 4, &kp, &kp_size, NULL, 0) != 0)
715 return (-1);
717 if (name_max_len > (MAXCOMLEN + 1))
718 name_max_len = MAXCOMLEN + 1;
720 strncpy (name, kp.kp_proc.p_comm, name_max_len - 1);
721 name[name_max_len - 1] = '\0';
723 DEBUG ("pid = %i; name = %s;", *pid, name);
725 /* We don't do the special handling for `p_comm == "LaunchCFMApp"' as
726 * `top' does it, because it is a lot of work and only used when
727 * debugging. -octo */
729 return (0);
730 }
731 #endif /* HAVE_THREAD_INFO */
733 static int ps_read (void)
734 {
735 #if HAVE_THREAD_INFO
736 kern_return_t status;
738 int pset;
739 processor_set_t port_pset_priv;
741 int task;
742 task_array_t task_list;
743 mach_msg_type_number_t task_list_len;
745 int task_pid;
746 char task_name[MAXCOMLEN + 1];
748 int thread;
749 thread_act_array_t thread_list;
750 mach_msg_type_number_t thread_list_len;
751 thread_basic_info_data_t thread_data;
752 mach_msg_type_number_t thread_data_len;
754 int running = 0;
755 int sleeping = 0;
756 int zombies = 0;
757 int stopped = 0;
758 int blocked = 0;
760 procstat_t *ps;
761 procstat_entry_t pse;
763 ps_list_reset ();
765 /*
766 * The Mach-concept is a little different from the traditional UNIX
767 * concept: All the work is done in threads. Threads are contained in
768 * `tasks'. Therefore, `task status' doesn't make much sense, since
769 * it's actually a `thread status'.
770 * Tasks are assigned to sets of processors, so that's where you go to
771 * get a list.
772 */
773 for (pset = 0; pset < pset_list_len; pset++)
774 {
775 if ((status = host_processor_set_priv (port_host_self,
776 pset_list[pset],
777 &port_pset_priv)) != KERN_SUCCESS)
778 {
779 ERROR ("host_processor_set_priv failed: %s\n",
780 mach_error_string (status));
781 continue;
782 }
784 if ((status = processor_set_tasks (port_pset_priv,
785 &task_list,
786 &task_list_len)) != KERN_SUCCESS)
787 {
788 ERROR ("processor_set_tasks failed: %s\n",
789 mach_error_string (status));
790 mach_port_deallocate (port_task_self, port_pset_priv);
791 continue;
792 }
794 for (task = 0; task < task_list_len; task++)
795 {
796 ps = NULL;
797 if (mach_get_task_name (task_list[task],
798 &task_pid,
799 task_name, PROCSTAT_NAME_LEN) == 0)
800 ps = ps_list_search (task_name);
802 /* Collect more detailed statistics for this process */
803 if (ps != NULL)
804 {
805 task_basic_info_data_t task_basic_info;
806 mach_msg_type_number_t task_basic_info_len;
807 task_events_info_data_t task_events_info;
808 mach_msg_type_number_t task_events_info_len;
809 task_absolutetime_info_data_t task_absolutetime_info;
810 mach_msg_type_number_t task_absolutetime_info_len;
812 memset (&pse, '\0', sizeof (pse));
813 pse.id = task_pid;
815 task_basic_info_len = TASK_BASIC_INFO_COUNT;
816 status = task_info (task_list[task],
817 TASK_BASIC_INFO,
818 (task_info_t) &task_basic_info,
819 &task_basic_info_len);
820 if (status != KERN_SUCCESS)
821 {
822 ERROR ("task_info failed: %s",
823 mach_error_string (status));
824 continue; /* with next thread_list */
825 }
827 task_events_info_len = TASK_EVENTS_INFO_COUNT;
828 status = task_info (task_list[task],
829 TASK_EVENTS_INFO,
830 (task_info_t) &task_events_info,
831 &task_events_info_len);
832 if (status != KERN_SUCCESS)
833 {
834 ERROR ("task_info failed: %s",
835 mach_error_string (status));
836 continue; /* with next thread_list */
837 }
839 task_absolutetime_info_len = TASK_ABSOLUTETIME_INFO_COUNT;
840 status = task_info (task_list[task],
841 TASK_ABSOLUTETIME_INFO,
842 (task_info_t) &task_absolutetime_info,
843 &task_absolutetime_info_len);
844 if (status != KERN_SUCCESS)
845 {
846 ERROR ("task_info failed: %s",
847 mach_error_string (status));
848 continue; /* with next thread_list */
849 }
851 pse.num_proc++;
852 pse.vmem_rss = task_basic_info.resident_size;
854 pse.vmem_minflt_counter = task_events_info.cow_faults;
855 pse.vmem_majflt_counter = task_events_info.faults;
857 pse.cpu_user_counter = task_absolutetime_info.total_user;
858 pse.cpu_system_counter = task_absolutetime_info.total_system;
859 }
861 status = task_threads (task_list[task], &thread_list,
862 &thread_list_len);
863 if (status != KERN_SUCCESS)
864 {
865 /* Apple's `top' treats this case a zombie. It
866 * makes sense to some extend: A `zombie'
867 * thread is nonsense, since the task/process
868 * is dead. */
869 zombies++;
870 DEBUG ("task_threads failed: %s",
871 mach_error_string (status));
872 if (task_list[task] != port_task_self)
873 mach_port_deallocate (port_task_self,
874 task_list[task]);
875 continue; /* with next task_list */
876 }
878 for (thread = 0; thread < thread_list_len; thread++)
879 {
880 thread_data_len = THREAD_BASIC_INFO_COUNT;
881 status = thread_info (thread_list[thread],
882 THREAD_BASIC_INFO,
883 (thread_info_t) &thread_data,
884 &thread_data_len);
885 if (status != KERN_SUCCESS)
886 {
887 ERROR ("thread_info failed: %s",
888 mach_error_string (status));
889 if (task_list[task] != port_task_self)
890 mach_port_deallocate (port_task_self,
891 thread_list[thread]);
892 continue; /* with next thread_list */
893 }
895 if (ps != NULL)
896 pse.num_lwp++;
898 switch (thread_data.run_state)
899 {
900 case TH_STATE_RUNNING:
901 running++;
902 break;
903 case TH_STATE_STOPPED:
904 /* What exactly is `halted'? */
905 case TH_STATE_HALTED:
906 stopped++;
907 break;
908 case TH_STATE_WAITING:
909 sleeping++;
910 break;
911 case TH_STATE_UNINTERRUPTIBLE:
912 blocked++;
913 break;
914 /* There is no `zombie' case here,
915 * since there are no zombie-threads.
916 * There's only zombie tasks, which are
917 * handled above. */
918 default:
919 WARNING ("Unknown thread status: %s",
920 thread_data.run_state);
921 break;
922 } /* switch (thread_data.run_state) */
924 if (task_list[task] != port_task_self)
925 {
926 status = mach_port_deallocate (port_task_self,
927 thread_list[thread]);
928 if (status != KERN_SUCCESS)
929 ERROR ("mach_port_deallocate failed: %s",
930 mach_error_string (status));
931 }
932 } /* for (thread_list) */
934 if ((status = vm_deallocate (port_task_self,
935 (vm_address_t) thread_list,
936 thread_list_len * sizeof (thread_act_t)))
937 != KERN_SUCCESS)
938 {
939 ERROR ("vm_deallocate failed: %s",
940 mach_error_string (status));
941 }
942 thread_list = NULL;
943 thread_list_len = 0;
945 /* Only deallocate the task port, if it isn't our own.
946 * Don't know what would happen in that case, but this
947 * is what Apple's top does.. ;) */
948 if (task_list[task] != port_task_self)
949 {
950 status = mach_port_deallocate (port_task_self,
951 task_list[task]);
952 if (status != KERN_SUCCESS)
953 ERROR ("mach_port_deallocate failed: %s",
954 mach_error_string (status));
955 }
957 if (ps != NULL)
958 ps_list_add (task_name, &pse);
959 } /* for (task_list) */
961 if ((status = vm_deallocate (port_task_self,
962 (vm_address_t) task_list,
963 task_list_len * sizeof (task_t))) != KERN_SUCCESS)
964 {
965 ERROR ("vm_deallocate failed: %s",
966 mach_error_string (status));
967 }
968 task_list = NULL;
969 task_list_len = 0;
971 if ((status = mach_port_deallocate (port_task_self, port_pset_priv))
972 != KERN_SUCCESS)
973 {
974 ERROR ("mach_port_deallocate failed: %s",
975 mach_error_string (status));
976 }
977 } /* for (pset_list) */
979 ps_submit_state ("running", running);
980 ps_submit_state ("sleeping", sleeping);
981 ps_submit_state ("zombies", zombies);
982 ps_submit_state ("stopped", stopped);
983 ps_submit_state ("blocked", blocked);
985 for (ps = list_head_g; ps != NULL; ps = ps->next)
986 ps_submit_proc_list (ps);
987 /* #endif HAVE_THREAD_INFO */
989 #elif KERNEL_LINUX
990 int running = 0;
991 int sleeping = 0;
992 int zombies = 0;
993 int stopped = 0;
994 int paging = 0;
995 int blocked = 0;
997 struct dirent *ent;
998 DIR *proc;
999 int pid;
1001 int status;
1002 procstat_t ps;
1003 procstat_entry_t pse;
1004 char state;
1006 procstat_t *ps_ptr;
1008 running = sleeping = zombies = stopped = paging = blocked = 0;
1009 ps_list_reset ();
1011 if ((proc = opendir ("/proc")) == NULL)
1012 {
1013 char errbuf[1024];
1014 ERROR ("Cannot open `/proc': %s",
1015 sstrerror (errno, errbuf, sizeof (errbuf)));
1016 return (-1);
1017 }
1019 while ((ent = readdir (proc)) != NULL)
1020 {
1021 if (!isdigit (ent->d_name[0]))
1022 continue;
1024 if ((pid = atoi (ent->d_name)) < 1)
1025 continue;
1027 status = ps_read_process (pid, &ps, &state);
1028 if (status != 0)
1029 {
1030 DEBUG ("ps_read_process failed: %i", status);
1031 continue;
1032 }
1034 pse.id = pid;
1035 pse.age = 0;
1037 pse.num_proc = ps.num_proc;
1038 pse.num_lwp = ps.num_lwp;
1039 pse.vmem_rss = ps.vmem_rss;
1041 pse.vmem_minflt = 0;
1042 pse.vmem_minflt_counter = ps.vmem_minflt_counter;
1043 pse.vmem_majflt = 0;
1044 pse.vmem_majflt_counter = ps.vmem_majflt_counter;
1046 pse.cpu_user = 0;
1047 pse.cpu_user_counter = ps.cpu_user_counter;
1048 pse.cpu_system = 0;
1049 pse.cpu_system_counter = ps.cpu_system_counter;
1051 switch (state)
1052 {
1053 case 'R': running++; break;
1054 case 'S': sleeping++; break;
1055 case 'D': blocked++; break;
1056 case 'Z': zombies++; break;
1057 case 'T': stopped++; break;
1058 case 'W': paging++; break;
1059 }
1061 ps_list_add (ps.name, &pse);
1062 }
1064 closedir (proc);
1066 ps_submit_state ("running", running);
1067 ps_submit_state ("sleeping", sleeping);
1068 ps_submit_state ("zombies", zombies);
1069 ps_submit_state ("stopped", stopped);
1070 ps_submit_state ("paging", paging);
1071 ps_submit_state ("blocked", blocked);
1073 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1074 ps_submit_proc_list (ps_ptr);
1075 #endif /* KERNEL_LINUX */
1077 return (0);
1078 } /* int ps_read */
1079 #endif /* PROCESSES_HAVE_READ */
1081 void module_register (void)
1082 {
1083 plugin_register_data_set (&state_ds);
1084 plugin_register_data_set (&rss_ds);
1085 plugin_register_data_set (&time_ds);
1086 plugin_register_data_set (&count_ds );
1087 plugin_register_data_set (&pagefaults_ds );
1089 #if PROCESSES_HAVE_READ
1090 #if HAVE_THREAD_INFO | KERNEL_LINUX
1091 plugin_register_config ("processes", ps_config,
1092 config_keys, config_keys_num);
1093 #endif
1094 plugin_register_init ("processes", ps_init);
1095 plugin_register_read ("processes", ps_read);
1096 #endif /* PROCESSES_HAVE_READ */
1097 }