Debian Codegraph @

src/processes.c

raw
1 /**
2 * collectd - src/processes.c
3 * Copyright (C) 2005 Lyonel Vincent
4 * Copyright (C) 2006-2017 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 **/
38
39 #include "collectd.h"
40
41 #include "plugin.h"
42 #include "utils/common/common.h"
43
44 #if HAVE_LIBTASKSTATS
45 #include "utils/taskstats/taskstats.h"
46 #include "utils_complain.h"
47 #endif
48
49 /* Include header files for the mach system, if they exist.. */
50 #if HAVE_THREAD_INFO
51 #if HAVE_MACH_MACH_INIT_H
52 #include <mach/mach_init.h>
53 #endif
54 #if HAVE_MACH_HOST_PRIV_H
55 #include <mach/host_priv.h>
56 #endif
57 #if HAVE_MACH_MACH_ERROR_H
58 #include <mach/mach_error.h>
59 #endif
60 #if HAVE_MACH_MACH_HOST_H
61 #include <mach/mach_host.h>
62 #endif
63 #if HAVE_MACH_MACH_PORT_H
64 #include <mach/mach_port.h>
65 #endif
66 #if HAVE_MACH_MACH_TYPES_H
67 #include <mach/mach_types.h>
68 #endif
69 #if HAVE_MACH_MESSAGE_H
70 #include <mach/message.h>
71 #endif
72 #if HAVE_MACH_PROCESSOR_SET_H
73 #include <mach/processor_set.h>
74 #endif
75 #if HAVE_MACH_TASK_H
76 #include <mach/task.h>
77 #endif
78 #if HAVE_MACH_THREAD_ACT_H
79 #include <mach/thread_act.h>
80 #endif
81 #if HAVE_MACH_VM_REGION_H
82 #include <mach/vm_region.h>
83 #endif
84 #if HAVE_MACH_VM_MAP_H
85 #include <mach/vm_map.h>
86 #endif
87 #if HAVE_MACH_VM_PROT_H
88 #include <mach/vm_prot.h>
89 #endif
90 #if HAVE_SYS_SYSCTL_H
91 #include <sys/sysctl.h>
92 #endif
93 /* #endif HAVE_THREAD_INFO */
94
95 #elif KERNEL_LINUX
96 #if HAVE_LINUX_CONFIG_H
97 #include <linux/config.h>
98 #endif
99 #ifndef CONFIG_HZ
100 #define CONFIG_HZ 100
101 #endif
102 /* #endif KERNEL_LINUX */
103
104 #elif HAVE_LIBKVM_GETPROCS && \
105 (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD || \
106 HAVE_STRUCT_KINFO_PROC2_NETBSD)
107 #include <kvm.h>
108 #include <sys/param.h>
109 #include <sys/proc.h>
110 #include <sys/sysctl.h>
111 #if defined(__FreeBSD__) || defined(__DragonFly__)
112 #include <sys/user.h>
113 #endif
114 /* #endif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD ||
115 * HAVE_STRUCT_KINFO_PROC_OPENBSD || HAVE_STRUCT_KINFO_PROC2_NETBSD) */
116
117 #elif HAVE_PROCINFO_H
118 #include <procinfo.h>
119 #include <sys/types.h>
120
121 #define MAXPROCENTRY 32
122 #define MAXTHRDENTRY 16
123 #define MAXARGLN 1024
124 /* #endif HAVE_PROCINFO_H */
125
126 #elif KERNEL_SOLARIS
127 /* Hack: Avoid #error when building a 32-bit binary with
128 * _FILE_OFFSET_BITS=64. There is a reason for this #error, as one
129 * of the structures in <sys/procfs.h> uses an off_t, but that
130 * isn't relevant to our usage of procfs. */
131 #if !defined(_LP64) && _FILE_OFFSET_BITS == 64
132 #define SAVE_FOB_64
133 #undef _FILE_OFFSET_BITS
134 #endif
135
136 #include <procfs.h>
137
138 #ifdef SAVE_FOB_64
139 #define _FILE_OFFSET_BITS 64
140 #undef SAVE_FOB_64
141 #endif
142
143 #include <dirent.h>
144 #include <sys/user.h>
145
146 #ifndef MAXCOMLEN
147 #define MAXCOMLEN 16
148 #endif
149
150 /* #endif KERNEL_SOLARIS */
151
152 #else
153 #error "No applicable input method."
154 #endif
155
156 #if HAVE_REGEX_H
157 #include <regex.h>
158 #endif
159
160 #if HAVE_KSTAT_H
161 #include <kstat.h>
162 #endif
163
164 #ifdef HAVE_SYS_CAPABILITY_H
165 #include <sys/capability.h>
166 #endif
167
168 #ifndef CMDLINE_BUFFER_SIZE
169 #if defined(ARG_MAX) && (ARG_MAX < 4096)
170 #define CMDLINE_BUFFER_SIZE ARG_MAX
171 #else
172 #define CMDLINE_BUFFER_SIZE 4096
173 #endif
174 #endif
175
176 #define PROCSTAT_NAME_LEN 256
177 typedef struct process_entry_s {
178 unsigned long id;
179 char name[PROCSTAT_NAME_LEN];
180
181 unsigned long num_proc;
182 unsigned long num_lwp;
183 unsigned long num_fd;
184 unsigned long num_maps;
185 unsigned long vmem_size;
186 unsigned long vmem_rss;
187 unsigned long vmem_data;
188 unsigned long vmem_code;
189 unsigned long stack_size;
190
191 derive_t vmem_minflt_counter;
192 derive_t vmem_majflt_counter;
193
194 derive_t cpu_user_counter;
195 derive_t cpu_system_counter;
196
197 /* io data */
198 derive_t io_rchar;
199 derive_t io_wchar;
200 derive_t io_syscr;
201 derive_t io_syscw;
202 derive_t io_diskr;
203 derive_t io_diskw;
204 bool has_io;
205
206 derive_t cswitch_vol;
207 derive_t cswitch_invol;
208 bool has_cswitch;
209
210 #if HAVE_LIBTASKSTATS
211 ts_delay_t delay;
212 #endif
213 bool has_delay;
214
215 bool has_fd;
216
217 bool has_maps;
218 } process_entry_t;
219
220 typedef struct procstat_entry_s {
221 unsigned long id;
222 unsigned char age;
223
224 derive_t vmem_minflt_counter;
225 derive_t vmem_majflt_counter;
226
227 derive_t cpu_user_counter;
228 derive_t cpu_system_counter;
229
230 /* io data */
231 derive_t io_rchar;
232 derive_t io_wchar;
233 derive_t io_syscr;
234 derive_t io_syscw;
235 derive_t io_diskr;
236 derive_t io_diskw;
237
238 derive_t cswitch_vol;
239 derive_t cswitch_invol;
240
241 #if HAVE_LIBTASKSTATS
242 value_to_rate_state_t delay_cpu;
243 value_to_rate_state_t delay_blkio;
244 value_to_rate_state_t delay_swapin;
245 value_to_rate_state_t delay_freepages;
246 #endif
247
248 struct procstat_entry_s *next;
249 } procstat_entry_t;
250
251 typedef struct procstat {
252 char name[PROCSTAT_NAME_LEN];
253 #if HAVE_REGEX_H
254 regex_t *re;
255 #endif
256
257 unsigned long num_proc;
258 unsigned long num_lwp;
259 unsigned long num_fd;
260 unsigned long num_maps;
261 unsigned long vmem_size;
262 unsigned long vmem_rss;
263 unsigned long vmem_data;
264 unsigned long vmem_code;
265 unsigned long stack_size;
266
267 derive_t vmem_minflt_counter;
268 derive_t vmem_majflt_counter;
269
270 derive_t cpu_user_counter;
271 derive_t cpu_system_counter;
272
273 /* io data */
274 derive_t io_rchar;
275 derive_t io_wchar;
276 derive_t io_syscr;
277 derive_t io_syscw;
278 derive_t io_diskr;
279 derive_t io_diskw;
280
281 derive_t cswitch_vol;
282 derive_t cswitch_invol;
283
284 /* Linux Delay Accounting. Unit is ns/s. */
285 gauge_t delay_cpu;
286 gauge_t delay_blkio;
287 gauge_t delay_swapin;
288 gauge_t delay_freepages;
289
290 bool report_fd_num;
291 bool report_maps_num;
292 bool report_ctx_switch;
293 bool report_delay;
294
295 struct procstat *next;
296 struct procstat_entry_s *instances;
297 } procstat_t;
298
299 static procstat_t *list_head_g;
300
301 static bool want_init = true;
302 static bool report_ctx_switch;
303 static bool report_fd_num;
304 static bool report_maps_num;
305 static bool report_delay;
306
307 #if HAVE_THREAD_INFO
308 static mach_port_t port_host_self;
309 static mach_port_t port_task_self;
310
311 static processor_set_name_array_t pset_list;
312 static mach_msg_type_number_t pset_list_len;
313 /* #endif HAVE_THREAD_INFO */
314
315 #elif KERNEL_LINUX
316 static long pagesize_g;
317 static void ps_fill_details(const procstat_t *ps, process_entry_t *entry);
318 /* #endif KERNEL_LINUX */
319
320 #elif HAVE_LIBKVM_GETPROCS && \
321 (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD || \
322 HAVE_STRUCT_KINFO_PROC2_NETBSD)
323 static int pagesize;
324
325 #if KERNEL_NETBSD
326 int maxslp;
327 #endif
328
329 /* #endif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD ||
330 * HAVE_STRUCT_KINFO_PROC_OPENBSD || HAVE_STRUCT_KINFO_PROC2_NETBSD) */
331
332 #elif HAVE_PROCINFO_H
333 static struct procentry64 procentry[MAXPROCENTRY];
334 static struct thrdentry64 thrdentry[MAXTHRDENTRY];
335 static int pagesize;
336
337 #ifndef _AIXVERSION_610
338 int getprocs64(void *procsinfo, int sizproc, void *fdsinfo, int sizfd,
339 pid_t *index, int count);
340 int getthrds64(pid_t, void *, int, tid64_t *, int);
341 #endif
342 int getargs(void *processBuffer, int bufferLen, char *argsBuffer, int argsLen);
343 #endif /* HAVE_PROCINFO_H */
344
345 #if HAVE_LIBTASKSTATS
346 static ts_t *taskstats_handle;
347 #endif
348
349 /* put name of process from config to list_head_g tree
350 * list_head_g is a list of 'procstat_t' structs with
351 * processes names we want to watch */
352 static procstat_t *ps_list_register(const char *name, const char *regexp) {
353 procstat_t *new;
354 procstat_t *ptr;
355 int status;
356
357 new = calloc(1, sizeof(*new));
358 if (new == NULL) {
359 ERROR("processes plugin: ps_list_register: calloc failed.");
360 return NULL;
361 }
362 sstrncpy(new->name, name, sizeof(new->name));
363
364 new->io_rchar = -1;
365 new->io_wchar = -1;
366 new->io_syscr = -1;
367 new->io_syscw = -1;
368 new->io_diskr = -1;
369 new->io_diskw = -1;
370 new->cswitch_vol = -1;
371 new->cswitch_invol = -1;
372
373 new->report_fd_num = report_fd_num;
374 new->report_maps_num = report_maps_num;
375 new->report_ctx_switch = report_ctx_switch;
376 new->report_delay = report_delay;
377
378 #if HAVE_REGEX_H
379 if (regexp != NULL) {
380 DEBUG("ProcessMatch: adding \"%s\" as criteria to process %s.", regexp,
381 name);
382 new->re = malloc(sizeof(*new->re));
383 if (new->re == NULL) {
384 ERROR("processes plugin: ps_list_register: malloc failed.");
385 sfree(new);
386 return NULL;
387 }
388
389 status = regcomp(new->re, regexp, REG_EXTENDED | REG_NOSUB);
390 if (status != 0) {
391 DEBUG("ProcessMatch: compiling the regular expression \"%s\" failed.",
392 regexp);
393 sfree(new->re);
394 sfree(new);
395 return NULL;
396 }
397 }
398 #else
399 if (regexp != NULL) {
400 ERROR("processes plugin: ps_list_register: "
401 "Regular expression \"%s\" found in config "
402 "file, but support for regular expressions "
403 "has been disabled at compile time.",
404 regexp);
405 sfree(new);
406 return NULL;
407 }
408 #endif
409
410 for (ptr = list_head_g; ptr != NULL; ptr = ptr->next) {
411 if (strcmp(ptr->name, name) == 0) {
412 WARNING("processes plugin: You have configured more "
413 "than one `Process' or "
414 "`ProcessMatch' with the same name. "
415 "All but the first setting will be "
416 "ignored.");
417 #if HAVE_REGEX_H
418 sfree(new->re);
419 #endif
420 sfree(new);
421 return NULL;
422 }
423
424 if (ptr->next == NULL)
425 break;
426 }
427
428 if (ptr == NULL)
429 list_head_g = new;
430 else
431 ptr->next = new;
432
433 return new;
434 } /* void ps_list_register */
435
436 /* try to match name against entry, returns 1 if success */
437 static int ps_list_match(const char *name, const char *cmdline,
438 procstat_t *ps) {
439 #if HAVE_REGEX_H
440 if (ps->re != NULL) {
441 int status;
442 const char *str;
443
444 str = cmdline;
445 if ((str == NULL) || (str[0] == 0))
446 str = name;
447
448 assert(str != NULL);
449
450 status = regexec(ps->re, str,
451 /* nmatch = */ 0,
452 /* pmatch = */ NULL,
453 /* eflags = */ 0);
454 if (status == 0)
455 return 1;
456 } else
457 #endif
458 if (strcmp(ps->name, name) == 0)
459 return 1;
460
461 return 0;
462 } /* int ps_list_match */
463
464 static void ps_update_counter(derive_t *group_counter, derive_t *curr_counter,
465 derive_t new_counter) {
466 unsigned long curr_value;
467
468 if (want_init) {
469 *curr_counter = new_counter;
470 return;
471 }
472
473 if (new_counter < *curr_counter)
474 curr_value = new_counter + (ULONG_MAX - *curr_counter);
475 else
476 curr_value = new_counter - *curr_counter;
477
478 if (*group_counter == -1)
479 *group_counter = 0;
480
481 *curr_counter = new_counter;
482 *group_counter += curr_value;
483 }
484
485 #if HAVE_LIBTASKSTATS
486 static void ps_update_delay_one(gauge_t *out_rate_sum,
487 value_to_rate_state_t *state, uint64_t cnt,
488 cdtime_t t) {
489 gauge_t rate = NAN;
490 int status = value_to_rate(&rate, (value_t){.counter = (counter_t)cnt},
491 DS_TYPE_COUNTER, t, state);
492 if ((status != 0) || isnan(rate)) {
493 return;
494 }
495
496 if (isnan(*out_rate_sum)) {
497 *out_rate_sum = rate;
498 } else {
499 *out_rate_sum += rate;
500 }
501 }
502
503 static void ps_update_delay(procstat_t *out, procstat_entry_t *prev,
504 process_entry_t *curr) {
505 cdtime_t now = cdtime();
506
507 ps_update_delay_one(&out->delay_cpu, &prev->delay_cpu, curr->delay.cpu_ns,
508 now);
509 ps_update_delay_one(&out->delay_blkio, &prev->delay_blkio,
510 curr->delay.blkio_ns, now);
511 ps_update_delay_one(&out->delay_swapin, &prev->delay_swapin,
512 curr->delay.swapin_ns, now);
513 ps_update_delay_one(&out->delay_freepages, &prev->delay_freepages,
514 curr->delay.freepages_ns, now);
515 }
516 #endif
517
518 /* add process entry to 'instances' of process 'name' (or refresh it) */
519 static void ps_list_add(const char *name, const char *cmdline,
520 process_entry_t *entry) {
521 procstat_entry_t *pse;
522
523 if (entry->id == 0)
524 return;
525
526 for (procstat_t *ps = list_head_g; ps != NULL; ps = ps->next) {
527 if ((ps_list_match(name, cmdline, ps)) == 0)
528 continue;
529
530 #if KERNEL_LINUX
531 ps_fill_details(ps, entry);
532 #endif
533
534 for (pse = ps->instances; pse != NULL; pse = pse->next)
535 if ((pse->id == entry->id) || (pse->next == NULL))
536 break;
537
538 if ((pse == NULL) || (pse->id != entry->id)) {
539 procstat_entry_t *new;
540
541 new = calloc(1, sizeof(*new));
542 if (new == NULL)
543 return;
544 new->id = entry->id;
545
546 if (pse == NULL)
547 ps->instances = new;
548 else
549 pse->next = new;
550
551 pse = new;
552 }
553
554 pse->age = 0;
555
556 ps->num_proc += entry->num_proc;
557 ps->num_lwp += entry->num_lwp;
558 ps->num_fd += entry->num_fd;
559 ps->num_maps += entry->num_maps;
560 ps->vmem_size += entry->vmem_size;
561 ps->vmem_rss += entry->vmem_rss;
562 ps->vmem_data += entry->vmem_data;
563 ps->vmem_code += entry->vmem_code;
564 ps->stack_size += entry->stack_size;
565
566 if ((entry->io_rchar != -1) && (entry->io_wchar != -1)) {
567 ps_update_counter(&ps->io_rchar, &pse->io_rchar, entry->io_rchar);
568 ps_update_counter(&ps->io_wchar, &pse->io_wchar, entry->io_wchar);
569 }
570
571 if ((entry->io_syscr != -1) && (entry->io_syscw != -1)) {
572 ps_update_counter(&ps->io_syscr, &pse->io_syscr, entry->io_syscr);
573 ps_update_counter(&ps->io_syscw, &pse->io_syscw, entry->io_syscw);
574 }
575
576 if ((entry->io_diskr != -1) && (entry->io_diskw != -1)) {
577 ps_update_counter(&ps->io_diskr, &pse->io_diskr, entry->io_diskr);
578 ps_update_counter(&ps->io_diskw, &pse->io_diskw, entry->io_diskw);
579 }
580
581 if ((entry->cswitch_vol != -1) && (entry->cswitch_invol != -1)) {
582 ps_update_counter(&ps->cswitch_vol, &pse->cswitch_vol,
583 entry->cswitch_vol);
584 ps_update_counter(&ps->cswitch_invol, &pse->cswitch_invol,
585 entry->cswitch_invol);
586 }
587
588 ps_update_counter(&ps->vmem_minflt_counter, &pse->vmem_minflt_counter,
589 entry->vmem_minflt_counter);
590 ps_update_counter(&ps->vmem_majflt_counter, &pse->vmem_majflt_counter,
591 entry->vmem_majflt_counter);
592
593 ps_update_counter(&ps->cpu_user_counter, &pse->cpu_user_counter,
594 entry->cpu_user_counter);
595 ps_update_counter(&ps->cpu_system_counter, &pse->cpu_system_counter,
596 entry->cpu_system_counter);
597
598 #if HAVE_LIBTASKSTATS
599 if (entry->has_delay)
600 ps_update_delay(ps, pse, entry);
601 #endif
602 }
603 }
604
605 /* remove old entries from instances of processes in list_head_g */
606 static void ps_list_reset(void) {
607 procstat_entry_t *pse;
608 procstat_entry_t *pse_prev;
609
610 for (procstat_t *ps = list_head_g; ps != NULL; ps = ps->next) {
611 ps->num_proc = 0;
612 ps->num_lwp = 0;
613 ps->num_fd = 0;
614 ps->num_maps = 0;
615 ps->vmem_size = 0;
616 ps->vmem_rss = 0;
617 ps->vmem_data = 0;
618 ps->vmem_code = 0;
619 ps->stack_size = 0;
620
621 ps->delay_cpu = NAN;
622 ps->delay_blkio = NAN;
623 ps->delay_swapin = NAN;
624 ps->delay_freepages = NAN;
625
626 pse_prev = NULL;
627 pse = ps->instances;
628 while (pse != NULL) {
629 if (pse->age > 0) {
630 DEBUG("Removing this procstat entry cause it's too old: "
631 "id = %lu; name = %s;",
632 pse->id, ps->name);
633
634 if (pse_prev == NULL) {
635 ps->instances = pse->next;
636 free(pse);
637 pse = ps->instances;
638 } else {
639 pse_prev->next = pse->next;
640 free(pse);
641 pse = pse_prev->next;
642 }
643 } else {
644 pse->age = 1;
645 pse_prev = pse;
646 pse = pse->next;
647 }
648 } /* while (pse != NULL) */
649 } /* for (ps = list_head_g; ps != NULL; ps = ps->next) */
650 }
651
652 static void ps_tune_instance(oconfig_item_t *ci, procstat_t *ps) {
653 for (int i = 0; i < ci->children_num; i++) {
654 oconfig_item_t *c = ci->children + i;
655
656 if (strcasecmp(c->key, "CollectContextSwitch") == 0)
657 cf_util_get_boolean(c, &ps->report_ctx_switch);
658 else if (strcasecmp(c->key, "CollectFileDescriptor") == 0)
659 cf_util_get_boolean(c, &ps->report_fd_num);
660 else if (strcasecmp(c->key, "CollectMemoryMaps") == 0)
661 cf_util_get_boolean(c, &ps->report_maps_num);
662 else if (strcasecmp(c->key, "CollectDelayAccounting") == 0) {
663 #if HAVE_LIBTASKSTATS
664 cf_util_get_boolean(c, &ps->report_delay);
665 #else
666 WARNING("processes plugin: The plugin has been compiled without support "
667 "for the \"CollectDelayAccounting\" option.");
668 #endif
669 } else {
670 ERROR("processes plugin: Option \"%s\" not allowed here.", c->key);
671 }
672 } /* for (ci->children) */
673 } /* void ps_tune_instance */
674
675 /* put all pre-defined 'Process' names from config to list_head_g tree */
676 static int ps_config(oconfig_item_t *ci) {
677 #if KERNEL_LINUX
678 const size_t max_procname_len = 15;
679 #elif KERNEL_SOLARIS || KERNEL_FREEBSD
680 const size_t max_procname_len = MAXCOMLEN - 1;
681 #endif
682
683 procstat_t *ps;
684
685 for (int i = 0; i < ci->children_num; ++i) {
686 oconfig_item_t *c = ci->children + i;
687
688 if (strcasecmp(c->key, "Process") == 0) {
689 if ((c->values_num != 1) || (OCONFIG_TYPE_STRING != c->values[0].type)) {
690 ERROR("processes plugin: `Process' expects exactly "
691 "one string argument (got %i).",
692 c->values_num);
693 continue;
694 }
695
696 #if KERNEL_LINUX || KERNEL_SOLARIS || KERNEL_FREEBSD
697 if (strlen(c->values[0].value.string) > max_procname_len) {
698 WARNING("processes plugin: this platform has a %" PRIsz
699 " character limit "
700 "to process names. The `Process \"%s\"' option will "
701 "not work as expected.",
702 max_procname_len, c->values[0].value.string);
703 }
704 #endif
705
706 ps = ps_list_register(c->values[0].value.string, NULL);
707
708 if (c->children_num != 0 && ps != NULL)
709 ps_tune_instance(c, ps);
710 } else if (strcasecmp(c->key, "ProcessMatch") == 0) {
711 if ((c->values_num != 2) || (OCONFIG_TYPE_STRING != c->values[0].type) ||
712 (OCONFIG_TYPE_STRING != c->values[1].type)) {
713 ERROR("processes plugin: `ProcessMatch' needs exactly "
714 "two string arguments (got %i).",
715 c->values_num);
716 continue;
717 }
718
719 ps = ps_list_register(c->values[0].value.string,
720 c->values[1].value.string);
721
722 if (c->children_num != 0 && ps != NULL)
723 ps_tune_instance(c, ps);
724 } else if (strcasecmp(c->key, "CollectContextSwitch") == 0) {
725 cf_util_get_boolean(c, &report_ctx_switch);
726 } else if (strcasecmp(c->key, "CollectFileDescriptor") == 0) {
727 cf_util_get_boolean(c, &report_fd_num);
728 } else if (strcasecmp(c->key, "CollectMemoryMaps") == 0) {
729 cf_util_get_boolean(c, &report_maps_num);
730 } else if (strcasecmp(c->key, "CollectDelayAccounting") == 0) {
731 #if HAVE_LIBTASKSTATS
732 cf_util_get_boolean(c, &report_delay);
733 #else
734 WARNING("processes plugin: The plugin has been compiled without support "
735 "for the \"CollectDelayAccounting\" option.");
736 #endif
737 } else {
738 ERROR("processes plugin: The `%s' configuration option is not "
739 "understood and will be ignored.",
740 c->key);
741 continue;
742 }
743 }
744
745 return 0;
746 }
747
748 static int ps_init(void) {
749 #if HAVE_THREAD_INFO
750 kern_return_t status;
751
752 port_host_self = mach_host_self();
753 port_task_self = mach_task_self();
754
755 if (pset_list != NULL) {
756 vm_deallocate(port_task_self, (vm_address_t)pset_list,
757 pset_list_len * sizeof(processor_set_t));
758 pset_list = NULL;
759 pset_list_len = 0;
760 }
761
762 if ((status = host_processor_sets(port_host_self, &pset_list,
763 &pset_list_len)) != KERN_SUCCESS) {
764 ERROR("host_processor_sets failed: %s\n", mach_error_string(status));
765 pset_list = NULL;
766 pset_list_len = 0;
767 return -1;
768 }
769 /* #endif HAVE_THREAD_INFO */
770
771 #elif KERNEL_LINUX
772 pagesize_g = sysconf(_SC_PAGESIZE);
773 DEBUG("pagesize_g = %li; CONFIG_HZ = %i;", pagesize_g, CONFIG_HZ);
774
775 #if HAVE_LIBTASKSTATS
776 if (taskstats_handle == NULL) {
777 taskstats_handle = ts_create();
778 if (taskstats_handle == NULL) {
779 WARNING("processes plugin: Creating taskstats handle failed.");
780 }
781 }
782 #endif
783 /* #endif KERNEL_LINUX */
784
785 #elif HAVE_LIBKVM_GETPROCS && \
786 (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD || \
787 HAVE_STRUCT_KINFO_PROC2_NETBSD)
788 #if KERNEL_NETBSD
789 int mib[2];
790 size_t size;
791
792 mib[0] = CTL_VM;
793 mib[1] = VM_MAXSLP;
794 size = sizeof(maxslp);
795 if (sysctl(mib, 2, &maxslp, &size, NULL, 0) == -1)
796 maxslp = 20; /* reasonable default? */
797 #endif
798
799 pagesize = getpagesize();
800 /* #endif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD ||
801 * HAVE_STRUCT_KINFO_PROC_OPENBSD || HAVE_STRUCT_KINFO_PROC2_NETBSD) */
802
803 #elif HAVE_PROCINFO_H
804 pagesize = getpagesize();
805 #endif /* HAVE_PROCINFO_H */
806
807 return 0;
808 } /* int ps_init */
809
810 /* submit global state (e.g.: qty of zombies, running, etc..) */
811 static void ps_submit_state(const char *state, double value) {
812 value_list_t vl = VALUE_LIST_INIT;
813
814 vl.values = &(value_t){.gauge = value};
815 vl.values_len = 1;
816 sstrncpy(vl.plugin, "processes", sizeof(vl.plugin));
817 sstrncpy(vl.plugin_instance, "", sizeof(vl.plugin_instance));
818 sstrncpy(vl.type, "ps_state", sizeof(vl.type));
819 sstrncpy(vl.type_instance, state, sizeof(vl.type_instance));
820
821 plugin_dispatch_values(&vl);
822 }
823
824 /* submit info about specific process (e.g.: memory taken, cpu usage, etc..) */
825 static void ps_submit_proc_list(procstat_t *ps) {
826 value_list_t vl = VALUE_LIST_INIT;
827 value_t values[2];
828
829 vl.values = values;
830 sstrncpy(vl.plugin, "processes", sizeof(vl.plugin));
831 sstrncpy(vl.plugin_instance, ps->name, sizeof(vl.plugin_instance));
832
833 sstrncpy(vl.type, "ps_vm", sizeof(vl.type));
834 vl.values[0].gauge = ps->vmem_size;
835 vl.values_len = 1;
836 plugin_dispatch_values(&vl);
837
838 sstrncpy(vl.type, "ps_rss", sizeof(vl.type));
839 vl.values[0].gauge = ps->vmem_rss;
840 vl.values_len = 1;
841 plugin_dispatch_values(&vl);
842
843 sstrncpy(vl.type, "ps_data", sizeof(vl.type));
844 vl.values[0].gauge = ps->vmem_data;
845 vl.values_len = 1;
846 plugin_dispatch_values(&vl);
847
848 sstrncpy(vl.type, "ps_code", sizeof(vl.type));
849 vl.values[0].gauge = ps->vmem_code;
850 vl.values_len = 1;
851 plugin_dispatch_values(&vl);
852
853 sstrncpy(vl.type, "ps_stacksize", sizeof(vl.type));
854 vl.values[0].gauge = ps->stack_size;
855 vl.values_len = 1;
856 plugin_dispatch_values(&vl);
857
858 sstrncpy(vl.type, "ps_cputime", sizeof(vl.type));
859 vl.values[0].derive = ps->cpu_user_counter;
860 vl.values[1].derive = ps->cpu_system_counter;
861 vl.values_len = 2;
862 plugin_dispatch_values(&vl);
863
864 sstrncpy(vl.type, "ps_count", sizeof(vl.type));
865 vl.values[0].gauge = ps->num_proc;
866 vl.values[1].gauge = ps->num_lwp;
867 vl.values_len = 2;
868 plugin_dispatch_values(&vl);
869
870 sstrncpy(vl.type, "ps_pagefaults", sizeof(vl.type));
871 vl.values[0].derive = ps->vmem_minflt_counter;
872 vl.values[1].derive = ps->vmem_majflt_counter;
873 vl.values_len = 2;
874 plugin_dispatch_values(&vl);
875
876 if ((ps->io_rchar != -1) && (ps->io_wchar != -1)) {
877 sstrncpy(vl.type, "io_octets", sizeof(vl.type));
878 vl.values[0].derive = ps->io_rchar;
879 vl.values[1].derive = ps->io_wchar;
880 vl.values_len = 2;
881 plugin_dispatch_values(&vl);
882 }
883
884 if ((ps->io_syscr != -1) && (ps->io_syscw != -1)) {
885 sstrncpy(vl.type, "io_ops", sizeof(vl.type));
886 vl.values[0].derive = ps->io_syscr;
887 vl.values[1].derive = ps->io_syscw;
888 vl.values_len = 2;
889 plugin_dispatch_values(&vl);
890 }
891
892 if ((ps->io_diskr != -1) && (ps->io_diskw != -1)) {
893 sstrncpy(vl.type, "disk_octets", sizeof(vl.type));
894 vl.values[0].derive = ps->io_diskr;
895 vl.values[1].derive = ps->io_diskw;
896 vl.values_len = 2;
897 plugin_dispatch_values(&vl);
898 }
899
900 if (ps->num_fd > 0) {
901 sstrncpy(vl.type, "file_handles", sizeof(vl.type));
902 vl.values[0].gauge = ps->num_fd;
903 vl.values_len = 1;
904 plugin_dispatch_values(&vl);
905 }
906
907 if (ps->num_maps > 0) {
908 sstrncpy(vl.type, "file_handles", sizeof(vl.type));
909 sstrncpy(vl.type_instance, "mapped", sizeof(vl.type_instance));
910 vl.values[0].gauge = ps->num_maps;
911 vl.values_len = 1;
912 plugin_dispatch_values(&vl);
913 }
914
915 if ((ps->cswitch_vol != -1) && (ps->cswitch_invol != -1)) {
916 sstrncpy(vl.type, "contextswitch", sizeof(vl.type));
917 sstrncpy(vl.type_instance, "voluntary", sizeof(vl.type_instance));
918 vl.values[0].derive = ps->cswitch_vol;
919 vl.values_len = 1;
920 plugin_dispatch_values(&vl);
921
922 sstrncpy(vl.type, "contextswitch", sizeof(vl.type));
923 sstrncpy(vl.type_instance, "involuntary", sizeof(vl.type_instance));
924 vl.values[0].derive = ps->cswitch_invol;
925 vl.values_len = 1;
926 plugin_dispatch_values(&vl);
927 }
928
929 /* The ps->delay_* metrics are in nanoseconds per second. Convert to seconds
930 * per second. */
931 gauge_t const delay_factor = 1000000000.0;
932
933 struct {
934 const char *type_instance;
935 gauge_t rate_ns;
936 } delay_metrics[] = {
937 {"delay-cpu", ps->delay_cpu},
938 {"delay-blkio", ps->delay_blkio},
939 {"delay-swapin", ps->delay_swapin},
940 {"delay-freepages", ps->delay_freepages},
941 };
942 for (size_t i = 0; i < STATIC_ARRAY_SIZE(delay_metrics); i++) {
943 if (isnan(delay_metrics[i].rate_ns)) {
944 continue;
945 }
946 sstrncpy(vl.type, "delay_rate", sizeof(vl.type));
947 sstrncpy(vl.type_instance, delay_metrics[i].type_instance,
948 sizeof(vl.type_instance));
949 vl.values[0].gauge = delay_metrics[i].rate_ns / delay_factor;
950 vl.values_len = 1;
951 plugin_dispatch_values(&vl);
952 }
953
954 DEBUG(
955 "name = %s; num_proc = %lu; num_lwp = %lu; num_fd = %lu; num_maps = %lu; "
956 "vmem_size = %lu; vmem_rss = %lu; vmem_data = %lu; "
957 "vmem_code = %lu; "
958 "vmem_minflt_counter = %" PRIi64 "; vmem_majflt_counter = %" PRIi64 "; "
959 "cpu_user_counter = %" PRIi64 "; cpu_system_counter = %" PRIi64 "; "
960 "io_rchar = %" PRIi64 "; io_wchar = %" PRIi64 "; "
961 "io_syscr = %" PRIi64 "; io_syscw = %" PRIi64 "; "
962 "io_diskr = %" PRIi64 "; io_diskw = %" PRIi64 "; "
963 "cswitch_vol = %" PRIi64 "; cswitch_invol = %" PRIi64 "; "
964 "delay_cpu = %g; delay_blkio = %g; "
965 "delay_swapin = %g; delay_freepages = %g;",
966 ps->name, ps->num_proc, ps->num_lwp, ps->num_fd, ps->num_maps,
967 ps->vmem_size, ps->vmem_rss, ps->vmem_data, ps->vmem_code,
968 ps->vmem_minflt_counter, ps->vmem_majflt_counter, ps->cpu_user_counter,
969 ps->cpu_system_counter, ps->io_rchar, ps->io_wchar, ps->io_syscr,
970 ps->io_syscw, ps->io_diskr, ps->io_diskw, ps->cswitch_vol,
971 ps->cswitch_invol, ps->delay_cpu, ps->delay_blkio, ps->delay_swapin,
972 ps->delay_freepages);
973
974 } /* void ps_submit_proc_list */
975
976 #if KERNEL_LINUX || KERNEL_SOLARIS
977 static void ps_submit_fork_rate(derive_t value) {
978 value_list_t vl = VALUE_LIST_INIT;
979
980 vl.values = &(value_t){.derive = value};
981 vl.values_len = 1;
982 sstrncpy(vl.plugin, "processes", sizeof(vl.plugin));
983 sstrncpy(vl.plugin_instance, "", sizeof(vl.plugin_instance));
984 sstrncpy(vl.type, "fork_rate", sizeof(vl.type));
985 sstrncpy(vl.type_instance, "", sizeof(vl.type_instance));
986
987 plugin_dispatch_values(&vl);
988 }
989 #endif /* KERNEL_LINUX || KERNEL_SOLARIS*/
990
991 /* ------- additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
992 #if KERNEL_LINUX
993 static int ps_read_tasks_status(process_entry_t *ps) {
994 char dirname[64];
995 DIR *dh;
996 char filename[64];
997 FILE *fh;
998 struct dirent *ent;
999 derive_t cswitch_vol = 0;
1000 derive_t cswitch_invol = 0;
1001 char buffer[1024];
1002 char *fields[8];
1003 int numfields;
1004
1005 snprintf(dirname, sizeof(dirname), "/proc/%li/task", ps->id);
1006
1007 if ((dh = opendir(dirname)) == NULL) {
1008 DEBUG("Failed to open directory `%s'", dirname);
1009 return -1;
1010 }
1011
1012 while ((ent = readdir(dh)) != NULL) {
1013 char *tpid;
1014
1015 if (!isdigit((int)ent->d_name[0]))
1016 continue;
1017
1018 tpid = ent->d_name;
1019
1020 int r = snprintf(filename, sizeof(filename), "/proc/%li/task/%s/status",
1021 ps->id, tpid);
1022 if ((size_t)r >= sizeof(filename)) {
1023 DEBUG("Filename too long: `%s'", filename);
1024 continue;
1025 }
1026
1027 if ((fh = fopen(filename, "r")) == NULL) {
1028 DEBUG("Failed to open file `%s'", filename);
1029 continue;
1030 }
1031
1032 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
1033 derive_t tmp;
1034 char *endptr;
1035
1036 if (strncmp(buffer, "voluntary_ctxt_switches", 23) != 0 &&
1037 strncmp(buffer, "nonvoluntary_ctxt_switches", 26) != 0)
1038 continue;
1039
1040 numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
1041
1042 if (numfields < 2)
1043 continue;
1044
1045 errno = 0;
1046 endptr = NULL;
1047 tmp = (derive_t)strtoll(fields[1], &endptr, /* base = */ 10);
1048 if ((errno == 0) && (endptr != fields[1])) {
1049 if (strncmp(buffer, "voluntary_ctxt_switches", 23) == 0) {
1050 cswitch_vol += tmp;
1051 } else if (strncmp(buffer, "nonvoluntary_ctxt_switches", 26) == 0) {
1052 cswitch_invol += tmp;
1053 }
1054 }
1055 } /* while (fgets) */
1056
1057 if (fclose(fh)) {
1058 WARNING("processes: fclose: %s", STRERRNO);
1059 }
1060 }
1061 closedir(dh);
1062
1063 ps->cswitch_vol = cswitch_vol;
1064 ps->cswitch_invol = cswitch_invol;
1065
1066 return 0;
1067 } /* int *ps_read_tasks_status */
1068
1069 /* Read data from /proc/pid/status */
1070 static int ps_read_status(long pid, process_entry_t *ps) {
1071 FILE *fh;
1072 char buffer[1024];
1073 char filename[64];
1074 unsigned long lib = 0;
1075 unsigned long exe = 0;
1076 unsigned long data = 0;
1077 unsigned long threads = 0;
1078 char *fields[8];
1079 int numfields;
1080
1081 snprintf(filename, sizeof(filename), "/proc/%li/status", pid);
1082 if ((fh = fopen(filename, "r")) == NULL)
1083 return -1;
1084
1085 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
1086 unsigned long tmp;
1087 char *endptr;
1088
1089 if (strncmp(buffer, "Vm", 2) != 0 && strncmp(buffer, "Threads", 7) != 0)
1090 continue;
1091
1092 numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
1093
1094 if (numfields < 2)
1095 continue;
1096
1097 errno = 0;
1098 endptr = NULL;
1099 tmp = strtoul(fields[1], &endptr, /* base = */ 10);
1100 if ((errno == 0) && (endptr != fields[1])) {
1101 if (strncmp(buffer, "VmData", 6) == 0) {
1102 data = tmp;
1103 } else if (strncmp(buffer, "VmLib", 5) == 0) {
1104 lib = tmp;
1105 } else if (strncmp(buffer, "VmExe", 5) == 0) {
1106 exe = tmp;
1107 } else if (strncmp(buffer, "Threads", 7) == 0) {
1108 threads = tmp;
1109 }
1110 }
1111 } /* while (fgets) */
1112
1113 if (fclose(fh)) {
1114 WARNING("processes: fclose: %s", STRERRNO);
1115 }
1116
1117 ps->vmem_data = data * 1024;
1118 ps->vmem_code = (exe + lib) * 1024;
1119 if (threads != 0)
1120 ps->num_lwp = threads;
1121
1122 return 0;
1123 } /* int *ps_read_status */
1124
1125 static int ps_read_io(process_entry_t *ps) {
1126 FILE *fh;
1127 char buffer[1024];
1128 char filename[64];
1129
1130 char *fields[8];
1131 int numfields;
1132
1133 snprintf(filename, sizeof(filename), "/proc/%li/io", ps->id);
1134 if ((fh = fopen(filename, "r")) == NULL) {
1135 DEBUG("ps_read_io: Failed to open file `%s'", filename);
1136 return -1;
1137 }
1138
1139 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
1140 derive_t *val = NULL;
1141 long long tmp;
1142 char *endptr;
1143
1144 if (strncasecmp(buffer, "rchar:", 6) == 0)
1145 val = &(ps->io_rchar);
1146 else if (strncasecmp(buffer, "wchar:", 6) == 0)
1147 val = &(ps->io_wchar);
1148 else if (strncasecmp(buffer, "syscr:", 6) == 0)
1149 val = &(ps->io_syscr);
1150 else if (strncasecmp(buffer, "syscw:", 6) == 0)
1151 val = &(ps->io_syscw);
1152 else if (strncasecmp(buffer, "read_bytes:", 11) == 0)
1153 val = &(ps->io_diskr);
1154 else if (strncasecmp(buffer, "write_bytes:", 12) == 0)
1155 val = &(ps->io_diskw);
1156 else
1157 continue;
1158
1159 numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
1160
1161 if (numfields < 2)
1162 continue;
1163
1164 errno = 0;
1165 endptr = NULL;
1166 tmp = strtoll(fields[1], &endptr, /* base = */ 10);
1167 if ((errno != 0) || (endptr == fields[1]))
1168 *val = -1;
1169 else
1170 *val = (derive_t)tmp;
1171 } /* while (fgets) */
1172
1173 if (fclose(fh)) {
1174 WARNING("processes: fclose: %s", STRERRNO);
1175 }
1176 return 0;
1177 } /* int ps_read_io (...) */
1178
1179 static int ps_count_maps(pid_t pid) {
1180 FILE *fh;
1181 char buffer[1024];
1182 char filename[64];
1183 int count = 0;
1184
1185 snprintf(filename, sizeof(filename), "/proc/%d/maps", pid);
1186 if ((fh = fopen(filename, "r")) == NULL) {
1187 DEBUG("ps_count_maps: Failed to open file `%s'", filename);
1188 return -1;
1189 }
1190
1191 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
1192 if (strchr(buffer, '\n')) {
1193 count++;
1194 }
1195 } /* while (fgets) */
1196
1197 if (fclose(fh)) {
1198 WARNING("processes: fclose: %s", STRERRNO);
1199 }
1200 return count;
1201 } /* int ps_count_maps (...) */
1202
1203 static int ps_count_fd(int pid) {
1204 char dirname[64];
1205 DIR *dh;
1206 struct dirent *ent;
1207 int count = 0;
1208
1209 snprintf(dirname, sizeof(dirname), "/proc/%i/fd", pid);
1210
1211 if ((dh = opendir(dirname)) == NULL) {
1212 DEBUG("Failed to open directory `%s'", dirname);
1213 return -1;
1214 }
1215 while ((ent = readdir(dh)) != NULL) {
1216 if (!isdigit((int)ent->d_name[0]))
1217 continue;
1218 else
1219 count++;
1220 }
1221 closedir(dh);
1222
1223 return (count >= 1) ? count : 1;
1224 } /* int ps_count_fd (pid) */
1225
1226 #if HAVE_LIBTASKSTATS
1227 static int ps_delay(process_entry_t *ps) {
1228 if (taskstats_handle == NULL) {
1229 return ENOTCONN;
1230 }
1231
1232 int status = ts_delay_by_tgid(taskstats_handle, (uint32_t)ps->id, &ps->delay);
1233 if (status == EPERM) {
1234 static c_complain_t c;
1235 #if defined(HAVE_SYS_CAPABILITY_H) && defined(CAP_NET_ADMIN)
1236 if (check_capability(CAP_NET_ADMIN) != 0) {
1237 if (getuid() == 0) {
1238 c_complain(
1239 LOG_ERR, &c,
1240 "processes plugin: Reading Delay Accounting metric failed: %s. "
1241 "collectd is running as root, but missing the CAP_NET_ADMIN "
1242 "capability. The most common cause for this is that the init "
1243 "system is dropping capabilities.",
1244 STRERROR(status));
1245 } else {
1246 c_complain(
1247 LOG_ERR, &c,
1248 "processes plugin: Reading Delay Accounting metric failed: %s. "
1249 "collectd is not running as root and missing the CAP_NET_ADMIN "
1250 "capability. Either run collectd as root or grant it the "
1251 "CAP_NET_ADMIN capability using \"setcap cap_net_admin=ep " PREFIX
1252 "/sbin/collectd\".",
1253 STRERROR(status));
1254 }
1255 } else {
1256 ERROR("processes plugin: ts_delay_by_tgid failed: %s. The CAP_NET_ADMIN "
1257 "capability is available (I checked), so this error is utterly "
1258 "unexpected.",
1259 STRERROR(status));
1260 }
1261 #else
1262 c_complain(LOG_ERR, &c,
1263 "processes plugin: Reading Delay Accounting metric failed: %s. "
1264 "Reading Delay Accounting metrics requires root privileges.",
1265 STRERROR(status));
1266 #endif
1267 return status;
1268 } else if (status != 0) {
1269 ERROR("processes plugin: ts_delay_by_tgid failed: %s", STRERROR(status));
1270 return status;
1271 }
1272
1273 return 0;
1274 }
1275 #endif
1276
1277 static void ps_fill_details(const procstat_t *ps, process_entry_t *entry) {
1278 if (entry->has_io == false) {
1279 ps_read_io(entry);
1280 entry->has_io = true;
1281 }
1282
1283 if (ps->report_ctx_switch) {
1284 if (entry->has_cswitch == false) {
1285 ps_read_tasks_status(entry);
1286 entry->has_cswitch = true;
1287 }
1288 }
1289
1290 if (ps->report_maps_num) {
1291 int num_maps;
1292 if (entry->has_maps == false && (num_maps = ps_count_maps(entry->id)) > 0) {
1293 entry->num_maps = num_maps;
1294 }
1295 entry->has_maps = true;
1296 }
1297
1298 if (ps->report_fd_num) {
1299 int num_fd;
1300 if (entry->has_fd == false && (num_fd = ps_count_fd(entry->id)) > 0) {
1301 entry->num_fd = num_fd;
1302 }
1303 entry->has_fd = true;
1304 }
1305
1306 #if HAVE_LIBTASKSTATS
1307 if (ps->report_delay && !entry->has_delay) {
1308 if (ps_delay(entry) == 0) {
1309 entry->has_delay = true;
1310 }
1311 }
1312 #endif
1313 } /* void ps_fill_details (...) */
1314
1315 /* ps_read_process reads process counters on Linux. */
1316 static int ps_read_process(long pid, process_entry_t *ps, char *state) {
1317 char filename[64];
1318 char buffer[1024];
1319
1320 char *fields[64];
1321 char fields_len;
1322
1323 size_t buffer_len;
1324
1325 char *buffer_ptr;
1326 size_t name_start_pos;
1327 size_t name_end_pos;
1328 size_t name_len;
1329
1330 derive_t cpu_user_counter;
1331 derive_t cpu_system_counter;
1332 long long unsigned vmem_size;
1333 long long unsigned vmem_rss;
1334 long long unsigned stack_size;
1335
1336 ssize_t status;
1337
1338 snprintf(filename, sizeof(filename), "/proc/%li/stat", pid);
1339
1340 status = read_text_file_contents(filename, buffer, sizeof(buffer));
1341 if (status <= 0)
1342 return -1;
1343 buffer_len = (size_t)status;
1344
1345 /* The name of the process is enclosed in parens. Since the name can
1346 * contain parens itself, spaces, numbers and pretty much everything
1347 * else, use these to determine the process name. We don't use
1348 * strchr(3) and strrchr(3) to avoid pointer arithmetic which would
1349 * otherwise be required to determine name_len. */
1350 name_start_pos = 0;
1351 while (name_start_pos < buffer_len && buffer[name_start_pos] != '(')
1352 name_start_pos++;
1353
1354 name_end_pos = buffer_len;
1355 while (name_end_pos > 0 && buffer[name_end_pos] != ')')
1356 name_end_pos--;
1357
1358 /* Either '(' or ')' is not found or they are in the wrong order.
1359 * Anyway, something weird that shouldn't happen ever. */
1360 if (name_start_pos >= name_end_pos) {
1361 ERROR("processes plugin: name_start_pos = %" PRIsz
1362 " >= name_end_pos = %" PRIsz,
1363 name_start_pos, name_end_pos);
1364 return -1;
1365 }
1366
1367 name_len = (name_end_pos - name_start_pos) - 1;
1368 if (name_len >= sizeof(ps->name))
1369 name_len = sizeof(ps->name) - 1;
1370
1371 sstrncpy(ps->name, &buffer[name_start_pos + 1], name_len + 1);
1372
1373 if ((buffer_len - name_end_pos) < 2)
1374 return -1;
1375 buffer_ptr = &buffer[name_end_pos + 2];
1376
1377 fields_len = strsplit(buffer_ptr, fields, STATIC_ARRAY_SIZE(fields));
1378 if (fields_len < 22) {
1379 DEBUG("processes plugin: ps_read_process (pid = %li):"
1380 " `%s' has only %i fields..",
1381 pid, filename, fields_len);
1382 return -1;
1383 }
1384
1385 *state = fields[0][0];
1386
1387 if (*state == 'Z') {
1388 ps->num_lwp = 0;
1389 ps->num_proc = 0;
1390 } else {
1391 ps->num_lwp = strtoul(fields[17], /* endptr = */ NULL, /* base = */ 10);
1392 if ((ps_read_status(pid, ps)) != 0) {
1393 /* No VMem data */
1394 ps->vmem_data = -1;
1395 ps->vmem_code = -1;
1396 DEBUG("ps_read_process: did not get vmem data for pid %li", pid);
1397 }
1398 if (ps->num_lwp == 0)
1399 ps->num_lwp = 1;
1400 ps->num_proc = 1;
1401 }
1402
1403 /* Leave the rest at zero if this is only a zombi */
1404 if (ps->num_proc == 0) {
1405 DEBUG("processes plugin: This is only a zombie: pid = %li; "
1406 "name = %s;",
1407 pid, ps->name);
1408 return 0;
1409 }
1410
1411 cpu_user_counter = atoll(fields[11]);
1412 cpu_system_counter = atoll(fields[12]);
1413 vmem_size = atoll(fields[20]);
1414 vmem_rss = atoll(fields[21]);
1415 ps->vmem_minflt_counter = atol(fields[7]);
1416 ps->vmem_majflt_counter = atol(fields[9]);
1417
1418 {
1419 unsigned long long stack_start = atoll(fields[25]);
1420 unsigned long long stack_ptr = atoll(fields[26]);
1421
1422 stack_size = (stack_start > stack_ptr) ? stack_start - stack_ptr
1423 : stack_ptr - stack_start;
1424 }
1425
1426 /* Convert jiffies to useconds */
1427 cpu_user_counter = cpu_user_counter * 1000000 / CONFIG_HZ;
1428 cpu_system_counter = cpu_system_counter * 1000000 / CONFIG_HZ;
1429 vmem_rss = vmem_rss * pagesize_g;
1430
1431 ps->cpu_user_counter = cpu_user_counter;
1432 ps->cpu_system_counter = cpu_system_counter;
1433 ps->vmem_size = (unsigned long)vmem_size;
1434 ps->vmem_rss = (unsigned long)vmem_rss;
1435 ps->stack_size = (unsigned long)stack_size;
1436
1437 /* no data by default. May be filled by ps_fill_details () */
1438 ps->io_rchar = -1;
1439 ps->io_wchar = -1;
1440 ps->io_syscr = -1;
1441 ps->io_syscw = -1;
1442 ps->io_diskr = -1;
1443 ps->io_diskw = -1;
1444
1445 ps->cswitch_vol = -1;
1446 ps->cswitch_invol = -1;
1447
1448 /* success */
1449 return 0;
1450 } /* int ps_read_process (...) */
1451
1452 static int procs_running(void) {
1453 char buffer[65536] = {};
1454 char id[] = "procs_running "; /* white space terminated */
1455 char *running;
1456 char *endptr = NULL;
1457 long result = 0L;
1458
1459 ssize_t status;
1460
1461 status = read_file_contents("/proc/stat", buffer, sizeof(buffer) - 1);
1462 if (status <= 0) {
1463 return -1;
1464 }
1465
1466 /* the data contains :
1467 * the literal string 'procs_running',
1468 * a whitespace
1469 * the number of running processes.
1470 * The parser does include the white-space character.
1471 */
1472 running = strstr(buffer, id);
1473 if (!running) {
1474 WARNING("procs_running not found");
1475 return -1;
1476 }
1477 running += strlen(id);
1478
1479 result = strtol(running, &endptr, 10);
1480 if ((*running != '\0') && ((*endptr == '\0') || (*endptr == '\n'))) {
1481 return (int)result;
1482 }
1483
1484 return -1;
1485 }
1486
1487 static char *ps_get_cmdline(long pid, char *name, char *buf, size_t buf_len) {
1488 char *buf_ptr;
1489 size_t len;
1490
1491 char file[PATH_MAX];
1492 int fd;
1493
1494 size_t n;
1495
1496 if ((pid < 1) || (NULL == buf) || (buf_len < 2))
1497 return NULL;
1498
1499 snprintf(file, sizeof(file), "/proc/%li/cmdline", pid);
1500
1501 errno = 0;
1502 fd = open(file, O_RDONLY);
1503 if (fd < 0) {
1504 /* ENOENT means the process exited while we were handling it.
1505 * Don't complain about this, it only fills the logs. */
1506 if (errno != ENOENT)
1507 WARNING("processes plugin: Failed to open `%s': %s.", file, STRERRNO);
1508 return NULL;
1509 }
1510
1511 buf_ptr = buf;
1512 len = buf_len;
1513
1514 n = 0;
1515
1516 while (42) {
1517 ssize_t status;
1518
1519 status = read(fd, (void *)buf_ptr, len);
1520
1521 if (status < 0) {
1522
1523 if ((EAGAIN == errno) || (EINTR == errno))
1524 continue;
1525
1526 WARNING("processes plugin: Failed to read from `%s': %s.", file,
1527 STRERRNO);
1528 close(fd);
1529 return NULL;
1530 }
1531
1532 n += status;
1533
1534 if (status == 0)
1535 break;
1536
1537 buf_ptr += status;
1538 len -= status;
1539
1540 if (len == 0)
1541 break;
1542 }
1543
1544 close(fd);
1545
1546 if (0 == n) {
1547 /* cmdline not available; e.g. kernel thread, zombie */
1548 if (NULL == name)
1549 return NULL;
1550
1551 snprintf(buf, buf_len, "[%s]", name);
1552 return buf;
1553 }
1554
1555 assert(n <= buf_len);
1556
1557 if (n == buf_len)
1558 --n;
1559 buf[n] = '\0';
1560
1561 --n;
1562 /* remove trailing whitespace */
1563 while ((n > 0) && (isspace(buf[n]) || ('\0' == buf[n]))) {
1564 buf[n] = '\0';
1565 --n;
1566 }
1567
1568 /* arguments are separated by '\0' in /proc/<pid>/cmdline */
1569 while (n > 0) {
1570 if ('\0' == buf[n])
1571 buf[n] = ' ';
1572 --n;
1573 }
1574 return buf;
1575 } /* char *ps_get_cmdline (...) */
1576
1577 static int read_fork_rate(void) {
1578 FILE *proc_stat;
1579 char buffer[1024];
1580 value_t value;
1581 bool value_valid = 0;
1582
1583 proc_stat = fopen("/proc/stat", "r");
1584 if (proc_stat == NULL) {
1585 ERROR("processes plugin: fopen (/proc/stat) failed: %s", STRERRNO);
1586 return -1;
1587 }
1588
1589 while (fgets(buffer, sizeof(buffer), proc_stat) != NULL) {
1590 int status;
1591 char *fields[3];
1592 int fields_num;
1593
1594 fields_num = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
1595 if (fields_num != 2)
1596 continue;
1597
1598 if (strcmp("processes", fields[0]) != 0)
1599 continue;
1600
1601 status = parse_value(fields[1], &value, DS_TYPE_DERIVE);
1602 if (status == 0)
1603 value_valid = 1;
1604
1605 break;
1606 }
1607 fclose(proc_stat);
1608
1609 if (!value_valid)
1610 return -1;
1611
1612 ps_submit_fork_rate(value.derive);
1613 return 0;
1614 }
1615 #endif /*KERNEL_LINUX */
1616
1617 #if KERNEL_SOLARIS
1618 static char *ps_get_cmdline(long pid,
1619 char *name __attribute__((unused)), /* {{{ */
1620 char *buffer, size_t buffer_size) {
1621 char path[PATH_MAX];
1622 psinfo_t info;
1623 ssize_t status;
1624
1625 snprintf(path, sizeof(path), "/proc/%li/psinfo", pid);
1626
1627 status = read_file_contents(path, &info, sizeof(info));
1628 if ((status < 0) || (((size_t)status) != sizeof(info))) {
1629 ERROR("processes plugin: Unexpected return value "
1630 "while reading \"%s\": "
1631 "Returned %zd but expected %" PRIsz ".",
1632 path, status, buffer_size);
1633 return NULL;
1634 }
1635
1636 sstrncpy(buffer, info.pr_psargs, buffer_size);
1637
1638 return buffer;
1639 } /* }}} int ps_get_cmdline */
1640
1641 /*
1642 * Reads process information on the Solaris OS. The information comes mainly
1643 * from
1644 * /proc/PID/status, /proc/PID/psinfo and /proc/PID/usage
1645 * The values for input and ouput chars are calculated "by hand"
1646 * Added a few "solaris" specific process states as well
1647 */
1648 static int ps_read_process(long pid, process_entry_t *ps, char *state) {
1649 char filename[64];
1650 char f_psinfo[64], f_usage[64];
1651 char *buffer;
1652
1653 pstatus_t *myStatus;
1654 psinfo_t *myInfo;
1655 prusage_t *myUsage;
1656
1657 snprintf(filename, sizeof(filename), "/proc/%li/status", pid);
1658 snprintf(f_psinfo, sizeof(f_psinfo), "/proc/%li/psinfo", pid);
1659 snprintf(f_usage, sizeof(f_usage), "/proc/%li/usage", pid);
1660
1661 buffer = calloc(1, sizeof(pstatus_t));
1662 read_file_contents(filename, buffer, sizeof(pstatus_t));
1663 myStatus = (pstatus_t *)buffer;
1664
1665 buffer = calloc(1, sizeof(psinfo_t));
1666 read_file_contents(f_psinfo, buffer, sizeof(psinfo_t));
1667 myInfo = (psinfo_t *)buffer;
1668
1669 buffer = calloc(1, sizeof(prusage_t));
1670 read_file_contents(f_usage, buffer, sizeof(prusage_t));
1671 myUsage = (prusage_t *)buffer;
1672
1673 sstrncpy(ps->name, myInfo->pr_fname, sizeof(myInfo->pr_fname));
1674 ps->num_lwp = myStatus->pr_nlwp;
1675 if (myInfo->pr_wstat != 0) {
1676 ps->num_proc = 0;
1677 ps->num_lwp = 0;
1678 *state = (char)'Z';
1679
1680 sfree(myStatus);
1681 sfree(myInfo);
1682 sfree(myUsage);
1683 return 0;
1684 } else {
1685 ps->num_proc = 1;
1686 ps->num_lwp = myInfo->pr_nlwp;
1687 }
1688
1689 /*
1690 * Convert system time and user time from nanoseconds to microseconds
1691 * for compatibility with the linux module
1692 */
1693 ps->cpu_system_counter = myStatus->pr_stime.tv_nsec / 1000;
1694 ps->cpu_user_counter = myStatus->pr_utime.tv_nsec / 1000;
1695
1696 /*
1697 * Convert rssize from KB to bytes to be consistent w/ the linux module
1698 */
1699 ps->vmem_rss = myInfo->pr_rssize * 1024;
1700 ps->vmem_size = myInfo->pr_size * 1024;
1701 ps->vmem_minflt_counter = myUsage->pr_minf;
1702 ps->vmem_majflt_counter = myUsage->pr_majf;
1703
1704 /*
1705 * TODO: Data and code segment calculations for Solaris
1706 */
1707
1708 ps->vmem_data = -1;
1709 ps->vmem_code = -1;
1710 ps->stack_size = myStatus->pr_stksize;
1711
1712 /*
1713 * TODO: File descriptor count for Solaris
1714 */
1715 ps->num_fd = 0;
1716
1717 /* Number of memory mappings */
1718 ps->num_maps = 0;
1719
1720 /*
1721 * Calculating input/ouput chars
1722 * Formula used is total chars / total blocks => chars/block
1723 * then convert input/output blocks to chars
1724 */
1725 ulong_t tot_chars = myUsage->pr_ioch;
1726 ulong_t tot_blocks = myUsage->pr_inblk + myUsage->pr_oublk;
1727 ulong_t chars_per_block = 1;
1728 if (tot_blocks != 0)
1729 chars_per_block = tot_chars / tot_blocks;
1730 ps->io_rchar = myUsage->pr_inblk * chars_per_block;
1731 ps->io_wchar = myUsage->pr_oublk * chars_per_block;
1732 ps->io_syscr = myUsage->pr_sysc;
1733 ps->io_syscw = myUsage->pr_sysc;
1734 ps->io_diskr = -1;
1735 ps->io_diskw = -1;
1736
1737 /*
1738 * TODO: context switch counters for Solaris
1739 */
1740 ps->cswitch_vol = -1;
1741 ps->cswitch_invol = -1;
1742
1743 /*
1744 * TODO: Find way of setting BLOCKED and PAGING status
1745 */
1746
1747 *state = (char)'R';
1748 if (myStatus->pr_flags & PR_ASLEEP)
1749 *state = (char)'S';
1750 else if (myStatus->pr_flags & PR_STOPPED)
1751 *state = (char)'T';
1752 else if (myStatus->pr_flags & PR_DETACH)
1753 *state = (char)'E';
1754 else if (myStatus->pr_flags & PR_DAEMON)
1755 *state = (char)'A';
1756 else if (myStatus->pr_flags & PR_ISSYS)
1757 *state = (char)'Y';
1758 else if (myStatus->pr_flags & PR_ORPHAN)
1759 *state = (char)'O';
1760
1761 sfree(myStatus);
1762 sfree(myInfo);
1763 sfree(myUsage);
1764
1765 return 0;
1766 }
1767
1768 /*
1769 * Reads the number of threads created since the last reboot. On Solaris these
1770 * are retrieved from kstat (module cpu, name sys, class misc, stat nthreads).
1771 * The result is the sum for all the threads created on each cpu
1772 */
1773 static int read_fork_rate(void) {
1774 extern kstat_ctl_t *kc;
1775 derive_t result = 0;
1776
1777 if (kc == NULL)
1778 return -1;
1779
1780 for (kstat_t *ksp_chain = kc->kc_chain; ksp_chain != NULL;
1781 ksp_chain = ksp_chain->ks_next) {
1782 if ((strcmp(ksp_chain->ks_module, "cpu") == 0) &&
1783 (strcmp(ksp_chain->ks_name, "sys") == 0) &&
1784 (strcmp(ksp_chain->ks_class, "misc") == 0)) {
1785 long long tmp;
1786
1787 kstat_read(kc, ksp_chain, NULL);
1788
1789 tmp = get_kstat_value(ksp_chain, "nthreads");
1790 if (tmp != -1LL)
1791 result += tmp;
1792 }
1793 }
1794
1795 ps_submit_fork_rate(result);
1796 return 0;
1797 }
1798 #endif /* KERNEL_SOLARIS */
1799
1800 #if HAVE_THREAD_INFO
1801 static int mach_get_task_name(task_t t, int *pid, char *name,
1802 size_t name_max_len) {
1803 int mib[4];
1804
1805 struct kinfo_proc kp;
1806 size_t kp_size;
1807
1808 mib[0] = CTL_KERN;
1809 mib[1] = KERN_PROC;
1810 mib[2] = KERN_PROC_PID;
1811
1812 if (pid_for_task(t, pid) != KERN_SUCCESS)
1813 return -1;
1814 mib[3] = *pid;
1815
1816 kp_size = sizeof(kp);
1817 if (sysctl(mib, 4, &kp, &kp_size, NULL, 0) != 0)
1818 return -1;
1819
1820 if (name_max_len > (MAXCOMLEN + 1))
1821 name_max_len = MAXCOMLEN + 1;
1822
1823 strncpy(name, kp.kp_proc.p_comm, name_max_len - 1);
1824 name[name_max_len - 1] = '\0';
1825
1826 DEBUG("pid = %i; name = %s;", *pid, name);
1827
1828 /* We don't do the special handling for `p_comm == "LaunchCFMApp"' as
1829 * `top' does it, because it is a lot of work and only used when
1830 * debugging. -octo */
1831
1832 return 0;
1833 }
1834 #endif /* HAVE_THREAD_INFO */
1835 /* end of additional functions for KERNEL_LINUX/HAVE_THREAD_INFO */
1836
1837 /* do actual readings from kernel */
1838 static int ps_read(void) {
1839 #if HAVE_THREAD_INFO
1840 kern_return_t status;
1841
1842 processor_set_t port_pset_priv;
1843
1844 task_array_t task_list;
1845 mach_msg_type_number_t task_list_len;
1846
1847 int task_pid;
1848 char task_name[MAXCOMLEN + 1];
1849
1850 thread_act_array_t thread_list;
1851 mach_msg_type_number_t thread_list_len;
1852 thread_basic_info_data_t thread_data;
1853 mach_msg_type_number_t thread_data_len;
1854
1855 int running = 0;
1856 int sleeping = 0;
1857 int zombies = 0;
1858 int stopped = 0;
1859 int blocked = 0;
1860
1861 procstat_t *ps;
1862 process_entry_t pse;
1863
1864 ps_list_reset();
1865
1866 /*
1867 * The Mach-concept is a little different from the traditional UNIX
1868 * concept: All the work is done in threads. Threads are contained in
1869 * `tasks'. Therefore, `task status' doesn't make much sense, since
1870 * it's actually a `thread status'.
1871 * Tasks are assigned to sets of processors, so that's where you go to
1872 * get a list.
1873 */
1874 for (mach_msg_type_number_t pset = 0; pset < pset_list_len; pset++) {
1875 if ((status = host_processor_set_priv(port_host_self, pset_list[pset],
1876 &port_pset_priv)) != KERN_SUCCESS) {
1877 ERROR("host_processor_set_priv failed: %s\n", mach_error_string(status));
1878 continue;
1879 }
1880
1881 if ((status = processor_set_tasks(port_pset_priv, &task_list,
1882 &task_list_len)) != KERN_SUCCESS) {
1883 ERROR("processor_set_tasks failed: %s\n", mach_error_string(status));
1884 mach_port_deallocate(port_task_self, port_pset_priv);
1885 continue;
1886 }
1887
1888 for (mach_msg_type_number_t task = 0; task < task_list_len; task++) {
1889 ps = NULL;
1890 if (mach_get_task_name(task_list[task], &task_pid, task_name,
1891 PROCSTAT_NAME_LEN) == 0) {
1892 /* search for at least one match */
1893 for (ps = list_head_g; ps != NULL; ps = ps->next)
1894 /* FIXME: cmdline should be here instead of NULL */
1895 if (ps_list_match(task_name, NULL, ps) == 1)
1896 break;
1897 }
1898
1899 /* Collect more detailed statistics for this process */
1900 if (ps != NULL) {
1901 task_basic_info_data_t task_basic_info;
1902 mach_msg_type_number_t task_basic_info_len;
1903 task_events_info_data_t task_events_info;
1904 mach_msg_type_number_t task_events_info_len;
1905 task_absolutetime_info_data_t task_absolutetime_info;
1906 mach_msg_type_number_t task_absolutetime_info_len;
1907
1908 memset(&pse, '\0', sizeof(pse));
1909 pse.id = task_pid;
1910
1911 task_basic_info_len = TASK_BASIC_INFO_COUNT;
1912 status = task_info(task_list[task], TASK_BASIC_INFO,
1913 (task_info_t)&task_basic_info, &task_basic_info_len);
1914 if (status != KERN_SUCCESS) {
1915 ERROR("task_info failed: %s", mach_error_string(status));
1916 continue; /* with next thread_list */
1917 }
1918
1919 task_events_info_len = TASK_EVENTS_INFO_COUNT;
1920 status =
1921 task_info(task_list[task], TASK_EVENTS_INFO,
1922 (task_info_t)&task_events_info, &task_events_info_len);
1923 if (status != KERN_SUCCESS) {
1924 ERROR("task_info failed: %s", mach_error_string(status));
1925 continue; /* with next thread_list */
1926 }
1927
1928 task_absolutetime_info_len = TASK_ABSOLUTETIME_INFO_COUNT;
1929 status = task_info(task_list[task], TASK_ABSOLUTETIME_INFO,
1930 (task_info_t)&task_absolutetime_info,
1931 &task_absolutetime_info_len);
1932 if (status != KERN_SUCCESS) {
1933 ERROR("task_info failed: %s", mach_error_string(status));
1934 continue; /* with next thread_list */
1935 }
1936
1937 pse.num_proc++;
1938 pse.vmem_size = task_basic_info.virtual_size;
1939 pse.vmem_rss = task_basic_info.resident_size;
1940 /* Does not seem to be easily exposed */
1941 pse.vmem_data = 0;
1942 pse.vmem_code = 0;
1943
1944 pse.io_rchar = -1;
1945 pse.io_wchar = -1;
1946 pse.io_syscr = -1;
1947 pse.io_syscw = -1;
1948 pse.io_diskr = -1;
1949 pse.io_diskw = -1;
1950
1951 /* File descriptor count not implemented */
1952 pse.num_fd = 0;
1953
1954 /* Number of memory mappings */
1955 pse.num_maps = 0;
1956
1957 pse.vmem_minflt_counter = task_events_info.cow_faults;
1958 pse.vmem_majflt_counter = task_events_info.faults;
1959
1960 pse.cpu_user_counter = task_absolutetime_info.total_user;
1961 pse.cpu_system_counter = task_absolutetime_info.total_system;
1962
1963 /* context switch counters not implemented */
1964 pse.cswitch_vol = -1;
1965 pse.cswitch_invol = -1;
1966 }
1967
1968 status = task_threads(task_list[task], &thread_list, &thread_list_len);
1969 if (status != KERN_SUCCESS) {
1970 /* Apple's `top' treats this case a zombie. It
1971 * makes sense to some extend: A `zombie'
1972 * thread is nonsense, since the task/process
1973 * is dead. */
1974 zombies++;
1975 DEBUG("task_threads failed: %s", mach_error_string(status));
1976 if (task_list[task] != port_task_self)
1977 mach_port_deallocate(port_task_self, task_list[task]);
1978 continue; /* with next task_list */
1979 }
1980
1981 for (mach_msg_type_number_t thread = 0; thread < thread_list_len;
1982 thread++) {
1983 thread_data_len = THREAD_BASIC_INFO_COUNT;
1984 status = thread_info(thread_list[thread], THREAD_BASIC_INFO,
1985 (thread_info_t)&thread_data, &thread_data_len);
1986 if (status != KERN_SUCCESS) {
1987 ERROR("thread_info failed: %s", mach_error_string(status));
1988 if (task_list[task] != port_task_self)
1989 mach_port_deallocate(port_task_self, thread_list[thread]);
1990 continue; /* with next thread_list */
1991 }
1992
1993 if (ps != NULL)
1994 pse.num_lwp++;
1995
1996 switch (thread_data.run_state) {
1997 case TH_STATE_RUNNING:
1998 running++;
1999 break;
2000 case TH_STATE_STOPPED:
2001 /* What exactly is `halted'? */
2002 case TH_STATE_HALTED:
2003 stopped++;
2004 break;
2005 case TH_STATE_WAITING:
2006 sleeping++;
2007 break;
2008 case TH_STATE_UNINTERRUPTIBLE:
2009 blocked++;
2010 break;
2011 /* There is no `zombie' case here,
2012 * since there are no zombie-threads.
2013 * There's only zombie tasks, which are
2014 * handled above. */
2015 default:
2016 WARNING("Unknown thread status: %i", thread_data.run_state);
2017 break;
2018 } /* switch (thread_data.run_state) */
2019
2020 if (task_list[task] != port_task_self) {
2021 status = mach_port_deallocate(port_task_self, thread_list[thread]);
2022 if (status != KERN_SUCCESS)
2023 ERROR("mach_port_deallocate failed: %s", mach_error_string(status));
2024 }
2025 } /* for (thread_list) */
2026
2027 if ((status = vm_deallocate(port_task_self, (vm_address_t)thread_list,
2028 thread_list_len * sizeof(thread_act_t))) !=
2029 KERN_SUCCESS) {
2030 ERROR("vm_deallocate failed: %s", mach_error_string(status));
2031 }
2032 thread_list = NULL;
2033 thread_list_len = 0;
2034
2035 /* Only deallocate the task port, if it isn't our own.
2036 * Don't know what would happen in that case, but this
2037 * is what Apple's top does.. ;) */
2038 if (task_list[task] != port_task_self) {
2039 status = mach_port_deallocate(port_task_self, task_list[task]);
2040 if (status != KERN_SUCCESS)
2041 ERROR("mach_port_deallocate failed: %s", mach_error_string(status));
2042 }
2043
2044 if (ps != NULL)
2045 /* FIXME: cmdline should be here instead of NULL */
2046 ps_list_add(task_name, NULL, &pse);
2047 } /* for (task_list) */
2048
2049 if ((status = vm_deallocate(port_task_self, (vm_address_t)task_list,
2050 task_list_len * sizeof(task_t))) !=
2051 KERN_SUCCESS) {
2052 ERROR("vm_deallocate failed: %s", mach_error_string(status));
2053 }
2054 task_list = NULL;
2055 task_list_len = 0;
2056
2057 if ((status = mach_port_deallocate(port_task_self, port_pset_priv)) !=
2058 KERN_SUCCESS) {
2059 ERROR("mach_port_deallocate failed: %s", mach_error_string(status));
2060 }
2061 } /* for (pset_list) */
2062
2063 ps_submit_state("running", running);
2064 ps_submit_state("sleeping", sleeping);
2065 ps_submit_state("zombies", zombies);
2066 ps_submit_state("stopped", stopped);
2067 ps_submit_state("blocked", blocked);
2068
2069 for (ps = list_head_g; ps != NULL; ps = ps->next)
2070 ps_submit_proc_list(ps);
2071 /* #endif HAVE_THREAD_INFO */
2072
2073 #elif KERNEL_LINUX
2074 int running = 0;
2075 int sleeping = 0;
2076 int zombies = 0;
2077 int stopped = 0;
2078 int paging = 0;
2079 int blocked = 0;
2080
2081 struct dirent *ent;
2082 DIR *proc;
2083 long pid;
2084
2085 char cmdline[CMDLINE_BUFFER_SIZE];
2086
2087 int status;
2088 process_entry_t pse;
2089 char state;
2090
2091 running = sleeping = zombies = stopped = paging = blocked = 0;
2092 ps_list_reset();
2093
2094 if ((proc = opendir("/proc")) == NULL) {
2095 ERROR("Cannot open `/proc': %s", STRERRNO);
2096 return -1;
2097 }
2098
2099 while ((ent = readdir(proc)) != NULL) {
2100 if (!isdigit(ent->d_name[0]))
2101 continue;
2102
2103 if ((pid = atol(ent->d_name)) < 1)
2104 continue;
2105
2106 memset(&pse, 0, sizeof(pse));
2107 pse.id = pid;
2108
2109 status = ps_read_process(pid, &pse, &state);
2110 if (status != 0) {
2111 DEBUG("ps_read_process failed: %i", status);
2112 continue;
2113 }
2114
2115 switch (state) {
2116 case 'R':
2117 running++;
2118 break;
2119 case 'S':
2120 sleeping++;
2121 break;
2122 case 'D':
2123 blocked++;
2124 break;
2125 case 'Z':
2126 zombies++;
2127 break;
2128 case 'T':
2129 stopped++;
2130 break;
2131 case 'W':
2132 paging++;
2133 break;
2134 }
2135
2136 ps_list_add(pse.name,
2137 ps_get_cmdline(pid, pse.name, cmdline, sizeof(cmdline)), &pse);
2138 }
2139
2140 closedir(proc);
2141
2142 /* get procs_running from /proc/stat
2143 * scanning /proc/stat AND computing other process stats takes too much time.
2144 * Consequently, the number of running processes based on the occurences
2145 * of 'R' as character indicating the running state is typically zero. Due
2146 * to processes are actually changing state during the evaluation of it's
2147 * stat(s).
2148 * The 'procs_running' number in /proc/stat on the other hand is more
2149 * accurate, and can be retrieved in a single 'read' call. */
2150 running = procs_running();
2151
2152 ps_submit_state("running", running);
2153 ps_submit_state("sleeping", sleeping);
2154 ps_submit_state("zombies", zombies);
2155 ps_submit_state("stopped", stopped);
2156 ps_submit_state("paging", paging);
2157 ps_submit_state("blocked", blocked);
2158
2159 for (procstat_t *ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
2160 ps_submit_proc_list(ps_ptr);
2161
2162 read_fork_rate();
2163 /* #endif KERNEL_LINUX */
2164
2165 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD
2166 int running = 0;
2167 int sleeping = 0;
2168 int zombies = 0;
2169 int stopped = 0;
2170 int blocked = 0;
2171 int idle = 0;
2172 int wait = 0;
2173
2174 kvm_t *kd;
2175 char errbuf[_POSIX2_LINE_MAX];
2176 struct kinfo_proc *procs; /* array of processes */
2177 struct kinfo_proc *proc_ptr = NULL;
2178 int count; /* returns number of processes */
2179
2180 process_entry_t pse;
2181
2182 ps_list_reset();
2183
2184 /* Open the kvm interface, get a descriptor */
2185 kd = kvm_openfiles(NULL, "/dev/null", NULL, 0, errbuf);
2186 if (kd == NULL) {
2187 ERROR("processes plugin: Cannot open kvm interface: %s", errbuf);
2188 return 0;
2189 }
2190
2191 /* Get the list of processes. */
2192 procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, &count);
2193 if (procs == NULL) {
2194 ERROR("processes plugin: Cannot get kvm processes list: %s",
2195 kvm_geterr(kd));
2196 kvm_close(kd);
2197 return 0;
2198 }
2199
2200 /* Iterate through the processes in kinfo_proc */
2201 for (int i = 0; i < count; i++) {
2202 /* Create only one process list entry per _process_, i.e.
2203 * filter out threads (duplicate PID entries). */
2204 if ((proc_ptr == NULL) || (proc_ptr->ki_pid != procs[i].ki_pid)) {
2205 char cmdline[CMDLINE_BUFFER_SIZE] = "";
2206 bool have_cmdline = 0;
2207
2208 proc_ptr = &(procs[i]);
2209 /* Don't probe system processes and processes without arguments */
2210 if (((procs[i].ki_flag & P_SYSTEM) == 0) && (procs[i].ki_args != NULL)) {
2211 char **argv;
2212 int argc;
2213 int status;
2214
2215 /* retrieve the arguments */
2216 argv = kvm_getargv(kd, proc_ptr, /* nchr = */ 0);
2217 argc = 0;
2218 if ((argv != NULL) && (argv[0] != NULL)) {
2219 while (argv[argc] != NULL)
2220 argc++;
2221
2222 status = strjoin(cmdline, sizeof(cmdline), argv, argc, " ");
2223 if (status < 0)
2224 WARNING("processes plugin: Command line did not fit into buffer.");
2225 else
2226 have_cmdline = 1;
2227 }
2228 } /* if (process has argument list) */
2229
2230 memset(&pse, 0, sizeof(pse));
2231 pse.id = procs[i].ki_pid;
2232
2233 pse.num_proc = 1;
2234 pse.num_lwp = procs[i].ki_numthreads;
2235
2236 pse.vmem_size = procs[i].ki_size;
2237 pse.vmem_rss = procs[i].ki_rssize * pagesize;
2238 pse.vmem_data = procs[i].ki_dsize * pagesize;
2239 pse.vmem_code = procs[i].ki_tsize * pagesize;
2240 pse.stack_size = procs[i].ki_ssize * pagesize;
2241 pse.vmem_minflt_counter = procs[i].ki_rusage.ru_minflt;
2242 pse.vmem_majflt_counter = procs[i].ki_rusage.ru_majflt;
2243
2244 pse.cpu_user_counter = 0;
2245 pse.cpu_system_counter = 0;
2246 /*
2247 * The u-area might be swapped out, and we can't get
2248 * at it because we have a crashdump and no swap.
2249 * If it's here fill in these fields, otherwise, just
2250 * leave them 0.
2251 */
2252 if (procs[i].ki_flag & P_INMEM) {
2253 pse.cpu_user_counter = procs[i].ki_rusage.ru_utime.tv_usec +
2254 (1000000lu * procs[i].ki_rusage.ru_utime.tv_sec);
2255 pse.cpu_system_counter =
2256 procs[i].ki_rusage.ru_stime.tv_usec +
2257 (1000000lu * procs[i].ki_rusage.ru_stime.tv_sec);
2258 }
2259
2260 /* no I/O data */
2261 pse.io_rchar = -1;
2262 pse.io_wchar = -1;
2263 pse.io_syscr = -1;
2264 pse.io_syscw = -1;
2265 pse.io_diskr = -1;
2266 pse.io_diskw = -1;
2267
2268 /* file descriptor count not implemented */
2269 pse.num_fd = 0;
2270
2271 /* Number of memory mappings */
2272 pse.num_maps = 0;
2273
2274 /* context switch counters not implemented */
2275 pse.cswitch_vol = -1;
2276 pse.cswitch_invol = -1;
2277
2278 ps_list_add(procs[i].ki_comm, have_cmdline ? cmdline : NULL, &pse);
2279
2280 switch (procs[i].ki_stat) {
2281 case SSTOP:
2282 stopped++;
2283 break;
2284 case SSLEEP:
2285 sleeping++;
2286 break;
2287 case SRUN:
2288 running++;
2289 break;
2290 case SIDL:
2291 idle++;
2292 break;
2293 case SWAIT:
2294 wait++;
2295 break;
2296 case SLOCK:
2297 blocked++;
2298 break;
2299 case SZOMB:
2300 zombies++;
2301 break;
2302 }
2303 } /* if ((proc_ptr == NULL) || (proc_ptr->ki_pid != procs[i].ki_pid)) */
2304 }
2305
2306 kvm_close(kd);
2307
2308 ps_submit_state("running", running);
2309 ps_submit_state("sleeping", sleeping);
2310 ps_submit_state("zombies", zombies);
2311 ps_submit_state("stopped", stopped);
2312 ps_submit_state("blocked", blocked);
2313 ps_submit_state("idle", idle);
2314 ps_submit_state("wait", wait);
2315
2316 for (procstat_t *ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
2317 ps_submit_proc_list(ps_ptr);
2318 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
2319
2320 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC2_NETBSD
2321 int running = 0;
2322 int sleeping = 0;
2323 int zombies = 0;
2324 int stopped = 0;
2325 int blocked = 0;
2326 int idle = 0;
2327 int wait = 0;
2328
2329 kvm_t *kd;
2330 char errbuf[_POSIX2_LINE_MAX];
2331 struct kinfo_proc2 *procs; /* array of processes */
2332 struct kinfo_proc2 *proc_ptr = NULL;
2333 struct kinfo_proc2 *p;
2334 int count; /* returns number of processes */
2335 int i;
2336 int l, nlwps;
2337 struct kinfo_lwp *kl;
2338
2339 procstat_t *ps_ptr;
2340 process_entry_t pse;
2341
2342 ps_list_reset();
2343
2344 /* Open the kvm interface, get a descriptor */
2345 kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf);
2346 if (kd == NULL) {
2347 ERROR("processes plugin: Cannot open kvm interface: %s", errbuf);
2348 return (0);
2349 }
2350
2351 /* Get the list of processes. */
2352 procs =
2353 kvm_getproc2(kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc2), &count);
2354 if (procs == NULL) {
2355 ERROR("processes plugin: Cannot get kvm processes list: %s",
2356 kvm_geterr(kd));
2357 kvm_close(kd);
2358 return (0);
2359 }
2360
2361 /* Iterate through the processes in kinfo_proc */
2362 for (i = 0; i < count; i++) {
2363 /* Create only one process list entry per _process_, i.e.
2364 * filter out threads (duplicate PID entries). */
2365 if ((proc_ptr == NULL) || (proc_ptr->p_pid != procs[i].p_pid)) {
2366 char cmdline[CMDLINE_BUFFER_SIZE] = "";
2367 _Bool have_cmdline = 0;
2368
2369 proc_ptr = &(procs[i]);
2370 /* Don't probe system processes and processes without arguments */
2371 if (((procs[i].p_flag & P_SYSTEM) == 0) && (procs[i].p_comm[0] != 0)) {
2372 char **argv;
2373 int argc;
2374 int status;
2375
2376 /* retrieve the arguments */
2377 argv = kvm_getargv2(kd, proc_ptr, 0);
2378 argc = 0;
2379 if ((argv != NULL) && (argv[0] != NULL)) {
2380 while (argv[argc] != NULL)
2381 argc++;
2382
2383 status = strjoin(cmdline, sizeof(cmdline), argv, argc, " ");
2384 if (status < 0)
2385 WARNING("processes plugin: Command line did not fit into buffer.");
2386 else
2387 have_cmdline = 1;
2388 }
2389 } /* if (process has argument list) */
2390
2391 memset(&pse, 0, sizeof(pse));
2392 pse.id = procs[i].p_pid;
2393
2394 pse.num_proc = 1;
2395 pse.num_lwp = procs[i].p_nlwps;
2396
2397 pse.vmem_size = procs[i].p_uru_maxrss * pagesize;
2398 pse.vmem_rss = procs[i].p_vm_rssize * pagesize;
2399 pse.vmem_data = procs[i].p_vm_dsize * pagesize;
2400 pse.vmem_code = procs[i].p_vm_tsize * pagesize;
2401 pse.stack_size = procs[i].p_vm_ssize * pagesize;
2402 pse.vmem_minflt_counter = procs[i].p_uru_minflt;
2403 pse.vmem_majflt_counter = procs[i].p_uru_majflt;
2404
2405 pse.cpu_user_counter = 0;
2406 pse.cpu_system_counter = 0;
2407 /* context switch counters not implemented */
2408 pse.cswitch_vol = -1;
2409 pse.cswitch_invol = -1;
2410
2411 /*
2412 * The u-area might be swapped out, and we can't get
2413 * at it because we have a crashdump and no swap.
2414 * If it's here fill in these fields, otherwise, just
2415 * leave them 0.
2416 */
2417 if (procs[i].p_flag & P_INMEM) {
2418 pse.cpu_user_counter =
2419 procs[i].p_uutime_usec + (1000000lu * procs[i].p_uutime_sec);
2420 pse.cpu_system_counter =
2421 procs[i].p_ustime_usec + (1000000lu * procs[i].p_ustime_sec);
2422 }
2423
2424 /* no I/O data */
2425 pse.io_rchar = -1;
2426 pse.io_wchar = -1;
2427 pse.io_syscr = procs[i].p_uru_inblock;
2428 pse.io_syscw = procs[i].p_uru_oublock;
2429
2430 /* file descriptor count not implemented */
2431 pse.num_fd = 0;
2432
2433 /* Number of memory mappings */
2434 pse.num_maps = 0;
2435
2436 /* context switch counters not implemented */
2437 pse.cswitch_vol = -1;
2438 pse.cswitch_invol = -1;
2439
2440 ps_list_add(procs[i].p_comm, have_cmdline ? cmdline : NULL, &pse);
2441 } /* if ((proc_ptr == NULL) || (proc_ptr->ki_pid != procs[i].ki_pid)) */
2442
2443 /* system processes' LWPs end up in "running" state */
2444 if ((procs[i].p_flag & P_SYSTEM) != 0)
2445 continue;
2446
2447 switch (procs[i].p_realstat) {
2448 case SSTOP:
2449 case SACTIVE:
2450 case SIDL:
2451 p = &(procs[i]);
2452 /* get info about LWPs */
2453 kl = kvm_getlwps(kd, p->p_pid, (u_long)p->p_paddr,
2454 sizeof(struct kinfo_lwp), &nlwps);
2455
2456 for (l = 0; kl && l < nlwps; l++) {
2457 switch (kl[l].l_stat) {
2458 case LSONPROC:
2459 case LSRUN:
2460 running++;
2461 break;
2462 case LSSLEEP:
2463 if (kl[l].l_flag & L_SINTR) {
2464 if (kl[l].l_slptime > maxslp)
2465 idle++;
2466 else
2467 sleeping++;
2468 } else
2469 blocked++;
2470 break;
2471 case LSSTOP:
2472 stopped++;
2473 break;
2474 case LSIDL:
2475 idle++;
2476 break;
2477 }
2478 }
2479 break;
2480 case SZOMB:
2481 case SDYING:
2482 case SDEAD:
2483 zombies++;
2484 break;
2485 }
2486 }
2487
2488 kvm_close(kd);
2489
2490 ps_submit_state("running", running);
2491 ps_submit_state("sleeping", sleeping);
2492 ps_submit_state("zombies", zombies);
2493 ps_submit_state("stopped", stopped);
2494 ps_submit_state("blocked", blocked);
2495 ps_submit_state("idle", idle);
2496 ps_submit_state("wait", wait);
2497
2498 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
2499 ps_submit_proc_list(ps_ptr);
2500 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC2_NETBSD */
2501
2502 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_OPENBSD
2503 int running = 0;
2504 int sleeping = 0;
2505 int zombies = 0;
2506 int stopped = 0;
2507 int onproc = 0;
2508 int idle = 0;
2509 int dead = 0;
2510
2511 kvm_t *kd;
2512 char errbuf[1024];
2513 struct kinfo_proc *procs; /* array of processes */
2514 struct kinfo_proc *proc_ptr = NULL;
2515 int count; /* returns number of processes */
2516
2517 process_entry_t pse;
2518
2519 ps_list_reset();
2520
2521 /* Open the kvm interface, get a descriptor */
2522 kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf);
2523 if (kd == NULL) {
2524 ERROR("processes plugin: Cannot open kvm interface: %s", errbuf);
2525 return 0;
2526 }
2527
2528 /* Get the list of processes. */
2529 procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc), &count);
2530 if (procs == NULL) {
2531 ERROR("processes plugin: Cannot get kvm processes list: %s",
2532 kvm_geterr(kd));
2533 kvm_close(kd);
2534 return 0;
2535 }
2536
2537 /* Iterate through the processes in kinfo_proc */
2538 for (int i = 0; i < count; i++) {
2539 /* Create only one process list entry per _process_, i.e.
2540 * filter out threads (duplicate PID entries). */
2541 if ((proc_ptr == NULL) || (proc_ptr->p_pid != procs[i].p_pid)) {
2542 char cmdline[CMDLINE_BUFFER_SIZE] = "";
2543 bool have_cmdline = 0;
2544
2545 proc_ptr = &(procs[i]);
2546 /* Don't probe zombie processes */
2547 if (!P_ZOMBIE(proc_ptr)) {
2548 char **argv;
2549 int argc;
2550 int status;
2551
2552 /* retrieve the arguments */
2553 argv = kvm_getargv(kd, proc_ptr, /* nchr = */ 0);
2554 argc = 0;
2555 if ((argv != NULL) && (argv[0] != NULL)) {
2556 while (argv[argc] != NULL)
2557 argc++;
2558
2559 status = strjoin(cmdline, sizeof(cmdline), argv, argc, " ");
2560 if (status < 0)
2561 WARNING("processes plugin: Command line did not fit into buffer.");
2562 else
2563 have_cmdline = 1;
2564 }
2565 } /* if (process has argument list) */
2566
2567 memset(&pse, 0, sizeof(pse));
2568 pse.id = procs[i].p_pid;
2569
2570 pse.num_proc = 1;
2571 pse.num_lwp = 1; /* XXX: accumulate p_tid values for a single p_pid ? */
2572
2573 pse.vmem_rss = procs[i].p_vm_rssize * pagesize;
2574 pse.vmem_data = procs[i].p_vm_dsize * pagesize;
2575 pse.vmem_code = procs[i].p_vm_tsize * pagesize;
2576 pse.stack_size = procs[i].p_vm_ssize * pagesize;
2577 pse.vmem_size = pse.stack_size + pse.vmem_code + pse.vmem_data;
2578 pse.vmem_minflt_counter = procs[i].p_uru_minflt;
2579 pse.vmem_majflt_counter = procs[i].p_uru_majflt;
2580
2581 pse.cpu_user_counter =
2582 procs[i].p_uutime_usec + (1000000lu * procs[i].p_uutime_sec);
2583 pse.cpu_system_counter =
2584 procs[i].p_ustime_usec + (1000000lu * procs[i].p_ustime_sec);
2585
2586 /* no I/O data */
2587 pse.io_rchar = -1;
2588 pse.io_wchar = -1;
2589 pse.io_syscr = -1;
2590 pse.io_syscw = -1;
2591 pse.io_diskr = -1;
2592 pse.io_diskw = -1;
2593
2594 /* file descriptor count not implemented */
2595 pse.num_fd = 0;
2596
2597 /* Number of memory mappings */
2598 pse.num_maps = 0;
2599
2600 /* context switch counters not implemented */
2601 pse.cswitch_vol = -1;
2602 pse.cswitch_invol = -1;
2603
2604 ps_list_add(procs[i].p_comm, have_cmdline ? cmdline : NULL, &pse);
2605
2606 switch (procs[i].p_stat) {
2607 case SSTOP:
2608 stopped++;
2609 break;
2610 case SSLEEP:
2611 sleeping++;
2612 break;
2613 case SRUN:
2614 running++;
2615 break;
2616 case SIDL:
2617 idle++;
2618 break;
2619 case SONPROC:
2620 onproc++;
2621 break;
2622 case SDEAD:
2623 dead++;
2624 break;
2625 case SZOMB:
2626 zombies++;
2627 break;
2628 }
2629 } /* if ((proc_ptr == NULL) || (proc_ptr->p_pid != procs[i].p_pid)) */
2630 }
2631
2632 kvm_close(kd);
2633
2634 ps_submit_state("running", running);
2635 ps_submit_state("sleeping", sleeping);
2636 ps_submit_state("zombies", zombies);
2637 ps_submit_state("stopped", stopped);
2638 ps_submit_state("onproc", onproc);
2639 ps_submit_state("idle", idle);
2640 ps_submit_state("dead", dead);
2641
2642 for (procstat_t *ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
2643 ps_submit_proc_list(ps_ptr);
2644 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_OPENBSD */
2645
2646 #elif HAVE_PROCINFO_H
2647 /* AIX */
2648 int running = 0;
2649 int sleeping = 0;
2650 int zombies = 0;
2651 int stopped = 0;
2652 int paging = 0;
2653 int blocked = 0;
2654
2655 pid_t pindex = 0;
2656 int nprocs;
2657
2658 process_entry_t pse;
2659
2660 ps_list_reset();
2661 while ((nprocs = getprocs64(procentry, sizeof(struct procentry64),
2662 /* fdsinfo = */ NULL, sizeof(struct fdsinfo64),
2663 &pindex, MAXPROCENTRY)) > 0) {
2664 for (int i = 0; i < nprocs; i++) {
2665 tid64_t thindex;
2666 int nthreads;
2667 char arglist[MAXARGLN + 1];
2668 char *cargs;
2669 char *cmdline;
2670
2671 if (procentry[i].pi_state == SNONE)
2672 continue;
2673 /* if (procentry[i].pi_state == SZOMB) FIXME */
2674
2675 cmdline = procentry[i].pi_comm;
2676 cargs = procentry[i].pi_comm;
2677 if (procentry[i].pi_flags & SKPROC) {
2678 if (procentry[i].pi_pid == 0)
2679 cmdline = "swapper";
2680 cargs = cmdline;
2681 } else {
2682 if (getargs(&procentry[i], sizeof(struct procentry64), arglist,
2683 MAXARGLN) >= 0) {
2684 int n;
2685
2686 n = -1;
2687 while (++n < MAXARGLN) {
2688 if (arglist[n] == '\0') {
2689 if (arglist[n + 1] == '\0')
2690 break;
2691 arglist[n] = ' ';
2692 }
2693 }
2694 cargs = arglist;
2695 }
2696 }
2697
2698 memset(&pse, 0, sizeof(pse));
2699
2700 pse.id = procentry[i].pi_pid;
2701 pse.num_lwp = procentry[i].pi_thcount;
2702 pse.num_proc = 1;
2703
2704 thindex = 0;
2705 while ((nthreads = getthrds64(procentry[i].pi_pid, thrdentry,
2706 sizeof(struct thrdentry64), &thindex,
2707 MAXTHRDENTRY)) > 0) {
2708 int j;
2709
2710 for (j = 0; j < nthreads; j++) {
2711 switch (thrdentry[j].ti_state) {
2712 /* case TSNONE: break; */
2713 case TSIDL:
2714 blocked++;
2715 break; /* FIXME is really blocked */
2716 case TSRUN:
2717 running++;
2718 break;
2719 case TSSLEEP:
2720 sleeping++;
2721 break;
2722 case TSSWAP:
2723 paging++;
2724 break;
2725 case TSSTOP:
2726 stopped++;
2727 break;
2728 case TSZOMB:
2729 zombies++;
2730 break;
2731 }
2732 }
2733 if (nthreads < MAXTHRDENTRY)
2734 break;
2735 }
2736
2737 /* tv_usec is nanosec ??? */
2738 pse.cpu_user_counter = procentry[i].pi_ru.ru_utime.tv_sec * 1000000 +
2739 procentry[i].pi_ru.ru_utime.tv_usec / 1000;
2740
2741 /* tv_usec is nanosec ??? */
2742 pse.cpu_system_counter = procentry[i].pi_ru.ru_stime.tv_sec * 1000000 +
2743 procentry[i].pi_ru.ru_stime.tv_usec / 1000;
2744
2745 pse.vmem_minflt_counter = procentry[i].pi_minflt;
2746 pse.vmem_majflt_counter = procentry[i].pi_majflt;
2747
2748 pse.vmem_size = procentry[i].pi_tsize + procentry[i].pi_dvm * pagesize;
2749 pse.vmem_rss = (procentry[i].pi_drss + procentry[i].pi_trss) * pagesize;
2750 /* Not supported/implemented */
2751 pse.vmem_data = 0;
2752 pse.vmem_code = 0;
2753 pse.stack_size = 0;
2754
2755 pse.io_rchar = -1;
2756 pse.io_wchar = -1;
2757 pse.io_syscr = -1;
2758 pse.io_syscw = -1;
2759 pse.io_diskr = -1;
2760 pse.io_diskw = -1;
2761
2762 pse.num_fd = 0;
2763 pse.num_maps = 0;
2764
2765 pse.cswitch_vol = -1;
2766 pse.cswitch_invol = -1;
2767
2768 ps_list_add(cmdline, cargs, &pse);
2769 } /* for (i = 0 .. nprocs) */
2770
2771 if (nprocs < MAXPROCENTRY)
2772 break;
2773 } /* while (getprocs64() > 0) */
2774 ps_submit_state("running", running);
2775 ps_submit_state("sleeping", sleeping);
2776 ps_submit_state("zombies", zombies);
2777 ps_submit_state("stopped", stopped);
2778 ps_submit_state("paging", paging);
2779 ps_submit_state("blocked", blocked);
2780
2781 for (procstat_t *ps = list_head_g; ps != NULL; ps = ps->next)
2782 ps_submit_proc_list(ps);
2783 /* #endif HAVE_PROCINFO_H */
2784
2785 #elif KERNEL_SOLARIS
2786 /*
2787 * The Solaris section adds a few more process states and removes some
2788 * process states compared to linux. Most notably there is no "PAGING"
2789 * and "BLOCKED" state for a process. The rest is similar to the linux
2790 * code.
2791 */
2792 int running = 0;
2793 int sleeping = 0;
2794 int zombies = 0;
2795 int stopped = 0;
2796 int detached = 0;
2797 int daemon = 0;
2798 int system = 0;
2799 int orphan = 0;
2800
2801 struct dirent *ent;
2802 DIR *proc;
2803
2804 int status;
2805 char state;
2806
2807 char cmdline[PRARGSZ];
2808
2809 ps_list_reset();
2810
2811 proc = opendir("/proc");
2812 if (proc == NULL)
2813 return -1;
2814
2815 while ((ent = readdir(proc)) != NULL) {
2816 long pid;
2817 process_entry_t pse;
2818 char *endptr;
2819
2820 if (!isdigit((int)ent->d_name[0]))
2821 continue;
2822
2823 pid = strtol(ent->d_name, &endptr, 10);
2824 if (*endptr != 0) /* value didn't completely parse as a number */
2825 continue;
2826
2827 memset(&pse, 0, sizeof(pse));
2828 pse.id = pid;
2829
2830 status = ps_read_process(pid, &pse, &state);
2831 if (status != 0) {
2832 DEBUG("ps_read_process failed: %i", status);
2833 continue;
2834 }
2835
2836 switch (state) {
2837 case 'R':
2838 running++;
2839 break;
2840 case 'S':
2841 sleeping++;
2842 break;
2843 case 'E':
2844 detached++;
2845 break;
2846 case 'Z':
2847 zombies++;
2848 break;
2849 case 'T':
2850 stopped++;
2851 break;
2852 case 'A':
2853 daemon++;
2854 break;
2855 case 'Y':
2856 system++;
2857 break;
2858 case 'O':
2859 orphan++;
2860 break;
2861 }
2862
2863 ps_list_add(pse.name,
2864 ps_get_cmdline(pid, pse.name, cmdline, sizeof(cmdline)), &pse);
2865 } /* while(readdir) */
2866 closedir(proc);
2867
2868 ps_submit_state("running", running);
2869 ps_submit_state("sleeping", sleeping);
2870 ps_submit_state("zombies", zombies);
2871 ps_submit_state("stopped", stopped);
2872 ps_submit_state("detached", detached);
2873 ps_submit_state("daemon", daemon);
2874 ps_submit_state("system", system);
2875 ps_submit_state("orphan", orphan);
2876
2877 for (procstat_t *ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
2878 ps_submit_proc_list(ps_ptr);
2879
2880 read_fork_rate();
2881 #endif /* KERNEL_SOLARIS */
2882
2883 want_init = false;
2884
2885 return 0;
2886 } /* int ps_read */
2887
2888 void module_register(void) {
2889 plugin_register_complex_config("processes", ps_config);
2890 plugin_register_init("processes", ps_init);
2891 plugin_register_read("processes", ps_read);
2892 } /* void module_register */