2 * collectd - src/intel_rdt.c
4 * Copyright(c) 2016-2018 Intel Corporation. All rights reserved.
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all 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>
26 * Starzyk, Mateusz <mateuszx.starzyk@intel.com>
30 #include "utils/common/common.h"
31 #include "utils/config_cores/config_cores.h"
34 #define RDT_PLUGIN "intel_rdt"
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)
40 * Process name inside comm file is limited to 16 chars.
41 * More info here: http://man7.org/linux/man-pages/man5/proc.5.html
43 #define RDT_MAX_PROC_COMM_LENGTH 16
51 core_groups_list_t cores;
52 enum pqos_mon_event events[RDT_MAX_CORES];
53 struct pqos_mon_data *pgroups[RDT_MAX_CORES];
55 const struct pqos_cpuinfo *pqos_cpu;
56 const struct pqos_cap *pqos_cap;
57 const struct pqos_capability *cap_mon;
59 typedef struct rdt_ctx_s rdt_ctx_t;
61 static rdt_ctx_t *g_rdt;
63 static rdt_config_status g_state = UNKNOWN;
66 static void rdt_dump_cgroups(void) {
67 char cores[RDT_MAX_CORES * 4];
72 DEBUG(RDT_PLUGIN ": Core Groups Dump");
73 DEBUG(RDT_PLUGIN ": groups count: %" PRIsz, g_rdt->num_groups);
75 for (size_t i = 0; i < g_rdt->num_groups; i++) {
76 core_group_t *cgroup = g_rdt->cores.cgroups + i;
78 memset(cores, 0, sizeof(cores));
79 for (size_t j = 0; j < cgroup->num_cores; j++) {
80 snprintf(cores + strlen(cores), sizeof(cores) - strlen(cores) - 1, " %d",
84 DEBUG(RDT_PLUGIN ": group[%zu]:", i);
85 DEBUG(RDT_PLUGIN ": description: %s", cgroup->desc);
86 DEBUG(RDT_PLUGIN ": cores: %s", cores);
87 DEBUG(RDT_PLUGIN ": events: 0x%X", g_rdt->events[i]);
93 static inline double bytes_to_kb(const double bytes) { return bytes / 1024.0; }
95 static inline double bytes_to_mb(const double bytes) {
96 return bytes / (1024.0 * 1024.0);
99 static void rdt_dump_data(void) {
101 * CORE - monitored group of cores
102 * RMID - Resource Monitoring ID associated with the monitored group
103 * LLC - last level cache occupancy
104 * MBL - local memory bandwidth
105 * MBR - remote memory bandwidth
107 DEBUG(" CORE RMID LLC[KB] MBL[MB] MBR[MB]");
108 for (size_t i = 0; i < g_rdt->num_groups; i++) {
109 const struct pqos_event_values *pv = &g_rdt->pgroups[i]->values;
111 double llc = bytes_to_kb(pv->llc);
112 double mbr = bytes_to_mb(pv->mbm_remote_delta);
113 double mbl = bytes_to_mb(pv->mbm_local_delta);
115 DEBUG(" [%s] %8u %10.1f %10.1f %10.1f", g_rdt->cores.cgroups[i].desc,
116 g_rdt->pgroups[i]->poll_ctx[0].rmid, llc, mbl, mbr);
119 #endif /* COLLECT_DEBUG */
121 static void rdt_free_cgroups(void) {
122 config_cores_cleanup(&g_rdt->cores);
123 for (int i = 0; i < RDT_MAX_CORES; i++) {
124 sfree(g_rdt->pgroups[i]);
128 static int rdt_default_cgroups(void) {
129 unsigned num_cores = g_rdt->pqos_cpu->num_cores;
131 g_rdt->cores.cgroups = calloc(num_cores, sizeof(*g_rdt->cores.cgroups));
132 if (g_rdt->cores.cgroups == NULL) {
133 ERROR(RDT_PLUGIN ": Error allocating core groups array");
136 g_rdt->cores.num_cgroups = num_cores;
138 /* configure each core in separate group */
139 for (unsigned i = 0; i < num_cores; i++) {
140 core_group_t *cgroup = g_rdt->cores.cgroups + i;
141 char desc[DATA_MAX_NAME_LEN];
143 /* set core group info */
144 cgroup->cores = calloc(1, sizeof(*cgroup->cores));
145 if (cgroup->cores == NULL) {
146 ERROR(RDT_PLUGIN ": Error allocating cores array");
150 cgroup->num_cores = 1;
151 cgroup->cores[0] = i;
153 snprintf(desc, sizeof(desc), "%d", g_rdt->pqos_cpu->cores[i].lcore);
154 cgroup->desc = strdup(desc);
155 if (cgroup->desc == NULL) {
156 ERROR(RDT_PLUGIN ": Error allocating core group description");
165 static int rdt_is_core_id_valid(unsigned int core_id) {
167 for (unsigned int i = 0; i < g_rdt->pqos_cpu->num_cores; i++)
168 if (core_id == g_rdt->pqos_cpu->cores[i].lcore)
174 static int rdt_config_cgroups(oconfig_item_t *item) {
176 enum pqos_mon_event events = 0;
178 if (config_cores_parse(item, &g_rdt->cores) < 0) {
180 ERROR(RDT_PLUGIN ": Error parsing core groups configuration.");
183 n = g_rdt->cores.num_cgroups;
185 /* validate configured core id values */
186 for (size_t group_idx = 0; group_idx < n; group_idx++) {
187 core_group_t *cgroup = g_rdt->cores.cgroups + group_idx;
188 for (size_t core_idx = 0; core_idx < cgroup->num_cores; core_idx++) {
189 if (!rdt_is_core_id_valid(cgroup->cores[core_idx])) {
190 ERROR(RDT_PLUGIN ": Core group '%s' contains invalid core id '%u'",
191 cgroup->desc, cgroup->cores[core_idx]);
199 /* create default core groups if "Cores" config option is empty */
200 int ret = rdt_default_cgroups();
203 ERROR(RDT_PLUGIN ": Error creating default core groups configuration.");
208 ": No core groups configured. Default core groups created.");
211 /* Get all available events on this platform */
212 for (unsigned int i = 0; i < g_rdt->cap_mon->u.mon->num_events; i++)
213 events |= g_rdt->cap_mon->u.mon->events[i].type;
215 events &= ~(PQOS_PERF_EVENT_LLC_MISS);
217 DEBUG(RDT_PLUGIN ": Number of cores in the system: %u",
218 g_rdt->pqos_cpu->num_cores);
219 DEBUG(RDT_PLUGIN ": Available events to monitor: %#x", events);
221 g_rdt->num_groups = n;
222 for (size_t i = 0; i < n; i++) {
223 for (size_t j = 0; j < i; j++) {
225 found = config_cores_cmp_cgroups(&g_rdt->cores.cgroups[j],
226 &g_rdt->cores.cgroups[i]);
229 ERROR(RDT_PLUGIN ": Cannot monitor same cores in different groups.");
234 g_rdt->events[i] = events;
235 g_rdt->pgroups[i] = calloc(1, sizeof(*g_rdt->pgroups[i]));
236 if (g_rdt->pgroups[i] == NULL) {
238 ERROR(RDT_PLUGIN ": Failed to allocate memory for monitoring data.");
246 /* Helper typedef for process name array
247 * Extra 1 char is added for string null termination.
249 typedef char proc_comm_t[RDT_MAX_PROC_COMM_LENGTH + 1];
251 /* Linked one-way list of pids. */
252 typedef struct pids_list_s {
254 struct pids_list_s *next;
257 /* Holds process name and list of pids assigned to that name */
258 typedef struct proc_pids_s {
259 proc_comm_t proccess_name;
268 * Adds pid at the end of the pids list.
269 * Allocates memory for new pid element, it is up to user to free it.
272 * `list' Head of target pids_list.
273 * `pid' Pid to be added.
276 * On success, returns 0.
277 * -1 on memory allocation error.
279 static int pids_list_add_pid(pids_list_t **list, const pid_t pid) {
280 pids_list_t *new_element = calloc(1, sizeof(*new_element));
282 if (new_element == NULL) {
283 ERROR(RDT_PLUGIN ": Alloc error\n");
286 new_element->pid = pid;
287 new_element->next = NULL;
289 pids_list_t **current = list;
290 while (*current != NULL) {
291 current = &((*current)->next);
293 *current = new_element;
302 * Reads process name from given pid directory.
303 * Strips new-line character (\n).
306 * `procfs_path` Path to systems proc directory (e.g. /proc)
307 * `pid_entry' Dirent for PID directory
308 * `name' Output buffer for process name, recommended proc_comm.
309 * `out_size' Output buffer size, recommended sizeof(proc_comm)
312 * On success, the number of read bytes (includes stripped \n).
313 * -1 on file open error
315 static int read_proc_name(const char *procfs_path,
316 const struct dirent *pid_entry, char *name,
317 const size_t out_size) {
322 memset(name, 0, out_size);
324 const char *comm_file_name = "comm";
326 char *path = ssnprintf_alloc("%s/%s/%s", procfs_path, pid_entry->d_name,
329 FILE *f = fopen(path, "r");
331 ERROR(RDT_PLUGIN ": Failed to open comm file, error: %d\n", errno);
335 size_t read_length = fread(name, sizeof(char), out_size, f);
338 /* strip new line ending */
339 char *newline = strchr(name, '\n');
352 * Gets pid number for given /proc/pid directory entry or
353 * returns error if input directory does not hold PID information.
356 * `entry' Dirent for PID directory
357 * `pid' PID number to be filled
360 * 0 on success. Negative number on error:
361 * -1: given entry is not a directory
362 * -2: PID conversion error
364 static int get_pid_number(struct dirent *entry, pid_t *pid) {
365 char *tmp_end; /* used for strtoul error check*/
367 if (pid == NULL || entry == NULL)
370 if (entry->d_type != DT_DIR)
373 /* trying to get pid number from directory name*/
374 *pid = strtoul(entry->d_name, &tmp_end, 10);
375 if (*tmp_end != '\0') {
376 return -2; /* conversion failed, not proc-pid */
378 /* all checks passed, marking as success */
384 * fetch_pids_for_procs
387 * Finds PIDs matching given process's names.
388 * Searches all PID directories in /proc fs and
389 * allocates memory for proc_pids structs, it is up to user to free it.
390 * Output array will have same element count as input array.
393 * `procfs_path' Path to systems proc directory (e.g. /proc)
394 * `procs' Array of null-terminated strings with
395 * process' names to search for
396 * `procs_size' procs array element count
397 * `proc_pids_array' Address of pointer, under which new
398 * array of proc_pids will be allocated. Must be NULL.
401 * 0 on success. Negative number on error:
402 * -1: could not open /proc dir
404 __attribute__((unused)) /* TODO: remove this attribute when PID monitoring is
407 fetch_pids_for_procs(const char *procfs_path, const char **procs_names_array,
408 const size_t procs_names_array_size,
409 proc_pids_t **proc_pids_array) {
411 assert(procs_names_array);
412 assert(procs_names_array_size);
413 assert(proc_pids_array);
414 assert(NULL == *proc_pids_array);
416 DIR *proc_dir = opendir(procfs_path);
417 if (proc_dir == NULL) {
418 ERROR(RDT_PLUGIN ": Could not open %s directory, error: %d", procfs_path,
423 /* Copy procs names to output array. Initialize pids list with NULL value. */
425 calloc(procs_names_array_size, sizeof(**proc_pids_array));
426 for (size_t i = 0; i < procs_names_array_size; ++i) {
427 sstrncpy((*proc_pids_array)[i].proccess_name, procs_names_array[i],
428 STATIC_ARRAY_SIZE((*proc_pids_array)[i].proccess_name));
429 (*proc_pids_array)[i].pids = NULL;
432 /* Go through procfs and find PIDS and their comms */
433 struct dirent *entry;
434 while ((entry = readdir(proc_dir)) != NULL) {
437 int pid_conversion = get_pid_number(entry, &pid);
438 if (pid_conversion < 0)
443 read_proc_name(procfs_path, entry, comm, sizeof(proc_comm_t));
444 if (read_result <= 0) {
445 ERROR(RDT_PLUGIN ": Comm file skipped. Read result: %d", read_result);
449 /* Try to find comm in input procs array (proc_pids_array has same names) */
450 for (size_t i = 0; i < procs_names_array_size; ++i) {
451 if (0 == strncmp(comm, (*proc_pids_array)[i].proccess_name,
452 STATIC_ARRAY_SIZE(comm)))
453 pids_list_add_pid(&((*proc_pids_array)[i].pids), pid);
457 int close_result = closedir(proc_dir);
458 if (0 != close_result) {
459 ERROR(RDT_PLUGIN ": failed to close %s directory, error: %d", procfs_path,
466 static void rdt_pqos_log(void *context, const size_t size, const char *msg) {
467 DEBUG(RDT_PLUGIN ": %s", msg);
470 static int rdt_preinit(void) {
474 /* already initialized if config callback was called before init callback */
478 g_rdt = calloc(1, sizeof(*g_rdt));
480 ERROR(RDT_PLUGIN ": Failed to allocate memory for rdt context.");
484 struct pqos_config pqos = {.fd_log = -1,
485 .callback_log = rdt_pqos_log,
489 ret = pqos_init(&pqos);
490 if (ret != PQOS_RETVAL_OK) {
491 ERROR(RDT_PLUGIN ": Error initializing PQoS library!");
492 goto rdt_preinit_error1;
495 ret = pqos_cap_get(&g_rdt->pqos_cap, &g_rdt->pqos_cpu);
496 if (ret != PQOS_RETVAL_OK) {
497 ERROR(RDT_PLUGIN ": Error retrieving PQoS capabilities.");
498 goto rdt_preinit_error2;
501 ret = pqos_cap_get_type(g_rdt->pqos_cap, PQOS_CAP_TYPE_MON, &g_rdt->cap_mon);
502 if (ret == PQOS_RETVAL_PARAM) {
503 ERROR(RDT_PLUGIN ": Error retrieving monitoring capabilities.");
504 goto rdt_preinit_error2;
507 if (g_rdt->cap_mon == NULL) {
510 ": Monitoring capability not detected. Nothing to do for the plugin.");
511 goto rdt_preinit_error2;
514 /* Reset pqos monitoring groups registers */
529 static int rdt_config(oconfig_item_t *ci) {
530 if (rdt_preinit() != 0) {
531 g_state = CONFIGURATION_ERROR;
532 /* if we return -1 at this point collectd
533 reports a failure in configuration and
539 for (int i = 0; i < ci->children_num; i++) {
540 oconfig_item_t *child = ci->children + i;
542 if (strcasecmp("Cores", child->key) == 0) {
543 if (rdt_config_cgroups(child) != 0) {
544 g_state = CONFIGURATION_ERROR;
545 /* if we return -1 at this point collectd
546 reports a failure in configuration and
554 #endif /* COLLECT_DEBUG */
556 ERROR(RDT_PLUGIN ": Unknown configuration parameter \"%s\".", child->key);
563 static void rdt_submit_derive(const char *cgroup, const char *type,
564 const char *type_instance, derive_t value) {
565 value_list_t vl = VALUE_LIST_INIT;
567 vl.values = &(value_t){.derive = value};
570 sstrncpy(vl.plugin, RDT_PLUGIN, sizeof(vl.plugin));
571 snprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%s", cgroup);
572 sstrncpy(vl.type, type, sizeof(vl.type));
574 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
576 plugin_dispatch_values(&vl);
579 static void rdt_submit_gauge(const char *cgroup, const char *type,
580 const char *type_instance, gauge_t value) {
581 value_list_t vl = VALUE_LIST_INIT;
583 vl.values = &(value_t){.gauge = value};
586 sstrncpy(vl.plugin, RDT_PLUGIN, sizeof(vl.plugin));
587 snprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%s", cgroup);
588 sstrncpy(vl.type, type, sizeof(vl.type));
590 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
592 plugin_dispatch_values(&vl);
595 static int rdt_read(__attribute__((unused)) user_data_t *ud) {
599 ERROR(RDT_PLUGIN ": rdt_read: plugin not initialized.");
603 ret = pqos_mon_poll(&g_rdt->pgroups[0], (unsigned)g_rdt->num_groups);
604 if (ret != PQOS_RETVAL_OK) {
605 ERROR(RDT_PLUGIN ": Failed to poll monitoring data.");
611 #endif /* COLLECT_DEBUG */
613 for (size_t i = 0; i < g_rdt->num_groups; i++) {
614 core_group_t *cgroup = g_rdt->cores.cgroups + i;
616 enum pqos_mon_event mbm_events =
617 (PQOS_MON_EVENT_LMEM_BW | PQOS_MON_EVENT_TMEM_BW |
618 PQOS_MON_EVENT_RMEM_BW);
620 const struct pqos_event_values *pv = &g_rdt->pgroups[i]->values;
622 /* Submit only monitored events data */
624 if (g_rdt->events[i] & PQOS_MON_EVENT_L3_OCCUP)
625 rdt_submit_gauge(cgroup->desc, "bytes", "llc", pv->llc);
627 if (g_rdt->events[i] & PQOS_PERF_EVENT_IPC)
628 rdt_submit_gauge(cgroup->desc, "ipc", NULL, pv->ipc);
630 if (g_rdt->events[i] & mbm_events) {
631 rdt_submit_derive(cgroup->desc, "memory_bandwidth", "local",
632 pv->mbm_local_delta);
633 rdt_submit_derive(cgroup->desc, "memory_bandwidth", "remote",
634 pv->mbm_remote_delta);
641 static int rdt_init(void) {
644 if (g_state == CONFIGURATION_ERROR)
651 /* Start monitoring */
652 for (size_t i = 0; i < g_rdt->num_groups; i++) {
653 core_group_t *cg = g_rdt->cores.cgroups + i;
655 ret = pqos_mon_start(cg->num_cores, cg->cores, g_rdt->events[i],
656 (void *)cg->desc, g_rdt->pgroups[i]);
658 if (ret != PQOS_RETVAL_OK)
659 ERROR(RDT_PLUGIN ": Error starting monitoring group %s (pqos status=%d)",
666 static int rdt_shutdown(void) {
669 DEBUG(RDT_PLUGIN ": rdt_shutdown.");
674 /* Stop monitoring */
675 for (size_t i = 0; i < g_rdt->num_groups; i++) {
676 pqos_mon_stop(g_rdt->pgroups[i]);
680 if (ret != PQOS_RETVAL_OK)
681 ERROR(RDT_PLUGIN ": Error shutting down PQoS library.");
689 void module_register(void) {
690 plugin_register_init(RDT_PLUGIN, rdt_init);
691 plugin_register_complex_config(RDT_PLUGIN, rdt_config);
692 plugin_register_complex_read(NULL, RDT_PLUGIN, rdt_read, 0, NULL);
693 plugin_register_shutdown(RDT_PLUGIN, rdt_shutdown);