b1cb8ebb26685438965396b40ee9c02a474c66eb
[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 typedef struct procstat
96 {
97 #define PROCSTAT_NAME_LEN 256
98         char         name[PROCSTAT_NAME_LEN];
99         unsigned int num_proc;
100         unsigned int num_lwp;
101         unsigned int vmem_rss;
102         unsigned int vmem_minflt;
103         unsigned int vmem_majflt;
104         unsigned int cpu_user;
105         unsigned int cpu_system;
106         struct procstat *next;
107 } procstat_t;
108
109 #if HAVE_THREAD_INFO
110 static mach_port_t port_host_self;
111 static mach_port_t port_task_self;
112
113 static processor_set_name_array_t pset_list;
114 static mach_msg_type_number_t     pset_list_len;
115 /* #endif HAVE_THREAD_INFO */
116
117 #elif KERNEL_LINUX
118 /* No global variables */
119 #endif /* KERNEL_LINUX */
120
121 static void ps_list_add (procstat_t *list, procstat_t *entry)
122 {
123         procstat_t *ptr;
124
125         ptr = list;
126         while ((ptr != NULL) && (strcmp (ptr->name, entry->name) != 0))
127                 ptr = ptr->next;
128
129         if (ptr == NULL)
130                 return;
131
132         ptr->num_proc    += entry->num_proc;
133         ptr->num_lwp     += entry->num_lwp;
134         ptr->vmem_rss    += entry->vmem_rss;
135         ptr->vmem_minflt += entry->vmem_minflt;
136         ptr->vmem_maxflt += entry->vmem_maxflt;
137         ptr->cpu_user    += entry->cpu_user;
138         ptr->cpu_system  += entry->cpu_system;
139 }
140
141 static void ps_list_reset (procstat_t *ps)
142 {
143         while (ps != NULL)
144         {
145                 ps->num_proc    = 0;
146                 ps->num_lwp     = 0;
147                 ps->vmem_rss    = 0;
148                 ps->vmem_minflt = 0;
149                 ps->vmem_maxflt = 0;
150                 ps->cpu_user    = 0;
151                 ps->cpu_system  = 0;
152                 ps = ps->next;
153         }
154 }
155
156 static int ps_config (char *key, char *value)
157 {
158 }
159
160 static void ps_init (void)
161 {
162 #if HAVE_THREAD_INFO
163         kern_return_t status;
164
165         port_host_self = mach_host_self ();
166         port_task_self = mach_task_self ();
167
168         if (pset_list != NULL)
169         {
170                 vm_deallocate (port_task_self,
171                                 (vm_address_t) pset_list,
172                                 pset_list_len * sizeof (processor_set_t));
173                 pset_list = NULL;
174                 pset_list_len = 0;
175         }
176
177         if ((status = host_processor_sets (port_host_self,
178                                         &pset_list,
179                                         &pset_list_len)) != KERN_SUCCESS)
180         {
181                 syslog (LOG_ERR, "host_processor_sets failed: %s\n",
182                                 mach_error_string (status));
183                 pset_list = NULL;
184                 pset_list_len = 0;
185                 return;
186         }
187 /* #endif HAVE_THREAD_INFO */
188
189 #elif KERNEL_LINUX
190         /* No init */
191 #endif /* KERNEL_LINUX */
192
193         return;
194 }
195
196 static void ps_write (char *host, char *inst, char *val)
197 {
198         rrd_update_file (host, ps_file, val, ds_def, ds_num);
199 }
200
201 #if PROCESSES_HAVE_READ
202 static void ps_submit (int running,
203                 int sleeping,
204                 int zombies,
205                 int stopped,
206                 int paging,
207                 int blocked)
208 {
209         char buf[BUFSIZE];
210
211         if (snprintf (buf, BUFSIZE, "%u:%i:%i:%i:%i:%i:%i",
212                                 (unsigned int) curtime,
213                                 running, sleeping, zombies, stopped, paging,
214                                 blocked) >= BUFSIZE)
215                 return;
216
217         DBG ("running = %i; sleeping = %i; zombies = %i; stopped = %i; paging = %i; blocked = %i;",
218                         running, sleeping, zombies, stopped, paging, blocked);
219
220         plugin_submit (MODULE_NAME, "-", buf);
221 }
222
223 static void ps_read (void)
224 {
225 #if HAVE_THREAD_INFO
226         kern_return_t            status;
227
228         int                      pset;
229         processor_set_t          port_pset_priv;
230
231         int                      task;
232         task_array_t             task_list;
233         mach_msg_type_number_t   task_list_len;
234
235         int                      thread;
236         thread_act_array_t       thread_list;
237         mach_msg_type_number_t   thread_list_len;
238         thread_basic_info_data_t thread_data;
239         mach_msg_type_number_t   thread_data_len;
240
241         int running  = 0;
242         int sleeping = 0;
243         int zombies  = 0;
244         int stopped  = 0;
245         int blocked  = 0;
246
247         /*
248          * The Mach-concept is a little different from the traditional UNIX
249          * concept: All the work is done in threads. Threads are contained in
250          * `tasks'. Therefore, `task status' doesn't make much sense, since
251          * it's actually a `thread status'.
252          * Tasks are assigned to sets of processors, so that's where you go to
253          * get a list.
254          */
255         for (pset = 0; pset < pset_list_len; pset++)
256         {
257                 if ((status = host_processor_set_priv (port_host_self,
258                                                 pset_list[pset],
259                                                 &port_pset_priv)) != KERN_SUCCESS)
260                 {
261                         syslog (LOG_ERR, "host_processor_set_priv failed: %s\n",
262                                         mach_error_string (status));
263                         continue;
264                 }
265
266                 if ((status = processor_set_tasks (port_pset_priv,
267                                                 &task_list,
268                                                 &task_list_len)) != KERN_SUCCESS)
269                 {
270                         syslog (LOG_ERR, "processor_set_tasks failed: %s\n",
271                                         mach_error_string (status));
272                         mach_port_deallocate (port_task_self, port_pset_priv);
273                         continue;
274                 }
275
276                 for (task = 0; task < task_list_len; task++)
277                 {
278                         status = task_threads (task_list[task], &thread_list,
279                                         &thread_list_len);
280                         if (status != KERN_SUCCESS)
281                         {
282                                 /* Apple's `top' treats this case a zombie. It
283                                  * makes sense to some extend: A `zombie'
284                                  * thread is nonsense, since the task/process
285                                  * is dead. */
286                                 zombies++;
287                                 DBG ("task_threads failed: %s",
288                                                 mach_error_string (status));
289                                 if (task_list[task] != port_task_self)
290                                         mach_port_deallocate (port_task_self,
291                                                         task_list[task]);
292                                 continue; /* with next task_list */
293                         }
294
295                         for (thread = 0; thread < thread_list_len; thread++)
296                         {
297                                 thread_data_len = THREAD_BASIC_INFO_COUNT;
298                                 status = thread_info (thread_list[thread],
299                                                 THREAD_BASIC_INFO,
300                                                 (thread_info_t) &thread_data,
301                                                 &thread_data_len);
302                                 if (status != KERN_SUCCESS)
303                                 {
304                                         syslog (LOG_ERR, "thread_info failed: %s\n",
305                                                         mach_error_string (status));
306                                         if (task_list[task] != port_task_self)
307                                                 mach_port_deallocate (port_task_self,
308                                                                 thread_list[thread]);
309                                         continue; /* with next thread_list */
310                                 }
311
312                                 switch (thread_data.run_state)
313                                 {
314                                         case TH_STATE_RUNNING:
315                                                 running++;
316                                                 break;
317                                         case TH_STATE_STOPPED:
318                                         /* What exactly is `halted'? */
319                                         case TH_STATE_HALTED:
320                                                 stopped++;
321                                                 break;
322                                         case TH_STATE_WAITING:
323                                                 sleeping++;
324                                                 break;
325                                         case TH_STATE_UNINTERRUPTIBLE:
326                                                 blocked++;
327                                                 break;
328                                         /* There is no `zombie' case here,
329                                          * since there are no zombie-threads.
330                                          * There's only zombie tasks, which are
331                                          * handled above. */
332                                         default:
333                                                 syslog (LOG_WARNING,
334                                                                 "Unknown thread status: %s",
335                                                                 thread_data.run_state);
336                                                 break;
337                                 } /* switch (thread_data.run_state) */
338
339                                 if (task_list[task] != port_task_self)
340                                 {
341                                         status = mach_port_deallocate (port_task_self,
342                                                         thread_list[thread]);
343                                         if (status != KERN_SUCCESS)
344                                                 syslog (LOG_ERR, "mach_port_deallocate failed: %s",
345                                                                 mach_error_string (status));
346                                 }
347                         } /* for (thread_list) */
348
349                         if ((status = vm_deallocate (port_task_self,
350                                                         (vm_address_t) thread_list,
351                                                         thread_list_len * sizeof (thread_act_t)))
352                                         != KERN_SUCCESS)
353                         {
354                                 syslog (LOG_ERR, "vm_deallocate failed: %s",
355                                                 mach_error_string (status));
356                         }
357                         thread_list = NULL;
358                         thread_list_len = 0;
359
360                         /* Only deallocate the task port, if it isn't our own.
361                          * Don't know what would happen in that case, but this
362                          * is what Apple's top does.. ;) */
363                         if (task_list[task] != port_task_self)
364                         {
365                                 status = mach_port_deallocate (port_task_self,
366                                                 task_list[task]);
367                                 if (status != KERN_SUCCESS)
368                                         syslog (LOG_ERR, "mach_port_deallocate failed: %s",
369                                                         mach_error_string (status));
370                         }
371                 } /* for (task_list) */
372
373                 if ((status = vm_deallocate (port_task_self,
374                                 (vm_address_t) task_list,
375                                 task_list_len * sizeof (task_t))) != KERN_SUCCESS)
376                 {
377                         syslog (LOG_ERR, "vm_deallocate failed: %s",
378                                         mach_error_string (status));
379                 }
380                 task_list = NULL;
381                 task_list_len = 0;
382
383                 if ((status = mach_port_deallocate (port_task_self, port_pset_priv))
384                                 != KERN_SUCCESS)
385                 {
386                         syslog (LOG_ERR, "mach_port_deallocate failed: %s",
387                                         mach_error_string (status));
388                 }
389         } /* for (pset_list) */
390
391         ps_submit (running, sleeping, zombies, stopped, -1, blocked);
392 /* #endif HAVE_THREAD_INFO */
393
394 #elif KERNEL_LINUX
395         int running  = 0;
396         int sleeping = 0;
397         int zombies  = 0;
398         int stopped  = 0;
399         int paging   = 0;
400         int blocked  = 0;
401
402         char buf[BUFSIZE];
403         char filename[20]; /* need 17 bytes */
404         char *fields[BUFSIZE];
405
406         struct dirent *ent;
407         DIR *proc;
408         FILE *fh;
409
410         running = sleeping = zombies = stopped = paging = blocked = 0;
411
412         if ((proc = opendir ("/proc")) == NULL)
413         {
414                 syslog (LOG_ERR, "Cannot open `/proc': %s", strerror (errno));
415                 return;
416         }
417
418         while ((ent = readdir (proc)) != NULL)
419         {
420                 if (!isdigit (ent->d_name[0]))
421                         continue;
422
423                 if (snprintf (filename, 20, "/proc/%s/stat", ent->d_name) >= 20)
424                         continue;
425
426                 if ((fh = fopen (filename, "r")) == NULL)
427                 {
428                         syslog (LOG_NOTICE, "Cannot open `%s': %s", filename,
429                                         strerror (errno));
430                         continue;
431                 }
432
433                 if (fgets (buf, BUFSIZE, fh) == NULL)
434                 {
435                         syslog (LOG_NOTICE, "Unable to read from `%s': %s",
436                                         filename, strerror (errno));
437                         fclose (fh);
438                         continue;
439                 }
440
441                 fclose (fh);
442
443                 if (strsplit (buf, fields, BUFSIZE) < 3)
444                 {
445                         DBG ("Line has less than three fields.");
446                         continue;
447                 }
448
449                 switch (fields[2][0])
450                 {
451                         case 'R': running++;  break;
452                         case 'S': sleeping++; break;
453                         case 'D': blocked++;  break;
454                         case 'Z': zombies++;  break;
455                         case 'T': stopped++;  break;
456                         case 'W': paging++;   break;
457                 }
458         }
459
460         closedir (proc);
461
462         ps_submit (running, sleeping, zombies, stopped, paging, blocked);
463 #endif /* KERNEL_LINUX */
464 }
465 #else
466 # define ps_read NULL
467 #endif /* PROCESSES_HAVE_READ */
468
469 void module_register (void)
470 {
471         plugin_register (MODULE_NAME, ps_init, ps_read, ps_write);
472 }
473
474 #undef BUFSIZE
475 #undef MODULE_NAME