10 static int max_cpu_num;
11 static int max_node_num;
12 static int *cpunode_map;
14 static struct cpu_map *cpu_map__default_new(void)
19 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
23 cpus = malloc(sizeof(*cpus) + nr_cpus * sizeof(int));
26 for (i = 0; i < nr_cpus; ++i)
30 atomic_set(&cpus->refcnt, 1);
36 static struct cpu_map *cpu_map__trim_new(int nr_cpus, int *tmp_cpus)
38 size_t payload_size = nr_cpus * sizeof(int);
39 struct cpu_map *cpus = malloc(sizeof(*cpus) + payload_size);
43 memcpy(cpus->map, tmp_cpus, payload_size);
44 atomic_set(&cpus->refcnt, 1);
50 struct cpu_map *cpu_map__read(FILE *file)
52 struct cpu_map *cpus = NULL;
54 int *tmp_cpus = NULL, *tmp;
62 n = fscanf(file, "%u%c", &cpu, &sep);
66 int new_max = nr_cpus + cpu - prev - 1;
68 if (new_max >= max_entries) {
69 max_entries = new_max + MAX_NR_CPUS / 2;
70 tmp = realloc(tmp_cpus, max_entries * sizeof(int));
77 tmp_cpus[nr_cpus++] = prev;
79 if (nr_cpus == max_entries) {
80 max_entries += MAX_NR_CPUS;
81 tmp = realloc(tmp_cpus, max_entries * sizeof(int));
87 tmp_cpus[nr_cpus++] = cpu;
88 if (n == 2 && sep == '-')
92 if (n == 1 || sep == '\n')
97 cpus = cpu_map__trim_new(nr_cpus, tmp_cpus);
99 cpus = cpu_map__default_new();
105 static struct cpu_map *cpu_map__read_all_cpu_map(void)
107 struct cpu_map *cpus = NULL;
110 onlnf = fopen("/sys/devices/system/cpu/online", "r");
112 return cpu_map__default_new();
114 cpus = cpu_map__read(onlnf);
119 struct cpu_map *cpu_map__new(const char *cpu_list)
121 struct cpu_map *cpus = NULL;
122 unsigned long start_cpu, end_cpu = 0;
125 int *tmp_cpus = NULL, *tmp;
129 return cpu_map__read_all_cpu_map();
132 * must handle the case of empty cpumap to cover
133 * TOPOLOGY header for NUMA nodes with no CPU
134 * ( e.g., because of CPU hotplug)
136 if (!isdigit(*cpu_list) && *cpu_list != '\0')
139 while (isdigit(*cpu_list)) {
141 start_cpu = strtoul(cpu_list, &p, 0);
142 if (start_cpu >= INT_MAX
143 || (*p != '\0' && *p != ',' && *p != '-'))
149 end_cpu = strtoul(cpu_list, &p, 0);
151 if (end_cpu >= INT_MAX || (*p != '\0' && *p != ','))
154 if (end_cpu < start_cpu)
160 for (; start_cpu <= end_cpu; start_cpu++) {
161 /* check for duplicates */
162 for (i = 0; i < nr_cpus; i++)
163 if (tmp_cpus[i] == (int)start_cpu)
166 if (nr_cpus == max_entries) {
167 max_entries += MAX_NR_CPUS;
168 tmp = realloc(tmp_cpus, max_entries * sizeof(int));
173 tmp_cpus[nr_cpus++] = (int)start_cpu;
182 cpus = cpu_map__trim_new(nr_cpus, tmp_cpus);
183 else if (*cpu_list != '\0')
184 cpus = cpu_map__default_new();
186 cpus = cpu_map__dummy_new();
193 size_t cpu_map__fprintf(struct cpu_map *map, FILE *fp)
196 size_t printed = fprintf(fp, "%d cpu%s: ",
197 map->nr, map->nr > 1 ? "s" : "");
198 for (i = 0; i < map->nr; ++i)
199 printed += fprintf(fp, "%s%d", i ? ", " : "", map->map[i]);
201 return printed + fprintf(fp, "\n");
204 struct cpu_map *cpu_map__dummy_new(void)
206 struct cpu_map *cpus = malloc(sizeof(*cpus) + sizeof(int));
211 atomic_set(&cpus->refcnt, 1);
217 struct cpu_map *cpu_map__empty_new(int nr)
219 struct cpu_map *cpus = malloc(sizeof(*cpus) + sizeof(int) * nr);
225 for (i = 0; i < nr; i++)
228 atomic_set(&cpus->refcnt, 1);
234 static void cpu_map__delete(struct cpu_map *map)
237 WARN_ONCE(atomic_read(&map->refcnt) != 0,
238 "cpu_map refcnt unbalanced\n");
243 struct cpu_map *cpu_map__get(struct cpu_map *map)
246 atomic_inc(&map->refcnt);
250 void cpu_map__put(struct cpu_map *map)
252 if (map && atomic_dec_and_test(&map->refcnt))
253 cpu_map__delete(map);
256 static int cpu__get_topology_int(int cpu, const char *name, int *value)
260 snprintf(path, PATH_MAX,
261 "devices/system/cpu/cpu%d/topology/%s", cpu, name);
263 return sysfs__read_int(path, value);
266 int cpu_map__get_socket_id(int cpu)
268 int value, ret = cpu__get_topology_int(cpu, "physical_package_id", &value);
272 int cpu_map__get_socket(struct cpu_map *map, int idx, void *data __maybe_unused)
281 return cpu_map__get_socket_id(cpu);
284 static int cmp_ids(const void *a, const void *b)
286 return *(int *)a - *(int *)b;
289 int cpu_map__build_map(struct cpu_map *cpus, struct cpu_map **res,
290 int (*f)(struct cpu_map *map, int cpu, void *data),
297 /* allocate as much as possible */
298 c = calloc(1, sizeof(*c) + nr * sizeof(int));
302 for (cpu = 0; cpu < nr; cpu++) {
303 s1 = f(cpus, cpu, data);
304 for (s2 = 0; s2 < c->nr; s2++) {
305 if (s1 == c->map[s2])
313 /* ensure we process id in increasing order */
314 qsort(c->map, c->nr, sizeof(int), cmp_ids);
316 atomic_set(&c->refcnt, 1);
321 int cpu_map__get_core_id(int cpu)
323 int value, ret = cpu__get_topology_int(cpu, "core_id", &value);
327 int cpu_map__get_core(struct cpu_map *map, int idx, void *data)
336 cpu = cpu_map__get_core_id(cpu);
338 s = cpu_map__get_socket(map, idx, data);
343 * encode socket in upper 16 bits
344 * core_id is relative to socket, and
345 * we need a global id. So we combine
348 return (s << 16) | (cpu & 0xffff);
351 int cpu_map__build_socket_map(struct cpu_map *cpus, struct cpu_map **sockp)
353 return cpu_map__build_map(cpus, sockp, cpu_map__get_socket, NULL);
356 int cpu_map__build_core_map(struct cpu_map *cpus, struct cpu_map **corep)
358 return cpu_map__build_map(cpus, corep, cpu_map__get_core, NULL);
361 /* setup simple routines to easily access node numbers given a cpu number */
362 static int get_max_num(char *path, int *max)
368 if (filename__read_str(path, &buf, &num))
373 /* start on the right, to find highest node num */
375 if ((buf[num] == ',') || (buf[num] == '-')) {
380 if (sscanf(&buf[num], "%d", max) < 1) {
385 /* convert from 0-based to 1-based */
393 /* Determine highest possible cpu in the system for sparse allocation */
394 static void set_max_cpu_num(void)
403 mnt = sysfs__mountpoint();
407 /* get the highest possible cpu number for a sparse allocation */
408 ret = snprintf(path, PATH_MAX, "%s/devices/system/cpu/possible", mnt);
409 if (ret == PATH_MAX) {
410 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
414 ret = get_max_num(path, &max_cpu_num);
418 pr_err("Failed to read max cpus, using default of %d\n", max_cpu_num);
421 /* Determine highest possible node in the system for sparse allocation */
422 static void set_max_node_num(void)
431 mnt = sysfs__mountpoint();
435 /* get the highest possible cpu number for a sparse allocation */
436 ret = snprintf(path, PATH_MAX, "%s/devices/system/node/possible", mnt);
437 if (ret == PATH_MAX) {
438 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
442 ret = get_max_num(path, &max_node_num);
446 pr_err("Failed to read max nodes, using default of %d\n", max_node_num);
449 int cpu__max_node(void)
451 if (unlikely(!max_node_num))
457 int cpu__max_cpu(void)
459 if (unlikely(!max_cpu_num))
465 int cpu__get_node(int cpu)
467 if (unlikely(cpunode_map == NULL)) {
468 pr_debug("cpu_map not initialized\n");
472 return cpunode_map[cpu];
475 static int init_cpunode_map(void)
482 cpunode_map = calloc(max_cpu_num, sizeof(int));
484 pr_err("%s: calloc failed\n", __func__);
488 for (i = 0; i < max_cpu_num; i++)
494 int cpu__setup_cpunode_map(void)
496 struct dirent *dent1, *dent2;
498 unsigned int cpu, mem;
504 /* initialize globals */
505 if (init_cpunode_map())
508 mnt = sysfs__mountpoint();
512 n = snprintf(path, PATH_MAX, "%s/devices/system/node", mnt);
514 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
518 dir1 = opendir(path);
522 /* walk tree and setup map */
523 while ((dent1 = readdir(dir1)) != NULL) {
524 if (dent1->d_type != DT_DIR || sscanf(dent1->d_name, "node%u", &mem) < 1)
527 n = snprintf(buf, PATH_MAX, "%s/%s", path, dent1->d_name);
529 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
536 while ((dent2 = readdir(dir2)) != NULL) {
537 if (dent2->d_type != DT_LNK || sscanf(dent2->d_name, "cpu%u", &cpu) < 1)
539 cpunode_map[cpu] = mem;