processes branch: A first version to collect more detailed process statistics..
[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         plugin_submit (MODULE_NAME, "-", buf);
218 }
219
220 static void ps_read (void)
221 {
222 #if HAVE_THREAD_INFO
223         kern_return_t            status;
224
225         int                      pset;
226         processor_set_t          port_pset_priv;
227
228         int                      task;
229         task_array_t             task_list;
230         mach_msg_type_number_t   task_list_len;
231
232         int                      thread;
233         thread_act_array_t       thread_list;
234         mach_msg_type_number_t   thread_list_len;
235         thread_basic_info_data_t thread_data;
236         mach_msg_type_number_t   thread_data_len;
237
238         int running  = 0;
239         int sleeping = 0;
240         int zombies  = 0;
241         int stopped  = 0;
242         int blocked  = 0;
243
244         /*
245          * The Mach-concept is a little different from the traditional UNIX
246          * concept: All the work is done in threads. Threads are contained in
247          * `tasks'. Therefore, `task status' doesn't make much sense, since
248          * it's actually a `thread status'.
249          * Tasks are assigned to sets of processors, so that's where you go to
250          * get a list.
251          */
252         for (pset = 0; pset < pset_list_len; pset++)
253         {
254                 if ((status = host_processor_set_priv (port_host_self,
255                                                 pset_list[pset],
256                                                 &port_pset_priv)) != KERN_SUCCESS)
257                 {
258                         syslog (LOG_ERR, "host_processor_set_priv failed: %s\n",
259                                         mach_error_string (status));
260                         continue;
261                 }
262
263                 if ((status = processor_set_tasks (port_pset_priv,
264                                                 &task_list,
265                                                 &task_list_len)) != KERN_SUCCESS)
266                 {
267                         syslog (LOG_ERR, "processor_set_tasks failed: %s\n",
268                                         mach_error_string (status));
269                         mach_port_deallocate (port_task_self, port_pset_priv);
270                         continue;
271                 }
272
273                 for (task = 0; task < task_list_len; task++)
274                 {
275                         status = task_threads (task_list[task], &thread_list,
276                                         &thread_list_len);
277                         if (status != KERN_SUCCESS)
278                         {
279                                 /* Apple's `top' treats this case a zombie. It
280                                  * makes sense to some extend: A `zombie'
281                                  * thread is nonsense, since the task/process
282                                  * is dead. */
283                                 zombies++;
284                                 DBG ("task_threads failed: %s",
285                                                 mach_error_string (status));
286                                 if (task_list[task] != port_task_self)
287                                         mach_port_deallocate (port_task_self,
288                                                         task_list[task]);
289                                 continue; /* with next task_list */
290                         }
291
292                         for (thread = 0; thread < thread_list_len; thread++)
293                         {
294                                 thread_data_len = THREAD_BASIC_INFO_COUNT;
295                                 status = thread_info (thread_list[thread],
296                                                 THREAD_BASIC_INFO,
297                                                 (thread_info_t) &thread_data,
298                                                 &thread_data_len);
299                                 if (status != KERN_SUCCESS)
300                                 {
301                                         syslog (LOG_ERR, "thread_info failed: %s\n",
302                                                         mach_error_string (status));
303                                         if (task_list[task] != port_task_self)
304                                                 mach_port_deallocate (port_task_self,
305                                                                 thread_list[thread]);
306                                         continue; /* with next thread_list */
307                                 }
308
309                                 switch (thread_data.run_state)
310                                 {
311                                         case TH_STATE_RUNNING:
312                                                 running++;
313                                                 break;
314                                         case TH_STATE_STOPPED:
315                                         /* What exactly is `halted'? */
316                                         case TH_STATE_HALTED:
317                                                 stopped++;
318                                                 break;
319                                         case TH_STATE_WAITING:
320                                                 sleeping++;
321                                                 break;
322                                         case TH_STATE_UNINTERRUPTIBLE:
323                                                 blocked++;
324                                                 break;
325                                         /* There is no `zombie' case here,
326                                          * since there are no zombie-threads.
327                                          * There's only zombie tasks, which are
328                                          * handled above. */
329                                         default:
330                                                 syslog (LOG_WARNING,
331                                                                 "Unknown thread status: %s",
332                                                                 thread_data.run_state);
333                                                 break;
334                                 } /* switch (thread_data.run_state) */
335
336                                 if (task_list[task] != port_task_self)
337                                 {
338                                         status = mach_port_deallocate (port_task_self,
339                                                         thread_list[thread]);
340                                         if (status != KERN_SUCCESS)
341                                                 syslog (LOG_ERR, "mach_port_deallocate failed: %s",
342                                                                 mach_error_string (status));
343                                 }
344                         } /* for (thread_list) */
345
346                         if ((status = vm_deallocate (port_task_self,
347                                                         (vm_address_t) thread_list,
348                                                         thread_list_len * sizeof (thread_act_t)))
349                                         != KERN_SUCCESS)
350                         {
351                                 syslog (LOG_ERR, "vm_deallocate failed: %s",
352                                                 mach_error_string (status));
353                         }
354                         thread_list = NULL;
355                         thread_list_len = 0;
356
357                         /* Only deallocate the task port, if it isn't our own.
358                          * Don't know what would happen in that case, but this
359                          * is what Apple's top does.. ;) */
360                         if (task_list[task] != port_task_self)
361                         {
362                                 status = mach_port_deallocate (port_task_self,
363                                                 task_list[task]);
364                                 if (status != KERN_SUCCESS)
365                                         syslog (LOG_ERR, "mach_port_deallocate failed: %s",
366                                                         mach_error_string (status));
367                         }
368                 } /* for (task_list) */
369
370                 if ((status = vm_deallocate (port_task_self,
371                                 (vm_address_t) task_list,
372                                 task_list_len * sizeof (task_t))) != KERN_SUCCESS)
373                 {
374                         syslog (LOG_ERR, "vm_deallocate failed: %s",
375                                         mach_error_string (status));
376                 }
377                 task_list = NULL;
378                 task_list_len = 0;
379
380                 if ((status = mach_port_deallocate (port_task_self, port_pset_priv))
381                                 != KERN_SUCCESS)
382                 {
383                         syslog (LOG_ERR, "mach_port_deallocate failed: %s",
384                                         mach_error_string (status));
385                 }
386         } /* for (pset_list) */
387
388         ps_submit (running, sleeping, zombies, stopped, -1, blocked);
389 /* #endif HAVE_THREAD_INFO */
390
391 #elif KERNEL_LINUX
392         int running  = 0;
393         int sleeping = 0;
394         int zombies  = 0;
395         int stopped  = 0;
396         int paging   = 0;
397         int blocked  = 0;
398
399         char buf[BUFSIZE];
400         char filename[20]; /* need 17 bytes */
401         char *fields[BUFSIZE];
402
403         struct dirent *ent;
404         DIR *proc;
405         FILE *fh;
406
407         running = sleeping = zombies = stopped = paging = blocked = 0;
408
409         if ((proc = opendir ("/proc")) == NULL)
410         {
411                 syslog (LOG_ERR, "Cannot open `/proc': %s", strerror (errno));
412                 return;
413         }
414
415         while ((ent = readdir (proc)) != NULL)
416         {
417                 if (!isdigit (ent->d_name[0]))
418                         continue;
419
420                 if (snprintf (filename, 20, "/proc/%s/stat", ent->d_name) >= 20)
421                         continue;
422
423                 if ((fh = fopen (filename, "r")) == NULL)
424                 {
425                         syslog (LOG_ERR, "Cannot open `%s': %s", filename, strerror (errno));
426                         continue;
427                 }
428
429                 if (fgets (buf, BUFSIZE, fh) == NULL)
430                 {
431                         fclose (fh);
432                         continue;
433                 }
434
435                 fclose (fh);
436
437                 if (strsplit (buf, fields, BUFSIZE) < 3)
438                         continue;
439
440                 switch (fields[2][0])
441                 {
442                         case 'R': running++;  break;
443                         case 'S': sleeping++; break;
444                         case 'D': blocked++;  break;
445                         case 'Z': zombies++;  break;
446                         case 'T': stopped++;  break;
447                         case 'W': paging++;   break;
448                 }
449         }
450
451         closedir(proc);
452
453         ps_submit (running, sleeping, zombies, stopped, paging, blocked);
454 #endif /* KERNEL_LINUX */
455 }
456 #else
457 # define ps_read NULL
458 #endif /* PROCESSES_HAVE_READ */
459
460 void module_register (void)
461 {
462         plugin_register (MODULE_NAME, ps_init, ps_read, ps_write);
463 }
464
465 #undef BUFSIZE
466 #undef MODULE_NAME