1 /* 2 * auxtrace.h: AUX area trace support 3 * Copyright (c) 2013-2015, Intel Corporation. 4 * 5 * This program is free software; you can redistribute it and/or modify it 6 * under the terms and conditions of the GNU General Public License, 7 * version 2, as published by the Free Software Foundation. 8 * 9 * This program is distributed in the hope it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 12 * more details. 13 * 14 */ 15 16 #ifndef __PERF_AUXTRACE_H 17 #define __PERF_AUXTRACE_H 18 19 #include <sys/types.h> 20 #include <stdbool.h> 21 #include <stddef.h> 22 #include <linux/list.h> 23 #include <linux/perf_event.h> 24 #include <linux/types.h> 25 26 #include "../perf.h" 27 #include "event.h" 28 #include "session.h" 29 #include "debug.h" 30 31 union perf_event; 32 struct perf_session; 33 struct perf_evlist; 34 struct perf_tool; 35 struct option; 36 struct record_opts; 37 struct auxtrace_info_event; 38 struct events_stats; 39 40 enum auxtrace_type { 41 PERF_AUXTRACE_UNKNOWN, 42 PERF_AUXTRACE_INTEL_PT, 43 PERF_AUXTRACE_INTEL_BTS, 44 }; 45 46 enum itrace_period_type { 47 PERF_ITRACE_PERIOD_INSTRUCTIONS, 48 PERF_ITRACE_PERIOD_TICKS, 49 PERF_ITRACE_PERIOD_NANOSECS, 50 }; 51 52 /** 53 * struct itrace_synth_opts - AUX area tracing synthesis options. 54 * @set: indicates whether or not options have been set 55 * @inject: indicates the event (not just the sample) must be fully synthesized 56 * because 'perf inject' will write it out 57 * @instructions: whether to synthesize 'instructions' events 58 * @branches: whether to synthesize 'branches' events 59 * @transactions: whether to synthesize events for transactions 60 * @errors: whether to synthesize decoder error events 61 * @dont_decode: whether to skip decoding entirely 62 * @log: write a decoding log 63 * @calls: limit branch samples to calls (can be combined with @returns) 64 * @returns: limit branch samples to returns (can be combined with @calls) 65 * @callchain: add callchain to 'instructions' events 66 * @last_branch: add branch context to 'instruction' events 67 * @callchain_sz: maximum callchain size 68 * @last_branch_sz: branch context size 69 * @period: 'instructions' events period 70 * @period_type: 'instructions' events period type 71 */ 72 struct itrace_synth_opts { 73 bool set; 74 bool inject; 75 bool instructions; 76 bool branches; 77 bool transactions; 78 bool errors; 79 bool dont_decode; 80 bool log; 81 bool calls; 82 bool returns; 83 bool callchain; 84 bool last_branch; 85 unsigned int callchain_sz; 86 unsigned int last_branch_sz; 87 unsigned long long period; 88 enum itrace_period_type period_type; 89 }; 90 91 /** 92 * struct auxtrace_index_entry - indexes a AUX area tracing event within a 93 * perf.data file. 94 * @file_offset: offset within the perf.data file 95 * @sz: size of the event 96 */ 97 struct auxtrace_index_entry { 98 u64 file_offset; 99 u64 sz; 100 }; 101 102 #define PERF_AUXTRACE_INDEX_ENTRY_COUNT 256 103 104 /** 105 * struct auxtrace_index - index of AUX area tracing events within a perf.data 106 * file. 107 * @list: linking a number of arrays of entries 108 * @nr: number of entries 109 * @entries: array of entries 110 */ 111 struct auxtrace_index { 112 struct list_head list; 113 size_t nr; 114 struct auxtrace_index_entry entries[PERF_AUXTRACE_INDEX_ENTRY_COUNT]; 115 }; 116 117 /** 118 * struct auxtrace - session callbacks to allow AUX area data decoding. 119 * @process_event: lets the decoder see all session events 120 * @flush_events: process any remaining data 121 * @free_events: free resources associated with event processing 122 * @free: free resources associated with the session 123 */ 124 struct auxtrace { 125 int (*process_event)(struct perf_session *session, 126 union perf_event *event, 127 struct perf_sample *sample, 128 struct perf_tool *tool); 129 int (*process_auxtrace_event)(struct perf_session *session, 130 union perf_event *event, 131 struct perf_tool *tool); 132 int (*flush_events)(struct perf_session *session, 133 struct perf_tool *tool); 134 void (*free_events)(struct perf_session *session); 135 void (*free)(struct perf_session *session); 136 }; 137 138 /** 139 * struct auxtrace_buffer - a buffer containing AUX area tracing data. 140 * @list: buffers are queued in a list held by struct auxtrace_queue 141 * @size: size of the buffer in bytes 142 * @pid: in per-thread mode, the pid this buffer is associated with 143 * @tid: in per-thread mode, the tid this buffer is associated with 144 * @cpu: in per-cpu mode, the cpu this buffer is associated with 145 * @data: actual buffer data (can be null if the data has not been loaded) 146 * @data_offset: file offset at which the buffer can be read 147 * @mmap_addr: mmap address at which the buffer can be read 148 * @mmap_size: size of the mmap at @mmap_addr 149 * @data_needs_freeing: @data was malloc'd so free it when it is no longer 150 * needed 151 * @consecutive: the original data was split up and this buffer is consecutive 152 * to the previous buffer 153 * @offset: offset as determined by aux_head / aux_tail members of struct 154 * perf_event_mmap_page 155 * @reference: an implementation-specific reference determined when the data is 156 * recorded 157 * @buffer_nr: used to number each buffer 158 * @use_size: implementation actually only uses this number of bytes 159 * @use_data: implementation actually only uses data starting at this address 160 */ 161 struct auxtrace_buffer { 162 struct list_head list; 163 size_t size; 164 pid_t pid; 165 pid_t tid; 166 int cpu; 167 void *data; 168 off_t data_offset; 169 void *mmap_addr; 170 size_t mmap_size; 171 bool data_needs_freeing; 172 bool consecutive; 173 u64 offset; 174 u64 reference; 175 u64 buffer_nr; 176 size_t use_size; 177 void *use_data; 178 }; 179 180 /** 181 * struct auxtrace_queue - a queue of AUX area tracing data buffers. 182 * @head: head of buffer list 183 * @tid: in per-thread mode, the tid this queue is associated with 184 * @cpu: in per-cpu mode, the cpu this queue is associated with 185 * @set: %true once this queue has been dedicated to a specific thread or cpu 186 * @priv: implementation-specific data 187 */ 188 struct auxtrace_queue { 189 struct list_head head; 190 pid_t tid; 191 int cpu; 192 bool set; 193 void *priv; 194 }; 195 196 /** 197 * struct auxtrace_queues - an array of AUX area tracing queues. 198 * @queue_array: array of queues 199 * @nr_queues: number of queues 200 * @new_data: set whenever new data is queued 201 * @populated: queues have been fully populated using the auxtrace_index 202 * @next_buffer_nr: used to number each buffer 203 */ 204 struct auxtrace_queues { 205 struct auxtrace_queue *queue_array; 206 unsigned int nr_queues; 207 bool new_data; 208 bool populated; 209 u64 next_buffer_nr; 210 }; 211 212 /** 213 * struct auxtrace_heap_item - element of struct auxtrace_heap. 214 * @queue_nr: queue number 215 * @ordinal: value used for sorting (lowest ordinal is top of the heap) expected 216 * to be a timestamp 217 */ 218 struct auxtrace_heap_item { 219 unsigned int queue_nr; 220 u64 ordinal; 221 }; 222 223 /** 224 * struct auxtrace_heap - a heap suitable for sorting AUX area tracing queues. 225 * @heap_array: the heap 226 * @heap_cnt: the number of elements in the heap 227 * @heap_sz: maximum number of elements (grows as needed) 228 */ 229 struct auxtrace_heap { 230 struct auxtrace_heap_item *heap_array; 231 unsigned int heap_cnt; 232 unsigned int heap_sz; 233 }; 234 235 /** 236 * struct auxtrace_mmap - records an mmap of the auxtrace buffer. 237 * @base: address of mapped area 238 * @userpg: pointer to buffer's perf_event_mmap_page 239 * @mask: %0 if @len is not a power of two, otherwise (@len - %1) 240 * @len: size of mapped area 241 * @prev: previous aux_head 242 * @idx: index of this mmap 243 * @tid: tid for a per-thread mmap (also set if there is only 1 tid on a per-cpu 244 * mmap) otherwise %0 245 * @cpu: cpu number for a per-cpu mmap otherwise %-1 246 */ 247 struct auxtrace_mmap { 248 void *base; 249 void *userpg; 250 size_t mask; 251 size_t len; 252 u64 prev; 253 int idx; 254 pid_t tid; 255 int cpu; 256 }; 257 258 /** 259 * struct auxtrace_mmap_params - parameters to set up struct auxtrace_mmap. 260 * @mask: %0 if @len is not a power of two, otherwise (@len - %1) 261 * @offset: file offset of mapped area 262 * @len: size of mapped area 263 * @prot: mmap memory protection 264 * @idx: index of this mmap 265 * @tid: tid for a per-thread mmap (also set if there is only 1 tid on a per-cpu 266 * mmap) otherwise %0 267 * @cpu: cpu number for a per-cpu mmap otherwise %-1 268 */ 269 struct auxtrace_mmap_params { 270 size_t mask; 271 off_t offset; 272 size_t len; 273 int prot; 274 int idx; 275 pid_t tid; 276 int cpu; 277 }; 278 279 /** 280 * struct auxtrace_record - callbacks for recording AUX area data. 281 * @recording_options: validate and process recording options 282 * @info_priv_size: return the size of the private data in auxtrace_info_event 283 * @info_fill: fill-in the private data in auxtrace_info_event 284 * @free: free this auxtrace record structure 285 * @snapshot_start: starting a snapshot 286 * @snapshot_finish: finishing a snapshot 287 * @find_snapshot: find data to snapshot within auxtrace mmap 288 * @parse_snapshot_options: parse snapshot options 289 * @reference: provide a 64-bit reference number for auxtrace_event 290 * @read_finish: called after reading from an auxtrace mmap 291 */ 292 struct auxtrace_record { 293 int (*recording_options)(struct auxtrace_record *itr, 294 struct perf_evlist *evlist, 295 struct record_opts *opts); 296 size_t (*info_priv_size)(struct auxtrace_record *itr, 297 struct perf_evlist *evlist); 298 int (*info_fill)(struct auxtrace_record *itr, 299 struct perf_session *session, 300 struct auxtrace_info_event *auxtrace_info, 301 size_t priv_size); 302 void (*free)(struct auxtrace_record *itr); 303 int (*snapshot_start)(struct auxtrace_record *itr); 304 int (*snapshot_finish)(struct auxtrace_record *itr); 305 int (*find_snapshot)(struct auxtrace_record *itr, int idx, 306 struct auxtrace_mmap *mm, unsigned char *data, 307 u64 *head, u64 *old); 308 int (*parse_snapshot_options)(struct auxtrace_record *itr, 309 struct record_opts *opts, 310 const char *str); 311 u64 (*reference)(struct auxtrace_record *itr); 312 int (*read_finish)(struct auxtrace_record *itr, int idx); 313 unsigned int alignment; 314 }; 315 316 #ifdef HAVE_AUXTRACE_SUPPORT 317 318 /* 319 * In snapshot mode the mmapped page is read-only which makes using 320 * __sync_val_compare_and_swap() problematic. However, snapshot mode expects 321 * the buffer is not updated while the snapshot is made (e.g. Intel PT disables 322 * the event) so there is not a race anyway. 323 */ 324 static inline u64 auxtrace_mmap__read_snapshot_head(struct auxtrace_mmap *mm) 325 { 326 struct perf_event_mmap_page *pc = mm->userpg; 327 u64 head = ACCESS_ONCE(pc->aux_head); 328 329 /* Ensure all reads are done after we read the head */ 330 rmb(); 331 return head; 332 } 333 334 static inline u64 auxtrace_mmap__read_head(struct auxtrace_mmap *mm) 335 { 336 struct perf_event_mmap_page *pc = mm->userpg; 337 #if BITS_PER_LONG == 64 || !defined(HAVE_SYNC_COMPARE_AND_SWAP_SUPPORT) 338 u64 head = ACCESS_ONCE(pc->aux_head); 339 #else 340 u64 head = __sync_val_compare_and_swap(&pc->aux_head, 0, 0); 341 #endif 342 343 /* Ensure all reads are done after we read the head */ 344 rmb(); 345 return head; 346 } 347 348 static inline void auxtrace_mmap__write_tail(struct auxtrace_mmap *mm, u64 tail) 349 { 350 struct perf_event_mmap_page *pc = mm->userpg; 351 #if BITS_PER_LONG != 64 && defined(HAVE_SYNC_COMPARE_AND_SWAP_SUPPORT) 352 u64 old_tail; 353 #endif 354 355 /* Ensure all reads are done before we write the tail out */ 356 mb(); 357 #if BITS_PER_LONG == 64 || !defined(HAVE_SYNC_COMPARE_AND_SWAP_SUPPORT) 358 pc->aux_tail = tail; 359 #else 360 do { 361 old_tail = __sync_val_compare_and_swap(&pc->aux_tail, 0, 0); 362 } while (!__sync_bool_compare_and_swap(&pc->aux_tail, old_tail, tail)); 363 #endif 364 } 365 366 int auxtrace_mmap__mmap(struct auxtrace_mmap *mm, 367 struct auxtrace_mmap_params *mp, 368 void *userpg, int fd); 369 void auxtrace_mmap__munmap(struct auxtrace_mmap *mm); 370 void auxtrace_mmap_params__init(struct auxtrace_mmap_params *mp, 371 off_t auxtrace_offset, 372 unsigned int auxtrace_pages, 373 bool auxtrace_overwrite); 374 void auxtrace_mmap_params__set_idx(struct auxtrace_mmap_params *mp, 375 struct perf_evlist *evlist, int idx, 376 bool per_cpu); 377 378 typedef int (*process_auxtrace_t)(struct perf_tool *tool, 379 union perf_event *event, void *data1, 380 size_t len1, void *data2, size_t len2); 381 382 int auxtrace_mmap__read(struct auxtrace_mmap *mm, struct auxtrace_record *itr, 383 struct perf_tool *tool, process_auxtrace_t fn); 384 385 int auxtrace_mmap__read_snapshot(struct auxtrace_mmap *mm, 386 struct auxtrace_record *itr, 387 struct perf_tool *tool, process_auxtrace_t fn, 388 size_t snapshot_size); 389 390 int auxtrace_queues__init(struct auxtrace_queues *queues); 391 int auxtrace_queues__add_event(struct auxtrace_queues *queues, 392 struct perf_session *session, 393 union perf_event *event, off_t data_offset, 394 struct auxtrace_buffer **buffer_ptr); 395 void auxtrace_queues__free(struct auxtrace_queues *queues); 396 int auxtrace_queues__process_index(struct auxtrace_queues *queues, 397 struct perf_session *session); 398 struct auxtrace_buffer *auxtrace_buffer__next(struct auxtrace_queue *queue, 399 struct auxtrace_buffer *buffer); 400 void *auxtrace_buffer__get_data(struct auxtrace_buffer *buffer, int fd); 401 void auxtrace_buffer__put_data(struct auxtrace_buffer *buffer); 402 void auxtrace_buffer__drop_data(struct auxtrace_buffer *buffer); 403 void auxtrace_buffer__free(struct auxtrace_buffer *buffer); 404 405 int auxtrace_heap__add(struct auxtrace_heap *heap, unsigned int queue_nr, 406 u64 ordinal); 407 void auxtrace_heap__pop(struct auxtrace_heap *heap); 408 void auxtrace_heap__free(struct auxtrace_heap *heap); 409 410 struct auxtrace_cache_entry { 411 struct hlist_node hash; 412 u32 key; 413 }; 414 415 struct auxtrace_cache *auxtrace_cache__new(unsigned int bits, size_t entry_size, 416 unsigned int limit_percent); 417 void auxtrace_cache__free(struct auxtrace_cache *auxtrace_cache); 418 void *auxtrace_cache__alloc_entry(struct auxtrace_cache *c); 419 void auxtrace_cache__free_entry(struct auxtrace_cache *c, void *entry); 420 int auxtrace_cache__add(struct auxtrace_cache *c, u32 key, 421 struct auxtrace_cache_entry *entry); 422 void *auxtrace_cache__lookup(struct auxtrace_cache *c, u32 key); 423 424 struct auxtrace_record *auxtrace_record__init(struct perf_evlist *evlist, 425 int *err); 426 427 int auxtrace_parse_snapshot_options(struct auxtrace_record *itr, 428 struct record_opts *opts, 429 const char *str); 430 int auxtrace_record__options(struct auxtrace_record *itr, 431 struct perf_evlist *evlist, 432 struct record_opts *opts); 433 size_t auxtrace_record__info_priv_size(struct auxtrace_record *itr, 434 struct perf_evlist *evlist); 435 int auxtrace_record__info_fill(struct auxtrace_record *itr, 436 struct perf_session *session, 437 struct auxtrace_info_event *auxtrace_info, 438 size_t priv_size); 439 void auxtrace_record__free(struct auxtrace_record *itr); 440 int auxtrace_record__snapshot_start(struct auxtrace_record *itr); 441 int auxtrace_record__snapshot_finish(struct auxtrace_record *itr); 442 int auxtrace_record__find_snapshot(struct auxtrace_record *itr, int idx, 443 struct auxtrace_mmap *mm, 444 unsigned char *data, u64 *head, u64 *old); 445 u64 auxtrace_record__reference(struct auxtrace_record *itr); 446 447 int auxtrace_index__auxtrace_event(struct list_head *head, union perf_event *event, 448 off_t file_offset); 449 int auxtrace_index__write(int fd, struct list_head *head); 450 int auxtrace_index__process(int fd, u64 size, struct perf_session *session, 451 bool needs_swap); 452 void auxtrace_index__free(struct list_head *head); 453 454 void auxtrace_synth_error(struct auxtrace_error_event *auxtrace_error, int type, 455 int code, int cpu, pid_t pid, pid_t tid, u64 ip, 456 const char *msg); 457 458 int perf_event__synthesize_auxtrace_info(struct auxtrace_record *itr, 459 struct perf_tool *tool, 460 struct perf_session *session, 461 perf_event__handler_t process); 462 int perf_event__process_auxtrace_info(struct perf_tool *tool, 463 union perf_event *event, 464 struct perf_session *session); 465 s64 perf_event__process_auxtrace(struct perf_tool *tool, 466 union perf_event *event, 467 struct perf_session *session); 468 int perf_event__process_auxtrace_error(struct perf_tool *tool, 469 union perf_event *event, 470 struct perf_session *session); 471 int itrace_parse_synth_opts(const struct option *opt, const char *str, 472 int unset); 473 void itrace_synth_opts__set_default(struct itrace_synth_opts *synth_opts); 474 475 size_t perf_event__fprintf_auxtrace_error(union perf_event *event, FILE *fp); 476 void perf_session__auxtrace_error_inc(struct perf_session *session, 477 union perf_event *event); 478 void events_stats__auxtrace_error_warn(const struct events_stats *stats); 479 480 static inline int auxtrace__process_event(struct perf_session *session, 481 union perf_event *event, 482 struct perf_sample *sample, 483 struct perf_tool *tool) 484 { 485 if (!session->auxtrace) 486 return 0; 487 488 return session->auxtrace->process_event(session, event, sample, tool); 489 } 490 491 static inline int auxtrace__flush_events(struct perf_session *session, 492 struct perf_tool *tool) 493 { 494 if (!session->auxtrace) 495 return 0; 496 497 return session->auxtrace->flush_events(session, tool); 498 } 499 500 static inline void auxtrace__free_events(struct perf_session *session) 501 { 502 if (!session->auxtrace) 503 return; 504 505 return session->auxtrace->free_events(session); 506 } 507 508 static inline void auxtrace__free(struct perf_session *session) 509 { 510 if (!session->auxtrace) 511 return; 512 513 return session->auxtrace->free(session); 514 } 515 516 #else 517 518 static inline struct auxtrace_record * 519 auxtrace_record__init(struct perf_evlist *evlist __maybe_unused, 520 int *err) 521 { 522 *err = 0; 523 return NULL; 524 } 525 526 static inline 527 void auxtrace_record__free(struct auxtrace_record *itr __maybe_unused) 528 { 529 } 530 531 static inline int 532 perf_event__synthesize_auxtrace_info(struct auxtrace_record *itr __maybe_unused, 533 struct perf_tool *tool __maybe_unused, 534 struct perf_session *session __maybe_unused, 535 perf_event__handler_t process __maybe_unused) 536 { 537 return -EINVAL; 538 } 539 540 static inline 541 int auxtrace_record__options(struct auxtrace_record *itr __maybe_unused, 542 struct perf_evlist *evlist __maybe_unused, 543 struct record_opts *opts __maybe_unused) 544 { 545 return 0; 546 } 547 548 #define perf_event__process_auxtrace_info 0 549 #define perf_event__process_auxtrace 0 550 #define perf_event__process_auxtrace_error 0 551 552 static inline 553 void perf_session__auxtrace_error_inc(struct perf_session *session 554 __maybe_unused, 555 union perf_event *event 556 __maybe_unused) 557 { 558 } 559 560 static inline 561 void events_stats__auxtrace_error_warn(const struct events_stats *stats 562 __maybe_unused) 563 { 564 } 565 566 static inline 567 int itrace_parse_synth_opts(const struct option *opt __maybe_unused, 568 const char *str __maybe_unused, 569 int unset __maybe_unused) 570 { 571 pr_err("AUX area tracing not supported\n"); 572 return -EINVAL; 573 } 574 575 static inline 576 int auxtrace_parse_snapshot_options(struct auxtrace_record *itr __maybe_unused, 577 struct record_opts *opts __maybe_unused, 578 const char *str) 579 { 580 if (!str) 581 return 0; 582 pr_err("AUX area tracing not supported\n"); 583 return -EINVAL; 584 } 585 586 static inline 587 int auxtrace__process_event(struct perf_session *session __maybe_unused, 588 union perf_event *event __maybe_unused, 589 struct perf_sample *sample __maybe_unused, 590 struct perf_tool *tool __maybe_unused) 591 { 592 return 0; 593 } 594 595 static inline 596 int auxtrace__flush_events(struct perf_session *session __maybe_unused, 597 struct perf_tool *tool __maybe_unused) 598 { 599 return 0; 600 } 601 602 static inline 603 void auxtrace__free_events(struct perf_session *session __maybe_unused) 604 { 605 } 606 607 static inline 608 void auxtrace_cache__free(struct auxtrace_cache *auxtrace_cache __maybe_unused) 609 { 610 } 611 612 static inline 613 void auxtrace__free(struct perf_session *session __maybe_unused) 614 { 615 } 616 617 static inline 618 int auxtrace_index__write(int fd __maybe_unused, 619 struct list_head *head __maybe_unused) 620 { 621 return -EINVAL; 622 } 623 624 static inline 625 int auxtrace_index__process(int fd __maybe_unused, 626 u64 size __maybe_unused, 627 struct perf_session *session __maybe_unused, 628 bool needs_swap __maybe_unused) 629 { 630 return -EINVAL; 631 } 632 633 static inline 634 void auxtrace_index__free(struct list_head *head __maybe_unused) 635 { 636 } 637 638 int auxtrace_mmap__mmap(struct auxtrace_mmap *mm, 639 struct auxtrace_mmap_params *mp, 640 void *userpg, int fd); 641 void auxtrace_mmap__munmap(struct auxtrace_mmap *mm); 642 void auxtrace_mmap_params__init(struct auxtrace_mmap_params *mp, 643 off_t auxtrace_offset, 644 unsigned int auxtrace_pages, 645 bool auxtrace_overwrite); 646 void auxtrace_mmap_params__set_idx(struct auxtrace_mmap_params *mp, 647 struct perf_evlist *evlist, int idx, 648 bool per_cpu); 649 650 #endif 651 652 #endif 653