xref: /linux/tools/perf/util/env.h (revision afa4363a91a19dff65dceb7fbce7bba689bbc854)
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 perf_env {
72 	char			*hostname;
73 	char			*os_release;
74 	char			*version;
75 	char			*arch;
76 	/* e_machine expanded from 16 to 32-bits for alignment. */
77 	u32			e_machine;
78 	u32			e_flags;
79 	int			nr_cpus_online;
80 	int			nr_cpus_avail;
81 	char			*cpu_desc;
82 	char			*cpuid;
83 	unsigned long long	total_mem;
84 	unsigned int		msr_pmu_type;
85 	unsigned int		max_branches;
86 	unsigned int		br_cntr_nr;
87 	unsigned int		br_cntr_width;
88 	unsigned int		schedstat_version;
89 	unsigned int		max_sched_domains;
90 	int			kernel_is_64_bit;
91 
92 	int			nr_cmdline;
93 	int			nr_sibling_cores;
94 	int			nr_sibling_dies;
95 	int			nr_sibling_threads;
96 	int			nr_numa_nodes;
97 	int			nr_memory_nodes;
98 	int			nr_pmu_mappings;
99 	int			nr_groups;
100 	int			nr_cpu_pmu_caps;
101 	int			nr_hybrid_nodes;
102 	int			nr_pmus_with_caps;
103 	char			*cmdline;
104 	const char		**cmdline_argv;
105 	char			*sibling_cores;
106 	char			*sibling_dies;
107 	char			*sibling_threads;
108 	char			*pmu_mappings;
109 	char			**cpu_pmu_caps;
110 	struct cpu_topology_map	*cpu;
111 	struct cpu_cache_level	*caches;
112 	struct cpu_domain_map	**cpu_domain;
113 	int			 caches_cnt;
114 	unsigned int		cln_size;
115 	u32			comp_ratio;
116 	u32			comp_ver;
117 	u32			comp_type;
118 	u32			comp_level;
119 	u32			comp_mmap_len;
120 	struct numa_node	*numa_nodes;
121 	struct memory_node	*memory_nodes;
122 	unsigned long long	 memory_bsize;
123 	struct hybrid_node	*hybrid_nodes;
124 	struct pmu_caps		*pmu_caps;
125 #ifdef HAVE_LIBBPF_SUPPORT
126 	/*
127 	 * bpf_info_lock protects bpf rbtrees. This is needed because the
128 	 * trees are accessed by different threads in perf-top
129 	 */
130 	struct {
131 		struct rw_semaphore	lock;
132 		struct rb_root		infos;
133 		u32			infos_cnt;
134 		struct rb_root		btfs;
135 		u32			btfs_cnt;
136 	} bpf_progs;
137 #endif // HAVE_LIBBPF_SUPPORT
138 	/* same reason as above (for perf-top) */
139 	struct {
140 		struct rw_semaphore	lock;
141 		struct rb_root		tree;
142 	} cgroups;
143 
144 	/* For fast cpu to numa node lookup via perf_env__numa_node */
145 	int			*numa_map;
146 	int			 nr_numa_map;
147 
148 	/* For real clock time reference. */
149 	struct {
150 		u64	tod_ns;
151 		u64	clockid_ns;
152 		u64     clockid_res_ns;
153 		int	clockid;
154 		/*
155 		 * enabled is valid for report mode, and is true if above
156 		 * values are set, it's set in process_clock_data
157 		 */
158 		bool	enabled;
159 	} clock;
160 	/* Protects lazy environment initialization (e.g. os_release, e_machine). */
161 	struct mutex		lock;
162 };
163 
164 enum perf_compress_type {
165 	PERF_COMP_NONE = 0,
166 	PERF_COMP_ZSTD,
167 	PERF_COMP_MAX
168 };
169 
170 struct bpf_prog_info_node;
171 struct btf_node;
172 
173 int perf_env__read_core_pmu_caps(struct perf_env *env);
174 void free_cpu_domain_info(struct cpu_domain_map **cd_map, u32 schedstat_version, u32 nr);
175 void perf_env__exit(struct perf_env *env);
176 
177 int perf_env__kernel_is_64_bit(struct perf_env *env);
178 bool perf_arch_is_big_endian(const char *arch);
179 const char *perf_env__os_release(struct perf_env *env);
180 
181 int perf_env__set_cmdline(struct perf_env *env, int argc, const char *argv[]);
182 
183 int perf_env__read_cpuid(struct perf_env *env);
184 int perf_env__read_pmu_mappings(struct perf_env *env);
185 int perf_env__nr_pmu_mappings(struct perf_env *env);
186 const char *perf_env__pmu_mappings(struct perf_env *env);
187 
188 int perf_env__read_cpu_topology_map(struct perf_env *env);
189 
190 /*
191  * Safe accessor for env->cpu[] topology array.  env->cpu can be NULL when
192  * reading old-format perf.data that predates topology information —
193  * process_cpu_topology() in header.c frees it while nr_cpus_avail remains
194  * set, so callers must not index env->cpu[] without this check.
195  */
196 static inline struct cpu_topology_map *
197 perf_env__get_cpu_topology(struct perf_env *env, struct perf_cpu cpu)
198 {
199 	if (env->cpu && cpu.cpu >= 0 && cpu.cpu < env->nr_cpus_avail)
200 		return &env->cpu[cpu.cpu];
201 	return NULL;
202 }
203 
204 void cpu_cache_level__free(struct cpu_cache_level *cache);
205 
206 uint16_t perf_env__e_machine_nocache(struct perf_env *env, uint32_t *e_flags);
207 uint16_t perf_env__e_machine(struct perf_env *env, uint32_t *e_flags);
208 const char *perf_env__arch(struct perf_env *env);
209 const char *perf_env__arch_strerrno(uint16_t e_machine, int err);
210 const char *perf_env__cpuid(struct perf_env *env);
211 int perf_env__nr_cpus_avail(struct perf_env *env);
212 
213 void perf_env__init(struct perf_env *env);
214 #ifdef HAVE_LIBBPF_SUPPORT
215 bool __perf_env__insert_bpf_prog_info(struct perf_env *env,
216 				      struct bpf_prog_info_node *info_node);
217 bool perf_env__insert_bpf_prog_info(struct perf_env *env,
218 				    struct bpf_prog_info_node *info_node);
219 struct bpf_prog_info_node *perf_env__find_bpf_prog_info(struct perf_env *env,
220 							__u32 prog_id);
221 void perf_env__iterate_bpf_prog_info(struct perf_env *env,
222 				     void (*cb)(struct bpf_prog_info_node *node,
223 						void *data),
224 				     void *data);
225 bool perf_env__insert_btf(struct perf_env *env, struct btf_node *btf_node);
226 bool __perf_env__insert_btf(struct perf_env *env, struct btf_node *btf_node);
227 struct btf_node *perf_env__find_btf(struct perf_env *env, __u32 btf_id);
228 struct btf_node *__perf_env__find_btf(struct perf_env *env, __u32 btf_id);
229 #endif // HAVE_LIBBPF_SUPPORT
230 
231 int perf_env__numa_node(struct perf_env *env, struct perf_cpu cpu);
232 char *perf_env__find_pmu_cap(struct perf_env *env, const char *pmu_name,
233 			     const char *cap);
234 
235 bool perf_env__has_pmu_mapping(struct perf_env *env, const char *pmu_name);
236 void perf_env__find_br_cntr_info(struct perf_env *env,
237 				 unsigned int *nr,
238 				 unsigned int *width);
239 
240 bool x86__is_amd_cpu(void);
241 bool perf_env__is_x86_amd_cpu(struct perf_env *env);
242 bool x86__is_intel_cpu(void);
243 bool perf_env__is_x86_intel_cpu(struct perf_env *env);
244 
245 #endif /* __PERF_ENV_H */
246