intel_rdt: refactored proc utils to increase efficiency
[collectd.git] / src / intel_rdt.c
1 /**
2  * collectd - src/intel_rdt.c
3  *
4  * Copyright(c) 2016-2019 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  *   Starzyk, Mateusz <mateuszx.starzyk@intel.com>
27  *   Wojciech Andralojc <wojciechx.andralojc@intel.com>
28  *   Michał Aleksiński <michalx.aleksinski@intel.com>
29  **/
30
31 #include "collectd.h"
32 #include "utils/common/common.h"
33 #include "utils/config_cores/config_cores.h"
34 #include "utils_proc_pids.h"
35 #include <pqos.h>
36
37 #define RDT_PLUGIN "intel_rdt"
38
39 /* libpqos v2.0 or newer is required for process monitoring*/
40 #undef LIBPQOS2
41 #if defined(PQOS_VERSION) && PQOS_VERSION >= 20000
42 #define LIBPQOS2
43 #endif
44
45 #define RDT_PLUGIN "intel_rdt"
46
47 #define RDT_MAX_SOCKETS 8
48 #define RDT_MAX_SOCKET_CORES 64
49 #define RDT_MAX_CORES (RDT_MAX_SOCKET_CORES * RDT_MAX_SOCKETS)
50
51 #ifdef LIBPQOS2
52 /*
53  * Process name inside comm file is limited to 16 chars.
54  * More info here: http://man7.org/linux/man-pages/man5/proc.5.html
55  */
56 #define RDT_MAX_NAMES_GROUPS 64
57 #define RDT_PROC_PATH "/proc"
58 #endif /* LIBPQOS2 */
59
60 typedef enum {
61   UNKNOWN = 0,
62   CONFIGURATION_ERROR,
63 } rdt_config_status;
64
65 #ifdef LIBPQOS2
66 struct rdt_name_group_s {
67   char *desc;
68   size_t num_names;
69   char **names;
70   proc_pids_t **proc_pids;
71   size_t monitored_pids_count;
72   enum pqos_mon_event events;
73 };
74 typedef struct rdt_name_group_s rdt_name_group_t;
75 #endif /* LIBPQOS2 */
76
77 struct rdt_ctx_s {
78   core_groups_list_t cores;
79   enum pqos_mon_event events[RDT_MAX_CORES];
80   struct pqos_mon_data *pcgroups[RDT_MAX_CORES];
81 #ifdef LIBPQOS2
82   rdt_name_group_t ngroups[RDT_MAX_NAMES_GROUPS];
83   struct pqos_mon_data *pngroups[RDT_MAX_NAMES_GROUPS];
84   size_t num_ngroups;
85   proc_pids_t **proc_pids;
86   size_t num_proc_pids;
87 #endif /* LIBPQOS2 */
88   const struct pqos_cpuinfo *pqos_cpu;
89   const struct pqos_cap *pqos_cap;
90   const struct pqos_capability *cap_mon;
91 };
92 typedef struct rdt_ctx_s rdt_ctx_t;
93
94 static rdt_ctx_t *g_rdt;
95
96 static rdt_config_status g_state = UNKNOWN;
97
98 static int g_interface = -1;
99
100 static void rdt_submit_derive(const char *cgroup, const char *type,
101                               const char *type_instance, derive_t value) {
102   value_list_t vl = VALUE_LIST_INIT;
103
104   vl.values = &(value_t){.derive = value};
105   vl.values_len = 1;
106
107   sstrncpy(vl.plugin, RDT_PLUGIN, sizeof(vl.plugin));
108   snprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%s", cgroup);
109   sstrncpy(vl.type, type, sizeof(vl.type));
110   if (type_instance)
111     sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
112
113   plugin_dispatch_values(&vl);
114 }
115
116 static void rdt_submit_gauge(const char *cgroup, const char *type,
117                              const char *type_instance, gauge_t value) {
118   value_list_t vl = VALUE_LIST_INIT;
119
120   vl.values = &(value_t){.gauge = value};
121   vl.values_len = 1;
122
123   sstrncpy(vl.plugin, RDT_PLUGIN, sizeof(vl.plugin));
124   snprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%s", cgroup);
125   sstrncpy(vl.type, type, sizeof(vl.type));
126   if (type_instance)
127     sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
128
129   plugin_dispatch_values(&vl);
130 }
131
132 #if COLLECT_DEBUG
133 static void rdt_dump_cgroups(void) {
134   char cores[RDT_MAX_CORES * 4];
135
136   if (g_rdt == NULL)
137     return;
138
139   DEBUG(RDT_PLUGIN ": Core Groups Dump");
140   DEBUG(RDT_PLUGIN ":  groups count: %" PRIsz, g_rdt->cores.num_cgroups);
141
142   for (size_t i = 0; i < g_rdt->cores.num_cgroups; i++) {
143     core_group_t *cgroup = g_rdt->cores.cgroups + i;
144
145     memset(cores, 0, sizeof(cores));
146     for (size_t j = 0; j < cgroup->num_cores; j++) {
147       snprintf(cores + strlen(cores), sizeof(cores) - strlen(cores) - 1, " %d",
148                cgroup->cores[j]);
149     }
150
151     DEBUG(RDT_PLUGIN ":  group[%zu]:", i);
152     DEBUG(RDT_PLUGIN ":    description: %s", cgroup->desc);
153     DEBUG(RDT_PLUGIN ":    cores: %s", cores);
154     DEBUG(RDT_PLUGIN ":    events: 0x%X", g_rdt->events[i]);
155   }
156
157   return;
158 }
159
160 #ifdef LIBPQOS2
161 static void rdt_dump_ngroups(void) {
162
163   char names[DATA_MAX_NAME_LEN];
164
165   if (g_rdt == NULL)
166     return;
167
168   DEBUG(RDT_PLUGIN ": Process Names Groups Dump");
169   DEBUG(RDT_PLUGIN ":  groups count: %" PRIsz, g_rdt->num_ngroups);
170
171   for (size_t i = 0; i < g_rdt->num_ngroups; i++) {
172     memset(names, 0, sizeof(names));
173     for (size_t j = 0; j < g_rdt->ngroups[i].num_names; j++)
174       snprintf(names + strlen(names), sizeof(names) - strlen(names) - 1, " %s",
175                g_rdt->ngroups[i].names[j]);
176
177     DEBUG(RDT_PLUGIN ":  group[%d]:", (int)i);
178     DEBUG(RDT_PLUGIN ":    description: %s", g_rdt->ngroups[i].desc);
179     DEBUG(RDT_PLUGIN ":    process names:%s", names);
180     DEBUG(RDT_PLUGIN ":    events: 0x%X", g_rdt->ngroups[i].events);
181   }
182
183   return;
184 }
185 #endif /* LIBPQOS2 */
186
187 static inline double bytes_to_kb(const double bytes) { return bytes / 1024.0; }
188
189 static inline double bytes_to_mb(const double bytes) {
190   return bytes / (1024.0 * 1024.0);
191 }
192
193 static void rdt_dump_cores_data(void) {
194 /*
195  * CORE - monitored group of cores
196  * RMID - Resource Monitoring ID associated with the monitored group
197  *        This is not available for monitoring with resource control
198  * LLC - last level cache occupancy
199  * MBL - local memory bandwidth
200  * MBR - remote memory bandwidth
201  */
202 #ifdef LIBPQOS2
203   if (g_interface == PQOS_INTER_OS_RESCTRL_MON) {
204     DEBUG(RDT_PLUGIN ":  CORE     LLC[KB]   MBL[MB]    MBR[MB]");
205   } else {
206     DEBUG(RDT_PLUGIN ":  CORE     RMID    LLC[KB]   MBL[MB]    MBR[MB]");
207   }
208 #else
209   DEBUG(RDT_PLUGIN ":  CORE     RMID    LLC[KB]   MBL[MB]    MBR[MB]");
210 #endif /* LIBPQOS2 */
211
212   for (int i = 0; i < g_rdt->cores.num_cgroups; i++) {
213     const struct pqos_event_values *pv = &g_rdt->pcgroups[i]->values;
214
215     double llc = bytes_to_kb(pv->llc);
216     double mbr = bytes_to_mb(pv->mbm_remote_delta);
217     double mbl = bytes_to_mb(pv->mbm_local_delta);
218 #ifdef LIBPQOS2
219     if (g_interface == PQOS_INTER_OS_RESCTRL_MON) {
220       DEBUG(RDT_PLUGIN ": [%s] %10.1f %10.1f %10.1f",
221             g_rdt->cores.cgroups[i].desc, llc, mbl, mbr);
222     } else {
223       DEBUG(RDT_PLUGIN ": [%s] %8u %10.1f %10.1f %10.1f",
224             g_rdt->cores.cgroups[i].desc, g_rdt->pcgroups[i]->poll_ctx[0].rmid,
225             llc, mbl, mbr);
226     }
227 #else
228     DEBUG(RDT_PLUGIN ": [%s] %8u %10.1f %10.1f %10.1f",
229           g_rdt->cores.cgroups[i].desc, g_rdt->pcgroups[i]->poll_ctx[0].rmid,
230           llc, mbl, mbr);
231 #endif /* LIBPQOS2 */
232   }
233 }
234
235 #ifdef LIBPQOS2
236 static void rdt_dump_pids_data(void) {
237   /*
238    * NAME - monitored group of processes
239    * PIDs - list of PID numbers in the NAME group
240    * LLC - last level cache occupancy
241    * MBL - local memory bandwidth
242    * MBR - remote memory bandwidth
243    */
244
245   DEBUG(RDT_PLUGIN ":  NAME     PIDs");
246   char pids[DATA_MAX_NAME_LEN];
247   for (size_t i = 0; i < g_rdt->num_ngroups; ++i) {
248     memset(pids, 0, sizeof(pids));
249     for (size_t j = 0; j < g_rdt->ngroups[i].num_names; ++j) {
250       pids_list_t *list = g_rdt->ngroups[i].proc_pids[j].curr;
251       for (size_t k = 0; k < list->size; k++)
252         snprintf(pids + strlen(pids), sizeof(pids) - strlen(pids) - 1, " %u",
253                  list->pids[k]);
254     }
255     DEBUG(RDT_PLUGIN ":  [%s] %s", g_rdt->ngroups[i].desc, pids);
256   }
257
258   DEBUG(RDT_PLUGIN ":  NAME    LLC[KB]   MBL[MB]    MBR[MB]");
259   for (size_t i = 0; i < g_rdt->num_ngroups; i++) {
260
261     const struct pqos_event_values *pv = &g_rdt->pngroups[i]->values;
262
263     double llc = bytes_to_kb(pv->llc);
264     double mbr = bytes_to_mb(pv->mbm_remote_delta);
265     double mbl = bytes_to_mb(pv->mbm_local_delta);
266
267     DEBUG(RDT_PLUGIN ":  [%s] %10.1f %10.1f %10.1f", g_rdt->ngroups[i].desc,
268           llc, mbl, mbr);
269   }
270 }
271 #endif /* LIBPQOS2 */
272 #endif /* COLLECT_DEBUG */
273
274 #ifdef LIBPQOS2
275 static int isdupstr(const char *names[], const size_t size, const char *name) {
276   for (size_t i = 0; i < size; i++)
277     if (strncmp(names[i], name, (size_t)MAX_PROC_NAME_LEN) == 0)
278       return 1;
279
280   return 0;
281 }
282
283 /*
284  * NAME
285  *   strlisttoarray
286  *
287  * DESCRIPTION
288  *   Converts string representing list of strings into array of strings.
289  *   Allowed format is:
290  *     name,name1,name2,name3
291  *
292  * PARAMETERS
293  *   `str_list'  String representing list of strings.
294  *   `names'     Array to put extracted strings into.
295  *   `names_num' Variable to put number of extracted strings.
296  *
297  * RETURN VALUE
298  *    Number of elements placed into names.
299  */
300 static int strlisttoarray(char *str_list, char ***names, size_t *names_num) {
301   char *saveptr = NULL;
302
303   if (str_list == NULL || names == NULL)
304     return -EINVAL;
305
306   if (strstr(str_list, ",,")) {
307     /* strtok ignores empty words between separators.
308      * This condition handles that by rejecting strings
309      * with consecutive seprators */
310     ERROR(RDT_PLUGIN ": Empty process name");
311     return -EINVAL;
312   }
313
314   for (;;) {
315     char *token = strtok_r(str_list, ",", &saveptr);
316     if (token == NULL)
317       break;
318
319     str_list = NULL;
320
321     while (isspace(*token))
322       token++;
323
324     if (*token == '\0')
325       continue;
326
327     if ((isdupstr((const char **)*names, *names_num, token))) {
328       ERROR(RDT_PLUGIN ": Duplicated process name \'%s\' in group \'%s\'",
329             token, str_list);
330       return -EINVAL;
331     } else {
332       if (0 != strarray_add(names, names_num, token)) {
333         ERROR(RDT_PLUGIN ": Error allocating process name string");
334         return -ENOMEM;
335       }
336     }
337   }
338
339   return 0;
340 }
341
342 /*
343  * NAME
344  *   ngroup_cmp
345  *
346  * DESCRIPTION
347  *   Function to compare names in two name groups.
348  *
349  * PARAMETERS
350  *   `ng_a'      Pointer to name group a.
351  *   `ng_b'      Pointer to name group b.
352  *
353  * RETURN VALUE
354  *    1 if both groups contain the same names
355  *    0 if none of their names match
356  *    -1 if some but not all names match
357  */
358 static int ngroup_cmp(const rdt_name_group_t *ng_a,
359                       const rdt_name_group_t *ng_b) {
360   unsigned found = 0;
361
362   assert(ng_a != NULL);
363   assert(ng_b != NULL);
364
365   const size_t sz_a = (unsigned)ng_a->num_names;
366   const size_t sz_b = (unsigned)ng_b->num_names;
367   const char **tab_a = (const char **)ng_a->names;
368   const char **tab_b = (const char **)ng_b->names;
369
370   for (size_t i = 0; i < sz_a; i++) {
371     for (size_t j = 0; j < sz_b; j++)
372       if (strncmp(tab_a[i], tab_b[j], (size_t)MAX_PROC_NAME_LEN) == 0)
373         found++;
374   }
375   /* if no names are the same */
376   if (!found)
377     return 0;
378   /* if group contains same names */
379   if (sz_a == sz_b && sz_b == (size_t)found)
380     return 1;
381   /* if not all names are the same */
382   return -1;
383 }
384
385 /*
386  * NAME
387  *   oconfig_to_ngroups
388  *
389  * DESCRIPTION
390  *   Function to set the descriptions and names for each process names group.
391  *   Takes a config option containing list of strings that are used to set
392  *   process group values.
393  *
394  * PARAMETERS
395  *   `item'        Config option containing process names groups.
396  *   `groups'      Table of process name groups to set values in.
397  *   `max_groups'  Maximum number of process name groups allowed.
398  *
399  * RETURN VALUE
400  *   On success, the number of name groups set up. On error, appropriate
401  *   negative error value.
402  */
403 static int oconfig_to_ngroups(const oconfig_item_t *item,
404                               rdt_name_group_t *groups,
405                               const size_t max_groups) {
406   int index = 0;
407
408   assert(groups != NULL);
409   assert(max_groups > 0);
410   assert(item != NULL);
411
412   for (int j = 0; j < item->values_num; j++) {
413     int ret;
414     char value[DATA_MAX_NAME_LEN];
415
416     if ((item->values[j].value.string == NULL) ||
417         (strlen(item->values[j].value.string) == 0)) {
418       ERROR(RDT_PLUGIN ": Error - empty group");
419       return -EINVAL;
420     }
421
422     sstrncpy(value, item->values[j].value.string, sizeof(value));
423
424     ret = strlisttoarray(value, &groups[index].names, &groups[index].num_names);
425     if (ret != 0 || groups[index].num_names == 0) {
426       ERROR(RDT_PLUGIN ": Error parsing process names group (%s)",
427             item->values[j].value.string);
428       return -EINVAL;
429     }
430
431     /* set group description info */
432     groups[index].desc = sstrdup(item->values[j].value.string);
433     if (groups[index].desc == NULL) {
434       ERROR(RDT_PLUGIN ": Error allocating name group description");
435       return -ENOMEM;
436     }
437
438     groups[index].proc_pids = NULL;
439     groups[index].monitored_pids_count = 0;
440
441     index++;
442
443     if (index >= (const int)max_groups) {
444       WARNING(RDT_PLUGIN ": Too many process names groups configured");
445       return index;
446     }
447   }
448
449   return index;
450 }
451
452 /*
453  * NAME
454  *   rdt_free_ngroups
455  *
456  * DESCRIPTION
457  *   Function to deallocate memory allocated for name groups.
458  *
459  * PARAMETERS
460  *   `rdt'       Pointer to rdt context
461  */
462 static void rdt_free_ngroups(rdt_ctx_t *rdt) {
463   for (int i = 0; i < RDT_MAX_NAMES_GROUPS; i++) {
464     if (rdt->ngroups[i].desc)
465       DEBUG(RDT_PLUGIN ": Freeing pids \'%s\' group\'s data...",
466             rdt->ngroups[i].desc);
467     sfree(rdt->ngroups[i].desc);
468     strarray_free(rdt->ngroups[i].names, rdt->ngroups[i].num_names);
469
470     if (rdt->ngroups[i].proc_pids)
471       proc_pids_free(rdt->ngroups[i].proc_pids, rdt->ngroups[i].num_names);
472
473     rdt->ngroups[i].num_names = 0;
474     sfree(rdt->pngroups[i]);
475   }
476   if (rdt->proc_pids)
477     sfree(rdt->proc_pids);
478 }
479
480 /*
481  * NAME
482  *   rdt_config_ngroups
483  *
484  * DESCRIPTION
485  *   Reads name groups configuration.
486  *
487  * PARAMETERS
488  *   `rdt`       Pointer to rdt context
489  *   `item'      Config option containing process names groups.
490  *
491  * RETURN VALUE
492  *  0 on success. Negative number on error.
493  */
494 static int rdt_config_ngroups(rdt_ctx_t *rdt, const oconfig_item_t *item) {
495   int n = 0;
496   enum pqos_mon_event events = 0;
497
498   if (item == NULL) {
499     DEBUG(RDT_PLUGIN ": ngroups_config: Invalid argument.");
500     return -EINVAL;
501   }
502
503   DEBUG(RDT_PLUGIN ": Process names groups [%d]:", item->values_num);
504   for (int j = 0; j < item->values_num; j++) {
505     if (item->values[j].type != OCONFIG_TYPE_STRING) {
506       ERROR(RDT_PLUGIN
507             ": given process names group value is not a string [idx=%d]",
508             j);
509       return -EINVAL;
510     }
511     DEBUG(RDT_PLUGIN ":  [%d]: %s", j, item->values[j].value.string);
512   }
513
514   n = oconfig_to_ngroups(item, rdt->ngroups, RDT_MAX_NAMES_GROUPS);
515   if (n < 0) {
516     rdt_free_ngroups(rdt);
517     ERROR(RDT_PLUGIN ": Error parsing process name groups configuration.");
518     return -EINVAL;
519   }
520
521   /* validate configured process name values */
522   for (int group_idx = 0; group_idx < n; group_idx++) {
523     DEBUG(RDT_PLUGIN ":  checking group [%d]: %s", group_idx,
524           rdt->ngroups[group_idx].desc);
525     for (size_t name_idx = 0; name_idx < rdt->ngroups[group_idx].num_names;
526          name_idx++) {
527       DEBUG(RDT_PLUGIN ":    checking process name [%zu]: %s", name_idx,
528             rdt->ngroups[group_idx].names[name_idx]);
529       if (!is_proc_name_valid(rdt->ngroups[group_idx].names[name_idx])) {
530         ERROR(RDT_PLUGIN ": Process name group '%s' contains invalid name '%s'",
531               rdt->ngroups[group_idx].desc,
532               rdt->ngroups[group_idx].names[name_idx]);
533         rdt_free_ngroups(rdt);
534         return -EINVAL;
535       }
536     }
537   }
538
539   if (n == 0) {
540     ERROR(RDT_PLUGIN ": Empty process name groups configured.");
541     return -EINVAL;
542   }
543
544   /* Get all available events on this platform */
545   for (unsigned i = 0; i < rdt->cap_mon->u.mon->num_events; i++)
546     events |= rdt->cap_mon->u.mon->events[i].type;
547
548   events &= ~(PQOS_PERF_EVENT_LLC_MISS);
549
550   DEBUG(RDT_PLUGIN ": Available events to monitor: %#x", events);
551
552   rdt->num_ngroups = n;
553   for (int i = 0; i < n; i++) {
554     for (int j = 0; j < i; j++) {
555       int found = ngroup_cmp(&rdt->ngroups[j], &rdt->ngroups[i]);
556       if (found != 0) {
557         rdt_free_ngroups(rdt);
558         ERROR(RDT_PLUGIN
559               ": Cannot monitor same process name in different groups.");
560         return -EINVAL;
561       }
562     }
563
564     rdt->ngroups[i].events = events;
565     rdt->pngroups[i] = calloc(1, sizeof(*rdt->pngroups[i]));
566     if (rdt->pngroups[i] == NULL) {
567       rdt_free_ngroups(rdt);
568       ERROR(RDT_PLUGIN
569             ": Failed to allocate memory for process name monitoring data.");
570       return -ENOMEM;
571     }
572   }
573
574   return 0;
575 }
576
577 /*
578  * NAME
579  *   rdt_refresh_ngroup
580  *
581  * DESCRIPTION
582  *   Refresh pids monitored by name group.
583  *
584  * PARAMETERS
585  *   `ngroup`         Pointer to name group.
586  *   `group_mon_data' PQoS monitoring context.
587  *
588  * RETURN VALUE
589  *  0 on success. Negative number on error.
590  */
591 static int rdt_refresh_ngroup(rdt_name_group_t *ngroup,
592                               struct pqos_mon_data *group_mon_data) {
593
594   int result = 0;
595
596   if (NULL == ngroup)
597     return -1;
598
599   if (NULL == ngroup->proc_pids) {
600     ERROR(RDT_PLUGIN
601           ": rdt_refresh_ngroup: \'%s\' uninitialized process pids array.",
602           ngroup->desc);
603
604     return -1;
605   }
606
607   DEBUG(RDT_PLUGIN ": rdt_refresh_ngroup: \'%s\' process names group.",
608         ngroup->desc);
609
610   proc_pids_t **proc_pids = ngroup->proc_pids;
611   pids_list_t added_pids;
612   pids_list_t removed_pids;
613
614   memset(&added_pids, 0, sizeof(added_pids));
615   memset(&removed_pids, 0, sizeof(removed_pids));
616
617   for (size_t i = 0; i < ngroup->num_names; ++i) {
618     int diff_result = pids_list_diff(proc_pids[i], &added_pids, &removed_pids);
619     if (0 != diff_result) {
620       ERROR(RDT_PLUGIN
621             ": rdt_refresh_ngroup: \'%s\'. Error [%d] during PID diff.",
622             ngroup->desc, diff_result);
623       result = -1;
624       goto cleanup;
625     }
626   }
627
628   DEBUG(RDT_PLUGIN ": rdt_refresh_ngroup: \'%s\' process names group, added: "
629                    "%u, removed: %u.",
630         ngroup->desc, (unsigned)added_pids.size, (unsigned)removed_pids.size);
631
632   if (added_pids.size > 0) {
633
634     /* no pids are monitored for this group yet: start monitoring */
635     if (0 == ngroup->monitored_pids_count) {
636
637       int start_result =
638           pqos_mon_start_pids(added_pids.size, added_pids.pids, ngroup->events,
639                               (void *)ngroup->desc, group_mon_data);
640       if (PQOS_RETVAL_OK == start_result) {
641         ngroup->monitored_pids_count = added_pids.size;
642       } else {
643         ERROR(RDT_PLUGIN ": rdt_refresh_ngroup: \'%s\'. Error [%d] while "
644                          "STARTING pids monitoring",
645               ngroup->desc, start_result);
646         result = -1;
647         goto pqos_error_recovery;
648       }
649
650     } else {
651
652       int add_result =
653           pqos_mon_add_pids(added_pids.size, added_pids.pids, group_mon_data);
654       if (PQOS_RETVAL_OK == add_result)
655         ngroup->monitored_pids_count += added_pids.size;
656       else {
657         ERROR(RDT_PLUGIN
658               ": rdt_refresh_ngroup: \'%s\'. Error [%d] while ADDING pids.",
659               ngroup->desc, add_result);
660         result = -1;
661         goto pqos_error_recovery;
662       }
663     }
664   }
665
666   if (removed_pids.size > 0) {
667
668     /* all pids are removed: stop monitoring */
669     if (removed_pids.size == ngroup->monitored_pids_count) {
670       /* all pids for this group are lost: stop monitoring */
671       int stop_result = pqos_mon_stop(group_mon_data);
672       if (PQOS_RETVAL_OK != stop_result) {
673         ERROR(RDT_PLUGIN ": rdt_refresh_ngroup: \'%s\'. Error [%d] while "
674                          "STOPPING monitoring",
675               ngroup->desc, stop_result);
676         result = -1;
677         goto pqos_error_recovery;
678       }
679       ngroup->monitored_pids_count = 0;
680     } else {
681       int remove_result = pqos_mon_remove_pids(
682           removed_pids.size, removed_pids.pids, group_mon_data);
683       if (PQOS_RETVAL_OK == remove_result) {
684         ngroup->monitored_pids_count -= removed_pids.size;
685       } else {
686         ERROR(RDT_PLUGIN
687               ": rdt_refresh_ngroup: \'%s\'. Error [%d] while REMOVING pids.",
688               ngroup->desc, remove_result);
689         result = -1;
690         goto pqos_error_recovery;
691       }
692     }
693   }
694
695   goto cleanup;
696
697 pqos_error_recovery:
698   /* Why?
699    * Resources might be temporary unavailable.
700    *
701    * How?
702    * Collectd will halt the reading thread for this
703    * plugin if it returns an error.
704    * Consecutive errors will be increasing the read period
705    * up to 1 day interval.
706    * On pqos error stop monitoring current group
707    * and reset the proc_pids array
708    * monitoring will be restarted on next collectd read cycle
709    */
710   DEBUG(RDT_PLUGIN ": rdt_refresh_ngroup: \'%s\' group RESET after error.",
711         ngroup->desc);
712   pqos_mon_stop(group_mon_data);
713   for (size_t i = 0; i < ngroup->num_names; ++i)
714     if (ngroup->proc_pids[i]->curr)
715       ngroup->proc_pids[i]->curr->size = 0;
716
717   ngroup->monitored_pids_count = 0;
718
719 cleanup:
720   pids_list_clear(&added_pids);
721   pids_list_clear(&removed_pids);
722
723   return result;
724 }
725
726 /*
727  * NAME
728  *   read_pids_data
729  *
730  * DESCRIPTION
731  *   Poll monitoring statistics for name groups
732  *
733  * RETURN VALUE
734  *  0 on success. Negative number on error.
735  */
736 static int read_pids_data() {
737
738   if (0 == g_rdt->num_ngroups) {
739     DEBUG(RDT_PLUGIN ": read_pids_data: not configured - PIDs read skipped");
740     return 0;
741   }
742
743   DEBUG(RDT_PLUGIN ": read_pids_data: Scanning active groups");
744   struct pqos_mon_data *active_groups[RDT_MAX_NAMES_GROUPS] = {0};
745   size_t active_group_idx = 0;
746   for (size_t pngroups_idx = 0;
747        pngroups_idx < STATIC_ARRAY_SIZE(g_rdt->pngroups); ++pngroups_idx)
748     if (0 != g_rdt->ngroups[pngroups_idx].monitored_pids_count)
749       active_groups[active_group_idx++] = g_rdt->pngroups[pngroups_idx];
750
751   int ret = 0;
752
753   if (0 == active_group_idx) {
754     DEBUG(RDT_PLUGIN ": read_pids_data: no active groups - PIDs read skipped");
755     goto groups_refresh;
756   }
757
758   DEBUG(RDT_PLUGIN ": read_pids_data: PIDs data polling");
759
760   int poll_result = pqos_mon_poll(active_groups, active_group_idx);
761   if (poll_result != PQOS_RETVAL_OK) {
762     ERROR(RDT_PLUGIN ": read_pids_data: Failed to poll monitoring data for "
763                      "pids. Error [%d].",
764           poll_result);
765     ret = -poll_result;
766     goto groups_refresh;
767   }
768
769   for (size_t i = 0; i < g_rdt->num_ngroups; i++) {
770     enum pqos_mon_event mbm_events =
771         (PQOS_MON_EVENT_LMEM_BW | PQOS_MON_EVENT_TMEM_BW |
772          PQOS_MON_EVENT_RMEM_BW);
773
774     if (g_rdt->pngroups[i] == NULL ||
775         g_rdt->ngroups[i].monitored_pids_count == 0)
776       continue;
777
778     const struct pqos_event_values *pv = &g_rdt->pngroups[i]->values;
779
780     /* Submit only monitored events data */
781
782     if (g_rdt->ngroups[i].events & PQOS_MON_EVENT_L3_OCCUP)
783       rdt_submit_gauge(g_rdt->ngroups[i].desc, "bytes", "llc", pv->llc);
784
785     if (g_rdt->ngroups[i].events & PQOS_PERF_EVENT_IPC)
786       rdt_submit_gauge(g_rdt->ngroups[i].desc, "ipc", NULL, pv->ipc);
787
788     if (g_rdt->ngroups[i].events & mbm_events) {
789       rdt_submit_derive(g_rdt->ngroups[i].desc, "memory_bandwidth", "local",
790                         pv->mbm_local_delta);
791       rdt_submit_derive(g_rdt->ngroups[i].desc, "memory_bandwidth", "remote",
792                         pv->mbm_remote_delta);
793     }
794   }
795
796 #if COLLECT_DEBUG
797   rdt_dump_pids_data();
798 #endif /* COLLECT_DEBUG */
799
800 groups_refresh:
801   ret = update_proc_pids(RDT_PROC_PATH, g_rdt->proc_pids, g_rdt->num_proc_pids);
802   if (0 != ret) {
803     ERROR(RDT_PLUGIN ": Initial update of proc pids failed");
804     return ret;
805   }
806
807   for (size_t i = 0; i < g_rdt->num_ngroups; i++) {
808     int refresh_result =
809         rdt_refresh_ngroup(&(g_rdt->ngroups[i]), g_rdt->pngroups[i]);
810
811     if (0 != refresh_result) {
812       ERROR(RDT_PLUGIN ": read_pids_data: NGroup %zu refresh failed. Error: %d",
813             i, refresh_result);
814       if (0 == ret) {
815         /* refresh error will be escalated only if there were no
816          * errors before.
817          */
818         ret = refresh_result;
819       }
820     }
821   }
822
823   assert(ret <= 0);
824   return ret;
825 }
826
827 /*
828  * NAME
829  *   rdt_init_pids_monitoring
830  *
831  * DESCRIPTION
832  *   Initialize pids monitoring for all name groups
833  */
834 static void rdt_init_pids_monitoring() {
835   for (size_t group_idx = 0; group_idx < g_rdt->num_ngroups; group_idx++) {
836     /*
837      * Each group must have not-null proc_pids array.
838      * Initial refresh is not mandatory for proper
839      * PIDs statistics detection.
840      */
841     rdt_name_group_t *ng = &g_rdt->ngroups[group_idx];
842     int init_result = initialize_proc_pids((const char **)ng->names,
843                                            ng->num_names, &ng->proc_pids);
844     if (0 != init_result) {
845       ERROR(RDT_PLUGIN
846             ": Initialization of proc_pids for group %zu failed. Error: %d",
847             group_idx, init_result);
848       continue;
849     }
850
851     /* update global proc_pids table */
852     proc_pids_t **proc_pids = realloc(g_rdt->proc_pids,
853                                       (g_rdt->num_proc_pids + ng->num_names) *
854                                           sizeof(*g_rdt->proc_pids));
855     if (NULL == proc_pids) {
856       ERROR(RDT_PLUGIN ": Alloc error\n");
857       continue;
858     }
859
860     for (size_t i = 0; i < ng->num_names; i++)
861       proc_pids[g_rdt->num_proc_pids + i] = ng->proc_pids[i];
862
863     g_rdt->proc_pids = proc_pids;
864     g_rdt->num_proc_pids += ng->num_names;
865   }
866
867   int update_result =
868       update_proc_pids(RDT_PROC_PATH, g_rdt->proc_pids, g_rdt->num_proc_pids);
869   if (0 != update_result)
870     ERROR(RDT_PLUGIN ": Initial update of proc pids failed");
871
872   for (size_t group_idx = 0; group_idx < g_rdt->num_ngroups; group_idx++) {
873     int refresh_result = rdt_refresh_ngroup(&(g_rdt->ngroups[group_idx]),
874                                             g_rdt->pngroups[group_idx]);
875     if (0 != refresh_result)
876       ERROR(RDT_PLUGIN ": Initial refresh of group %zu failed. Error: %d",
877             group_idx, refresh_result);
878   }
879 }
880 #endif /* LIBPQOS2 */
881 /*
882  * NAME
883  *   rdt_free_ngroups
884  *
885  * DESCRIPTION
886  *   Function to deallocate memory allocated for core groups.
887  */
888 static void rdt_free_cgroups(void) {
889   config_cores_cleanup(&g_rdt->cores);
890   for (int i = 0; i < RDT_MAX_CORES; i++) {
891     sfree(g_rdt->pcgroups[i]);
892   }
893 }
894
895 static int rdt_default_cgroups(void) {
896   unsigned num_cores = g_rdt->pqos_cpu->num_cores;
897
898   g_rdt->cores.cgroups = calloc(num_cores, sizeof(*(g_rdt->cores.cgroups)));
899   if (g_rdt->cores.cgroups == NULL) {
900     ERROR(RDT_PLUGIN ": Error allocating core groups array");
901     return -ENOMEM;
902   }
903   g_rdt->cores.num_cgroups = num_cores;
904
905   /* configure each core in separate group */
906   for (unsigned i = 0; i < num_cores; i++) {
907     core_group_t *cgroup = g_rdt->cores.cgroups + i;
908     char desc[DATA_MAX_NAME_LEN];
909
910     /* set core group info */
911     cgroup->cores = calloc(1, sizeof(*cgroup->cores));
912     if (cgroup->cores == NULL) {
913       ERROR(RDT_PLUGIN ": Error allocating cores array");
914       rdt_free_cgroups();
915       return -ENOMEM;
916     }
917     cgroup->num_cores = 1;
918     cgroup->cores[0] = i;
919
920     snprintf(desc, sizeof(desc), "%d", g_rdt->pqos_cpu->cores[i].lcore);
921     cgroup->desc = strdup(desc);
922     if (cgroup->desc == NULL) {
923       ERROR(RDT_PLUGIN ": Error allocating core group description");
924       rdt_free_cgroups();
925       return -ENOMEM;
926     }
927   }
928
929   return num_cores;
930 }
931
932 static int rdt_is_core_id_valid(unsigned int core_id) {
933
934   for (unsigned int i = 0; i < g_rdt->pqos_cpu->num_cores; i++)
935     if (core_id == g_rdt->pqos_cpu->cores[i].lcore)
936       return 1;
937
938   return 0;
939 }
940
941 static int rdt_config_cgroups(oconfig_item_t *item) {
942   size_t n = 0;
943   enum pqos_mon_event events = 0;
944
945   if (config_cores_parse(item, &g_rdt->cores) < 0) {
946     rdt_free_cgroups();
947     ERROR(RDT_PLUGIN ": Error parsing core groups configuration.");
948     return -EINVAL;
949   }
950   n = g_rdt->cores.num_cgroups;
951
952   /* validate configured core id values */
953   for (size_t group_idx = 0; group_idx < n; group_idx++) {
954     core_group_t *cgroup = g_rdt->cores.cgroups + group_idx;
955     for (size_t core_idx = 0; core_idx < cgroup->num_cores; core_idx++) {
956       if (!rdt_is_core_id_valid(cgroup->cores[core_idx])) {
957         ERROR(RDT_PLUGIN ": Core group '%s' contains invalid core id '%u'",
958               cgroup->desc, cgroup->cores[core_idx]);
959         rdt_free_cgroups();
960         return -EINVAL;
961       }
962     }
963   }
964
965   if (n == 0) {
966     /* create default core groups if "Cores" config option is empty */
967     int ret = rdt_default_cgroups();
968     if (ret < 0) {
969       rdt_free_cgroups();
970       ERROR(RDT_PLUGIN ": Error creating default core groups configuration.");
971       return ret;
972     }
973     n = (size_t)ret;
974     INFO(RDT_PLUGIN
975          ": No core groups configured. Default core groups created.");
976   }
977
978   /* Get all available events on this platform */
979   for (unsigned int i = 0; i < g_rdt->cap_mon->u.mon->num_events; i++)
980     events |= g_rdt->cap_mon->u.mon->events[i].type;
981
982   events &= ~(PQOS_PERF_EVENT_LLC_MISS);
983
984   DEBUG(RDT_PLUGIN ": Number of cores in the system: %u",
985         g_rdt->pqos_cpu->num_cores);
986   DEBUG(RDT_PLUGIN ": Available events to monitor: %#x", events);
987
988   g_rdt->cores.num_cgroups = n;
989   for (int i = 0; i < n; i++) {
990     for (int j = 0; j < i; j++) {
991       int found = 0;
992       found = config_cores_cmp_cgroups(&g_rdt->cores.cgroups[j],
993                                        &g_rdt->cores.cgroups[i]);
994       if (found != 0) {
995         rdt_free_cgroups();
996         ERROR(RDT_PLUGIN ": Cannot monitor same cores in different groups.");
997         return -EINVAL;
998       }
999     }
1000
1001     g_rdt->events[i] = events;
1002     g_rdt->pcgroups[i] = calloc(1, sizeof(*g_rdt->pcgroups[i]));
1003     if (g_rdt->pcgroups[i] == NULL) {
1004       rdt_free_cgroups();
1005       ERROR(RDT_PLUGIN ": Failed to allocate memory for monitoring data.");
1006       return -ENOMEM;
1007     }
1008   }
1009
1010   return 0;
1011 }
1012
1013 static void rdt_pqos_log(void *context, const size_t size, const char *msg) {
1014   DEBUG(RDT_PLUGIN ": %s", msg);
1015 }
1016
1017 static int rdt_preinit(void) {
1018   int ret;
1019
1020   if (g_rdt != NULL) {
1021     /* already initialized if config callback was called before init callback */
1022     return 0;
1023   }
1024
1025   g_rdt = calloc(1, sizeof(*g_rdt));
1026   if (g_rdt == NULL) {
1027     ERROR(RDT_PLUGIN ": Failed to allocate memory for rdt context.");
1028     return -ENOMEM;
1029   }
1030
1031   struct pqos_config pqos = {.fd_log = -1,
1032                              .callback_log = rdt_pqos_log,
1033                              .context_log = NULL,
1034                              .verbose = 0,
1035 #ifdef LIBPQOS2
1036                              .interface = PQOS_INTER_OS_RESCTRL_MON};
1037   DEBUG(RDT_PLUGIN ": Initializing PQoS with RESCTRL interface");
1038 #else
1039                              .interface = PQOS_INTER_MSR};
1040   DEBUG(RDT_PLUGIN ": Initializing PQoS with MSR interface");
1041 #endif
1042
1043   ret = pqos_init(&pqos);
1044   DEBUG(RDT_PLUGIN ": PQoS initialization result: [%d]", ret);
1045
1046 #ifdef LIBPQOS2
1047   if (ret == PQOS_RETVAL_INTER) {
1048     pqos.interface = PQOS_INTER_MSR;
1049     DEBUG(RDT_PLUGIN ": Initializing PQoS with MSR interface");
1050     ret = pqos_init(&pqos);
1051     DEBUG(RDT_PLUGIN ": PQoS initialization result: [%d]", ret);
1052   }
1053 #endif
1054
1055   if (ret != PQOS_RETVAL_OK) {
1056     ERROR(RDT_PLUGIN ": Error initializing PQoS library!");
1057     goto rdt_preinit_error1;
1058   }
1059
1060   g_interface = pqos.interface;
1061
1062   ret = pqos_cap_get(&g_rdt->pqos_cap, &g_rdt->pqos_cpu);
1063   if (ret != PQOS_RETVAL_OK) {
1064     ERROR(RDT_PLUGIN ": Error retrieving PQoS capabilities.");
1065     goto rdt_preinit_error2;
1066   }
1067
1068   ret = pqos_cap_get_type(g_rdt->pqos_cap, PQOS_CAP_TYPE_MON, &g_rdt->cap_mon);
1069   if (ret == PQOS_RETVAL_PARAM) {
1070     ERROR(RDT_PLUGIN ": Error retrieving monitoring capabilities.");
1071     goto rdt_preinit_error2;
1072   }
1073
1074   if (g_rdt->cap_mon == NULL) {
1075     ERROR(
1076         RDT_PLUGIN
1077         ": Monitoring capability not detected. Nothing to do for the plugin.");
1078     goto rdt_preinit_error2;
1079   }
1080
1081   /* Reset pqos monitoring groups registers */
1082   pqos_mon_reset();
1083
1084   return 0;
1085
1086 rdt_preinit_error2:
1087   pqos_fini();
1088
1089 rdt_preinit_error1:
1090   sfree(g_rdt);
1091
1092   return -1;
1093 }
1094
1095 static int rdt_config(oconfig_item_t *ci) {
1096   if (rdt_preinit() != 0) {
1097     g_state = CONFIGURATION_ERROR;
1098     /* if we return -1 at this point collectd
1099       reports a failure in configuration and
1100       aborts
1101     */
1102     return (0);
1103   }
1104
1105   for (int i = 0; i < ci->children_num; i++) {
1106     oconfig_item_t *child = ci->children + i;
1107
1108     if (strncasecmp("Cores", child->key, (size_t)strlen("Cores")) == 0) {
1109       if (rdt_config_cgroups(child) != 0) {
1110         g_state = CONFIGURATION_ERROR;
1111         /* if we return -1 at this point collectd
1112            reports a failure in configuration and
1113            aborts
1114          */
1115         return (0);
1116       }
1117
1118 #if COLLECT_DEBUG
1119       rdt_dump_cgroups();
1120 #endif /* COLLECT_DEBUG */
1121     } else if (strncasecmp("Processes", child->key,
1122                            (size_t)strlen("Processes")) == 0) {
1123 #ifdef LIBPQOS2
1124       if (g_interface != PQOS_INTER_OS_RESCTRL_MON) {
1125         ERROR(RDT_PLUGIN ": Configuration parameter \"%s\" not supported. "
1126                          "Resctrl monitoring is needed for PIDs monitoring.",
1127               child->key);
1128         g_state = CONFIGURATION_ERROR;
1129         /* if we return -1 at this point collectd
1130            reports a failure in configuration and
1131            aborts
1132          */
1133         return 0;
1134       }
1135
1136       if (rdt_config_ngroups(g_rdt, child) != 0) {
1137         g_state = CONFIGURATION_ERROR;
1138         /* if we return -1 at this point collectd
1139            reports a failure in configuration and
1140            aborts
1141          */
1142         return 0;
1143       }
1144
1145 #if COLLECT_DEBUG
1146       rdt_dump_ngroups();
1147 #endif /* COLLECT_DEBUG */
1148 #else  /* !LIBPQOS2 */
1149       ERROR(RDT_PLUGIN ": Configuration parameter \"%s\" not supported, please "
1150                        "recompile collectd with libpqos version 2.0 or newer.",
1151             child->key);
1152 #endif /* LIBPQOS2 */
1153     } else {
1154       ERROR(RDT_PLUGIN ": Unknown configuration parameter \"%s\".", child->key);
1155     }
1156   }
1157
1158   return 0;
1159 }
1160
1161 static int read_cores_data() {
1162
1163   if (0 == g_rdt->cores.num_cgroups) {
1164     DEBUG(RDT_PLUGIN ": read_cores_data: not configured - Cores read skipped");
1165     return 0;
1166   }
1167   DEBUG(RDT_PLUGIN ": read_cores_data: Cores data poll");
1168
1169   int ret =
1170       pqos_mon_poll(&g_rdt->pcgroups[0], (unsigned)g_rdt->cores.num_cgroups);
1171   if (ret != PQOS_RETVAL_OK) {
1172     ERROR(RDT_PLUGIN ": read_cores_data: Failed to poll monitoring data for "
1173                      "cores. Error [%d].",
1174           ret);
1175     return -1;
1176   }
1177
1178   for (size_t i = 0; i < g_rdt->cores.num_cgroups; i++) {
1179     core_group_t *cgroup = g_rdt->cores.cgroups + i;
1180     enum pqos_mon_event mbm_events =
1181         (PQOS_MON_EVENT_LMEM_BW | PQOS_MON_EVENT_TMEM_BW |
1182          PQOS_MON_EVENT_RMEM_BW);
1183
1184     const struct pqos_event_values *pv = &g_rdt->pcgroups[i]->values;
1185
1186     /* Submit only monitored events data */
1187
1188     if (g_rdt->events[i] & PQOS_MON_EVENT_L3_OCCUP)
1189       rdt_submit_gauge(cgroup->desc, "bytes", "llc", pv->llc);
1190
1191     if (g_rdt->events[i] & PQOS_PERF_EVENT_IPC)
1192       rdt_submit_gauge(cgroup->desc, "ipc", NULL, pv->ipc);
1193
1194     if (g_rdt->events[i] & mbm_events) {
1195       rdt_submit_derive(cgroup->desc, "memory_bandwidth", "local",
1196                         pv->mbm_local_delta);
1197       rdt_submit_derive(cgroup->desc, "memory_bandwidth", "remote",
1198                         pv->mbm_remote_delta);
1199     }
1200   }
1201
1202 #if COLLECT_DEBUG
1203   rdt_dump_cores_data();
1204 #endif /* COLLECT_DEBUG */
1205
1206   return 0;
1207 }
1208
1209 static int rdt_read(__attribute__((unused)) user_data_t *ud) {
1210
1211   if (g_rdt == NULL) {
1212     ERROR(RDT_PLUGIN ": rdt_read: plugin not initialized.");
1213     return -EINVAL;
1214   }
1215
1216   int cores_read_result = read_cores_data();
1217
1218 #ifdef LIBPQOS2
1219   int pids_read_result = read_pids_data();
1220 #endif /* LIBPQOS2 */
1221
1222   if (0 != cores_read_result)
1223     return cores_read_result;
1224
1225 #ifdef LIBPQOS2
1226   if (0 != pids_read_result)
1227     return pids_read_result;
1228 #endif /* LIBPQOS2 */
1229
1230   return 0;
1231 }
1232
1233 static void rdt_init_cores_monitoring() {
1234   for (size_t i = 0; i < g_rdt->cores.num_cgroups; i++) {
1235     core_group_t *cg = g_rdt->cores.cgroups + i;
1236
1237     int mon_start_result =
1238         pqos_mon_start(cg->num_cores, cg->cores, g_rdt->events[i],
1239                        (void *)cg->desc, g_rdt->pcgroups[i]);
1240
1241     if (mon_start_result != PQOS_RETVAL_OK)
1242       ERROR(RDT_PLUGIN
1243             ": Error starting cores monitoring group %s (pqos status=%d)",
1244             cg->desc, mon_start_result);
1245   }
1246 }
1247
1248 static int rdt_init(void) {
1249
1250   if (g_state == CONFIGURATION_ERROR)
1251     return -1;
1252
1253   int rdt_preinint_result = rdt_preinit();
1254   if (rdt_preinint_result != 0)
1255     return rdt_preinint_result;
1256
1257   rdt_init_cores_monitoring();
1258 #ifdef LIBPQOS2
1259   rdt_init_pids_monitoring();
1260 #endif /* LIBPQOS2 */
1261
1262   return 0;
1263 }
1264
1265 static int rdt_shutdown(void) {
1266   int ret;
1267
1268   DEBUG(RDT_PLUGIN ": rdt_shutdown.");
1269
1270   if (g_rdt == NULL)
1271     return 0;
1272
1273   /* Stop monitoring cores */
1274   for (size_t i = 0; i < g_rdt->cores.num_cgroups; i++) {
1275     pqos_mon_stop(g_rdt->pcgroups[i]);
1276   }
1277
1278 /* Stop pids monitoring */
1279 #ifdef LIBPQOS2
1280   for (size_t i = 0; i < g_rdt->num_ngroups; i++)
1281     pqos_mon_stop(g_rdt->pngroups[i]);
1282 #endif
1283
1284   ret = pqos_fini();
1285   if (ret != PQOS_RETVAL_OK)
1286     ERROR(RDT_PLUGIN ": Error shutting down PQoS library.");
1287   rdt_free_cgroups();
1288 #ifdef LIBPQOS2
1289   rdt_free_ngroups(g_rdt);
1290 #endif /* LIBPQOS2 */
1291   sfree(g_rdt);
1292
1293   return 0;
1294 }
1295
1296 void module_register(void) {
1297   plugin_register_init(RDT_PLUGIN, rdt_init);
1298   plugin_register_complex_config(RDT_PLUGIN, rdt_config);
1299   plugin_register_complex_read(NULL, RDT_PLUGIN, rdt_read, 0, NULL);
1300   plugin_register_shutdown(RDT_PLUGIN, rdt_shutdown);
1301 }