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 * Aurelien Reynaud <collectd at wattapower.net>
25 #include <sys/protosw.h>
26 #include <libperfstat.h>
27 #include <sys/systemcfg.h>
28 #include <sys/utsname.h>
31 # define XINTFRAC ((double)(_system_configuration.Xint) / \
32 (double)(_system_configuration.Xfrac))
35 /* Max length of the type instance string */
36 #define TYPE_INST_LEN (sizeof("lpar--total") + 2*sizeof(int) + 1)
38 static const char *config_keys[] =
42 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
43 static int pool_stats = 0;
45 /* As an LPAR can be moved transparently across physical systems
46 * through Live Partition Mobility (LPM), and the resources we are
47 * monitoring are tied to the underlying hardware, we need to keep
48 * track on which physical server we are currently on. This is done
49 * through the plugin instance which holds the chassis' serial.
51 static char plugin_inst[SYS_NMLN];
53 static u_longlong_t last_time_base;
54 static u_longlong_t last_pcpu_user,
58 static u_longlong_t last_pool_idle_time = 0;
59 static u_longlong_t last_idle_donated_purr = 0,
60 last_busy_donated_purr = 0,
61 last_busy_stolen_purr = 0,
62 last_idle_stolen_purr = 0;
63 static int donate_flag = 0;
66 /* Save the current values for the next iteration */
67 static void save_last_values (perfstat_partition_total_t *lparstats)
69 last_time_base = lparstats->timebase_last;
71 last_pcpu_user = lparstats->puser;
72 last_pcpu_sys = lparstats->psys;
73 last_pcpu_idle = lparstats->pidle;
74 last_pcpu_wait = lparstats->pwait;
78 last_idle_donated_purr = lparstats->idle_donated_purr;
79 last_busy_donated_purr = lparstats->busy_donated_purr;
80 last_busy_stolen_purr = lparstats->busy_stolen_purr;
81 last_idle_stolen_purr = lparstats->idle_stolen_purr;
84 last_pool_idle_time = lparstats->pool_idle_time;
87 static int lpar_config (const char *key, const char *value)
89 if (strcasecmp ("CpuPoolStats", key) == 0)
102 } /* int lpar_config */
104 static int lpar_init (void)
106 perfstat_partition_total_t lparstats;
108 /* retrieve the initial metrics */
109 if (!perfstat_partition_total (NULL, &lparstats,
110 sizeof (perfstat_partition_total_t), 1))
112 ERROR ("lpar plugin: perfstat_partition_total failed.");
116 if (!lparstats.type.b.shared_enabled && lparstats.type.b.donate_enabled)
121 /* save the initial data */
122 save_last_values (&lparstats);
125 } /* int lpar_init */
127 static void lpar_submit (const char *type_instance, double value)
130 value_list_t vl = VALUE_LIST_INIT;
132 values[0].gauge = (gauge_t)value;
136 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
137 sstrncpy (vl.plugin, "lpar", sizeof (vl.plugin));
138 sstrncpy (vl.plugin_instance, plugin_inst, sizeof (vl.plugin));
139 sstrncpy (vl.type, "lpar_pcpu", sizeof (vl.type));
140 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
142 plugin_dispatch_values (&vl);
145 static int lpar_read (void)
147 u_longlong_t dlt_pcpu_user, dlt_pcpu_sys, dlt_pcpu_idle, dlt_pcpu_wait;
148 u_longlong_t delta_time_base;
149 perfstat_partition_total_t lparstats;
152 /* retrieve the current physical server's id and build the plugin
154 if (uname (&name) != 0)
156 ERROR ("lpar plugin: uname failed.");
159 sstrncpy (plugin_inst, name.machine, sizeof (plugin_inst));
161 /* retrieve the current metrics */
162 if (!perfstat_partition_total (NULL, &lparstats,
163 sizeof (perfstat_partition_total_t), 1))
165 ERROR ("lpar plugin: perfstat_partition_total failed.");
169 delta_time_base = lparstats.timebase_last - last_time_base;
170 if (delta_time_base == 0)
172 /* The system stats have not been updated since last time */
176 dlt_pcpu_user = lparstats.puser - last_pcpu_user;
177 dlt_pcpu_sys = lparstats.psys - last_pcpu_sys;
178 dlt_pcpu_idle = lparstats.pidle - last_pcpu_idle;
179 dlt_pcpu_wait = lparstats.pwait - last_pcpu_wait;
181 lpar_submit ("user", (double)dlt_pcpu_user / delta_time_base);
182 lpar_submit ("sys", (double)dlt_pcpu_sys / delta_time_base);
183 lpar_submit ("wait", (double)dlt_pcpu_wait / delta_time_base);
184 lpar_submit ("idle", (double)dlt_pcpu_idle / delta_time_base);
185 lpar_submit ("ent", (double)lparstats.entitled_proc_capacity / 100.0);
186 lpar_submit ("max", (double)lparstats.max_proc_capacity / 100.0);
187 lpar_submit ("min", (double)lparstats.min_proc_capacity / 100.0);
191 u_longlong_t dlt_busy_stolen, dlt_idle_stolen;
192 u_longlong_t dlt_idle_donated, dlt_busy_donated;
194 dlt_idle_donated = lparstats.idle_donated_purr - last_idle_donated_purr;
195 dlt_busy_donated = lparstats.busy_donated_purr - last_busy_donated_purr;
196 dlt_idle_stolen = lparstats.idle_stolen_purr - last_idle_stolen_purr;
197 dlt_busy_stolen = lparstats.busy_stolen_purr - last_busy_stolen_purr;
199 lpar_submit ("idle_donated", (double)dlt_idle_donated / delta_time_base);
200 lpar_submit ("busy_donated", (double)dlt_busy_donated / delta_time_base);
201 lpar_submit ("idle_stolen", (double)dlt_idle_stolen / delta_time_base);
202 lpar_submit ("busy_stolen", (double)dlt_busy_stolen / delta_time_base);
207 if (!lparstats.type.b.pool_util_authority)
209 WARNING ("lpar plugin: this system does not have pool authority.");
213 u_longlong_t dlt_pit;
215 char type[TYPE_INST_LEN];
217 dlt_pit = lparstats.pool_idle_time - last_pool_idle_time;
218 total = (double)lparstats.phys_cpus_pool;
219 idle = (double)dlt_pit / XINTFRAC / (double)delta_time_base;
220 ssnprintf (type, sizeof(type), "pool-%X-total", lparstats.pool_id);
221 lpar_submit (type, total);
222 ssnprintf (type, sizeof(type), "pool-%X-used", lparstats.pool_id);
223 lpar_submit (type, total - idle);
227 save_last_values (&lparstats);
230 } /* int lpar_read */
232 void module_register (void)
234 plugin_register_config ("lpar", lpar_config,
235 config_keys, config_keys_num);
236 plugin_register_init ("lpar", lpar_init);
237 plugin_register_read ("lpar", lpar_read);
238 } /* void module_register */
240 /* vim: set sw=2 sts=2 ts=8 : */