1#ifndef __PERF_EVSEL_H
2#define __PERF_EVSEL_H 1
3
4#include <linux/list.h>
5#include <stdbool.h>
6#include <stddef.h>
7#include <linux/perf_event.h>
8#include "types.h"
9#include "xyarray.h"
10#include "cgroup.h"
11#include "hist.h"
12#include "symbol.h"
13
14struct perf_counts_values {
15	union {
16		struct {
17			u64 val;
18			u64 ena;
19			u64 run;
20		};
21		u64 values[3];
22	};
23};
24
25struct perf_counts {
26	s8		   	  scaled;
27	struct perf_counts_values aggr;
28	struct perf_counts_values cpu[];
29};
30
31struct perf_evsel;
32
33/*
34 * Per fd, to map back from PERF_SAMPLE_ID to evsel, only used when there are
35 * more than one entry in the evlist.
36 */
37struct perf_sample_id {
38	struct hlist_node 	node;
39	u64		 	id;
40	struct perf_evsel	*evsel;
41
42	/* Holds total ID period value for PERF_SAMPLE_READ processing. */
43	u64			period;
44};
45
46/** struct perf_evsel - event selector
47 *
48 * @name - Can be set to retain the original event name passed by the user,
49 *         so that when showing results in tools such as 'perf stat', we
50 *         show the name used, not some alias.
51 * @id_pos: the position of the event id (PERF_SAMPLE_ID or
52 *          PERF_SAMPLE_IDENTIFIER) in a sample event i.e. in the array of
53 *          struct sample_event
54 * @is_pos: the position (counting backwards) of the event id (PERF_SAMPLE_ID or
55 *          PERF_SAMPLE_IDENTIFIER) in a non-sample event i.e. if sample_id_all
56 *          is used there is an id sample appended to non-sample events
57 */
58struct perf_evsel {
59	struct list_head	node;
60	struct perf_event_attr	attr;
61	char			*filter;
62	struct xyarray		*fd;
63	struct xyarray		*sample_id;
64	u64			*id;
65	struct perf_counts	*counts;
66	struct perf_counts	*prev_raw_counts;
67	int			idx;
68	u32			ids;
69	struct hists		hists;
70	char			*name;
71	struct event_format	*tp_format;
72	union {
73		void		*priv;
74		off_t		id_offset;
75	};
76	struct cgroup_sel	*cgrp;
77	struct {
78		void		*func;
79		void		*data;
80	} handler;
81	struct cpu_map		*cpus;
82	unsigned int		sample_size;
83	int			id_pos;
84	int			is_pos;
85	bool 			supported;
86	bool 			needs_swap;
87	/* parse modifier helper */
88	int			exclude_GH;
89	int			nr_members;
90	int			sample_read;
91	struct perf_evsel	*leader;
92	char			*group_name;
93};
94
95#define hists_to_evsel(h) container_of(h, struct perf_evsel, hists)
96
97struct cpu_map;
98struct thread_map;
99struct perf_evlist;
100struct perf_record_opts;
101
102struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr, int idx);
103struct perf_evsel *perf_evsel__newtp(const char *sys, const char *name, int idx);
104
105struct event_format *event_format__new(const char *sys, const char *name);
106
107void perf_evsel__init(struct perf_evsel *evsel,
108		      struct perf_event_attr *attr, int idx);
109void perf_evsel__exit(struct perf_evsel *evsel);
110void perf_evsel__delete(struct perf_evsel *evsel);
111
112void perf_evsel__config(struct perf_evsel *evsel,
113			struct perf_record_opts *opts);
114
115int __perf_evsel__sample_size(u64 sample_type);
116void perf_evsel__calc_id_pos(struct perf_evsel *evsel);
117
118bool perf_evsel__is_cache_op_valid(u8 type, u8 op);
119
120#define PERF_EVSEL__MAX_ALIASES 8
121
122extern const char *perf_evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX]
123				       [PERF_EVSEL__MAX_ALIASES];
124extern const char *perf_evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX]
125					  [PERF_EVSEL__MAX_ALIASES];
126extern const char *perf_evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX]
127					      [PERF_EVSEL__MAX_ALIASES];
128extern const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX];
129extern const char *perf_evsel__sw_names[PERF_COUNT_SW_MAX];
130int __perf_evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result,
131					    char *bf, size_t size);
132const char *perf_evsel__name(struct perf_evsel *evsel);
133const char *perf_evsel__group_name(struct perf_evsel *evsel);
134int perf_evsel__group_desc(struct perf_evsel *evsel, char *buf, size_t size);
135
136int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads);
137int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads);
138int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus);
139void perf_evsel__reset_counts(struct perf_evsel *evsel, int ncpus);
140void perf_evsel__free_fd(struct perf_evsel *evsel);
141void perf_evsel__free_id(struct perf_evsel *evsel);
142void perf_evsel__free_counts(struct perf_evsel *evsel);
143void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads);
144
145void __perf_evsel__set_sample_bit(struct perf_evsel *evsel,
146				  enum perf_event_sample_format bit);
147void __perf_evsel__reset_sample_bit(struct perf_evsel *evsel,
148				    enum perf_event_sample_format bit);
149
150#define perf_evsel__set_sample_bit(evsel, bit) \
151	__perf_evsel__set_sample_bit(evsel, PERF_SAMPLE_##bit)
152
153#define perf_evsel__reset_sample_bit(evsel, bit) \
154	__perf_evsel__reset_sample_bit(evsel, PERF_SAMPLE_##bit)
155
156void perf_evsel__set_sample_id(struct perf_evsel *evsel,
157			       bool use_sample_identifier);
158
159int perf_evsel__set_filter(struct perf_evsel *evsel, int ncpus, int nthreads,
160			   const char *filter);
161int perf_evsel__enable(struct perf_evsel *evsel, int ncpus, int nthreads);
162
163int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
164			     struct cpu_map *cpus);
165int perf_evsel__open_per_thread(struct perf_evsel *evsel,
166				struct thread_map *threads);
167int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
168		     struct thread_map *threads);
169void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads);
170
171struct perf_sample;
172
173void *perf_evsel__rawptr(struct perf_evsel *evsel, struct perf_sample *sample,
174			 const char *name);
175u64 perf_evsel__intval(struct perf_evsel *evsel, struct perf_sample *sample,
176		       const char *name);
177
178static inline char *perf_evsel__strval(struct perf_evsel *evsel,
179				       struct perf_sample *sample,
180				       const char *name)
181{
182	return perf_evsel__rawptr(evsel, sample, name);
183}
184
185struct format_field;
186
187struct format_field *perf_evsel__field(struct perf_evsel *evsel, const char *name);
188
189#define perf_evsel__match(evsel, t, c)		\
190	(evsel->attr.type == PERF_TYPE_##t &&	\
191	 evsel->attr.config == PERF_COUNT_##c)
192
193static inline bool perf_evsel__match2(struct perf_evsel *e1,
194				      struct perf_evsel *e2)
195{
196	return (e1->attr.type == e2->attr.type) &&
197	       (e1->attr.config == e2->attr.config);
198}
199
200int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
201			      int cpu, int thread, bool scale);
202
203/**
204 * perf_evsel__read_on_cpu - Read out the results on a CPU and thread
205 *
206 * @evsel - event selector to read value
207 * @cpu - CPU of interest
208 * @thread - thread of interest
209 */
210static inline int perf_evsel__read_on_cpu(struct perf_evsel *evsel,
211					  int cpu, int thread)
212{
213	return __perf_evsel__read_on_cpu(evsel, cpu, thread, false);
214}
215
216/**
217 * perf_evsel__read_on_cpu_scaled - Read out the results on a CPU and thread, scaled
218 *
219 * @evsel - event selector to read value
220 * @cpu - CPU of interest
221 * @thread - thread of interest
222 */
223static inline int perf_evsel__read_on_cpu_scaled(struct perf_evsel *evsel,
224						 int cpu, int thread)
225{
226	return __perf_evsel__read_on_cpu(evsel, cpu, thread, true);
227}
228
229int __perf_evsel__read(struct perf_evsel *evsel, int ncpus, int nthreads,
230		       bool scale);
231
232/**
233 * perf_evsel__read - Read the aggregate results on all CPUs
234 *
235 * @evsel - event selector to read value
236 * @ncpus - Number of cpus affected, from zero
237 * @nthreads - Number of threads affected, from zero
238 */
239static inline int perf_evsel__read(struct perf_evsel *evsel,
240				    int ncpus, int nthreads)
241{
242	return __perf_evsel__read(evsel, ncpus, nthreads, false);
243}
244
245/**
246 * perf_evsel__read_scaled - Read the aggregate results on all CPUs, scaled
247 *
248 * @evsel - event selector to read value
249 * @ncpus - Number of cpus affected, from zero
250 * @nthreads - Number of threads affected, from zero
251 */
252static inline int perf_evsel__read_scaled(struct perf_evsel *evsel,
253					  int ncpus, int nthreads)
254{
255	return __perf_evsel__read(evsel, ncpus, nthreads, true);
256}
257
258void hists__init(struct hists *hists);
259
260int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
261			     struct perf_sample *sample);
262
263static inline struct perf_evsel *perf_evsel__next(struct perf_evsel *evsel)
264{
265	return list_entry(evsel->node.next, struct perf_evsel, node);
266}
267
268/**
269 * perf_evsel__is_group_leader - Return whether given evsel is a leader event
270 *
271 * @evsel - evsel selector to be tested
272 *
273 * Return %true if @evsel is a group leader or a stand-alone event
274 */
275static inline bool perf_evsel__is_group_leader(const struct perf_evsel *evsel)
276{
277	return evsel->leader == evsel;
278}
279
280/**
281 * perf_evsel__is_group_event - Return whether given evsel is a group event
282 *
283 * @evsel - evsel selector to be tested
284 *
285 * Return %true iff event group view is enabled and @evsel is a actual group
286 * leader which has other members in the group
287 */
288static inline bool perf_evsel__is_group_event(struct perf_evsel *evsel)
289{
290	if (!symbol_conf.event_group)
291		return false;
292
293	return perf_evsel__is_group_leader(evsel) && evsel->nr_members > 1;
294}
295
296struct perf_attr_details {
297	bool freq;
298	bool verbose;
299	bool event_group;
300};
301
302int perf_evsel__fprintf(struct perf_evsel *evsel,
303			struct perf_attr_details *details, FILE *fp);
304
305bool perf_evsel__fallback(struct perf_evsel *evsel, int err,
306			  char *msg, size_t msgsize);
307int perf_evsel__open_strerror(struct perf_evsel *evsel,
308			      struct perf_target *target,
309			      int err, char *msg, size_t size);
310
311static inline int perf_evsel__group_idx(struct perf_evsel *evsel)
312{
313	return evsel->idx - evsel->leader->idx;
314}
315
316#define for_each_group_member(_evsel, _leader) 					\
317for ((_evsel) = list_entry((_leader)->node.next, struct perf_evsel, node); 	\
318     (_evsel) && (_evsel)->leader == (_leader);					\
319     (_evsel) = list_entry((_evsel)->node.next, struct perf_evsel, node))
320
321#endif /* __PERF_EVSEL_H */
322