2 * collectd - src/utils_config_cores.c
4 * Copyright(c) 2018 Intel Corporation. All rights reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * Kamil Wiatrowski <kamilx.wiatrowski@intel.com>
32 #include "utils_config_cores.h"
34 #define UTIL_NAME "utils_config_cores"
37 #define MAX_SOCKET_CORES 64
38 #define MAX_CORES (MAX_SOCKET_CORES * MAX_SOCKETS)
40 static inline _Bool is_in_list(unsigned val, const unsigned *list, size_t len) {
41 for (size_t i = 0; i < len; i++)
47 static int str_to_uint(const char *s, unsigned *n) {
48 if (s == NULL || n == NULL)
52 *n = (unsigned)strtoul(s, &endptr, 0);
53 if (*s == '\0' || *endptr != '\0') {
54 ERROR(UTIL_NAME ": Failed to parse '%s' into unsigned number", s);
66 * Converts string of characters representing list of numbers into array of
67 * numbers. Allowed formats are:
72 * Numbers can be in decimal or hexadecimal format.
75 * `s' String representing list of unsigned numbers.
76 * `nums' Array to put converted numeric values into.
77 * `nums_len' Maximum number of elements that nums can accommodate.
80 * Number of elements placed into nums.
82 static size_t str_list_to_nums(char *s, unsigned *nums, size_t nums_len) {
87 while ((token = strtok_r(s, ",", &saveptr))) {
89 unsigned start, end = 0;
92 while (isspace(*token))
97 pos = strchr(token, '-');
102 if (str_to_uint(token, &start))
106 if (str_to_uint(pos + 1, &end))
113 unsigned swap = start;
118 for (unsigned i = start; i <= end; i++) {
119 if (is_in_list(i, nums, idx))
121 if (idx >= nums_len) {
122 WARNING(UTIL_NAME ": exceeded the cores number limit: %" PRIsz,
135 * check_core_grouping
138 * Look for [...] brackets in *in string and if found copy the
139 * part between brackets into *out string and set grouped to 0.
140 * Otherwise grouped is set to 1 and input is copied without leading
144 * `out' Output string to store result.
145 * `in' Input string to be parsed and copied.
146 * `out_size' Maximum number of elements that out can accommodate.
147 * `grouped' Set by function depending if cores should be grouped or not.
150 * Zero upon success or non-zero if an error occurred.
152 static int check_core_grouping(char *out, const char *in, size_t out_size,
154 const char *start = in;
156 while (isspace(*start))
158 if (start[0] == '[') {
161 end = strchr(start, ']');
163 ERROR(UTIL_NAME ": Missing closing bracket ] in option %s.", in);
166 if ((end - start) >= out_size) {
167 ERROR(UTIL_NAME ": Out buffer is too small.");
170 sstrncpy(out, start, end - start + 1);
171 DEBUG(UTIL_NAME ": Mask for individual (not aggregated) cores: %s", out);
174 sstrncpy(out, start, out_size);
179 int config_cores_parse(const oconfig_item_t *ci, core_groups_list_t *cgl) {
180 if (ci == NULL || cgl == NULL)
182 if (ci->values_num == 0 || ci->values_num > MAX_CORES)
184 core_group_t cgroups[MAX_CORES] = {{0}};
185 size_t cg_idx = 0; /* index for cgroups array */
188 for (int i = 0; i < ci->values_num; i++) {
189 if (ci->values[i].type != OCONFIG_TYPE_STRING) {
190 WARNING(UTIL_NAME ": The %s option requires string arguments.", ci->key);
195 if (ci->values_num == 1 && ci->values[0].value.string &&
196 strlen(ci->values[0].value.string) == 0)
199 for (int i = 0; i < ci->values_num; i++) {
202 char str[DATA_MAX_NAME_LEN];
203 unsigned cores[MAX_CORES] = {0};
205 if (cg_idx >= STATIC_ARRAY_SIZE(cgroups)) {
207 ": Configuration exceeds maximum number of cores: %" PRIsz,
208 STATIC_ARRAY_SIZE(cgroups));
212 if ((ci->values[i].value.string == NULL) ||
213 (strlen(ci->values[i].value.string) == 0)) {
214 ERROR(UTIL_NAME ": Failed to parse parameters for %s option.", ci->key);
219 ret = check_core_grouping(str, ci->values[i].value.string, sizeof(str),
222 ERROR(UTIL_NAME ": Failed to parse config option [%d] %s.", i,
223 ci->values[i].value.string);
226 n = str_list_to_nums(str, cores, STATIC_ARRAY_SIZE(cores));
228 ERROR(UTIL_NAME ": Failed to parse config option [%d] %s.", i,
229 ci->values[i].value.string);
235 cgroups[cg_idx].desc = strdup(ci->values[i].value.string);
236 if (cgroups[cg_idx].desc == NULL) {
237 ERROR(UTIL_NAME ": Failed to allocate description.");
242 cgroups[cg_idx].cores = calloc(n, sizeof(*cgroups[cg_idx].cores));
243 if (cgroups[cg_idx].cores == NULL) {
244 ERROR(UTIL_NAME ": Failed to allocate cores for cgroup.");
249 for (size_t j = 0; j < n; j++)
250 cgroups[cg_idx].cores[j] = cores[j];
252 cgroups[cg_idx].num_cores = n;
255 for (size_t j = 0; j < n && cg_idx < STATIC_ARRAY_SIZE(cgroups); j++) {
256 char desc[DATA_MAX_NAME_LEN];
257 snprintf(desc, sizeof(desc), "%u", cores[j]);
259 cgroups[cg_idx].desc = strdup(desc);
260 if (cgroups[cg_idx].desc == NULL) {
261 ERROR(UTIL_NAME ": Failed to allocate desc for core %u.", cores[j]);
266 cgroups[cg_idx].cores = calloc(1, sizeof(*(cgroups[cg_idx].cores)));
267 if (cgroups[cg_idx].cores == NULL) {
268 ERROR(UTIL_NAME ": Failed to allocate cgroup for core %u.", cores[j]);
272 cgroups[cg_idx].num_cores = 1;
273 cgroups[cg_idx].cores[0] = cores[j];
279 cgl->cgroups = calloc(cg_idx, sizeof(*cgl->cgroups));
280 if (cgl->cgroups == NULL) {
281 ERROR(UTIL_NAME ": Failed to allocate core groups.");
286 cgl->num_cgroups = cg_idx;
287 for (size_t i = 0; i < cg_idx; i++)
288 cgl->cgroups[i] = cgroups[i];
295 while (cg_idx < STATIC_ARRAY_SIZE(cgroups) && cgroups[cg_idx].desc != NULL) {
296 sfree(cgroups[cg_idx].desc);
297 sfree(cgroups[cg_idx].cores);
303 int config_cores_default(int num_cores, core_groups_list_t *cgl) {
304 if (cgl == NULL || num_cores < 0 || num_cores > MAX_CORES)
307 cgl->cgroups = calloc(num_cores, sizeof(*(cgl->cgroups)));
308 if (cgl->cgroups == NULL) {
309 ERROR(UTIL_NAME ": Failed to allocate memory for core groups.");
312 cgl->num_cgroups = num_cores;
314 for (int i = 0; i < num_cores; i++) {
315 char desc[DATA_MAX_NAME_LEN];
316 snprintf(desc, sizeof(desc), "%d", i);
318 cgl->cgroups[i].cores = calloc(1, sizeof(*(cgl->cgroups[i].cores)));
319 if (cgl->cgroups[i].cores == NULL) {
320 ERROR(UTIL_NAME ": Failed to allocate default cores for cgroup %d.", i);
321 config_cores_cleanup(cgl);
324 cgl->cgroups[i].num_cores = 1;
325 cgl->cgroups[i].cores[0] = i;
327 cgl->cgroups[i].desc = strdup(desc);
328 if (cgl->cgroups[i].desc == NULL) {
329 ERROR(UTIL_NAME ": Failed to allocate description for cgroup %d.", i);
330 config_cores_cleanup(cgl);
337 void config_cores_cleanup(core_groups_list_t *cgl) {
340 for (size_t i = 0; i < cgl->num_cgroups; i++) {
341 sfree(cgl->cgroups[i].desc);
342 sfree(cgl->cgroups[i].cores);
345 cgl->num_cgroups = 0;
348 int config_cores_cmp_cgroups(const core_group_t *cg_a,
349 const core_group_t *cg_b) {
352 assert(cg_a != NULL);
353 assert(cg_b != NULL);
355 const size_t sz_a = cg_a->num_cores;
356 const size_t sz_b = cg_b->num_cores;
357 const unsigned *tab_a = cg_a->cores;
358 const unsigned *tab_b = cg_b->cores;
360 for (size_t i = 0; i < sz_a; i++)
361 if (is_in_list(tab_a[i], tab_b, sz_b))
364 /* if no cores are the same */
367 /* if group contains same cores */
368 if (sz_a == sz_b && sz_b == found)
370 /* if not all cores are the same */