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