1 /**
2 * collectd - src/processes.c
3 * Copyright (C) 2005 Lyonel Vincent
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors:
20 * Lyonel Vincent <lyonel at ezix.org>
21 * Florian octo Forster <octo at verplant.org>
22 **/
24 #include "collectd.h"
25 #include "common.h"
26 #include "plugin.h"
27 #include "utils_debug.h"
29 /* Include header files for the mach system, if they exist.. */
30 #if HAVE_MACH_MACH_INIT_H
31 # include <mach/mach_init.h>
32 #endif
33 #if HAVE_MACH_HOST_PRIV_H
34 # include <mach/host_priv.h>
35 #endif
36 #if HAVE_MACH_MACH_ERROR_H
37 # include <mach/mach_error.h>
38 #endif
39 #if HAVE_MACH_MACH_HOST_H
40 # include <mach/mach_host.h>
41 #endif
42 #if HAVE_MACH_MACH_PORT_H
43 # include <mach/mach_port.h>
44 #endif
45 #if HAVE_MACH_MACH_TYPES_H
46 # include <mach/mach_types.h>
47 #endif
48 #if HAVE_MACH_MESSAGE_H
49 # include <mach/message.h>
50 #endif
51 #if HAVE_MACH_PROCESSOR_SET_H
52 # include <mach/processor_set.h>
53 #endif
54 #if HAVE_MACH_TASK_H
55 # include <mach/task.h>
56 #endif
57 #if HAVE_MACH_THREAD_ACT_H
58 # include <mach/thread_act.h>
59 #endif
60 #if HAVE_MACH_VM_REGION_H
61 # include <mach/vm_region.h>
62 #endif
63 #if HAVE_MACH_VM_MAP_H
64 # include <mach/vm_map.h>
65 #endif
66 #if HAVE_MACH_VM_PROT_H
67 # include <mach/vm_prot.h>
68 #endif
70 #define MODULE_NAME "processes"
72 #if HAVE_THREAD_INFO || defined(KERNEL_LINUX)
73 # define PROCESSES_HAVE_READ 1
74 #else
75 # define PROCESSES_HAVE_READ 0
76 #endif
78 #define BUFSIZE 256
80 static char *ps_file = "processes.rrd";
82 static char *ds_def[] =
83 {
84 "DS:running:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
85 "DS:sleeping:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
86 "DS:zombies:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
87 "DS:stopped:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
88 "DS:paging:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
89 "DS:blocked:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
90 NULL
91 };
92 static int ds_num = 6;
94 #if HAVE_THREAD_INFO
95 static mach_port_t port_host_self;
96 static mach_port_t port_task_self;
98 static processor_set_name_array_t pset_list;
99 static mach_msg_type_number_t pset_list_len;
100 /* #endif HAVE_THREAD_INFO */
102 #elif KERNEL_LINUX
103 /* No global variables */
104 #endif /* KERNEL_LINUX */
106 static void ps_init (void)
107 {
108 #if HAVE_THREAD_INFO
109 kern_return_t status;
111 port_host_self = mach_host_self ();
112 port_task_self = mach_task_self ();
114 if (pset_list != NULL)
115 {
116 vm_deallocate (port_task_self,
117 (vm_address_t) pset_list,
118 pset_list_len * sizeof (processor_set_t));
119 pset_list = NULL;
120 pset_list_len = 0;
121 }
123 if ((status = host_processor_sets (port_host_self,
124 &pset_list,
125 &pset_list_len)) != KERN_SUCCESS)
126 {
127 syslog (LOG_ERR, "host_processor_sets failed: %s\n",
128 mach_error_string (status));
129 pset_list = NULL;
130 pset_list_len = 0;
131 return;
132 }
133 /* #endif HAVE_THREAD_INFO */
135 #elif KERNEL_LINUX
136 /* No init */
137 #endif /* KERNEL_LINUX */
139 return;
140 }
142 static void ps_write (char *host, char *inst, char *val)
143 {
144 rrd_update_file (host, ps_file, val, ds_def, ds_num);
145 }
147 #if PROCESSES_HAVE_READ
148 static void ps_submit (int running,
149 int sleeping,
150 int zombies,
151 int stopped,
152 int paging,
153 int blocked)
154 {
155 char buf[BUFSIZE];
157 if (snprintf (buf, BUFSIZE, "%u:%i:%i:%i:%i:%i:%i",
158 (unsigned int) curtime,
159 running, sleeping, zombies, stopped, paging,
160 blocked) >= BUFSIZE)
161 return;
163 plugin_submit (MODULE_NAME, "-", buf);
164 }
166 static void ps_read (void)
167 {
168 #if HAVE_THREAD_INFO
169 kern_return_t status;
171 int pset;
172 processor_set_t port_pset_priv;
174 int task;
175 task_array_t task_list;
176 mach_msg_type_number_t task_list_len;
178 int thread;
179 thread_act_array_t thread_list;
180 mach_msg_type_number_t thread_list_len;
181 thread_basic_info_data_t thread_data;
182 mach_msg_type_number_t thread_data_len;
184 int running = 0;
185 int sleeping = 0;
186 int zombies = 0;
187 int stopped = 0;
188 int blocked = 0;
190 /*
191 * The Mach-concept is a little different from the traditional UNIX
192 * concept: All the work is done in threads. Threads are contained in
193 * `tasks'. Therefore, `task status' doesn't make much sense, since
194 * it's actually a `thread status'.
195 * Tasks are assigned to sets of processors, so that's where you go to
196 * get a list.
197 */
198 for (pset = 0; pset < pset_list_len; pset++)
199 {
200 if ((status = host_processor_set_priv (port_host_self,
201 pset_list[pset],
202 &port_pset_priv)) != KERN_SUCCESS)
203 {
204 syslog (LOG_ERR, "host_processor_set_priv failed: %s\n",
205 mach_error_string (status));
206 continue;
207 }
209 if ((status = processor_set_tasks (port_pset_priv,
210 &task_list,
211 &task_list_len)) != KERN_SUCCESS)
212 {
213 syslog (LOG_ERR, "processor_set_tasks failed: %s\n",
214 mach_error_string (status));
215 mach_port_deallocate (port_task_self, port_pset_priv);
216 continue;
217 }
219 for (task = 0; task < task_list_len; task++)
220 {
221 status = task_threads (task_list[task], &thread_list,
222 &thread_list_len);
223 if (status != KERN_SUCCESS)
224 {
225 /* Apple's `top' treats this case a zombie. It
226 * makes sense to some extend: A `zombie'
227 * thread is nonsense, since the task/process
228 * is dead. */
229 zombies++;
230 DBG ("task_threads failed: %s",
231 mach_error_string (status));
232 if (task_list[task] != port_task_self)
233 mach_port_deallocate (port_task_self,
234 task_list[task]);
235 continue; /* with next task_list */
236 }
238 for (thread = 0; thread < thread_list_len; thread++)
239 {
240 thread_data_len = THREAD_BASIC_INFO_COUNT;
241 status = thread_info (thread_list[thread],
242 THREAD_BASIC_INFO,
243 (thread_info_t) &thread_data,
244 &thread_data_len);
245 if (status != KERN_SUCCESS)
246 {
247 syslog (LOG_ERR, "thread_info failed: %s\n",
248 mach_error_string (status));
249 if (task_list[task] != port_task_self)
250 mach_port_deallocate (port_task_self,
251 thread_list[thread]);
252 continue; /* with next thread_list */
253 }
255 switch (thread_data.run_state)
256 {
257 case TH_STATE_RUNNING:
258 running++;
259 break;
260 case TH_STATE_STOPPED:
261 /* What exactly is `halted'? */
262 case TH_STATE_HALTED:
263 stopped++;
264 break;
265 case TH_STATE_WAITING:
266 sleeping++;
267 break;
268 case TH_STATE_UNINTERRUPTIBLE:
269 blocked++;
270 break;
271 /* There is no `zombie' case here,
272 * since there are no zombie-threads.
273 * There's only zombie tasks, which are
274 * handled above. */
275 default:
276 syslog (LOG_WARNING,
277 "Unknown thread status: %s",
278 thread_data.run_state);
279 break;
280 } /* switch (thread_data.run_state) */
282 if (task_list[task] != port_task_self)
283 {
284 status = mach_port_deallocate (port_task_self,
285 thread_list[thread]);
286 if (status != KERN_SUCCESS)
287 syslog (LOG_ERR, "mach_port_deallocate failed: %s",
288 mach_error_string (status));
289 }
290 } /* for (thread_list) */
292 if ((status = vm_deallocate (port_task_self,
293 (vm_address_t) thread_list,
294 thread_list_len * sizeof (thread_act_t)))
295 != KERN_SUCCESS)
296 {
297 syslog (LOG_ERR, "vm_deallocate failed: %s",
298 mach_error_string (status));
299 }
300 thread_list = NULL;
301 thread_list_len = 0;
303 /* Only deallocate the task port, if it isn't our own.
304 * Don't know what would happen in that case, but this
305 * is what Apple's top does.. ;) */
306 if (task_list[task] != port_task_self)
307 {
308 status = mach_port_deallocate (port_task_self,
309 task_list[task]);
310 if (status != KERN_SUCCESS)
311 syslog (LOG_ERR, "mach_port_deallocate failed: %s",
312 mach_error_string (status));
313 }
314 } /* for (task_list) */
316 if ((status = vm_deallocate (port_task_self,
317 (vm_address_t) task_list,
318 task_list_len * sizeof (task_t))) != KERN_SUCCESS)
319 {
320 syslog (LOG_ERR, "vm_deallocate failed: %s",
321 mach_error_string (status));
322 }
323 task_list = NULL;
324 task_list_len = 0;
326 if ((status = mach_port_deallocate (port_task_self, port_pset_priv))
327 != KERN_SUCCESS)
328 {
329 syslog (LOG_ERR, "mach_port_deallocate failed: %s",
330 mach_error_string (status));
331 }
332 } /* for (pset_list) */
333 /* #endif HAVE_THREAD_INFO */
335 #elif KERNEL_LINUX
336 int running = 0;
337 int sleeping = 0;
338 int zombies = 0;
339 int stopped = 0;
340 int paging = 0;
341 int blocked = 0;
343 char buf[BUFSIZE];
344 char filename[20]; /* need 17 bytes */
345 char *fields[BUFSIZE];
347 struct dirent *ent;
348 DIR *proc;
349 FILE *fh;
351 running = sleeping = zombies = stopped = paging = blocked = 0;
353 if ((proc = opendir ("/proc")) == NULL)
354 {
355 syslog (LOG_ERR, "Cannot open `/proc': %s", strerror (errno));
356 return;
357 }
359 while ((ent = readdir (proc)) != NULL)
360 {
361 if (!isdigit (ent->d_name[0]))
362 continue;
364 if (snprintf (filename, 20, "/proc/%s/stat", ent->d_name) >= 20)
365 continue;
367 if ((fh = fopen (filename, "r")) == NULL)
368 {
369 syslog (LOG_ERR, "Cannot open `%s': %s", filename, strerror (errno));
370 continue;
371 }
373 if (fgets (buf, BUFSIZE, fh) == NULL)
374 {
375 fclose (fh);
376 continue;
377 }
379 fclose (fh);
381 if (strsplit (buf, fields, BUFSIZE) < 3)
382 continue;
384 switch (fields[2][0])
385 {
386 case 'R': running++; break;
387 case 'S': sleeping++; break;
388 case 'D': blocked++; break;
389 case 'Z': zombies++; break;
390 case 'T': stopped++; break;
391 case 'W': paging++; break;
392 }
393 }
395 closedir(proc);
397 ps_submit (running, sleeping, zombies, stopped, paging, blocked);
398 #endif /* KERNEL_LINUX */
399 }
400 #else
401 # define ps_read NULL
402 #endif /* PROCESSES_HAVE_READ */
404 void module_register (void)
405 {
406 plugin_register (MODULE_NAME, ps_init, ps_read, ps_write);
407 }
409 #undef BUFSIZE
410 #undef MODULE_NAME