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 * Pavel Rochnyack <pavel2000 at ngs.ru>
36 * Wilfried Goesgens <dothebart at citadel.org>
37 **/
39 #include "collectd.h"
41 #include "common.h"
42 #include "plugin.h"
44 /* Include header files for the mach system, if they exist.. */
45 #if HAVE_THREAD_INFO
46 #if HAVE_MACH_MACH_INIT_H
47 #include <mach/mach_init.h>
48 #endif
49 #if HAVE_MACH_HOST_PRIV_H
50 #include <mach/host_priv.h>
51 #endif
52 #if HAVE_MACH_MACH_ERROR_H
53 #include <mach/mach_error.h>
54 #endif
55 #if HAVE_MACH_MACH_HOST_H
56 #include <mach/mach_host.h>
57 #endif
58 #if HAVE_MACH_MACH_PORT_H
59 #include <mach/mach_port.h>
60 #endif
61 #if HAVE_MACH_MACH_TYPES_H
62 #include <mach/mach_types.h>
63 #endif
64 #if HAVE_MACH_MESSAGE_H
65 #include <mach/message.h>
66 #endif
67 #if HAVE_MACH_PROCESSOR_SET_H
68 #include <mach/processor_set.h>
69 #endif
70 #if HAVE_MACH_TASK_H
71 #include <mach/task.h>
72 #endif
73 #if HAVE_MACH_THREAD_ACT_H
74 #include <mach/thread_act.h>
75 #endif
76 #if HAVE_MACH_VM_REGION_H
77 #include <mach/vm_region.h>
78 #endif
79 #if HAVE_MACH_VM_MAP_H
80 #include <mach/vm_map.h>
81 #endif
82 #if HAVE_MACH_VM_PROT_H
83 #include <mach/vm_prot.h>
84 #endif
85 #if HAVE_SYS_SYSCTL_H
86 #include <sys/sysctl.h>
87 #endif
88 /* #endif HAVE_THREAD_INFO */
90 #elif KERNEL_LINUX
91 #if HAVE_LINUX_CONFIG_H
92 #include <linux/config.h>
93 #endif
94 #ifndef CONFIG_HZ
95 #define CONFIG_HZ 100
96 #endif
97 /* #endif KERNEL_LINUX */
99 #elif HAVE_LIBKVM_GETPROCS && \
100 (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD)
101 #include <kvm.h>
102 #include <sys/param.h>
103 #include <sys/proc.h>
104 #include <sys/sysctl.h>
105 #include <sys/user.h>
106 /* #endif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD ||
107 * HAVE_STRUCT_KINFO_PROC_OPENBSD) */
109 #elif HAVE_PROCINFO_H
110 #include <procinfo.h>
111 #include <sys/types.h>
113 #define MAXPROCENTRY 32
114 #define MAXTHRDENTRY 16
115 #define MAXARGLN 1024
116 /* #endif HAVE_PROCINFO_H */
118 #elif KERNEL_SOLARIS
119 /* Hack: Avoid #error when building a 32-bit binary with
120 * _FILE_OFFSET_BITS=64. There is a reason for this #error, as one
121 * of the structures in <sys/procfs.h> uses an off_t, but that
122 * isn't relevant to our usage of procfs. */
123 #if !defined(_LP64) && _FILE_OFFSET_BITS == 64
124 #define SAVE_FOB_64
125 #undef _FILE_OFFSET_BITS
126 #endif
128 #include <procfs.h>
130 #ifdef SAVE_FOB_64
131 #define _FILE_OFFSET_BITS 64
132 #undef SAVE_FOB_64
133 #endif
135 #include <dirent.h>
136 #include <sys/user.h>
138 #ifndef MAXCOMLEN
139 #define MAXCOMLEN 16
140 #endif
142 /* #endif KERNEL_SOLARIS */
144 #else
145 #error "No applicable input method."
146 #endif
148 #if HAVE_REGEX_H
149 #include <regex.h>
150 #endif
152 #if HAVE_KSTAT_H
153 #include <kstat.h>
154 #endif
156 #ifndef CMDLINE_BUFFER_SIZE
157 #if defined(ARG_MAX) && (ARG_MAX < 4096)
158 #define CMDLINE_BUFFER_SIZE ARG_MAX
159 #else
160 #define CMDLINE_BUFFER_SIZE 4096
161 #endif
162 #endif
164 #define PROCSTAT_NAME_LEN 256
165 typedef struct process_entry_s {
166 unsigned long id;
167 char name[PROCSTAT_NAME_LEN];
169 unsigned long num_proc;
170 unsigned long num_lwp;
171 unsigned long num_fd;
172 unsigned long num_maps;
173 unsigned long vmem_size;
174 unsigned long vmem_rss;
175 unsigned long vmem_data;
176 unsigned long vmem_code;
177 unsigned long stack_size;
179 derive_t vmem_minflt_counter;
180 derive_t vmem_majflt_counter;
182 derive_t cpu_user_counter;
183 derive_t cpu_system_counter;
185 /* io data */
186 derive_t io_rchar;
187 derive_t io_wchar;
188 derive_t io_syscr;
189 derive_t io_syscw;
190 derive_t io_diskr;
191 derive_t io_diskw;
192 _Bool has_io;
194 derive_t cswitch_vol;
195 derive_t cswitch_invol;
196 _Bool has_cswitch;
198 _Bool has_fd;
200 _Bool has_maps;
201 } process_entry_t;
203 typedef struct procstat_entry_s {
204 unsigned long id;
205 unsigned long age;
207 derive_t vmem_minflt_counter;
208 derive_t vmem_majflt_counter;
210 derive_t cpu_user_counter;
211 derive_t cpu_system_counter;
213 /* io data */
214 derive_t io_rchar;
215 derive_t io_wchar;
216 derive_t io_syscr;
217 derive_t io_syscw;
218 derive_t io_diskr;
219 derive_t io_diskw;
221 derive_t cswitch_vol;
222 derive_t cswitch_invol;
224 struct procstat_entry_s *next;
225 } procstat_entry_t;
227 typedef struct procstat {
228 char name[PROCSTAT_NAME_LEN];
229 #if HAVE_REGEX_H
230 regex_t *re;
231 #endif
233 unsigned long num_proc;
234 unsigned long num_lwp;
235 unsigned long num_fd;
236 unsigned long num_maps;
237 unsigned long vmem_size;
238 unsigned long vmem_rss;
239 unsigned long vmem_data;
240 unsigned long vmem_code;
241 unsigned long stack_size;
243 derive_t vmem_minflt_counter;
244 derive_t vmem_majflt_counter;
246 derive_t cpu_user_counter;
247 derive_t cpu_system_counter;
249 /* io data */
250 derive_t io_rchar;
251 derive_t io_wchar;
252 derive_t io_syscr;
253 derive_t io_syscw;
254 derive_t io_diskr;
255 derive_t io_diskw;
257 derive_t cswitch_vol;
258 derive_t cswitch_invol;
260 _Bool report_fd_num;
261 _Bool report_maps_num;
262 _Bool report_ctx_switch;
264 struct procstat *next;
265 struct procstat_entry_s *instances;
266 } procstat_t;
268 static procstat_t *list_head_g = NULL;
270 static _Bool want_init = 1;
271 static _Bool report_ctx_switch = 0;
272 static _Bool report_fd_num = 0;
273 static _Bool report_maps_num = 0;
275 #if HAVE_THREAD_INFO
276 static mach_port_t port_host_self;
277 static mach_port_t port_task_self;
279 static processor_set_name_array_t pset_list;
280 static mach_msg_type_number_t pset_list_len;
281 /* #endif HAVE_THREAD_INFO */
283 #elif KERNEL_LINUX
284 static long pagesize_g;
285 static void ps_fill_details(const procstat_t *ps, process_entry_t *entry);
286 /* #endif KERNEL_LINUX */
288 #elif HAVE_LIBKVM_GETPROCS && \
289 (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD)
290 static int pagesize;
291 /* #endif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD ||
292 * HAVE_STRUCT_KINFO_PROC_OPENBSD) */
294 #elif HAVE_PROCINFO_H
295 static struct procentry64 procentry[MAXPROCENTRY];
296 static struct thrdentry64 thrdentry[MAXTHRDENTRY];
297 static int pagesize;
299 #ifndef _AIXVERSION_610
300 int getprocs64(void *procsinfo, int sizproc, void *fdsinfo, int sizfd,
301 pid_t *index, int count);
302 int getthrds64(pid_t, void *, int, tid64_t *, int);
303 #endif
304 int getargs(void *processBuffer, int bufferLen, char *argsBuffer, int argsLen);
305 #endif /* HAVE_PROCINFO_H */
307 /* put name of process from config to list_head_g tree
308 * list_head_g is a list of 'procstat_t' structs with
309 * processes names we want to watch */
310 static procstat_t *ps_list_register(const char *name, const char *regexp) {
311 procstat_t *new;
312 procstat_t *ptr;
313 int status;
315 new = calloc(1, sizeof(*new));
316 if (new == NULL) {
317 ERROR("processes plugin: ps_list_register: calloc failed.");
318 return NULL;
319 }
320 sstrncpy(new->name, name, sizeof(new->name));
322 new->io_rchar = -1;
323 new->io_wchar = -1;
324 new->io_syscr = -1;
325 new->io_syscw = -1;
326 new->io_diskr = -1;
327 new->io_diskw = -1;
328 new->cswitch_vol = -1;
329 new->cswitch_invol = -1;
331 new->report_fd_num = report_fd_num;
332 new->report_maps_num = report_maps_num;
333 new->report_ctx_switch = report_ctx_switch;
335 #if HAVE_REGEX_H
336 if (regexp != NULL) {
337 DEBUG("ProcessMatch: adding \"%s\" as criteria to process %s.", regexp,
338 name);
339 new->re = malloc(sizeof(*new->re));
340 if (new->re == NULL) {
341 ERROR("processes plugin: ps_list_register: malloc failed.");
342 sfree(new);
343 return NULL;
344 }
346 status = regcomp(new->re, regexp, REG_EXTENDED | REG_NOSUB);
347 if (status != 0) {
348 DEBUG("ProcessMatch: compiling the regular expression \"%s\" failed.",
349 regexp);
350 sfree(new->re);
351 sfree(new);
352 return NULL;
353 }
354 }
355 #else
356 if (regexp != NULL) {
357 ERROR("processes plugin: ps_list_register: "
358 "Regular expression \"%s\" found in config "
359 "file, but support for regular expressions "
360 "has been disabled at compile time.",
361 regexp);
362 sfree(new);
363 return NULL;
364 }
365 #endif
367 for (ptr = list_head_g; ptr != NULL; ptr = ptr->next) {
368 if (strcmp(ptr->name, name) == 0) {
369 WARNING("processes plugin: You have configured more "
370 "than one `Process' or "
371 "`ProcessMatch' with the same name. "
372 "All but the first setting will be "
373 "ignored.");
374 #if HAVE_REGEX_H
375 sfree(new->re);
376 #endif
377 sfree(new);
378 return NULL;
379 }
381 if (ptr->next == NULL)
382 break;
383 }
385 if (ptr == NULL)
386 list_head_g = new;
387 else
388 ptr->next = new;
390 return new;
391 } /* void ps_list_register */
393 /* try to match name against entry, returns 1 if success */
394 static int ps_list_match(const char *name, const char *cmdline,
395 procstat_t *ps) {
396 #if HAVE_REGEX_H
397 if (ps->re != NULL) {
398 int status;
399 const char *str;
401 str = cmdline;
402 if ((str == NULL) || (str[0] == 0))
403 str = name;
405 assert(str != NULL);
407 status = regexec(ps->re, str,
408 /* nmatch = */ 0,
409 /* pmatch = */ NULL,
410 /* eflags = */ 0);
411 if (status == 0)
412 return 1;
413 } else
414 #endif
415 if (strcmp(ps->name, name) == 0)
416 return 1;
418 return 0;
419 } /* int ps_list_match */
421 static void ps_update_counter(derive_t *group_counter, derive_t *curr_counter,
422 derive_t new_counter) {
423 unsigned long curr_value;
425 if (want_init) {
426 *curr_counter = new_counter;
427 return;
428 }
430 if (new_counter < *curr_counter)
431 curr_value = new_counter + (ULONG_MAX - *curr_counter);
432 else
433 curr_value = new_counter - *curr_counter;
435 if (*group_counter == -1)
436 *group_counter = 0;
438 *curr_counter = new_counter;
439 *group_counter += curr_value;
440 }
442 /* add process entry to 'instances' of process 'name' (or refresh it) */
443 static void ps_list_add(const char *name, const char *cmdline,
444 process_entry_t *entry) {
445 procstat_entry_t *pse;
447 if (entry->id == 0)
448 return;
450 for (procstat_t *ps = list_head_g; ps != NULL; ps = ps->next) {
451 if ((ps_list_match(name, cmdline, ps)) == 0)
452 continue;
454 #if KERNEL_LINUX
455 ps_fill_details(ps, entry);
456 #endif
458 for (pse = ps->instances; pse != NULL; pse = pse->next)
459 if ((pse->id == entry->id) || (pse->next == NULL))
460 break;
462 if ((pse == NULL) || (pse->id != entry->id)) {
463 procstat_entry_t *new;
465 new = calloc(1, sizeof(*new));
466 if (new == NULL)
467 return;
468 new->id = entry->id;
470 if (pse == NULL)
471 ps->instances = new;
472 else
473 pse->next = new;
475 pse = new;
476 }
478 pse->age = 0;
480 ps->num_proc += entry->num_proc;
481 ps->num_lwp += entry->num_lwp;
482 ps->num_fd += entry->num_fd;
483 ps->num_maps += entry->num_maps;
484 ps->vmem_size += entry->vmem_size;
485 ps->vmem_rss += entry->vmem_rss;
486 ps->vmem_data += entry->vmem_data;
487 ps->vmem_code += entry->vmem_code;
488 ps->stack_size += entry->stack_size;
490 if ((entry->io_rchar != -1) && (entry->io_wchar != -1)) {
491 ps_update_counter(&ps->io_rchar, &pse->io_rchar, entry->io_rchar);
492 ps_update_counter(&ps->io_wchar, &pse->io_wchar, entry->io_wchar);
493 }
495 if ((entry->io_syscr != -1) && (entry->io_syscw != -1)) {
496 ps_update_counter(&ps->io_syscr, &pse->io_syscr, entry->io_syscr);
497 ps_update_counter(&ps->io_syscw, &pse->io_syscw, entry->io_syscw);
498 }
500 if ((entry->io_diskr != -1) && (entry->io_diskw != -1)) {
501 ps_update_counter(&ps->io_diskr, &pse->io_diskr, entry->io_diskr);
502 ps_update_counter(&ps->io_diskw, &pse->io_diskw, entry->io_diskw);
503 }
505 if ((entry->cswitch_vol != -1) && (entry->cswitch_vol != -1)) {
506 ps_update_counter(&ps->cswitch_vol, &pse->cswitch_vol,
507 entry->cswitch_vol);
508 ps_update_counter(&ps->cswitch_invol, &pse->cswitch_invol,
509 entry->cswitch_invol);
510 }
512 ps_update_counter(&ps->vmem_minflt_counter, &pse->vmem_minflt_counter,
513 entry->vmem_minflt_counter);
514 ps_update_counter(&ps->vmem_majflt_counter, &pse->vmem_majflt_counter,
515 entry->vmem_majflt_counter);
517 ps_update_counter(&ps->cpu_user_counter, &pse->cpu_user_counter,
518 entry->cpu_user_counter);
519 ps_update_counter(&ps->cpu_system_counter, &pse->cpu_system_counter,
520 entry->cpu_system_counter);
521 }
522 }
524 /* remove old entries from instances of processes in list_head_g */
525 static void ps_list_reset(void) {
526 procstat_entry_t *pse;
527 procstat_entry_t *pse_prev;
529 for (procstat_t *ps = list_head_g; ps != NULL; ps = ps->next) {
530 ps->num_proc = 0;
531 ps->num_lwp = 0;
532 ps->num_fd = 0;
533 ps->num_maps = 0;
534 ps->vmem_size = 0;
535 ps->vmem_rss = 0;
536 ps->vmem_data = 0;
537 ps->vmem_code = 0;
538 ps->stack_size = 0;
540 pse_prev = NULL;
541 pse = ps->instances;
542 while (pse != NULL) {
543 if (pse->age > 10) {
544 DEBUG("Removing this procstat entry cause it's too old: "
545 "id = %lu; name = %s;",
546 pse->id, ps->name);
548 if (pse_prev == NULL) {
549 ps->instances = pse->next;
550 free(pse);
551 pse = ps->instances;
552 } else {
553 pse_prev->next = pse->next;
554 free(pse);
555 pse = pse_prev->next;
556 }
557 } else {
558 pse->age++;
559 pse_prev = pse;
560 pse = pse->next;
561 }
562 } /* while (pse != NULL) */
563 } /* for (ps = list_head_g; ps != NULL; ps = ps->next) */
564 }
566 static void ps_tune_instance(oconfig_item_t *ci, procstat_t *ps) {
567 for (int i = 0; i < ci->children_num; i++) {
568 oconfig_item_t *c = ci->children + i;
570 if (strcasecmp(c->key, "CollectContextSwitch") == 0)
571 cf_util_get_boolean(c, &ps->report_ctx_switch);
572 else if (strcasecmp(c->key, "CollectFileDescriptor") == 0)
573 cf_util_get_boolean(c, &ps->report_fd_num);
574 else if (strcasecmp(c->key, "CollectMemoryMaps") == 0)
575 cf_util_get_boolean(c, &ps->report_maps_num);
576 else {
577 ERROR("processes plugin: Option `%s' not allowed here.", c->key);
578 }
579 } /* for (ci->children) */
580 } /* void ps_tune_instance */
582 /* put all pre-defined 'Process' names from config to list_head_g tree */
583 static int ps_config(oconfig_item_t *ci) {
584 #if KERNEL_LINUX
585 const size_t max_procname_len = 15;
586 #elif KERNEL_SOLARIS || KERNEL_FREEBSD
587 const size_t max_procname_len = MAXCOMLEN - 1;
588 #endif
590 procstat_t *ps;
592 for (int i = 0; i < ci->children_num; ++i) {
593 oconfig_item_t *c = ci->children + i;
595 if (strcasecmp(c->key, "Process") == 0) {
596 if ((c->values_num != 1) || (OCONFIG_TYPE_STRING != c->values[0].type)) {
597 ERROR("processes plugin: `Process' expects exactly "
598 "one string argument (got %i).",
599 c->values_num);
600 continue;
601 }
603 #if KERNEL_LINUX || KERNEL_SOLARIS || KERNEL_FREEBSD
604 if (strlen(c->values[0].value.string) > max_procname_len) {
605 WARNING("processes plugin: this platform has a %zu character limit "
606 "to process names. The `Process \"%s\"' option will "
607 "not work as expected.",
608 max_procname_len, c->values[0].value.string);
609 }
610 #endif
612 ps = ps_list_register(c->values[0].value.string, NULL);
614 if (c->children_num != 0 && ps != NULL)
615 ps_tune_instance(c, ps);
616 } else if (strcasecmp(c->key, "ProcessMatch") == 0) {
617 if ((c->values_num != 2) || (OCONFIG_TYPE_STRING != c->values[0].type) ||
618 (OCONFIG_TYPE_STRING != c->values[1].type)) {
619 ERROR("processes plugin: `ProcessMatch' needs exactly "
620 "two string arguments (got %i).",
621 c->values_num);
622 continue;
623 }
625 ps = ps_list_register(c->values[0].value.string,
626 c->values[1].value.string);
628 if (c->children_num != 0 && ps != NULL)
629 ps_tune_instance(c, ps);
630 } else if (strcasecmp(c->key, "CollectContextSwitch") == 0) {
631 cf_util_get_boolean(c, &report_ctx_switch);
632 } else if (strcasecmp(c->key, "CollectFileDescriptor") == 0) {
633 cf_util_get_boolean(c, &report_fd_num);
634 } else if (strcasecmp(c->key, "CollectMemoryMaps") == 0) {
635 cf_util_get_boolean(c, &report_maps_num);
636 } else {
637 ERROR("processes plugin: The `%s' configuration option is not "
638 "understood and will be ignored.",
639 c->key);
640 continue;
641 }
642 }
644 return 0;
645 }
647 static int ps_init(void) {
648 #if HAVE_THREAD_INFO
649 kern_return_t status;
651 port_host_self = mach_host_self();
652 port_task_self = mach_task_self();
654 if (pset_list != NULL) {
655 vm_deallocate(port_task_self, (vm_address_t)pset_list,
656 pset_list_len * sizeof(processor_set_t));
657 pset_list = NULL;
658 pset_list_len = 0;
659 }
661 if ((status = host_processor_sets(port_host_self, &pset_list,
662 &pset_list_len)) != KERN_SUCCESS) {
663 ERROR("host_processor_sets failed: %s\n", mach_error_string(status));
664 pset_list = NULL;
665 pset_list_len = 0;
666 return -1;
667 }
668 /* #endif HAVE_THREAD_INFO */
670 #elif KERNEL_LINUX
671 pagesize_g = sysconf(_SC_PAGESIZE);
672 DEBUG("pagesize_g = %li; CONFIG_HZ = %i;", pagesize_g, CONFIG_HZ);
673 /* #endif KERNEL_LINUX */
675 #elif HAVE_LIBKVM_GETPROCS && \
676 (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD)
677 pagesize = getpagesize();
678 /* #endif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD ||
679 * HAVE_STRUCT_KINFO_PROC_OPENBSD) */
681 #elif HAVE_PROCINFO_H
682 pagesize = getpagesize();
683 #endif /* HAVE_PROCINFO_H */
685 return 0;
686 } /* int ps_init */
688 /* submit global state (e.g.: qty of zombies, running, etc..) */
689 static void ps_submit_state(const char *state, double value) {
690 value_list_t vl = VALUE_LIST_INIT;
692 vl.values = &(value_t){.gauge = value};
693 vl.values_len = 1;
694 sstrncpy(vl.plugin, "processes", sizeof(vl.plugin));
695 sstrncpy(vl.plugin_instance, "", sizeof(vl.plugin_instance));
696 sstrncpy(vl.type, "ps_state", sizeof(vl.type));
697 sstrncpy(vl.type_instance, state, sizeof(vl.type_instance));
699 plugin_dispatch_values(&vl);
700 }
702 /* submit info about specific process (e.g.: memory taken, cpu usage, etc..) */
703 static void ps_submit_proc_list(procstat_t *ps) {
704 value_list_t vl = VALUE_LIST_INIT;
705 value_t values[2];
707 vl.values = values;
708 sstrncpy(vl.plugin, "processes", sizeof(vl.plugin));
709 sstrncpy(vl.plugin_instance, ps->name, sizeof(vl.plugin_instance));
711 sstrncpy(vl.type, "ps_vm", sizeof(vl.type));
712 vl.values[0].gauge = ps->vmem_size;
713 vl.values_len = 1;
714 plugin_dispatch_values(&vl);
716 sstrncpy(vl.type, "ps_rss", sizeof(vl.type));
717 vl.values[0].gauge = ps->vmem_rss;
718 vl.values_len = 1;
719 plugin_dispatch_values(&vl);
721 sstrncpy(vl.type, "ps_data", sizeof(vl.type));
722 vl.values[0].gauge = ps->vmem_data;
723 vl.values_len = 1;
724 plugin_dispatch_values(&vl);
726 sstrncpy(vl.type, "ps_code", sizeof(vl.type));
727 vl.values[0].gauge = ps->vmem_code;
728 vl.values_len = 1;
729 plugin_dispatch_values(&vl);
731 sstrncpy(vl.type, "ps_stacksize", sizeof(vl.type));
732 vl.values[0].gauge = ps->stack_size;
733 vl.values_len = 1;
734 plugin_dispatch_values(&vl);
736 sstrncpy(vl.type, "ps_cputime", sizeof(vl.type));
737 vl.values[0].derive = ps->cpu_user_counter;
738 vl.values[1].derive = ps->cpu_system_counter;
739 vl.values_len = 2;
740 plugin_dispatch_values(&vl);
742 sstrncpy(vl.type, "ps_count", sizeof(vl.type));
743 vl.values[0].gauge = ps->num_proc;
744 vl.values[1].gauge = ps->num_lwp;
745 vl.values_len = 2;
746 plugin_dispatch_values(&vl);
748 sstrncpy(vl.type, "ps_pagefaults", sizeof(vl.type));
749 vl.values[0].derive = ps->vmem_minflt_counter;
750 vl.values[1].derive = ps->vmem_majflt_counter;
751 vl.values_len = 2;
752 plugin_dispatch_values(&vl);
754 if ((ps->io_rchar != -1) && (ps->io_wchar != -1)) {
755 sstrncpy(vl.type, "io_octets", sizeof(vl.type));
756 vl.values[0].derive = ps->io_rchar;
757 vl.values[1].derive = ps->io_wchar;
758 vl.values_len = 2;
759 plugin_dispatch_values(&vl);
760 }
762 if ((ps->io_syscr != -1) && (ps->io_syscw != -1)) {
763 sstrncpy(vl.type, "io_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 ((ps->io_diskr != -1) && (ps->io_diskw != -1)) {
771 sstrncpy(vl.type, "disk_octets", sizeof(vl.type));
772 vl.values[0].derive = ps->io_diskr;
773 vl.values[1].derive = ps->io_diskw;
774 vl.values_len = 2;
775 plugin_dispatch_values(&vl);
776 }
778 if (ps->num_fd > 0) {
779 sstrncpy(vl.type, "file_handles", sizeof(vl.type));
780 vl.values[0].gauge = ps->num_fd;
781 vl.values_len = 1;
782 plugin_dispatch_values(&vl);
783 }
785 if (ps->num_maps > 0) {
786 sstrncpy(vl.type, "file_handles", sizeof(vl.type));
787 sstrncpy(vl.type_instance, "mapped", sizeof(vl.type_instance));
788 vl.values[0].gauge = ps->num_maps;
789 vl.values_len = 1;
790 plugin_dispatch_values(&vl);
791 }
793 if ((ps->cswitch_vol != -1) && (ps->cswitch_invol != -1)) {
794 sstrncpy(vl.type, "contextswitch", sizeof(vl.type));
795 sstrncpy(vl.type_instance, "voluntary", sizeof(vl.type_instance));
796 vl.values[0].derive = ps->cswitch_vol;
797 vl.values_len = 1;
798 plugin_dispatch_values(&vl);
800 sstrncpy(vl.type, "contextswitch", sizeof(vl.type));
801 sstrncpy(vl.type_instance, "involuntary", sizeof(vl.type_instance));
802 vl.values[0].derive = ps->cswitch_invol;
803 vl.values_len = 1;
804 plugin_dispatch_values(&vl);
805 }
807 DEBUG(
808 "name = %s; num_proc = %lu; num_lwp = %lu; num_fd = %lu; num_maps = %lu; "
809 "vmem_size = %lu; vmem_rss = %lu; vmem_data = %lu; "
810 "vmem_code = %lu; "
811 "vmem_minflt_counter = %" PRIi64 "; vmem_majflt_counter = %" PRIi64 "; "
812 "cpu_user_counter = %" PRIi64 "; cpu_system_counter = %" PRIi64 "; "
813 "io_rchar = %" PRIi64 "; io_wchar = %" PRIi64 "; "
814 "io_syscr = %" PRIi64 "; io_syscw = %" PRIi64 "; "
815 "io_diskr = %" PRIi64 "; io_diskw = %" PRIi64 "; "
816 "cswitch_vol = %" PRIi64 "; cswitch_invol = %" PRIi64 ";",
817 ps->name, ps->num_proc, ps->num_lwp, ps->num_fd, ps->num_maps,
818 ps->vmem_size, ps->vmem_rss, ps->vmem_data, ps->vmem_code,
819 ps->vmem_minflt_counter, ps->vmem_majflt_counter, ps->cpu_user_counter,
820 ps->cpu_system_counter, ps->io_rchar, ps->io_wchar, ps->io_syscr,
821 ps->io_syscw, ps->io_diskr, ps->io_diskw, ps->cswitch_vol,
822 ps->cswitch_invol);
824 } /* void ps_submit_proc_list */
826 #if KERNEL_LINUX || KERNEL_SOLARIS
827 static void ps_submit_fork_rate(derive_t value) {
828 value_list_t vl = VALUE_LIST_INIT;
830 vl.values = &(value_t){.derive = value};
831 vl.values_len = 1;
832 sstrncpy(vl.plugin, "processes", sizeof(vl.plugin));
833 sstrncpy(vl.plugin_instance, "", sizeof(vl.plugin_instance));
834 sstrncpy(vl.type, "fork_rate", sizeof(vl.type));
835 sstrncpy(vl.type_instance, "", sizeof(vl.type_instance));
837 plugin_dispatch_values(&vl);
838 }
839 #endif /* KERNEL_LINUX || KERNEL_SOLARIS*/
841 /* ------- additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
842 #if KERNEL_LINUX
843 static int ps_read_tasks_status(process_entry_t *ps) {
844 char dirname[64];
845 DIR *dh;
846 char filename[64];
847 FILE *fh;
848 struct dirent *ent;
849 derive_t cswitch_vol = 0;
850 derive_t cswitch_invol = 0;
851 char buffer[1024];
852 char *fields[8];
853 int numfields;
855 snprintf(dirname, sizeof(dirname), "/proc/%li/task", ps->id);
857 if ((dh = opendir(dirname)) == NULL) {
858 DEBUG("Failed to open directory `%s'", dirname);
859 return -1;
860 }
862 while ((ent = readdir(dh)) != NULL) {
863 char *tpid;
865 if (!isdigit((int)ent->d_name[0]))
866 continue;
868 tpid = ent->d_name;
870 if (snprintf(filename, sizeof(filename), "/proc/%li/task/%s/status", ps->id,
871 tpid) >= sizeof(filename)) {
872 DEBUG("Filename too long: `%s'", filename);
873 continue;
874 }
876 if ((fh = fopen(filename, "r")) == NULL) {
877 DEBUG("Failed to open file `%s'", filename);
878 continue;
879 }
881 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
882 derive_t tmp;
883 char *endptr;
885 if (strncmp(buffer, "voluntary_ctxt_switches", 23) != 0 &&
886 strncmp(buffer, "nonvoluntary_ctxt_switches", 26) != 0)
887 continue;
889 numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
891 if (numfields < 2)
892 continue;
894 errno = 0;
895 endptr = NULL;
896 tmp = (derive_t)strtoll(fields[1], &endptr, /* base = */ 10);
897 if ((errno == 0) && (endptr != fields[1])) {
898 if (strncmp(buffer, "voluntary_ctxt_switches", 23) == 0) {
899 cswitch_vol += tmp;
900 } else if (strncmp(buffer, "nonvoluntary_ctxt_switches", 26) == 0) {
901 cswitch_invol += tmp;
902 }
903 }
904 } /* while (fgets) */
906 if (fclose(fh)) {
907 char errbuf[1024];
908 WARNING("processes: fclose: %s",
909 sstrerror(errno, errbuf, sizeof(errbuf)));
910 }
911 }
912 closedir(dh);
914 ps->cswitch_vol = cswitch_vol;
915 ps->cswitch_invol = cswitch_invol;
917 return 0;
918 } /* int *ps_read_tasks_status */
920 /* Read data from /proc/pid/status */
921 static int ps_read_status(long pid, process_entry_t *ps) {
922 FILE *fh;
923 char buffer[1024];
924 char filename[64];
925 unsigned long lib = 0;
926 unsigned long exe = 0;
927 unsigned long data = 0;
928 unsigned long threads = 0;
929 char *fields[8];
930 int numfields;
932 snprintf(filename, sizeof(filename), "/proc/%li/status", pid);
933 if ((fh = fopen(filename, "r")) == NULL)
934 return -1;
936 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
937 unsigned long tmp;
938 char *endptr;
940 if (strncmp(buffer, "Vm", 2) != 0 && strncmp(buffer, "Threads", 7) != 0)
941 continue;
943 numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
945 if (numfields < 2)
946 continue;
948 errno = 0;
949 endptr = NULL;
950 tmp = strtoul(fields[1], &endptr, /* base = */ 10);
951 if ((errno == 0) && (endptr != fields[1])) {
952 if (strncmp(buffer, "VmData", 6) == 0) {
953 data = tmp;
954 } else if (strncmp(buffer, "VmLib", 5) == 0) {
955 lib = tmp;
956 } else if (strncmp(buffer, "VmExe", 5) == 0) {
957 exe = tmp;
958 } else if (strncmp(buffer, "Threads", 7) == 0) {
959 threads = tmp;
960 }
961 }
962 } /* while (fgets) */
964 if (fclose(fh)) {
965 char errbuf[1024];
966 WARNING("processes: fclose: %s", sstrerror(errno, errbuf, sizeof(errbuf)));
967 }
969 ps->vmem_data = data * 1024;
970 ps->vmem_code = (exe + lib) * 1024;
971 if (threads != 0)
972 ps->num_lwp = threads;
974 return 0;
975 } /* int *ps_read_status */
977 static int ps_read_io(process_entry_t *ps) {
978 FILE *fh;
979 char buffer[1024];
980 char filename[64];
982 char *fields[8];
983 int numfields;
985 snprintf(filename, sizeof(filename), "/proc/%li/io", ps->id);
986 if ((fh = fopen(filename, "r")) == NULL) {
987 DEBUG("ps_read_io: Failed to open file `%s'", filename);
988 return -1;
989 }
991 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
992 derive_t *val = NULL;
993 long long tmp;
994 char *endptr;
996 if (strncasecmp(buffer, "rchar:", 6) == 0)
997 val = &(ps->io_rchar);
998 else if (strncasecmp(buffer, "wchar:", 6) == 0)
999 val = &(ps->io_wchar);
1000 else if (strncasecmp(buffer, "syscr:", 6) == 0)
1001 val = &(ps->io_syscr);
1002 else if (strncasecmp(buffer, "syscw:", 6) == 0)
1003 val = &(ps->io_syscw);
1004 else if (strncasecmp(buffer, "read_bytes:", 11) == 0)
1005 val = &(ps->io_diskr);
1006 else if (strncasecmp(buffer, "write_bytes:", 12) == 0)
1007 val = &(ps->io_diskw);
1008 else
1009 continue;
1011 numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
1013 if (numfields < 2)
1014 continue;
1016 errno = 0;
1017 endptr = NULL;
1018 tmp = strtoll(fields[1], &endptr, /* base = */ 10);
1019 if ((errno != 0) || (endptr == fields[1]))
1020 *val = -1;
1021 else
1022 *val = (derive_t)tmp;
1023 } /* while (fgets) */
1025 if (fclose(fh)) {
1026 char errbuf[1024];
1027 WARNING("processes: fclose: %s", sstrerror(errno, errbuf, sizeof(errbuf)));
1028 }
1029 return 0;
1030 } /* int ps_read_io (...) */
1032 static int ps_count_maps(pid_t pid) {
1033 FILE *fh;
1034 char buffer[1024];
1035 char filename[64];
1036 int count = 0;
1038 snprintf(filename, sizeof(filename), "/proc/%d/maps", pid);
1039 if ((fh = fopen(filename, "r")) == NULL) {
1040 DEBUG("ps_count_maps: Failed to open file `%s'", filename);
1041 return -1;
1042 }
1044 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
1045 if (strchr(buffer, '\n')) {
1046 count++;
1047 }
1048 } /* while (fgets) */
1050 if (fclose(fh)) {
1051 char errbuf[1024];
1052 WARNING("processes: fclose: %s", sstrerror(errno, errbuf, sizeof(errbuf)));
1053 }
1054 return count;
1055 } /* int ps_count_maps (...) */
1057 static int ps_count_fd(int pid) {
1058 char dirname[64];
1059 DIR *dh;
1060 struct dirent *ent;
1061 int count = 0;
1063 snprintf(dirname, sizeof(dirname), "/proc/%i/fd", pid);
1065 if ((dh = opendir(dirname)) == NULL) {
1066 DEBUG("Failed to open directory `%s'", dirname);
1067 return -1;
1068 }
1069 while ((ent = readdir(dh)) != NULL) {
1070 if (!isdigit((int)ent->d_name[0]))
1071 continue;
1072 else
1073 count++;
1074 }
1075 closedir(dh);
1077 return (count >= 1) ? count : 1;
1078 } /* int ps_count_fd (pid) */
1080 static void ps_fill_details(const procstat_t *ps, process_entry_t *entry) {
1081 if (entry->has_io == 0) {
1082 ps_read_io(entry);
1083 entry->has_io = 1;
1084 }
1086 if (ps->report_ctx_switch) {
1087 if (entry->has_cswitch == 0) {
1088 ps_read_tasks_status(entry);
1089 entry->has_cswitch = 1;
1090 }
1091 }
1093 if (ps->report_maps_num) {
1094 int num_maps;
1095 if (entry->has_maps == 0 && (num_maps = ps_count_maps(entry->id)) > 0) {
1096 entry->num_maps = num_maps;
1097 }
1098 entry->has_maps = 1;
1099 }
1101 if (ps->report_fd_num) {
1102 int num_fd;
1103 if (entry->has_fd == 0 && (num_fd = ps_count_fd(entry->id)) > 0) {
1104 entry->num_fd = num_fd;
1105 }
1106 entry->has_fd = 1;
1107 }
1108 } /* void ps_fill_details (...) */
1110 static int ps_read_process(long pid, process_entry_t *ps, char *state) {
1111 char filename[64];
1112 char buffer[1024];
1114 char *fields[64];
1115 char fields_len;
1117 size_t buffer_len;
1119 char *buffer_ptr;
1120 size_t name_start_pos;
1121 size_t name_end_pos;
1122 size_t name_len;
1124 derive_t cpu_user_counter;
1125 derive_t cpu_system_counter;
1126 long long unsigned vmem_size;
1127 long long unsigned vmem_rss;
1128 long long unsigned stack_size;
1130 ssize_t status;
1132 snprintf(filename, sizeof(filename), "/proc/%li/stat", pid);
1134 status = read_file_contents(filename, buffer, sizeof(buffer) - 1);
1135 if (status <= 0)
1136 return -1;
1137 buffer_len = (size_t)status;
1138 buffer[buffer_len] = 0;
1140 /* The name of the process is enclosed in parens. Since the name can
1141 * contain parens itself, spaces, numbers and pretty much everything
1142 * else, use these to determine the process name. We don't use
1143 * strchr(3) and strrchr(3) to avoid pointer arithmetic which would
1144 * otherwise be required to determine name_len. */
1145 name_start_pos = 0;
1146 while (name_start_pos < buffer_len && buffer[name_start_pos] != '(')
1147 name_start_pos++;
1149 name_end_pos = buffer_len;
1150 while (name_end_pos > 0 && buffer[name_end_pos] != ')')
1151 name_end_pos--;
1153 /* Either '(' or ')' is not found or they are in the wrong order.
1154 * Anyway, something weird that shouldn't happen ever. */
1155 if (name_start_pos >= name_end_pos) {
1156 ERROR("processes plugin: name_start_pos = %zu >= name_end_pos = %zu",
1157 name_start_pos, name_end_pos);
1158 return -1;
1159 }
1161 name_len = (name_end_pos - name_start_pos) - 1;
1162 if (name_len >= sizeof(ps->name))
1163 name_len = sizeof(ps->name) - 1;
1165 sstrncpy(ps->name, &buffer[name_start_pos + 1], name_len + 1);
1167 if ((buffer_len - name_end_pos) < 2)
1168 return -1;
1169 buffer_ptr = &buffer[name_end_pos + 2];
1171 fields_len = strsplit(buffer_ptr, fields, STATIC_ARRAY_SIZE(fields));
1172 if (fields_len < 22) {
1173 DEBUG("processes plugin: ps_read_process (pid = %li):"
1174 " `%s' has only %i fields..",
1175 pid, filename, fields_len);
1176 return -1;
1177 }
1179 *state = fields[0][0];
1181 if (*state == 'Z') {
1182 ps->num_lwp = 0;
1183 ps->num_proc = 0;
1184 } else {
1185 ps->num_lwp = strtoul(fields[17], /* endptr = */ NULL, /* base = */ 10);
1186 if ((ps_read_status(pid, ps)) != 0) {
1187 /* No VMem data */
1188 ps->vmem_data = -1;
1189 ps->vmem_code = -1;
1190 DEBUG("ps_read_process: did not get vmem data for pid %li", pid);
1191 }
1192 if (ps->num_lwp == 0)
1193 ps->num_lwp = 1;
1194 ps->num_proc = 1;
1195 }
1197 /* Leave the rest at zero if this is only a zombi */
1198 if (ps->num_proc == 0) {
1199 DEBUG("processes plugin: This is only a zombie: pid = %li; "
1200 "name = %s;",
1201 pid, ps->name);
1202 return 0;
1203 }
1205 cpu_user_counter = atoll(fields[11]);
1206 cpu_system_counter = atoll(fields[12]);
1207 vmem_size = atoll(fields[20]);
1208 vmem_rss = atoll(fields[21]);
1209 ps->vmem_minflt_counter = atol(fields[7]);
1210 ps->vmem_majflt_counter = atol(fields[9]);
1212 {
1213 unsigned long long stack_start = atoll(fields[25]);
1214 unsigned long long stack_ptr = atoll(fields[26]);
1216 stack_size = (stack_start > stack_ptr) ? stack_start - stack_ptr
1217 : stack_ptr - stack_start;
1218 }
1220 /* Convert jiffies to useconds */
1221 cpu_user_counter = cpu_user_counter * 1000000 / CONFIG_HZ;
1222 cpu_system_counter = cpu_system_counter * 1000000 / CONFIG_HZ;
1223 vmem_rss = vmem_rss * pagesize_g;
1225 ps->cpu_user_counter = cpu_user_counter;
1226 ps->cpu_system_counter = cpu_system_counter;
1227 ps->vmem_size = (unsigned long)vmem_size;
1228 ps->vmem_rss = (unsigned long)vmem_rss;
1229 ps->stack_size = (unsigned long)stack_size;
1231 /* no data by default. May be filled by ps_fill_details () */
1232 ps->io_rchar = -1;
1233 ps->io_wchar = -1;
1234 ps->io_syscr = -1;
1235 ps->io_syscw = -1;
1236 ps->io_diskr = -1;
1237 ps->io_diskw = -1;
1239 ps->cswitch_vol = -1;
1240 ps->cswitch_invol = -1;
1242 /* success */
1243 return 0;
1244 } /* int ps_read_process (...) */
1246 static char *ps_get_cmdline(long pid, char *name, char *buf, size_t buf_len) {
1247 char *buf_ptr;
1248 size_t len;
1250 char file[PATH_MAX];
1251 int fd;
1253 size_t n;
1255 if ((pid < 1) || (NULL == buf) || (buf_len < 2))
1256 return NULL;
1258 snprintf(file, sizeof(file), "/proc/%li/cmdline", pid);
1260 errno = 0;
1261 fd = open(file, O_RDONLY);
1262 if (fd < 0) {
1263 char errbuf[4096];
1264 /* ENOENT means the process exited while we were handling it.
1265 * Don't complain about this, it only fills the logs. */
1266 if (errno != ENOENT)
1267 WARNING("processes plugin: Failed to open `%s': %s.", file,
1268 sstrerror(errno, errbuf, sizeof(errbuf)));
1269 return NULL;
1270 }
1272 buf_ptr = buf;
1273 len = buf_len;
1275 n = 0;
1277 while (42) {
1278 ssize_t status;
1280 status = read(fd, (void *)buf_ptr, len);
1282 if (status < 0) {
1283 char errbuf[1024];
1285 if ((EAGAIN == errno) || (EINTR == errno))
1286 continue;
1288 WARNING("processes plugin: Failed to read from `%s': %s.", file,
1289 sstrerror(errno, errbuf, sizeof(errbuf)));
1290 close(fd);
1291 return NULL;
1292 }
1294 n += status;
1296 if (status == 0)
1297 break;
1299 buf_ptr += status;
1300 len -= status;
1302 if (len == 0)
1303 break;
1304 }
1306 close(fd);
1308 if (0 == n) {
1309 /* cmdline not available; e.g. kernel thread, zombie */
1310 if (NULL == name)
1311 return NULL;
1313 snprintf(buf, buf_len, "[%s]", name);
1314 return buf;
1315 }
1317 assert(n <= buf_len);
1319 if (n == buf_len)
1320 --n;
1321 buf[n] = '\0';
1323 --n;
1324 /* remove trailing whitespace */
1325 while ((n > 0) && (isspace(buf[n]) || ('\0' == buf[n]))) {
1326 buf[n] = '\0';
1327 --n;
1328 }
1330 /* arguments are separated by '\0' in /proc/<pid>/cmdline */
1331 while (n > 0) {
1332 if ('\0' == buf[n])
1333 buf[n] = ' ';
1334 --n;
1335 }
1336 return buf;
1337 } /* char *ps_get_cmdline (...) */
1339 static int read_fork_rate(void) {
1340 FILE *proc_stat;
1341 char buffer[1024];
1342 value_t value;
1343 _Bool value_valid = 0;
1345 proc_stat = fopen("/proc/stat", "r");
1346 if (proc_stat == NULL) {
1347 char errbuf[1024];
1348 ERROR("processes plugin: fopen (/proc/stat) failed: %s",
1349 sstrerror(errno, errbuf, sizeof(errbuf)));
1350 return -1;
1351 }
1353 while (fgets(buffer, sizeof(buffer), proc_stat) != NULL) {
1354 int status;
1355 char *fields[3];
1356 int fields_num;
1358 fields_num = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
1359 if (fields_num != 2)
1360 continue;
1362 if (strcmp("processes", fields[0]) != 0)
1363 continue;
1365 status = parse_value(fields[1], &value, DS_TYPE_DERIVE);
1366 if (status == 0)
1367 value_valid = 1;
1369 break;
1370 }
1371 fclose(proc_stat);
1373 if (!value_valid)
1374 return -1;
1376 ps_submit_fork_rate(value.derive);
1377 return 0;
1378 }
1379 #endif /*KERNEL_LINUX */
1381 #if KERNEL_SOLARIS
1382 static char *ps_get_cmdline(long pid,
1383 char *name __attribute__((unused)), /* {{{ */
1384 char *buffer, size_t buffer_size) {
1385 char path[PATH_MAX];
1386 psinfo_t info;
1387 ssize_t status;
1389 snprintf(path, sizeof(path), "/proc/%li/psinfo", pid);
1391 status = read_file_contents(path, (void *)&info, sizeof(info));
1392 if ((status < 0) || (((size_t)status) != sizeof(info))) {
1393 ERROR("processes plugin: Unexpected return value "
1394 "while reading \"%s\": "
1395 "Returned %zd but expected %zu.",
1396 path, status, buffer_size);
1397 return NULL;
1398 }
1400 info.pr_psargs[sizeof(info.pr_psargs) - 1] = 0;
1401 sstrncpy(buffer, info.pr_psargs, buffer_size);
1403 return buffer;
1404 } /* }}} int ps_get_cmdline */
1406 /*
1407 * Reads process information on the Solaris OS. The information comes mainly
1408 * from
1409 * /proc/PID/status, /proc/PID/psinfo and /proc/PID/usage
1410 * The values for input and ouput chars are calculated "by hand"
1411 * Added a few "solaris" specific process states as well
1412 */
1413 static int ps_read_process(long pid, process_entry_t *ps, char *state) {
1414 char filename[64];
1415 char f_psinfo[64], f_usage[64];
1416 char *buffer;
1418 pstatus_t *myStatus;
1419 psinfo_t *myInfo;
1420 prusage_t *myUsage;
1422 snprintf(filename, sizeof(filename), "/proc/%li/status", pid);
1423 snprintf(f_psinfo, sizeof(f_psinfo), "/proc/%li/psinfo", pid);
1424 snprintf(f_usage, sizeof(f_usage), "/proc/%li/usage", pid);
1426 buffer = calloc(1, sizeof(pstatus_t));
1427 read_file_contents(filename, buffer, sizeof(pstatus_t));
1428 myStatus = (pstatus_t *)buffer;
1430 buffer = calloc(1, sizeof(psinfo_t));
1431 read_file_contents(f_psinfo, buffer, sizeof(psinfo_t));
1432 myInfo = (psinfo_t *)buffer;
1434 buffer = calloc(1, sizeof(prusage_t));
1435 read_file_contents(f_usage, buffer, sizeof(prusage_t));
1436 myUsage = (prusage_t *)buffer;
1438 sstrncpy(ps->name, myInfo->pr_fname, sizeof(myInfo->pr_fname));
1439 ps->num_lwp = myStatus->pr_nlwp;
1440 if (myInfo->pr_wstat != 0) {
1441 ps->num_proc = 0;
1442 ps->num_lwp = 0;
1443 *state = (char)'Z';
1445 sfree(myStatus);
1446 sfree(myInfo);
1447 sfree(myUsage);
1448 return 0;
1449 } else {
1450 ps->num_proc = 1;
1451 ps->num_lwp = myInfo->pr_nlwp;
1452 }
1454 /*
1455 * Convert system time and user time from nanoseconds to microseconds
1456 * for compatibility with the linux module
1457 */
1458 ps->cpu_system_counter = myStatus->pr_stime.tv_nsec / 1000;
1459 ps->cpu_user_counter = myStatus->pr_utime.tv_nsec / 1000;
1461 /*
1462 * Convert rssize from KB to bytes to be consistent w/ the linux module
1463 */
1464 ps->vmem_rss = myInfo->pr_rssize * 1024;
1465 ps->vmem_size = myInfo->pr_size * 1024;
1466 ps->vmem_minflt_counter = myUsage->pr_minf;
1467 ps->vmem_majflt_counter = myUsage->pr_majf;
1469 /*
1470 * TODO: Data and code segment calculations for Solaris
1471 */
1473 ps->vmem_data = -1;
1474 ps->vmem_code = -1;
1475 ps->stack_size = myStatus->pr_stksize;
1477 /*
1478 * TODO: File descriptor count for Solaris
1479 */
1480 ps->num_fd = 0;
1482 /* Number of memory mappings */
1483 ps->num_maps = 0;
1485 /*
1486 * Calculating input/ouput chars
1487 * Formula used is total chars / total blocks => chars/block
1488 * then convert input/output blocks to chars
1489 */
1490 ulong_t tot_chars = myUsage->pr_ioch;
1491 ulong_t tot_blocks = myUsage->pr_inblk + myUsage->pr_oublk;
1492 ulong_t chars_per_block = 1;
1493 if (tot_blocks != 0)
1494 chars_per_block = tot_chars / tot_blocks;
1495 ps->io_rchar = myUsage->pr_inblk * chars_per_block;
1496 ps->io_wchar = myUsage->pr_oublk * chars_per_block;
1497 ps->io_syscr = myUsage->pr_sysc;
1498 ps->io_syscw = myUsage->pr_sysc;
1499 ps->io_diskr = -1;
1500 ps->io_diskw = -1;
1502 /*
1503 * TODO: context switch counters for Solaris
1504 */
1505 ps->cswitch_vol = -1;
1506 ps->cswitch_invol = -1;
1508 /*
1509 * TODO: Find way of setting BLOCKED and PAGING status
1510 */
1512 *state = (char)'R';
1513 if (myStatus->pr_flags & PR_ASLEEP)
1514 *state = (char)'S';
1515 else if (myStatus->pr_flags & PR_STOPPED)
1516 *state = (char)'T';
1517 else if (myStatus->pr_flags & PR_DETACH)
1518 *state = (char)'E';
1519 else if (myStatus->pr_flags & PR_DAEMON)
1520 *state = (char)'A';
1521 else if (myStatus->pr_flags & PR_ISSYS)
1522 *state = (char)'Y';
1523 else if (myStatus->pr_flags & PR_ORPHAN)
1524 *state = (char)'O';
1526 sfree(myStatus);
1527 sfree(myInfo);
1528 sfree(myUsage);
1530 return 0;
1531 }
1533 /*
1534 * Reads the number of threads created since the last reboot. On Solaris these
1535 * are retrieved from kstat (module cpu, name sys, class misc, stat nthreads).
1536 * The result is the sum for all the threads created on each cpu
1537 */
1538 static int read_fork_rate(void) {
1539 extern kstat_ctl_t *kc;
1540 derive_t result = 0;
1542 if (kc == NULL)
1543 return -1;
1545 for (kstat_t *ksp_chain = kc->kc_chain; ksp_chain != NULL;
1546 ksp_chain = ksp_chain->ks_next) {
1547 if ((strcmp(ksp_chain->ks_module, "cpu") == 0) &&
1548 (strcmp(ksp_chain->ks_name, "sys") == 0) &&
1549 (strcmp(ksp_chain->ks_class, "misc") == 0)) {
1550 long long tmp;
1552 kstat_read(kc, ksp_chain, NULL);
1554 tmp = get_kstat_value(ksp_chain, "nthreads");
1555 if (tmp != -1LL)
1556 result += tmp;
1557 }
1558 }
1560 ps_submit_fork_rate(result);
1561 return 0;
1562 }
1563 #endif /* KERNEL_SOLARIS */
1565 #if HAVE_THREAD_INFO
1566 static int mach_get_task_name(task_t t, int *pid, char *name,
1567 size_t name_max_len) {
1568 int mib[4];
1570 struct kinfo_proc kp;
1571 size_t kp_size;
1573 mib[0] = CTL_KERN;
1574 mib[1] = KERN_PROC;
1575 mib[2] = KERN_PROC_PID;
1577 if (pid_for_task(t, pid) != KERN_SUCCESS)
1578 return -1;
1579 mib[3] = *pid;
1581 kp_size = sizeof(kp);
1582 if (sysctl(mib, 4, &kp, &kp_size, NULL, 0) != 0)
1583 return -1;
1585 if (name_max_len > (MAXCOMLEN + 1))
1586 name_max_len = MAXCOMLEN + 1;
1588 strncpy(name, kp.kp_proc.p_comm, name_max_len - 1);
1589 name[name_max_len - 1] = '\0';
1591 DEBUG("pid = %i; name = %s;", *pid, name);
1593 /* We don't do the special handling for `p_comm == "LaunchCFMApp"' as
1594 * `top' does it, because it is a lot of work and only used when
1595 * debugging. -octo */
1597 return 0;
1598 }
1599 #endif /* HAVE_THREAD_INFO */
1600 /* ------- end of additional functions for KERNEL_LINUX/HAVE_THREAD_INFO -------
1601 */
1603 /* do actual readings from kernel */
1604 static int ps_read(void) {
1605 #if HAVE_THREAD_INFO
1606 kern_return_t status;
1608 processor_set_t port_pset_priv;
1610 task_array_t task_list;
1611 mach_msg_type_number_t task_list_len;
1613 int task_pid;
1614 char task_name[MAXCOMLEN + 1];
1616 thread_act_array_t thread_list;
1617 mach_msg_type_number_t thread_list_len;
1618 thread_basic_info_data_t thread_data;
1619 mach_msg_type_number_t thread_data_len;
1621 int running = 0;
1622 int sleeping = 0;
1623 int zombies = 0;
1624 int stopped = 0;
1625 int blocked = 0;
1627 procstat_t *ps;
1628 process_entry_t pse;
1630 ps_list_reset();
1632 /*
1633 * The Mach-concept is a little different from the traditional UNIX
1634 * concept: All the work is done in threads. Threads are contained in
1635 * `tasks'. Therefore, `task status' doesn't make much sense, since
1636 * it's actually a `thread status'.
1637 * Tasks are assigned to sets of processors, so that's where you go to
1638 * get a list.
1639 */
1640 for (mach_msg_type_number_t pset = 0; pset < pset_list_len; pset++) {
1641 if ((status = host_processor_set_priv(port_host_self, pset_list[pset],
1642 &port_pset_priv)) != KERN_SUCCESS) {
1643 ERROR("host_processor_set_priv failed: %s\n", mach_error_string(status));
1644 continue;
1645 }
1647 if ((status = processor_set_tasks(port_pset_priv, &task_list,
1648 &task_list_len)) != KERN_SUCCESS) {
1649 ERROR("processor_set_tasks failed: %s\n", mach_error_string(status));
1650 mach_port_deallocate(port_task_self, port_pset_priv);
1651 continue;
1652 }
1654 for (mach_msg_type_number_t task = 0; task < task_list_len; task++) {
1655 ps = NULL;
1656 if (mach_get_task_name(task_list[task], &task_pid, task_name,
1657 PROCSTAT_NAME_LEN) == 0) {
1658 /* search for at least one match */
1659 for (ps = list_head_g; ps != NULL; ps = ps->next)
1660 /* FIXME: cmdline should be here instead of NULL */
1661 if (ps_list_match(task_name, NULL, ps) == 1)
1662 break;
1663 }
1665 /* Collect more detailed statistics for this process */
1666 if (ps != NULL) {
1667 task_basic_info_data_t task_basic_info;
1668 mach_msg_type_number_t task_basic_info_len;
1669 task_events_info_data_t task_events_info;
1670 mach_msg_type_number_t task_events_info_len;
1671 task_absolutetime_info_data_t task_absolutetime_info;
1672 mach_msg_type_number_t task_absolutetime_info_len;
1674 memset(&pse, '\0', sizeof(pse));
1675 pse.id = task_pid;
1677 task_basic_info_len = TASK_BASIC_INFO_COUNT;
1678 status = task_info(task_list[task], TASK_BASIC_INFO,
1679 (task_info_t)&task_basic_info, &task_basic_info_len);
1680 if (status != KERN_SUCCESS) {
1681 ERROR("task_info failed: %s", mach_error_string(status));
1682 continue; /* with next thread_list */
1683 }
1685 task_events_info_len = TASK_EVENTS_INFO_COUNT;
1686 status =
1687 task_info(task_list[task], TASK_EVENTS_INFO,
1688 (task_info_t)&task_events_info, &task_events_info_len);
1689 if (status != KERN_SUCCESS) {
1690 ERROR("task_info failed: %s", mach_error_string(status));
1691 continue; /* with next thread_list */
1692 }
1694 task_absolutetime_info_len = TASK_ABSOLUTETIME_INFO_COUNT;
1695 status = task_info(task_list[task], TASK_ABSOLUTETIME_INFO,
1696 (task_info_t)&task_absolutetime_info,
1697 &task_absolutetime_info_len);
1698 if (status != KERN_SUCCESS) {
1699 ERROR("task_info failed: %s", mach_error_string(status));
1700 continue; /* with next thread_list */
1701 }
1703 pse.num_proc++;
1704 pse.vmem_size = task_basic_info.virtual_size;
1705 pse.vmem_rss = task_basic_info.resident_size;
1706 /* Does not seem to be easily exposed */
1707 pse.vmem_data = 0;
1708 pse.vmem_code = 0;
1710 pse.io_rchar = -1;
1711 pse.io_wchar = -1;
1712 pse.io_syscr = -1;
1713 pse.io_syscw = -1;
1714 pse.io_diskr = -1;
1715 pse.io_diskw = -1;
1717 /* File descriptor count not implemented */
1718 pse.num_fd = 0;
1720 /* Number of memory mappings */
1721 pse.num_maps = 0;
1723 pse.vmem_minflt_counter = task_events_info.cow_faults;
1724 pse.vmem_majflt_counter = task_events_info.faults;
1726 pse.cpu_user_counter = task_absolutetime_info.total_user;
1727 pse.cpu_system_counter = task_absolutetime_info.total_system;
1729 /* context switch counters not implemented */
1730 pse.cswitch_vol = -1;
1731 pse.cswitch_invol = -1;
1732 }
1734 status = task_threads(task_list[task], &thread_list, &thread_list_len);
1735 if (status != KERN_SUCCESS) {
1736 /* Apple's `top' treats this case a zombie. It
1737 * makes sense to some extend: A `zombie'
1738 * thread is nonsense, since the task/process
1739 * is dead. */
1740 zombies++;
1741 DEBUG("task_threads failed: %s", mach_error_string(status));
1742 if (task_list[task] != port_task_self)
1743 mach_port_deallocate(port_task_self, task_list[task]);
1744 continue; /* with next task_list */
1745 }
1747 for (mach_msg_type_number_t thread = 0; thread < thread_list_len;
1748 thread++) {
1749 thread_data_len = THREAD_BASIC_INFO_COUNT;
1750 status = thread_info(thread_list[thread], THREAD_BASIC_INFO,
1751 (thread_info_t)&thread_data, &thread_data_len);
1752 if (status != KERN_SUCCESS) {
1753 ERROR("thread_info failed: %s", mach_error_string(status));
1754 if (task_list[task] != port_task_self)
1755 mach_port_deallocate(port_task_self, thread_list[thread]);
1756 continue; /* with next thread_list */
1757 }
1759 if (ps != NULL)
1760 pse.num_lwp++;
1762 switch (thread_data.run_state) {
1763 case TH_STATE_RUNNING:
1764 running++;
1765 break;
1766 case TH_STATE_STOPPED:
1767 /* What exactly is `halted'? */
1768 case TH_STATE_HALTED:
1769 stopped++;
1770 break;
1771 case TH_STATE_WAITING:
1772 sleeping++;
1773 break;
1774 case TH_STATE_UNINTERRUPTIBLE:
1775 blocked++;
1776 break;
1777 /* There is no `zombie' case here,
1778 * since there are no zombie-threads.
1779 * There's only zombie tasks, which are
1780 * handled above. */
1781 default:
1782 WARNING("Unknown thread status: %i", thread_data.run_state);
1783 break;
1784 } /* switch (thread_data.run_state) */
1786 if (task_list[task] != port_task_self) {
1787 status = mach_port_deallocate(port_task_self, thread_list[thread]);
1788 if (status != KERN_SUCCESS)
1789 ERROR("mach_port_deallocate failed: %s", mach_error_string(status));
1790 }
1791 } /* for (thread_list) */
1793 if ((status = vm_deallocate(port_task_self, (vm_address_t)thread_list,
1794 thread_list_len * sizeof(thread_act_t))) !=
1795 KERN_SUCCESS) {
1796 ERROR("vm_deallocate failed: %s", mach_error_string(status));
1797 }
1798 thread_list = NULL;
1799 thread_list_len = 0;
1801 /* Only deallocate the task port, if it isn't our own.
1802 * Don't know what would happen in that case, but this
1803 * is what Apple's top does.. ;) */
1804 if (task_list[task] != port_task_self) {
1805 status = mach_port_deallocate(port_task_self, task_list[task]);
1806 if (status != KERN_SUCCESS)
1807 ERROR("mach_port_deallocate failed: %s", mach_error_string(status));
1808 }
1810 if (ps != NULL)
1811 /* FIXME: cmdline should be here instead of NULL */
1812 ps_list_add(task_name, NULL, &pse);
1813 } /* for (task_list) */
1815 if ((status = vm_deallocate(port_task_self, (vm_address_t)task_list,
1816 task_list_len * sizeof(task_t))) !=
1817 KERN_SUCCESS) {
1818 ERROR("vm_deallocate failed: %s", mach_error_string(status));
1819 }
1820 task_list = NULL;
1821 task_list_len = 0;
1823 if ((status = mach_port_deallocate(port_task_self, port_pset_priv)) !=
1824 KERN_SUCCESS) {
1825 ERROR("mach_port_deallocate failed: %s", mach_error_string(status));
1826 }
1827 } /* for (pset_list) */
1829 ps_submit_state("running", running);
1830 ps_submit_state("sleeping", sleeping);
1831 ps_submit_state("zombies", zombies);
1832 ps_submit_state("stopped", stopped);
1833 ps_submit_state("blocked", blocked);
1835 for (ps = list_head_g; ps != NULL; ps = ps->next)
1836 ps_submit_proc_list(ps);
1837 /* #endif HAVE_THREAD_INFO */
1839 #elif KERNEL_LINUX
1840 int running = 0;
1841 int sleeping = 0;
1842 int zombies = 0;
1843 int stopped = 0;
1844 int paging = 0;
1845 int blocked = 0;
1847 struct dirent *ent;
1848 DIR *proc;
1849 long pid;
1851 char cmdline[CMDLINE_BUFFER_SIZE];
1853 int status;
1854 process_entry_t pse;
1855 char state;
1857 running = sleeping = zombies = stopped = paging = blocked = 0;
1858 ps_list_reset();
1860 if ((proc = opendir("/proc")) == NULL) {
1861 char errbuf[1024];
1862 ERROR("Cannot open `/proc': %s", sstrerror(errno, errbuf, sizeof(errbuf)));
1863 return -1;
1864 }
1866 while ((ent = readdir(proc)) != NULL) {
1867 if (!isdigit(ent->d_name[0]))
1868 continue;
1870 if ((pid = atol(ent->d_name)) < 1)
1871 continue;
1873 memset(&pse, 0, sizeof(pse));
1874 pse.id = pid;
1876 status = ps_read_process(pid, &pse, &state);
1877 if (status != 0) {
1878 DEBUG("ps_read_process failed: %i", status);
1879 continue;
1880 }
1882 switch (state) {
1883 case 'R':
1884 running++;
1885 break;
1886 case 'S':
1887 sleeping++;
1888 break;
1889 case 'D':
1890 blocked++;
1891 break;
1892 case 'Z':
1893 zombies++;
1894 break;
1895 case 'T':
1896 stopped++;
1897 break;
1898 case 'W':
1899 paging++;
1900 break;
1901 }
1903 ps_list_add(pse.name,
1904 ps_get_cmdline(pid, pse.name, cmdline, sizeof(cmdline)), &pse);
1905 }
1907 closedir(proc);
1909 ps_submit_state("running", running);
1910 ps_submit_state("sleeping", sleeping);
1911 ps_submit_state("zombies", zombies);
1912 ps_submit_state("stopped", stopped);
1913 ps_submit_state("paging", paging);
1914 ps_submit_state("blocked", blocked);
1916 for (procstat_t *ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1917 ps_submit_proc_list(ps_ptr);
1919 read_fork_rate();
1920 /* #endif KERNEL_LINUX */
1922 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD
1923 int running = 0;
1924 int sleeping = 0;
1925 int zombies = 0;
1926 int stopped = 0;
1927 int blocked = 0;
1928 int idle = 0;
1929 int wait = 0;
1931 kvm_t *kd;
1932 char errbuf[_POSIX2_LINE_MAX];
1933 struct kinfo_proc *procs; /* array of processes */
1934 struct kinfo_proc *proc_ptr = NULL;
1935 int count; /* returns number of processes */
1937 process_entry_t pse;
1939 ps_list_reset();
1941 /* Open the kvm interface, get a descriptor */
1942 kd = kvm_openfiles(NULL, "/dev/null", NULL, 0, errbuf);
1943 if (kd == NULL) {
1944 ERROR("processes plugin: Cannot open kvm interface: %s", errbuf);
1945 return 0;
1946 }
1948 /* Get the list of processes. */
1949 procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, &count);
1950 if (procs == NULL) {
1951 ERROR("processes plugin: Cannot get kvm processes list: %s",
1952 kvm_geterr(kd));
1953 kvm_close(kd);
1954 return 0;
1955 }
1957 /* Iterate through the processes in kinfo_proc */
1958 for (int i = 0; i < count; i++) {
1959 /* Create only one process list entry per _process_, i.e.
1960 * filter out threads (duplicate PID entries). */
1961 if ((proc_ptr == NULL) || (proc_ptr->ki_pid != procs[i].ki_pid)) {
1962 char cmdline[CMDLINE_BUFFER_SIZE] = "";
1963 _Bool have_cmdline = 0;
1965 proc_ptr = &(procs[i]);
1966 /* Don't probe system processes and processes without arguments */
1967 if (((procs[i].ki_flag & P_SYSTEM) == 0) && (procs[i].ki_args != NULL)) {
1968 char **argv;
1969 int argc;
1970 int status;
1972 /* retrieve the arguments */
1973 argv = kvm_getargv(kd, proc_ptr, /* nchr = */ 0);
1974 argc = 0;
1975 if ((argv != NULL) && (argv[0] != NULL)) {
1976 while (argv[argc] != NULL)
1977 argc++;
1979 status = strjoin(cmdline, sizeof(cmdline), argv, argc, " ");
1980 if (status < 0)
1981 WARNING("processes plugin: Command line did not fit into buffer.");
1982 else
1983 have_cmdline = 1;
1984 }
1985 } /* if (process has argument list) */
1987 memset(&pse, 0, sizeof(pse));
1988 pse.id = procs[i].ki_pid;
1990 pse.num_proc = 1;
1991 pse.num_lwp = procs[i].ki_numthreads;
1993 pse.vmem_size = procs[i].ki_size;
1994 pse.vmem_rss = procs[i].ki_rssize * pagesize;
1995 pse.vmem_data = procs[i].ki_dsize * pagesize;
1996 pse.vmem_code = procs[i].ki_tsize * pagesize;
1997 pse.stack_size = procs[i].ki_ssize * pagesize;
1998 pse.vmem_minflt_counter = procs[i].ki_rusage.ru_minflt;
1999 pse.vmem_majflt_counter = procs[i].ki_rusage.ru_majflt;
2001 pse.cpu_user_counter = 0;
2002 pse.cpu_system_counter = 0;
2003 /*
2004 * The u-area might be swapped out, and we can't get
2005 * at it because we have a crashdump and no swap.
2006 * If it's here fill in these fields, otherwise, just
2007 * leave them 0.
2008 */
2009 if (procs[i].ki_flag & P_INMEM) {
2010 pse.cpu_user_counter = procs[i].ki_rusage.ru_utime.tv_usec +
2011 (1000000lu * procs[i].ki_rusage.ru_utime.tv_sec);
2012 pse.cpu_system_counter =
2013 procs[i].ki_rusage.ru_stime.tv_usec +
2014 (1000000lu * procs[i].ki_rusage.ru_stime.tv_sec);
2015 }
2017 /* no I/O data */
2018 pse.io_rchar = -1;
2019 pse.io_wchar = -1;
2020 pse.io_syscr = -1;
2021 pse.io_syscw = -1;
2022 pse.io_diskr = -1;
2023 pse.io_diskw = -1;
2025 /* file descriptor count not implemented */
2026 pse.num_fd = 0;
2028 /* Number of memory mappings */
2029 pse.num_maps = 0;
2031 /* context switch counters not implemented */
2032 pse.cswitch_vol = -1;
2033 pse.cswitch_invol = -1;
2035 ps_list_add(procs[i].ki_comm, have_cmdline ? cmdline : NULL, &pse);
2037 switch (procs[i].ki_stat) {
2038 case SSTOP:
2039 stopped++;
2040 break;
2041 case SSLEEP:
2042 sleeping++;
2043 break;
2044 case SRUN:
2045 running++;
2046 break;
2047 case SIDL:
2048 idle++;
2049 break;
2050 case SWAIT:
2051 wait++;
2052 break;
2053 case SLOCK:
2054 blocked++;
2055 break;
2056 case SZOMB:
2057 zombies++;
2058 break;
2059 }
2060 } /* if ((proc_ptr == NULL) || (proc_ptr->ki_pid != procs[i].ki_pid)) */
2061 }
2063 kvm_close(kd);
2065 ps_submit_state("running", running);
2066 ps_submit_state("sleeping", sleeping);
2067 ps_submit_state("zombies", zombies);
2068 ps_submit_state("stopped", stopped);
2069 ps_submit_state("blocked", blocked);
2070 ps_submit_state("idle", idle);
2071 ps_submit_state("wait", wait);
2073 for (procstat_t *ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
2074 ps_submit_proc_list(ps_ptr);
2075 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
2077 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_OPENBSD
2078 int running = 0;
2079 int sleeping = 0;
2080 int zombies = 0;
2081 int stopped = 0;
2082 int onproc = 0;
2083 int idle = 0;
2084 int dead = 0;
2086 kvm_t *kd;
2087 char errbuf[1024];
2088 struct kinfo_proc *procs; /* array of processes */
2089 struct kinfo_proc *proc_ptr = NULL;
2090 int count; /* returns number of processes */
2092 process_entry_t pse;
2094 ps_list_reset();
2096 /* Open the kvm interface, get a descriptor */
2097 kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf);
2098 if (kd == NULL) {
2099 ERROR("processes plugin: Cannot open kvm interface: %s", errbuf);
2100 return 0;
2101 }
2103 /* Get the list of processes. */
2104 procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc), &count);
2105 if (procs == NULL) {
2106 ERROR("processes plugin: Cannot get kvm processes list: %s",
2107 kvm_geterr(kd));
2108 kvm_close(kd);
2109 return 0;
2110 }
2112 /* Iterate through the processes in kinfo_proc */
2113 for (int i = 0; i < count; i++) {
2114 /* Create only one process list entry per _process_, i.e.
2115 * filter out threads (duplicate PID entries). */
2116 if ((proc_ptr == NULL) || (proc_ptr->p_pid != procs[i].p_pid)) {
2117 char cmdline[CMDLINE_BUFFER_SIZE] = "";
2118 _Bool have_cmdline = 0;
2120 proc_ptr = &(procs[i]);
2121 /* Don't probe zombie processes */
2122 if (!P_ZOMBIE(proc_ptr)) {
2123 char **argv;
2124 int argc;
2125 int status;
2127 /* retrieve the arguments */
2128 argv = kvm_getargv(kd, proc_ptr, /* nchr = */ 0);
2129 argc = 0;
2130 if ((argv != NULL) && (argv[0] != NULL)) {
2131 while (argv[argc] != NULL)
2132 argc++;
2134 status = strjoin(cmdline, sizeof(cmdline), argv, argc, " ");
2135 if (status < 0)
2136 WARNING("processes plugin: Command line did not fit into buffer.");
2137 else
2138 have_cmdline = 1;
2139 }
2140 } /* if (process has argument list) */
2142 memset(&pse, 0, sizeof(pse));
2143 pse.id = procs[i].p_pid;
2145 pse.num_proc = 1;
2146 pse.num_lwp = 1; /* XXX: accumulate p_tid values for a single p_pid ? */
2148 pse.vmem_rss = procs[i].p_vm_rssize * pagesize;
2149 pse.vmem_data = procs[i].p_vm_dsize * pagesize;
2150 pse.vmem_code = procs[i].p_vm_tsize * pagesize;
2151 pse.stack_size = procs[i].p_vm_ssize * pagesize;
2152 pse.vmem_size = pse.stack_size + pse.vmem_code + pse.vmem_data;
2153 pse.vmem_minflt_counter = procs[i].p_uru_minflt;
2154 pse.vmem_majflt_counter = procs[i].p_uru_majflt;
2156 pse.cpu_user_counter =
2157 procs[i].p_uutime_usec + (1000000lu * procs[i].p_uutime_sec);
2158 pse.cpu_system_counter =
2159 procs[i].p_ustime_usec + (1000000lu * procs[i].p_ustime_sec);
2161 /* no I/O data */
2162 pse.io_rchar = -1;
2163 pse.io_wchar = -1;
2164 pse.io_syscr = -1;
2165 pse.io_syscw = -1;
2166 pse.io_diskr = -1;
2167 pse.io_diskw = -1;
2169 /* file descriptor count not implemented */
2170 pse.num_fd = 0;
2172 /* Number of memory mappings */
2173 pse.num_maps = 0;
2175 /* context switch counters not implemented */
2176 pse.cswitch_vol = -1;
2177 pse.cswitch_invol = -1;
2179 ps_list_add(procs[i].p_comm, have_cmdline ? cmdline : NULL, &pse);
2181 switch (procs[i].p_stat) {
2182 case SSTOP:
2183 stopped++;
2184 break;
2185 case SSLEEP:
2186 sleeping++;
2187 break;
2188 case SRUN:
2189 running++;
2190 break;
2191 case SIDL:
2192 idle++;
2193 break;
2194 case SONPROC:
2195 onproc++;
2196 break;
2197 case SDEAD:
2198 dead++;
2199 break;
2200 case SZOMB:
2201 zombies++;
2202 break;
2203 }
2204 } /* if ((proc_ptr == NULL) || (proc_ptr->p_pid != procs[i].p_pid)) */
2205 }
2207 kvm_close(kd);
2209 ps_submit_state("running", running);
2210 ps_submit_state("sleeping", sleeping);
2211 ps_submit_state("zombies", zombies);
2212 ps_submit_state("stopped", stopped);
2213 ps_submit_state("onproc", onproc);
2214 ps_submit_state("idle", idle);
2215 ps_submit_state("dead", dead);
2217 for (procstat_t *ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
2218 ps_submit_proc_list(ps_ptr);
2219 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_OPENBSD */
2221 #elif HAVE_PROCINFO_H
2222 /* AIX */
2223 int running = 0;
2224 int sleeping = 0;
2225 int zombies = 0;
2226 int stopped = 0;
2227 int paging = 0;
2228 int blocked = 0;
2230 pid_t pindex = 0;
2231 int nprocs;
2233 process_entry_t pse;
2235 ps_list_reset();
2236 while ((nprocs = getprocs64(procentry, sizeof(struct procentry64),
2237 /* fdsinfo = */ NULL, sizeof(struct fdsinfo64),
2238 &pindex, MAXPROCENTRY)) > 0) {
2239 for (int i = 0; i < nprocs; i++) {
2240 tid64_t thindex;
2241 int nthreads;
2242 char arglist[MAXARGLN + 1];
2243 char *cargs;
2244 char *cmdline;
2246 if (procentry[i].pi_state == SNONE)
2247 continue;
2248 /* if (procentry[i].pi_state == SZOMB) FIXME */
2250 cmdline = procentry[i].pi_comm;
2251 cargs = procentry[i].pi_comm;
2252 if (procentry[i].pi_flags & SKPROC) {
2253 if (procentry[i].pi_pid == 0)
2254 cmdline = "swapper";
2255 cargs = cmdline;
2256 } else {
2257 if (getargs(&procentry[i], sizeof(struct procentry64), arglist,
2258 MAXARGLN) >= 0) {
2259 int n;
2261 n = -1;
2262 while (++n < MAXARGLN) {
2263 if (arglist[n] == '\0') {
2264 if (arglist[n + 1] == '\0')
2265 break;
2266 arglist[n] = ' ';
2267 }
2268 }
2269 cargs = arglist;
2270 }
2271 }
2273 memset(&pse, 0, sizeof(pse));
2275 pse.id = procentry[i].pi_pid;
2276 pse.num_lwp = procentry[i].pi_thcount;
2277 pse.num_proc = 1;
2279 thindex = 0;
2280 while ((nthreads = getthrds64(procentry[i].pi_pid, thrdentry,
2281 sizeof(struct thrdentry64), &thindex,
2282 MAXTHRDENTRY)) > 0) {
2283 int j;
2285 for (j = 0; j < nthreads; j++) {
2286 switch (thrdentry[j].ti_state) {
2287 /* case TSNONE: break; */
2288 case TSIDL:
2289 blocked++;
2290 break; /* FIXME is really blocked */
2291 case TSRUN:
2292 running++;
2293 break;
2294 case TSSLEEP:
2295 sleeping++;
2296 break;
2297 case TSSWAP:
2298 paging++;
2299 break;
2300 case TSSTOP:
2301 stopped++;
2302 break;
2303 case TSZOMB:
2304 zombies++;
2305 break;
2306 }
2307 }
2308 if (nthreads < MAXTHRDENTRY)
2309 break;
2310 }
2312 /* tv_usec is nanosec ??? */
2313 pse.cpu_user_counter = procentry[i].pi_ru.ru_utime.tv_sec * 1000000 +
2314 procentry[i].pi_ru.ru_utime.tv_usec / 1000;
2316 /* tv_usec is nanosec ??? */
2317 pse.cpu_system_counter = procentry[i].pi_ru.ru_stime.tv_sec * 1000000 +
2318 procentry[i].pi_ru.ru_stime.tv_usec / 1000;
2320 pse.vmem_minflt_counter = procentry[i].pi_minflt;
2321 pse.vmem_majflt_counter = procentry[i].pi_majflt;
2323 pse.vmem_size = procentry[i].pi_tsize + procentry[i].pi_dvm * pagesize;
2324 pse.vmem_rss = (procentry[i].pi_drss + procentry[i].pi_trss) * pagesize;
2325 /* Not supported/implemented */
2326 pse.vmem_data = 0;
2327 pse.vmem_code = 0;
2328 pse.stack_size = 0;
2330 pse.io_rchar = -1;
2331 pse.io_wchar = -1;
2332 pse.io_syscr = -1;
2333 pse.io_syscw = -1;
2334 pse.io_diskr = -1;
2335 pse.io_diskw = -1;
2337 pse.num_fd = 0;
2338 pse.num_maps = 0;
2340 pse.cswitch_vol = -1;
2341 pse.cswitch_invol = -1;
2343 ps_list_add(cmdline, cargs, &pse);
2344 } /* for (i = 0 .. nprocs) */
2346 if (nprocs < MAXPROCENTRY)
2347 break;
2348 } /* while (getprocs64() > 0) */
2349 ps_submit_state("running", running);
2350 ps_submit_state("sleeping", sleeping);
2351 ps_submit_state("zombies", zombies);
2352 ps_submit_state("stopped", stopped);
2353 ps_submit_state("paging", paging);
2354 ps_submit_state("blocked", blocked);
2356 for (procstat_t *ps = list_head_g; ps != NULL; ps = ps->next)
2357 ps_submit_proc_list(ps);
2358 /* #endif HAVE_PROCINFO_H */
2360 #elif KERNEL_SOLARIS
2361 /*
2362 * The Solaris section adds a few more process states and removes some
2363 * process states compared to linux. Most notably there is no "PAGING"
2364 * and "BLOCKED" state for a process. The rest is similar to the linux
2365 * code.
2366 */
2367 int running = 0;
2368 int sleeping = 0;
2369 int zombies = 0;
2370 int stopped = 0;
2371 int detached = 0;
2372 int daemon = 0;
2373 int system = 0;
2374 int orphan = 0;
2376 struct dirent *ent;
2377 DIR *proc;
2379 int status;
2380 char state;
2382 char cmdline[PRARGSZ];
2384 ps_list_reset();
2386 proc = opendir("/proc");
2387 if (proc == NULL)
2388 return -1;
2390 while ((ent = readdir(proc)) != NULL) {
2391 long pid;
2392 process_entry_t pse;
2393 char *endptr;
2395 if (!isdigit((int)ent->d_name[0]))
2396 continue;
2398 pid = strtol(ent->d_name, &endptr, 10);
2399 if (*endptr != 0) /* value didn't completely parse as a number */
2400 continue;
2402 memset(&pse, 0, sizeof(pse));
2403 pse.id = pid;
2405 status = ps_read_process(pid, &pse, &state);
2406 if (status != 0) {
2407 DEBUG("ps_read_process failed: %i", status);
2408 continue;
2409 }
2411 switch (state) {
2412 case 'R':
2413 running++;
2414 break;
2415 case 'S':
2416 sleeping++;
2417 break;
2418 case 'E':
2419 detached++;
2420 break;
2421 case 'Z':
2422 zombies++;
2423 break;
2424 case 'T':
2425 stopped++;
2426 break;
2427 case 'A':
2428 daemon++;
2429 break;
2430 case 'Y':
2431 system++;
2432 break;
2433 case 'O':
2434 orphan++;
2435 break;
2436 }
2438 ps_list_add(pse.name,
2439 ps_get_cmdline(pid, pse.name, cmdline, sizeof(cmdline)), &pse);
2440 } /* while(readdir) */
2441 closedir(proc);
2443 ps_submit_state("running", running);
2444 ps_submit_state("sleeping", sleeping);
2445 ps_submit_state("zombies", zombies);
2446 ps_submit_state("stopped", stopped);
2447 ps_submit_state("detached", detached);
2448 ps_submit_state("daemon", daemon);
2449 ps_submit_state("system", system);
2450 ps_submit_state("orphan", orphan);
2452 for (procstat_t *ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
2453 ps_submit_proc_list(ps_ptr);
2455 read_fork_rate();
2456 #endif /* KERNEL_SOLARIS */
2458 want_init = 0;
2460 return 0;
2461 } /* int ps_read */
2463 void module_register(void) {
2464 plugin_register_complex_config("processes", ps_config);
2465 plugin_register_init("processes", ps_init);
2466 plugin_register_read("processes", ps_read);
2467 } /* void module_register */