evsel.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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 (perf_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. char *filter;
  42. struct perf_counts *counts;
  43. struct perf_counts *prev_raw_counts;
  44. int idx;
  45. unsigned long max_events;
  46. unsigned long nr_events_printed;
  47. char *name;
  48. double scale;
  49. const char *unit;
  50. struct tep_event *tp_format;
  51. off_t id_offset;
  52. struct perf_stat_evsel *stats;
  53. void *priv;
  54. u64 db_id;
  55. struct cgroup *cgrp;
  56. void *handler;
  57. unsigned int sample_size;
  58. int id_pos;
  59. int is_pos;
  60. enum perf_tool_event tool_event;
  61. bool uniquified_name;
  62. bool snapshot;
  63. bool supported;
  64. bool needs_swap;
  65. bool disabled;
  66. bool no_aux_samples;
  67. bool immediate;
  68. bool tracking;
  69. bool per_pkg;
  70. bool precise_max;
  71. bool ignore_missing_thread;
  72. bool forced_leader;
  73. bool use_uncore_alias;
  74. /* parse modifier helper */
  75. int exclude_GH;
  76. int sample_read;
  77. unsigned long *per_pkg_mask;
  78. struct evsel *leader;
  79. char *group_name;
  80. bool cmdline_group_boundary;
  81. struct list_head config_terms;
  82. struct bpf_object *bpf_obj;
  83. int bpf_fd;
  84. bool auto_merge_stats;
  85. bool merged_stat;
  86. const char * metric_expr;
  87. const char * metric_name;
  88. struct evsel **metric_events;
  89. struct evsel *metric_leader;
  90. bool collect_stat;
  91. bool weak_group;
  92. bool percore;
  93. const char *pmu_name;
  94. struct {
  95. perf_evsel__sb_cb_t *cb;
  96. void *data;
  97. } side_band;
  98. };
  99. struct perf_missing_features {
  100. bool sample_id_all;
  101. bool exclude_guest;
  102. bool mmap2;
  103. bool cloexec;
  104. bool clockid;
  105. bool clockid_wrong;
  106. bool lbr_flags;
  107. bool write_backward;
  108. bool group_read;
  109. bool ksymbol;
  110. bool bpf;
  111. bool aux_output;
  112. };
  113. extern struct perf_missing_features perf_missing_features;
  114. struct perf_cpu_map;
  115. struct target;
  116. struct thread_map;
  117. struct record_opts;
  118. static inline struct perf_cpu_map *evsel__cpus(struct evsel *evsel)
  119. {
  120. return perf_evsel__cpus(&evsel->core);
  121. }
  122. static inline int perf_evsel__nr_cpus(struct evsel *evsel)
  123. {
  124. return evsel__cpus(evsel)->nr;
  125. }
  126. void perf_counts_values__scale(struct perf_counts_values *count,
  127. bool scale, s8 *pscaled);
  128. void perf_evsel__compute_deltas(struct evsel *evsel, int cpu, int thread,
  129. struct perf_counts_values *count);
  130. int perf_evsel__object_config(size_t object_size,
  131. int (*init)(struct evsel *evsel),
  132. void (*fini)(struct evsel *evsel));
  133. struct evsel *perf_evsel__new_idx(struct perf_event_attr *attr, int idx);
  134. static inline struct evsel *evsel__new(struct perf_event_attr *attr)
  135. {
  136. return perf_evsel__new_idx(attr, 0);
  137. }
  138. struct evsel *perf_evsel__newtp_idx(const char *sys, const char *name, int idx);
  139. /*
  140. * Returns pointer with encoded error via <linux/err.h> interface.
  141. */
  142. static inline struct evsel *perf_evsel__newtp(const char *sys, const char *name)
  143. {
  144. return perf_evsel__newtp_idx(sys, name, 0);
  145. }
  146. struct evsel *perf_evsel__new_cycles(bool precise);
  147. struct tep_event *event_format__new(const char *sys, const char *name);
  148. void evsel__init(struct evsel *evsel, struct perf_event_attr *attr, int idx);
  149. void perf_evsel__exit(struct evsel *evsel);
  150. void evsel__delete(struct evsel *evsel);
  151. struct callchain_param;
  152. void perf_evsel__config(struct evsel *evsel,
  153. struct record_opts *opts,
  154. struct callchain_param *callchain);
  155. void perf_evsel__config_callchain(struct evsel *evsel,
  156. struct record_opts *opts,
  157. struct callchain_param *callchain);
  158. int __perf_evsel__sample_size(u64 sample_type);
  159. void perf_evsel__calc_id_pos(struct evsel *evsel);
  160. bool perf_evsel__is_cache_op_valid(u8 type, u8 op);
  161. #define PERF_EVSEL__MAX_ALIASES 8
  162. extern const char *perf_evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX]
  163. [PERF_EVSEL__MAX_ALIASES];
  164. extern const char *perf_evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX]
  165. [PERF_EVSEL__MAX_ALIASES];
  166. extern const char *perf_evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX]
  167. [PERF_EVSEL__MAX_ALIASES];
  168. extern const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX];
  169. extern const char *perf_evsel__sw_names[PERF_COUNT_SW_MAX];
  170. int __perf_evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result,
  171. char *bf, size_t size);
  172. const char *perf_evsel__name(struct evsel *evsel);
  173. const char *perf_evsel__group_name(struct evsel *evsel);
  174. int perf_evsel__group_desc(struct evsel *evsel, char *buf, size_t size);
  175. void __perf_evsel__set_sample_bit(struct evsel *evsel,
  176. enum perf_event_sample_format bit);
  177. void __perf_evsel__reset_sample_bit(struct evsel *evsel,
  178. enum perf_event_sample_format bit);
  179. #define perf_evsel__set_sample_bit(evsel, bit) \
  180. __perf_evsel__set_sample_bit(evsel, PERF_SAMPLE_##bit)
  181. #define perf_evsel__reset_sample_bit(evsel, bit) \
  182. __perf_evsel__reset_sample_bit(evsel, PERF_SAMPLE_##bit)
  183. void perf_evsel__set_sample_id(struct evsel *evsel,
  184. bool use_sample_identifier);
  185. int perf_evsel__set_filter(struct evsel *evsel, const char *filter);
  186. int perf_evsel__append_tp_filter(struct evsel *evsel, const char *filter);
  187. int perf_evsel__append_addr_filter(struct evsel *evsel,
  188. const char *filter);
  189. int evsel__enable(struct evsel *evsel);
  190. int evsel__disable(struct evsel *evsel);
  191. int perf_evsel__open_per_cpu(struct evsel *evsel,
  192. struct perf_cpu_map *cpus);
  193. int perf_evsel__open_per_thread(struct evsel *evsel,
  194. struct perf_thread_map *threads);
  195. int evsel__open(struct evsel *evsel, struct perf_cpu_map *cpus,
  196. struct perf_thread_map *threads);
  197. void evsel__close(struct evsel *evsel);
  198. struct perf_sample;
  199. void *perf_evsel__rawptr(struct evsel *evsel, struct perf_sample *sample,
  200. const char *name);
  201. u64 perf_evsel__intval(struct evsel *evsel, struct perf_sample *sample,
  202. const char *name);
  203. static inline char *perf_evsel__strval(struct evsel *evsel,
  204. struct perf_sample *sample,
  205. const char *name)
  206. {
  207. return perf_evsel__rawptr(evsel, sample, name);
  208. }
  209. struct tep_format_field;
  210. u64 format_field__intval(struct tep_format_field *field, struct perf_sample *sample, bool needs_swap);
  211. struct tep_format_field *perf_evsel__field(struct evsel *evsel, const char *name);
  212. #define perf_evsel__match(evsel, t, c) \
  213. (evsel->core.attr.type == PERF_TYPE_##t && \
  214. evsel->core.attr.config == PERF_COUNT_##c)
  215. static inline bool perf_evsel__match2(struct evsel *e1,
  216. struct evsel *e2)
  217. {
  218. return (e1->core.attr.type == e2->core.attr.type) &&
  219. (e1->core.attr.config == e2->core.attr.config);
  220. }
  221. #define perf_evsel__cmp(a, b) \
  222. ((a) && \
  223. (b) && \
  224. (a)->core.attr.type == (b)->core.attr.type && \
  225. (a)->core.attr.config == (b)->core.attr.config)
  226. int perf_evsel__read_counter(struct evsel *evsel, int cpu, int thread);
  227. int __perf_evsel__read_on_cpu(struct evsel *evsel,
  228. int cpu, int thread, bool scale);
  229. /**
  230. * perf_evsel__read_on_cpu - Read out the results on a CPU and thread
  231. *
  232. * @evsel - event selector to read value
  233. * @cpu - CPU of interest
  234. * @thread - thread of interest
  235. */
  236. static inline int perf_evsel__read_on_cpu(struct evsel *evsel,
  237. int cpu, int thread)
  238. {
  239. return __perf_evsel__read_on_cpu(evsel, cpu, thread, false);
  240. }
  241. /**
  242. * perf_evsel__read_on_cpu_scaled - Read out the results on a CPU and thread, scaled
  243. *
  244. * @evsel - event selector to read value
  245. * @cpu - CPU of interest
  246. * @thread - thread of interest
  247. */
  248. static inline int perf_evsel__read_on_cpu_scaled(struct evsel *evsel,
  249. int cpu, int thread)
  250. {
  251. return __perf_evsel__read_on_cpu(evsel, cpu, thread, true);
  252. }
  253. int perf_evsel__parse_sample(struct evsel *evsel, union perf_event *event,
  254. struct perf_sample *sample);
  255. int perf_evsel__parse_sample_timestamp(struct evsel *evsel,
  256. union perf_event *event,
  257. u64 *timestamp);
  258. static inline struct evsel *perf_evsel__next(struct evsel *evsel)
  259. {
  260. return list_entry(evsel->core.node.next, struct evsel, core.node);
  261. }
  262. static inline struct evsel *perf_evsel__prev(struct evsel *evsel)
  263. {
  264. return list_entry(evsel->core.node.prev, struct evsel, core.node);
  265. }
  266. /**
  267. * perf_evsel__is_group_leader - Return whether given evsel is a leader event
  268. *
  269. * @evsel - evsel selector to be tested
  270. *
  271. * Return %true if @evsel is a group leader or a stand-alone event
  272. */
  273. static inline bool perf_evsel__is_group_leader(const struct evsel *evsel)
  274. {
  275. return evsel->leader == evsel;
  276. }
  277. /**
  278. * perf_evsel__is_group_event - Return whether given evsel is a group event
  279. *
  280. * @evsel - evsel selector to be tested
  281. *
  282. * Return %true iff event group view is enabled and @evsel is a actual group
  283. * leader which has other members in the group
  284. */
  285. static inline bool perf_evsel__is_group_event(struct evsel *evsel)
  286. {
  287. if (!symbol_conf.event_group)
  288. return false;
  289. return perf_evsel__is_group_leader(evsel) && evsel->core.nr_members > 1;
  290. }
  291. bool perf_evsel__is_function_event(struct evsel *evsel);
  292. static inline bool perf_evsel__is_bpf_output(struct evsel *evsel)
  293. {
  294. return perf_evsel__match(evsel, SOFTWARE, SW_BPF_OUTPUT);
  295. }
  296. static inline bool perf_evsel__is_clock(struct evsel *evsel)
  297. {
  298. return perf_evsel__match(evsel, SOFTWARE, SW_CPU_CLOCK) ||
  299. perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK);
  300. }
  301. bool perf_evsel__fallback(struct evsel *evsel, int err,
  302. char *msg, size_t msgsize);
  303. int perf_evsel__open_strerror(struct evsel *evsel, struct target *target,
  304. int err, char *msg, size_t size);
  305. static inline int perf_evsel__group_idx(struct evsel *evsel)
  306. {
  307. return evsel->idx - evsel->leader->idx;
  308. }
  309. /* Iterates group WITHOUT the leader. */
  310. #define for_each_group_member(_evsel, _leader) \
  311. for ((_evsel) = list_entry((_leader)->core.node.next, struct evsel, core.node); \
  312. (_evsel) && (_evsel)->leader == (_leader); \
  313. (_evsel) = list_entry((_evsel)->core.node.next, struct evsel, core.node))
  314. /* Iterates group WITH the leader. */
  315. #define for_each_group_evsel(_evsel, _leader) \
  316. for ((_evsel) = _leader; \
  317. (_evsel) && (_evsel)->leader == (_leader); \
  318. (_evsel) = list_entry((_evsel)->core.node.next, struct evsel, core.node))
  319. static inline bool perf_evsel__has_branch_callstack(const struct evsel *evsel)
  320. {
  321. return evsel->core.attr.branch_sample_type & PERF_SAMPLE_BRANCH_CALL_STACK;
  322. }
  323. static inline bool evsel__has_callchain(const struct evsel *evsel)
  324. {
  325. return (evsel->core.attr.sample_type & PERF_SAMPLE_CALLCHAIN) != 0;
  326. }
  327. struct perf_env *perf_evsel__env(struct evsel *evsel);
  328. int perf_evsel__store_ids(struct evsel *evsel, struct evlist *evlist);
  329. #endif /* __PERF_EVSEL_H */