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