2 * collectd - src/lpar.c
3 * Copyright (C) 2010 Aurélien Reynaud
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.
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.
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
19 * Aurélien Reynaud <collectd at wattapower.net>
26 #include <sys/protosw.h>
27 #include <libperfstat.h>
28 #include <sys/utsname.h>
30 /* XINTFRAC was defined in libperfstat.h somewhere between AIX 5.3 and 6.1 */
32 # include <sys/systemcfg.h>
33 # define XINTFRAC ((double)(_system_configuration.Xint) / \
34 (double)(_system_configuration.Xfrac))
37 #define NS_TO_TICKS(ns) ((ns) / XINTFRAC)
39 static const char *config_keys[] =
44 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
46 static _Bool pool_stats = 0;
47 static _Bool report_by_serial = 0;
48 static _Bool donate_flag = 0;
49 static char serial[SYS_NMLN];
51 static u_longlong_t time_old;
52 static u_longlong_t user_old,
56 static u_longlong_t pool_busy_time_old,
58 static u_longlong_t idle_donated_old,
64 static void save_last_values (perfstat_partition_total_t *lparstats)
66 time_old = lparstats->timebase_last;
68 user_old = lparstats->puser;
69 syst_old = lparstats->psys;
70 idle_old = lparstats->pidle;
71 wait_old = lparstats->pwait;
75 idle_donated_old = lparstats->idle_donated_purr;
76 busy_donated_old = lparstats->busy_donated_purr;
77 busy_stolen_old = lparstats->busy_stolen_purr;
78 idle_stolen_old = lparstats->idle_stolen_purr;
83 pool_busy_time_old = lparstats->pool_busy_time;
84 pool_max_time_old = lparstats->pool_max_time;
86 } /* void save_last_values */
88 static int lpar_config (const char *key, const char *value)
90 if (strcasecmp ("CpuPoolStats", key) == 0)
97 else if (strcasecmp ("ReportBySerial", key) == 0)
100 report_by_serial = 1;
102 report_by_serial = 0;
110 } /* int lpar_config */
112 static int lpar_init (void)
114 perfstat_partition_total_t lparstats;
117 /* Retrieve the initial metrics. Returns the number of structures filled. */
118 status = perfstat_partition_total (/* name = */ NULL, /* (must be NULL) */
119 &lparstats, sizeof (perfstat_partition_total_t),
120 /* number = */ 1 /* (must be 1) */);
124 ERROR ("lpar plugin: perfstat_partition_total failed: %s (%i)",
125 sstrerror (errno, errbuf, sizeof (errbuf)),
130 if (!lparstats.type.b.shared_enabled && lparstats.type.b.donate_enabled)
135 if (pool_stats && !lparstats.type.b.pool_util_authority)
137 WARNING ("lpar plugin: This partition does not have pool authority. "
138 "Disabling CPU pool statistics collection.");
142 /* Save the initial data */
143 save_last_values (&lparstats);
146 } /* int lpar_init */
148 static void lpar_submit (const char *type_instance, double value)
151 value_list_t vl = VALUE_LIST_INIT;
153 values[0].gauge = (gauge_t)value;
157 if (report_by_serial)
159 sstrncpy (vl.host, serial, sizeof (vl.host));
160 sstrncpy (vl.plugin_instance, hostname_g, sizeof (vl.plugin));
164 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
166 sstrncpy (vl.plugin, "lpar", sizeof (vl.plugin));
167 sstrncpy (vl.type, "vcpu", sizeof (vl.type));
168 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
170 plugin_dispatch_values (&vl);
171 } /* void lpar_submit */
173 static int lpar_read (void)
175 perfstat_partition_total_t lparstats;
179 u_longlong_t user_ticks, syst_ticks, wait_ticks, idle_ticks;
180 u_longlong_t consumed_ticks;
181 double entitled_proc_capacity;
183 /* An LPAR has the same serial number as the physical system it is currently
184 running on. It is a convenient way of tracking LPARs as they are moved
185 from chassis to chassis through Live Partition Mobility (LPM). */
186 if (uname (&name) != 0)
188 ERROR ("lpar plugin: uname failed.");
191 sstrncpy (serial, name.machine, sizeof (serial));
193 /* Retrieve the current metrics. Returns the number of structures filled. */
194 status = perfstat_partition_total (/* name = */ NULL, /* (must be NULL) */
195 &lparstats, sizeof (perfstat_partition_total_t),
196 /* number = */ 1 /* (must be 1) */);
200 ERROR ("lpar plugin: perfstat_partition_total failed: %s (%i)",
201 sstrerror (errno, errbuf, sizeof (errbuf)),
206 /* Number of ticks since we last run. */
207 ticks = lparstats.timebase_last - time_old;
210 /* The stats have not been updated. Return now to avoid dividing by zero */
215 * On a shared partition, we're "entitled" to a certain amount of
216 * processing power, for example 250/100 of a physical CPU. Processing
217 * capacity not used by the partition may be assigned to a different
218 * partition by the hypervisor, so "idle" is hopefully a very small
221 * A dedicated partition may donate its CPUs to another partition and
222 * may steal ticks from somewhere else (another partition or maybe the
223 * shared pool, I don't know --octo).
226 /* entitled_proc_capacity is in 1/100th of a CPU */
227 entitled_proc_capacity = 0.01 * ((double) lparstats.entitled_proc_capacity);
228 lpar_submit ("entitled", entitled_proc_capacity);
230 /* The number of ticks actually spent in the various states */
231 user_ticks = lparstats.puser - user_old;
232 syst_ticks = lparstats.psys - syst_old;
233 wait_ticks = lparstats.pwait - wait_old;
234 idle_ticks = lparstats.pidle - idle_old;
235 consumed_ticks = user_ticks + syst_ticks + wait_ticks + idle_ticks;
237 lpar_submit ("user", (double) user_ticks / (double) ticks);
238 lpar_submit ("system", (double) syst_ticks / (double) ticks);
239 lpar_submit ("wait", (double) wait_ticks / (double) ticks);
240 lpar_submit ("idle", (double) idle_ticks / (double) ticks);
244 /* donated => ticks given to another partition
245 * stolen => ticks received from another partition */
246 u_longlong_t idle_donated_ticks, busy_donated_ticks;
247 u_longlong_t idle_stolen_ticks, busy_stolen_ticks;
249 /* FYI: PURR == Processor Utilization of Resources Register
250 * SPURR == Scaled PURR */
251 idle_donated_ticks = lparstats.idle_donated_purr - idle_donated_old;
252 busy_donated_ticks = lparstats.busy_donated_purr - busy_donated_old;
253 idle_stolen_ticks = lparstats.idle_stolen_purr - idle_stolen_old;
254 busy_stolen_ticks = lparstats.busy_stolen_purr - busy_stolen_old;
256 lpar_submit ("idle_donated", (double) idle_donated_ticks / (double) ticks);
257 lpar_submit ("busy_donated", (double) busy_donated_ticks / (double) ticks);
258 lpar_submit ("idle_stolen", (double) idle_stolen_ticks / (double) ticks);
259 lpar_submit ("busy_stolen", (double) busy_stolen_ticks / (double) ticks);
261 /* Donated ticks will be accounted for as stolen ticks in other LPARs */
262 consumed_ticks += idle_stolen_ticks + busy_stolen_ticks;
265 lpar_submit ("consumed", (double) consumed_ticks / (double) ticks);
269 char typinst[DATA_MAX_NAME_LEN];
270 u_longlong_t pool_busy_ns, pool_max_ns;
272 pool_busy_ns = lparstats.pool_busy_time - pool_busy_time_old;
273 pool_max_ns = lparstats.pool_max_time - pool_max_time_old;
275 /* Pool stats are in CPU x ns */
276 ssnprintf (typinst, sizeof (typinst), "pool-%X-busy", lparstats.pool_id);
277 lpar_submit (typinst, NS_TO_TICKS ((double) pool_busy_ns) / (double) ticks);
279 ssnprintf (typinst, sizeof (typinst), "pool-%X-total", lparstats.pool_id);
280 lpar_submit (typinst, NS_TO_TICKS ((double) pool_max_ns) / (double) ticks);
283 save_last_values (&lparstats);
286 } /* int lpar_read */
288 void module_register (void)
290 plugin_register_config ("lpar", lpar_config,
291 config_keys, config_keys_num);
292 plugin_register_init ("lpar", lpar_init);
293 plugin_register_read ("lpar", lpar_read);
294 } /* void module_register */
296 /* vim: set sw=8 noet : */