xref: /linux-6.15/tools/perf/util/header.h (revision 538737da)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __PERF_HEADER_H
3 #define __PERF_HEADER_H
4 
5 #include <linux/stddef.h>
6 #include <linux/perf_event.h>
7 #include <sys/types.h>
8 #include <stdio.h> // FILE
9 #include <stdbool.h>
10 #include <linux/bitmap.h>
11 #include <linux/types.h>
12 #include "env.h"
13 #include "pmu.h"
14 #include <perf/cpumap.h>
15 
16 enum {
17 	HEADER_RESERVED		= 0,	/* always cleared */
18 	HEADER_FIRST_FEATURE	= 1,
19 	HEADER_TRACING_DATA	= 1,
20 	HEADER_BUILD_ID,
21 
22 	HEADER_HOSTNAME,
23 	HEADER_OSRELEASE,
24 	HEADER_VERSION,
25 	HEADER_ARCH,
26 	HEADER_NRCPUS,
27 	HEADER_CPUDESC,
28 	HEADER_CPUID,
29 	HEADER_TOTAL_MEM,
30 	HEADER_CMDLINE,
31 	HEADER_EVENT_DESC,
32 	HEADER_CPU_TOPOLOGY,
33 	HEADER_NUMA_TOPOLOGY,
34 	HEADER_BRANCH_STACK,
35 	HEADER_PMU_MAPPINGS,
36 	HEADER_GROUP_DESC,
37 	HEADER_AUXTRACE,
38 	HEADER_STAT,
39 	HEADER_CACHE,
40 	HEADER_SAMPLE_TIME,
41 	HEADER_MEM_TOPOLOGY,
42 	HEADER_CLOCKID,
43 	HEADER_DIR_FORMAT,
44 	HEADER_BPF_PROG_INFO,
45 	HEADER_BPF_BTF,
46 	HEADER_COMPRESSED,
47 	HEADER_CPU_PMU_CAPS,
48 	HEADER_CLOCK_DATA,
49 	HEADER_HYBRID_TOPOLOGY,
50 	HEADER_PMU_CAPS,
51 	HEADER_LAST_FEATURE,
52 	HEADER_FEAT_BITS	= 256,
53 };
54 
55 enum perf_header_version {
56 	PERF_HEADER_VERSION_1,
57 	PERF_HEADER_VERSION_2,
58 };
59 
60 struct perf_file_section {
61 	u64 offset;
62 	u64 size;
63 };
64 
65 /**
66  * struct perf_file_header: Header representation on disk.
67  */
68 struct perf_file_header {
69 	/** @magic: Holds "PERFILE2". */
70 	u64				magic;
71 	/** @size: Size of this header - sizeof(struct perf_file_header). */
72 	u64				size;
73 	/**
74 	 * @attr_size: Size of attrs entries - sizeof(struct perf_event_attr) +
75 	 * sizeof(struct perf_file_section).
76 	 */
77 	u64				attr_size;
78 	/** @attrs: Offset and size of file section holding attributes. */
79 	struct perf_file_section	attrs;
80 	/** @data: Offset and size of file section holding regular event data. */
81 	struct perf_file_section	data;
82 	/** @event_types: Ignored. */
83 	struct perf_file_section	event_types;
84 	/**
85 	 * @adds_features: Bitmap of features. The features are immediately after the data section.
86 	 */
87 	DECLARE_BITMAP(adds_features, HEADER_FEAT_BITS);
88 };
89 
90 struct perf_pipe_file_header {
91 	u64				magic;
92 	u64				size;
93 };
94 
95 struct perf_header;
96 
97 int perf_file_header__read(struct perf_file_header *header,
98 			   struct perf_header *ph, int fd);
99 
100 struct perf_header {
101 	enum perf_header_version	version;
102 	bool				needs_swap;
103 	u64				data_offset;
104 	u64				data_size;
105 	u64				feat_offset;
106 	DECLARE_BITMAP(adds_features, HEADER_FEAT_BITS);
107 	struct perf_env 	env;
108 };
109 
110 struct feat_fd {
111 	struct perf_header *ph;
112 	int		   fd;
113 	void		   *buf;	/* Either buf != NULL or fd >= 0 */
114 	ssize_t		   offset;
115 	size_t		   size;
116 	struct evsel	   *events;
117 };
118 
119 struct perf_header_feature_ops {
120 	int	   (*write)(struct feat_fd *ff, struct evlist *evlist);
121 	void	   (*print)(struct feat_fd *ff, FILE *fp);
122 	int	   (*process)(struct feat_fd *ff, void *data);
123 	const char *name;
124 	bool	   full_only;
125 	bool	   synthesize;
126 };
127 
128 struct evlist;
129 struct perf_session;
130 struct perf_tool;
131 union perf_event;
132 
133 extern const char perf_version_string[];
134 
135 int perf_session__read_header(struct perf_session *session);
136 int perf_session__write_header(struct perf_session *session,
137 			       struct evlist *evlist,
138 			       int fd, bool at_exit);
139 int perf_header__write_pipe(int fd);
140 
141 /* feat_writer writes a feature section to output */
142 struct feat_writer {
143 	int (*write)(struct feat_writer *fw, void *buf, size_t sz);
144 };
145 
146 /* feat_copier copies a feature section using feat_writer to output */
147 struct feat_copier {
148 	int (*copy)(struct feat_copier *fc, int feat, struct feat_writer *fw);
149 };
150 
151 int perf_session__inject_header(struct perf_session *session,
152 				struct evlist *evlist,
153 				int fd,
154 				struct feat_copier *fc,
155 				bool write_attrs_after_data);
156 
157 size_t perf_session__data_offset(const struct evlist *evlist);
158 
159 void perf_header__set_feat(struct perf_header *header, int feat);
160 void perf_header__clear_feat(struct perf_header *header, int feat);
161 bool perf_header__has_feat(const struct perf_header *header, int feat);
162 
163 int perf_header__set_cmdline(int argc, const char **argv);
164 
165 int perf_header__process_sections(struct perf_header *header, int fd,
166 				  void *data,
167 				  int (*process)(struct perf_file_section *section,
168 				  struct perf_header *ph,
169 				  int feat, int fd, void *data));
170 
171 int perf_header__fprintf_info(struct perf_session *s, FILE *fp, bool full);
172 
173 int perf_event__process_feature(struct perf_session *session,
174 				union perf_event *event);
175 int perf_event__process_attr(const struct perf_tool *tool, union perf_event *event,
176 			     struct evlist **pevlist);
177 int perf_event__process_event_update(const struct perf_tool *tool,
178 				     union perf_event *event,
179 				     struct evlist **pevlist);
180 size_t perf_event__fprintf_event_update(union perf_event *event, FILE *fp);
181 #ifdef HAVE_LIBTRACEEVENT
182 int perf_event__process_tracing_data(struct perf_session *session,
183 				     union perf_event *event);
184 #endif
185 int perf_event__process_build_id(struct perf_session *session,
186 				 union perf_event *event);
187 bool is_perf_magic(u64 magic);
188 
189 #define NAME_ALIGN 64
190 
191 struct feat_fd;
192 
193 int do_write(struct feat_fd *fd, const void *buf, size_t size);
194 
195 int write_padded(struct feat_fd *fd, const void *bf,
196 		 size_t count, size_t count_aligned);
197 
198 #define MAX_CACHE_LVL 4
199 
200 int build_caches_for_cpu(u32 cpu, struct cpu_cache_level caches[], u32 *cntp);
201 
202 /*
203  * arch specific callback
204  */
205 int get_cpuid(char *buffer, size_t sz, struct perf_cpu cpu);
206 
207 char *get_cpuid_str(struct perf_pmu *pmu __maybe_unused);
208 int strcmp_cpuid_str(const char *s1, const char *s2);
209 #endif /* __PERF_HEADER_H */
210