xref: /openbmc/linux/tools/perf/util/cpumap.c (revision a2cce7a9)
1 #include "util.h"
2 #include <api/fs/fs.h>
3 #include "../perf.h"
4 #include "cpumap.h"
5 #include <assert.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include "asm/bug.h"
9 
10 static struct cpu_map *cpu_map__default_new(void)
11 {
12 	struct cpu_map *cpus;
13 	int nr_cpus;
14 
15 	nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
16 	if (nr_cpus < 0)
17 		return NULL;
18 
19 	cpus = malloc(sizeof(*cpus) + nr_cpus * sizeof(int));
20 	if (cpus != NULL) {
21 		int i;
22 		for (i = 0; i < nr_cpus; ++i)
23 			cpus->map[i] = i;
24 
25 		cpus->nr = nr_cpus;
26 		atomic_set(&cpus->refcnt, 1);
27 	}
28 
29 	return cpus;
30 }
31 
32 static struct cpu_map *cpu_map__trim_new(int nr_cpus, int *tmp_cpus)
33 {
34 	size_t payload_size = nr_cpus * sizeof(int);
35 	struct cpu_map *cpus = malloc(sizeof(*cpus) + payload_size);
36 
37 	if (cpus != NULL) {
38 		cpus->nr = nr_cpus;
39 		memcpy(cpus->map, tmp_cpus, payload_size);
40 		atomic_set(&cpus->refcnt, 1);
41 	}
42 
43 	return cpus;
44 }
45 
46 struct cpu_map *cpu_map__read(FILE *file)
47 {
48 	struct cpu_map *cpus = NULL;
49 	int nr_cpus = 0;
50 	int *tmp_cpus = NULL, *tmp;
51 	int max_entries = 0;
52 	int n, cpu, prev;
53 	char sep;
54 
55 	sep = 0;
56 	prev = -1;
57 	for (;;) {
58 		n = fscanf(file, "%u%c", &cpu, &sep);
59 		if (n <= 0)
60 			break;
61 		if (prev >= 0) {
62 			int new_max = nr_cpus + cpu - prev - 1;
63 
64 			if (new_max >= max_entries) {
65 				max_entries = new_max + MAX_NR_CPUS / 2;
66 				tmp = realloc(tmp_cpus, max_entries * sizeof(int));
67 				if (tmp == NULL)
68 					goto out_free_tmp;
69 				tmp_cpus = tmp;
70 			}
71 
72 			while (++prev < cpu)
73 				tmp_cpus[nr_cpus++] = prev;
74 		}
75 		if (nr_cpus == max_entries) {
76 			max_entries += MAX_NR_CPUS;
77 			tmp = realloc(tmp_cpus, max_entries * sizeof(int));
78 			if (tmp == NULL)
79 				goto out_free_tmp;
80 			tmp_cpus = tmp;
81 		}
82 
83 		tmp_cpus[nr_cpus++] = cpu;
84 		if (n == 2 && sep == '-')
85 			prev = cpu;
86 		else
87 			prev = -1;
88 		if (n == 1 || sep == '\n')
89 			break;
90 	}
91 
92 	if (nr_cpus > 0)
93 		cpus = cpu_map__trim_new(nr_cpus, tmp_cpus);
94 	else
95 		cpus = cpu_map__default_new();
96 out_free_tmp:
97 	free(tmp_cpus);
98 	return cpus;
99 }
100 
101 static struct cpu_map *cpu_map__read_all_cpu_map(void)
102 {
103 	struct cpu_map *cpus = NULL;
104 	FILE *onlnf;
105 
106 	onlnf = fopen("/sys/devices/system/cpu/online", "r");
107 	if (!onlnf)
108 		return cpu_map__default_new();
109 
110 	cpus = cpu_map__read(onlnf);
111 	fclose(onlnf);
112 	return cpus;
113 }
114 
115 struct cpu_map *cpu_map__new(const char *cpu_list)
116 {
117 	struct cpu_map *cpus = NULL;
118 	unsigned long start_cpu, end_cpu = 0;
119 	char *p = NULL;
120 	int i, nr_cpus = 0;
121 	int *tmp_cpus = NULL, *tmp;
122 	int max_entries = 0;
123 
124 	if (!cpu_list)
125 		return cpu_map__read_all_cpu_map();
126 
127 	if (!isdigit(*cpu_list))
128 		goto out;
129 
130 	while (isdigit(*cpu_list)) {
131 		p = NULL;
132 		start_cpu = strtoul(cpu_list, &p, 0);
133 		if (start_cpu >= INT_MAX
134 		    || (*p != '\0' && *p != ',' && *p != '-'))
135 			goto invalid;
136 
137 		if (*p == '-') {
138 			cpu_list = ++p;
139 			p = NULL;
140 			end_cpu = strtoul(cpu_list, &p, 0);
141 
142 			if (end_cpu >= INT_MAX || (*p != '\0' && *p != ','))
143 				goto invalid;
144 
145 			if (end_cpu < start_cpu)
146 				goto invalid;
147 		} else {
148 			end_cpu = start_cpu;
149 		}
150 
151 		for (; start_cpu <= end_cpu; start_cpu++) {
152 			/* check for duplicates */
153 			for (i = 0; i < nr_cpus; i++)
154 				if (tmp_cpus[i] == (int)start_cpu)
155 					goto invalid;
156 
157 			if (nr_cpus == max_entries) {
158 				max_entries += MAX_NR_CPUS;
159 				tmp = realloc(tmp_cpus, max_entries * sizeof(int));
160 				if (tmp == NULL)
161 					goto invalid;
162 				tmp_cpus = tmp;
163 			}
164 			tmp_cpus[nr_cpus++] = (int)start_cpu;
165 		}
166 		if (*p)
167 			++p;
168 
169 		cpu_list = p;
170 	}
171 
172 	if (nr_cpus > 0)
173 		cpus = cpu_map__trim_new(nr_cpus, tmp_cpus);
174 	else
175 		cpus = cpu_map__default_new();
176 invalid:
177 	free(tmp_cpus);
178 out:
179 	return cpus;
180 }
181 
182 size_t cpu_map__fprintf(struct cpu_map *map, FILE *fp)
183 {
184 	int i;
185 	size_t printed = fprintf(fp, "%d cpu%s: ",
186 				 map->nr, map->nr > 1 ? "s" : "");
187 	for (i = 0; i < map->nr; ++i)
188 		printed += fprintf(fp, "%s%d", i ? ", " : "", map->map[i]);
189 
190 	return printed + fprintf(fp, "\n");
191 }
192 
193 struct cpu_map *cpu_map__dummy_new(void)
194 {
195 	struct cpu_map *cpus = malloc(sizeof(*cpus) + sizeof(int));
196 
197 	if (cpus != NULL) {
198 		cpus->nr = 1;
199 		cpus->map[0] = -1;
200 		atomic_set(&cpus->refcnt, 1);
201 	}
202 
203 	return cpus;
204 }
205 
206 static void cpu_map__delete(struct cpu_map *map)
207 {
208 	if (map) {
209 		WARN_ONCE(atomic_read(&map->refcnt) != 0,
210 			  "cpu_map refcnt unbalanced\n");
211 		free(map);
212 	}
213 }
214 
215 struct cpu_map *cpu_map__get(struct cpu_map *map)
216 {
217 	if (map)
218 		atomic_inc(&map->refcnt);
219 	return map;
220 }
221 
222 void cpu_map__put(struct cpu_map *map)
223 {
224 	if (map && atomic_dec_and_test(&map->refcnt))
225 		cpu_map__delete(map);
226 }
227 
228 int cpu_map__get_socket(struct cpu_map *map, int idx)
229 {
230 	FILE *fp;
231 	const char *mnt;
232 	char path[PATH_MAX];
233 	int cpu, ret;
234 
235 	if (idx > map->nr)
236 		return -1;
237 
238 	cpu = map->map[idx];
239 
240 	mnt = sysfs__mountpoint();
241 	if (!mnt)
242 		return -1;
243 
244 	snprintf(path, PATH_MAX,
245 		"%s/devices/system/cpu/cpu%d/topology/physical_package_id",
246 		mnt, cpu);
247 
248 	fp = fopen(path, "r");
249 	if (!fp)
250 		return -1;
251 	ret = fscanf(fp, "%d", &cpu);
252 	fclose(fp);
253 	return ret == 1 ? cpu : -1;
254 }
255 
256 static int cmp_ids(const void *a, const void *b)
257 {
258 	return *(int *)a - *(int *)b;
259 }
260 
261 static int cpu_map__build_map(struct cpu_map *cpus, struct cpu_map **res,
262 			      int (*f)(struct cpu_map *map, int cpu))
263 {
264 	struct cpu_map *c;
265 	int nr = cpus->nr;
266 	int cpu, s1, s2;
267 
268 	/* allocate as much as possible */
269 	c = calloc(1, sizeof(*c) + nr * sizeof(int));
270 	if (!c)
271 		return -1;
272 
273 	for (cpu = 0; cpu < nr; cpu++) {
274 		s1 = f(cpus, cpu);
275 		for (s2 = 0; s2 < c->nr; s2++) {
276 			if (s1 == c->map[s2])
277 				break;
278 		}
279 		if (s2 == c->nr) {
280 			c->map[c->nr] = s1;
281 			c->nr++;
282 		}
283 	}
284 	/* ensure we process id in increasing order */
285 	qsort(c->map, c->nr, sizeof(int), cmp_ids);
286 
287 	atomic_set(&cpus->refcnt, 1);
288 	*res = c;
289 	return 0;
290 }
291 
292 int cpu_map__get_core(struct cpu_map *map, int idx)
293 {
294 	FILE *fp;
295 	const char *mnt;
296 	char path[PATH_MAX];
297 	int cpu, ret, s;
298 
299 	if (idx > map->nr)
300 		return -1;
301 
302 	cpu = map->map[idx];
303 
304 	mnt = sysfs__mountpoint();
305 	if (!mnt)
306 		return -1;
307 
308 	snprintf(path, PATH_MAX,
309 		"%s/devices/system/cpu/cpu%d/topology/core_id",
310 		mnt, cpu);
311 
312 	fp = fopen(path, "r");
313 	if (!fp)
314 		return -1;
315 	ret = fscanf(fp, "%d", &cpu);
316 	fclose(fp);
317 	if (ret != 1)
318 		return -1;
319 
320 	s = cpu_map__get_socket(map, idx);
321 	if (s == -1)
322 		return -1;
323 
324 	/*
325 	 * encode socket in upper 16 bits
326 	 * core_id is relative to socket, and
327 	 * we need a global id. So we combine
328 	 * socket+ core id
329 	 */
330 	return (s << 16) | (cpu & 0xffff);
331 }
332 
333 int cpu_map__build_socket_map(struct cpu_map *cpus, struct cpu_map **sockp)
334 {
335 	return cpu_map__build_map(cpus, sockp, cpu_map__get_socket);
336 }
337 
338 int cpu_map__build_core_map(struct cpu_map *cpus, struct cpu_map **corep)
339 {
340 	return cpu_map__build_map(cpus, corep, cpu_map__get_core);
341 }
342 
343 /* setup simple routines to easily access node numbers given a cpu number */
344 static int get_max_num(char *path, int *max)
345 {
346 	size_t num;
347 	char *buf;
348 	int err = 0;
349 
350 	if (filename__read_str(path, &buf, &num))
351 		return -1;
352 
353 	buf[num] = '\0';
354 
355 	/* start on the right, to find highest node num */
356 	while (--num) {
357 		if ((buf[num] == ',') || (buf[num] == '-')) {
358 			num++;
359 			break;
360 		}
361 	}
362 	if (sscanf(&buf[num], "%d", max) < 1) {
363 		err = -1;
364 		goto out;
365 	}
366 
367 	/* convert from 0-based to 1-based */
368 	(*max)++;
369 
370 out:
371 	free(buf);
372 	return err;
373 }
374 
375 /* Determine highest possible cpu in the system for sparse allocation */
376 static void set_max_cpu_num(void)
377 {
378 	const char *mnt;
379 	char path[PATH_MAX];
380 	int ret = -1;
381 
382 	/* set up default */
383 	max_cpu_num = 4096;
384 
385 	mnt = sysfs__mountpoint();
386 	if (!mnt)
387 		goto out;
388 
389 	/* get the highest possible cpu number for a sparse allocation */
390 	ret = snprintf(path, PATH_MAX, "%s/devices/system/cpu/possible", mnt);
391 	if (ret == PATH_MAX) {
392 		pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
393 		goto out;
394 	}
395 
396 	ret = get_max_num(path, &max_cpu_num);
397 
398 out:
399 	if (ret)
400 		pr_err("Failed to read max cpus, using default of %d\n", max_cpu_num);
401 }
402 
403 /* Determine highest possible node in the system for sparse allocation */
404 static void set_max_node_num(void)
405 {
406 	const char *mnt;
407 	char path[PATH_MAX];
408 	int ret = -1;
409 
410 	/* set up default */
411 	max_node_num = 8;
412 
413 	mnt = sysfs__mountpoint();
414 	if (!mnt)
415 		goto out;
416 
417 	/* get the highest possible cpu number for a sparse allocation */
418 	ret = snprintf(path, PATH_MAX, "%s/devices/system/node/possible", mnt);
419 	if (ret == PATH_MAX) {
420 		pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
421 		goto out;
422 	}
423 
424 	ret = get_max_num(path, &max_node_num);
425 
426 out:
427 	if (ret)
428 		pr_err("Failed to read max nodes, using default of %d\n", max_node_num);
429 }
430 
431 static int init_cpunode_map(void)
432 {
433 	int i;
434 
435 	set_max_cpu_num();
436 	set_max_node_num();
437 
438 	cpunode_map = calloc(max_cpu_num, sizeof(int));
439 	if (!cpunode_map) {
440 		pr_err("%s: calloc failed\n", __func__);
441 		return -1;
442 	}
443 
444 	for (i = 0; i < max_cpu_num; i++)
445 		cpunode_map[i] = -1;
446 
447 	return 0;
448 }
449 
450 int cpu__setup_cpunode_map(void)
451 {
452 	struct dirent *dent1, *dent2;
453 	DIR *dir1, *dir2;
454 	unsigned int cpu, mem;
455 	char buf[PATH_MAX];
456 	char path[PATH_MAX];
457 	const char *mnt;
458 	int n;
459 
460 	/* initialize globals */
461 	if (init_cpunode_map())
462 		return -1;
463 
464 	mnt = sysfs__mountpoint();
465 	if (!mnt)
466 		return 0;
467 
468 	n = snprintf(path, PATH_MAX, "%s/devices/system/node", mnt);
469 	if (n == PATH_MAX) {
470 		pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
471 		return -1;
472 	}
473 
474 	dir1 = opendir(path);
475 	if (!dir1)
476 		return 0;
477 
478 	/* walk tree and setup map */
479 	while ((dent1 = readdir(dir1)) != NULL) {
480 		if (dent1->d_type != DT_DIR || sscanf(dent1->d_name, "node%u", &mem) < 1)
481 			continue;
482 
483 		n = snprintf(buf, PATH_MAX, "%s/%s", path, dent1->d_name);
484 		if (n == PATH_MAX) {
485 			pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
486 			continue;
487 		}
488 
489 		dir2 = opendir(buf);
490 		if (!dir2)
491 			continue;
492 		while ((dent2 = readdir(dir2)) != NULL) {
493 			if (dent2->d_type != DT_LNK || sscanf(dent2->d_name, "cpu%u", &cpu) < 1)
494 				continue;
495 			cpunode_map[cpu] = mem;
496 		}
497 		closedir(dir2);
498 	}
499 	closedir(dir1);
500 	return 0;
501 }
502