New plugin - lpar
[collectd.git] / src / lpar.c
1 /**
2  * collectd - src/lpar.c
3  * Copyright (C) 2010  AurĂ©lien Reynaud
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; only version 2 of the License is applicable.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  * Authors:
19  *   Aurelien Reynaud <collectd at wattapower.net>
20  **/
21
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25 #include <sys/protosw.h>
26 #include <libperfstat.h>
27 #include <sys/systemcfg.h>
28 #include <sys/utsname.h>
29
30 #ifndef XINTFRAC
31 # define XINTFRAC ((double)(_system_configuration.Xint) / \
32                    (double)(_system_configuration.Xfrac))
33 #endif
34
35 /* Max length of the type instance string */
36 #define TYPE_INST_LEN (sizeof("lpar--total") + 2*sizeof(int) + 1)
37
38 static const char *config_keys[] =
39 {
40   "CpuPoolStats"
41 };
42 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
43 static int pool_stats = 0;
44
45 /* As an LPAR can be moved transparently across physical systems
46  * through Live Partition Mobility (LPM), and the resources we are
47  * monitoring are tied to the underlying hardware, we need to keep
48  * track on which physical server we are currently on. This is done
49  * through the plugin instance which holds the chassis' serial.
50  */
51 static char plugin_inst[SYS_NMLN];
52
53 static u_longlong_t last_time_base;
54 static u_longlong_t last_pcpu_user,
55                     last_pcpu_sys,
56                     last_pcpu_idle,
57                     last_pcpu_wait;
58 static u_longlong_t last_pool_idle_time = 0;
59 static u_longlong_t last_idle_donated_purr = 0,
60                     last_busy_donated_purr = 0,
61                     last_busy_stolen_purr = 0,
62                     last_idle_stolen_purr = 0;
63 static int donate_flag = 0;
64
65
66 /* Save the current values for the next iteration */
67 static void save_last_values (perfstat_partition_total_t *lparstats)
68 {
69         last_time_base = lparstats->timebase_last;
70
71         last_pcpu_user = lparstats->puser;
72         last_pcpu_sys  = lparstats->psys;
73         last_pcpu_idle = lparstats->pidle;
74         last_pcpu_wait = lparstats->pwait;
75
76         if (donate_flag)
77         {
78                 last_idle_donated_purr = lparstats->idle_donated_purr;
79                 last_busy_donated_purr = lparstats->busy_donated_purr;
80                 last_busy_stolen_purr  = lparstats->busy_stolen_purr;
81                 last_idle_stolen_purr  = lparstats->idle_stolen_purr;
82         }
83
84         last_pool_idle_time = lparstats->pool_idle_time;
85 }
86
87 static int lpar_config (const char *key, const char *value)
88 {
89         if (strcasecmp ("CpuPoolStats", key) == 0)
90         {
91                 if (IS_TRUE (value))
92                         pool_stats = 1;
93                 else
94                         pool_stats = 0;
95         }
96         else
97         {
98                 return (-1);
99         }
100
101         return (0);
102 } /* int lpar_config */
103
104 static int lpar_init (void)
105 {
106         perfstat_partition_total_t lparstats;
107
108         /* retrieve the initial metrics */
109         if (!perfstat_partition_total (NULL, &lparstats,
110                                        sizeof (perfstat_partition_total_t), 1))
111         {
112                 ERROR ("lpar plugin: perfstat_partition_total failed.");
113                 return (-1);
114         }
115
116         if (!lparstats.type.b.shared_enabled && lparstats.type.b.donate_enabled)
117         {
118                 donate_flag = 1;
119         }
120
121         /* save the initial data */
122         save_last_values (&lparstats);
123
124         return (0);
125 } /* int lpar_init */
126
127 static void lpar_submit (const char *type_instance, double value)
128 {
129         value_t values[1];
130         value_list_t vl = VALUE_LIST_INIT;
131
132         values[0].gauge = (gauge_t)value;
133
134         vl.values = values;
135         vl.values_len = 1;
136         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
137         sstrncpy (vl.plugin, "lpar", sizeof (vl.plugin));
138         sstrncpy (vl.plugin_instance, plugin_inst, sizeof (vl.plugin));
139         sstrncpy (vl.type, "lpar_pcpu", sizeof (vl.type));
140         sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
141
142         plugin_dispatch_values (&vl);
143 }
144
145 static int lpar_read (void)
146 {
147         u_longlong_t dlt_pcpu_user, dlt_pcpu_sys, dlt_pcpu_idle, dlt_pcpu_wait;
148         u_longlong_t delta_time_base;
149         perfstat_partition_total_t lparstats;
150         struct utsname name;
151
152         /* retrieve the current physical server's id and build the plugin
153            instance's name */
154         if (uname (&name) != 0)
155         {
156                 ERROR ("lpar plugin: uname failed.");
157                 return (-1);
158         }
159         sstrncpy (plugin_inst, name.machine, sizeof (plugin_inst));
160
161         /* retrieve the current metrics */
162         if (!perfstat_partition_total (NULL, &lparstats,
163                                        sizeof (perfstat_partition_total_t), 1))
164         {
165                 ERROR ("lpar plugin: perfstat_partition_total failed.");
166                 return (-1);
167         }
168
169         delta_time_base = lparstats.timebase_last - last_time_base;
170         if (delta_time_base == 0)
171         {
172                 /* The system stats have not been updated since last time */
173                 return (0);
174         }
175
176         dlt_pcpu_user = lparstats.puser - last_pcpu_user;
177         dlt_pcpu_sys  = lparstats.psys  - last_pcpu_sys;
178         dlt_pcpu_idle = lparstats.pidle - last_pcpu_idle;
179         dlt_pcpu_wait = lparstats.pwait - last_pcpu_wait;
180
181         lpar_submit ("user", (double)dlt_pcpu_user / delta_time_base);
182         lpar_submit ("sys",  (double)dlt_pcpu_sys  / delta_time_base);
183         lpar_submit ("wait", (double)dlt_pcpu_wait / delta_time_base);
184         lpar_submit ("idle", (double)dlt_pcpu_idle / delta_time_base);
185         lpar_submit ("ent",  (double)lparstats.entitled_proc_capacity / 100.0);
186         lpar_submit ("max",  (double)lparstats.max_proc_capacity / 100.0);
187         lpar_submit ("min",  (double)lparstats.min_proc_capacity / 100.0);
188
189         if (donate_flag)
190         {
191                 u_longlong_t dlt_busy_stolen, dlt_idle_stolen;
192                 u_longlong_t dlt_idle_donated, dlt_busy_donated;
193
194                 dlt_idle_donated = lparstats.idle_donated_purr - last_idle_donated_purr;
195                 dlt_busy_donated = lparstats.busy_donated_purr - last_busy_donated_purr;
196                 dlt_idle_stolen  = lparstats.idle_stolen_purr  - last_idle_stolen_purr;
197                 dlt_busy_stolen  = lparstats.busy_stolen_purr  - last_busy_stolen_purr;
198
199                 lpar_submit ("idle_donated", (double)dlt_idle_donated / delta_time_base);
200                 lpar_submit ("busy_donated", (double)dlt_busy_donated / delta_time_base);
201                 lpar_submit ("idle_stolen",  (double)dlt_idle_stolen  / delta_time_base);
202                 lpar_submit ("busy_stolen",  (double)dlt_busy_stolen  / delta_time_base);
203         }
204
205         if (pool_stats)
206         {
207                 if (!lparstats.type.b.pool_util_authority)
208                 {
209                         WARNING ("lpar plugin: this system does not have pool authority.");
210                 }
211                 else
212                 {
213                         u_longlong_t dlt_pit;
214                         double total, idle;
215                         char type[TYPE_INST_LEN];
216
217                         dlt_pit = lparstats.pool_idle_time - last_pool_idle_time;
218                         total = (double)lparstats.phys_cpus_pool;
219                         idle  = (double)dlt_pit / XINTFRAC / (double)delta_time_base;
220                         ssnprintf (type, sizeof(type), "pool-%X-total", lparstats.pool_id);
221                         lpar_submit (type, total);
222                         ssnprintf (type, sizeof(type), "pool-%X-used", lparstats.pool_id);
223                         lpar_submit (type, total - idle);
224                 }
225         }
226
227         save_last_values (&lparstats);
228
229         return (0);
230 } /* int lpar_read */
231
232 void module_register (void)
233 {
234         plugin_register_config ("lpar", lpar_config,
235                                 config_keys, config_keys_num);
236         plugin_register_init ("lpar", lpar_init);
237         plugin_register_read ("lpar", lpar_read);
238 } /* void module_register */
239
240 /* vim: set sw=2 sts=2 ts=8 : */
241