2 * collectd - src/intel_rdt.c
4 * Copyright(c) 2016 Intel Corporation. All rights reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a copy of
7 * this software and associated documentation files (the "Software"), to deal in
8 * the Software without restriction, including without limitation the rights to
9 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10 * of the Software, and to permit persons to whom the Software is furnished to do
11 * so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
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
25 * Serhiy Pshyk <serhiyx.pshyk@intel.com>
32 #define RDT_PLUGIN "intel_rdt"
34 #define RDT_MAX_SOCKETS 8
35 #define RDT_MAX_SOCKET_CORES 64
36 #define RDT_MAX_CORES (RDT_MAX_SOCKET_CORES * RDT_MAX_SOCKETS)
38 struct rdt_core_group_s {
42 enum pqos_mon_event events;
44 typedef struct rdt_core_group_s rdt_core_group_t;
47 rdt_core_group_t cgroups[RDT_MAX_CORES];
48 struct pqos_mon_data *pgroups[RDT_MAX_CORES];
50 const struct pqos_cpuinfo *pqos_cpu;
51 const struct pqos_cap *pqos_cap;
52 const struct pqos_capability *cap_mon;
54 typedef struct rdt_ctx_s rdt_ctx_t;
56 static rdt_ctx_t *g_rdt = NULL;
58 static int isdup(const uint64_t *nums, size_t size, uint64_t val) {
59 for (size_t i = 0; i < size; i++)
65 static int strtouint64(const char *s, uint64_t *n) {
71 *n = strtoull(s, &endptr, 0);
73 if (!(*s != '\0' && *endptr == '\0')) {
74 DEBUG(RDT_PLUGIN ": Error converting '%s' to unsigned number.", s);
86 * Converts string of characters representing list of numbers into array of
87 * numbers. Allowed formats are:
92 * Numbers can be in decimal or hexadecimal format.
95 * `s' String representing list of unsigned numbers.
96 * `nums' Array to put converted numeric values into.
97 * `max' Maximum number of elements that nums can accommodate.
100 * Number of elements placed into nums.
102 static size_t strlisttonums(char *s, uint64_t *nums, size_t max) {
105 char *saveptr = NULL;
107 if (s == NULL || nums == NULL || max == 0)
114 token = strtok_r(s, ",", &saveptr);
120 while (isspace(*token))
125 p = strchr(token, '-');
127 uint64_t n, start, end;
129 ret = strtouint64(token, &start);
132 ret = strtouint64(p + 1, &end);
138 for (n = start; n <= end; n++) {
139 if (!(isdup(nums, index, n))) {
149 ret = strtouint64(token, &val);
153 if (!(isdup(nums, index, val))) {
170 * Function to compare cores in 2 core groups.
173 * `cg_a' Pointer to core group a.
174 * `cg_b' Pointer to core group b.
177 * 1 if both groups contain the same cores
178 * 0 if none of their cores match
179 * -1 if some but not all cores match
181 static int cgroup_cmp(const rdt_core_group_t *cg_a,
182 const rdt_core_group_t *cg_b) {
185 assert(cg_a != NULL);
186 assert(cg_b != NULL);
188 const int sz_a = cg_a->num_cores;
189 const int sz_b = cg_b->num_cores;
190 const unsigned *tab_a = cg_a->cores;
191 const unsigned *tab_b = cg_b->cores;
193 for (int i = 0; i < sz_a; i++) {
194 for (int j = 0; j < sz_b; j++)
195 if (tab_a[i] == tab_b[j])
198 /* if no cores are the same */
201 /* if group contains same cores */
202 if (sz_a == sz_b && sz_b == found)
204 /* if not all cores are the same */
208 static int cgroup_set(rdt_core_group_t *cg, char *desc, uint64_t *cores,
211 assert(desc != NULL);
212 assert(cores != NULL);
213 assert(num_cores > 0);
215 cg->cores = calloc(num_cores, sizeof(unsigned));
216 if (cg->cores == NULL) {
217 ERROR(RDT_PLUGIN ": Error allocating core group table");
220 cg->num_cores = num_cores;
221 cg->desc = strdup(desc);
222 if (cg->desc == NULL) {
223 ERROR(RDT_PLUGIN ": Error allocating core group description");
228 for (size_t i = 0; i < num_cores; i++)
229 cg->cores[i] = (unsigned)cores[i];
239 * Function to set the descriptions and cores for each core group.
240 * Takes a config option containing list of strings that are used to set
244 * `item' Config option containing core groups.
245 * `groups' Table of core groups to set values in.
246 * `max_groups' Maximum number of core groups allowed.
247 * `max_core' Maximum allowed core value.
250 * On success, the number of core groups set up. On error, appropriate
251 * negative error value.
253 static int oconfig_to_cgroups(oconfig_item_t *item, rdt_core_group_t *groups,
254 size_t max_groups, uint64_t max_core) {
257 assert(groups != NULL);
258 assert(max_groups > 0);
259 assert(item != NULL);
261 for (int j = 0; j < item->values_num; j++) {
264 uint64_t cores[RDT_MAX_CORES] = {0};
265 char value[DATA_MAX_NAME_LEN];
267 if ((item->values[j].value.string == NULL) || (strlen(item->values[j].value.string) == 0))
270 sstrncpy(value, item->values[j].value.string, sizeof(value));
272 n = strlisttonums(value, cores, STATIC_ARRAY_SIZE(cores));
274 ERROR(RDT_PLUGIN ": Error parsing core group (%s)",
275 item->values[j].value.string);
279 for (int i = 0; i < n; i++) {
280 if (cores[i] > max_core) {
281 ERROR(RDT_PLUGIN ": Core group (%s) contains invalid core id (%d)",
282 item->values[j].value.string, (int)cores[i]);
287 /* set core group info */
288 ret = cgroup_set(&groups[index], item->values[j].value.string, cores, n);
294 if (index >= max_groups) {
295 WARNING(RDT_PLUGIN ": Too many core groups configured");
304 static void rdt_dump_cgroups(void) {
305 char cores[RDT_MAX_CORES * 4];
310 DEBUG(RDT_PLUGIN ": Core Groups Dump");
311 DEBUG(RDT_PLUGIN ": groups count: %zu", g_rdt->num_groups);
313 for (int i = 0; i < g_rdt->num_groups; i++) {
315 memset(cores, 0, sizeof(cores));
316 for (int j = 0; j < g_rdt->cgroups[i].num_cores; j++) {
317 snprintf(cores + strlen(cores), sizeof(cores) - strlen(cores) - 1, " %d",
318 g_rdt->cgroups[i].cores[j]);
321 DEBUG(RDT_PLUGIN ": group[%d]:", i);
322 DEBUG(RDT_PLUGIN ": description: %s", g_rdt->cgroups[i].desc);
323 DEBUG(RDT_PLUGIN ": cores: %s", cores);
324 DEBUG(RDT_PLUGIN ": events: 0x%X", g_rdt->cgroups[i].events);
330 static inline double bytes_to_kb(const double bytes) { return bytes / 1024.0; }
332 static inline double bytes_to_mb(const double bytes) {
333 return bytes / (1024.0 * 1024.0);
336 static void rdt_dump_data(void) {
338 * CORE - monitored group of cores
339 * RMID - Resource Monitoring ID associated with the monitored group
340 * LLC - last level cache occupancy
341 * MBL - local memory bandwidth
342 * MBR - remote memory bandwidth
344 DEBUG(" CORE RMID LLC[KB] MBL[MB] MBR[MB]");
345 for (int i = 0; i < g_rdt->num_groups; i++) {
347 const struct pqos_event_values *pv = &g_rdt->pgroups[i]->values;
349 double llc = bytes_to_kb(pv->llc);
350 double mbr = bytes_to_mb(pv->mbm_remote_delta);
351 double mbl = bytes_to_mb(pv->mbm_local_delta);
353 DEBUG(" [%s] %8u %10.1f %10.1f %10.1f", g_rdt->cgroups[i].desc,
354 g_rdt->pgroups[i]->poll_ctx[0].rmid, llc, mbl, mbr);
357 #endif /* COLLECT_DEBUG */
359 static void rdt_free_cgroups(void) {
360 for (int i = 0; i < RDT_MAX_CORES; i++) {
361 sfree(g_rdt->cgroups[i].desc);
363 sfree(g_rdt->cgroups[i].cores);
364 g_rdt->cgroups[i].num_cores = 0;
366 sfree(g_rdt->pgroups[i]);
370 static int rdt_default_cgroups(void) {
373 /* configure each core in separate group */
374 for (unsigned i = 0; i < g_rdt->pqos_cpu->num_cores; i++) {
375 char desc[DATA_MAX_NAME_LEN];
378 ssnprintf(desc, sizeof(desc), "%d", g_rdt->pqos_cpu->cores[i].lcore);
380 /* set core group info */
381 ret = cgroup_set(&g_rdt->cgroups[i], desc, &core, 1);
386 return g_rdt->pqos_cpu->num_cores;
389 static int rdt_config_cgroups(oconfig_item_t *item) {
391 enum pqos_mon_event events = 0;
394 DEBUG(RDT_PLUGIN ": cgroups_config: Invalid argument.");
398 DEBUG(RDT_PLUGIN ": Core groups [%d]:", item->values_num);
399 for (int j = 0; j < item->values_num; j++) {
400 if (item->values[j].type != OCONFIG_TYPE_STRING) {
401 ERROR(RDT_PLUGIN ": given core group value is not a string [idx=%d]",
405 DEBUG(RDT_PLUGIN ": [%d]: %s", j, item->values[j].value.string);
408 n = oconfig_to_cgroups(item, g_rdt->cgroups, RDT_MAX_CORES,
409 g_rdt->pqos_cpu->num_cores-1);
412 ERROR(RDT_PLUGIN ": Error parsing core groups configuration.");
417 /* create default core groups if "Cores" config option is empty */
418 n = rdt_default_cgroups();
422 ": Error creating default core groups configuration.");
426 ": No core groups configured. Default core groups created.");
429 /* Get all available events on this platform */
430 for (int i = 0; i < g_rdt->cap_mon->u.mon->num_events; i++)
431 events |= g_rdt->cap_mon->u.mon->events[i].type;
433 events &= ~(PQOS_PERF_EVENT_LLC_MISS);
435 DEBUG(RDT_PLUGIN ": Number of cores in the system: %u",
436 g_rdt->pqos_cpu->num_cores);
437 DEBUG(RDT_PLUGIN ": Available events to monitor: %#x", events);
439 g_rdt->num_groups = n;
440 for (int i = 0; i < n; i++) {
441 for (int j = 0; j < i; j++) {
443 found = cgroup_cmp(&g_rdt->cgroups[j], &g_rdt->cgroups[i]);
446 ERROR(RDT_PLUGIN ": Cannot monitor same cores in different groups.");
451 g_rdt->cgroups[i].events = events;
452 g_rdt->pgroups[i] = calloc(1, sizeof(*g_rdt->pgroups[i]));
453 if (g_rdt->pgroups[i] == NULL) {
455 ERROR(RDT_PLUGIN ": Failed to allocate memory for monitoring data.");
463 static int rdt_preinit(void) {
467 /* already initialized if config callback was called before init callback */
471 g_rdt = calloc(1, sizeof(*g_rdt));
473 ERROR(RDT_PLUGIN ": Failed to allocate memory for rdt context.");
477 /* In case previous instance of the application was not closed properly
478 * call fini and ignore return code. */
482 * stdout should not be used here. Will be reworked when support of log
483 * callback is added to PQoS library.
485 ret = pqos_init(&(struct pqos_config){.fd_log = STDOUT_FILENO});
486 if (ret != PQOS_RETVAL_OK) {
487 ERROR(RDT_PLUGIN ": Error initializing PQoS library!");
488 goto rdt_preinit_error1;
491 ret = pqos_cap_get(&g_rdt->pqos_cap, &g_rdt->pqos_cpu);
492 if (ret != PQOS_RETVAL_OK) {
493 ERROR(RDT_PLUGIN ": Error retrieving PQoS capabilities.");
494 goto rdt_preinit_error2;
497 ret = pqos_cap_get_type(g_rdt->pqos_cap, PQOS_CAP_TYPE_MON,
499 if (ret == PQOS_RETVAL_PARAM) {
500 ERROR(RDT_PLUGIN ": Error retrieving monitoring capabilities.");
501 goto rdt_preinit_error2;
504 if (g_rdt->cap_mon == NULL) {
507 ": Monitoring capability not detected. Nothing to do for the plugin.");
508 goto rdt_preinit_error2;
523 static int rdt_config(oconfig_item_t *ci) {
530 for (int i = 0; i < ci->children_num; i++) {
531 oconfig_item_t *child = ci->children + i;
533 if (strcasecmp("Cores", child->key) == 0) {
535 ret = rdt_config_cgroups(child);
541 #endif /* COLLECT_DEBUG */
544 ERROR(RDT_PLUGIN ": Unknown configuration parameter \"%s\".",
552 static void rdt_submit_derive(char *cgroup, char *type, char *type_instance,
554 value_list_t vl = VALUE_LIST_INIT;
556 vl.values = &(value_t) { .derive = value };
559 sstrncpy(vl.plugin, RDT_PLUGIN, sizeof(vl.plugin));
560 snprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%s", cgroup);
561 sstrncpy(vl.type, type, sizeof(vl.type));
563 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
565 plugin_dispatch_values(&vl);
568 static void rdt_submit_gauge(char *cgroup, char *type, char *type_instance,
570 value_list_t vl = VALUE_LIST_INIT;
572 vl.values = &(value_t) { .gauge = value };
575 sstrncpy(vl.plugin, RDT_PLUGIN, sizeof(vl.plugin));
576 snprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%s", cgroup);
577 sstrncpy(vl.type, type, sizeof(vl.type));
579 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
581 plugin_dispatch_values(&vl);
584 static int rdt_read(__attribute__((unused)) user_data_t *ud) {
588 ERROR(RDT_PLUGIN ": rdt_read: plugin not initialized.");
592 ret = pqos_mon_poll(&g_rdt->pgroups[0], (unsigned)g_rdt->num_groups);
593 if (ret != PQOS_RETVAL_OK) {
594 ERROR(RDT_PLUGIN ": Failed to poll monitoring data.");
600 #endif /* COLLECT_DEBUG */
602 for (int i = 0; i < g_rdt->num_groups; i++) {
603 enum pqos_mon_event mbm_events =
604 (PQOS_MON_EVENT_LMEM_BW | PQOS_MON_EVENT_TMEM_BW |
605 PQOS_MON_EVENT_RMEM_BW);
607 const struct pqos_event_values *pv = &g_rdt->pgroups[i]->values;
609 /* Submit only monitored events data */
611 if (g_rdt->cgroups[i].events & PQOS_MON_EVENT_L3_OCCUP)
612 rdt_submit_gauge(g_rdt->cgroups[i].desc, "bytes", "llc", pv->llc);
614 if (g_rdt->cgroups[i].events & PQOS_PERF_EVENT_IPC)
615 rdt_submit_gauge(g_rdt->cgroups[i].desc, "ipc", NULL, pv->ipc);
617 if (g_rdt->cgroups[i].events & mbm_events) {
618 rdt_submit_derive(g_rdt->cgroups[i].desc, "memory_bandwidth",
619 "local", pv->mbm_local_delta);
620 rdt_submit_derive(g_rdt->cgroups[i].desc, "memory_bandwidth",
621 "remote", pv->mbm_remote_delta);
628 static int rdt_init(void) {
635 /* Start monitoring */
636 for (int i = 0; i < g_rdt->num_groups; i++) {
637 rdt_core_group_t *cg = &g_rdt->cgroups[i];
639 ret = pqos_mon_start(cg->num_cores, cg->cores, cg->events, (void *)cg->desc,
642 if (ret != PQOS_RETVAL_OK)
643 ERROR(RDT_PLUGIN ": Error starting monitoring group %s (pqos status=%d)",
650 static int rdt_shutdown(void) {
653 DEBUG(RDT_PLUGIN ": rdt_shutdown.");
658 /* Stop monitoring */
659 for (int i = 0; i < g_rdt->num_groups; i++) {
660 pqos_mon_stop(g_rdt->pgroups[i]);
664 if (ret != PQOS_RETVAL_OK)
665 ERROR(RDT_PLUGIN ": Error shutting down PQoS library.");
673 void module_register(void) {
674 plugin_register_init(RDT_PLUGIN, rdt_init);
675 plugin_register_complex_config(RDT_PLUGIN, rdt_config);
676 plugin_register_complex_read(NULL, RDT_PLUGIN, rdt_read, 0, NULL);
677 plugin_register_shutdown(RDT_PLUGIN, rdt_shutdown);