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 * Aurélien Reynaud <collectd at wattapower.net>
20 **/
22 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
27 #include <sys/protosw.h>
28 #include <libperfstat.h>
29 #include <sys/utsname.h>
31 /* XINTFRAC was defined in libperfstat.h somewhere between AIX 5.3 and 6.1 */
32 #ifndef XINTFRAC
33 # include <sys/systemcfg.h>
34 # define XINTFRAC ((double)(_system_configuration.Xint) / \
35 (double)(_system_configuration.Xfrac))
36 #endif
38 #define CLOCKTICKS_TO_TICKS(cticks) ((cticks) / XINTFRAC)
40 static const char *config_keys[] =
41 {
42 "CpuPoolStats",
43 "ReportBySerial"
44 };
45 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
47 static _Bool pool_stats = 0;
48 static _Bool report_by_serial = 0;
49 #if PERFSTAT_SUPPORTS_DONATION
50 static _Bool donate_flag = 0;
51 #endif
52 static char serial[SYS_NMLN];
54 static perfstat_partition_total_t lparstats_old;
56 static int lpar_config (const char *key, const char *value)
57 {
58 if (strcasecmp ("CpuPoolStats", key) == 0)
59 {
60 if (IS_TRUE (value))
61 pool_stats = 1;
62 else
63 pool_stats = 0;
64 }
65 else if (strcasecmp ("ReportBySerial", key) == 0)
66 {
67 if (IS_TRUE (value))
68 report_by_serial = 1;
69 else
70 report_by_serial = 0;
71 }
72 else
73 {
74 return (-1);
75 }
77 return (0);
78 } /* int lpar_config */
80 static int lpar_init (void)
81 {
82 int status;
84 /* Retrieve the initial metrics. Returns the number of structures filled. */
85 status = perfstat_partition_total (/* name = */ NULL, /* (must be NULL) */
86 &lparstats_old, sizeof (perfstat_partition_total_t),
87 /* number = */ 1 /* (must be 1) */);
88 if (status != 1)
89 {
90 char errbuf[1024];
91 ERROR ("lpar plugin: perfstat_partition_total failed: %s (%i)",
92 sstrerror (errno, errbuf, sizeof (errbuf)),
93 status);
94 return (-1);
95 }
97 #if PERFSTAT_SUPPORTS_DONATION
98 if (!lparstats_old.type.b.shared_enabled
99 && lparstats_old.type.b.donate_enabled)
100 {
101 donate_flag = 1;
102 }
103 #endif
105 if (pool_stats && !lparstats_old.type.b.pool_util_authority)
106 {
107 WARNING ("lpar plugin: This partition does not have pool authority. "
108 "Disabling CPU pool statistics collection.");
109 pool_stats = 0;
110 }
112 return (0);
113 } /* int lpar_init */
115 static void lpar_submit (const char *type_instance, double value)
116 {
117 value_t values[1];
118 value_list_t vl = VALUE_LIST_INIT;
120 values[0].gauge = (gauge_t)value;
122 vl.values = values;
123 vl.values_len = 1;
124 if (report_by_serial)
125 {
126 sstrncpy (vl.host, serial, sizeof (vl.host));
127 sstrncpy (vl.plugin_instance, hostname_g, sizeof (vl.plugin));
128 }
129 else
130 {
131 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
132 }
133 sstrncpy (vl.plugin, "lpar", sizeof (vl.plugin));
134 sstrncpy (vl.type, "vcpu", sizeof (vl.type));
135 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
137 plugin_dispatch_values (&vl);
138 } /* void lpar_submit */
140 static int lpar_read (void)
141 {
142 perfstat_partition_total_t lparstats;
143 int status;
144 struct utsname name;
145 u_longlong_t ticks;
146 u_longlong_t user_ticks, syst_ticks, wait_ticks, idle_ticks;
147 u_longlong_t consumed_ticks;
148 double entitled_proc_capacity;
150 /* An LPAR has the same serial number as the physical system it is currently
151 running on. It is a convenient way of tracking LPARs as they are moved
152 from chassis to chassis through Live Partition Mobility (LPM). */
153 if (uname (&name) != 0)
154 {
155 ERROR ("lpar plugin: uname failed.");
156 return (-1);
157 }
158 sstrncpy (serial, name.machine, sizeof (serial));
160 /* Retrieve the current metrics. Returns the number of structures filled. */
161 status = perfstat_partition_total (/* name = */ NULL, /* (must be NULL) */
162 &lparstats, sizeof (perfstat_partition_total_t),
163 /* number = */ 1 /* (must be 1) */);
164 if (status != 1)
165 {
166 char errbuf[1024];
167 ERROR ("lpar plugin: perfstat_partition_total failed: %s (%i)",
168 sstrerror (errno, errbuf, sizeof (errbuf)),
169 status);
170 return (-1);
171 }
173 /* Number of ticks since we last run. */
174 ticks = lparstats.timebase_last - lparstats_old.timebase_last;
175 if (ticks == 0)
176 {
177 /* The stats have not been updated. Return now to avoid
178 * dividing by zero */
179 return (0);
180 }
182 /*
183 * On a shared partition, we're "entitled" to a certain amount of
184 * processing power, for example 250/100 of a physical CPU. Processing
185 * capacity not used by the partition may be assigned to a different
186 * partition by the hypervisor, so "idle" is hopefully a very small
187 * number.
188 *
189 * A dedicated partition may donate its CPUs to another partition and
190 * may steal ticks from somewhere else (another partition or maybe the
191 * shared pool, I don't know --octo).
192 */
194 /* entitled_proc_capacity is in 1/100th of a CPU */
195 entitled_proc_capacity = 0.01 * ((double) lparstats.entitled_proc_capacity);
196 lpar_submit ("entitled", entitled_proc_capacity);
198 /* The number of ticks actually spent in the various states */
199 user_ticks = lparstats.puser - lparstats_old.puser;
200 syst_ticks = lparstats.psys - lparstats_old.psys;
201 wait_ticks = lparstats.pwait - lparstats_old.pwait;
202 idle_ticks = lparstats.pidle - lparstats_old.pidle;
203 consumed_ticks = user_ticks + syst_ticks + wait_ticks + idle_ticks;
205 lpar_submit ("user", (double) user_ticks / (double) ticks);
206 lpar_submit ("system", (double) syst_ticks / (double) ticks);
207 lpar_submit ("wait", (double) wait_ticks / (double) ticks);
208 lpar_submit ("idle", (double) idle_ticks / (double) ticks);
210 #if PERFSTAT_SUPPORTS_DONATION
211 if (donate_flag)
212 {
213 /* donated => ticks given to another partition
214 * stolen => ticks received from another partition */
215 u_longlong_t idle_donated_ticks, busy_donated_ticks;
216 u_longlong_t idle_stolen_ticks, busy_stolen_ticks;
218 /* FYI: PURR == Processor Utilization of Resources Register
219 * SPURR == Scaled PURR */
220 idle_donated_ticks = lparstats.idle_donated_purr - lparstats_old.idle_donated_purr;
221 busy_donated_ticks = lparstats.busy_donated_purr - lparstats_old.busy_donated_purr;
222 idle_stolen_ticks = lparstats.idle_stolen_purr - lparstats_old.idle_stolen_purr;
223 busy_stolen_ticks = lparstats.busy_stolen_purr - lparstats_old.busy_stolen_purr;
225 lpar_submit ("idle_donated", (double) idle_donated_ticks / (double) ticks);
226 lpar_submit ("busy_donated", (double) busy_donated_ticks / (double) ticks);
227 lpar_submit ("idle_stolen", (double) idle_stolen_ticks / (double) ticks);
228 lpar_submit ("busy_stolen", (double) busy_stolen_ticks / (double) ticks);
230 /* Donated ticks will be accounted for as stolen ticks in other LPARs */
231 consumed_ticks += idle_stolen_ticks + busy_stolen_ticks;
232 }
233 #endif
235 lpar_submit ("consumed", (double) consumed_ticks / (double) ticks);
237 if (pool_stats)
238 {
239 char typinst[DATA_MAX_NAME_LEN];
240 u_longlong_t pool_idle_cticks;
241 double pool_idle_cpus;
242 double pool_busy_cpus;
244 /* We're calculating "busy" from "idle" and the total number of
245 * CPUs, because the "busy" member didn't exist in early versions
246 * of libperfstat. It was added somewhere between AIX 5.3 ML5 and ML9. */
247 pool_idle_cticks = lparstats.pool_idle_time - lparstats_old.pool_idle_time;
248 pool_idle_cpus = CLOCKTICKS_TO_TICKS ((double) pool_idle_cticks) / (double) ticks;
249 pool_busy_cpus = ((double) lparstats.phys_cpus_pool) - pool_idle_cpus;
250 if (pool_busy_cpus < 0.0)
251 pool_busy_cpus = 0.0;
253 ssnprintf (typinst, sizeof (typinst), "pool-%X-busy", lparstats.pool_id);
254 lpar_submit (typinst, pool_busy_cpus);
256 ssnprintf (typinst, sizeof (typinst), "pool-%X-idle", lparstats.pool_id);
257 lpar_submit (typinst, pool_idle_cpus);
258 }
260 memcpy (&lparstats_old, &lparstats, sizeof (lparstats_old));
262 return (0);
263 } /* int lpar_read */
265 void module_register (void)
266 {
267 plugin_register_config ("lpar", lpar_config,
268 config_keys, config_keys_num);
269 plugin_register_init ("lpar", lpar_init);
270 plugin_register_read ("lpar", lpar_read);
271 } /* void module_register */
273 /* vim: set sw=8 noet : */