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>
27 #include <libperfstat.h>
28 #include <sys/protosw.h>
29 #include <sys/utsname.h>
31 /* XINTFRAC was defined in libperfstat.h somewhere between AIX 5.3 and 6.1 */
33 #include <sys/systemcfg.h>
35 ((double)(_system_configuration.Xint) / (double)(_system_configuration.Xfrac))
38 #define CLOCKTICKS_TO_TICKS(cticks) ((cticks) / XINTFRAC)
40 static const char *config_keys[] = {"CpuPoolStats", "ReportBySerial"};
41 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
43 static _Bool pool_stats = 0;
44 static _Bool report_by_serial = 0;
45 #if PERFSTAT_SUPPORTS_DONATION
46 static _Bool donate_flag = 0;
48 static char serial[SYS_NMLN];
50 static perfstat_partition_total_t lparstats_old;
52 static int lpar_config(const char *key, const char *value) {
53 if (strcasecmp("CpuPoolStats", key) == 0) {
58 } else if (strcasecmp("ReportBySerial", key) == 0) {
68 } /* int lpar_config */
70 static int lpar_init(void) {
73 /* Retrieve the initial metrics. Returns the number of structures filled. */
74 status = perfstat_partition_total(/* name = */ NULL, /* (must be NULL) */
76 sizeof(perfstat_partition_total_t),
77 /* number = */ 1 /* (must be 1) */);
79 ERROR("lpar plugin: perfstat_partition_total failed: %s (%i)", STRERRNO,
84 #if PERFSTAT_SUPPORTS_DONATION
85 if (!lparstats_old.type.b.shared_enabled &&
86 lparstats_old.type.b.donate_enabled) {
91 if (pool_stats && !lparstats_old.type.b.pool_util_authority) {
92 WARNING("lpar plugin: This partition does not have pool authority. "
93 "Disabling CPU pool statistics collection.");
100 static void lpar_submit(const char *type_instance, double value) {
101 value_list_t vl = VALUE_LIST_INIT;
103 vl.values = &(value_t){.gauge = value};
105 if (report_by_serial) {
106 sstrncpy(vl.host, serial, sizeof(vl.host));
107 sstrncpy(vl.plugin_instance, hostname_g, sizeof(vl.plugin));
109 sstrncpy(vl.plugin, "lpar", sizeof(vl.plugin));
110 sstrncpy(vl.type, "vcpu", sizeof(vl.type));
111 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
113 plugin_dispatch_values(&vl);
114 } /* void lpar_submit */
116 static int lpar_read(void) {
117 perfstat_partition_total_t lparstats;
121 u_longlong_t user_ticks, syst_ticks, wait_ticks, idle_ticks;
122 u_longlong_t consumed_ticks;
123 double entitled_proc_capacity;
125 /* An LPAR has the same serial number as the physical system it is currently
126 running on. It is a convenient way of tracking LPARs as they are moved
127 from chassis to chassis through Live Partition Mobility (LPM). */
128 if (uname(&name) != 0) {
129 ERROR("lpar plugin: uname failed.");
132 sstrncpy(serial, name.machine, sizeof(serial));
134 /* Retrieve the current metrics. Returns the number of structures filled. */
136 perfstat_partition_total(/* name = */ NULL, /* (must be NULL) */
137 &lparstats, sizeof(perfstat_partition_total_t),
138 /* number = */ 1 /* (must be 1) */);
140 ERROR("lpar plugin: perfstat_partition_total failed: %s (%i)", STRERRNO,
145 /* Number of ticks since we last run. */
146 ticks = lparstats.timebase_last - lparstats_old.timebase_last;
148 /* The stats have not been updated. Return now to avoid
149 * dividing by zero */
154 * On a shared partition, we're "entitled" to a certain amount of
155 * processing power, for example 250/100 of a physical CPU. Processing
156 * capacity not used by the partition may be assigned to a different
157 * partition by the hypervisor, so "idle" is hopefully a very small
160 * A dedicated partition may donate its CPUs to another partition and
161 * may steal ticks from somewhere else (another partition or maybe the
162 * shared pool, I don't know --octo).
165 /* entitled_proc_capacity is in 1/100th of a CPU */
166 entitled_proc_capacity = 0.01 * ((double)lparstats.entitled_proc_capacity);
167 lpar_submit("entitled", entitled_proc_capacity);
169 /* The number of ticks actually spent in the various states */
170 user_ticks = lparstats.puser - lparstats_old.puser;
171 syst_ticks = lparstats.psys - lparstats_old.psys;
172 wait_ticks = lparstats.pwait - lparstats_old.pwait;
173 idle_ticks = lparstats.pidle - lparstats_old.pidle;
174 consumed_ticks = user_ticks + syst_ticks + wait_ticks + idle_ticks;
176 lpar_submit("user", (double)user_ticks / (double)ticks);
177 lpar_submit("system", (double)syst_ticks / (double)ticks);
178 lpar_submit("wait", (double)wait_ticks / (double)ticks);
179 lpar_submit("idle", (double)idle_ticks / (double)ticks);
181 #if PERFSTAT_SUPPORTS_DONATION
183 /* donated => ticks given to another partition
184 * stolen => ticks received from another partition */
185 u_longlong_t idle_donated_ticks, busy_donated_ticks;
186 u_longlong_t idle_stolen_ticks, busy_stolen_ticks;
188 /* FYI: PURR == Processor Utilization of Resources Register
189 * SPURR == Scaled PURR */
191 lparstats.idle_donated_purr - lparstats_old.idle_donated_purr;
193 lparstats.busy_donated_purr - lparstats_old.busy_donated_purr;
195 lparstats.idle_stolen_purr - lparstats_old.idle_stolen_purr;
197 lparstats.busy_stolen_purr - lparstats_old.busy_stolen_purr;
199 lpar_submit("idle_donated", (double)idle_donated_ticks / (double)ticks);
200 lpar_submit("busy_donated", (double)busy_donated_ticks / (double)ticks);
201 lpar_submit("idle_stolen", (double)idle_stolen_ticks / (double)ticks);
202 lpar_submit("busy_stolen", (double)busy_stolen_ticks / (double)ticks);
204 /* Donated ticks will be accounted for as stolen ticks in other LPARs */
205 consumed_ticks += idle_stolen_ticks + busy_stolen_ticks;
209 lpar_submit("consumed", (double)consumed_ticks / (double)ticks);
212 char typinst[DATA_MAX_NAME_LEN];
213 u_longlong_t pool_idle_cticks;
214 double pool_idle_cpus;
215 double pool_busy_cpus;
217 /* We're calculating "busy" from "idle" and the total number of
218 * CPUs, because the "busy" member didn't exist in early versions
219 * of libperfstat. It was added somewhere between AIX 5.3 ML5 and ML9. */
220 pool_idle_cticks = lparstats.pool_idle_time - lparstats_old.pool_idle_time;
222 CLOCKTICKS_TO_TICKS((double)pool_idle_cticks) / (double)ticks;
223 pool_busy_cpus = ((double)lparstats.phys_cpus_pool) - pool_idle_cpus;
224 if (pool_busy_cpus < 0.0)
225 pool_busy_cpus = 0.0;
227 snprintf(typinst, sizeof(typinst), "pool-%X-busy", lparstats.pool_id);
228 lpar_submit(typinst, pool_busy_cpus);
230 snprintf(typinst, sizeof(typinst), "pool-%X-idle", lparstats.pool_id);
231 lpar_submit(typinst, pool_idle_cpus);
234 memcpy(&lparstats_old, &lparstats, sizeof(lparstats_old));
237 } /* int lpar_read */
239 void module_register(void) {
240 plugin_register_config("lpar", lpar_config, config_keys, config_keys_num);
241 plugin_register_init("lpar", lpar_init);
242 plugin_register_read("lpar", lpar_read);
243 } /* void module_register */