Merge branch 'collectd-3.9'
[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         DBG ("running = %i; sleeping = %i; zombies = %i; stopped = %i; paging = %i; blocked = %i;",
165                         running, sleeping, zombies, stopped, paging, blocked);
166
167         plugin_submit (MODULE_NAME, "-", buf);
168 }
169
170 static void ps_read (void)
171 {
172 #if HAVE_THREAD_INFO
173         kern_return_t            status;
174
175         int                      pset;
176         processor_set_t          port_pset_priv;
177
178         int                      task;
179         task_array_t             task_list;
180         mach_msg_type_number_t   task_list_len;
181
182         int                      thread;
183         thread_act_array_t       thread_list;
184         mach_msg_type_number_t   thread_list_len;
185         thread_basic_info_data_t thread_data;
186         mach_msg_type_number_t   thread_data_len;
187
188         int running  = 0;
189         int sleeping = 0;
190         int zombies  = 0;
191         int stopped  = 0;
192         int blocked  = 0;
193
194         /*
195          * The Mach-concept is a little different from the traditional UNIX
196          * concept: All the work is done in threads. Threads are contained in
197          * `tasks'. Therefore, `task status' doesn't make much sense, since
198          * it's actually a `thread status'.
199          * Tasks are assigned to sets of processors, so that's where you go to
200          * get a list.
201          */
202         for (pset = 0; pset < pset_list_len; pset++)
203         {
204                 if ((status = host_processor_set_priv (port_host_self,
205                                                 pset_list[pset],
206                                                 &port_pset_priv)) != KERN_SUCCESS)
207                 {
208                         syslog (LOG_ERR, "host_processor_set_priv failed: %s\n",
209                                         mach_error_string (status));
210                         continue;
211                 }
212
213                 if ((status = processor_set_tasks (port_pset_priv,
214                                                 &task_list,
215                                                 &task_list_len)) != KERN_SUCCESS)
216                 {
217                         syslog (LOG_ERR, "processor_set_tasks failed: %s\n",
218                                         mach_error_string (status));
219                         mach_port_deallocate (port_task_self, port_pset_priv);
220                         continue;
221                 }
222
223                 for (task = 0; task < task_list_len; task++)
224                 {
225                         status = task_threads (task_list[task], &thread_list,
226                                         &thread_list_len);
227                         if (status != KERN_SUCCESS)
228                         {
229                                 /* Apple's `top' treats this case a zombie. It
230                                  * makes sense to some extend: A `zombie'
231                                  * thread is nonsense, since the task/process
232                                  * is dead. */
233                                 zombies++;
234                                 DBG ("task_threads failed: %s",
235                                                 mach_error_string (status));
236                                 if (task_list[task] != port_task_self)
237                                         mach_port_deallocate (port_task_self,
238                                                         task_list[task]);
239                                 continue; /* with next task_list */
240                         }
241
242                         for (thread = 0; thread < thread_list_len; thread++)
243                         {
244                                 thread_data_len = THREAD_BASIC_INFO_COUNT;
245                                 status = thread_info (thread_list[thread],
246                                                 THREAD_BASIC_INFO,
247                                                 (thread_info_t) &thread_data,
248                                                 &thread_data_len);
249                                 if (status != KERN_SUCCESS)
250                                 {
251                                         syslog (LOG_ERR, "thread_info failed: %s\n",
252                                                         mach_error_string (status));
253                                         if (task_list[task] != port_task_self)
254                                                 mach_port_deallocate (port_task_self,
255                                                                 thread_list[thread]);
256                                         continue; /* with next thread_list */
257                                 }
258
259                                 switch (thread_data.run_state)
260                                 {
261                                         case TH_STATE_RUNNING:
262                                                 running++;
263                                                 break;
264                                         case TH_STATE_STOPPED:
265                                         /* What exactly is `halted'? */
266                                         case TH_STATE_HALTED:
267                                                 stopped++;
268                                                 break;
269                                         case TH_STATE_WAITING:
270                                                 sleeping++;
271                                                 break;
272                                         case TH_STATE_UNINTERRUPTIBLE:
273                                                 blocked++;
274                                                 break;
275                                         /* There is no `zombie' case here,
276                                          * since there are no zombie-threads.
277                                          * There's only zombie tasks, which are
278                                          * handled above. */
279                                         default:
280                                                 syslog (LOG_WARNING,
281                                                                 "Unknown thread status: %s",
282                                                                 thread_data.run_state);
283                                                 break;
284                                 } /* switch (thread_data.run_state) */
285
286                                 if (task_list[task] != port_task_self)
287                                 {
288                                         status = mach_port_deallocate (port_task_self,
289                                                         thread_list[thread]);
290                                         if (status != KERN_SUCCESS)
291                                                 syslog (LOG_ERR, "mach_port_deallocate failed: %s",
292                                                                 mach_error_string (status));
293                                 }
294                         } /* for (thread_list) */
295
296                         if ((status = vm_deallocate (port_task_self,
297                                                         (vm_address_t) thread_list,
298                                                         thread_list_len * sizeof (thread_act_t)))
299                                         != KERN_SUCCESS)
300                         {
301                                 syslog (LOG_ERR, "vm_deallocate failed: %s",
302                                                 mach_error_string (status));
303                         }
304                         thread_list = NULL;
305                         thread_list_len = 0;
306
307                         /* Only deallocate the task port, if it isn't our own.
308                          * Don't know what would happen in that case, but this
309                          * is what Apple's top does.. ;) */
310                         if (task_list[task] != port_task_self)
311                         {
312                                 status = mach_port_deallocate (port_task_self,
313                                                 task_list[task]);
314                                 if (status != KERN_SUCCESS)
315                                         syslog (LOG_ERR, "mach_port_deallocate failed: %s",
316                                                         mach_error_string (status));
317                         }
318                 } /* for (task_list) */
319
320                 if ((status = vm_deallocate (port_task_self,
321                                 (vm_address_t) task_list,
322                                 task_list_len * sizeof (task_t))) != KERN_SUCCESS)
323                 {
324                         syslog (LOG_ERR, "vm_deallocate failed: %s",
325                                         mach_error_string (status));
326                 }
327                 task_list = NULL;
328                 task_list_len = 0;
329
330                 if ((status = mach_port_deallocate (port_task_self, port_pset_priv))
331                                 != KERN_SUCCESS)
332                 {
333                         syslog (LOG_ERR, "mach_port_deallocate failed: %s",
334                                         mach_error_string (status));
335                 }
336         } /* for (pset_list) */
337
338         ps_submit (running, sleeping, zombies, stopped, -1, blocked);
339 /* #endif HAVE_THREAD_INFO */
340
341 #elif KERNEL_LINUX
342         int running  = 0;
343         int sleeping = 0;
344         int zombies  = 0;
345         int stopped  = 0;
346         int paging   = 0;
347         int blocked  = 0;
348
349         char buf[BUFSIZE];
350         char filename[20]; /* need 17 bytes */
351         char *fields[BUFSIZE];
352
353         struct dirent *ent;
354         DIR *proc;
355         FILE *fh;
356
357         running = sleeping = zombies = stopped = paging = blocked = 0;
358
359         if ((proc = opendir ("/proc")) == NULL)
360         {
361                 syslog (LOG_ERR, "Cannot open `/proc': %s", strerror (errno));
362                 return;
363         }
364
365         while ((ent = readdir (proc)) != NULL)
366         {
367                 if (!isdigit (ent->d_name[0]))
368                         continue;
369
370                 if (snprintf (filename, 20, "/proc/%s/stat", ent->d_name) >= 20)
371                         continue;
372
373                 if ((fh = fopen (filename, "r")) == NULL)
374                 {
375                         syslog (LOG_NOTICE, "Cannot open `%s': %s", filename,
376                                         strerror (errno));
377                         continue;
378                 }
379
380                 if (fgets (buf, BUFSIZE, fh) == NULL)
381                 {
382                         syslog (LOG_NOTICE, "Unable to read from `%s': %s",
383                                         filename, strerror (errno));
384                         fclose (fh);
385                         continue;
386                 }
387
388                 fclose (fh);
389
390                 if (strsplit (buf, fields, BUFSIZE) < 3)
391                 {
392                         DBG ("Line has less than three fields.");
393                         continue;
394                 }
395
396                 switch (fields[2][0])
397                 {
398                         case 'R': running++;  break;
399                         case 'S': sleeping++; break;
400                         case 'D': blocked++;  break;
401                         case 'Z': zombies++;  break;
402                         case 'T': stopped++;  break;
403                         case 'W': paging++;   break;
404                 }
405         }
406
407         closedir (proc);
408
409         ps_submit (running, sleeping, zombies, stopped, paging, blocked);
410 #endif /* KERNEL_LINUX */
411 }
412 #else
413 # define ps_read NULL
414 #endif /* PROCESSES_HAVE_READ */
415
416 void module_register (void)
417 {
418         plugin_register (MODULE_NAME, ps_init, ps_read, ps_write);
419 }
420
421 #undef BUFSIZE
422 #undef MODULE_NAME