1 // SPDX-License-Identifier: GPL-2.0
10 #include <linux/bitmap.h>
13 #include <linux/ctype.h>
14 #include <linux/zalloc.h>
16 static struct perf_cpu max_cpu_num;
17 static struct perf_cpu max_present_cpu_num;
18 static int max_node_num;
20 * The numa node X as read from /sys/devices/system/node/nodeX indexed by the
23 static int *cpunode_map;
25 static struct perf_cpu_map *cpu_map__from_entries(struct cpu_map_entries *cpus)
27 struct perf_cpu_map *map;
29 map = perf_cpu_map__empty_new(cpus->nr);
33 for (i = 0; i < cpus->nr; i++) {
35 * Special treatment for -1, which is not real cpu number,
36 * and we need to use (int) -1 to initialize map[i],
37 * otherwise it would become 65535.
39 if (cpus->cpu[i] == (u16) -1)
42 map->map[i].cpu = (int) cpus->cpu[i];
49 static struct perf_cpu_map *cpu_map__from_mask(struct perf_record_record_cpu_map *mask)
51 struct perf_cpu_map *map;
52 int nr, nbits = mask->nr * mask->long_size * BITS_PER_BYTE;
54 nr = bitmap_weight(mask->mask, nbits);
56 map = perf_cpu_map__empty_new(nr);
60 for_each_set_bit(cpu, mask->mask, nbits)
61 map->map[i++].cpu = cpu;
67 struct perf_cpu_map *cpu_map__new_data(struct perf_record_cpu_map_data *data)
69 if (data->type == PERF_CPU_MAP__CPUS)
70 return cpu_map__from_entries((struct cpu_map_entries *)data->data);
72 return cpu_map__from_mask((struct perf_record_record_cpu_map *)data->data);
75 size_t cpu_map__fprintf(struct perf_cpu_map *map, FILE *fp)
80 cpu_map__snprint(map, buf, sizeof(buf));
81 return fprintf(fp, "%s\n", buf);
85 struct perf_cpu_map *perf_cpu_map__empty_new(int nr)
87 struct perf_cpu_map *cpus = malloc(sizeof(*cpus) + sizeof(int) * nr);
93 for (i = 0; i < nr; i++)
94 cpus->map[i].cpu = -1;
96 refcount_set(&cpus->refcnt, 1);
102 struct cpu_aggr_map *cpu_aggr_map__empty_new(int nr)
104 struct cpu_aggr_map *cpus = malloc(sizeof(*cpus) + sizeof(struct aggr_cpu_id) * nr);
110 for (i = 0; i < nr; i++)
111 cpus->map[i] = aggr_cpu_id__empty();
113 refcount_set(&cpus->refcnt, 1);
119 static int cpu__get_topology_int(int cpu, const char *name, int *value)
123 snprintf(path, PATH_MAX,
124 "devices/system/cpu/cpu%d/topology/%s", cpu, name);
126 return sysfs__read_int(path, value);
129 int cpu__get_socket_id(struct perf_cpu cpu)
131 int value, ret = cpu__get_topology_int(cpu.cpu, "physical_package_id", &value);
135 struct aggr_cpu_id aggr_cpu_id__socket(struct perf_cpu cpu, void *data __maybe_unused)
137 struct aggr_cpu_id id = aggr_cpu_id__empty();
139 id.socket = cpu__get_socket_id(cpu);
143 static int aggr_cpu_id__cmp(const void *a_pointer, const void *b_pointer)
145 struct aggr_cpu_id *a = (struct aggr_cpu_id *)a_pointer;
146 struct aggr_cpu_id *b = (struct aggr_cpu_id *)b_pointer;
148 if (a->node != b->node)
149 return a->node - b->node;
150 else if (a->socket != b->socket)
151 return a->socket - b->socket;
152 else if (a->die != b->die)
153 return a->die - b->die;
154 else if (a->core != b->core)
155 return a->core - b->core;
157 return a->thread - b->thread;
160 struct cpu_aggr_map *cpu_aggr_map__new(const struct perf_cpu_map *cpus,
161 aggr_cpu_id_get_t get_id,
166 struct cpu_aggr_map *c = cpu_aggr_map__empty_new(cpus->nr);
171 /* Reset size as it may only be partially filled */
174 perf_cpu_map__for_each_cpu(cpu, idx, cpus) {
175 bool duplicate = false;
176 struct aggr_cpu_id cpu_id = get_id(cpu, data);
178 for (int j = 0; j < c->nr; j++) {
179 if (aggr_cpu_id__equal(&cpu_id, &c->map[j])) {
185 c->map[c->nr] = cpu_id;
190 if (c->nr != cpus->nr) {
191 struct cpu_aggr_map *trimmed_c =
193 sizeof(struct cpu_aggr_map) + sizeof(struct aggr_cpu_id) * c->nr);
198 /* ensure we process id in increasing order */
199 qsort(c->map, c->nr, sizeof(struct aggr_cpu_id), aggr_cpu_id__cmp);
205 int cpu__get_die_id(struct perf_cpu cpu)
207 int value, ret = cpu__get_topology_int(cpu.cpu, "die_id", &value);
212 struct aggr_cpu_id aggr_cpu_id__die(struct perf_cpu cpu, void *data)
214 struct aggr_cpu_id id;
217 die = cpu__get_die_id(cpu);
218 /* There is no die_id on legacy system. */
223 * die_id is relative to socket, so start
224 * with the socket ID and then add die to
227 id = aggr_cpu_id__socket(cpu, data);
228 if (aggr_cpu_id__is_empty(&id))
235 int cpu__get_core_id(struct perf_cpu cpu)
237 int value, ret = cpu__get_topology_int(cpu.cpu, "core_id", &value);
241 struct aggr_cpu_id aggr_cpu_id__core(struct perf_cpu cpu, void *data)
243 struct aggr_cpu_id id;
244 int core = cpu__get_core_id(cpu);
246 /* aggr_cpu_id__die returns a struct with socket and die set. */
247 id = aggr_cpu_id__die(cpu, data);
248 if (aggr_cpu_id__is_empty(&id))
252 * core_id is relative to socket and die, we need a global id.
253 * So we combine the result from cpu_map__get_die with the core id
260 struct aggr_cpu_id aggr_cpu_id__cpu(struct perf_cpu cpu, void *data)
262 struct aggr_cpu_id id;
264 /* aggr_cpu_id__core returns a struct with socket, die and core set. */
265 id = aggr_cpu_id__core(cpu, data);
266 if (aggr_cpu_id__is_empty(&id))
274 struct aggr_cpu_id aggr_cpu_id__node(struct perf_cpu cpu, void *data __maybe_unused)
276 struct aggr_cpu_id id = aggr_cpu_id__empty();
278 id.node = cpu__get_node(cpu);
282 /* setup simple routines to easily access node numbers given a cpu number */
283 static int get_max_num(char *path, int *max)
289 if (filename__read_str(path, &buf, &num))
294 /* start on the right, to find highest node num */
296 if ((buf[num] == ',') || (buf[num] == '-')) {
301 if (sscanf(&buf[num], "%d", max) < 1) {
306 /* convert from 0-based to 1-based */
314 /* Determine highest possible cpu in the system for sparse allocation */
315 static void set_max_cpu_num(void)
322 max_cpu_num.cpu = 4096;
323 max_present_cpu_num.cpu = 4096;
325 mnt = sysfs__mountpoint();
329 /* get the highest possible cpu number for a sparse allocation */
330 ret = snprintf(path, PATH_MAX, "%s/devices/system/cpu/possible", mnt);
331 if (ret >= PATH_MAX) {
332 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
336 ret = get_max_num(path, &max_cpu_num.cpu);
340 /* get the highest present cpu number for a sparse allocation */
341 ret = snprintf(path, PATH_MAX, "%s/devices/system/cpu/present", mnt);
342 if (ret >= PATH_MAX) {
343 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
347 ret = get_max_num(path, &max_present_cpu_num.cpu);
351 pr_err("Failed to read max cpus, using default of %d\n", max_cpu_num.cpu);
354 /* Determine highest possible node in the system for sparse allocation */
355 static void set_max_node_num(void)
364 mnt = sysfs__mountpoint();
368 /* get the highest possible cpu number for a sparse allocation */
369 ret = snprintf(path, PATH_MAX, "%s/devices/system/node/possible", mnt);
370 if (ret >= PATH_MAX) {
371 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
375 ret = get_max_num(path, &max_node_num);
379 pr_err("Failed to read max nodes, using default of %d\n", max_node_num);
382 int cpu__max_node(void)
384 if (unlikely(!max_node_num))
390 struct perf_cpu cpu__max_cpu(void)
392 if (unlikely(!max_cpu_num.cpu))
398 struct perf_cpu cpu__max_present_cpu(void)
400 if (unlikely(!max_present_cpu_num.cpu))
403 return max_present_cpu_num;
407 int cpu__get_node(struct perf_cpu cpu)
409 if (unlikely(cpunode_map == NULL)) {
410 pr_debug("cpu_map not initialized\n");
414 return cpunode_map[cpu.cpu];
417 static int init_cpunode_map(void)
424 cpunode_map = calloc(max_cpu_num.cpu, sizeof(int));
426 pr_err("%s: calloc failed\n", __func__);
430 for (i = 0; i < max_cpu_num.cpu; i++)
436 int cpu__setup_cpunode_map(void)
438 struct dirent *dent1, *dent2;
440 unsigned int cpu, mem;
446 /* initialize globals */
447 if (init_cpunode_map())
450 mnt = sysfs__mountpoint();
454 n = snprintf(path, PATH_MAX, "%s/devices/system/node", mnt);
456 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
460 dir1 = opendir(path);
464 /* walk tree and setup map */
465 while ((dent1 = readdir(dir1)) != NULL) {
466 if (dent1->d_type != DT_DIR || sscanf(dent1->d_name, "node%u", &mem) < 1)
469 n = snprintf(buf, PATH_MAX, "%s/%s", path, dent1->d_name);
471 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
478 while ((dent2 = readdir(dir2)) != NULL) {
479 if (dent2->d_type != DT_LNK || sscanf(dent2->d_name, "cpu%u", &cpu) < 1)
481 cpunode_map[cpu] = mem;
489 size_t cpu_map__snprint(struct perf_cpu_map *map, char *buf, size_t size)
495 #define COMMA first ? "" : ","
497 for (i = 0; i < map->nr + 1; i++) {
498 struct perf_cpu cpu = { .cpu = INT_MAX };
499 bool last = i == map->nr;
507 ret += snprintf(buf + ret, size - ret,
511 } else if (((i - start) != (cpu.cpu - map->map[start].cpu)) || last) {
515 ret += snprintf(buf + ret, size - ret,
517 map->map[start].cpu);
519 ret += snprintf(buf + ret, size - ret,
521 map->map[start].cpu, map->map[end].cpu);
530 pr_debug2("cpumask list: %s\n", buf);
534 static char hex_char(unsigned char val)
539 return val - 10 + 'a';
543 size_t cpu_map__snprint_mask(struct perf_cpu_map *map, char *buf, size_t size)
547 unsigned char *bitmap;
548 struct perf_cpu last_cpu = perf_cpu_map__cpu(map, map->nr - 1);
553 bitmap = zalloc(last_cpu.cpu / 8 + 1);
554 if (bitmap == NULL) {
559 for (i = 0; i < map->nr; i++) {
560 cpu = perf_cpu_map__cpu(map, i).cpu;
561 bitmap[cpu / 8] |= 1 << (cpu % 8);
564 for (cpu = last_cpu.cpu / 4 * 4; cpu >= 0; cpu -= 4) {
565 unsigned char bits = bitmap[cpu / 8];
572 *ptr++ = hex_char(bits);
573 if ((cpu % 32) == 0 && cpu > 0)
579 buf[size - 1] = '\0';
583 const struct perf_cpu_map *cpu_map__online(void) /* thread unsafe */
585 static const struct perf_cpu_map *online = NULL;
588 online = perf_cpu_map__new(NULL); /* from /sys/devices/system/cpu/online */
593 bool aggr_cpu_id__equal(const struct aggr_cpu_id *a, const struct aggr_cpu_id *b)
595 return a->thread == b->thread &&
596 a->node == b->node &&
597 a->socket == b->socket &&
599 a->core == b->core &&
600 a->cpu.cpu == b->cpu.cpu;
603 bool aggr_cpu_id__is_empty(const struct aggr_cpu_id *a)
605 return a->thread == -1 &&
613 struct aggr_cpu_id aggr_cpu_id__empty(void)
615 struct aggr_cpu_id ret = {
621 .cpu = (struct perf_cpu){ .cpu = -1 },