8 #include <linux/bitmap.h>
11 static struct cpu_map
*cpu_map__default_new(void)
16 nr_cpus
= sysconf(_SC_NPROCESSORS_ONLN
);
20 cpus
= malloc(sizeof(*cpus
) + nr_cpus
* sizeof(int));
23 for (i
= 0; i
< nr_cpus
; ++i
)
27 atomic_set(&cpus
->refcnt
, 1);
33 static struct cpu_map
*cpu_map__trim_new(int nr_cpus
, int *tmp_cpus
)
35 size_t payload_size
= nr_cpus
* sizeof(int);
36 struct cpu_map
*cpus
= malloc(sizeof(*cpus
) + payload_size
);
40 memcpy(cpus
->map
, tmp_cpus
, payload_size
);
41 atomic_set(&cpus
->refcnt
, 1);
47 struct cpu_map
*cpu_map__read(FILE *file
)
49 struct cpu_map
*cpus
= NULL
;
51 int *tmp_cpus
= NULL
, *tmp
;
59 n
= fscanf(file
, "%u%c", &cpu
, &sep
);
63 int new_max
= nr_cpus
+ cpu
- prev
- 1;
65 if (new_max
>= max_entries
) {
66 max_entries
= new_max
+ MAX_NR_CPUS
/ 2;
67 tmp
= realloc(tmp_cpus
, max_entries
* sizeof(int));
74 tmp_cpus
[nr_cpus
++] = prev
;
76 if (nr_cpus
== max_entries
) {
77 max_entries
+= MAX_NR_CPUS
;
78 tmp
= realloc(tmp_cpus
, max_entries
* sizeof(int));
84 tmp_cpus
[nr_cpus
++] = cpu
;
85 if (n
== 2 && sep
== '-')
89 if (n
== 1 || sep
== '\n')
94 cpus
= cpu_map__trim_new(nr_cpus
, tmp_cpus
);
96 cpus
= cpu_map__default_new();
102 static struct cpu_map
*cpu_map__read_all_cpu_map(void)
104 struct cpu_map
*cpus
= NULL
;
107 onlnf
= fopen("/sys/devices/system/cpu/online", "r");
109 return cpu_map__default_new();
111 cpus
= cpu_map__read(onlnf
);
116 struct cpu_map
*cpu_map__new(const char *cpu_list
)
118 struct cpu_map
*cpus
= NULL
;
119 unsigned long start_cpu
, end_cpu
= 0;
122 int *tmp_cpus
= NULL
, *tmp
;
126 return cpu_map__read_all_cpu_map();
128 if (!isdigit(*cpu_list
))
131 while (isdigit(*cpu_list
)) {
133 start_cpu
= strtoul(cpu_list
, &p
, 0);
134 if (start_cpu
>= INT_MAX
135 || (*p
!= '\0' && *p
!= ',' && *p
!= '-'))
141 end_cpu
= strtoul(cpu_list
, &p
, 0);
143 if (end_cpu
>= INT_MAX
|| (*p
!= '\0' && *p
!= ','))
146 if (end_cpu
< start_cpu
)
152 for (; start_cpu
<= end_cpu
; start_cpu
++) {
153 /* check for duplicates */
154 for (i
= 0; i
< nr_cpus
; i
++)
155 if (tmp_cpus
[i
] == (int)start_cpu
)
158 if (nr_cpus
== max_entries
) {
159 max_entries
+= MAX_NR_CPUS
;
160 tmp
= realloc(tmp_cpus
, max_entries
* sizeof(int));
165 tmp_cpus
[nr_cpus
++] = (int)start_cpu
;
174 cpus
= cpu_map__trim_new(nr_cpus
, tmp_cpus
);
176 cpus
= cpu_map__default_new();
183 static struct cpu_map
*cpu_map__from_entries(struct cpu_map_entries
*cpus
)
187 map
= cpu_map__empty_new(cpus
->nr
);
191 for (i
= 0; i
< cpus
->nr
; i
++) {
193 * Special treatment for -1, which is not real cpu number,
194 * and we need to use (int) -1 to initialize map[i],
195 * otherwise it would become 65535.
197 if (cpus
->cpu
[i
] == (u16
) -1)
200 map
->map
[i
] = (int) cpus
->cpu
[i
];
207 static struct cpu_map
*cpu_map__from_mask(struct cpu_map_mask
*mask
)
210 int nr
, nbits
= mask
->nr
* mask
->long_size
* BITS_PER_BYTE
;
212 nr
= bitmap_weight(mask
->mask
, nbits
);
214 map
= cpu_map__empty_new(nr
);
218 for_each_set_bit(cpu
, mask
->mask
, nbits
)
225 struct cpu_map
*cpu_map__new_data(struct cpu_map_data
*data
)
227 if (data
->type
== PERF_CPU_MAP__CPUS
)
228 return cpu_map__from_entries((struct cpu_map_entries
*)data
->data
);
230 return cpu_map__from_mask((struct cpu_map_mask
*)data
->data
);
233 size_t cpu_map__fprintf(struct cpu_map
*map
, FILE *fp
)
236 size_t printed
= fprintf(fp
, "%d cpu%s: ",
237 map
->nr
, map
->nr
> 1 ? "s" : "");
238 for (i
= 0; i
< map
->nr
; ++i
)
239 printed
+= fprintf(fp
, "%s%d", i
? ", " : "", map
->map
[i
]);
241 return printed
+ fprintf(fp
, "\n");
244 struct cpu_map
*cpu_map__dummy_new(void)
246 struct cpu_map
*cpus
= malloc(sizeof(*cpus
) + sizeof(int));
251 atomic_set(&cpus
->refcnt
, 1);
257 struct cpu_map
*cpu_map__empty_new(int nr
)
259 struct cpu_map
*cpus
= malloc(sizeof(*cpus
) + sizeof(int) * nr
);
265 for (i
= 0; i
< nr
; i
++)
268 atomic_set(&cpus
->refcnt
, 1);
274 static void cpu_map__delete(struct cpu_map
*map
)
277 WARN_ONCE(atomic_read(&map
->refcnt
) != 0,
278 "cpu_map refcnt unbalanced\n");
283 struct cpu_map
*cpu_map__get(struct cpu_map
*map
)
286 atomic_inc(&map
->refcnt
);
290 void cpu_map__put(struct cpu_map
*map
)
292 if (map
&& atomic_dec_and_test(&map
->refcnt
))
293 cpu_map__delete(map
);
296 static int cpu__get_topology_int(int cpu
, const char *name
, int *value
)
300 snprintf(path
, PATH_MAX
,
301 "devices/system/cpu/cpu%d/topology/%s", cpu
, name
);
303 return sysfs__read_int(path
, value
);
306 int cpu_map__get_socket_id(int cpu
)
308 int value
, ret
= cpu__get_topology_int(cpu
, "physical_package_id", &value
);
312 int cpu_map__get_socket(struct cpu_map
*map
, int idx
, void *data __maybe_unused
)
321 return cpu_map__get_socket_id(cpu
);
324 static int cmp_ids(const void *a
, const void *b
)
326 return *(int *)a
- *(int *)b
;
329 int cpu_map__build_map(struct cpu_map
*cpus
, struct cpu_map
**res
,
330 int (*f
)(struct cpu_map
*map
, int cpu
, void *data
),
337 /* allocate as much as possible */
338 c
= calloc(1, sizeof(*c
) + nr
* sizeof(int));
342 for (cpu
= 0; cpu
< nr
; cpu
++) {
343 s1
= f(cpus
, cpu
, data
);
344 for (s2
= 0; s2
< c
->nr
; s2
++) {
345 if (s1
== c
->map
[s2
])
353 /* ensure we process id in increasing order */
354 qsort(c
->map
, c
->nr
, sizeof(int), cmp_ids
);
356 atomic_set(&c
->refcnt
, 1);
361 int cpu_map__get_core_id(int cpu
)
363 int value
, ret
= cpu__get_topology_int(cpu
, "core_id", &value
);
367 int cpu_map__get_core(struct cpu_map
*map
, int idx
, void *data
)
376 cpu
= cpu_map__get_core_id(cpu
);
378 s
= cpu_map__get_socket(map
, idx
, data
);
383 * encode socket in upper 16 bits
384 * core_id is relative to socket, and
385 * we need a global id. So we combine
388 return (s
<< 16) | (cpu
& 0xffff);
391 int cpu_map__build_socket_map(struct cpu_map
*cpus
, struct cpu_map
**sockp
)
393 return cpu_map__build_map(cpus
, sockp
, cpu_map__get_socket
, NULL
);
396 int cpu_map__build_core_map(struct cpu_map
*cpus
, struct cpu_map
**corep
)
398 return cpu_map__build_map(cpus
, corep
, cpu_map__get_core
, NULL
);
401 /* setup simple routines to easily access node numbers given a cpu number */
402 static int get_max_num(char *path
, int *max
)
408 if (filename__read_str(path
, &buf
, &num
))
413 /* start on the right, to find highest node num */
415 if ((buf
[num
] == ',') || (buf
[num
] == '-')) {
420 if (sscanf(&buf
[num
], "%d", max
) < 1) {
425 /* convert from 0-based to 1-based */
433 /* Determine highest possible cpu in the system for sparse allocation */
434 static void set_max_cpu_num(void)
443 mnt
= sysfs__mountpoint();
447 /* get the highest possible cpu number for a sparse allocation */
448 ret
= snprintf(path
, PATH_MAX
, "%s/devices/system/cpu/possible", mnt
);
449 if (ret
== PATH_MAX
) {
450 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX
);
454 ret
= get_max_num(path
, &max_cpu_num
);
458 pr_err("Failed to read max cpus, using default of %d\n", max_cpu_num
);
461 /* Determine highest possible node in the system for sparse allocation */
462 static void set_max_node_num(void)
471 mnt
= sysfs__mountpoint();
475 /* get the highest possible cpu number for a sparse allocation */
476 ret
= snprintf(path
, PATH_MAX
, "%s/devices/system/node/possible", mnt
);
477 if (ret
== PATH_MAX
) {
478 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX
);
482 ret
= get_max_num(path
, &max_node_num
);
486 pr_err("Failed to read max nodes, using default of %d\n", max_node_num
);
489 static int init_cpunode_map(void)
496 cpunode_map
= calloc(max_cpu_num
, sizeof(int));
498 pr_err("%s: calloc failed\n", __func__
);
502 for (i
= 0; i
< max_cpu_num
; i
++)
508 int cpu__setup_cpunode_map(void)
510 struct dirent
*dent1
, *dent2
;
512 unsigned int cpu
, mem
;
518 /* initialize globals */
519 if (init_cpunode_map())
522 mnt
= sysfs__mountpoint();
526 n
= snprintf(path
, PATH_MAX
, "%s/devices/system/node", mnt
);
528 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX
);
532 dir1
= opendir(path
);
536 /* walk tree and setup map */
537 while ((dent1
= readdir(dir1
)) != NULL
) {
538 if (dent1
->d_type
!= DT_DIR
|| sscanf(dent1
->d_name
, "node%u", &mem
) < 1)
541 n
= snprintf(buf
, PATH_MAX
, "%s/%s", path
, dent1
->d_name
);
543 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX
);
550 while ((dent2
= readdir(dir2
)) != NULL
) {
551 if (dent2
->d_type
!= DT_LNK
|| sscanf(dent2
->d_name
, "cpu%u", &cpu
) < 1)
553 cpunode_map
[cpu
] = mem
;