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>
27 * Wojciech Andralojc <wojciechx.andralojc@intel.com>
31 #include "utils/common/common.h"
32 #include "utils/config_cores/config_cores.h"
35 #define RDT_PLUGIN "intel_rdt"
37 /* libpqos v2.0 or newer is required for process monitoring*/
39 #if defined(PQOS_VERSION) && PQOS_VERSION >= 20000
43 #define RDT_PLUGIN "intel_rdt"
45 #define RDT_MAX_SOCKETS 8
46 #define RDT_MAX_SOCKET_CORES 64
47 #define RDT_MAX_CORES (RDT_MAX_SOCKET_CORES * RDT_MAX_SOCKETS)
51 * Process name inside comm file is limited to 16 chars.
52 * More info here: http://man7.org/linux/man-pages/man5/proc.5.html
54 #define RDT_MAX_NAME_LEN 16
55 #define RDT_MAX_NAMES_GROUPS 64
57 #define RDT_PROC_PATH "/proc"
66 /* Helper typedef for process name array
67 * Extra 1 char is added for string null termination.
69 typedef char proc_comm_t[RDT_MAX_NAME_LEN + 1];
71 /* Linked one-way list of pids. */
72 typedef struct pids_list_s {
74 struct pids_list_s *next;
77 /* Holds process name and list of pids assigned to that name */
78 typedef struct proc_pids_s {
79 proc_comm_t proccess_name;
83 struct rdt_name_group_s {
87 proc_pids_t *proc_pids_array;
88 enum pqos_mon_event events;
90 typedef struct rdt_name_group_s rdt_name_group_t;
94 core_groups_list_t cores;
95 enum pqos_mon_event events[RDT_MAX_CORES];
96 struct pqos_mon_data *pcgroups[RDT_MAX_CORES];
98 rdt_name_group_t ngroups[RDT_MAX_NAMES_GROUPS];
99 struct pqos_mon_data *pngroups[RDT_MAX_NAMES_GROUPS];
101 #endif /* LIBPQOS2 */
102 const struct pqos_cpuinfo *pqos_cpu;
103 const struct pqos_cap *pqos_cap;
104 const struct pqos_capability *cap_mon;
106 typedef struct rdt_ctx_s rdt_ctx_t;
108 static rdt_ctx_t *g_rdt;
110 static rdt_config_status g_state = UNKNOWN;
113 static int isdupstr(const char *names[], const size_t size, const char *name) {
114 for (size_t i = 0; i < size; i++)
115 if (strncmp(names[i], name, (size_t)RDT_MAX_NAME_LEN) == 0)
126 * Converts string representing list of strings into array of strings.
128 * name,name1,name2,name3
131 * `str_list' String representing list of strings.
132 * `names' Array to put extracted strings into.
133 * `names_num' Variable to put number of extracted strings.
136 * Number of elements placed into names.
138 static int strlisttoarray(char *str_list, char ***names, size_t *names_num) {
139 char *saveptr = NULL;
141 if (str_list == NULL || names == NULL)
145 char *token = strtok_r(str_list, ",", &saveptr);
151 while (isspace(*token))
157 if (!(isdupstr((const char **)*names, *names_num, token)))
158 if (0 != strarray_add(names, names_num, token)) {
159 ERROR(RDT_PLUGIN ": Error allocating process name string");
172 * Function to compare names in two name groups.
175 * `ng_a' Pointer to name group a.
176 * `ng_b' Pointer to name group b.
179 * 1 if both groups contain the same names
180 * 0 if none of their names match
181 * -1 if some but not all names match
183 static int ngroup_cmp(const rdt_name_group_t *ng_a,
184 const rdt_name_group_t *ng_b) {
187 assert(ng_a != NULL);
188 assert(ng_b != NULL);
190 const size_t sz_a = (unsigned)ng_a->num_names;
191 const size_t sz_b = (unsigned)ng_b->num_names;
192 const char **tab_a = (const char **)ng_a->names;
193 const char **tab_b = (const char **)ng_b->names;
195 for (size_t i = 0; i < sz_a; i++) {
196 for (size_t j = 0; j < sz_b; j++)
197 if (strncmp(tab_a[i], tab_b[j], (size_t)RDT_MAX_NAME_LEN) == 0)
200 /* if no names are the same */
203 /* if group contains same names */
204 if (sz_a == sz_b && sz_b == (size_t)found)
206 /* if not all names are the same */
215 * Function to set the descriptions and names for each process names group.
216 * Takes a config option containing list of strings that are used to set
217 * process group values.
220 * `item' Config option containing process names groups.
221 * `groups' Table of process name groups to set values in.
222 * `max_groups' Maximum number of process name groups allowed.
225 * On success, the number of name groups set up. On error, appropriate
226 * negative error value.
228 static int oconfig_to_ngroups(const oconfig_item_t *item,
229 rdt_name_group_t *groups,
230 const size_t max_groups) {
233 assert(groups != NULL);
234 assert(max_groups > 0);
235 assert(item != NULL);
237 for (int j = 0; j < item->values_num; j++) {
239 char value[DATA_MAX_NAME_LEN];
241 if ((item->values[j].value.string == NULL) ||
242 (strlen(item->values[j].value.string) == 0))
245 sstrncpy(value, item->values[j].value.string, sizeof(value));
247 ret = strlisttoarray(value, &groups[index].names, &groups[index].num_names);
248 if (ret != 0 || groups[index].num_names == 0) {
249 ERROR(RDT_PLUGIN ": Error parsing process names group (%s)",
250 item->values[j].value.string);
254 /* set group description info */
255 groups[index].desc = sstrdup(item->values[j].value.string);
256 if (groups[index].desc == NULL) {
257 ERROR(RDT_PLUGIN ": Error allocating name group description");
261 groups[index].proc_pids_array = NULL;
265 if (index >= (const int)max_groups) {
266 WARNING(RDT_PLUGIN ": Too many process names groups configured");
273 #endif /* LIBPQOS2 */
276 static void rdt_dump_cgroups(void) {
277 char cores[RDT_MAX_CORES * 4];
282 DEBUG(RDT_PLUGIN ": Core Groups Dump");
283 DEBUG(RDT_PLUGIN ": groups count: %" PRIsz, g_rdt->cores.num_cgroups);
285 for (size_t i = 0; i < g_rdt->cores.num_cgroups; i++) {
286 core_group_t *cgroup = g_rdt->cores.cgroups + i;
288 memset(cores, 0, sizeof(cores));
289 for (size_t j = 0; j < cgroup->num_cores; j++) {
290 snprintf(cores + strlen(cores), sizeof(cores) - strlen(cores) - 1, " %d",
294 DEBUG(RDT_PLUGIN ": group[%zu]:", i);
295 DEBUG(RDT_PLUGIN ": description: %s", cgroup->desc);
296 DEBUG(RDT_PLUGIN ": cores: %s", cores);
297 DEBUG(RDT_PLUGIN ": events: 0x%X", g_rdt->events[i]);
304 static void rdt_dump_ngroups(void) {
306 char names[DATA_MAX_NAME_LEN];
311 DEBUG(RDT_PLUGIN ": Process Names Groups Dump");
312 DEBUG(RDT_PLUGIN ": groups count: %" PRIsz, g_rdt->num_ngroups);
314 for (size_t i = 0; i < g_rdt->num_ngroups; i++) {
315 memset(names, 0, sizeof(names));
316 for (size_t j = 0; j < g_rdt->ngroups[i].num_names; j++)
317 snprintf(names + strlen(names), sizeof(names) - strlen(names) - 1, " %s",
318 g_rdt->ngroups[i].names[j]);
320 DEBUG(RDT_PLUGIN ": group[%d]:", (int)i);
321 DEBUG(RDT_PLUGIN ": description: %s", g_rdt->ngroups[i].desc);
322 DEBUG(RDT_PLUGIN ": process names:%s", names);
323 DEBUG(RDT_PLUGIN ": events: 0x%X", g_rdt->ngroups[i].events);
328 #endif /* LIBPQOS2 */
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 * NAME - monitored group of processes
340 * RMID - Resource Monitoring ID associated with the monitored group
341 * LLC - last level cache occupancy
342 * MBL - local memory bandwidth
343 * MBR - remote memory bandwidth
345 DEBUG(" CORE RMID LLC[KB] MBL[MB] MBR[MB]");
346 for (size_t i = 0; i < g_rdt->cores.num_cgroups; i++) {
348 const struct pqos_event_values *pv = &g_rdt->pcgroups[i]->values;
350 double llc = bytes_to_kb(pv->llc);
351 double mbr = bytes_to_mb(pv->mbm_remote_delta);
352 double mbl = bytes_to_mb(pv->mbm_local_delta);
354 DEBUG(" [%s] %8u %10.1f %10.1f %10.1f", g_rdt->cores.cgroups[i].desc,
355 g_rdt->pcgroups[i]->poll_ctx[0].rmid, llc, mbl, mbr);
360 char pids[DATA_MAX_NAME_LEN];
361 for (size_t i = 0; i < g_rdt->num_ngroups; ++i) {
362 memset(pids, 0, sizeof(pids));
363 for (size_t j = 0; j < g_rdt->ngroups[i].num_names; ++j) {
364 pids_list_t *list = g_rdt->ngroups[i].proc_pids_array[j].pids;
365 while (list != NULL) {
366 snprintf(pids + strlen(pids), sizeof(pids) - strlen(pids) - 1, " %u",
371 DEBUG(" [%s] %s", g_rdt->ngroups[i].desc, pids);
374 DEBUG(" NAME RMID LLC[KB] MBL[MB] MBR[MB]");
375 for (size_t i = 0; i < g_rdt->num_ngroups; i++) {
377 if (g_rdt->pngroups[i]->poll_ctx == NULL)
380 const struct pqos_event_values *pv = &g_rdt->pngroups[i]->values;
382 double llc = bytes_to_kb(pv->llc);
383 double mbr = bytes_to_mb(pv->mbm_remote_delta);
384 double mbl = bytes_to_mb(pv->mbm_local_delta);
386 DEBUG(" [%s] %8u %10.1f %10.1f %10.1f", g_rdt->ngroups[i].desc,
387 g_rdt->pngroups[i]->poll_ctx[0].rmid, llc, mbl, mbr);
389 #endif /* LIBPQOS2 */
391 #endif /* COLLECT_DEBUG */
393 static void rdt_free_cgroups(void) {
394 config_cores_cleanup(&g_rdt->cores);
395 for (int i = 0; i < RDT_MAX_CORES; i++) {
396 sfree(g_rdt->pcgroups[i]);
401 static int pids_list_free(pids_list_t *list) {
404 pids_list_t *current = list;
405 while (current != NULL) {
406 pids_list_t *previous = current;
407 current = current->next;
413 static void rdt_free_ngroups(void) {
414 for (int i = 0; i < RDT_MAX_NAMES_GROUPS; i++) {
415 DEBUG(RDT_PLUGIN ": Freeing \'%s\' group\'s data...",
416 g_rdt->ngroups[i].desc);
417 sfree(g_rdt->ngroups[i].desc);
418 strarray_free(g_rdt->ngroups[i].names, g_rdt->ngroups[i].num_names);
420 if (g_rdt->ngroups[i].proc_pids_array) {
421 for (size_t j = 0; j < g_rdt->ngroups[i].num_names; ++j) {
422 if (NULL == g_rdt->ngroups[i].proc_pids_array[j].pids)
424 pids_list_free(g_rdt->ngroups[i].proc_pids_array[j].pids);
427 sfree(g_rdt->ngroups[i].proc_pids_array);
430 g_rdt->ngroups[i].num_names = 0;
431 sfree(g_rdt->pngroups[i]);
434 #endif /* LIBPQOS2 */
436 static int rdt_default_cgroups(void) {
437 unsigned num_cores = g_rdt->pqos_cpu->num_cores;
439 g_rdt->cores.cgroups = calloc(num_cores, sizeof(*g_rdt->cores.cgroups));
440 if (g_rdt->cores.cgroups == NULL) {
441 ERROR(RDT_PLUGIN ": Error allocating core groups array");
444 g_rdt->cores.num_cgroups = num_cores;
446 /* configure each core in separate group */
447 for (unsigned i = 0; i < num_cores; i++) {
448 core_group_t *cgroup = g_rdt->cores.cgroups + i;
449 char desc[DATA_MAX_NAME_LEN];
451 /* set core group info */
452 cgroup->cores = calloc(1, sizeof(*cgroup->cores));
453 if (cgroup->cores == NULL) {
454 ERROR(RDT_PLUGIN ": Error allocating cores array");
458 cgroup->num_cores = 1;
459 cgroup->cores[0] = i;
461 snprintf(desc, sizeof(desc), "%d", g_rdt->pqos_cpu->cores[i].lcore);
462 cgroup->desc = strdup(desc);
463 if (cgroup->desc == NULL) {
464 ERROR(RDT_PLUGIN ": Error allocating core group description");
473 static int rdt_is_core_id_valid(unsigned int core_id) {
475 for (unsigned int i = 0; i < g_rdt->pqos_cpu->num_cores; i++)
476 if (core_id == g_rdt->pqos_cpu->cores[i].lcore)
483 static int rdt_is_proc_name_valid(const char *name) {
486 unsigned len = strlen(name);
487 if (len > 0 && len <= RDT_MAX_NAME_LEN)
491 ": Process name \'%s\' is too long. Max supported len is %d chars.",
492 name, RDT_MAX_NAME_LEN);
498 #endif /* LIBPQOS2 */
500 static int rdt_config_cgroups(oconfig_item_t *item) {
502 enum pqos_mon_event events = 0;
504 if (config_cores_parse(item, &g_rdt->cores) < 0) {
506 ERROR(RDT_PLUGIN ": Error parsing core groups configuration.");
509 n = g_rdt->cores.num_cgroups;
511 /* validate configured core id values */
512 for (size_t group_idx = 0; group_idx < n; group_idx++) {
513 core_group_t *cgroup = g_rdt->cores.cgroups + group_idx;
514 for (size_t core_idx = 0; core_idx < cgroup->num_cores; core_idx++) {
515 if (!rdt_is_core_id_valid(cgroup->cores[core_idx])) {
516 ERROR(RDT_PLUGIN ": Core group '%s' contains invalid core id '%u'",
517 cgroup->desc, cgroup->cores[core_idx]);
525 /* create default core groups if "Cores" config option is empty */
526 int ret = rdt_default_cgroups();
529 ERROR(RDT_PLUGIN ": Error creating default core groups configuration.");
534 ": No core groups configured. Default core groups created.");
537 /* Get all available events on this platform */
538 for (unsigned int i = 0; i < g_rdt->cap_mon->u.mon->num_events; i++)
539 events |= g_rdt->cap_mon->u.mon->events[i].type;
541 events &= ~(PQOS_PERF_EVENT_LLC_MISS);
543 DEBUG(RDT_PLUGIN ": Number of cores in the system: %u",
544 g_rdt->pqos_cpu->num_cores);
545 DEBUG(RDT_PLUGIN ": Available events to monitor: %#x", events);
547 g_rdt->cores.num_cgroups = n;
548 for (int i = 0; i < n; i++) {
549 for (int j = 0; j < i; j++) {
551 found = config_cores_cmp_cgroups(&g_rdt->cores.cgroups[j],
552 &g_rdt->cores.cgroups[i]);
555 ERROR(RDT_PLUGIN ": Cannot monitor same cores in different groups.");
560 g_rdt->events[i] = events;
561 g_rdt->pcgroups[i] = calloc(1, sizeof(*g_rdt->pcgroups[i]));
562 if (g_rdt->pcgroups[i] == NULL) {
564 ERROR(RDT_PLUGIN ": Failed to allocate memory for monitoring data.");
573 static int rdt_config_ngroups(const oconfig_item_t *item) {
575 enum pqos_mon_event events = 0;
578 DEBUG(RDT_PLUGIN ": ngroups_config: Invalid argument.");
582 DEBUG(RDT_PLUGIN ": Process names groups [%d]:", item->values_num);
583 for (int j = 0; j < item->values_num; j++) {
584 if (item->values[j].type != OCONFIG_TYPE_STRING) {
586 ": given process names group value is not a string [idx=%d]",
590 DEBUG(RDT_PLUGIN ": [%d]: %s", j, item->values[j].value.string);
593 n = oconfig_to_ngroups(item, g_rdt->ngroups, RDT_MAX_NAMES_GROUPS);
596 ERROR(RDT_PLUGIN ": Error parsing process name groups configuration.");
600 /* validate configured process name values */
601 for (int group_idx = 0; group_idx < n; group_idx++) {
602 for (size_t name_idx = 0; name_idx < g_rdt->ngroups[group_idx].num_names;
604 if (!rdt_is_proc_name_valid(g_rdt->ngroups[group_idx].names[name_idx])) {
605 ERROR(RDT_PLUGIN ": Process name group '%s' contains invalid name '%s'",
606 g_rdt->ngroups[group_idx].desc,
607 g_rdt->ngroups[group_idx].names[name_idx]);
615 ERROR(RDT_PLUGIN ": Empty process name groups configured.");
619 /* Get all available events on this platform */
620 for (unsigned i = 0; i < g_rdt->cap_mon->u.mon->num_events; i++)
621 events |= g_rdt->cap_mon->u.mon->events[i].type;
623 events &= ~(PQOS_PERF_EVENT_LLC_MISS);
625 DEBUG(RDT_PLUGIN ": Available events to monitor: %#x", events);
627 g_rdt->num_ngroups = n;
628 for (int i = 0; i < n; i++) {
629 for (int j = 0; j < i; j++) {
630 int found = ngroup_cmp(&g_rdt->ngroups[j], &g_rdt->ngroups[i]);
634 ": Cannot monitor same process name in different groups.");
639 g_rdt->ngroups[i].events = events;
640 g_rdt->pngroups[i] = calloc(1, sizeof(*g_rdt->pngroups[i]));
641 if (g_rdt->pngroups[i] == NULL) {
644 ": Failed to allocate memory for process name monitoring data.");
657 * Adds pid at the end of the pids list.
658 * Allocates memory for new pid element, it is up to user to free it.
661 * `list' Head of target pids_list.
662 * `pid' Pid to be added.
665 * On success, returns 0.
666 * -1 on memory allocation error.
668 static int pids_list_add_pid(pids_list_t **list, const pid_t pid) {
671 pids_list_t *new_element = calloc(1, sizeof(*new_element));
673 if (new_element == NULL) {
674 ERROR(RDT_PLUGIN ": Alloc error\n");
677 new_element->pid = pid;
678 new_element->next = NULL;
680 pids_list_t **current = list;
681 while (*current != NULL) {
682 current = &((*current)->next);
684 *current = new_element;
690 * pids_list_contains_pid
693 * Tests if pids list contains specific pid.
696 * `list' Head of pids_list.
697 * `pid' Pid to be searched for.
700 * If PID found in list, returns 1,
701 * Otherwise returns 0.
703 static int pids_list_contains_pid(pids_list_t *list, const pid_t pid) {
706 pids_list_t *current = list;
707 while (current != NULL) {
708 if (current->pid == pid)
710 current = current->next;
717 * pids_list_add_pids_list
720 * Adds pids list at the end of the pids list.
721 * Allocates memory for new pid elements, it is up to user to free it.
722 * Increases dst_num by a number of added PIDs.
725 * `dst' Head of target PIDs list.
726 * `src' Head of source PIDs list.
727 * `dst_num' Variable to be increased by a number of appended PIDs.
730 * On success, returns 0.
731 * -1 on memory allocation error.
733 static int pids_list_add_pids_list(pids_list_t **dst, pids_list_t *src,
739 pids_list_t *current = src;
742 while (current != NULL) {
743 ret = pids_list_add_pid(dst, current->pid);
748 current = current->next;
759 * Reads process name from given pid directory.
760 * Strips new-line character (\n).
763 * `procfs_path` Path to systems proc directory (e.g. /proc)
764 * `pid_entry' Dirent for PID directory
765 * `name' Output buffer for process name, recommended proc_comm.
766 * `out_size' Output buffer size, recommended sizeof(proc_comm)
769 * On success, the number of read bytes (includes stripped \n).
770 * -1 on file open error
772 static int read_proc_name(const char *procfs_path,
773 const struct dirent *pid_entry, char *name,
774 const size_t out_size) {
779 memset(name, 0, out_size);
781 const char *comm_file_name = "comm";
783 char *path = ssnprintf_alloc("%s/%s/%s", procfs_path, pid_entry->d_name,
786 FILE *f = fopen(path, "r");
788 ERROR(RDT_PLUGIN ": Failed to open comm file, error: %d\n", errno);
792 size_t read_length = fread(name, sizeof(char), out_size, f);
795 /* strip new line ending */
796 char *newline = strchr(name, '\n');
809 * Gets pid number for given /proc/pid directory entry or
810 * returns error if input directory does not hold PID information.
813 * `entry' Dirent for PID directory
814 * `pid' PID number to be filled
817 * 0 on success. Negative number on error:
818 * -1: given entry is not a directory
819 * -2: PID conversion error
821 static int get_pid_number(struct dirent *entry, pid_t *pid) {
822 char *tmp_end; /* used for strtoul error check*/
824 if (pid == NULL || entry == NULL)
827 if (entry->d_type != DT_DIR)
830 /* trying to get pid number from directory name*/
831 *pid = strtoul(entry->d_name, &tmp_end, 10);
832 if (*tmp_end != '\0') {
833 return -2; /* conversion failed, not proc-pid */
835 /* all checks passed, marking as success */
841 * fetch_pids_for_procs
844 * Finds PIDs matching given process's names.
845 * Searches all PID directories in /proc fs and
846 * allocates memory for proc_pids structs, it is up to user to free it.
847 * Output array will have same element count as input array.
850 * `procfs_path' Path to systems proc directory (e.g. /proc)
851 * `procs' Array of null-terminated strings with
852 * process' names to search for
853 * `procs_size' procs array element count
854 * `proc_pids_array' Address of pointer, under which new
855 * array of proc_pids will be allocated. Must be NULL.
858 * 0 on success. Negative number on error:
859 * -1: could not open /proc dir
861 __attribute__((unused)) /* TODO: remove this attribute when PID monitoring is
864 fetch_pids_for_procs(const char *procfs_path, const char **procs_names_array,
865 const size_t procs_names_array_size,
866 proc_pids_t **proc_pids_array) {
868 assert(procs_names_array);
869 assert(procs_names_array_size);
870 assert(proc_pids_array);
871 assert(NULL == *proc_pids_array);
873 DIR *proc_dir = opendir(procfs_path);
874 if (proc_dir == NULL) {
875 ERROR(RDT_PLUGIN ": Could not open %s directory, error: %d", procfs_path,
880 /* Copy procs names to output array. Initialize pids list with NULL value. */
882 calloc(procs_names_array_size, sizeof(**proc_pids_array));
883 for (size_t i = 0; i < procs_names_array_size; ++i) {
884 sstrncpy((*proc_pids_array)[i].proccess_name, procs_names_array[i],
885 STATIC_ARRAY_SIZE((*proc_pids_array)[i].proccess_name));
886 (*proc_pids_array)[i].pids = NULL;
889 /* Go through procfs and find PIDS and their comms */
890 struct dirent *entry;
891 while ((entry = readdir(proc_dir)) != NULL) {
894 int pid_conversion = get_pid_number(entry, &pid);
895 if (pid_conversion < 0)
900 read_proc_name(procfs_path, entry, comm, sizeof(proc_comm_t));
901 if (read_result <= 0) {
902 ERROR(RDT_PLUGIN ": Comm file skipped. Read result: %d", read_result);
906 /* Try to find comm in input procs array (proc_pids_array has same names) */
907 for (size_t i = 0; i < procs_names_array_size; ++i) {
908 if (0 == strncmp(comm, (*proc_pids_array)[i].proccess_name,
909 STATIC_ARRAY_SIZE(comm)))
910 pids_list_add_pid(&((*proc_pids_array)[i].pids), pid);
914 int close_result = closedir(proc_dir);
915 if (0 != close_result) {
916 ERROR(RDT_PLUGIN ": failed to close %s directory, error: %d", procfs_path,
922 #endif /* LIBPQOS2 */
924 static void rdt_pqos_log(void *context, const size_t size, const char *msg) {
925 DEBUG(RDT_PLUGIN ": %s", msg);
928 static int rdt_preinit(void) {
932 /* already initialized if config callback was called before init callback */
936 g_rdt = calloc(1, sizeof(*g_rdt));
938 ERROR(RDT_PLUGIN ": Failed to allocate memory for rdt context.");
942 struct pqos_config pqos = {.fd_log = -1,
943 .callback_log = rdt_pqos_log,
947 ret = pqos_init(&pqos);
948 if (ret != PQOS_RETVAL_OK) {
949 ERROR(RDT_PLUGIN ": Error initializing PQoS library!");
950 goto rdt_preinit_error1;
953 ret = pqos_cap_get(&g_rdt->pqos_cap, &g_rdt->pqos_cpu);
954 if (ret != PQOS_RETVAL_OK) {
955 ERROR(RDT_PLUGIN ": Error retrieving PQoS capabilities.");
956 goto rdt_preinit_error2;
959 ret = pqos_cap_get_type(g_rdt->pqos_cap, PQOS_CAP_TYPE_MON, &g_rdt->cap_mon);
960 if (ret == PQOS_RETVAL_PARAM) {
961 ERROR(RDT_PLUGIN ": Error retrieving monitoring capabilities.");
962 goto rdt_preinit_error2;
965 if (g_rdt->cap_mon == NULL) {
968 ": Monitoring capability not detected. Nothing to do for the plugin.");
969 goto rdt_preinit_error2;
972 /* Reset pqos monitoring groups registers */
987 static int rdt_config(oconfig_item_t *ci) {
988 if (rdt_preinit() != 0) {
989 g_state = CONFIGURATION_ERROR;
990 /* if we return -1 at this point collectd
991 reports a failure in configuration and
997 for (int i = 0; i < ci->children_num; i++) {
998 oconfig_item_t *child = ci->children + i;
1000 if (strncasecmp("Cores", child->key, (size_t)strlen("Cores")) == 0) {
1001 if (rdt_config_cgroups(child) != 0) {
1002 g_state = CONFIGURATION_ERROR;
1003 /* if we return -1 at this point collectd
1004 reports a failure in configuration and
1012 #endif /* COLLECT_DEBUG */
1013 } else if (strncasecmp("Processes", child->key,
1014 (size_t)strlen("Processes")) == 0) {
1016 if (rdt_config_ngroups(child) != 0) {
1017 g_state = CONFIGURATION_ERROR;
1018 /* if we return -1 at this point collectd
1019 reports a failure in configuration and
1027 #endif /* COLLECT_DEBUG */
1028 #else /* !LIBPQOS2 */
1029 ERROR(RDT_PLUGIN ": Configuration parameter \"%s\" not supported, please "
1030 "recompile collectd with libpqos version 2.0 or newer.",
1032 #endif /* LIBPQOS2 */
1034 ERROR(RDT_PLUGIN ": Unknown configuration parameter \"%s\".", child->key);
1041 static void rdt_submit_derive(const char *cgroup, const char *type,
1042 const char *type_instance, derive_t value) {
1043 value_list_t vl = VALUE_LIST_INIT;
1045 vl.values = &(value_t){.derive = value};
1048 sstrncpy(vl.plugin, RDT_PLUGIN, sizeof(vl.plugin));
1049 snprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%s", cgroup);
1050 sstrncpy(vl.type, type, sizeof(vl.type));
1052 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
1054 plugin_dispatch_values(&vl);
1057 static void rdt_submit_gauge(const char *cgroup, const char *type,
1058 const char *type_instance, gauge_t value) {
1059 value_list_t vl = VALUE_LIST_INIT;
1061 vl.values = &(value_t){.gauge = value};
1064 sstrncpy(vl.plugin, RDT_PLUGIN, sizeof(vl.plugin));
1065 snprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%s", cgroup);
1066 sstrncpy(vl.type, type, sizeof(vl.type));
1068 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
1070 plugin_dispatch_values(&vl);
1074 static int rdt_pid_list_diff(pids_list_t *prev, pids_list_t *curr,
1075 pids_list_t **added, size_t *added_num,
1076 pids_list_t **removed, size_t *removed_num) {
1077 assert(prev || curr);
1082 /* append all PIDs from curr to added*/
1083 return pids_list_add_pids_list(added, curr, added_num);
1084 } else if (NULL == curr) {
1085 /* append all PIDs from prev to removed*/
1086 return pids_list_add_pids_list(removed, prev, removed_num);
1089 pids_list_t *item = prev;
1090 while (item != NULL) {
1091 if (0 == pids_list_contains_pid(curr, item->pid)) {
1092 pids_list_add_pid(removed, item->pid);
1099 while (item != NULL) {
1100 if (0 == pids_list_contains_pid(prev, item->pid)) {
1101 pids_list_add_pid(added, item->pid);
1110 static int rdt_refresh_ngroup(rdt_name_group_t *ngroup) {
1114 DEBUG(RDT_PLUGIN ": rdt_refresh_ngroup: \'%s\' process names group.",
1117 proc_pids_t *pids_array_prev = ngroup->proc_pids_array;
1118 proc_pids_t *pids_array_curr = NULL;
1121 fetch_pids_for_procs(RDT_PROC_PATH, (const char **)ngroup->names,
1122 ngroup->num_names, &pids_array_curr);
1124 if (0 != fetch_result) {
1125 ERROR(RDT_PLUGIN ": rdt_refresh_ngroup: failed to fetch PIDs for \'%s\' "
1126 "process names group.",
1128 return fetch_result;
1131 if (NULL == pids_array_prev) {
1132 /*no PIDs info yet, just save current one for next iteration*/
1133 ngroup->proc_pids_array = pids_array_curr;
1137 pids_list_t *added = NULL;
1138 size_t added_num = 0;
1140 pids_list_t *removed = NULL;
1141 size_t removed_num = 0;
1143 for (size_t i = 0; i < ngroup->num_names; ++i) {
1144 if (NULL == pids_array_prev[i].pids && NULL == pids_array_curr[i].pids)
1146 rdt_pid_list_diff(pids_array_prev[i].pids, pids_array_curr[i].pids, &added,
1147 &added_num, &removed, &removed_num);
1150 DEBUG(RDT_PLUGIN ": rdt_refresh_ngroup: \'%s\' process names group, added: "
1152 ngroup->desc, (unsigned)added_num, (unsigned)removed_num);
1154 if (added_num != 0 || removed_num != 0) {
1155 ngroup->proc_pids_array = pids_array_curr;
1157 /*call pqos add and remove functions here*/
1160 /*free prev PID lists, only if new was saved in ngroup struct*/
1161 if (pids_array_prev && pids_array_prev != ngroup->proc_pids_array) {
1162 for (size_t i = 0; i < ngroup->num_names; ++i) {
1163 if (NULL == pids_array_prev[i].pids)
1165 pids_list_free(pids_array_prev[i].pids);
1168 sfree(pids_array_prev);
1173 #endif /* LIBPQOS2 */
1175 static int rdt_read(__attribute__((unused)) user_data_t *ud) {
1178 if (g_rdt == NULL) {
1179 ERROR(RDT_PLUGIN ": rdt_read: plugin not initialized.");
1183 ret = pqos_mon_poll(&g_rdt->pcgroups[0], (unsigned)g_rdt->cores.num_cgroups);
1184 if (ret != PQOS_RETVAL_OK) {
1185 ERROR(RDT_PLUGIN ": Failed to poll monitoring data.");
1190 for (size_t i = 0; i < g_rdt->num_ngroups; i++)
1191 rdt_refresh_ngroup(&g_rdt->ngroups[i]);
1192 #endif /* LIBPQOS2 */
1196 #endif /* COLLECT_DEBUG */
1198 for (size_t i = 0; i < g_rdt->cores.num_cgroups; i++) {
1199 core_group_t *cgroup = g_rdt->cores.cgroups + i;
1200 enum pqos_mon_event mbm_events =
1201 (PQOS_MON_EVENT_LMEM_BW | PQOS_MON_EVENT_TMEM_BW |
1202 PQOS_MON_EVENT_RMEM_BW);
1204 const struct pqos_event_values *pv = &g_rdt->pcgroups[i]->values;
1206 /* Submit only monitored events data */
1208 if (g_rdt->events[i] & PQOS_MON_EVENT_L3_OCCUP)
1209 rdt_submit_gauge(cgroup->desc, "bytes", "llc", pv->llc);
1211 if (g_rdt->events[i] & PQOS_PERF_EVENT_IPC)
1212 rdt_submit_gauge(cgroup->desc, "ipc", NULL, pv->ipc);
1214 if (g_rdt->events[i] & mbm_events) {
1215 rdt_submit_derive(cgroup->desc, "memory_bandwidth", "local",
1216 pv->mbm_local_delta);
1217 rdt_submit_derive(cgroup->desc, "memory_bandwidth", "remote",
1218 pv->mbm_remote_delta);
1225 static int rdt_init(void) {
1228 if (g_state == CONFIGURATION_ERROR)
1231 ret = rdt_preinit();
1235 /* Start monitoring */
1236 for (size_t i = 0; i < g_rdt->cores.num_cgroups; i++) {
1237 core_group_t *cg = g_rdt->cores.cgroups + i;
1239 ret = pqos_mon_start(cg->num_cores, cg->cores, g_rdt->events[i],
1240 (void *)cg->desc, g_rdt->pcgroups[i]);
1242 if (ret != PQOS_RETVAL_OK)
1243 ERROR(RDT_PLUGIN ": Error starting monitoring group %s (pqos status=%d)",
1250 static int rdt_shutdown(void) {
1253 DEBUG(RDT_PLUGIN ": rdt_shutdown.");
1258 /* Stop monitoring */
1259 for (size_t i = 0; i < g_rdt->cores.num_cgroups; i++) {
1260 pqos_mon_stop(g_rdt->pcgroups[i]);
1264 if (ret != PQOS_RETVAL_OK)
1265 ERROR(RDT_PLUGIN ": Error shutting down PQoS library.");
1273 void module_register(void) {
1274 plugin_register_init(RDT_PLUGIN, rdt_init);
1275 plugin_register_complex_config(RDT_PLUGIN, rdt_config);
1276 plugin_register_complex_read(NULL, RDT_PLUGIN, rdt_read, 0, NULL);
1277 plugin_register_shutdown(RDT_PLUGIN, rdt_shutdown);