intel_rdt: collect data for monitored processes
[collectd.git] / src / intel_rdt.c
1 /**
2  * collectd - src/intel_rdt.c
3  *
4  * Copyright(c) 2016-2018 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  **/
29
30 #include "collectd.h"
31 #include "utils/common/common.h"
32 #include "utils/config_cores/config_cores.h"
33 #include <pqos.h>
34
35 #define RDT_PLUGIN "intel_rdt"
36
37 /* libpqos v2.0 or newer is required for process monitoring*/
38 #undef LIBPQOS2
39 #if defined(PQOS_VERSION) && PQOS_VERSION >= 20000
40 #define LIBPQOS2
41 #endif
42
43 #define RDT_PLUGIN "intel_rdt"
44
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)
48
49 #ifdef LIBPQOS2
50 /*
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
53  */
54 #define RDT_MAX_NAME_LEN 16
55 #define RDT_MAX_NAMES_GROUPS 64
56
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 /* Helper typedef for process name array
67  * Extra 1 char is added for string null termination.
68  */
69 typedef char proc_comm_t[RDT_MAX_NAME_LEN + 1];
70
71 /* Linked one-way list of pids. */
72 typedef struct pids_list_s {
73   pid_t pid;
74   struct pids_list_s *next;
75 } pids_list_t;
76
77 /* Holds process name and list of pids assigned to that name */
78 typedef struct proc_pids_s {
79   proc_comm_t proccess_name;
80   pids_list_t *pids;
81 } proc_pids_t;
82
83 struct rdt_name_group_s {
84   char *desc;
85   size_t num_names;
86   char **names;
87   proc_pids_t *proc_pids_array;
88   size_t monitored_pids_count;
89   enum pqos_mon_event events;
90 };
91 typedef struct rdt_name_group_s rdt_name_group_t;
92 #endif /* LIBPQOS2 */
93
94 struct rdt_ctx_s {
95   core_groups_list_t cores;
96   enum pqos_mon_event events[RDT_MAX_CORES];
97   struct pqos_mon_data *pcgroups[RDT_MAX_CORES];
98 #ifdef LIBPQOS2
99   rdt_name_group_t ngroups[RDT_MAX_NAMES_GROUPS];
100   struct pqos_mon_data *pngroups[RDT_MAX_NAMES_GROUPS];
101   size_t num_ngroups;
102 #endif /* LIBPQOS2 */
103   const struct pqos_cpuinfo *pqos_cpu;
104   const struct pqos_cap *pqos_cap;
105   const struct pqos_capability *cap_mon;
106 };
107 typedef struct rdt_ctx_s rdt_ctx_t;
108
109 static rdt_ctx_t *g_rdt;
110
111 static rdt_config_status g_state = UNKNOWN;
112
113 static int g_interface = -1;
114
115 #ifdef LIBPQOS2
116 static int isdupstr(const char *names[], const size_t size, const char *name) {
117   for (size_t i = 0; i < size; i++)
118     if (strncmp(names[i], name, (size_t)RDT_MAX_NAME_LEN) == 0)
119       return 1;
120
121   return 0;
122 }
123
124 /*
125  * NAME
126  *   strlisttoarray
127  *
128  * DESCRIPTION
129  *   Converts string representing list of strings into array of strings.
130  *   Allowed format is:
131  *     name,name1,name2,name3
132  *
133  * PARAMETERS
134  *   `str_list'  String representing list of strings.
135  *   `names'     Array to put extracted strings into.
136  *   `names_num' Variable to put number of extracted strings.
137  *
138  * RETURN VALUE
139  *    Number of elements placed into names.
140  */
141 static int strlisttoarray(char *str_list, char ***names, size_t *names_num) {
142   char *saveptr = NULL;
143
144   if (str_list == NULL || names == NULL)
145     return -EINVAL;
146
147   for (;;) {
148     char *token = strtok_r(str_list, ",", &saveptr);
149     if (token == NULL)
150       break;
151
152     str_list = NULL;
153
154     while (isspace(*token))
155       token++;
156
157     if (*token == '\0')
158       continue;
159
160     if (!(isdupstr((const char **)*names, *names_num, token)))
161       if (0 != strarray_add(names, names_num, token)) {
162         ERROR(RDT_PLUGIN ": Error allocating process name string");
163         return -ENOMEM;
164       }
165   }
166
167   return 0;
168 }
169
170 /*
171  * NAME
172  *   ngroup_cmp
173  *
174  * DESCRIPTION
175  *   Function to compare names in two name groups.
176  *
177  * PARAMETERS
178  *   `ng_a'      Pointer to name group a.
179  *   `ng_b'      Pointer to name group b.
180  *
181  * RETURN VALUE
182  *    1 if both groups contain the same names
183  *    0 if none of their names match
184  *    -1 if some but not all names match
185  */
186 static int ngroup_cmp(const rdt_name_group_t *ng_a,
187                       const rdt_name_group_t *ng_b) {
188   unsigned found = 0;
189
190   assert(ng_a != NULL);
191   assert(ng_b != NULL);
192
193   const size_t sz_a = (unsigned)ng_a->num_names;
194   const size_t sz_b = (unsigned)ng_b->num_names;
195   const char **tab_a = (const char **)ng_a->names;
196   const char **tab_b = (const char **)ng_b->names;
197
198   for (size_t i = 0; i < sz_a; i++) {
199     for (size_t j = 0; j < sz_b; j++)
200       if (strncmp(tab_a[i], tab_b[j], (size_t)RDT_MAX_NAME_LEN) == 0)
201         found++;
202   }
203   /* if no names are the same */
204   if (!found)
205     return 0;
206   /* if group contains same names */
207   if (sz_a == sz_b && sz_b == (size_t)found)
208     return 1;
209   /* if not all names are the same */
210   return -1;
211 }
212
213 /*
214  * NAME
215  *   oconfig_to_ngroups
216  *
217  * DESCRIPTION
218  *   Function to set the descriptions and names for each process names group.
219  *   Takes a config option containing list of strings that are used to set
220  *   process group values.
221  *
222  * PARAMETERS
223  *   `item'        Config option containing process names groups.
224  *   `groups'      Table of process name groups to set values in.
225  *   `max_groups'  Maximum number of process name groups allowed.
226  *
227  * RETURN VALUE
228  *   On success, the number of name groups set up. On error, appropriate
229  *   negative error value.
230  */
231 static int oconfig_to_ngroups(const oconfig_item_t *item,
232                               rdt_name_group_t *groups,
233                               const size_t max_groups) {
234   int index = 0;
235
236   assert(groups != NULL);
237   assert(max_groups > 0);
238   assert(item != NULL);
239
240   for (int j = 0; j < item->values_num; j++) {
241     int ret;
242     char value[DATA_MAX_NAME_LEN];
243
244     if ((item->values[j].value.string == NULL) ||
245         (strlen(item->values[j].value.string) == 0))
246       continue;
247
248     sstrncpy(value, item->values[j].value.string, sizeof(value));
249
250     ret = strlisttoarray(value, &groups[index].names, &groups[index].num_names);
251     if (ret != 0 || groups[index].num_names == 0) {
252       ERROR(RDT_PLUGIN ": Error parsing process names group (%s)",
253             item->values[j].value.string);
254       return -EINVAL;
255     }
256
257     /* set group description info */
258     groups[index].desc = sstrdup(item->values[j].value.string);
259     if (groups[index].desc == NULL) {
260       ERROR(RDT_PLUGIN ": Error allocating name group description");
261       return -ENOMEM;
262     }
263
264     groups[index].proc_pids_array = NULL;
265     groups[index].monitored_pids_count = 0;
266
267     index++;
268
269     if (index >= (const int)max_groups) {
270       WARNING(RDT_PLUGIN ": Too many process names groups configured");
271       return index;
272     }
273   }
274
275   return index;
276 }
277 #endif /* LIBPQOS2 */
278
279 #if COLLECT_DEBUG
280 static void rdt_dump_cgroups(void) {
281   char cores[RDT_MAX_CORES * 4];
282
283   if (g_rdt == NULL)
284     return;
285
286   DEBUG(RDT_PLUGIN ": Core Groups Dump");
287   DEBUG(RDT_PLUGIN ":  groups count: %" PRIsz, g_rdt->cores.num_cgroups);
288
289   for (size_t i = 0; i < g_rdt->cores.num_cgroups; i++) {
290     core_group_t *cgroup = g_rdt->cores.cgroups + i;
291
292     memset(cores, 0, sizeof(cores));
293     for (size_t j = 0; j < cgroup->num_cores; j++) {
294       snprintf(cores + strlen(cores), sizeof(cores) - strlen(cores) - 1, " %d",
295                cgroup->cores[j]);
296     }
297
298     DEBUG(RDT_PLUGIN ":  group[%zu]:", i);
299     DEBUG(RDT_PLUGIN ":    description: %s", cgroup->desc);
300     DEBUG(RDT_PLUGIN ":    cores: %s", cores);
301     DEBUG(RDT_PLUGIN ":    events: 0x%X", g_rdt->events[i]);
302   }
303
304   return;
305 }
306
307 #ifdef LIBPQOS2
308 static void rdt_dump_ngroups(void) {
309
310   char names[DATA_MAX_NAME_LEN];
311
312   if (g_rdt == NULL)
313     return;
314
315   DEBUG(RDT_PLUGIN ": Process Names Groups Dump");
316   DEBUG(RDT_PLUGIN ":  groups count: %" PRIsz, g_rdt->num_ngroups);
317
318   for (size_t i = 0; i < g_rdt->num_ngroups; i++) {
319     memset(names, 0, sizeof(names));
320     for (size_t j = 0; j < g_rdt->ngroups[i].num_names; j++)
321       snprintf(names + strlen(names), sizeof(names) - strlen(names) - 1, " %s",
322                g_rdt->ngroups[i].names[j]);
323
324     DEBUG(RDT_PLUGIN ":  group[%d]:", (int)i);
325     DEBUG(RDT_PLUGIN ":    description: %s", g_rdt->ngroups[i].desc);
326     DEBUG(RDT_PLUGIN ":    process names:%s", names);
327     DEBUG(RDT_PLUGIN ":    events: 0x%X", g_rdt->ngroups[i].events);
328   }
329
330   return;
331 }
332 #endif /* LIBPQOS2 */
333
334 static inline double bytes_to_kb(const double bytes) { return bytes / 1024.0; }
335
336 static inline double bytes_to_mb(const double bytes) {
337   return bytes / (1024.0 * 1024.0);
338 }
339
340 static void rdt_dump_cores_data(void) {
341 /*
342  * CORE - monitored group of cores
343  * RMID - Resource Monitoring ID associated with the monitored group
344  *        This is not available for monitoring with resource control
345  * LLC - last level cache occupancy
346  * MBL - local memory bandwidth
347  * MBR - remote memory bandwidth
348  */
349 #ifdef LIBPQOS2
350   if (g_interface == PQOS_INTER_OS_RESCTRL_MON) {
351     DEBUG(RDT_PLUGIN ":  CORE     LLC[KB]   MBL[MB]    MBR[MB]");
352   } else {
353     DEBUG(RDT_PLUGIN ":  CORE     RMID    LLC[KB]   MBL[MB]    MBR[MB]");
354   }
355 #else
356   DEBUG(RDT_PLUGIN ":  CORE     RMID    LLC[KB]   MBL[MB]    MBR[MB]");
357 #endif /* LIBPQOS2 */
358
359   for (size_t i = 0; i < g_rdt->cores.num_cgroups; i++) {
360     const struct pqos_event_values *pv = &g_rdt->pcgroups[i]->values;
361
362     double llc = bytes_to_kb(pv->llc);
363     double mbr = bytes_to_mb(pv->mbm_remote_delta);
364     double mbl = bytes_to_mb(pv->mbm_local_delta);
365 #ifdef LIBPQOS2
366     if (g_interface == PQOS_INTER_OS_RESCTRL_MON) {
367       DEBUG(RDT_PLUGIN ": [%s] %10.1f %10.1f %10.1f",
368             g_rdt->cores.cgroups[i].desc, llc, mbl, mbr);
369     } else {
370       DEBUG(RDT_PLUGIN ": [%s] %8u %10.1f %10.1f %10.1f",
371             g_rdt->cores.cgroups[i].desc, g_rdt->pcgroups[i]->poll_ctx[0].rmid,
372             llc, mbl, mbr);
373     }
374 #else
375     DEBUG(RDT_PLUGIN ": [%s] %8u %10.1f %10.1f %10.1f",
376           g_rdt->cores.cgroups[i].desc, g_rdt->pcgroups[i]->poll_ctx[0].rmid,
377           llc, mbl, mbr);
378 #endif /* LIBPQOS2 */
379   }
380 }
381
382 #ifdef LIBPQOS2
383 static void rdt_dump_pids_data(void) {
384   /*
385    * NAME - monitored group of processes
386    * PIDs - list of PID numbers in the NAME group
387    * LLC - last level cache occupancy
388    * MBL - local memory bandwidth
389    * MBR - remote memory bandwidth
390    */
391
392   DEBUG(RDT_PLUGIN ":  NAME     PIDs");
393   char pids[DATA_MAX_NAME_LEN];
394   for (size_t i = 0; i < g_rdt->num_ngroups; ++i) {
395     memset(pids, 0, sizeof(pids));
396     for (size_t j = 0; j < g_rdt->ngroups[i].num_names; ++j) {
397       pids_list_t *list = g_rdt->ngroups[i].proc_pids_array[j].pids;
398       while (list != NULL) {
399         snprintf(pids + strlen(pids), sizeof(pids) - strlen(pids) - 1, " %u",
400                  list->pid);
401         list = list->next;
402       }
403     }
404     DEBUG(RDT_PLUGIN ":  [%s] %s", g_rdt->ngroups[i].desc, pids);
405   }
406
407   DEBUG(RDT_PLUGIN ":  NAME    LLC[KB]   MBL[MB]    MBR[MB]");
408   for (size_t i = 0; i < g_rdt->num_ngroups; i++) {
409
410     const struct pqos_event_values *pv = &g_rdt->pngroups[i]->values;
411
412     double llc = bytes_to_kb(pv->llc);
413     double mbr = bytes_to_mb(pv->mbm_remote_delta);
414     double mbl = bytes_to_mb(pv->mbm_local_delta);
415
416     DEBUG(RDT_PLUGIN ":  [%s] %10.1f %10.1f %10.1f", g_rdt->ngroups[i].desc,
417           llc, mbl, mbr);
418   }
419 }
420 #endif /* LIBPQOS2 */
421 #endif /* COLLECT_DEBUG */
422
423 static void rdt_free_cgroups(void) {
424   config_cores_cleanup(&g_rdt->cores);
425   for (int i = 0; i < RDT_MAX_CORES; i++) {
426     sfree(g_rdt->pcgroups[i]);
427   }
428 }
429
430 #ifdef LIBPQOS2
431 static int pids_list_free(pids_list_t *list) {
432   assert(list);
433
434   pids_list_t *current = list;
435   while (current != NULL) {
436     pids_list_t *previous = current;
437     current = current->next;
438     sfree(previous);
439   }
440   return 0;
441 }
442
443 static void rdt_free_ngroups(void) {
444   for (int i = 0; i < RDT_MAX_NAMES_GROUPS; i++) {
445     if (g_rdt->ngroups[i].desc)
446       DEBUG(RDT_PLUGIN ": Freeing pids \'%s\' group\'s data...",
447             g_rdt->ngroups[i].desc);
448     sfree(g_rdt->ngroups[i].desc);
449     strarray_free(g_rdt->ngroups[i].names, g_rdt->ngroups[i].num_names);
450
451     if (g_rdt->ngroups[i].proc_pids_array) {
452       for (size_t j = 0; j < g_rdt->ngroups[i].num_names; ++j) {
453         if (NULL == g_rdt->ngroups[i].proc_pids_array[j].pids)
454           continue;
455         pids_list_free(g_rdt->ngroups[i].proc_pids_array[j].pids);
456       }
457
458       sfree(g_rdt->ngroups[i].proc_pids_array);
459     }
460
461     g_rdt->ngroups[i].num_names = 0;
462     sfree(g_rdt->pngroups[i]);
463   }
464 }
465 #endif /* LIBPQOS2 */
466
467 static int rdt_default_cgroups(void) {
468   unsigned num_cores = g_rdt->pqos_cpu->num_cores;
469
470   g_rdt->cores.cgroups = calloc(num_cores, sizeof(*g_rdt->cores.cgroups));
471   if (g_rdt->cores.cgroups == NULL) {
472     ERROR(RDT_PLUGIN ": Error allocating core groups array");
473     return -ENOMEM;
474   }
475   g_rdt->cores.num_cgroups = num_cores;
476
477   /* configure each core in separate group */
478   for (unsigned i = 0; i < num_cores; i++) {
479     core_group_t *cgroup = g_rdt->cores.cgroups + i;
480     char desc[DATA_MAX_NAME_LEN];
481
482     /* set core group info */
483     cgroup->cores = calloc(1, sizeof(*cgroup->cores));
484     if (cgroup->cores == NULL) {
485       ERROR(RDT_PLUGIN ": Error allocating cores array");
486       rdt_free_cgroups();
487       return -ENOMEM;
488     }
489     cgroup->num_cores = 1;
490     cgroup->cores[0] = i;
491
492     snprintf(desc, sizeof(desc), "%d", g_rdt->pqos_cpu->cores[i].lcore);
493     cgroup->desc = strdup(desc);
494     if (cgroup->desc == NULL) {
495       ERROR(RDT_PLUGIN ": Error allocating core group description");
496       rdt_free_cgroups();
497       return -ENOMEM;
498     }
499   }
500
501   return num_cores;
502 }
503
504 static int rdt_is_core_id_valid(unsigned int core_id) {
505
506   for (unsigned int i = 0; i < g_rdt->pqos_cpu->num_cores; i++)
507     if (core_id == g_rdt->pqos_cpu->cores[i].lcore)
508       return 1;
509
510   return 0;
511 }
512
513 #ifdef LIBPQOS2
514 static int rdt_is_proc_name_valid(const char *name) {
515
516   if (name != NULL) {
517     unsigned len = strlen(name);
518     if (len > 0 && len <= RDT_MAX_NAME_LEN)
519       return 1;
520     else {
521       DEBUG(RDT_PLUGIN
522             ": Process name \'%s\' is too long. Max supported len is %d chars.",
523             name, RDT_MAX_NAME_LEN);
524     }
525   }
526
527   return 0;
528 }
529 #endif /* LIBPQOS2 */
530
531 static int rdt_config_cgroups(oconfig_item_t *item) {
532   size_t n = 0;
533   enum pqos_mon_event events = 0;
534
535   if (config_cores_parse(item, &g_rdt->cores) < 0) {
536     rdt_free_cgroups();
537     ERROR(RDT_PLUGIN ": Error parsing core groups configuration.");
538     return -EINVAL;
539   }
540   n = g_rdt->cores.num_cgroups;
541
542   /* validate configured core id values */
543   for (size_t group_idx = 0; group_idx < n; group_idx++) {
544     core_group_t *cgroup = g_rdt->cores.cgroups + group_idx;
545     for (size_t core_idx = 0; core_idx < cgroup->num_cores; core_idx++) {
546       if (!rdt_is_core_id_valid(cgroup->cores[core_idx])) {
547         ERROR(RDT_PLUGIN ": Core group '%s' contains invalid core id '%u'",
548               cgroup->desc, cgroup->cores[core_idx]);
549         rdt_free_cgroups();
550         return -EINVAL;
551       }
552     }
553   }
554
555   if (n == 0) {
556     /* create default core groups if "Cores" config option is empty */
557     int ret = rdt_default_cgroups();
558     if (ret < 0) {
559       rdt_free_cgroups();
560       ERROR(RDT_PLUGIN ": Error creating default core groups configuration.");
561       return ret;
562     }
563     n = (size_t)ret;
564     INFO(RDT_PLUGIN
565          ": No core groups configured. Default core groups created.");
566   }
567
568   /* Get all available events on this platform */
569   for (unsigned int i = 0; i < g_rdt->cap_mon->u.mon->num_events; i++)
570     events |= g_rdt->cap_mon->u.mon->events[i].type;
571
572   events &= ~(PQOS_PERF_EVENT_LLC_MISS);
573
574   DEBUG(RDT_PLUGIN ": Number of cores in the system: %u",
575         g_rdt->pqos_cpu->num_cores);
576   DEBUG(RDT_PLUGIN ": Available events to monitor: %#x", events);
577
578   g_rdt->cores.num_cgroups = n;
579   for (int i = 0; i < n; i++) {
580     for (int j = 0; j < i; j++) {
581       int found = 0;
582       found = config_cores_cmp_cgroups(&g_rdt->cores.cgroups[j],
583                                        &g_rdt->cores.cgroups[i]);
584       if (found != 0) {
585         rdt_free_cgroups();
586         ERROR(RDT_PLUGIN ": Cannot monitor same cores in different groups.");
587         return -EINVAL;
588       }
589     }
590
591     g_rdt->events[i] = events;
592     g_rdt->pcgroups[i] = calloc(1, sizeof(*g_rdt->pcgroups[i]));
593     if (g_rdt->pcgroups[i] == NULL) {
594       rdt_free_cgroups();
595       ERROR(RDT_PLUGIN ": Failed to allocate memory for monitoring data.");
596       return -ENOMEM;
597     }
598   }
599
600   return 0;
601 }
602
603 #ifdef LIBPQOS2
604 static int rdt_config_ngroups(const oconfig_item_t *item) {
605   int n = 0;
606   enum pqos_mon_event events = 0;
607
608   if (item == NULL) {
609     DEBUG(RDT_PLUGIN ": ngroups_config: Invalid argument.");
610     return -EINVAL;
611   }
612
613   DEBUG(RDT_PLUGIN ": Process names groups [%d]:", item->values_num);
614   for (int j = 0; j < item->values_num; j++) {
615     if (item->values[j].type != OCONFIG_TYPE_STRING) {
616       ERROR(RDT_PLUGIN
617             ": given process names group value is not a string [idx=%d]",
618             j);
619       return -EINVAL;
620     }
621     DEBUG(RDT_PLUGIN ":  [%d]: %s", j, item->values[j].value.string);
622   }
623
624   n = oconfig_to_ngroups(item, g_rdt->ngroups, RDT_MAX_NAMES_GROUPS);
625   if (n < 0) {
626     rdt_free_ngroups();
627     ERROR(RDT_PLUGIN ": Error parsing process name groups configuration.");
628     return -EINVAL;
629   }
630
631   /* validate configured process name values */
632   for (int group_idx = 0; group_idx < n; group_idx++) {
633     for (size_t name_idx = 0; name_idx < g_rdt->ngroups[group_idx].num_names;
634          name_idx++) {
635       if (!rdt_is_proc_name_valid(g_rdt->ngroups[group_idx].names[name_idx])) {
636         ERROR(RDT_PLUGIN ": Process name group '%s' contains invalid name '%s'",
637               g_rdt->ngroups[group_idx].desc,
638               g_rdt->ngroups[group_idx].names[name_idx]);
639         rdt_free_ngroups();
640         return -EINVAL;
641       }
642     }
643   }
644
645   if (n == 0) {
646     ERROR(RDT_PLUGIN ": Empty process name groups configured.");
647     return -EINVAL;
648   }
649
650   /* Get all available events on this platform */
651   for (unsigned i = 0; i < g_rdt->cap_mon->u.mon->num_events; i++)
652     events |= g_rdt->cap_mon->u.mon->events[i].type;
653
654   events &= ~(PQOS_PERF_EVENT_LLC_MISS);
655
656   DEBUG(RDT_PLUGIN ": Available events to monitor: %#x", events);
657
658   g_rdt->num_ngroups = n;
659   for (int i = 0; i < n; i++) {
660     for (int j = 0; j < i; j++) {
661       int found = ngroup_cmp(&g_rdt->ngroups[j], &g_rdt->ngroups[i]);
662       if (found != 0) {
663         rdt_free_ngroups();
664         ERROR(RDT_PLUGIN
665               ": Cannot monitor same process name in different groups.");
666         return -EINVAL;
667       }
668     }
669
670     g_rdt->ngroups[i].events = events;
671     g_rdt->pngroups[i] = calloc(1, sizeof(*g_rdt->pngroups[i]));
672     if (g_rdt->pngroups[i] == NULL) {
673       rdt_free_ngroups();
674       ERROR(RDT_PLUGIN
675             ": Failed to allocate memory for process name monitoring data.");
676       return -ENOMEM;
677     }
678   }
679
680   return 0;
681 }
682
683 /*
684  * NAME
685  *   pids_list_add_pid
686  *
687  * DESCRIPTION
688  *   Adds pid at the end of the pids list.
689  *   Allocates memory for new pid element, it is up to user to free it.
690  *
691  * PARAMETERS
692  *   `list'     Head of target pids_list.
693  *   `pid'      Pid to be added.
694  *
695  * RETURN VALUE
696  *   On success, returns 0.
697  *   -1 on memory allocation error.
698  */
699 static int pids_list_add_pid(pids_list_t **list, const pid_t pid) {
700   assert(list);
701
702   pids_list_t *new_element = calloc(1, sizeof(*new_element));
703
704   if (new_element == NULL) {
705     ERROR(RDT_PLUGIN ": Alloc error\n");
706     return -1;
707   }
708   new_element->pid = pid;
709   new_element->next = NULL;
710
711   pids_list_t **current = list;
712   while (*current != NULL) {
713     current = &((*current)->next);
714   }
715   *current = new_element;
716   return 0;
717 }
718
719 /*
720  * NAME
721  *   pids_list_contains_pid
722  *
723  * DESCRIPTION
724  *   Tests if pids list contains specific pid.
725  *
726  * PARAMETERS
727  *   `list'     Head of pids_list.
728  *   `pid'      Pid to be searched for.
729  *
730  * RETURN VALUE
731  *   If PID found in list, returns 1,
732  *   Otherwise returns 0.
733  */
734 static int pids_list_contains_pid(pids_list_t *list, const pid_t pid) {
735   assert(list);
736
737   pids_list_t *current = list;
738   while (current != NULL) {
739     if (current->pid == pid)
740       return 1;
741     current = current->next;
742   }
743   return 0;
744 }
745
746 /*
747  * NAME
748  *   pids_list_add_pids_list
749  *
750  * DESCRIPTION
751  *   Adds pids list at the end of the pids list.
752  *   Allocates memory for new pid elements, it is up to user to free it.
753  *   Increases dst_num by a number of added PIDs.
754  *
755  * PARAMETERS
756  *   `dst'      Head of target PIDs list.
757  *   `src'      Head of source PIDs list.
758  *   `dst_num'  Variable to be increased by a number of appended PIDs.
759  *
760  * RETURN VALUE
761  *   On success, returns 0.
762  *   -1 on memory allocation error.
763  */
764 static int pids_list_add_pids_list(pids_list_t **dst, pids_list_t *src,
765                                    size_t *dst_num) {
766   assert(dst);
767   assert(src);
768   assert(dst_num);
769
770   pids_list_t *current = src;
771   int ret;
772
773   while (current != NULL) {
774     ret = pids_list_add_pid(dst, current->pid);
775     if (0 != ret)
776       return ret;
777
778     ++(*dst_num);
779     current = current->next;
780   }
781
782   return 0;
783 }
784
785 /*
786  * NAME
787  *   read_proc_name
788  *
789  * DESCRIPTION
790  *   Reads process name from given pid directory.
791  *   Strips new-line character (\n).
792  *
793  * PARAMETERS
794  *   `procfs_path` Path to systems proc directory (e.g. /proc)
795  *   `pid_entry'   Dirent for PID directory
796  *   `name'        Output buffer for process name, recommended proc_comm.
797  *   `out_size'    Output buffer size, recommended sizeof(proc_comm)
798  *
799  * RETURN VALUE
800  *   On success, the number of read bytes (includes stripped \n).
801  *   -1 on file open error
802  */
803 static int read_proc_name(const char *procfs_path,
804                           const struct dirent *pid_entry, char *name,
805                           const size_t out_size) {
806   assert(procfs_path);
807   assert(pid_entry);
808   assert(name);
809   assert(out_size);
810   memset(name, 0, out_size);
811
812   const char *comm_file_name = "comm";
813
814   char *path = ssnprintf_alloc("%s/%s/%s", procfs_path, pid_entry->d_name,
815                                comm_file_name);
816
817   FILE *f = fopen(path, "r");
818   if (f == NULL) {
819     ERROR(RDT_PLUGIN ": Failed to open comm file, error: %d\n", errno);
820     sfree(path);
821     return -1;
822   }
823   size_t read_length = fread(name, sizeof(char), out_size, f);
824   fclose(f);
825   sfree(path);
826   /* strip new line ending */
827   char *newline = strchr(name, '\n');
828   if (newline) {
829     *newline = '\0';
830   }
831
832   return read_length;
833 }
834
835 /*
836  * NAME
837  *   get_pid_number
838  *
839  * DESCRIPTION
840  *   Gets pid number for given /proc/pid directory entry or
841  *   returns error if input directory does not hold PID information.
842  *
843  * PARAMETERS
844  *   `entry'    Dirent for PID directory
845  *   `pid'      PID number to be filled
846  *
847  * RETURN VALUE
848  *   0 on success. -1 on error.
849  */
850 static int get_pid_number(struct dirent *entry, pid_t *pid) {
851   char *tmp_end; /* used for strtoul error check*/
852
853   if (pid == NULL || entry == NULL)
854     return -1;
855
856   if (entry->d_type != DT_DIR)
857     return -1;
858
859   /* trying to get pid number from directory name*/
860   *pid = strtoul(entry->d_name, &tmp_end, 10);
861   if (*tmp_end != '\0') {
862     return -1; /* conversion failed, not proc-pid */
863   }
864   /* all checks passed, marking as success */
865   return 0;
866 }
867
868 /*
869  * NAME
870  *   pids_list_to_array
871  *
872  * DESCRIPTION
873  *   Copies element from list to array. Assumes the space for the array is
874  *   allocated.
875  *
876  * PARAMETERS
877  *   `array'      First element of target array
878  *   `list'       Head of the list
879  *   `array_length' Length (element count) of the target array
880  */
881 static void pids_list_to_array(pid_t *array, pids_list_t *list,
882                                const size_t array_length) {
883
884   assert(list);
885   assert(array);
886   assert(array_length > 0);
887
888   size_t current = 0;
889
890   while (list != NULL && current < array_length) {
891     array[current] = list->pid;
892     list = list->next;
893     ++current;
894   }
895 }
896
897 /*
898  * NAME
899  *   initialize_proc_pids
900  *
901  * DESCRIPTION
902  *   Helper function to properly initialize array of proc_pids.
903  *   Allocates memory for proc_pids structs.
904  *
905  * PARAMETERS
906  *   `procs_names_array'      Array of null-terminated strings with
907  *                            process' names to be copied to new array
908  *   `procs_names_array_size' procs_names_array element count
909  *   `proc_pids_array'        Address of pointer, under which new
910  *                            array of proc_pids will be allocated.
911  *                            Must be NULL.
912  * RETURN VALUE
913  *   0 on success. Negative number on error:
914  *   -1: allocation error
915  */
916 static int initialize_proc_pids(const char **procs_names_array,
917                                 const size_t procs_names_array_size,
918                                 proc_pids_t **proc_pids_array) {
919
920   assert(proc_pids_array);
921   assert(NULL == *proc_pids_array);
922
923   /* Copy procs names to output array. Initialize pids list with NULL value. */
924   *proc_pids_array = calloc(procs_names_array_size, sizeof(**proc_pids_array));
925
926   if (NULL == *proc_pids_array)
927     return -1;
928
929   for (size_t i = 0; i < procs_names_array_size; ++i) {
930     sstrncpy((*proc_pids_array)[i].proccess_name, procs_names_array[i],
931              STATIC_ARRAY_SIZE((*proc_pids_array)[i].proccess_name));
932     (*proc_pids_array)[i].pids = NULL;
933   }
934
935   return 0;
936 }
937
938 /*
939  * NAME
940  *   fetch_pids_for_procs
941  *
942  * DESCRIPTION
943  *   Finds PIDs matching given process's names.
944  *   Searches all PID directories in /proc fs and
945  *   allocates memory for proc_pids structs, it is up to user to free it.
946  *   Output array will have same element count as input array.
947  *
948  * PARAMETERS
949  *   `procfs_path'            Path to systems proc directory (e.g. /proc)
950  *   `procs_names_array'      Array of null-terminated strings with
951  *                            process' names to be copied to new array
952  *   `procs_names_array_size' procs_names_array element count
953  *   `proc_pids_array'        Address of pointer, under which new
954  *                            array of proc_pids will be allocated.
955  *                            Must be NULL.
956  *
957  * RETURN VALUE
958  *   0 on success. -1 on error.
959  */
960 static int fetch_pids_for_procs(const char *procfs_path,
961                                 const char **procs_names_array,
962                                 const size_t procs_names_array_size,
963                                 proc_pids_t **proc_pids_array) {
964   assert(procfs_path);
965   assert(procs_names_array);
966   assert(procs_names_array_size);
967
968   DIR *proc_dir = opendir(procfs_path);
969   if (proc_dir == NULL) {
970     ERROR(RDT_PLUGIN ": Could not open %s directory, error: %d", procfs_path,
971           errno);
972     return -1;
973   }
974
975   int init_result = initialize_proc_pids(
976       procs_names_array, procs_names_array_size, proc_pids_array);
977   if (0 != init_result)
978     return -1;
979
980   /* Go through procfs and find PIDS and their comms */
981   struct dirent *entry;
982   while ((entry = readdir(proc_dir)) != NULL) {
983
984     pid_t pid;
985     int pid_conversion = get_pid_number(entry, &pid);
986     if (pid_conversion < 0)
987       continue;
988
989     proc_comm_t comm;
990     int read_result =
991         read_proc_name(procfs_path, entry, comm, sizeof(proc_comm_t));
992     if (read_result <= 0) {
993       ERROR(RDT_PLUGIN ": Comm file skipped. Read result: %d", read_result);
994       continue;
995     }
996
997     /* Try to find comm in input procs array (proc_pids_array has same names) */
998     for (size_t i = 0; i < procs_names_array_size; ++i) {
999       if (0 == strncmp(comm, (*proc_pids_array)[i].proccess_name,
1000                        STATIC_ARRAY_SIZE(comm)))
1001         pids_list_add_pid(&((*proc_pids_array)[i].pids), pid);
1002     }
1003   }
1004
1005   int close_result = closedir(proc_dir);
1006   if (0 != close_result) {
1007     ERROR(RDT_PLUGIN ": failed to close %s directory, error: %d", procfs_path,
1008           errno);
1009     sfree(*proc_pids_array);
1010     return -1;
1011   }
1012   return 0;
1013 }
1014 #endif /* LIBPQOS2 */
1015
1016 static void rdt_pqos_log(void *context, const size_t size, const char *msg) {
1017   DEBUG(RDT_PLUGIN ": %s", msg);
1018 }
1019
1020 static int rdt_preinit(void) {
1021   int ret;
1022
1023   if (g_rdt != NULL) {
1024     /* already initialized if config callback was called before init callback */
1025     return 0;
1026   }
1027
1028   g_rdt = calloc(1, sizeof(*g_rdt));
1029   if (g_rdt == NULL) {
1030     ERROR(RDT_PLUGIN ": Failed to allocate memory for rdt context.");
1031     return -ENOMEM;
1032   }
1033
1034   struct pqos_config pqos = {.fd_log = -1,
1035                              .callback_log = rdt_pqos_log,
1036                              .context_log = NULL,
1037                              .verbose = 0,
1038 #ifdef LIBPQOS2
1039                              .interface = PQOS_INTER_OS_RESCTRL_MON};
1040   DEBUG(RDT_PLUGIN ": Initializing PQoS with RESCTRL interface");
1041 #else
1042                              .interface = PQOS_INTER_MSR};
1043   DEBUG(RDT_PLUGIN ": Initializing PQoS with MSR interface");
1044 #endif
1045
1046   ret = pqos_init(&pqos);
1047   DEBUG(RDT_PLUGIN ": PQoS initialization result: [%d]", ret);
1048
1049 #ifdef LIBPQOS2
1050   if (ret == PQOS_RETVAL_INTER) {
1051     pqos.interface = PQOS_INTER_MSR;
1052     DEBUG(RDT_PLUGIN ": Initializing PQoS with MSR interface");
1053     ret = pqos_init(&pqos);
1054     DEBUG(RDT_PLUGIN ": PQoS initialization result: [%d]", ret);
1055   }
1056 #endif
1057
1058   if (ret != PQOS_RETVAL_OK) {
1059     ERROR(RDT_PLUGIN ": Error initializing PQoS library!");
1060     goto rdt_preinit_error1;
1061   }
1062
1063   g_interface = pqos.interface;
1064
1065   ret = pqos_cap_get(&g_rdt->pqos_cap, &g_rdt->pqos_cpu);
1066   if (ret != PQOS_RETVAL_OK) {
1067     ERROR(RDT_PLUGIN ": Error retrieving PQoS capabilities.");
1068     goto rdt_preinit_error2;
1069   }
1070
1071   ret = pqos_cap_get_type(g_rdt->pqos_cap, PQOS_CAP_TYPE_MON, &g_rdt->cap_mon);
1072   if (ret == PQOS_RETVAL_PARAM) {
1073     ERROR(RDT_PLUGIN ": Error retrieving monitoring capabilities.");
1074     goto rdt_preinit_error2;
1075   }
1076
1077   if (g_rdt->cap_mon == NULL) {
1078     ERROR(
1079         RDT_PLUGIN
1080         ": Monitoring capability not detected. Nothing to do for the plugin.");
1081     goto rdt_preinit_error2;
1082   }
1083
1084   /* Reset pqos monitoring groups registers */
1085   pqos_mon_reset();
1086
1087   return 0;
1088
1089 rdt_preinit_error2:
1090   pqos_fini();
1091
1092 rdt_preinit_error1:
1093   sfree(g_rdt);
1094
1095   return -1;
1096 }
1097
1098 static int rdt_config(oconfig_item_t *ci) {
1099   if (rdt_preinit() != 0) {
1100     g_state = CONFIGURATION_ERROR;
1101     /* if we return -1 at this point collectd
1102       reports a failure in configuration and
1103       aborts
1104     */
1105     return (0);
1106   }
1107
1108   for (int i = 0; i < ci->children_num; i++) {
1109     oconfig_item_t *child = ci->children + i;
1110
1111     if (strncasecmp("Cores", child->key, (size_t)strlen("Cores")) == 0) {
1112       if (rdt_config_cgroups(child) != 0) {
1113         g_state = CONFIGURATION_ERROR;
1114         /* if we return -1 at this point collectd
1115            reports a failure in configuration and
1116            aborts
1117          */
1118         return (0);
1119       }
1120
1121 #if COLLECT_DEBUG
1122       rdt_dump_cgroups();
1123 #endif /* COLLECT_DEBUG */
1124     } else if (strncasecmp("Processes", child->key,
1125                            (size_t)strlen("Processes")) == 0) {
1126 #ifdef LIBPQOS2
1127       if (g_interface != PQOS_INTER_OS_RESCTRL_MON) {
1128         ERROR(RDT_PLUGIN ": Configuration parameter \"%s\" not supported. "
1129                          "Resctrl monitoring is needed for PIDs monitoring.",
1130               child->key);
1131         g_state = CONFIGURATION_ERROR;
1132         /* if we return -1 at this point collectd
1133            reports a failure in configuration and
1134            aborts
1135          */
1136         return 0;
1137       }
1138
1139       if (rdt_config_ngroups(child) != 0) {
1140         g_state = CONFIGURATION_ERROR;
1141         /* if we return -1 at this point collectd
1142            reports a failure in configuration and
1143            aborts
1144          */
1145         return 0;
1146       }
1147
1148 #if COLLECT_DEBUG
1149       rdt_dump_ngroups();
1150 #endif /* COLLECT_DEBUG */
1151 #else  /* !LIBPQOS2 */
1152       ERROR(RDT_PLUGIN ": Configuration parameter \"%s\" not supported, please "
1153                        "recompile collectd with libpqos version 2.0 or newer.",
1154             child->key);
1155 #endif /* LIBPQOS2 */
1156     } else {
1157       ERROR(RDT_PLUGIN ": Unknown configuration parameter \"%s\".", child->key);
1158     }
1159   }
1160
1161   return 0;
1162 }
1163
1164 static void rdt_submit_derive(const char *cgroup, const char *type,
1165                               const char *type_instance, derive_t value) {
1166   value_list_t vl = VALUE_LIST_INIT;
1167
1168   vl.values = &(value_t){.derive = value};
1169   vl.values_len = 1;
1170
1171   sstrncpy(vl.plugin, RDT_PLUGIN, sizeof(vl.plugin));
1172   snprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%s", cgroup);
1173   sstrncpy(vl.type, type, sizeof(vl.type));
1174   if (type_instance)
1175     sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
1176
1177   plugin_dispatch_values(&vl);
1178 }
1179
1180 static void rdt_submit_gauge(const char *cgroup, const char *type,
1181                              const char *type_instance, gauge_t value) {
1182   value_list_t vl = VALUE_LIST_INIT;
1183
1184   vl.values = &(value_t){.gauge = value};
1185   vl.values_len = 1;
1186
1187   sstrncpy(vl.plugin, RDT_PLUGIN, sizeof(vl.plugin));
1188   snprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%s", cgroup);
1189   sstrncpy(vl.type, type, sizeof(vl.type));
1190   if (type_instance)
1191     sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
1192
1193   plugin_dispatch_values(&vl);
1194 }
1195
1196 #ifdef LIBPQOS2
1197 static int rdt_pid_list_diff(pids_list_t *prev, pids_list_t *curr,
1198                              pids_list_t **added, size_t *added_num,
1199                              pids_list_t **removed, size_t *removed_num) {
1200   assert(prev || curr);
1201   assert(added);
1202   assert(removed);
1203
1204   if (NULL == prev) {
1205     /* append all PIDs from curr to added*/
1206     return pids_list_add_pids_list(added, curr, added_num);
1207   } else if (NULL == curr) {
1208     /* append all PIDs from prev to removed*/
1209     return pids_list_add_pids_list(removed, prev, removed_num);
1210   }
1211
1212   pids_list_t *item = prev;
1213   while (item != NULL) {
1214     if (0 == pids_list_contains_pid(curr, item->pid)) {
1215       pids_list_add_pid(removed, item->pid);
1216       ++(*removed_num);
1217     }
1218     item = item->next;
1219   }
1220
1221   item = curr;
1222   while (item != NULL) {
1223     if (0 == pids_list_contains_pid(prev, item->pid)) {
1224       pids_list_add_pid(added, item->pid);
1225       ++(*added_num);
1226     }
1227     item = item->next;
1228   }
1229
1230   return 0;
1231 }
1232
1233 static int rdt_refresh_ngroup(rdt_name_group_t *ngroup,
1234                               struct pqos_mon_data *group_mon_data) {
1235
1236   int result = 0;
1237
1238   if (NULL == ngroup)
1239     return -1;
1240
1241   if (NULL == ngroup->proc_pids_array) {
1242     ERROR(RDT_PLUGIN
1243           ": rdt_refresh_ngroup: \'%s\' uninitialized process pids array.",
1244           ngroup->desc);
1245
1246     return -1;
1247   }
1248
1249   DEBUG(RDT_PLUGIN ": rdt_refresh_ngroup: \'%s\' process names group.",
1250         ngroup->desc);
1251
1252   proc_pids_t *proc_pids_array_prev = ngroup->proc_pids_array;
1253   proc_pids_t *proc_pids_array_curr = NULL;
1254
1255   int fetch_result =
1256       fetch_pids_for_procs(RDT_PROC_PATH, (const char **)ngroup->names,
1257                            ngroup->num_names, &proc_pids_array_curr);
1258
1259   if (0 != fetch_result) {
1260     ERROR(RDT_PLUGIN ": rdt_refresh_ngroup: \'%s\' failed to fetch PIDs.",
1261           ngroup->desc);
1262     return fetch_result;
1263   }
1264
1265   pids_list_t *new_pids = NULL;
1266   size_t new_pids_count = 0;
1267
1268   pids_list_t *lost_pids = NULL;
1269   size_t lost_pids_count = 0;
1270
1271   for (size_t i = 0; i < ngroup->num_names; ++i) {
1272     if (NULL == proc_pids_array_prev[i].pids &&
1273         NULL == proc_pids_array_curr[i].pids)
1274       continue;
1275     int diff_result = rdt_pid_list_diff(
1276         proc_pids_array_prev[i].pids, proc_pids_array_curr[i].pids, &new_pids,
1277         &new_pids_count, &lost_pids, &lost_pids_count);
1278     if (0 != diff_result) {
1279       ERROR(RDT_PLUGIN
1280             ": rdt_refresh_ngroup: \'%s\'. Error [%d] during PID diff.",
1281             ngroup->desc, diff_result);
1282       result = -1;
1283       goto cleanup;
1284     }
1285   }
1286
1287   DEBUG(RDT_PLUGIN ": rdt_refresh_ngroup: \'%s\' process names group, added: "
1288                    "%u, removed: %u.",
1289         ngroup->desc, (unsigned)new_pids_count, (unsigned)lost_pids_count);
1290
1291   if (new_pids_count != 0 || lost_pids_count != 0) {
1292
1293     if (new_pids) {
1294       pid_t new_pids_array[new_pids_count];
1295       pids_list_to_array(new_pids_array, new_pids,
1296                          STATIC_ARRAY_SIZE(new_pids_array));
1297
1298       /* no pids are monitored for this group yet: start monitoring */
1299       if (0 == ngroup->monitored_pids_count) {
1300
1301         int start_result =
1302             pqos_mon_start_pids(new_pids_count, new_pids_array, ngroup->events,
1303                                 (void *)ngroup->desc, group_mon_data);
1304         if (PQOS_RETVAL_OK == start_result) {
1305           ngroup->monitored_pids_count = new_pids_count;
1306         } else {
1307           ERROR(RDT_PLUGIN ": rdt_refresh_ngroup: \'%s\'. Error [%d] while "
1308                            "STARTING pids monitoring",
1309                 ngroup->desc, start_result);
1310           result = -1;
1311           goto pqos_error_recovery;
1312         }
1313
1314       } else {
1315
1316         int add_result =
1317             pqos_mon_add_pids(new_pids_count, new_pids_array, group_mon_data);
1318         if (PQOS_RETVAL_OK == add_result)
1319           ngroup->monitored_pids_count += new_pids_count;
1320         else {
1321           ERROR(RDT_PLUGIN
1322                 ": rdt_refresh_ngroup: \'%s\'. Error [%d] while ADDING pids.",
1323                 ngroup->desc, add_result);
1324           result = -1;
1325           goto pqos_error_recovery;
1326         }
1327       }
1328     }
1329
1330     if (lost_pids) {
1331       pid_t lost_pids_array[lost_pids_count];
1332       pids_list_to_array(lost_pids_array, lost_pids,
1333                          STATIC_ARRAY_SIZE(lost_pids_array));
1334
1335       if (lost_pids_count == ngroup->monitored_pids_count) {
1336         /* all pids for this group are lost: stop monitoring */
1337         int stop_result = pqos_mon_stop(group_mon_data);
1338         if (PQOS_RETVAL_OK != stop_result) {
1339           ERROR(RDT_PLUGIN ": rdt_refresh_ngroup: \'%s\'. Error [%d] while "
1340                            "STOPPING monitoring",
1341                 ngroup->desc, stop_result);
1342           result = -1;
1343           goto pqos_error_recovery;
1344         }
1345         ngroup->monitored_pids_count = 0;
1346       } else {
1347         assert(lost_pids_count < ngroup->monitored_pids_count);
1348         int remove_result = pqos_mon_remove_pids(
1349             lost_pids_count, lost_pids_array, group_mon_data);
1350         if (PQOS_RETVAL_OK == remove_result) {
1351           ngroup->monitored_pids_count -= lost_pids_count;
1352         } else {
1353           ERROR(RDT_PLUGIN
1354                 ": rdt_refresh_ngroup: \'%s\'. Error [%d] while REMOVING pids.",
1355                 ngroup->desc, remove_result);
1356           result = -1;
1357           goto pqos_error_recovery;
1358         }
1359       }
1360     }
1361
1362     ngroup->proc_pids_array = proc_pids_array_curr;
1363   }
1364
1365   goto cleanup;
1366
1367 pqos_error_recovery:
1368   /* Why?
1369    * Resources might be temporary unavailable.
1370    *
1371    * How?
1372    * Collectd will halt the reading thread for this
1373    * plugin if it returns an error.
1374    * Consecutive errors will be increasing the read period
1375    * up to 1 day interval.
1376    * On pqos error stop monitoring current group
1377    * and reset the proc_pids array
1378    * monitoring will be restarted on next collectd read cycle
1379    */
1380   DEBUG(RDT_PLUGIN ": rdt_refresh_ngroup: \'%s\' group RESET after error.",
1381         ngroup->desc);
1382   pqos_mon_stop(group_mon_data);
1383   for (size_t i = 0; i < ngroup->num_names; ++i) {
1384     if (ngroup->proc_pids_array[i].pids)
1385       pids_list_free(ngroup->proc_pids_array[i].pids);
1386   }
1387   sfree(ngroup->proc_pids_array);
1388
1389   initialize_proc_pids((const char **)ngroup->names, ngroup->num_names,
1390                        &ngroup->proc_pids_array);
1391   ngroup->monitored_pids_count = 0;
1392
1393 cleanup:
1394   if (ngroup->proc_pids_array == proc_pids_array_curr) {
1395     assert(proc_pids_array_curr);
1396     /* new list was successfully saved, free the old one */
1397     for (size_t i = 0; i < ngroup->num_names; ++i)
1398       if (proc_pids_array_prev[i].pids)
1399         pids_list_free(proc_pids_array_prev[i].pids);
1400
1401     sfree(proc_pids_array_prev);
1402
1403   } else {
1404     /* new list was not saved. Free the new list, keep the old one*/
1405     for (size_t i = 0; i < ngroup->num_names; ++i)
1406       if (proc_pids_array_curr[i].pids)
1407         pids_list_free(proc_pids_array_curr[i].pids);
1408
1409     sfree(proc_pids_array_curr);
1410   }
1411
1412   if (new_pids)
1413     pids_list_free(new_pids);
1414
1415   if (lost_pids)
1416     pids_list_free(lost_pids);
1417
1418   return result;
1419 }
1420
1421 static int read_pids_data() {
1422
1423   if (0 == g_rdt->num_ngroups) {
1424     DEBUG(RDT_PLUGIN ": read_pids_data: not configured - PIDs read skipped");
1425     return 0;
1426   }
1427
1428   DEBUG(RDT_PLUGIN ": read_pids_data: Scanning active groups");
1429   struct pqos_mon_data *active_groups[RDT_MAX_NAMES_GROUPS] = {0};
1430   size_t active_group_idx = 0;
1431   for (size_t pngroups_idx = 0;
1432        pngroups_idx < STATIC_ARRAY_SIZE(g_rdt->pngroups); ++pngroups_idx)
1433     if (0 != g_rdt->ngroups[pngroups_idx].monitored_pids_count)
1434       active_groups[active_group_idx++] = g_rdt->pngroups[pngroups_idx];
1435
1436   int ret = 0;
1437
1438   if (0 == active_group_idx) {
1439     DEBUG(RDT_PLUGIN ": read_pids_data: no active groups - PIDs read skipped");
1440     goto groups_refresh;
1441   }
1442
1443   DEBUG(RDT_PLUGIN ": read_pids_data: PIDs data polling");
1444
1445   int poll_result = pqos_mon_poll(active_groups, active_group_idx);
1446   if (poll_result != PQOS_RETVAL_OK) {
1447     ERROR(RDT_PLUGIN ": read_pids_data: Failed to poll monitoring data for "
1448                      "pids. Error [%d].",
1449           poll_result);
1450     ret = -poll_result;
1451     goto groups_refresh;
1452   }
1453
1454   for (size_t i = 0; i < g_rdt->num_ngroups; i++) {
1455     enum pqos_mon_event mbm_events =
1456         (PQOS_MON_EVENT_LMEM_BW | PQOS_MON_EVENT_TMEM_BW |
1457          PQOS_MON_EVENT_RMEM_BW);
1458
1459     if (g_rdt->pngroups[i] == NULL ||
1460         g_rdt->ngroups[i].monitored_pids_count == 0)
1461       continue;
1462
1463     const struct pqos_event_values *pv = &g_rdt->pngroups[i]->values;
1464
1465     /* Submit only monitored events data */
1466
1467     if (g_rdt->ngroups[i].events & PQOS_MON_EVENT_L3_OCCUP)
1468       rdt_submit_gauge(g_rdt->ngroups[i].desc, "bytes", "llc", pv->llc);
1469
1470     if (g_rdt->ngroups[i].events & PQOS_PERF_EVENT_IPC)
1471       rdt_submit_gauge(g_rdt->ngroups[i].desc, "ipc", NULL, pv->ipc);
1472
1473     if (g_rdt->ngroups[i].events & mbm_events) {
1474       rdt_submit_derive(g_rdt->ngroups[i].desc, "memory_bandwidth", "local",
1475                         pv->mbm_local_delta);
1476       rdt_submit_derive(g_rdt->ngroups[i].desc, "memory_bandwidth", "remote",
1477                         pv->mbm_remote_delta);
1478     }
1479   }
1480
1481 #if COLLECT_DEBUG
1482   rdt_dump_pids_data();
1483 #endif /* COLLECT_DEBUG */
1484
1485 groups_refresh:
1486   for (size_t i = 0; i < g_rdt->num_ngroups; i++) {
1487     int refresh_result =
1488         rdt_refresh_ngroup(&(g_rdt->ngroups[i]), g_rdt->pngroups[i]);
1489
1490     if (0 != refresh_result) {
1491       ERROR(RDT_PLUGIN ": read_pids_data: NGroup %zu refresh failed. Error: %d",
1492             i, refresh_result);
1493       if (0 == ret) {
1494         /* refresh error will be escalated only if there were no
1495          * errors before.
1496          */
1497         ret = refresh_result;
1498       }
1499     }
1500   }
1501
1502   assert(ret <= 0);
1503   return ret;
1504 }
1505
1506 static void rdt_init_pids_monitoring() {
1507   for (size_t group_idx = 0; group_idx < g_rdt->num_ngroups; group_idx++) {
1508     /*
1509      * Each group must have not-null proc_pids array.
1510      * Initial refresh is not mandatory for proper
1511      * PIDs statistics detection.
1512      */
1513     rdt_name_group_t *ng = &g_rdt->ngroups[group_idx];
1514     int init_result = initialize_proc_pids((const char **)ng->names,
1515                                            ng->num_names, &ng->proc_pids_array);
1516     if (0 != init_result) {
1517       ERROR(RDT_PLUGIN
1518             ": Initialization of proc_pids for group %zu failed. Error: %d",
1519             group_idx, init_result);
1520       continue;
1521     }
1522
1523     int refresh_result = rdt_refresh_ngroup(&(g_rdt->ngroups[group_idx]),
1524                                             g_rdt->pngroups[group_idx]);
1525     if (0 != refresh_result)
1526       ERROR(RDT_PLUGIN ": Initial refresh of group %zu failed. Error: %d",
1527             group_idx, refresh_result);
1528   }
1529 }
1530 #endif /* LIBPQOS2 */
1531
1532 static int read_cores_data() {
1533
1534   if (0 == g_rdt->cores.num_cgroups) {
1535     DEBUG(RDT_PLUGIN ": read_cores_data: not configured - Cores read skipped");
1536     return 0;
1537   }
1538   DEBUG(RDT_PLUGIN ": read_cores_data: Cores data poll");
1539
1540   int ret =
1541       pqos_mon_poll(&g_rdt->pcgroups[0], (unsigned)g_rdt->cores.num_cgroups);
1542   if (ret != PQOS_RETVAL_OK) {
1543     ERROR(RDT_PLUGIN ": read_cores_data: Failed to poll monitoring data for "
1544                      "cores. Error [%d].",
1545           ret);
1546     return -1;
1547   }
1548
1549   for (size_t i = 0; i < g_rdt->cores.num_cgroups; i++) {
1550     core_group_t *cgroup = g_rdt->cores.cgroups + i;
1551     enum pqos_mon_event mbm_events =
1552         (PQOS_MON_EVENT_LMEM_BW | PQOS_MON_EVENT_TMEM_BW |
1553          PQOS_MON_EVENT_RMEM_BW);
1554
1555     const struct pqos_event_values *pv = &g_rdt->pcgroups[i]->values;
1556
1557     /* Submit only monitored events data */
1558
1559     if (g_rdt->events[i] & PQOS_MON_EVENT_L3_OCCUP)
1560       rdt_submit_gauge(cgroup->desc, "bytes", "llc", pv->llc);
1561
1562     if (g_rdt->events[i] & PQOS_PERF_EVENT_IPC)
1563       rdt_submit_gauge(cgroup->desc, "ipc", NULL, pv->ipc);
1564
1565     if (g_rdt->events[i] & mbm_events) {
1566       rdt_submit_derive(cgroup->desc, "memory_bandwidth", "local",
1567                         pv->mbm_local_delta);
1568       rdt_submit_derive(cgroup->desc, "memory_bandwidth", "remote",
1569                         pv->mbm_remote_delta);
1570     }
1571   }
1572
1573 #if COLLECT_DEBUG
1574   rdt_dump_cores_data();
1575 #endif /* COLLECT_DEBUG */
1576
1577   return 0;
1578 }
1579
1580 static int rdt_read(__attribute__((unused)) user_data_t *ud) {
1581
1582   if (g_rdt == NULL) {
1583     ERROR(RDT_PLUGIN ": rdt_read: plugin not initialized.");
1584     return -EINVAL;
1585   }
1586
1587   int cores_read_result = read_cores_data();
1588
1589 #ifdef LIBPQOS2
1590   int pids_read_result = read_pids_data();
1591 #endif /* LIBPQOS2 */
1592
1593   if (0 != cores_read_result)
1594     return cores_read_result;
1595
1596 #ifdef LIBPQOS2
1597   if (0 != pids_read_result)
1598     return pids_read_result;
1599 #endif /* LIBPQOS2 */
1600
1601   return 0;
1602 }
1603
1604 static void rdt_init_cores_monitoring() {
1605   for (size_t i = 0; i < g_rdt->cores.num_cgroups; i++) {
1606     core_group_t *cg = g_rdt->cores.cgroups + i;
1607
1608     int mon_start_result =
1609         pqos_mon_start(cg->num_cores, cg->cores, g_rdt->events[i],
1610                        (void *)cg->desc, g_rdt->pcgroups[i]);
1611
1612     if (mon_start_result != PQOS_RETVAL_OK)
1613       ERROR(RDT_PLUGIN
1614             ": Error starting cores monitoring group %s (pqos status=%d)",
1615             cg->desc, mon_start_result);
1616   }
1617 }
1618
1619 static int rdt_init(void) {
1620
1621   if (g_state == CONFIGURATION_ERROR)
1622     return -1;
1623
1624   int rdt_preinint_result = rdt_preinit();
1625   if (rdt_preinint_result != 0)
1626     return rdt_preinint_result;
1627
1628   rdt_init_cores_monitoring();
1629 #ifdef LIBPQOS2
1630   rdt_init_pids_monitoring();
1631 #endif /* LIBPQOS2 */
1632
1633   return 0;
1634 }
1635
1636 static int rdt_shutdown(void) {
1637   int ret;
1638
1639   DEBUG(RDT_PLUGIN ": rdt_shutdown.");
1640
1641   if (g_rdt == NULL)
1642     return 0;
1643
1644   /* Stop monitoring cores */
1645   for (size_t i = 0; i < g_rdt->cores.num_cgroups; i++) {
1646     pqos_mon_stop(g_rdt->pcgroups[i]);
1647   }
1648
1649 /* Stop pids monitoring */
1650 #ifdef LIBPQOS2
1651   for (size_t i = 0; i < g_rdt->num_ngroups; i++)
1652     pqos_mon_stop(g_rdt->pngroups[i]);
1653 #endif
1654
1655   ret = pqos_fini();
1656   if (ret != PQOS_RETVAL_OK)
1657     ERROR(RDT_PLUGIN ": Error shutting down PQoS library.");
1658
1659   rdt_free_cgroups();
1660 #ifdef LIBPQOS2
1661   rdt_free_ngroups();
1662 #endif /* LIBPQOS2 */
1663   sfree(g_rdt);
1664
1665   return 0;
1666 }
1667
1668 void module_register(void) {
1669   plugin_register_init(RDT_PLUGIN, rdt_init);
1670   plugin_register_complex_config(RDT_PLUGIN, rdt_config);
1671   plugin_register_complex_read(NULL, RDT_PLUGIN, rdt_read, 0, NULL);
1672   plugin_register_shutdown(RDT_PLUGIN, rdt_shutdown);
1673 }