`KERNEL_LINUX' and `KERNEL_SOLARIS' is now defined to `1' instead of <nothing>, so...
[collectd.git] / src / processes.c
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  **/
24
25 #include "collectd.h"
26 #include "common.h"
27 #include "plugin.h"
28 #include "utils_debug.h"
29
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
70
71 #define MODULE_NAME "processes"
72
73 #if HAVE_THREAD_INFO || KERNEL_LINUX
74 # define PROCESSES_HAVE_READ 1
75 #else
76 # define PROCESSES_HAVE_READ 0
77 #endif
78
79 #define BUFSIZE 256
80
81 static char *ps_file = "processes.rrd";
82
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;
94
95 #if HAVE_THREAD_INFO
96 static mach_port_t port_host_self;
97 static mach_port_t port_task_self;
98
99 static processor_set_name_array_t pset_list;
100 static mach_msg_type_number_t     pset_list_len;
101 /* #endif HAVE_THREAD_INFO */
102
103 #elif KERNEL_LINUX
104 /* No global variables */
105 #endif /* KERNEL_LINUX */
106
107 static void ps_init (void)
108 {
109 #if HAVE_THREAD_INFO
110         kern_return_t status;
111
112         port_host_self = mach_host_self ();
113         port_task_self = mach_task_self ();
114
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         }
123
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 */
135
136 #elif KERNEL_LINUX
137         /* No init */
138 #endif /* KERNEL_LINUX */
139
140         return;
141 }
142
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 }
147
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];
157
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;
163
164         plugin_submit (MODULE_NAME, "-", buf);
165 }
166
167 static void ps_read (void)
168 {
169 #if HAVE_THREAD_INFO
170         kern_return_t            status;
171
172         int                      pset;
173         processor_set_t          port_pset_priv;
174
175         int                      task;
176         task_array_t             task_list;
177         mach_msg_type_number_t   task_list_len;
178
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;
184
185         int running  = 0;
186         int sleeping = 0;
187         int zombies  = 0;
188         int stopped  = 0;
189         int blocked  = 0;
190
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                 }
209
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                 }
219
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                         }
238
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                                 }
255
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) */
282
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) */
292
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;
303
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) */
316
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;
326
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) */
334 /* #endif HAVE_THREAD_INFO */
335
336 #elif KERNEL_LINUX
337         int running  = 0;
338         int sleeping = 0;
339         int zombies  = 0;
340         int stopped  = 0;
341         int paging   = 0;
342         int blocked  = 0;
343
344         char buf[BUFSIZE];
345         char filename[20]; /* need 17 bytes */
346         char *fields[BUFSIZE];
347
348         struct dirent *ent;
349         DIR *proc;
350         FILE *fh;
351
352         running = sleeping = zombies = stopped = paging = blocked = 0;
353
354         if ((proc = opendir ("/proc")) == NULL)
355         {
356                 syslog (LOG_ERR, "Cannot open `/proc': %s", strerror (errno));
357                 return;
358         }
359
360         while ((ent = readdir (proc)) != NULL)
361         {
362                 if (!isdigit (ent->d_name[0]))
363                         continue;
364
365                 if (snprintf (filename, 20, "/proc/%s/stat", ent->d_name) >= 20)
366                         continue;
367
368                 if ((fh = fopen (filename, "r")) == NULL)
369                 {
370                         syslog (LOG_ERR, "Cannot open `%s': %s", filename, strerror (errno));
371                         continue;
372                 }
373
374                 if (fgets (buf, BUFSIZE, fh) == NULL)
375                 {
376                         fclose (fh);
377                         continue;
378                 }
379
380                 fclose (fh);
381
382                 if (strsplit (buf, fields, BUFSIZE) < 3)
383                         continue;
384
385                 switch (fields[2][0])
386                 {
387                         case 'R': running++;  break;
388                         case 'S': sleeping++; break;
389                         case 'D': blocked++;  break;
390                         case 'Z': zombies++;  break;
391                         case 'T': stopped++;  break;
392                         case 'W': paging++;   break;
393                 }
394         }
395
396         closedir(proc);
397
398         ps_submit (running, sleeping, zombies, stopped, paging, blocked);
399 #endif /* KERNEL_LINUX */
400 }
401 #else
402 # define ps_read NULL
403 #endif /* PROCESSES_HAVE_READ */
404
405 void module_register (void)
406 {
407         plugin_register (MODULE_NAME, ps_init, ps_read, ps_write);
408 }
409
410 #undef BUFSIZE
411 #undef MODULE_NAME