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 #if HAVE_THREAD_INFO | KERNEL_LINUX
155 static const char *config_keys[] =
156 {
157 "Process",
158 NULL
159 };
160 static int config_keys_num = 1;
161 #endif
163 typedef struct procstat_entry_s
164 {
165 unsigned long id;
166 unsigned long age;
168 unsigned long num_proc;
169 unsigned long num_lwp;
170 unsigned long vmem_rss;
172 unsigned long vmem_minflt;
173 unsigned long vmem_majflt;
174 unsigned long vmem_minflt_counter;
175 unsigned long vmem_majflt_counter;
177 unsigned long cpu_user;
178 unsigned long cpu_system;
179 unsigned long cpu_user_counter;
180 unsigned long cpu_system_counter;
182 struct procstat_entry_s *next;
183 } procstat_entry_t;
185 #define PROCSTAT_NAME_LEN 256
186 typedef struct procstat
187 {
188 char name[PROCSTAT_NAME_LEN];
190 unsigned long num_proc;
191 unsigned long num_lwp;
192 unsigned long vmem_rss;
194 unsigned long vmem_minflt_counter;
195 unsigned long vmem_majflt_counter;
197 unsigned long cpu_user_counter;
198 unsigned long cpu_system_counter;
200 struct procstat *next;
201 struct procstat_entry_s *instances;
202 } procstat_t;
204 #if HAVE_THREAD_INFO | KERNEL_LINUX
205 static procstat_t *list_head_g = NULL;
206 #endif
208 #if HAVE_THREAD_INFO
209 static mach_port_t port_host_self;
210 static mach_port_t port_task_self;
212 static processor_set_name_array_t pset_list;
213 static mach_msg_type_number_t pset_list_len;
214 /* #endif HAVE_THREAD_INFO */
216 #elif KERNEL_LINUX
217 static long pagesize_g;
218 #endif /* KERNEL_LINUX */
220 #if HAVE_THREAD_INFO | KERNEL_LINUX
221 static void ps_list_register (const char *name)
222 {
223 procstat_t *new;
224 procstat_t *ptr;
226 if ((new = (procstat_t *) malloc (sizeof (procstat_t))) == NULL)
227 return;
228 memset (new, 0, sizeof (procstat_t));
229 strncpy (new->name, name, PROCSTAT_NAME_LEN);
231 for (ptr = list_head_g; ptr != NULL; ptr = ptr->next)
232 {
233 if (strcmp (ptr->name, name) == 0)
234 return;
235 if (ptr->next == NULL)
236 break;
237 }
239 if (ptr == NULL)
240 list_head_g = new;
241 else
242 ptr->next = new;
243 }
245 static procstat_t *ps_list_search (const char *name)
246 {
247 procstat_t *ptr;
249 for (ptr = list_head_g; ptr != NULL; ptr = ptr->next)
250 if (strcmp (ptr->name, name) == 0)
251 break;
253 return (ptr);
254 }
256 static void ps_list_add (const char *name, procstat_entry_t *entry)
257 {
258 procstat_t *ps;
259 procstat_entry_t *pse;
261 if (entry->id == 0)
262 return;
264 if ((ps = ps_list_search (name)) == NULL)
265 return;
267 for (pse = ps->instances; pse != NULL; pse = pse->next)
268 if ((pse->id == entry->id) || (pse->next == NULL))
269 break;
271 if ((pse == NULL) || (pse->id != entry->id))
272 {
273 procstat_entry_t *new;
275 new = (procstat_entry_t *) malloc (sizeof (procstat_entry_t));
276 if (new == NULL)
277 return;
278 memset (new, 0, sizeof (procstat_entry_t));
279 new->id = entry->id;
281 if (pse == NULL)
282 ps->instances = new;
283 else
284 pse->next = new;
286 pse = new;
287 }
289 pse->age = 0;
290 pse->num_proc = entry->num_proc;
291 pse->num_lwp = entry->num_lwp;
292 pse->vmem_rss = entry->vmem_rss;
294 ps->num_proc += pse->num_proc;
295 ps->num_lwp += pse->num_lwp;
296 ps->vmem_rss += pse->vmem_rss;
298 if ((entry->vmem_minflt_counter == 0)
299 && (entry->vmem_majflt_counter == 0))
300 {
301 pse->vmem_minflt_counter += entry->vmem_minflt;
302 pse->vmem_minflt = entry->vmem_minflt;
304 pse->vmem_majflt_counter += entry->vmem_majflt;
305 pse->vmem_majflt = entry->vmem_majflt;
306 }
307 else
308 {
309 if (entry->vmem_minflt_counter < pse->vmem_minflt_counter)
310 {
311 pse->vmem_minflt = entry->vmem_minflt_counter
312 + (ULONG_MAX - pse->vmem_minflt_counter);
313 }
314 else
315 {
316 pse->vmem_minflt = entry->vmem_minflt_counter - pse->vmem_minflt_counter;
317 }
318 pse->vmem_minflt_counter = entry->vmem_minflt_counter;
320 if (entry->vmem_majflt_counter < pse->vmem_majflt_counter)
321 {
322 pse->vmem_majflt = entry->vmem_majflt_counter
323 + (ULONG_MAX - pse->vmem_majflt_counter);
324 }
325 else
326 {
327 pse->vmem_majflt = entry->vmem_majflt_counter - pse->vmem_majflt_counter;
328 }
329 pse->vmem_majflt_counter = entry->vmem_majflt_counter;
330 }
332 ps->vmem_minflt_counter += pse->vmem_minflt;
333 ps->vmem_majflt_counter += pse->vmem_majflt;
335 if ((entry->cpu_user_counter == 0)
336 && (entry->cpu_system_counter == 0))
337 {
338 pse->cpu_user_counter += entry->cpu_user;
339 pse->cpu_user = entry->cpu_user;
341 pse->cpu_system_counter += entry->cpu_system;
342 pse->cpu_system = entry->cpu_system;
343 }
344 else
345 {
346 if (entry->cpu_user_counter < pse->cpu_user_counter)
347 {
348 pse->cpu_user = entry->cpu_user_counter
349 + (ULONG_MAX - pse->cpu_user_counter);
350 }
351 else
352 {
353 pse->cpu_user = entry->cpu_user_counter - pse->cpu_user_counter;
354 }
355 pse->cpu_user_counter = entry->cpu_user_counter;
357 if (entry->cpu_system_counter < pse->cpu_system_counter)
358 {
359 pse->cpu_system = entry->cpu_system_counter
360 + (ULONG_MAX - pse->cpu_system_counter);
361 }
362 else
363 {
364 pse->cpu_system = entry->cpu_system_counter - pse->cpu_system_counter;
365 }
366 pse->cpu_system_counter = entry->cpu_system_counter;
367 }
369 ps->cpu_user_counter += pse->cpu_user;
370 ps->cpu_system_counter += pse->cpu_system;
371 }
373 static void ps_list_reset (void)
374 {
375 procstat_t *ps;
376 procstat_entry_t *pse;
377 procstat_entry_t *pse_prev;
379 for (ps = list_head_g; ps != NULL; ps = ps->next)
380 {
381 ps->num_proc = 0;
382 ps->num_lwp = 0;
383 ps->vmem_rss = 0;
385 pse_prev = NULL;
386 pse = ps->instances;
387 while (pse != NULL)
388 {
389 if (pse->age > 10)
390 {
391 DBG ("Removing this procstat entry cause it's too old: "
392 "id = %lu; name = %s;",
393 pse->id, ps->name);
395 if (pse_prev == NULL)
396 {
397 ps->instances = pse->next;
398 free (pse);
399 pse = ps->instances;
400 }
401 else
402 {
403 pse_prev->next = pse->next;
404 free (pse);
405 pse = pse_prev->next;
406 }
407 }
408 else
409 {
410 pse->age++;
411 pse_prev = pse;
412 pse = pse->next;
413 }
414 } /* while (pse != NULL) */
415 } /* for (ps = list_head_g; ps != NULL; ps = ps->next) */
416 }
418 static int ps_config (const char *key, const char *value)
419 {
420 if (strcasecmp (key, "Process") == 0)
421 {
422 ps_list_register (value);
423 }
424 else
425 {
426 return (-1);
427 }
429 return (0);
430 }
431 #endif /* HAVE_THREAD_INFO | KERNEL_LINUX */
433 static int ps_init (void)
434 {
435 #if HAVE_THREAD_INFO
436 kern_return_t status;
438 port_host_self = mach_host_self ();
439 port_task_self = mach_task_self ();
441 if (pset_list != NULL)
442 {
443 vm_deallocate (port_task_self,
444 (vm_address_t) pset_list,
445 pset_list_len * sizeof (processor_set_t));
446 pset_list = NULL;
447 pset_list_len = 0;
448 }
450 if ((status = host_processor_sets (port_host_self,
451 &pset_list,
452 &pset_list_len)) != KERN_SUCCESS)
453 {
454 syslog (LOG_ERR, "host_processor_sets failed: %s\n",
455 mach_error_string (status));
456 pset_list = NULL;
457 pset_list_len = 0;
458 return (-1);
459 }
460 /* #endif HAVE_THREAD_INFO */
462 #elif KERNEL_LINUX
463 pagesize_g = sysconf(_SC_PAGESIZE);
464 DBG ("pagesize_g = %li; CONFIG_HZ = %i;",
465 pagesize_g, CONFIG_HZ);
466 #endif /* KERNEL_LINUX */
468 return (0);
469 } /* int ps_init */
471 static void ps_submit_state (const char *state, double value)
472 {
473 value_t values[1];
474 value_list_t vl = VALUE_LIST_INIT;
476 values[0].gauge = value;
478 vl.values = values;
479 vl.values_len = 1;
480 vl.time = time (NULL);
481 strcpy (vl.host, hostname_g);
482 strcpy (vl.plugin, "processes");
483 strcpy (vl.plugin_instance, "");
484 strncpy (vl.type_instance, state, sizeof (vl.type_instance));
486 plugin_dispatch_values ("ps_state", &vl);
487 }
489 static void ps_submit_proc_list (procstat_t *ps)
490 {
491 value_t values[2];
492 value_list_t vl = VALUE_LIST_INIT;
494 vl.values = values;
495 vl.values_len = 2;
496 vl.time = time (NULL);
497 strcpy (vl.host, hostname_g);
498 strcpy (vl.plugin, "processes");
499 strncpy (vl.plugin_instance, ps->name, sizeof (vl.plugin_instance));
501 vl.values[0].gauge = ps->vmem_rss;
502 vl.values_len = 1;
503 plugin_dispatch_values ("ps_rss", &vl);
505 vl.values[0].counter = ps->cpu_user_counter;
506 vl.values[1].counter = ps->cpu_system_counter;
507 vl.values_len = 2;
508 plugin_dispatch_values ("ps_cputime", &vl);
510 vl.values[0].gauge = ps->num_proc;
511 vl.values[1].gauge = ps->num_lwp;
512 vl.values_len = 2;
513 plugin_dispatch_values ("ps_count", &vl);
515 vl.values[0].counter = ps->vmem_minflt_counter;
516 vl.values[1].counter = ps->vmem_majflt_counter;
517 vl.values_len = 2;
518 plugin_dispatch_values ("ps_pagefaults", &vl);
520 DBG ("name = %s; num_proc = %lu; num_lwp = %lu; vmem_rss = %lu; "
521 "vmem_minflt_counter = %lu; vmem_majflt_counter = %lu; "
522 "cpu_user_counter = %lu; cpu_system_counter = %lu;",
523 ps->name, ps->num_proc, ps->num_lwp, ps->vmem_rss,
524 ps->vmem_minflt_counter, ps->vmem_majflt_counter,
525 ps->cpu_user_counter, ps->cpu_system_counter);
526 } /* void ps_submit_proc_list */
528 #if KERNEL_LINUX
529 static int *ps_read_tasks (int pid)
530 {
531 int *list = NULL;
532 int list_size = 1; /* size of allocated space, in elements */
533 int list_len = 0; /* number of currently used elements */
535 char dirname[64];
536 DIR *dh;
537 struct dirent *ent;
539 snprintf (dirname, 64, "/proc/%i/task", pid);
540 dirname[63] = '\0';
542 if ((dh = opendir (dirname)) == NULL)
543 {
544 DBG ("Failed to open directory `%s'", dirname);
545 return (NULL);
546 }
548 while ((ent = readdir (dh)) != NULL)
549 {
550 if (!isdigit (ent->d_name[0]))
551 continue;
553 if ((list_len + 1) >= list_size)
554 {
555 int *new_ptr;
556 int new_size = 2 * list_size;
557 /* Comes in sizes: 2, 4, 8, 16, ... */
559 new_ptr = (int *) realloc (list, (size_t) (sizeof (int) * new_size));
560 if (new_ptr == NULL)
561 {
562 if (list != NULL)
563 free (list);
564 syslog (LOG_ERR, "processes plugin: "
565 "Failed to allocate more memory.");
566 return (NULL);
567 }
569 list = new_ptr;
570 list_size = new_size;
572 memset (list + list_len, 0, sizeof (int) * (list_size - list_len));
573 }
575 list[list_len] = atoi (ent->d_name);
576 if (list[list_len] != 0)
577 list_len++;
578 }
580 closedir (dh);
582 assert (list_len < list_size);
583 assert (list[list_len] == 0);
585 return (list);
586 }
588 int ps_read_process (int pid, procstat_t *ps, char *state)
589 {
590 char filename[64];
591 char buffer[1024];
592 FILE *fh;
594 char *fields[64];
595 char fields_len;
597 int *tasks;
598 int i;
600 int ppid;
601 int name_len;
603 long long unsigned cpu_user_counter;
604 long long unsigned cpu_system_counter;
605 long long unsigned vmem_rss;
607 memset (ps, 0, sizeof (procstat_t));
609 snprintf (filename, 64, "/proc/%i/stat", pid);
610 filename[63] = '\0';
612 if ((fh = fopen (filename, "r")) == NULL)
613 return (-1);
615 if (fgets (buffer, 1024, fh) == NULL)
616 {
617 fclose (fh);
618 return (-1);
619 }
621 fclose (fh);
623 fields_len = strsplit (buffer, fields, 64);
624 if (fields_len < 24)
625 {
626 DBG ("`%s' has only %i fields..",
627 filename, fields_len);
628 return (-1);
629 }
630 else if (fields_len != 41)
631 {
632 DBG ("WARNING: (fields_len = %i) != 41", fields_len);
633 }
635 /* copy the name, strip brackets in the process */
636 name_len = strlen (fields[1]) - 2;
637 if ((fields[1][0] != '(') || (fields[1][name_len + 1] != ')'))
638 {
639 DBG ("No brackets found in process name: `%s'", fields[1]);
640 return (-1);
641 }
642 fields[1] = fields[1] + 1;
643 fields[1][name_len] = '\0';
644 strncpy (ps->name, fields[1], PROCSTAT_NAME_LEN);
646 ppid = atoi (fields[3]);
648 if ((tasks = ps_read_tasks (pid)) == NULL)
649 {
650 /* This happends for zombied, e.g. */
651 DBG ("ps_read_tasks (%i) failed.", pid);
652 *state = 'Z';
653 ps->num_lwp = 0;
654 ps->num_proc = 0;
655 }
656 else
657 {
658 *state = '\0';
659 ps->num_lwp = 0;
660 ps->num_proc = 1;
661 for (i = 0; tasks[i] != 0; i++)
662 ps->num_lwp++;
664 free (tasks);
665 tasks = NULL;
666 }
668 /* Leave the rest at zero if this is only an LWP */
669 if (ps->num_proc == 0)
670 {
671 DBG ("This is only an LWP: pid = %i; name = %s;",
672 pid, ps->name);
673 return (0);
674 }
676 cpu_user_counter = atoll (fields[13]);
677 cpu_system_counter = atoll (fields[14]);
678 vmem_rss = atoll (fields[23]);
679 ps->vmem_minflt_counter = atol (fields[9]);
680 ps->vmem_majflt_counter = atol (fields[11]);
682 /* Convert jiffies to useconds */
683 cpu_user_counter = cpu_user_counter * 1000000 / CONFIG_HZ;
684 cpu_system_counter = cpu_system_counter * 1000000 / CONFIG_HZ;
685 vmem_rss = vmem_rss * pagesize_g;
687 ps->cpu_user_counter = (unsigned long) cpu_user_counter;
688 ps->cpu_system_counter = (unsigned long) cpu_system_counter;
689 ps->vmem_rss = (unsigned long) vmem_rss;
691 *state = fields[2][0];
693 /* success */
694 return (0);
695 } /* int ps_read_process (...) */
696 #endif /* KERNEL_LINUX */
698 #if HAVE_THREAD_INFO
699 static int mach_get_task_name (task_t t, int *pid, char *name, size_t name_max_len)
700 {
701 int mib[4];
703 struct kinfo_proc kp;
704 size_t kp_size;
706 mib[0] = CTL_KERN;
707 mib[1] = KERN_PROC;
708 mib[2] = KERN_PROC_PID;
710 if (pid_for_task (t, pid) != KERN_SUCCESS)
711 return (-1);
712 mib[3] = *pid;
714 kp_size = sizeof (kp);
715 if (sysctl (mib, 4, &kp, &kp_size, NULL, 0) != 0)
716 return (-1);
718 if (name_max_len > (MAXCOMLEN + 1))
719 name_max_len = MAXCOMLEN + 1;
721 strncpy (name, kp.kp_proc.p_comm, name_max_len - 1);
722 name[name_max_len - 1] = '\0';
724 DBG ("pid = %i; name = %s;", *pid, name);
726 /* We don't do the special handling for `p_comm == "LaunchCFMApp"' as
727 * `top' does it, because it is a lot of work and only used when
728 * debugging. -octo */
730 return (0);
731 }
732 #endif /* HAVE_THREAD_INFO */
734 static int ps_read (void)
735 {
736 #if HAVE_THREAD_INFO
737 kern_return_t status;
739 int pset;
740 processor_set_t port_pset_priv;
742 int task;
743 task_array_t task_list;
744 mach_msg_type_number_t task_list_len;
746 int task_pid;
747 char task_name[MAXCOMLEN + 1];
749 int thread;
750 thread_act_array_t thread_list;
751 mach_msg_type_number_t thread_list_len;
752 thread_basic_info_data_t thread_data;
753 mach_msg_type_number_t thread_data_len;
755 int running = 0;
756 int sleeping = 0;
757 int zombies = 0;
758 int stopped = 0;
759 int blocked = 0;
761 procstat_t *ps;
762 procstat_entry_t pse;
764 ps_list_reset ();
766 /*
767 * The Mach-concept is a little different from the traditional UNIX
768 * concept: All the work is done in threads. Threads are contained in
769 * `tasks'. Therefore, `task status' doesn't make much sense, since
770 * it's actually a `thread status'.
771 * Tasks are assigned to sets of processors, so that's where you go to
772 * get a list.
773 */
774 for (pset = 0; pset < pset_list_len; pset++)
775 {
776 if ((status = host_processor_set_priv (port_host_self,
777 pset_list[pset],
778 &port_pset_priv)) != KERN_SUCCESS)
779 {
780 syslog (LOG_ERR, "host_processor_set_priv failed: %s\n",
781 mach_error_string (status));
782 continue;
783 }
785 if ((status = processor_set_tasks (port_pset_priv,
786 &task_list,
787 &task_list_len)) != KERN_SUCCESS)
788 {
789 syslog (LOG_ERR, "processor_set_tasks failed: %s\n",
790 mach_error_string (status));
791 mach_port_deallocate (port_task_self, port_pset_priv);
792 continue;
793 }
795 for (task = 0; task < task_list_len; task++)
796 {
797 ps = NULL;
798 if (mach_get_task_name (task_list[task],
799 &task_pid,
800 task_name, PROCSTAT_NAME_LEN) == 0)
801 ps = ps_list_search (task_name);
803 /* Collect more detailed statistics for this process */
804 if (ps != NULL)
805 {
806 task_basic_info_data_t task_basic_info;
807 mach_msg_type_number_t task_basic_info_len;
808 task_events_info_data_t task_events_info;
809 mach_msg_type_number_t task_events_info_len;
810 task_absolutetime_info_data_t task_absolutetime_info;
811 mach_msg_type_number_t task_absolutetime_info_len;
813 memset (&pse, '\0', sizeof (pse));
814 pse.id = task_pid;
816 task_basic_info_len = TASK_BASIC_INFO_COUNT;
817 status = task_info (task_list[task],
818 TASK_BASIC_INFO,
819 (task_info_t) &task_basic_info,
820 &task_basic_info_len);
821 if (status != KERN_SUCCESS)
822 {
823 syslog (LOG_ERR, "task_info failed: %s",
824 mach_error_string (status));
825 continue; /* with next thread_list */
826 }
828 task_events_info_len = TASK_EVENTS_INFO_COUNT;
829 status = task_info (task_list[task],
830 TASK_EVENTS_INFO,
831 (task_info_t) &task_events_info,
832 &task_events_info_len);
833 if (status != KERN_SUCCESS)
834 {
835 syslog (LOG_ERR, "task_info failed: %s",
836 mach_error_string (status));
837 continue; /* with next thread_list */
838 }
840 task_absolutetime_info_len = TASK_ABSOLUTETIME_INFO_COUNT;
841 status = task_info (task_list[task],
842 TASK_ABSOLUTETIME_INFO,
843 (task_info_t) &task_absolutetime_info,
844 &task_absolutetime_info_len);
845 if (status != KERN_SUCCESS)
846 {
847 syslog (LOG_ERR, "task_info failed: %s",
848 mach_error_string (status));
849 continue; /* with next thread_list */
850 }
852 pse.num_proc++;
853 pse.vmem_rss = task_basic_info.resident_size;
855 pse.vmem_minflt_counter = task_events_info.cow_faults;
856 pse.vmem_majflt_counter = task_events_info.faults;
858 pse.cpu_user_counter = task_absolutetime_info.total_user;
859 pse.cpu_system_counter = task_absolutetime_info.total_system;
860 }
862 status = task_threads (task_list[task], &thread_list,
863 &thread_list_len);
864 if (status != KERN_SUCCESS)
865 {
866 /* Apple's `top' treats this case a zombie. It
867 * makes sense to some extend: A `zombie'
868 * thread is nonsense, since the task/process
869 * is dead. */
870 zombies++;
871 DBG ("task_threads failed: %s",
872 mach_error_string (status));
873 if (task_list[task] != port_task_self)
874 mach_port_deallocate (port_task_self,
875 task_list[task]);
876 continue; /* with next task_list */
877 }
879 for (thread = 0; thread < thread_list_len; thread++)
880 {
881 thread_data_len = THREAD_BASIC_INFO_COUNT;
882 status = thread_info (thread_list[thread],
883 THREAD_BASIC_INFO,
884 (thread_info_t) &thread_data,
885 &thread_data_len);
886 if (status != KERN_SUCCESS)
887 {
888 syslog (LOG_ERR, "thread_info failed: %s",
889 mach_error_string (status));
890 if (task_list[task] != port_task_self)
891 mach_port_deallocate (port_task_self,
892 thread_list[thread]);
893 continue; /* with next thread_list */
894 }
896 if (ps != NULL)
897 pse.num_lwp++;
899 switch (thread_data.run_state)
900 {
901 case TH_STATE_RUNNING:
902 running++;
903 break;
904 case TH_STATE_STOPPED:
905 /* What exactly is `halted'? */
906 case TH_STATE_HALTED:
907 stopped++;
908 break;
909 case TH_STATE_WAITING:
910 sleeping++;
911 break;
912 case TH_STATE_UNINTERRUPTIBLE:
913 blocked++;
914 break;
915 /* There is no `zombie' case here,
916 * since there are no zombie-threads.
917 * There's only zombie tasks, which are
918 * handled above. */
919 default:
920 syslog (LOG_WARNING,
921 "Unknown thread status: %s",
922 thread_data.run_state);
923 break;
924 } /* switch (thread_data.run_state) */
926 if (task_list[task] != port_task_self)
927 {
928 status = mach_port_deallocate (port_task_self,
929 thread_list[thread]);
930 if (status != KERN_SUCCESS)
931 syslog (LOG_ERR, "mach_port_deallocate failed: %s",
932 mach_error_string (status));
933 }
934 } /* for (thread_list) */
936 if ((status = vm_deallocate (port_task_self,
937 (vm_address_t) thread_list,
938 thread_list_len * sizeof (thread_act_t)))
939 != KERN_SUCCESS)
940 {
941 syslog (LOG_ERR, "vm_deallocate failed: %s",
942 mach_error_string (status));
943 }
944 thread_list = NULL;
945 thread_list_len = 0;
947 /* Only deallocate the task port, if it isn't our own.
948 * Don't know what would happen in that case, but this
949 * is what Apple's top does.. ;) */
950 if (task_list[task] != port_task_self)
951 {
952 status = mach_port_deallocate (port_task_self,
953 task_list[task]);
954 if (status != KERN_SUCCESS)
955 syslog (LOG_ERR, "mach_port_deallocate failed: %s",
956 mach_error_string (status));
957 }
959 if (ps != NULL)
960 ps_list_add (task_name, &pse);
961 } /* for (task_list) */
963 if ((status = vm_deallocate (port_task_self,
964 (vm_address_t) task_list,
965 task_list_len * sizeof (task_t))) != KERN_SUCCESS)
966 {
967 syslog (LOG_ERR, "vm_deallocate failed: %s",
968 mach_error_string (status));
969 }
970 task_list = NULL;
971 task_list_len = 0;
973 if ((status = mach_port_deallocate (port_task_self, port_pset_priv))
974 != KERN_SUCCESS)
975 {
976 syslog (LOG_ERR, "mach_port_deallocate failed: %s",
977 mach_error_string (status));
978 }
979 } /* for (pset_list) */
981 ps_submit_state ("running", running);
982 ps_submit_state ("sleeping", sleeping);
983 ps_submit_state ("zombies", zombies);
984 ps_submit_state ("stopped", stopped);
985 ps_submit_state ("blocked", blocked);
987 for (ps = list_head_g; ps != NULL; ps = ps->next)
988 ps_submit_proc_list (ps);
989 /* #endif HAVE_THREAD_INFO */
991 #elif KERNEL_LINUX
992 int running = 0;
993 int sleeping = 0;
994 int zombies = 0;
995 int stopped = 0;
996 int paging = 0;
997 int blocked = 0;
999 struct dirent *ent;
1000 DIR *proc;
1001 int pid;
1003 int status;
1004 procstat_t ps;
1005 procstat_entry_t pse;
1006 char state;
1008 procstat_t *ps_ptr;
1010 running = sleeping = zombies = stopped = paging = blocked = 0;
1011 ps_list_reset ();
1013 if ((proc = opendir ("/proc")) == NULL)
1014 {
1015 syslog (LOG_ERR, "Cannot open `/proc': %s", strerror (errno));
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 DBG ("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 }