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"
23 #include "common.h"
24 #include "plugin.h"
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 */
31 #ifndef XINTFRAC
32 # include <sys/systemcfg.h>
33 # define XINTFRAC ((double)(_system_configuration.Xint) / \
34 (double)(_system_configuration.Xfrac))
35 #endif
37 #define NS_TO_TICKS(ns) ((ns) / XINTFRAC)
39 static const char *config_keys[] =
40 {
41 "CpuPoolStats",
42 "ReportBySerial"
43 };
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,
53 syst_old,
54 idle_old,
55 wait_old;
56 static u_longlong_t pool_busy_time_old,
57 pool_max_time_old;
58 static u_longlong_t idle_donated_old,
59 busy_donated_old,
60 busy_stolen_old,
61 idle_stolen_old;
64 static void save_last_values (perfstat_partition_total_t *lparstats)
65 {
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;
73 if (donate_flag)
74 {
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;
79 }
81 if (pool_stats)
82 {
83 pool_busy_time_old = lparstats->pool_busy_time;
84 pool_max_time_old = lparstats->pool_max_time;
85 }
86 } /* void save_last_values */
88 static int lpar_config (const char *key, const char *value)
89 {
90 if (strcasecmp ("CpuPoolStats", key) == 0)
91 {
92 if (IS_TRUE (value))
93 pool_stats = 1;
94 else
95 pool_stats = 0;
96 }
97 else if (strcasecmp ("ReportBySerial", key) == 0)
98 {
99 if (IS_TRUE (value))
100 report_by_serial = 1;
101 else
102 report_by_serial = 0;
103 }
104 else
105 {
106 return (-1);
107 }
109 return (0);
110 } /* int lpar_config */
112 static int lpar_init (void)
113 {
114 perfstat_partition_total_t lparstats;
115 int status;
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) */);
121 if (status != 1)
122 {
123 char errbuf[1024];
124 ERROR ("lpar plugin: perfstat_partition_total failed: %s (%i)",
125 sstrerror (errno, errbuf, sizeof (errbuf)),
126 status);
127 return (-1);
128 }
130 if (!lparstats.type.b.shared_enabled && lparstats.type.b.donate_enabled)
131 {
132 donate_flag = 1;
133 }
135 if (pool_stats && !lparstats.type.b.pool_util_authority)
136 {
137 WARNING ("lpar plugin: This partition does not have pool authority. "
138 "Disabling CPU pool statistics collection.");
139 pool_stats = 0;
140 }
142 /* Save the initial data */
143 save_last_values (&lparstats);
145 return (0);
146 } /* int lpar_init */
148 static void lpar_submit (const char *type_instance, double value)
149 {
150 value_t values[1];
151 value_list_t vl = VALUE_LIST_INIT;
153 values[0].gauge = (gauge_t)value;
155 vl.values = values;
156 vl.values_len = 1;
157 if (report_by_serial)
158 {
159 sstrncpy (vl.host, serial, sizeof (vl.host));
160 sstrncpy (vl.plugin_instance, hostname_g, sizeof (vl.plugin));
161 }
162 else
163 {
164 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
165 }
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)
174 {
175 perfstat_partition_total_t lparstats;
176 int status;
177 struct utsname name;
178 u_longlong_t ticks;
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)
187 {
188 ERROR ("lpar plugin: uname failed.");
189 return (-1);
190 }
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) */);
197 if (status != 1)
198 {
199 char errbuf[1024];
200 ERROR ("lpar plugin: perfstat_partition_total failed: %s (%i)",
201 sstrerror (errno, errbuf, sizeof (errbuf)),
202 status);
203 return (-1);
204 }
206 /* Number of ticks since we last run. */
207 ticks = lparstats.timebase_last - time_old;
208 if (ticks == 0)
209 {
210 /* The stats have not been updated. Return now to avoid dividing by zero */
211 return (0);
212 }
214 /*
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
219 * number.
220 */
222 /* entitled_proc_capacity is in 1/100th of a CPU */
223 entitled_proc_capacity = 0.01 * ((double) lparstats.entitled_proc_capacity);
224 lpar_submit ("entitled", entitled_proc_capacity);
226 /* The number of ticks actually spent in the various states */
227 user_ticks = lparstats.puser - user_old;
228 syst_ticks = lparstats.psys - syst_old;
229 wait_ticks = lparstats.pwait - wait_old;
230 idle_ticks = lparstats.pidle - idle_old;
231 consumed_ticks = user_ticks + syst_ticks + wait_ticks + idle_ticks;
233 lpar_submit ("user", (double) user_ticks / (double) ticks);
234 lpar_submit ("sys", (double) syst_ticks / (double) ticks);
235 lpar_submit ("wait", (double) wait_ticks / (double) ticks);
236 lpar_submit ("idle", (double) idle_ticks / (double) ticks);
238 if (donate_flag)
239 {
240 u_longlong_t idle_donated_ticks, busy_donated_ticks;
241 u_longlong_t idle_stolen_ticks, busy_stolen_ticks;
243 idle_donated_ticks = lparstats.idle_donated_purr - idle_donated_old;
244 busy_donated_ticks = lparstats.busy_donated_purr - busy_donated_old;
245 idle_stolen_ticks = lparstats.idle_stolen_purr - idle_stolen_old;
246 busy_stolen_ticks = lparstats.busy_stolen_purr - busy_stolen_old;
248 /* FYI: PURR == Processor Utilization of Resources Register
249 * SPURR == Scaled PURR */
250 /* donated => ticks given to another partition
251 * stolen => ticks received from another partition */
252 lpar_submit ("idle_donated", (double) idle_donated_ticks / (double) ticks);
253 lpar_submit ("busy_donated", (double) busy_donated_ticks / (double) ticks);
254 lpar_submit ("idle_stolen", (double) idle_stolen_ticks / (double) ticks);
255 lpar_submit ("busy_stolen", (double) busy_stolen_ticks / (double) ticks);
257 /* Donated ticks will be accounted for as stolen ticks in other LPARs */
258 consumed_ticks += idle_stolen_ticks + busy_stolen_ticks;
259 }
261 lpar_submit ("consumed", (double) consumed_ticks / (double) ticks);
263 if (pool_stats)
264 {
265 char typinst[DATA_MAX_NAME_LEN];
266 u_longlong_t pool_busy_ns, pool_max_ns;
268 pool_busy_ns = lparstats.pool_busy_time - pool_busy_time_old;
269 pool_max_ns = lparstats.pool_max_time - pool_max_time_old;
271 /* Pool stats are in CPU x ns */
272 ssnprintf (typinst, sizeof (typinst), "pool-%X-busy", lparstats.pool_id);
273 lpar_submit (typinst, NS_TO_TICKS ((double) pool_busy_ns) / (double) ticks);
275 ssnprintf (typinst, sizeof (typinst), "pool-%X-total", lparstats.pool_id);
276 lpar_submit (typinst, NS_TO_TICKS ((double) pool_max_ns) / (double) ticks);
277 }
279 save_last_values (&lparstats);
281 return (0);
282 } /* int lpar_read */
284 void module_register (void)
285 {
286 plugin_register_config ("lpar", lpar_config,
287 config_keys, config_keys_num);
288 plugin_register_init ("lpar", lpar_init);
289 plugin_register_read ("lpar", lpar_read);
290 } /* void module_register */
292 /* vim: set sw=8 noet : */