^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include <api/fs/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include "cpumap.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include "debug.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include "event.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <assert.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <dirent.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/bitmap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include "asm/bug.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/ctype.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/zalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) static int max_cpu_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) static int max_present_cpu_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) static int max_node_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) static int *cpunode_map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) static struct perf_cpu_map *cpu_map__from_entries(struct cpu_map_entries *cpus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) struct perf_cpu_map *map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) map = perf_cpu_map__empty_new(cpus->nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) if (map) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) unsigned i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) for (i = 0; i < cpus->nr; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * Special treatment for -1, which is not real cpu number,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * and we need to use (int) -1 to initialize map[i],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * otherwise it would become 65535.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) if (cpus->cpu[i] == (u16) -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) map->map[i] = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) map->map[i] = (int) cpus->cpu[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) return map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) static struct perf_cpu_map *cpu_map__from_mask(struct perf_record_record_cpu_map *mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct perf_cpu_map *map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) int nr, nbits = mask->nr * mask->long_size * BITS_PER_BYTE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) nr = bitmap_weight(mask->mask, nbits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) map = perf_cpu_map__empty_new(nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) if (map) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) int cpu, i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) for_each_set_bit(cpu, mask->mask, nbits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) map->map[i++] = cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) return map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) struct perf_cpu_map *cpu_map__new_data(struct perf_record_cpu_map_data *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) if (data->type == PERF_CPU_MAP__CPUS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) return cpu_map__from_entries((struct cpu_map_entries *)data->data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) return cpu_map__from_mask((struct perf_record_record_cpu_map *)data->data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) size_t cpu_map__fprintf(struct perf_cpu_map *map, FILE *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) #define BUFSIZE 1024
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) char buf[BUFSIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) cpu_map__snprint(map, buf, sizeof(buf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) return fprintf(fp, "%s\n", buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) #undef BUFSIZE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) struct perf_cpu_map *perf_cpu_map__empty_new(int nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) struct perf_cpu_map *cpus = malloc(sizeof(*cpus) + sizeof(int) * nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) if (cpus != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) cpus->nr = nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) for (i = 0; i < nr; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) cpus->map[i] = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) refcount_set(&cpus->refcnt, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) return cpus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) static int cpu__get_topology_int(int cpu, const char *name, int *value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) char path[PATH_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) snprintf(path, PATH_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) "devices/system/cpu/cpu%d/topology/%s", cpu, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) return sysfs__read_int(path, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) int cpu_map__get_socket_id(int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) int value, ret = cpu__get_topology_int(cpu, "physical_package_id", &value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) return ret ?: value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) int cpu_map__get_socket(struct perf_cpu_map *map, int idx, void *data __maybe_unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) if (idx > map->nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) cpu = map->map[idx];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) return cpu_map__get_socket_id(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) static int cmp_ids(const void *a, const void *b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) return *(int *)a - *(int *)b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) int cpu_map__build_map(struct perf_cpu_map *cpus, struct perf_cpu_map **res,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) int (*f)(struct perf_cpu_map *map, int cpu, void *data),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) struct perf_cpu_map *c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) int nr = cpus->nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) int cpu, s1, s2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) /* allocate as much as possible */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) c = calloc(1, sizeof(*c) + nr * sizeof(int));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) if (!c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) for (cpu = 0; cpu < nr; cpu++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) s1 = f(cpus, cpu, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) for (s2 = 0; s2 < c->nr; s2++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) if (s1 == c->map[s2])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) if (s2 == c->nr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) c->map[c->nr] = s1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) c->nr++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) /* ensure we process id in increasing order */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) qsort(c->map, c->nr, sizeof(int), cmp_ids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) refcount_set(&c->refcnt, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) *res = c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) int cpu_map__get_die_id(int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) int value, ret = cpu__get_topology_int(cpu, "die_id", &value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) return ret ?: value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) int cpu_map__get_die(struct perf_cpu_map *map, int idx, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) int cpu, die_id, s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) if (idx > map->nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) cpu = map->map[idx];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) die_id = cpu_map__get_die_id(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) /* There is no die_id on legacy system. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) if (die_id == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) die_id = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) s = cpu_map__get_socket(map, idx, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) if (s == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) * Encode socket in bit range 15:8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) * die_id is relative to socket, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) * we need a global id. So we combine
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) * socket + die id
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) if (WARN_ONCE(die_id >> 8, "The die id number is too big.\n"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) if (WARN_ONCE(s >> 8, "The socket id number is too big.\n"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) return (s << 8) | (die_id & 0xff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) int cpu_map__get_core_id(int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) int value, ret = cpu__get_topology_int(cpu, "core_id", &value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) return ret ?: value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) int cpu_map__get_node_id(int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) return cpu__get_node(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) int cpu_map__get_core(struct perf_cpu_map *map, int idx, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) int cpu, s_die;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) if (idx > map->nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) cpu = map->map[idx];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) cpu = cpu_map__get_core_id(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) /* s_die is the combination of socket + die id */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) s_die = cpu_map__get_die(map, idx, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) if (s_die == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) * encode socket in bit range 31:24
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) * encode die id in bit range 23:16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) * core_id is relative to socket and die,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) * we need a global id. So we combine
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) * socket + die id + core id
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) if (WARN_ONCE(cpu >> 16, "The core id number is too big.\n"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) return (s_die << 16) | (cpu & 0xffff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) int cpu_map__get_node(struct perf_cpu_map *map, int idx, void *data __maybe_unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) if (idx < 0 || idx >= map->nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) return cpu_map__get_node_id(map->map[idx]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) int cpu_map__build_socket_map(struct perf_cpu_map *cpus, struct perf_cpu_map **sockp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) return cpu_map__build_map(cpus, sockp, cpu_map__get_socket, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) int cpu_map__build_die_map(struct perf_cpu_map *cpus, struct perf_cpu_map **diep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) return cpu_map__build_map(cpus, diep, cpu_map__get_die, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) int cpu_map__build_core_map(struct perf_cpu_map *cpus, struct perf_cpu_map **corep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) return cpu_map__build_map(cpus, corep, cpu_map__get_core, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) int cpu_map__build_node_map(struct perf_cpu_map *cpus, struct perf_cpu_map **numap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) return cpu_map__build_map(cpus, numap, cpu_map__get_node, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) /* setup simple routines to easily access node numbers given a cpu number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) static int get_max_num(char *path, int *max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) size_t num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) char *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) if (filename__read_str(path, &buf, &num))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) buf[num] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) /* start on the right, to find highest node num */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) while (--num) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) if ((buf[num] == ',') || (buf[num] == '-')) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) num++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) if (sscanf(&buf[num], "%d", max) < 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) err = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) /* convert from 0-based to 1-based */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) (*max)++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) free(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) /* Determine highest possible cpu in the system for sparse allocation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) static void set_max_cpu_num(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) const char *mnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) char path[PATH_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) int ret = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) /* set up default */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) max_cpu_num = 4096;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) max_present_cpu_num = 4096;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) mnt = sysfs__mountpoint();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) if (!mnt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) /* get the highest possible cpu number for a sparse allocation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) ret = snprintf(path, PATH_MAX, "%s/devices/system/cpu/possible", mnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) if (ret >= PATH_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) ret = get_max_num(path, &max_cpu_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) /* get the highest present cpu number for a sparse allocation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) ret = snprintf(path, PATH_MAX, "%s/devices/system/cpu/present", mnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) if (ret >= PATH_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) ret = get_max_num(path, &max_present_cpu_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) pr_err("Failed to read max cpus, using default of %d\n", max_cpu_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) /* Determine highest possible node in the system for sparse allocation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) static void set_max_node_num(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) const char *mnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) char path[PATH_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) int ret = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) /* set up default */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) max_node_num = 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) mnt = sysfs__mountpoint();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) if (!mnt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) /* get the highest possible cpu number for a sparse allocation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) ret = snprintf(path, PATH_MAX, "%s/devices/system/node/possible", mnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) if (ret >= PATH_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) ret = get_max_num(path, &max_node_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) pr_err("Failed to read max nodes, using default of %d\n", max_node_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) int cpu__max_node(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) if (unlikely(!max_node_num))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) set_max_node_num();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) return max_node_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) int cpu__max_cpu(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) if (unlikely(!max_cpu_num))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) set_max_cpu_num();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) return max_cpu_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) int cpu__max_present_cpu(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) if (unlikely(!max_present_cpu_num))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) set_max_cpu_num();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) return max_present_cpu_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) int cpu__get_node(int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) if (unlikely(cpunode_map == NULL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) pr_debug("cpu_map not initialized\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) return cpunode_map[cpu];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) static int init_cpunode_map(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) set_max_cpu_num();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) set_max_node_num();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) cpunode_map = calloc(max_cpu_num, sizeof(int));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) if (!cpunode_map) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) pr_err("%s: calloc failed\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) for (i = 0; i < max_cpu_num; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) cpunode_map[i] = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) int cpu__setup_cpunode_map(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) struct dirent *dent1, *dent2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) DIR *dir1, *dir2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) unsigned int cpu, mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) char buf[PATH_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) char path[PATH_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) const char *mnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) int n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) /* initialize globals */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) if (init_cpunode_map())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) mnt = sysfs__mountpoint();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) if (!mnt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) n = snprintf(path, PATH_MAX, "%s/devices/system/node", mnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) if (n >= PATH_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) dir1 = opendir(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) if (!dir1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) /* walk tree and setup map */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) while ((dent1 = readdir(dir1)) != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) if (dent1->d_type != DT_DIR || sscanf(dent1->d_name, "node%u", &mem) < 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) n = snprintf(buf, PATH_MAX, "%s/%s", path, dent1->d_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) if (n >= PATH_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) dir2 = opendir(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) if (!dir2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) while ((dent2 = readdir(dir2)) != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) if (dent2->d_type != DT_LNK || sscanf(dent2->d_name, "cpu%u", &cpu) < 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) cpunode_map[cpu] = mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) closedir(dir2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) closedir(dir1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) bool cpu_map__has(struct perf_cpu_map *cpus, int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) return perf_cpu_map__idx(cpus, cpu) != -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) int cpu_map__cpu(struct perf_cpu_map *cpus, int idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) return cpus->map[idx];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) size_t cpu_map__snprint(struct perf_cpu_map *map, char *buf, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) int i, cpu, start = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) bool first = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) size_t ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) #define COMMA first ? "" : ","
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) for (i = 0; i < map->nr + 1; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) bool last = i == map->nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) cpu = last ? INT_MAX : map->map[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) if (start == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) start = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) if (last) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) ret += snprintf(buf + ret, size - ret,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) "%s%d", COMMA,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) map->map[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) } else if (((i - start) != (cpu - map->map[start])) || last) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) int end = i - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) if (start == end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) ret += snprintf(buf + ret, size - ret,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) "%s%d", COMMA,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) map->map[start]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) ret += snprintf(buf + ret, size - ret,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) "%s%d-%d", COMMA,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) map->map[start], map->map[end]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) first = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) start = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) #undef COMMA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) pr_debug2("cpumask list: %s\n", buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) static char hex_char(unsigned char val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) if (val < 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) return val + '0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) if (val < 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) return val - 10 + 'a';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) return '?';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) size_t cpu_map__snprint_mask(struct perf_cpu_map *map, char *buf, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) int i, cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) char *ptr = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) unsigned char *bitmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) int last_cpu = cpu_map__cpu(map, map->nr - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) if (buf == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) bitmap = zalloc(last_cpu / 8 + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) if (bitmap == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) buf[0] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) for (i = 0; i < map->nr; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) cpu = cpu_map__cpu(map, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) bitmap[cpu / 8] |= 1 << (cpu % 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) for (cpu = last_cpu / 4 * 4; cpu >= 0; cpu -= 4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) unsigned char bits = bitmap[cpu / 8];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) if (cpu % 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) bits >>= 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) bits &= 0xf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) *ptr++ = hex_char(bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) if ((cpu % 32) == 0 && cpu > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) *ptr++ = ',';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) *ptr = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) free(bitmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) buf[size - 1] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) return ptr - buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) const struct perf_cpu_map *cpu_map__online(void) /* thread unsafe */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) static const struct perf_cpu_map *online = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) if (!online)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) online = perf_cpu_map__new(NULL); /* from /sys/devices/system/cpu/online */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) return online;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) }