9 static struct cpu_map
*cpu_map__default_new(void)
14 nr_cpus
= sysconf(_SC_NPROCESSORS_ONLN
);
18 cpus
= malloc(sizeof(*cpus
) + nr_cpus
* sizeof(int));
21 for (i
= 0; i
< nr_cpus
; ++i
)
30 static struct cpu_map
*cpu_map__trim_new(int nr_cpus
, int *tmp_cpus
)
32 size_t payload_size
= nr_cpus
* sizeof(int);
33 struct cpu_map
*cpus
= malloc(sizeof(*cpus
) + payload_size
);
37 memcpy(cpus
->map
, tmp_cpus
, payload_size
);
43 struct cpu_map
*cpu_map__read(FILE *file
)
45 struct cpu_map
*cpus
= NULL
;
47 int *tmp_cpus
= NULL
, *tmp
;
55 n
= fscanf(file
, "%u%c", &cpu
, &sep
);
59 int new_max
= nr_cpus
+ cpu
- prev
- 1;
61 if (new_max
>= max_entries
) {
62 max_entries
= new_max
+ MAX_NR_CPUS
/ 2;
63 tmp
= realloc(tmp_cpus
, max_entries
* sizeof(int));
70 tmp_cpus
[nr_cpus
++] = prev
;
72 if (nr_cpus
== max_entries
) {
73 max_entries
+= MAX_NR_CPUS
;
74 tmp
= realloc(tmp_cpus
, max_entries
* sizeof(int));
80 tmp_cpus
[nr_cpus
++] = cpu
;
81 if (n
== 2 && sep
== '-')
85 if (n
== 1 || sep
== '\n')
90 cpus
= cpu_map__trim_new(nr_cpus
, tmp_cpus
);
92 cpus
= cpu_map__default_new();
98 static struct cpu_map
*cpu_map__read_all_cpu_map(void)
100 struct cpu_map
*cpus
= NULL
;
103 onlnf
= fopen("/sys/devices/system/cpu/online", "r");
105 return cpu_map__default_new();
107 cpus
= cpu_map__read(onlnf
);
112 struct cpu_map
*cpu_map__new(const char *cpu_list
)
114 struct cpu_map
*cpus
= NULL
;
115 unsigned long start_cpu
, end_cpu
= 0;
118 int *tmp_cpus
= NULL
, *tmp
;
122 return cpu_map__read_all_cpu_map();
124 if (!isdigit(*cpu_list
))
127 while (isdigit(*cpu_list
)) {
129 start_cpu
= strtoul(cpu_list
, &p
, 0);
130 if (start_cpu
>= INT_MAX
131 || (*p
!= '\0' && *p
!= ',' && *p
!= '-'))
137 end_cpu
= strtoul(cpu_list
, &p
, 0);
139 if (end_cpu
>= INT_MAX
|| (*p
!= '\0' && *p
!= ','))
142 if (end_cpu
< start_cpu
)
148 for (; start_cpu
<= end_cpu
; start_cpu
++) {
149 /* check for duplicates */
150 for (i
= 0; i
< nr_cpus
; i
++)
151 if (tmp_cpus
[i
] == (int)start_cpu
)
154 if (nr_cpus
== max_entries
) {
155 max_entries
+= MAX_NR_CPUS
;
156 tmp
= realloc(tmp_cpus
, max_entries
* sizeof(int));
161 tmp_cpus
[nr_cpus
++] = (int)start_cpu
;
170 cpus
= cpu_map__trim_new(nr_cpus
, tmp_cpus
);
172 cpus
= cpu_map__default_new();
179 size_t cpu_map__fprintf(struct cpu_map
*map
, FILE *fp
)
182 size_t printed
= fprintf(fp
, "%d cpu%s: ",
183 map
->nr
, map
->nr
> 1 ? "s" : "");
184 for (i
= 0; i
< map
->nr
; ++i
)
185 printed
+= fprintf(fp
, "%s%d", i
? ", " : "", map
->map
[i
]);
187 return printed
+ fprintf(fp
, "\n");
190 struct cpu_map
*cpu_map__dummy_new(void)
192 struct cpu_map
*cpus
= malloc(sizeof(*cpus
) + sizeof(int));
202 void cpu_map__delete(struct cpu_map
*map
)
207 int cpu_map__get_socket(struct cpu_map
*map
, int idx
)
219 mnt
= sysfs__mountpoint();
223 snprintf(path
, PATH_MAX
,
224 "%s/devices/system/cpu/cpu%d/topology/physical_package_id",
227 fp
= fopen(path
, "r");
230 ret
= fscanf(fp
, "%d", &cpu
);
232 return ret
== 1 ? cpu
: -1;
235 static int cmp_ids(const void *a
, const void *b
)
237 return *(int *)a
- *(int *)b
;
240 static int cpu_map__build_map(struct cpu_map
*cpus
, struct cpu_map
**res
,
241 int (*f
)(struct cpu_map
*map
, int cpu
))
247 /* allocate as much as possible */
248 c
= calloc(1, sizeof(*c
) + nr
* sizeof(int));
252 for (cpu
= 0; cpu
< nr
; cpu
++) {
254 for (s2
= 0; s2
< c
->nr
; s2
++) {
255 if (s1
== c
->map
[s2
])
263 /* ensure we process id in increasing order */
264 qsort(c
->map
, c
->nr
, sizeof(int), cmp_ids
);
270 int cpu_map__get_core(struct cpu_map
*map
, int idx
)
282 mnt
= sysfs__mountpoint();
286 snprintf(path
, PATH_MAX
,
287 "%s/devices/system/cpu/cpu%d/topology/core_id",
290 fp
= fopen(path
, "r");
293 ret
= fscanf(fp
, "%d", &cpu
);
298 s
= cpu_map__get_socket(map
, idx
);
303 * encode socket in upper 16 bits
304 * core_id is relative to socket, and
305 * we need a global id. So we combine
308 return (s
<< 16) | (cpu
& 0xffff);
311 int cpu_map__build_socket_map(struct cpu_map
*cpus
, struct cpu_map
**sockp
)
313 return cpu_map__build_map(cpus
, sockp
, cpu_map__get_socket
);
316 int cpu_map__build_core_map(struct cpu_map
*cpus
, struct cpu_map
**corep
)
318 return cpu_map__build_map(cpus
, corep
, cpu_map__get_core
);
321 /* setup simple routines to easily access node numbers given a cpu number */
322 static int get_max_num(char *path
, int *max
)
328 if (filename__read_str(path
, &buf
, &num
))
333 /* start on the right, to find highest node num */
335 if ((buf
[num
] == ',') || (buf
[num
] == '-')) {
340 if (sscanf(&buf
[num
], "%d", max
) < 1) {
345 /* convert from 0-based to 1-based */
353 /* Determine highest possible cpu in the system for sparse allocation */
354 static void set_max_cpu_num(void)
363 mnt
= sysfs__mountpoint();
367 /* get the highest possible cpu number for a sparse allocation */
368 ret
= snprintf(path
, PATH_MAX
, "%s/devices/system/cpu/possible", mnt
);
369 if (ret
== PATH_MAX
) {
370 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX
);
374 ret
= get_max_num(path
, &max_cpu_num
);
378 pr_err("Failed to read max cpus, using default of %d\n", max_cpu_num
);
381 /* Determine highest possible node in the system for sparse allocation */
382 static void set_max_node_num(void)
391 mnt
= sysfs__mountpoint();
395 /* get the highest possible cpu number for a sparse allocation */
396 ret
= snprintf(path
, PATH_MAX
, "%s/devices/system/node/possible", mnt
);
397 if (ret
== PATH_MAX
) {
398 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX
);
402 ret
= get_max_num(path
, &max_node_num
);
406 pr_err("Failed to read max nodes, using default of %d\n", max_node_num
);
409 static int init_cpunode_map(void)
416 cpunode_map
= calloc(max_cpu_num
, sizeof(int));
418 pr_err("%s: calloc failed\n", __func__
);
422 for (i
= 0; i
< max_cpu_num
; i
++)
428 int cpu__setup_cpunode_map(void)
430 struct dirent
*dent1
, *dent2
;
432 unsigned int cpu
, mem
;
438 /* initialize globals */
439 if (init_cpunode_map())
442 mnt
= sysfs__mountpoint();
446 n
= snprintf(path
, PATH_MAX
, "%s/devices/system/node", mnt
);
448 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX
);
452 dir1
= opendir(path
);
456 /* walk tree and setup map */
457 while ((dent1
= readdir(dir1
)) != NULL
) {
458 if (dent1
->d_type
!= DT_DIR
|| sscanf(dent1
->d_name
, "node%u", &mem
) < 1)
461 n
= snprintf(buf
, PATH_MAX
, "%s/%s", path
, dent1
->d_name
);
463 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX
);
470 while ((dent2
= readdir(dir2
)) != NULL
) {
471 if (dent2
->d_type
!= DT_LNK
|| sscanf(dent2
->d_name
, "cpu%u", &cpu
) < 1)
473 cpunode_map
[cpu
] = mem
;