event.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __PERF_RECORD_H
  3. #define __PERF_RECORD_H
  4. /*
  5. * The linux/stddef.h isn't need here, but is needed for __always_inline used
  6. * in files included from uapi/linux/perf_event.h such as
  7. * /usr/include/linux/swab.h and /usr/include/linux/byteorder/little_endian.h,
  8. * detected in at least musl libc, used in Alpine Linux. -acme
  9. */
  10. #include <stdio.h>
  11. #include <linux/stddef.h>
  12. #include <perf/event.h>
  13. #include <linux/types.h>
  14. #include "perf_regs.h"
  15. struct dso;
  16. struct machine;
  17. struct perf_event_attr;
  18. #ifdef __LP64__
  19. /*
  20. * /usr/include/inttypes.h uses just 'lu' for PRIu64, but we end up defining
  21. * __u64 as long long unsigned int, and then -Werror=format= kicks in and
  22. * complains of the mismatched types, so use these two special extra PRI
  23. * macros to overcome that.
  24. */
  25. #define PRI_lu64 "l" PRIu64
  26. #define PRI_lx64 "l" PRIx64
  27. #define PRI_ld64 "l" PRId64
  28. #else
  29. #define PRI_lu64 PRIu64
  30. #define PRI_lx64 PRIx64
  31. #define PRI_ld64 PRId64
  32. #endif
  33. #define PERF_SAMPLE_MASK \
  34. (PERF_SAMPLE_IP | PERF_SAMPLE_TID | \
  35. PERF_SAMPLE_TIME | PERF_SAMPLE_ADDR | \
  36. PERF_SAMPLE_ID | PERF_SAMPLE_STREAM_ID | \
  37. PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD | \
  38. PERF_SAMPLE_IDENTIFIER)
  39. /* perf sample has 16 bits size limit */
  40. #define PERF_SAMPLE_MAX_SIZE (1 << 16)
  41. struct regs_dump {
  42. u64 abi;
  43. u64 mask;
  44. u64 *regs;
  45. /* Cached values/mask filled by first register access. */
  46. u64 cache_regs[PERF_REGS_MAX];
  47. u64 cache_mask;
  48. };
  49. struct stack_dump {
  50. u16 offset;
  51. u64 size;
  52. char *data;
  53. };
  54. struct sample_read_value {
  55. u64 value;
  56. u64 id;
  57. };
  58. struct sample_read {
  59. u64 time_enabled;
  60. u64 time_running;
  61. union {
  62. struct {
  63. u64 nr;
  64. struct sample_read_value *values;
  65. } group;
  66. struct sample_read_value one;
  67. };
  68. };
  69. struct ip_callchain {
  70. u64 nr;
  71. u64 ips[0];
  72. };
  73. struct branch_stack;
  74. enum {
  75. PERF_IP_FLAG_BRANCH = 1ULL << 0,
  76. PERF_IP_FLAG_CALL = 1ULL << 1,
  77. PERF_IP_FLAG_RETURN = 1ULL << 2,
  78. PERF_IP_FLAG_CONDITIONAL = 1ULL << 3,
  79. PERF_IP_FLAG_SYSCALLRET = 1ULL << 4,
  80. PERF_IP_FLAG_ASYNC = 1ULL << 5,
  81. PERF_IP_FLAG_INTERRUPT = 1ULL << 6,
  82. PERF_IP_FLAG_TX_ABORT = 1ULL << 7,
  83. PERF_IP_FLAG_TRACE_BEGIN = 1ULL << 8,
  84. PERF_IP_FLAG_TRACE_END = 1ULL << 9,
  85. PERF_IP_FLAG_IN_TX = 1ULL << 10,
  86. };
  87. #define PERF_IP_FLAG_CHARS "bcrosyiABEx"
  88. #define PERF_BRANCH_MASK (\
  89. PERF_IP_FLAG_BRANCH |\
  90. PERF_IP_FLAG_CALL |\
  91. PERF_IP_FLAG_RETURN |\
  92. PERF_IP_FLAG_CONDITIONAL |\
  93. PERF_IP_FLAG_SYSCALLRET |\
  94. PERF_IP_FLAG_ASYNC |\
  95. PERF_IP_FLAG_INTERRUPT |\
  96. PERF_IP_FLAG_TX_ABORT |\
  97. PERF_IP_FLAG_TRACE_BEGIN |\
  98. PERF_IP_FLAG_TRACE_END)
  99. #define MAX_INSN 16
  100. struct perf_sample {
  101. u64 ip;
  102. u32 pid, tid;
  103. u64 time;
  104. u64 addr;
  105. u64 id;
  106. u64 stream_id;
  107. u64 period;
  108. u64 weight;
  109. u64 transaction;
  110. u64 insn_cnt;
  111. u64 cyc_cnt;
  112. u32 cpu;
  113. u32 raw_size;
  114. u64 data_src;
  115. u64 phys_addr;
  116. u32 flags;
  117. u16 insn_len;
  118. u8 cpumode;
  119. u16 misc;
  120. char insn[MAX_INSN];
  121. void *raw_data;
  122. struct ip_callchain *callchain;
  123. struct branch_stack *branch_stack;
  124. struct regs_dump user_regs;
  125. struct regs_dump intr_regs;
  126. struct stack_dump user_stack;
  127. struct sample_read read;
  128. };
  129. #define PERF_MEM_DATA_SRC_NONE \
  130. (PERF_MEM_S(OP, NA) |\
  131. PERF_MEM_S(LVL, NA) |\
  132. PERF_MEM_S(SNOOP, NA) |\
  133. PERF_MEM_S(LOCK, NA) |\
  134. PERF_MEM_S(TLB, NA))
  135. /* Attribute type for custom synthesized events */
  136. #define PERF_TYPE_SYNTH (INT_MAX + 1U)
  137. /* Attribute config for custom synthesized events */
  138. enum perf_synth_id {
  139. PERF_SYNTH_INTEL_PTWRITE,
  140. PERF_SYNTH_INTEL_MWAIT,
  141. PERF_SYNTH_INTEL_PWRE,
  142. PERF_SYNTH_INTEL_EXSTOP,
  143. PERF_SYNTH_INTEL_PWRX,
  144. PERF_SYNTH_INTEL_CBR,
  145. };
  146. /*
  147. * Raw data formats for synthesized events. Note that 4 bytes of padding are
  148. * present to match the 'size' member of PERF_SAMPLE_RAW data which is always
  149. * 8-byte aligned. That means we must dereference raw_data with an offset of 4.
  150. * Refer perf_sample__synth_ptr() and perf_synth__raw_data(). It also means the
  151. * structure sizes are 4 bytes bigger than the raw_size, refer
  152. * perf_synth__raw_size().
  153. */
  154. struct perf_synth_intel_ptwrite {
  155. u32 padding;
  156. union {
  157. struct {
  158. u32 ip : 1,
  159. reserved : 31;
  160. };
  161. u32 flags;
  162. };
  163. u64 payload;
  164. };
  165. struct perf_synth_intel_mwait {
  166. u32 padding;
  167. u32 reserved;
  168. union {
  169. struct {
  170. u64 hints : 8,
  171. reserved1 : 24,
  172. extensions : 2,
  173. reserved2 : 30;
  174. };
  175. u64 payload;
  176. };
  177. };
  178. struct perf_synth_intel_pwre {
  179. u32 padding;
  180. u32 reserved;
  181. union {
  182. struct {
  183. u64 reserved1 : 7,
  184. hw : 1,
  185. subcstate : 4,
  186. cstate : 4,
  187. reserved2 : 48;
  188. };
  189. u64 payload;
  190. };
  191. };
  192. struct perf_synth_intel_exstop {
  193. u32 padding;
  194. union {
  195. struct {
  196. u32 ip : 1,
  197. reserved : 31;
  198. };
  199. u32 flags;
  200. };
  201. };
  202. struct perf_synth_intel_pwrx {
  203. u32 padding;
  204. u32 reserved;
  205. union {
  206. struct {
  207. u64 deepest_cstate : 4,
  208. last_cstate : 4,
  209. wake_reason : 4,
  210. reserved1 : 52;
  211. };
  212. u64 payload;
  213. };
  214. };
  215. struct perf_synth_intel_cbr {
  216. u32 padding;
  217. union {
  218. struct {
  219. u32 cbr : 8,
  220. reserved1 : 8,
  221. max_nonturbo : 8,
  222. reserved2 : 8;
  223. };
  224. u32 flags;
  225. };
  226. u32 freq;
  227. u32 reserved3;
  228. };
  229. /*
  230. * raw_data is always 4 bytes from an 8-byte boundary, so subtract 4 to get
  231. * 8-byte alignment.
  232. */
  233. static inline void *perf_sample__synth_ptr(struct perf_sample *sample)
  234. {
  235. return sample->raw_data - 4;
  236. }
  237. static inline void *perf_synth__raw_data(void *p)
  238. {
  239. return p + 4;
  240. }
  241. #define perf_synth__raw_size(d) (sizeof(d) - 4)
  242. #define perf_sample__bad_synth_size(s, d) ((s)->raw_size < sizeof(d) - 4)
  243. enum {
  244. PERF_STAT_ROUND_TYPE__INTERVAL = 0,
  245. PERF_STAT_ROUND_TYPE__FINAL = 1,
  246. };
  247. void perf_event__print_totals(void);
  248. struct perf_cpu_map;
  249. struct perf_record_stat_config;
  250. struct perf_stat_config;
  251. struct perf_tool;
  252. void perf_event__read_stat_config(struct perf_stat_config *config,
  253. struct perf_record_stat_config *event);
  254. int perf_event__process_comm(struct perf_tool *tool,
  255. union perf_event *event,
  256. struct perf_sample *sample,
  257. struct machine *machine);
  258. int perf_event__process_lost(struct perf_tool *tool,
  259. union perf_event *event,
  260. struct perf_sample *sample,
  261. struct machine *machine);
  262. int perf_event__process_lost_samples(struct perf_tool *tool,
  263. union perf_event *event,
  264. struct perf_sample *sample,
  265. struct machine *machine);
  266. int perf_event__process_aux(struct perf_tool *tool,
  267. union perf_event *event,
  268. struct perf_sample *sample,
  269. struct machine *machine);
  270. int perf_event__process_itrace_start(struct perf_tool *tool,
  271. union perf_event *event,
  272. struct perf_sample *sample,
  273. struct machine *machine);
  274. int perf_event__process_switch(struct perf_tool *tool,
  275. union perf_event *event,
  276. struct perf_sample *sample,
  277. struct machine *machine);
  278. int perf_event__process_namespaces(struct perf_tool *tool,
  279. union perf_event *event,
  280. struct perf_sample *sample,
  281. struct machine *machine);
  282. int perf_event__process_mmap(struct perf_tool *tool,
  283. union perf_event *event,
  284. struct perf_sample *sample,
  285. struct machine *machine);
  286. int perf_event__process_mmap2(struct perf_tool *tool,
  287. union perf_event *event,
  288. struct perf_sample *sample,
  289. struct machine *machine);
  290. int perf_event__process_fork(struct perf_tool *tool,
  291. union perf_event *event,
  292. struct perf_sample *sample,
  293. struct machine *machine);
  294. int perf_event__process_exit(struct perf_tool *tool,
  295. union perf_event *event,
  296. struct perf_sample *sample,
  297. struct machine *machine);
  298. int perf_event__process_ksymbol(struct perf_tool *tool,
  299. union perf_event *event,
  300. struct perf_sample *sample,
  301. struct machine *machine);
  302. int perf_event__process_bpf(struct perf_tool *tool,
  303. union perf_event *event,
  304. struct perf_sample *sample,
  305. struct machine *machine);
  306. int perf_event__process(struct perf_tool *tool,
  307. union perf_event *event,
  308. struct perf_sample *sample,
  309. struct machine *machine);
  310. struct addr_location;
  311. int machine__resolve(struct machine *machine, struct addr_location *al,
  312. struct perf_sample *sample);
  313. void addr_location__put(struct addr_location *al);
  314. struct thread;
  315. bool is_bts_event(struct perf_event_attr *attr);
  316. bool sample_addr_correlates_sym(struct perf_event_attr *attr);
  317. void thread__resolve(struct thread *thread, struct addr_location *al,
  318. struct perf_sample *sample);
  319. const char *perf_event__name(unsigned int id);
  320. size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp);
  321. size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp);
  322. size_t perf_event__fprintf_mmap2(union perf_event *event, FILE *fp);
  323. size_t perf_event__fprintf_task(union perf_event *event, FILE *fp);
  324. size_t perf_event__fprintf_aux(union perf_event *event, FILE *fp);
  325. size_t perf_event__fprintf_itrace_start(union perf_event *event, FILE *fp);
  326. size_t perf_event__fprintf_switch(union perf_event *event, FILE *fp);
  327. size_t perf_event__fprintf_thread_map(union perf_event *event, FILE *fp);
  328. size_t perf_event__fprintf_cpu_map(union perf_event *event, FILE *fp);
  329. size_t perf_event__fprintf_namespaces(union perf_event *event, FILE *fp);
  330. size_t perf_event__fprintf_ksymbol(union perf_event *event, FILE *fp);
  331. size_t perf_event__fprintf_bpf(union perf_event *event, FILE *fp);
  332. size_t perf_event__fprintf(union perf_event *event, FILE *fp);
  333. int kallsyms__get_function_start(const char *kallsyms_filename,
  334. const char *symbol_name, u64 *addr);
  335. void *cpu_map_data__alloc(struct perf_cpu_map *map, size_t *size, u16 *type, int *max);
  336. void cpu_map_data__synthesize(struct perf_record_cpu_map_data *data, struct perf_cpu_map *map,
  337. u16 type, int max);
  338. void event_attr_init(struct perf_event_attr *attr);
  339. int perf_event_paranoid(void);
  340. bool perf_event_paranoid_check(int max_level);
  341. extern int sysctl_perf_event_max_stack;
  342. extern int sysctl_perf_event_max_contexts_per_stack;
  343. extern unsigned int proc_map_timeout;
  344. #endif /* __PERF_RECORD_H */