xref: /linux/tools/perf/util/cpumap.h (revision 96ac6d435100450f0565708d9b885ea2a7400e0a)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __PERF_CPUMAP_H
3 #define __PERF_CPUMAP_H
4 
5 #include <stdio.h>
6 #include <stdbool.h>
7 #include <linux/refcount.h>
8 
9 #include "perf.h"
10 #include "util/debug.h"
11 
12 struct cpu_map {
13 	refcount_t refcnt;
14 	int nr;
15 	int map[];
16 };
17 
18 struct cpu_map *cpu_map__new(const char *cpu_list);
19 struct cpu_map *cpu_map__empty_new(int nr);
20 struct cpu_map *cpu_map__dummy_new(void);
21 struct cpu_map *cpu_map__new_data(struct cpu_map_data *data);
22 struct cpu_map *cpu_map__read(FILE *file);
23 size_t cpu_map__snprint(struct cpu_map *map, char *buf, size_t size);
24 size_t cpu_map__snprint_mask(struct cpu_map *map, char *buf, size_t size);
25 size_t cpu_map__fprintf(struct cpu_map *map, FILE *fp);
26 int cpu_map__get_socket_id(int cpu);
27 int cpu_map__get_socket(struct cpu_map *map, int idx, void *data);
28 int cpu_map__get_core_id(int cpu);
29 int cpu_map__get_core(struct cpu_map *map, int idx, void *data);
30 int cpu_map__build_socket_map(struct cpu_map *cpus, struct cpu_map **sockp);
31 int cpu_map__build_core_map(struct cpu_map *cpus, struct cpu_map **corep);
32 const struct cpu_map *cpu_map__online(void); /* thread unsafe */
33 
34 struct cpu_map *cpu_map__get(struct cpu_map *map);
35 void cpu_map__put(struct cpu_map *map);
36 
37 static inline int cpu_map__socket(struct cpu_map *sock, int s)
38 {
39 	if (!sock || s > sock->nr || s < 0)
40 		return 0;
41 	return sock->map[s];
42 }
43 
44 static inline int cpu_map__id_to_socket(int id)
45 {
46 	return id >> 16;
47 }
48 
49 static inline int cpu_map__id_to_cpu(int id)
50 {
51 	return id & 0xffff;
52 }
53 
54 static inline int cpu_map__nr(const struct cpu_map *map)
55 {
56 	return map ? map->nr : 1;
57 }
58 
59 static inline bool cpu_map__empty(const struct cpu_map *map)
60 {
61 	return map ? map->map[0] == -1 : true;
62 }
63 
64 int cpu__setup_cpunode_map(void);
65 
66 int cpu__max_node(void);
67 int cpu__max_cpu(void);
68 int cpu__max_present_cpu(void);
69 int cpu__get_node(int cpu);
70 
71 int cpu_map__build_map(struct cpu_map *cpus, struct cpu_map **res,
72 		       int (*f)(struct cpu_map *map, int cpu, void *data),
73 		       void *data);
74 
75 int cpu_map__cpu(struct cpu_map *cpus, int idx);
76 bool cpu_map__has(struct cpu_map *cpus, int cpu);
77 int cpu_map__idx(struct cpu_map *cpus, int cpu);
78 #endif /* __PERF_CPUMAP_H */
79