fb88a3153e1e150618805141404da25100602498
1 /**
2 * collectd - src/processes.c
3 * Copyright (C) 2005 Lyonel Vincent
4 * Copyright (C) 2006-2010 Florian octo Forster
5 * Copyright (C) 2008 Oleg King
6 * Copyright (C) 2009 Sebastian Harl
7 * Copyright (C) 2009 Andrés J. Díaz
8 * Copyright (C) 2009 Manuel Sanmartin
9 * Copyright (C) 2010 Clément Stenac
10 * Copyright (C) 2012 Cosmin Ioiart
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 *
26 * Authors:
27 * Lyonel Vincent <lyonel at ezix.org>
28 * Florian octo Forster <octo at collectd.org>
29 * Oleg King <king2 at kaluga.ru>
30 * Sebastian Harl <sh at tokkee.org>
31 * Andrés J. Díaz <ajdiaz at connectical.com>
32 * Manuel Sanmartin
33 * Clément Stenac <clement.stenac at diwi.org>
34 * Cosmin Ioiart <cioiart at gmail.com>
35 **/
37 #include "collectd.h"
38 #include "common.h"
39 #include "plugin.h"
40 #include "configfile.h"
42 /* Include header files for the mach system, if they exist.. */
43 #if HAVE_THREAD_INFO
44 # if HAVE_MACH_MACH_INIT_H
45 # include <mach/mach_init.h>
46 # endif
47 # if HAVE_MACH_HOST_PRIV_H
48 # include <mach/host_priv.h>
49 # endif
50 # if HAVE_MACH_MACH_ERROR_H
51 # include <mach/mach_error.h>
52 # endif
53 # if HAVE_MACH_MACH_HOST_H
54 # include <mach/mach_host.h>
55 # endif
56 # if HAVE_MACH_MACH_PORT_H
57 # include <mach/mach_port.h>
58 # endif
59 # if HAVE_MACH_MACH_TYPES_H
60 # include <mach/mach_types.h>
61 # endif
62 # if HAVE_MACH_MESSAGE_H
63 # include <mach/message.h>
64 # endif
65 # if HAVE_MACH_PROCESSOR_SET_H
66 # include <mach/processor_set.h>
67 # endif
68 # if HAVE_MACH_TASK_H
69 # include <mach/task.h>
70 # endif
71 # if HAVE_MACH_THREAD_ACT_H
72 # include <mach/thread_act.h>
73 # endif
74 # if HAVE_MACH_VM_REGION_H
75 # include <mach/vm_region.h>
76 # endif
77 # if HAVE_MACH_VM_MAP_H
78 # include <mach/vm_map.h>
79 # endif
80 # if HAVE_MACH_VM_PROT_H
81 # include <mach/vm_prot.h>
82 # endif
83 # if HAVE_SYS_SYSCTL_H
84 # include <sys/sysctl.h>
85 # endif
86 /* #endif HAVE_THREAD_INFO */
88 #elif KERNEL_LINUX
89 # if HAVE_LINUX_CONFIG_H
90 # include <linux/config.h>
91 # endif
92 # ifndef CONFIG_HZ
93 # define CONFIG_HZ 100
94 # endif
95 /* #endif KERNEL_LINUX */
97 #elif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD)
98 # include <kvm.h>
99 # include <sys/param.h>
100 # include <sys/sysctl.h>
101 # include <sys/user.h>
102 # include <sys/proc.h>
103 /* #endif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD) */
105 #elif HAVE_PROCINFO_H
106 # include <procinfo.h>
107 # include <sys/types.h>
109 #define MAXPROCENTRY 32
110 #define MAXTHRDENTRY 16
111 #define MAXARGLN 1024
112 /* #endif HAVE_PROCINFO_H */
114 #elif KERNEL_SOLARIS
115 # include <procfs.h>
116 # include <dirent.h>
117 /* #endif KERNEL_SOLARIS */
119 #else
120 # error "No applicable input method."
121 #endif
123 #if HAVE_REGEX_H
124 # include <regex.h>
125 #endif
127 #if HAVE_KSTAT_H
128 # include <kstat.h>
129 #endif
131 #ifndef CMDLINE_BUFFER_SIZE
132 # if defined(ARG_MAX) && (ARG_MAX < 4096)
133 # define CMDLINE_BUFFER_SIZE ARG_MAX
134 # else
135 # define CMDLINE_BUFFER_SIZE 4096
136 # endif
137 #endif
139 typedef struct procstat_entry_s
140 {
141 unsigned long id;
142 unsigned long age;
144 unsigned long num_proc;
145 unsigned long num_lwp;
146 unsigned long vmem_size;
147 unsigned long vmem_rss;
148 unsigned long vmem_data;
149 unsigned long vmem_code;
150 unsigned long stack_size;
152 unsigned long vmem_minflt;
153 unsigned long vmem_majflt;
154 derive_t vmem_minflt_counter;
155 derive_t vmem_majflt_counter;
157 unsigned long cpu_user;
158 unsigned long cpu_system;
159 derive_t cpu_user_counter;
160 derive_t cpu_system_counter;
162 /* io data */
163 derive_t io_rchar;
164 derive_t io_wchar;
165 derive_t io_syscr;
166 derive_t io_syscw;
168 struct procstat_entry_s *next;
169 } procstat_entry_t;
171 #define PROCSTAT_NAME_LEN 256
172 typedef struct procstat
173 {
174 char name[PROCSTAT_NAME_LEN];
175 #if HAVE_REGEX_H
176 regex_t *re;
177 #endif
179 unsigned long num_proc;
180 unsigned long num_lwp;
181 unsigned long vmem_size;
182 unsigned long vmem_rss;
183 unsigned long vmem_data;
184 unsigned long vmem_code;
185 unsigned long stack_size;
187 derive_t vmem_minflt_counter;
188 derive_t vmem_majflt_counter;
190 derive_t cpu_user_counter;
191 derive_t cpu_system_counter;
193 /* io data */
194 derive_t io_rchar;
195 derive_t io_wchar;
196 derive_t io_syscr;
197 derive_t io_syscw;
199 struct procstat *next;
200 struct procstat_entry_s *instances;
201 } procstat_t;
203 static procstat_t *list_head_g = NULL;
205 #if HAVE_THREAD_INFO
206 static mach_port_t port_host_self;
207 static mach_port_t port_task_self;
209 static processor_set_name_array_t pset_list;
210 static mach_msg_type_number_t pset_list_len;
211 /* #endif HAVE_THREAD_INFO */
213 #elif KERNEL_LINUX
214 static long pagesize_g;
215 /* #endif KERNEL_LINUX */
217 #elif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD)
218 static int pagesize;
219 /* #endif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD) */
221 #elif HAVE_PROCINFO_H
222 static struct procentry64 procentry[MAXPROCENTRY];
223 static struct thrdentry64 thrdentry[MAXTHRDENTRY];
224 static int pagesize;
226 #ifndef _AIXVERSION_610
227 int getprocs64 (void *procsinfo, int sizproc, void *fdsinfo, int sizfd, pid_t *index, int count);
228 int getthrds64( pid_t, void *, int, tid64_t *, int );
229 #endif
230 int getargs (void *processBuffer, int bufferLen, char *argsBuffer, int argsLen);
231 #endif /* HAVE_PROCINFO_H */
233 /* put name of process from config to list_head_g tree
234 * list_head_g is a list of 'procstat_t' structs with
235 * processes names we want to watch */
236 static void ps_list_register (const char *name, const char *regexp)
237 {
238 procstat_t *new;
239 procstat_t *ptr;
240 int status;
242 new = (procstat_t *) malloc (sizeof (procstat_t));
243 if (new == NULL)
244 {
245 ERROR ("processes plugin: ps_list_register: malloc failed.");
246 return;
247 }
248 memset (new, 0, sizeof (procstat_t));
249 sstrncpy (new->name, name, sizeof (new->name));
251 #if HAVE_REGEX_H
252 if (regexp != NULL)
253 {
254 DEBUG ("ProcessMatch: adding \"%s\" as criteria to process %s.", regexp, name);
255 new->re = (regex_t *) malloc (sizeof (regex_t));
256 if (new->re == NULL)
257 {
258 ERROR ("processes plugin: ps_list_register: malloc failed.");
259 sfree (new);
260 return;
261 }
263 status = regcomp (new->re, regexp, REG_EXTENDED | REG_NOSUB);
264 if (status != 0)
265 {
266 DEBUG ("ProcessMatch: compiling the regular expression \"%s\" failed.", regexp);
267 sfree(new->re);
268 return;
269 }
270 }
271 #else
272 if (regexp != NULL)
273 {
274 ERROR ("processes plugin: ps_list_register: "
275 "Regular expression \"%s\" found in config "
276 "file, but support for regular expressions "
277 "has been disabled at compile time.",
278 regexp);
279 sfree (new);
280 return;
281 }
282 #endif
284 for (ptr = list_head_g; ptr != NULL; ptr = ptr->next)
285 {
286 if (strcmp (ptr->name, name) == 0)
287 {
288 WARNING ("processes plugin: You have configured more "
289 "than one `Process' or "
290 "`ProcessMatch' with the same name. "
291 "All but the first setting will be "
292 "ignored.");
293 sfree (new->re);
294 sfree (new);
295 return;
296 }
298 if (ptr->next == NULL)
299 break;
300 }
302 if (ptr == NULL)
303 list_head_g = new;
304 else
305 ptr->next = new;
306 } /* void ps_list_register */
308 /* try to match name against entry, returns 1 if success */
309 static int ps_list_match (const char *name, const char *cmdline, procstat_t *ps)
310 {
311 #if HAVE_REGEX_H
312 if (ps->re != NULL)
313 {
314 int status;
315 const char *str;
317 str = cmdline;
318 if ((str == NULL) || (str[0] == 0))
319 str = name;
321 assert (str != NULL);
323 status = regexec (ps->re, str,
324 /* nmatch = */ 0,
325 /* pmatch = */ NULL,
326 /* eflags = */ 0);
327 if (status == 0)
328 return (1);
329 }
330 else
331 #endif
332 if (strcmp (ps->name, name) == 0)
333 return (1);
335 return (0);
336 } /* int ps_list_match */
338 /* add process entry to 'instances' of process 'name' (or refresh it) */
339 static void ps_list_add (const char *name, const char *cmdline, procstat_entry_t *entry)
340 {
341 procstat_t *ps;
342 procstat_entry_t *pse;
344 if (entry->id == 0)
345 return;
347 for (ps = list_head_g; ps != NULL; ps = ps->next)
348 {
349 if ((ps_list_match (name, cmdline, ps)) == 0)
350 continue;
352 for (pse = ps->instances; pse != NULL; pse = pse->next)
353 if ((pse->id == entry->id) || (pse->next == NULL))
354 break;
356 if ((pse == NULL) || (pse->id != entry->id))
357 {
358 procstat_entry_t *new;
360 new = (procstat_entry_t *) malloc (sizeof (procstat_entry_t));
361 if (new == NULL)
362 return;
363 memset (new, 0, sizeof (procstat_entry_t));
364 new->id = entry->id;
366 if (pse == NULL)
367 ps->instances = new;
368 else
369 pse->next = new;
371 pse = new;
372 }
374 pse->age = 0;
375 pse->num_proc = entry->num_proc;
376 pse->num_lwp = entry->num_lwp;
377 pse->vmem_size = entry->vmem_size;
378 pse->vmem_rss = entry->vmem_rss;
379 pse->vmem_data = entry->vmem_data;
380 pse->vmem_code = entry->vmem_code;
381 pse->stack_size = entry->stack_size;
382 pse->io_rchar = entry->io_rchar;
383 pse->io_wchar = entry->io_wchar;
384 pse->io_syscr = entry->io_syscr;
385 pse->io_syscw = entry->io_syscw;
387 ps->num_proc += pse->num_proc;
388 ps->num_lwp += pse->num_lwp;
389 ps->vmem_size += pse->vmem_size;
390 ps->vmem_rss += pse->vmem_rss;
391 ps->vmem_data += pse->vmem_data;
392 ps->vmem_code += pse->vmem_code;
393 ps->stack_size += pse->stack_size;
395 ps->io_rchar += ((pse->io_rchar == -1)?0:pse->io_rchar);
396 ps->io_wchar += ((pse->io_wchar == -1)?0:pse->io_wchar);
397 ps->io_syscr += ((pse->io_syscr == -1)?0:pse->io_syscr);
398 ps->io_syscw += ((pse->io_syscw == -1)?0:pse->io_syscw);
400 if ((entry->vmem_minflt_counter == 0)
401 && (entry->vmem_majflt_counter == 0))
402 {
403 pse->vmem_minflt_counter += entry->vmem_minflt;
404 pse->vmem_minflt = entry->vmem_minflt;
406 pse->vmem_majflt_counter += entry->vmem_majflt;
407 pse->vmem_majflt = entry->vmem_majflt;
408 }
409 else
410 {
411 if (entry->vmem_minflt_counter < pse->vmem_minflt_counter)
412 {
413 pse->vmem_minflt = entry->vmem_minflt_counter
414 + (ULONG_MAX - pse->vmem_minflt_counter);
415 }
416 else
417 {
418 pse->vmem_minflt = entry->vmem_minflt_counter - pse->vmem_minflt_counter;
419 }
420 pse->vmem_minflt_counter = entry->vmem_minflt_counter;
422 if (entry->vmem_majflt_counter < pse->vmem_majflt_counter)
423 {
424 pse->vmem_majflt = entry->vmem_majflt_counter
425 + (ULONG_MAX - pse->vmem_majflt_counter);
426 }
427 else
428 {
429 pse->vmem_majflt = entry->vmem_majflt_counter - pse->vmem_majflt_counter;
430 }
431 pse->vmem_majflt_counter = entry->vmem_majflt_counter;
432 }
434 ps->vmem_minflt_counter += pse->vmem_minflt;
435 ps->vmem_majflt_counter += pse->vmem_majflt;
437 if ((entry->cpu_user_counter == 0)
438 && (entry->cpu_system_counter == 0))
439 {
440 pse->cpu_user_counter += entry->cpu_user;
441 pse->cpu_user = entry->cpu_user;
443 pse->cpu_system_counter += entry->cpu_system;
444 pse->cpu_system = entry->cpu_system;
445 }
446 else
447 {
448 if (entry->cpu_user_counter < pse->cpu_user_counter)
449 {
450 pse->cpu_user = entry->cpu_user_counter
451 + (ULONG_MAX - pse->cpu_user_counter);
452 }
453 else
454 {
455 pse->cpu_user = entry->cpu_user_counter - pse->cpu_user_counter;
456 }
457 pse->cpu_user_counter = entry->cpu_user_counter;
459 if (entry->cpu_system_counter < pse->cpu_system_counter)
460 {
461 pse->cpu_system = entry->cpu_system_counter
462 + (ULONG_MAX - pse->cpu_system_counter);
463 }
464 else
465 {
466 pse->cpu_system = entry->cpu_system_counter - pse->cpu_system_counter;
467 }
468 pse->cpu_system_counter = entry->cpu_system_counter;
469 }
471 ps->cpu_user_counter += pse->cpu_user;
472 ps->cpu_system_counter += pse->cpu_system;
473 }
474 }
476 /* remove old entries from instances of processes in list_head_g */
477 static void ps_list_reset (void)
478 {
479 procstat_t *ps;
480 procstat_entry_t *pse;
481 procstat_entry_t *pse_prev;
483 for (ps = list_head_g; ps != NULL; ps = ps->next)
484 {
485 ps->num_proc = 0;
486 ps->num_lwp = 0;
487 ps->vmem_size = 0;
488 ps->vmem_rss = 0;
489 ps->vmem_data = 0;
490 ps->vmem_code = 0;
491 ps->stack_size = 0;
492 ps->io_rchar = -1;
493 ps->io_wchar = -1;
494 ps->io_syscr = -1;
495 ps->io_syscw = -1;
497 pse_prev = NULL;
498 pse = ps->instances;
499 while (pse != NULL)
500 {
501 if (pse->age > 10)
502 {
503 DEBUG ("Removing this procstat entry cause it's too old: "
504 "id = %lu; name = %s;",
505 pse->id, ps->name);
507 if (pse_prev == NULL)
508 {
509 ps->instances = pse->next;
510 free (pse);
511 pse = ps->instances;
512 }
513 else
514 {
515 pse_prev->next = pse->next;
516 free (pse);
517 pse = pse_prev->next;
518 }
519 }
520 else
521 {
522 pse->age++;
523 pse_prev = pse;
524 pse = pse->next;
525 }
526 } /* while (pse != NULL) */
527 } /* for (ps = list_head_g; ps != NULL; ps = ps->next) */
528 }
530 /* put all pre-defined 'Process' names from config to list_head_g tree */
531 static int ps_config (oconfig_item_t *ci)
532 {
533 int i;
535 for (i = 0; i < ci->children_num; ++i) {
536 oconfig_item_t *c = ci->children + i;
538 if (strcasecmp (c->key, "Process") == 0)
539 {
540 if ((c->values_num != 1)
541 || (OCONFIG_TYPE_STRING != c->values[0].type)) {
542 ERROR ("processes plugin: `Process' expects exactly "
543 "one string argument (got %i).",
544 c->values_num);
545 continue;
546 }
548 if (c->children_num != 0) {
549 WARNING ("processes plugin: the `Process' config option "
550 "does not expect any child elements -- ignoring "
551 "content (%i elements) of the <Process '%s'> block.",
552 c->children_num, c->values[0].value.string);
553 }
555 ps_list_register (c->values[0].value.string, NULL);
556 }
557 else if (strcasecmp (c->key, "ProcessMatch") == 0)
558 {
559 if ((c->values_num != 2)
560 || (OCONFIG_TYPE_STRING != c->values[0].type)
561 || (OCONFIG_TYPE_STRING != c->values[1].type))
562 {
563 ERROR ("processes plugin: `ProcessMatch' needs exactly "
564 "two string arguments (got %i).",
565 c->values_num);
566 continue;
567 }
569 if (c->children_num != 0) {
570 WARNING ("processes plugin: the `ProcessMatch' config option "
571 "does not expect any child elements -- ignoring "
572 "content (%i elements) of the <ProcessMatch '%s' '%s'> "
573 "block.", c->children_num, c->values[0].value.string,
574 c->values[1].value.string);
575 }
577 ps_list_register (c->values[0].value.string,
578 c->values[1].value.string);
579 }
580 else
581 {
582 ERROR ("processes plugin: The `%s' configuration option is not "
583 "understood and will be ignored.", c->key);
584 continue;
585 }
586 }
588 return (0);
589 }
591 static int ps_init (void)
592 {
593 #if HAVE_THREAD_INFO
594 kern_return_t status;
596 port_host_self = mach_host_self ();
597 port_task_self = mach_task_self ();
599 if (pset_list != NULL)
600 {
601 vm_deallocate (port_task_self,
602 (vm_address_t) pset_list,
603 pset_list_len * sizeof (processor_set_t));
604 pset_list = NULL;
605 pset_list_len = 0;
606 }
608 if ((status = host_processor_sets (port_host_self,
609 &pset_list,
610 &pset_list_len)) != KERN_SUCCESS)
611 {
612 ERROR ("host_processor_sets failed: %s\n",
613 mach_error_string (status));
614 pset_list = NULL;
615 pset_list_len = 0;
616 return (-1);
617 }
618 /* #endif HAVE_THREAD_INFO */
620 #elif KERNEL_LINUX
621 pagesize_g = sysconf(_SC_PAGESIZE);
622 DEBUG ("pagesize_g = %li; CONFIG_HZ = %i;",
623 pagesize_g, CONFIG_HZ);
624 /* #endif KERNEL_LINUX */
626 #elif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD)
627 pagesize = getpagesize();
628 /* #endif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD) */
630 #elif HAVE_PROCINFO_H
631 pagesize = getpagesize();
632 #endif /* HAVE_PROCINFO_H */
634 return (0);
635 } /* int ps_init */
637 /* submit global state (e.g.: qty of zombies, running, etc..) */
638 static void ps_submit_state (const char *state, double value)
639 {
640 value_t values[1];
641 value_list_t vl = VALUE_LIST_INIT;
643 values[0].gauge = value;
645 vl.values = values;
646 vl.values_len = 1;
647 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
648 sstrncpy (vl.plugin, "processes", sizeof (vl.plugin));
649 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
650 sstrncpy (vl.type, "ps_state", sizeof (vl.type));
651 sstrncpy (vl.type_instance, state, sizeof (vl.type_instance));
653 plugin_dispatch_values (&vl);
654 }
656 /* submit info about specific process (e.g.: memory taken, cpu usage, etc..) */
657 static void ps_submit_proc_list (procstat_t *ps)
658 {
659 value_t values[2];
660 value_list_t vl = VALUE_LIST_INIT;
662 vl.values = values;
663 vl.values_len = 2;
664 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
665 sstrncpy (vl.plugin, "processes", sizeof (vl.plugin));
666 sstrncpy (vl.plugin_instance, ps->name, sizeof (vl.plugin_instance));
668 sstrncpy (vl.type, "ps_vm", sizeof (vl.type));
669 vl.values[0].gauge = ps->vmem_size;
670 vl.values_len = 1;
671 plugin_dispatch_values (&vl);
673 sstrncpy (vl.type, "ps_rss", sizeof (vl.type));
674 vl.values[0].gauge = ps->vmem_rss;
675 vl.values_len = 1;
676 plugin_dispatch_values (&vl);
678 sstrncpy (vl.type, "ps_data", sizeof (vl.type));
679 vl.values[0].gauge = ps->vmem_data;
680 vl.values_len = 1;
681 plugin_dispatch_values (&vl);
683 sstrncpy (vl.type, "ps_code", sizeof (vl.type));
684 vl.values[0].gauge = ps->vmem_code;
685 vl.values_len = 1;
686 plugin_dispatch_values (&vl);
688 sstrncpy (vl.type, "ps_stacksize", sizeof (vl.type));
689 vl.values[0].gauge = ps->stack_size;
690 vl.values_len = 1;
691 plugin_dispatch_values (&vl);
693 sstrncpy (vl.type, "ps_cputime", sizeof (vl.type));
694 vl.values[0].derive = ps->cpu_user_counter;
695 vl.values[1].derive = ps->cpu_system_counter;
696 vl.values_len = 2;
697 plugin_dispatch_values (&vl);
699 sstrncpy (vl.type, "ps_count", sizeof (vl.type));
700 vl.values[0].gauge = ps->num_proc;
701 vl.values[1].gauge = ps->num_lwp;
702 vl.values_len = 2;
703 plugin_dispatch_values (&vl);
705 sstrncpy (vl.type, "ps_pagefaults", sizeof (vl.type));
706 vl.values[0].derive = ps->vmem_minflt_counter;
707 vl.values[1].derive = ps->vmem_majflt_counter;
708 vl.values_len = 2;
709 plugin_dispatch_values (&vl);
711 if ( (ps->io_rchar != -1) && (ps->io_wchar != -1) )
712 {
713 sstrncpy (vl.type, "ps_disk_octets", sizeof (vl.type));
714 vl.values[0].derive = ps->io_rchar;
715 vl.values[1].derive = ps->io_wchar;
716 vl.values_len = 2;
717 plugin_dispatch_values (&vl);
718 }
720 if ( (ps->io_syscr != -1) && (ps->io_syscw != -1) )
721 {
722 sstrncpy (vl.type, "ps_disk_ops", sizeof (vl.type));
723 vl.values[0].derive = ps->io_syscr;
724 vl.values[1].derive = ps->io_syscw;
725 vl.values_len = 2;
726 plugin_dispatch_values (&vl);
727 }
729 DEBUG ("name = %s; num_proc = %lu; num_lwp = %lu; "
730 "vmem_size = %lu; vmem_rss = %lu; vmem_data = %lu; "
731 "vmem_code = %lu; "
732 "vmem_minflt_counter = %"PRIi64"; vmem_majflt_counter = %"PRIi64"; "
733 "cpu_user_counter = %"PRIi64"; cpu_system_counter = %"PRIi64"; "
734 "io_rchar = %"PRIi64"; io_wchar = %"PRIi64"; "
735 "io_syscr = %"PRIi64"; io_syscw = %"PRIi64";",
736 ps->name, ps->num_proc, ps->num_lwp,
737 ps->vmem_size, ps->vmem_rss,
738 ps->vmem_data, ps->vmem_code,
739 ps->vmem_minflt_counter, ps->vmem_majflt_counter,
740 ps->cpu_user_counter, ps->cpu_system_counter,
741 ps->io_rchar, ps->io_wchar, ps->io_syscr, ps->io_syscw);
742 } /* void ps_submit_proc_list */
744 #if KERNEL_LINUX || KERNEL_SOLARIS
745 static void ps_submit_fork_rate (derive_t value)
746 {
747 value_t values[1];
748 value_list_t vl = VALUE_LIST_INIT;
750 values[0].derive = value;
752 vl.values = values;
753 vl.values_len = 1;
754 sstrncpy(vl.host, hostname_g, sizeof (vl.host));
755 sstrncpy(vl.plugin, "processes", sizeof (vl.plugin));
756 sstrncpy(vl.plugin_instance, "", sizeof (vl.plugin_instance));
757 sstrncpy(vl.type, "fork_rate", sizeof (vl.type));
758 sstrncpy(vl.type_instance, "", sizeof (vl.type_instance));
760 plugin_dispatch_values(&vl);
761 }
762 #endif /* KERNEL_LINUX || KERNEL_SOLARIS*/
764 /* ------- additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
765 #if KERNEL_LINUX
766 static int ps_read_tasks (int pid)
767 {
768 char dirname[64];
769 DIR *dh;
770 struct dirent *ent;
771 int count = 0;
773 ssnprintf (dirname, sizeof (dirname), "/proc/%i/task", pid);
775 if ((dh = opendir (dirname)) == NULL)
776 {
777 DEBUG ("Failed to open directory `%s'", dirname);
778 return (-1);
779 }
781 while ((ent = readdir (dh)) != NULL)
782 {
783 if (!isdigit ((int) ent->d_name[0]))
784 continue;
785 else
786 count++;
787 }
788 closedir (dh);
790 return ((count >= 1) ? count : 1);
791 } /* int *ps_read_tasks */
793 /* Read advanced virtual memory data from /proc/pid/status */
794 static procstat_t *ps_read_vmem (int pid, procstat_t *ps)
795 {
796 FILE *fh;
797 char buffer[1024];
798 char filename[64];
799 unsigned long long lib = 0;
800 unsigned long long exe = 0;
801 unsigned long long data = 0;
802 char *fields[8];
803 int numfields;
805 ssnprintf (filename, sizeof (filename), "/proc/%i/status", pid);
806 if ((fh = fopen (filename, "r")) == NULL)
807 return (NULL);
809 while (fgets (buffer, sizeof(buffer), fh) != NULL)
810 {
811 long long tmp;
812 char *endptr;
814 if (strncmp (buffer, "Vm", 2) != 0)
815 continue;
817 numfields = strsplit (buffer, fields,
818 STATIC_ARRAY_SIZE (fields));
820 if (numfields < 2)
821 continue;
823 errno = 0;
824 endptr = NULL;
825 tmp = strtoll (fields[1], &endptr, /* base = */ 10);
826 if ((errno == 0) && (endptr != fields[1]))
827 {
828 if (strncmp (buffer, "VmData", 6) == 0)
829 {
830 data = tmp;
831 }
832 else if (strncmp (buffer, "VmLib", 5) == 0)
833 {
834 lib = tmp;
835 }
836 else if (strncmp(buffer, "VmExe", 5) == 0)
837 {
838 exe = tmp;
839 }
840 }
841 } /* while (fgets) */
843 if (fclose (fh))
844 {
845 char errbuf[1024];
846 WARNING ("processes: fclose: %s",
847 sstrerror (errno, errbuf, sizeof (errbuf)));
848 }
850 ps->vmem_data = data * 1024;
851 ps->vmem_code = (exe + lib) * 1024;
853 return (ps);
854 } /* procstat_t *ps_read_vmem */
856 static procstat_t *ps_read_io (int pid, procstat_t *ps)
857 {
858 FILE *fh;
859 char buffer[1024];
860 char filename[64];
862 char *fields[8];
863 int numfields;
865 ssnprintf (filename, sizeof (filename), "/proc/%i/io", pid);
866 if ((fh = fopen (filename, "r")) == NULL)
867 return (NULL);
869 while (fgets (buffer, sizeof (buffer), fh) != NULL)
870 {
871 derive_t *val = NULL;
872 long long tmp;
873 char *endptr;
875 if (strncasecmp (buffer, "rchar:", 6) == 0)
876 val = &(ps->io_rchar);
877 else if (strncasecmp (buffer, "wchar:", 6) == 0)
878 val = &(ps->io_wchar);
879 else if (strncasecmp (buffer, "syscr:", 6) == 0)
880 val = &(ps->io_syscr);
881 else if (strncasecmp (buffer, "syscw:", 6) == 0)
882 val = &(ps->io_syscw);
883 else
884 continue;
886 numfields = strsplit (buffer, fields,
887 STATIC_ARRAY_SIZE (fields));
889 if (numfields < 2)
890 continue;
892 errno = 0;
893 endptr = NULL;
894 tmp = strtoll (fields[1], &endptr, /* base = */ 10);
895 if ((errno != 0) || (endptr == fields[1]))
896 *val = -1;
897 else
898 *val = (derive_t) tmp;
899 } /* while (fgets) */
901 if (fclose (fh))
902 {
903 char errbuf[1024];
904 WARNING ("processes: fclose: %s",
905 sstrerror (errno, errbuf, sizeof (errbuf)));
906 }
908 return (ps);
909 } /* procstat_t *ps_read_io */
911 int ps_read_process (int pid, procstat_t *ps, char *state)
912 {
913 char filename[64];
914 char buffer[1024];
916 char *fields[64];
917 char fields_len;
919 int buffer_len;
921 char *buffer_ptr;
922 size_t name_start_pos;
923 size_t name_end_pos;
924 size_t name_len;
926 derive_t cpu_user_counter;
927 derive_t cpu_system_counter;
928 long long unsigned vmem_size;
929 long long unsigned vmem_rss;
930 long long unsigned stack_size;
932 memset (ps, 0, sizeof (procstat_t));
934 ssnprintf (filename, sizeof (filename), "/proc/%i/stat", pid);
936 buffer_len = read_file_contents (filename,
937 buffer, sizeof(buffer) - 1);
938 if (buffer_len <= 0)
939 return (-1);
940 buffer[buffer_len] = 0;
942 /* The name of the process is enclosed in parens. Since the name can
943 * contain parens itself, spaces, numbers and pretty much everything
944 * else, use these to determine the process name. We don't use
945 * strchr(3) and strrchr(3) to avoid pointer arithmetic which would
946 * otherwise be required to determine name_len. */
947 name_start_pos = 0;
948 while ((buffer[name_start_pos] != '(')
949 && (name_start_pos < buffer_len))
950 name_start_pos++;
952 name_end_pos = buffer_len;
953 while ((buffer[name_end_pos] != ')')
954 && (name_end_pos > 0))
955 name_end_pos--;
957 /* Either '(' or ')' is not found or they are in the wrong order.
958 * Anyway, something weird that shouldn't happen ever. */
959 if (name_start_pos >= name_end_pos)
960 {
961 ERROR ("processes plugin: name_start_pos = %zu >= name_end_pos = %zu",
962 name_start_pos, name_end_pos);
963 return (-1);
964 }
966 name_len = (name_end_pos - name_start_pos) - 1;
967 if (name_len >= sizeof (ps->name))
968 name_len = sizeof (ps->name) - 1;
970 sstrncpy (ps->name, &buffer[name_start_pos + 1], name_len + 1);
972 if ((buffer_len - name_end_pos) < 2)
973 return (-1);
974 buffer_ptr = &buffer[name_end_pos + 2];
976 fields_len = strsplit (buffer_ptr, fields, STATIC_ARRAY_SIZE (fields));
977 if (fields_len < 22)
978 {
979 DEBUG ("processes plugin: ps_read_process (pid = %i):"
980 " `%s' has only %i fields..",
981 (int) pid, filename, fields_len);
982 return (-1);
983 }
985 *state = fields[0][0];
987 if (*state == 'Z')
988 {
989 ps->num_lwp = 0;
990 ps->num_proc = 0;
991 }
992 else
993 {
994 if ( (ps->num_lwp = ps_read_tasks (pid)) == -1 )
995 {
996 /* returns -1 => kernel 2.4 */
997 ps->num_lwp = 1;
998 }
999 ps->num_proc = 1;
1000 }
1002 /* Leave the rest at zero if this is only a zombi */
1003 if (ps->num_proc == 0)
1004 {
1005 DEBUG ("processes plugin: This is only a zombi: pid = %i; "
1006 "name = %s;", pid, ps->name);
1007 return (0);
1008 }
1010 cpu_user_counter = atoll (fields[11]);
1011 cpu_system_counter = atoll (fields[12]);
1012 vmem_size = atoll (fields[20]);
1013 vmem_rss = atoll (fields[21]);
1014 ps->vmem_minflt_counter = atol (fields[7]);
1015 ps->vmem_majflt_counter = atol (fields[9]);
1017 {
1018 unsigned long long stack_start = atoll (fields[25]);
1019 unsigned long long stack_ptr = atoll (fields[26]);
1021 stack_size = (stack_start > stack_ptr)
1022 ? stack_start - stack_ptr
1023 : stack_ptr - stack_start;
1024 }
1026 /* Convert jiffies to useconds */
1027 cpu_user_counter = cpu_user_counter * 1000000 / CONFIG_HZ;
1028 cpu_system_counter = cpu_system_counter * 1000000 / CONFIG_HZ;
1029 vmem_rss = vmem_rss * pagesize_g;
1031 if ( (ps_read_vmem(pid, ps)) == NULL)
1032 {
1033 /* No VMem data */
1034 ps->vmem_data = -1;
1035 ps->vmem_code = -1;
1036 DEBUG("ps_read_process: did not get vmem data for pid %i",pid);
1037 }
1039 ps->cpu_user_counter = cpu_user_counter;
1040 ps->cpu_system_counter = cpu_system_counter;
1041 ps->vmem_size = (unsigned long) vmem_size;
1042 ps->vmem_rss = (unsigned long) vmem_rss;
1043 ps->stack_size = (unsigned long) stack_size;
1045 if ( (ps_read_io (pid, ps)) == NULL)
1046 {
1047 /* no io data */
1048 ps->io_rchar = -1;
1049 ps->io_wchar = -1;
1050 ps->io_syscr = -1;
1051 ps->io_syscw = -1;
1053 DEBUG("ps_read_process: not get io data for pid %i",pid);
1054 }
1056 /* success */
1057 return (0);
1058 } /* int ps_read_process (...) */
1060 static char *ps_get_cmdline (pid_t pid, char *name, char *buf, size_t buf_len)
1061 {
1062 char *buf_ptr;
1063 size_t len;
1065 char file[PATH_MAX];
1066 int fd;
1068 size_t n;
1070 if ((pid < 1) || (NULL == buf) || (buf_len < 2))
1071 return NULL;
1073 ssnprintf (file, sizeof (file), "/proc/%u/cmdline",
1074 (unsigned int) pid);
1076 errno = 0;
1077 fd = open (file, O_RDONLY);
1078 if (fd < 0) {
1079 char errbuf[4096];
1080 /* ENOENT means the process exited while we were handling it.
1081 * Don't complain about this, it only fills the logs. */
1082 if (errno != ENOENT)
1083 WARNING ("processes plugin: Failed to open `%s': %s.", file,
1084 sstrerror (errno, errbuf, sizeof (errbuf)));
1085 return NULL;
1086 }
1088 buf_ptr = buf;
1089 len = buf_len;
1091 n = 0;
1093 while (42) {
1094 ssize_t status;
1096 status = read (fd, (void *)buf_ptr, len);
1098 if (status < 0) {
1099 char errbuf[1024];
1101 if ((EAGAIN == errno) || (EINTR == errno))
1102 continue;
1104 WARNING ("processes plugin: Failed to read from `%s': %s.", file,
1105 sstrerror (errno, errbuf, sizeof (errbuf)));
1106 close (fd);
1107 return NULL;
1108 }
1110 n += status;
1112 if (status == 0)
1113 break;
1115 buf_ptr += status;
1116 len -= status;
1118 if (len <= 0)
1119 break;
1120 }
1122 close (fd);
1124 if (0 == n) {
1125 /* cmdline not available; e.g. kernel thread, zombie */
1126 if (NULL == name)
1127 return NULL;
1129 ssnprintf (buf, buf_len, "[%s]", name);
1130 return buf;
1131 }
1133 assert (n <= buf_len);
1135 if (n == buf_len)
1136 --n;
1137 buf[n] = '\0';
1139 --n;
1140 /* remove trailing whitespace */
1141 while ((n > 0) && (isspace (buf[n]) || ('\0' == buf[n]))) {
1142 buf[n] = '\0';
1143 --n;
1144 }
1146 /* arguments are separated by '\0' in /proc/<pid>/cmdline */
1147 while (n > 0) {
1148 if ('\0' == buf[n])
1149 buf[n] = ' ';
1150 --n;
1151 }
1152 return buf;
1153 } /* char *ps_get_cmdline (...) */
1155 static int read_fork_rate ()
1156 {
1157 FILE *proc_stat;
1158 char buffer[1024];
1159 value_t value;
1160 _Bool value_valid = 0;
1162 proc_stat = fopen ("/proc/stat", "r");
1163 if (proc_stat == NULL)
1164 {
1165 char errbuf[1024];
1166 ERROR ("processes plugin: fopen (/proc/stat) failed: %s",
1167 sstrerror (errno, errbuf, sizeof (errbuf)));
1168 return (-1);
1169 }
1171 while (fgets (buffer, sizeof (buffer), proc_stat) != NULL)
1172 {
1173 int status;
1174 char *fields[3];
1175 int fields_num;
1177 fields_num = strsplit (buffer, fields,
1178 STATIC_ARRAY_SIZE (fields));
1179 if (fields_num != 2)
1180 continue;
1182 if (strcmp ("processes", fields[0]) != 0)
1183 continue;
1185 status = parse_value (fields[1], &value, DS_TYPE_DERIVE);
1186 if (status == 0)
1187 value_valid = 1;
1189 break;
1190 }
1191 fclose(proc_stat);
1193 if (!value_valid)
1194 return (-1);
1196 ps_submit_fork_rate (value.derive);
1197 return (0);
1198 }
1199 #endif /*KERNEL_LINUX */
1201 #if KERNEL_SOLARIS
1202 static const char *ps_get_cmdline (pid_t pid, /* {{{ */
1203 char *buffer, size_t buffer_size)
1204 {
1205 char path[PATH_MAX];
1206 psinfo_t info;
1207 int status;
1209 snprintf(path, sizeof (path), "/proc/%i/psinfo", pid);
1211 status = read_file_contents (path, (void *) &info, sizeof (info));
1212 if (status != sizeof (info))
1213 {
1214 ERROR ("processes plugin: Unexpected return value "
1215 "while reading \"%s\": "
1216 "Returned %i but expected %zu.",
1217 path, status, buffer_size);
1218 return (NULL);
1219 }
1221 info.pr_psargs[sizeof (info.pr_psargs) - 1] = 0;
1222 sstrncpy (buffer, info.pr_psargs, buffer_size);
1224 return (buffer);
1225 } /* }}} int ps_get_cmdline */
1227 /*
1228 * Reads process information on the Solaris OS. The information comes mainly from
1229 * /proc/PID/status, /proc/PID/psinfo and /proc/PID/usage
1230 * The values for input and ouput chars are calculated "by hand"
1231 * Added a few "solaris" specific process states as well
1232 */
1233 static int ps_read_process(int pid, procstat_t *ps, char *state)
1234 {
1235 char filename[64];
1236 char f_psinfo[64], f_usage[64];
1237 char *buffer;
1239 pstatus_t *myStatus;
1240 psinfo_t *myInfo;
1241 prusage_t *myUsage;
1243 snprintf(filename, sizeof (filename), "/proc/%i/status", pid);
1244 snprintf(f_psinfo, sizeof (f_psinfo), "/proc/%i/psinfo", pid);
1245 snprintf(f_usage, sizeof (f_usage), "/proc/%i/usage", pid);
1248 buffer = malloc(sizeof (pstatus_t));
1249 memset(buffer, 0, sizeof (pstatus_t));
1250 read_file_contents(filename, buffer, sizeof (pstatus_t));
1251 myStatus = (pstatus_t *) buffer;
1253 buffer = malloc(sizeof (psinfo_t));
1254 memset(buffer, 0, sizeof(psinfo_t));
1255 read_file_contents(f_psinfo, buffer, sizeof (psinfo_t));
1256 myInfo = (psinfo_t *) buffer;
1258 buffer = malloc(sizeof (prusage_t));
1259 memset(buffer, 0, sizeof(prusage_t));
1260 read_file_contents(f_usage, buffer, sizeof (prusage_t));
1261 myUsage = (prusage_t *) buffer;
1263 sstrncpy(ps->name, myInfo->pr_fname, sizeof (myInfo->pr_fname));
1264 ps->num_lwp = myStatus->pr_nlwp;
1265 if (myInfo->pr_wstat != 0) {
1266 ps->num_proc = 0;
1267 ps->num_lwp = 0;
1268 *state = (char) 'Z';
1269 return (0);
1270 } else {
1271 ps->num_proc = 1;
1272 ps->num_lwp = myInfo->pr_nlwp;
1273 }
1275 /*
1276 * Convert system time and user time from nanoseconds to microseconds
1277 * for compatibility with the linux module
1278 */
1279 ps->cpu_system_counter = myStatus -> pr_stime.tv_nsec / 1000;
1280 ps->cpu_user_counter = myStatus -> pr_utime.tv_nsec / 1000;
1282 /*
1283 * Convert rssize from KB to bytes to be consistent w/ the linux module
1284 */
1285 ps->vmem_rss = myInfo->pr_rssize * 1024;
1286 ps->vmem_size = myInfo->pr_size * 1024;
1287 ps->vmem_minflt_counter = myUsage->pr_minf;
1288 ps->vmem_majflt_counter = myUsage->pr_majf;
1290 /*
1291 * TODO: Data and code segment calculations for Solaris
1292 */
1294 ps->vmem_data = -1;
1295 ps->vmem_code = -1;
1296 ps->stack_size = myStatus->pr_stksize;
1298 /*
1299 * Calculating input/ouput chars
1300 * Formula used is total chars / total blocks => chars/block
1301 * then convert input/output blocks to chars
1302 */
1303 ulong_t tot_chars = myUsage->pr_ioch;
1304 ulong_t tot_blocks = myUsage->pr_inblk + myUsage->pr_oublk;
1305 ulong_t chars_per_block = 1;
1306 if (tot_blocks != 0)
1307 chars_per_block = tot_chars / tot_blocks;
1308 ps->io_rchar = myUsage->pr_inblk * chars_per_block;
1309 ps->io_wchar = myUsage->pr_oublk * chars_per_block;
1310 ps->io_syscr = myUsage->pr_sysc;
1311 ps->io_syscw = myUsage->pr_sysc;
1314 /*
1315 * TODO: Find way of setting BLOCKED and PAGING status
1316 */
1318 *state = (char) 'R';
1319 if (myStatus->pr_flags & PR_ASLEEP)
1320 *state = (char) 'S';
1321 else if (myStatus->pr_flags & PR_STOPPED)
1322 *state = (char) 'T';
1323 else if (myStatus->pr_flags & PR_DETACH)
1324 *state = (char) 'E';
1325 else if (myStatus->pr_flags & PR_DAEMON)
1326 *state = (char) 'A';
1327 else if (myStatus->pr_flags & PR_ISSYS)
1328 *state = (char) 'Y';
1329 else if (myStatus->pr_flags & PR_ORPHAN)
1330 *state = (char) 'O';
1332 sfree(myStatus);
1333 sfree(myInfo);
1334 sfree(myUsage);
1336 return (0);
1337 }
1339 /*
1340 * Reads the number of threads created since the last reboot. On Solaris these
1341 * are retrieved from kstat (module cpu, name sys, class misc, stat nthreads).
1342 * The result is the sum for all the threads created on each cpu
1343 */
1344 static int read_fork_rate()
1345 {
1346 extern kstat_ctl_t *kc;
1347 kstat_t *ksp_chain = NULL;
1348 derive_t result = 0;
1350 if (kc == NULL)
1351 return (-1);
1353 for (ksp_chain = kc->kc_chain;
1354 ksp_chain != NULL;
1355 ksp_chain = ksp_chain->ks_next)
1356 {
1357 if ((strcmp (ksp_chain->ks_module, "cpu") == 0)
1358 && (strcmp (ksp_chain->ks_name, "sys") == 0)
1359 && (strcmp (ksp_chain->ks_class, "misc") == 0))
1360 {
1361 long long tmp;
1363 kstat_read (kc, ksp_chain, NULL);
1365 tmp = get_kstat_value(ksp_chain, "nthreads");
1366 if (tmp != -1LL)
1367 result += tmp;
1368 }
1369 }
1371 ps_submit_fork_rate (result);
1372 return (0);
1373 }
1374 #endif /* KERNEL_SOLARIS */
1376 #if HAVE_THREAD_INFO
1377 static int mach_get_task_name (task_t t, int *pid, char *name, size_t name_max_len)
1378 {
1379 int mib[4];
1381 struct kinfo_proc kp;
1382 size_t kp_size;
1384 mib[0] = CTL_KERN;
1385 mib[1] = KERN_PROC;
1386 mib[2] = KERN_PROC_PID;
1388 if (pid_for_task (t, pid) != KERN_SUCCESS)
1389 return (-1);
1390 mib[3] = *pid;
1392 kp_size = sizeof (kp);
1393 if (sysctl (mib, 4, &kp, &kp_size, NULL, 0) != 0)
1394 return (-1);
1396 if (name_max_len > (MAXCOMLEN + 1))
1397 name_max_len = MAXCOMLEN + 1;
1399 strncpy (name, kp.kp_proc.p_comm, name_max_len - 1);
1400 name[name_max_len - 1] = '\0';
1402 DEBUG ("pid = %i; name = %s;", *pid, name);
1404 /* We don't do the special handling for `p_comm == "LaunchCFMApp"' as
1405 * `top' does it, because it is a lot of work and only used when
1406 * debugging. -octo */
1408 return (0);
1409 }
1410 #endif /* HAVE_THREAD_INFO */
1411 /* ------- end of additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
1413 /* do actual readings from kernel */
1414 static int ps_read (void)
1415 {
1416 #if HAVE_THREAD_INFO
1417 kern_return_t status;
1419 int pset;
1420 processor_set_t port_pset_priv;
1422 int task;
1423 task_array_t task_list;
1424 mach_msg_type_number_t task_list_len;
1426 int task_pid;
1427 char task_name[MAXCOMLEN + 1];
1429 int thread;
1430 thread_act_array_t thread_list;
1431 mach_msg_type_number_t thread_list_len;
1432 thread_basic_info_data_t thread_data;
1433 mach_msg_type_number_t thread_data_len;
1435 int running = 0;
1436 int sleeping = 0;
1437 int zombies = 0;
1438 int stopped = 0;
1439 int blocked = 0;
1441 procstat_t *ps;
1442 procstat_entry_t pse;
1444 ps_list_reset ();
1446 /*
1447 * The Mach-concept is a little different from the traditional UNIX
1448 * concept: All the work is done in threads. Threads are contained in
1449 * `tasks'. Therefore, `task status' doesn't make much sense, since
1450 * it's actually a `thread status'.
1451 * Tasks are assigned to sets of processors, so that's where you go to
1452 * get a list.
1453 */
1454 for (pset = 0; pset < pset_list_len; pset++)
1455 {
1456 if ((status = host_processor_set_priv (port_host_self,
1457 pset_list[pset],
1458 &port_pset_priv)) != KERN_SUCCESS)
1459 {
1460 ERROR ("host_processor_set_priv failed: %s\n",
1461 mach_error_string (status));
1462 continue;
1463 }
1465 if ((status = processor_set_tasks (port_pset_priv,
1466 &task_list,
1467 &task_list_len)) != KERN_SUCCESS)
1468 {
1469 ERROR ("processor_set_tasks failed: %s\n",
1470 mach_error_string (status));
1471 mach_port_deallocate (port_task_self, port_pset_priv);
1472 continue;
1473 }
1475 for (task = 0; task < task_list_len; task++)
1476 {
1477 ps = NULL;
1478 if (mach_get_task_name (task_list[task],
1479 &task_pid,
1480 task_name, PROCSTAT_NAME_LEN) == 0)
1481 {
1482 /* search for at least one match */
1483 for (ps = list_head_g; ps != NULL; ps = ps->next)
1484 /* FIXME: cmdline should be here instead of NULL */
1485 if (ps_list_match (task_name, NULL, ps) == 1)
1486 break;
1487 }
1489 /* Collect more detailed statistics for this process */
1490 if (ps != NULL)
1491 {
1492 task_basic_info_data_t task_basic_info;
1493 mach_msg_type_number_t task_basic_info_len;
1494 task_events_info_data_t task_events_info;
1495 mach_msg_type_number_t task_events_info_len;
1496 task_absolutetime_info_data_t task_absolutetime_info;
1497 mach_msg_type_number_t task_absolutetime_info_len;
1499 memset (&pse, '\0', sizeof (pse));
1500 pse.id = task_pid;
1502 task_basic_info_len = TASK_BASIC_INFO_COUNT;
1503 status = task_info (task_list[task],
1504 TASK_BASIC_INFO,
1505 (task_info_t) &task_basic_info,
1506 &task_basic_info_len);
1507 if (status != KERN_SUCCESS)
1508 {
1509 ERROR ("task_info failed: %s",
1510 mach_error_string (status));
1511 continue; /* with next thread_list */
1512 }
1514 task_events_info_len = TASK_EVENTS_INFO_COUNT;
1515 status = task_info (task_list[task],
1516 TASK_EVENTS_INFO,
1517 (task_info_t) &task_events_info,
1518 &task_events_info_len);
1519 if (status != KERN_SUCCESS)
1520 {
1521 ERROR ("task_info failed: %s",
1522 mach_error_string (status));
1523 continue; /* with next thread_list */
1524 }
1526 task_absolutetime_info_len = TASK_ABSOLUTETIME_INFO_COUNT;
1527 status = task_info (task_list[task],
1528 TASK_ABSOLUTETIME_INFO,
1529 (task_info_t) &task_absolutetime_info,
1530 &task_absolutetime_info_len);
1531 if (status != KERN_SUCCESS)
1532 {
1533 ERROR ("task_info failed: %s",
1534 mach_error_string (status));
1535 continue; /* with next thread_list */
1536 }
1538 pse.num_proc++;
1539 pse.vmem_size = task_basic_info.virtual_size;
1540 pse.vmem_rss = task_basic_info.resident_size;
1541 /* Does not seem to be easily exposed */
1542 pse.vmem_data = 0;
1543 pse.vmem_code = 0;
1545 pse.vmem_minflt_counter = task_events_info.cow_faults;
1546 pse.vmem_majflt_counter = task_events_info.faults;
1548 pse.cpu_user_counter = task_absolutetime_info.total_user;
1549 pse.cpu_system_counter = task_absolutetime_info.total_system;
1550 }
1552 status = task_threads (task_list[task], &thread_list,
1553 &thread_list_len);
1554 if (status != KERN_SUCCESS)
1555 {
1556 /* Apple's `top' treats this case a zombie. It
1557 * makes sense to some extend: A `zombie'
1558 * thread is nonsense, since the task/process
1559 * is dead. */
1560 zombies++;
1561 DEBUG ("task_threads failed: %s",
1562 mach_error_string (status));
1563 if (task_list[task] != port_task_self)
1564 mach_port_deallocate (port_task_self,
1565 task_list[task]);
1566 continue; /* with next task_list */
1567 }
1569 for (thread = 0; thread < thread_list_len; thread++)
1570 {
1571 thread_data_len = THREAD_BASIC_INFO_COUNT;
1572 status = thread_info (thread_list[thread],
1573 THREAD_BASIC_INFO,
1574 (thread_info_t) &thread_data,
1575 &thread_data_len);
1576 if (status != KERN_SUCCESS)
1577 {
1578 ERROR ("thread_info failed: %s",
1579 mach_error_string (status));
1580 if (task_list[task] != port_task_self)
1581 mach_port_deallocate (port_task_self,
1582 thread_list[thread]);
1583 continue; /* with next thread_list */
1584 }
1586 if (ps != NULL)
1587 pse.num_lwp++;
1589 switch (thread_data.run_state)
1590 {
1591 case TH_STATE_RUNNING:
1592 running++;
1593 break;
1594 case TH_STATE_STOPPED:
1595 /* What exactly is `halted'? */
1596 case TH_STATE_HALTED:
1597 stopped++;
1598 break;
1599 case TH_STATE_WAITING:
1600 sleeping++;
1601 break;
1602 case TH_STATE_UNINTERRUPTIBLE:
1603 blocked++;
1604 break;
1605 /* There is no `zombie' case here,
1606 * since there are no zombie-threads.
1607 * There's only zombie tasks, which are
1608 * handled above. */
1609 default:
1610 WARNING ("Unknown thread status: %i",
1611 thread_data.run_state);
1612 break;
1613 } /* switch (thread_data.run_state) */
1615 if (task_list[task] != port_task_self)
1616 {
1617 status = mach_port_deallocate (port_task_self,
1618 thread_list[thread]);
1619 if (status != KERN_SUCCESS)
1620 ERROR ("mach_port_deallocate failed: %s",
1621 mach_error_string (status));
1622 }
1623 } /* for (thread_list) */
1625 if ((status = vm_deallocate (port_task_self,
1626 (vm_address_t) thread_list,
1627 thread_list_len * sizeof (thread_act_t)))
1628 != KERN_SUCCESS)
1629 {
1630 ERROR ("vm_deallocate failed: %s",
1631 mach_error_string (status));
1632 }
1633 thread_list = NULL;
1634 thread_list_len = 0;
1636 /* Only deallocate the task port, if it isn't our own.
1637 * Don't know what would happen in that case, but this
1638 * is what Apple's top does.. ;) */
1639 if (task_list[task] != port_task_self)
1640 {
1641 status = mach_port_deallocate (port_task_self,
1642 task_list[task]);
1643 if (status != KERN_SUCCESS)
1644 ERROR ("mach_port_deallocate failed: %s",
1645 mach_error_string (status));
1646 }
1648 if (ps != NULL)
1649 /* FIXME: cmdline should be here instead of NULL */
1650 ps_list_add (task_name, NULL, &pse);
1651 } /* for (task_list) */
1653 if ((status = vm_deallocate (port_task_self,
1654 (vm_address_t) task_list,
1655 task_list_len * sizeof (task_t))) != KERN_SUCCESS)
1656 {
1657 ERROR ("vm_deallocate failed: %s",
1658 mach_error_string (status));
1659 }
1660 task_list = NULL;
1661 task_list_len = 0;
1663 if ((status = mach_port_deallocate (port_task_self, port_pset_priv))
1664 != KERN_SUCCESS)
1665 {
1666 ERROR ("mach_port_deallocate failed: %s",
1667 mach_error_string (status));
1668 }
1669 } /* for (pset_list) */
1671 ps_submit_state ("running", running);
1672 ps_submit_state ("sleeping", sleeping);
1673 ps_submit_state ("zombies", zombies);
1674 ps_submit_state ("stopped", stopped);
1675 ps_submit_state ("blocked", blocked);
1677 for (ps = list_head_g; ps != NULL; ps = ps->next)
1678 ps_submit_proc_list (ps);
1679 /* #endif HAVE_THREAD_INFO */
1681 #elif KERNEL_LINUX
1682 int running = 0;
1683 int sleeping = 0;
1684 int zombies = 0;
1685 int stopped = 0;
1686 int paging = 0;
1687 int blocked = 0;
1689 struct dirent *ent;
1690 DIR *proc;
1691 int pid;
1693 char cmdline[CMDLINE_BUFFER_SIZE];
1695 int status;
1696 procstat_t ps;
1697 procstat_entry_t pse;
1698 char state;
1700 procstat_t *ps_ptr;
1702 running = sleeping = zombies = stopped = paging = blocked = 0;
1703 ps_list_reset ();
1705 if ((proc = opendir ("/proc")) == NULL)
1706 {
1707 char errbuf[1024];
1708 ERROR ("Cannot open `/proc': %s",
1709 sstrerror (errno, errbuf, sizeof (errbuf)));
1710 return (-1);
1711 }
1713 while ((ent = readdir (proc)) != NULL)
1714 {
1715 if (!isdigit (ent->d_name[0]))
1716 continue;
1718 if ((pid = atoi (ent->d_name)) < 1)
1719 continue;
1721 status = ps_read_process (pid, &ps, &state);
1722 if (status != 0)
1723 {
1724 DEBUG ("ps_read_process failed: %i", status);
1725 continue;
1726 }
1728 pse.id = pid;
1729 pse.age = 0;
1731 pse.num_proc = ps.num_proc;
1732 pse.num_lwp = ps.num_lwp;
1733 pse.vmem_size = ps.vmem_size;
1734 pse.vmem_rss = ps.vmem_rss;
1735 pse.vmem_data = ps.vmem_data;
1736 pse.vmem_code = ps.vmem_code;
1737 pse.stack_size = ps.stack_size;
1739 pse.vmem_minflt = 0;
1740 pse.vmem_minflt_counter = ps.vmem_minflt_counter;
1741 pse.vmem_majflt = 0;
1742 pse.vmem_majflt_counter = ps.vmem_majflt_counter;
1744 pse.cpu_user = 0;
1745 pse.cpu_user_counter = ps.cpu_user_counter;
1746 pse.cpu_system = 0;
1747 pse.cpu_system_counter = ps.cpu_system_counter;
1749 pse.io_rchar = ps.io_rchar;
1750 pse.io_wchar = ps.io_wchar;
1751 pse.io_syscr = ps.io_syscr;
1752 pse.io_syscw = ps.io_syscw;
1754 switch (state)
1755 {
1756 case 'R': running++; break;
1757 case 'S': sleeping++; break;
1758 case 'D': blocked++; break;
1759 case 'Z': zombies++; break;
1760 case 'T': stopped++; break;
1761 case 'W': paging++; break;
1762 }
1764 ps_list_add (ps.name,
1765 ps_get_cmdline (pid, ps.name, cmdline, sizeof (cmdline)),
1766 &pse);
1767 }
1769 closedir (proc);
1771 ps_submit_state ("running", running);
1772 ps_submit_state ("sleeping", sleeping);
1773 ps_submit_state ("zombies", zombies);
1774 ps_submit_state ("stopped", stopped);
1775 ps_submit_state ("paging", paging);
1776 ps_submit_state ("blocked", blocked);
1778 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1779 ps_submit_proc_list (ps_ptr);
1781 read_fork_rate();
1782 /* #endif KERNEL_LINUX */
1784 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD
1785 int running = 0;
1786 int sleeping = 0;
1787 int zombies = 0;
1788 int stopped = 0;
1789 int blocked = 0;
1790 int idle = 0;
1791 int wait = 0;
1793 kvm_t *kd;
1794 char errbuf[1024];
1795 struct kinfo_proc *procs; /* array of processes */
1796 struct kinfo_proc *proc_ptr = NULL;
1797 int count; /* returns number of processes */
1798 int i;
1800 procstat_t *ps_ptr;
1801 procstat_entry_t pse;
1803 ps_list_reset ();
1805 /* Open the kvm interface, get a descriptor */
1806 kd = kvm_open (NULL, NULL, NULL, 0, errbuf);
1807 if (kd == NULL)
1808 {
1809 ERROR ("processes plugin: Cannot open kvm interface: %s",
1810 errbuf);
1811 return (0);
1812 }
1814 /* Get the list of processes. */
1815 procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, &count);
1816 if (procs == NULL)
1817 {
1818 ERROR ("processes plugin: Cannot get kvm processes list: %s",
1819 kvm_geterr(kd));
1820 kvm_close (kd);
1821 return (0);
1822 }
1824 /* Iterate through the processes in kinfo_proc */
1825 for (i = 0; i < count; i++)
1826 {
1827 /* Create only one process list entry per _process_, i.e.
1828 * filter out threads (duplicate PID entries). */
1829 if ((proc_ptr == NULL) || (proc_ptr->ki_pid != procs[i].ki_pid))
1830 {
1831 char cmdline[CMDLINE_BUFFER_SIZE] = "";
1832 _Bool have_cmdline = 0;
1834 proc_ptr = &(procs[i]);
1835 /* Don't probe system processes and processes without arguments */
1836 if (((procs[i].ki_flag & P_SYSTEM) == 0)
1837 && (procs[i].ki_args != NULL))
1838 {
1839 char **argv;
1840 int argc;
1841 int status;
1843 /* retrieve the arguments */
1844 argv = kvm_getargv (kd, proc_ptr, /* nchr = */ 0);
1845 argc = 0;
1846 if ((argv != NULL) && (argv[0] != NULL))
1847 {
1848 while (argv[argc] != NULL)
1849 argc++;
1851 status = strjoin (cmdline, sizeof (cmdline), argv, argc, " ");
1852 if (status < 0)
1853 WARNING ("processes plugin: Command line did not fit into buffer.");
1854 else
1855 have_cmdline = 1;
1856 }
1857 } /* if (process has argument list) */
1859 pse.id = procs[i].ki_pid;
1860 pse.age = 0;
1862 pse.num_proc = 1;
1863 pse.num_lwp = procs[i].ki_numthreads;
1865 pse.vmem_size = procs[i].ki_size;
1866 pse.vmem_rss = procs[i].ki_rssize * pagesize;
1867 pse.vmem_data = procs[i].ki_dsize * pagesize;
1868 pse.vmem_code = procs[i].ki_tsize * pagesize;
1869 pse.stack_size = procs[i].ki_ssize * pagesize;
1870 pse.vmem_minflt = 0;
1871 pse.vmem_minflt_counter = procs[i].ki_rusage.ru_minflt;
1872 pse.vmem_majflt = 0;
1873 pse.vmem_majflt_counter = procs[i].ki_rusage.ru_majflt;
1875 pse.cpu_user = 0;
1876 pse.cpu_system = 0;
1877 pse.cpu_user_counter = 0;
1878 pse.cpu_system_counter = 0;
1879 /*
1880 * The u-area might be swapped out, and we can't get
1881 * at it because we have a crashdump and no swap.
1882 * If it's here fill in these fields, otherwise, just
1883 * leave them 0.
1884 */
1885 if (procs[i].ki_flag & P_INMEM)
1886 {
1887 pse.cpu_user_counter = procs[i].ki_rusage.ru_utime.tv_usec
1888 + (1000000lu * procs[i].ki_rusage.ru_utime.tv_sec);
1889 pse.cpu_system_counter = procs[i].ki_rusage.ru_stime.tv_usec
1890 + (1000000lu * procs[i].ki_rusage.ru_stime.tv_sec);
1891 }
1893 /* no I/O data */
1894 pse.io_rchar = -1;
1895 pse.io_wchar = -1;
1896 pse.io_syscr = -1;
1897 pse.io_syscw = -1;
1899 ps_list_add (procs[i].ki_comm, have_cmdline ? cmdline : NULL, &pse);
1900 } /* if ((proc_ptr == NULL) || (proc_ptr->ki_pid != procs[i].ki_pid)) */
1902 switch (procs[i].ki_stat)
1903 {
1904 case SSTOP: stopped++; break;
1905 case SSLEEP: sleeping++; break;
1906 case SRUN: running++; break;
1907 case SIDL: idle++; break;
1908 case SWAIT: wait++; break;
1909 case SLOCK: blocked++; break;
1910 case SZOMB: zombies++; break;
1911 }
1912 }
1914 kvm_close(kd);
1916 ps_submit_state ("running", running);
1917 ps_submit_state ("sleeping", sleeping);
1918 ps_submit_state ("zombies", zombies);
1919 ps_submit_state ("stopped", stopped);
1920 ps_submit_state ("blocked", blocked);
1921 ps_submit_state ("idle", idle);
1922 ps_submit_state ("wait", wait);
1924 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1925 ps_submit_proc_list (ps_ptr);
1926 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
1928 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_OPENBSD
1929 int running = 0;
1930 int sleeping = 0;
1931 int zombies = 0;
1932 int stopped = 0;
1933 int onproc = 0;
1934 int idle = 0;
1935 int dead = 0;
1937 kvm_t *kd;
1938 char errbuf[1024];
1939 struct kinfo_proc *procs; /* array of processes */
1940 struct kinfo_proc *proc_ptr = NULL;
1941 int count; /* returns number of processes */
1942 int i;
1944 procstat_t *ps_ptr;
1945 procstat_entry_t pse;
1947 ps_list_reset ();
1949 /* Open the kvm interface, get a descriptor */
1950 kd = kvm_open (NULL, NULL, NULL, 0, errbuf);
1951 if (kd == NULL)
1952 {
1953 ERROR ("processes plugin: Cannot open kvm interface: %s",
1954 errbuf);
1955 return (0);
1956 }
1958 /* Get the list of processes. */
1959 procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc), &count);
1960 if (procs == NULL)
1961 {
1962 ERROR ("processes plugin: Cannot get kvm processes list: %s",
1963 kvm_geterr(kd));
1964 kvm_close (kd);
1965 return (0);
1966 }
1968 /* Iterate through the processes in kinfo_proc */
1969 for (i = 0; i < count; i++)
1970 {
1971 /* Create only one process list entry per _process_, i.e.
1972 * filter out threads (duplicate PID entries). */
1973 if ((proc_ptr == NULL) || (proc_ptr->p_pid != procs[i].p_pid))
1974 {
1975 char cmdline[CMDLINE_BUFFER_SIZE] = "";
1976 _Bool have_cmdline = 0;
1978 proc_ptr = &(procs[i]);
1979 /* Don't probe zombie processes */
1980 if (!P_ZOMBIE(proc_ptr))
1981 {
1982 char **argv;
1983 int argc;
1984 int status;
1986 /* retrieve the arguments */
1987 argv = kvm_getargv (kd, proc_ptr, /* nchr = */ 0);
1988 argc = 0;
1989 if ((argv != NULL) && (argv[0] != NULL))
1990 {
1991 while (argv[argc] != NULL)
1992 argc++;
1994 status = strjoin (cmdline, sizeof (cmdline), argv, argc, " ");
1995 if (status < 0)
1996 WARNING ("processes plugin: Command line did not fit into buffer.");
1997 else
1998 have_cmdline = 1;
1999 }
2000 } /* if (process has argument list) */
2002 pse.id = procs[i].p_pid;
2003 pse.age = 0;
2005 pse.num_proc = 1;
2006 pse.num_lwp = 1; /* XXX: accumulate p_tid values for a single p_pid ? */
2008 pse.vmem_rss = procs[i].p_vm_rssize * pagesize;
2009 pse.vmem_data = procs[i].p_vm_dsize * pagesize;
2010 pse.vmem_code = procs[i].p_vm_tsize * pagesize;
2011 pse.stack_size = procs[i].p_vm_ssize * pagesize;
2012 pse.vmem_size = pse.stack_size + pse.vmem_code + pse.vmem_data;
2013 pse.vmem_minflt = 0;
2014 pse.vmem_minflt_counter = procs[i].p_uru_minflt;
2015 pse.vmem_majflt = 0;
2016 pse.vmem_majflt_counter = procs[i].p_uru_majflt;
2018 pse.cpu_user = 0;
2019 pse.cpu_system = 0;
2020 pse.cpu_user_counter = procs[i].p_uutime_usec +
2021 (1000000lu * procs[i].p_uutime_sec);
2022 pse.cpu_system_counter = procs[i].p_ustime_usec +
2023 (1000000lu * procs[i].p_ustime_sec);
2025 /* no I/O data */
2026 pse.io_rchar = -1;
2027 pse.io_wchar = -1;
2028 pse.io_syscr = -1;
2029 pse.io_syscw = -1;
2031 ps_list_add (procs[i].p_comm, have_cmdline ? cmdline : NULL, &pse);
2032 } /* if ((proc_ptr == NULL) || (proc_ptr->p_pid != procs[i].p_pid)) */
2034 switch (procs[i].p_stat)
2035 {
2036 case SSTOP: stopped++; break;
2037 case SSLEEP: sleeping++; break;
2038 case SRUN: running++; break;
2039 case SIDL: idle++; break;
2040 case SONPROC: onproc++; break;
2041 case SDEAD: dead++; break;
2042 case SZOMB: zombies++; break;
2043 }
2044 }
2046 kvm_close(kd);
2048 ps_submit_state ("running", running);
2049 ps_submit_state ("sleeping", sleeping);
2050 ps_submit_state ("zombies", zombies);
2051 ps_submit_state ("stopped", stopped);
2052 ps_submit_state ("onproc", onproc);
2053 ps_submit_state ("idle", idle);
2054 ps_submit_state ("dead", dead);
2056 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
2057 ps_submit_proc_list (ps_ptr);
2058 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_OPENBSD */
2060 #elif HAVE_PROCINFO_H
2061 /* AIX */
2062 int running = 0;
2063 int sleeping = 0;
2064 int zombies = 0;
2065 int stopped = 0;
2066 int paging = 0;
2067 int blocked = 0;
2069 pid_t pindex = 0;
2070 int nprocs;
2072 procstat_t *ps;
2073 procstat_entry_t pse;
2075 ps_list_reset ();
2076 while ((nprocs = getprocs64 (procentry, sizeof(struct procentry64),
2077 /* fdsinfo = */ NULL, sizeof(struct fdsinfo64),
2078 &pindex, MAXPROCENTRY)) > 0)
2079 {
2080 int i;
2082 for (i = 0; i < nprocs; i++)
2083 {
2084 tid64_t thindex;
2085 int nthreads;
2086 char arglist[MAXARGLN+1];
2087 char *cargs;
2088 char *cmdline;
2090 if (procentry[i].pi_state == SNONE) continue;
2091 /* if (procentry[i].pi_state == SZOMB) FIXME */
2093 cmdline = procentry[i].pi_comm;
2094 cargs = procentry[i].pi_comm;
2095 if ( procentry[i].pi_flags & SKPROC )
2096 {
2097 if (procentry[i].pi_pid == 0)
2098 cmdline = "swapper";
2099 cargs = cmdline;
2100 }
2101 else
2102 {
2103 if (getargs(&procentry[i], sizeof(struct procentry64), arglist, MAXARGLN) >= 0)
2104 {
2105 int n;
2107 n = -1;
2108 while (++n < MAXARGLN)
2109 {
2110 if (arglist[n] == '\0')
2111 {
2112 if (arglist[n+1] == '\0')
2113 break;
2114 arglist[n] = ' ';
2115 }
2116 }
2117 cargs = arglist;
2118 }
2119 }
2121 pse.id = procentry[i].pi_pid;
2122 pse.age = 0;
2123 pse.num_lwp = procentry[i].pi_thcount;
2124 pse.num_proc = 1;
2126 thindex=0;
2127 while ((nthreads = getthrds64(procentry[i].pi_pid,
2128 thrdentry, sizeof(struct thrdentry64),
2129 &thindex, MAXTHRDENTRY)) > 0)
2130 {
2131 int j;
2133 for (j=0; j< nthreads; j++)
2134 {
2135 switch (thrdentry[j].ti_state)
2136 {
2137 /* case TSNONE: break; */
2138 case TSIDL: blocked++; break; /* FIXME is really blocked */
2139 case TSRUN: running++; break;
2140 case TSSLEEP: sleeping++; break;
2141 case TSSWAP: paging++; break;
2142 case TSSTOP: stopped++; break;
2143 case TSZOMB: zombies++; break;
2144 }
2145 }
2146 if (nthreads < MAXTHRDENTRY)
2147 break;
2148 }
2150 pse.cpu_user = 0;
2151 /* tv_usec is nanosec ??? */
2152 pse.cpu_user_counter = procentry[i].pi_ru.ru_utime.tv_sec * 1000000 +
2153 procentry[i].pi_ru.ru_utime.tv_usec / 1000;
2155 pse.cpu_system = 0;
2156 /* tv_usec is nanosec ??? */
2157 pse.cpu_system_counter = procentry[i].pi_ru.ru_stime.tv_sec * 1000000 +
2158 procentry[i].pi_ru.ru_stime.tv_usec / 1000;
2160 pse.vmem_minflt = 0;
2161 pse.vmem_minflt_counter = procentry[i].pi_minflt;
2162 pse.vmem_majflt = 0;
2163 pse.vmem_majflt_counter = procentry[i].pi_majflt;
2165 pse.vmem_size = procentry[i].pi_tsize + procentry[i].pi_dvm * pagesize;
2166 pse.vmem_rss = (procentry[i].pi_drss + procentry[i].pi_trss) * pagesize;
2167 /* Not supported */
2168 pse.vmem_data = 0;
2169 pse.vmem_code = 0;
2170 pse.stack_size = 0;
2172 pse.io_rchar = -1;
2173 pse.io_wchar = -1;
2174 pse.io_syscr = -1;
2175 pse.io_syscw = -1;
2177 ps_list_add (cmdline, cargs, &pse);
2178 } /* for (i = 0 .. nprocs) */
2180 if (nprocs < MAXPROCENTRY)
2181 break;
2182 } /* while (getprocs64() > 0) */
2183 ps_submit_state ("running", running);
2184 ps_submit_state ("sleeping", sleeping);
2185 ps_submit_state ("zombies", zombies);
2186 ps_submit_state ("stopped", stopped);
2187 ps_submit_state ("paging", paging);
2188 ps_submit_state ("blocked", blocked);
2190 for (ps = list_head_g; ps != NULL; ps = ps->next)
2191 ps_submit_proc_list (ps);
2192 /* #endif HAVE_PROCINFO_H */
2194 #elif KERNEL_SOLARIS
2195 /*
2196 * The Solaris section adds a few more process states and removes some
2197 * process states compared to linux. Most notably there is no "PAGING"
2198 * and "BLOCKED" state for a process. The rest is similar to the linux
2199 * code.
2200 */
2201 int running = 0;
2202 int sleeping = 0;
2203 int zombies = 0;
2204 int stopped = 0;
2205 int detached = 0;
2206 int daemon = 0;
2207 int system = 0;
2208 int orphan = 0;
2210 struct dirent *ent;
2211 DIR *proc;
2213 int status;
2214 procstat_t *ps_ptr;
2215 char state;
2217 char cmdline[PRARGSZ];
2219 ps_list_reset ();
2221 proc = opendir ("/proc");
2222 if (proc == NULL)
2223 return (-1);
2225 while ((ent = readdir(proc)) != NULL)
2226 {
2227 int pid;
2228 struct procstat ps;
2229 procstat_entry_t pse;
2231 if (!isdigit ((int) ent->d_name[0]))
2232 continue;
2234 if ((pid = atoi (ent->d_name)) < 1)
2235 continue;
2237 status = ps_read_process (pid, &ps, &state);
2238 if (status != 0)
2239 {
2240 DEBUG("ps_read_process failed: %i", status);
2241 continue;
2242 }
2244 pse.id = pid;
2245 pse.age = 0;
2247 pse.num_proc = ps.num_proc;
2248 pse.num_lwp = ps.num_lwp;
2249 pse.vmem_size = ps.vmem_size;
2250 pse.vmem_rss = ps.vmem_rss;
2251 pse.vmem_data = ps.vmem_data;
2252 pse.vmem_code = ps.vmem_code;
2253 pse.stack_size = ps.stack_size;
2255 pse.vmem_minflt = 0;
2256 pse.vmem_minflt_counter = ps.vmem_minflt_counter;
2257 pse.vmem_majflt = 0;
2258 pse.vmem_majflt_counter = ps.vmem_majflt_counter;
2260 pse.cpu_user = 0;
2261 pse.cpu_user_counter = ps.cpu_user_counter;
2262 pse.cpu_system = 0;
2263 pse.cpu_system_counter = ps.cpu_system_counter;
2265 pse.io_rchar = ps.io_rchar;
2266 pse.io_wchar = ps.io_wchar;
2267 pse.io_syscr = ps.io_syscr;
2268 pse.io_syscw = ps.io_syscw;
2270 switch (state)
2271 {
2272 case 'R': running++; break;
2273 case 'S': sleeping++; break;
2274 case 'E': detached++; break;
2275 case 'Z': zombies++; break;
2276 case 'T': stopped++; break;
2277 case 'A': daemon++; break;
2278 case 'Y': system++; break;
2279 case 'O': orphan++; break;
2280 }
2283 ps_list_add (ps.name,
2284 ps_get_cmdline ((pid_t) pid,
2285 cmdline, sizeof (cmdline)),
2286 &pse);
2287 } /* while(readdir) */
2288 closedir (proc);
2290 ps_submit_state ("running", running);
2291 ps_submit_state ("sleeping", sleeping);
2292 ps_submit_state ("zombies", zombies);
2293 ps_submit_state ("stopped", stopped);
2294 ps_submit_state ("detached", detached);
2295 ps_submit_state ("daemon", daemon);
2296 ps_submit_state ("system", system);
2297 ps_submit_state ("orphan", orphan);
2299 for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
2300 ps_submit_proc_list (ps_ptr);
2302 read_fork_rate();
2303 #endif /* KERNEL_SOLARIS */
2305 return (0);
2306 } /* int ps_read */
2308 void module_register (void)
2309 {
2310 plugin_register_complex_config ("processes", ps_config);
2311 plugin_register_init ("processes", ps_init);
2312 plugin_register_read ("processes", ps_read);
2313 } /* void module_register */