fae1c2d02536cb4fc9387d0fb3c40832968ae3bb
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"
30 /* Include header files for the mach system, if they exist.. */
31 #if HAVE_MACH_MACH_INIT_H
32 # include <mach/mach_init.h>
33 #endif
34 #if HAVE_MACH_HOST_PRIV_H
35 # include <mach/host_priv.h>
36 #endif
37 #if HAVE_MACH_MACH_ERROR_H
38 # include <mach/mach_error.h>
39 #endif
40 #if HAVE_MACH_MACH_HOST_H
41 # include <mach/mach_host.h>
42 #endif
43 #if HAVE_MACH_MACH_PORT_H
44 # include <mach/mach_port.h>
45 #endif
46 #if HAVE_MACH_MACH_TYPES_H
47 # include <mach/mach_types.h>
48 #endif
49 #if HAVE_MACH_MESSAGE_H
50 # include <mach/message.h>
51 #endif
52 #if HAVE_MACH_PROCESSOR_SET_H
53 # include <mach/processor_set.h>
54 #endif
55 #if HAVE_MACH_TASK_H
56 # include <mach/task.h>
57 #endif
58 #if HAVE_MACH_THREAD_ACT_H
59 # include <mach/thread_act.h>
60 #endif
61 #if HAVE_MACH_VM_REGION_H
62 # include <mach/vm_region.h>
63 #endif
64 #if HAVE_MACH_VM_MAP_H
65 # include <mach/vm_map.h>
66 #endif
67 #if HAVE_MACH_VM_PROT_H
68 # include <mach/vm_prot.h>
69 #endif
71 #define MODULE_NAME "processes"
73 #if HAVE_THREAD_INFO || KERNEL_LINUX
74 # define PROCESSES_HAVE_READ 1
75 #else
76 # define PROCESSES_HAVE_READ 0
77 #endif
79 #define BUFSIZE 256
81 static char *ps_file = "processes.rrd";
83 static char *ds_def[] =
84 {
85 "DS:running:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
86 "DS:sleeping:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
87 "DS:zombies:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
88 "DS:stopped:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
89 "DS:paging:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
90 "DS:blocked:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
91 NULL
92 };
93 static int ds_num = 6;
95 #if HAVE_THREAD_INFO
96 static mach_port_t port_host_self;
97 static mach_port_t port_task_self;
99 static processor_set_name_array_t pset_list;
100 static mach_msg_type_number_t pset_list_len;
101 /* #endif HAVE_THREAD_INFO */
103 #elif KERNEL_LINUX
104 /* No global variables */
105 #endif /* KERNEL_LINUX */
107 static void ps_init (void)
108 {
109 #if HAVE_THREAD_INFO
110 kern_return_t status;
112 port_host_self = mach_host_self ();
113 port_task_self = mach_task_self ();
115 if (pset_list != NULL)
116 {
117 vm_deallocate (port_task_self,
118 (vm_address_t) pset_list,
119 pset_list_len * sizeof (processor_set_t));
120 pset_list = NULL;
121 pset_list_len = 0;
122 }
124 if ((status = host_processor_sets (port_host_self,
125 &pset_list,
126 &pset_list_len)) != KERN_SUCCESS)
127 {
128 syslog (LOG_ERR, "host_processor_sets failed: %s\n",
129 mach_error_string (status));
130 pset_list = NULL;
131 pset_list_len = 0;
132 return;
133 }
134 /* #endif HAVE_THREAD_INFO */
136 #elif KERNEL_LINUX
137 /* No init */
138 #endif /* KERNEL_LINUX */
140 return;
141 }
143 static void ps_write (char *host, char *inst, char *val)
144 {
145 rrd_update_file (host, ps_file, val, ds_def, ds_num);
146 }
148 #if PROCESSES_HAVE_READ
149 static void ps_submit (int running,
150 int sleeping,
151 int zombies,
152 int stopped,
153 int paging,
154 int blocked)
155 {
156 char buf[BUFSIZE];
158 if (snprintf (buf, BUFSIZE, "%u:%i:%i:%i:%i:%i:%i",
159 (unsigned int) curtime,
160 running, sleeping, zombies, stopped, paging,
161 blocked) >= BUFSIZE)
162 return;
164 plugin_submit (MODULE_NAME, "-", buf);
165 }
167 static void ps_read (void)
168 {
169 #if HAVE_THREAD_INFO
170 kern_return_t status;
172 int pset;
173 processor_set_t port_pset_priv;
175 int task;
176 task_array_t task_list;
177 mach_msg_type_number_t task_list_len;
179 int thread;
180 thread_act_array_t thread_list;
181 mach_msg_type_number_t thread_list_len;
182 thread_basic_info_data_t thread_data;
183 mach_msg_type_number_t thread_data_len;
185 int running = 0;
186 int sleeping = 0;
187 int zombies = 0;
188 int stopped = 0;
189 int blocked = 0;
191 /*
192 * The Mach-concept is a little different from the traditional UNIX
193 * concept: All the work is done in threads. Threads are contained in
194 * `tasks'. Therefore, `task status' doesn't make much sense, since
195 * it's actually a `thread status'.
196 * Tasks are assigned to sets of processors, so that's where you go to
197 * get a list.
198 */
199 for (pset = 0; pset < pset_list_len; pset++)
200 {
201 if ((status = host_processor_set_priv (port_host_self,
202 pset_list[pset],
203 &port_pset_priv)) != KERN_SUCCESS)
204 {
205 syslog (LOG_ERR, "host_processor_set_priv failed: %s\n",
206 mach_error_string (status));
207 continue;
208 }
210 if ((status = processor_set_tasks (port_pset_priv,
211 &task_list,
212 &task_list_len)) != KERN_SUCCESS)
213 {
214 syslog (LOG_ERR, "processor_set_tasks failed: %s\n",
215 mach_error_string (status));
216 mach_port_deallocate (port_task_self, port_pset_priv);
217 continue;
218 }
220 for (task = 0; task < task_list_len; task++)
221 {
222 status = task_threads (task_list[task], &thread_list,
223 &thread_list_len);
224 if (status != KERN_SUCCESS)
225 {
226 /* Apple's `top' treats this case a zombie. It
227 * makes sense to some extend: A `zombie'
228 * thread is nonsense, since the task/process
229 * is dead. */
230 zombies++;
231 DBG ("task_threads failed: %s",
232 mach_error_string (status));
233 if (task_list[task] != port_task_self)
234 mach_port_deallocate (port_task_self,
235 task_list[task]);
236 continue; /* with next task_list */
237 }
239 for (thread = 0; thread < thread_list_len; thread++)
240 {
241 thread_data_len = THREAD_BASIC_INFO_COUNT;
242 status = thread_info (thread_list[thread],
243 THREAD_BASIC_INFO,
244 (thread_info_t) &thread_data,
245 &thread_data_len);
246 if (status != KERN_SUCCESS)
247 {
248 syslog (LOG_ERR, "thread_info failed: %s\n",
249 mach_error_string (status));
250 if (task_list[task] != port_task_self)
251 mach_port_deallocate (port_task_self,
252 thread_list[thread]);
253 continue; /* with next thread_list */
254 }
256 switch (thread_data.run_state)
257 {
258 case TH_STATE_RUNNING:
259 running++;
260 break;
261 case TH_STATE_STOPPED:
262 /* What exactly is `halted'? */
263 case TH_STATE_HALTED:
264 stopped++;
265 break;
266 case TH_STATE_WAITING:
267 sleeping++;
268 break;
269 case TH_STATE_UNINTERRUPTIBLE:
270 blocked++;
271 break;
272 /* There is no `zombie' case here,
273 * since there are no zombie-threads.
274 * There's only zombie tasks, which are
275 * handled above. */
276 default:
277 syslog (LOG_WARNING,
278 "Unknown thread status: %s",
279 thread_data.run_state);
280 break;
281 } /* switch (thread_data.run_state) */
283 if (task_list[task] != port_task_self)
284 {
285 status = mach_port_deallocate (port_task_self,
286 thread_list[thread]);
287 if (status != KERN_SUCCESS)
288 syslog (LOG_ERR, "mach_port_deallocate failed: %s",
289 mach_error_string (status));
290 }
291 } /* for (thread_list) */
293 if ((status = vm_deallocate (port_task_self,
294 (vm_address_t) thread_list,
295 thread_list_len * sizeof (thread_act_t)))
296 != KERN_SUCCESS)
297 {
298 syslog (LOG_ERR, "vm_deallocate failed: %s",
299 mach_error_string (status));
300 }
301 thread_list = NULL;
302 thread_list_len = 0;
304 /* Only deallocate the task port, if it isn't our own.
305 * Don't know what would happen in that case, but this
306 * is what Apple's top does.. ;) */
307 if (task_list[task] != port_task_self)
308 {
309 status = mach_port_deallocate (port_task_self,
310 task_list[task]);
311 if (status != KERN_SUCCESS)
312 syslog (LOG_ERR, "mach_port_deallocate failed: %s",
313 mach_error_string (status));
314 }
315 } /* for (task_list) */
317 if ((status = vm_deallocate (port_task_self,
318 (vm_address_t) task_list,
319 task_list_len * sizeof (task_t))) != KERN_SUCCESS)
320 {
321 syslog (LOG_ERR, "vm_deallocate failed: %s",
322 mach_error_string (status));
323 }
324 task_list = NULL;
325 task_list_len = 0;
327 if ((status = mach_port_deallocate (port_task_self, port_pset_priv))
328 != KERN_SUCCESS)
329 {
330 syslog (LOG_ERR, "mach_port_deallocate failed: %s",
331 mach_error_string (status));
332 }
333 } /* for (pset_list) */
335 ps_submit (running, sleeping, zombies, stopped, -1, blocked);
336 /* #endif HAVE_THREAD_INFO */
338 #elif KERNEL_LINUX
339 int running = 0;
340 int sleeping = 0;
341 int zombies = 0;
342 int stopped = 0;
343 int paging = 0;
344 int blocked = 0;
346 char buf[BUFSIZE];
347 char filename[20]; /* need 17 bytes */
348 char *fields[BUFSIZE];
350 struct dirent *ent;
351 DIR *proc;
352 FILE *fh;
354 running = sleeping = zombies = stopped = paging = blocked = 0;
356 if ((proc = opendir ("/proc")) == NULL)
357 {
358 syslog (LOG_ERR, "Cannot open `/proc': %s", strerror (errno));
359 return;
360 }
362 while ((ent = readdir (proc)) != NULL)
363 {
364 if (!isdigit (ent->d_name[0]))
365 continue;
367 if (snprintf (filename, 20, "/proc/%s/stat", ent->d_name) >= 20)
368 continue;
370 if ((fh = fopen (filename, "r")) == NULL)
371 {
372 syslog (LOG_ERR, "Cannot open `%s': %s", filename, strerror (errno));
373 continue;
374 }
376 if (fgets (buf, BUFSIZE, fh) == NULL)
377 {
378 fclose (fh);
379 continue;
380 }
382 fclose (fh);
384 if (strsplit (buf, fields, BUFSIZE) < 3)
385 continue;
387 switch (fields[2][0])
388 {
389 case 'R': running++; break;
390 case 'S': sleeping++; break;
391 case 'D': blocked++; break;
392 case 'Z': zombies++; break;
393 case 'T': stopped++; break;
394 case 'W': paging++; break;
395 }
396 }
398 closedir(proc);
400 ps_submit (running, sleeping, zombies, stopped, paging, blocked);
401 #endif /* KERNEL_LINUX */
402 }
403 #else
404 # define ps_read NULL
405 #endif /* PROCESSES_HAVE_READ */
407 void module_register (void)
408 {
409 plugin_register (MODULE_NAME, ps_init, ps_read, ps_write);
410 }
412 #undef BUFSIZE
413 #undef MODULE_NAME