xref: /linux/tools/perf/util/sample.h (revision 01ee8c14354f1fb90ed7ac81998b863a9d42e285)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __PERF_SAMPLE_H
3 #define __PERF_SAMPLE_H
4 
5 #include <linux/perf_event.h>
6 #include <linux/types.h>
7 
8 struct evsel;
9 struct machine;
10 struct thread;
11 
12 /* number of register is bound by the number of bits in regs_dump::mask (64) */
13 #define PERF_SAMPLE_REGS_CACHE_SIZE (8 * sizeof(u64))
14 
15 struct regs_dump {
16 	u64 abi;
17 	u64 mask;
18 	u64 *regs;
19 	u64 nr_vectors;
20 	u64 vector_qwords;
21 	u64 nr_pred;
22 	u64 pred_qwords;
23 	u64 *simd_data;
24 
25 	/* Cached values/mask filled by first register access. */
26 	u64 cache_regs[PERF_SAMPLE_REGS_CACHE_SIZE];
27 	u64 cache_mask;
28 };
29 
30 struct stack_dump {
31 	u16 offset;
32 	u64 size;
33 	char *data;
34 };
35 
36 struct sample_read_value {
37 	u64 value;
38 	u64 id;   /* only if PERF_FORMAT_ID */
39 	u64 lost; /* only if PERF_FORMAT_LOST */
40 };
41 
42 struct sample_read {
43 	u64 time_enabled;
44 	u64 time_running;
45 	union {
46 		struct {
47 			u64 nr;
48 			struct sample_read_value *values;
49 		} group;
50 		struct sample_read_value one;
51 	};
52 };
53 
54 static inline size_t sample_read_value_size(u64 read_format)
55 {
56 	/* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
57 	if (read_format & PERF_FORMAT_LOST)
58 		return sizeof(struct sample_read_value);
59 	else
60 		return offsetof(struct sample_read_value, lost);
61 }
62 
63 static inline struct sample_read_value *next_sample_read_value(struct sample_read_value *v, u64 read_format)
64 {
65 	return (void *)v + sample_read_value_size(read_format);
66 }
67 
68 #define sample_read_group__for_each(v, nr, rf) \
69 	for (int __i = 0; __i < (int)nr; v = next_sample_read_value(v, rf), __i++)
70 
71 #define MAX_INSN 16
72 
73 struct aux_sample {
74 	u64 size;
75 	void *data;
76 };
77 
78 struct simd_flags {
79 	u8	arch:  2,	/* architecture (isa) */
80 		pred:  3,	/* predication */
81 		resv:  3;	/* reserved */
82 };
83 
84 /* simd architecture flags */
85 enum simd_op_flags {
86 	SIMD_OP_FLAGS_ARCH_NONE = 0x0,	/* No SIMD operation */
87 	SIMD_OP_FLAGS_ARCH_SVE,		/* Arm SVE */
88 	SIMD_OP_FLAGS_ARCH_SME,		/* Arm SME */
89 	SIMD_OP_FLAGS_ARCH_ASE,		/* Arm Advanced SIMD */
90 };
91 
92 /* simd predicate flags */
93 enum simd_pred_flags {
94 	SIMD_OP_FLAGS_PRED_NONE = 0x0,	/* Not available */
95 	SIMD_OP_FLAGS_PRED_PARTIAL,	/* partial predicate */
96 	SIMD_OP_FLAGS_PRED_EMPTY,	/* empty predicate */
97 	SIMD_OP_FLAGS_PRED_FULL,	/* full predicate */
98 	SIMD_OP_FLAGS_PRED_DISABLED,	/* disabled predicate */
99 };
100 
101 /**
102  * struct perf_sample
103  *
104  * A sample is generally filled in by evlist__parse_sample/evsel__parse_sample
105  * which fills in the variables from a "union perf_event *event" which is data
106  * from a perf ring buffer or perf.data file. The "event" sample is variable in
107  * length as determined by the perf_event_attr (in the evsel) and details within
108  * the sample event itself. A struct perf_sample avoids needing to care about
109  * the variable length nature of the original event.
110  *
111  * To avoid being excessively large parts of the struct perf_sample are pointers
112  * into the original sample event. In general the lifetime of a struct
113  * perf_sample needs to be less than the "union perf_event *event" it was
114  * derived from.
115  *
116  * The struct regs_dump user_regs and intr_regs are lazily allocated again for
117  * size reasons, due to them holding a cache of looked up registers. The
118  * function pair of perf_sample__init and perf_sample__exit correctly initialize
119  * and clean up these values.
120  */
121 struct perf_sample {
122 	/** @evsel: Backward reference to the evsel used when constructing the sample. */
123 	struct evsel *evsel;
124 	/** @ip: The sample event PERF_SAMPLE_IP value. */
125 	u64 ip;
126 	/** @pid: The sample event PERF_SAMPLE_TID pid value. */
127 	u32 pid;
128 	/** @tid: The sample event PERF_SAMPLE_TID tid value. */
129 	u32 tid;
130 	/** @time: The sample event PERF_SAMPLE_TIME value. */
131 	u64 time;
132 	/** @addr: The sample event PERF_SAMPLE_ADDR value. */
133 	u64 addr;
134 	/** @id: The sample event PERF_SAMPLE_ID or PERF_SAMPLE_IDENTIFIER value. */
135 	u64 id;
136 	/** @stream_id: The sample event PERF_SAMPLE_STREAM_ID value. */
137 	u64 stream_id;
138 	/** @period: The sample event PERF_SAMPLE_PERIOD value. */
139 	u64 period;
140 	/** @weight: Data determined by PERF_SAMPLE_WEIGHT or PERF_SAMPLE_WEIGHT_STRUCT. */
141 	u64 weight;
142 	/** @transaction: The sample event PERF_SAMPLE_TRANSACTION value. */
143 	u64 transaction;
144 	/** @insn_cnt: Filled in and used by intel-pt. */
145 	u64 insn_cnt;
146 	/** @cyc_cnt: Filled in and used by intel-pt. */
147 	u64 cyc_cnt;
148 	/** @cpu: The sample event PERF_SAMPLE_CPU value. */
149 	u32 cpu;
150 	/**
151 	 * @raw_size: The size in bytes of raw data from PERF_SAMPLE_RAW. For
152 	 *            alignment reasons this should always be sizeof(u32)
153 	 *            followed by a multiple of sizeof(u64).
154 	 */
155 	u32 raw_size;
156 	/** @data_src: The sample event PERF_SAMPLE_DATA_SRC value. */
157 	u64 data_src;
158 	/** @phys_addr: The sample event PERF_SAMPLE_PHYS_ADDR value. */
159 	u64 phys_addr;
160 	/** @data_page_size: The sample event PERF_SAMPLE_DATA_PAGE_SIZE value. */
161 	u64 data_page_size;
162 	/** @code_page_size: The sample event PERF_SAMPLE_CODE_PAGE_SIZE value. */
163 	u64 code_page_size;
164 	/** @cgroup: The sample event PERF_SAMPLE_CGROUP value. */
165 	u64 cgroup;
166 	/** @file_offset: Byte offset of this event in the perf.data file. */
167 	u64 file_offset;
168 	/** @flags: Extra flag data from auxiliary events like intel-pt. */
169 	u32 flags;
170 	/** @machine_pid: The guest machine pid derived from the sample id. */
171 	u32 machine_pid;
172 	/** @vcpu: The guest machine vcpu derived from the sample id. */
173 	u32 vcpu;
174 	/**
175 	 * @insn_len: Instruction length from auxiliary events like
176 	 *            intel-pt. The instruction itself is held in insn.
177 	 */
178 	u16 insn_len;
179 	/** @misc: The entire struct perf_event_header misc variable. */
180 	u16 misc;
181 	/**
182 	 * @ins_lat: Instruction latency information from weight2 in
183 	 *           PERF_SAMPLE_WEIGHT_STRUCT or auxiliary events like
184 	 *           intel-pt.
185 	 */
186 	u16 ins_lat;
187 	/**
188 	 * @weight3: From PERF_SAMPLE_WEIGHT_STRUCT. On x86 holds retire_lat, on
189 	 *           powerpc holds p_stage_cyc.
190 	 */
191 	u16 weight3;
192 	/**
193 	 * @cpumode: The cpumode from struct perf_event_header misc variable
194 	 *           masked with CPUMODE_MASK. Gives user, kernel and hypervisor
195 	 *           information.
196 	 */
197 	u8  cpumode;
198 	/**
199 	 * @no_hw_idx: For PERF_SAMPLE_BRANCH_STACK, true when
200 	 *             PERF_SAMPLE_BRANCH_HW_INDEX isn't set.
201 	 */
202 	bool no_hw_idx;
203 	/**
204 	 * @deferred_callchain: When processing PERF_SAMPLE_CALLCHAIN a deferred
205 	 *                      user callchain marker was encountered.
206 	 */
207 	bool deferred_callchain;
208 	/**
209 	 * @merged_callchain: A synthesized merged callchain that is allocated
210 	 *                    and needs freeing.
211 	 */
212 	bool merged_callchain;
213 	/**
214 	 * @deferred_cookie: Identifier of the deferred callchain in the later
215 	 *                   PERF_RECORD_CALLCHAIN_DEFERRED event.
216 	 */
217 	u64 deferred_cookie;
218 	/** @insn: A copy of the sampled instruction filled in by perf_sample__fetch_insn. */
219 	char insn[MAX_INSN];
220 	/** @raw_data: Pointer into the original event for PERF_SAMPLE_RAW data. */
221 	void *raw_data;
222 	/**
223 	 * @callchain: Pointer into the original event for PERF_SAMPLE_CALLCHAIN
224 	 *             data. For deferred callchains this may be a copy that
225 	 *             needs freeing, see sample__merge_deferred_callchain.
226 	 */
227 	struct ip_callchain *callchain;
228 	/** @branch_stack: Pointer into the original event for PERF_SAMPLE_BRANCH_STACK data. */
229 	struct branch_stack *branch_stack;
230 	/**
231 	 * @branch_stack_cntr: Pointer into the original event for
232 	 *                     PERF_SAMPLE_BRANCH_COUNTERS data.
233 	 */
234 	u64 *branch_stack_cntr;
235 	/** @user_regs: Values and pointers into the sample for PERF_SAMPLE_REGS_USER. */
236 	struct regs_dump  *user_regs;
237 	/** @intr_regs: Values and pointers into the sample for PERF_SAMPLE_REGS_INTR. */
238 	struct regs_dump  *intr_regs;
239 	/** @user_stack: Size and pointer into the sample for PERF_SAMPLE_STACK_USER. */
240 	struct stack_dump user_stack;
241 	/**
242 	 * @read: The sample event PERF_SAMPLE_READ counter values. The valid
243 	 *        values depend on the attr.read_format PERF_FORMAT_ values.
244 	 */
245 	struct sample_read read;
246 	/**
247 	 * @aux_sample: Similar to raw data but with a 64-bit size and
248 	 *              alignment, PERF_SAMPLE_AUX data.
249 	 */
250 	struct aux_sample aux_sample;
251 	/** @simd_flags: SIMD flag information from ARM SPE auxiliary events. */
252 	struct simd_flags simd_flags;
253 };
254 
255 void perf_sample__init(struct perf_sample *sample, bool all);
256 void perf_sample__exit(struct perf_sample *sample);
257 struct regs_dump *perf_sample__user_regs(struct perf_sample *sample);
258 struct regs_dump *perf_sample__intr_regs(struct perf_sample *sample);
259 
260 void perf_sample__fetch_insn(struct perf_sample *sample,
261 			     struct thread *thread,
262 			     struct machine *machine);
263 
264 /*
265  * raw_data is always 4 bytes from an 8-byte boundary, so subtract 4 to get
266  * 8-byte alignment.
267  */
268 static inline void *perf_sample__synth_ptr(struct perf_sample *sample)
269 {
270 	return sample->raw_data - 4;
271 }
272 
273 #endif /* __PERF_SAMPLE_H */
274