Fix compile time issues
[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/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   ssnprintf(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   ssnprintf(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       ssnprintf(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       ssnprintf(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         ssnprintf(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       if (str_list != NULL)
329         ERROR(RDT_PLUGIN ": Duplicated process name \'%s\' in group \'%s\'",
330               token, str_list);
331       else
332         ERROR(RDT_PLUGIN ": Duplicated process name \'%s\'", token);
333
334       return -EINVAL;
335     } else {
336       if (0 != strarray_add(names, names_num, token)) {
337         ERROR(RDT_PLUGIN ": Error allocating process name string");
338         return -ENOMEM;
339       }
340     }
341   }
342
343   return 0;
344 }
345
346 /*
347  * NAME
348  *   ngroup_cmp
349  *
350  * DESCRIPTION
351  *   Function to compare names in two name groups.
352  *
353  * PARAMETERS
354  *   `ng_a'      Pointer to name group a.
355  *   `ng_b'      Pointer to name group b.
356  *
357  * RETURN VALUE
358  *    1 if both groups contain the same names
359  *    0 if none of their names match
360  *    -1 if some but not all names match
361  */
362 static int ngroup_cmp(const rdt_name_group_t *ng_a,
363                       const rdt_name_group_t *ng_b) {
364   unsigned found = 0;
365
366   assert(ng_a != NULL);
367   assert(ng_b != NULL);
368
369   const size_t sz_a = (unsigned)ng_a->num_names;
370   const size_t sz_b = (unsigned)ng_b->num_names;
371   const char **tab_a = (const char **)ng_a->names;
372   const char **tab_b = (const char **)ng_b->names;
373
374   for (size_t i = 0; i < sz_a; i++) {
375     for (size_t j = 0; j < sz_b; j++)
376       if (strncmp(tab_a[i], tab_b[j], (size_t)MAX_PROC_NAME_LEN) == 0)
377         found++;
378   }
379   /* if no names are the same */
380   if (!found)
381     return 0;
382   /* if group contains same names */
383   if (sz_a == sz_b && sz_b == (size_t)found)
384     return 1;
385   /* if not all names are the same */
386   return -1;
387 }
388
389 /*
390  * NAME
391  *   oconfig_to_ngroups
392  *
393  * DESCRIPTION
394  *   Function to set the descriptions and names for each process names group.
395  *   Takes a config option containing list of strings that are used to set
396  *   process group values.
397  *
398  * PARAMETERS
399  *   `item'        Config option containing process names groups.
400  *   `groups'      Table of process name groups to set values in.
401  *   `max_groups'  Maximum number of process name groups allowed.
402  *
403  * RETURN VALUE
404  *   On success, the number of name groups set up. On error, appropriate
405  *   negative error value.
406  */
407 static int oconfig_to_ngroups(const oconfig_item_t *item,
408                               rdt_name_group_t *groups,
409                               const size_t max_groups) {
410   int index = 0;
411
412   assert(groups != NULL);
413   assert(max_groups > 0);
414   assert(item != NULL);
415
416   for (int j = 0; j < item->values_num; j++) {
417     int ret;
418     char value[DATA_MAX_NAME_LEN];
419
420     if ((item->values[j].value.string == NULL) ||
421         (strlen(item->values[j].value.string) == 0)) {
422       ERROR(RDT_PLUGIN ": Error - empty group");
423       return -EINVAL;
424     }
425
426     sstrncpy(value, item->values[j].value.string, sizeof(value));
427
428     ret = strlisttoarray(value, &groups[index].names, &groups[index].num_names);
429     if (ret != 0 || groups[index].num_names == 0) {
430       ERROR(RDT_PLUGIN ": Error parsing process names group (%s)",
431             item->values[j].value.string);
432       return -EINVAL;
433     }
434
435     /* set group description info */
436     groups[index].desc = sstrdup(item->values[j].value.string);
437     if (groups[index].desc == NULL) {
438       ERROR(RDT_PLUGIN ": Error allocating name group description");
439       return -ENOMEM;
440     }
441
442     groups[index].proc_pids = NULL;
443     groups[index].monitored_pids_count = 0;
444
445     index++;
446
447     if (index >= (const int)max_groups) {
448       WARNING(RDT_PLUGIN ": Too many process names groups configured");
449       return index;
450     }
451   }
452
453   return index;
454 }
455
456 /*
457  * NAME
458  *   rdt_free_ngroups
459  *
460  * DESCRIPTION
461  *   Function to deallocate memory allocated for name groups.
462  *
463  * PARAMETERS
464  *   `rdt'       Pointer to rdt context
465  */
466 static void rdt_free_ngroups(rdt_ctx_t *rdt) {
467   for (int i = 0; i < RDT_MAX_NAMES_GROUPS; i++) {
468     if (rdt->ngroups[i].desc)
469       DEBUG(RDT_PLUGIN ": Freeing pids \'%s\' group\'s data...",
470             rdt->ngroups[i].desc);
471     sfree(rdt->ngroups[i].desc);
472     strarray_free(rdt->ngroups[i].names, rdt->ngroups[i].num_names);
473
474     if (rdt->ngroups[i].proc_pids)
475       proc_pids_free(rdt->ngroups[i].proc_pids, rdt->ngroups[i].num_names);
476
477     rdt->ngroups[i].num_names = 0;
478     sfree(rdt->pngroups[i]);
479   }
480   if (rdt->proc_pids)
481     sfree(rdt->proc_pids);
482
483   rdt->num_ngroups = 0;
484 }
485
486 /*
487  * NAME
488  *   rdt_config_ngroups
489  *
490  * DESCRIPTION
491  *   Reads name groups configuration.
492  *
493  * PARAMETERS
494  *   `rdt`       Pointer to rdt context
495  *   `item'      Config option containing process names groups.
496  *
497  * RETURN VALUE
498  *  0 on success. Negative number on error.
499  */
500 static int rdt_config_ngroups(rdt_ctx_t *rdt, const oconfig_item_t *item) {
501   int n = 0;
502   enum pqos_mon_event events = 0;
503
504   if (item == NULL) {
505     DEBUG(RDT_PLUGIN ": ngroups_config: Invalid argument.");
506     return -EINVAL;
507   }
508
509   DEBUG(RDT_PLUGIN ": Process names groups [%d]:", item->values_num);
510   for (int j = 0; j < item->values_num; j++) {
511     if (item->values[j].type != OCONFIG_TYPE_STRING) {
512       ERROR(RDT_PLUGIN
513             ": given process names group value is not a string [idx=%d]",
514             j);
515       return -EINVAL;
516     }
517     DEBUG(RDT_PLUGIN ":  [%d]: %s", j, item->values[j].value.string);
518   }
519
520   n = oconfig_to_ngroups(item, rdt->ngroups, RDT_MAX_NAMES_GROUPS);
521   if (n < 0) {
522     rdt_free_ngroups(rdt);
523     ERROR(RDT_PLUGIN ": Error parsing process name groups configuration.");
524     return -EINVAL;
525   }
526
527   /* validate configured process name values */
528   for (int group_idx = 0; group_idx < n; group_idx++) {
529     DEBUG(RDT_PLUGIN ":  checking group [%d]: %s", group_idx,
530           rdt->ngroups[group_idx].desc);
531     for (size_t name_idx = 0; name_idx < rdt->ngroups[group_idx].num_names;
532          name_idx++) {
533       DEBUG(RDT_PLUGIN ":    checking process name [%zu]: %s", name_idx,
534             rdt->ngroups[group_idx].names[name_idx]);
535       if (!proc_pids_is_name_valid(rdt->ngroups[group_idx].names[name_idx])) {
536         ERROR(RDT_PLUGIN ": Process name group '%s' contains invalid name '%s'",
537               rdt->ngroups[group_idx].desc,
538               rdt->ngroups[group_idx].names[name_idx]);
539         rdt_free_ngroups(rdt);
540         return -EINVAL;
541       }
542     }
543   }
544
545   if (n == 0) {
546     ERROR(RDT_PLUGIN ": Empty process name groups configured.");
547     return -EINVAL;
548   }
549
550   /* Get all available events on this platform */
551   for (unsigned i = 0; i < rdt->cap_mon->u.mon->num_events; i++)
552     events |= rdt->cap_mon->u.mon->events[i].type;
553
554   events &= ~(PQOS_PERF_EVENT_LLC_MISS);
555
556   DEBUG(RDT_PLUGIN ": Available events to monitor: %#x", events);
557
558   rdt->num_ngroups = n;
559   for (int i = 0; i < n; i++) {
560     for (int j = 0; j < i; j++) {
561       int found = ngroup_cmp(&rdt->ngroups[j], &rdt->ngroups[i]);
562       if (found != 0) {
563         rdt_free_ngroups(rdt);
564         ERROR(RDT_PLUGIN
565               ": Cannot monitor same process name in different groups.");
566         return -EINVAL;
567       }
568     }
569
570     rdt->ngroups[i].events = events;
571     rdt->pngroups[i] = calloc(1, sizeof(*rdt->pngroups[i]));
572     if (rdt->pngroups[i] == NULL) {
573       rdt_free_ngroups(rdt);
574       ERROR(RDT_PLUGIN
575             ": Failed to allocate memory for process name monitoring data.");
576       return -ENOMEM;
577     }
578   }
579
580   return 0;
581 }
582
583 /*
584  * NAME
585  *   rdt_refresh_ngroup
586  *
587  * DESCRIPTION
588  *   Refresh pids monitored by name group.
589  *
590  * PARAMETERS
591  *   `ngroup`         Pointer to name group.
592  *   `group_mon_data' PQoS monitoring context.
593  *
594  * RETURN VALUE
595  *  0 on success. Negative number on error.
596  */
597 static int rdt_refresh_ngroup(rdt_name_group_t *ngroup,
598                               struct pqos_mon_data *group_mon_data) {
599
600   int result = 0;
601
602   if (NULL == ngroup)
603     return -1;
604
605   if (NULL == ngroup->proc_pids) {
606     ERROR(RDT_PLUGIN
607           ": rdt_refresh_ngroup: \'%s\' uninitialized process pids array.",
608           ngroup->desc);
609
610     return -1;
611   }
612
613   DEBUG(RDT_PLUGIN ": rdt_refresh_ngroup: \'%s\' process names group.",
614         ngroup->desc);
615
616   proc_pids_t **proc_pids = ngroup->proc_pids;
617   pids_list_t added_pids;
618   pids_list_t removed_pids;
619
620   memset(&added_pids, 0, sizeof(added_pids));
621   memset(&removed_pids, 0, sizeof(removed_pids));
622
623   for (size_t i = 0; i < ngroup->num_names; ++i) {
624     int diff_result = pids_list_diff(proc_pids[i], &added_pids, &removed_pids);
625     if (0 != diff_result) {
626       ERROR(RDT_PLUGIN
627             ": rdt_refresh_ngroup: \'%s\'. Error [%d] during PID diff.",
628             ngroup->desc, diff_result);
629       result = -1;
630       goto cleanup;
631     }
632   }
633
634   DEBUG(RDT_PLUGIN ": rdt_refresh_ngroup: \'%s\' process names group, added: "
635                    "%u, removed: %u.",
636         ngroup->desc, (unsigned)added_pids.size, (unsigned)removed_pids.size);
637
638   if (added_pids.size > 0) {
639
640     /* no pids are monitored for this group yet: start monitoring */
641     if (0 == ngroup->monitored_pids_count) {
642
643       int start_result =
644           pqos_mon_start_pids(added_pids.size, added_pids.pids, ngroup->events,
645                               (void *)ngroup->desc, group_mon_data);
646       if (PQOS_RETVAL_OK == start_result) {
647         ngroup->monitored_pids_count = added_pids.size;
648       } else {
649         ERROR(RDT_PLUGIN ": rdt_refresh_ngroup: \'%s\'. Error [%d] while "
650                          "STARTING pids monitoring",
651               ngroup->desc, start_result);
652         result = -1;
653         goto pqos_error_recovery;
654       }
655
656     } else {
657
658       int add_result =
659           pqos_mon_add_pids(added_pids.size, added_pids.pids, group_mon_data);
660       if (PQOS_RETVAL_OK == add_result)
661         ngroup->monitored_pids_count += added_pids.size;
662       else {
663         ERROR(RDT_PLUGIN
664               ": rdt_refresh_ngroup: \'%s\'. Error [%d] while ADDING pids.",
665               ngroup->desc, add_result);
666         result = -1;
667         goto pqos_error_recovery;
668       }
669     }
670   }
671
672   if (removed_pids.size > 0) {
673
674     /* all pids are removed: stop monitoring */
675     if (removed_pids.size == ngroup->monitored_pids_count) {
676       /* all pids for this group are lost: stop monitoring */
677       int stop_result = pqos_mon_stop(group_mon_data);
678       if (PQOS_RETVAL_OK != stop_result) {
679         ERROR(RDT_PLUGIN ": rdt_refresh_ngroup: \'%s\'. Error [%d] while "
680                          "STOPPING monitoring",
681               ngroup->desc, stop_result);
682         result = -1;
683         goto pqos_error_recovery;
684       }
685       ngroup->monitored_pids_count = 0;
686     } else {
687       int remove_result = pqos_mon_remove_pids(
688           removed_pids.size, removed_pids.pids, group_mon_data);
689       if (PQOS_RETVAL_OK == remove_result) {
690         ngroup->monitored_pids_count -= removed_pids.size;
691       } else {
692         ERROR(RDT_PLUGIN
693               ": rdt_refresh_ngroup: \'%s\'. Error [%d] while REMOVING pids.",
694               ngroup->desc, remove_result);
695         result = -1;
696         goto pqos_error_recovery;
697       }
698     }
699   }
700
701   goto cleanup;
702
703 pqos_error_recovery:
704   /* Why?
705    * Resources might be temporary unavailable.
706    *
707    * How?
708    * Collectd will halt the reading thread for this
709    * plugin if it returns an error.
710    * Consecutive errors will be increasing the read period
711    * up to 1 day interval.
712    * On pqos error stop monitoring current group
713    * and reset the proc_pids array
714    * monitoring will be restarted on next collectd read cycle
715    */
716   DEBUG(RDT_PLUGIN ": rdt_refresh_ngroup: \'%s\' group RESET after error.",
717         ngroup->desc);
718   pqos_mon_stop(group_mon_data);
719   for (size_t i = 0; i < ngroup->num_names; ++i)
720     if (ngroup->proc_pids[i]->curr)
721       ngroup->proc_pids[i]->curr->size = 0;
722
723   ngroup->monitored_pids_count = 0;
724
725 cleanup:
726   pids_list_clear(&added_pids);
727   pids_list_clear(&removed_pids);
728
729   return result;
730 }
731
732 /*
733  * NAME
734  *   read_pids_data
735  *
736  * DESCRIPTION
737  *   Poll monitoring statistics for name groups
738  *
739  * RETURN VALUE
740  *  0 on success. Negative number on error.
741  */
742 static int read_pids_data() {
743
744   if (0 == g_rdt->num_ngroups) {
745     DEBUG(RDT_PLUGIN ": read_pids_data: not configured - PIDs read skipped");
746     return 0;
747   }
748
749   DEBUG(RDT_PLUGIN ": read_pids_data: Scanning active groups");
750   struct pqos_mon_data *active_groups[RDT_MAX_NAMES_GROUPS] = {0};
751   size_t active_group_idx = 0;
752   for (size_t pngroups_idx = 0;
753        pngroups_idx < STATIC_ARRAY_SIZE(g_rdt->pngroups); ++pngroups_idx)
754     if (0 != g_rdt->ngroups[pngroups_idx].monitored_pids_count)
755       active_groups[active_group_idx++] = g_rdt->pngroups[pngroups_idx];
756
757   int ret = 0;
758
759   if (0 == active_group_idx) {
760     DEBUG(RDT_PLUGIN ": read_pids_data: no active groups - PIDs read skipped");
761     goto groups_refresh;
762   }
763
764   DEBUG(RDT_PLUGIN ": read_pids_data: PIDs data polling");
765
766   int poll_result = pqos_mon_poll(active_groups, active_group_idx);
767   if (poll_result != PQOS_RETVAL_OK) {
768     ERROR(RDT_PLUGIN ": read_pids_data: Failed to poll monitoring data for "
769                      "pids. Error [%d].",
770           poll_result);
771     ret = -poll_result;
772     goto groups_refresh;
773   }
774
775   for (size_t i = 0; i < g_rdt->num_ngroups; i++) {
776     enum pqos_mon_event mbm_events =
777         (PQOS_MON_EVENT_LMEM_BW | PQOS_MON_EVENT_TMEM_BW |
778          PQOS_MON_EVENT_RMEM_BW);
779
780     if (g_rdt->pngroups[i] == NULL ||
781         g_rdt->ngroups[i].monitored_pids_count == 0)
782       continue;
783
784     const struct pqos_event_values *pv = &g_rdt->pngroups[i]->values;
785
786     /* Submit only monitored events data */
787
788     if (g_rdt->ngroups[i].events & PQOS_MON_EVENT_L3_OCCUP)
789       rdt_submit_gauge(g_rdt->ngroups[i].desc, "bytes", "llc", pv->llc);
790
791     if (g_rdt->ngroups[i].events & PQOS_PERF_EVENT_IPC)
792       rdt_submit_gauge(g_rdt->ngroups[i].desc, "ipc", NULL, pv->ipc);
793
794     if (g_rdt->ngroups[i].events & mbm_events) {
795       rdt_submit_derive(g_rdt->ngroups[i].desc, "memory_bandwidth", "local",
796                         pv->mbm_local_delta);
797       rdt_submit_derive(g_rdt->ngroups[i].desc, "memory_bandwidth", "remote",
798                         pv->mbm_remote_delta);
799     }
800   }
801
802 #if COLLECT_DEBUG
803   rdt_dump_pids_data();
804 #endif /* COLLECT_DEBUG */
805
806 groups_refresh:
807   ret = proc_pids_update(RDT_PROC_PATH, g_rdt->proc_pids, g_rdt->num_proc_pids);
808   if (0 != ret) {
809     ERROR(RDT_PLUGIN ": Initial update of proc pids failed");
810     return ret;
811   }
812
813   for (size_t i = 0; i < g_rdt->num_ngroups; i++) {
814     int refresh_result =
815         rdt_refresh_ngroup(&(g_rdt->ngroups[i]), g_rdt->pngroups[i]);
816
817     if (0 != refresh_result) {
818       ERROR(RDT_PLUGIN ": read_pids_data: NGroup %zu refresh failed. Error: %d",
819             i, refresh_result);
820       if (0 == ret) {
821         /* refresh error will be escalated only if there were no
822          * errors before.
823          */
824         ret = refresh_result;
825       }
826     }
827   }
828
829   assert(ret <= 0);
830   return ret;
831 }
832
833 /*
834  * NAME
835  *   rdt_init_pids_monitoring
836  *
837  * DESCRIPTION
838  *   Initialize pids monitoring for all name groups
839  */
840 static void rdt_init_pids_monitoring() {
841   for (size_t group_idx = 0; group_idx < g_rdt->num_ngroups; group_idx++) {
842     /*
843      * Each group must have not-null proc_pids array.
844      * Initial refresh is not mandatory for proper
845      * PIDs statistics detection.
846      */
847     rdt_name_group_t *ng = &g_rdt->ngroups[group_idx];
848     int init_result =
849         proc_pids_init((const char **)ng->names, ng->num_names, &ng->proc_pids);
850     if (0 != init_result) {
851       ERROR(RDT_PLUGIN
852             ": Initialization of proc_pids for group %zu failed. Error: %d",
853             group_idx, init_result);
854       continue;
855     }
856
857     /* update global proc_pids table */
858     proc_pids_t **proc_pids =
859         realloc(g_rdt->proc_pids, (g_rdt->num_proc_pids + ng->num_names) *
860                                       sizeof(*g_rdt->proc_pids));
861     if (NULL == proc_pids) {
862       ERROR(RDT_PLUGIN ": Alloc error\n");
863       continue;
864     }
865
866     for (size_t i = 0; i < ng->num_names; i++)
867       proc_pids[g_rdt->num_proc_pids + i] = ng->proc_pids[i];
868
869     g_rdt->proc_pids = proc_pids;
870     g_rdt->num_proc_pids += ng->num_names;
871   }
872
873   if (g_rdt->num_ngroups > 0) {
874     int update_result =
875         proc_pids_update(RDT_PROC_PATH, g_rdt->proc_pids, g_rdt->num_proc_pids);
876     if (0 != update_result)
877       ERROR(RDT_PLUGIN ": Initial update of proc pids failed");
878   }
879
880   for (size_t group_idx = 0; group_idx < g_rdt->num_ngroups; group_idx++) {
881     int refresh_result = rdt_refresh_ngroup(&(g_rdt->ngroups[group_idx]),
882                                             g_rdt->pngroups[group_idx]);
883     if (0 != refresh_result)
884       ERROR(RDT_PLUGIN ": Initial refresh of group %zu failed. Error: %d",
885             group_idx, refresh_result);
886   }
887 }
888 #endif /* LIBPQOS2 */
889 /*
890  * NAME
891  *   rdt_free_cgroups
892  *
893  * DESCRIPTION
894  *   Function to deallocate memory allocated for core groups.
895  */
896 static void rdt_free_cgroups(void) {
897   config_cores_cleanup(&g_rdt->cores);
898   for (int i = 0; i < RDT_MAX_CORES; i++) {
899     sfree(g_rdt->pcgroups[i]);
900   }
901   g_rdt->cores.num_cgroups = 0;
902 }
903
904 static int rdt_default_cgroups(void) {
905   unsigned num_cores = g_rdt->pqos_cpu->num_cores;
906
907   g_rdt->cores.cgroups = calloc(num_cores, sizeof(*(g_rdt->cores.cgroups)));
908   if (g_rdt->cores.cgroups == NULL) {
909     ERROR(RDT_PLUGIN ": Error allocating core groups array");
910     return -ENOMEM;
911   }
912   g_rdt->cores.num_cgroups = num_cores;
913
914   /* configure each core in separate group */
915   for (unsigned i = 0; i < num_cores; i++) {
916     core_group_t *cgroup = g_rdt->cores.cgroups + i;
917     char desc[DATA_MAX_NAME_LEN];
918
919     /* set core group info */
920     cgroup->cores = calloc(1, sizeof(*cgroup->cores));
921     if (cgroup->cores == NULL) {
922       ERROR(RDT_PLUGIN ": Error allocating cores array");
923       rdt_free_cgroups();
924       return -ENOMEM;
925     }
926     cgroup->num_cores = 1;
927     cgroup->cores[0] = i;
928
929     ssnprintf(desc, sizeof(desc), "%d", g_rdt->pqos_cpu->cores[i].lcore);
930     cgroup->desc = strdup(desc);
931     if (cgroup->desc == NULL) {
932       ERROR(RDT_PLUGIN ": Error allocating core group description");
933       rdt_free_cgroups();
934       return -ENOMEM;
935     }
936   }
937
938   return num_cores;
939 }
940
941 static int rdt_is_core_id_valid(unsigned int core_id) {
942
943   for (unsigned int i = 0; i < g_rdt->pqos_cpu->num_cores; i++)
944     if (core_id == g_rdt->pqos_cpu->cores[i].lcore)
945       return 1;
946
947   return 0;
948 }
949
950 static int rdt_config_cgroups(oconfig_item_t *item) {
951   size_t n = 0;
952   enum pqos_mon_event events = 0;
953
954   if (config_cores_parse(item, &g_rdt->cores) < 0) {
955     rdt_free_cgroups();
956     ERROR(RDT_PLUGIN ": Error parsing core groups configuration.");
957     return -EINVAL;
958   }
959   n = g_rdt->cores.num_cgroups;
960
961   /* validate configured core id values */
962   for (size_t group_idx = 0; group_idx < n; group_idx++) {
963     core_group_t *cgroup = g_rdt->cores.cgroups + group_idx;
964     for (size_t core_idx = 0; core_idx < cgroup->num_cores; core_idx++) {
965       if (!rdt_is_core_id_valid(cgroup->cores[core_idx])) {
966         ERROR(RDT_PLUGIN ": Core group '%s' contains invalid core id '%u'",
967               cgroup->desc, cgroup->cores[core_idx]);
968         rdt_free_cgroups();
969         return -EINVAL;
970       }
971     }
972   }
973
974   if (n == 0) {
975     /* create default core groups if "Cores" config option is empty */
976     int ret = rdt_default_cgroups();
977     if (ret < 0) {
978       rdt_free_cgroups();
979       ERROR(RDT_PLUGIN ": Error creating default core groups configuration.");
980       return ret;
981     }
982     n = (size_t)ret;
983     INFO(RDT_PLUGIN
984          ": No core groups configured. Default core groups created.");
985   }
986
987   /* Get all available events on this platform */
988   for (unsigned int i = 0; i < g_rdt->cap_mon->u.mon->num_events; i++)
989     events |= g_rdt->cap_mon->u.mon->events[i].type;
990
991   events &= ~(PQOS_PERF_EVENT_LLC_MISS);
992
993   DEBUG(RDT_PLUGIN ": Number of cores in the system: %u",
994         g_rdt->pqos_cpu->num_cores);
995   DEBUG(RDT_PLUGIN ": Available events to monitor: %#x", events);
996
997   g_rdt->cores.num_cgroups = n;
998   for (int i = 0; i < n; i++) {
999     for (int j = 0; j < i; j++) {
1000       int found = 0;
1001       found = config_cores_cmp_cgroups(&g_rdt->cores.cgroups[j],
1002                                        &g_rdt->cores.cgroups[i]);
1003       if (found != 0) {
1004         rdt_free_cgroups();
1005         ERROR(RDT_PLUGIN ": Cannot monitor same cores in different groups.");
1006         return -EINVAL;
1007       }
1008     }
1009
1010     g_rdt->events[i] = events;
1011     g_rdt->pcgroups[i] = calloc(1, sizeof(*g_rdt->pcgroups[i]));
1012     if (g_rdt->pcgroups[i] == NULL) {
1013       rdt_free_cgroups();
1014       ERROR(RDT_PLUGIN ": Failed to allocate memory for monitoring data.");
1015       return -ENOMEM;
1016     }
1017   }
1018
1019   return 0;
1020 }
1021
1022 static void rdt_pqos_log(void *context, const size_t size, const char *msg) {
1023   DEBUG(RDT_PLUGIN ": %s", msg);
1024 }
1025
1026 static int rdt_preinit(void) {
1027   int ret;
1028
1029   if (g_rdt != NULL) {
1030     /* already initialized if config callback was called before init callback */
1031     return 0;
1032   }
1033
1034   g_rdt = calloc(1, sizeof(*g_rdt));
1035   if (g_rdt == NULL) {
1036     ERROR(RDT_PLUGIN ": Failed to allocate memory for rdt context.");
1037     return -ENOMEM;
1038   }
1039
1040   struct pqos_config pqos = {.fd_log = -1,
1041                              .callback_log = rdt_pqos_log,
1042                              .context_log = NULL,
1043                              .verbose = 0,
1044 #ifdef LIBPQOS2
1045                              .interface = PQOS_INTER_OS_RESCTRL_MON};
1046   DEBUG(RDT_PLUGIN ": Initializing PQoS with RESCTRL interface");
1047 #else
1048                              .interface = PQOS_INTER_MSR};
1049   DEBUG(RDT_PLUGIN ": Initializing PQoS with MSR interface");
1050 #endif
1051
1052   ret = pqos_init(&pqos);
1053   DEBUG(RDT_PLUGIN ": PQoS initialization result: [%d]", ret);
1054
1055 #ifdef LIBPQOS2
1056   if (ret == PQOS_RETVAL_INTER) {
1057     pqos.interface = PQOS_INTER_MSR;
1058     DEBUG(RDT_PLUGIN ": Initializing PQoS with MSR interface");
1059     ret = pqos_init(&pqos);
1060     DEBUG(RDT_PLUGIN ": PQoS initialization result: [%d]", ret);
1061   }
1062 #endif
1063
1064   if (ret != PQOS_RETVAL_OK) {
1065     ERROR(RDT_PLUGIN ": Error initializing PQoS library!");
1066     goto rdt_preinit_error1;
1067   }
1068
1069   g_interface = pqos.interface;
1070
1071   ret = pqos_cap_get(&g_rdt->pqos_cap, &g_rdt->pqos_cpu);
1072   if (ret != PQOS_RETVAL_OK) {
1073     ERROR(RDT_PLUGIN ": Error retrieving PQoS capabilities.");
1074     goto rdt_preinit_error2;
1075   }
1076
1077   ret = pqos_cap_get_type(g_rdt->pqos_cap, PQOS_CAP_TYPE_MON, &g_rdt->cap_mon);
1078   if (ret == PQOS_RETVAL_PARAM) {
1079     ERROR(RDT_PLUGIN ": Error retrieving monitoring capabilities.");
1080     goto rdt_preinit_error2;
1081   }
1082
1083   if (g_rdt->cap_mon == NULL) {
1084     ERROR(
1085         RDT_PLUGIN
1086         ": Monitoring capability not detected. Nothing to do for the plugin.");
1087     goto rdt_preinit_error2;
1088   }
1089
1090   /* Reset pqos monitoring groups registers */
1091   pqos_mon_reset();
1092
1093   return 0;
1094
1095 rdt_preinit_error2:
1096   pqos_fini();
1097
1098 rdt_preinit_error1:
1099   sfree(g_rdt);
1100
1101   return -1;
1102 }
1103
1104 static int rdt_config(oconfig_item_t *ci) {
1105   if (rdt_preinit() != 0) {
1106     g_state = CONFIGURATION_ERROR;
1107     /* if we return -1 at this point collectd
1108       reports a failure in configuration and
1109       aborts
1110     */
1111     return 0;
1112   }
1113
1114   for (int i = 0; i < ci->children_num; i++) {
1115     oconfig_item_t *child = ci->children + i;
1116
1117     if (strncasecmp("Cores", child->key, (size_t)strlen("Cores")) == 0) {
1118       if (g_rdt->cores.num_cgroups > 0) {
1119         ERROR(RDT_PLUGIN
1120               ": Configuration parameter \"%s\" can be used only once.",
1121               child->key);
1122         g_state = CONFIGURATION_ERROR;
1123       } else if (rdt_config_cgroups(child) != 0)
1124         g_state = CONFIGURATION_ERROR;
1125
1126       if (g_state == CONFIGURATION_ERROR)
1127         /* if we return -1 at this point collectd
1128            reports a failure in configuration and
1129            aborts
1130          */
1131         return 0;
1132
1133 #if COLLECT_DEBUG
1134       rdt_dump_cgroups();
1135 #endif /* COLLECT_DEBUG */
1136     } else if (strncasecmp("Processes", child->key,
1137                            (size_t)strlen("Processes")) == 0) {
1138 #ifdef LIBPQOS2
1139       if (g_interface != PQOS_INTER_OS_RESCTRL_MON) {
1140         ERROR(RDT_PLUGIN ": Configuration parameter \"%s\" not supported. "
1141                          "Resctrl monitoring is needed for PIDs monitoring.",
1142               child->key);
1143         g_state = CONFIGURATION_ERROR;
1144       }
1145
1146       else if (g_rdt->num_ngroups > 0) {
1147         ERROR(RDT_PLUGIN
1148               ": Configuration parameter \"%s\" can be used only once.",
1149               child->key);
1150         g_state = CONFIGURATION_ERROR;
1151       }
1152
1153       else if (rdt_config_ngroups(g_rdt, child) != 0)
1154         g_state = CONFIGURATION_ERROR;
1155
1156       if (g_state == CONFIGURATION_ERROR)
1157         /* if we return -1 at this point collectd
1158            reports a failure in configuration and
1159            aborts
1160          */
1161         return 0;
1162
1163 #if COLLECT_DEBUG
1164       rdt_dump_ngroups();
1165 #endif /* COLLECT_DEBUG */
1166 #else  /* !LIBPQOS2 */
1167       ERROR(RDT_PLUGIN ": Configuration parameter \"%s\" not supported, please "
1168                        "recompile collectd with libpqos version 2.0 or newer.",
1169             child->key);
1170 #endif /* LIBPQOS2 */
1171     } else {
1172       ERROR(RDT_PLUGIN ": Unknown configuration parameter \"%s\".", child->key);
1173     }
1174   }
1175
1176   return 0;
1177 }
1178
1179 static int read_cores_data() {
1180
1181   if (0 == g_rdt->cores.num_cgroups) {
1182     DEBUG(RDT_PLUGIN ": read_cores_data: not configured - Cores read skipped");
1183     return 0;
1184   }
1185   DEBUG(RDT_PLUGIN ": read_cores_data: Cores data poll");
1186
1187   int ret =
1188       pqos_mon_poll(&g_rdt->pcgroups[0], (unsigned)g_rdt->cores.num_cgroups);
1189   if (ret != PQOS_RETVAL_OK) {
1190     ERROR(RDT_PLUGIN ": read_cores_data: Failed to poll monitoring data for "
1191                      "cores. Error [%d].",
1192           ret);
1193     return -1;
1194   }
1195
1196   for (size_t i = 0; i < g_rdt->cores.num_cgroups; i++) {
1197     core_group_t *cgroup = g_rdt->cores.cgroups + i;
1198     enum pqos_mon_event mbm_events =
1199         (PQOS_MON_EVENT_LMEM_BW | PQOS_MON_EVENT_TMEM_BW |
1200          PQOS_MON_EVENT_RMEM_BW);
1201
1202     const struct pqos_event_values *pv = &g_rdt->pcgroups[i]->values;
1203
1204     /* Submit only monitored events data */
1205
1206     if (g_rdt->events[i] & PQOS_MON_EVENT_L3_OCCUP)
1207       rdt_submit_gauge(cgroup->desc, "bytes", "llc", pv->llc);
1208
1209     if (g_rdt->events[i] & PQOS_PERF_EVENT_IPC)
1210       rdt_submit_gauge(cgroup->desc, "ipc", NULL, pv->ipc);
1211
1212     if (g_rdt->events[i] & mbm_events) {
1213       rdt_submit_derive(cgroup->desc, "memory_bandwidth", "local",
1214                         pv->mbm_local_delta);
1215       rdt_submit_derive(cgroup->desc, "memory_bandwidth", "remote",
1216                         pv->mbm_remote_delta);
1217     }
1218   }
1219
1220 #if COLLECT_DEBUG
1221   rdt_dump_cores_data();
1222 #endif /* COLLECT_DEBUG */
1223
1224   return 0;
1225 }
1226
1227 static int rdt_read(__attribute__((unused)) user_data_t *ud) {
1228
1229   if (g_rdt == NULL) {
1230     ERROR(RDT_PLUGIN ": rdt_read: plugin not initialized.");
1231     return -EINVAL;
1232   }
1233
1234   int cores_read_result = read_cores_data();
1235
1236 #ifdef LIBPQOS2
1237   int pids_read_result = read_pids_data();
1238 #endif /* LIBPQOS2 */
1239
1240   if (0 != cores_read_result)
1241     return cores_read_result;
1242
1243 #ifdef LIBPQOS2
1244   if (0 != pids_read_result)
1245     return pids_read_result;
1246 #endif /* LIBPQOS2 */
1247
1248   return 0;
1249 }
1250
1251 static void rdt_init_cores_monitoring() {
1252   for (size_t i = 0; i < g_rdt->cores.num_cgroups; i++) {
1253     core_group_t *cg = g_rdt->cores.cgroups + i;
1254
1255     int mon_start_result =
1256         pqos_mon_start(cg->num_cores, cg->cores, g_rdt->events[i],
1257                        (void *)cg->desc, g_rdt->pcgroups[i]);
1258
1259     if (mon_start_result != PQOS_RETVAL_OK)
1260       ERROR(RDT_PLUGIN
1261             ": Error starting cores monitoring group %s (pqos status=%d)",
1262             cg->desc, mon_start_result);
1263   }
1264 }
1265
1266 static int rdt_init(void) {
1267
1268   if (g_state == CONFIGURATION_ERROR) {
1269     if (g_rdt != NULL) {
1270       if (g_rdt->cores.num_cgroups > 0)
1271         rdt_free_cgroups();
1272 #ifdef LIBPQOS2
1273       if (g_rdt->num_ngroups > 0)
1274         rdt_free_ngroups(g_rdt);
1275 #endif
1276     }
1277     return -1;
1278   }
1279
1280   int rdt_preinint_result = rdt_preinit();
1281   if (rdt_preinint_result != 0)
1282     return rdt_preinint_result;
1283
1284   rdt_init_cores_monitoring();
1285 #ifdef LIBPQOS2
1286   rdt_init_pids_monitoring();
1287 #endif /* LIBPQOS2 */
1288
1289   return 0;
1290 }
1291
1292 static int rdt_shutdown(void) {
1293   int ret;
1294
1295   DEBUG(RDT_PLUGIN ": rdt_shutdown.");
1296
1297   if (g_rdt == NULL)
1298     return 0;
1299
1300   /* Stop monitoring cores */
1301   for (size_t i = 0; i < g_rdt->cores.num_cgroups; i++) {
1302     pqos_mon_stop(g_rdt->pcgroups[i]);
1303   }
1304
1305 /* Stop pids monitoring */
1306 #ifdef LIBPQOS2
1307   for (size_t i = 0; i < g_rdt->num_ngroups; i++)
1308     pqos_mon_stop(g_rdt->pngroups[i]);
1309 #endif
1310
1311   ret = pqos_fini();
1312   if (ret != PQOS_RETVAL_OK)
1313     ERROR(RDT_PLUGIN ": Error shutting down PQoS library.");
1314   rdt_free_cgroups();
1315 #ifdef LIBPQOS2
1316   rdt_free_ngroups(g_rdt);
1317 #endif /* LIBPQOS2 */
1318   sfree(g_rdt);
1319
1320   return 0;
1321 }
1322
1323 void module_register(void) {
1324   plugin_register_init(RDT_PLUGIN, rdt_init);
1325   plugin_register_complex_config(RDT_PLUGIN, rdt_config);
1326   plugin_register_complex_read(NULL, RDT_PLUGIN, rdt_read, 0, NULL);
1327   plugin_register_shutdown(RDT_PLUGIN, rdt_shutdown);
1328 }