1 /**
2 * collectd - src/processes.c
3 * Copyright (C) 2005 Lyonel Vincent
4 * Copyright (C) 2006-2008 Florian octo Forster
5 * Copyright (C) 2008 Oleg King
6 * Copyright (C) 2009 Sebastian Harl
7 * Copyright (C) 2009 Andrés J. Díaz
8 * Copyright (C) 2009 Manuel Sanmartin
9 * Copyright (C) 2010 Clément Stenac
10 *
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 *
25 * Authors:
26 * Lyonel Vincent <lyonel at ezix.org>
27 * Florian octo Forster <octo at verplant.org>
28 * Oleg King <king2 at kaluga.ru>
29 * Sebastian Harl <sh at tokkee.org>
30 * Andrés J. Díaz <ajdiaz at connectical.com>
31 * Manuel Sanmartin
32 * Clément Stenac <clement.stenac at diwi.org>
33 **/
35 #include "collectd.h"
36 #include "common.h"
37 #include "plugin.h"
38 #include "configfile.h"
40 /* Include header files for the mach system, if they exist.. */
41 #if HAVE_THREAD_INFO
42 # if HAVE_MACH_MACH_INIT_H
43 # include <mach/mach_init.h>
44 # endif
45 # if HAVE_MACH_HOST_PRIV_H
46 # include <mach/host_priv.h>
47 # endif
48 # if HAVE_MACH_MACH_ERROR_H
49 # include <mach/mach_error.h>
50 # endif
51 # if HAVE_MACH_MACH_HOST_H
52 # include <mach/mach_host.h>
53 # endif
54 # if HAVE_MACH_MACH_PORT_H
55 # include <mach/mach_port.h>
56 # endif
57 # if HAVE_MACH_MACH_TYPES_H
58 # include <mach/mach_types.h>
59 # endif
60 # if HAVE_MACH_MESSAGE_H
61 # include <mach/message.h>
62 # endif
63 # if HAVE_MACH_PROCESSOR_SET_H
64 # include <mach/processor_set.h>
65 # endif
66 # if HAVE_MACH_TASK_H
67 # include <mach/task.h>
68 # endif
69 # if HAVE_MACH_THREAD_ACT_H
70 # include <mach/thread_act.h>
71 # endif
72 # if HAVE_MACH_VM_REGION_H
73 # include <mach/vm_region.h>
74 # endif
75 # if HAVE_MACH_VM_MAP_H
76 # include <mach/vm_map.h>
77 # endif
78 # if HAVE_MACH_VM_PROT_H
79 # include <mach/vm_prot.h>
80 # endif
81 # if HAVE_SYS_SYSCTL_H
82 # include <sys/sysctl.h>
83 # endif
84 /* #endif HAVE_THREAD_INFO */
86 #elif KERNEL_LINUX
87 # if HAVE_LINUX_CONFIG_H
88 # include <linux/config.h>
89 # endif
90 # ifndef CONFIG_HZ
91 # define CONFIG_HZ 100
92 # endif
93 /* #endif KERNEL_LINUX */
95 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD
96 # include <kvm.h>
97 # include <sys/param.h>
98 # include <sys/sysctl.h>
99 # include <sys/user.h>
100 # include <sys/proc.h>
101 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
103 #elif HAVE_PROCINFO_H
104 # include <procinfo.h>
105 # include <sys/types.h>
107 #define MAXPROCENTRY 32
108 #define MAXTHRDENTRY 16
109 #define MAXARGLN 1024
110 /* #endif HAVE_PROCINFO_H */
112 #else
113 # error "No applicable input method."
114 #endif
116 #if HAVE_REGEX_H
117 # include <regex.h>
118 #endif
120 #ifndef ARG_MAX
121 # define ARG_MAX 4096
122 #endif
124 #define BUFSIZE 256
126 static const char *config_keys[] =
127 {
128 "Process",
129 "ProcessMatch"
130 };
131 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
133 typedef struct procstat_entry_s
134 {
135 unsigned long id;
136 unsigned long age;
138 unsigned long num_proc;
139 unsigned long num_lwp;
140 unsigned long vmem_size;
141 unsigned long vmem_rss;
142 unsigned long vmem_data;
143 unsigned long vmem_code;
144 unsigned long stack_size;
146 unsigned long vmem_minflt;
147 unsigned long vmem_majflt;
148 unsigned long vmem_minflt_counter;
149 unsigned long vmem_majflt_counter;
151 unsigned long cpu_user;
152 unsigned long cpu_system;
153 unsigned long cpu_user_counter;
154 unsigned long cpu_system_counter;
156 /* io data */
157 derive_t io_rchar;
158 derive_t io_wchar;
159 derive_t io_syscr;
160 derive_t io_syscw;
162 struct procstat_entry_s *next;
163 } procstat_entry_t;
165 #define PROCSTAT_NAME_LEN 256
166 typedef struct procstat
167 {
168 char name[PROCSTAT_NAME_LEN];
169 #if HAVE_REGEX_H
170 regex_t *re;
171 #endif
173 unsigned long num_proc;
174 unsigned long num_lwp;
175 unsigned long vmem_size;
176 unsigned long vmem_rss;
177 unsigned long vmem_data;
178 unsigned long vmem_code;
179 unsigned long stack_size;
181 unsigned long vmem_minflt_counter;
182 unsigned long vmem_majflt_counter;
184 unsigned long cpu_user_counter;
185 unsigned long cpu_system_counter;
187 /* io data */
188 derive_t io_rchar;
189 derive_t io_wchar;
190 derive_t io_syscr;
191 derive_t io_syscw;
193 struct procstat *next;
194 struct procstat_entry_s *instances;
195 } procstat_t;
197 static procstat_t *list_head_g = NULL;
199 #if HAVE_THREAD_INFO
200 static mach_port_t port_host_self;
201 static mach_port_t port_task_self;
203 static processor_set_name_array_t pset_list;
204 static mach_msg_type_number_t pset_list_len;
205 /* #endif HAVE_THREAD_INFO */
207 #elif KERNEL_LINUX
208 static long pagesize_g;
209 /* #endif KERNEL_LINUX */
211 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD
212 /* no global variables */
213 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
215 #elif HAVE_PROCINFO_H
216 static struct procentry64 procentry[MAXPROCENTRY];
217 static struct thrdentry64 thrdentry[MAXTHRDENTRY];
218 static int pagesize;
220 #ifndef _AIXVERSION_610
221 int getprocs64 (void *procsinfo, int sizproc, void *fdsinfo, int sizfd, pid_t *index, int count);
222 int getthrds64( pid_t, void *, int, tid64_t *, int );
223 #endif
224 int getargs (struct procentry64 *processBuffer, int bufferLen, char *argsBuffer, int argsLen);
225 #endif /* HAVE_PROCINFO_H */
227 /* put name of process from config to list_head_g tree
228 list_head_g is a list of 'procstat_t' structs with
229 processes names we want to watch */
230 static void ps_list_register (const char *name, const char *regexp)
231 {
232 procstat_t *new;
233 procstat_t *ptr;
234 int status;
236 new = (procstat_t *) malloc (sizeof (procstat_t));
237 if (new == NULL)
238 {
239 ERROR ("processes plugin: ps_list_register: malloc failed.");
240 return;
241 }
242 memset (new, 0, sizeof (procstat_t));
243 sstrncpy (new->name, name, sizeof (new->name));
245 #if HAVE_REGEX_H
246 if (regexp != NULL)
247 {
248 DEBUG ("ProcessMatch: adding \"%s\" as criteria to process %s.", regexp, name);
249 new->re = (regex_t *) malloc (sizeof (regex_t));
250 if (new->re == NULL)
251 {
252 ERROR ("processes plugin: ps_list_register: malloc failed.");
253 sfree (new);
254 return;
255 }
257 status = regcomp (new->re, regexp, REG_EXTENDED | REG_NOSUB);
258 if (status != 0)
259 {
260 DEBUG ("ProcessMatch: compiling the regular expression \"%s\" failed.", regexp);
261 sfree(new->re);
262 return;
263 }
264 }
265 #else
266 if (regexp != NULL)
267 {
268 ERROR ("processes plugin: ps_list_register: "
269 "Regular expression \"%s\" found in config "
270 "file, but support for regular expressions "
271 "has been disabled at compile time.",
272 regexp);
273 sfree (new);
274 return;
275 }
276 #endif
278 for (ptr = list_head_g; ptr != NULL; ptr = ptr->next)
279 {
280 if (strcmp (ptr->name, name) == 0)
281 {
282 WARNING ("processes plugin: You have configured more "
283 "than one `Process' or "
284 "`ProcessMatch' with the same name. "
285 "All but the first setting will be "
286 "ignored.");
287 sfree (new->re);
288 sfree (new);
289 return;
290 }
292 if (ptr->next == NULL)
293 break;
294 }
296 if (ptr == NULL)
297 list_head_g = new;
298 else
299 ptr->next = new;
300 } /* void ps_list_register */
302 /* try to match name against entry, returns 1 if success */
303 static int ps_list_match (const char *name, const char *cmdline, procstat_t *ps)
304 {
305 #if HAVE_REGEX_H
306 if (ps->re != NULL)
307 {
308 int status;
309 const char *str;
311 str = cmdline;
312 if ((str == NULL) || (str[0] == 0))
313 str = name;
315 assert (str != NULL);
317 status = regexec (ps->re, str,
318 /* nmatch = */ 0,
319 /* pmatch = */ NULL,
320 /* eflags = */ 0);
321 if (status == 0)
322 return (1);
323 }
324 else
325 #endif
326 if (strcmp (ps->name, name) == 0)
327 return (1);
329 return (0);
330 } /* int ps_list_match */
332 /* add process entry to 'instances' of process 'name' (or refresh it) */
333 static void ps_list_add (const char *name, const char *cmdline, procstat_entry_t *entry)
334 {
335 procstat_t *ps;
336 procstat_entry_t *pse;
338 if (entry->id == 0)
339 return;
341 for (ps = list_head_g; ps != NULL; ps = ps->next)
342 {
343 if ((ps_list_match (name, cmdline, ps)) == 0)
344 continue;
346 for (pse = ps->instances; pse != NULL; pse = pse->next)
347 if ((pse->id == entry->id) || (pse->next == NULL))
348 break;
350 if ((pse == NULL) || (pse->id != entry->id))
351 {
352 procstat_entry_t *new;
354 new = (procstat_entry_t *) malloc (sizeof (procstat_entry_t));
355 if (new == NULL)
356 return;
357 memset (new, 0, sizeof (procstat_entry_t));
358 new->id = entry->id;
360 if (pse == NULL)
361 ps->instances = new;
362 else
363 pse->next = new;
365 pse = new;
366 }
368 pse->age = 0;
369 pse->num_proc = entry->num_proc;
370 pse->num_lwp = entry->num_lwp;
371 pse->vmem_size = entry->vmem_size;
372 pse->vmem_rss = entry->vmem_rss;
373 pse->vmem_data = entry->vmem_data;
374 pse->vmem_code = entry->vmem_code;
375 pse->stack_size = entry->stack_size;
376 pse->io_rchar = entry->io_rchar;
377 pse->io_wchar = entry->io_wchar;
378 pse->io_syscr = entry->io_syscr;
379 pse->io_syscw = entry->io_syscw;
381 ps->num_proc += pse->num_proc;
382 ps->num_lwp += pse->num_lwp;
383 ps->vmem_size += pse->vmem_size;
384 ps->vmem_rss += pse->vmem_rss;
385 ps->vmem_data += pse->vmem_data;
386 ps->vmem_code += pse->vmem_code;
387 ps->stack_size += pse->stack_size;
389 ps->io_rchar += ((pse->io_rchar == -1)?0:pse->io_rchar);
390 ps->io_wchar += ((pse->io_wchar == -1)?0:pse->io_wchar);
391 ps->io_syscr += ((pse->io_syscr == -1)?0:pse->io_syscr);
392 ps->io_syscw += ((pse->io_syscw == -1)?0:pse->io_syscw);
394 if ((entry->vmem_minflt_counter == 0)
395 && (entry->vmem_majflt_counter == 0))
396 {
397 pse->vmem_minflt_counter += entry->vmem_minflt;
398 pse->vmem_minflt = entry->vmem_minflt;
400 pse->vmem_majflt_counter += entry->vmem_majflt;
401 pse->vmem_majflt = entry->vmem_majflt;
402 }
403 else
404 {
405 if (entry->vmem_minflt_counter < pse->vmem_minflt_counter)
406 {
407 pse->vmem_minflt = entry->vmem_minflt_counter
408 + (ULONG_MAX - pse->vmem_minflt_counter);
409 }
410 else
411 {
412 pse->vmem_minflt = entry->vmem_minflt_counter - pse->vmem_minflt_counter;
413 }
414 pse->vmem_minflt_counter = entry->vmem_minflt_counter;
416 if (entry->vmem_majflt_counter < pse->vmem_majflt_counter)
417 {
418 pse->vmem_majflt = entry->vmem_majflt_counter
419 + (ULONG_MAX - pse->vmem_majflt_counter);
420 }
421 else
422 {
423 pse->vmem_majflt = entry->vmem_majflt_counter - pse->vmem_majflt_counter;
424 }
425 pse->vmem_majflt_counter = entry->vmem_majflt_counter;
426 }
428 ps->vmem_minflt_counter += pse->vmem_minflt;
429 ps->vmem_majflt_counter += pse->vmem_majflt;
431 if ((entry->cpu_user_counter == 0)
432 && (entry->cpu_system_counter == 0))
433 {
434 pse->cpu_user_counter += entry->cpu_user;
435 pse->cpu_user = entry->cpu_user;
437 pse->cpu_system_counter += entry->cpu_system;
438 pse->cpu_system = entry->cpu_system;
439 }
440 else
441 {
442 if (entry->cpu_user_counter < pse->cpu_user_counter)
443 {
444 pse->cpu_user = entry->cpu_user_counter
445 + (ULONG_MAX - pse->cpu_user_counter);
446 }
447 else
448 {
449 pse->cpu_user = entry->cpu_user_counter - pse->cpu_user_counter;
450 }
451 pse->cpu_user_counter = entry->cpu_user_counter;
453 if (entry->cpu_system_counter < pse->cpu_system_counter)
454 {
455 pse->cpu_system = entry->cpu_system_counter
456 + (ULONG_MAX - pse->cpu_system_counter);
457 }
458 else
459 {
460 pse->cpu_system = entry->cpu_system_counter - pse->cpu_system_counter;
461 }
462 pse->cpu_system_counter = entry->cpu_system_counter;
463 }
465 ps->cpu_user_counter += pse->cpu_user;
466 ps->cpu_system_counter += pse->cpu_system;
467 }
468 }
470 /* remove old entries from instances of processes in list_head_g */
471 static void ps_list_reset (void)
472 {
473 procstat_t *ps;
474 procstat_entry_t *pse;
475 procstat_entry_t *pse_prev;
477 for (ps = list_head_g; ps != NULL; ps = ps->next)
478 {
479 ps->num_proc = 0;
480 ps->num_lwp = 0;
481 ps->vmem_size = 0;
482 ps->vmem_rss = 0;
483 ps->vmem_data = 0;
484 ps->vmem_code = 0;
485 ps->stack_size = 0;
486 ps->io_rchar = -1;
487 ps->io_wchar = -1;
488 ps->io_syscr = -1;
489 ps->io_syscw = -1;
491 pse_prev = NULL;
492 pse = ps->instances;
493 while (pse != NULL)
494 {
495 if (pse->age > 10)
496 {
497 DEBUG ("Removing this procstat entry cause it's too old: "
498 "id = %lu; name = %s;",
499 pse->id, ps->name);
501 if (pse_prev == NULL)
502 {
503 ps->instances = pse->next;
504 free (pse);
505 pse = ps->instances;
506 }
507 else
508 {
509 pse_prev->next = pse->next;
510 free (pse);
511 pse = pse_prev->next;
512 }
513 }
514 else
515 {
516 pse->age++;
517 pse_prev = pse;
518 pse = pse->next;
519 }
520 } /* while (pse != NULL) */
521 } /* for (ps = list_head_g; ps != NULL; ps = ps->next) */
522 }
524 /* put all pre-defined 'Process' names from config to list_head_g tree */
525 static int ps_config (const char *key, const char *value)
526 {
527 if (strcasecmp (key, "Process") == 0)
528 {
529 ps_list_register (value, NULL);
530 }
531 else if (strcasecmp (key, "ProcessMatch") == 0)
532 {
533 char *new_val;
534 char *fields[3];
535 int fields_num;
537 new_val = strdup (value);
538 if (new_val == NULL) {
539 ERROR ("processes plugin: strdup failed when processing "
540 "`ProcessMatch %s'.", value);
541 return (1);
542 }
544 fields_num = strsplit (new_val, fields,
545 STATIC_ARRAY_SIZE (fields));
546 if (fields_num != 2)
547 {
548 ERROR ("processes plugin: `ProcessMatch' needs exactly "
549 "two string arguments.");
550 sfree (new_val);
551 return (1);
552 }
553 ps_list_register (fields[0], fields[1]);
554 sfree (new_val);
555 }
556 else
557 {
558 ERROR ("processes plugin: The `%s' configuration option is not "
559 "understood and will be ignored.", key);
560 return (-1);
561 }
563 return (0);
564 }
566 static int ps_init (void)
567 {
568 #if HAVE_THREAD_INFO
569 kern_return_t status;
571 port_host_self = mach_host_self ();
572 port_task_self = mach_task_self ();
574 if (pset_list != NULL)
575 {
576 vm_deallocate (port_task_self,
577 (vm_address_t) pset_list,
578 pset_list_len * sizeof (processor_set_t));
579 pset_list = NULL;
580 pset_list_len = 0;
581 }
583 if ((status = host_processor_sets (port_host_self,
584 &pset_list,
585 &pset_list_len)) != KERN_SUCCESS)
586 {
587 ERROR ("host_processor_sets failed: %s\n",
588 mach_error_string (status));
589 pset_list = NULL;
590 pset_list_len = 0;
591 return (-1);
592 }
593 /* #endif HAVE_THREAD_INFO */
595 #elif KERNEL_LINUX
596 pagesize_g = sysconf(_SC_PAGESIZE);
597 DEBUG ("pagesize_g = %li; CONFIG_HZ = %i;",
598 pagesize_g, CONFIG_HZ);
599 /* #endif KERNEL_LINUX */
601 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD
602 /* no initialization */
603 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
605 #elif HAVE_PROCINFO_H
606 pagesize = getpagesize();
607 #endif /* HAVE_PROCINFO_H */
609 return (0);
610 } /* int ps_init */
612 /* submit global state (e.g.: qty of zombies, running, etc..) */
613 static void ps_submit_state (const char *state, double value)
614 {
615 value_t values[1];
616 value_list_t vl = VALUE_LIST_INIT;
618 values[0].gauge = value;
620 vl.values = values;
621 vl.values_len = 1;
622 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
623 sstrncpy (vl.plugin, "processes", sizeof (vl.plugin));
624 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
625 sstrncpy (vl.type, "ps_state", sizeof (vl.type));
626 sstrncpy (vl.type_instance, state, sizeof (vl.type_instance));
628 plugin_dispatch_values (&vl);
629 }
631 /* submit info about specific process (e.g.: memory taken, cpu usage, etc..) */
632 static void ps_submit_proc_list (procstat_t *ps)
633 {
634 value_t values[2];
635 value_list_t vl = VALUE_LIST_INIT;
637 vl.values = values;
638 vl.values_len = 2;
639 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
640 sstrncpy (vl.plugin, "processes", sizeof (vl.plugin));
641 sstrncpy (vl.plugin_instance, ps->name, sizeof (vl.plugin_instance));
643 sstrncpy (vl.type, "ps_vm", sizeof (vl.type));
644 vl.values[0].gauge = ps->vmem_size;
645 vl.values_len = 1;
646 plugin_dispatch_values (&vl);
648 sstrncpy (vl.type, "ps_rss", sizeof (vl.type));
649 vl.values[0].gauge = ps->vmem_rss;
650 vl.values_len = 1;
651 plugin_dispatch_values (&vl);
653 sstrncpy (vl.type, "ps_data", sizeof (vl.type));
654 vl.values[0].gauge = ps->vmem_data;
655 vl.values_len = 1;
656 plugin_dispatch_values (&vl);
658 sstrncpy (vl.type, "ps_code", sizeof (vl.type));
659 vl.values[0].gauge = ps->vmem_code;
660 vl.values_len = 1;
661 plugin_dispatch_values (&vl);
663 sstrncpy (vl.type, "ps_stacksize", sizeof (vl.type));
664 vl.values[0].gauge = ps->stack_size;
665 vl.values_len = 1;
666 plugin_dispatch_values (&vl);
668 sstrncpy (vl.type, "ps_cputime", sizeof (vl.type));
669 vl.values[0].counter = ps->cpu_user_counter;
670 vl.values[1].counter = ps->cpu_system_counter;
671 vl.values_len = 2;
672 plugin_dispatch_values (&vl);
674 sstrncpy (vl.type, "ps_count", sizeof (vl.type));
675 vl.values[0].gauge = ps->num_proc;
676 vl.values[1].gauge = ps->num_lwp;
677 vl.values_len = 2;
678 plugin_dispatch_values (&vl);
680 sstrncpy (vl.type, "ps_pagefaults", sizeof (vl.type));
681 vl.values[0].counter = ps->vmem_minflt_counter;
682 vl.values[1].counter = ps->vmem_majflt_counter;
683 vl.values_len = 2;
684 plugin_dispatch_values (&vl);
686 if ( (ps->io_rchar != -1) && (ps->io_wchar != -1) )
687 {
688 sstrncpy (vl.type, "ps_disk_octets", sizeof (vl.type));
689 vl.values[0].derive = ps->io_rchar;
690 vl.values[1].derive = ps->io_wchar;
691 vl.values_len = 2;
692 plugin_dispatch_values (&vl);
693 }
695 if ( (ps->io_syscr != -1) && (ps->io_syscw != -1) )
696 {
697 sstrncpy (vl.type, "ps_disk_ops", sizeof (vl.type));
698 vl.values[0].derive = ps->io_syscr;
699 vl.values[1].derive = ps->io_syscw;
700 vl.values_len = 2;
701 plugin_dispatch_values (&vl);
702 }
704 DEBUG ("name = %s; num_proc = %lu; num_lwp = %lu; "
705 "vmem_size = %lu; vmem_rss = %lu; vmem_data = %lu; "
706 "vmem_code = %lu; "
707 "vmem_minflt_counter = %lu; vmem_majflt_counter = %lu; "
708 "cpu_user_counter = %lu; cpu_system_counter = %lu; "
709 "io_rchar = %"PRIi64"; io_wchar = %"PRIi64"; "
710 "io_syscr = %"PRIi64"; io_syscw = %"PRIi64";",
711 ps->name, ps->num_proc, ps->num_lwp,
712 ps->vmem_size, ps->vmem_rss,
713 ps->vmem_data, ps->vmem_code,
714 ps->vmem_minflt_counter, ps->vmem_majflt_counter,
715 ps->cpu_user_counter, ps->cpu_system_counter,
716 ps->io_rchar, ps->io_wchar, ps->io_syscr, ps->io_syscw);
717 } /* void ps_submit_proc_list */
719 /* ------- additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
720 #if KERNEL_LINUX
721 static int ps_read_tasks (int pid)
722 {
723 char dirname[64];
724 DIR *dh;
725 struct dirent *ent;
726 int count = 0;
728 ssnprintf (dirname, sizeof (dirname), "/proc/%i/task", pid);
730 if ((dh = opendir (dirname)) == NULL)
731 {
732 DEBUG ("Failed to open directory `%s'", dirname);
733 return (-1);
734 }
736 while ((ent = readdir (dh)) != NULL)
737 {
738 if (!isdigit ((int) ent->d_name[0]))
739 continue;
740 else
741 count++;
742 }
743 closedir (dh);
745 return ((count >= 1) ? count : 1);
746 } /* int *ps_read_tasks */
748 /* Read advanced virtual memory data from /proc/pid/status */
749 static procstat_t *ps_read_vmem (int pid, procstat_t *ps)
750 {
751 FILE *fh;
752 char buffer[1024];
753 char filename[64];
754 unsigned long long lib = 0;
755 unsigned long long exe = 0;
756 unsigned long long data = 0;
757 char *fields[8];
758 int numfields;
760 ssnprintf (filename, sizeof (filename), "/proc/%i/status", pid);
761 if ((fh = fopen (filename, "r")) == NULL)
762 return (NULL);
764 while (fgets (buffer, sizeof(buffer), fh) != NULL)
765 {
766 long long tmp;
767 char *endptr;
769 if (strncmp (buffer, "Vm", 2) != 0)
770 continue;
772 numfields = strsplit (buffer, fields,
773 STATIC_ARRAY_SIZE (fields));
775 if (numfields < 2)
776 continue;
778 errno = 0;
779 endptr = NULL;
780 tmp = strtoll (fields[1], &endptr, /* base = */ 10);
781 if ((errno == 0) && (endptr != fields[1]))
782 {
783 if (strncmp (buffer, "VmData", 6) == 0)
784 {
785 data = tmp;
786 }
787 else if (strncmp (buffer, "VmLib", 5) == 0)
788 {
789 lib = tmp;
790 }
791 else if (strncmp(buffer, "VmExe", 5) == 0)
792 {
793 exe = tmp;
794 }
795 }
796 } /* while (fgets) */
798 if (fclose (fh))
799 {
800 char errbuf[1024];
801 WARNING ("processes: fclose: %s",
802 sstrerror (errno, errbuf, sizeof (errbuf)));
803 }
805 ps->vmem_data = data * 1024;
806 ps->vmem_code = (exe + lib) * 1024;
808 return (ps);
809 } /* procstat_t *ps_read_vmem */
811 static procstat_t *ps_read_io (int pid, procstat_t *ps)
812 {
813 FILE *fh;
814 char buffer[1024];
815 char filename[64];
817 char *fields[8];
818 int numfields;
820 ssnprintf (filename, sizeof (filename), "/proc/%i/io", pid);
821 if ((fh = fopen (filename, "r")) == NULL)
822 return (NULL);
824 while (fgets (buffer, 1024, fh) != NULL)
825 {
826 derive_t *val = NULL;
827 long long tmp;
828 char *endptr;
830 if (strncasecmp (buffer, "rchar:", 6) == 0)
831 val = &(ps->io_rchar);
832 else if (strncasecmp (buffer, "wchar:", 6) == 0)
833 val = &(ps->io_wchar);
834 else if (strncasecmp (buffer, "syscr:", 6) == 0)
835 val = &(ps->io_syscr);
836 else if (strncasecmp (buffer, "syscw:", 6) == 0)
837 val = &(ps->io_syscw);
838 else
839 continue;
841 numfields = strsplit (buffer, fields, 8);
843 if (numfields < 2)
844 continue;
846 errno = 0;
847 endptr = NULL;
848 tmp = strtoll (fields[1], &endptr, /* base = */ 10);
849 if ((errno != 0) || (endptr == fields[1]))
850 *val = -1;
851 else
852 *val = (derive_t) tmp;
853 } /* while (fgets) */
855 if (fclose (fh))
856 {
857 char errbuf[1024];
858 WARNING ("processes: fclose: %s",
859 sstrerror (errno, errbuf, sizeof (errbuf)));
860 }
862 return (ps);
863 } /* procstat_t *ps_read_io */
865 int ps_read_process (int pid, procstat_t *ps, char *state)
866 {
867 char filename[64];
868 char buffer[1024];
870 char *fields[64];
871 char fields_len;
873 int i;
875 int ppid;
876 int name_len;
878 long long unsigned cpu_user_counter;
879 long long unsigned cpu_system_counter;
880 long long unsigned vmem_size;
881 long long unsigned vmem_rss;
882 long long unsigned stack_size;
884 memset (ps, 0, sizeof (procstat_t));
886 ssnprintf (filename, sizeof (filename), "/proc/%i/stat", pid);
888 i = read_file_contents (filename, buffer, sizeof(buffer) - 1);
889 if (i <= 0)
890 return (-1);
891 buffer[i] = 0;
893 fields_len = strsplit (buffer, fields, 64);
894 if (fields_len < 24)
895 {
896 DEBUG ("processes plugin: ps_read_process (pid = %i):"
897 " `%s' has only %i fields..",
898 (int) pid, filename, fields_len);
899 return (-1);
900 }
902 /* copy the name, strip brackets in the process */
903 name_len = strlen (fields[1]) - 2;
904 if ((fields[1][0] != '(') || (fields[1][name_len + 1] != ')'))
905 {
906 DEBUG ("No brackets found in process name: `%s'", fields[1]);
907 return (-1);
908 }
909 fields[1] = fields[1] + 1;
910 fields[1][name_len] = '\0';
911 strncpy (ps->name, fields[1], PROCSTAT_NAME_LEN);
913 ppid = atoi (fields[3]);
915 *state = fields[2][0];
917 if (*state == 'Z')
918 {
919 ps->num_lwp = 0;
920 ps->num_proc = 0;
921 }
922 else
923 {
924 if ( (ps->num_lwp = ps_read_tasks (pid)) == -1 )
925 {
926 /* returns -1 => kernel 2.4 */
927 ps->num_lwp = 1;
928 }
929 ps->num_proc = 1;
930 }
932 /* Leave the rest at zero if this is only a zombi */
933 if (ps->num_proc == 0)
934 {
935 DEBUG ("processes plugin: This is only a zombi: pid = %i; "
936 "name = %s;", pid, ps->name);
937 return (0);
938 }
940 cpu_user_counter = atoll (fields[13]);
941 cpu_system_counter = atoll (fields[14]);
942 vmem_size = atoll (fields[22]);
943 vmem_rss = atoll (fields[23]);
944 ps->vmem_minflt_counter = atol (fields[9]);
945 ps->vmem_majflt_counter = atol (fields[11]);
947 {
948 unsigned long long stack_start = atoll (fields[27]);
949 unsigned long long stack_ptr = atoll (fields[28]);
951 stack_size = (stack_start > stack_ptr)
952 ? stack_start - stack_ptr
953 : stack_ptr - stack_start;
954 }
956 /* Convert jiffies to useconds */
957 cpu_user_counter = cpu_user_counter * 1000000 / CONFIG_HZ;
958 cpu_system_counter = cpu_system_counter * 1000000 / CONFIG_HZ;
959 vmem_rss = vmem_rss * pagesize_g;
961 if ( (ps_read_vmem(pid, ps)) == NULL)
962 {
963 /* No VMem data */
964 ps->vmem_data = -1;
965 ps->vmem_code = -1;
966 DEBUG("ps_read_process: did not get vmem data for pid %i",pid);
967 }
969 ps->cpu_user_counter = (unsigned long) cpu_user_counter;
970 ps->cpu_system_counter = (unsigned long) cpu_system_counter;
971 ps->vmem_size = (unsigned long) vmem_size;
972 ps->vmem_rss = (unsigned long) vmem_rss;
973 ps->stack_size = (unsigned long) stack_size;
975 if ( (ps_read_io (pid, ps)) == NULL)
976 {
977 /* no io data */
978 ps->io_rchar = -1;
979 ps->io_wchar = -1;
980 ps->io_syscr = -1;
981 ps->io_syscw = -1;
983 DEBUG("ps_read_process: not get io data for pid %i",pid);
984 }
986 /* success */
987 return (0);
988 } /* int ps_read_process (...) */
990 static char *ps_get_cmdline (pid_t pid, char *name, char *buf, size_t buf_len)
991 {
992 char *buf_ptr;
993 size_t len;
995 char file[PATH_MAX];
996 int fd;
998 size_t n;
1000 if ((pid < 1) || (NULL == buf) || (buf_len < 2))
1001 return NULL;
1003 ssnprintf (file, sizeof (file), "/proc/%u/cmdline", pid);
1005 fd = open (file, O_RDONLY);
1006 if (fd < 0) {
1007 char errbuf[4096];
1008 WARNING ("processes plugin: Failed to open `%s': %s.", file,
1009 sstrerror (errno, errbuf, sizeof (errbuf)));
1010 return NULL;
1011 }
1013 buf_ptr = buf;
1014 len = buf_len;
1016 n = 0;
1018 while (42) {
1019 ssize_t status;
1021 status = read (fd, (void *)buf_ptr, len);
1023 if (status < 0) {
1024 char errbuf[4096];
1026 if ((EAGAIN == errno) || (EINTR == errno))
1027 continue;
1029 WARNING ("processes plugin: Failed to read from `%s': %s.", file,
1030 sstrerror (errno, errbuf, sizeof (errbuf)));
1031 close (fd);
1032 return NULL;
1033 }
1035 n += status;
1037 if (status == 0)
1038 break;
1040 buf_ptr += status;
1041 len -= status;
1043 if (len <= 0)
1044 break;
1045 }
1047 close (fd);
1049 if (0 == n) {
1050 /* cmdline not available; e.g. kernel thread, zombie */
1051 if (NULL == name)
1052 return NULL;
1054 ssnprintf (buf, buf_len, "[%s]", name);
1055 return buf;
1056 }
1058 assert (n <= buf_len);
1060 if (n == buf_len)
1061 --n;
1062 buf[n] = '\0';
1064 --n;
1065 /* remove trailing whitespace */
1066 while ((n > 0) && (isspace (buf[n]) || ('\0' == buf[n]))) {
1067 buf[n] = '\0';
1068 --n;
1069 }
1071 /* arguments are separated by '\0' in /proc/<pid>/cmdline */
1072 while (n > 0) {
1073 if ('\0' == buf[n])
1074 buf[n] = ' ';
1075 --n;
1076 }
1077 return buf;
1078 } /* char *ps_get_cmdline (...) */
1080 static unsigned long read_fork_rate ()
1081 {
1082 FILE *proc_stat;
1083 char buf[1024];
1084 unsigned long result = 0;
1085 int numfields;
1086 char *fields[3];
1088 proc_stat = fopen("/proc/stat", "r");
1089 if (proc_stat == NULL) {
1090 char errbuf[1024];
1091 ERROR ("processes plugin: fopen (/proc/stat) failed: %s",
1092 sstrerror (errno, errbuf, sizeof (errbuf)));
1093 return ULONG_MAX;
1094 }
1096 while (fgets (buf, sizeof(buf), proc_stat) != NULL)
1097 {
1098 char *endptr;
1100 numfields = strsplit(buf, fields, STATIC_ARRAY_SIZE (fields));
1101 if (numfields != 2)
1102 continue;
1104 if (strcmp ("processes", fields[0]) != 0)
1105 continue;
1107 errno = 0;
1108 endptr = NULL;
1109 result = strtoul(fields[1], &endptr, 10);
1110 if ((endptr == fields[1]) || (errno != 0)) {
1111 ERROR ("processes plugin: Cannot parse fork rate: %s",
1112 fields[1]);
1113 result = ULONG_MAX;
1114 break;
1115 }
1117 break;
1118 }
1120 fclose(proc_stat);
1122 return result;
1123 }
1125 static void ps_submit_fork_rate (unsigned long value)
1126 {
1127 value_t values[1];
1128 value_list_t vl = VALUE_LIST_INIT;
1130 values[0].derive = (derive_t) value;
1132 vl.values = values;
1133 vl.values_len = 1;
1134 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
1135 sstrncpy (vl.plugin, "processes", sizeof (vl.plugin));
1136 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
1137 sstrncpy (vl.type, "fork_rate", sizeof (vl.type));
1138 sstrncpy (vl.type_instance, "", sizeof (vl.type_instance));
1140 plugin_dispatch_values (&vl);
1141 }
1143 #endif /* KERNEL_LINUX */
1145 #if HAVE_THREAD_INFO
1146 static int mach_get_task_name (task_t t, int *pid, char *name, size_t name_max_len)
1147 {
1148 int mib[4];
1150 struct kinfo_proc kp;
1151 size_t kp_size;
1153 mib[0] = CTL_KERN;
1154 mib[1] = KERN_PROC;
1155 mib[2] = KERN_PROC_PID;
1157 if (pid_for_task (t, pid) != KERN_SUCCESS)
1158 return (-1);
1159 mib[3] = *pid;
1161 kp_size = sizeof (kp);
1162 if (sysctl (mib, 4, &kp, &kp_size, NULL, 0) != 0)
1163 return (-1);
1165 if (name_max_len > (MAXCOMLEN + 1))
1166 name_max_len = MAXCOMLEN + 1;
1168 strncpy (name, kp.kp_proc.p_comm, name_max_len - 1);
1169 name[name_max_len - 1] = '\0';
1171 DEBUG ("pid = %i; name = %s;", *pid, name);
1173 /* We don't do the special handling for `p_comm == "LaunchCFMApp"' as
1174 * `top' does it, because it is a lot of work and only used when
1175 * debugging. -octo */
1177 return (0);
1178 }
1179 #endif /* HAVE_THREAD_INFO */
1180 /* ------- end of additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
1182 /* do actual readings from kernel */
1183 static int ps_read (void)
1184 {
1185 #if HAVE_THREAD_INFO
1186 kern_return_t status;
1188 int pset;
1189 processor_set_t port_pset_priv;
1191 int task;
1192 task_array_t task_list;
1193 mach_msg_type_number_t task_list_len;
1195 int task_pid;
1196 char task_name[MAXCOMLEN + 1];
1198 int thread;
1199 thread_act_array_t thread_list;
1200 mach_msg_type_number_t thread_list_len;
1201 thread_basic_info_data_t thread_data;
1202 mach_msg_type_number_t thread_data_len;
1204 int running = 0;
1205 int sleeping = 0;
1206 int zombies = 0;
1207 int stopped = 0;
1208 int blocked = 0;
1210 procstat_t *ps;
1211 procstat_entry_t pse;
1213 ps_list_reset ();
1215 /*
1216 * The Mach-concept is a little different from the traditional UNIX
1217 * concept: All the work is done in threads. Threads are contained in
1218 * `tasks'. Therefore, `task status' doesn't make much sense, since
1219 * it's actually a `thread status'.
1220 * Tasks are assigned to sets of processors, so that's where you go to
1221 * get a list.
1222 */
1223 for (pset = 0; pset < pset_list_len; pset++)
1224 {
1225 if ((status = host_processor_set_priv (port_host_self,
1226 pset_list[pset],
1227 &port_pset_priv)) != KERN_SUCCESS)
1228 {
1229 ERROR ("host_processor_set_priv failed: %s\n",
1230 mach_error_string (status));
1231 continue;
1232 }
1234 if ((status = processor_set_tasks (port_pset_priv,
1235 &task_list,
1236 &task_list_len)) != KERN_SUCCESS)
1237 {
1238 ERROR ("processor_set_tasks failed: %s\n",
1239 mach_error_string (status));
1240 mach_port_deallocate (port_task_self, port_pset_priv);
1241 continue;
1242 }
1244 for (task = 0; task < task_list_len; task++)
1245 {
1246 ps = NULL;
1247 if (mach_get_task_name (task_list[task],
1248 &task_pid,
1249 task_name, PROCSTAT_NAME_LEN) == 0)
1250 {
1251 /* search for at least one match */
1252 for (ps = list_head_g; ps != NULL; ps = ps->next)
1253 /* FIXME: cmdline should be here instead of NULL */
1254 if (ps_list_match (task_name, NULL, ps) == 1)
1255 break;
1256 }
1258 /* Collect more detailed statistics for this process */
1259 if (ps != NULL)
1260 {
1261 task_basic_info_data_t task_basic_info;
1262 mach_msg_type_number_t task_basic_info_len;
1263 task_events_info_data_t task_events_info;
1264 mach_msg_type_number_t task_events_info_len;
1265 task_absolutetime_info_data_t task_absolutetime_info;
1266 mach_msg_type_number_t task_absolutetime_info_len;
1268 memset (&pse, '\0', sizeof (pse));
1269 pse.id = task_pid;
1271 task_basic_info_len = TASK_BASIC_INFO_COUNT;
1272 status = task_info (task_list[task],
1273 TASK_BASIC_INFO,
1274 (task_info_t) &task_basic_info,
1275 &task_basic_info_len);
1276 if (status != KERN_SUCCESS)
1277 {
1278 ERROR ("task_info failed: %s",
1279 mach_error_string (status));
1280 continue; /* with next thread_list */
1281 }
1283 task_events_info_len = TASK_EVENTS_INFO_COUNT;
1284 status = task_info (task_list[task],
1285 TASK_EVENTS_INFO,
1286 (task_info_t) &task_events_info,
1287 &task_events_info_len);
1288 if (status != KERN_SUCCESS)
1289 {
1290 ERROR ("task_info failed: %s",
1291 mach_error_string (status));
1292 continue; /* with next thread_list */
1293 }
1295 task_absolutetime_info_len = TASK_ABSOLUTETIME_INFO_COUNT;
1296 status = task_info (task_list[task],
1297 TASK_ABSOLUTETIME_INFO,
1298 (task_info_t) &task_absolutetime_info,
1299 &task_absolutetime_info_len);
1300 if (status != KERN_SUCCESS)
1301 {
1302 ERROR ("task_info failed: %s",
1303 mach_error_string (status));
1304 continue; /* with next thread_list */
1305 }
1307 pse.num_proc++;
1308 pse.vmem_size = task_basic_info.virtual_size;
1309 pse.vmem_rss = task_basic_info.resident_size;
1310 /* Does not seem to be easily exposed */
1311 pse.vmem_data = 0;
1312 pse.vmem_code = 0;
1314 pse.vmem_minflt_counter = task_events_info.cow_faults;
1315 pse.vmem_majflt_counter = task_events_info.faults;
1317 pse.cpu_user_counter = task_absolutetime_info.total_user;
1318 pse.cpu_system_counter = task_absolutetime_info.total_system;
1319 }
1321 status = task_threads (task_list[task], &thread_list,
1322 &thread_list_len);
1323 if (status != KERN_SUCCESS)
1324 {
1325 /* Apple's `top' treats this case a zombie. It
1326 * makes sense to some extend: A `zombie'
1327 * thread is nonsense, since the task/process
1328 * is dead. */
1329 zombies++;
1330 DEBUG ("task_threads failed: %s",
1331 mach_error_string (status));
1332 if (task_list[task] != port_task_self)
1333 mach_port_deallocate (port_task_self,
1334 task_list[task]);
1335 continue; /* with next task_list */
1336 }
1338 for (thread = 0; thread < thread_list_len; thread++)
1339 {
1340 thread_data_len = THREAD_BASIC_INFO_COUNT;
1341 status = thread_info (thread_list[thread],
1342 THREAD_BASIC_INFO,
1343 (thread_info_t) &thread_data,
1344 &thread_data_len);
1345 if (status != KERN_SUCCESS)
1346 {
1347 ERROR ("thread_info failed: %s",
1348 mach_error_string (status));
1349 if (task_list[task] != port_task_self)
1350 mach_port_deallocate (port_task_self,
1351 thread_list[thread]);
1352 continue; /* with next thread_list */
1353 }
1355 if (ps != NULL)
1356 pse.num_lwp++;
1358 switch (thread_data.run_state)
1359 {
1360 case TH_STATE_RUNNING:
1361 running++;
1362 break;
1363 case TH_STATE_STOPPED:
1364 /* What exactly is `halted'? */
1365 case TH_STATE_HALTED:
1366 stopped++;
1367 break;
1368 case TH_STATE_WAITING:
1369 sleeping++;
1370 break;
1371 case TH_STATE_UNINTERRUPTIBLE:
1372 blocked++;
1373 break;
1374 /* There is no `zombie' case here,
1375 * since there are no zombie-threads.
1376 * There's only zombie tasks, which are
1377 * handled above. */
1378 default:
1379 WARNING ("Unknown thread status: %i",
1380 thread_data.run_state);
1381 break;
1382 } /* switch (thread_data.run_state) */
1384 if (task_list[task] != port_task_self)
1385 {
1386 status = mach_port_deallocate (port_task_self,
1387 thread_list[thread]);
1388 if (status != KERN_SUCCESS)
1389 ERROR ("mach_port_deallocate failed: %s",
1390 mach_error_string (status));
1391 }
1392 } /* for (thread_list) */
1394 if ((status = vm_deallocate (port_task_self,
1395 (vm_address_t) thread_list,
1396 thread_list_len * sizeof (thread_act_t)))
1397 != KERN_SUCCESS)
1398 {
1399 ERROR ("vm_deallocate failed: %s",
1400 mach_error_string (status));
1401 }
1402 thread_list = NULL;
1403 thread_list_len = 0;
1405 /* Only deallocate the task port, if it isn't our own.
1406 * Don't know what would happen in that case, but this
1407 * is what Apple's top does.. ;) */
1408 if (task_list[task] != port_task_self)
1409 {
1410 status = mach_port_deallocate (port_task_self,
1411 task_list[task]);
1412 if (status != KERN_SUCCESS)
1413 ERROR ("mach_port_deallocate failed: %s",
1414 mach_error_string (status));
1415 }
1417 if (ps != NULL)
1418 /* FIXME: cmdline should be here instead of NULL */
1419 ps_list_add (task_name, NULL, &pse);
1420 } /* for (task_list) */
1422 if ((status = vm_deallocate (port_task_self,
1423 (vm_address_t) task_list,
1424 task_list_len * sizeof (task_t))) != KERN_SUCCESS)
1425 {
1426 ERROR ("vm_deallocate failed: %s",
1427 mach_error_string (status));
1428 }
1429 task_list = NULL;
1430 task_list_len = 0;
1432 if ((status = mach_port_deallocate (port_task_self, port_pset_priv))
1433 != KERN_SUCCESS)
1434 {
1435 ERROR ("mach_port_deallocate failed: %s",
1436 mach_error_string (status));
1437 }
1438 } /* for (pset_list) */
1440 ps_submit_state ("running", running);
1441 ps_submit_state ("sleeping", sleeping);
1442 ps_submit_state ("zombies", zombies);
1443 ps_submit_state ("stopped", stopped);
1444 ps_submit_state ("blocked", blocked);
1446 for (ps = list_head_g; ps != NULL; ps = ps->next)
1447 ps_submit_proc_list (ps);
1448 /* #endif HAVE_THREAD_INFO */
1450 #elif KERNEL_LINUX
1451 int running = 0;
1452 int sleeping = 0;
1453 int zombies = 0;
1454 int stopped = 0;
1455 int paging = 0;
1456 int blocked = 0;
1458 struct dirent *ent;
1459 DIR *proc;
1460 int pid;
1462 char cmdline[ARG_MAX];
1464 int status;
1465 procstat_t ps;
1466 procstat_entry_t pse;
1467 char state;
1469 unsigned long fork_rate;
1471 procstat_t *ps_ptr;
1473 running = sleeping = zombies = stopped = paging = blocked = 0;
1474 ps_list_reset ();
1476 if ((proc = opendir ("/proc")) == NULL)
1477 {
1478 char errbuf[1024];
1479 ERROR ("Cannot open `/proc': %s",
1480 sstrerror (errno, errbuf, sizeof (errbuf)));
1481 return (-1);
1482 }
1484 while ((ent = readdir (proc)) != NULL)
1485 {
1486 if (!isdigit (ent->d_name[0]))
1487 continue;
1489 if ((pid = atoi (ent->d_name)) < 1)
1490 continue;
1492 status = ps_read_process (pid, &ps, &state);
1493 if (status != 0)
1494 {
1495 DEBUG ("ps_read_process failed: %i", status);
1496 continue;
1497 }
1499 pse.id = pid;
1500 pse.age = 0;
1502 pse.num_proc = ps.num_proc;
1503 pse.num_lwp = ps.num_lwp;
1504 pse.vmem_size = ps.vmem_size;
1505 pse.vmem_rss = ps.vmem_rss;
1506 pse.vmem_data = ps.vmem_data;
1507 pse.vmem_code = ps.vmem_code;
1508 pse.stack_size = ps.stack_size;
1510 pse.vmem_minflt = 0;
1511 pse.vmem_minflt_counter = ps.vmem_minflt_counter;
1512 pse.vmem_majflt = 0;
1513 pse.vmem_majflt_counter = ps.vmem_majflt_counter;
1515 pse.cpu_user = 0;
1516 pse.cpu_user_counter = ps.cpu_user_counter;
1517 pse.cpu_system = 0;
1518 pse.cpu_system_counter = ps.cpu_system_counter;
1520 pse.io_rchar = ps.io_rchar;
1521 pse.io_wchar = ps.io_wchar;
1522 pse.io_syscr = ps.io_syscr;
1523 pse.io_syscw = ps.io_syscw;
1525 switch (state)
1526 {
1527 case 'R': running++; break;
1528 case 'S': sleeping++; break;
1529 case 'D': blocked++; break;
1530 case 'Z': zombies++; break;
1531 case 'T': stopped++; break;
1532 case 'W': paging++; break;
1533 }
1535 ps_list_add (ps.name,
1536 ps_get_cmdline (pid, ps.name, cmdline, sizeof (cmdline)),
1537 &pse);
1538 }
1540 closedir (proc);
1542 ps_submit_state ("running", running);
1543 ps_submit_state ("sleeping", sleeping);
1544 ps_submit_state ("zombies", zombies);
1545 ps_submit_state ("stopped", stopped);
1546 ps_submit_state ("paging", paging);
1547 ps_submit_state ("blocked", blocked);
1549 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1550 ps_submit_proc_list (ps_ptr);
1552 fork_rate = read_fork_rate();
1553 if (fork_rate != ULONG_MAX)
1554 ps_submit_fork_rate(fork_rate);
1555 /* #endif KERNEL_LINUX */
1557 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD
1558 int running = 0;
1559 int sleeping = 0;
1560 int zombies = 0;
1561 int stopped = 0;
1562 int blocked = 0;
1563 int idle = 0;
1564 int wait = 0;
1566 kvm_t *kd;
1567 char errbuf[1024];
1568 char cmdline[ARG_MAX];
1569 char *cmdline_ptr;
1570 struct kinfo_proc *procs; /* array of processes */
1571 char **argv;
1572 int count; /* returns number of processes */
1573 int i;
1575 procstat_t *ps_ptr;
1576 procstat_entry_t pse;
1578 ps_list_reset ();
1580 /* Open the kvm interface, get a descriptor */
1581 kd = kvm_open (NULL, NULL, NULL, 0, errbuf);
1582 if (kd == NULL)
1583 {
1584 ERROR ("processes plugin: Cannot open kvm interface: %s",
1585 errbuf);
1586 return (0);
1587 }
1589 /* Get the list of processes. */
1590 procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, &count);
1591 if (procs == NULL)
1592 {
1593 kvm_close (kd);
1594 ERROR ("processes plugin: Cannot get kvm processes list: %s",
1595 kvm_geterr(kd));
1596 return (0);
1597 }
1599 /* Iterate through the processes in kinfo_proc */
1600 for (i = 0; i < count; i++)
1601 {
1602 /* retrieve the arguments */
1603 cmdline[0] = 0;
1604 cmdline_ptr = NULL;
1606 argv = kvm_getargv (kd, (const struct kinfo_proc *) &(procs[i]), 0);
1607 if (argv != NULL)
1608 {
1609 int status;
1610 int argc;
1612 argc = 0;
1613 while (argv[argc] != NULL)
1614 argc++;
1616 status = strjoin (cmdline, sizeof (cmdline),
1617 argv, argc, " ");
1619 if (status < 0)
1620 {
1621 WARNING ("processes plugin: Command line did "
1622 "not fit into buffer.");
1623 }
1624 else
1625 {
1626 cmdline_ptr = &cmdline[0];
1627 }
1628 }
1630 pse.id = procs[i].ki_pid;
1631 pse.age = 0;
1633 pse.num_proc = 1;
1634 pse.num_lwp = procs[i].ki_numthreads;
1636 pse.vmem_size = procs[i].ki_size;
1637 pse.vmem_rss = procs[i].ki_rssize * getpagesize();
1638 pse.vmem_data = procs[i].ki_dsize * getpagesize();
1639 pse.vmem_code = procs[i].ki_tsize * getpagesize();
1640 pse.stack_size = procs[i].ki_ssize * getpagesize();
1641 pse.vmem_minflt = 0;
1642 pse.vmem_minflt_counter = procs[i].ki_rusage.ru_minflt;
1643 pse.vmem_majflt = 0;
1644 pse.vmem_majflt_counter = procs[i].ki_rusage.ru_majflt;
1646 pse.cpu_user = 0;
1647 pse.cpu_user_counter = procs[i].ki_rusage.ru_utime.tv_sec
1648 * 1000
1649 + procs[i].ki_rusage.ru_utime.tv_usec;
1650 pse.cpu_system = 0;
1651 pse.cpu_system_counter = procs[i].ki_rusage.ru_stime.tv_sec
1652 * 1000
1653 + procs[i].ki_rusage.ru_stime.tv_usec;
1655 /* no io data */
1656 pse.io_rchar = -1;
1657 pse.io_wchar = -1;
1658 pse.io_syscr = -1;
1659 pse.io_syscw = -1;
1661 switch (procs[i].ki_stat)
1662 {
1663 case SSTOP: stopped++; break;
1664 case SSLEEP: sleeping++; break;
1665 case SRUN: running++; break;
1666 case SIDL: idle++; break;
1667 case SWAIT: wait++; break;
1668 case SLOCK: blocked++; break;
1669 case SZOMB: zombies++; break;
1670 }
1672 ps_list_add (procs[i].ki_comm, cmdline_ptr, &pse);
1673 }
1675 kvm_close(kd);
1677 ps_submit_state ("running", running);
1678 ps_submit_state ("sleeping", sleeping);
1679 ps_submit_state ("zombies", zombies);
1680 ps_submit_state ("stopped", stopped);
1681 ps_submit_state ("blocked", blocked);
1682 ps_submit_state ("idle", idle);
1683 ps_submit_state ("wait", wait);
1685 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1686 ps_submit_proc_list (ps_ptr);
1687 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
1689 #elif HAVE_PROCINFO_H
1690 /* AIX */
1691 int running = 0;
1692 int sleeping = 0;
1693 int zombies = 0;
1694 int stopped = 0;
1695 int paging = 0;
1696 int blocked = 0;
1698 pid_t pindex = 0;
1699 int nprocs;
1701 procstat_t *ps;
1702 procstat_entry_t pse;
1704 ps_list_reset ();
1705 while ((nprocs = getprocs64 (procentry, sizeof(struct procentry64),
1706 /* fdsinfo = */ NULL, sizeof(struct fdsinfo64),
1707 &pindex, MAXPROCENTRY)) > 0)
1708 {
1709 int i;
1711 for (i = 0; i < nprocs; i++)
1712 {
1713 tid64_t thindex;
1714 int nthreads;
1715 char arglist[MAXARGLN+1];
1716 char *cargs;
1717 char *cmdline;
1719 if (procentry[i].pi_state == SNONE) continue;
1720 /* if (procentry[i].pi_state == SZOMB) FIXME */
1722 cmdline = procentry[i].pi_comm;
1723 cargs = procentry[i].pi_comm;
1724 if ( procentry[i].pi_flags & SKPROC )
1725 {
1726 if (procentry[i].pi_pid == 0)
1727 cmdline = "swapper";
1728 cargs = cmdline;
1729 }
1730 else
1731 {
1732 if (getargs(&procentry[i], sizeof(struct procentry64), arglist, MAXARGLN) >= 0)
1733 {
1734 int n;
1736 n = -1;
1737 while (++n < MAXARGLN)
1738 {
1739 if (arglist[n] == '\0')
1740 {
1741 if (arglist[n+1] == '\0')
1742 break;
1743 arglist[n] = ' ';
1744 }
1745 }
1746 cargs = arglist;
1747 }
1748 }
1750 pse.id = procentry[i].pi_pid;
1751 pse.age = 0;
1752 pse.num_lwp = procentry[i].pi_thcount;
1753 pse.num_proc = 1;
1755 thindex=0;
1756 while ((nthreads = getthrds64(procentry[i].pi_pid,
1757 thrdentry, sizeof(struct thrdentry64),
1758 &thindex, MAXTHRDENTRY)) > 0)
1759 {
1760 int j;
1762 for (j=0; j< nthreads; j++)
1763 {
1764 switch (thrdentry[j].ti_state)
1765 {
1766 /* case TSNONE: break; */
1767 case TSIDL: blocked++; break; /* FIXME is really blocked */
1768 case TSRUN: running++; break;
1769 case TSSLEEP: sleeping++; break;
1770 case TSSWAP: paging++; break;
1771 case TSSTOP: stopped++; break;
1772 case TSZOMB: zombies++; break;
1773 }
1774 }
1775 if (nthreads < MAXTHRDENTRY)
1776 break;
1777 }
1779 pse.cpu_user = 0;
1780 /* tv_usec is nanosec ??? */
1781 pse.cpu_user_counter = procentry[i].pi_ru.ru_utime.tv_sec * 1000000 +
1782 procentry[i].pi_ru.ru_utime.tv_usec / 1000;
1784 pse.cpu_system = 0;
1785 /* tv_usec is nanosec ??? */
1786 pse.cpu_system_counter = procentry[i].pi_ru.ru_stime.tv_sec * 1000000 +
1787 procentry[i].pi_ru.ru_stime.tv_usec / 1000;
1789 pse.vmem_minflt = 0;
1790 pse.vmem_minflt_counter = procentry[i].pi_minflt;
1791 pse.vmem_majflt = 0;
1792 pse.vmem_majflt_counter = procentry[i].pi_majflt;
1794 pse.vmem_size = procentry[i].pi_tsize + procentry[i].pi_dvm * pagesize;
1795 pse.vmem_rss = (procentry[i].pi_drss + procentry[i].pi_trss) * pagesize;
1796 /* Not supported */
1797 pse.vmem_data = 0;
1798 pse.vmem_code = 0;
1799 pse.stack_size = 0;
1801 ps_list_add (cmdline, cargs, &pse);
1802 } /* for (i = 0 .. nprocs) */
1804 if (nprocs < MAXPROCENTRY)
1805 break;
1806 } /* while (getprocs64() > 0) */
1807 ps_submit_state ("running", running);
1808 ps_submit_state ("sleeping", sleeping);
1809 ps_submit_state ("zombies", zombies);
1810 ps_submit_state ("stopped", stopped);
1811 ps_submit_state ("paging", paging);
1812 ps_submit_state ("blocked", blocked);
1814 for (ps = list_head_g; ps != NULL; ps = ps->next)
1815 ps_submit_proc_list (ps);
1816 #endif /* HAVE_PROCINFO_H */
1818 return (0);
1819 } /* int ps_read */
1821 void module_register (void)
1822 {
1823 plugin_register_config ("processes", ps_config,
1824 config_keys, config_keys_num);
1825 plugin_register_init ("processes", ps_init);
1826 plugin_register_read ("processes", ps_read);
1827 } /* void module_register */