]>
git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - tools/perf/util/cpumap.c
9 #include <linux/bitmap.h>
12 #include "sane_ctype.h"
14 static int max_cpu_num
;
15 static int max_present_cpu_num
;
16 static int max_node_num
;
17 static int *cpunode_map
;
19 static struct cpu_map
*cpu_map__default_new(void)
24 nr_cpus
= sysconf(_SC_NPROCESSORS_ONLN
);
28 cpus
= malloc(sizeof(*cpus
) + nr_cpus
* sizeof(int));
31 for (i
= 0; i
< nr_cpus
; ++i
)
35 refcount_set(&cpus
->refcnt
, 1);
41 static struct cpu_map
*cpu_map__trim_new(int nr_cpus
, int *tmp_cpus
)
43 size_t payload_size
= nr_cpus
* sizeof(int);
44 struct cpu_map
*cpus
= malloc(sizeof(*cpus
) + payload_size
);
48 memcpy(cpus
->map
, tmp_cpus
, payload_size
);
49 refcount_set(&cpus
->refcnt
, 1);
55 struct cpu_map
*cpu_map__read(FILE *file
)
57 struct cpu_map
*cpus
= NULL
;
59 int *tmp_cpus
= NULL
, *tmp
;
67 n
= fscanf(file
, "%u%c", &cpu
, &sep
);
71 int new_max
= nr_cpus
+ cpu
- prev
- 1;
73 if (new_max
>= max_entries
) {
74 max_entries
= new_max
+ MAX_NR_CPUS
/ 2;
75 tmp
= realloc(tmp_cpus
, max_entries
* sizeof(int));
82 tmp_cpus
[nr_cpus
++] = prev
;
84 if (nr_cpus
== max_entries
) {
85 max_entries
+= MAX_NR_CPUS
;
86 tmp
= realloc(tmp_cpus
, max_entries
* sizeof(int));
92 tmp_cpus
[nr_cpus
++] = cpu
;
93 if (n
== 2 && sep
== '-')
97 if (n
== 1 || sep
== '\n')
102 cpus
= cpu_map__trim_new(nr_cpus
, tmp_cpus
);
104 cpus
= cpu_map__default_new();
110 static struct cpu_map
*cpu_map__read_all_cpu_map(void)
112 struct cpu_map
*cpus
= NULL
;
115 onlnf
= fopen("/sys/devices/system/cpu/online", "r");
117 return cpu_map__default_new();
119 cpus
= cpu_map__read(onlnf
);
124 struct cpu_map
*cpu_map__new(const char *cpu_list
)
126 struct cpu_map
*cpus
= NULL
;
127 unsigned long start_cpu
, end_cpu
= 0;
130 int *tmp_cpus
= NULL
, *tmp
;
134 return cpu_map__read_all_cpu_map();
136 if (!isdigit(*cpu_list
))
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
);
184 cpus
= cpu_map__default_new();
191 static struct cpu_map
*cpu_map__from_entries(struct cpu_map_entries
*cpus
)
195 map
= cpu_map__empty_new(cpus
->nr
);
199 for (i
= 0; i
< cpus
->nr
; i
++) {
201 * Special treatment for -1, which is not real cpu number,
202 * and we need to use (int) -1 to initialize map[i],
203 * otherwise it would become 65535.
205 if (cpus
->cpu
[i
] == (u16
) -1)
208 map
->map
[i
] = (int) cpus
->cpu
[i
];
215 static struct cpu_map
*cpu_map__from_mask(struct cpu_map_mask
*mask
)
218 int nr
, nbits
= mask
->nr
* mask
->long_size
* BITS_PER_BYTE
;
220 nr
= bitmap_weight(mask
->mask
, nbits
);
222 map
= cpu_map__empty_new(nr
);
226 for_each_set_bit(cpu
, mask
->mask
, nbits
)
233 struct cpu_map
*cpu_map__new_data(struct cpu_map_data
*data
)
235 if (data
->type
== PERF_CPU_MAP__CPUS
)
236 return cpu_map__from_entries((struct cpu_map_entries
*)data
->data
);
238 return cpu_map__from_mask((struct cpu_map_mask
*)data
->data
);
241 size_t cpu_map__fprintf(struct cpu_map
*map
, FILE *fp
)
246 cpu_map__snprint(map
, buf
, sizeof(buf
));
247 return fprintf(fp
, "%s\n", buf
);
251 struct cpu_map
*cpu_map__dummy_new(void)
253 struct cpu_map
*cpus
= malloc(sizeof(*cpus
) + sizeof(int));
258 refcount_set(&cpus
->refcnt
, 1);
264 struct cpu_map
*cpu_map__empty_new(int nr
)
266 struct cpu_map
*cpus
= malloc(sizeof(*cpus
) + sizeof(int) * nr
);
272 for (i
= 0; i
< nr
; i
++)
275 refcount_set(&cpus
->refcnt
, 1);
281 static void cpu_map__delete(struct cpu_map
*map
)
284 WARN_ONCE(refcount_read(&map
->refcnt
) != 0,
285 "cpu_map refcnt unbalanced\n");
290 struct cpu_map
*cpu_map__get(struct cpu_map
*map
)
293 refcount_inc(&map
->refcnt
);
297 void cpu_map__put(struct cpu_map
*map
)
299 if (map
&& refcount_dec_and_test(&map
->refcnt
))
300 cpu_map__delete(map
);
303 static int cpu__get_topology_int(int cpu
, const char *name
, int *value
)
307 snprintf(path
, PATH_MAX
,
308 "devices/system/cpu/cpu%d/topology/%s", cpu
, name
);
310 return sysfs__read_int(path
, value
);
313 int cpu_map__get_socket_id(int cpu
)
315 int value
, ret
= cpu__get_topology_int(cpu
, "physical_package_id", &value
);
319 int cpu_map__get_socket(struct cpu_map
*map
, int idx
, void *data __maybe_unused
)
328 return cpu_map__get_socket_id(cpu
);
331 static int cmp_ids(const void *a
, const void *b
)
333 return *(int *)a
- *(int *)b
;
336 int cpu_map__build_map(struct cpu_map
*cpus
, struct cpu_map
**res
,
337 int (*f
)(struct cpu_map
*map
, int cpu
, void *data
),
344 /* allocate as much as possible */
345 c
= calloc(1, sizeof(*c
) + nr
* sizeof(int));
349 for (cpu
= 0; cpu
< nr
; cpu
++) {
350 s1
= f(cpus
, cpu
, data
);
351 for (s2
= 0; s2
< c
->nr
; s2
++) {
352 if (s1
== c
->map
[s2
])
360 /* ensure we process id in increasing order */
361 qsort(c
->map
, c
->nr
, sizeof(int), cmp_ids
);
363 refcount_set(&c
->refcnt
, 1);
368 int cpu_map__get_core_id(int cpu
)
370 int value
, ret
= cpu__get_topology_int(cpu
, "core_id", &value
);
374 int cpu_map__get_core(struct cpu_map
*map
, int idx
, void *data
)
383 cpu
= cpu_map__get_core_id(cpu
);
385 s
= cpu_map__get_socket(map
, idx
, data
);
390 * encode socket in upper 16 bits
391 * core_id is relative to socket, and
392 * we need a global id. So we combine
395 return (s
<< 16) | (cpu
& 0xffff);
398 int cpu_map__build_socket_map(struct cpu_map
*cpus
, struct cpu_map
**sockp
)
400 return cpu_map__build_map(cpus
, sockp
, cpu_map__get_socket
, NULL
);
403 int cpu_map__build_core_map(struct cpu_map
*cpus
, struct cpu_map
**corep
)
405 return cpu_map__build_map(cpus
, corep
, cpu_map__get_core
, NULL
);
408 /* setup simple routines to easily access node numbers given a cpu number */
409 static int get_max_num(char *path
, int *max
)
415 if (filename__read_str(path
, &buf
, &num
))
420 /* start on the right, to find highest node num */
422 if ((buf
[num
] == ',') || (buf
[num
] == '-')) {
427 if (sscanf(&buf
[num
], "%d", max
) < 1) {
432 /* convert from 0-based to 1-based */
440 /* Determine highest possible cpu in the system for sparse allocation */
441 static void set_max_cpu_num(void)
449 max_present_cpu_num
= 4096;
451 mnt
= sysfs__mountpoint();
455 /* get the highest possible cpu number for a sparse allocation */
456 ret
= snprintf(path
, PATH_MAX
, "%s/devices/system/cpu/possible", mnt
);
457 if (ret
== PATH_MAX
) {
458 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX
);
462 ret
= get_max_num(path
, &max_cpu_num
);
466 /* get the highest present cpu number for a sparse allocation */
467 ret
= snprintf(path
, PATH_MAX
, "%s/devices/system/cpu/present", mnt
);
468 if (ret
== PATH_MAX
) {
469 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX
);
473 ret
= get_max_num(path
, &max_present_cpu_num
);
477 pr_err("Failed to read max cpus, using default of %d\n", max_cpu_num
);
480 /* Determine highest possible node in the system for sparse allocation */
481 static void set_max_node_num(void)
490 mnt
= sysfs__mountpoint();
494 /* get the highest possible cpu number for a sparse allocation */
495 ret
= snprintf(path
, PATH_MAX
, "%s/devices/system/node/possible", mnt
);
496 if (ret
== PATH_MAX
) {
497 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX
);
501 ret
= get_max_num(path
, &max_node_num
);
505 pr_err("Failed to read max nodes, using default of %d\n", max_node_num
);
508 int cpu__max_node(void)
510 if (unlikely(!max_node_num
))
516 int cpu__max_cpu(void)
518 if (unlikely(!max_cpu_num
))
524 int cpu__max_present_cpu(void)
526 if (unlikely(!max_present_cpu_num
))
529 return max_present_cpu_num
;
533 int cpu__get_node(int cpu
)
535 if (unlikely(cpunode_map
== NULL
)) {
536 pr_debug("cpu_map not initialized\n");
540 return cpunode_map
[cpu
];
543 static int init_cpunode_map(void)
550 cpunode_map
= calloc(max_cpu_num
, sizeof(int));
552 pr_err("%s: calloc failed\n", __func__
);
556 for (i
= 0; i
< max_cpu_num
; i
++)
562 int cpu__setup_cpunode_map(void)
564 struct dirent
*dent1
, *dent2
;
566 unsigned int cpu
, mem
;
572 /* initialize globals */
573 if (init_cpunode_map())
576 mnt
= sysfs__mountpoint();
580 n
= snprintf(path
, PATH_MAX
, "%s/devices/system/node", mnt
);
582 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX
);
586 dir1
= opendir(path
);
590 /* walk tree and setup map */
591 while ((dent1
= readdir(dir1
)) != NULL
) {
592 if (dent1
->d_type
!= DT_DIR
|| sscanf(dent1
->d_name
, "node%u", &mem
) < 1)
595 n
= snprintf(buf
, PATH_MAX
, "%s/%s", path
, dent1
->d_name
);
597 pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX
);
604 while ((dent2
= readdir(dir2
)) != NULL
) {
605 if (dent2
->d_type
!= DT_LNK
|| sscanf(dent2
->d_name
, "cpu%u", &cpu
) < 1)
607 cpunode_map
[cpu
] = mem
;
615 bool cpu_map__has(struct cpu_map
*cpus
, int cpu
)
617 return cpu_map__idx(cpus
, cpu
) != -1;
620 int cpu_map__idx(struct cpu_map
*cpus
, int cpu
)
624 for (i
= 0; i
< cpus
->nr
; ++i
) {
625 if (cpus
->map
[i
] == cpu
)
632 int cpu_map__cpu(struct cpu_map
*cpus
, int idx
)
634 return cpus
->map
[idx
];
637 size_t cpu_map__snprint(struct cpu_map
*map
, char *buf
, size_t size
)
639 int i
, cpu
, start
= -1;
643 #define COMMA first ? "" : ","
645 for (i
= 0; i
< map
->nr
+ 1; i
++) {
646 bool last
= i
== map
->nr
;
648 cpu
= last
? INT_MAX
: map
->map
[i
];
653 ret
+= snprintf(buf
+ ret
, size
- ret
,
657 } else if (((i
- start
) != (cpu
- map
->map
[start
])) || last
) {
661 ret
+= snprintf(buf
+ ret
, size
- ret
,
665 ret
+= snprintf(buf
+ ret
, size
- ret
,
667 map
->map
[start
], map
->map
[end
]);
676 pr_debug("cpumask list: %s\n", buf
);
680 static char hex_char(unsigned char val
)
685 return val
- 10 + 'a';
689 size_t cpu_map__snprint_mask(struct cpu_map
*map
, char *buf
, size_t size
)
693 unsigned char *bitmap
;
694 int last_cpu
= cpu_map__cpu(map
, map
->nr
- 1);
696 bitmap
= zalloc((last_cpu
+ 7) / 8);
697 if (bitmap
== NULL
) {
702 for (i
= 0; i
< map
->nr
; i
++) {
703 cpu
= cpu_map__cpu(map
, i
);
704 bitmap
[cpu
/ 8] |= 1 << (cpu
% 8);
707 for (cpu
= last_cpu
/ 4 * 4; cpu
>= 0; cpu
-= 4) {
708 unsigned char bits
= bitmap
[cpu
/ 8];
715 *ptr
++ = hex_char(bits
);
716 if ((cpu
% 32) == 0 && cpu
> 0)
722 buf
[size
- 1] = '\0';