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();
127 if (!isdigit(*cpu_list))
130 while (isdigit(*cpu_list)) {
132 start_cpu = strtoul(cpu_list, &p, 0);
133 if (start_cpu >= INT_MAX
134 || (*p != '\0' && *p != ',' && *p != '-'))
140 end_cpu = strtoul(cpu_list, &p, 0);
142 if (end_cpu >= INT_MAX || (*p != '\0' && *p != ','))
145 if (end_cpu < start_cpu)
151 for (; start_cpu <= end_cpu; start_cpu++) {
152 /* check for duplicates */
153 for (i = 0; i < nr_cpus; i++)
154 if (tmp_cpus[i] == (int)start_cpu)
157 if (nr_cpus == max_entries) {
158 max_entries += MAX_NR_CPUS;
159 tmp = realloc(tmp_cpus, max_entries * sizeof(int));
164 tmp_cpus[nr_cpus++] = (int)start_cpu;
173 cpus = cpu_map__trim_new(nr_cpus, tmp_cpus);
175 cpus = cpu_map__default_new();
182 size_t cpu_map__fprintf(struct cpu_map *map, FILE *fp)
185 size_t printed = fprintf(fp, "%d cpu%s: ",
186 map->nr, map->nr > 1 ? "s" : "");
187 for (i = 0; i < map->nr; ++i)
188 printed += fprintf(fp, "%s%d", i ? ", " : "", map->map[i]);
190 return printed + fprintf(fp, "\n");
193 struct cpu_map *cpu_map__dummy_new(void)
195 struct cpu_map *cpus = malloc(sizeof(*cpus) + sizeof(int));
200 atomic_set(&cpus->refcnt, 1);
206 static void cpu_map__delete(struct cpu_map *map)
209 WARN_ONCE(atomic_read(&map->refcnt) != 0,
210 "cpu_map refcnt unbalanced\n");
215 struct cpu_map *cpu_map__get(struct cpu_map *map)
218 atomic_inc(&map->refcnt);
222 void cpu_map__put(struct cpu_map *map)
224 if (map && atomic_dec_and_test(&map->refcnt))
225 cpu_map__delete(map);
228 int cpu_map__get_socket(struct cpu_map *map, int idx)
240 mnt = sysfs__mountpoint();
244 snprintf(path, PATH_MAX,
245 "%s/devices/system/cpu/cpu%d/topology/physical_package_id",
248 fp = fopen(path, "r");
251 ret = fscanf(fp, "%d", &cpu);
253 return ret == 1 ? cpu : -1;
256 static int cmp_ids(const void *a, const void *b)
258 return *(int *)a - *(int *)b;
261 static int cpu_map__build_map(struct cpu_map *cpus, struct cpu_map **res,
262 int (*f)(struct cpu_map *map, int cpu))
268 /* allocate as much as possible */
269 c = calloc(1, sizeof(*c) + nr * sizeof(int));
273 for (cpu = 0; cpu < nr; cpu++) {
275 for (s2 = 0; s2 < c->nr; s2++) {
276 if (s1 == c->map[s2])
284 /* ensure we process id in increasing order */
285 qsort(c->map, c->nr, sizeof(int), cmp_ids);
287 atomic_set(&cpus->refcnt, 1);
292 int cpu_map__get_core(struct cpu_map *map, int idx)
304 mnt = sysfs__mountpoint();
308 snprintf(path, PATH_MAX,
309 "%s/devices/system/cpu/cpu%d/topology/core_id",
312 fp = fopen(path, "r");
315 ret = fscanf(fp, "%d", &cpu);
320 s = cpu_map__get_socket(map, idx);
325 * encode socket in upper 16 bits
326 * core_id is relative to socket, and
327 * we need a global id. So we combine
330 return (s << 16) | (cpu & 0xffff);
333 int cpu_map__build_socket_map(struct cpu_map *cpus, struct cpu_map **sockp)
335 return cpu_map__build_map(cpus, sockp, cpu_map__get_socket);
338 int cpu_map__build_core_map(struct cpu_map *cpus, struct cpu_map **corep)
340 return cpu_map__build_map(cpus, corep, cpu_map__get_core);
343 /* setup simple routines to easily access node numbers given a cpu number */
344 static int get_max_num(char *path, int *max)
350 if (filename__read_str(path, &buf, &num))
355 /* start on the right, to find highest node num */
357 if ((buf[num] == ',') || (buf[num] == '-')) {
362 if (sscanf(&buf[num], "%d", max) < 1) {
367 /* convert from 0-based to 1-based */
375 /* Determine highest possible cpu in the system for sparse allocation */
376 static void set_max_cpu_num(void)
385 mnt = sysfs__mountpoint();
389 /* get the highest possible cpu number for a sparse allocation */
390 ret = snprintf(path, PATH_MAX, "%s/devices/system/cpu/possible", mnt);
391 if (ret == PATH_MAX) {
392 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
396 ret = get_max_num(path, &max_cpu_num);
400 pr_err("Failed to read max cpus, using default of %d\n", max_cpu_num);
403 /* Determine highest possible node in the system for sparse allocation */
404 static void set_max_node_num(void)
413 mnt = sysfs__mountpoint();
417 /* get the highest possible cpu number for a sparse allocation */
418 ret = snprintf(path, PATH_MAX, "%s/devices/system/node/possible", mnt);
419 if (ret == PATH_MAX) {
420 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
424 ret = get_max_num(path, &max_node_num);
428 pr_err("Failed to read max nodes, using default of %d\n", max_node_num);
431 static int init_cpunode_map(void)
438 cpunode_map = calloc(max_cpu_num, sizeof(int));
440 pr_err("%s: calloc failed\n", __func__);
444 for (i = 0; i < max_cpu_num; i++)
450 int cpu__setup_cpunode_map(void)
452 struct dirent *dent1, *dent2;
454 unsigned int cpu, mem;
460 /* initialize globals */
461 if (init_cpunode_map())
464 mnt = sysfs__mountpoint();
468 n = snprintf(path, PATH_MAX, "%s/devices/system/node", mnt);
470 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
474 dir1 = opendir(path);
478 /* walk tree and setup map */
479 while ((dent1 = readdir(dir1)) != NULL) {
480 if (dent1->d_type != DT_DIR || sscanf(dent1->d_name, "node%u", &mem) < 1)
483 n = snprintf(buf, PATH_MAX, "%s/%s", path, dent1->d_name);
485 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
492 while ((dent2 = readdir(dir2)) != NULL) {
493 if (dent2->d_type != DT_LNK || sscanf(dent2->d_name, "cpu%u", &cpu) < 1)
495 cpunode_map[cpu] = mem;