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