Merge branch 'collectd-5.8'
[collectd.git] / src / intel_rdt.c
1 /**
2  * collectd - src/intel_rdt.c
3  *
4  * Copyright(c) 2016-2018 Intel Corporation. All rights reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  *
24  * Authors:
25  *   Serhiy Pshyk <serhiyx.pshyk@intel.com>
26  **/
27
28 #include "collectd.h"
29 #include "utils/common/common.h"
30 #include "utils/config_cores/config_cores.h"
31
32 #include <pqos.h>
33
34 #define RDT_PLUGIN "intel_rdt"
35
36 #define RDT_MAX_SOCKETS 8
37 #define RDT_MAX_SOCKET_CORES 64
38 #define RDT_MAX_CORES (RDT_MAX_SOCKET_CORES * RDT_MAX_SOCKETS)
39
40 typedef enum {
41   UNKNOWN = 0,
42   CONFIGURATION_ERROR,
43 } rdt_config_status;
44
45 struct rdt_ctx_s {
46   core_groups_list_t cores;
47   enum pqos_mon_event events[RDT_MAX_CORES];
48   struct pqos_mon_data *pgroups[RDT_MAX_CORES];
49   size_t num_groups;
50   const struct pqos_cpuinfo *pqos_cpu;
51   const struct pqos_cap *pqos_cap;
52   const struct pqos_capability *cap_mon;
53 };
54 typedef struct rdt_ctx_s rdt_ctx_t;
55
56 static rdt_ctx_t *g_rdt;
57
58 static rdt_config_status g_state = UNKNOWN;
59
60 #if COLLECT_DEBUG
61 static void rdt_dump_cgroups(void) {
62   char cores[RDT_MAX_CORES * 4];
63
64   if (g_rdt == NULL)
65     return;
66
67   DEBUG(RDT_PLUGIN ": Core Groups Dump");
68   DEBUG(RDT_PLUGIN ":  groups count: %" PRIsz, g_rdt->num_groups);
69
70   for (size_t i = 0; i < g_rdt->num_groups; i++) {
71     core_group_t *cgroup = g_rdt->cores.cgroups + i;
72
73     memset(cores, 0, sizeof(cores));
74     for (int j = 0; j < cgroup->num_cores; j++) {
75       snprintf(cores + strlen(cores), sizeof(cores) - strlen(cores) - 1, " %d",
76                cgroup->cores[j]);
77     }
78
79     DEBUG(RDT_PLUGIN ":  group[%zu]:", i);
80     DEBUG(RDT_PLUGIN ":    description: %s", cgroup->desc);
81     DEBUG(RDT_PLUGIN ":    cores: %s", cores);
82     DEBUG(RDT_PLUGIN ":    events: 0x%X", g_rdt->events[i]);
83   }
84
85   return;
86 }
87
88 static inline double bytes_to_kb(const double bytes) { return bytes / 1024.0; }
89
90 static inline double bytes_to_mb(const double bytes) {
91   return bytes / (1024.0 * 1024.0);
92 }
93
94 static void rdt_dump_data(void) {
95   /*
96    * CORE - monitored group of cores
97    * RMID - Resource Monitoring ID associated with the monitored group
98    * LLC - last level cache occupancy
99    * MBL - local memory bandwidth
100    * MBR - remote memory bandwidth
101    */
102   DEBUG("  CORE     RMID    LLC[KB]   MBL[MB]    MBR[MB]");
103   for (int i = 0; i < g_rdt->num_groups; i++) {
104
105     const struct pqos_event_values *pv = &g_rdt->pgroups[i]->values;
106
107     double llc = bytes_to_kb(pv->llc);
108     double mbr = bytes_to_mb(pv->mbm_remote_delta);
109     double mbl = bytes_to_mb(pv->mbm_local_delta);
110
111     DEBUG(" [%s] %8u %10.1f %10.1f %10.1f", g_rdt->cores.cgroups[i].desc,
112           g_rdt->pgroups[i]->poll_ctx[0].rmid, llc, mbl, mbr);
113   }
114 }
115 #endif /* COLLECT_DEBUG */
116
117 static void rdt_free_cgroups(void) {
118   config_cores_cleanup(&g_rdt->cores);
119   for (int i = 0; i < RDT_MAX_CORES; i++) {
120     sfree(g_rdt->pgroups[i]);
121   }
122 }
123
124 static int rdt_default_cgroups(void) {
125   unsigned num_cores = g_rdt->pqos_cpu->num_cores;
126
127   g_rdt->cores.cgroups = calloc(num_cores, sizeof(*g_rdt->cores.cgroups));
128   if (g_rdt->cores.cgroups == NULL) {
129     ERROR(RDT_PLUGIN ": Error allocating core groups array");
130     return -ENOMEM;
131   }
132   g_rdt->cores.num_cgroups = num_cores;
133
134   /* configure each core in separate group */
135   for (unsigned i = 0; i < num_cores; i++) {
136     core_group_t *cgroup = g_rdt->cores.cgroups + i;
137     char desc[DATA_MAX_NAME_LEN];
138
139     /* set core group info */
140     cgroup->cores = calloc(1, sizeof(*cgroup->cores));
141     if (cgroup->cores == NULL) {
142       ERROR(RDT_PLUGIN ": Error allocating cores array");
143       rdt_free_cgroups();
144       return -ENOMEM;
145     }
146     cgroup->num_cores = 1;
147     cgroup->cores[0] = i;
148
149     snprintf(desc, sizeof(desc), "%d", g_rdt->pqos_cpu->cores[i].lcore);
150     cgroup->desc = strdup(desc);
151     if (cgroup->desc == NULL) {
152       ERROR(RDT_PLUGIN ": Error allocating core group description");
153       rdt_free_cgroups();
154       return -ENOMEM;
155     }
156   }
157
158   return num_cores;
159 }
160
161 static int rdt_is_core_id_valid(unsigned int core_id) {
162
163   for (unsigned int i = 0; i < g_rdt->pqos_cpu->num_cores; i++)
164     if (core_id == g_rdt->pqos_cpu->cores[i].lcore)
165       return 1;
166
167   return 0;
168 }
169
170 static int rdt_config_cgroups(oconfig_item_t *item) {
171   size_t n = 0;
172   enum pqos_mon_event events = 0;
173
174   if (config_cores_parse(item, &g_rdt->cores) < 0) {
175     rdt_free_cgroups();
176     ERROR(RDT_PLUGIN ": Error parsing core groups configuration.");
177     return -EINVAL;
178   }
179   n = g_rdt->cores.num_cgroups;
180
181   /* validate configured core id values */
182   for (size_t group_idx = 0; group_idx < n; group_idx++) {
183     core_group_t *cgroup = g_rdt->cores.cgroups + group_idx;
184     for (size_t core_idx = 0; core_idx < cgroup->num_cores; core_idx++) {
185       if (!rdt_is_core_id_valid(cgroup->cores[core_idx])) {
186         ERROR(RDT_PLUGIN ": Core group '%s' contains invalid core id '%u'",
187               cgroup->desc, cgroup->cores[core_idx]);
188         rdt_free_cgroups();
189         return -EINVAL;
190       }
191     }
192   }
193
194   if (n == 0) {
195     /* create default core groups if "Cores" config option is empty */
196     int ret = rdt_default_cgroups();
197     if (ret < 0) {
198       rdt_free_cgroups();
199       ERROR(RDT_PLUGIN ": Error creating default core groups configuration.");
200       return ret;
201     }
202     n = (size_t)ret;
203     INFO(RDT_PLUGIN
204          ": No core groups configured. Default core groups created.");
205   }
206
207   /* Get all available events on this platform */
208   for (unsigned int i = 0; i < g_rdt->cap_mon->u.mon->num_events; i++)
209     events |= g_rdt->cap_mon->u.mon->events[i].type;
210
211   events &= ~(PQOS_PERF_EVENT_LLC_MISS);
212
213   DEBUG(RDT_PLUGIN ": Number of cores in the system: %u",
214         g_rdt->pqos_cpu->num_cores);
215   DEBUG(RDT_PLUGIN ": Available events to monitor: %#x", events);
216
217   g_rdt->num_groups = n;
218   for (size_t i = 0; i < n; i++) {
219     for (size_t j = 0; j < i; j++) {
220       int found = 0;
221       found = config_cores_cmp_cgroups(&g_rdt->cores.cgroups[j],
222                                        &g_rdt->cores.cgroups[i]);
223       if (found != 0) {
224         rdt_free_cgroups();
225         ERROR(RDT_PLUGIN ": Cannot monitor same cores in different groups.");
226         return -EINVAL;
227       }
228     }
229
230     g_rdt->events[i] = events;
231     g_rdt->pgroups[i] = calloc(1, sizeof(*g_rdt->pgroups[i]));
232     if (g_rdt->pgroups[i] == NULL) {
233       rdt_free_cgroups();
234       ERROR(RDT_PLUGIN ": Failed to allocate memory for monitoring data.");
235       return -ENOMEM;
236     }
237   }
238
239   return 0;
240 }
241
242 static void rdt_pqos_log(void *context, const size_t size, const char *msg) {
243   DEBUG(RDT_PLUGIN ": %s", msg);
244 }
245
246 static int rdt_preinit(void) {
247   int ret;
248
249   if (g_rdt != NULL) {
250     /* already initialized if config callback was called before init callback */
251     return 0;
252   }
253
254   g_rdt = calloc(1, sizeof(*g_rdt));
255   if (g_rdt == NULL) {
256     ERROR(RDT_PLUGIN ": Failed to allocate memory for rdt context.");
257     return -ENOMEM;
258   }
259
260   struct pqos_config pqos = {.fd_log = -1,
261                              .callback_log = rdt_pqos_log,
262                              .context_log = NULL,
263                              .verbose = 0};
264
265   ret = pqos_init(&pqos);
266   if (ret != PQOS_RETVAL_OK) {
267     ERROR(RDT_PLUGIN ": Error initializing PQoS library!");
268     goto rdt_preinit_error1;
269   }
270
271   ret = pqos_cap_get(&g_rdt->pqos_cap, &g_rdt->pqos_cpu);
272   if (ret != PQOS_RETVAL_OK) {
273     ERROR(RDT_PLUGIN ": Error retrieving PQoS capabilities.");
274     goto rdt_preinit_error2;
275   }
276
277   ret = pqos_cap_get_type(g_rdt->pqos_cap, PQOS_CAP_TYPE_MON, &g_rdt->cap_mon);
278   if (ret == PQOS_RETVAL_PARAM) {
279     ERROR(RDT_PLUGIN ": Error retrieving monitoring capabilities.");
280     goto rdt_preinit_error2;
281   }
282
283   if (g_rdt->cap_mon == NULL) {
284     ERROR(
285         RDT_PLUGIN
286         ": Monitoring capability not detected. Nothing to do for the plugin.");
287     goto rdt_preinit_error2;
288   }
289
290   /* Reset pqos monitoring groups registers */
291   pqos_mon_reset();
292
293   return 0;
294
295 rdt_preinit_error2:
296   pqos_fini();
297
298 rdt_preinit_error1:
299
300   sfree(g_rdt);
301
302   return -1;
303 }
304
305 static int rdt_config(oconfig_item_t *ci) {
306   if (rdt_preinit() != 0) {
307     g_state = CONFIGURATION_ERROR;
308     /* if we return -1 at this point collectd
309       reports a failure in configuration and
310       aborts
311     */
312     return (0);
313   }
314
315   for (int i = 0; i < ci->children_num; i++) {
316     oconfig_item_t *child = ci->children + i;
317
318     if (strcasecmp("Cores", child->key) == 0) {
319       if (rdt_config_cgroups(child) != 0) {
320         g_state = CONFIGURATION_ERROR;
321         /* if we return -1 at this point collectd
322            reports a failure in configuration and
323            aborts
324          */
325         return (0);
326       }
327
328 #if COLLECT_DEBUG
329       rdt_dump_cgroups();
330 #endif /* COLLECT_DEBUG */
331     } else {
332       ERROR(RDT_PLUGIN ": Unknown configuration parameter \"%s\".", child->key);
333     }
334   }
335
336   return 0;
337 }
338
339 static void rdt_submit_derive(const char *cgroup, const char *type,
340                               const char *type_instance, derive_t value) {
341   value_list_t vl = VALUE_LIST_INIT;
342
343   vl.values = &(value_t){.derive = value};
344   vl.values_len = 1;
345
346   sstrncpy(vl.plugin, RDT_PLUGIN, sizeof(vl.plugin));
347   snprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%s", cgroup);
348   sstrncpy(vl.type, type, sizeof(vl.type));
349   if (type_instance)
350     sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
351
352   plugin_dispatch_values(&vl);
353 }
354
355 static void rdt_submit_gauge(const char *cgroup, const char *type,
356                              const char *type_instance, gauge_t value) {
357   value_list_t vl = VALUE_LIST_INIT;
358
359   vl.values = &(value_t){.gauge = value};
360   vl.values_len = 1;
361
362   sstrncpy(vl.plugin, RDT_PLUGIN, sizeof(vl.plugin));
363   snprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%s", cgroup);
364   sstrncpy(vl.type, type, sizeof(vl.type));
365   if (type_instance)
366     sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
367
368   plugin_dispatch_values(&vl);
369 }
370
371 static int rdt_read(__attribute__((unused)) user_data_t *ud) {
372   int ret;
373
374   if (g_rdt == NULL) {
375     ERROR(RDT_PLUGIN ": rdt_read: plugin not initialized.");
376     return -EINVAL;
377   }
378
379   ret = pqos_mon_poll(&g_rdt->pgroups[0], (unsigned)g_rdt->num_groups);
380   if (ret != PQOS_RETVAL_OK) {
381     ERROR(RDT_PLUGIN ": Failed to poll monitoring data.");
382     return -1;
383   }
384
385 #if COLLECT_DEBUG
386   rdt_dump_data();
387 #endif /* COLLECT_DEBUG */
388
389   for (size_t i = 0; i < g_rdt->num_groups; i++) {
390     core_group_t *cgroup = g_rdt->cores.cgroups + i;
391
392     enum pqos_mon_event mbm_events =
393         (PQOS_MON_EVENT_LMEM_BW | PQOS_MON_EVENT_TMEM_BW |
394          PQOS_MON_EVENT_RMEM_BW);
395
396     const struct pqos_event_values *pv = &g_rdt->pgroups[i]->values;
397
398     /* Submit only monitored events data */
399
400     if (g_rdt->events[i] & PQOS_MON_EVENT_L3_OCCUP)
401       rdt_submit_gauge(cgroup->desc, "bytes", "llc", pv->llc);
402
403     if (g_rdt->events[i] & PQOS_PERF_EVENT_IPC)
404       rdt_submit_gauge(cgroup->desc, "ipc", NULL, pv->ipc);
405
406     if (g_rdt->events[i] & mbm_events) {
407       rdt_submit_derive(cgroup->desc, "memory_bandwidth", "local",
408                         pv->mbm_local_delta);
409       rdt_submit_derive(cgroup->desc, "memory_bandwidth", "remote",
410                         pv->mbm_remote_delta);
411     }
412   }
413
414   return 0;
415 }
416
417 static int rdt_init(void) {
418   int ret;
419
420   if (g_state == CONFIGURATION_ERROR)
421     return -1;
422
423   ret = rdt_preinit();
424   if (ret != 0)
425     return ret;
426
427   /* Start monitoring */
428   for (size_t i = 0; i < g_rdt->num_groups; i++) {
429     core_group_t *cg = g_rdt->cores.cgroups + i;
430
431     ret = pqos_mon_start(cg->num_cores, cg->cores, g_rdt->events[i],
432                          (void *)cg->desc, g_rdt->pgroups[i]);
433
434     if (ret != PQOS_RETVAL_OK)
435       ERROR(RDT_PLUGIN ": Error starting monitoring group %s (pqos status=%d)",
436             cg->desc, ret);
437   }
438
439   return 0;
440 }
441
442 static int rdt_shutdown(void) {
443   int ret;
444
445   DEBUG(RDT_PLUGIN ": rdt_shutdown.");
446
447   if (g_rdt == NULL)
448     return 0;
449
450   /* Stop monitoring */
451   for (size_t i = 0; i < g_rdt->num_groups; i++) {
452     pqos_mon_stop(g_rdt->pgroups[i]);
453   }
454
455   ret = pqos_fini();
456   if (ret != PQOS_RETVAL_OK)
457     ERROR(RDT_PLUGIN ": Error shutting down PQoS library.");
458
459   rdt_free_cgroups();
460   sfree(g_rdt);
461
462   return 0;
463 }
464
465 void module_register(void) {
466   plugin_register_init(RDT_PLUGIN, rdt_init);
467   plugin_register_complex_config(RDT_PLUGIN, rdt_config);
468   plugin_register_complex_read(NULL, RDT_PLUGIN, rdt_read, 0, NULL);
469   plugin_register_shutdown(RDT_PLUGIN, rdt_shutdown);
470 }