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 verplant.org>
29 * Oleg King <king2 at kaluga.ru>
30 * Sebastian Harl <sh at tokkee.org>
31 * Andrés J. Díaz <ajdiaz at connectical.com>
32 * Manuel Sanmartin
33 * Clément Stenac <clement.stenac at diwi.org>
34 * Cosmin Ioiart <cioiart at gmail.com>
35 **/
37 #include "collectd.h"
38 #include "common.h"
39 #include "plugin.h"
40 #include "configfile.h"
42 /* Include header files for the mach system, if they exist.. */
43 #if HAVE_THREAD_INFO
44 # if HAVE_MACH_MACH_INIT_H
45 # include <mach/mach_init.h>
46 # endif
47 # if HAVE_MACH_HOST_PRIV_H
48 # include <mach/host_priv.h>
49 # endif
50 # if HAVE_MACH_MACH_ERROR_H
51 # include <mach/mach_error.h>
52 # endif
53 # if HAVE_MACH_MACH_HOST_H
54 # include <mach/mach_host.h>
55 # endif
56 # if HAVE_MACH_MACH_PORT_H
57 # include <mach/mach_port.h>
58 # endif
59 # if HAVE_MACH_MACH_TYPES_H
60 # include <mach/mach_types.h>
61 # endif
62 # if HAVE_MACH_MESSAGE_H
63 # include <mach/message.h>
64 # endif
65 # if HAVE_MACH_PROCESSOR_SET_H
66 # include <mach/processor_set.h>
67 # endif
68 # if HAVE_MACH_TASK_H
69 # include <mach/task.h>
70 # endif
71 # if HAVE_MACH_THREAD_ACT_H
72 # include <mach/thread_act.h>
73 # endif
74 # if HAVE_MACH_VM_REGION_H
75 # include <mach/vm_region.h>
76 # endif
77 # if HAVE_MACH_VM_MAP_H
78 # include <mach/vm_map.h>
79 # endif
80 # if HAVE_MACH_VM_PROT_H
81 # include <mach/vm_prot.h>
82 # endif
83 # if HAVE_SYS_SYSCTL_H
84 # include <sys/sysctl.h>
85 # endif
86 /* #endif HAVE_THREAD_INFO */
88 #elif KERNEL_LINUX
89 # if HAVE_LINUX_CONFIG_H
90 # include <linux/config.h>
91 # endif
92 # ifndef CONFIG_HZ
93 # define CONFIG_HZ 100
94 # endif
95 /* #endif KERNEL_LINUX */
97 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD
98 # include <kvm.h>
99 # include <sys/param.h>
100 # include <sys/sysctl.h>
101 # include <sys/user.h>
102 # include <sys/proc.h>
103 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
105 #elif HAVE_PROCINFO_H
106 # include <procinfo.h>
107 # include <sys/types.h>
109 #define MAXPROCENTRY 32
110 #define MAXTHRDENTRY 16
111 #define MAXARGLN 1024
112 /* #endif HAVE_PROCINFO_H */
114 #elif KERNEL_SOLARIS
115 /* Hack: Avoid #error when building a 32-bit binary with
116 * _FILE_OFFSET_BITS=64. There is a reason for this #error, as one
117 * of the structures in <sys/procfs.h> uses an off_t, but that
118 * isn't relevant to our usage of procfs. */
119 #if !defined(_LP64) && _FILE_OFFSET_BITS == 64
120 # define SAVE_FOB_64
121 # undef _FILE_OFFSET_BITS
122 #endif
124 # include <procfs.h>
126 #ifdef SAVE_FOB_64
127 # define _FILE_OFFSET_BITS 64
128 # undef SAVE_FOB_64
129 #endif
131 # include <sys/user.h>
132 # include <dirent.h>
133 /* #endif KERNEL_SOLARIS */
135 #else
136 # error "No applicable input method."
137 #endif
139 #if HAVE_REGEX_H
140 # include <regex.h>
141 #endif
143 #if HAVE_KSTAT_H
144 # include <kstat.h>
145 #endif
147 #ifndef ARG_MAX
148 # define ARG_MAX 4096
149 #endif
151 typedef struct procstat_entry_s
152 {
153 unsigned long id;
154 unsigned long age;
156 unsigned long num_proc;
157 unsigned long num_lwp;
158 unsigned long vmem_size;
159 unsigned long vmem_rss;
160 unsigned long vmem_data;
161 unsigned long vmem_code;
162 unsigned long stack_size;
164 unsigned long vmem_minflt;
165 unsigned long vmem_majflt;
166 derive_t vmem_minflt_counter;
167 derive_t vmem_majflt_counter;
169 unsigned long cpu_user;
170 unsigned long cpu_system;
171 derive_t cpu_user_counter;
172 derive_t cpu_system_counter;
174 /* io data */
175 derive_t io_rchar;
176 derive_t io_wchar;
177 derive_t io_syscr;
178 derive_t io_syscw;
180 struct procstat_entry_s *next;
181 } procstat_entry_t;
183 #define PROCSTAT_NAME_LEN 256
184 typedef struct procstat
185 {
186 char name[PROCSTAT_NAME_LEN];
187 #if HAVE_REGEX_H
188 regex_t *re;
189 #endif
191 unsigned long num_proc;
192 unsigned long num_lwp;
193 unsigned long vmem_size;
194 unsigned long vmem_rss;
195 unsigned long vmem_data;
196 unsigned long vmem_code;
197 unsigned long stack_size;
199 derive_t vmem_minflt_counter;
200 derive_t vmem_majflt_counter;
202 derive_t cpu_user_counter;
203 derive_t cpu_system_counter;
205 /* io data */
206 derive_t io_rchar;
207 derive_t io_wchar;
208 derive_t io_syscr;
209 derive_t io_syscw;
211 struct procstat *next;
212 struct procstat_entry_s *instances;
213 } procstat_t;
215 static procstat_t *list_head_g = NULL;
217 #if HAVE_THREAD_INFO
218 static mach_port_t port_host_self;
219 static mach_port_t port_task_self;
221 static processor_set_name_array_t pset_list;
222 static mach_msg_type_number_t pset_list_len;
223 /* #endif HAVE_THREAD_INFO */
225 #elif KERNEL_LINUX
226 static long pagesize_g;
227 /* #endif KERNEL_LINUX */
229 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD
230 static int pagesize;
231 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
233 #elif HAVE_PROCINFO_H
234 static struct procentry64 procentry[MAXPROCENTRY];
235 static struct thrdentry64 thrdentry[MAXTHRDENTRY];
236 static int pagesize;
238 #ifndef _AIXVERSION_610
239 int getprocs64 (void *procsinfo, int sizproc, void *fdsinfo, int sizfd, pid_t *index, int count);
240 int getthrds64( pid_t, void *, int, tid64_t *, int );
241 #endif
242 int getargs (void *processBuffer, int bufferLen, char *argsBuffer, int argsLen);
243 #endif /* HAVE_PROCINFO_H */
245 /* put name of process from config to list_head_g tree
246 * list_head_g is a list of 'procstat_t' structs with
247 * processes names we want to watch */
248 static void ps_list_register (const char *name, const char *regexp)
249 {
250 procstat_t *new;
251 procstat_t *ptr;
252 int status;
254 new = (procstat_t *) malloc (sizeof (procstat_t));
255 if (new == NULL)
256 {
257 ERROR ("processes plugin: ps_list_register: malloc failed.");
258 return;
259 }
260 memset (new, 0, sizeof (procstat_t));
261 sstrncpy (new->name, name, sizeof (new->name));
263 #if HAVE_REGEX_H
264 if (regexp != NULL)
265 {
266 DEBUG ("ProcessMatch: adding \"%s\" as criteria to process %s.", regexp, name);
267 new->re = (regex_t *) malloc (sizeof (regex_t));
268 if (new->re == NULL)
269 {
270 ERROR ("processes plugin: ps_list_register: malloc failed.");
271 sfree (new);
272 return;
273 }
275 status = regcomp (new->re, regexp, REG_EXTENDED | REG_NOSUB);
276 if (status != 0)
277 {
278 DEBUG ("ProcessMatch: compiling the regular expression \"%s\" failed.", regexp);
279 sfree(new->re);
280 sfree(new);
281 return;
282 }
283 }
284 #else
285 if (regexp != NULL)
286 {
287 ERROR ("processes plugin: ps_list_register: "
288 "Regular expression \"%s\" found in config "
289 "file, but support for regular expressions "
290 "has been disabled at compile time.",
291 regexp);
292 sfree (new);
293 return;
294 }
295 #endif
297 for (ptr = list_head_g; ptr != NULL; ptr = ptr->next)
298 {
299 if (strcmp (ptr->name, name) == 0)
300 {
301 WARNING ("processes plugin: You have configured more "
302 "than one `Process' or "
303 "`ProcessMatch' with the same name. "
304 "All but the first setting will be "
305 "ignored.");
306 #if HAVE_REGEX_H
307 sfree (new->re);
308 #endif
309 sfree (new);
310 return;
311 }
313 if (ptr->next == NULL)
314 break;
315 }
317 if (ptr == NULL)
318 list_head_g = new;
319 else
320 ptr->next = new;
321 } /* void ps_list_register */
323 /* try to match name against entry, returns 1 if success */
324 static int ps_list_match (const char *name, const char *cmdline, procstat_t *ps)
325 {
326 #if HAVE_REGEX_H
327 if (ps->re != NULL)
328 {
329 int status;
330 const char *str;
332 str = cmdline;
333 if ((str == NULL) || (str[0] == 0))
334 str = name;
336 assert (str != NULL);
338 status = regexec (ps->re, str,
339 /* nmatch = */ 0,
340 /* pmatch = */ NULL,
341 /* eflags = */ 0);
342 if (status == 0)
343 return (1);
344 }
345 else
346 #endif
347 if (strcmp (ps->name, name) == 0)
348 return (1);
350 return (0);
351 } /* int ps_list_match */
353 /* add process entry to 'instances' of process 'name' (or refresh it) */
354 static void ps_list_add (const char *name, const char *cmdline, procstat_entry_t *entry)
355 {
356 procstat_t *ps;
357 procstat_entry_t *pse;
359 if (entry->id == 0)
360 return;
362 for (ps = list_head_g; ps != NULL; ps = ps->next)
363 {
364 if ((ps_list_match (name, cmdline, ps)) == 0)
365 continue;
367 for (pse = ps->instances; pse != NULL; pse = pse->next)
368 if ((pse->id == entry->id) || (pse->next == NULL))
369 break;
371 if ((pse == NULL) || (pse->id != entry->id))
372 {
373 procstat_entry_t *new;
375 new = (procstat_entry_t *) malloc (sizeof (procstat_entry_t));
376 if (new == NULL)
377 return;
378 memset (new, 0, sizeof (procstat_entry_t));
379 new->id = entry->id;
381 if (pse == NULL)
382 ps->instances = new;
383 else
384 pse->next = new;
386 pse = new;
387 }
389 pse->age = 0;
390 pse->num_proc = entry->num_proc;
391 pse->num_lwp = entry->num_lwp;
392 pse->vmem_size = entry->vmem_size;
393 pse->vmem_rss = entry->vmem_rss;
394 pse->vmem_data = entry->vmem_data;
395 pse->vmem_code = entry->vmem_code;
396 pse->stack_size = entry->stack_size;
397 pse->io_rchar = entry->io_rchar;
398 pse->io_wchar = entry->io_wchar;
399 pse->io_syscr = entry->io_syscr;
400 pse->io_syscw = entry->io_syscw;
402 ps->num_proc += pse->num_proc;
403 ps->num_lwp += pse->num_lwp;
404 ps->vmem_size += pse->vmem_size;
405 ps->vmem_rss += pse->vmem_rss;
406 ps->vmem_data += pse->vmem_data;
407 ps->vmem_code += pse->vmem_code;
408 ps->stack_size += pse->stack_size;
410 ps->io_rchar += ((pse->io_rchar == -1)?0:pse->io_rchar);
411 ps->io_wchar += ((pse->io_wchar == -1)?0:pse->io_wchar);
412 ps->io_syscr += ((pse->io_syscr == -1)?0:pse->io_syscr);
413 ps->io_syscw += ((pse->io_syscw == -1)?0:pse->io_syscw);
415 if ((entry->vmem_minflt_counter == 0)
416 && (entry->vmem_majflt_counter == 0))
417 {
418 pse->vmem_minflt_counter += entry->vmem_minflt;
419 pse->vmem_minflt = entry->vmem_minflt;
421 pse->vmem_majflt_counter += entry->vmem_majflt;
422 pse->vmem_majflt = entry->vmem_majflt;
423 }
424 else
425 {
426 if (entry->vmem_minflt_counter < pse->vmem_minflt_counter)
427 {
428 pse->vmem_minflt = entry->vmem_minflt_counter
429 + (ULONG_MAX - pse->vmem_minflt_counter);
430 }
431 else
432 {
433 pse->vmem_minflt = entry->vmem_minflt_counter - pse->vmem_minflt_counter;
434 }
435 pse->vmem_minflt_counter = entry->vmem_minflt_counter;
437 if (entry->vmem_majflt_counter < pse->vmem_majflt_counter)
438 {
439 pse->vmem_majflt = entry->vmem_majflt_counter
440 + (ULONG_MAX - pse->vmem_majflt_counter);
441 }
442 else
443 {
444 pse->vmem_majflt = entry->vmem_majflt_counter - pse->vmem_majflt_counter;
445 }
446 pse->vmem_majflt_counter = entry->vmem_majflt_counter;
447 }
449 ps->vmem_minflt_counter += pse->vmem_minflt;
450 ps->vmem_majflt_counter += pse->vmem_majflt;
452 if ((entry->cpu_user_counter == 0)
453 && (entry->cpu_system_counter == 0))
454 {
455 pse->cpu_user_counter += entry->cpu_user;
456 pse->cpu_user = entry->cpu_user;
458 pse->cpu_system_counter += entry->cpu_system;
459 pse->cpu_system = entry->cpu_system;
460 }
461 else
462 {
463 if (entry->cpu_user_counter < pse->cpu_user_counter)
464 {
465 pse->cpu_user = entry->cpu_user_counter
466 + (ULONG_MAX - pse->cpu_user_counter);
467 }
468 else
469 {
470 pse->cpu_user = entry->cpu_user_counter - pse->cpu_user_counter;
471 }
472 pse->cpu_user_counter = entry->cpu_user_counter;
474 if (entry->cpu_system_counter < pse->cpu_system_counter)
475 {
476 pse->cpu_system = entry->cpu_system_counter
477 + (ULONG_MAX - pse->cpu_system_counter);
478 }
479 else
480 {
481 pse->cpu_system = entry->cpu_system_counter - pse->cpu_system_counter;
482 }
483 pse->cpu_system_counter = entry->cpu_system_counter;
484 }
486 ps->cpu_user_counter += pse->cpu_user;
487 ps->cpu_system_counter += pse->cpu_system;
488 }
489 }
491 /* remove old entries from instances of processes in list_head_g */
492 static void ps_list_reset (void)
493 {
494 procstat_t *ps;
495 procstat_entry_t *pse;
496 procstat_entry_t *pse_prev;
498 for (ps = list_head_g; ps != NULL; ps = ps->next)
499 {
500 ps->num_proc = 0;
501 ps->num_lwp = 0;
502 ps->vmem_size = 0;
503 ps->vmem_rss = 0;
504 ps->vmem_data = 0;
505 ps->vmem_code = 0;
506 ps->stack_size = 0;
507 ps->io_rchar = -1;
508 ps->io_wchar = -1;
509 ps->io_syscr = -1;
510 ps->io_syscw = -1;
512 pse_prev = NULL;
513 pse = ps->instances;
514 while (pse != NULL)
515 {
516 if (pse->age > 10)
517 {
518 DEBUG ("Removing this procstat entry cause it's too old: "
519 "id = %lu; name = %s;",
520 pse->id, ps->name);
522 if (pse_prev == NULL)
523 {
524 ps->instances = pse->next;
525 free (pse);
526 pse = ps->instances;
527 }
528 else
529 {
530 pse_prev->next = pse->next;
531 free (pse);
532 pse = pse_prev->next;
533 }
534 }
535 else
536 {
537 pse->age++;
538 pse_prev = pse;
539 pse = pse->next;
540 }
541 } /* while (pse != NULL) */
542 } /* for (ps = list_head_g; ps != NULL; ps = ps->next) */
543 }
545 /* put all pre-defined 'Process' names from config to list_head_g tree */
546 static int ps_config (oconfig_item_t *ci)
547 {
548 int i;
550 #if KERNEL_LINUX
551 const size_t max_procname_len = 15;
552 #elif KERNEL_SOLARIS || KERNEL_FREEBSD
553 const size_t max_procname_len = MAXCOMLEN -1;
554 #endif
556 for (i = 0; i < ci->children_num; ++i) {
557 oconfig_item_t *c = ci->children + i;
559 if (strcasecmp (c->key, "Process") == 0)
560 {
561 if ((c->values_num != 1)
562 || (OCONFIG_TYPE_STRING != c->values[0].type)) {
563 ERROR ("processes plugin: `Process' expects exactly "
564 "one string argument (got %i).",
565 c->values_num);
566 continue;
567 }
569 if (c->children_num != 0) {
570 WARNING ("processes plugin: the `Process' config option "
571 "does not expect any child elements -- ignoring "
572 "content (%i elements) of the <Process '%s'> block.",
573 c->children_num, c->values[0].value.string);
574 }
576 #if KERNEL_LINUX || KERNEL_SOLARIS || KERNEL_FREEBSD
577 if (strlen (c->values[0].value.string) > max_procname_len) {
578 WARNING ("processes plugin: this platform has a %zu character limit "
579 "to process names. The `Process \"%s\"' option will "
580 "not work as expected.",
581 max_procname_len, c->values[0].value.string);
582 }
583 #endif
585 ps_list_register (c->values[0].value.string, NULL);
586 }
587 else if (strcasecmp (c->key, "ProcessMatch") == 0)
588 {
589 if ((c->values_num != 2)
590 || (OCONFIG_TYPE_STRING != c->values[0].type)
591 || (OCONFIG_TYPE_STRING != c->values[1].type))
592 {
593 ERROR ("processes plugin: `ProcessMatch' needs exactly "
594 "two string arguments (got %i).",
595 c->values_num);
596 continue;
597 }
599 if (c->children_num != 0) {
600 WARNING ("processes plugin: the `ProcessMatch' config option "
601 "does not expect any child elements -- ignoring "
602 "content (%i elements) of the <ProcessMatch '%s' '%s'> "
603 "block.", c->children_num, c->values[0].value.string,
604 c->values[1].value.string);
605 }
607 ps_list_register (c->values[0].value.string,
608 c->values[1].value.string);
609 }
610 else
611 {
612 ERROR ("processes plugin: The `%s' configuration option is not "
613 "understood and will be ignored.", c->key);
614 continue;
615 }
616 }
618 return (0);
619 }
621 static int ps_init (void)
622 {
623 #if HAVE_THREAD_INFO
624 kern_return_t status;
626 port_host_self = mach_host_self ();
627 port_task_self = mach_task_self ();
629 if (pset_list != NULL)
630 {
631 vm_deallocate (port_task_self,
632 (vm_address_t) pset_list,
633 pset_list_len * sizeof (processor_set_t));
634 pset_list = NULL;
635 pset_list_len = 0;
636 }
638 if ((status = host_processor_sets (port_host_self,
639 &pset_list,
640 &pset_list_len)) != KERN_SUCCESS)
641 {
642 ERROR ("host_processor_sets failed: %s\n",
643 mach_error_string (status));
644 pset_list = NULL;
645 pset_list_len = 0;
646 return (-1);
647 }
648 /* #endif HAVE_THREAD_INFO */
650 #elif KERNEL_LINUX
651 pagesize_g = sysconf(_SC_PAGESIZE);
652 DEBUG ("pagesize_g = %li; CONFIG_HZ = %i;",
653 pagesize_g, CONFIG_HZ);
654 /* #endif KERNEL_LINUX */
656 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD
657 pagesize = getpagesize();
658 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
660 #elif HAVE_PROCINFO_H
661 pagesize = getpagesize();
662 #endif /* HAVE_PROCINFO_H */
664 return (0);
665 } /* int ps_init */
667 /* submit global state (e.g.: qty of zombies, running, etc..) */
668 static void ps_submit_state (const char *state, double value)
669 {
670 value_t values[1];
671 value_list_t vl = VALUE_LIST_INIT;
673 values[0].gauge = value;
675 vl.values = values;
676 vl.values_len = 1;
677 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
678 sstrncpy (vl.plugin, "processes", sizeof (vl.plugin));
679 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
680 sstrncpy (vl.type, "ps_state", sizeof (vl.type));
681 sstrncpy (vl.type_instance, state, sizeof (vl.type_instance));
683 plugin_dispatch_values (&vl);
684 }
686 /* submit info about specific process (e.g.: memory taken, cpu usage, etc..) */
687 static void ps_submit_proc_list (procstat_t *ps)
688 {
689 value_t values[2];
690 value_list_t vl = VALUE_LIST_INIT;
692 vl.values = values;
693 vl.values_len = 2;
694 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
695 sstrncpy (vl.plugin, "processes", sizeof (vl.plugin));
696 sstrncpy (vl.plugin_instance, ps->name, sizeof (vl.plugin_instance));
698 sstrncpy (vl.type, "ps_vm", sizeof (vl.type));
699 vl.values[0].gauge = ps->vmem_size;
700 vl.values_len = 1;
701 plugin_dispatch_values (&vl);
703 sstrncpy (vl.type, "ps_rss", sizeof (vl.type));
704 vl.values[0].gauge = ps->vmem_rss;
705 vl.values_len = 1;
706 plugin_dispatch_values (&vl);
708 sstrncpy (vl.type, "ps_data", sizeof (vl.type));
709 vl.values[0].gauge = ps->vmem_data;
710 vl.values_len = 1;
711 plugin_dispatch_values (&vl);
713 sstrncpy (vl.type, "ps_code", sizeof (vl.type));
714 vl.values[0].gauge = ps->vmem_code;
715 vl.values_len = 1;
716 plugin_dispatch_values (&vl);
718 sstrncpy (vl.type, "ps_stacksize", sizeof (vl.type));
719 vl.values[0].gauge = ps->stack_size;
720 vl.values_len = 1;
721 plugin_dispatch_values (&vl);
723 sstrncpy (vl.type, "ps_cputime", sizeof (vl.type));
724 vl.values[0].derive = ps->cpu_user_counter;
725 vl.values[1].derive = ps->cpu_system_counter;
726 vl.values_len = 2;
727 plugin_dispatch_values (&vl);
729 sstrncpy (vl.type, "ps_count", sizeof (vl.type));
730 vl.values[0].gauge = ps->num_proc;
731 vl.values[1].gauge = ps->num_lwp;
732 vl.values_len = 2;
733 plugin_dispatch_values (&vl);
735 sstrncpy (vl.type, "ps_pagefaults", sizeof (vl.type));
736 vl.values[0].derive = ps->vmem_minflt_counter;
737 vl.values[1].derive = ps->vmem_majflt_counter;
738 vl.values_len = 2;
739 plugin_dispatch_values (&vl);
741 if ( (ps->io_rchar != -1) && (ps->io_wchar != -1) )
742 {
743 sstrncpy (vl.type, "ps_disk_octets", sizeof (vl.type));
744 vl.values[0].derive = ps->io_rchar;
745 vl.values[1].derive = ps->io_wchar;
746 vl.values_len = 2;
747 plugin_dispatch_values (&vl);
748 }
750 if ( (ps->io_syscr != -1) && (ps->io_syscw != -1) )
751 {
752 sstrncpy (vl.type, "ps_disk_ops", sizeof (vl.type));
753 vl.values[0].derive = ps->io_syscr;
754 vl.values[1].derive = ps->io_syscw;
755 vl.values_len = 2;
756 plugin_dispatch_values (&vl);
757 }
759 DEBUG ("name = %s; num_proc = %lu; num_lwp = %lu; "
760 "vmem_size = %lu; vmem_rss = %lu; vmem_data = %lu; "
761 "vmem_code = %lu; "
762 "vmem_minflt_counter = %"PRIi64"; vmem_majflt_counter = %"PRIi64"; "
763 "cpu_user_counter = %"PRIi64"; cpu_system_counter = %"PRIi64"; "
764 "io_rchar = %"PRIi64"; io_wchar = %"PRIi64"; "
765 "io_syscr = %"PRIi64"; io_syscw = %"PRIi64";",
766 ps->name, ps->num_proc, ps->num_lwp,
767 ps->vmem_size, ps->vmem_rss,
768 ps->vmem_data, ps->vmem_code,
769 ps->vmem_minflt_counter, ps->vmem_majflt_counter,
770 ps->cpu_user_counter, ps->cpu_system_counter,
771 ps->io_rchar, ps->io_wchar, ps->io_syscr, ps->io_syscw);
772 } /* void ps_submit_proc_list */
774 #if KERNEL_LINUX || KERNEL_SOLARIS
775 static void ps_submit_fork_rate (derive_t value)
776 {
777 value_t values[1];
778 value_list_t vl = VALUE_LIST_INIT;
780 values[0].derive = value;
782 vl.values = values;
783 vl.values_len = 1;
784 sstrncpy(vl.host, hostname_g, sizeof (vl.host));
785 sstrncpy(vl.plugin, "processes", sizeof (vl.plugin));
786 sstrncpy(vl.plugin_instance, "", sizeof (vl.plugin_instance));
787 sstrncpy(vl.type, "fork_rate", sizeof (vl.type));
788 sstrncpy(vl.type_instance, "", sizeof (vl.type_instance));
790 plugin_dispatch_values(&vl);
791 }
792 #endif /* KERNEL_LINUX || KERNEL_SOLARIS*/
794 /* ------- additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
795 #if KERNEL_LINUX
796 static int ps_read_tasks (long pid)
797 {
798 char dirname[64];
799 DIR *dh;
800 struct dirent *ent;
801 int count = 0;
803 ssnprintf (dirname, sizeof (dirname), "/proc/%li/task", pid);
805 if ((dh = opendir (dirname)) == NULL)
806 {
807 DEBUG ("Failed to open directory `%s'", dirname);
808 return (-1);
809 }
811 while ((ent = readdir (dh)) != NULL)
812 {
813 if (!isdigit ((int) ent->d_name[0]))
814 continue;
815 else
816 count++;
817 }
818 closedir (dh);
820 return ((count >= 1) ? count : 1);
821 } /* int *ps_read_tasks */
823 /* Read advanced virtual memory data from /proc/pid/status */
824 static procstat_t *ps_read_vmem (long pid, procstat_t *ps)
825 {
826 FILE *fh;
827 char buffer[1024];
828 char filename[64];
829 unsigned long long lib = 0;
830 unsigned long long exe = 0;
831 unsigned long long data = 0;
832 char *fields[8];
833 int numfields;
835 ssnprintf (filename, sizeof (filename), "/proc/%li/status", pid);
836 if ((fh = fopen (filename, "r")) == NULL)
837 return (NULL);
839 while (fgets (buffer, sizeof(buffer), fh) != NULL)
840 {
841 long long tmp;
842 char *endptr;
844 if (strncmp (buffer, "Vm", 2) != 0)
845 continue;
847 numfields = strsplit (buffer, fields,
848 STATIC_ARRAY_SIZE (fields));
850 if (numfields < 2)
851 continue;
853 errno = 0;
854 endptr = NULL;
855 tmp = strtoll (fields[1], &endptr, /* base = */ 10);
856 if ((errno == 0) && (endptr != fields[1]))
857 {
858 if (strncmp (buffer, "VmData", 6) == 0)
859 {
860 data = tmp;
861 }
862 else if (strncmp (buffer, "VmLib", 5) == 0)
863 {
864 lib = tmp;
865 }
866 else if (strncmp(buffer, "VmExe", 5) == 0)
867 {
868 exe = tmp;
869 }
870 }
871 } /* while (fgets) */
873 if (fclose (fh))
874 {
875 char errbuf[1024];
876 WARNING ("processes: fclose: %s",
877 sstrerror (errno, errbuf, sizeof (errbuf)));
878 }
880 ps->vmem_data = data * 1024;
881 ps->vmem_code = (exe + lib) * 1024;
883 return (ps);
884 } /* procstat_t *ps_read_vmem */
886 static procstat_t *ps_read_io (long pid, procstat_t *ps)
887 {
888 FILE *fh;
889 char buffer[1024];
890 char filename[64];
892 char *fields[8];
893 int numfields;
895 ssnprintf (filename, sizeof (filename), "/proc/%li/io", pid);
896 if ((fh = fopen (filename, "r")) == NULL)
897 return (NULL);
899 while (fgets (buffer, sizeof (buffer), fh) != NULL)
900 {
901 derive_t *val = NULL;
902 long long tmp;
903 char *endptr;
905 if (strncasecmp (buffer, "rchar:", 6) == 0)
906 val = &(ps->io_rchar);
907 else if (strncasecmp (buffer, "wchar:", 6) == 0)
908 val = &(ps->io_wchar);
909 else if (strncasecmp (buffer, "syscr:", 6) == 0)
910 val = &(ps->io_syscr);
911 else if (strncasecmp (buffer, "syscw:", 6) == 0)
912 val = &(ps->io_syscw);
913 else
914 continue;
916 numfields = strsplit (buffer, fields,
917 STATIC_ARRAY_SIZE (fields));
919 if (numfields < 2)
920 continue;
922 errno = 0;
923 endptr = NULL;
924 tmp = strtoll (fields[1], &endptr, /* base = */ 10);
925 if ((errno != 0) || (endptr == fields[1]))
926 *val = -1;
927 else
928 *val = (derive_t) tmp;
929 } /* while (fgets) */
931 if (fclose (fh))
932 {
933 char errbuf[1024];
934 WARNING ("processes: fclose: %s",
935 sstrerror (errno, errbuf, sizeof (errbuf)));
936 }
938 return (ps);
939 } /* procstat_t *ps_read_io */
941 static int ps_read_process (long pid, procstat_t *ps, char *state)
942 {
943 char filename[64];
944 char buffer[1024];
946 char *fields[64];
947 char fields_len;
949 int buffer_len;
951 char *buffer_ptr;
952 size_t name_start_pos;
953 size_t name_end_pos;
954 size_t name_len;
956 derive_t cpu_user_counter;
957 derive_t cpu_system_counter;
958 long long unsigned vmem_size;
959 long long unsigned vmem_rss;
960 long long unsigned stack_size;
962 memset (ps, 0, sizeof (procstat_t));
964 ssnprintf (filename, sizeof (filename), "/proc/%li/stat", pid);
966 buffer_len = read_file_contents (filename,
967 buffer, sizeof(buffer) - 1);
968 if (buffer_len <= 0)
969 return (-1);
970 buffer[buffer_len] = 0;
972 /* The name of the process is enclosed in parens. Since the name can
973 * contain parens itself, spaces, numbers and pretty much everything
974 * else, use these to determine the process name. We don't use
975 * strchr(3) and strrchr(3) to avoid pointer arithmetic which would
976 * otherwise be required to determine name_len. */
977 name_start_pos = 0;
978 while ((buffer[name_start_pos] != '(')
979 && (name_start_pos < buffer_len))
980 name_start_pos++;
982 name_end_pos = buffer_len;
983 while ((buffer[name_end_pos] != ')')
984 && (name_end_pos > 0))
985 name_end_pos--;
987 /* Either '(' or ')' is not found or they are in the wrong order.
988 * Anyway, something weird that shouldn't happen ever. */
989 if (name_start_pos >= name_end_pos)
990 {
991 ERROR ("processes plugin: name_start_pos = %zu >= name_end_pos = %zu",
992 name_start_pos, name_end_pos);
993 return (-1);
994 }
996 name_len = (name_end_pos - name_start_pos) - 1;
997 if (name_len >= sizeof (ps->name))
998 name_len = sizeof (ps->name) - 1;
1000 sstrncpy (ps->name, &buffer[name_start_pos + 1], name_len + 1);
1002 if ((buffer_len - name_end_pos) < 2)
1003 return (-1);
1004 buffer_ptr = &buffer[name_end_pos + 2];
1006 fields_len = strsplit (buffer_ptr, fields, STATIC_ARRAY_SIZE (fields));
1007 if (fields_len < 22)
1008 {
1009 DEBUG ("processes plugin: ps_read_process (pid = %li):"
1010 " `%s' has only %i fields..",
1011 pid, filename, fields_len);
1012 return (-1);
1013 }
1015 *state = fields[0][0];
1017 if (*state == 'Z')
1018 {
1019 ps->num_lwp = 0;
1020 ps->num_proc = 0;
1021 }
1022 else
1023 {
1024 if ( (ps->num_lwp = ps_read_tasks (pid)) == -1 )
1025 {
1026 /* returns -1 => kernel 2.4 */
1027 ps->num_lwp = 1;
1028 }
1029 ps->num_proc = 1;
1030 }
1032 /* Leave the rest at zero if this is only a zombi */
1033 if (ps->num_proc == 0)
1034 {
1035 DEBUG ("processes plugin: This is only a zombie: pid = %li; "
1036 "name = %s;", pid, ps->name);
1037 return (0);
1038 }
1040 cpu_user_counter = atoll (fields[11]);
1041 cpu_system_counter = atoll (fields[12]);
1042 vmem_size = atoll (fields[20]);
1043 vmem_rss = atoll (fields[21]);
1044 ps->vmem_minflt_counter = atol (fields[7]);
1045 ps->vmem_majflt_counter = atol (fields[9]);
1047 {
1048 unsigned long long stack_start = atoll (fields[25]);
1049 unsigned long long stack_ptr = atoll (fields[26]);
1051 stack_size = (stack_start > stack_ptr)
1052 ? stack_start - stack_ptr
1053 : stack_ptr - stack_start;
1054 }
1056 /* Convert jiffies to useconds */
1057 cpu_user_counter = cpu_user_counter * 1000000 / CONFIG_HZ;
1058 cpu_system_counter = cpu_system_counter * 1000000 / CONFIG_HZ;
1059 vmem_rss = vmem_rss * pagesize_g;
1061 if ( (ps_read_vmem(pid, ps)) == NULL)
1062 {
1063 /* No VMem data */
1064 ps->vmem_data = -1;
1065 ps->vmem_code = -1;
1066 DEBUG("ps_read_process: did not get vmem data for pid %li", pid);
1067 }
1069 ps->cpu_user_counter = cpu_user_counter;
1070 ps->cpu_system_counter = cpu_system_counter;
1071 ps->vmem_size = (unsigned long) vmem_size;
1072 ps->vmem_rss = (unsigned long) vmem_rss;
1073 ps->stack_size = (unsigned long) stack_size;
1075 if ( (ps_read_io (pid, ps)) == NULL)
1076 {
1077 /* no io data */
1078 ps->io_rchar = -1;
1079 ps->io_wchar = -1;
1080 ps->io_syscr = -1;
1081 ps->io_syscw = -1;
1083 DEBUG("ps_read_process: not get io data for pid %li", pid);
1084 }
1086 /* success */
1087 return (0);
1088 } /* int ps_read_process (...) */
1090 static char *ps_get_cmdline (long pid, char *name, char *buf, size_t buf_len)
1091 {
1092 char *buf_ptr;
1093 size_t len;
1095 char file[PATH_MAX];
1096 int fd;
1098 size_t n;
1100 if ((pid < 1) || (NULL == buf) || (buf_len < 2))
1101 return NULL;
1103 ssnprintf (file, sizeof (file), "/proc/%li/cmdline", pid);
1105 errno = 0;
1106 fd = open (file, O_RDONLY);
1107 if (fd < 0) {
1108 char errbuf[4096];
1109 /* ENOENT means the process exited while we were handling it.
1110 * Don't complain about this, it only fills the logs. */
1111 if (errno != ENOENT)
1112 WARNING ("processes plugin: Failed to open `%s': %s.", file,
1113 sstrerror (errno, errbuf, sizeof (errbuf)));
1114 return NULL;
1115 }
1117 buf_ptr = buf;
1118 len = buf_len;
1120 n = 0;
1122 while (42) {
1123 ssize_t status;
1125 status = read (fd, (void *)buf_ptr, len);
1127 if (status < 0) {
1128 char errbuf[1024];
1130 if ((EAGAIN == errno) || (EINTR == errno))
1131 continue;
1133 WARNING ("processes plugin: Failed to read from `%s': %s.", file,
1134 sstrerror (errno, errbuf, sizeof (errbuf)));
1135 close (fd);
1136 return NULL;
1137 }
1139 n += status;
1141 if (status == 0)
1142 break;
1144 buf_ptr += status;
1145 len -= status;
1147 if (len <= 0)
1148 break;
1149 }
1151 close (fd);
1153 if (0 == n) {
1154 /* cmdline not available; e.g. kernel thread, zombie */
1155 if (NULL == name)
1156 return NULL;
1158 ssnprintf (buf, buf_len, "[%s]", name);
1159 return buf;
1160 }
1162 assert (n <= buf_len);
1164 if (n == buf_len)
1165 --n;
1166 buf[n] = '\0';
1168 --n;
1169 /* remove trailing whitespace */
1170 while ((n > 0) && (isspace (buf[n]) || ('\0' == buf[n]))) {
1171 buf[n] = '\0';
1172 --n;
1173 }
1175 /* arguments are separated by '\0' in /proc/<pid>/cmdline */
1176 while (n > 0) {
1177 if ('\0' == buf[n])
1178 buf[n] = ' ';
1179 --n;
1180 }
1181 return buf;
1182 } /* char *ps_get_cmdline (...) */
1184 static int read_fork_rate (void)
1185 {
1186 FILE *proc_stat;
1187 char buffer[1024];
1188 value_t value;
1189 _Bool value_valid = 0;
1191 proc_stat = fopen ("/proc/stat", "r");
1192 if (proc_stat == NULL)
1193 {
1194 char errbuf[1024];
1195 ERROR ("processes plugin: fopen (/proc/stat) failed: %s",
1196 sstrerror (errno, errbuf, sizeof (errbuf)));
1197 return (-1);
1198 }
1200 while (fgets (buffer, sizeof (buffer), proc_stat) != NULL)
1201 {
1202 int status;
1203 char *fields[3];
1204 int fields_num;
1206 fields_num = strsplit (buffer, fields,
1207 STATIC_ARRAY_SIZE (fields));
1208 if (fields_num != 2)
1209 continue;
1211 if (strcmp ("processes", fields[0]) != 0)
1212 continue;
1214 status = parse_value (fields[1], &value, DS_TYPE_DERIVE);
1215 if (status == 0)
1216 value_valid = 1;
1218 break;
1219 }
1220 fclose(proc_stat);
1222 if (!value_valid)
1223 return (-1);
1225 ps_submit_fork_rate (value.derive);
1226 return (0);
1227 }
1228 #endif /*KERNEL_LINUX */
1230 #if KERNEL_SOLARIS
1231 static char *ps_get_cmdline (long pid, char *name __attribute__((unused)), /* {{{ */
1232 char *buffer, size_t buffer_size)
1233 {
1234 char path[PATH_MAX];
1235 psinfo_t info;
1236 int status;
1238 snprintf(path, sizeof (path), "/proc/%li/psinfo", pid);
1240 status = read_file_contents (path, (void *) &info, sizeof (info));
1241 if (status != sizeof (info))
1242 {
1243 ERROR ("processes plugin: Unexpected return value "
1244 "while reading \"%s\": "
1245 "Returned %i but expected %zu.",
1246 path, status, buffer_size);
1247 return (NULL);
1248 }
1250 info.pr_psargs[sizeof (info.pr_psargs) - 1] = 0;
1251 sstrncpy (buffer, info.pr_psargs, buffer_size);
1253 return (buffer);
1254 } /* }}} int ps_get_cmdline */
1256 /*
1257 * Reads process information on the Solaris OS. The information comes mainly from
1258 * /proc/PID/status, /proc/PID/psinfo and /proc/PID/usage
1259 * The values for input and ouput chars are calculated "by hand"
1260 * Added a few "solaris" specific process states as well
1261 */
1262 static int ps_read_process(long pid, procstat_t *ps, char *state)
1263 {
1264 char filename[64];
1265 char f_psinfo[64], f_usage[64];
1266 char *buffer;
1268 pstatus_t *myStatus;
1269 psinfo_t *myInfo;
1270 prusage_t *myUsage;
1272 snprintf(filename, sizeof (filename), "/proc/%li/status", pid);
1273 snprintf(f_psinfo, sizeof (f_psinfo), "/proc/%li/psinfo", pid);
1274 snprintf(f_usage, sizeof (f_usage), "/proc/%li/usage", pid);
1277 buffer = malloc(sizeof (pstatus_t));
1278 memset(buffer, 0, sizeof (pstatus_t));
1279 read_file_contents(filename, buffer, sizeof (pstatus_t));
1280 myStatus = (pstatus_t *) buffer;
1282 buffer = malloc(sizeof (psinfo_t));
1283 memset(buffer, 0, sizeof(psinfo_t));
1284 read_file_contents(f_psinfo, buffer, sizeof (psinfo_t));
1285 myInfo = (psinfo_t *) buffer;
1287 buffer = malloc(sizeof (prusage_t));
1288 memset(buffer, 0, sizeof(prusage_t));
1289 read_file_contents(f_usage, buffer, sizeof (prusage_t));
1290 myUsage = (prusage_t *) buffer;
1292 sstrncpy(ps->name, myInfo->pr_fname, sizeof (myInfo->pr_fname));
1293 ps->num_lwp = myStatus->pr_nlwp;
1294 if (myInfo->pr_wstat != 0) {
1295 ps->num_proc = 0;
1296 ps->num_lwp = 0;
1297 *state = (char) 'Z';
1299 sfree(myStatus);
1300 sfree(myInfo);
1301 sfree(myUsage);
1302 return (0);
1303 } else {
1304 ps->num_proc = 1;
1305 ps->num_lwp = myInfo->pr_nlwp;
1306 }
1308 /*
1309 * Convert system time and user time from nanoseconds to microseconds
1310 * for compatibility with the linux module
1311 */
1312 ps->cpu_system_counter = myStatus -> pr_stime.tv_nsec / 1000;
1313 ps->cpu_user_counter = myStatus -> pr_utime.tv_nsec / 1000;
1315 /*
1316 * Convert rssize from KB to bytes to be consistent w/ the linux module
1317 */
1318 ps->vmem_rss = myInfo->pr_rssize * 1024;
1319 ps->vmem_size = myInfo->pr_size * 1024;
1320 ps->vmem_minflt_counter = myUsage->pr_minf;
1321 ps->vmem_majflt_counter = myUsage->pr_majf;
1323 /*
1324 * TODO: Data and code segment calculations for Solaris
1325 */
1327 ps->vmem_data = -1;
1328 ps->vmem_code = -1;
1329 ps->stack_size = myStatus->pr_stksize;
1331 /*
1332 * Calculating input/ouput chars
1333 * Formula used is total chars / total blocks => chars/block
1334 * then convert input/output blocks to chars
1335 */
1336 ulong_t tot_chars = myUsage->pr_ioch;
1337 ulong_t tot_blocks = myUsage->pr_inblk + myUsage->pr_oublk;
1338 ulong_t chars_per_block = 1;
1339 if (tot_blocks != 0)
1340 chars_per_block = tot_chars / tot_blocks;
1341 ps->io_rchar = myUsage->pr_inblk * chars_per_block;
1342 ps->io_wchar = myUsage->pr_oublk * chars_per_block;
1343 ps->io_syscr = myUsage->pr_sysc;
1344 ps->io_syscw = myUsage->pr_sysc;
1347 /*
1348 * TODO: Find way of setting BLOCKED and PAGING status
1349 */
1351 *state = (char) 'R';
1352 if (myStatus->pr_flags & PR_ASLEEP)
1353 *state = (char) 'S';
1354 else if (myStatus->pr_flags & PR_STOPPED)
1355 *state = (char) 'T';
1356 else if (myStatus->pr_flags & PR_DETACH)
1357 *state = (char) 'E';
1358 else if (myStatus->pr_flags & PR_DAEMON)
1359 *state = (char) 'A';
1360 else if (myStatus->pr_flags & PR_ISSYS)
1361 *state = (char) 'Y';
1362 else if (myStatus->pr_flags & PR_ORPHAN)
1363 *state = (char) 'O';
1365 sfree(myStatus);
1366 sfree(myInfo);
1367 sfree(myUsage);
1369 return (0);
1370 }
1372 /*
1373 * Reads the number of threads created since the last reboot. On Solaris these
1374 * are retrieved from kstat (module cpu, name sys, class misc, stat nthreads).
1375 * The result is the sum for all the threads created on each cpu
1376 */
1377 static int read_fork_rate()
1378 {
1379 extern kstat_ctl_t *kc;
1380 kstat_t *ksp_chain = NULL;
1381 derive_t result = 0;
1383 if (kc == NULL)
1384 return (-1);
1386 for (ksp_chain = kc->kc_chain;
1387 ksp_chain != NULL;
1388 ksp_chain = ksp_chain->ks_next)
1389 {
1390 if ((strcmp (ksp_chain->ks_module, "cpu") == 0)
1391 && (strcmp (ksp_chain->ks_name, "sys") == 0)
1392 && (strcmp (ksp_chain->ks_class, "misc") == 0))
1393 {
1394 long long tmp;
1396 kstat_read (kc, ksp_chain, NULL);
1398 tmp = get_kstat_value(ksp_chain, "nthreads");
1399 if (tmp != -1LL)
1400 result += tmp;
1401 }
1402 }
1404 ps_submit_fork_rate (result);
1405 return (0);
1406 }
1407 #endif /* KERNEL_SOLARIS */
1409 #if HAVE_THREAD_INFO
1410 static int mach_get_task_name (task_t t, int *pid, char *name, size_t name_max_len)
1411 {
1412 int mib[4];
1414 struct kinfo_proc kp;
1415 size_t kp_size;
1417 mib[0] = CTL_KERN;
1418 mib[1] = KERN_PROC;
1419 mib[2] = KERN_PROC_PID;
1421 if (pid_for_task (t, pid) != KERN_SUCCESS)
1422 return (-1);
1423 mib[3] = *pid;
1425 kp_size = sizeof (kp);
1426 if (sysctl (mib, 4, &kp, &kp_size, NULL, 0) != 0)
1427 return (-1);
1429 if (name_max_len > (MAXCOMLEN + 1))
1430 name_max_len = MAXCOMLEN + 1;
1432 strncpy (name, kp.kp_proc.p_comm, name_max_len - 1);
1433 name[name_max_len - 1] = '\0';
1435 DEBUG ("pid = %i; name = %s;", *pid, name);
1437 /* We don't do the special handling for `p_comm == "LaunchCFMApp"' as
1438 * `top' does it, because it is a lot of work and only used when
1439 * debugging. -octo */
1441 return (0);
1442 }
1443 #endif /* HAVE_THREAD_INFO */
1444 /* ------- end of additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
1446 /* do actual readings from kernel */
1447 static int ps_read (void)
1448 {
1449 #if HAVE_THREAD_INFO
1450 kern_return_t status;
1452 int pset;
1453 processor_set_t port_pset_priv;
1455 int task;
1456 task_array_t task_list;
1457 mach_msg_type_number_t task_list_len;
1459 int task_pid;
1460 char task_name[MAXCOMLEN + 1];
1462 int thread;
1463 thread_act_array_t thread_list;
1464 mach_msg_type_number_t thread_list_len;
1465 thread_basic_info_data_t thread_data;
1466 mach_msg_type_number_t thread_data_len;
1468 int running = 0;
1469 int sleeping = 0;
1470 int zombies = 0;
1471 int stopped = 0;
1472 int blocked = 0;
1474 procstat_t *ps;
1475 procstat_entry_t pse;
1477 ps_list_reset ();
1479 /*
1480 * The Mach-concept is a little different from the traditional UNIX
1481 * concept: All the work is done in threads. Threads are contained in
1482 * `tasks'. Therefore, `task status' doesn't make much sense, since
1483 * it's actually a `thread status'.
1484 * Tasks are assigned to sets of processors, so that's where you go to
1485 * get a list.
1486 */
1487 for (pset = 0; pset < pset_list_len; pset++)
1488 {
1489 if ((status = host_processor_set_priv (port_host_self,
1490 pset_list[pset],
1491 &port_pset_priv)) != KERN_SUCCESS)
1492 {
1493 ERROR ("host_processor_set_priv failed: %s\n",
1494 mach_error_string (status));
1495 continue;
1496 }
1498 if ((status = processor_set_tasks (port_pset_priv,
1499 &task_list,
1500 &task_list_len)) != KERN_SUCCESS)
1501 {
1502 ERROR ("processor_set_tasks failed: %s\n",
1503 mach_error_string (status));
1504 mach_port_deallocate (port_task_self, port_pset_priv);
1505 continue;
1506 }
1508 for (task = 0; task < task_list_len; task++)
1509 {
1510 ps = NULL;
1511 if (mach_get_task_name (task_list[task],
1512 &task_pid,
1513 task_name, PROCSTAT_NAME_LEN) == 0)
1514 {
1515 /* search for at least one match */
1516 for (ps = list_head_g; ps != NULL; ps = ps->next)
1517 /* FIXME: cmdline should be here instead of NULL */
1518 if (ps_list_match (task_name, NULL, ps) == 1)
1519 break;
1520 }
1522 /* Collect more detailed statistics for this process */
1523 if (ps != NULL)
1524 {
1525 task_basic_info_data_t task_basic_info;
1526 mach_msg_type_number_t task_basic_info_len;
1527 task_events_info_data_t task_events_info;
1528 mach_msg_type_number_t task_events_info_len;
1529 task_absolutetime_info_data_t task_absolutetime_info;
1530 mach_msg_type_number_t task_absolutetime_info_len;
1532 memset (&pse, '\0', sizeof (pse));
1533 pse.id = task_pid;
1535 task_basic_info_len = TASK_BASIC_INFO_COUNT;
1536 status = task_info (task_list[task],
1537 TASK_BASIC_INFO,
1538 (task_info_t) &task_basic_info,
1539 &task_basic_info_len);
1540 if (status != KERN_SUCCESS)
1541 {
1542 ERROR ("task_info failed: %s",
1543 mach_error_string (status));
1544 continue; /* with next thread_list */
1545 }
1547 task_events_info_len = TASK_EVENTS_INFO_COUNT;
1548 status = task_info (task_list[task],
1549 TASK_EVENTS_INFO,
1550 (task_info_t) &task_events_info,
1551 &task_events_info_len);
1552 if (status != KERN_SUCCESS)
1553 {
1554 ERROR ("task_info failed: %s",
1555 mach_error_string (status));
1556 continue; /* with next thread_list */
1557 }
1559 task_absolutetime_info_len = TASK_ABSOLUTETIME_INFO_COUNT;
1560 status = task_info (task_list[task],
1561 TASK_ABSOLUTETIME_INFO,
1562 (task_info_t) &task_absolutetime_info,
1563 &task_absolutetime_info_len);
1564 if (status != KERN_SUCCESS)
1565 {
1566 ERROR ("task_info failed: %s",
1567 mach_error_string (status));
1568 continue; /* with next thread_list */
1569 }
1571 pse.num_proc++;
1572 pse.vmem_size = task_basic_info.virtual_size;
1573 pse.vmem_rss = task_basic_info.resident_size;
1574 /* Does not seem to be easily exposed */
1575 pse.vmem_data = 0;
1576 pse.vmem_code = 0;
1578 pse.vmem_minflt_counter = task_events_info.cow_faults;
1579 pse.vmem_majflt_counter = task_events_info.faults;
1581 pse.cpu_user_counter = task_absolutetime_info.total_user;
1582 pse.cpu_system_counter = task_absolutetime_info.total_system;
1583 }
1585 status = task_threads (task_list[task], &thread_list,
1586 &thread_list_len);
1587 if (status != KERN_SUCCESS)
1588 {
1589 /* Apple's `top' treats this case a zombie. It
1590 * makes sense to some extend: A `zombie'
1591 * thread is nonsense, since the task/process
1592 * is dead. */
1593 zombies++;
1594 DEBUG ("task_threads failed: %s",
1595 mach_error_string (status));
1596 if (task_list[task] != port_task_self)
1597 mach_port_deallocate (port_task_self,
1598 task_list[task]);
1599 continue; /* with next task_list */
1600 }
1602 for (thread = 0; thread < thread_list_len; thread++)
1603 {
1604 thread_data_len = THREAD_BASIC_INFO_COUNT;
1605 status = thread_info (thread_list[thread],
1606 THREAD_BASIC_INFO,
1607 (thread_info_t) &thread_data,
1608 &thread_data_len);
1609 if (status != KERN_SUCCESS)
1610 {
1611 ERROR ("thread_info failed: %s",
1612 mach_error_string (status));
1613 if (task_list[task] != port_task_self)
1614 mach_port_deallocate (port_task_self,
1615 thread_list[thread]);
1616 continue; /* with next thread_list */
1617 }
1619 if (ps != NULL)
1620 pse.num_lwp++;
1622 switch (thread_data.run_state)
1623 {
1624 case TH_STATE_RUNNING:
1625 running++;
1626 break;
1627 case TH_STATE_STOPPED:
1628 /* What exactly is `halted'? */
1629 case TH_STATE_HALTED:
1630 stopped++;
1631 break;
1632 case TH_STATE_WAITING:
1633 sleeping++;
1634 break;
1635 case TH_STATE_UNINTERRUPTIBLE:
1636 blocked++;
1637 break;
1638 /* There is no `zombie' case here,
1639 * since there are no zombie-threads.
1640 * There's only zombie tasks, which are
1641 * handled above. */
1642 default:
1643 WARNING ("Unknown thread status: %i",
1644 thread_data.run_state);
1645 break;
1646 } /* switch (thread_data.run_state) */
1648 if (task_list[task] != port_task_self)
1649 {
1650 status = mach_port_deallocate (port_task_self,
1651 thread_list[thread]);
1652 if (status != KERN_SUCCESS)
1653 ERROR ("mach_port_deallocate failed: %s",
1654 mach_error_string (status));
1655 }
1656 } /* for (thread_list) */
1658 if ((status = vm_deallocate (port_task_self,
1659 (vm_address_t) thread_list,
1660 thread_list_len * sizeof (thread_act_t)))
1661 != KERN_SUCCESS)
1662 {
1663 ERROR ("vm_deallocate failed: %s",
1664 mach_error_string (status));
1665 }
1666 thread_list = NULL;
1667 thread_list_len = 0;
1669 /* Only deallocate the task port, if it isn't our own.
1670 * Don't know what would happen in that case, but this
1671 * is what Apple's top does.. ;) */
1672 if (task_list[task] != port_task_self)
1673 {
1674 status = mach_port_deallocate (port_task_self,
1675 task_list[task]);
1676 if (status != KERN_SUCCESS)
1677 ERROR ("mach_port_deallocate failed: %s",
1678 mach_error_string (status));
1679 }
1681 if (ps != NULL)
1682 /* FIXME: cmdline should be here instead of NULL */
1683 ps_list_add (task_name, NULL, &pse);
1684 } /* for (task_list) */
1686 if ((status = vm_deallocate (port_task_self,
1687 (vm_address_t) task_list,
1688 task_list_len * sizeof (task_t))) != KERN_SUCCESS)
1689 {
1690 ERROR ("vm_deallocate failed: %s",
1691 mach_error_string (status));
1692 }
1693 task_list = NULL;
1694 task_list_len = 0;
1696 if ((status = mach_port_deallocate (port_task_self, port_pset_priv))
1697 != KERN_SUCCESS)
1698 {
1699 ERROR ("mach_port_deallocate failed: %s",
1700 mach_error_string (status));
1701 }
1702 } /* for (pset_list) */
1704 ps_submit_state ("running", running);
1705 ps_submit_state ("sleeping", sleeping);
1706 ps_submit_state ("zombies", zombies);
1707 ps_submit_state ("stopped", stopped);
1708 ps_submit_state ("blocked", blocked);
1710 for (ps = list_head_g; ps != NULL; ps = ps->next)
1711 ps_submit_proc_list (ps);
1712 /* #endif HAVE_THREAD_INFO */
1714 #elif KERNEL_LINUX
1715 int running = 0;
1716 int sleeping = 0;
1717 int zombies = 0;
1718 int stopped = 0;
1719 int paging = 0;
1720 int blocked = 0;
1722 struct dirent *ent;
1723 DIR *proc;
1724 long pid;
1726 char cmdline[ARG_MAX];
1728 int status;
1729 procstat_t ps;
1730 procstat_entry_t pse;
1731 char state;
1733 procstat_t *ps_ptr;
1735 running = sleeping = zombies = stopped = paging = blocked = 0;
1736 ps_list_reset ();
1738 if ((proc = opendir ("/proc")) == NULL)
1739 {
1740 char errbuf[1024];
1741 ERROR ("Cannot open `/proc': %s",
1742 sstrerror (errno, errbuf, sizeof (errbuf)));
1743 return (-1);
1744 }
1746 while ((ent = readdir (proc)) != NULL)
1747 {
1748 if (!isdigit (ent->d_name[0]))
1749 continue;
1751 if ((pid = atol (ent->d_name)) < 1)
1752 continue;
1754 status = ps_read_process (pid, &ps, &state);
1755 if (status != 0)
1756 {
1757 DEBUG ("ps_read_process failed: %i", status);
1758 continue;
1759 }
1761 pse.id = pid;
1762 pse.age = 0;
1764 pse.num_proc = ps.num_proc;
1765 pse.num_lwp = ps.num_lwp;
1766 pse.vmem_size = ps.vmem_size;
1767 pse.vmem_rss = ps.vmem_rss;
1768 pse.vmem_data = ps.vmem_data;
1769 pse.vmem_code = ps.vmem_code;
1770 pse.stack_size = ps.stack_size;
1772 pse.vmem_minflt = 0;
1773 pse.vmem_minflt_counter = ps.vmem_minflt_counter;
1774 pse.vmem_majflt = 0;
1775 pse.vmem_majflt_counter = ps.vmem_majflt_counter;
1777 pse.cpu_user = 0;
1778 pse.cpu_user_counter = ps.cpu_user_counter;
1779 pse.cpu_system = 0;
1780 pse.cpu_system_counter = ps.cpu_system_counter;
1782 pse.io_rchar = ps.io_rchar;
1783 pse.io_wchar = ps.io_wchar;
1784 pse.io_syscr = ps.io_syscr;
1785 pse.io_syscw = ps.io_syscw;
1787 switch (state)
1788 {
1789 case 'R': running++; break;
1790 case 'S': sleeping++; break;
1791 case 'D': blocked++; break;
1792 case 'Z': zombies++; break;
1793 case 'T': stopped++; break;
1794 case 'W': paging++; break;
1795 }
1797 ps_list_add (ps.name,
1798 ps_get_cmdline (pid, ps.name, cmdline, sizeof (cmdline)),
1799 &pse);
1800 }
1802 closedir (proc);
1804 ps_submit_state ("running", running);
1805 ps_submit_state ("sleeping", sleeping);
1806 ps_submit_state ("zombies", zombies);
1807 ps_submit_state ("stopped", stopped);
1808 ps_submit_state ("paging", paging);
1809 ps_submit_state ("blocked", blocked);
1811 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1812 ps_submit_proc_list (ps_ptr);
1814 read_fork_rate();
1815 /* #endif KERNEL_LINUX */
1817 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD
1818 int running = 0;
1819 int sleeping = 0;
1820 int zombies = 0;
1821 int stopped = 0;
1822 int blocked = 0;
1823 int idle = 0;
1824 int wait = 0;
1826 kvm_t *kd;
1827 char errbuf[_POSIX2_LINE_MAX];
1828 struct kinfo_proc *procs; /* array of processes */
1829 struct kinfo_proc *proc_ptr = NULL;
1830 int count; /* returns number of processes */
1831 int i;
1833 procstat_t *ps_ptr;
1834 procstat_entry_t pse;
1836 ps_list_reset ();
1838 /* Open the kvm interface, get a descriptor */
1839 kd = kvm_openfiles (NULL, "/dev/null", NULL, 0, errbuf);
1840 if (kd == NULL)
1841 {
1842 ERROR ("processes plugin: Cannot open kvm interface: %s",
1843 errbuf);
1844 return (0);
1845 }
1847 /* Get the list of processes. */
1848 procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, &count);
1849 if (procs == NULL)
1850 {
1851 ERROR ("processes plugin: Cannot get kvm processes list: %s",
1852 kvm_geterr(kd));
1853 kvm_close (kd);
1854 return (0);
1855 }
1857 /* Iterate through the processes in kinfo_proc */
1858 for (i = 0; i < count; i++)
1859 {
1860 /* Create only one process list entry per _process_, i.e.
1861 * filter out threads (duplicate PID entries). */
1862 if ((proc_ptr == NULL) || (proc_ptr->ki_pid != procs[i].ki_pid))
1863 {
1864 char cmdline[ARG_MAX] = "";
1865 _Bool have_cmdline = 0;
1867 proc_ptr = &(procs[i]);
1868 /* Don't probe system processes and processes without arguments */
1869 if (((procs[i].ki_flag & P_SYSTEM) == 0)
1870 && (procs[i].ki_args != NULL))
1871 {
1872 char **argv;
1873 int argc;
1874 int status;
1876 /* retrieve the arguments */
1877 argv = kvm_getargv (kd, proc_ptr, /* nchr = */ 0);
1878 argc = 0;
1879 if ((argv != NULL) && (argv[0] != NULL))
1880 {
1881 while (argv[argc] != NULL)
1882 argc++;
1884 status = strjoin (cmdline, sizeof (cmdline), argv, argc, " ");
1885 if (status < 0)
1886 WARNING ("processes plugin: Command line did not fit into buffer.");
1887 else
1888 have_cmdline = 1;
1889 }
1890 } /* if (process has argument list) */
1892 pse.id = procs[i].ki_pid;
1893 pse.age = 0;
1895 pse.num_proc = 1;
1896 pse.num_lwp = procs[i].ki_numthreads;
1898 pse.vmem_size = procs[i].ki_size;
1899 pse.vmem_rss = procs[i].ki_rssize * pagesize;
1900 pse.vmem_data = procs[i].ki_dsize * pagesize;
1901 pse.vmem_code = procs[i].ki_tsize * pagesize;
1902 pse.stack_size = procs[i].ki_ssize * pagesize;
1903 pse.vmem_minflt = 0;
1904 pse.vmem_minflt_counter = procs[i].ki_rusage.ru_minflt;
1905 pse.vmem_majflt = 0;
1906 pse.vmem_majflt_counter = procs[i].ki_rusage.ru_majflt;
1908 pse.cpu_user = 0;
1909 pse.cpu_system = 0;
1910 pse.cpu_user_counter = 0;
1911 pse.cpu_system_counter = 0;
1912 /*
1913 * The u-area might be swapped out, and we can't get
1914 * at it because we have a crashdump and no swap.
1915 * If it's here fill in these fields, otherwise, just
1916 * leave them 0.
1917 */
1918 if (procs[i].ki_flag & P_INMEM)
1919 {
1920 pse.cpu_user_counter = procs[i].ki_rusage.ru_utime.tv_usec
1921 + (1000000lu * procs[i].ki_rusage.ru_utime.tv_sec);
1922 pse.cpu_system_counter = procs[i].ki_rusage.ru_stime.tv_usec
1923 + (1000000lu * procs[i].ki_rusage.ru_stime.tv_sec);
1924 }
1926 /* no I/O data */
1927 pse.io_rchar = -1;
1928 pse.io_wchar = -1;
1929 pse.io_syscr = -1;
1930 pse.io_syscw = -1;
1932 ps_list_add (procs[i].ki_comm, have_cmdline ? cmdline : NULL, &pse);
1934 switch (procs[i].ki_stat)
1935 {
1936 case SSTOP: stopped++; break;
1937 case SSLEEP: sleeping++; break;
1938 case SRUN: running++; break;
1939 case SIDL: idle++; break;
1940 case SWAIT: wait++; break;
1941 case SLOCK: blocked++; break;
1942 case SZOMB: zombies++; break;
1943 }
1944 } /* if ((proc_ptr == NULL) || (proc_ptr->ki_pid != procs[i].ki_pid)) */
1945 }
1947 kvm_close(kd);
1949 ps_submit_state ("running", running);
1950 ps_submit_state ("sleeping", sleeping);
1951 ps_submit_state ("zombies", zombies);
1952 ps_submit_state ("stopped", stopped);
1953 ps_submit_state ("blocked", blocked);
1954 ps_submit_state ("idle", idle);
1955 ps_submit_state ("wait", wait);
1957 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1958 ps_submit_proc_list (ps_ptr);
1959 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
1961 #elif HAVE_PROCINFO_H
1962 /* AIX */
1963 int running = 0;
1964 int sleeping = 0;
1965 int zombies = 0;
1966 int stopped = 0;
1967 int paging = 0;
1968 int blocked = 0;
1970 pid_t pindex = 0;
1971 int nprocs;
1973 procstat_t *ps;
1974 procstat_entry_t pse;
1976 ps_list_reset ();
1977 while ((nprocs = getprocs64 (procentry, sizeof(struct procentry64),
1978 /* fdsinfo = */ NULL, sizeof(struct fdsinfo64),
1979 &pindex, MAXPROCENTRY)) > 0)
1980 {
1981 int i;
1983 for (i = 0; i < nprocs; i++)
1984 {
1985 tid64_t thindex;
1986 int nthreads;
1987 char arglist[MAXARGLN+1];
1988 char *cargs;
1989 char *cmdline;
1991 if (procentry[i].pi_state == SNONE) continue;
1992 /* if (procentry[i].pi_state == SZOMB) FIXME */
1994 cmdline = procentry[i].pi_comm;
1995 cargs = procentry[i].pi_comm;
1996 if ( procentry[i].pi_flags & SKPROC )
1997 {
1998 if (procentry[i].pi_pid == 0)
1999 cmdline = "swapper";
2000 cargs = cmdline;
2001 }
2002 else
2003 {
2004 if (getargs(&procentry[i], sizeof(struct procentry64), arglist, MAXARGLN) >= 0)
2005 {
2006 int n;
2008 n = -1;
2009 while (++n < MAXARGLN)
2010 {
2011 if (arglist[n] == '\0')
2012 {
2013 if (arglist[n+1] == '\0')
2014 break;
2015 arglist[n] = ' ';
2016 }
2017 }
2018 cargs = arglist;
2019 }
2020 }
2022 pse.id = procentry[i].pi_pid;
2023 pse.age = 0;
2024 pse.num_lwp = procentry[i].pi_thcount;
2025 pse.num_proc = 1;
2027 thindex=0;
2028 while ((nthreads = getthrds64(procentry[i].pi_pid,
2029 thrdentry, sizeof(struct thrdentry64),
2030 &thindex, MAXTHRDENTRY)) > 0)
2031 {
2032 int j;
2034 for (j=0; j< nthreads; j++)
2035 {
2036 switch (thrdentry[j].ti_state)
2037 {
2038 /* case TSNONE: break; */
2039 case TSIDL: blocked++; break; /* FIXME is really blocked */
2040 case TSRUN: running++; break;
2041 case TSSLEEP: sleeping++; break;
2042 case TSSWAP: paging++; break;
2043 case TSSTOP: stopped++; break;
2044 case TSZOMB: zombies++; break;
2045 }
2046 }
2047 if (nthreads < MAXTHRDENTRY)
2048 break;
2049 }
2051 pse.cpu_user = 0;
2052 /* tv_usec is nanosec ??? */
2053 pse.cpu_user_counter = procentry[i].pi_ru.ru_utime.tv_sec * 1000000 +
2054 procentry[i].pi_ru.ru_utime.tv_usec / 1000;
2056 pse.cpu_system = 0;
2057 /* tv_usec is nanosec ??? */
2058 pse.cpu_system_counter = procentry[i].pi_ru.ru_stime.tv_sec * 1000000 +
2059 procentry[i].pi_ru.ru_stime.tv_usec / 1000;
2061 pse.vmem_minflt = 0;
2062 pse.vmem_minflt_counter = procentry[i].pi_minflt;
2063 pse.vmem_majflt = 0;
2064 pse.vmem_majflt_counter = procentry[i].pi_majflt;
2066 pse.vmem_size = procentry[i].pi_tsize + procentry[i].pi_dvm * pagesize;
2067 pse.vmem_rss = (procentry[i].pi_drss + procentry[i].pi_trss) * pagesize;
2068 /* Not supported */
2069 pse.vmem_data = 0;
2070 pse.vmem_code = 0;
2071 pse.stack_size = 0;
2073 pse.io_rchar = -1;
2074 pse.io_wchar = -1;
2075 pse.io_syscr = -1;
2076 pse.io_syscw = -1;
2078 ps_list_add (cmdline, cargs, &pse);
2079 } /* for (i = 0 .. nprocs) */
2081 if (nprocs < MAXPROCENTRY)
2082 break;
2083 } /* while (getprocs64() > 0) */
2084 ps_submit_state ("running", running);
2085 ps_submit_state ("sleeping", sleeping);
2086 ps_submit_state ("zombies", zombies);
2087 ps_submit_state ("stopped", stopped);
2088 ps_submit_state ("paging", paging);
2089 ps_submit_state ("blocked", blocked);
2091 for (ps = list_head_g; ps != NULL; ps = ps->next)
2092 ps_submit_proc_list (ps);
2093 /* #endif HAVE_PROCINFO_H */
2095 #elif KERNEL_SOLARIS
2096 /*
2097 * The Solaris section adds a few more process states and removes some
2098 * process states compared to linux. Most notably there is no "PAGING"
2099 * and "BLOCKED" state for a process. The rest is similar to the linux
2100 * code.
2101 */
2102 int running = 0;
2103 int sleeping = 0;
2104 int zombies = 0;
2105 int stopped = 0;
2106 int detached = 0;
2107 int daemon = 0;
2108 int system = 0;
2109 int orphan = 0;
2111 struct dirent *ent;
2112 DIR *proc;
2114 int status;
2115 procstat_t *ps_ptr;
2116 char state;
2118 char cmdline[PRARGSZ];
2120 ps_list_reset ();
2122 proc = opendir ("/proc");
2123 if (proc == NULL)
2124 return (-1);
2126 while ((ent = readdir(proc)) != NULL)
2127 {
2128 long pid;
2129 struct procstat ps;
2130 procstat_entry_t pse;
2131 char *endptr;
2133 if (!isdigit ((int) ent->d_name[0]))
2134 continue;
2136 pid = strtol (ent->d_name, &endptr, 10);
2137 if (*endptr != 0) /* value didn't completely parse as a number */
2138 continue;
2140 status = ps_read_process (pid, &ps, &state);
2141 if (status != 0)
2142 {
2143 DEBUG("ps_read_process failed: %i", status);
2144 continue;
2145 }
2147 pse.id = pid;
2148 pse.age = 0;
2150 pse.num_proc = ps.num_proc;
2151 pse.num_lwp = ps.num_lwp;
2152 pse.vmem_size = ps.vmem_size;
2153 pse.vmem_rss = ps.vmem_rss;
2154 pse.vmem_data = ps.vmem_data;
2155 pse.vmem_code = ps.vmem_code;
2156 pse.stack_size = ps.stack_size;
2158 pse.vmem_minflt = 0;
2159 pse.vmem_minflt_counter = ps.vmem_minflt_counter;
2160 pse.vmem_majflt = 0;
2161 pse.vmem_majflt_counter = ps.vmem_majflt_counter;
2163 pse.cpu_user = 0;
2164 pse.cpu_user_counter = ps.cpu_user_counter;
2165 pse.cpu_system = 0;
2166 pse.cpu_system_counter = ps.cpu_system_counter;
2168 pse.io_rchar = ps.io_rchar;
2169 pse.io_wchar = ps.io_wchar;
2170 pse.io_syscr = ps.io_syscr;
2171 pse.io_syscw = ps.io_syscw;
2173 switch (state)
2174 {
2175 case 'R': running++; break;
2176 case 'S': sleeping++; break;
2177 case 'E': detached++; break;
2178 case 'Z': zombies++; break;
2179 case 'T': stopped++; break;
2180 case 'A': daemon++; break;
2181 case 'Y': system++; break;
2182 case 'O': orphan++; break;
2183 }
2186 ps_list_add (ps.name,
2187 ps_get_cmdline (pid, ps.name, cmdline, sizeof (cmdline)),
2188 &pse);
2189 } /* while(readdir) */
2190 closedir (proc);
2192 ps_submit_state ("running", running);
2193 ps_submit_state ("sleeping", sleeping);
2194 ps_submit_state ("zombies", zombies);
2195 ps_submit_state ("stopped", stopped);
2196 ps_submit_state ("detached", detached);
2197 ps_submit_state ("daemon", daemon);
2198 ps_submit_state ("system", system);
2199 ps_submit_state ("orphan", orphan);
2201 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
2202 ps_submit_proc_list (ps_ptr);
2204 read_fork_rate();
2205 #endif /* KERNEL_SOLARIS */
2207 return (0);
2208 } /* int ps_read */
2210 void module_register (void)
2211 {
2212 plugin_register_complex_config ("processes", ps_config);
2213 plugin_register_init ("processes", ps_init);
2214 plugin_register_read ("processes", ps_read);
2215 } /* void module_register */