Debian Codegraph @

gmond/modules/cpu/mod_multicpu.c

raw
1 /*******************************************************************************
2 * Portions Copyright (C) 2007 Novell, Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * - Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 *
10 * - Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * - Neither the name of Novell, Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from this
16 * software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL Novell, Inc. OR THE CONTRIBUTORS
22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 *
30 * Author: Brad Nicholes (bnicholes novell.com)
31 ******************************************************************************/
32
33 #include <gm_metric.h>
34
35 #include <stdio.h>
36 #include <time.h>
37 #include <string.h>
38 #include <stdlib.h>
39
40 #include "gm_file.h"
41 #include "libmetrics.h"
42
43 #include <apr_tables.h>
44 #include <apr_strings.h>
45
46 /*
47 * Declare ourselves so the configuration routines can find and know us.
48 * We'll fill it in at the end of the module.
49 */
50 mmodule multicpu_module;
51
52 static timely_file proc_stat = { {0,0} , 1., "/proc/stat", NULL, BUFFSIZE };
53
54 struct cpu_util {
55 g_val_t val;
56 struct timeval stamp;
57 double last_jiffies;
58 double curr_jiffies;
59 double last_total_jiffies;
60 double curr_total_jiffies;
61 double diff;
62 };
63 typedef struct cpu_util cpu_util;
64
65
66 #define NUM_CPUSTATES_24X 4
67 #define NUM_CPUSTATES_26X 7
68 static unsigned int num_cpustates;
69 static unsigned int cpu_count = 0;
70 static apr_pool_t *pool;
71 static cpu_util *cpu_user = NULL;
72 static cpu_util *cpu_nice = NULL;
73 static cpu_util *cpu_system = NULL;
74 static cpu_util *cpu_idle = NULL;
75 static cpu_util *cpu_wio = NULL;
76 static cpu_util *cpu_intr = NULL;
77 static cpu_util *cpu_sintr = NULL;
78 static cpu_util *cpu_steal = NULL;
79
80 /*
81 * A helper function to determine the number of cpustates in /proc/stat (MKN)
82 * Count how many cpus there are in the system
83 */
84 static void init_cpu_info (void)
85 {
86 char *p;
87 unsigned int i=0;
88
89 proc_stat.last_read.tv_sec=0;
90 proc_stat.last_read.tv_usec=0;
91 p = update_file(&proc_stat);
92 proc_stat.last_read.tv_sec=0;
93 proc_stat.last_read.tv_usec=0;
94
95 /*
96 ** Skip initial "cpu" token
97 */
98 p = skip_token(p);
99 p = skip_whitespace(p);
100
101 /*
102 ** Loop over file until next "cpu" token is found.
103 ** i=4 : Linux 2.4.x
104 ** i=7 : Linux 2.6.x
105 */
106 while (strncmp(p,"cpu",3)) {
107 p = skip_token(p);
108 p = skip_whitespace(p);
109 i++;
110 }
111 num_cpustates = i;
112
113 for (i=1; strlen(p) > 0;) {
114 p = skip_token(p);
115 p = skip_whitespace(p);
116 if (strncmp(p,"cpu",3) == 0) {
117 i++;
118 }
119 }
120 cpu_count = i;
121
122 return;
123 }
124
125 /*
126 * Get the metric name and index from the original
127 * metric name
128 */
129 static void get_metric_name_cpu (char *metric, char *name, int *index)
130 {
131 /* The metric name contains both the name and the cpu id.
132 Split the cpu id from the name so that we can use it to
133 decide which metric handler to call and for which cpu.
134 */
135 size_t numIndex = strcspn (metric, "0123456789");
136
137 if (numIndex > 0) {
138 strncpy (name, metric, numIndex);
139 name[numIndex] = '\0';
140 *index = atoi(&metric[numIndex]);
141 } else {
142 *name = '\0';
143 *index = 0;
144 }
145
146 return;
147 }
148
149 static double total_jiffies_func (char *p);
150
151 /*
152 * Find the correct cpu info with the buffer,
153 * given a cpu index
154 */
155 static char *find_cpu (char *p, int cpu_index, double *total_jiffies)
156 {
157 int i;
158
159 /*
160 ** Skip initial "cpu" token
161 */
162 p = skip_token(p);
163 p = skip_whitespace(p);
164
165 for (i=0; i<=cpu_index; i++) {
166 while (strlen(p) > 0) {
167 p = skip_token(p);
168 p = skip_whitespace(p);
169 if (strncmp(p,"cpu",3) == 0) {
170 break;
171 }
172 }
173 }
174
175 /*
176 ** Skip last "cpu" token
177 */
178 p = skip_token(p);
179 p = skip_whitespace(p);
180
181 *total_jiffies = total_jiffies_func(p);
182
183 return p;
184 }
185
186 /*
187 * A helper function to return the total number of cpu jiffies
188 * Assumes that "p" has been aligned already to the CPU line to evaluate
189 */
190 static double total_jiffies_func (char *p)
191 {
192 unsigned long user_jiffies, nice_jiffies, system_jiffies, idle_jiffies,
193 wio_jiffies, irq_jiffies, sirq_jiffies, steal_jiffies;
194
195 user_jiffies = strtod( p, &p );
196 p = skip_whitespace(p);
197 nice_jiffies = strtod( p, &p );
198 p = skip_whitespace(p);
199 system_jiffies = strtod( p , &p );
200 p = skip_whitespace(p);
201 idle_jiffies = strtod( p , &p );
202
203 if (num_cpustates == NUM_CPUSTATES_24X)
204 return user_jiffies + nice_jiffies + system_jiffies + idle_jiffies;
205
206 p = skip_whitespace(p);
207 wio_jiffies = strtod( p , &p );
208 p = skip_whitespace(p);
209 irq_jiffies = strtod( p , &p );
210 p = skip_whitespace(p);
211 sirq_jiffies = strtod( p , &p );
212
213 if (num_cpustates == NUM_CPUSTATES_26X)
214 return user_jiffies + nice_jiffies + system_jiffies + idle_jiffies +
215 wio_jiffies + irq_jiffies + sirq_jiffies;
216
217 p = skip_whitespace(p);
218 steal_jiffies = strtod( p , &p );
219 return user_jiffies + nice_jiffies + system_jiffies + idle_jiffies +
220 wio_jiffies + irq_jiffies + sirq_jiffies + steal_jiffies;
221 }
222
223 /* Common function to calculate the utilization */
224 static void calculate_utilization (char *p, cpu_util *cpu)
225 {
226 cpu->curr_jiffies = strtod(p, (char **)NULL);
227
228 cpu->diff = (cpu->curr_jiffies - cpu->last_jiffies);
229
230 if (cpu->diff)
231 cpu->val.f = (cpu->diff/(cpu->curr_total_jiffies - cpu->last_total_jiffies))*100;
232 else
233 cpu->val.f = 0.0;
234
235 cpu->last_jiffies = cpu->curr_jiffies;
236 cpu->last_total_jiffies = cpu->curr_total_jiffies;
237
238 return;
239 }
240
241 static g_val_t multi_cpu_user_func (int cpu_index)
242 {
243 char *p;
244 cpu_util *cpu = &(cpu_user[cpu_index]);
245
246 p = update_file(&proc_stat);
247 if((proc_stat.last_read.tv_sec != cpu->stamp.tv_sec) &&
248 (proc_stat.last_read.tv_usec != cpu->stamp.tv_usec)) {
249 cpu->stamp = proc_stat.last_read;
250
251 p = find_cpu (p, cpu_index, &cpu->curr_total_jiffies);
252 calculate_utilization (p, cpu);
253 }
254
255 return cpu->val;
256 }
257
258 static g_val_t multi_cpu_nice_func (int cpu_index)
259 {
260 char *p;
261 cpu_util *cpu = &(cpu_nice[cpu_index]);
262
263 p = update_file(&proc_stat);
264 if((proc_stat.last_read.tv_sec != cpu->stamp.tv_sec) &&
265 (proc_stat.last_read.tv_usec != cpu->stamp.tv_usec)) {
266 cpu->stamp = proc_stat.last_read;
267
268 p = find_cpu (p, cpu_index, &cpu->curr_total_jiffies);
269 p = skip_token(p);
270 p = skip_whitespace(p);
271
272 calculate_utilization (p, cpu);
273 }
274
275 return cpu->val;
276 }
277
278 static g_val_t multi_cpu_system_func (int cpu_index)
279 {
280 char *p;
281 cpu_util *cpu = &(cpu_system[cpu_index]);
282
283 p = update_file(&proc_stat);
284 if((proc_stat.last_read.tv_sec != cpu->stamp.tv_sec) &&
285 (proc_stat.last_read.tv_usec != cpu->stamp.tv_usec)) {
286 cpu->stamp = proc_stat.last_read;
287
288 p = find_cpu (p, cpu_index, &cpu->curr_total_jiffies);
289 p = skip_token(p);
290 p = skip_token(p);
291 p = skip_whitespace(p);
292 cpu->curr_jiffies = strtod(p , (char **)NULL);
293 if (num_cpustates > NUM_CPUSTATES_24X) {
294 p = skip_token(p);
295 p = skip_token(p);
296 p = skip_token(p);
297 p = skip_whitespace(p);
298
299 cpu->curr_jiffies += strtod(p , (char **)NULL); /* "intr" counted in system */
300 p = skip_token(p);
301 cpu->curr_jiffies += strtod(p , (char **)NULL); /* "sintr" counted in system */
302 }
303
304 cpu->diff = cpu->curr_jiffies - cpu->last_jiffies;
305
306 if (cpu->diff)
307 cpu->val.f = (cpu->diff/(cpu->curr_total_jiffies - cpu->last_total_jiffies))*100;
308 else
309 cpu->val.f = 0.0;
310
311 cpu->last_jiffies = cpu->curr_jiffies;
312 cpu->last_total_jiffies = cpu->curr_total_jiffies;
313 }
314
315 return cpu->val;
316 }
317
318 static g_val_t multi_cpu_idle_func (int cpu_index)
319 {
320 char *p;
321 cpu_util *cpu = &(cpu_idle[cpu_index]);
322
323 p = update_file(&proc_stat);
324 if((proc_stat.last_read.tv_sec != cpu->stamp.tv_sec) &&
325 (proc_stat.last_read.tv_usec != cpu->stamp.tv_usec)) {
326 cpu->stamp = proc_stat.last_read;
327
328 p = find_cpu (p, cpu_index, &cpu->curr_total_jiffies);
329 p = skip_token(p);
330 p = skip_token(p);
331 p = skip_token(p);
332 p = skip_whitespace(p);
333
334 calculate_utilization (p, cpu);
335 }
336
337 return cpu->val;
338 }
339
340 static g_val_t multi_cpu_wio_func (int cpu_index)
341 {
342 char *p;
343 cpu_util *cpu = &(cpu_wio[cpu_index]);
344
345 if (num_cpustates == NUM_CPUSTATES_24X) {
346 cpu->val.f = 0.;
347 return cpu->val;
348 }
349
350 p = update_file(&proc_stat);
351 if((proc_stat.last_read.tv_sec != cpu->stamp.tv_sec) &&
352 (proc_stat.last_read.tv_usec != cpu->stamp.tv_usec)) {
353 cpu->stamp = proc_stat.last_read;
354
355 p = find_cpu (p, cpu_index, &cpu->curr_total_jiffies);
356 p = skip_token(p);
357 p = skip_token(p);
358 p = skip_token(p);
359 p = skip_token(p);
360 p = skip_whitespace(p);
361
362 calculate_utilization (p, cpu);
363 }
364
365 return cpu->val;
366 }
367
368 static g_val_t multi_cpu_intr_func (int cpu_index)
369 {
370 char *p;
371 cpu_util *cpu = &(cpu_intr[cpu_index]);
372
373 if (num_cpustates == NUM_CPUSTATES_24X) {
374 cpu->val.f = 0.;
375 return cpu->val;
376 }
377
378 p = update_file(&proc_stat);
379 if((proc_stat.last_read.tv_sec != cpu->stamp.tv_sec) &&
380 (proc_stat.last_read.tv_usec != cpu->stamp.tv_usec)) {
381 cpu->stamp = proc_stat.last_read;
382
383 p = find_cpu (p, cpu_index, &cpu->curr_total_jiffies);
384 p = skip_token(p);
385 p = skip_token(p);
386 p = skip_token(p);
387 p = skip_token(p);
388 p = skip_token(p);
389 p = skip_whitespace(p);
390
391 calculate_utilization (p, cpu);
392 }
393
394 return cpu->val;
395 }
396
397 static g_val_t multi_cpu_sintr_func (int cpu_index)
398 {
399 char *p;
400 cpu_util *cpu = &(cpu_sintr[cpu_index]);
401
402 if (num_cpustates == NUM_CPUSTATES_24X) {
403 cpu->val.f = 0.;
404 return cpu->val;
405 }
406
407 p = update_file(&proc_stat);
408 if((proc_stat.last_read.tv_sec != cpu->stamp.tv_sec) &&
409 (proc_stat.last_read.tv_usec != cpu->stamp.tv_usec)) {
410 cpu->stamp = proc_stat.last_read;
411
412 p = find_cpu (p, cpu_index, &cpu->curr_total_jiffies);
413 p = skip_token(p);
414 p = skip_token(p);
415 p = skip_token(p);
416 p = skip_token(p);
417 p = skip_token(p);
418 p = skip_token(p);
419 p = skip_whitespace(p);
420
421 calculate_utilization (p, cpu);
422 }
423
424 return cpu->val;
425 }
426
427 static apr_array_header_t *metric_info = NULL;
428
429 /* Initialize the give metric by allocating the per metric data
430 structure and inserting a metric definition for each cpu found
431 */
432 static cpu_util *init_metric (apr_pool_t *p, apr_array_header_t *ar, int cpu_count, char *name, char *desc)
433 {
434 int i;
435 Ganglia_25metric *gmi;
436 cpu_util *cpu;
437
438 cpu = apr_pcalloc (p, sizeof(cpu_util)*cpu_count);
439
440 for (i=0; i<cpu_count; i++) {
441 gmi = apr_array_push(ar);
442
443 /* gmi->key will be automatically assigned by gmond */
444 gmi->name = apr_psprintf (p, "%s%d", name, i);
445 gmi->tmax = 90;
446 gmi->type = GANGLIA_VALUE_FLOAT;
447 gmi->units = apr_pstrdup(p, "%");
448 gmi->slope = apr_pstrdup(p, "both");
449 gmi->fmt = apr_pstrdup(p, "%.1f");
450 gmi->msg_size = UDP_HEADER_SIZE+8;
451 gmi->desc = apr_pstrdup(p, desc);
452 }
453
454 return cpu;
455 }
456
457 static g_val_t multi_cpu_steal_func (int cpu_index)
458 {
459 char *p;
460 cpu_util *cpu = &(cpu_user[cpu_index]);
461
462 p = update_file(&proc_stat);
463 if((proc_stat.last_read.tv_sec != cpu->stamp.tv_sec) &&
464 (proc_stat.last_read.tv_usec != cpu->stamp.tv_usec)) {
465 cpu->stamp = proc_stat.last_read;
466
467 p = find_cpu (p, cpu_index, &cpu->curr_total_jiffies);
468 p = skip_token(p);
469 p = skip_token(p);
470 p = skip_token(p);
471 p = skip_token(p);
472 p = skip_token(p);
473 p = skip_token(p);
474 p = skip_token(p);
475 p = skip_whitespace(p);
476 calculate_utilization (p, cpu);
477 }
478
479 return cpu->val;
480 }
481
482 static int ex_metric_init (apr_pool_t *p)
483 {
484 int i;
485 Ganglia_25metric *gmi;
486
487 init_cpu_info ();
488
489 /* Allocate a pool that will be used by this module */
490 apr_pool_create(&pool, p);
491
492 metric_info = apr_array_make(pool, 2, sizeof(Ganglia_25metric));
493
494 /* Initialize each metric */
495 cpu_user = init_metric (pool, metric_info, cpu_count, "multicpu_user",
496 "Percentage of CPU utilization that occurred while "
497 "executing at the user level");
498 cpu_nice = init_metric (pool, metric_info, cpu_count, "multicpu_nice",
499 "Percentage of CPU utilization that occurred while "
500 "executing at the nice level");
501 cpu_system = init_metric (pool, metric_info, cpu_count, "multicpu_system",
502 "Percentage of CPU utilization that occurred while "
503 "executing at the system level");
504 cpu_idle = init_metric (pool, metric_info, cpu_count, "multicpu_idle",
505 "Percentage of CPU utilization that occurred while "
506 "executing at the idle level");
507 cpu_wio = init_metric (pool, metric_info, cpu_count, "multicpu_wio",
508 "Percentage of CPU utilization that occurred while "
509 "executing at the wio level");
510 cpu_intr = init_metric (pool, metric_info, cpu_count, "multicpu_intr",
511 "Percentage of CPU utilization that occurred while "
512 "executing at the intr level");
513 cpu_sintr = init_metric (pool, metric_info, cpu_count, "multicpu_sintr",
514 "Percentage of CPU utilization that occurred while "
515 "executing at the sintr level");
516 cpu_steal = init_metric (pool, metric_info, cpu_count, "multicpu_steal",
517 "Percentage of CPU preempted by the hypervisor");
518
519 /* Add a terminator to the array and replace the empty static metric definition
520 array with the dynamic array that we just created
521 */
522 gmi = apr_array_push(metric_info);
523 memset (gmi, 0, sizeof(*gmi));
524
525 multicpu_module.metrics_info = (Ganglia_25metric *)metric_info->elts;
526
527 for (i = 0; multicpu_module.metrics_info[i].name != NULL; i++) {
528 /* Initialize the metadata storage for each of the metrics and then
529 * store one or more key/value pairs. The define MGROUPS defines
530 * the key for the grouping attribute. */
531 MMETRIC_INIT_METADATA(&(multicpu_module.metrics_info[i]),p);
532 MMETRIC_ADD_METADATA(&(multicpu_module.metrics_info[i]),MGROUP,"cpu");
533 }
534
535 return 0;
536 }
537
538 static void ex_metric_cleanup ( void )
539 {
540 }
541
542 static g_val_t ex_metric_handler ( int metric_index )
543 {
544 g_val_t val;
545 int index;
546 char name[64];
547
548 /* Get the metric name and cpu index from the combined
549 name that was passed in
550 */
551 get_metric_name_cpu (multicpu_module.metrics_info[metric_index].name, name, &index);
552
553 if (strcmp(name, "multicpu_user") == 0)
554 return multi_cpu_user_func(index);
555
556 if (strcmp(name, "multicpu_nice") == 0)
557 return multi_cpu_nice_func(index);
558
559 if (strcmp(name, "multicpu_system") == 0)
560 return multi_cpu_system_func(index);
561
562 if (strcmp(name, "multicpu_idle") == 0)
563 return multi_cpu_idle_func(index);
564
565 if (strcmp(name, "multicpu_wio") == 0)
566 return multi_cpu_wio_func(index);
567
568 if (strcmp(name, "multicpu_intr") == 0)
569 return multi_cpu_intr_func(index);
570
571 if (strcmp(name, "multicpu_sintr") == 0)
572 return multi_cpu_sintr_func(index);
573
574 if (strcmp(name, "multicpu_steal") == 0)
575 return multi_cpu_steal_func(index);
576
577 /* default case */
578 val.f = 0;
579 return val;
580 }
581
582 mmodule multicpu_module =
583 {
584 STD_MMODULE_STUFF,
585 ex_metric_init,
586 ex_metric_cleanup,
587 NULL, /* defined dynamically */
588 ex_metric_handler,
589 };