1daa76db6f5f9698c01f239f756eda29a13e0c2c
[collectd.git] / src / utils_config_cores.c
1 /**
2  * collectd - src/utils_config_cores.c
3  *
4  * Copyright(c) 2017 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  *   Kamil Wiatrowski <kamilx.wiatrowski@intel.com>
26  **/
27
28 #include "collectd.h"
29
30 #include "common.h"
31
32 #include "utils_config_cores.h"
33
34 #define UTIL_NAME "utils_config_cores"
35
36 #define MAX_SOCKETS 8
37 #define MAX_SOCKET_CORES 64
38 #define MAX_CORES (MAX_SOCKET_CORES * MAX_SOCKETS)
39
40 static inline _Bool is_in_list(unsigned val, const unsigned *list, size_t len) {
41   for (size_t i = 0; i < len; i++)
42     if (list[i] == val)
43       return 1;
44   return 0;
45 }
46
47 static int str_to_uint(const char *s, unsigned *n) {
48   if (s == NULL || n == NULL)
49     return -EINVAL;
50   char *endptr = NULL;
51
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);
55     return -EINVAL;
56   }
57
58   return 0;
59 }
60
61 /*
62  * NAME
63  *   str_list_to_nums
64  *
65  * DESCRIPTION
66  *   Converts string of characters representing list of numbers into array of
67  *   numbers. Allowed formats are:
68  *     0,1,2,3
69  *     0-10,20-18
70  *     1,3,5-8,10,0x10-12
71  *
72  *   Numbers can be in decimal or hexadecimal format.
73  *
74  * PARAMETERS
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.
78  *
79  * RETURN VALUE
80  *    Number of elements placed into nums.
81  */
82 static size_t str_list_to_nums(char *s, unsigned *nums, size_t nums_len) {
83   char *saveptr;
84   char *token;
85   size_t idx = 0;
86
87   while((token = strtok_r(s, ",", &saveptr))) {
88     char *pos;
89     unsigned start, end = 0;
90     s = NULL;
91
92     while (isspace(*token))
93       token++;
94     if (*token == '\0')
95       continue;
96
97     pos = strchr(token, '-');
98     if (pos) {
99       *pos = '\0';
100     }
101
102     if (str_to_uint(token, &start))
103       return 0;
104
105     if (pos) {
106       if (str_to_uint(pos + 1, &end))
107         return 0;
108     } else {
109       end = start;
110     }
111
112     if (start > end) {
113       unsigned swap = start;
114       start = end;
115       end = swap;
116     }
117
118     for (unsigned i = start; i <= end; i++) {
119       if (is_in_list(i, nums, idx))
120         continue;
121       if (idx >= nums_len) {
122         WARNING(UTIL_NAME ": exceeded the cores number limit: %zu", nums_len);
123         return idx;
124       }
125       nums[idx] = i;
126       idx++;
127     }
128   }
129   return idx;
130 }
131
132 /*
133  * NAME
134  *   check_core_grouping
135  *
136  * DESCRIPTION
137  *   Look for [...] brackets in *in string and if found copy the
138  *   part between brackets into *out string and set grouped to 0.
139  *   Otherwise grouped is set to 1 and input is copied without leading
140  *   whitespaces.
141  *
142  * PARAMETERS
143  *   `out'       Output string to store result.
144  *   `in'        Input string to be parsed and copied.
145  *   `out_size'  Maximum number of elements that out can accommodate.
146  *   `grouped'   Set by function depending if cores should be grouped or not.
147  *
148  * RETURN VALUE
149  *    Zero upon success or non-zero if an error occurred.
150  */
151 static int check_core_grouping(char *out, const char *in, size_t out_size,
152                                _Bool *grouped) {
153   const char *start = in;
154   char *end;
155   while (isspace(*start))
156     ++start;
157   if (start[0] == '[') {
158     *grouped = 0;
159     ++start;
160     end = strchr(start, ']');
161     if (end == NULL) {
162       ERROR(UTIL_NAME ": Missing closing bracket ] in option %s.", in);
163       return -EINVAL;
164     }
165     if ((end - start) >= out_size) {
166       ERROR(UTIL_NAME ": Out buffer is too small.");
167       return -EINVAL;
168     }
169     sstrncpy(out, start, end - start + 1);
170     DEBUG(UTIL_NAME ": Mask for individual (not aggregated) cores: %s", out);
171   } else {
172     *grouped = 1;
173     sstrncpy(out, start, out_size);
174   }
175   return 0;
176 }
177
178 int config_cores_parse(const oconfig_item_t *ci, core_groups_list_t *cgl) {
179   if (ci == NULL || cgl == NULL)
180     return -EINVAL;
181   if (ci->values_num == 0 || ci->values_num > MAX_CORES)
182     return -EINVAL;
183   core_group_t cgroups[MAX_CORES] = {{0}};
184   size_t cg_idx = 0; /* index for cgroups array */
185   int ret = 0;
186
187   for (int i = 0; i < ci->values_num; i++) {
188     if (ci->values[i].type != OCONFIG_TYPE_STRING) {
189       WARNING(UTIL_NAME ": The %s option requires string arguments.", ci->key);
190       return -EINVAL;
191     }
192   }
193
194   if (ci->values_num == 1 && ci->values[0].value.string &&
195       strlen(ci->values[0].value.string) == 0)
196     return 0;
197
198   for (int i = 0; i < ci->values_num; i++) {
199     size_t n;
200     _Bool grouped = 1;
201     char str[DATA_MAX_NAME_LEN];
202     unsigned cores[MAX_CORES] = {0};
203
204     if (cg_idx >= STATIC_ARRAY_SIZE(cgroups)) {
205       ERROR(UTIL_NAME ": Configuration exceeds maximum number of cores: %zu",
206             STATIC_ARRAY_SIZE(cgroups));
207       ret = -EINVAL;
208       goto parse_error;
209     }
210     if ((ci->values[i].value.string == NULL) ||
211         (strlen(ci->values[i].value.string) == 0)) {
212       ERROR(UTIL_NAME ": Failed to parse parameters for %s option.", ci->key);
213       ret = -EINVAL;
214       goto parse_error;
215     }
216
217     ret = check_core_grouping(str, ci->values[i].value.string, sizeof(str),
218                               &grouped);
219     if (ret != 0) {
220       ERROR(UTIL_NAME ": Failed to parse config option [%d] %s.", i,
221             ci->values[i].value.string);
222       goto parse_error;
223     }
224     n = str_list_to_nums(str, cores, STATIC_ARRAY_SIZE(cores));
225     if (n == 0) {
226       ERROR(UTIL_NAME ": Failed to parse config option [%d] %s.", i,
227             ci->values[i].value.string);
228       ret = -EINVAL;
229       goto parse_error;
230     }
231
232     if (grouped) {
233       cgroups[cg_idx].desc = strdup(ci->values[i].value.string);
234       if (cgroups[cg_idx].desc == NULL) {
235         ERROR(UTIL_NAME ": Failed to allocate description.");
236         ret = -ENOMEM;
237         goto parse_error;
238       }
239
240       cgroups[cg_idx].cores = calloc(n, sizeof(*cgroups[cg_idx].cores));
241       if (cgroups[cg_idx].cores == NULL) {
242         ERROR(UTIL_NAME ": Failed to allocate cores for cgroup.");
243         ret = -ENOMEM;
244         goto parse_error;
245       }
246
247       for (size_t j = 0; j < n; j++)
248         cgroups[cg_idx].cores[j] = cores[j];
249
250       cgroups[cg_idx].num_cores = n;
251       cg_idx++;
252     } else {
253       for (size_t j = 0; j < n && cg_idx < STATIC_ARRAY_SIZE(cgroups); j++) {
254         char desc[DATA_MAX_NAME_LEN];
255         snprintf(desc, sizeof(desc), "%u", cores[j]);
256
257         cgroups[cg_idx].desc = strdup(desc);
258         if (cgroups[cg_idx].desc == NULL) {
259           ERROR(UTIL_NAME ": Failed to allocate desc for core %u.", cores[j]);
260           ret = -ENOMEM;
261           goto parse_error;
262         }
263
264         cgroups[cg_idx].cores = calloc(1, sizeof(*(cgroups[cg_idx].cores)));
265         if (cgroups[cg_idx].cores == NULL) {
266           ERROR(UTIL_NAME ": Failed to allocate cgroup for core %u.", cores[j]);
267           ret = -ENOMEM;
268           goto parse_error;
269         }
270         cgroups[cg_idx].num_cores = 1;
271         cgroups[cg_idx].cores[0] = cores[j];
272         cg_idx++;
273       }
274     }
275   }
276
277   cgl->cgroups = calloc(cg_idx, sizeof(*cgl->cgroups));
278   if (cgl->cgroups == NULL) {
279     ERROR(UTIL_NAME ": Failed to allocate core groups.");
280     ret = -ENOMEM;
281     goto parse_error;
282   }
283
284   cgl->num_cgroups = cg_idx;
285   for (size_t i = 0; i < cg_idx; i++)
286     cgl->cgroups[i] = cgroups[i];
287
288   return 0;
289
290 parse_error:
291
292   cg_idx = 0;
293   while(cg_idx < STATIC_ARRAY_SIZE(cgroups) && cgroups[cg_idx].desc != NULL) {
294     sfree(cgroups[cg_idx].desc);
295     sfree(cgroups[cg_idx].cores);
296     cg_idx++;
297   }
298   return ret;
299 }
300
301 int config_cores_default(int num_cores, core_groups_list_t *cgl) {
302   if (cgl == NULL || num_cores < 0 || num_cores > MAX_CORES)
303     return -EINVAL;
304
305   cgl->cgroups = calloc(num_cores, sizeof(*(cgl->cgroups)));
306   if (cgl->cgroups == NULL) {
307     ERROR(UTIL_NAME ": Failed to allocate memory for core groups.");
308     return -ENOMEM;
309   }
310   cgl->num_cgroups = num_cores;
311
312   for (int i = 0; i < num_cores; i++) {
313     char desc[DATA_MAX_NAME_LEN];
314     snprintf(desc, sizeof(desc), "%d", i);
315
316     cgl->cgroups[i].cores = calloc(1, sizeof(*(cgl->cgroups[i].cores)));
317     if (cgl->cgroups[i].cores == NULL) {
318       ERROR(UTIL_NAME ": Failed to allocate default cores for cgroup %d.", i);
319       config_cores_cleanup(cgl);
320       return -ENOMEM;
321     }
322     cgl->cgroups[i].num_cores = 1;
323     cgl->cgroups[i].cores[0] = i;
324
325     cgl->cgroups[i].desc = strdup(desc);
326     if (cgl->cgroups[i].desc == NULL) {
327        ERROR(UTIL_NAME ": Failed to allocate description for cgroup %d.", i);
328        config_cores_cleanup(cgl);
329        return -ENOMEM;
330     }
331   }
332   return 0;
333 }
334
335 void config_cores_cleanup(core_groups_list_t *cgl) {
336   if (cgl == NULL)
337     return;
338   for (size_t i = 0; i < cgl->num_cgroups; i++) {
339     sfree(cgl->cgroups[i].desc);
340     sfree(cgl->cgroups[i].cores);
341   }
342   sfree(cgl->cgroups);
343   cgl->num_cgroups = 0;
344 }
345
346 int config_cores_cmp_cgroups(const core_group_t *cg_a,
347                              const core_group_t *cg_b) {
348   size_t found = 0;
349
350   assert(cg_a != NULL);
351   assert(cg_b != NULL);
352
353   const size_t sz_a = cg_a->num_cores;
354   const size_t sz_b = cg_b->num_cores;
355   const unsigned *tab_a = cg_a->cores;
356   const unsigned *tab_b = cg_b->cores;
357
358   for (size_t i = 0; i < sz_a; i++)
359     if (is_in_list(tab_a[i], tab_b, sz_b))
360       found++;
361
362   /* if no cores are the same */
363   if (!found)
364     return 0;
365   /* if group contains same cores */
366   if (sz_a == sz_b && sz_b == found)
367     return 1;
368   /* if not all cores are the same */
369   return -1;
370 }
371