evsel.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __PERF_EVSEL_H
  3. #define __PERF_EVSEL_H 1
  4. #include <linux/list.h>
  5. #include <stdbool.h>
  6. #include <sys/types.h>
  7. #include <linux/perf_event.h>
  8. #include <linux/types.h>
  9. #include <internal/evsel.h>
  10. #include <perf/evsel.h>
  11. #include "symbol_conf.h"
  12. #include <internal/cpumap.h>
  13. struct bpf_object;
  14. struct cgroup;
  15. struct perf_counts;
  16. struct perf_stat_evsel;
  17. union perf_event;
  18. typedef int (evsel__sb_cb_t)(union perf_event *event, void *data);
  19. enum perf_tool_event {
  20. PERF_TOOL_NONE = 0,
  21. PERF_TOOL_DURATION_TIME = 1,
  22. };
  23. /** struct evsel - event selector
  24. *
  25. * @evlist - evlist this evsel is in, if it is in one.
  26. * @core - libperf evsel object
  27. * @name - Can be set to retain the original event name passed by the user,
  28. * so that when showing results in tools such as 'perf stat', we
  29. * show the name used, not some alias.
  30. * @id_pos: the position of the event id (PERF_SAMPLE_ID or
  31. * PERF_SAMPLE_IDENTIFIER) in a sample event i.e. in the array of
  32. * struct perf_record_sample
  33. * @is_pos: the position (counting backwards) of the event id (PERF_SAMPLE_ID or
  34. * PERF_SAMPLE_IDENTIFIER) in a non-sample event i.e. if sample_id_all
  35. * is used there is an id sample appended to non-sample events
  36. * @priv: And what is in its containing unnamed union are tool specific
  37. */
  38. struct evsel {
  39. struct perf_evsel core;
  40. struct evlist *evlist;
  41. off_t id_offset;
  42. int idx;
  43. int id_pos;
  44. int is_pos;
  45. unsigned int sample_size;
  46. /*
  47. * These fields can be set in the parse-events code or similar.
  48. * Please check evsel__clone() to copy them properly so that
  49. * they can be released properly.
  50. */
  51. struct {
  52. char *name;
  53. char *group_name;
  54. const char *pmu_name;
  55. struct tep_event *tp_format;
  56. char *filter;
  57. unsigned long max_events;
  58. double scale;
  59. const char *unit;
  60. struct cgroup *cgrp;
  61. enum perf_tool_event tool_event;
  62. /* parse modifier helper */
  63. int exclude_GH;
  64. int sample_read;
  65. bool snapshot;
  66. bool per_pkg;
  67. bool percore;
  68. bool precise_max;
  69. bool use_uncore_alias;
  70. bool is_libpfm_event;
  71. bool auto_merge_stats;
  72. bool collect_stat;
  73. bool weak_group;
  74. int bpf_fd;
  75. struct bpf_object *bpf_obj;
  76. };
  77. /*
  78. * metric fields are similar, but needs more care as they can have
  79. * references to other metric (evsel).
  80. */
  81. const char * metric_expr;
  82. const char * metric_name;
  83. struct evsel **metric_events;
  84. struct evsel *metric_leader;
  85. void *handler;
  86. struct perf_counts *counts;
  87. struct perf_counts *prev_raw_counts;
  88. unsigned long nr_events_printed;
  89. struct perf_stat_evsel *stats;
  90. void *priv;
  91. u64 db_id;
  92. bool uniquified_name;
  93. bool supported;
  94. bool needs_swap;
  95. bool disabled;
  96. bool no_aux_samples;
  97. bool immediate;
  98. bool tracking;
  99. bool ignore_missing_thread;
  100. bool forced_leader;
  101. bool cmdline_group_boundary;
  102. bool merged_stat;
  103. bool reset_group;
  104. bool errored;
  105. unsigned long *per_pkg_mask;
  106. struct evsel *leader;
  107. struct list_head config_terms;
  108. int err;
  109. int cpu_iter;
  110. struct {
  111. evsel__sb_cb_t *cb;
  112. void *data;
  113. } side_band;
  114. /*
  115. * For reporting purposes, an evsel sample can have a callchain
  116. * synthesized from AUX area data. Keep track of synthesized sample
  117. * types here. Note, the recorded sample_type cannot be changed because
  118. * it is needed to continue to parse events.
  119. * See also evsel__has_callchain().
  120. */
  121. __u64 synth_sample_type;
  122. };
  123. struct perf_missing_features {
  124. bool sample_id_all;
  125. bool exclude_guest;
  126. bool mmap2;
  127. bool cloexec;
  128. bool clockid;
  129. bool clockid_wrong;
  130. bool lbr_flags;
  131. bool write_backward;
  132. bool group_read;
  133. bool ksymbol;
  134. bool bpf;
  135. bool aux_output;
  136. bool branch_hw_idx;
  137. bool cgroup;
  138. };
  139. extern struct perf_missing_features perf_missing_features;
  140. struct perf_cpu_map;
  141. struct target;
  142. struct thread_map;
  143. struct record_opts;
  144. static inline struct perf_cpu_map *evsel__cpus(struct evsel *evsel)
  145. {
  146. return perf_evsel__cpus(&evsel->core);
  147. }
  148. static inline int evsel__nr_cpus(struct evsel *evsel)
  149. {
  150. return evsel__cpus(evsel)->nr;
  151. }
  152. void perf_counts_values__scale(struct perf_counts_values *count,
  153. bool scale, s8 *pscaled);
  154. void evsel__compute_deltas(struct evsel *evsel, int cpu, int thread,
  155. struct perf_counts_values *count);
  156. int evsel__object_config(size_t object_size,
  157. int (*init)(struct evsel *evsel),
  158. void (*fini)(struct evsel *evsel));
  159. struct perf_pmu *evsel__find_pmu(struct evsel *evsel);
  160. bool evsel__is_aux_event(struct evsel *evsel);
  161. struct evsel *evsel__new_idx(struct perf_event_attr *attr, int idx);
  162. static inline struct evsel *evsel__new(struct perf_event_attr *attr)
  163. {
  164. return evsel__new_idx(attr, 0);
  165. }
  166. struct evsel *evsel__clone(struct evsel *orig);
  167. struct evsel *evsel__newtp_idx(const char *sys, const char *name, int idx);
  168. /*
  169. * Returns pointer with encoded error via <linux/err.h> interface.
  170. */
  171. static inline struct evsel *evsel__newtp(const char *sys, const char *name)
  172. {
  173. return evsel__newtp_idx(sys, name, 0);
  174. }
  175. struct evsel *evsel__new_cycles(bool precise);
  176. struct tep_event *event_format__new(const char *sys, const char *name);
  177. void evsel__init(struct evsel *evsel, struct perf_event_attr *attr, int idx);
  178. void evsel__exit(struct evsel *evsel);
  179. void evsel__delete(struct evsel *evsel);
  180. struct callchain_param;
  181. void evsel__config(struct evsel *evsel, struct record_opts *opts,
  182. struct callchain_param *callchain);
  183. void evsel__config_callchain(struct evsel *evsel, struct record_opts *opts,
  184. struct callchain_param *callchain);
  185. int __evsel__sample_size(u64 sample_type);
  186. void evsel__calc_id_pos(struct evsel *evsel);
  187. bool evsel__is_cache_op_valid(u8 type, u8 op);
  188. #define EVSEL__MAX_ALIASES 8
  189. extern const char *evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX][EVSEL__MAX_ALIASES];
  190. extern const char *evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX][EVSEL__MAX_ALIASES];
  191. extern const char *evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX][EVSEL__MAX_ALIASES];
  192. extern const char *evsel__hw_names[PERF_COUNT_HW_MAX];
  193. extern const char *evsel__sw_names[PERF_COUNT_SW_MAX];
  194. int __evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result, char *bf, size_t size);
  195. const char *evsel__name(struct evsel *evsel);
  196. const char *evsel__group_name(struct evsel *evsel);
  197. int evsel__group_desc(struct evsel *evsel, char *buf, size_t size);
  198. void __evsel__set_sample_bit(struct evsel *evsel, enum perf_event_sample_format bit);
  199. void __evsel__reset_sample_bit(struct evsel *evsel, enum perf_event_sample_format bit);
  200. #define evsel__set_sample_bit(evsel, bit) \
  201. __evsel__set_sample_bit(evsel, PERF_SAMPLE_##bit)
  202. #define evsel__reset_sample_bit(evsel, bit) \
  203. __evsel__reset_sample_bit(evsel, PERF_SAMPLE_##bit)
  204. void evsel__set_sample_id(struct evsel *evsel, bool use_sample_identifier);
  205. int evsel__set_filter(struct evsel *evsel, const char *filter);
  206. int evsel__append_tp_filter(struct evsel *evsel, const char *filter);
  207. int evsel__append_addr_filter(struct evsel *evsel, const char *filter);
  208. int evsel__enable_cpu(struct evsel *evsel, int cpu);
  209. int evsel__enable(struct evsel *evsel);
  210. int evsel__disable(struct evsel *evsel);
  211. int evsel__disable_cpu(struct evsel *evsel, int cpu);
  212. int evsel__open_per_cpu(struct evsel *evsel, struct perf_cpu_map *cpus, int cpu);
  213. int evsel__open_per_thread(struct evsel *evsel, struct perf_thread_map *threads);
  214. int evsel__open(struct evsel *evsel, struct perf_cpu_map *cpus,
  215. struct perf_thread_map *threads);
  216. void evsel__close(struct evsel *evsel);
  217. struct perf_sample;
  218. void *evsel__rawptr(struct evsel *evsel, struct perf_sample *sample, const char *name);
  219. u64 evsel__intval(struct evsel *evsel, struct perf_sample *sample, const char *name);
  220. static inline char *evsel__strval(struct evsel *evsel, struct perf_sample *sample, const char *name)
  221. {
  222. return evsel__rawptr(evsel, sample, name);
  223. }
  224. struct tep_format_field;
  225. u64 format_field__intval(struct tep_format_field *field, struct perf_sample *sample, bool needs_swap);
  226. struct tep_format_field *evsel__field(struct evsel *evsel, const char *name);
  227. #define evsel__match(evsel, t, c) \
  228. (evsel->core.attr.type == PERF_TYPE_##t && \
  229. evsel->core.attr.config == PERF_COUNT_##c)
  230. static inline bool evsel__match2(struct evsel *e1, struct evsel *e2)
  231. {
  232. return (e1->core.attr.type == e2->core.attr.type) &&
  233. (e1->core.attr.config == e2->core.attr.config);
  234. }
  235. int evsel__read_counter(struct evsel *evsel, int cpu, int thread);
  236. int __evsel__read_on_cpu(struct evsel *evsel, int cpu, int thread, bool scale);
  237. /**
  238. * evsel__read_on_cpu - Read out the results on a CPU and thread
  239. *
  240. * @evsel - event selector to read value
  241. * @cpu - CPU of interest
  242. * @thread - thread of interest
  243. */
  244. static inline int evsel__read_on_cpu(struct evsel *evsel, int cpu, int thread)
  245. {
  246. return __evsel__read_on_cpu(evsel, cpu, thread, false);
  247. }
  248. /**
  249. * evsel__read_on_cpu_scaled - Read out the results on a CPU and thread, scaled
  250. *
  251. * @evsel - event selector to read value
  252. * @cpu - CPU of interest
  253. * @thread - thread of interest
  254. */
  255. static inline int evsel__read_on_cpu_scaled(struct evsel *evsel, int cpu, int thread)
  256. {
  257. return __evsel__read_on_cpu(evsel, cpu, thread, true);
  258. }
  259. int evsel__parse_sample(struct evsel *evsel, union perf_event *event,
  260. struct perf_sample *sample);
  261. int evsel__parse_sample_timestamp(struct evsel *evsel, union perf_event *event,
  262. u64 *timestamp);
  263. static inline struct evsel *evsel__next(struct evsel *evsel)
  264. {
  265. return list_entry(evsel->core.node.next, struct evsel, core.node);
  266. }
  267. static inline struct evsel *evsel__prev(struct evsel *evsel)
  268. {
  269. return list_entry(evsel->core.node.prev, struct evsel, core.node);
  270. }
  271. /**
  272. * evsel__is_group_leader - Return whether given evsel is a leader event
  273. *
  274. * @evsel - evsel selector to be tested
  275. *
  276. * Return %true if @evsel is a group leader or a stand-alone event
  277. */
  278. static inline bool evsel__is_group_leader(const struct evsel *evsel)
  279. {
  280. return evsel->leader == evsel;
  281. }
  282. /**
  283. * evsel__is_group_event - Return whether given evsel is a group event
  284. *
  285. * @evsel - evsel selector to be tested
  286. *
  287. * Return %true iff event group view is enabled and @evsel is a actual group
  288. * leader which has other members in the group
  289. */
  290. static inline bool evsel__is_group_event(struct evsel *evsel)
  291. {
  292. if (!symbol_conf.event_group)
  293. return false;
  294. return evsel__is_group_leader(evsel) && evsel->core.nr_members > 1;
  295. }
  296. bool evsel__is_function_event(struct evsel *evsel);
  297. static inline bool evsel__is_bpf_output(struct evsel *evsel)
  298. {
  299. return evsel__match(evsel, SOFTWARE, SW_BPF_OUTPUT);
  300. }
  301. static inline bool evsel__is_clock(struct evsel *evsel)
  302. {
  303. return evsel__match(evsel, SOFTWARE, SW_CPU_CLOCK) ||
  304. evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK);
  305. }
  306. bool evsel__fallback(struct evsel *evsel, int err, char *msg, size_t msgsize);
  307. int evsel__open_strerror(struct evsel *evsel, struct target *target,
  308. int err, char *msg, size_t size);
  309. static inline int evsel__group_idx(struct evsel *evsel)
  310. {
  311. return evsel->idx - evsel->leader->idx;
  312. }
  313. /* Iterates group WITHOUT the leader. */
  314. #define for_each_group_member(_evsel, _leader) \
  315. for ((_evsel) = list_entry((_leader)->core.node.next, struct evsel, core.node); \
  316. (_evsel) && (_evsel)->leader == (_leader); \
  317. (_evsel) = list_entry((_evsel)->core.node.next, struct evsel, core.node))
  318. /* Iterates group WITH the leader. */
  319. #define for_each_group_evsel(_evsel, _leader) \
  320. for ((_evsel) = _leader; \
  321. (_evsel) && (_evsel)->leader == (_leader); \
  322. (_evsel) = list_entry((_evsel)->core.node.next, struct evsel, core.node))
  323. static inline bool evsel__has_branch_callstack(const struct evsel *evsel)
  324. {
  325. return evsel->core.attr.branch_sample_type & PERF_SAMPLE_BRANCH_CALL_STACK;
  326. }
  327. static inline bool evsel__has_branch_hw_idx(const struct evsel *evsel)
  328. {
  329. return evsel->core.attr.branch_sample_type & PERF_SAMPLE_BRANCH_HW_INDEX;
  330. }
  331. static inline bool evsel__has_callchain(const struct evsel *evsel)
  332. {
  333. /*
  334. * For reporting purposes, an evsel sample can have a recorded callchain
  335. * or a callchain synthesized from AUX area data.
  336. */
  337. return evsel->core.attr.sample_type & PERF_SAMPLE_CALLCHAIN ||
  338. evsel->synth_sample_type & PERF_SAMPLE_CALLCHAIN;
  339. }
  340. static inline bool evsel__has_br_stack(const struct evsel *evsel)
  341. {
  342. /*
  343. * For reporting purposes, an evsel sample can have a recorded branch
  344. * stack or a branch stack synthesized from AUX area data.
  345. */
  346. return evsel->core.attr.sample_type & PERF_SAMPLE_BRANCH_STACK ||
  347. evsel->synth_sample_type & PERF_SAMPLE_BRANCH_STACK;
  348. }
  349. static inline bool evsel__is_dummy_event(struct evsel *evsel)
  350. {
  351. return (evsel->core.attr.type == PERF_TYPE_SOFTWARE) &&
  352. (evsel->core.attr.config == PERF_COUNT_SW_DUMMY);
  353. }
  354. struct perf_env *evsel__env(struct evsel *evsel);
  355. int evsel__store_ids(struct evsel *evsel, struct evlist *evlist);
  356. #endif /* __PERF_EVSEL_H */