8 #include <linux/bitmap.h>
11 static int max_cpu_num;
12 static int max_present_cpu_num;
13 static int max_node_num;
14 static int *cpunode_map;
16 static struct cpu_map *cpu_map__default_new(void)
21 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
25 cpus = malloc(sizeof(*cpus) + nr_cpus * sizeof(int));
28 for (i = 0; i < nr_cpus; ++i)
32 atomic_set(&cpus->refcnt, 1);
38 static struct cpu_map *cpu_map__trim_new(int nr_cpus, int *tmp_cpus)
40 size_t payload_size = nr_cpus * sizeof(int);
41 struct cpu_map *cpus = malloc(sizeof(*cpus) + payload_size);
45 memcpy(cpus->map, tmp_cpus, payload_size);
46 atomic_set(&cpus->refcnt, 1);
52 struct cpu_map *cpu_map__read(FILE *file)
54 struct cpu_map *cpus = NULL;
56 int *tmp_cpus = NULL, *tmp;
64 n = fscanf(file, "%u%c", &cpu, &sep);
68 int new_max = nr_cpus + cpu - prev - 1;
70 if (new_max >= max_entries) {
71 max_entries = new_max + MAX_NR_CPUS / 2;
72 tmp = realloc(tmp_cpus, max_entries * sizeof(int));
79 tmp_cpus[nr_cpus++] = prev;
81 if (nr_cpus == max_entries) {
82 max_entries += MAX_NR_CPUS;
83 tmp = realloc(tmp_cpus, max_entries * sizeof(int));
89 tmp_cpus[nr_cpus++] = cpu;
90 if (n == 2 && sep == '-')
94 if (n == 1 || sep == '\n')
99 cpus = cpu_map__trim_new(nr_cpus, tmp_cpus);
101 cpus = cpu_map__default_new();
107 static struct cpu_map *cpu_map__read_all_cpu_map(void)
109 struct cpu_map *cpus = NULL;
112 onlnf = fopen("/sys/devices/system/cpu/online", "r");
114 return cpu_map__default_new();
116 cpus = cpu_map__read(onlnf);
121 struct cpu_map *cpu_map__new(const char *cpu_list)
123 struct cpu_map *cpus = NULL;
124 unsigned long start_cpu, end_cpu = 0;
127 int *tmp_cpus = NULL, *tmp;
131 return cpu_map__read_all_cpu_map();
133 if (!isdigit(*cpu_list))
136 while (isdigit(*cpu_list)) {
138 start_cpu = strtoul(cpu_list, &p, 0);
139 if (start_cpu >= INT_MAX
140 || (*p != '\0' && *p != ',' && *p != '-'))
146 end_cpu = strtoul(cpu_list, &p, 0);
148 if (end_cpu >= INT_MAX || (*p != '\0' && *p != ','))
151 if (end_cpu < start_cpu)
157 for (; start_cpu <= end_cpu; start_cpu++) {
158 /* check for duplicates */
159 for (i = 0; i < nr_cpus; i++)
160 if (tmp_cpus[i] == (int)start_cpu)
163 if (nr_cpus == max_entries) {
164 max_entries += MAX_NR_CPUS;
165 tmp = realloc(tmp_cpus, max_entries * sizeof(int));
170 tmp_cpus[nr_cpus++] = (int)start_cpu;
179 cpus = cpu_map__trim_new(nr_cpus, tmp_cpus);
181 cpus = cpu_map__default_new();
188 static struct cpu_map *cpu_map__from_entries(struct cpu_map_entries *cpus)
192 map = cpu_map__empty_new(cpus->nr);
196 for (i = 0; i < cpus->nr; i++) {
198 * Special treatment for -1, which is not real cpu number,
199 * and we need to use (int) -1 to initialize map[i],
200 * otherwise it would become 65535.
202 if (cpus->cpu[i] == (u16) -1)
205 map->map[i] = (int) cpus->cpu[i];
212 static struct cpu_map *cpu_map__from_mask(struct cpu_map_mask *mask)
215 int nr, nbits = mask->nr * mask->long_size * BITS_PER_BYTE;
217 nr = bitmap_weight(mask->mask, nbits);
219 map = cpu_map__empty_new(nr);
223 for_each_set_bit(cpu, mask->mask, nbits)
230 struct cpu_map *cpu_map__new_data(struct cpu_map_data *data)
232 if (data->type == PERF_CPU_MAP__CPUS)
233 return cpu_map__from_entries((struct cpu_map_entries *)data->data);
235 return cpu_map__from_mask((struct cpu_map_mask *)data->data);
238 size_t cpu_map__fprintf(struct cpu_map *map, FILE *fp)
243 cpu_map__snprint(map, buf, sizeof(buf));
244 return fprintf(fp, "%s\n", buf);
248 struct cpu_map *cpu_map__dummy_new(void)
250 struct cpu_map *cpus = malloc(sizeof(*cpus) + sizeof(int));
255 atomic_set(&cpus->refcnt, 1);
261 struct cpu_map *cpu_map__empty_new(int nr)
263 struct cpu_map *cpus = malloc(sizeof(*cpus) + sizeof(int) * nr);
269 for (i = 0; i < nr; i++)
272 atomic_set(&cpus->refcnt, 1);
278 static void cpu_map__delete(struct cpu_map *map)
281 WARN_ONCE(atomic_read(&map->refcnt) != 0,
282 "cpu_map refcnt unbalanced\n");
287 struct cpu_map *cpu_map__get(struct cpu_map *map)
290 atomic_inc(&map->refcnt);
294 void cpu_map__put(struct cpu_map *map)
296 if (map && atomic_dec_and_test(&map->refcnt))
297 cpu_map__delete(map);
300 static int cpu__get_topology_int(int cpu, const char *name, int *value)
304 snprintf(path, PATH_MAX,
305 "devices/system/cpu/cpu%d/topology/%s", cpu, name);
307 return sysfs__read_int(path, value);
310 int cpu_map__get_socket_id(int cpu)
312 int value, ret = cpu__get_topology_int(cpu, "physical_package_id", &value);
316 int cpu_map__get_socket(struct cpu_map *map, int idx, void *data __maybe_unused)
325 return cpu_map__get_socket_id(cpu);
328 static int cmp_ids(const void *a, const void *b)
330 return *(int *)a - *(int *)b;
333 int cpu_map__build_map(struct cpu_map *cpus, struct cpu_map **res,
334 int (*f)(struct cpu_map *map, int cpu, void *data),
341 /* allocate as much as possible */
342 c = calloc(1, sizeof(*c) + nr * sizeof(int));
346 for (cpu = 0; cpu < nr; cpu++) {
347 s1 = f(cpus, cpu, data);
348 for (s2 = 0; s2 < c->nr; s2++) {
349 if (s1 == c->map[s2])
357 /* ensure we process id in increasing order */
358 qsort(c->map, c->nr, sizeof(int), cmp_ids);
360 atomic_set(&c->refcnt, 1);
365 int cpu_map__get_core_id(int cpu)
367 int value, ret = cpu__get_topology_int(cpu, "core_id", &value);
371 int cpu_map__get_core(struct cpu_map *map, int idx, void *data)
380 cpu = cpu_map__get_core_id(cpu);
382 s = cpu_map__get_socket(map, idx, data);
387 * encode socket in upper 16 bits
388 * core_id is relative to socket, and
389 * we need a global id. So we combine
392 return (s << 16) | (cpu & 0xffff);
395 int cpu_map__build_socket_map(struct cpu_map *cpus, struct cpu_map **sockp)
397 return cpu_map__build_map(cpus, sockp, cpu_map__get_socket, NULL);
400 int cpu_map__build_core_map(struct cpu_map *cpus, struct cpu_map **corep)
402 return cpu_map__build_map(cpus, corep, cpu_map__get_core, NULL);
405 /* setup simple routines to easily access node numbers given a cpu number */
406 static int get_max_num(char *path, int *max)
412 if (filename__read_str(path, &buf, &num))
417 /* start on the right, to find highest node num */
419 if ((buf[num] == ',') || (buf[num] == '-')) {
424 if (sscanf(&buf[num], "%d", max) < 1) {
429 /* convert from 0-based to 1-based */
437 /* Determine highest possible cpu in the system for sparse allocation */
438 static void set_max_cpu_num(void)
446 max_present_cpu_num = 4096;
448 mnt = sysfs__mountpoint();
452 /* get the highest possible cpu number for a sparse allocation */
453 ret = snprintf(path, PATH_MAX, "%s/devices/system/cpu/possible", mnt);
454 if (ret == PATH_MAX) {
455 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
459 ret = get_max_num(path, &max_cpu_num);
463 /* get the highest present cpu number for a sparse allocation */
464 ret = snprintf(path, PATH_MAX, "%s/devices/system/cpu/present", mnt);
465 if (ret == PATH_MAX) {
466 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
470 ret = get_max_num(path, &max_present_cpu_num);
474 pr_err("Failed to read max cpus, using default of %d\n", max_cpu_num);
477 /* Determine highest possible node in the system for sparse allocation */
478 static void set_max_node_num(void)
487 mnt = sysfs__mountpoint();
491 /* get the highest possible cpu number for a sparse allocation */
492 ret = snprintf(path, PATH_MAX, "%s/devices/system/node/possible", mnt);
493 if (ret == PATH_MAX) {
494 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
498 ret = get_max_num(path, &max_node_num);
502 pr_err("Failed to read max nodes, using default of %d\n", max_node_num);
505 int cpu__max_node(void)
507 if (unlikely(!max_node_num))
513 int cpu__max_cpu(void)
515 if (unlikely(!max_cpu_num))
521 int cpu__max_present_cpu(void)
523 if (unlikely(!max_present_cpu_num))
526 return max_present_cpu_num;
530 int cpu__get_node(int cpu)
532 if (unlikely(cpunode_map == NULL)) {
533 pr_debug("cpu_map not initialized\n");
537 return cpunode_map[cpu];
540 static int init_cpunode_map(void)
547 cpunode_map = calloc(max_cpu_num, sizeof(int));
549 pr_err("%s: calloc failed\n", __func__);
553 for (i = 0; i < max_cpu_num; i++)
559 int cpu__setup_cpunode_map(void)
561 struct dirent *dent1, *dent2;
563 unsigned int cpu, mem;
569 /* initialize globals */
570 if (init_cpunode_map())
573 mnt = sysfs__mountpoint();
577 n = snprintf(path, PATH_MAX, "%s/devices/system/node", mnt);
579 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
583 dir1 = opendir(path);
587 /* walk tree and setup map */
588 while ((dent1 = readdir(dir1)) != NULL) {
589 if (dent1->d_type != DT_DIR || sscanf(dent1->d_name, "node%u", &mem) < 1)
592 n = snprintf(buf, PATH_MAX, "%s/%s", path, dent1->d_name);
594 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
601 while ((dent2 = readdir(dir2)) != NULL) {
602 if (dent2->d_type != DT_LNK || sscanf(dent2->d_name, "cpu%u", &cpu) < 1)
604 cpunode_map[cpu] = mem;
612 bool cpu_map__has(struct cpu_map *cpus, int cpu)
614 return cpu_map__idx(cpus, cpu) != -1;
617 int cpu_map__idx(struct cpu_map *cpus, int cpu)
621 for (i = 0; i < cpus->nr; ++i) {
622 if (cpus->map[i] == cpu)
629 int cpu_map__cpu(struct cpu_map *cpus, int idx)
631 return cpus->map[idx];
634 size_t cpu_map__snprint(struct cpu_map *map, char *buf, size_t size)
636 int i, cpu, start = -1;
640 #define COMMA first ? "" : ","
642 for (i = 0; i < map->nr + 1; i++) {
643 bool last = i == map->nr;
645 cpu = last ? INT_MAX : map->map[i];
650 ret += snprintf(buf + ret, size - ret,
654 } else if (((i - start) != (cpu - map->map[start])) || last) {
658 ret += snprintf(buf + ret, size - ret,
662 ret += snprintf(buf + ret, size - ret,
664 map->map[start], map->map[end]);
673 pr_debug("cpumask list: %s\n", buf);