8 #include <linux/bitmap.h>
11 static int max_cpu_num;
12 static int max_node_num;
13 static int *cpunode_map;
15 static struct cpu_map *cpu_map__default_new(void)
20 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
24 cpus = malloc(sizeof(*cpus) + nr_cpus * sizeof(int));
27 for (i = 0; i < nr_cpus; ++i)
31 atomic_set(&cpus->refcnt, 1);
37 static struct cpu_map *cpu_map__trim_new(int nr_cpus, int *tmp_cpus)
39 size_t payload_size = nr_cpus * sizeof(int);
40 struct cpu_map *cpus = malloc(sizeof(*cpus) + payload_size);
44 memcpy(cpus->map, tmp_cpus, payload_size);
45 atomic_set(&cpus->refcnt, 1);
51 struct cpu_map *cpu_map__read(FILE *file)
53 struct cpu_map *cpus = NULL;
55 int *tmp_cpus = NULL, *tmp;
63 n = fscanf(file, "%u%c", &cpu, &sep);
67 int new_max = nr_cpus + cpu - prev - 1;
69 if (new_max >= max_entries) {
70 max_entries = new_max + MAX_NR_CPUS / 2;
71 tmp = realloc(tmp_cpus, max_entries * sizeof(int));
78 tmp_cpus[nr_cpus++] = prev;
80 if (nr_cpus == max_entries) {
81 max_entries += MAX_NR_CPUS;
82 tmp = realloc(tmp_cpus, max_entries * sizeof(int));
88 tmp_cpus[nr_cpus++] = cpu;
89 if (n == 2 && sep == '-')
93 if (n == 1 || sep == '\n')
98 cpus = cpu_map__trim_new(nr_cpus, tmp_cpus);
100 cpus = cpu_map__default_new();
106 static struct cpu_map *cpu_map__read_all_cpu_map(void)
108 struct cpu_map *cpus = NULL;
111 onlnf = fopen("/sys/devices/system/cpu/online", "r");
113 return cpu_map__default_new();
115 cpus = cpu_map__read(onlnf);
120 struct cpu_map *cpu_map__new(const char *cpu_list)
122 struct cpu_map *cpus = NULL;
123 unsigned long start_cpu, end_cpu = 0;
126 int *tmp_cpus = NULL, *tmp;
130 return cpu_map__read_all_cpu_map();
133 * must handle the case of empty cpumap to cover
134 * TOPOLOGY header for NUMA nodes with no CPU
135 * ( e.g., because of CPU hotplug)
137 if (!isdigit(*cpu_list) && *cpu_list != '\0')
140 while (isdigit(*cpu_list)) {
142 start_cpu = strtoul(cpu_list, &p, 0);
143 if (start_cpu >= INT_MAX
144 || (*p != '\0' && *p != ',' && *p != '-'))
150 end_cpu = strtoul(cpu_list, &p, 0);
152 if (end_cpu >= INT_MAX || (*p != '\0' && *p != ','))
155 if (end_cpu < start_cpu)
161 for (; start_cpu <= end_cpu; start_cpu++) {
162 /* check for duplicates */
163 for (i = 0; i < nr_cpus; i++)
164 if (tmp_cpus[i] == (int)start_cpu)
167 if (nr_cpus == max_entries) {
168 max_entries += MAX_NR_CPUS;
169 tmp = realloc(tmp_cpus, max_entries * sizeof(int));
174 tmp_cpus[nr_cpus++] = (int)start_cpu;
183 cpus = cpu_map__trim_new(nr_cpus, tmp_cpus);
184 else if (*cpu_list != '\0')
185 cpus = cpu_map__default_new();
187 cpus = cpu_map__dummy_new();
194 static struct cpu_map *cpu_map__from_entries(struct cpu_map_entries *cpus)
198 map = cpu_map__empty_new(cpus->nr);
202 for (i = 0; i < cpus->nr; i++) {
204 * Special treatment for -1, which is not real cpu number,
205 * and we need to use (int) -1 to initialize map[i],
206 * otherwise it would become 65535.
208 if (cpus->cpu[i] == (u16) -1)
211 map->map[i] = (int) cpus->cpu[i];
218 static struct cpu_map *cpu_map__from_mask(struct cpu_map_mask *mask)
221 int nr, nbits = mask->nr * mask->long_size * BITS_PER_BYTE;
223 nr = bitmap_weight(mask->mask, nbits);
225 map = cpu_map__empty_new(nr);
229 for_each_set_bit(cpu, mask->mask, nbits)
236 struct cpu_map *cpu_map__new_data(struct cpu_map_data *data)
238 if (data->type == PERF_CPU_MAP__CPUS)
239 return cpu_map__from_entries((struct cpu_map_entries *)data->data);
241 return cpu_map__from_mask((struct cpu_map_mask *)data->data);
244 size_t cpu_map__fprintf(struct cpu_map *map, FILE *fp)
249 cpu_map__snprint(map, buf, sizeof(buf));
250 return fprintf(fp, "%s\n", buf);
254 struct cpu_map *cpu_map__dummy_new(void)
256 struct cpu_map *cpus = malloc(sizeof(*cpus) + sizeof(int));
261 atomic_set(&cpus->refcnt, 1);
267 struct cpu_map *cpu_map__empty_new(int nr)
269 struct cpu_map *cpus = malloc(sizeof(*cpus) + sizeof(int) * nr);
275 for (i = 0; i < nr; i++)
278 atomic_set(&cpus->refcnt, 1);
284 static void cpu_map__delete(struct cpu_map *map)
287 WARN_ONCE(atomic_read(&map->refcnt) != 0,
288 "cpu_map refcnt unbalanced\n");
293 struct cpu_map *cpu_map__get(struct cpu_map *map)
296 atomic_inc(&map->refcnt);
300 void cpu_map__put(struct cpu_map *map)
302 if (map && atomic_dec_and_test(&map->refcnt))
303 cpu_map__delete(map);
306 static int cpu__get_topology_int(int cpu, const char *name, int *value)
310 snprintf(path, PATH_MAX,
311 "devices/system/cpu/cpu%d/topology/%s", cpu, name);
313 return sysfs__read_int(path, value);
316 int cpu_map__get_socket_id(int cpu)
318 int value, ret = cpu__get_topology_int(cpu, "physical_package_id", &value);
322 int cpu_map__get_socket(struct cpu_map *map, int idx, void *data __maybe_unused)
331 return cpu_map__get_socket_id(cpu);
334 static int cmp_ids(const void *a, const void *b)
336 return *(int *)a - *(int *)b;
339 int cpu_map__build_map(struct cpu_map *cpus, struct cpu_map **res,
340 int (*f)(struct cpu_map *map, int cpu, void *data),
347 /* allocate as much as possible */
348 c = calloc(1, sizeof(*c) + nr * sizeof(int));
352 for (cpu = 0; cpu < nr; cpu++) {
353 s1 = f(cpus, cpu, data);
354 for (s2 = 0; s2 < c->nr; s2++) {
355 if (s1 == c->map[s2])
363 /* ensure we process id in increasing order */
364 qsort(c->map, c->nr, sizeof(int), cmp_ids);
366 atomic_set(&c->refcnt, 1);
371 int cpu_map__get_core_id(int cpu)
373 int value, ret = cpu__get_topology_int(cpu, "core_id", &value);
377 int cpu_map__get_core(struct cpu_map *map, int idx, void *data)
386 cpu = cpu_map__get_core_id(cpu);
388 s = cpu_map__get_socket(map, idx, data);
393 * encode socket in upper 16 bits
394 * core_id is relative to socket, and
395 * we need a global id. So we combine
398 return (s << 16) | (cpu & 0xffff);
401 int cpu_map__build_socket_map(struct cpu_map *cpus, struct cpu_map **sockp)
403 return cpu_map__build_map(cpus, sockp, cpu_map__get_socket, NULL);
406 int cpu_map__build_core_map(struct cpu_map *cpus, struct cpu_map **corep)
408 return cpu_map__build_map(cpus, corep, cpu_map__get_core, NULL);
411 /* setup simple routines to easily access node numbers given a cpu number */
412 static int get_max_num(char *path, int *max)
418 if (filename__read_str(path, &buf, &num))
423 /* start on the right, to find highest node num */
425 if ((buf[num] == ',') || (buf[num] == '-')) {
430 if (sscanf(&buf[num], "%d", max) < 1) {
435 /* convert from 0-based to 1-based */
443 /* Determine highest possible cpu in the system for sparse allocation */
444 static void set_max_cpu_num(void)
453 mnt = sysfs__mountpoint();
457 /* get the highest possible cpu number for a sparse allocation */
458 ret = snprintf(path, PATH_MAX, "%s/devices/system/cpu/possible", mnt);
459 if (ret == PATH_MAX) {
460 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
464 ret = get_max_num(path, &max_cpu_num);
468 pr_err("Failed to read max cpus, using default of %d\n", max_cpu_num);
471 /* Determine highest possible node in the system for sparse allocation */
472 static void set_max_node_num(void)
481 mnt = sysfs__mountpoint();
485 /* get the highest possible cpu number for a sparse allocation */
486 ret = snprintf(path, PATH_MAX, "%s/devices/system/node/possible", mnt);
487 if (ret == PATH_MAX) {
488 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
492 ret = get_max_num(path, &max_node_num);
496 pr_err("Failed to read max nodes, using default of %d\n", max_node_num);
499 int cpu__max_node(void)
501 if (unlikely(!max_node_num))
507 int cpu__max_cpu(void)
509 if (unlikely(!max_cpu_num))
515 int cpu__get_node(int cpu)
517 if (unlikely(cpunode_map == NULL)) {
518 pr_debug("cpu_map not initialized\n");
522 return cpunode_map[cpu];
525 static int init_cpunode_map(void)
532 cpunode_map = calloc(max_cpu_num, sizeof(int));
534 pr_err("%s: calloc failed\n", __func__);
538 for (i = 0; i < max_cpu_num; i++)
544 int cpu__setup_cpunode_map(void)
546 struct dirent *dent1, *dent2;
548 unsigned int cpu, mem;
554 /* initialize globals */
555 if (init_cpunode_map())
558 mnt = sysfs__mountpoint();
562 n = snprintf(path, PATH_MAX, "%s/devices/system/node", mnt);
564 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
568 dir1 = opendir(path);
572 /* walk tree and setup map */
573 while ((dent1 = readdir(dir1)) != NULL) {
574 if (dent1->d_type != DT_DIR || sscanf(dent1->d_name, "node%u", &mem) < 1)
577 n = snprintf(buf, PATH_MAX, "%s/%s", path, dent1->d_name);
579 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
586 while ((dent2 = readdir(dir2)) != NULL) {
587 if (dent2->d_type != DT_LNK || sscanf(dent2->d_name, "cpu%u", &cpu) < 1)
589 cpunode_map[cpu] = mem;
597 bool cpu_map__has(struct cpu_map *cpus, int cpu)
599 return cpu_map__idx(cpus, cpu) != -1;
602 int cpu_map__idx(struct cpu_map *cpus, int cpu)
606 for (i = 0; i < cpus->nr; ++i) {
607 if (cpus->map[i] == cpu)
614 int cpu_map__cpu(struct cpu_map *cpus, int idx)
616 return cpus->map[idx];
619 size_t cpu_map__snprint(struct cpu_map *map, char *buf, size_t size)
621 int i, cpu, start = -1;
625 #define COMMA first ? "" : ","
627 for (i = 0; i < map->nr + 1; i++) {
628 bool last = i == map->nr;
630 cpu = last ? INT_MAX : map->map[i];
635 ret += snprintf(buf + ret, size - ret,
639 } else if (((i - start) != (cpu - map->map[start])) || last) {
643 ret += snprintf(buf + ret, size - ret,
647 ret += snprintf(buf + ret, size - ret,
649 map->map[start], map->map[end]);
658 pr_debug("cpumask list: %s\n", buf);