| 1 |
// Copyright (C) 2013 Red Hat Inc., Aaron Tomlin <[email protected]> |
| 2 |
// |
| 3 |
// This file is part of systemtap, and is free software. You can |
| 4 |
// redistribute it and/or modify it under the terms of the GNU General |
| 5 |
// Public License (GPL); either version 2, or (at your option) any |
| 6 |
// later version. |
| 7 |
|
| 8 |
// <tapsetdescription> |
| 9 |
// Functions in the loadavg tapset allow a probe handler to capture |
| 10 |
// the load average. |
| 11 |
// </tapsetdescription> |
| 12 |
|
| 13 |
|
| 14 |
%{ |
| 15 |
#include <linux/sched.h> |
| 16 |
#if defined(STAPCONF_LINUX_SCHED_HEADERS) |
| 17 |
#include <linux/sched/loadavg.h> |
| 18 |
#endif |
| 19 |
|
| 20 |
#define LOAD_INT(x) ((x) >> FSHIFT) |
| 21 |
#define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100) |
| 22 |
%} |
| 23 |
|
| 24 |
/** |
| 25 |
* sfunction get_loadavg_index - Get the load average for a specified interval |
| 26 |
* @indx: The load average interval to capture. |
| 27 |
* |
| 28 |
* Description: This function returns the load average at a specified interval. |
| 29 |
* The three load average values 1, 5 and 15 minute average corresponds to |
| 30 |
* indexes 0, 1 and 2 of the avenrun array - see linux/sched.h. |
| 31 |
* Please note that the truncated-integer portion of the load average is returned. |
| 32 |
* If the specified index is out-of-bounds, then an error message and exception is |
| 33 |
* thrown. |
| 34 |
*/ |
| 35 |
function get_loadavg_index:long (indx:long) %{ /* pure */ |
| 36 |
int value; |
| 37 |
|
| 38 |
if (STAP_ARG_indx < 0 || STAP_ARG_indx > 2) |
| 39 |
STAP_ERROR("Invalid loadavg index %lld specified.", |
| 40 |
STAP_ARG_indx); |
| 41 |
|
| 42 |
value = avenrun[STAP_ARG_indx] + (FIXED_1/200); |
| 43 |
|
| 44 |
STAP_RETVALUE = LOAD_INT(value); |
| 45 |
%} |
| 46 |
|
| 47 |
/** |
| 48 |
* sfunction sprint_loadavg - Report a pretty-printed load average |
| 49 |
* |
| 50 |
* Description: Returns the a string with three decimal numbers |
| 51 |
* in the usual format for 1-, 5- and 15-minute load averages. |
| 52 |
*/ |
| 53 |
function sprint_loadavg:string () %{ /* pure */ /* stable */ |
| 54 |
int a, b, c; |
| 55 |
|
| 56 |
a = avenrun[0] + (FIXED_1/200); |
| 57 |
b = avenrun[1] + (FIXED_1/200); |
| 58 |
c = avenrun[2] + (FIXED_1/200); |
| 59 |
|
| 60 |
snprintf(STAP_RETVALUE, MAXSTRINGLEN, |
| 61 |
"%d.%02d %d.%02d %d.%02d\n", |
| 62 |
LOAD_INT(a), LOAD_FRAC(a), |
| 63 |
LOAD_INT(b), LOAD_FRAC(b), |
| 64 |
LOAD_INT(c), LOAD_FRAC(c)); |
| 65 |
%} |