1 /* SPDX-License-Identifier: GPL-2.0 */
3 #define __PERF_EVSEL_H 1
5 #include <linux/list.h>
8 #include <linux/perf_event.h>
9 #include <linux/types.h>
10 #include <internal/evsel.h>
11 #include <perf/evsel.h>
12 #include "symbol_conf.h"
13 #include <internal/cpumap.h>
18 struct perf_stat_evsel;
20 struct bpf_counter_ops;
23 typedef int (evsel__sb_cb_t)(union perf_event *event, void *data);
25 enum perf_tool_event {
27 PERF_TOOL_DURATION_TIME = 1,
30 /** struct evsel - event selector
32 * @evlist - evlist this evsel is in, if it is in one.
33 * @core - libperf evsel object
34 * @name - Can be set to retain the original event name passed by the user,
35 * so that when showing results in tools such as 'perf stat', we
36 * show the name used, not some alias.
37 * @id_pos: the position of the event id (PERF_SAMPLE_ID or
38 * PERF_SAMPLE_IDENTIFIER) in a sample event i.e. in the array of
39 * struct perf_record_sample
40 * @is_pos: the position (counting backwards) of the event id (PERF_SAMPLE_ID or
41 * PERF_SAMPLE_IDENTIFIER) in a non-sample event i.e. if sample_id_all
42 * is used there is an id sample appended to non-sample events
43 * @priv: And what is in its containing unnamed union are tool specific
46 struct perf_evsel core;
47 struct evlist *evlist;
52 unsigned int sample_size;
55 * These fields can be set in the parse-events code or similar.
56 * Please check evsel__clone() to copy them properly so that
57 * they can be released properly.
63 struct tep_event *tp_format;
65 unsigned long max_events;
69 enum perf_tool_event tool_event;
70 /* parse modifier helper */
77 bool use_uncore_alias;
79 bool auto_merge_stats;
83 struct bpf_object *bpf_obj;
87 * metric fields are similar, but needs more care as they can have
88 * references to other metric (evsel).
90 const char * metric_expr;
91 const char * metric_name;
92 struct evsel **metric_events;
93 struct evsel *metric_leader;
96 struct perf_counts *counts;
97 struct perf_counts *prev_raw_counts;
98 unsigned long nr_events_printed;
99 struct perf_stat_evsel *stats;
102 bool uniquified_name;
109 bool ignore_missing_thread;
111 bool cmdline_group_boundary;
115 unsigned long *per_pkg_mask;
116 struct evsel *leader;
117 struct list_head config_terms;
125 * For reporting purposes, an evsel sample can have a callchain
126 * synthesized from AUX area data. Keep track of synthesized sample
127 * types here. Note, the recorded sample_type cannot be changed because
128 * it is needed to continue to parse events.
129 * See also evsel__has_callchain().
131 __u64 synth_sample_type;
132 struct list_head bpf_counter_list;
133 struct bpf_counter_ops *bpf_counter_ops;
136 struct perf_missing_features {
156 extern struct perf_missing_features perf_missing_features;
163 static inline struct perf_cpu_map *evsel__cpus(struct evsel *evsel)
165 return perf_evsel__cpus(&evsel->core);
168 static inline int evsel__nr_cpus(struct evsel *evsel)
170 return evsel__cpus(evsel)->nr;
173 void perf_counts_values__scale(struct perf_counts_values *count,
174 bool scale, s8 *pscaled);
176 void evsel__compute_deltas(struct evsel *evsel, int cpu, int thread,
177 struct perf_counts_values *count);
179 int evsel__object_config(size_t object_size,
180 int (*init)(struct evsel *evsel),
181 void (*fini)(struct evsel *evsel));
183 struct perf_pmu *evsel__find_pmu(struct evsel *evsel);
184 bool evsel__is_aux_event(struct evsel *evsel);
186 struct evsel *evsel__new_idx(struct perf_event_attr *attr, int idx);
188 static inline struct evsel *evsel__new(struct perf_event_attr *attr)
190 return evsel__new_idx(attr, 0);
193 struct evsel *evsel__clone(struct evsel *orig);
194 struct evsel *evsel__newtp_idx(const char *sys, const char *name, int idx);
197 * Returns pointer with encoded error via <linux/err.h> interface.
199 static inline struct evsel *evsel__newtp(const char *sys, const char *name)
201 return evsel__newtp_idx(sys, name, 0);
204 struct evsel *evsel__new_cycles(bool precise);
206 struct tep_event *event_format__new(const char *sys, const char *name);
208 void evsel__init(struct evsel *evsel, struct perf_event_attr *attr, int idx);
209 void evsel__exit(struct evsel *evsel);
210 void evsel__delete(struct evsel *evsel);
212 struct callchain_param;
214 void evsel__config(struct evsel *evsel, struct record_opts *opts,
215 struct callchain_param *callchain);
216 void evsel__config_callchain(struct evsel *evsel, struct record_opts *opts,
217 struct callchain_param *callchain);
219 int __evsel__sample_size(u64 sample_type);
220 void evsel__calc_id_pos(struct evsel *evsel);
222 bool evsel__is_cache_op_valid(u8 type, u8 op);
224 #define EVSEL__MAX_ALIASES 8
226 extern const char *evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX][EVSEL__MAX_ALIASES];
227 extern const char *evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX][EVSEL__MAX_ALIASES];
228 extern const char *evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX][EVSEL__MAX_ALIASES];
229 extern const char *evsel__hw_names[PERF_COUNT_HW_MAX];
230 extern const char *evsel__sw_names[PERF_COUNT_SW_MAX];
231 int __evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result, char *bf, size_t size);
232 const char *evsel__name(struct evsel *evsel);
234 const char *evsel__group_name(struct evsel *evsel);
235 int evsel__group_desc(struct evsel *evsel, char *buf, size_t size);
237 void __evsel__set_sample_bit(struct evsel *evsel, enum perf_event_sample_format bit);
238 void __evsel__reset_sample_bit(struct evsel *evsel, enum perf_event_sample_format bit);
240 #define evsel__set_sample_bit(evsel, bit) \
241 __evsel__set_sample_bit(evsel, PERF_SAMPLE_##bit)
243 #define evsel__reset_sample_bit(evsel, bit) \
244 __evsel__reset_sample_bit(evsel, PERF_SAMPLE_##bit)
246 void evsel__set_sample_id(struct evsel *evsel, bool use_sample_identifier);
248 void arch_evsel__set_sample_weight(struct evsel *evsel);
250 int evsel__set_filter(struct evsel *evsel, const char *filter);
251 int evsel__append_tp_filter(struct evsel *evsel, const char *filter);
252 int evsel__append_addr_filter(struct evsel *evsel, const char *filter);
253 int evsel__enable_cpu(struct evsel *evsel, int cpu);
254 int evsel__enable(struct evsel *evsel);
255 int evsel__disable(struct evsel *evsel);
256 int evsel__disable_cpu(struct evsel *evsel, int cpu);
258 int evsel__open_per_cpu(struct evsel *evsel, struct perf_cpu_map *cpus, int cpu);
259 int evsel__open_per_thread(struct evsel *evsel, struct perf_thread_map *threads);
260 int evsel__open(struct evsel *evsel, struct perf_cpu_map *cpus,
261 struct perf_thread_map *threads);
262 void evsel__close(struct evsel *evsel);
266 void *evsel__rawptr(struct evsel *evsel, struct perf_sample *sample, const char *name);
267 u64 evsel__intval(struct evsel *evsel, struct perf_sample *sample, const char *name);
269 static inline char *evsel__strval(struct evsel *evsel, struct perf_sample *sample, const char *name)
271 return evsel__rawptr(evsel, sample, name);
274 struct tep_format_field;
276 u64 format_field__intval(struct tep_format_field *field, struct perf_sample *sample, bool needs_swap);
278 struct tep_format_field *evsel__field(struct evsel *evsel, const char *name);
280 #define evsel__match(evsel, t, c) \
281 (evsel->core.attr.type == PERF_TYPE_##t && \
282 evsel->core.attr.config == PERF_COUNT_##c)
284 static inline bool evsel__match2(struct evsel *e1, struct evsel *e2)
286 return (e1->core.attr.type == e2->core.attr.type) &&
287 (e1->core.attr.config == e2->core.attr.config);
290 int evsel__read_counter(struct evsel *evsel, int cpu, int thread);
292 int __evsel__read_on_cpu(struct evsel *evsel, int cpu, int thread, bool scale);
295 * evsel__read_on_cpu - Read out the results on a CPU and thread
297 * @evsel - event selector to read value
298 * @cpu - CPU of interest
299 * @thread - thread of interest
301 static inline int evsel__read_on_cpu(struct evsel *evsel, int cpu, int thread)
303 return __evsel__read_on_cpu(evsel, cpu, thread, false);
307 * evsel__read_on_cpu_scaled - Read out the results on a CPU and thread, scaled
309 * @evsel - event selector to read value
310 * @cpu - CPU of interest
311 * @thread - thread of interest
313 static inline int evsel__read_on_cpu_scaled(struct evsel *evsel, int cpu, int thread)
315 return __evsel__read_on_cpu(evsel, cpu, thread, true);
318 int evsel__parse_sample(struct evsel *evsel, union perf_event *event,
319 struct perf_sample *sample);
321 int evsel__parse_sample_timestamp(struct evsel *evsel, union perf_event *event,
324 static inline struct evsel *evsel__next(struct evsel *evsel)
326 return list_entry(evsel->core.node.next, struct evsel, core.node);
329 static inline struct evsel *evsel__prev(struct evsel *evsel)
331 return list_entry(evsel->core.node.prev, struct evsel, core.node);
335 * evsel__is_group_leader - Return whether given evsel is a leader event
337 * @evsel - evsel selector to be tested
339 * Return %true if @evsel is a group leader or a stand-alone event
341 static inline bool evsel__is_group_leader(const struct evsel *evsel)
343 return evsel->leader == evsel;
347 * evsel__is_group_event - Return whether given evsel is a group event
349 * @evsel - evsel selector to be tested
351 * Return %true iff event group view is enabled and @evsel is a actual group
352 * leader which has other members in the group
354 static inline bool evsel__is_group_event(struct evsel *evsel)
356 if (!symbol_conf.event_group)
359 return evsel__is_group_leader(evsel) && evsel->core.nr_members > 1;
362 bool evsel__is_function_event(struct evsel *evsel);
364 static inline bool evsel__is_bpf_output(struct evsel *evsel)
366 return evsel__match(evsel, SOFTWARE, SW_BPF_OUTPUT);
369 static inline bool evsel__is_clock(struct evsel *evsel)
371 return evsel__match(evsel, SOFTWARE, SW_CPU_CLOCK) ||
372 evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK);
375 bool evsel__fallback(struct evsel *evsel, int err, char *msg, size_t msgsize);
376 int evsel__open_strerror(struct evsel *evsel, struct target *target,
377 int err, char *msg, size_t size);
379 static inline int evsel__group_idx(struct evsel *evsel)
381 return evsel->idx - evsel->leader->idx;
384 /* Iterates group WITHOUT the leader. */
385 #define for_each_group_member(_evsel, _leader) \
386 for ((_evsel) = list_entry((_leader)->core.node.next, struct evsel, core.node); \
387 (_evsel) && (_evsel)->leader == (_leader); \
388 (_evsel) = list_entry((_evsel)->core.node.next, struct evsel, core.node))
390 /* Iterates group WITH the leader. */
391 #define for_each_group_evsel(_evsel, _leader) \
392 for ((_evsel) = _leader; \
393 (_evsel) && (_evsel)->leader == (_leader); \
394 (_evsel) = list_entry((_evsel)->core.node.next, struct evsel, core.node))
396 static inline bool evsel__has_branch_callstack(const struct evsel *evsel)
398 return evsel->core.attr.branch_sample_type & PERF_SAMPLE_BRANCH_CALL_STACK;
401 static inline bool evsel__has_branch_hw_idx(const struct evsel *evsel)
403 return evsel->core.attr.branch_sample_type & PERF_SAMPLE_BRANCH_HW_INDEX;
406 static inline bool evsel__has_callchain(const struct evsel *evsel)
409 * For reporting purposes, an evsel sample can have a recorded callchain
410 * or a callchain synthesized from AUX area data.
412 return evsel->core.attr.sample_type & PERF_SAMPLE_CALLCHAIN ||
413 evsel->synth_sample_type & PERF_SAMPLE_CALLCHAIN;
416 static inline bool evsel__has_br_stack(const struct evsel *evsel)
419 * For reporting purposes, an evsel sample can have a recorded branch
420 * stack or a branch stack synthesized from AUX area data.
422 return evsel->core.attr.sample_type & PERF_SAMPLE_BRANCH_STACK ||
423 evsel->synth_sample_type & PERF_SAMPLE_BRANCH_STACK;
426 static inline bool evsel__is_dummy_event(struct evsel *evsel)
428 return (evsel->core.attr.type == PERF_TYPE_SOFTWARE) &&
429 (evsel->core.attr.config == PERF_COUNT_SW_DUMMY);
432 struct perf_env *evsel__env(struct evsel *evsel);
434 int evsel__store_ids(struct evsel *evsel, struct evlist *evlist);
436 #endif /* __PERF_EVSEL_H */