1 /**
2 * collectd - src/processes.c
3 * Copyright (C) 2005 Lyonel Vincent
4 * Copyright (C) 2006-2010 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 * Copyright (C) 2012 Cosmin Ioiart
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 *
26 * Authors:
27 * Lyonel Vincent <lyonel at ezix.org>
28 * Florian octo Forster <octo at collectd.org>
29 * Oleg King <king2 at kaluga.ru>
30 * Sebastian Harl <sh at tokkee.org>
31 * Andrés J. Díaz <ajdiaz at connectical.com>
32 * Manuel Sanmartin
33 * Clément Stenac <clement.stenac at diwi.org>
34 * Cosmin Ioiart <cioiart at gmail.com>
35 **/
37 #include "collectd.h"
38 #include "common.h"
39 #include "plugin.h"
40 #include "configfile.h"
42 /* Include header files for the mach system, if they exist.. */
43 #if HAVE_THREAD_INFO
44 # if HAVE_MACH_MACH_INIT_H
45 # include <mach/mach_init.h>
46 # endif
47 # if HAVE_MACH_HOST_PRIV_H
48 # include <mach/host_priv.h>
49 # endif
50 # if HAVE_MACH_MACH_ERROR_H
51 # include <mach/mach_error.h>
52 # endif
53 # if HAVE_MACH_MACH_HOST_H
54 # include <mach/mach_host.h>
55 # endif
56 # if HAVE_MACH_MACH_PORT_H
57 # include <mach/mach_port.h>
58 # endif
59 # if HAVE_MACH_MACH_TYPES_H
60 # include <mach/mach_types.h>
61 # endif
62 # if HAVE_MACH_MESSAGE_H
63 # include <mach/message.h>
64 # endif
65 # if HAVE_MACH_PROCESSOR_SET_H
66 # include <mach/processor_set.h>
67 # endif
68 # if HAVE_MACH_TASK_H
69 # include <mach/task.h>
70 # endif
71 # if HAVE_MACH_THREAD_ACT_H
72 # include <mach/thread_act.h>
73 # endif
74 # if HAVE_MACH_VM_REGION_H
75 # include <mach/vm_region.h>
76 # endif
77 # if HAVE_MACH_VM_MAP_H
78 # include <mach/vm_map.h>
79 # endif
80 # if HAVE_MACH_VM_PROT_H
81 # include <mach/vm_prot.h>
82 # endif
83 # if HAVE_SYS_SYSCTL_H
84 # include <sys/sysctl.h>
85 # endif
86 /* #endif HAVE_THREAD_INFO */
88 #elif KERNEL_LINUX
89 # if HAVE_LINUX_CONFIG_H
90 # include <linux/config.h>
91 # endif
92 # ifndef CONFIG_HZ
93 # define CONFIG_HZ 100
94 # endif
95 /* #endif KERNEL_LINUX */
97 #elif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD)
98 # include <kvm.h>
99 # include <sys/param.h>
100 # include <sys/sysctl.h>
101 # include <sys/user.h>
102 # include <sys/proc.h>
103 /* #endif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD) */
105 #elif HAVE_PROCINFO_H
106 # include <procinfo.h>
107 # include <sys/types.h>
109 #define MAXPROCENTRY 32
110 #define MAXTHRDENTRY 16
111 #define MAXARGLN 1024
112 /* #endif HAVE_PROCINFO_H */
114 #elif KERNEL_SOLARIS
115 /* Hack: Avoid #error when building a 32-bit binary with
116 * _FILE_OFFSET_BITS=64. There is a reason for this #error, as one
117 * of the structures in <sys/procfs.h> uses an off_t, but that
118 * isn't relevant to our usage of procfs. */
119 #if !defined(_LP64) && _FILE_OFFSET_BITS == 64
120 # define SAVE_FOB_64
121 # undef _FILE_OFFSET_BITS
122 #endif
124 # include <procfs.h>
126 #ifdef SAVE_FOB_64
127 # define _FILE_OFFSET_BITS 64
128 # undef SAVE_FOB_64
129 #endif
131 # include <dirent.h>
132 /* #endif KERNEL_SOLARIS */
134 #else
135 # error "No applicable input method."
136 #endif
138 #if HAVE_REGEX_H
139 # include <regex.h>
140 #endif
142 #if HAVE_KSTAT_H
143 # include <kstat.h>
144 #endif
146 #ifndef CMDLINE_BUFFER_SIZE
147 # if defined(ARG_MAX) && (ARG_MAX < 4096)
148 # define CMDLINE_BUFFER_SIZE ARG_MAX
149 # else
150 # define CMDLINE_BUFFER_SIZE 4096
151 # endif
152 #endif
154 typedef struct procstat_entry_s
155 {
156 unsigned long id;
157 unsigned long age;
159 unsigned long num_proc;
160 unsigned long num_lwp;
161 unsigned long vmem_size;
162 unsigned long vmem_rss;
163 unsigned long vmem_data;
164 unsigned long vmem_code;
165 unsigned long stack_size;
167 unsigned long vmem_minflt;
168 unsigned long vmem_majflt;
169 derive_t vmem_minflt_counter;
170 derive_t vmem_majflt_counter;
172 unsigned long cpu_user;
173 unsigned long cpu_system;
174 derive_t cpu_user_counter;
175 derive_t cpu_system_counter;
177 /* io data */
178 derive_t io_rchar;
179 derive_t io_wchar;
180 derive_t io_syscr;
181 derive_t io_syscw;
183 derive_t cswitch_vol;
184 derive_t cswitch_invol;
186 struct procstat_entry_s *next;
187 } procstat_entry_t;
189 #define PROCSTAT_NAME_LEN 256
190 typedef struct procstat
191 {
192 char name[PROCSTAT_NAME_LEN];
193 #if HAVE_REGEX_H
194 regex_t *re;
195 #endif
197 unsigned long num_proc;
198 unsigned long num_lwp;
199 unsigned long vmem_size;
200 unsigned long vmem_rss;
201 unsigned long vmem_data;
202 unsigned long vmem_code;
203 unsigned long stack_size;
205 derive_t vmem_minflt_counter;
206 derive_t vmem_majflt_counter;
208 derive_t cpu_user_counter;
209 derive_t cpu_system_counter;
211 /* io data */
212 derive_t io_rchar;
213 derive_t io_wchar;
214 derive_t io_syscr;
215 derive_t io_syscw;
217 derive_t cswitch_vol;
218 derive_t cswitch_invol;
220 struct procstat *next;
221 struct procstat_entry_s *instances;
222 } procstat_t;
224 static procstat_t *list_head_g = NULL;
226 static _Bool report_ctx_switch = 0;
228 #if HAVE_THREAD_INFO
229 static mach_port_t port_host_self;
230 static mach_port_t port_task_self;
232 static processor_set_name_array_t pset_list;
233 static mach_msg_type_number_t pset_list_len;
234 /* #endif HAVE_THREAD_INFO */
236 #elif KERNEL_LINUX
237 static long pagesize_g;
238 /* #endif KERNEL_LINUX */
240 #elif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD)
241 static int pagesize;
242 /* #endif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD) */
244 #elif HAVE_PROCINFO_H
245 static struct procentry64 procentry[MAXPROCENTRY];
246 static struct thrdentry64 thrdentry[MAXTHRDENTRY];
247 static int pagesize;
249 #ifndef _AIXVERSION_610
250 int getprocs64 (void *procsinfo, int sizproc, void *fdsinfo, int sizfd, pid_t *index, int count);
251 int getthrds64( pid_t, void *, int, tid64_t *, int );
252 #endif
253 int getargs (void *processBuffer, int bufferLen, char *argsBuffer, int argsLen);
254 #endif /* HAVE_PROCINFO_H */
256 /* put name of process from config to list_head_g tree
257 * list_head_g is a list of 'procstat_t' structs with
258 * processes names we want to watch */
259 static void ps_list_register (const char *name, const char *regexp)
260 {
261 procstat_t *new;
262 procstat_t *ptr;
263 int status;
265 new = (procstat_t *) malloc (sizeof (procstat_t));
266 if (new == NULL)
267 {
268 ERROR ("processes plugin: ps_list_register: malloc failed.");
269 return;
270 }
271 memset (new, 0, sizeof (procstat_t));
272 sstrncpy (new->name, name, sizeof (new->name));
274 #if HAVE_REGEX_H
275 if (regexp != NULL)
276 {
277 DEBUG ("ProcessMatch: adding \"%s\" as criteria to process %s.", regexp, name);
278 new->re = (regex_t *) malloc (sizeof (regex_t));
279 if (new->re == NULL)
280 {
281 ERROR ("processes plugin: ps_list_register: malloc failed.");
282 sfree (new);
283 return;
284 }
286 status = regcomp (new->re, regexp, REG_EXTENDED | REG_NOSUB);
287 if (status != 0)
288 {
289 DEBUG ("ProcessMatch: compiling the regular expression \"%s\" failed.", regexp);
290 sfree(new->re);
291 return;
292 }
293 }
294 #else
295 if (regexp != NULL)
296 {
297 ERROR ("processes plugin: ps_list_register: "
298 "Regular expression \"%s\" found in config "
299 "file, but support for regular expressions "
300 "has been disabled at compile time.",
301 regexp);
302 sfree (new);
303 return;
304 }
305 #endif
307 for (ptr = list_head_g; ptr != NULL; ptr = ptr->next)
308 {
309 if (strcmp (ptr->name, name) == 0)
310 {
311 WARNING ("processes plugin: You have configured more "
312 "than one `Process' or "
313 "`ProcessMatch' with the same name. "
314 "All but the first setting will be "
315 "ignored.");
316 sfree (new->re);
317 sfree (new);
318 return;
319 }
321 if (ptr->next == NULL)
322 break;
323 }
325 if (ptr == NULL)
326 list_head_g = new;
327 else
328 ptr->next = new;
329 } /* void ps_list_register */
331 /* try to match name against entry, returns 1 if success */
332 static int ps_list_match (const char *name, const char *cmdline, procstat_t *ps)
333 {
334 #if HAVE_REGEX_H
335 if (ps->re != NULL)
336 {
337 int status;
338 const char *str;
340 str = cmdline;
341 if ((str == NULL) || (str[0] == 0))
342 str = name;
344 assert (str != NULL);
346 status = regexec (ps->re, str,
347 /* nmatch = */ 0,
348 /* pmatch = */ NULL,
349 /* eflags = */ 0);
350 if (status == 0)
351 return (1);
352 }
353 else
354 #endif
355 if (strcmp (ps->name, name) == 0)
356 return (1);
358 return (0);
359 } /* int ps_list_match */
361 /* add process entry to 'instances' of process 'name' (or refresh it) */
362 static void ps_list_add (const char *name, const char *cmdline, procstat_entry_t *entry)
363 {
364 procstat_t *ps;
365 procstat_entry_t *pse;
367 if (entry->id == 0)
368 return;
370 for (ps = list_head_g; ps != NULL; ps = ps->next)
371 {
372 if ((ps_list_match (name, cmdline, ps)) == 0)
373 continue;
375 for (pse = ps->instances; pse != NULL; pse = pse->next)
376 if ((pse->id == entry->id) || (pse->next == NULL))
377 break;
379 if ((pse == NULL) || (pse->id != entry->id))
380 {
381 procstat_entry_t *new;
383 new = (procstat_entry_t *) malloc (sizeof (procstat_entry_t));
384 if (new == NULL)
385 return;
386 memset (new, 0, sizeof (procstat_entry_t));
387 new->id = entry->id;
389 if (pse == NULL)
390 ps->instances = new;
391 else
392 pse->next = new;
394 pse = new;
395 }
397 pse->age = 0;
398 pse->num_proc = entry->num_proc;
399 pse->num_lwp = entry->num_lwp;
400 pse->vmem_size = entry->vmem_size;
401 pse->vmem_rss = entry->vmem_rss;
402 pse->vmem_data = entry->vmem_data;
403 pse->vmem_code = entry->vmem_code;
404 pse->stack_size = entry->stack_size;
405 pse->io_rchar = entry->io_rchar;
406 pse->io_wchar = entry->io_wchar;
407 pse->io_syscr = entry->io_syscr;
408 pse->io_syscw = entry->io_syscw;
409 pse->cswitch_vol = entry->cswitch_vol;
410 pse->cswitch_invol = entry->cswitch_invol;
412 ps->num_proc += pse->num_proc;
413 ps->num_lwp += pse->num_lwp;
414 ps->vmem_size += pse->vmem_size;
415 ps->vmem_rss += pse->vmem_rss;
416 ps->vmem_data += pse->vmem_data;
417 ps->vmem_code += pse->vmem_code;
418 ps->stack_size += pse->stack_size;
420 ps->io_rchar += ((pse->io_rchar == -1)?0:pse->io_rchar);
421 ps->io_wchar += ((pse->io_wchar == -1)?0:pse->io_wchar);
422 ps->io_syscr += ((pse->io_syscr == -1)?0:pse->io_syscr);
423 ps->io_syscw += ((pse->io_syscw == -1)?0:pse->io_syscw);
425 ps->cswitch_vol += ((pse->cswitch_vol == -1)?0:pse->cswitch_vol);
426 ps->cswitch_invol += ((pse->cswitch_invol == -1)?0:pse->cswitch_invol);
428 if ((entry->vmem_minflt_counter == 0)
429 && (entry->vmem_majflt_counter == 0))
430 {
431 pse->vmem_minflt_counter += entry->vmem_minflt;
432 pse->vmem_minflt = entry->vmem_minflt;
434 pse->vmem_majflt_counter += entry->vmem_majflt;
435 pse->vmem_majflt = entry->vmem_majflt;
436 }
437 else
438 {
439 if (entry->vmem_minflt_counter < pse->vmem_minflt_counter)
440 {
441 pse->vmem_minflt = entry->vmem_minflt_counter
442 + (ULONG_MAX - pse->vmem_minflt_counter);
443 }
444 else
445 {
446 pse->vmem_minflt = entry->vmem_minflt_counter - pse->vmem_minflt_counter;
447 }
448 pse->vmem_minflt_counter = entry->vmem_minflt_counter;
450 if (entry->vmem_majflt_counter < pse->vmem_majflt_counter)
451 {
452 pse->vmem_majflt = entry->vmem_majflt_counter
453 + (ULONG_MAX - pse->vmem_majflt_counter);
454 }
455 else
456 {
457 pse->vmem_majflt = entry->vmem_majflt_counter - pse->vmem_majflt_counter;
458 }
459 pse->vmem_majflt_counter = entry->vmem_majflt_counter;
460 }
462 ps->vmem_minflt_counter += pse->vmem_minflt;
463 ps->vmem_majflt_counter += pse->vmem_majflt;
465 if ((entry->cpu_user_counter == 0)
466 && (entry->cpu_system_counter == 0))
467 {
468 pse->cpu_user_counter += entry->cpu_user;
469 pse->cpu_user = entry->cpu_user;
471 pse->cpu_system_counter += entry->cpu_system;
472 pse->cpu_system = entry->cpu_system;
473 }
474 else
475 {
476 if (entry->cpu_user_counter < pse->cpu_user_counter)
477 {
478 pse->cpu_user = entry->cpu_user_counter
479 + (ULONG_MAX - pse->cpu_user_counter);
480 }
481 else
482 {
483 pse->cpu_user = entry->cpu_user_counter - pse->cpu_user_counter;
484 }
485 pse->cpu_user_counter = entry->cpu_user_counter;
487 if (entry->cpu_system_counter < pse->cpu_system_counter)
488 {
489 pse->cpu_system = entry->cpu_system_counter
490 + (ULONG_MAX - pse->cpu_system_counter);
491 }
492 else
493 {
494 pse->cpu_system = entry->cpu_system_counter - pse->cpu_system_counter;
495 }
496 pse->cpu_system_counter = entry->cpu_system_counter;
497 }
499 ps->cpu_user_counter += pse->cpu_user;
500 ps->cpu_system_counter += pse->cpu_system;
501 }
502 }
504 /* remove old entries from instances of processes in list_head_g */
505 static void ps_list_reset (void)
506 {
507 procstat_t *ps;
508 procstat_entry_t *pse;
509 procstat_entry_t *pse_prev;
511 for (ps = list_head_g; ps != NULL; ps = ps->next)
512 {
513 ps->num_proc = 0;
514 ps->num_lwp = 0;
515 ps->vmem_size = 0;
516 ps->vmem_rss = 0;
517 ps->vmem_data = 0;
518 ps->vmem_code = 0;
519 ps->stack_size = 0;
520 ps->io_rchar = -1;
521 ps->io_wchar = -1;
522 ps->io_syscr = -1;
523 ps->io_syscw = -1;
524 ps->cswitch_vol = -1;
525 ps->cswitch_invol = -1;
527 pse_prev = NULL;
528 pse = ps->instances;
529 while (pse != NULL)
530 {
531 if (pse->age > 10)
532 {
533 DEBUG ("Removing this procstat entry cause it's too old: "
534 "id = %lu; name = %s;",
535 pse->id, ps->name);
537 if (pse_prev == NULL)
538 {
539 ps->instances = pse->next;
540 free (pse);
541 pse = ps->instances;
542 }
543 else
544 {
545 pse_prev->next = pse->next;
546 free (pse);
547 pse = pse_prev->next;
548 }
549 }
550 else
551 {
552 pse->age++;
553 pse_prev = pse;
554 pse = pse->next;
555 }
556 } /* while (pse != NULL) */
557 } /* for (ps = list_head_g; ps != NULL; ps = ps->next) */
558 }
560 /* put all pre-defined 'Process' names from config to list_head_g tree */
561 static int ps_config (oconfig_item_t *ci)
562 {
563 int i;
565 for (i = 0; i < ci->children_num; ++i) {
566 oconfig_item_t *c = ci->children + i;
568 if (strcasecmp (c->key, "Process") == 0)
569 {
570 if ((c->values_num != 1)
571 || (OCONFIG_TYPE_STRING != c->values[0].type)) {
572 ERROR ("processes plugin: `Process' expects exactly "
573 "one string argument (got %i).",
574 c->values_num);
575 continue;
576 }
578 if (c->children_num != 0) {
579 WARNING ("processes plugin: the `Process' config option "
580 "does not expect any child elements -- ignoring "
581 "content (%i elements) of the <Process '%s'> block.",
582 c->children_num, c->values[0].value.string);
583 }
585 ps_list_register (c->values[0].value.string, NULL);
586 }
587 else if (strcasecmp (c->key, "ProcessMatch") == 0)
588 {
589 if ((c->values_num != 2)
590 || (OCONFIG_TYPE_STRING != c->values[0].type)
591 || (OCONFIG_TYPE_STRING != c->values[1].type))
592 {
593 ERROR ("processes plugin: `ProcessMatch' needs exactly "
594 "two string arguments (got %i).",
595 c->values_num);
596 continue;
597 }
599 if (c->children_num != 0) {
600 WARNING ("processes plugin: the `ProcessMatch' config option "
601 "does not expect any child elements -- ignoring "
602 "content (%i elements) of the <ProcessMatch '%s' '%s'> "
603 "block.", c->children_num, c->values[0].value.string,
604 c->values[1].value.string);
605 }
607 ps_list_register (c->values[0].value.string,
608 c->values[1].value.string);
609 }
610 else if (strcasecmp (c->key, "CollectContextSwitch") == 0)
611 {
612 if ((c->values_num != 1)
613 || (c->values[0].type != OCONFIG_TYPE_BOOLEAN))
614 {
615 ERROR ("processes plugin: `CollectContextSwitch' needs exactly "
616 "one boolean argument.");
617 continue;
618 }
619 report_ctx_switch = c->values[0].value.boolean ? 1 : 0;
620 }
621 else
622 {
623 ERROR ("processes plugin: The `%s' configuration option is not "
624 "understood and will be ignored.", c->key);
625 continue;
626 }
627 }
629 return (0);
630 }
632 static int ps_init (void)
633 {
634 #if HAVE_THREAD_INFO
635 kern_return_t status;
637 port_host_self = mach_host_self ();
638 port_task_self = mach_task_self ();
640 if (pset_list != NULL)
641 {
642 vm_deallocate (port_task_self,
643 (vm_address_t) pset_list,
644 pset_list_len * sizeof (processor_set_t));
645 pset_list = NULL;
646 pset_list_len = 0;
647 }
649 if ((status = host_processor_sets (port_host_self,
650 &pset_list,
651 &pset_list_len)) != KERN_SUCCESS)
652 {
653 ERROR ("host_processor_sets failed: %s\n",
654 mach_error_string (status));
655 pset_list = NULL;
656 pset_list_len = 0;
657 return (-1);
658 }
659 /* #endif HAVE_THREAD_INFO */
661 #elif KERNEL_LINUX
662 pagesize_g = sysconf(_SC_PAGESIZE);
663 DEBUG ("pagesize_g = %li; CONFIG_HZ = %i;",
664 pagesize_g, CONFIG_HZ);
665 /* #endif KERNEL_LINUX */
667 #elif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD)
668 pagesize = getpagesize();
669 /* #endif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD) */
671 #elif HAVE_PROCINFO_H
672 pagesize = getpagesize();
673 #endif /* HAVE_PROCINFO_H */
675 return (0);
676 } /* int ps_init */
678 /* submit global state (e.g.: qty of zombies, running, etc..) */
679 static void ps_submit_state (const char *state, double value)
680 {
681 value_t values[1];
682 value_list_t vl = VALUE_LIST_INIT;
684 values[0].gauge = value;
686 vl.values = values;
687 vl.values_len = 1;
688 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
689 sstrncpy (vl.plugin, "processes", sizeof (vl.plugin));
690 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
691 sstrncpy (vl.type, "ps_state", sizeof (vl.type));
692 sstrncpy (vl.type_instance, state, sizeof (vl.type_instance));
694 plugin_dispatch_values (&vl);
695 }
697 /* submit info about specific process (e.g.: memory taken, cpu usage, etc..) */
698 static void ps_submit_proc_list (procstat_t *ps)
699 {
700 value_t values[2];
701 value_list_t vl = VALUE_LIST_INIT;
703 vl.values = values;
704 vl.values_len = 2;
705 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
706 sstrncpy (vl.plugin, "processes", sizeof (vl.plugin));
707 sstrncpy (vl.plugin_instance, ps->name, sizeof (vl.plugin_instance));
709 sstrncpy (vl.type, "ps_vm", sizeof (vl.type));
710 vl.values[0].gauge = ps->vmem_size;
711 vl.values_len = 1;
712 plugin_dispatch_values (&vl);
714 sstrncpy (vl.type, "ps_rss", sizeof (vl.type));
715 vl.values[0].gauge = ps->vmem_rss;
716 vl.values_len = 1;
717 plugin_dispatch_values (&vl);
719 sstrncpy (vl.type, "ps_data", sizeof (vl.type));
720 vl.values[0].gauge = ps->vmem_data;
721 vl.values_len = 1;
722 plugin_dispatch_values (&vl);
724 sstrncpy (vl.type, "ps_code", sizeof (vl.type));
725 vl.values[0].gauge = ps->vmem_code;
726 vl.values_len = 1;
727 plugin_dispatch_values (&vl);
729 sstrncpy (vl.type, "ps_stacksize", sizeof (vl.type));
730 vl.values[0].gauge = ps->stack_size;
731 vl.values_len = 1;
732 plugin_dispatch_values (&vl);
734 sstrncpy (vl.type, "ps_cputime", sizeof (vl.type));
735 vl.values[0].derive = ps->cpu_user_counter;
736 vl.values[1].derive = ps->cpu_system_counter;
737 vl.values_len = 2;
738 plugin_dispatch_values (&vl);
740 sstrncpy (vl.type, "ps_count", sizeof (vl.type));
741 vl.values[0].gauge = ps->num_proc;
742 vl.values[1].gauge = ps->num_lwp;
743 vl.values_len = 2;
744 plugin_dispatch_values (&vl);
746 sstrncpy (vl.type, "ps_pagefaults", sizeof (vl.type));
747 vl.values[0].derive = ps->vmem_minflt_counter;
748 vl.values[1].derive = ps->vmem_majflt_counter;
749 vl.values_len = 2;
750 plugin_dispatch_values (&vl);
752 if ( (ps->io_rchar != -1) && (ps->io_wchar != -1) )
753 {
754 sstrncpy (vl.type, "ps_disk_octets", sizeof (vl.type));
755 vl.values[0].derive = ps->io_rchar;
756 vl.values[1].derive = ps->io_wchar;
757 vl.values_len = 2;
758 plugin_dispatch_values (&vl);
759 }
761 if ( (ps->io_syscr != -1) && (ps->io_syscw != -1) )
762 {
763 sstrncpy (vl.type, "ps_disk_ops", sizeof (vl.type));
764 vl.values[0].derive = ps->io_syscr;
765 vl.values[1].derive = ps->io_syscw;
766 vl.values_len = 2;
767 plugin_dispatch_values (&vl);
768 }
770 if ( report_ctx_switch )
771 {
772 sstrncpy (vl.type, "ps_cswitch_vol", sizeof (vl.type));
773 vl.values[0].derive = ps->cswitch_vol;
774 vl.values_len = 1;
775 plugin_dispatch_values (&vl);
777 sstrncpy (vl.type, "ps_cswitch_invol", sizeof (vl.type));
778 vl.values[0].derive = ps->cswitch_invol;
779 vl.values_len = 1;
780 plugin_dispatch_values (&vl);
781 }
783 DEBUG ("name = %s; num_proc = %lu; num_lwp = %lu; "
784 "vmem_size = %lu; vmem_rss = %lu; vmem_data = %lu; "
785 "vmem_code = %lu; "
786 "vmem_minflt_counter = %"PRIi64"; vmem_majflt_counter = %"PRIi64"; "
787 "cpu_user_counter = %"PRIi64"; cpu_system_counter = %"PRIi64"; "
788 "io_rchar = %"PRIi64"; io_wchar = %"PRIi64"; "
789 "io_syscr = %"PRIi64"; io_syscw = %"PRIi64"; "
790 "cswitch_vol = %"PRIi64"; cswitch_invol = %"PRIi64";",
791 ps->name, ps->num_proc, ps->num_lwp,
792 ps->vmem_size, ps->vmem_rss,
793 ps->vmem_data, ps->vmem_code,
794 ps->vmem_minflt_counter, ps->vmem_majflt_counter,
795 ps->cpu_user_counter, ps->cpu_system_counter,
796 ps->io_rchar, ps->io_wchar, ps->io_syscr, ps->io_syscw,
797 ps->cswitch_vol, ps->cswitch_invol);
798 } /* void ps_submit_proc_list */
800 #if KERNEL_LINUX || KERNEL_SOLARIS
801 static void ps_submit_fork_rate (derive_t value)
802 {
803 value_t values[1];
804 value_list_t vl = VALUE_LIST_INIT;
806 values[0].derive = value;
808 vl.values = values;
809 vl.values_len = 1;
810 sstrncpy(vl.host, hostname_g, sizeof (vl.host));
811 sstrncpy(vl.plugin, "processes", sizeof (vl.plugin));
812 sstrncpy(vl.plugin_instance, "", sizeof (vl.plugin_instance));
813 sstrncpy(vl.type, "fork_rate", sizeof (vl.type));
814 sstrncpy(vl.type_instance, "", sizeof (vl.type_instance));
816 plugin_dispatch_values(&vl);
817 }
818 #endif /* KERNEL_LINUX || KERNEL_SOLARIS*/
820 /* ------- additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
821 #if KERNEL_LINUX
822 static procstat_t *ps_read_tasks_status (int pid, procstat_t *ps)
823 {
824 char dirname[64];
825 DIR *dh;
826 char filename[64];
827 FILE *fh;
828 struct dirent *ent;
829 unsigned long long cswitch_vol = 0;
830 unsigned long long cswitch_invol = 0;
831 char buffer[1024];
832 char *fields[8];
833 int numfields;
835 ssnprintf (dirname, sizeof (dirname), "/proc/%i/task", pid);
837 if ((dh = opendir (dirname)) == NULL)
838 {
839 DEBUG ("Failed to open directory `%s'", dirname);
840 return (NULL);
841 }
843 while ((ent = readdir (dh)) != NULL)
844 {
845 char *tpid;
847 if (!isdigit ((int) ent->d_name[0]))
848 continue;
850 tpid = ent->d_name;
852 ssnprintf (filename, sizeof (filename), "/proc/%i/task/%s/status", pid, tpid);
853 if ((fh = fopen (filename, "r")) == NULL)
854 {
855 DEBUG ("Failed to open file `%s'", filename);
856 continue;
857 }
859 while (fgets (buffer, sizeof(buffer), fh) != NULL)
860 {
861 long long tmp;
862 char *endptr;
864 if (strncmp (buffer, "voluntary_ctxt_switches", 23) != 0
865 && strncmp (buffer, "nonvoluntary_ctxt_switches", 26) != 0)
866 continue;
868 numfields = strsplit (buffer, fields,
869 STATIC_ARRAY_SIZE (fields));
871 if (numfields < 2)
872 continue;
874 errno = 0;
875 endptr = NULL;
876 tmp = strtoll (fields[1], &endptr, /* base = */ 10);
877 if ((errno == 0) && (endptr != fields[1]))
878 {
879 if (strncmp (buffer, "voluntary_ctxt_switches", 23) == 0)
880 {
881 cswitch_vol += tmp;
882 }
883 else if (strncmp (buffer, "nonvoluntary_ctxt_switches", 26) == 0)
884 {
885 cswitch_invol += tmp;
886 }
887 }
888 } /* while (fgets) */
890 if (fclose (fh))
891 {
892 char errbuf[1024];
893 WARNING ("processes: fclose: %s",
894 sstrerror (errno, errbuf, sizeof (errbuf)));
895 }
896 }
897 closedir (dh);
899 ps->cswitch_vol = cswitch_vol;
900 ps->cswitch_invol = cswitch_invol;
902 return (ps);
903 } /* int *ps_read_tasks_status */
905 static int ps_read_tasks (int pid)
906 {
907 char dirname[64];
908 DIR *dh;
909 struct dirent *ent;
910 int count = 0;
912 ssnprintf (dirname, sizeof (dirname), "/proc/%i/task", pid);
914 if ((dh = opendir (dirname)) == NULL)
915 {
916 DEBUG ("Failed to open directory `%s'", dirname);
917 return (-1);
918 }
920 while ((ent = readdir (dh)) != NULL)
921 {
922 if (!isdigit ((int) ent->d_name[0]))
923 continue;
924 else
925 count++;
926 }
927 closedir (dh);
929 return ((count >= 1) ? count : 1);
930 } /* int *ps_read_tasks */
932 /* Read advanced virtual memory data from /proc/pid/status */
933 static procstat_t *ps_read_vmem (int pid, procstat_t *ps)
934 {
935 FILE *fh;
936 char buffer[1024];
937 char filename[64];
938 unsigned long long lib = 0;
939 unsigned long long exe = 0;
940 unsigned long long data = 0;
941 char *fields[8];
942 int numfields;
944 ssnprintf (filename, sizeof (filename), "/proc/%i/status", pid);
945 if ((fh = fopen (filename, "r")) == NULL)
946 return (NULL);
948 while (fgets (buffer, sizeof(buffer), fh) != NULL)
949 {
950 long long tmp;
951 char *endptr;
953 if (strncmp (buffer, "Vm", 2) != 0)
954 continue;
956 numfields = strsplit (buffer, fields,
957 STATIC_ARRAY_SIZE (fields));
959 if (numfields < 2)
960 continue;
962 errno = 0;
963 endptr = NULL;
964 tmp = strtoll (fields[1], &endptr, /* base = */ 10);
965 if ((errno == 0) && (endptr != fields[1]))
966 {
967 if (strncmp (buffer, "VmData", 6) == 0)
968 {
969 data = tmp;
970 }
971 else if (strncmp (buffer, "VmLib", 5) == 0)
972 {
973 lib = tmp;
974 }
975 else if (strncmp(buffer, "VmExe", 5) == 0)
976 {
977 exe = tmp;
978 }
979 }
980 } /* while (fgets) */
982 if (fclose (fh))
983 {
984 char errbuf[1024];
985 WARNING ("processes: fclose: %s",
986 sstrerror (errno, errbuf, sizeof (errbuf)));
987 }
989 ps->vmem_data = data * 1024;
990 ps->vmem_code = (exe + lib) * 1024;
992 return (ps);
993 } /* procstat_t *ps_read_vmem */
995 static procstat_t *ps_read_io (int pid, procstat_t *ps)
996 {
997 FILE *fh;
998 char buffer[1024];
999 char filename[64];
1001 char *fields[8];
1002 int numfields;
1004 ssnprintf (filename, sizeof (filename), "/proc/%i/io", pid);
1005 if ((fh = fopen (filename, "r")) == NULL)
1006 return (NULL);
1008 while (fgets (buffer, sizeof (buffer), fh) != NULL)
1009 {
1010 derive_t *val = NULL;
1011 long long tmp;
1012 char *endptr;
1014 if (strncasecmp (buffer, "rchar:", 6) == 0)
1015 val = &(ps->io_rchar);
1016 else if (strncasecmp (buffer, "wchar:", 6) == 0)
1017 val = &(ps->io_wchar);
1018 else if (strncasecmp (buffer, "syscr:", 6) == 0)
1019 val = &(ps->io_syscr);
1020 else if (strncasecmp (buffer, "syscw:", 6) == 0)
1021 val = &(ps->io_syscw);
1022 else
1023 continue;
1025 numfields = strsplit (buffer, fields,
1026 STATIC_ARRAY_SIZE (fields));
1028 if (numfields < 2)
1029 continue;
1031 errno = 0;
1032 endptr = NULL;
1033 tmp = strtoll (fields[1], &endptr, /* base = */ 10);
1034 if ((errno != 0) || (endptr == fields[1]))
1035 *val = -1;
1036 else
1037 *val = (derive_t) tmp;
1038 } /* while (fgets) */
1040 if (fclose (fh))
1041 {
1042 char errbuf[1024];
1043 WARNING ("processes: fclose: %s",
1044 sstrerror (errno, errbuf, sizeof (errbuf)));
1045 }
1047 return (ps);
1048 } /* procstat_t *ps_read_io */
1050 int ps_read_process (int pid, procstat_t *ps, char *state)
1051 {
1052 char filename[64];
1053 char buffer[1024];
1055 char *fields[64];
1056 char fields_len;
1058 int buffer_len;
1060 char *buffer_ptr;
1061 size_t name_start_pos;
1062 size_t name_end_pos;
1063 size_t name_len;
1065 derive_t cpu_user_counter;
1066 derive_t cpu_system_counter;
1067 long long unsigned vmem_size;
1068 long long unsigned vmem_rss;
1069 long long unsigned stack_size;
1071 memset (ps, 0, sizeof (procstat_t));
1073 ssnprintf (filename, sizeof (filename), "/proc/%i/stat", pid);
1075 buffer_len = read_file_contents (filename,
1076 buffer, sizeof(buffer) - 1);
1077 if (buffer_len <= 0)
1078 return (-1);
1079 buffer[buffer_len] = 0;
1081 /* The name of the process is enclosed in parens. Since the name can
1082 * contain parens itself, spaces, numbers and pretty much everything
1083 * else, use these to determine the process name. We don't use
1084 * strchr(3) and strrchr(3) to avoid pointer arithmetic which would
1085 * otherwise be required to determine name_len. */
1086 name_start_pos = 0;
1087 while ((buffer[name_start_pos] != '(')
1088 && (name_start_pos < buffer_len))
1089 name_start_pos++;
1091 name_end_pos = buffer_len;
1092 while ((buffer[name_end_pos] != ')')
1093 && (name_end_pos > 0))
1094 name_end_pos--;
1096 /* Either '(' or ')' is not found or they are in the wrong order.
1097 * Anyway, something weird that shouldn't happen ever. */
1098 if (name_start_pos >= name_end_pos)
1099 {
1100 ERROR ("processes plugin: name_start_pos = %zu >= name_end_pos = %zu",
1101 name_start_pos, name_end_pos);
1102 return (-1);
1103 }
1105 name_len = (name_end_pos - name_start_pos) - 1;
1106 if (name_len >= sizeof (ps->name))
1107 name_len = sizeof (ps->name) - 1;
1109 sstrncpy (ps->name, &buffer[name_start_pos + 1], name_len + 1);
1111 if ((buffer_len - name_end_pos) < 2)
1112 return (-1);
1113 buffer_ptr = &buffer[name_end_pos + 2];
1115 fields_len = strsplit (buffer_ptr, fields, STATIC_ARRAY_SIZE (fields));
1116 if (fields_len < 22)
1117 {
1118 DEBUG ("processes plugin: ps_read_process (pid = %i):"
1119 " `%s' has only %i fields..",
1120 (int) pid, filename, fields_len);
1121 return (-1);
1122 }
1124 *state = fields[0][0];
1126 if (*state == 'Z')
1127 {
1128 ps->num_lwp = 0;
1129 ps->num_proc = 0;
1130 }
1131 else
1132 {
1133 if ( (ps->num_lwp = ps_read_tasks (pid)) == -1 )
1134 {
1135 /* returns -1 => kernel 2.4 */
1136 ps->num_lwp = 1;
1137 }
1138 ps->num_proc = 1;
1139 }
1141 /* Leave the rest at zero if this is only a zombi */
1142 if (ps->num_proc == 0)
1143 {
1144 DEBUG ("processes plugin: This is only a zombi: pid = %i; "
1145 "name = %s;", pid, ps->name);
1146 return (0);
1147 }
1149 cpu_user_counter = atoll (fields[11]);
1150 cpu_system_counter = atoll (fields[12]);
1151 vmem_size = atoll (fields[20]);
1152 vmem_rss = atoll (fields[21]);
1153 ps->vmem_minflt_counter = atol (fields[7]);
1154 ps->vmem_majflt_counter = atol (fields[9]);
1156 {
1157 unsigned long long stack_start = atoll (fields[25]);
1158 unsigned long long stack_ptr = atoll (fields[26]);
1160 stack_size = (stack_start > stack_ptr)
1161 ? stack_start - stack_ptr
1162 : stack_ptr - stack_start;
1163 }
1165 /* Convert jiffies to useconds */
1166 cpu_user_counter = cpu_user_counter * 1000000 / CONFIG_HZ;
1167 cpu_system_counter = cpu_system_counter * 1000000 / CONFIG_HZ;
1168 vmem_rss = vmem_rss * pagesize_g;
1170 if ( (ps_read_vmem(pid, ps)) == NULL)
1171 {
1172 /* No VMem data */
1173 ps->vmem_data = -1;
1174 ps->vmem_code = -1;
1175 DEBUG("ps_read_process: did not get vmem data for pid %i",pid);
1176 }
1178 ps->cpu_user_counter = cpu_user_counter;
1179 ps->cpu_system_counter = cpu_system_counter;
1180 ps->vmem_size = (unsigned long) vmem_size;
1181 ps->vmem_rss = (unsigned long) vmem_rss;
1182 ps->stack_size = (unsigned long) stack_size;
1184 if ( (ps_read_io (pid, ps)) == NULL)
1185 {
1186 /* no io data */
1187 ps->io_rchar = -1;
1188 ps->io_wchar = -1;
1189 ps->io_syscr = -1;
1190 ps->io_syscw = -1;
1192 DEBUG("ps_read_process: not get io data for pid %i",pid);
1193 }
1195 if ( report_ctx_switch )
1196 {
1197 if ( (ps_read_tasks_status(pid, ps)) == NULL)
1198 {
1199 ps->cswitch_vol = -1;
1200 ps->cswitch_invol = -1;
1202 DEBUG("ps_read_tasks_status: not get context "
1203 "switch data for pid %i",pid);
1204 }
1205 }
1207 /* success */
1208 return (0);
1209 } /* int ps_read_process (...) */
1211 static char *ps_get_cmdline (pid_t pid, char *name, char *buf, size_t buf_len)
1212 {
1213 char *buf_ptr;
1214 size_t len;
1216 char file[PATH_MAX];
1217 int fd;
1219 size_t n;
1221 if ((pid < 1) || (NULL == buf) || (buf_len < 2))
1222 return NULL;
1224 ssnprintf (file, sizeof (file), "/proc/%u/cmdline",
1225 (unsigned int) pid);
1227 errno = 0;
1228 fd = open (file, O_RDONLY);
1229 if (fd < 0) {
1230 char errbuf[4096];
1231 /* ENOENT means the process exited while we were handling it.
1232 * Don't complain about this, it only fills the logs. */
1233 if (errno != ENOENT)
1234 WARNING ("processes plugin: Failed to open `%s': %s.", file,
1235 sstrerror (errno, errbuf, sizeof (errbuf)));
1236 return NULL;
1237 }
1239 buf_ptr = buf;
1240 len = buf_len;
1242 n = 0;
1244 while (42) {
1245 ssize_t status;
1247 status = read (fd, (void *)buf_ptr, len);
1249 if (status < 0) {
1250 char errbuf[1024];
1252 if ((EAGAIN == errno) || (EINTR == errno))
1253 continue;
1255 WARNING ("processes plugin: Failed to read from `%s': %s.", file,
1256 sstrerror (errno, errbuf, sizeof (errbuf)));
1257 close (fd);
1258 return NULL;
1259 }
1261 n += status;
1263 if (status == 0)
1264 break;
1266 buf_ptr += status;
1267 len -= status;
1269 if (len <= 0)
1270 break;
1271 }
1273 close (fd);
1275 if (0 == n) {
1276 /* cmdline not available; e.g. kernel thread, zombie */
1277 if (NULL == name)
1278 return NULL;
1280 ssnprintf (buf, buf_len, "[%s]", name);
1281 return buf;
1282 }
1284 assert (n <= buf_len);
1286 if (n == buf_len)
1287 --n;
1288 buf[n] = '\0';
1290 --n;
1291 /* remove trailing whitespace */
1292 while ((n > 0) && (isspace (buf[n]) || ('\0' == buf[n]))) {
1293 buf[n] = '\0';
1294 --n;
1295 }
1297 /* arguments are separated by '\0' in /proc/<pid>/cmdline */
1298 while (n > 0) {
1299 if ('\0' == buf[n])
1300 buf[n] = ' ';
1301 --n;
1302 }
1303 return buf;
1304 } /* char *ps_get_cmdline (...) */
1306 static int read_fork_rate ()
1307 {
1308 FILE *proc_stat;
1309 char buffer[1024];
1310 value_t value;
1311 _Bool value_valid = 0;
1313 proc_stat = fopen ("/proc/stat", "r");
1314 if (proc_stat == NULL)
1315 {
1316 char errbuf[1024];
1317 ERROR ("processes plugin: fopen (/proc/stat) failed: %s",
1318 sstrerror (errno, errbuf, sizeof (errbuf)));
1319 return (-1);
1320 }
1322 while (fgets (buffer, sizeof (buffer), proc_stat) != NULL)
1323 {
1324 int status;
1325 char *fields[3];
1326 int fields_num;
1328 fields_num = strsplit (buffer, fields,
1329 STATIC_ARRAY_SIZE (fields));
1330 if (fields_num != 2)
1331 continue;
1333 if (strcmp ("processes", fields[0]) != 0)
1334 continue;
1336 status = parse_value (fields[1], &value, DS_TYPE_DERIVE);
1337 if (status == 0)
1338 value_valid = 1;
1340 break;
1341 }
1342 fclose(proc_stat);
1344 if (!value_valid)
1345 return (-1);
1347 ps_submit_fork_rate (value.derive);
1348 return (0);
1349 }
1350 #endif /*KERNEL_LINUX */
1352 #if KERNEL_SOLARIS
1353 static const char *ps_get_cmdline (long pid, /* {{{ */
1354 char *buffer, size_t buffer_size)
1355 {
1356 char path[PATH_MAX];
1357 psinfo_t info;
1358 int status;
1360 snprintf(path, sizeof (path), "/proc/%li/psinfo", pid);
1362 status = read_file_contents (path, (void *) &info, sizeof (info));
1363 if (status != sizeof (info))
1364 {
1365 ERROR ("processes plugin: Unexpected return value "
1366 "while reading \"%s\": "
1367 "Returned %i but expected %zu.",
1368 path, status, buffer_size);
1369 return (NULL);
1370 }
1372 info.pr_psargs[sizeof (info.pr_psargs) - 1] = 0;
1373 sstrncpy (buffer, info.pr_psargs, buffer_size);
1375 return (buffer);
1376 } /* }}} int ps_get_cmdline */
1378 /*
1379 * Reads process information on the Solaris OS. The information comes mainly from
1380 * /proc/PID/status, /proc/PID/psinfo and /proc/PID/usage
1381 * The values for input and ouput chars are calculated "by hand"
1382 * Added a few "solaris" specific process states as well
1383 */
1384 static int ps_read_process(long pid, procstat_t *ps, char *state)
1385 {
1386 char filename[64];
1387 char f_psinfo[64], f_usage[64];
1388 char *buffer;
1390 pstatus_t *myStatus;
1391 psinfo_t *myInfo;
1392 prusage_t *myUsage;
1394 snprintf(filename, sizeof (filename), "/proc/%li/status", pid);
1395 snprintf(f_psinfo, sizeof (f_psinfo), "/proc/%li/psinfo", pid);
1396 snprintf(f_usage, sizeof (f_usage), "/proc/%li/usage", pid);
1399 buffer = malloc(sizeof (pstatus_t));
1400 memset(buffer, 0, sizeof (pstatus_t));
1401 read_file_contents(filename, buffer, sizeof (pstatus_t));
1402 myStatus = (pstatus_t *) buffer;
1404 buffer = malloc(sizeof (psinfo_t));
1405 memset(buffer, 0, sizeof(psinfo_t));
1406 read_file_contents(f_psinfo, buffer, sizeof (psinfo_t));
1407 myInfo = (psinfo_t *) buffer;
1409 buffer = malloc(sizeof (prusage_t));
1410 memset(buffer, 0, sizeof(prusage_t));
1411 read_file_contents(f_usage, buffer, sizeof (prusage_t));
1412 myUsage = (prusage_t *) buffer;
1414 sstrncpy(ps->name, myInfo->pr_fname, sizeof (myInfo->pr_fname));
1415 ps->num_lwp = myStatus->pr_nlwp;
1416 if (myInfo->pr_wstat != 0) {
1417 ps->num_proc = 0;
1418 ps->num_lwp = 0;
1419 *state = (char) 'Z';
1420 return (0);
1421 } else {
1422 ps->num_proc = 1;
1423 ps->num_lwp = myInfo->pr_nlwp;
1424 }
1426 /*
1427 * Convert system time and user time from nanoseconds to microseconds
1428 * for compatibility with the linux module
1429 */
1430 ps->cpu_system_counter = myStatus -> pr_stime.tv_nsec / 1000;
1431 ps->cpu_user_counter = myStatus -> pr_utime.tv_nsec / 1000;
1433 /*
1434 * Convert rssize from KB to bytes to be consistent w/ the linux module
1435 */
1436 ps->vmem_rss = myInfo->pr_rssize * 1024;
1437 ps->vmem_size = myInfo->pr_size * 1024;
1438 ps->vmem_minflt_counter = myUsage->pr_minf;
1439 ps->vmem_majflt_counter = myUsage->pr_majf;
1441 /*
1442 * TODO: Data and code segment calculations for Solaris
1443 */
1445 ps->vmem_data = -1;
1446 ps->vmem_code = -1;
1447 ps->stack_size = myStatus->pr_stksize;
1449 /*
1450 * Calculating input/ouput chars
1451 * Formula used is total chars / total blocks => chars/block
1452 * then convert input/output blocks to chars
1453 */
1454 ulong_t tot_chars = myUsage->pr_ioch;
1455 ulong_t tot_blocks = myUsage->pr_inblk + myUsage->pr_oublk;
1456 ulong_t chars_per_block = 1;
1457 if (tot_blocks != 0)
1458 chars_per_block = tot_chars / tot_blocks;
1459 ps->io_rchar = myUsage->pr_inblk * chars_per_block;
1460 ps->io_wchar = myUsage->pr_oublk * chars_per_block;
1461 ps->io_syscr = myUsage->pr_sysc;
1462 ps->io_syscw = myUsage->pr_sysc;
1465 /*
1466 * TODO: Find way of setting BLOCKED and PAGING status
1467 */
1469 *state = (char) 'R';
1470 if (myStatus->pr_flags & PR_ASLEEP)
1471 *state = (char) 'S';
1472 else if (myStatus->pr_flags & PR_STOPPED)
1473 *state = (char) 'T';
1474 else if (myStatus->pr_flags & PR_DETACH)
1475 *state = (char) 'E';
1476 else if (myStatus->pr_flags & PR_DAEMON)
1477 *state = (char) 'A';
1478 else if (myStatus->pr_flags & PR_ISSYS)
1479 *state = (char) 'Y';
1480 else if (myStatus->pr_flags & PR_ORPHAN)
1481 *state = (char) 'O';
1483 sfree(myStatus);
1484 sfree(myInfo);
1485 sfree(myUsage);
1487 return (0);
1488 }
1490 /*
1491 * Reads the number of threads created since the last reboot. On Solaris these
1492 * are retrieved from kstat (module cpu, name sys, class misc, stat nthreads).
1493 * The result is the sum for all the threads created on each cpu
1494 */
1495 static int read_fork_rate()
1496 {
1497 extern kstat_ctl_t *kc;
1498 kstat_t *ksp_chain = NULL;
1499 derive_t result = 0;
1501 if (kc == NULL)
1502 return (-1);
1504 for (ksp_chain = kc->kc_chain;
1505 ksp_chain != NULL;
1506 ksp_chain = ksp_chain->ks_next)
1507 {
1508 if ((strcmp (ksp_chain->ks_module, "cpu") == 0)
1509 && (strcmp (ksp_chain->ks_name, "sys") == 0)
1510 && (strcmp (ksp_chain->ks_class, "misc") == 0))
1511 {
1512 long long tmp;
1514 kstat_read (kc, ksp_chain, NULL);
1516 tmp = get_kstat_value(ksp_chain, "nthreads");
1517 if (tmp != -1LL)
1518 result += tmp;
1519 }
1520 }
1522 ps_submit_fork_rate (result);
1523 return (0);
1524 }
1525 #endif /* KERNEL_SOLARIS */
1527 #if HAVE_THREAD_INFO
1528 static int mach_get_task_name (task_t t, int *pid, char *name, size_t name_max_len)
1529 {
1530 int mib[4];
1532 struct kinfo_proc kp;
1533 size_t kp_size;
1535 mib[0] = CTL_KERN;
1536 mib[1] = KERN_PROC;
1537 mib[2] = KERN_PROC_PID;
1539 if (pid_for_task (t, pid) != KERN_SUCCESS)
1540 return (-1);
1541 mib[3] = *pid;
1543 kp_size = sizeof (kp);
1544 if (sysctl (mib, 4, &kp, &kp_size, NULL, 0) != 0)
1545 return (-1);
1547 if (name_max_len > (MAXCOMLEN + 1))
1548 name_max_len = MAXCOMLEN + 1;
1550 strncpy (name, kp.kp_proc.p_comm, name_max_len - 1);
1551 name[name_max_len - 1] = '\0';
1553 DEBUG ("pid = %i; name = %s;", *pid, name);
1555 /* We don't do the special handling for `p_comm == "LaunchCFMApp"' as
1556 * `top' does it, because it is a lot of work and only used when
1557 * debugging. -octo */
1559 return (0);
1560 }
1561 #endif /* HAVE_THREAD_INFO */
1562 /* ------- end of additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
1564 /* do actual readings from kernel */
1565 static int ps_read (void)
1566 {
1567 #if HAVE_THREAD_INFO
1568 kern_return_t status;
1570 int pset;
1571 processor_set_t port_pset_priv;
1573 int task;
1574 task_array_t task_list;
1575 mach_msg_type_number_t task_list_len;
1577 int task_pid;
1578 char task_name[MAXCOMLEN + 1];
1580 int thread;
1581 thread_act_array_t thread_list;
1582 mach_msg_type_number_t thread_list_len;
1583 thread_basic_info_data_t thread_data;
1584 mach_msg_type_number_t thread_data_len;
1586 int running = 0;
1587 int sleeping = 0;
1588 int zombies = 0;
1589 int stopped = 0;
1590 int blocked = 0;
1592 procstat_t *ps;
1593 procstat_entry_t pse;
1595 ps_list_reset ();
1597 /*
1598 * The Mach-concept is a little different from the traditional UNIX
1599 * concept: All the work is done in threads. Threads are contained in
1600 * `tasks'. Therefore, `task status' doesn't make much sense, since
1601 * it's actually a `thread status'.
1602 * Tasks are assigned to sets of processors, so that's where you go to
1603 * get a list.
1604 */
1605 for (pset = 0; pset < pset_list_len; pset++)
1606 {
1607 if ((status = host_processor_set_priv (port_host_self,
1608 pset_list[pset],
1609 &port_pset_priv)) != KERN_SUCCESS)
1610 {
1611 ERROR ("host_processor_set_priv failed: %s\n",
1612 mach_error_string (status));
1613 continue;
1614 }
1616 if ((status = processor_set_tasks (port_pset_priv,
1617 &task_list,
1618 &task_list_len)) != KERN_SUCCESS)
1619 {
1620 ERROR ("processor_set_tasks failed: %s\n",
1621 mach_error_string (status));
1622 mach_port_deallocate (port_task_self, port_pset_priv);
1623 continue;
1624 }
1626 for (task = 0; task < task_list_len; task++)
1627 {
1628 ps = NULL;
1629 if (mach_get_task_name (task_list[task],
1630 &task_pid,
1631 task_name, PROCSTAT_NAME_LEN) == 0)
1632 {
1633 /* search for at least one match */
1634 for (ps = list_head_g; ps != NULL; ps = ps->next)
1635 /* FIXME: cmdline should be here instead of NULL */
1636 if (ps_list_match (task_name, NULL, ps) == 1)
1637 break;
1638 }
1640 /* Collect more detailed statistics for this process */
1641 if (ps != NULL)
1642 {
1643 task_basic_info_data_t task_basic_info;
1644 mach_msg_type_number_t task_basic_info_len;
1645 task_events_info_data_t task_events_info;
1646 mach_msg_type_number_t task_events_info_len;
1647 task_absolutetime_info_data_t task_absolutetime_info;
1648 mach_msg_type_number_t task_absolutetime_info_len;
1650 memset (&pse, '\0', sizeof (pse));
1651 pse.id = task_pid;
1653 task_basic_info_len = TASK_BASIC_INFO_COUNT;
1654 status = task_info (task_list[task],
1655 TASK_BASIC_INFO,
1656 (task_info_t) &task_basic_info,
1657 &task_basic_info_len);
1658 if (status != KERN_SUCCESS)
1659 {
1660 ERROR ("task_info failed: %s",
1661 mach_error_string (status));
1662 continue; /* with next thread_list */
1663 }
1665 task_events_info_len = TASK_EVENTS_INFO_COUNT;
1666 status = task_info (task_list[task],
1667 TASK_EVENTS_INFO,
1668 (task_info_t) &task_events_info,
1669 &task_events_info_len);
1670 if (status != KERN_SUCCESS)
1671 {
1672 ERROR ("task_info failed: %s",
1673 mach_error_string (status));
1674 continue; /* with next thread_list */
1675 }
1677 task_absolutetime_info_len = TASK_ABSOLUTETIME_INFO_COUNT;
1678 status = task_info (task_list[task],
1679 TASK_ABSOLUTETIME_INFO,
1680 (task_info_t) &task_absolutetime_info,
1681 &task_absolutetime_info_len);
1682 if (status != KERN_SUCCESS)
1683 {
1684 ERROR ("task_info failed: %s",
1685 mach_error_string (status));
1686 continue; /* with next thread_list */
1687 }
1689 pse.num_proc++;
1690 pse.vmem_size = task_basic_info.virtual_size;
1691 pse.vmem_rss = task_basic_info.resident_size;
1692 /* Does not seem to be easily exposed */
1693 pse.vmem_data = 0;
1694 pse.vmem_code = 0;
1696 pse.vmem_minflt_counter = task_events_info.cow_faults;
1697 pse.vmem_majflt_counter = task_events_info.faults;
1699 pse.cpu_user_counter = task_absolutetime_info.total_user;
1700 pse.cpu_system_counter = task_absolutetime_info.total_system;
1701 }
1703 status = task_threads (task_list[task], &thread_list,
1704 &thread_list_len);
1705 if (status != KERN_SUCCESS)
1706 {
1707 /* Apple's `top' treats this case a zombie. It
1708 * makes sense to some extend: A `zombie'
1709 * thread is nonsense, since the task/process
1710 * is dead. */
1711 zombies++;
1712 DEBUG ("task_threads failed: %s",
1713 mach_error_string (status));
1714 if (task_list[task] != port_task_self)
1715 mach_port_deallocate (port_task_self,
1716 task_list[task]);
1717 continue; /* with next task_list */
1718 }
1720 for (thread = 0; thread < thread_list_len; thread++)
1721 {
1722 thread_data_len = THREAD_BASIC_INFO_COUNT;
1723 status = thread_info (thread_list[thread],
1724 THREAD_BASIC_INFO,
1725 (thread_info_t) &thread_data,
1726 &thread_data_len);
1727 if (status != KERN_SUCCESS)
1728 {
1729 ERROR ("thread_info failed: %s",
1730 mach_error_string (status));
1731 if (task_list[task] != port_task_self)
1732 mach_port_deallocate (port_task_self,
1733 thread_list[thread]);
1734 continue; /* with next thread_list */
1735 }
1737 if (ps != NULL)
1738 pse.num_lwp++;
1740 switch (thread_data.run_state)
1741 {
1742 case TH_STATE_RUNNING:
1743 running++;
1744 break;
1745 case TH_STATE_STOPPED:
1746 /* What exactly is `halted'? */
1747 case TH_STATE_HALTED:
1748 stopped++;
1749 break;
1750 case TH_STATE_WAITING:
1751 sleeping++;
1752 break;
1753 case TH_STATE_UNINTERRUPTIBLE:
1754 blocked++;
1755 break;
1756 /* There is no `zombie' case here,
1757 * since there are no zombie-threads.
1758 * There's only zombie tasks, which are
1759 * handled above. */
1760 default:
1761 WARNING ("Unknown thread status: %i",
1762 thread_data.run_state);
1763 break;
1764 } /* switch (thread_data.run_state) */
1766 if (task_list[task] != port_task_self)
1767 {
1768 status = mach_port_deallocate (port_task_self,
1769 thread_list[thread]);
1770 if (status != KERN_SUCCESS)
1771 ERROR ("mach_port_deallocate failed: %s",
1772 mach_error_string (status));
1773 }
1774 } /* for (thread_list) */
1776 if ((status = vm_deallocate (port_task_self,
1777 (vm_address_t) thread_list,
1778 thread_list_len * sizeof (thread_act_t)))
1779 != KERN_SUCCESS)
1780 {
1781 ERROR ("vm_deallocate failed: %s",
1782 mach_error_string (status));
1783 }
1784 thread_list = NULL;
1785 thread_list_len = 0;
1787 /* Only deallocate the task port, if it isn't our own.
1788 * Don't know what would happen in that case, but this
1789 * is what Apple's top does.. ;) */
1790 if (task_list[task] != port_task_self)
1791 {
1792 status = mach_port_deallocate (port_task_self,
1793 task_list[task]);
1794 if (status != KERN_SUCCESS)
1795 ERROR ("mach_port_deallocate failed: %s",
1796 mach_error_string (status));
1797 }
1799 if (ps != NULL)
1800 /* FIXME: cmdline should be here instead of NULL */
1801 ps_list_add (task_name, NULL, &pse);
1802 } /* for (task_list) */
1804 if ((status = vm_deallocate (port_task_self,
1805 (vm_address_t) task_list,
1806 task_list_len * sizeof (task_t))) != KERN_SUCCESS)
1807 {
1808 ERROR ("vm_deallocate failed: %s",
1809 mach_error_string (status));
1810 }
1811 task_list = NULL;
1812 task_list_len = 0;
1814 if ((status = mach_port_deallocate (port_task_self, port_pset_priv))
1815 != KERN_SUCCESS)
1816 {
1817 ERROR ("mach_port_deallocate failed: %s",
1818 mach_error_string (status));
1819 }
1820 } /* for (pset_list) */
1822 ps_submit_state ("running", running);
1823 ps_submit_state ("sleeping", sleeping);
1824 ps_submit_state ("zombies", zombies);
1825 ps_submit_state ("stopped", stopped);
1826 ps_submit_state ("blocked", blocked);
1828 for (ps = list_head_g; ps != NULL; ps = ps->next)
1829 ps_submit_proc_list (ps);
1830 /* #endif HAVE_THREAD_INFO */
1832 #elif KERNEL_LINUX
1833 int running = 0;
1834 int sleeping = 0;
1835 int zombies = 0;
1836 int stopped = 0;
1837 int paging = 0;
1838 int blocked = 0;
1840 struct dirent *ent;
1841 DIR *proc;
1842 int pid;
1844 char cmdline[CMDLINE_BUFFER_SIZE];
1846 int status;
1847 procstat_t ps;
1848 procstat_entry_t pse;
1849 char state;
1851 procstat_t *ps_ptr;
1853 running = sleeping = zombies = stopped = paging = blocked = 0;
1854 ps_list_reset ();
1856 if ((proc = opendir ("/proc")) == NULL)
1857 {
1858 char errbuf[1024];
1859 ERROR ("Cannot open `/proc': %s",
1860 sstrerror (errno, errbuf, sizeof (errbuf)));
1861 return (-1);
1862 }
1864 while ((ent = readdir (proc)) != NULL)
1865 {
1866 if (!isdigit (ent->d_name[0]))
1867 continue;
1869 if ((pid = atoi (ent->d_name)) < 1)
1870 continue;
1872 status = ps_read_process (pid, &ps, &state);
1873 if (status != 0)
1874 {
1875 DEBUG ("ps_read_process failed: %i", status);
1876 continue;
1877 }
1879 pse.id = pid;
1880 pse.age = 0;
1882 pse.num_proc = ps.num_proc;
1883 pse.num_lwp = ps.num_lwp;
1884 pse.vmem_size = ps.vmem_size;
1885 pse.vmem_rss = ps.vmem_rss;
1886 pse.vmem_data = ps.vmem_data;
1887 pse.vmem_code = ps.vmem_code;
1888 pse.stack_size = ps.stack_size;
1890 pse.vmem_minflt = 0;
1891 pse.vmem_minflt_counter = ps.vmem_minflt_counter;
1892 pse.vmem_majflt = 0;
1893 pse.vmem_majflt_counter = ps.vmem_majflt_counter;
1895 pse.cpu_user = 0;
1896 pse.cpu_user_counter = ps.cpu_user_counter;
1897 pse.cpu_system = 0;
1898 pse.cpu_system_counter = ps.cpu_system_counter;
1900 pse.io_rchar = ps.io_rchar;
1901 pse.io_wchar = ps.io_wchar;
1902 pse.io_syscr = ps.io_syscr;
1903 pse.io_syscw = ps.io_syscw;
1905 pse.cswitch_vol = ps.cswitch_vol;
1906 pse.cswitch_invol = ps.cswitch_invol;
1908 switch (state)
1909 {
1910 case 'R': running++; break;
1911 case 'S': sleeping++; break;
1912 case 'D': blocked++; break;
1913 case 'Z': zombies++; break;
1914 case 'T': stopped++; break;
1915 case 'W': paging++; break;
1916 }
1918 ps_list_add (ps.name,
1919 ps_get_cmdline (pid, ps.name, cmdline, sizeof (cmdline)),
1920 &pse);
1921 }
1923 closedir (proc);
1925 ps_submit_state ("running", running);
1926 ps_submit_state ("sleeping", sleeping);
1927 ps_submit_state ("zombies", zombies);
1928 ps_submit_state ("stopped", stopped);
1929 ps_submit_state ("paging", paging);
1930 ps_submit_state ("blocked", blocked);
1932 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1933 ps_submit_proc_list (ps_ptr);
1935 read_fork_rate();
1936 /* #endif KERNEL_LINUX */
1938 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD
1939 int running = 0;
1940 int sleeping = 0;
1941 int zombies = 0;
1942 int stopped = 0;
1943 int blocked = 0;
1944 int idle = 0;
1945 int wait = 0;
1947 kvm_t *kd;
1948 char errbuf[_POSIX2_LINE_MAX];
1949 struct kinfo_proc *procs; /* array of processes */
1950 struct kinfo_proc *proc_ptr = NULL;
1951 int count; /* returns number of processes */
1952 int i;
1954 procstat_t *ps_ptr;
1955 procstat_entry_t pse;
1957 ps_list_reset ();
1959 /* Open the kvm interface, get a descriptor */
1960 kd = kvm_openfiles (NULL, "/dev/null", NULL, 0, errbuf);
1961 if (kd == NULL)
1962 {
1963 ERROR ("processes plugin: Cannot open kvm interface: %s",
1964 errbuf);
1965 return (0);
1966 }
1968 /* Get the list of processes. */
1969 procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, &count);
1970 if (procs == NULL)
1971 {
1972 ERROR ("processes plugin: Cannot get kvm processes list: %s",
1973 kvm_geterr(kd));
1974 kvm_close (kd);
1975 return (0);
1976 }
1978 /* Iterate through the processes in kinfo_proc */
1979 for (i = 0; i < count; i++)
1980 {
1981 /* Create only one process list entry per _process_, i.e.
1982 * filter out threads (duplicate PID entries). */
1983 if ((proc_ptr == NULL) || (proc_ptr->ki_pid != procs[i].ki_pid))
1984 {
1985 char cmdline[CMDLINE_BUFFER_SIZE] = "";
1986 _Bool have_cmdline = 0;
1988 proc_ptr = &(procs[i]);
1989 /* Don't probe system processes and processes without arguments */
1990 if (((procs[i].ki_flag & P_SYSTEM) == 0)
1991 && (procs[i].ki_args != NULL))
1992 {
1993 char **argv;
1994 int argc;
1995 int status;
1997 /* retrieve the arguments */
1998 argv = kvm_getargv (kd, proc_ptr, /* nchr = */ 0);
1999 argc = 0;
2000 if ((argv != NULL) && (argv[0] != NULL))
2001 {
2002 while (argv[argc] != NULL)
2003 argc++;
2005 status = strjoin (cmdline, sizeof (cmdline), argv, argc, " ");
2006 if (status < 0)
2007 WARNING ("processes plugin: Command line did not fit into buffer.");
2008 else
2009 have_cmdline = 1;
2010 }
2011 } /* if (process has argument list) */
2013 pse.id = procs[i].ki_pid;
2014 pse.age = 0;
2016 pse.num_proc = 1;
2017 pse.num_lwp = procs[i].ki_numthreads;
2019 pse.vmem_size = procs[i].ki_size;
2020 pse.vmem_rss = procs[i].ki_rssize * pagesize;
2021 pse.vmem_data = procs[i].ki_dsize * pagesize;
2022 pse.vmem_code = procs[i].ki_tsize * pagesize;
2023 pse.stack_size = procs[i].ki_ssize * pagesize;
2024 pse.vmem_minflt = 0;
2025 pse.vmem_minflt_counter = procs[i].ki_rusage.ru_minflt;
2026 pse.vmem_majflt = 0;
2027 pse.vmem_majflt_counter = procs[i].ki_rusage.ru_majflt;
2029 pse.cpu_user = 0;
2030 pse.cpu_system = 0;
2031 pse.cpu_user_counter = 0;
2032 pse.cpu_system_counter = 0;
2033 /*
2034 * The u-area might be swapped out, and we can't get
2035 * at it because we have a crashdump and no swap.
2036 * If it's here fill in these fields, otherwise, just
2037 * leave them 0.
2038 */
2039 if (procs[i].ki_flag & P_INMEM)
2040 {
2041 pse.cpu_user_counter = procs[i].ki_rusage.ru_utime.tv_usec
2042 + (1000000lu * procs[i].ki_rusage.ru_utime.tv_sec);
2043 pse.cpu_system_counter = procs[i].ki_rusage.ru_stime.tv_usec
2044 + (1000000lu * procs[i].ki_rusage.ru_stime.tv_sec);
2045 }
2047 /* no I/O data */
2048 pse.io_rchar = -1;
2049 pse.io_wchar = -1;
2050 pse.io_syscr = -1;
2051 pse.io_syscw = -1;
2053 ps_list_add (procs[i].ki_comm, have_cmdline ? cmdline : NULL, &pse);
2054 } /* if ((proc_ptr == NULL) || (proc_ptr->ki_pid != procs[i].ki_pid)) */
2056 switch (procs[i].ki_stat)
2057 {
2058 case SSTOP: stopped++; break;
2059 case SSLEEP: sleeping++; break;
2060 case SRUN: running++; break;
2061 case SIDL: idle++; break;
2062 case SWAIT: wait++; break;
2063 case SLOCK: blocked++; break;
2064 case SZOMB: zombies++; break;
2065 }
2066 }
2068 kvm_close(kd);
2070 ps_submit_state ("running", running);
2071 ps_submit_state ("sleeping", sleeping);
2072 ps_submit_state ("zombies", zombies);
2073 ps_submit_state ("stopped", stopped);
2074 ps_submit_state ("blocked", blocked);
2075 ps_submit_state ("idle", idle);
2076 ps_submit_state ("wait", wait);
2078 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
2079 ps_submit_proc_list (ps_ptr);
2080 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
2082 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_OPENBSD
2083 int running = 0;
2084 int sleeping = 0;
2085 int zombies = 0;
2086 int stopped = 0;
2087 int onproc = 0;
2088 int idle = 0;
2089 int dead = 0;
2091 kvm_t *kd;
2092 char errbuf[1024];
2093 struct kinfo_proc *procs; /* array of processes */
2094 struct kinfo_proc *proc_ptr = NULL;
2095 int count; /* returns number of processes */
2096 int i;
2098 procstat_t *ps_ptr;
2099 procstat_entry_t pse;
2101 ps_list_reset ();
2103 /* Open the kvm interface, get a descriptor */
2104 kd = kvm_open (NULL, NULL, NULL, 0, errbuf);
2105 if (kd == NULL)
2106 {
2107 ERROR ("processes plugin: Cannot open kvm interface: %s",
2108 errbuf);
2109 return (0);
2110 }
2112 /* Get the list of processes. */
2113 procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc), &count);
2114 if (procs == NULL)
2115 {
2116 ERROR ("processes plugin: Cannot get kvm processes list: %s",
2117 kvm_geterr(kd));
2118 kvm_close (kd);
2119 return (0);
2120 }
2122 /* Iterate through the processes in kinfo_proc */
2123 for (i = 0; i < count; i++)
2124 {
2125 /* Create only one process list entry per _process_, i.e.
2126 * filter out threads (duplicate PID entries). */
2127 if ((proc_ptr == NULL) || (proc_ptr->p_pid != procs[i].p_pid))
2128 {
2129 char cmdline[CMDLINE_BUFFER_SIZE] = "";
2130 _Bool have_cmdline = 0;
2132 proc_ptr = &(procs[i]);
2133 /* Don't probe zombie processes */
2134 if (!P_ZOMBIE(proc_ptr))
2135 {
2136 char **argv;
2137 int argc;
2138 int status;
2140 /* retrieve the arguments */
2141 argv = kvm_getargv (kd, proc_ptr, /* nchr = */ 0);
2142 argc = 0;
2143 if ((argv != NULL) && (argv[0] != NULL))
2144 {
2145 while (argv[argc] != NULL)
2146 argc++;
2148 status = strjoin (cmdline, sizeof (cmdline), argv, argc, " ");
2149 if (status < 0)
2150 WARNING ("processes plugin: Command line did not fit into buffer.");
2151 else
2152 have_cmdline = 1;
2153 }
2154 } /* if (process has argument list) */
2156 pse.id = procs[i].p_pid;
2157 pse.age = 0;
2159 pse.num_proc = 1;
2160 pse.num_lwp = 1; /* XXX: accumulate p_tid values for a single p_pid ? */
2162 pse.vmem_rss = procs[i].p_vm_rssize * pagesize;
2163 pse.vmem_data = procs[i].p_vm_dsize * pagesize;
2164 pse.vmem_code = procs[i].p_vm_tsize * pagesize;
2165 pse.stack_size = procs[i].p_vm_ssize * pagesize;
2166 pse.vmem_size = pse.stack_size + pse.vmem_code + pse.vmem_data;
2167 pse.vmem_minflt = 0;
2168 pse.vmem_minflt_counter = procs[i].p_uru_minflt;
2169 pse.vmem_majflt = 0;
2170 pse.vmem_majflt_counter = procs[i].p_uru_majflt;
2172 pse.cpu_user = 0;
2173 pse.cpu_system = 0;
2174 pse.cpu_user_counter = procs[i].p_uutime_usec +
2175 (1000000lu * procs[i].p_uutime_sec);
2176 pse.cpu_system_counter = procs[i].p_ustime_usec +
2177 (1000000lu * procs[i].p_ustime_sec);
2179 /* no I/O data */
2180 pse.io_rchar = -1;
2181 pse.io_wchar = -1;
2182 pse.io_syscr = -1;
2183 pse.io_syscw = -1;
2185 ps_list_add (procs[i].p_comm, have_cmdline ? cmdline : NULL, &pse);
2186 } /* if ((proc_ptr == NULL) || (proc_ptr->p_pid != procs[i].p_pid)) */
2188 switch (procs[i].p_stat)
2189 {
2190 case SSTOP: stopped++; break;
2191 case SSLEEP: sleeping++; break;
2192 case SRUN: running++; break;
2193 case SIDL: idle++; break;
2194 case SONPROC: onproc++; break;
2195 case SDEAD: dead++; break;
2196 case SZOMB: zombies++; break;
2197 }
2198 }
2200 kvm_close(kd);
2202 ps_submit_state ("running", running);
2203 ps_submit_state ("sleeping", sleeping);
2204 ps_submit_state ("zombies", zombies);
2205 ps_submit_state ("stopped", stopped);
2206 ps_submit_state ("onproc", onproc);
2207 ps_submit_state ("idle", idle);
2208 ps_submit_state ("dead", dead);
2210 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
2211 ps_submit_proc_list (ps_ptr);
2212 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_OPENBSD */
2214 #elif HAVE_PROCINFO_H
2215 /* AIX */
2216 int running = 0;
2217 int sleeping = 0;
2218 int zombies = 0;
2219 int stopped = 0;
2220 int paging = 0;
2221 int blocked = 0;
2223 pid_t pindex = 0;
2224 int nprocs;
2226 procstat_t *ps;
2227 procstat_entry_t pse;
2229 ps_list_reset ();
2230 while ((nprocs = getprocs64 (procentry, sizeof(struct procentry64),
2231 /* fdsinfo = */ NULL, sizeof(struct fdsinfo64),
2232 &pindex, MAXPROCENTRY)) > 0)
2233 {
2234 int i;
2236 for (i = 0; i < nprocs; i++)
2237 {
2238 tid64_t thindex;
2239 int nthreads;
2240 char arglist[MAXARGLN+1];
2241 char *cargs;
2242 char *cmdline;
2244 if (procentry[i].pi_state == SNONE) continue;
2245 /* if (procentry[i].pi_state == SZOMB) FIXME */
2247 cmdline = procentry[i].pi_comm;
2248 cargs = procentry[i].pi_comm;
2249 if ( procentry[i].pi_flags & SKPROC )
2250 {
2251 if (procentry[i].pi_pid == 0)
2252 cmdline = "swapper";
2253 cargs = cmdline;
2254 }
2255 else
2256 {
2257 if (getargs(&procentry[i], sizeof(struct procentry64), arglist, MAXARGLN) >= 0)
2258 {
2259 int n;
2261 n = -1;
2262 while (++n < MAXARGLN)
2263 {
2264 if (arglist[n] == '\0')
2265 {
2266 if (arglist[n+1] == '\0')
2267 break;
2268 arglist[n] = ' ';
2269 }
2270 }
2271 cargs = arglist;
2272 }
2273 }
2275 pse.id = procentry[i].pi_pid;
2276 pse.age = 0;
2277 pse.num_lwp = procentry[i].pi_thcount;
2278 pse.num_proc = 1;
2280 thindex=0;
2281 while ((nthreads = getthrds64(procentry[i].pi_pid,
2282 thrdentry, sizeof(struct thrdentry64),
2283 &thindex, MAXTHRDENTRY)) > 0)
2284 {
2285 int j;
2287 for (j=0; j< nthreads; j++)
2288 {
2289 switch (thrdentry[j].ti_state)
2290 {
2291 /* case TSNONE: break; */
2292 case TSIDL: blocked++; break; /* FIXME is really blocked */
2293 case TSRUN: running++; break;
2294 case TSSLEEP: sleeping++; break;
2295 case TSSWAP: paging++; break;
2296 case TSSTOP: stopped++; break;
2297 case TSZOMB: zombies++; break;
2298 }
2299 }
2300 if (nthreads < MAXTHRDENTRY)
2301 break;
2302 }
2304 pse.cpu_user = 0;
2305 /* tv_usec is nanosec ??? */
2306 pse.cpu_user_counter = procentry[i].pi_ru.ru_utime.tv_sec * 1000000 +
2307 procentry[i].pi_ru.ru_utime.tv_usec / 1000;
2309 pse.cpu_system = 0;
2310 /* tv_usec is nanosec ??? */
2311 pse.cpu_system_counter = procentry[i].pi_ru.ru_stime.tv_sec * 1000000 +
2312 procentry[i].pi_ru.ru_stime.tv_usec / 1000;
2314 pse.vmem_minflt = 0;
2315 pse.vmem_minflt_counter = procentry[i].pi_minflt;
2316 pse.vmem_majflt = 0;
2317 pse.vmem_majflt_counter = procentry[i].pi_majflt;
2319 pse.vmem_size = procentry[i].pi_tsize + procentry[i].pi_dvm * pagesize;
2320 pse.vmem_rss = (procentry[i].pi_drss + procentry[i].pi_trss) * pagesize;
2321 /* Not supported */
2322 pse.vmem_data = 0;
2323 pse.vmem_code = 0;
2324 pse.stack_size = 0;
2326 pse.io_rchar = -1;
2327 pse.io_wchar = -1;
2328 pse.io_syscr = -1;
2329 pse.io_syscw = -1;
2331 ps_list_add (cmdline, cargs, &pse);
2332 } /* for (i = 0 .. nprocs) */
2334 if (nprocs < MAXPROCENTRY)
2335 break;
2336 } /* while (getprocs64() > 0) */
2337 ps_submit_state ("running", running);
2338 ps_submit_state ("sleeping", sleeping);
2339 ps_submit_state ("zombies", zombies);
2340 ps_submit_state ("stopped", stopped);
2341 ps_submit_state ("paging", paging);
2342 ps_submit_state ("blocked", blocked);
2344 for (ps = list_head_g; ps != NULL; ps = ps->next)
2345 ps_submit_proc_list (ps);
2346 /* #endif HAVE_PROCINFO_H */
2348 #elif KERNEL_SOLARIS
2349 /*
2350 * The Solaris section adds a few more process states and removes some
2351 * process states compared to linux. Most notably there is no "PAGING"
2352 * and "BLOCKED" state for a process. The rest is similar to the linux
2353 * code.
2354 */
2355 int running = 0;
2356 int sleeping = 0;
2357 int zombies = 0;
2358 int stopped = 0;
2359 int detached = 0;
2360 int daemon = 0;
2361 int system = 0;
2362 int orphan = 0;
2364 struct dirent *ent;
2365 DIR *proc;
2367 int status;
2368 procstat_t *ps_ptr;
2369 char state;
2371 char cmdline[PRARGSZ];
2373 ps_list_reset ();
2375 proc = opendir ("/proc");
2376 if (proc == NULL)
2377 return (-1);
2379 while ((ent = readdir(proc)) != NULL)
2380 {
2381 long pid;
2382 struct procstat ps;
2383 procstat_entry_t pse;
2384 char *endptr;
2386 if (!isdigit ((int) ent->d_name[0]))
2387 continue;
2389 pid = strtol (ent->d_name, &endptr, 10);
2390 if (*endptr != 0) /* value didn't completely parse as a number */
2391 continue;
2393 status = ps_read_process (pid, &ps, &state);
2394 if (status != 0)
2395 {
2396 DEBUG("ps_read_process failed: %i", status);
2397 continue;
2398 }
2400 pse.id = pid;
2401 pse.age = 0;
2403 pse.num_proc = ps.num_proc;
2404 pse.num_lwp = ps.num_lwp;
2405 pse.vmem_size = ps.vmem_size;
2406 pse.vmem_rss = ps.vmem_rss;
2407 pse.vmem_data = ps.vmem_data;
2408 pse.vmem_code = ps.vmem_code;
2409 pse.stack_size = ps.stack_size;
2411 pse.vmem_minflt = 0;
2412 pse.vmem_minflt_counter = ps.vmem_minflt_counter;
2413 pse.vmem_majflt = 0;
2414 pse.vmem_majflt_counter = ps.vmem_majflt_counter;
2416 pse.cpu_user = 0;
2417 pse.cpu_user_counter = ps.cpu_user_counter;
2418 pse.cpu_system = 0;
2419 pse.cpu_system_counter = ps.cpu_system_counter;
2421 pse.io_rchar = ps.io_rchar;
2422 pse.io_wchar = ps.io_wchar;
2423 pse.io_syscr = ps.io_syscr;
2424 pse.io_syscw = ps.io_syscw;
2426 switch (state)
2427 {
2428 case 'R': running++; break;
2429 case 'S': sleeping++; break;
2430 case 'E': detached++; break;
2431 case 'Z': zombies++; break;
2432 case 'T': stopped++; break;
2433 case 'A': daemon++; break;
2434 case 'Y': system++; break;
2435 case 'O': orphan++; break;
2436 }
2439 ps_list_add (ps.name,
2440 ps_get_cmdline (pid, cmdline, sizeof (cmdline)),
2441 &pse);
2442 } /* while(readdir) */
2443 closedir (proc);
2445 ps_submit_state ("running", running);
2446 ps_submit_state ("sleeping", sleeping);
2447 ps_submit_state ("zombies", zombies);
2448 ps_submit_state ("stopped", stopped);
2449 ps_submit_state ("detached", detached);
2450 ps_submit_state ("daemon", daemon);
2451 ps_submit_state ("system", system);
2452 ps_submit_state ("orphan", orphan);
2454 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
2455 ps_submit_proc_list (ps_ptr);
2457 read_fork_rate();
2458 #endif /* KERNEL_SOLARIS */
2460 return (0);
2461 } /* int ps_read */
2463 void module_register (void)
2464 {
2465 plugin_register_complex_config ("processes", ps_config);
2466 plugin_register_init ("processes", ps_init);
2467 plugin_register_read ("processes", ps_read);
2468 } /* void module_register */