321371cfe39cbed8b98399127bdd152f02949f45
1 /**
2 * collectd - src/processes.c
3 * Copyright (C) 2005 Lyonel Vincent
4 * Copyright (C) 2006 Florian Forster (Mach code)
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 * Authors:
21 * Lyonel Vincent <lyonel at ezix.org>
22 * Florian octo Forster <octo at verplant.org>
23 **/
25 #include "collectd.h"
26 #include "common.h"
27 #include "plugin.h"
28 #include "utils_debug.h"
29 #include "configfile.h"
31 /* Include header files for the mach system, if they exist.. */
32 #if HAVE_MACH_MACH_INIT_H
33 # include <mach/mach_init.h>
34 #endif
35 #if HAVE_MACH_HOST_PRIV_H
36 # include <mach/host_priv.h>
37 #endif
38 #if HAVE_MACH_MACH_ERROR_H
39 # include <mach/mach_error.h>
40 #endif
41 #if HAVE_MACH_MACH_HOST_H
42 # include <mach/mach_host.h>
43 #endif
44 #if HAVE_MACH_MACH_PORT_H
45 # include <mach/mach_port.h>
46 #endif
47 #if HAVE_MACH_MACH_TYPES_H
48 # include <mach/mach_types.h>
49 #endif
50 #if HAVE_MACH_MESSAGE_H
51 # include <mach/message.h>
52 #endif
53 #if HAVE_MACH_PROCESSOR_SET_H
54 # include <mach/processor_set.h>
55 #endif
56 #if HAVE_MACH_TASK_H
57 # include <mach/task.h>
58 #endif
59 #if HAVE_MACH_THREAD_ACT_H
60 # include <mach/thread_act.h>
61 #endif
62 #if HAVE_MACH_VM_REGION_H
63 # include <mach/vm_region.h>
64 #endif
65 #if HAVE_MACH_VM_MAP_H
66 # include <mach/vm_map.h>
67 #endif
68 #if HAVE_MACH_VM_PROT_H
69 # include <mach/vm_prot.h>
70 #endif
72 #define MODULE_NAME "processes"
74 #if HAVE_THREAD_INFO || KERNEL_LINUX
75 # define PROCESSES_HAVE_READ 1
76 #else
77 # define PROCESSES_HAVE_READ 0
78 #endif
80 #define BUFSIZE 256
82 static char *ps_file = "processes.rrd";
84 static char *ds_def[] =
85 {
86 "DS:running:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
87 "DS:sleeping:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
88 "DS:zombies:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
89 "DS:stopped:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
90 "DS:paging:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
91 "DS:blocked:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
92 NULL
93 };
94 static int ds_num = 6;
96 static char *config_keys[] =
97 {
98 "CollectName",
99 NULL
100 };
101 static int config_keys_num = 1;
103 typedef struct procstat
104 {
105 #define PROCSTAT_NAME_LEN 256
106 char name[PROCSTAT_NAME_LEN];
107 unsigned int num_proc;
108 unsigned int num_lwp;
109 unsigned int vmem_rss;
110 unsigned int vmem_minflt;
111 unsigned int vmem_majflt;
112 unsigned int cpu_user;
113 unsigned int cpu_system;
114 struct procstat *next;
115 } procstat_t;
117 static procstat_t *list_head_g = NULL;
119 #if HAVE_THREAD_INFO
120 static mach_port_t port_host_self;
121 static mach_port_t port_task_self;
123 static processor_set_name_array_t pset_list;
124 static mach_msg_type_number_t pset_list_len;
125 /* #endif HAVE_THREAD_INFO */
127 #elif KERNEL_LINUX
128 /* No global variables */
129 #endif /* KERNEL_LINUX */
131 static procstat_t *ps_list_append (procstat_t *list, const char *name)
132 {
133 procstat_t *new;
134 procstat_t *ptr;
136 if ((new = (procstat_t *) malloc (sizeof (procstat_t))) == NULL)
137 return (NULL);
138 memset (new, 0, sizeof (procstat_t));
139 strncpy (new->name, name, PROCSTAT_NAME_LEN);
141 for (ptr = list; ptr != NULL; ptr = ptr->next)
142 if (ptr->next == NULL)
143 break;
145 if (ptr != NULL)
146 ptr->next = new;
148 return (new);
149 }
151 static void ps_list_add (procstat_t *list, procstat_t *entry)
152 {
153 procstat_t *ptr;
155 ptr = list;
156 while ((ptr != NULL) && (strcmp (ptr->name, entry->name) != 0))
157 ptr = ptr->next;
159 if (ptr == NULL)
160 return;
162 ptr->num_proc += entry->num_proc;
163 ptr->num_lwp += entry->num_lwp;
164 ptr->vmem_rss += entry->vmem_rss;
165 ptr->vmem_minflt += entry->vmem_minflt;
166 ptr->vmem_majflt += entry->vmem_majflt;
167 ptr->cpu_user += entry->cpu_user;
168 ptr->cpu_system += entry->cpu_system;
169 }
171 static void ps_list_reset (procstat_t *ps)
172 {
173 while (ps != NULL)
174 {
175 ps->num_proc = 0;
176 ps->num_lwp = 0;
177 ps->vmem_rss = 0;
178 ps->vmem_minflt = 0;
179 ps->vmem_majflt = 0;
180 ps->cpu_user = 0;
181 ps->cpu_system = 0;
182 ps = ps->next;
183 }
184 }
186 static int ps_config (char *key, char *value)
187 {
188 if (strcasecmp (key, "CollectName") == 0)
189 {
190 procstat_t *entry;
192 entry = ps_list_append (list_head_g, value);
193 if (entry == NULL)
194 {
195 syslog (LOG_ERR, "processes plugin: ps_list_append failed.");
196 return (1);
197 }
198 if (list_head_g != NULL)
199 list_head_g = entry;
200 }
201 else
202 {
203 return (-1);
204 }
206 return (0);
207 }
209 static void ps_init (void)
210 {
211 #if HAVE_THREAD_INFO
212 kern_return_t status;
214 port_host_self = mach_host_self ();
215 port_task_self = mach_task_self ();
217 if (pset_list != NULL)
218 {
219 vm_deallocate (port_task_self,
220 (vm_address_t) pset_list,
221 pset_list_len * sizeof (processor_set_t));
222 pset_list = NULL;
223 pset_list_len = 0;
224 }
226 if ((status = host_processor_sets (port_host_self,
227 &pset_list,
228 &pset_list_len)) != KERN_SUCCESS)
229 {
230 syslog (LOG_ERR, "host_processor_sets failed: %s\n",
231 mach_error_string (status));
232 pset_list = NULL;
233 pset_list_len = 0;
234 return;
235 }
236 /* #endif HAVE_THREAD_INFO */
238 #elif KERNEL_LINUX
239 /* No init */
240 #endif /* KERNEL_LINUX */
242 return;
243 }
245 static void ps_write (char *host, char *inst, char *val)
246 {
247 rrd_update_file (host, ps_file, val, ds_def, ds_num);
248 }
250 #if PROCESSES_HAVE_READ
251 static void ps_submit (int running,
252 int sleeping,
253 int zombies,
254 int stopped,
255 int paging,
256 int blocked)
257 {
258 char buf[BUFSIZE];
260 if (snprintf (buf, BUFSIZE, "%u:%i:%i:%i:%i:%i:%i",
261 (unsigned int) curtime,
262 running, sleeping, zombies, stopped, paging,
263 blocked) >= BUFSIZE)
264 return;
266 DBG ("running = %i; sleeping = %i; zombies = %i; stopped = %i; paging = %i; blocked = %i;",
267 running, sleeping, zombies, stopped, paging, blocked);
269 plugin_submit (MODULE_NAME, "-", buf);
270 }
272 static int *ps_read_tasks (int pid)
273 {
274 int *list;
275 /* TODO */
276 }
278 static void ps_read (void)
279 {
280 #if HAVE_THREAD_INFO
281 kern_return_t status;
283 int pset;
284 processor_set_t port_pset_priv;
286 int task;
287 task_array_t task_list;
288 mach_msg_type_number_t task_list_len;
290 int thread;
291 thread_act_array_t thread_list;
292 mach_msg_type_number_t thread_list_len;
293 thread_basic_info_data_t thread_data;
294 mach_msg_type_number_t thread_data_len;
296 int running = 0;
297 int sleeping = 0;
298 int zombies = 0;
299 int stopped = 0;
300 int blocked = 0;
302 /*
303 * The Mach-concept is a little different from the traditional UNIX
304 * concept: All the work is done in threads. Threads are contained in
305 * `tasks'. Therefore, `task status' doesn't make much sense, since
306 * it's actually a `thread status'.
307 * Tasks are assigned to sets of processors, so that's where you go to
308 * get a list.
309 */
310 for (pset = 0; pset < pset_list_len; pset++)
311 {
312 if ((status = host_processor_set_priv (port_host_self,
313 pset_list[pset],
314 &port_pset_priv)) != KERN_SUCCESS)
315 {
316 syslog (LOG_ERR, "host_processor_set_priv failed: %s\n",
317 mach_error_string (status));
318 continue;
319 }
321 if ((status = processor_set_tasks (port_pset_priv,
322 &task_list,
323 &task_list_len)) != KERN_SUCCESS)
324 {
325 syslog (LOG_ERR, "processor_set_tasks failed: %s\n",
326 mach_error_string (status));
327 mach_port_deallocate (port_task_self, port_pset_priv);
328 continue;
329 }
331 for (task = 0; task < task_list_len; task++)
332 {
333 status = task_threads (task_list[task], &thread_list,
334 &thread_list_len);
335 if (status != KERN_SUCCESS)
336 {
337 /* Apple's `top' treats this case a zombie. It
338 * makes sense to some extend: A `zombie'
339 * thread is nonsense, since the task/process
340 * is dead. */
341 zombies++;
342 DBG ("task_threads failed: %s",
343 mach_error_string (status));
344 if (task_list[task] != port_task_self)
345 mach_port_deallocate (port_task_self,
346 task_list[task]);
347 continue; /* with next task_list */
348 }
350 for (thread = 0; thread < thread_list_len; thread++)
351 {
352 thread_data_len = THREAD_BASIC_INFO_COUNT;
353 status = thread_info (thread_list[thread],
354 THREAD_BASIC_INFO,
355 (thread_info_t) &thread_data,
356 &thread_data_len);
357 if (status != KERN_SUCCESS)
358 {
359 syslog (LOG_ERR, "thread_info failed: %s\n",
360 mach_error_string (status));
361 if (task_list[task] != port_task_self)
362 mach_port_deallocate (port_task_self,
363 thread_list[thread]);
364 continue; /* with next thread_list */
365 }
367 switch (thread_data.run_state)
368 {
369 case TH_STATE_RUNNING:
370 running++;
371 break;
372 case TH_STATE_STOPPED:
373 /* What exactly is `halted'? */
374 case TH_STATE_HALTED:
375 stopped++;
376 break;
377 case TH_STATE_WAITING:
378 sleeping++;
379 break;
380 case TH_STATE_UNINTERRUPTIBLE:
381 blocked++;
382 break;
383 /* There is no `zombie' case here,
384 * since there are no zombie-threads.
385 * There's only zombie tasks, which are
386 * handled above. */
387 default:
388 syslog (LOG_WARNING,
389 "Unknown thread status: %s",
390 thread_data.run_state);
391 break;
392 } /* switch (thread_data.run_state) */
394 if (task_list[task] != port_task_self)
395 {
396 status = mach_port_deallocate (port_task_self,
397 thread_list[thread]);
398 if (status != KERN_SUCCESS)
399 syslog (LOG_ERR, "mach_port_deallocate failed: %s",
400 mach_error_string (status));
401 }
402 } /* for (thread_list) */
404 if ((status = vm_deallocate (port_task_self,
405 (vm_address_t) thread_list,
406 thread_list_len * sizeof (thread_act_t)))
407 != KERN_SUCCESS)
408 {
409 syslog (LOG_ERR, "vm_deallocate failed: %s",
410 mach_error_string (status));
411 }
412 thread_list = NULL;
413 thread_list_len = 0;
415 /* Only deallocate the task port, if it isn't our own.
416 * Don't know what would happen in that case, but this
417 * is what Apple's top does.. ;) */
418 if (task_list[task] != port_task_self)
419 {
420 status = mach_port_deallocate (port_task_self,
421 task_list[task]);
422 if (status != KERN_SUCCESS)
423 syslog (LOG_ERR, "mach_port_deallocate failed: %s",
424 mach_error_string (status));
425 }
426 } /* for (task_list) */
428 if ((status = vm_deallocate (port_task_self,
429 (vm_address_t) task_list,
430 task_list_len * sizeof (task_t))) != KERN_SUCCESS)
431 {
432 syslog (LOG_ERR, "vm_deallocate failed: %s",
433 mach_error_string (status));
434 }
435 task_list = NULL;
436 task_list_len = 0;
438 if ((status = mach_port_deallocate (port_task_self, port_pset_priv))
439 != KERN_SUCCESS)
440 {
441 syslog (LOG_ERR, "mach_port_deallocate failed: %s",
442 mach_error_string (status));
443 }
444 } /* for (pset_list) */
446 ps_submit (running, sleeping, zombies, stopped, -1, blocked);
447 /* #endif HAVE_THREAD_INFO */
449 #elif KERNEL_LINUX
450 int running = 0;
451 int sleeping = 0;
452 int zombies = 0;
453 int stopped = 0;
454 int paging = 0;
455 int blocked = 0;
457 char buf[BUFSIZE];
458 char filename[20]; /* need 17 bytes */
459 char *fields[BUFSIZE];
461 struct dirent *ent;
462 DIR *proc;
463 FILE *fh;
465 running = sleeping = zombies = stopped = paging = blocked = 0;
467 if ((proc = opendir ("/proc")) == NULL)
468 {
469 syslog (LOG_ERR, "Cannot open `/proc': %s", strerror (errno));
470 return;
471 }
473 while ((ent = readdir (proc)) != NULL)
474 {
475 if (!isdigit (ent->d_name[0]))
476 continue;
478 if (snprintf (filename, 20, "/proc/%s/stat", ent->d_name) >= 20)
479 continue;
481 if ((fh = fopen (filename, "r")) == NULL)
482 {
483 syslog (LOG_NOTICE, "Cannot open `%s': %s", filename,
484 strerror (errno));
485 continue;
486 }
488 if (fgets (buf, BUFSIZE, fh) == NULL)
489 {
490 syslog (LOG_NOTICE, "Unable to read from `%s': %s",
491 filename, strerror (errno));
492 fclose (fh);
493 continue;
494 }
496 fclose (fh);
498 if (strsplit (buf, fields, BUFSIZE) < 3)
499 {
500 DBG ("Line has less than three fields.");
501 continue;
502 }
504 switch (fields[2][0])
505 {
506 case 'R': running++; break;
507 case 'S': sleeping++; break;
508 case 'D': blocked++; break;
509 case 'Z': zombies++; break;
510 case 'T': stopped++; break;
511 case 'W': paging++; break;
512 }
513 }
515 closedir (proc);
517 ps_submit (running, sleeping, zombies, stopped, paging, blocked);
518 #endif /* KERNEL_LINUX */
519 }
520 #else
521 # define ps_read NULL
522 #endif /* PROCESSES_HAVE_READ */
524 void module_register (void)
525 {
526 plugin_register (MODULE_NAME, ps_init, ps_read, ps_write);
527 cf_register (MODULE_NAME, ps_config, config_keys, config_keys_num);
528 }
530 #undef BUFSIZE
531 #undef MODULE_NAME