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