xref: /linux/tools/perf/util/cpumap.h (revision fd6f57bfda7c36f2d465cee39d5d8c623db5d7aa)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __PERF_CPUMAP_H
3 #define __PERF_CPUMAP_H
4 
5 #include <stdbool.h>
6 #include <stdio.h>
7 #include <stdbool.h>
8 #include <internal/cpumap.h>
9 #include <perf/cpumap.h>
10 
11 /** Identify where counts are aggregated, -1 implies not to aggregate. */
12 struct aggr_cpu_id {
13 	/** A value in the range 0 to number of threads. */
14 	int thread;
15 	/** The numa node X as read from /sys/devices/system/node/nodeX. */
16 	int node;
17 	/**
18 	 * The socket number as read from
19 	 * /sys/devices/system/cpu/cpuX/topology/physical_package_id.
20 	 */
21 	int socket;
22 	/** The die id as read from /sys/devices/system/cpu/cpuX/topology/die_id. */
23 	int die;
24 	/** The core id as read from /sys/devices/system/cpu/cpuX/topology/core_id. */
25 	int core;
26 	/** CPU aggregation, note there is one CPU for each SMT thread. */
27 	struct perf_cpu cpu;
28 };
29 
30 /** A collection of aggr_cpu_id values, the "built" version is sorted and uniqued. */
31 struct cpu_aggr_map {
32 	refcount_t refcnt;
33 	/** Number of valid entries. */
34 	int nr;
35 	/** The entries. */
36 	struct aggr_cpu_id map[];
37 };
38 
39 struct perf_record_cpu_map_data;
40 
41 struct perf_cpu_map *perf_cpu_map__empty_new(int nr);
42 
43 struct perf_cpu_map *cpu_map__new_data(struct perf_record_cpu_map_data *data);
44 size_t cpu_map__snprint(struct perf_cpu_map *map, char *buf, size_t size);
45 size_t cpu_map__snprint_mask(struct perf_cpu_map *map, char *buf, size_t size);
46 size_t cpu_map__fprintf(struct perf_cpu_map *map, FILE *fp);
47 const struct perf_cpu_map *cpu_map__online(void); /* thread unsafe */
48 
49 int cpu__setup_cpunode_map(void);
50 
51 int cpu__max_node(void);
52 struct perf_cpu cpu__max_cpu(void);
53 struct perf_cpu cpu__max_present_cpu(void);
54 
55 /**
56  * cpu_map__is_dummy - Events associated with a pid, rather than a CPU, use a single dummy map with an entry of -1.
57  */
58 static inline bool cpu_map__is_dummy(struct perf_cpu_map *cpus)
59 {
60 	return cpus->nr == 1 && cpus->map[0].cpu == -1;
61 }
62 
63 /**
64  * cpu__get_node - Returns the numa node X as read from
65  * /sys/devices/system/node/nodeX for the given CPU.
66  */
67 int cpu__get_node(struct perf_cpu cpu);
68 /**
69  * cpu__get_socket_id - Returns the socket number as read from
70  * /sys/devices/system/cpu/cpuX/topology/physical_package_id for the given CPU.
71  */
72 int cpu__get_socket_id(struct perf_cpu cpu);
73 /**
74  * cpu__get_die_id - Returns the die id as read from
75  * /sys/devices/system/cpu/cpuX/topology/die_id for the given CPU.
76  */
77 int cpu__get_die_id(struct perf_cpu cpu);
78 /**
79  * cpu__get_core_id - Returns the core id as read from
80  * /sys/devices/system/cpu/cpuX/topology/core_id for the given CPU.
81  */
82 int cpu__get_core_id(struct perf_cpu cpu);
83 
84 /**
85  * cpu_aggr_map__empty_new - Create a cpu_aggr_map of size nr with every entry
86  * being empty.
87  */
88 struct cpu_aggr_map *cpu_aggr_map__empty_new(int nr);
89 
90 typedef struct aggr_cpu_id (*aggr_cpu_id_get_t)(struct perf_cpu cpu, void *data);
91 
92 /**
93  * cpu_aggr_map__new - Create a cpu_aggr_map with an aggr_cpu_id for each cpu in
94  * cpus. The aggr_cpu_id is created with 'get_id' that may have a data value
95  * passed to it. The cpu_aggr_map is sorted with duplicate values removed.
96  */
97 struct cpu_aggr_map *cpu_aggr_map__new(const struct perf_cpu_map *cpus,
98 				       aggr_cpu_id_get_t get_id,
99 				       void *data);
100 
101 bool aggr_cpu_id__equal(const struct aggr_cpu_id *a, const struct aggr_cpu_id *b);
102 bool aggr_cpu_id__is_empty(const struct aggr_cpu_id *a);
103 struct aggr_cpu_id aggr_cpu_id__empty(void);
104 
105 
106 /**
107  * aggr_cpu_id__socket - Create an aggr_cpu_id with the socket populated with
108  * the socket for cpu. The function signature is compatible with
109  * aggr_cpu_id_get_t.
110  */
111 struct aggr_cpu_id aggr_cpu_id__socket(struct perf_cpu cpu, void *data);
112 /**
113  * aggr_cpu_id__die - Create an aggr_cpu_id with the die and socket populated
114  * with the die and socket for cpu. The function signature is compatible with
115  * aggr_cpu_id_get_t.
116  */
117 struct aggr_cpu_id aggr_cpu_id__die(struct perf_cpu cpu, void *data);
118 /**
119  * aggr_cpu_id__core - Create an aggr_cpu_id with the core, die and socket
120  * populated with the core, die and socket for cpu. The function signature is
121  * compatible with aggr_cpu_id_get_t.
122  */
123 struct aggr_cpu_id aggr_cpu_id__core(struct perf_cpu cpu, void *data);
124 /**
125  * aggr_cpu_id__core - Create an aggr_cpu_id with the cpu, core, die and socket
126  * populated with the cpu, core, die and socket for cpu. The function signature
127  * is compatible with aggr_cpu_id_get_t.
128  */
129 struct aggr_cpu_id aggr_cpu_id__cpu(struct perf_cpu cpu, void *data);
130 /**
131  * aggr_cpu_id__node - Create an aggr_cpu_id with the numa node populated for
132  * cpu. The function signature is compatible with aggr_cpu_id_get_t.
133  */
134 struct aggr_cpu_id aggr_cpu_id__node(struct perf_cpu cpu, void *data);
135 
136 #endif /* __PERF_CPUMAP_H */
137