evlist.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. #ifndef __PERF_EVLIST_H
  2. #define __PERF_EVLIST_H 1
  3. #include <linux/atomic.h>
  4. #include <linux/list.h>
  5. #include <api/fd/array.h>
  6. #include <stdio.h>
  7. #include "../perf.h"
  8. #include "event.h"
  9. #include "evsel.h"
  10. #include "util.h"
  11. #include "auxtrace.h"
  12. #include <unistd.h>
  13. struct pollfd;
  14. struct thread_map;
  15. struct cpu_map;
  16. struct record_opts;
  17. #define PERF_EVLIST__HLIST_BITS 8
  18. #define PERF_EVLIST__HLIST_SIZE (1 << PERF_EVLIST__HLIST_BITS)
  19. /**
  20. * struct perf_mmap - perf's ring buffer mmap details
  21. *
  22. * @refcnt - e.g. code using PERF_EVENT_IOC_SET_OUTPUT to share this
  23. */
  24. struct perf_mmap {
  25. void *base;
  26. int mask;
  27. int fd;
  28. atomic_t refcnt;
  29. u64 prev;
  30. struct auxtrace_mmap auxtrace_mmap;
  31. char event_copy[PERF_SAMPLE_MAX_SIZE] __attribute__((aligned(8)));
  32. };
  33. static inline size_t
  34. perf_mmap__mmap_len(struct perf_mmap *map)
  35. {
  36. return map->mask + 1 + page_size;
  37. }
  38. /*
  39. * State machine of bkw_mmap_state:
  40. *
  41. * .________________(forbid)_____________.
  42. * | V
  43. * NOTREADY --(0)--> RUNNING --(1)--> DATA_PENDING --(2)--> EMPTY
  44. * ^ ^ | ^ |
  45. * | |__(forbid)____/ |___(forbid)___/|
  46. * | |
  47. * \_________________(3)_______________/
  48. *
  49. * NOTREADY : Backward ring buffers are not ready
  50. * RUNNING : Backward ring buffers are recording
  51. * DATA_PENDING : We are required to collect data from backward ring buffers
  52. * EMPTY : We have collected data from backward ring buffers.
  53. *
  54. * (0): Setup backward ring buffer
  55. * (1): Pause ring buffers for reading
  56. * (2): Read from ring buffers
  57. * (3): Resume ring buffers for recording
  58. */
  59. enum bkw_mmap_state {
  60. BKW_MMAP_NOTREADY,
  61. BKW_MMAP_RUNNING,
  62. BKW_MMAP_DATA_PENDING,
  63. BKW_MMAP_EMPTY,
  64. };
  65. struct perf_evlist {
  66. struct list_head entries;
  67. struct hlist_head heads[PERF_EVLIST__HLIST_SIZE];
  68. int nr_entries;
  69. int nr_groups;
  70. int nr_mmaps;
  71. bool overwrite;
  72. bool enabled;
  73. bool has_user_cpus;
  74. size_t mmap_len;
  75. int id_pos;
  76. int is_pos;
  77. u64 combined_sample_type;
  78. enum bkw_mmap_state bkw_mmap_state;
  79. struct {
  80. int cork_fd;
  81. pid_t pid;
  82. } workload;
  83. struct fdarray pollfd;
  84. struct perf_mmap *mmap;
  85. struct perf_mmap *backward_mmap;
  86. struct thread_map *threads;
  87. struct cpu_map *cpus;
  88. struct perf_evsel *selected;
  89. struct events_stats stats;
  90. struct perf_env *env;
  91. };
  92. struct perf_evsel_str_handler {
  93. const char *name;
  94. void *handler;
  95. };
  96. struct perf_evlist *perf_evlist__new(void);
  97. struct perf_evlist *perf_evlist__new_default(void);
  98. struct perf_evlist *perf_evlist__new_dummy(void);
  99. void perf_evlist__init(struct perf_evlist *evlist, struct cpu_map *cpus,
  100. struct thread_map *threads);
  101. void perf_evlist__exit(struct perf_evlist *evlist);
  102. void perf_evlist__delete(struct perf_evlist *evlist);
  103. void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry);
  104. void perf_evlist__remove(struct perf_evlist *evlist, struct perf_evsel *evsel);
  105. int perf_evlist__add_default(struct perf_evlist *evlist);
  106. int __perf_evlist__add_default_attrs(struct perf_evlist *evlist,
  107. struct perf_event_attr *attrs, size_t nr_attrs);
  108. #define perf_evlist__add_default_attrs(evlist, array) \
  109. __perf_evlist__add_default_attrs(evlist, array, ARRAY_SIZE(array))
  110. int perf_evlist__add_dummy(struct perf_evlist *evlist);
  111. int perf_evlist__add_newtp(struct perf_evlist *evlist,
  112. const char *sys, const char *name, void *handler);
  113. void __perf_evlist__set_sample_bit(struct perf_evlist *evlist,
  114. enum perf_event_sample_format bit);
  115. void __perf_evlist__reset_sample_bit(struct perf_evlist *evlist,
  116. enum perf_event_sample_format bit);
  117. #define perf_evlist__set_sample_bit(evlist, bit) \
  118. __perf_evlist__set_sample_bit(evlist, PERF_SAMPLE_##bit)
  119. #define perf_evlist__reset_sample_bit(evlist, bit) \
  120. __perf_evlist__reset_sample_bit(evlist, PERF_SAMPLE_##bit)
  121. int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter);
  122. int perf_evlist__set_filter_pid(struct perf_evlist *evlist, pid_t pid);
  123. int perf_evlist__set_filter_pids(struct perf_evlist *evlist, size_t npids, pid_t *pids);
  124. struct perf_evsel *
  125. perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id);
  126. struct perf_evsel *
  127. perf_evlist__find_tracepoint_by_name(struct perf_evlist *evlist,
  128. const char *name);
  129. void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel,
  130. int cpu, int thread, u64 id);
  131. int perf_evlist__id_add_fd(struct perf_evlist *evlist,
  132. struct perf_evsel *evsel,
  133. int cpu, int thread, int fd);
  134. int perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd);
  135. int perf_evlist__alloc_pollfd(struct perf_evlist *evlist);
  136. int perf_evlist__filter_pollfd(struct perf_evlist *evlist, short revents_and_mask);
  137. int perf_evlist__poll(struct perf_evlist *evlist, int timeout);
  138. struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id);
  139. struct perf_evsel *perf_evlist__id2evsel_strict(struct perf_evlist *evlist,
  140. u64 id);
  141. struct perf_sample_id *perf_evlist__id2sid(struct perf_evlist *evlist, u64 id);
  142. void perf_evlist__toggle_bkw_mmap(struct perf_evlist *evlist, enum bkw_mmap_state state);
  143. union perf_event *perf_mmap__read_forward(struct perf_mmap *map, bool check_messup);
  144. union perf_event *perf_mmap__read_backward(struct perf_mmap *map);
  145. void perf_mmap__read_catchup(struct perf_mmap *md);
  146. void perf_mmap__consume(struct perf_mmap *md, bool overwrite);
  147. union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx);
  148. union perf_event *perf_evlist__mmap_read_forward(struct perf_evlist *evlist,
  149. int idx);
  150. union perf_event *perf_evlist__mmap_read_backward(struct perf_evlist *evlist,
  151. int idx);
  152. void perf_evlist__mmap_read_catchup(struct perf_evlist *evlist, int idx);
  153. void perf_evlist__mmap_consume(struct perf_evlist *evlist, int idx);
  154. int perf_evlist__open(struct perf_evlist *evlist);
  155. void perf_evlist__close(struct perf_evlist *evlist);
  156. struct callchain_param;
  157. void perf_evlist__set_id_pos(struct perf_evlist *evlist);
  158. bool perf_can_sample_identifier(void);
  159. bool perf_can_record_switch_events(void);
  160. bool perf_can_record_cpu_wide(void);
  161. void perf_evlist__config(struct perf_evlist *evlist, struct record_opts *opts,
  162. struct callchain_param *callchain);
  163. int record_opts__config(struct record_opts *opts);
  164. int perf_evlist__prepare_workload(struct perf_evlist *evlist,
  165. struct target *target,
  166. const char *argv[], bool pipe_output,
  167. void (*exec_error)(int signo, siginfo_t *info,
  168. void *ucontext));
  169. int perf_evlist__start_workload(struct perf_evlist *evlist);
  170. struct option;
  171. int __perf_evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str);
  172. int perf_evlist__parse_mmap_pages(const struct option *opt,
  173. const char *str,
  174. int unset);
  175. unsigned long perf_event_mlock_kb_in_pages(void);
  176. int perf_evlist__mmap_ex(struct perf_evlist *evlist, unsigned int pages,
  177. bool overwrite, unsigned int auxtrace_pages,
  178. bool auxtrace_overwrite);
  179. int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages,
  180. bool overwrite);
  181. void perf_evlist__munmap(struct perf_evlist *evlist);
  182. void perf_evlist__disable(struct perf_evlist *evlist);
  183. void perf_evlist__enable(struct perf_evlist *evlist);
  184. void perf_evlist__toggle_enable(struct perf_evlist *evlist);
  185. int perf_evlist__enable_event_idx(struct perf_evlist *evlist,
  186. struct perf_evsel *evsel, int idx);
  187. void perf_evlist__set_selected(struct perf_evlist *evlist,
  188. struct perf_evsel *evsel);
  189. void perf_evlist__set_maps(struct perf_evlist *evlist, struct cpu_map *cpus,
  190. struct thread_map *threads);
  191. int perf_evlist__create_maps(struct perf_evlist *evlist, struct target *target);
  192. int perf_evlist__apply_filters(struct perf_evlist *evlist, struct perf_evsel **err_evsel);
  193. void __perf_evlist__set_leader(struct list_head *list);
  194. void perf_evlist__set_leader(struct perf_evlist *evlist);
  195. u64 perf_evlist__read_format(struct perf_evlist *evlist);
  196. u64 __perf_evlist__combined_sample_type(struct perf_evlist *evlist);
  197. u64 perf_evlist__combined_sample_type(struct perf_evlist *evlist);
  198. u64 perf_evlist__combined_branch_type(struct perf_evlist *evlist);
  199. bool perf_evlist__sample_id_all(struct perf_evlist *evlist);
  200. u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist);
  201. int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event,
  202. struct perf_sample *sample);
  203. bool perf_evlist__valid_sample_type(struct perf_evlist *evlist);
  204. bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist);
  205. bool perf_evlist__valid_read_format(struct perf_evlist *evlist);
  206. void perf_evlist__splice_list_tail(struct perf_evlist *evlist,
  207. struct list_head *list);
  208. static inline struct perf_evsel *perf_evlist__first(struct perf_evlist *evlist)
  209. {
  210. return list_entry(evlist->entries.next, struct perf_evsel, node);
  211. }
  212. static inline struct perf_evsel *perf_evlist__last(struct perf_evlist *evlist)
  213. {
  214. return list_entry(evlist->entries.prev, struct perf_evsel, node);
  215. }
  216. size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp);
  217. int perf_evlist__strerror_open(struct perf_evlist *evlist, int err, char *buf, size_t size);
  218. int perf_evlist__strerror_mmap(struct perf_evlist *evlist, int err, char *buf, size_t size);
  219. static inline u64 perf_mmap__read_head(struct perf_mmap *mm)
  220. {
  221. struct perf_event_mmap_page *pc = mm->base;
  222. u64 head = ACCESS_ONCE(pc->data_head);
  223. rmb();
  224. return head;
  225. }
  226. static inline void perf_mmap__write_tail(struct perf_mmap *md, u64 tail)
  227. {
  228. struct perf_event_mmap_page *pc = md->base;
  229. /*
  230. * ensure all reads are done before we write the tail out.
  231. */
  232. mb();
  233. pc->data_tail = tail;
  234. }
  235. bool perf_evlist__can_select_event(struct perf_evlist *evlist, const char *str);
  236. void perf_evlist__to_front(struct perf_evlist *evlist,
  237. struct perf_evsel *move_evsel);
  238. /**
  239. * __evlist__for_each_entry - iterate thru all the evsels
  240. * @list: list_head instance to iterate
  241. * @evsel: struct evsel iterator
  242. */
  243. #define __evlist__for_each_entry(list, evsel) \
  244. list_for_each_entry(evsel, list, node)
  245. /**
  246. * evlist__for_each_entry - iterate thru all the evsels
  247. * @evlist: evlist instance to iterate
  248. * @evsel: struct evsel iterator
  249. */
  250. #define evlist__for_each_entry(evlist, evsel) \
  251. __evlist__for_each_entry(&(evlist)->entries, evsel)
  252. /**
  253. * __evlist__for_each_entry_continue - continue iteration thru all the evsels
  254. * @list: list_head instance to iterate
  255. * @evsel: struct evsel iterator
  256. */
  257. #define __evlist__for_each_entry_continue(list, evsel) \
  258. list_for_each_entry_continue(evsel, list, node)
  259. /**
  260. * evlist__for_each_entry_continue - continue iteration thru all the evsels
  261. * @evlist: evlist instance to iterate
  262. * @evsel: struct evsel iterator
  263. */
  264. #define evlist__for_each_entry_continue(evlist, evsel) \
  265. __evlist__for_each_entry_continue(&(evlist)->entries, evsel)
  266. /**
  267. * __evlist__for_each_entry_reverse - iterate thru all the evsels in reverse order
  268. * @list: list_head instance to iterate
  269. * @evsel: struct evsel iterator
  270. */
  271. #define __evlist__for_each_entry_reverse(list, evsel) \
  272. list_for_each_entry_reverse(evsel, list, node)
  273. /**
  274. * evlist__for_each_entry_reverse - iterate thru all the evsels in reverse order
  275. * @evlist: evlist instance to iterate
  276. * @evsel: struct evsel iterator
  277. */
  278. #define evlist__for_each_entry_reverse(evlist, evsel) \
  279. __evlist__for_each_entry_reverse(&(evlist)->entries, evsel)
  280. /**
  281. * __evlist__for_each_entry_safe - safely iterate thru all the evsels
  282. * @list: list_head instance to iterate
  283. * @tmp: struct evsel temp iterator
  284. * @evsel: struct evsel iterator
  285. */
  286. #define __evlist__for_each_entry_safe(list, tmp, evsel) \
  287. list_for_each_entry_safe(evsel, tmp, list, node)
  288. /**
  289. * evlist__for_each_entry_safe - safely iterate thru all the evsels
  290. * @evlist: evlist instance to iterate
  291. * @evsel: struct evsel iterator
  292. * @tmp: struct evsel temp iterator
  293. */
  294. #define evlist__for_each_entry_safe(evlist, tmp, evsel) \
  295. __evlist__for_each_entry_safe(&(evlist)->entries, tmp, evsel)
  296. void perf_evlist__set_tracking_event(struct perf_evlist *evlist,
  297. struct perf_evsel *tracking_evsel);
  298. void perf_event_attr__set_max_precise_ip(struct perf_event_attr *attr);
  299. struct perf_evsel *
  300. perf_evlist__find_evsel_by_str(struct perf_evlist *evlist, const char *str);
  301. struct perf_evsel *perf_evlist__event2evsel(struct perf_evlist *evlist,
  302. union perf_event *event);
  303. #endif /* __PERF_EVLIST_H */