10 static struct cpu_map *cpu_map__default_new(void)
15 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
19 cpus = malloc(sizeof(*cpus) + nr_cpus * sizeof(int));
22 for (i = 0; i < nr_cpus; ++i)
26 atomic_set(&cpus->refcnt, 1);
32 static struct cpu_map *cpu_map__trim_new(int nr_cpus, int *tmp_cpus)
34 size_t payload_size = nr_cpus * sizeof(int);
35 struct cpu_map *cpus = malloc(sizeof(*cpus) + payload_size);
39 memcpy(cpus->map, tmp_cpus, payload_size);
40 atomic_set(&cpus->refcnt, 1);
46 struct cpu_map *cpu_map__read(FILE *file)
48 struct cpu_map *cpus = NULL;
50 int *tmp_cpus = NULL, *tmp;
58 n = fscanf(file, "%u%c", &cpu, &sep);
62 int new_max = nr_cpus + cpu - prev - 1;
64 if (new_max >= max_entries) {
65 max_entries = new_max + MAX_NR_CPUS / 2;
66 tmp = realloc(tmp_cpus, max_entries * sizeof(int));
73 tmp_cpus[nr_cpus++] = prev;
75 if (nr_cpus == max_entries) {
76 max_entries += MAX_NR_CPUS;
77 tmp = realloc(tmp_cpus, max_entries * sizeof(int));
83 tmp_cpus[nr_cpus++] = cpu;
84 if (n == 2 && sep == '-')
88 if (n == 1 || sep == '\n')
93 cpus = cpu_map__trim_new(nr_cpus, tmp_cpus);
95 cpus = cpu_map__default_new();
101 static struct cpu_map *cpu_map__read_all_cpu_map(void)
103 struct cpu_map *cpus = NULL;
106 onlnf = fopen("/sys/devices/system/cpu/online", "r");
108 return cpu_map__default_new();
110 cpus = cpu_map__read(onlnf);
115 struct cpu_map *cpu_map__new(const char *cpu_list)
117 struct cpu_map *cpus = NULL;
118 unsigned long start_cpu, end_cpu = 0;
121 int *tmp_cpus = NULL, *tmp;
125 return cpu_map__read_all_cpu_map();
128 * must handle the case of empty cpumap to cover
129 * TOPOLOGY header for NUMA nodes with no CPU
130 * ( e.g., because of CPU hotplug)
132 if (!isdigit(*cpu_list) && *cpu_list != '\0')
135 while (isdigit(*cpu_list)) {
137 start_cpu = strtoul(cpu_list, &p, 0);
138 if (start_cpu >= INT_MAX
139 || (*p != '\0' && *p != ',' && *p != '-'))
145 end_cpu = strtoul(cpu_list, &p, 0);
147 if (end_cpu >= INT_MAX || (*p != '\0' && *p != ','))
150 if (end_cpu < start_cpu)
156 for (; start_cpu <= end_cpu; start_cpu++) {
157 /* check for duplicates */
158 for (i = 0; i < nr_cpus; i++)
159 if (tmp_cpus[i] == (int)start_cpu)
162 if (nr_cpus == max_entries) {
163 max_entries += MAX_NR_CPUS;
164 tmp = realloc(tmp_cpus, max_entries * sizeof(int));
169 tmp_cpus[nr_cpus++] = (int)start_cpu;
178 cpus = cpu_map__trim_new(nr_cpus, tmp_cpus);
179 else if (*cpu_list != '\0')
180 cpus = cpu_map__default_new();
182 cpus = cpu_map__dummy_new();
189 size_t cpu_map__fprintf(struct cpu_map *map, FILE *fp)
192 size_t printed = fprintf(fp, "%d cpu%s: ",
193 map->nr, map->nr > 1 ? "s" : "");
194 for (i = 0; i < map->nr; ++i)
195 printed += fprintf(fp, "%s%d", i ? ", " : "", map->map[i]);
197 return printed + fprintf(fp, "\n");
200 struct cpu_map *cpu_map__dummy_new(void)
202 struct cpu_map *cpus = malloc(sizeof(*cpus) + sizeof(int));
207 atomic_set(&cpus->refcnt, 1);
213 struct cpu_map *cpu_map__empty_new(int nr)
215 struct cpu_map *cpus = malloc(sizeof(*cpus) + sizeof(int) * nr);
221 for (i = 0; i < nr; i++)
224 atomic_set(&cpus->refcnt, 1);
230 static void cpu_map__delete(struct cpu_map *map)
233 WARN_ONCE(atomic_read(&map->refcnt) != 0,
234 "cpu_map refcnt unbalanced\n");
239 struct cpu_map *cpu_map__get(struct cpu_map *map)
242 atomic_inc(&map->refcnt);
246 void cpu_map__put(struct cpu_map *map)
248 if (map && atomic_dec_and_test(&map->refcnt))
249 cpu_map__delete(map);
252 static int cpu__get_topology_int(int cpu, const char *name, int *value)
256 snprintf(path, PATH_MAX,
257 "devices/system/cpu/cpu%d/topology/%s", cpu, name);
259 return sysfs__read_int(path, value);
262 int cpu_map__get_socket_id(int cpu)
264 int value, ret = cpu__get_topology_int(cpu, "physical_package_id", &value);
268 int cpu_map__get_socket(struct cpu_map *map, int idx, void *data __maybe_unused)
277 return cpu_map__get_socket_id(cpu);
280 static int cmp_ids(const void *a, const void *b)
282 return *(int *)a - *(int *)b;
285 int cpu_map__build_map(struct cpu_map *cpus, struct cpu_map **res,
286 int (*f)(struct cpu_map *map, int cpu, void *data),
293 /* allocate as much as possible */
294 c = calloc(1, sizeof(*c) + nr * sizeof(int));
298 for (cpu = 0; cpu < nr; cpu++) {
299 s1 = f(cpus, cpu, data);
300 for (s2 = 0; s2 < c->nr; s2++) {
301 if (s1 == c->map[s2])
309 /* ensure we process id in increasing order */
310 qsort(c->map, c->nr, sizeof(int), cmp_ids);
312 atomic_set(&c->refcnt, 1);
317 int cpu_map__get_core_id(int cpu)
319 int value, ret = cpu__get_topology_int(cpu, "core_id", &value);
323 int cpu_map__get_core(struct cpu_map *map, int idx, void *data)
332 cpu = cpu_map__get_core_id(cpu);
334 s = cpu_map__get_socket(map, idx, data);
339 * encode socket in upper 16 bits
340 * core_id is relative to socket, and
341 * we need a global id. So we combine
344 return (s << 16) | (cpu & 0xffff);
347 int cpu_map__build_socket_map(struct cpu_map *cpus, struct cpu_map **sockp)
349 return cpu_map__build_map(cpus, sockp, cpu_map__get_socket, NULL);
352 int cpu_map__build_core_map(struct cpu_map *cpus, struct cpu_map **corep)
354 return cpu_map__build_map(cpus, corep, cpu_map__get_core, NULL);
357 /* setup simple routines to easily access node numbers given a cpu number */
358 static int get_max_num(char *path, int *max)
364 if (filename__read_str(path, &buf, &num))
369 /* start on the right, to find highest node num */
371 if ((buf[num] == ',') || (buf[num] == '-')) {
376 if (sscanf(&buf[num], "%d", max) < 1) {
381 /* convert from 0-based to 1-based */
389 /* Determine highest possible cpu in the system for sparse allocation */
390 static void set_max_cpu_num(void)
399 mnt = sysfs__mountpoint();
403 /* get the highest possible cpu number for a sparse allocation */
404 ret = snprintf(path, PATH_MAX, "%s/devices/system/cpu/possible", mnt);
405 if (ret == PATH_MAX) {
406 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
410 ret = get_max_num(path, &max_cpu_num);
414 pr_err("Failed to read max cpus, using default of %d\n", max_cpu_num);
417 /* Determine highest possible node in the system for sparse allocation */
418 static void set_max_node_num(void)
427 mnt = sysfs__mountpoint();
431 /* get the highest possible cpu number for a sparse allocation */
432 ret = snprintf(path, PATH_MAX, "%s/devices/system/node/possible", mnt);
433 if (ret == PATH_MAX) {
434 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
438 ret = get_max_num(path, &max_node_num);
442 pr_err("Failed to read max nodes, using default of %d\n", max_node_num);
445 static int init_cpunode_map(void)
452 cpunode_map = calloc(max_cpu_num, sizeof(int));
454 pr_err("%s: calloc failed\n", __func__);
458 for (i = 0; i < max_cpu_num; i++)
464 int cpu__setup_cpunode_map(void)
466 struct dirent *dent1, *dent2;
468 unsigned int cpu, mem;
474 /* initialize globals */
475 if (init_cpunode_map())
478 mnt = sysfs__mountpoint();
482 n = snprintf(path, PATH_MAX, "%s/devices/system/node", mnt);
484 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
488 dir1 = opendir(path);
492 /* walk tree and setup map */
493 while ((dent1 = readdir(dir1)) != NULL) {
494 if (dent1->d_type != DT_DIR || sscanf(dent1->d_name, "node%u", &mem) < 1)
497 n = snprintf(buf, PATH_MAX, "%s/%s", path, dent1->d_name);
499 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
506 while ((dent2 = readdir(dir2)) != NULL) {
507 if (dent2->d_type != DT_LNK || sscanf(dent2->d_name, "cpu%u", &cpu) < 1)
509 cpunode_map[cpu] = mem;