xref: /linux/tools/perf/util/env.h (revision 6cdecad561fffb7c44241ad35a8bdb6a0ed30b5b)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __PERF_ENV_H
3 #define __PERF_ENV_H
4 
5 #include <linux/types.h>
6 #include <linux/rbtree.h>
7 #include "cpumap.h"
8 #include "rwsem.h"
9 #include "mutex.h"
10 
11 struct perf_cpu_map;
12 
13 struct cpu_topology_map {
14 	int	socket_id;
15 	int	die_id;
16 	int	cluster_id;
17 	int	core_id;
18 };
19 
20 struct cpu_cache_level {
21 	u32	level;
22 	u32	line_size;
23 	u32	sets;
24 	u32	ways;
25 	char	*type;
26 	char	*size;
27 	char	*map;
28 };
29 
30 struct numa_node {
31 	u32		 node;
32 	u64		 mem_total;
33 	u64		 mem_free;
34 	struct perf_cpu_map	*map;
35 };
36 
37 struct memory_node {
38 	u64		 node;
39 	u64		 size;
40 	unsigned long	*set;
41 };
42 
43 struct hybrid_node {
44 	char	*pmu_name;
45 	char	*cpus;
46 };
47 
48 struct pmu_caps {
49 	int		nr_caps;
50 	unsigned int    max_branches;
51 	unsigned int	br_cntr_nr;
52 	unsigned int	br_cntr_width;
53 
54 	char            **caps;
55 	char            *pmu_name;
56 };
57 
58 struct domain_info {
59 	u32	domain;
60 	char	*dname;
61 	char	*cpumask;
62 	char	*cpulist;
63 };
64 
65 struct cpu_domain_map {
66 	u32			cpu;
67 	u32			nr_domains;
68 	struct domain_info	**domains;
69 };
70 
71 struct memory_range {
72 	u64 base;
73 	u64 length;
74 	int node;
75 	u8 local_region_id;
76 	u8 remote_region_id;
77 };
78 
79 struct perf_env {
80 	char			*hostname;
81 	char			*os_release;
82 	char			*version;
83 	char			*arch;
84 	/* e_machine expanded from 16 to 32-bits for alignment. */
85 	u32			e_machine;
86 	u32			e_flags;
87 	int			nr_cpus_online;
88 	int			nr_cpus_avail;
89 	char			*cpu_desc;
90 	char			*cpuid;
91 	unsigned long long	total_mem;
92 	unsigned int		msr_pmu_type;
93 	unsigned int		max_branches;
94 	unsigned int		br_cntr_nr;
95 	unsigned int		br_cntr_width;
96 	unsigned int		schedstat_version;
97 	unsigned int		max_sched_domains;
98 	int			kernel_is_64_bit;
99 
100 	int			nr_cmdline;
101 	int			nr_sibling_cores;
102 	int			nr_sibling_dies;
103 	int			nr_sibling_threads;
104 	int			nr_numa_nodes;
105 	int			nr_memory_nodes;
106 	int			nr_pmu_mappings;
107 	int			nr_groups;
108 	int			nr_cpu_pmu_caps;
109 	int			nr_hybrid_nodes;
110 	int			nr_pmus_with_caps;
111 	int			nr_memory_ranges;
112 	char			*cmdline;
113 	const char		**cmdline_argv;
114 	char			*sibling_cores;
115 	char			*sibling_dies;
116 	char			*sibling_threads;
117 	char			*pmu_mappings;
118 	char			**cpu_pmu_caps;
119 	struct cpu_topology_map	*cpu;
120 	struct cpu_cache_level	*caches;
121 	struct cpu_domain_map	**cpu_domain;
122 	int			 caches_cnt;
123 	unsigned int		cln_size;
124 	u32			comp_ratio;
125 	u32			comp_ver;
126 	u32			comp_type;
127 	u32			comp_level;
128 	u32			comp_mmap_len;
129 	struct numa_node	*numa_nodes;
130 	struct memory_node	*memory_nodes;
131 	unsigned long long	 memory_bsize;
132 	struct hybrid_node	*hybrid_nodes;
133 	struct pmu_caps		*pmu_caps;
134 	struct memory_range	*memory_ranges;
135 #ifdef HAVE_LIBBPF_SUPPORT
136 	/*
137 	 * bpf_info_lock protects bpf rbtrees. This is needed because the
138 	 * trees are accessed by different threads in perf-top
139 	 */
140 	struct {
141 		struct rw_semaphore	lock;
142 		struct rb_root		infos;
143 		u32			infos_cnt;
144 		struct rb_root		btfs;
145 		u32			btfs_cnt;
146 	} bpf_progs;
147 #endif // HAVE_LIBBPF_SUPPORT
148 	/* same reason as above (for perf-top) */
149 	struct {
150 		struct rw_semaphore	lock;
151 		struct rb_root		tree;
152 	} cgroups;
153 
154 	/* For fast cpu to numa node lookup via perf_env__numa_node */
155 	int			*numa_map;
156 	int			 nr_numa_map;
157 
158 	/* For real clock time reference. */
159 	struct {
160 		u64	tod_ns;
161 		u64	clockid_ns;
162 		u64     clockid_res_ns;
163 		int	clockid;
164 		/*
165 		 * enabled is valid for report mode, and is true if above
166 		 * values are set, it's set in process_clock_data
167 		 */
168 		bool	enabled;
169 	} clock;
170 	/* Protects lazy environment initialization (e.g. os_release, e_machine). */
171 	struct mutex		lock;
172 };
173 
174 enum perf_compress_type {
175 	PERF_COMP_NONE = 0,
176 	PERF_COMP_ZSTD,
177 	PERF_COMP_MAX
178 };
179 
180 struct bpf_prog_info_node;
181 struct btf_node;
182 
183 int perf_env__read_core_pmu_caps(struct perf_env *env);
184 void free_cpu_domain_info(struct cpu_domain_map **cd_map, u32 schedstat_version, u32 nr);
185 void perf_env__exit(struct perf_env *env);
186 
187 int perf_env__kernel_is_64_bit(struct perf_env *env);
188 bool perf_arch_is_big_endian(const char *arch);
189 const char *perf_env__os_release(struct perf_env *env);
190 
191 int perf_env__set_cmdline(struct perf_env *env, int argc, const char *argv[]);
192 
193 int perf_env__read_cpuid(struct perf_env *env);
194 int perf_env__read_pmu_mappings(struct perf_env *env);
195 int perf_env__nr_pmu_mappings(struct perf_env *env);
196 const char *perf_env__pmu_mappings(struct perf_env *env);
197 
198 int perf_env__read_cpu_topology_map(struct perf_env *env);
199 
200 /*
201  * Safe accessor for env->cpu[] topology array.  env->cpu can be NULL when
202  * reading old-format perf.data that predates topology information —
203  * process_cpu_topology() in header.c frees it while nr_cpus_avail remains
204  * set, so callers must not index env->cpu[] without this check.
205  */
206 static inline struct cpu_topology_map *
207 perf_env__get_cpu_topology(struct perf_env *env, struct perf_cpu cpu)
208 {
209 	if (env->cpu && cpu.cpu >= 0 && cpu.cpu < env->nr_cpus_avail)
210 		return &env->cpu[cpu.cpu];
211 	return NULL;
212 }
213 
214 void cpu_cache_level__free(struct cpu_cache_level *cache);
215 
216 uint16_t perf_env__e_machine_nocache(struct perf_env *env, uint32_t *e_flags);
217 uint16_t perf_env__e_machine(struct perf_env *env, uint32_t *e_flags);
218 const char *perf_env__arch(struct perf_env *env);
219 const char *perf_env__arch_strerrno(uint16_t e_machine, int err);
220 const char *perf_env__cpuid(struct perf_env *env);
221 int perf_env__nr_cpus_avail(struct perf_env *env);
222 
223 void perf_env__init(struct perf_env *env);
224 #ifdef HAVE_LIBBPF_SUPPORT
225 bool __perf_env__insert_bpf_prog_info(struct perf_env *env,
226 				      struct bpf_prog_info_node *info_node);
227 bool perf_env__insert_bpf_prog_info(struct perf_env *env,
228 				    struct bpf_prog_info_node *info_node);
229 struct bpf_prog_info_node *perf_env__find_bpf_prog_info(struct perf_env *env,
230 							__u32 prog_id);
231 void perf_env__iterate_bpf_prog_info(struct perf_env *env,
232 				     void (*cb)(struct bpf_prog_info_node *node,
233 						void *data),
234 				     void *data);
235 bool perf_env__insert_btf(struct perf_env *env, struct btf_node *btf_node);
236 bool __perf_env__insert_btf(struct perf_env *env, struct btf_node *btf_node);
237 struct btf_node *perf_env__find_btf(struct perf_env *env, __u32 btf_id);
238 struct btf_node *__perf_env__find_btf(struct perf_env *env, __u32 btf_id);
239 #endif // HAVE_LIBBPF_SUPPORT
240 
241 int perf_env__numa_node(struct perf_env *env, struct perf_cpu cpu);
242 char *perf_env__find_pmu_cap(struct perf_env *env, const char *pmu_name,
243 			     const char *cap);
244 
245 bool perf_env__has_pmu_mapping(struct perf_env *env, const char *pmu_name);
246 void perf_env__find_br_cntr_info(struct perf_env *env,
247 				 unsigned int *nr,
248 				 unsigned int *width);
249 
250 bool x86__is_amd_cpu(void);
251 bool perf_env__is_x86_amd_cpu(struct perf_env *env);
252 bool x86__is_intel_cpu(void);
253 bool perf_env__is_x86_intel_cpu(struct perf_env *env);
254 
255 #endif /* __PERF_ENV_H */
256