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 **/
38 #include "collectd.h"
40 #include "common.h"
41 #include "plugin.h"
43 /* Include header files for the mach system, if they exist.. */
44 #if HAVE_THREAD_INFO
45 #if HAVE_MACH_MACH_INIT_H
46 #include <mach/mach_init.h>
47 #endif
48 #if HAVE_MACH_HOST_PRIV_H
49 #include <mach/host_priv.h>
50 #endif
51 #if HAVE_MACH_MACH_ERROR_H
52 #include <mach/mach_error.h>
53 #endif
54 #if HAVE_MACH_MACH_HOST_H
55 #include <mach/mach_host.h>
56 #endif
57 #if HAVE_MACH_MACH_PORT_H
58 #include <mach/mach_port.h>
59 #endif
60 #if HAVE_MACH_MACH_TYPES_H
61 #include <mach/mach_types.h>
62 #endif
63 #if HAVE_MACH_MESSAGE_H
64 #include <mach/message.h>
65 #endif
66 #if HAVE_MACH_PROCESSOR_SET_H
67 #include <mach/processor_set.h>
68 #endif
69 #if HAVE_MACH_TASK_H
70 #include <mach/task.h>
71 #endif
72 #if HAVE_MACH_THREAD_ACT_H
73 #include <mach/thread_act.h>
74 #endif
75 #if HAVE_MACH_VM_REGION_H
76 #include <mach/vm_region.h>
77 #endif
78 #if HAVE_MACH_VM_MAP_H
79 #include <mach/vm_map.h>
80 #endif
81 #if HAVE_MACH_VM_PROT_H
82 #include <mach/vm_prot.h>
83 #endif
84 #if HAVE_SYS_SYSCTL_H
85 #include <sys/sysctl.h>
86 #endif
87 /* #endif HAVE_THREAD_INFO */
89 #elif KERNEL_LINUX
90 #if HAVE_LINUX_CONFIG_H
91 #include <linux/config.h>
92 #endif
93 #ifndef CONFIG_HZ
94 #define CONFIG_HZ 100
95 #endif
96 /* #endif KERNEL_LINUX */
98 #elif HAVE_LIBKVM_GETPROCS && \
99 (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD)
100 #include <kvm.h>
101 #include <sys/param.h>
102 #include <sys/proc.h>
103 #include <sys/sysctl.h>
104 #include <sys/user.h>
105 /* #endif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD ||
106 * HAVE_STRUCT_KINFO_PROC_OPENBSD) */
108 #elif HAVE_PROCINFO_H
109 #include <procinfo.h>
110 #include <sys/types.h>
112 #define MAXPROCENTRY 32
113 #define MAXTHRDENTRY 16
114 #define MAXARGLN 1024
115 /* #endif HAVE_PROCINFO_H */
117 #elif KERNEL_SOLARIS
118 /* Hack: Avoid #error when building a 32-bit binary with
119 * _FILE_OFFSET_BITS=64. There is a reason for this #error, as one
120 * of the structures in <sys/procfs.h> uses an off_t, but that
121 * isn't relevant to our usage of procfs. */
122 #if !defined(_LP64) && _FILE_OFFSET_BITS == 64
123 #define SAVE_FOB_64
124 #undef _FILE_OFFSET_BITS
125 #endif
127 #include <procfs.h>
129 #ifdef SAVE_FOB_64
130 #define _FILE_OFFSET_BITS 64
131 #undef SAVE_FOB_64
132 #endif
134 #include <dirent.h>
135 #include <sys/user.h>
137 #ifndef MAXCOMLEN
138 #define MAXCOMLEN 16
139 #endif
141 /* #endif KERNEL_SOLARIS */
143 #else
144 #error "No applicable input method."
145 #endif
147 #if HAVE_REGEX_H
148 #include <regex.h>
149 #endif
151 #if HAVE_KSTAT_H
152 #include <kstat.h>
153 #endif
155 #ifndef CMDLINE_BUFFER_SIZE
156 #if defined(ARG_MAX) && (ARG_MAX < 4096)
157 #define CMDLINE_BUFFER_SIZE ARG_MAX
158 #else
159 #define CMDLINE_BUFFER_SIZE 4096
160 #endif
161 #endif
163 #define PROCSTAT_NAME_LEN 256
164 typedef struct process_entry_s {
165 unsigned long id;
166 char name[PROCSTAT_NAME_LEN];
168 unsigned long num_proc;
169 unsigned long num_lwp;
170 unsigned long num_fd;
171 unsigned long vmem_size;
172 unsigned long vmem_rss;
173 unsigned long vmem_data;
174 unsigned long vmem_code;
175 unsigned long stack_size;
177 derive_t vmem_minflt_counter;
178 derive_t vmem_majflt_counter;
180 derive_t cpu_user_counter;
181 derive_t cpu_system_counter;
183 /* io data */
184 derive_t io_rchar;
185 derive_t io_wchar;
186 derive_t io_syscr;
187 derive_t io_syscw;
188 derive_t io_diskr;
189 derive_t io_diskw;
190 _Bool has_io;
192 derive_t cswitch_vol;
193 derive_t cswitch_invol;
194 _Bool has_cswitch;
196 _Bool has_fd;
197 } process_entry_t;
199 typedef struct procstat_entry_s {
200 unsigned long id;
201 unsigned long age;
203 derive_t vmem_minflt_counter;
204 derive_t vmem_majflt_counter;
206 derive_t cpu_user_counter;
207 derive_t cpu_system_counter;
209 /* io data */
210 derive_t io_rchar;
211 derive_t io_wchar;
212 derive_t io_syscr;
213 derive_t io_syscw;
214 derive_t io_diskr;
215 derive_t io_diskw;
217 derive_t cswitch_vol;
218 derive_t cswitch_invol;
220 struct procstat_entry_s *next;
221 } procstat_entry_t;
223 typedef struct procstat {
224 char name[PROCSTAT_NAME_LEN];
225 #if HAVE_REGEX_H
226 regex_t *re;
227 #endif
229 unsigned long num_proc;
230 unsigned long num_lwp;
231 unsigned long num_fd;
232 unsigned long vmem_size;
233 unsigned long vmem_rss;
234 unsigned long vmem_data;
235 unsigned long vmem_code;
236 unsigned long stack_size;
238 derive_t vmem_minflt_counter;
239 derive_t vmem_majflt_counter;
241 derive_t cpu_user_counter;
242 derive_t cpu_system_counter;
244 /* io data */
245 derive_t io_rchar;
246 derive_t io_wchar;
247 derive_t io_syscr;
248 derive_t io_syscw;
249 derive_t io_diskr;
250 derive_t io_diskw;
252 derive_t cswitch_vol;
253 derive_t cswitch_invol;
255 _Bool report_fd_num;
256 _Bool report_ctx_switch;
258 struct procstat *next;
259 struct procstat_entry_s *instances;
260 } procstat_t;
262 static procstat_t *list_head_g = NULL;
264 static _Bool want_init = 1;
265 static _Bool report_ctx_switch = 0;
266 static _Bool report_fd_num = 0;
268 #if HAVE_THREAD_INFO
269 static mach_port_t port_host_self;
270 static mach_port_t port_task_self;
272 static processor_set_name_array_t pset_list;
273 static mach_msg_type_number_t pset_list_len;
274 /* #endif HAVE_THREAD_INFO */
276 #elif KERNEL_LINUX
277 static long pagesize_g;
278 static void ps_fill_details(const procstat_t *ps, process_entry_t *entry);
279 /* #endif KERNEL_LINUX */
281 #elif HAVE_LIBKVM_GETPROCS && \
282 (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD)
283 static int pagesize;
284 /* #endif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD ||
285 * HAVE_STRUCT_KINFO_PROC_OPENBSD) */
287 #elif HAVE_PROCINFO_H
288 static struct procentry64 procentry[MAXPROCENTRY];
289 static struct thrdentry64 thrdentry[MAXTHRDENTRY];
290 static int pagesize;
292 #ifndef _AIXVERSION_610
293 int getprocs64(void *procsinfo, int sizproc, void *fdsinfo, int sizfd,
294 pid_t *index, int count);
295 int getthrds64(pid_t, void *, int, tid64_t *, int);
296 #endif
297 int getargs(void *processBuffer, int bufferLen, char *argsBuffer, int argsLen);
298 #endif /* HAVE_PROCINFO_H */
300 /* put name of process from config to list_head_g tree
301 * list_head_g is a list of 'procstat_t' structs with
302 * processes names we want to watch */
303 static procstat_t *ps_list_register(const char *name, const char *regexp) {
304 procstat_t *new;
305 procstat_t *ptr;
306 int status;
308 new = calloc(1, sizeof(*new));
309 if (new == NULL) {
310 ERROR("processes plugin: ps_list_register: calloc failed.");
311 return NULL;
312 }
313 sstrncpy(new->name, name, sizeof(new->name));
315 new->io_rchar = -1;
316 new->io_wchar = -1;
317 new->io_syscr = -1;
318 new->io_syscw = -1;
319 new->io_diskr = -1;
320 new->io_diskw = -1;
321 new->cswitch_vol = -1;
322 new->cswitch_invol = -1;
324 new->report_fd_num = report_fd_num;
325 new->report_ctx_switch = report_ctx_switch;
327 #if HAVE_REGEX_H
328 if (regexp != NULL) {
329 DEBUG("ProcessMatch: adding \"%s\" as criteria to process %s.", regexp,
330 name);
331 new->re = malloc(sizeof(*new->re));
332 if (new->re == NULL) {
333 ERROR("processes plugin: ps_list_register: malloc failed.");
334 sfree(new);
335 return NULL;
336 }
338 status = regcomp(new->re, regexp, REG_EXTENDED | REG_NOSUB);
339 if (status != 0) {
340 DEBUG("ProcessMatch: compiling the regular expression \"%s\" failed.",
341 regexp);
342 sfree(new->re);
343 sfree(new);
344 return NULL;
345 }
346 }
347 #else
348 if (regexp != NULL) {
349 ERROR("processes plugin: ps_list_register: "
350 "Regular expression \"%s\" found in config "
351 "file, but support for regular expressions "
352 "has been disabled at compile time.",
353 regexp);
354 sfree(new);
355 return NULL;
356 }
357 #endif
359 for (ptr = list_head_g; ptr != NULL; ptr = ptr->next) {
360 if (strcmp(ptr->name, name) == 0) {
361 WARNING("processes plugin: You have configured more "
362 "than one `Process' or "
363 "`ProcessMatch' with the same name. "
364 "All but the first setting will be "
365 "ignored.");
366 #if HAVE_REGEX_H
367 sfree(new->re);
368 #endif
369 sfree(new);
370 return NULL;
371 }
373 if (ptr->next == NULL)
374 break;
375 }
377 if (ptr == NULL)
378 list_head_g = new;
379 else
380 ptr->next = new;
382 return new;
383 } /* void ps_list_register */
385 /* try to match name against entry, returns 1 if success */
386 static int ps_list_match(const char *name, const char *cmdline,
387 procstat_t *ps) {
388 #if HAVE_REGEX_H
389 if (ps->re != NULL) {
390 int status;
391 const char *str;
393 str = cmdline;
394 if ((str == NULL) || (str[0] == 0))
395 str = name;
397 assert(str != NULL);
399 status = regexec(ps->re, str,
400 /* nmatch = */ 0,
401 /* pmatch = */ NULL,
402 /* eflags = */ 0);
403 if (status == 0)
404 return 1;
405 } else
406 #endif
407 if (strcmp(ps->name, name) == 0)
408 return 1;
410 return 0;
411 } /* int ps_list_match */
413 static void ps_update_counter(derive_t *group_counter, derive_t *curr_counter,
414 derive_t new_counter) {
415 unsigned long curr_value;
417 if (want_init) {
418 *curr_counter = new_counter;
419 return;
420 }
422 if (new_counter < *curr_counter)
423 curr_value = new_counter + (ULONG_MAX - *curr_counter);
424 else
425 curr_value = new_counter - *curr_counter;
427 if (*group_counter == -1)
428 *group_counter = 0;
430 *curr_counter = new_counter;
431 *group_counter += curr_value;
432 }
434 /* add process entry to 'instances' of process 'name' (or refresh it) */
435 static void ps_list_add(const char *name, const char *cmdline,
436 process_entry_t *entry) {
437 procstat_entry_t *pse;
439 if (entry->id == 0)
440 return;
442 for (procstat_t *ps = list_head_g; ps != NULL; ps = ps->next) {
443 if ((ps_list_match(name, cmdline, ps)) == 0)
444 continue;
446 #if KERNEL_LINUX
447 ps_fill_details(ps, entry);
448 #endif
450 for (pse = ps->instances; pse != NULL; pse = pse->next)
451 if ((pse->id == entry->id) || (pse->next == NULL))
452 break;
454 if ((pse == NULL) || (pse->id != entry->id)) {
455 procstat_entry_t *new;
457 new = calloc(1, sizeof(*new));
458 if (new == NULL)
459 return;
460 new->id = entry->id;
462 if (pse == NULL)
463 ps->instances = new;
464 else
465 pse->next = new;
467 pse = new;
468 }
470 pse->age = 0;
472 ps->num_proc += entry->num_proc;
473 ps->num_lwp += entry->num_lwp;
474 ps->num_fd += entry->num_fd;
475 ps->vmem_size += entry->vmem_size;
476 ps->vmem_rss += entry->vmem_rss;
477 ps->vmem_data += entry->vmem_data;
478 ps->vmem_code += entry->vmem_code;
479 ps->stack_size += entry->stack_size;
481 if ((entry->io_rchar != -1) && (entry->io_wchar != -1)) {
482 ps_update_counter(&ps->io_rchar, &pse->io_rchar, entry->io_rchar);
483 ps_update_counter(&ps->io_wchar, &pse->io_wchar, entry->io_wchar);
484 }
486 if ((entry->io_syscr != -1) && (entry->io_syscw != -1)) {
487 ps_update_counter(&ps->io_syscr, &pse->io_syscr, entry->io_syscr);
488 ps_update_counter(&ps->io_syscw, &pse->io_syscw, entry->io_syscw);
489 }
491 if ((entry->io_diskr != -1) && (entry->io_diskw != -1)) {
492 ps_update_counter(&ps->io_diskr, &pse->io_diskr, entry->io_diskr);
493 ps_update_counter(&ps->io_diskw, &pse->io_diskw, entry->io_diskw);
494 }
496 if ((entry->cswitch_vol != -1) && (entry->cswitch_vol != -1)) {
497 ps_update_counter(&ps->cswitch_vol, &pse->cswitch_vol,
498 entry->cswitch_vol);
499 ps_update_counter(&ps->cswitch_invol, &pse->cswitch_invol,
500 entry->cswitch_invol);
501 }
503 ps_update_counter(&ps->vmem_minflt_counter, &pse->vmem_minflt_counter,
504 entry->vmem_minflt_counter);
505 ps_update_counter(&ps->vmem_majflt_counter, &pse->vmem_majflt_counter,
506 entry->vmem_majflt_counter);
508 ps_update_counter(&ps->cpu_user_counter, &pse->cpu_user_counter,
509 entry->cpu_user_counter);
510 ps_update_counter(&ps->cpu_system_counter, &pse->cpu_system_counter,
511 entry->cpu_system_counter);
512 }
513 }
515 /* remove old entries from instances of processes in list_head_g */
516 static void ps_list_reset(void) {
517 procstat_entry_t *pse;
518 procstat_entry_t *pse_prev;
520 for (procstat_t *ps = list_head_g; ps != NULL; ps = ps->next) {
521 ps->num_proc = 0;
522 ps->num_lwp = 0;
523 ps->num_fd = 0;
524 ps->vmem_size = 0;
525 ps->vmem_rss = 0;
526 ps->vmem_data = 0;
527 ps->vmem_code = 0;
528 ps->stack_size = 0;
530 pse_prev = NULL;
531 pse = ps->instances;
532 while (pse != NULL) {
533 if (pse->age > 10) {
534 DEBUG("Removing this procstat entry cause it's too old: "
535 "id = %lu; name = %s;",
536 pse->id, ps->name);
538 if (pse_prev == NULL) {
539 ps->instances = pse->next;
540 free(pse);
541 pse = ps->instances;
542 } else {
543 pse_prev->next = pse->next;
544 free(pse);
545 pse = pse_prev->next;
546 }
547 } else {
548 pse->age++;
549 pse_prev = pse;
550 pse = pse->next;
551 }
552 } /* while (pse != NULL) */
553 } /* for (ps = list_head_g; ps != NULL; ps = ps->next) */
554 }
556 static void ps_tune_instance(oconfig_item_t *ci, procstat_t *ps) {
557 for (int i = 0; i < ci->children_num; i++) {
558 oconfig_item_t *c = ci->children + i;
560 if (strcasecmp(c->key, "CollectContextSwitch") == 0)
561 cf_util_get_boolean(c, &ps->report_ctx_switch);
562 else if (strcasecmp(c->key, "CollectFileDescriptor") == 0)
563 cf_util_get_boolean(c, &ps->report_fd_num);
564 else {
565 ERROR("processes plugin: Option `%s' not allowed here.", c->key);
566 }
567 } /* for (ci->children) */
568 } /* void ps_tune_instance */
570 /* put all pre-defined 'Process' names from config to list_head_g tree */
571 static int ps_config(oconfig_item_t *ci) {
572 #if KERNEL_LINUX
573 const size_t max_procname_len = 15;
574 #elif KERNEL_SOLARIS || KERNEL_FREEBSD
575 const size_t max_procname_len = MAXCOMLEN - 1;
576 #endif
578 procstat_t *ps;
580 for (int i = 0; i < ci->children_num; ++i) {
581 oconfig_item_t *c = ci->children + i;
583 if (strcasecmp(c->key, "Process") == 0) {
584 if ((c->values_num != 1) || (OCONFIG_TYPE_STRING != c->values[0].type)) {
585 ERROR("processes plugin: `Process' expects exactly "
586 "one string argument (got %i).",
587 c->values_num);
588 continue;
589 }
591 #if KERNEL_LINUX || KERNEL_SOLARIS || KERNEL_FREEBSD
592 if (strlen(c->values[0].value.string) > max_procname_len) {
593 WARNING("processes plugin: this platform has a %zu character limit "
594 "to process names. The `Process \"%s\"' option will "
595 "not work as expected.",
596 max_procname_len, c->values[0].value.string);
597 }
598 #endif
600 ps = ps_list_register(c->values[0].value.string, NULL);
602 if (c->children_num != 0 && ps != NULL)
603 ps_tune_instance(c, ps);
604 } else if (strcasecmp(c->key, "ProcessMatch") == 0) {
605 if ((c->values_num != 2) || (OCONFIG_TYPE_STRING != c->values[0].type) ||
606 (OCONFIG_TYPE_STRING != c->values[1].type)) {
607 ERROR("processes plugin: `ProcessMatch' needs exactly "
608 "two string arguments (got %i).",
609 c->values_num);
610 continue;
611 }
613 ps = ps_list_register(c->values[0].value.string,
614 c->values[1].value.string);
616 if (c->children_num != 0 && ps != NULL)
617 ps_tune_instance(c, ps);
618 } else if (strcasecmp(c->key, "CollectContextSwitch") == 0) {
619 cf_util_get_boolean(c, &report_ctx_switch);
620 } else if (strcasecmp(c->key, "CollectFileDescriptor") == 0) {
621 cf_util_get_boolean(c, &report_fd_num);
622 } else {
623 ERROR("processes plugin: The `%s' configuration option is not "
624 "understood and will be ignored.",
625 c->key);
626 continue;
627 }
628 }
630 return 0;
631 }
633 static int ps_init(void) {
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 vm_deallocate(port_task_self, (vm_address_t)pset_list,
642 pset_list_len * sizeof(processor_set_t));
643 pset_list = NULL;
644 pset_list_len = 0;
645 }
647 if ((status = host_processor_sets(port_host_self, &pset_list,
648 &pset_list_len)) != KERN_SUCCESS) {
649 ERROR("host_processor_sets failed: %s\n", mach_error_string(status));
650 pset_list = NULL;
651 pset_list_len = 0;
652 return -1;
653 }
654 /* #endif HAVE_THREAD_INFO */
656 #elif KERNEL_LINUX
657 pagesize_g = sysconf(_SC_PAGESIZE);
658 DEBUG("pagesize_g = %li; CONFIG_HZ = %i;", pagesize_g, CONFIG_HZ);
659 /* #endif KERNEL_LINUX */
661 #elif HAVE_LIBKVM_GETPROCS && \
662 (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD)
663 pagesize = getpagesize();
664 /* #endif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD ||
665 * HAVE_STRUCT_KINFO_PROC_OPENBSD) */
667 #elif HAVE_PROCINFO_H
668 pagesize = getpagesize();
669 #endif /* HAVE_PROCINFO_H */
671 return 0;
672 } /* int ps_init */
674 /* submit global state (e.g.: qty of zombies, running, etc..) */
675 static void ps_submit_state(const char *state, double value) {
676 value_list_t vl = VALUE_LIST_INIT;
678 vl.values = &(value_t){.gauge = value};
679 vl.values_len = 1;
680 sstrncpy(vl.plugin, "processes", sizeof(vl.plugin));
681 sstrncpy(vl.plugin_instance, "", sizeof(vl.plugin_instance));
682 sstrncpy(vl.type, "ps_state", sizeof(vl.type));
683 sstrncpy(vl.type_instance, state, sizeof(vl.type_instance));
685 plugin_dispatch_values(&vl);
686 }
688 /* submit info about specific process (e.g.: memory taken, cpu usage, etc..) */
689 static void ps_submit_proc_list(procstat_t *ps) {
690 value_list_t vl = VALUE_LIST_INIT;
691 value_t values[2];
693 vl.values = values;
694 sstrncpy(vl.plugin, "processes", sizeof(vl.plugin));
695 sstrncpy(vl.plugin_instance, ps->name, sizeof(vl.plugin_instance));
697 sstrncpy(vl.type, "ps_vm", sizeof(vl.type));
698 vl.values[0].gauge = ps->vmem_size;
699 vl.values_len = 1;
700 plugin_dispatch_values(&vl);
702 sstrncpy(vl.type, "ps_rss", sizeof(vl.type));
703 vl.values[0].gauge = ps->vmem_rss;
704 vl.values_len = 1;
705 plugin_dispatch_values(&vl);
707 sstrncpy(vl.type, "ps_data", sizeof(vl.type));
708 vl.values[0].gauge = ps->vmem_data;
709 vl.values_len = 1;
710 plugin_dispatch_values(&vl);
712 sstrncpy(vl.type, "ps_code", sizeof(vl.type));
713 vl.values[0].gauge = ps->vmem_code;
714 vl.values_len = 1;
715 plugin_dispatch_values(&vl);
717 sstrncpy(vl.type, "ps_stacksize", sizeof(vl.type));
718 vl.values[0].gauge = ps->stack_size;
719 vl.values_len = 1;
720 plugin_dispatch_values(&vl);
722 sstrncpy(vl.type, "ps_cputime", sizeof(vl.type));
723 vl.values[0].derive = ps->cpu_user_counter;
724 vl.values[1].derive = ps->cpu_system_counter;
725 vl.values_len = 2;
726 plugin_dispatch_values(&vl);
728 sstrncpy(vl.type, "ps_count", sizeof(vl.type));
729 vl.values[0].gauge = ps->num_proc;
730 vl.values[1].gauge = ps->num_lwp;
731 vl.values_len = 2;
732 plugin_dispatch_values(&vl);
734 sstrncpy(vl.type, "ps_pagefaults", sizeof(vl.type));
735 vl.values[0].derive = ps->vmem_minflt_counter;
736 vl.values[1].derive = ps->vmem_majflt_counter;
737 vl.values_len = 2;
738 plugin_dispatch_values(&vl);
740 if ((ps->io_rchar != -1) && (ps->io_wchar != -1)) {
741 sstrncpy(vl.type, "io_octets", sizeof(vl.type));
742 vl.values[0].derive = ps->io_rchar;
743 vl.values[1].derive = ps->io_wchar;
744 vl.values_len = 2;
745 plugin_dispatch_values(&vl);
746 }
748 if ((ps->io_syscr != -1) && (ps->io_syscw != -1)) {
749 sstrncpy(vl.type, "io_ops", sizeof(vl.type));
750 vl.values[0].derive = ps->io_syscr;
751 vl.values[1].derive = ps->io_syscw;
752 vl.values_len = 2;
753 plugin_dispatch_values(&vl);
754 }
756 if ((ps->io_diskr != -1) && (ps->io_diskw != -1)) {
757 sstrncpy(vl.type, "disk_octets", sizeof(vl.type));
758 vl.values[0].derive = ps->io_diskr;
759 vl.values[1].derive = ps->io_diskw;
760 vl.values_len = 2;
761 plugin_dispatch_values(&vl);
762 }
764 if (ps->num_fd > 0) {
765 sstrncpy(vl.type, "file_handles", sizeof(vl.type));
766 vl.values[0].gauge = ps->num_fd;
767 vl.values_len = 1;
768 plugin_dispatch_values(&vl);
769 }
771 if ((ps->cswitch_vol != -1) && (ps->cswitch_invol != -1)) {
772 sstrncpy(vl.type, "contextswitch", sizeof(vl.type));
773 sstrncpy(vl.type_instance, "voluntary", sizeof(vl.type_instance));
774 vl.values[0].derive = ps->cswitch_vol;
775 vl.values_len = 1;
776 plugin_dispatch_values(&vl);
778 sstrncpy(vl.type, "contextswitch", sizeof(vl.type));
779 sstrncpy(vl.type_instance, "involuntary", sizeof(vl.type_instance));
780 vl.values[0].derive = ps->cswitch_invol;
781 vl.values_len = 1;
782 plugin_dispatch_values(&vl);
783 }
785 DEBUG("name = %s; num_proc = %lu; num_lwp = %lu; num_fd = %lu; "
786 "vmem_size = %lu; vmem_rss = %lu; vmem_data = %lu; "
787 "vmem_code = %lu; "
788 "vmem_minflt_counter = %" PRIi64 "; vmem_majflt_counter = %" PRIi64 "; "
789 "cpu_user_counter = %" PRIi64 "; cpu_system_counter = %" PRIi64 "; "
790 "io_rchar = %" PRIi64 "; io_wchar = %" PRIi64 "; "
791 "io_syscr = %" PRIi64 "; io_syscw = %" PRIi64 "; "
792 "io_diskr = %" PRIi64 "; io_diskw = %" PRIi64 "; "
793 "cswitch_vol = %" PRIi64 "; cswitch_invol = %" PRIi64 ";",
794 ps->name, ps->num_proc, ps->num_lwp, ps->num_fd, ps->vmem_size,
795 ps->vmem_rss, ps->vmem_data, ps->vmem_code, ps->vmem_minflt_counter,
796 ps->vmem_majflt_counter, ps->cpu_user_counter, ps->cpu_system_counter,
797 ps->io_rchar, ps->io_wchar, ps->io_syscr, ps->io_syscw, ps->io_diskr,
798 ps->io_diskw, ps->cswitch_vol, ps->cswitch_invol);
799 } /* void ps_submit_proc_list */
801 #if KERNEL_LINUX || KERNEL_SOLARIS
802 static void ps_submit_fork_rate(derive_t value) {
803 value_list_t vl = VALUE_LIST_INIT;
805 vl.values = &(value_t){.derive = value};
806 vl.values_len = 1;
807 sstrncpy(vl.plugin, "processes", sizeof(vl.plugin));
808 sstrncpy(vl.plugin_instance, "", sizeof(vl.plugin_instance));
809 sstrncpy(vl.type, "fork_rate", sizeof(vl.type));
810 sstrncpy(vl.type_instance, "", sizeof(vl.type_instance));
812 plugin_dispatch_values(&vl);
813 }
814 #endif /* KERNEL_LINUX || KERNEL_SOLARIS*/
816 /* ------- additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
817 #if KERNEL_LINUX
818 static int ps_read_tasks_status(process_entry_t *ps) {
819 char dirname[64];
820 DIR *dh;
821 char filename[64];
822 FILE *fh;
823 struct dirent *ent;
824 derive_t cswitch_vol = 0;
825 derive_t cswitch_invol = 0;
826 char buffer[1024];
827 char *fields[8];
828 int numfields;
830 snprintf(dirname, sizeof(dirname), "/proc/%li/task", ps->id);
832 if ((dh = opendir(dirname)) == NULL) {
833 DEBUG("Failed to open directory `%s'", dirname);
834 return -1;
835 }
837 while ((ent = readdir(dh)) != NULL) {
838 char *tpid;
840 if (!isdigit((int)ent->d_name[0]))
841 continue;
843 tpid = ent->d_name;
845 if (snprintf(filename, sizeof(filename), "/proc/%li/task/%s/status", ps->id,
846 tpid) >= sizeof(filename)) {
847 DEBUG("Filename too long: `%s'", filename);
848 continue;
849 }
851 if ((fh = fopen(filename, "r")) == NULL) {
852 DEBUG("Failed to open file `%s'", filename);
853 continue;
854 }
856 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
857 derive_t tmp;
858 char *endptr;
860 if (strncmp(buffer, "voluntary_ctxt_switches", 23) != 0 &&
861 strncmp(buffer, "nonvoluntary_ctxt_switches", 26) != 0)
862 continue;
864 numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
866 if (numfields < 2)
867 continue;
869 errno = 0;
870 endptr = NULL;
871 tmp = (derive_t)strtoll(fields[1], &endptr, /* base = */ 10);
872 if ((errno == 0) && (endptr != fields[1])) {
873 if (strncmp(buffer, "voluntary_ctxt_switches", 23) == 0) {
874 cswitch_vol += tmp;
875 } else if (strncmp(buffer, "nonvoluntary_ctxt_switches", 26) == 0) {
876 cswitch_invol += tmp;
877 }
878 }
879 } /* while (fgets) */
881 if (fclose(fh)) {
882 char errbuf[1024];
883 WARNING("processes: fclose: %s",
884 sstrerror(errno, errbuf, sizeof(errbuf)));
885 }
886 }
887 closedir(dh);
889 ps->cswitch_vol = cswitch_vol;
890 ps->cswitch_invol = cswitch_invol;
892 return 0;
893 } /* int *ps_read_tasks_status */
895 /* Read data from /proc/pid/status */
896 static int ps_read_status(long pid, process_entry_t *ps) {
897 FILE *fh;
898 char buffer[1024];
899 char filename[64];
900 unsigned long lib = 0;
901 unsigned long exe = 0;
902 unsigned long data = 0;
903 unsigned long threads = 0;
904 char *fields[8];
905 int numfields;
907 snprintf(filename, sizeof(filename), "/proc/%li/status", pid);
908 if ((fh = fopen(filename, "r")) == NULL)
909 return -1;
911 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
912 unsigned long tmp;
913 char *endptr;
915 if (strncmp(buffer, "Vm", 2) != 0 && strncmp(buffer, "Threads", 7) != 0)
916 continue;
918 numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
920 if (numfields < 2)
921 continue;
923 errno = 0;
924 endptr = NULL;
925 tmp = strtoul(fields[1], &endptr, /* base = */ 10);
926 if ((errno == 0) && (endptr != fields[1])) {
927 if (strncmp(buffer, "VmData", 6) == 0) {
928 data = tmp;
929 } else if (strncmp(buffer, "VmLib", 5) == 0) {
930 lib = tmp;
931 } else if (strncmp(buffer, "VmExe", 5) == 0) {
932 exe = tmp;
933 } else if (strncmp(buffer, "Threads", 7) == 0) {
934 threads = tmp;
935 }
936 }
937 } /* while (fgets) */
939 if (fclose(fh)) {
940 char errbuf[1024];
941 WARNING("processes: fclose: %s", sstrerror(errno, errbuf, sizeof(errbuf)));
942 }
944 ps->vmem_data = data * 1024;
945 ps->vmem_code = (exe + lib) * 1024;
946 if (threads != 0)
947 ps->num_lwp = threads;
949 return 0;
950 } /* int *ps_read_status */
952 static int ps_read_io(process_entry_t *ps) {
953 FILE *fh;
954 char buffer[1024];
955 char filename[64];
957 char *fields[8];
958 int numfields;
960 snprintf(filename, sizeof(filename), "/proc/%li/io", ps->id);
961 if ((fh = fopen(filename, "r")) == NULL) {
962 DEBUG("ps_read_io: Failed to open file `%s'", filename);
963 return -1;
964 }
966 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
967 derive_t *val = NULL;
968 long long tmp;
969 char *endptr;
971 if (strncasecmp(buffer, "rchar:", 6) == 0)
972 val = &(ps->io_rchar);
973 else if (strncasecmp(buffer, "wchar:", 6) == 0)
974 val = &(ps->io_wchar);
975 else if (strncasecmp(buffer, "syscr:", 6) == 0)
976 val = &(ps->io_syscr);
977 else if (strncasecmp(buffer, "syscw:", 6) == 0)
978 val = &(ps->io_syscw);
979 else if (strncasecmp(buffer, "read_bytes:", 11) == 0)
980 val = &(ps->io_diskr);
981 else if (strncasecmp(buffer, "write_bytes:", 12) == 0)
982 val = &(ps->io_diskw);
983 else
984 continue;
986 numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
988 if (numfields < 2)
989 continue;
991 errno = 0;
992 endptr = NULL;
993 tmp = strtoll(fields[1], &endptr, /* base = */ 10);
994 if ((errno != 0) || (endptr == fields[1]))
995 *val = -1;
996 else
997 *val = (derive_t)tmp;
998 } /* while (fgets) */
1000 if (fclose(fh)) {
1001 char errbuf[1024];
1002 WARNING("processes: fclose: %s", sstrerror(errno, errbuf, sizeof(errbuf)));
1003 }
1004 return 0;
1005 } /* int ps_read_io (...) */
1007 static int ps_count_fd(int pid) {
1008 char dirname[64];
1009 DIR *dh;
1010 struct dirent *ent;
1011 int count = 0;
1013 snprintf(dirname, sizeof(dirname), "/proc/%i/fd", pid);
1015 if ((dh = opendir(dirname)) == NULL) {
1016 DEBUG("Failed to open directory `%s'", dirname);
1017 return -1;
1018 }
1019 while ((ent = readdir(dh)) != NULL) {
1020 if (!isdigit((int)ent->d_name[0]))
1021 continue;
1022 else
1023 count++;
1024 }
1025 closedir(dh);
1027 return (count >= 1) ? count : 1;
1028 } /* int ps_count_fd (pid) */
1030 static void ps_fill_details(const procstat_t *ps, process_entry_t *entry) {
1031 if (entry->has_io == 0) {
1032 ps_read_io(entry);
1033 entry->has_io = 1;
1034 }
1036 if (ps->report_ctx_switch) {
1037 if (entry->has_cswitch == 0) {
1038 ps_read_tasks_status(entry);
1039 entry->has_cswitch = 1;
1040 }
1041 }
1043 if (ps->report_fd_num) {
1044 int num_fd;
1045 if (entry->has_fd == 0 && (num_fd = ps_count_fd(entry->id)) > 0) {
1046 entry->num_fd = num_fd;
1047 }
1048 entry->has_fd = 1;
1049 }
1050 } /* void ps_fill_details (...) */
1052 static int ps_read_process(long pid, process_entry_t *ps, char *state) {
1053 char filename[64];
1054 char buffer[1024];
1056 char *fields[64];
1057 char fields_len;
1059 size_t buffer_len;
1061 char *buffer_ptr;
1062 size_t name_start_pos;
1063 size_t name_end_pos;
1064 size_t name_len;
1066 derive_t cpu_user_counter;
1067 derive_t cpu_system_counter;
1068 long long unsigned vmem_size;
1069 long long unsigned vmem_rss;
1070 long long unsigned stack_size;
1072 ssize_t status;
1074 snprintf(filename, sizeof(filename), "/proc/%li/stat", pid);
1076 status = read_file_contents(filename, buffer, sizeof(buffer) - 1);
1077 if (status <= 0)
1078 return -1;
1079 buffer_len = (size_t)status;
1080 buffer[buffer_len] = 0;
1082 /* The name of the process is enclosed in parens. Since the name can
1083 * contain parens itself, spaces, numbers and pretty much everything
1084 * else, use these to determine the process name. We don't use
1085 * strchr(3) and strrchr(3) to avoid pointer arithmetic which would
1086 * otherwise be required to determine name_len. */
1087 name_start_pos = 0;
1088 while (name_start_pos < buffer_len && buffer[name_start_pos] != '(')
1089 name_start_pos++;
1091 name_end_pos = buffer_len;
1092 while (name_end_pos > 0 && buffer[name_end_pos] != ')')
1093 name_end_pos--;
1095 /* Either '(' or ')' is not found or they are in the wrong order.
1096 * Anyway, something weird that shouldn't happen ever. */
1097 if (name_start_pos >= name_end_pos) {
1098 ERROR("processes plugin: name_start_pos = %zu >= name_end_pos = %zu",
1099 name_start_pos, name_end_pos);
1100 return -1;
1101 }
1103 name_len = (name_end_pos - name_start_pos) - 1;
1104 if (name_len >= sizeof(ps->name))
1105 name_len = sizeof(ps->name) - 1;
1107 sstrncpy(ps->name, &buffer[name_start_pos + 1], name_len + 1);
1109 if ((buffer_len - name_end_pos) < 2)
1110 return -1;
1111 buffer_ptr = &buffer[name_end_pos + 2];
1113 fields_len = strsplit(buffer_ptr, fields, STATIC_ARRAY_SIZE(fields));
1114 if (fields_len < 22) {
1115 DEBUG("processes plugin: ps_read_process (pid = %li):"
1116 " `%s' has only %i fields..",
1117 pid, filename, fields_len);
1118 return -1;
1119 }
1121 *state = fields[0][0];
1123 if (*state == 'Z') {
1124 ps->num_lwp = 0;
1125 ps->num_proc = 0;
1126 } else {
1127 ps->num_lwp = strtoul(fields[17], /* endptr = */ NULL, /* base = */ 10);
1128 if ((ps_read_status(pid, ps)) != 0) {
1129 /* No VMem data */
1130 ps->vmem_data = -1;
1131 ps->vmem_code = -1;
1132 DEBUG("ps_read_process: did not get vmem data for pid %li", pid);
1133 }
1134 if (ps->num_lwp == 0)
1135 ps->num_lwp = 1;
1136 ps->num_proc = 1;
1137 }
1139 /* Leave the rest at zero if this is only a zombi */
1140 if (ps->num_proc == 0) {
1141 DEBUG("processes plugin: This is only a zombie: pid = %li; "
1142 "name = %s;",
1143 pid, ps->name);
1144 return 0;
1145 }
1147 cpu_user_counter = atoll(fields[11]);
1148 cpu_system_counter = atoll(fields[12]);
1149 vmem_size = atoll(fields[20]);
1150 vmem_rss = atoll(fields[21]);
1151 ps->vmem_minflt_counter = atol(fields[7]);
1152 ps->vmem_majflt_counter = atol(fields[9]);
1154 {
1155 unsigned long long stack_start = atoll(fields[25]);
1156 unsigned long long stack_ptr = atoll(fields[26]);
1158 stack_size = (stack_start > stack_ptr) ? stack_start - stack_ptr
1159 : stack_ptr - stack_start;
1160 }
1162 /* Convert jiffies to useconds */
1163 cpu_user_counter = cpu_user_counter * 1000000 / CONFIG_HZ;
1164 cpu_system_counter = cpu_system_counter * 1000000 / CONFIG_HZ;
1165 vmem_rss = vmem_rss * pagesize_g;
1167 ps->cpu_user_counter = cpu_user_counter;
1168 ps->cpu_system_counter = cpu_system_counter;
1169 ps->vmem_size = (unsigned long)vmem_size;
1170 ps->vmem_rss = (unsigned long)vmem_rss;
1171 ps->stack_size = (unsigned long)stack_size;
1173 /* no data by default. May be filled by ps_fill_details () */
1174 ps->io_rchar = -1;
1175 ps->io_wchar = -1;
1176 ps->io_syscr = -1;
1177 ps->io_syscw = -1;
1178 ps->io_diskr = -1;
1179 ps->io_diskw = -1;
1181 ps->cswitch_vol = -1;
1182 ps->cswitch_invol = -1;
1184 /* success */
1185 return 0;
1186 } /* int ps_read_process (...) */
1188 static char *ps_get_cmdline(long pid, char *name, char *buf, size_t buf_len) {
1189 char *buf_ptr;
1190 size_t len;
1192 char file[PATH_MAX];
1193 int fd;
1195 size_t n;
1197 if ((pid < 1) || (NULL == buf) || (buf_len < 2))
1198 return NULL;
1200 snprintf(file, sizeof(file), "/proc/%li/cmdline", pid);
1202 errno = 0;
1203 fd = open(file, O_RDONLY);
1204 if (fd < 0) {
1205 char errbuf[4096];
1206 /* ENOENT means the process exited while we were handling it.
1207 * Don't complain about this, it only fills the logs. */
1208 if (errno != ENOENT)
1209 WARNING("processes plugin: Failed to open `%s': %s.", file,
1210 sstrerror(errno, errbuf, sizeof(errbuf)));
1211 return NULL;
1212 }
1214 buf_ptr = buf;
1215 len = buf_len;
1217 n = 0;
1219 while (42) {
1220 ssize_t status;
1222 status = read(fd, (void *)buf_ptr, len);
1224 if (status < 0) {
1225 char errbuf[1024];
1227 if ((EAGAIN == errno) || (EINTR == errno))
1228 continue;
1230 WARNING("processes plugin: Failed to read from `%s': %s.", file,
1231 sstrerror(errno, errbuf, sizeof(errbuf)));
1232 close(fd);
1233 return NULL;
1234 }
1236 n += status;
1238 if (status == 0)
1239 break;
1241 buf_ptr += status;
1242 len -= status;
1244 if (len == 0)
1245 break;
1246 }
1248 close(fd);
1250 if (0 == n) {
1251 /* cmdline not available; e.g. kernel thread, zombie */
1252 if (NULL == name)
1253 return NULL;
1255 snprintf(buf, buf_len, "[%s]", name);
1256 return buf;
1257 }
1259 assert(n <= buf_len);
1261 if (n == buf_len)
1262 --n;
1263 buf[n] = '\0';
1265 --n;
1266 /* remove trailing whitespace */
1267 while ((n > 0) && (isspace(buf[n]) || ('\0' == buf[n]))) {
1268 buf[n] = '\0';
1269 --n;
1270 }
1272 /* arguments are separated by '\0' in /proc/<pid>/cmdline */
1273 while (n > 0) {
1274 if ('\0' == buf[n])
1275 buf[n] = ' ';
1276 --n;
1277 }
1278 return buf;
1279 } /* char *ps_get_cmdline (...) */
1281 static int read_fork_rate(void) {
1282 FILE *proc_stat;
1283 char buffer[1024];
1284 value_t value;
1285 _Bool value_valid = 0;
1287 proc_stat = fopen("/proc/stat", "r");
1288 if (proc_stat == NULL) {
1289 char errbuf[1024];
1290 ERROR("processes plugin: fopen (/proc/stat) failed: %s",
1291 sstrerror(errno, errbuf, sizeof(errbuf)));
1292 return -1;
1293 }
1295 while (fgets(buffer, sizeof(buffer), proc_stat) != NULL) {
1296 int status;
1297 char *fields[3];
1298 int fields_num;
1300 fields_num = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
1301 if (fields_num != 2)
1302 continue;
1304 if (strcmp("processes", fields[0]) != 0)
1305 continue;
1307 status = parse_value(fields[1], &value, DS_TYPE_DERIVE);
1308 if (status == 0)
1309 value_valid = 1;
1311 break;
1312 }
1313 fclose(proc_stat);
1315 if (!value_valid)
1316 return -1;
1318 ps_submit_fork_rate(value.derive);
1319 return 0;
1320 }
1321 #endif /*KERNEL_LINUX */
1323 #if KERNEL_SOLARIS
1324 static char *ps_get_cmdline(long pid,
1325 char *name __attribute__((unused)), /* {{{ */
1326 char *buffer, size_t buffer_size) {
1327 char path[PATH_MAX];
1328 psinfo_t info;
1329 ssize_t status;
1331 snprintf(path, sizeof(path), "/proc/%li/psinfo", pid);
1333 status = read_file_contents(path, (void *)&info, sizeof(info));
1334 if ((status < 0) || (((size_t)status) != sizeof(info))) {
1335 ERROR("processes plugin: Unexpected return value "
1336 "while reading \"%s\": "
1337 "Returned %zd but expected %zu.",
1338 path, status, buffer_size);
1339 return NULL;
1340 }
1342 info.pr_psargs[sizeof(info.pr_psargs) - 1] = 0;
1343 sstrncpy(buffer, info.pr_psargs, buffer_size);
1345 return buffer;
1346 } /* }}} int ps_get_cmdline */
1348 /*
1349 * Reads process information on the Solaris OS. The information comes mainly
1350 * from
1351 * /proc/PID/status, /proc/PID/psinfo and /proc/PID/usage
1352 * The values for input and ouput chars are calculated "by hand"
1353 * Added a few "solaris" specific process states as well
1354 */
1355 static int ps_read_process(long pid, process_entry_t *ps, char *state) {
1356 char filename[64];
1357 char f_psinfo[64], f_usage[64];
1358 char *buffer;
1360 pstatus_t *myStatus;
1361 psinfo_t *myInfo;
1362 prusage_t *myUsage;
1364 snprintf(filename, sizeof(filename), "/proc/%li/status", pid);
1365 snprintf(f_psinfo, sizeof(f_psinfo), "/proc/%li/psinfo", pid);
1366 snprintf(f_usage, sizeof(f_usage), "/proc/%li/usage", pid);
1368 buffer = calloc(1, sizeof(pstatus_t));
1369 read_file_contents(filename, buffer, sizeof(pstatus_t));
1370 myStatus = (pstatus_t *)buffer;
1372 buffer = calloc(1, sizeof(psinfo_t));
1373 read_file_contents(f_psinfo, buffer, sizeof(psinfo_t));
1374 myInfo = (psinfo_t *)buffer;
1376 buffer = calloc(1, sizeof(prusage_t));
1377 read_file_contents(f_usage, buffer, sizeof(prusage_t));
1378 myUsage = (prusage_t *)buffer;
1380 sstrncpy(ps->name, myInfo->pr_fname, sizeof(myInfo->pr_fname));
1381 ps->num_lwp = myStatus->pr_nlwp;
1382 if (myInfo->pr_wstat != 0) {
1383 ps->num_proc = 0;
1384 ps->num_lwp = 0;
1385 *state = (char)'Z';
1387 sfree(myStatus);
1388 sfree(myInfo);
1389 sfree(myUsage);
1390 return 0;
1391 } else {
1392 ps->num_proc = 1;
1393 ps->num_lwp = myInfo->pr_nlwp;
1394 }
1396 /*
1397 * Convert system time and user time from nanoseconds to microseconds
1398 * for compatibility with the linux module
1399 */
1400 ps->cpu_system_counter = myStatus->pr_stime.tv_nsec / 1000;
1401 ps->cpu_user_counter = myStatus->pr_utime.tv_nsec / 1000;
1403 /*
1404 * Convert rssize from KB to bytes to be consistent w/ the linux module
1405 */
1406 ps->vmem_rss = myInfo->pr_rssize * 1024;
1407 ps->vmem_size = myInfo->pr_size * 1024;
1408 ps->vmem_minflt_counter = myUsage->pr_minf;
1409 ps->vmem_majflt_counter = myUsage->pr_majf;
1411 /*
1412 * TODO: Data and code segment calculations for Solaris
1413 */
1415 ps->vmem_data = -1;
1416 ps->vmem_code = -1;
1417 ps->stack_size = myStatus->pr_stksize;
1419 /*
1420 * TODO: File descriptor count for Solaris
1421 */
1422 ps->num_fd = 0;
1424 /*
1425 * Calculating input/ouput chars
1426 * Formula used is total chars / total blocks => chars/block
1427 * then convert input/output blocks to chars
1428 */
1429 ulong_t tot_chars = myUsage->pr_ioch;
1430 ulong_t tot_blocks = myUsage->pr_inblk + myUsage->pr_oublk;
1431 ulong_t chars_per_block = 1;
1432 if (tot_blocks != 0)
1433 chars_per_block = tot_chars / tot_blocks;
1434 ps->io_rchar = myUsage->pr_inblk * chars_per_block;
1435 ps->io_wchar = myUsage->pr_oublk * chars_per_block;
1436 ps->io_syscr = myUsage->pr_sysc;
1437 ps->io_syscw = myUsage->pr_sysc;
1438 ps->io_diskr = -1;
1439 ps->io_diskw = -1;
1441 /*
1442 * TODO: context switch counters for Solaris
1443 */
1444 ps->cswitch_vol = -1;
1445 ps->cswitch_invol = -1;
1447 /*
1448 * TODO: Find way of setting BLOCKED and PAGING status
1449 */
1451 *state = (char)'R';
1452 if (myStatus->pr_flags & PR_ASLEEP)
1453 *state = (char)'S';
1454 else if (myStatus->pr_flags & PR_STOPPED)
1455 *state = (char)'T';
1456 else if (myStatus->pr_flags & PR_DETACH)
1457 *state = (char)'E';
1458 else if (myStatus->pr_flags & PR_DAEMON)
1459 *state = (char)'A';
1460 else if (myStatus->pr_flags & PR_ISSYS)
1461 *state = (char)'Y';
1462 else if (myStatus->pr_flags & PR_ORPHAN)
1463 *state = (char)'O';
1465 sfree(myStatus);
1466 sfree(myInfo);
1467 sfree(myUsage);
1469 return 0;
1470 }
1472 /*
1473 * Reads the number of threads created since the last reboot. On Solaris these
1474 * are retrieved from kstat (module cpu, name sys, class misc, stat nthreads).
1475 * The result is the sum for all the threads created on each cpu
1476 */
1477 static int read_fork_rate(void) {
1478 extern kstat_ctl_t *kc;
1479 derive_t result = 0;
1481 if (kc == NULL)
1482 return -1;
1484 for (kstat_t *ksp_chain = kc->kc_chain; ksp_chain != NULL;
1485 ksp_chain = ksp_chain->ks_next) {
1486 if ((strcmp(ksp_chain->ks_module, "cpu") == 0) &&
1487 (strcmp(ksp_chain->ks_name, "sys") == 0) &&
1488 (strcmp(ksp_chain->ks_class, "misc") == 0)) {
1489 long long tmp;
1491 kstat_read(kc, ksp_chain, NULL);
1493 tmp = get_kstat_value(ksp_chain, "nthreads");
1494 if (tmp != -1LL)
1495 result += tmp;
1496 }
1497 }
1499 ps_submit_fork_rate(result);
1500 return 0;
1501 }
1502 #endif /* KERNEL_SOLARIS */
1504 #if HAVE_THREAD_INFO
1505 static int mach_get_task_name(task_t t, int *pid, char *name,
1506 size_t name_max_len) {
1507 int mib[4];
1509 struct kinfo_proc kp;
1510 size_t kp_size;
1512 mib[0] = CTL_KERN;
1513 mib[1] = KERN_PROC;
1514 mib[2] = KERN_PROC_PID;
1516 if (pid_for_task(t, pid) != KERN_SUCCESS)
1517 return -1;
1518 mib[3] = *pid;
1520 kp_size = sizeof(kp);
1521 if (sysctl(mib, 4, &kp, &kp_size, NULL, 0) != 0)
1522 return -1;
1524 if (name_max_len > (MAXCOMLEN + 1))
1525 name_max_len = MAXCOMLEN + 1;
1527 strncpy(name, kp.kp_proc.p_comm, name_max_len - 1);
1528 name[name_max_len - 1] = '\0';
1530 DEBUG("pid = %i; name = %s;", *pid, name);
1532 /* We don't do the special handling for `p_comm == "LaunchCFMApp"' as
1533 * `top' does it, because it is a lot of work and only used when
1534 * debugging. -octo */
1536 return 0;
1537 }
1538 #endif /* HAVE_THREAD_INFO */
1539 /* ------- end of additional functions for KERNEL_LINUX/HAVE_THREAD_INFO -------
1540 */
1542 /* do actual readings from kernel */
1543 static int ps_read(void) {
1544 #if HAVE_THREAD_INFO
1545 kern_return_t status;
1547 processor_set_t port_pset_priv;
1549 task_array_t task_list;
1550 mach_msg_type_number_t task_list_len;
1552 int task_pid;
1553 char task_name[MAXCOMLEN + 1];
1555 thread_act_array_t thread_list;
1556 mach_msg_type_number_t thread_list_len;
1557 thread_basic_info_data_t thread_data;
1558 mach_msg_type_number_t thread_data_len;
1560 int running = 0;
1561 int sleeping = 0;
1562 int zombies = 0;
1563 int stopped = 0;
1564 int blocked = 0;
1566 procstat_t *ps;
1567 process_entry_t pse;
1569 ps_list_reset();
1571 /*
1572 * The Mach-concept is a little different from the traditional UNIX
1573 * concept: All the work is done in threads. Threads are contained in
1574 * `tasks'. Therefore, `task status' doesn't make much sense, since
1575 * it's actually a `thread status'.
1576 * Tasks are assigned to sets of processors, so that's where you go to
1577 * get a list.
1578 */
1579 for (mach_msg_type_number_t pset = 0; pset < pset_list_len; pset++) {
1580 if ((status = host_processor_set_priv(port_host_self, pset_list[pset],
1581 &port_pset_priv)) != KERN_SUCCESS) {
1582 ERROR("host_processor_set_priv failed: %s\n", mach_error_string(status));
1583 continue;
1584 }
1586 if ((status = processor_set_tasks(port_pset_priv, &task_list,
1587 &task_list_len)) != KERN_SUCCESS) {
1588 ERROR("processor_set_tasks failed: %s\n", mach_error_string(status));
1589 mach_port_deallocate(port_task_self, port_pset_priv);
1590 continue;
1591 }
1593 for (mach_msg_type_number_t task = 0; task < task_list_len; task++) {
1594 ps = NULL;
1595 if (mach_get_task_name(task_list[task], &task_pid, task_name,
1596 PROCSTAT_NAME_LEN) == 0) {
1597 /* search for at least one match */
1598 for (ps = list_head_g; ps != NULL; ps = ps->next)
1599 /* FIXME: cmdline should be here instead of NULL */
1600 if (ps_list_match(task_name, NULL, ps) == 1)
1601 break;
1602 }
1604 /* Collect more detailed statistics for this process */
1605 if (ps != NULL) {
1606 task_basic_info_data_t task_basic_info;
1607 mach_msg_type_number_t task_basic_info_len;
1608 task_events_info_data_t task_events_info;
1609 mach_msg_type_number_t task_events_info_len;
1610 task_absolutetime_info_data_t task_absolutetime_info;
1611 mach_msg_type_number_t task_absolutetime_info_len;
1613 memset(&pse, '\0', sizeof(pse));
1614 pse.id = task_pid;
1616 task_basic_info_len = TASK_BASIC_INFO_COUNT;
1617 status = task_info(task_list[task], TASK_BASIC_INFO,
1618 (task_info_t)&task_basic_info, &task_basic_info_len);
1619 if (status != KERN_SUCCESS) {
1620 ERROR("task_info failed: %s", mach_error_string(status));
1621 continue; /* with next thread_list */
1622 }
1624 task_events_info_len = TASK_EVENTS_INFO_COUNT;
1625 status =
1626 task_info(task_list[task], TASK_EVENTS_INFO,
1627 (task_info_t)&task_events_info, &task_events_info_len);
1628 if (status != KERN_SUCCESS) {
1629 ERROR("task_info failed: %s", mach_error_string(status));
1630 continue; /* with next thread_list */
1631 }
1633 task_absolutetime_info_len = TASK_ABSOLUTETIME_INFO_COUNT;
1634 status = task_info(task_list[task], TASK_ABSOLUTETIME_INFO,
1635 (task_info_t)&task_absolutetime_info,
1636 &task_absolutetime_info_len);
1637 if (status != KERN_SUCCESS) {
1638 ERROR("task_info failed: %s", mach_error_string(status));
1639 continue; /* with next thread_list */
1640 }
1642 pse.num_proc++;
1643 pse.vmem_size = task_basic_info.virtual_size;
1644 pse.vmem_rss = task_basic_info.resident_size;
1645 /* Does not seem to be easily exposed */
1646 pse.vmem_data = 0;
1647 pse.vmem_code = 0;
1649 pse.io_rchar = -1;
1650 pse.io_wchar = -1;
1651 pse.io_syscr = -1;
1652 pse.io_syscw = -1;
1653 pse.io_diskr = -1;
1654 pse.io_diskw = -1;
1656 /* File descriptor count not implemented */
1657 pse.num_fd = 0;
1659 pse.vmem_minflt_counter = task_events_info.cow_faults;
1660 pse.vmem_majflt_counter = task_events_info.faults;
1662 pse.cpu_user_counter = task_absolutetime_info.total_user;
1663 pse.cpu_system_counter = task_absolutetime_info.total_system;
1665 /* context switch counters not implemented */
1666 pse.cswitch_vol = -1;
1667 pse.cswitch_invol = -1;
1668 }
1670 status = task_threads(task_list[task], &thread_list, &thread_list_len);
1671 if (status != KERN_SUCCESS) {
1672 /* Apple's `top' treats this case a zombie. It
1673 * makes sense to some extend: A `zombie'
1674 * thread is nonsense, since the task/process
1675 * is dead. */
1676 zombies++;
1677 DEBUG("task_threads failed: %s", mach_error_string(status));
1678 if (task_list[task] != port_task_self)
1679 mach_port_deallocate(port_task_self, task_list[task]);
1680 continue; /* with next task_list */
1681 }
1683 for (mach_msg_type_number_t thread = 0; thread < thread_list_len;
1684 thread++) {
1685 thread_data_len = THREAD_BASIC_INFO_COUNT;
1686 status = thread_info(thread_list[thread], THREAD_BASIC_INFO,
1687 (thread_info_t)&thread_data, &thread_data_len);
1688 if (status != KERN_SUCCESS) {
1689 ERROR("thread_info failed: %s", mach_error_string(status));
1690 if (task_list[task] != port_task_self)
1691 mach_port_deallocate(port_task_self, thread_list[thread]);
1692 continue; /* with next thread_list */
1693 }
1695 if (ps != NULL)
1696 pse.num_lwp++;
1698 switch (thread_data.run_state) {
1699 case TH_STATE_RUNNING:
1700 running++;
1701 break;
1702 case TH_STATE_STOPPED:
1703 /* What exactly is `halted'? */
1704 case TH_STATE_HALTED:
1705 stopped++;
1706 break;
1707 case TH_STATE_WAITING:
1708 sleeping++;
1709 break;
1710 case TH_STATE_UNINTERRUPTIBLE:
1711 blocked++;
1712 break;
1713 /* There is no `zombie' case here,
1714 * since there are no zombie-threads.
1715 * There's only zombie tasks, which are
1716 * handled above. */
1717 default:
1718 WARNING("Unknown thread status: %i", thread_data.run_state);
1719 break;
1720 } /* switch (thread_data.run_state) */
1722 if (task_list[task] != port_task_self) {
1723 status = mach_port_deallocate(port_task_self, thread_list[thread]);
1724 if (status != KERN_SUCCESS)
1725 ERROR("mach_port_deallocate failed: %s", mach_error_string(status));
1726 }
1727 } /* for (thread_list) */
1729 if ((status = vm_deallocate(port_task_self, (vm_address_t)thread_list,
1730 thread_list_len * sizeof(thread_act_t))) !=
1731 KERN_SUCCESS) {
1732 ERROR("vm_deallocate failed: %s", mach_error_string(status));
1733 }
1734 thread_list = NULL;
1735 thread_list_len = 0;
1737 /* Only deallocate the task port, if it isn't our own.
1738 * Don't know what would happen in that case, but this
1739 * is what Apple's top does.. ;) */
1740 if (task_list[task] != port_task_self) {
1741 status = mach_port_deallocate(port_task_self, task_list[task]);
1742 if (status != KERN_SUCCESS)
1743 ERROR("mach_port_deallocate failed: %s", mach_error_string(status));
1744 }
1746 if (ps != NULL)
1747 /* FIXME: cmdline should be here instead of NULL */
1748 ps_list_add(task_name, NULL, &pse);
1749 } /* for (task_list) */
1751 if ((status = vm_deallocate(port_task_self, (vm_address_t)task_list,
1752 task_list_len * sizeof(task_t))) !=
1753 KERN_SUCCESS) {
1754 ERROR("vm_deallocate failed: %s", mach_error_string(status));
1755 }
1756 task_list = NULL;
1757 task_list_len = 0;
1759 if ((status = mach_port_deallocate(port_task_self, port_pset_priv)) !=
1760 KERN_SUCCESS) {
1761 ERROR("mach_port_deallocate failed: %s", mach_error_string(status));
1762 }
1763 } /* for (pset_list) */
1765 ps_submit_state("running", running);
1766 ps_submit_state("sleeping", sleeping);
1767 ps_submit_state("zombies", zombies);
1768 ps_submit_state("stopped", stopped);
1769 ps_submit_state("blocked", blocked);
1771 for (ps = list_head_g; ps != NULL; ps = ps->next)
1772 ps_submit_proc_list(ps);
1773 /* #endif HAVE_THREAD_INFO */
1775 #elif KERNEL_LINUX
1776 int running = 0;
1777 int sleeping = 0;
1778 int zombies = 0;
1779 int stopped = 0;
1780 int paging = 0;
1781 int blocked = 0;
1783 struct dirent *ent;
1784 DIR *proc;
1785 long pid;
1787 char cmdline[CMDLINE_BUFFER_SIZE];
1789 int status;
1790 process_entry_t pse;
1791 char state;
1793 running = sleeping = zombies = stopped = paging = blocked = 0;
1794 ps_list_reset();
1796 if ((proc = opendir("/proc")) == NULL) {
1797 char errbuf[1024];
1798 ERROR("Cannot open `/proc': %s", sstrerror(errno, errbuf, sizeof(errbuf)));
1799 return -1;
1800 }
1802 while ((ent = readdir(proc)) != NULL) {
1803 if (!isdigit(ent->d_name[0]))
1804 continue;
1806 if ((pid = atol(ent->d_name)) < 1)
1807 continue;
1809 memset(&pse, 0, sizeof(pse));
1810 pse.id = pid;
1812 status = ps_read_process(pid, &pse, &state);
1813 if (status != 0) {
1814 DEBUG("ps_read_process failed: %i", status);
1815 continue;
1816 }
1818 switch (state) {
1819 case 'R':
1820 running++;
1821 break;
1822 case 'S':
1823 sleeping++;
1824 break;
1825 case 'D':
1826 blocked++;
1827 break;
1828 case 'Z':
1829 zombies++;
1830 break;
1831 case 'T':
1832 stopped++;
1833 break;
1834 case 'W':
1835 paging++;
1836 break;
1837 }
1839 ps_list_add(pse.name,
1840 ps_get_cmdline(pid, pse.name, cmdline, sizeof(cmdline)), &pse);
1841 }
1843 closedir(proc);
1845 ps_submit_state("running", running);
1846 ps_submit_state("sleeping", sleeping);
1847 ps_submit_state("zombies", zombies);
1848 ps_submit_state("stopped", stopped);
1849 ps_submit_state("paging", paging);
1850 ps_submit_state("blocked", blocked);
1852 for (procstat_t *ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1853 ps_submit_proc_list(ps_ptr);
1855 read_fork_rate();
1856 /* #endif KERNEL_LINUX */
1858 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD
1859 int running = 0;
1860 int sleeping = 0;
1861 int zombies = 0;
1862 int stopped = 0;
1863 int blocked = 0;
1864 int idle = 0;
1865 int wait = 0;
1867 kvm_t *kd;
1868 char errbuf[_POSIX2_LINE_MAX];
1869 struct kinfo_proc *procs; /* array of processes */
1870 struct kinfo_proc *proc_ptr = NULL;
1871 int count; /* returns number of processes */
1873 process_entry_t pse;
1875 ps_list_reset();
1877 /* Open the kvm interface, get a descriptor */
1878 kd = kvm_openfiles(NULL, "/dev/null", NULL, 0, errbuf);
1879 if (kd == NULL) {
1880 ERROR("processes plugin: Cannot open kvm interface: %s", errbuf);
1881 return 0;
1882 }
1884 /* Get the list of processes. */
1885 procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, &count);
1886 if (procs == NULL) {
1887 ERROR("processes plugin: Cannot get kvm processes list: %s",
1888 kvm_geterr(kd));
1889 kvm_close(kd);
1890 return 0;
1891 }
1893 /* Iterate through the processes in kinfo_proc */
1894 for (int i = 0; i < count; i++) {
1895 /* Create only one process list entry per _process_, i.e.
1896 * filter out threads (duplicate PID entries). */
1897 if ((proc_ptr == NULL) || (proc_ptr->ki_pid != procs[i].ki_pid)) {
1898 char cmdline[CMDLINE_BUFFER_SIZE] = "";
1899 _Bool have_cmdline = 0;
1901 proc_ptr = &(procs[i]);
1902 /* Don't probe system processes and processes without arguments */
1903 if (((procs[i].ki_flag & P_SYSTEM) == 0) && (procs[i].ki_args != NULL)) {
1904 char **argv;
1905 int argc;
1906 int status;
1908 /* retrieve the arguments */
1909 argv = kvm_getargv(kd, proc_ptr, /* nchr = */ 0);
1910 argc = 0;
1911 if ((argv != NULL) && (argv[0] != NULL)) {
1912 while (argv[argc] != NULL)
1913 argc++;
1915 status = strjoin(cmdline, sizeof(cmdline), argv, argc, " ");
1916 if (status < 0)
1917 WARNING("processes plugin: Command line did not fit into buffer.");
1918 else
1919 have_cmdline = 1;
1920 }
1921 } /* if (process has argument list) */
1923 memset(&pse, 0, sizeof(pse));
1924 pse.id = procs[i].ki_pid;
1926 pse.num_proc = 1;
1927 pse.num_lwp = procs[i].ki_numthreads;
1929 pse.vmem_size = procs[i].ki_size;
1930 pse.vmem_rss = procs[i].ki_rssize * pagesize;
1931 pse.vmem_data = procs[i].ki_dsize * pagesize;
1932 pse.vmem_code = procs[i].ki_tsize * pagesize;
1933 pse.stack_size = procs[i].ki_ssize * pagesize;
1934 pse.vmem_minflt_counter = procs[i].ki_rusage.ru_minflt;
1935 pse.vmem_majflt_counter = procs[i].ki_rusage.ru_majflt;
1937 pse.cpu_user_counter = 0;
1938 pse.cpu_system_counter = 0;
1939 /*
1940 * The u-area might be swapped out, and we can't get
1941 * at it because we have a crashdump and no swap.
1942 * If it's here fill in these fields, otherwise, just
1943 * leave them 0.
1944 */
1945 if (procs[i].ki_flag & P_INMEM) {
1946 pse.cpu_user_counter = procs[i].ki_rusage.ru_utime.tv_usec +
1947 (1000000lu * procs[i].ki_rusage.ru_utime.tv_sec);
1948 pse.cpu_system_counter =
1949 procs[i].ki_rusage.ru_stime.tv_usec +
1950 (1000000lu * procs[i].ki_rusage.ru_stime.tv_sec);
1951 }
1953 /* no I/O data */
1954 pse.io_rchar = -1;
1955 pse.io_wchar = -1;
1956 pse.io_syscr = -1;
1957 pse.io_syscw = -1;
1958 pse.io_diskr = -1;
1959 pse.io_diskw = -1;
1961 /* file descriptor count not implemented */
1962 pse.num_fd = 0;
1964 /* context switch counters not implemented */
1965 pse.cswitch_vol = -1;
1966 pse.cswitch_invol = -1;
1968 ps_list_add(procs[i].ki_comm, have_cmdline ? cmdline : NULL, &pse);
1970 switch (procs[i].ki_stat) {
1971 case SSTOP:
1972 stopped++;
1973 break;
1974 case SSLEEP:
1975 sleeping++;
1976 break;
1977 case SRUN:
1978 running++;
1979 break;
1980 case SIDL:
1981 idle++;
1982 break;
1983 case SWAIT:
1984 wait++;
1985 break;
1986 case SLOCK:
1987 blocked++;
1988 break;
1989 case SZOMB:
1990 zombies++;
1991 break;
1992 }
1993 } /* if ((proc_ptr == NULL) || (proc_ptr->ki_pid != procs[i].ki_pid)) */
1994 }
1996 kvm_close(kd);
1998 ps_submit_state("running", running);
1999 ps_submit_state("sleeping", sleeping);
2000 ps_submit_state("zombies", zombies);
2001 ps_submit_state("stopped", stopped);
2002 ps_submit_state("blocked", blocked);
2003 ps_submit_state("idle", idle);
2004 ps_submit_state("wait", wait);
2006 for (procstat_t *ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
2007 ps_submit_proc_list(ps_ptr);
2008 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
2010 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_OPENBSD
2011 int running = 0;
2012 int sleeping = 0;
2013 int zombies = 0;
2014 int stopped = 0;
2015 int onproc = 0;
2016 int idle = 0;
2017 int dead = 0;
2019 kvm_t *kd;
2020 char errbuf[1024];
2021 struct kinfo_proc *procs; /* array of processes */
2022 struct kinfo_proc *proc_ptr = NULL;
2023 int count; /* returns number of processes */
2025 process_entry_t pse;
2027 ps_list_reset();
2029 /* Open the kvm interface, get a descriptor */
2030 kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf);
2031 if (kd == NULL) {
2032 ERROR("processes plugin: Cannot open kvm interface: %s", errbuf);
2033 return 0;
2034 }
2036 /* Get the list of processes. */
2037 procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc), &count);
2038 if (procs == NULL) {
2039 ERROR("processes plugin: Cannot get kvm processes list: %s",
2040 kvm_geterr(kd));
2041 kvm_close(kd);
2042 return 0;
2043 }
2045 /* Iterate through the processes in kinfo_proc */
2046 for (int i = 0; i < count; i++) {
2047 /* Create only one process list entry per _process_, i.e.
2048 * filter out threads (duplicate PID entries). */
2049 if ((proc_ptr == NULL) || (proc_ptr->p_pid != procs[i].p_pid)) {
2050 char cmdline[CMDLINE_BUFFER_SIZE] = "";
2051 _Bool have_cmdline = 0;
2053 proc_ptr = &(procs[i]);
2054 /* Don't probe zombie processes */
2055 if (!P_ZOMBIE(proc_ptr)) {
2056 char **argv;
2057 int argc;
2058 int status;
2060 /* retrieve the arguments */
2061 argv = kvm_getargv(kd, proc_ptr, /* nchr = */ 0);
2062 argc = 0;
2063 if ((argv != NULL) && (argv[0] != NULL)) {
2064 while (argv[argc] != NULL)
2065 argc++;
2067 status = strjoin(cmdline, sizeof(cmdline), argv, argc, " ");
2068 if (status < 0)
2069 WARNING("processes plugin: Command line did not fit into buffer.");
2070 else
2071 have_cmdline = 1;
2072 }
2073 } /* if (process has argument list) */
2075 memset(&pse, 0, sizeof(pse));
2076 pse.id = procs[i].p_pid;
2078 pse.num_proc = 1;
2079 pse.num_lwp = 1; /* XXX: accumulate p_tid values for a single p_pid ? */
2081 pse.vmem_rss = procs[i].p_vm_rssize * pagesize;
2082 pse.vmem_data = procs[i].p_vm_dsize * pagesize;
2083 pse.vmem_code = procs[i].p_vm_tsize * pagesize;
2084 pse.stack_size = procs[i].p_vm_ssize * pagesize;
2085 pse.vmem_size = pse.stack_size + pse.vmem_code + pse.vmem_data;
2086 pse.vmem_minflt_counter = procs[i].p_uru_minflt;
2087 pse.vmem_majflt_counter = procs[i].p_uru_majflt;
2089 pse.cpu_user_counter =
2090 procs[i].p_uutime_usec + (1000000lu * procs[i].p_uutime_sec);
2091 pse.cpu_system_counter =
2092 procs[i].p_ustime_usec + (1000000lu * procs[i].p_ustime_sec);
2094 /* no I/O data */
2095 pse.io_rchar = -1;
2096 pse.io_wchar = -1;
2097 pse.io_syscr = -1;
2098 pse.io_syscw = -1;
2099 pse.io_diskr = -1;
2100 pse.io_diskw = -1;
2102 /* file descriptor count not implemented */
2103 pse.num_fd = 0;
2105 /* context switch counters not implemented */
2106 pse.cswitch_vol = -1;
2107 pse.cswitch_invol = -1;
2109 ps_list_add(procs[i].p_comm, have_cmdline ? cmdline : NULL, &pse);
2111 switch (procs[i].p_stat) {
2112 case SSTOP:
2113 stopped++;
2114 break;
2115 case SSLEEP:
2116 sleeping++;
2117 break;
2118 case SRUN:
2119 running++;
2120 break;
2121 case SIDL:
2122 idle++;
2123 break;
2124 case SONPROC:
2125 onproc++;
2126 break;
2127 case SDEAD:
2128 dead++;
2129 break;
2130 case SZOMB:
2131 zombies++;
2132 break;
2133 }
2134 } /* if ((proc_ptr == NULL) || (proc_ptr->p_pid != procs[i].p_pid)) */
2135 }
2137 kvm_close(kd);
2139 ps_submit_state("running", running);
2140 ps_submit_state("sleeping", sleeping);
2141 ps_submit_state("zombies", zombies);
2142 ps_submit_state("stopped", stopped);
2143 ps_submit_state("onproc", onproc);
2144 ps_submit_state("idle", idle);
2145 ps_submit_state("dead", dead);
2147 for (procstat_t *ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
2148 ps_submit_proc_list(ps_ptr);
2149 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_OPENBSD */
2151 #elif HAVE_PROCINFO_H
2152 /* AIX */
2153 int running = 0;
2154 int sleeping = 0;
2155 int zombies = 0;
2156 int stopped = 0;
2157 int paging = 0;
2158 int blocked = 0;
2160 pid_t pindex = 0;
2161 int nprocs;
2163 process_entry_t pse;
2165 ps_list_reset();
2166 while ((nprocs = getprocs64(procentry, sizeof(struct procentry64),
2167 /* fdsinfo = */ NULL, sizeof(struct fdsinfo64),
2168 &pindex, MAXPROCENTRY)) > 0) {
2169 for (int i = 0; i < nprocs; i++) {
2170 tid64_t thindex;
2171 int nthreads;
2172 char arglist[MAXARGLN + 1];
2173 char *cargs;
2174 char *cmdline;
2176 if (procentry[i].pi_state == SNONE)
2177 continue;
2178 /* if (procentry[i].pi_state == SZOMB) FIXME */
2180 cmdline = procentry[i].pi_comm;
2181 cargs = procentry[i].pi_comm;
2182 if (procentry[i].pi_flags & SKPROC) {
2183 if (procentry[i].pi_pid == 0)
2184 cmdline = "swapper";
2185 cargs = cmdline;
2186 } else {
2187 if (getargs(&procentry[i], sizeof(struct procentry64), arglist,
2188 MAXARGLN) >= 0) {
2189 int n;
2191 n = -1;
2192 while (++n < MAXARGLN) {
2193 if (arglist[n] == '\0') {
2194 if (arglist[n + 1] == '\0')
2195 break;
2196 arglist[n] = ' ';
2197 }
2198 }
2199 cargs = arglist;
2200 }
2201 }
2203 memset(&pse, 0, sizeof(pse));
2205 pse.id = procentry[i].pi_pid;
2206 pse.num_lwp = procentry[i].pi_thcount;
2207 pse.num_proc = 1;
2209 thindex = 0;
2210 while ((nthreads = getthrds64(procentry[i].pi_pid, thrdentry,
2211 sizeof(struct thrdentry64), &thindex,
2212 MAXTHRDENTRY)) > 0) {
2213 int j;
2215 for (j = 0; j < nthreads; j++) {
2216 switch (thrdentry[j].ti_state) {
2217 /* case TSNONE: break; */
2218 case TSIDL:
2219 blocked++;
2220 break; /* FIXME is really blocked */
2221 case TSRUN:
2222 running++;
2223 break;
2224 case TSSLEEP:
2225 sleeping++;
2226 break;
2227 case TSSWAP:
2228 paging++;
2229 break;
2230 case TSSTOP:
2231 stopped++;
2232 break;
2233 case TSZOMB:
2234 zombies++;
2235 break;
2236 }
2237 }
2238 if (nthreads < MAXTHRDENTRY)
2239 break;
2240 }
2242 /* tv_usec is nanosec ??? */
2243 pse.cpu_user_counter = procentry[i].pi_ru.ru_utime.tv_sec * 1000000 +
2244 procentry[i].pi_ru.ru_utime.tv_usec / 1000;
2246 /* tv_usec is nanosec ??? */
2247 pse.cpu_system_counter = procentry[i].pi_ru.ru_stime.tv_sec * 1000000 +
2248 procentry[i].pi_ru.ru_stime.tv_usec / 1000;
2250 pse.vmem_minflt_counter = procentry[i].pi_minflt;
2251 pse.vmem_majflt_counter = procentry[i].pi_majflt;
2253 pse.vmem_size = procentry[i].pi_tsize + procentry[i].pi_dvm * pagesize;
2254 pse.vmem_rss = (procentry[i].pi_drss + procentry[i].pi_trss) * pagesize;
2255 /* Not supported/implemented */
2256 pse.vmem_data = 0;
2257 pse.vmem_code = 0;
2258 pse.stack_size = 0;
2260 pse.io_rchar = -1;
2261 pse.io_wchar = -1;
2262 pse.io_syscr = -1;
2263 pse.io_syscw = -1;
2264 pse.io_diskr = -1;
2265 pse.io_diskw = -1;
2267 pse.num_fd = 0;
2269 pse.cswitch_vol = -1;
2270 pse.cswitch_invol = -1;
2272 ps_list_add(cmdline, cargs, &pse);
2273 } /* for (i = 0 .. nprocs) */
2275 if (nprocs < MAXPROCENTRY)
2276 break;
2277 } /* while (getprocs64() > 0) */
2278 ps_submit_state("running", running);
2279 ps_submit_state("sleeping", sleeping);
2280 ps_submit_state("zombies", zombies);
2281 ps_submit_state("stopped", stopped);
2282 ps_submit_state("paging", paging);
2283 ps_submit_state("blocked", blocked);
2285 for (procstat_t *ps = list_head_g; ps != NULL; ps = ps->next)
2286 ps_submit_proc_list(ps);
2287 /* #endif HAVE_PROCINFO_H */
2289 #elif KERNEL_SOLARIS
2290 /*
2291 * The Solaris section adds a few more process states and removes some
2292 * process states compared to linux. Most notably there is no "PAGING"
2293 * and "BLOCKED" state for a process. The rest is similar to the linux
2294 * code.
2295 */
2296 int running = 0;
2297 int sleeping = 0;
2298 int zombies = 0;
2299 int stopped = 0;
2300 int detached = 0;
2301 int daemon = 0;
2302 int system = 0;
2303 int orphan = 0;
2305 struct dirent *ent;
2306 DIR *proc;
2308 int status;
2309 char state;
2311 char cmdline[PRARGSZ];
2313 ps_list_reset();
2315 proc = opendir("/proc");
2316 if (proc == NULL)
2317 return -1;
2319 while ((ent = readdir(proc)) != NULL) {
2320 long pid;
2321 process_entry_t pse;
2322 char *endptr;
2324 if (!isdigit((int)ent->d_name[0]))
2325 continue;
2327 pid = strtol(ent->d_name, &endptr, 10);
2328 if (*endptr != 0) /* value didn't completely parse as a number */
2329 continue;
2331 memset(&pse, 0, sizeof(pse));
2332 pse.id = pid;
2334 status = ps_read_process(pid, &pse, &state);
2335 if (status != 0) {
2336 DEBUG("ps_read_process failed: %i", status);
2337 continue;
2338 }
2340 switch (state) {
2341 case 'R':
2342 running++;
2343 break;
2344 case 'S':
2345 sleeping++;
2346 break;
2347 case 'E':
2348 detached++;
2349 break;
2350 case 'Z':
2351 zombies++;
2352 break;
2353 case 'T':
2354 stopped++;
2355 break;
2356 case 'A':
2357 daemon++;
2358 break;
2359 case 'Y':
2360 system++;
2361 break;
2362 case 'O':
2363 orphan++;
2364 break;
2365 }
2367 ps_list_add(pse.name,
2368 ps_get_cmdline(pid, pse.name, cmdline, sizeof(cmdline)), &pse);
2369 } /* while(readdir) */
2370 closedir(proc);
2372 ps_submit_state("running", running);
2373 ps_submit_state("sleeping", sleeping);
2374 ps_submit_state("zombies", zombies);
2375 ps_submit_state("stopped", stopped);
2376 ps_submit_state("detached", detached);
2377 ps_submit_state("daemon", daemon);
2378 ps_submit_state("system", system);
2379 ps_submit_state("orphan", orphan);
2381 for (procstat_t *ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
2382 ps_submit_proc_list(ps_ptr);
2384 read_fork_rate();
2385 #endif /* KERNEL_SOLARIS */
2387 want_init = 0;
2389 return 0;
2390 } /* int ps_read */
2392 void module_register(void) {
2393 plugin_register_complex_config("processes", ps_config);
2394 plugin_register_init("processes", ps_init);
2395 plugin_register_read("processes", ps_read);
2396 } /* void module_register */