1 /**
2 * collectd - src/processes.c
3 * Copyright (C) 2005 Lyonel Vincent
4 * Copyright (C) 2006 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 /* #endif HAVE_THREAD_INFO */
74 #elif KERNEL_LINUX
75 # if HAVE_LINUX_CONFIG_H
76 # include <linux/config.h>
77 # endif
78 # ifndef CONFIG_HZ
79 # define CONFIG_HZ 100
80 # endif
81 #endif /* KERNEL_LINUX */
83 #define MODULE_NAME "processes"
85 #if HAVE_THREAD_INFO || KERNEL_LINUX
86 # define PROCESSES_HAVE_READ 1
87 #else
88 # define PROCESSES_HAVE_READ 0
89 #endif
91 #define BUFSIZE 256
93 static char *processes_file = "processes.rrd";
94 static char *processes_ds_def[] =
95 {
96 "DS:running:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
97 "DS:sleeping:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
98 "DS:zombies:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
99 "DS:stopped:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
100 "DS:paging:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
101 "DS:blocked:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
102 NULL
103 };
104 static int processes_ds_num = 6;
106 static char *ps_rss_file = "processes/ps_rss-%s.rrd";
107 static char *ps_rss_ds_def[] =
108 {
109 /* max = 2^63 - 1 */
110 "DS:byte:GAUGE:"COLLECTD_HEARTBEAT":0:9223372036854775807",
111 NULL
112 };
113 static int ps_rss_ds_num = 1;
115 static char *ps_cputime_file = "processes/ps_cputime-%s.rrd";
116 static char *ps_cputime_ds_def[] =
117 {
118 /* 1 second in user-mode per second ought to be enough.. */
119 "DS:user:COUNTER:"COLLECTD_HEARTBEAT":0:1000000",
120 "DS:syst:COUNTER:"COLLECTD_HEARTBEAT":0:1000000",
121 NULL
122 };
123 static int ps_cputime_ds_num = 2;
125 static char *ps_count_file = "processes/ps_count-%s.rrd";
126 static char *ps_count_ds_def[] =
127 {
128 "DS:processes:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
129 "DS:threads:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
130 NULL
131 };
132 static int ps_count_ds_num = 2;
134 static char *ps_pagefaults_file = "processes/ps_pagefaults-%s.rrd";
135 static char *ps_pagefaults_ds_def[] =
136 {
137 /* max = 2^63 - 1 */
138 "DS:minflt:COUNTER:"COLLECTD_HEARTBEAT":0:9223372036854775807",
139 "DS:majflt:COUNTER:"COLLECTD_HEARTBEAT":0:9223372036854775807",
140 NULL
141 };
142 static int ps_pagefaults_ds_num = 2;
144 static char *config_keys[] =
145 {
146 "Process",
147 NULL
148 };
149 static int config_keys_num = 1;
151 typedef struct procstat_entry_s
152 {
153 unsigned long id;
154 unsigned long age;
156 unsigned long num_proc;
157 unsigned long num_lwp;
158 unsigned long vmem_rss;
160 unsigned long vmem_minflt;
161 unsigned long vmem_majflt;
162 unsigned long vmem_minflt_counter;
163 unsigned long vmem_majflt_counter;
165 unsigned long cpu_user;
166 unsigned long cpu_system;
167 unsigned long cpu_user_counter;
168 unsigned long cpu_system_counter;
170 struct procstat_entry_s *next;
171 } procstat_entry_t;
173 #define PROCSTAT_NAME_LEN 256
174 typedef struct procstat
175 {
176 char name[PROCSTAT_NAME_LEN];
178 unsigned long num_proc;
179 unsigned long num_lwp;
180 unsigned long vmem_rss;
182 unsigned long vmem_minflt_counter;
183 unsigned long vmem_majflt_counter;
185 unsigned long cpu_user_counter;
186 unsigned long cpu_system_counter;
188 struct procstat *next;
189 struct procstat_entry_s *instances;
190 } procstat_t;
192 static procstat_t *list_head_g = NULL;
194 #if HAVE_THREAD_INFO
195 static mach_port_t port_host_self;
196 static mach_port_t port_task_self;
198 static processor_set_name_array_t pset_list;
199 static mach_msg_type_number_t pset_list_len;
200 /* #endif HAVE_THREAD_INFO */
202 #elif KERNEL_LINUX
203 static long pagesize_g;
204 #endif /* KERNEL_LINUX */
206 #if HAVE_THREAD_INFO
207 /* ps_list_* not used yet */
208 /* #endif HAVE_THREAD_INFO */
210 #elif KERNEL_LINUX
211 static void ps_list_register (const char *name)
212 {
213 procstat_t *new;
214 procstat_t *ptr;
216 if ((new = (procstat_t *) malloc (sizeof (procstat_t))) == NULL)
217 return;
218 memset (new, 0, sizeof (procstat_t));
219 strncpy (new->name, name, PROCSTAT_NAME_LEN);
221 for (ptr = list_head_g; ptr != NULL; ptr = ptr->next)
222 {
223 if (strcmp (ptr->name, name) == 0)
224 return;
225 if (ptr->next == NULL)
226 break;
227 }
229 if (ptr == NULL)
230 list_head_g = new;
231 else
232 ptr->next = new;
233 }
235 static procstat_t *ps_list_search (const char *name)
236 {
237 procstat_t *ptr;
239 for (ptr = list_head_g; ptr != NULL; ptr = ptr->next)
240 if (strcmp (ptr->name, name) == 0)
241 break;
243 return (ptr);
244 }
246 static void ps_list_add (const char *name, procstat_entry_t *entry)
247 {
248 procstat_t *ps;
249 procstat_entry_t *pse;
251 if (entry->id == 0)
252 return;
254 if ((ps = ps_list_search (name)) == NULL)
255 return;
257 for (pse = ps->instances; pse != NULL; pse = pse->next)
258 if ((pse->id == entry->id) || (pse->next == NULL))
259 break;
261 if ((pse == NULL) || (pse->id != entry->id))
262 {
263 procstat_entry_t *new;
265 new = (procstat_entry_t *) malloc (sizeof (procstat_entry_t));
266 if (new == NULL)
267 return;
268 memset (new, 0, sizeof (procstat_entry_t));
269 new->id = entry->id;
271 if (pse == NULL)
272 ps->instances = new;
273 else
274 pse->next = new;
276 pse = new;
277 }
279 pse->age = 0;
280 pse->num_proc = entry->num_proc;
281 pse->num_lwp = entry->num_lwp;
282 pse->vmem_rss = entry->vmem_rss;
284 ps->num_proc += pse->num_proc;
285 ps->num_lwp += pse->num_lwp;
286 ps->vmem_rss += pse->vmem_rss;
288 if ((entry->vmem_minflt_counter == 0)
289 && (entry->vmem_majflt_counter == 0))
290 {
291 pse->vmem_minflt_counter += entry->vmem_minflt;
292 pse->vmem_minflt = entry->vmem_minflt;
294 pse->vmem_majflt_counter += entry->vmem_majflt;
295 pse->vmem_majflt = entry->vmem_majflt;
296 }
297 else
298 {
299 if (entry->vmem_minflt_counter < pse->vmem_minflt_counter)
300 {
301 pse->vmem_minflt = entry->vmem_minflt_counter
302 + (ULONG_MAX - pse->vmem_minflt_counter);
303 }
304 else
305 {
306 pse->vmem_minflt = entry->vmem_minflt_counter - pse->vmem_minflt_counter;
307 }
308 pse->vmem_minflt_counter = entry->vmem_minflt_counter;
310 if (entry->vmem_majflt_counter < pse->vmem_majflt_counter)
311 {
312 pse->vmem_majflt = entry->vmem_majflt_counter
313 + (ULONG_MAX - pse->vmem_majflt_counter);
314 }
315 else
316 {
317 pse->vmem_majflt = entry->vmem_majflt_counter - pse->vmem_majflt_counter;
318 }
319 pse->vmem_majflt_counter = entry->vmem_majflt_counter;
320 }
322 ps->vmem_minflt_counter += pse->vmem_minflt;
323 ps->vmem_majflt_counter += pse->vmem_majflt;
325 if ((entry->cpu_user_counter == 0)
326 && (entry->cpu_system_counter == 0))
327 {
328 pse->cpu_user_counter += entry->cpu_user;
329 pse->cpu_user = entry->cpu_user;
331 pse->cpu_system_counter += entry->cpu_system;
332 pse->cpu_system = entry->cpu_system;
333 }
334 else
335 {
336 if (entry->cpu_user_counter < pse->cpu_user_counter)
337 {
338 pse->cpu_user = entry->cpu_user_counter
339 + (ULONG_MAX - pse->cpu_user_counter);
340 }
341 else
342 {
343 pse->cpu_user = entry->cpu_user_counter - pse->cpu_user_counter;
344 }
345 pse->cpu_user_counter = entry->cpu_user_counter;
347 if (entry->cpu_system_counter < pse->cpu_system_counter)
348 {
349 pse->cpu_system = entry->cpu_system_counter
350 + (ULONG_MAX - pse->cpu_system_counter);
351 }
352 else
353 {
354 pse->cpu_system = entry->cpu_system_counter - pse->cpu_system_counter;
355 }
356 pse->cpu_system_counter = entry->cpu_system_counter;
357 }
359 ps->cpu_user_counter += pse->cpu_user;
360 ps->cpu_system_counter += pse->cpu_system;
361 }
363 static void ps_list_reset (void)
364 {
365 procstat_t *ps;
366 procstat_entry_t *pse;
367 procstat_entry_t *pse_prev;
369 for (ps = list_head_g; ps != NULL; ps = ps->next)
370 {
371 ps->num_proc = 0;
372 ps->num_lwp = 0;
373 ps->vmem_rss = 0;
375 pse_prev = NULL;
376 pse = ps->instances;
377 while (pse != NULL)
378 {
379 if (pse->age > 10)
380 {
381 DBG ("Removing this procstat entry cause it's too old: "
382 "id = %lu; name = %s;",
383 pse->id, ps->name);
385 if (pse_prev == NULL)
386 {
387 ps->instances = pse->next;
388 free (pse);
389 pse = ps->instances;
390 }
391 else
392 {
393 pse_prev->next = pse->next;
394 free (pse);
395 pse = pse_prev->next;
396 }
397 }
398 else
399 {
400 pse->age++;
401 pse_prev = pse;
402 pse = pse->next;
403 }
404 } /* while (pse != NULL) */
405 } /* for (ps = list_head_g; ps != NULL; ps = ps->next) */
406 }
407 #endif /* KERNEL_LINUX */
409 static int ps_config (char *key, char *value)
410 {
411 if (strcasecmp (key, "Process") == 0)
412 {
413 ps_list_register (value);
414 }
415 else
416 {
417 return (-1);
418 }
420 return (0);
421 }
423 static void ps_init (void)
424 {
425 #if HAVE_THREAD_INFO
426 kern_return_t status;
428 port_host_self = mach_host_self ();
429 port_task_self = mach_task_self ();
431 if (pset_list != NULL)
432 {
433 vm_deallocate (port_task_self,
434 (vm_address_t) pset_list,
435 pset_list_len * sizeof (processor_set_t));
436 pset_list = NULL;
437 pset_list_len = 0;
438 }
440 if ((status = host_processor_sets (port_host_self,
441 &pset_list,
442 &pset_list_len)) != KERN_SUCCESS)
443 {
444 syslog (LOG_ERR, "host_processor_sets failed: %s\n",
445 mach_error_string (status));
446 pset_list = NULL;
447 pset_list_len = 0;
448 return;
449 }
450 /* #endif HAVE_THREAD_INFO */
452 #elif KERNEL_LINUX
453 pagesize_g = sysconf(_SC_PAGESIZE);
454 DBG ("pagesize_g = %li; CONFIG_HZ = %i;",
455 pagesize_g, CONFIG_HZ);
456 #endif /* KERNEL_LINUX */
458 return;
459 }
461 static void ps_write (char *host, char *inst, char *val)
462 {
463 rrd_update_file (host, processes_file, val,
464 processes_ds_def, processes_ds_num);
465 }
467 static void ps_rss_write (char *host, char *inst, char *val)
468 {
469 char filename[256];
470 int status;
472 status = snprintf (filename, 256, ps_rss_file, inst);
473 if ((status < 1) || (status >= 256))
474 return;
476 rrd_update_file (host, filename, val, ps_rss_ds_def, ps_rss_ds_num);
477 }
479 static void ps_cputime_write (char *host, char *inst, char *val)
480 {
481 char filename[256];
482 int status;
484 status = snprintf (filename, 256, ps_cputime_file, inst);
485 if ((status < 1) || (status >= 256))
486 return;
488 DBG ("host = %s; filename = %s; val = %s;",
489 host, filename, val);
490 rrd_update_file (host, filename, val,
491 ps_cputime_ds_def, ps_cputime_ds_num);
492 }
494 static void ps_count_write (char *host, char *inst, char *val)
495 {
496 char filename[256];
497 int status;
499 status = snprintf (filename, 256, ps_count_file, inst);
500 if ((status < 1) || (status >= 256))
501 return;
503 DBG ("host = %s; filename = %s; val = %s;",
504 host, filename, val);
505 rrd_update_file (host, filename, val,
506 ps_count_ds_def, ps_count_ds_num);
507 }
509 static void ps_pagefaults_write (char *host, char *inst, char *val)
510 {
511 char filename[256];
512 int status;
514 status = snprintf (filename, 256, ps_pagefaults_file, inst);
515 if ((status < 1) || (status >= 256))
516 return;
518 DBG ("host = %s; filename = %s; val = %s;",
519 host, filename, val);
520 rrd_update_file (host, filename, val,
521 ps_pagefaults_ds_def, ps_pagefaults_ds_num);
522 }
524 #if PROCESSES_HAVE_READ
525 static void ps_submit (int running,
526 int sleeping,
527 int zombies,
528 int stopped,
529 int paging,
530 int blocked)
531 {
532 char buf[BUFSIZE];
534 if (snprintf (buf, BUFSIZE, "%u:%i:%i:%i:%i:%i:%i",
535 (unsigned int) curtime,
536 running, sleeping, zombies, stopped, paging,
537 blocked) >= BUFSIZE)
538 return;
540 DBG ("running = %i; sleeping = %i; zombies = %i; stopped = %i; paging = %i; blocked = %i;",
541 running, sleeping, zombies, stopped, paging, blocked);
543 plugin_submit (MODULE_NAME, "-", buf);
544 }
546 static void ps_submit_proc_list (procstat_t *ps)
547 {
548 char buffer[64];
550 if (ps == NULL)
551 return;
553 snprintf (buffer, 64, "%u:%lu",
554 (unsigned int) curtime,
555 ps->vmem_rss);
556 buffer[63] = '\0';
557 plugin_submit ("ps_rss", ps->name, buffer);
559 snprintf (buffer, 64, "%u:%u:%u",
560 (unsigned int) curtime,
561 /* Make the counter overflow */
562 (unsigned int) (ps->cpu_user_counter & 0xFFFFFFFF),
563 (unsigned int) (ps->cpu_system_counter & 0xFFFFFFFF));
564 buffer[63] = '\0';
565 plugin_submit ("ps_cputime", ps->name, buffer);
567 snprintf (buffer, 64, "%u:%lu:%lu",
568 (unsigned int) curtime,
569 ps->num_proc, ps->num_lwp);
570 buffer[63] = '\0';
571 plugin_submit ("ps_count", ps->name, buffer);
573 snprintf (buffer, 64, "%u:%lu:%lu",
574 (unsigned int) curtime,
575 ps->vmem_minflt_counter, ps->vmem_majflt_counter);
576 buffer[63] = '\0';
577 plugin_submit ("ps_pagefaults", ps->name, buffer);
579 DBG ("name = %s; num_proc = %lu; num_lwp = %lu; vmem_rss = %lu; "
580 "vmem_minflt_counter = %i; vmem_majflt_counter = %i; "
581 "cpu_user_counter = %i; cpu_system_counter = %i;",
582 ps->name, ps->num_proc, ps->num_lwp, ps->vmem_rss,
583 ps->vmem_minflt_counter, ps->vmem_majflt_counter, ps->cpu_user_counter,
584 ps->cpu_system_counter);
586 }
588 #if KERNEL_LINUX
589 static int *ps_read_tasks (int pid)
590 {
591 int *list = NULL;
592 int list_size = 1; /* size of allocated space, in elements */
593 int list_len = 0; /* number of currently used elements */
595 char dirname[64];
596 DIR *dh;
597 struct dirent *ent;
599 snprintf (dirname, 64, "/proc/%i/task", pid);
600 dirname[63] = '\0';
602 if ((dh = opendir (dirname)) == NULL)
603 {
604 syslog (LOG_NOTICE, "processes plugin: Failed to open directory `%s'",
605 dirname);
606 return (NULL);
607 }
609 while ((ent = readdir (dh)) != NULL)
610 {
611 if (!isdigit (ent->d_name[0]))
612 continue;
614 if ((list_len + 1) >= list_size)
615 {
616 int *new_ptr;
617 int new_size = 2 * list_size;
618 /* Comes in sizes: 2, 4, 8, 16, ... */
620 new_ptr = (int *) realloc (list, (size_t) (sizeof (int) * new_size));
621 if (new_ptr == NULL)
622 {
623 if (list != NULL)
624 free (list);
625 syslog (LOG_ERR, "processes plugin: "
626 "Failed to allocate more memory.");
627 return (NULL);
628 }
630 list = new_ptr;
631 list_size = new_size;
633 memset (list + list_len, 0, sizeof (int) * (list_size - list_len));
634 }
636 list[list_len] = atoi (ent->d_name);
637 if (list[list_len] != 0)
638 list_len++;
639 }
641 closedir (dh);
643 assert (list_len < list_size);
644 assert (list[list_len] == 0);
646 return (list);
647 }
649 int ps_read_process (int pid, procstat_t *ps, char *state)
650 {
651 char filename[64];
652 char buffer[1024];
653 FILE *fh;
655 char *fields[64];
656 char fields_len;
658 int *tasks;
659 int i;
661 int ppid;
662 int name_len;
664 long long unsigned cpu_user_counter;
665 long long unsigned cpu_system_counter;
666 long long unsigned vmem_rss;
668 memset (ps, 0, sizeof (procstat_t));
670 snprintf (filename, 64, "/proc/%i/stat", pid);
671 filename[63] = '\0';
673 if ((fh = fopen (filename, "r")) == NULL)
674 return (-1);
676 if (fgets (buffer, 1024, fh) == NULL)
677 {
678 fclose (fh);
679 return (-1);
680 }
682 fclose (fh);
684 fields_len = strsplit (buffer, fields, 64);
685 if (fields_len < 24)
686 {
687 DBG ("`%s' has only %i fields..",
688 filename, fields_len);
689 return (-1);
690 }
691 else if (fields_len != 41)
692 {
693 DBG ("WARNING: (fields_len = %i) != 41", fields_len);
694 }
696 /* copy the name, strip brackets in the process */
697 name_len = strlen (fields[1]) - 2;
698 if ((fields[1][0] != '(') || (fields[1][name_len + 1] != ')'))
699 {
700 DBG ("No brackets found in process name: `%s'", fields[1]);
701 return (-1);
702 }
703 fields[1] = fields[1] + 1;
704 fields[1][name_len] = '\0';
705 strncpy (ps->name, fields[1], PROCSTAT_NAME_LEN);
707 ppid = atoi (fields[3]);
709 if ((tasks = ps_read_tasks (pid)) == NULL)
710 {
711 /* This happends for zombied, e.g. */
712 DBG ("ps_read_tasks (%i) failed.", pid);
713 *state = 'Z';
714 ps->num_lwp = 0;
715 ps->num_proc = 0;
716 }
717 else
718 {
719 *state = '\0';
720 ps->num_lwp = 0;
721 ps->num_proc = 1;
722 for (i = 0; tasks[i] != 0; i++)
723 ps->num_lwp++;
725 free (tasks);
726 tasks = NULL;
727 }
729 /* Leave the rest at zero if this is only an LWP */
730 if (ps->num_proc == 0)
731 {
732 DBG ("This is only an LWP: pid = %i; name = %s;",
733 pid, ps->name);
734 return (0);
735 }
737 cpu_user_counter = atoll (fields[13]);
738 cpu_system_counter = atoll (fields[14]);
739 vmem_rss = atoll (fields[23]);
740 ps->vmem_minflt_counter = atol (fields[9]);
741 ps->vmem_majflt_counter = atol (fields[11]);
743 /* Convert jiffies to useconds */
744 cpu_user_counter = cpu_user_counter * 1000000 / CONFIG_HZ;
745 cpu_system_counter = cpu_system_counter * 1000000 / CONFIG_HZ;
746 vmem_rss = vmem_rss * pagesize_g;
748 ps->cpu_user_counter = (unsigned long) cpu_user_counter;
749 ps->cpu_system_counter = (unsigned long) cpu_system_counter;
750 ps->vmem_rss = (unsigned long) vmem_rss;
752 *state = fields[2][0];
754 /* success */
755 return (0);
756 } /* int ps_read_process (...) */
757 #endif /* KERNEL_LINUX */
759 static void ps_read (void)
760 {
761 #if HAVE_THREAD_INFO
762 kern_return_t status;
764 int pset;
765 processor_set_t port_pset_priv;
767 int task;
768 task_array_t task_list;
769 mach_msg_type_number_t task_list_len;
771 int thread;
772 thread_act_array_t thread_list;
773 mach_msg_type_number_t thread_list_len;
774 thread_basic_info_data_t thread_data;
775 mach_msg_type_number_t thread_data_len;
777 int running = 0;
778 int sleeping = 0;
779 int zombies = 0;
780 int stopped = 0;
781 int blocked = 0;
783 /*
784 * The Mach-concept is a little different from the traditional UNIX
785 * concept: All the work is done in threads. Threads are contained in
786 * `tasks'. Therefore, `task status' doesn't make much sense, since
787 * it's actually a `thread status'.
788 * Tasks are assigned to sets of processors, so that's where you go to
789 * get a list.
790 */
791 for (pset = 0; pset < pset_list_len; pset++)
792 {
793 if ((status = host_processor_set_priv (port_host_self,
794 pset_list[pset],
795 &port_pset_priv)) != KERN_SUCCESS)
796 {
797 syslog (LOG_ERR, "host_processor_set_priv failed: %s\n",
798 mach_error_string (status));
799 continue;
800 }
802 if ((status = processor_set_tasks (port_pset_priv,
803 &task_list,
804 &task_list_len)) != KERN_SUCCESS)
805 {
806 syslog (LOG_ERR, "processor_set_tasks failed: %s\n",
807 mach_error_string (status));
808 mach_port_deallocate (port_task_self, port_pset_priv);
809 continue;
810 }
812 for (task = 0; task < task_list_len; task++)
813 {
814 status = task_threads (task_list[task], &thread_list,
815 &thread_list_len);
816 if (status != KERN_SUCCESS)
817 {
818 /* Apple's `top' treats this case a zombie. It
819 * makes sense to some extend: A `zombie'
820 * thread is nonsense, since the task/process
821 * is dead. */
822 zombies++;
823 DBG ("task_threads failed: %s",
824 mach_error_string (status));
825 if (task_list[task] != port_task_self)
826 mach_port_deallocate (port_task_self,
827 task_list[task]);
828 continue; /* with next task_list */
829 }
831 for (thread = 0; thread < thread_list_len; thread++)
832 {
833 thread_data_len = THREAD_BASIC_INFO_COUNT;
834 status = thread_info (thread_list[thread],
835 THREAD_BASIC_INFO,
836 (thread_info_t) &thread_data,
837 &thread_data_len);
838 if (status != KERN_SUCCESS)
839 {
840 syslog (LOG_ERR, "thread_info failed: %s\n",
841 mach_error_string (status));
842 if (task_list[task] != port_task_self)
843 mach_port_deallocate (port_task_self,
844 thread_list[thread]);
845 continue; /* with next thread_list */
846 }
848 switch (thread_data.run_state)
849 {
850 case TH_STATE_RUNNING:
851 running++;
852 break;
853 case TH_STATE_STOPPED:
854 /* What exactly is `halted'? */
855 case TH_STATE_HALTED:
856 stopped++;
857 break;
858 case TH_STATE_WAITING:
859 sleeping++;
860 break;
861 case TH_STATE_UNINTERRUPTIBLE:
862 blocked++;
863 break;
864 /* There is no `zombie' case here,
865 * since there are no zombie-threads.
866 * There's only zombie tasks, which are
867 * handled above. */
868 default:
869 syslog (LOG_WARNING,
870 "Unknown thread status: %s",
871 thread_data.run_state);
872 break;
873 } /* switch (thread_data.run_state) */
875 if (task_list[task] != port_task_self)
876 {
877 status = mach_port_deallocate (port_task_self,
878 thread_list[thread]);
879 if (status != KERN_SUCCESS)
880 syslog (LOG_ERR, "mach_port_deallocate failed: %s",
881 mach_error_string (status));
882 }
883 } /* for (thread_list) */
885 if ((status = vm_deallocate (port_task_self,
886 (vm_address_t) thread_list,
887 thread_list_len * sizeof (thread_act_t)))
888 != KERN_SUCCESS)
889 {
890 syslog (LOG_ERR, "vm_deallocate failed: %s",
891 mach_error_string (status));
892 }
893 thread_list = NULL;
894 thread_list_len = 0;
896 /* Only deallocate the task port, if it isn't our own.
897 * Don't know what would happen in that case, but this
898 * is what Apple's top does.. ;) */
899 if (task_list[task] != port_task_self)
900 {
901 status = mach_port_deallocate (port_task_self,
902 task_list[task]);
903 if (status != KERN_SUCCESS)
904 syslog (LOG_ERR, "mach_port_deallocate failed: %s",
905 mach_error_string (status));
906 }
907 } /* for (task_list) */
909 if ((status = vm_deallocate (port_task_self,
910 (vm_address_t) task_list,
911 task_list_len * sizeof (task_t))) != KERN_SUCCESS)
912 {
913 syslog (LOG_ERR, "vm_deallocate failed: %s",
914 mach_error_string (status));
915 }
916 task_list = NULL;
917 task_list_len = 0;
919 if ((status = mach_port_deallocate (port_task_self, port_pset_priv))
920 != KERN_SUCCESS)
921 {
922 syslog (LOG_ERR, "mach_port_deallocate failed: %s",
923 mach_error_string (status));
924 }
925 } /* for (pset_list) */
927 ps_submit (running, sleeping, zombies, stopped, -1, blocked);
928 /* #endif HAVE_THREAD_INFO */
930 #elif KERNEL_LINUX
931 int running = 0;
932 int sleeping = 0;
933 int zombies = 0;
934 int stopped = 0;
935 int paging = 0;
936 int blocked = 0;
938 struct dirent *ent;
939 DIR *proc;
940 int pid;
942 int status;
943 procstat_t ps;
944 procstat_entry_t pse;
945 char state;
947 procstat_t *ps_ptr;
949 running = sleeping = zombies = stopped = paging = blocked = 0;
950 ps_list_reset ();
952 if ((proc = opendir ("/proc")) == NULL)
953 {
954 syslog (LOG_ERR, "Cannot open `/proc': %s", strerror (errno));
955 return;
956 }
958 while ((ent = readdir (proc)) != NULL)
959 {
960 if (!isdigit (ent->d_name[0]))
961 continue;
963 if ((pid = atoi (ent->d_name)) < 1)
964 continue;
966 status = ps_read_process (pid, &ps, &state);
967 if (status != 0)
968 {
969 DBG ("ps_read_process failed: %i", status);
970 continue;
971 }
973 pse.id = pid;
974 pse.age = 0;
976 pse.num_proc = ps.num_proc;
977 pse.num_lwp = ps.num_lwp;
978 pse.vmem_rss = ps.vmem_rss;
980 pse.vmem_minflt = 0;
981 pse.vmem_minflt_counter = ps.vmem_minflt_counter;
982 pse.vmem_majflt = 0;
983 pse.vmem_majflt_counter = ps.vmem_majflt_counter;
985 pse.cpu_user = 0;
986 pse.cpu_user_counter = ps.cpu_user_counter;
987 pse.cpu_system = 0;
988 pse.cpu_system_counter = ps.cpu_system_counter;
990 switch (state)
991 {
992 case 'R': running++; break;
993 case 'S': sleeping++; break;
994 case 'D': blocked++; break;
995 case 'Z': zombies++; break;
996 case 'T': stopped++; break;
997 case 'W': paging++; break;
998 }
1000 ps_list_add (ps.name, &pse);
1001 }
1003 closedir (proc);
1005 ps_submit (running, sleeping, zombies, stopped, paging, 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 */
1010 }
1011 #else
1012 # define ps_read NULL
1013 #endif /* PROCESSES_HAVE_READ */
1015 void module_register (void)
1016 {
1017 plugin_register (MODULE_NAME, ps_init, ps_read, ps_write);
1018 plugin_register ("ps_rss", NULL, NULL, ps_rss_write);
1019 plugin_register ("ps_cputime", NULL, NULL, ps_cputime_write);
1020 plugin_register ("ps_count", NULL, NULL, ps_count_write);
1021 plugin_register ("ps_pagefaults", NULL, NULL, ps_pagefaults_write);
1022 cf_register (MODULE_NAME, ps_config, config_keys, config_keys_num);
1023 }
1025 #undef BUFSIZE
1026 #undef MODULE_NAME