Thomas Gleixner | 2025cf9 | 2019-05-29 07:18:02 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 2 | /* |
| 3 | * intel_pt.c: Intel Processor Trace support |
| 4 | * Copyright (c) 2013-2015, Intel Corporation. |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 5 | */ |
| 6 | |
Arnaldo Carvalho de Melo | fd20e81 | 2017-04-17 15:23:08 -0300 | [diff] [blame] | 7 | #include <inttypes.h> |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 8 | #include <stdio.h> |
| 9 | #include <stdbool.h> |
| 10 | #include <errno.h> |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/types.h> |
| 13 | |
| 14 | #include "../perf.h" |
| 15 | #include "session.h" |
| 16 | #include "machine.h" |
Arnaldo Carvalho de Melo | 98521b3 | 2017-04-25 15:45:35 -0300 | [diff] [blame] | 17 | #include "memswap.h" |
Adrian Hunter | f14445e | 2015-09-25 16:15:45 +0300 | [diff] [blame] | 18 | #include "sort.h" |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 19 | #include "tool.h" |
| 20 | #include "event.h" |
| 21 | #include "evlist.h" |
| 22 | #include "evsel.h" |
| 23 | #include "map.h" |
| 24 | #include "color.h" |
| 25 | #include "util.h" |
| 26 | #include "thread.h" |
| 27 | #include "thread-stack.h" |
| 28 | #include "symbol.h" |
| 29 | #include "callchain.h" |
| 30 | #include "dso.h" |
| 31 | #include "debug.h" |
| 32 | #include "auxtrace.h" |
| 33 | #include "tsc.h" |
| 34 | #include "intel-pt.h" |
Taeung Song | 41840d2 | 2016-06-23 17:55:17 +0900 | [diff] [blame] | 35 | #include "config.h" |
Adrian Hunter | 2c47db9 | 2019-06-04 16:00:09 +0300 | [diff] [blame] | 36 | #include "time-utils.h" |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 37 | |
Adrian Hunter | 9e9a618 | 2019-06-10 10:27:59 +0300 | [diff] [blame^] | 38 | #include "../arch/x86/include/uapi/asm/perf_regs.h" |
| 39 | |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 40 | #include "intel-pt-decoder/intel-pt-log.h" |
| 41 | #include "intel-pt-decoder/intel-pt-decoder.h" |
| 42 | #include "intel-pt-decoder/intel-pt-insn-decoder.h" |
| 43 | #include "intel-pt-decoder/intel-pt-pkt-decoder.h" |
| 44 | |
| 45 | #define MAX_TIMESTAMP (~0ULL) |
| 46 | |
Adrian Hunter | 2c47db9 | 2019-06-04 16:00:09 +0300 | [diff] [blame] | 47 | struct range { |
| 48 | u64 start; |
| 49 | u64 end; |
| 50 | }; |
| 51 | |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 52 | struct intel_pt { |
| 53 | struct auxtrace auxtrace; |
| 54 | struct auxtrace_queues queues; |
| 55 | struct auxtrace_heap heap; |
| 56 | u32 auxtrace_type; |
| 57 | struct perf_session *session; |
| 58 | struct machine *machine; |
| 59 | struct perf_evsel *switch_evsel; |
| 60 | struct thread *unknown_thread; |
| 61 | bool timeless_decoding; |
| 62 | bool sampling_mode; |
| 63 | bool snapshot_mode; |
| 64 | bool per_cpu_mmaps; |
| 65 | bool have_tsc; |
| 66 | bool data_queued; |
| 67 | bool est_tsc; |
| 68 | bool sync_switch; |
Adrian Hunter | ba11ba6 | 2015-09-25 16:15:56 +0300 | [diff] [blame] | 69 | bool mispred_all; |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 70 | int have_sched_switch; |
| 71 | u32 pmu_type; |
| 72 | u64 kernel_start; |
| 73 | u64 switch_ip; |
| 74 | u64 ptss_ip; |
| 75 | |
| 76 | struct perf_tsc_conversion tc; |
| 77 | bool cap_user_time_zero; |
| 78 | |
| 79 | struct itrace_synth_opts synth_opts; |
| 80 | |
| 81 | bool sample_instructions; |
| 82 | u64 instructions_sample_type; |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 83 | u64 instructions_id; |
| 84 | |
| 85 | bool sample_branches; |
| 86 | u32 branches_filter; |
| 87 | u64 branches_sample_type; |
| 88 | u64 branches_id; |
| 89 | |
| 90 | bool sample_transactions; |
| 91 | u64 transactions_sample_type; |
| 92 | u64 transactions_id; |
| 93 | |
Adrian Hunter | 3797307 | 2017-06-30 11:36:45 +0300 | [diff] [blame] | 94 | bool sample_ptwrites; |
| 95 | u64 ptwrites_sample_type; |
| 96 | u64 ptwrites_id; |
| 97 | |
| 98 | bool sample_pwr_events; |
| 99 | u64 pwr_events_sample_type; |
| 100 | u64 mwait_id; |
| 101 | u64 pwre_id; |
| 102 | u64 exstop_id; |
| 103 | u64 pwrx_id; |
| 104 | u64 cbr_id; |
| 105 | |
Adrian Hunter | e62ca65 | 2019-06-10 10:27:56 +0300 | [diff] [blame] | 106 | bool sample_pebs; |
| 107 | struct perf_evsel *pebs_evsel; |
| 108 | |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 109 | u64 tsc_bit; |
Adrian Hunter | 11fa7cb | 2015-07-17 19:33:54 +0300 | [diff] [blame] | 110 | u64 mtc_bit; |
| 111 | u64 mtc_freq_bits; |
| 112 | u32 tsc_ctc_ratio_n; |
| 113 | u32 tsc_ctc_ratio_d; |
| 114 | u64 cyc_bit; |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 115 | u64 noretcomp_bit; |
| 116 | unsigned max_non_turbo_ratio; |
Adrian Hunter | 3797307 | 2017-06-30 11:36:45 +0300 | [diff] [blame] | 117 | unsigned cbr2khz; |
Andi Kleen | d1706b3 | 2016-03-28 10:45:38 -0700 | [diff] [blame] | 118 | |
| 119 | unsigned long num_events; |
Adrian Hunter | 2b9e32c | 2016-09-23 17:38:46 +0300 | [diff] [blame] | 120 | |
| 121 | char *filter; |
Adrian Hunter | 2acee10 | 2016-09-23 17:38:48 +0300 | [diff] [blame] | 122 | struct addr_filters filts; |
Adrian Hunter | 2c47db9 | 2019-06-04 16:00:09 +0300 | [diff] [blame] | 123 | |
| 124 | struct range *time_ranges; |
| 125 | unsigned int range_cnt; |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 126 | }; |
| 127 | |
| 128 | enum switch_state { |
| 129 | INTEL_PT_SS_NOT_TRACING, |
| 130 | INTEL_PT_SS_UNKNOWN, |
| 131 | INTEL_PT_SS_TRACING, |
| 132 | INTEL_PT_SS_EXPECTING_SWITCH_EVENT, |
| 133 | INTEL_PT_SS_EXPECTING_SWITCH_IP, |
| 134 | }; |
| 135 | |
| 136 | struct intel_pt_queue { |
| 137 | struct intel_pt *pt; |
| 138 | unsigned int queue_nr; |
| 139 | struct auxtrace_buffer *buffer; |
Adrian Hunter | 9c66506 | 2018-03-07 16:02:27 +0200 | [diff] [blame] | 140 | struct auxtrace_buffer *old_buffer; |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 141 | void *decoder; |
| 142 | const struct intel_pt_state *state; |
| 143 | struct ip_callchain *chain; |
Adrian Hunter | f14445e | 2015-09-25 16:15:45 +0300 | [diff] [blame] | 144 | struct branch_stack *last_branch; |
| 145 | struct branch_stack *last_branch_rb; |
| 146 | size_t last_branch_pos; |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 147 | union perf_event *event_buf; |
| 148 | bool on_heap; |
| 149 | bool stop; |
| 150 | bool step_through_buffers; |
| 151 | bool use_buffer_pid_tid; |
Adrian Hunter | 63d8e38 | 2018-03-07 16:02:22 +0200 | [diff] [blame] | 152 | bool sync_switch; |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 153 | pid_t pid, tid; |
| 154 | int cpu; |
| 155 | int switch_state; |
| 156 | pid_t next_tid; |
| 157 | struct thread *thread; |
| 158 | bool exclude_kernel; |
| 159 | bool have_sample; |
| 160 | u64 time; |
| 161 | u64 timestamp; |
Adrian Hunter | 2c47db9 | 2019-06-04 16:00:09 +0300 | [diff] [blame] | 162 | u64 sel_timestamp; |
| 163 | bool sel_start; |
| 164 | unsigned int sel_idx; |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 165 | u32 flags; |
| 166 | u16 insn_len; |
Adrian Hunter | 2a21d03 | 2015-07-17 19:33:48 +0300 | [diff] [blame] | 167 | u64 last_insn_cnt; |
Adrian Hunter | 5b1dc0f | 2019-05-20 14:37:13 +0300 | [diff] [blame] | 168 | u64 ipc_insn_cnt; |
| 169 | u64 ipc_cyc_cnt; |
| 170 | u64 last_in_insn_cnt; |
| 171 | u64 last_in_cyc_cnt; |
| 172 | u64 last_br_insn_cnt; |
| 173 | u64 last_br_cyc_cnt; |
Andi Kleen | faaa876 | 2016-10-07 16:42:26 +0300 | [diff] [blame] | 174 | char insn[INTEL_PT_INSN_BUF_SZ]; |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 175 | }; |
| 176 | |
| 177 | static void intel_pt_dump(struct intel_pt *pt __maybe_unused, |
| 178 | unsigned char *buf, size_t len) |
| 179 | { |
| 180 | struct intel_pt_pkt packet; |
| 181 | size_t pos = 0; |
| 182 | int ret, pkt_len, i; |
| 183 | char desc[INTEL_PT_PKT_DESC_MAX]; |
| 184 | const char *color = PERF_COLOR_BLUE; |
Adrian Hunter | edff780 | 2019-06-10 10:27:53 +0300 | [diff] [blame] | 185 | enum intel_pt_pkt_ctx ctx = INTEL_PT_NO_CTX; |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 186 | |
| 187 | color_fprintf(stdout, color, |
| 188 | ". ... Intel Processor Trace data: size %zu bytes\n", |
| 189 | len); |
| 190 | |
| 191 | while (len) { |
Adrian Hunter | edff780 | 2019-06-10 10:27:53 +0300 | [diff] [blame] | 192 | ret = intel_pt_get_packet(buf, len, &packet, &ctx); |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 193 | if (ret > 0) |
| 194 | pkt_len = ret; |
| 195 | else |
| 196 | pkt_len = 1; |
| 197 | printf("."); |
| 198 | color_fprintf(stdout, color, " %08x: ", pos); |
| 199 | for (i = 0; i < pkt_len; i++) |
| 200 | color_fprintf(stdout, color, " %02x", buf[i]); |
| 201 | for (; i < 16; i++) |
| 202 | color_fprintf(stdout, color, " "); |
| 203 | if (ret > 0) { |
| 204 | ret = intel_pt_pkt_desc(&packet, desc, |
| 205 | INTEL_PT_PKT_DESC_MAX); |
| 206 | if (ret > 0) |
| 207 | color_fprintf(stdout, color, " %s\n", desc); |
| 208 | } else { |
| 209 | color_fprintf(stdout, color, " Bad packet!\n"); |
| 210 | } |
| 211 | pos += pkt_len; |
| 212 | buf += pkt_len; |
| 213 | len -= pkt_len; |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | static void intel_pt_dump_event(struct intel_pt *pt, unsigned char *buf, |
| 218 | size_t len) |
| 219 | { |
| 220 | printf(".\n"); |
| 221 | intel_pt_dump(pt, buf, len); |
| 222 | } |
| 223 | |
Adrian Hunter | 93f8be2 | 2018-11-05 09:35:04 +0200 | [diff] [blame] | 224 | static void intel_pt_log_event(union perf_event *event) |
| 225 | { |
| 226 | FILE *f = intel_pt_log_fp(); |
| 227 | |
| 228 | if (!intel_pt_enable_logging || !f) |
| 229 | return; |
| 230 | |
| 231 | perf_event__fprintf(event, f); |
| 232 | } |
| 233 | |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 234 | static int intel_pt_do_fix_overlap(struct intel_pt *pt, struct auxtrace_buffer *a, |
| 235 | struct auxtrace_buffer *b) |
| 236 | { |
Adrian Hunter | 117db4b | 2018-03-07 16:02:21 +0200 | [diff] [blame] | 237 | bool consecutive = false; |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 238 | void *start; |
| 239 | |
| 240 | start = intel_pt_find_overlap(a->data, a->size, b->data, b->size, |
Adrian Hunter | 117db4b | 2018-03-07 16:02:21 +0200 | [diff] [blame] | 241 | pt->have_tsc, &consecutive); |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 242 | if (!start) |
| 243 | return -EINVAL; |
| 244 | b->use_size = b->data + b->size - start; |
| 245 | b->use_data = start; |
Adrian Hunter | 117db4b | 2018-03-07 16:02:21 +0200 | [diff] [blame] | 246 | if (b->use_size && consecutive) |
| 247 | b->consecutive = true; |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 248 | return 0; |
| 249 | } |
| 250 | |
Adrian Hunter | e96f7df | 2019-06-04 16:00:07 +0300 | [diff] [blame] | 251 | static int intel_pt_get_buffer(struct intel_pt_queue *ptq, |
| 252 | struct auxtrace_buffer *buffer, |
| 253 | struct auxtrace_buffer *old_buffer, |
| 254 | struct intel_pt_buffer *b) |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 255 | { |
Adrian Hunter | 599a5be | 2018-03-07 16:02:29 +0200 | [diff] [blame] | 256 | bool might_overlap; |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 257 | |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 258 | if (!buffer->data) { |
Jiri Olsa | 8ceb41d | 2017-01-23 22:07:59 +0100 | [diff] [blame] | 259 | int fd = perf_data__fd(ptq->pt->session->data); |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 260 | |
| 261 | buffer->data = auxtrace_buffer__get_data(buffer, fd); |
| 262 | if (!buffer->data) |
| 263 | return -ENOMEM; |
| 264 | } |
| 265 | |
Adrian Hunter | 599a5be | 2018-03-07 16:02:29 +0200 | [diff] [blame] | 266 | might_overlap = ptq->pt->snapshot_mode || ptq->pt->sampling_mode; |
| 267 | if (might_overlap && !buffer->consecutive && old_buffer && |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 268 | intel_pt_do_fix_overlap(ptq->pt, old_buffer, buffer)) |
| 269 | return -ENOMEM; |
| 270 | |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 271 | if (buffer->use_data) { |
| 272 | b->len = buffer->use_size; |
| 273 | b->buf = buffer->use_data; |
| 274 | } else { |
| 275 | b->len = buffer->size; |
| 276 | b->buf = buffer->data; |
| 277 | } |
| 278 | b->ref_timestamp = buffer->reference; |
| 279 | |
Adrian Hunter | 599a5be | 2018-03-07 16:02:29 +0200 | [diff] [blame] | 280 | if (!old_buffer || (might_overlap && !buffer->consecutive)) { |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 281 | b->consecutive = false; |
| 282 | b->trace_nr = buffer->buffer_nr + 1; |
| 283 | } else { |
| 284 | b->consecutive = true; |
| 285 | } |
| 286 | |
Adrian Hunter | e96f7df | 2019-06-04 16:00:07 +0300 | [diff] [blame] | 287 | return 0; |
| 288 | } |
| 289 | |
Adrian Hunter | da9000a | 2019-06-04 16:00:08 +0300 | [diff] [blame] | 290 | /* Do not drop buffers with references - refer intel_pt_get_trace() */ |
| 291 | static void intel_pt_lookahead_drop_buffer(struct intel_pt_queue *ptq, |
| 292 | struct auxtrace_buffer *buffer) |
| 293 | { |
| 294 | if (!buffer || buffer == ptq->buffer || buffer == ptq->old_buffer) |
| 295 | return; |
| 296 | |
| 297 | auxtrace_buffer__drop_data(buffer); |
| 298 | } |
| 299 | |
| 300 | /* Must be serialized with respect to intel_pt_get_trace() */ |
| 301 | static int intel_pt_lookahead(void *data, intel_pt_lookahead_cb_t cb, |
| 302 | void *cb_data) |
| 303 | { |
| 304 | struct intel_pt_queue *ptq = data; |
| 305 | struct auxtrace_buffer *buffer = ptq->buffer; |
| 306 | struct auxtrace_buffer *old_buffer = ptq->old_buffer; |
| 307 | struct auxtrace_queue *queue; |
| 308 | int err = 0; |
| 309 | |
| 310 | queue = &ptq->pt->queues.queue_array[ptq->queue_nr]; |
| 311 | |
| 312 | while (1) { |
| 313 | struct intel_pt_buffer b = { .len = 0 }; |
| 314 | |
| 315 | buffer = auxtrace_buffer__next(queue, buffer); |
| 316 | if (!buffer) |
| 317 | break; |
| 318 | |
| 319 | err = intel_pt_get_buffer(ptq, buffer, old_buffer, &b); |
| 320 | if (err) |
| 321 | break; |
| 322 | |
| 323 | if (b.len) { |
| 324 | intel_pt_lookahead_drop_buffer(ptq, old_buffer); |
| 325 | old_buffer = buffer; |
| 326 | } else { |
| 327 | intel_pt_lookahead_drop_buffer(ptq, buffer); |
| 328 | continue; |
| 329 | } |
| 330 | |
| 331 | err = cb(&b, cb_data); |
| 332 | if (err) |
| 333 | break; |
| 334 | } |
| 335 | |
| 336 | if (buffer != old_buffer) |
| 337 | intel_pt_lookahead_drop_buffer(ptq, buffer); |
| 338 | intel_pt_lookahead_drop_buffer(ptq, old_buffer); |
| 339 | |
| 340 | return err; |
| 341 | } |
| 342 | |
| 343 | /* |
| 344 | * This function assumes data is processed sequentially only. |
| 345 | * Must be serialized with respect to intel_pt_lookahead() |
| 346 | */ |
Adrian Hunter | e96f7df | 2019-06-04 16:00:07 +0300 | [diff] [blame] | 347 | static int intel_pt_get_trace(struct intel_pt_buffer *b, void *data) |
| 348 | { |
| 349 | struct intel_pt_queue *ptq = data; |
| 350 | struct auxtrace_buffer *buffer = ptq->buffer; |
| 351 | struct auxtrace_buffer *old_buffer = ptq->old_buffer; |
| 352 | struct auxtrace_queue *queue; |
| 353 | int err; |
| 354 | |
| 355 | if (ptq->stop) { |
| 356 | b->len = 0; |
| 357 | return 0; |
| 358 | } |
| 359 | |
| 360 | queue = &ptq->pt->queues.queue_array[ptq->queue_nr]; |
| 361 | |
| 362 | buffer = auxtrace_buffer__next(queue, buffer); |
| 363 | if (!buffer) { |
| 364 | if (old_buffer) |
| 365 | auxtrace_buffer__drop_data(old_buffer); |
| 366 | b->len = 0; |
| 367 | return 0; |
| 368 | } |
| 369 | |
| 370 | ptq->buffer = buffer; |
| 371 | |
| 372 | err = intel_pt_get_buffer(ptq, buffer, old_buffer, b); |
| 373 | if (err) |
| 374 | return err; |
| 375 | |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 376 | if (ptq->step_through_buffers) |
| 377 | ptq->stop = true; |
| 378 | |
Adrian Hunter | 9c66506 | 2018-03-07 16:02:27 +0200 | [diff] [blame] | 379 | if (b->len) { |
| 380 | if (old_buffer) |
| 381 | auxtrace_buffer__drop_data(old_buffer); |
| 382 | ptq->old_buffer = buffer; |
| 383 | } else { |
| 384 | auxtrace_buffer__drop_data(buffer); |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 385 | return intel_pt_get_trace(b, data); |
Adrian Hunter | 9c66506 | 2018-03-07 16:02:27 +0200 | [diff] [blame] | 386 | } |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 387 | |
| 388 | return 0; |
| 389 | } |
| 390 | |
| 391 | struct intel_pt_cache_entry { |
| 392 | struct auxtrace_cache_entry entry; |
| 393 | u64 insn_cnt; |
| 394 | u64 byte_cnt; |
| 395 | enum intel_pt_insn_op op; |
| 396 | enum intel_pt_insn_branch branch; |
| 397 | int length; |
| 398 | int32_t rel; |
Andi Kleen | faaa876 | 2016-10-07 16:42:26 +0300 | [diff] [blame] | 399 | char insn[INTEL_PT_INSN_BUF_SZ]; |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 400 | }; |
| 401 | |
| 402 | static int intel_pt_config_div(const char *var, const char *value, void *data) |
| 403 | { |
| 404 | int *d = data; |
| 405 | long val; |
| 406 | |
| 407 | if (!strcmp(var, "intel-pt.cache-divisor")) { |
| 408 | val = strtol(value, NULL, 0); |
| 409 | if (val > 0 && val <= INT_MAX) |
| 410 | *d = val; |
| 411 | } |
| 412 | |
| 413 | return 0; |
| 414 | } |
| 415 | |
| 416 | static int intel_pt_cache_divisor(void) |
| 417 | { |
| 418 | static int d; |
| 419 | |
| 420 | if (d) |
| 421 | return d; |
| 422 | |
| 423 | perf_config(intel_pt_config_div, &d); |
| 424 | |
| 425 | if (!d) |
| 426 | d = 64; |
| 427 | |
| 428 | return d; |
| 429 | } |
| 430 | |
| 431 | static unsigned int intel_pt_cache_size(struct dso *dso, |
| 432 | struct machine *machine) |
| 433 | { |
| 434 | off_t size; |
| 435 | |
| 436 | size = dso__data_size(dso, machine); |
| 437 | size /= intel_pt_cache_divisor(); |
| 438 | if (size < 1000) |
| 439 | return 10; |
| 440 | if (size > (1 << 21)) |
| 441 | return 21; |
| 442 | return 32 - __builtin_clz(size); |
| 443 | } |
| 444 | |
| 445 | static struct auxtrace_cache *intel_pt_cache(struct dso *dso, |
| 446 | struct machine *machine) |
| 447 | { |
| 448 | struct auxtrace_cache *c; |
| 449 | unsigned int bits; |
| 450 | |
| 451 | if (dso->auxtrace_cache) |
| 452 | return dso->auxtrace_cache; |
| 453 | |
| 454 | bits = intel_pt_cache_size(dso, machine); |
| 455 | |
| 456 | /* Ignoring cache creation failure */ |
| 457 | c = auxtrace_cache__new(bits, sizeof(struct intel_pt_cache_entry), 200); |
| 458 | |
| 459 | dso->auxtrace_cache = c; |
| 460 | |
| 461 | return c; |
| 462 | } |
| 463 | |
| 464 | static int intel_pt_cache_add(struct dso *dso, struct machine *machine, |
| 465 | u64 offset, u64 insn_cnt, u64 byte_cnt, |
| 466 | struct intel_pt_insn *intel_pt_insn) |
| 467 | { |
| 468 | struct auxtrace_cache *c = intel_pt_cache(dso, machine); |
| 469 | struct intel_pt_cache_entry *e; |
| 470 | int err; |
| 471 | |
| 472 | if (!c) |
| 473 | return -ENOMEM; |
| 474 | |
| 475 | e = auxtrace_cache__alloc_entry(c); |
| 476 | if (!e) |
| 477 | return -ENOMEM; |
| 478 | |
| 479 | e->insn_cnt = insn_cnt; |
| 480 | e->byte_cnt = byte_cnt; |
| 481 | e->op = intel_pt_insn->op; |
| 482 | e->branch = intel_pt_insn->branch; |
| 483 | e->length = intel_pt_insn->length; |
| 484 | e->rel = intel_pt_insn->rel; |
Andi Kleen | faaa876 | 2016-10-07 16:42:26 +0300 | [diff] [blame] | 485 | memcpy(e->insn, intel_pt_insn->buf, INTEL_PT_INSN_BUF_SZ); |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 486 | |
| 487 | err = auxtrace_cache__add(c, offset, &e->entry); |
| 488 | if (err) |
| 489 | auxtrace_cache__free_entry(c, e); |
| 490 | |
| 491 | return err; |
| 492 | } |
| 493 | |
| 494 | static struct intel_pt_cache_entry * |
| 495 | intel_pt_cache_lookup(struct dso *dso, struct machine *machine, u64 offset) |
| 496 | { |
| 497 | struct auxtrace_cache *c = intel_pt_cache(dso, machine); |
| 498 | |
| 499 | if (!c) |
| 500 | return NULL; |
| 501 | |
| 502 | return auxtrace_cache__lookup(dso->auxtrace_cache, offset); |
| 503 | } |
| 504 | |
Adrian Hunter | 5d4f0ed | 2018-10-31 11:10:43 +0200 | [diff] [blame] | 505 | static inline u8 intel_pt_cpumode(struct intel_pt *pt, uint64_t ip) |
| 506 | { |
| 507 | return ip >= pt->kernel_start ? |
| 508 | PERF_RECORD_MISC_KERNEL : |
| 509 | PERF_RECORD_MISC_USER; |
| 510 | } |
| 511 | |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 512 | static int intel_pt_walk_next_insn(struct intel_pt_insn *intel_pt_insn, |
| 513 | uint64_t *insn_cnt_ptr, uint64_t *ip, |
| 514 | uint64_t to_ip, uint64_t max_insn_cnt, |
| 515 | void *data) |
| 516 | { |
| 517 | struct intel_pt_queue *ptq = data; |
| 518 | struct machine *machine = ptq->pt->machine; |
| 519 | struct thread *thread; |
| 520 | struct addr_location al; |
Adrian Hunter | 32f98aa | 2016-10-07 16:42:25 +0300 | [diff] [blame] | 521 | unsigned char buf[INTEL_PT_INSN_BUF_SZ]; |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 522 | ssize_t len; |
| 523 | int x86_64; |
| 524 | u8 cpumode; |
| 525 | u64 offset, start_offset, start_ip; |
| 526 | u64 insn_cnt = 0; |
| 527 | bool one_map = true; |
| 528 | |
Andi Kleen | faaa876 | 2016-10-07 16:42:26 +0300 | [diff] [blame] | 529 | intel_pt_insn->length = 0; |
| 530 | |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 531 | if (to_ip && *ip == to_ip) |
| 532 | goto out_no_cache; |
| 533 | |
Adrian Hunter | 5d4f0ed | 2018-10-31 11:10:43 +0200 | [diff] [blame] | 534 | cpumode = intel_pt_cpumode(ptq->pt, *ip); |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 535 | |
| 536 | thread = ptq->thread; |
| 537 | if (!thread) { |
| 538 | if (cpumode != PERF_RECORD_MISC_KERNEL) |
| 539 | return -EINVAL; |
| 540 | thread = ptq->pt->unknown_thread; |
| 541 | } |
| 542 | |
| 543 | while (1) { |
Arnaldo Carvalho de Melo | 71a84b5 | 2018-04-24 11:58:56 -0300 | [diff] [blame] | 544 | if (!thread__find_map(thread, cpumode, *ip, &al) || !al.map->dso) |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 545 | return -EINVAL; |
| 546 | |
| 547 | if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR && |
| 548 | dso__data_status_seen(al.map->dso, |
| 549 | DSO_DATA_STATUS_SEEN_ITRACE)) |
| 550 | return -ENOENT; |
| 551 | |
| 552 | offset = al.map->map_ip(al.map, *ip); |
| 553 | |
| 554 | if (!to_ip && one_map) { |
| 555 | struct intel_pt_cache_entry *e; |
| 556 | |
| 557 | e = intel_pt_cache_lookup(al.map->dso, machine, offset); |
| 558 | if (e && |
| 559 | (!max_insn_cnt || e->insn_cnt <= max_insn_cnt)) { |
| 560 | *insn_cnt_ptr = e->insn_cnt; |
| 561 | *ip += e->byte_cnt; |
| 562 | intel_pt_insn->op = e->op; |
| 563 | intel_pt_insn->branch = e->branch; |
| 564 | intel_pt_insn->length = e->length; |
| 565 | intel_pt_insn->rel = e->rel; |
Andi Kleen | faaa876 | 2016-10-07 16:42:26 +0300 | [diff] [blame] | 566 | memcpy(intel_pt_insn->buf, e->insn, |
| 567 | INTEL_PT_INSN_BUF_SZ); |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 568 | intel_pt_log_insn_no_data(intel_pt_insn, *ip); |
| 569 | return 0; |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | start_offset = offset; |
| 574 | start_ip = *ip; |
| 575 | |
| 576 | /* Load maps to ensure dso->is_64_bit has been updated */ |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame] | 577 | map__load(al.map); |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 578 | |
| 579 | x86_64 = al.map->dso->is_64_bit; |
| 580 | |
| 581 | while (1) { |
| 582 | len = dso__data_read_offset(al.map->dso, machine, |
Adrian Hunter | 32f98aa | 2016-10-07 16:42:25 +0300 | [diff] [blame] | 583 | offset, buf, |
| 584 | INTEL_PT_INSN_BUF_SZ); |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 585 | if (len <= 0) |
| 586 | return -EINVAL; |
| 587 | |
| 588 | if (intel_pt_get_insn(buf, len, x86_64, intel_pt_insn)) |
| 589 | return -EINVAL; |
| 590 | |
| 591 | intel_pt_log_insn(intel_pt_insn, *ip); |
| 592 | |
| 593 | insn_cnt += 1; |
| 594 | |
| 595 | if (intel_pt_insn->branch != INTEL_PT_BR_NO_BRANCH) |
| 596 | goto out; |
| 597 | |
| 598 | if (max_insn_cnt && insn_cnt >= max_insn_cnt) |
| 599 | goto out_no_cache; |
| 600 | |
| 601 | *ip += intel_pt_insn->length; |
| 602 | |
| 603 | if (to_ip && *ip == to_ip) |
| 604 | goto out_no_cache; |
| 605 | |
| 606 | if (*ip >= al.map->end) |
| 607 | break; |
| 608 | |
| 609 | offset += intel_pt_insn->length; |
| 610 | } |
| 611 | one_map = false; |
| 612 | } |
| 613 | out: |
| 614 | *insn_cnt_ptr = insn_cnt; |
| 615 | |
| 616 | if (!one_map) |
| 617 | goto out_no_cache; |
| 618 | |
| 619 | /* |
| 620 | * Didn't lookup in the 'to_ip' case, so do it now to prevent duplicate |
| 621 | * entries. |
| 622 | */ |
| 623 | if (to_ip) { |
| 624 | struct intel_pt_cache_entry *e; |
| 625 | |
| 626 | e = intel_pt_cache_lookup(al.map->dso, machine, start_offset); |
| 627 | if (e) |
| 628 | return 0; |
| 629 | } |
| 630 | |
| 631 | /* Ignore cache errors */ |
| 632 | intel_pt_cache_add(al.map->dso, machine, start_offset, insn_cnt, |
| 633 | *ip - start_ip, intel_pt_insn); |
| 634 | |
| 635 | return 0; |
| 636 | |
| 637 | out_no_cache: |
| 638 | *insn_cnt_ptr = insn_cnt; |
| 639 | return 0; |
| 640 | } |
| 641 | |
Adrian Hunter | 2acee10 | 2016-09-23 17:38:48 +0300 | [diff] [blame] | 642 | static bool intel_pt_match_pgd_ip(struct intel_pt *pt, uint64_t ip, |
| 643 | uint64_t offset, const char *filename) |
| 644 | { |
| 645 | struct addr_filter *filt; |
| 646 | bool have_filter = false; |
| 647 | bool hit_tracestop = false; |
| 648 | bool hit_filter = false; |
| 649 | |
| 650 | list_for_each_entry(filt, &pt->filts.head, list) { |
| 651 | if (filt->start) |
| 652 | have_filter = true; |
| 653 | |
| 654 | if ((filename && !filt->filename) || |
| 655 | (!filename && filt->filename) || |
| 656 | (filename && strcmp(filename, filt->filename))) |
| 657 | continue; |
| 658 | |
| 659 | if (!(offset >= filt->addr && offset < filt->addr + filt->size)) |
| 660 | continue; |
| 661 | |
| 662 | intel_pt_log("TIP.PGD ip %#"PRIx64" offset %#"PRIx64" in %s hit filter: %s offset %#"PRIx64" size %#"PRIx64"\n", |
| 663 | ip, offset, filename ? filename : "[kernel]", |
| 664 | filt->start ? "filter" : "stop", |
| 665 | filt->addr, filt->size); |
| 666 | |
| 667 | if (filt->start) |
| 668 | hit_filter = true; |
| 669 | else |
| 670 | hit_tracestop = true; |
| 671 | } |
| 672 | |
| 673 | if (!hit_tracestop && !hit_filter) |
| 674 | intel_pt_log("TIP.PGD ip %#"PRIx64" offset %#"PRIx64" in %s is not in a filter region\n", |
| 675 | ip, offset, filename ? filename : "[kernel]"); |
| 676 | |
| 677 | return hit_tracestop || (have_filter && !hit_filter); |
| 678 | } |
| 679 | |
| 680 | static int __intel_pt_pgd_ip(uint64_t ip, void *data) |
| 681 | { |
| 682 | struct intel_pt_queue *ptq = data; |
| 683 | struct thread *thread; |
| 684 | struct addr_location al; |
| 685 | u8 cpumode; |
| 686 | u64 offset; |
| 687 | |
| 688 | if (ip >= ptq->pt->kernel_start) |
| 689 | return intel_pt_match_pgd_ip(ptq->pt, ip, ip, NULL); |
| 690 | |
| 691 | cpumode = PERF_RECORD_MISC_USER; |
| 692 | |
| 693 | thread = ptq->thread; |
| 694 | if (!thread) |
| 695 | return -EINVAL; |
| 696 | |
Arnaldo Carvalho de Melo | 71a84b5 | 2018-04-24 11:58:56 -0300 | [diff] [blame] | 697 | if (!thread__find_map(thread, cpumode, ip, &al) || !al.map->dso) |
Adrian Hunter | 2acee10 | 2016-09-23 17:38:48 +0300 | [diff] [blame] | 698 | return -EINVAL; |
| 699 | |
| 700 | offset = al.map->map_ip(al.map, ip); |
| 701 | |
| 702 | return intel_pt_match_pgd_ip(ptq->pt, ip, offset, |
| 703 | al.map->dso->long_name); |
| 704 | } |
| 705 | |
| 706 | static bool intel_pt_pgd_ip(uint64_t ip, void *data) |
| 707 | { |
| 708 | return __intel_pt_pgd_ip(ip, data) > 0; |
| 709 | } |
| 710 | |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 711 | static bool intel_pt_get_config(struct intel_pt *pt, |
| 712 | struct perf_event_attr *attr, u64 *config) |
| 713 | { |
| 714 | if (attr->type == pt->pmu_type) { |
| 715 | if (config) |
| 716 | *config = attr->config; |
| 717 | return true; |
| 718 | } |
| 719 | |
| 720 | return false; |
| 721 | } |
| 722 | |
| 723 | static bool intel_pt_exclude_kernel(struct intel_pt *pt) |
| 724 | { |
| 725 | struct perf_evsel *evsel; |
| 726 | |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 727 | evlist__for_each_entry(pt->session->evlist, evsel) { |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 728 | if (intel_pt_get_config(pt, &evsel->attr, NULL) && |
| 729 | !evsel->attr.exclude_kernel) |
| 730 | return false; |
| 731 | } |
| 732 | return true; |
| 733 | } |
| 734 | |
| 735 | static bool intel_pt_return_compression(struct intel_pt *pt) |
| 736 | { |
| 737 | struct perf_evsel *evsel; |
| 738 | u64 config; |
| 739 | |
| 740 | if (!pt->noretcomp_bit) |
| 741 | return true; |
| 742 | |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 743 | evlist__for_each_entry(pt->session->evlist, evsel) { |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 744 | if (intel_pt_get_config(pt, &evsel->attr, &config) && |
| 745 | (config & pt->noretcomp_bit)) |
| 746 | return false; |
| 747 | } |
| 748 | return true; |
| 749 | } |
| 750 | |
Adrian Hunter | 8395981 | 2017-05-26 11:17:11 +0300 | [diff] [blame] | 751 | static bool intel_pt_branch_enable(struct intel_pt *pt) |
| 752 | { |
| 753 | struct perf_evsel *evsel; |
| 754 | u64 config; |
| 755 | |
| 756 | evlist__for_each_entry(pt->session->evlist, evsel) { |
| 757 | if (intel_pt_get_config(pt, &evsel->attr, &config) && |
| 758 | (config & 1) && !(config & 0x2000)) |
| 759 | return false; |
| 760 | } |
| 761 | return true; |
| 762 | } |
| 763 | |
Adrian Hunter | 11fa7cb | 2015-07-17 19:33:54 +0300 | [diff] [blame] | 764 | static unsigned int intel_pt_mtc_period(struct intel_pt *pt) |
| 765 | { |
| 766 | struct perf_evsel *evsel; |
| 767 | unsigned int shift; |
| 768 | u64 config; |
| 769 | |
| 770 | if (!pt->mtc_freq_bits) |
| 771 | return 0; |
| 772 | |
| 773 | for (shift = 0, config = pt->mtc_freq_bits; !(config & 1); shift++) |
| 774 | config >>= 1; |
| 775 | |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 776 | evlist__for_each_entry(pt->session->evlist, evsel) { |
Adrian Hunter | 11fa7cb | 2015-07-17 19:33:54 +0300 | [diff] [blame] | 777 | if (intel_pt_get_config(pt, &evsel->attr, &config)) |
| 778 | return (config & pt->mtc_freq_bits) >> shift; |
| 779 | } |
| 780 | return 0; |
| 781 | } |
| 782 | |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 783 | static bool intel_pt_timeless_decoding(struct intel_pt *pt) |
| 784 | { |
| 785 | struct perf_evsel *evsel; |
| 786 | bool timeless_decoding = true; |
| 787 | u64 config; |
| 788 | |
| 789 | if (!pt->tsc_bit || !pt->cap_user_time_zero) |
| 790 | return true; |
| 791 | |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 792 | evlist__for_each_entry(pt->session->evlist, evsel) { |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 793 | if (!(evsel->attr.sample_type & PERF_SAMPLE_TIME)) |
| 794 | return true; |
| 795 | if (intel_pt_get_config(pt, &evsel->attr, &config)) { |
| 796 | if (config & pt->tsc_bit) |
| 797 | timeless_decoding = false; |
| 798 | else |
| 799 | return true; |
| 800 | } |
| 801 | } |
| 802 | return timeless_decoding; |
| 803 | } |
| 804 | |
| 805 | static bool intel_pt_tracing_kernel(struct intel_pt *pt) |
| 806 | { |
| 807 | struct perf_evsel *evsel; |
| 808 | |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 809 | evlist__for_each_entry(pt->session->evlist, evsel) { |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 810 | if (intel_pt_get_config(pt, &evsel->attr, NULL) && |
| 811 | !evsel->attr.exclude_kernel) |
| 812 | return true; |
| 813 | } |
| 814 | return false; |
| 815 | } |
| 816 | |
| 817 | static bool intel_pt_have_tsc(struct intel_pt *pt) |
| 818 | { |
| 819 | struct perf_evsel *evsel; |
| 820 | bool have_tsc = false; |
| 821 | u64 config; |
| 822 | |
| 823 | if (!pt->tsc_bit) |
| 824 | return false; |
| 825 | |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 826 | evlist__for_each_entry(pt->session->evlist, evsel) { |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 827 | if (intel_pt_get_config(pt, &evsel->attr, &config)) { |
| 828 | if (config & pt->tsc_bit) |
| 829 | have_tsc = true; |
| 830 | else |
| 831 | return false; |
| 832 | } |
| 833 | } |
| 834 | return have_tsc; |
| 835 | } |
| 836 | |
| 837 | static u64 intel_pt_ns_to_ticks(const struct intel_pt *pt, u64 ns) |
| 838 | { |
| 839 | u64 quot, rem; |
| 840 | |
| 841 | quot = ns / pt->tc.time_mult; |
| 842 | rem = ns % pt->tc.time_mult; |
| 843 | return (quot << pt->tc.time_shift) + (rem << pt->tc.time_shift) / |
| 844 | pt->tc.time_mult; |
| 845 | } |
| 846 | |
| 847 | static struct intel_pt_queue *intel_pt_alloc_queue(struct intel_pt *pt, |
| 848 | unsigned int queue_nr) |
| 849 | { |
| 850 | struct intel_pt_params params = { .get_trace = 0, }; |
Adrian Hunter | 9fb5233 | 2018-05-31 13:23:45 +0300 | [diff] [blame] | 851 | struct perf_env *env = pt->machine->env; |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 852 | struct intel_pt_queue *ptq; |
| 853 | |
| 854 | ptq = zalloc(sizeof(struct intel_pt_queue)); |
| 855 | if (!ptq) |
| 856 | return NULL; |
| 857 | |
| 858 | if (pt->synth_opts.callchain) { |
| 859 | size_t sz = sizeof(struct ip_callchain); |
| 860 | |
Adrian Hunter | 2424830 | 2018-10-31 11:10:42 +0200 | [diff] [blame] | 861 | /* Add 1 to callchain_sz for callchain context */ |
| 862 | sz += (pt->synth_opts.callchain_sz + 1) * sizeof(u64); |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 863 | ptq->chain = zalloc(sz); |
| 864 | if (!ptq->chain) |
| 865 | goto out_free; |
| 866 | } |
| 867 | |
Adrian Hunter | f14445e | 2015-09-25 16:15:45 +0300 | [diff] [blame] | 868 | if (pt->synth_opts.last_branch) { |
| 869 | size_t sz = sizeof(struct branch_stack); |
| 870 | |
| 871 | sz += pt->synth_opts.last_branch_sz * |
| 872 | sizeof(struct branch_entry); |
| 873 | ptq->last_branch = zalloc(sz); |
| 874 | if (!ptq->last_branch) |
| 875 | goto out_free; |
| 876 | ptq->last_branch_rb = zalloc(sz); |
| 877 | if (!ptq->last_branch_rb) |
| 878 | goto out_free; |
| 879 | } |
| 880 | |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 881 | ptq->event_buf = malloc(PERF_SAMPLE_MAX_SIZE); |
| 882 | if (!ptq->event_buf) |
| 883 | goto out_free; |
| 884 | |
| 885 | ptq->pt = pt; |
| 886 | ptq->queue_nr = queue_nr; |
| 887 | ptq->exclude_kernel = intel_pt_exclude_kernel(pt); |
| 888 | ptq->pid = -1; |
| 889 | ptq->tid = -1; |
| 890 | ptq->cpu = -1; |
| 891 | ptq->next_tid = -1; |
| 892 | |
| 893 | params.get_trace = intel_pt_get_trace; |
| 894 | params.walk_insn = intel_pt_walk_next_insn; |
Adrian Hunter | da9000a | 2019-06-04 16:00:08 +0300 | [diff] [blame] | 895 | params.lookahead = intel_pt_lookahead; |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 896 | params.data = ptq; |
| 897 | params.return_compression = intel_pt_return_compression(pt); |
Adrian Hunter | 8395981 | 2017-05-26 11:17:11 +0300 | [diff] [blame] | 898 | params.branch_enable = intel_pt_branch_enable(pt); |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 899 | params.max_non_turbo_ratio = pt->max_non_turbo_ratio; |
Adrian Hunter | 11fa7cb | 2015-07-17 19:33:54 +0300 | [diff] [blame] | 900 | params.mtc_period = intel_pt_mtc_period(pt); |
| 901 | params.tsc_ctc_ratio_n = pt->tsc_ctc_ratio_n; |
| 902 | params.tsc_ctc_ratio_d = pt->tsc_ctc_ratio_d; |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 903 | |
Adrian Hunter | 2acee10 | 2016-09-23 17:38:48 +0300 | [diff] [blame] | 904 | if (pt->filts.cnt > 0) |
| 905 | params.pgd_ip = intel_pt_pgd_ip; |
| 906 | |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 907 | if (pt->synth_opts.instructions) { |
| 908 | if (pt->synth_opts.period) { |
| 909 | switch (pt->synth_opts.period_type) { |
| 910 | case PERF_ITRACE_PERIOD_INSTRUCTIONS: |
| 911 | params.period_type = |
| 912 | INTEL_PT_PERIOD_INSTRUCTIONS; |
| 913 | params.period = pt->synth_opts.period; |
| 914 | break; |
| 915 | case PERF_ITRACE_PERIOD_TICKS: |
| 916 | params.period_type = INTEL_PT_PERIOD_TICKS; |
| 917 | params.period = pt->synth_opts.period; |
| 918 | break; |
| 919 | case PERF_ITRACE_PERIOD_NANOSECS: |
| 920 | params.period_type = INTEL_PT_PERIOD_TICKS; |
| 921 | params.period = intel_pt_ns_to_ticks(pt, |
| 922 | pt->synth_opts.period); |
| 923 | break; |
| 924 | default: |
| 925 | break; |
| 926 | } |
| 927 | } |
| 928 | |
| 929 | if (!params.period) { |
| 930 | params.period_type = INTEL_PT_PERIOD_INSTRUCTIONS; |
Adrian Hunter | e179134 | 2015-09-25 16:15:32 +0300 | [diff] [blame] | 931 | params.period = 1; |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 932 | } |
| 933 | } |
| 934 | |
Adrian Hunter | 9fb5233 | 2018-05-31 13:23:45 +0300 | [diff] [blame] | 935 | if (env->cpuid && !strncmp(env->cpuid, "GenuineIntel,6,92,", 18)) |
| 936 | params.flags |= INTEL_PT_FUP_WITH_NLIP; |
| 937 | |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 938 | ptq->decoder = intel_pt_decoder_new(¶ms); |
| 939 | if (!ptq->decoder) |
| 940 | goto out_free; |
| 941 | |
| 942 | return ptq; |
| 943 | |
| 944 | out_free: |
| 945 | zfree(&ptq->event_buf); |
Adrian Hunter | f14445e | 2015-09-25 16:15:45 +0300 | [diff] [blame] | 946 | zfree(&ptq->last_branch); |
| 947 | zfree(&ptq->last_branch_rb); |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 948 | zfree(&ptq->chain); |
| 949 | free(ptq); |
| 950 | return NULL; |
| 951 | } |
| 952 | |
| 953 | static void intel_pt_free_queue(void *priv) |
| 954 | { |
| 955 | struct intel_pt_queue *ptq = priv; |
| 956 | |
| 957 | if (!ptq) |
| 958 | return; |
| 959 | thread__zput(ptq->thread); |
| 960 | intel_pt_decoder_free(ptq->decoder); |
| 961 | zfree(&ptq->event_buf); |
Adrian Hunter | f14445e | 2015-09-25 16:15:45 +0300 | [diff] [blame] | 962 | zfree(&ptq->last_branch); |
| 963 | zfree(&ptq->last_branch_rb); |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 964 | zfree(&ptq->chain); |
| 965 | free(ptq); |
| 966 | } |
| 967 | |
| 968 | static void intel_pt_set_pid_tid_cpu(struct intel_pt *pt, |
| 969 | struct auxtrace_queue *queue) |
| 970 | { |
| 971 | struct intel_pt_queue *ptq = queue->priv; |
| 972 | |
| 973 | if (queue->tid == -1 || pt->have_sched_switch) { |
| 974 | ptq->tid = machine__get_current_tid(pt->machine, ptq->cpu); |
| 975 | thread__zput(ptq->thread); |
| 976 | } |
| 977 | |
| 978 | if (!ptq->thread && ptq->tid != -1) |
| 979 | ptq->thread = machine__find_thread(pt->machine, -1, ptq->tid); |
| 980 | |
| 981 | if (ptq->thread) { |
| 982 | ptq->pid = ptq->thread->pid_; |
| 983 | if (queue->cpu == -1) |
| 984 | ptq->cpu = ptq->thread->cpu; |
| 985 | } |
| 986 | } |
| 987 | |
| 988 | static void intel_pt_sample_flags(struct intel_pt_queue *ptq) |
| 989 | { |
| 990 | if (ptq->state->flags & INTEL_PT_ABORT_TX) { |
| 991 | ptq->flags = PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT; |
| 992 | } else if (ptq->state->flags & INTEL_PT_ASYNC) { |
| 993 | if (ptq->state->to_ip) |
| 994 | ptq->flags = PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | |
| 995 | PERF_IP_FLAG_ASYNC | |
| 996 | PERF_IP_FLAG_INTERRUPT; |
| 997 | else |
| 998 | ptq->flags = PERF_IP_FLAG_BRANCH | |
| 999 | PERF_IP_FLAG_TRACE_END; |
| 1000 | ptq->insn_len = 0; |
| 1001 | } else { |
| 1002 | if (ptq->state->from_ip) |
| 1003 | ptq->flags = intel_pt_insn_type(ptq->state->insn_op); |
| 1004 | else |
| 1005 | ptq->flags = PERF_IP_FLAG_BRANCH | |
| 1006 | PERF_IP_FLAG_TRACE_BEGIN; |
| 1007 | if (ptq->state->flags & INTEL_PT_IN_TX) |
| 1008 | ptq->flags |= PERF_IP_FLAG_IN_TX; |
| 1009 | ptq->insn_len = ptq->state->insn_len; |
Andi Kleen | faaa876 | 2016-10-07 16:42:26 +0300 | [diff] [blame] | 1010 | memcpy(ptq->insn, ptq->state->insn, INTEL_PT_INSN_BUF_SZ); |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 1011 | } |
Adrian Hunter | c6b5da0 | 2018-09-20 16:00:47 +0300 | [diff] [blame] | 1012 | |
| 1013 | if (ptq->state->type & INTEL_PT_TRACE_BEGIN) |
| 1014 | ptq->flags |= PERF_IP_FLAG_TRACE_BEGIN; |
| 1015 | if (ptq->state->type & INTEL_PT_TRACE_END) |
| 1016 | ptq->flags |= PERF_IP_FLAG_TRACE_END; |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 1017 | } |
| 1018 | |
Adrian Hunter | 2c47db9 | 2019-06-04 16:00:09 +0300 | [diff] [blame] | 1019 | static void intel_pt_setup_time_range(struct intel_pt *pt, |
| 1020 | struct intel_pt_queue *ptq) |
| 1021 | { |
| 1022 | if (!pt->range_cnt) |
| 1023 | return; |
| 1024 | |
| 1025 | ptq->sel_timestamp = pt->time_ranges[0].start; |
| 1026 | ptq->sel_idx = 0; |
| 1027 | |
| 1028 | if (ptq->sel_timestamp) { |
| 1029 | ptq->sel_start = true; |
| 1030 | } else { |
| 1031 | ptq->sel_timestamp = pt->time_ranges[0].end; |
| 1032 | ptq->sel_start = false; |
| 1033 | } |
| 1034 | } |
| 1035 | |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 1036 | static int intel_pt_setup_queue(struct intel_pt *pt, |
| 1037 | struct auxtrace_queue *queue, |
| 1038 | unsigned int queue_nr) |
| 1039 | { |
| 1040 | struct intel_pt_queue *ptq = queue->priv; |
| 1041 | |
| 1042 | if (list_empty(&queue->head)) |
| 1043 | return 0; |
| 1044 | |
| 1045 | if (!ptq) { |
| 1046 | ptq = intel_pt_alloc_queue(pt, queue_nr); |
| 1047 | if (!ptq) |
| 1048 | return -ENOMEM; |
| 1049 | queue->priv = ptq; |
| 1050 | |
| 1051 | if (queue->cpu != -1) |
| 1052 | ptq->cpu = queue->cpu; |
| 1053 | ptq->tid = queue->tid; |
| 1054 | |
Adrian Hunter | 1c071c8 | 2018-03-07 16:02:26 +0200 | [diff] [blame] | 1055 | if (pt->sampling_mode && !pt->snapshot_mode && |
| 1056 | pt->timeless_decoding) |
| 1057 | ptq->step_through_buffers = true; |
Adrian Hunter | 63d8e38 | 2018-03-07 16:02:22 +0200 | [diff] [blame] | 1058 | |
| 1059 | ptq->sync_switch = pt->sync_switch; |
Adrian Hunter | 2c47db9 | 2019-06-04 16:00:09 +0300 | [diff] [blame] | 1060 | |
| 1061 | intel_pt_setup_time_range(pt, ptq); |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 1062 | } |
| 1063 | |
| 1064 | if (!ptq->on_heap && |
Adrian Hunter | 63d8e38 | 2018-03-07 16:02:22 +0200 | [diff] [blame] | 1065 | (!ptq->sync_switch || |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 1066 | ptq->switch_state != INTEL_PT_SS_EXPECTING_SWITCH_EVENT)) { |
| 1067 | const struct intel_pt_state *state; |
| 1068 | int ret; |
| 1069 | |
| 1070 | if (pt->timeless_decoding) |
| 1071 | return 0; |
| 1072 | |
| 1073 | intel_pt_log("queue %u getting timestamp\n", queue_nr); |
| 1074 | intel_pt_log("queue %u decoding cpu %d pid %d tid %d\n", |
| 1075 | queue_nr, ptq->cpu, ptq->pid, ptq->tid); |
Adrian Hunter | 2c47db9 | 2019-06-04 16:00:09 +0300 | [diff] [blame] | 1076 | |
| 1077 | if (ptq->sel_start && ptq->sel_timestamp) { |
| 1078 | ret = intel_pt_fast_forward(ptq->decoder, |
| 1079 | ptq->sel_timestamp); |
| 1080 | if (ret) |
| 1081 | return ret; |
| 1082 | } |
| 1083 | |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 1084 | while (1) { |
| 1085 | state = intel_pt_decode(ptq->decoder); |
| 1086 | if (state->err) { |
| 1087 | if (state->err == INTEL_PT_ERR_NODATA) { |
| 1088 | intel_pt_log("queue %u has no timestamp\n", |
| 1089 | queue_nr); |
| 1090 | return 0; |
| 1091 | } |
| 1092 | continue; |
| 1093 | } |
| 1094 | if (state->timestamp) |
| 1095 | break; |
| 1096 | } |
| 1097 | |
| 1098 | ptq->timestamp = state->timestamp; |
| 1099 | intel_pt_log("queue %u timestamp 0x%" PRIx64 "\n", |
| 1100 | queue_nr, ptq->timestamp); |
| 1101 | ptq->state = state; |
| 1102 | ptq->have_sample = true; |
Adrian Hunter | 2c47db9 | 2019-06-04 16:00:09 +0300 | [diff] [blame] | 1103 | if (ptq->sel_start && ptq->sel_timestamp && |
| 1104 | ptq->timestamp < ptq->sel_timestamp) |
| 1105 | ptq->have_sample = false; |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 1106 | intel_pt_sample_flags(ptq); |
| 1107 | ret = auxtrace_heap__add(&pt->heap, queue_nr, ptq->timestamp); |
| 1108 | if (ret) |
| 1109 | return ret; |
| 1110 | ptq->on_heap = true; |
| 1111 | } |
| 1112 | |
| 1113 | return 0; |
| 1114 | } |
| 1115 | |
| 1116 | static int intel_pt_setup_queues(struct intel_pt *pt) |
| 1117 | { |
| 1118 | unsigned int i; |
| 1119 | int ret; |
| 1120 | |
| 1121 | for (i = 0; i < pt->queues.nr_queues; i++) { |
| 1122 | ret = intel_pt_setup_queue(pt, &pt->queues.queue_array[i], i); |
| 1123 | if (ret) |
| 1124 | return ret; |
| 1125 | } |
| 1126 | return 0; |
| 1127 | } |
| 1128 | |
Adrian Hunter | f14445e | 2015-09-25 16:15:45 +0300 | [diff] [blame] | 1129 | static inline void intel_pt_copy_last_branch_rb(struct intel_pt_queue *ptq) |
| 1130 | { |
| 1131 | struct branch_stack *bs_src = ptq->last_branch_rb; |
| 1132 | struct branch_stack *bs_dst = ptq->last_branch; |
| 1133 | size_t nr = 0; |
| 1134 | |
| 1135 | bs_dst->nr = bs_src->nr; |
| 1136 | |
| 1137 | if (!bs_src->nr) |
| 1138 | return; |
| 1139 | |
| 1140 | nr = ptq->pt->synth_opts.last_branch_sz - ptq->last_branch_pos; |
| 1141 | memcpy(&bs_dst->entries[0], |
| 1142 | &bs_src->entries[ptq->last_branch_pos], |
| 1143 | sizeof(struct branch_entry) * nr); |
| 1144 | |
| 1145 | if (bs_src->nr >= ptq->pt->synth_opts.last_branch_sz) { |
| 1146 | memcpy(&bs_dst->entries[nr], |
| 1147 | &bs_src->entries[0], |
| 1148 | sizeof(struct branch_entry) * ptq->last_branch_pos); |
| 1149 | } |
| 1150 | } |
| 1151 | |
| 1152 | static inline void intel_pt_reset_last_branch_rb(struct intel_pt_queue *ptq) |
| 1153 | { |
| 1154 | ptq->last_branch_pos = 0; |
| 1155 | ptq->last_branch_rb->nr = 0; |
| 1156 | } |
| 1157 | |
| 1158 | static void intel_pt_update_last_branch_rb(struct intel_pt_queue *ptq) |
| 1159 | { |
| 1160 | const struct intel_pt_state *state = ptq->state; |
| 1161 | struct branch_stack *bs = ptq->last_branch_rb; |
| 1162 | struct branch_entry *be; |
| 1163 | |
| 1164 | if (!ptq->last_branch_pos) |
| 1165 | ptq->last_branch_pos = ptq->pt->synth_opts.last_branch_sz; |
| 1166 | |
| 1167 | ptq->last_branch_pos -= 1; |
| 1168 | |
| 1169 | be = &bs->entries[ptq->last_branch_pos]; |
| 1170 | be->from = state->from_ip; |
| 1171 | be->to = state->to_ip; |
| 1172 | be->flags.abort = !!(state->flags & INTEL_PT_ABORT_TX); |
| 1173 | be->flags.in_tx = !!(state->flags & INTEL_PT_IN_TX); |
| 1174 | /* No support for mispredict */ |
Adrian Hunter | ba11ba6 | 2015-09-25 16:15:56 +0300 | [diff] [blame] | 1175 | be->flags.mispred = ptq->pt->mispred_all; |
Adrian Hunter | f14445e | 2015-09-25 16:15:45 +0300 | [diff] [blame] | 1176 | |
| 1177 | if (bs->nr < ptq->pt->synth_opts.last_branch_sz) |
| 1178 | bs->nr += 1; |
| 1179 | } |
| 1180 | |
Adrian Hunter | 0f3e537 | 2017-05-26 11:17:27 +0300 | [diff] [blame] | 1181 | static inline bool intel_pt_skip_event(struct intel_pt *pt) |
| 1182 | { |
| 1183 | return pt->synth_opts.initial_skip && |
| 1184 | pt->num_events++ < pt->synth_opts.initial_skip; |
| 1185 | } |
| 1186 | |
Adrian Hunter | 0dfded3 | 2019-06-10 10:27:57 +0300 | [diff] [blame] | 1187 | static void intel_pt_prep_a_sample(struct intel_pt_queue *ptq, |
| 1188 | union perf_event *event, |
| 1189 | struct perf_sample *sample) |
| 1190 | { |
| 1191 | event->sample.header.type = PERF_RECORD_SAMPLE; |
| 1192 | event->sample.header.size = sizeof(struct perf_event_header); |
| 1193 | |
| 1194 | sample->pid = ptq->pid; |
| 1195 | sample->tid = ptq->tid; |
| 1196 | sample->cpu = ptq->cpu; |
| 1197 | sample->insn_len = ptq->insn_len; |
| 1198 | memcpy(sample->insn, ptq->insn, INTEL_PT_INSN_BUF_SZ); |
| 1199 | } |
| 1200 | |
Adrian Hunter | 0f3e537 | 2017-05-26 11:17:27 +0300 | [diff] [blame] | 1201 | static void intel_pt_prep_b_sample(struct intel_pt *pt, |
| 1202 | struct intel_pt_queue *ptq, |
| 1203 | union perf_event *event, |
| 1204 | struct perf_sample *sample) |
| 1205 | { |
Adrian Hunter | 0dfded3 | 2019-06-10 10:27:57 +0300 | [diff] [blame] | 1206 | intel_pt_prep_a_sample(ptq, event, sample); |
| 1207 | |
Adrian Hunter | 0f3e537 | 2017-05-26 11:17:27 +0300 | [diff] [blame] | 1208 | if (!pt->timeless_decoding) |
| 1209 | sample->time = tsc_to_perf_time(ptq->timestamp, &pt->tc); |
| 1210 | |
Adrian Hunter | 0f3e537 | 2017-05-26 11:17:27 +0300 | [diff] [blame] | 1211 | sample->ip = ptq->state->from_ip; |
Adrian Hunter | 5d4f0ed | 2018-10-31 11:10:43 +0200 | [diff] [blame] | 1212 | sample->cpumode = intel_pt_cpumode(pt, sample->ip); |
Adrian Hunter | 0f3e537 | 2017-05-26 11:17:27 +0300 | [diff] [blame] | 1213 | sample->addr = ptq->state->to_ip; |
| 1214 | sample->period = 1; |
Adrian Hunter | 0f3e537 | 2017-05-26 11:17:27 +0300 | [diff] [blame] | 1215 | sample->flags = ptq->flags; |
Adrian Hunter | 5d4f0ed | 2018-10-31 11:10:43 +0200 | [diff] [blame] | 1216 | |
Adrian Hunter | 5d4f0ed | 2018-10-31 11:10:43 +0200 | [diff] [blame] | 1217 | event->sample.header.misc = sample->cpumode; |
Adrian Hunter | 0f3e537 | 2017-05-26 11:17:27 +0300 | [diff] [blame] | 1218 | } |
| 1219 | |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 1220 | static int intel_pt_inject_event(union perf_event *event, |
Adrian Hunter | a10eb53 | 2018-01-16 15:14:50 +0200 | [diff] [blame] | 1221 | struct perf_sample *sample, u64 type) |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 1222 | { |
| 1223 | event->header.size = perf_event__sample_event_size(sample, type, 0); |
Adrian Hunter | 936f1f3 | 2018-01-16 15:14:52 +0200 | [diff] [blame] | 1224 | return perf_event__synthesize_sample(event, type, 0, sample); |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 1225 | } |
| 1226 | |
Adrian Hunter | 0f3e537 | 2017-05-26 11:17:27 +0300 | [diff] [blame] | 1227 | static inline int intel_pt_opt_inject(struct intel_pt *pt, |
| 1228 | union perf_event *event, |
| 1229 | struct perf_sample *sample, u64 type) |
| 1230 | { |
| 1231 | if (!pt->synth_opts.inject) |
| 1232 | return 0; |
| 1233 | |
Adrian Hunter | a10eb53 | 2018-01-16 15:14:50 +0200 | [diff] [blame] | 1234 | return intel_pt_inject_event(event, sample, type); |
Adrian Hunter | 0f3e537 | 2017-05-26 11:17:27 +0300 | [diff] [blame] | 1235 | } |
| 1236 | |
| 1237 | static int intel_pt_deliver_synth_b_event(struct intel_pt *pt, |
| 1238 | union perf_event *event, |
| 1239 | struct perf_sample *sample, u64 type) |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 1240 | { |
| 1241 | int ret; |
Adrian Hunter | 0f3e537 | 2017-05-26 11:17:27 +0300 | [diff] [blame] | 1242 | |
| 1243 | ret = intel_pt_opt_inject(pt, event, sample, type); |
| 1244 | if (ret) |
| 1245 | return ret; |
| 1246 | |
| 1247 | ret = perf_session__deliver_synth_event(pt->session, event, sample); |
| 1248 | if (ret) |
| 1249 | pr_err("Intel PT: failed to deliver event, error %d\n", ret); |
| 1250 | |
| 1251 | return ret; |
| 1252 | } |
| 1253 | |
| 1254 | static int intel_pt_synth_branch_sample(struct intel_pt_queue *ptq) |
| 1255 | { |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 1256 | struct intel_pt *pt = ptq->pt; |
| 1257 | union perf_event *event = ptq->event_buf; |
| 1258 | struct perf_sample sample = { .ip = 0, }; |
Adrian Hunter | f14445e | 2015-09-25 16:15:45 +0300 | [diff] [blame] | 1259 | struct dummy_branch_stack { |
| 1260 | u64 nr; |
| 1261 | struct branch_entry entries; |
| 1262 | } dummy_bs; |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 1263 | |
Adrian Hunter | 385e330 | 2015-09-25 16:15:44 +0300 | [diff] [blame] | 1264 | if (pt->branches_filter && !(pt->branches_filter & ptq->flags)) |
| 1265 | return 0; |
| 1266 | |
Adrian Hunter | 0f3e537 | 2017-05-26 11:17:27 +0300 | [diff] [blame] | 1267 | if (intel_pt_skip_event(pt)) |
Andi Kleen | d1706b3 | 2016-03-28 10:45:38 -0700 | [diff] [blame] | 1268 | return 0; |
| 1269 | |
Adrian Hunter | 0f3e537 | 2017-05-26 11:17:27 +0300 | [diff] [blame] | 1270 | intel_pt_prep_b_sample(pt, ptq, event, &sample); |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 1271 | |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 1272 | sample.id = ptq->pt->branches_id; |
| 1273 | sample.stream_id = ptq->pt->branches_id; |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 1274 | |
Adrian Hunter | f14445e | 2015-09-25 16:15:45 +0300 | [diff] [blame] | 1275 | /* |
| 1276 | * perf report cannot handle events without a branch stack when using |
| 1277 | * SORT_MODE__BRANCH so make a dummy one. |
| 1278 | */ |
| 1279 | if (pt->synth_opts.last_branch && sort__mode == SORT_MODE__BRANCH) { |
| 1280 | dummy_bs = (struct dummy_branch_stack){ |
| 1281 | .nr = 1, |
| 1282 | .entries = { |
| 1283 | .from = sample.ip, |
| 1284 | .to = sample.addr, |
| 1285 | }, |
| 1286 | }; |
| 1287 | sample.branch_stack = (struct branch_stack *)&dummy_bs; |
| 1288 | } |
| 1289 | |
Adrian Hunter | 5b1dc0f | 2019-05-20 14:37:13 +0300 | [diff] [blame] | 1290 | sample.cyc_cnt = ptq->ipc_cyc_cnt - ptq->last_br_cyc_cnt; |
| 1291 | if (sample.cyc_cnt) { |
| 1292 | sample.insn_cnt = ptq->ipc_insn_cnt - ptq->last_br_insn_cnt; |
| 1293 | ptq->last_br_insn_cnt = ptq->ipc_insn_cnt; |
| 1294 | ptq->last_br_cyc_cnt = ptq->ipc_cyc_cnt; |
| 1295 | } |
| 1296 | |
Adrian Hunter | 0f3e537 | 2017-05-26 11:17:27 +0300 | [diff] [blame] | 1297 | return intel_pt_deliver_synth_b_event(pt, event, &sample, |
| 1298 | pt->branches_sample_type); |
| 1299 | } |
| 1300 | |
| 1301 | static void intel_pt_prep_sample(struct intel_pt *pt, |
| 1302 | struct intel_pt_queue *ptq, |
| 1303 | union perf_event *event, |
| 1304 | struct perf_sample *sample) |
| 1305 | { |
| 1306 | intel_pt_prep_b_sample(pt, ptq, event, sample); |
| 1307 | |
| 1308 | if (pt->synth_opts.callchain) { |
Adrian Hunter | 256d92b | 2018-12-21 14:06:19 +0200 | [diff] [blame] | 1309 | thread_stack__sample(ptq->thread, ptq->cpu, ptq->chain, |
Adrian Hunter | 2424830 | 2018-10-31 11:10:42 +0200 | [diff] [blame] | 1310 | pt->synth_opts.callchain_sz + 1, |
| 1311 | sample->ip, pt->kernel_start); |
Adrian Hunter | 0f3e537 | 2017-05-26 11:17:27 +0300 | [diff] [blame] | 1312 | sample->callchain = ptq->chain; |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 1313 | } |
| 1314 | |
Adrian Hunter | 0f3e537 | 2017-05-26 11:17:27 +0300 | [diff] [blame] | 1315 | if (pt->synth_opts.last_branch) { |
| 1316 | intel_pt_copy_last_branch_rb(ptq); |
| 1317 | sample->branch_stack = ptq->last_branch; |
| 1318 | } |
| 1319 | } |
| 1320 | |
| 1321 | static inline int intel_pt_deliver_synth_event(struct intel_pt *pt, |
| 1322 | struct intel_pt_queue *ptq, |
| 1323 | union perf_event *event, |
| 1324 | struct perf_sample *sample, |
| 1325 | u64 type) |
| 1326 | { |
| 1327 | int ret; |
| 1328 | |
| 1329 | ret = intel_pt_deliver_synth_b_event(pt, event, sample, type); |
| 1330 | |
| 1331 | if (pt->synth_opts.last_branch) |
| 1332 | intel_pt_reset_last_branch_rb(ptq); |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 1333 | |
| 1334 | return ret; |
| 1335 | } |
| 1336 | |
| 1337 | static int intel_pt_synth_instruction_sample(struct intel_pt_queue *ptq) |
| 1338 | { |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 1339 | struct intel_pt *pt = ptq->pt; |
| 1340 | union perf_event *event = ptq->event_buf; |
| 1341 | struct perf_sample sample = { .ip = 0, }; |
| 1342 | |
Adrian Hunter | 0f3e537 | 2017-05-26 11:17:27 +0300 | [diff] [blame] | 1343 | if (intel_pt_skip_event(pt)) |
Andi Kleen | d1706b3 | 2016-03-28 10:45:38 -0700 | [diff] [blame] | 1344 | return 0; |
| 1345 | |
Adrian Hunter | 0f3e537 | 2017-05-26 11:17:27 +0300 | [diff] [blame] | 1346 | intel_pt_prep_sample(pt, ptq, event, &sample); |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 1347 | |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 1348 | sample.id = ptq->pt->instructions_id; |
| 1349 | sample.stream_id = ptq->pt->instructions_id; |
Adrian Hunter | 2a21d03 | 2015-07-17 19:33:48 +0300 | [diff] [blame] | 1350 | sample.period = ptq->state->tot_insn_cnt - ptq->last_insn_cnt; |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 1351 | |
Adrian Hunter | 5b1dc0f | 2019-05-20 14:37:13 +0300 | [diff] [blame] | 1352 | sample.cyc_cnt = ptq->ipc_cyc_cnt - ptq->last_in_cyc_cnt; |
| 1353 | if (sample.cyc_cnt) { |
| 1354 | sample.insn_cnt = ptq->ipc_insn_cnt - ptq->last_in_insn_cnt; |
| 1355 | ptq->last_in_insn_cnt = ptq->ipc_insn_cnt; |
| 1356 | ptq->last_in_cyc_cnt = ptq->ipc_cyc_cnt; |
| 1357 | } |
| 1358 | |
Adrian Hunter | 2a21d03 | 2015-07-17 19:33:48 +0300 | [diff] [blame] | 1359 | ptq->last_insn_cnt = ptq->state->tot_insn_cnt; |
| 1360 | |
Adrian Hunter | 0f3e537 | 2017-05-26 11:17:27 +0300 | [diff] [blame] | 1361 | return intel_pt_deliver_synth_event(pt, ptq, event, &sample, |
| 1362 | pt->instructions_sample_type); |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 1363 | } |
| 1364 | |
| 1365 | static int intel_pt_synth_transaction_sample(struct intel_pt_queue *ptq) |
| 1366 | { |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 1367 | struct intel_pt *pt = ptq->pt; |
| 1368 | union perf_event *event = ptq->event_buf; |
| 1369 | struct perf_sample sample = { .ip = 0, }; |
| 1370 | |
Adrian Hunter | 0f3e537 | 2017-05-26 11:17:27 +0300 | [diff] [blame] | 1371 | if (intel_pt_skip_event(pt)) |
Andi Kleen | d1706b3 | 2016-03-28 10:45:38 -0700 | [diff] [blame] | 1372 | return 0; |
| 1373 | |
Adrian Hunter | 0f3e537 | 2017-05-26 11:17:27 +0300 | [diff] [blame] | 1374 | intel_pt_prep_sample(pt, ptq, event, &sample); |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 1375 | |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 1376 | sample.id = ptq->pt->transactions_id; |
| 1377 | sample.stream_id = ptq->pt->transactions_id; |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 1378 | |
Adrian Hunter | 0f3e537 | 2017-05-26 11:17:27 +0300 | [diff] [blame] | 1379 | return intel_pt_deliver_synth_event(pt, ptq, event, &sample, |
| 1380 | pt->transactions_sample_type); |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 1381 | } |
| 1382 | |
Adrian Hunter | 3797307 | 2017-06-30 11:36:45 +0300 | [diff] [blame] | 1383 | static void intel_pt_prep_p_sample(struct intel_pt *pt, |
| 1384 | struct intel_pt_queue *ptq, |
| 1385 | union perf_event *event, |
| 1386 | struct perf_sample *sample) |
| 1387 | { |
| 1388 | intel_pt_prep_sample(pt, ptq, event, sample); |
| 1389 | |
| 1390 | /* |
| 1391 | * Zero IP is used to mean "trace start" but that is not the case for |
| 1392 | * power or PTWRITE events with no IP, so clear the flags. |
| 1393 | */ |
| 1394 | if (!sample->ip) |
| 1395 | sample->flags = 0; |
| 1396 | } |
| 1397 | |
| 1398 | static int intel_pt_synth_ptwrite_sample(struct intel_pt_queue *ptq) |
| 1399 | { |
| 1400 | struct intel_pt *pt = ptq->pt; |
| 1401 | union perf_event *event = ptq->event_buf; |
| 1402 | struct perf_sample sample = { .ip = 0, }; |
| 1403 | struct perf_synth_intel_ptwrite raw; |
| 1404 | |
| 1405 | if (intel_pt_skip_event(pt)) |
| 1406 | return 0; |
| 1407 | |
| 1408 | intel_pt_prep_p_sample(pt, ptq, event, &sample); |
| 1409 | |
| 1410 | sample.id = ptq->pt->ptwrites_id; |
| 1411 | sample.stream_id = ptq->pt->ptwrites_id; |
| 1412 | |
| 1413 | raw.flags = 0; |
| 1414 | raw.ip = !!(ptq->state->flags & INTEL_PT_FUP_IP); |
| 1415 | raw.payload = cpu_to_le64(ptq->state->ptw_payload); |
| 1416 | |
| 1417 | sample.raw_size = perf_synth__raw_size(raw); |
| 1418 | sample.raw_data = perf_synth__raw_data(&raw); |
| 1419 | |
| 1420 | return intel_pt_deliver_synth_event(pt, ptq, event, &sample, |
| 1421 | pt->ptwrites_sample_type); |
| 1422 | } |
| 1423 | |
| 1424 | static int intel_pt_synth_cbr_sample(struct intel_pt_queue *ptq) |
| 1425 | { |
| 1426 | struct intel_pt *pt = ptq->pt; |
| 1427 | union perf_event *event = ptq->event_buf; |
| 1428 | struct perf_sample sample = { .ip = 0, }; |
| 1429 | struct perf_synth_intel_cbr raw; |
| 1430 | u32 flags; |
| 1431 | |
| 1432 | if (intel_pt_skip_event(pt)) |
| 1433 | return 0; |
| 1434 | |
| 1435 | intel_pt_prep_p_sample(pt, ptq, event, &sample); |
| 1436 | |
| 1437 | sample.id = ptq->pt->cbr_id; |
| 1438 | sample.stream_id = ptq->pt->cbr_id; |
| 1439 | |
| 1440 | flags = (u16)ptq->state->cbr_payload | (pt->max_non_turbo_ratio << 16); |
| 1441 | raw.flags = cpu_to_le32(flags); |
| 1442 | raw.freq = cpu_to_le32(raw.cbr * pt->cbr2khz); |
| 1443 | raw.reserved3 = 0; |
| 1444 | |
| 1445 | sample.raw_size = perf_synth__raw_size(raw); |
| 1446 | sample.raw_data = perf_synth__raw_data(&raw); |
| 1447 | |
| 1448 | return intel_pt_deliver_synth_event(pt, ptq, event, &sample, |
| 1449 | pt->pwr_events_sample_type); |
| 1450 | } |
| 1451 | |
| 1452 | static int intel_pt_synth_mwait_sample(struct intel_pt_queue *ptq) |
| 1453 | { |
| 1454 | struct intel_pt *pt = ptq->pt; |
| 1455 | union perf_event *event = ptq->event_buf; |
| 1456 | struct perf_sample sample = { .ip = 0, }; |
| 1457 | struct perf_synth_intel_mwait raw; |
| 1458 | |
| 1459 | if (intel_pt_skip_event(pt)) |
| 1460 | return 0; |
| 1461 | |
| 1462 | intel_pt_prep_p_sample(pt, ptq, event, &sample); |
| 1463 | |
| 1464 | sample.id = ptq->pt->mwait_id; |
| 1465 | sample.stream_id = ptq->pt->mwait_id; |
| 1466 | |
| 1467 | raw.reserved = 0; |
| 1468 | raw.payload = cpu_to_le64(ptq->state->mwait_payload); |
| 1469 | |
| 1470 | sample.raw_size = perf_synth__raw_size(raw); |
| 1471 | sample.raw_data = perf_synth__raw_data(&raw); |
| 1472 | |
| 1473 | return intel_pt_deliver_synth_event(pt, ptq, event, &sample, |
| 1474 | pt->pwr_events_sample_type); |
| 1475 | } |
| 1476 | |
| 1477 | static int intel_pt_synth_pwre_sample(struct intel_pt_queue *ptq) |
| 1478 | { |
| 1479 | struct intel_pt *pt = ptq->pt; |
| 1480 | union perf_event *event = ptq->event_buf; |
| 1481 | struct perf_sample sample = { .ip = 0, }; |
| 1482 | struct perf_synth_intel_pwre raw; |
| 1483 | |
| 1484 | if (intel_pt_skip_event(pt)) |
| 1485 | return 0; |
| 1486 | |
| 1487 | intel_pt_prep_p_sample(pt, ptq, event, &sample); |
| 1488 | |
| 1489 | sample.id = ptq->pt->pwre_id; |
| 1490 | sample.stream_id = ptq->pt->pwre_id; |
| 1491 | |
| 1492 | raw.reserved = 0; |
| 1493 | raw.payload = cpu_to_le64(ptq->state->pwre_payload); |
| 1494 | |
| 1495 | sample.raw_size = perf_synth__raw_size(raw); |
| 1496 | sample.raw_data = perf_synth__raw_data(&raw); |
| 1497 | |
| 1498 | return intel_pt_deliver_synth_event(pt, ptq, event, &sample, |
| 1499 | pt->pwr_events_sample_type); |
| 1500 | } |
| 1501 | |
| 1502 | static int intel_pt_synth_exstop_sample(struct intel_pt_queue *ptq) |
| 1503 | { |
| 1504 | struct intel_pt *pt = ptq->pt; |
| 1505 | union perf_event *event = ptq->event_buf; |
| 1506 | struct perf_sample sample = { .ip = 0, }; |
| 1507 | struct perf_synth_intel_exstop raw; |
| 1508 | |
| 1509 | if (intel_pt_skip_event(pt)) |
| 1510 | return 0; |
| 1511 | |
| 1512 | intel_pt_prep_p_sample(pt, ptq, event, &sample); |
| 1513 | |
| 1514 | sample.id = ptq->pt->exstop_id; |
| 1515 | sample.stream_id = ptq->pt->exstop_id; |
| 1516 | |
| 1517 | raw.flags = 0; |
| 1518 | raw.ip = !!(ptq->state->flags & INTEL_PT_FUP_IP); |
| 1519 | |
| 1520 | sample.raw_size = perf_synth__raw_size(raw); |
| 1521 | sample.raw_data = perf_synth__raw_data(&raw); |
| 1522 | |
| 1523 | return intel_pt_deliver_synth_event(pt, ptq, event, &sample, |
| 1524 | pt->pwr_events_sample_type); |
| 1525 | } |
| 1526 | |
| 1527 | static int intel_pt_synth_pwrx_sample(struct intel_pt_queue *ptq) |
| 1528 | { |
| 1529 | struct intel_pt *pt = ptq->pt; |
| 1530 | union perf_event *event = ptq->event_buf; |
| 1531 | struct perf_sample sample = { .ip = 0, }; |
| 1532 | struct perf_synth_intel_pwrx raw; |
| 1533 | |
| 1534 | if (intel_pt_skip_event(pt)) |
| 1535 | return 0; |
| 1536 | |
| 1537 | intel_pt_prep_p_sample(pt, ptq, event, &sample); |
| 1538 | |
| 1539 | sample.id = ptq->pt->pwrx_id; |
| 1540 | sample.stream_id = ptq->pt->pwrx_id; |
| 1541 | |
| 1542 | raw.reserved = 0; |
| 1543 | raw.payload = cpu_to_le64(ptq->state->pwrx_payload); |
| 1544 | |
| 1545 | sample.raw_size = perf_synth__raw_size(raw); |
| 1546 | sample.raw_data = perf_synth__raw_data(&raw); |
| 1547 | |
| 1548 | return intel_pt_deliver_synth_event(pt, ptq, event, &sample, |
| 1549 | pt->pwr_events_sample_type); |
| 1550 | } |
| 1551 | |
Adrian Hunter | 9e9a618 | 2019-06-10 10:27:59 +0300 | [diff] [blame^] | 1552 | /* |
| 1553 | * PEBS gp_regs array indexes plus 1 so that 0 means not present. Refer |
| 1554 | * intel_pt_add_gp_regs(). |
| 1555 | */ |
| 1556 | static const int pebs_gp_regs[] = { |
| 1557 | [PERF_REG_X86_FLAGS] = 1, |
| 1558 | [PERF_REG_X86_IP] = 2, |
| 1559 | [PERF_REG_X86_AX] = 3, |
| 1560 | [PERF_REG_X86_CX] = 4, |
| 1561 | [PERF_REG_X86_DX] = 5, |
| 1562 | [PERF_REG_X86_BX] = 6, |
| 1563 | [PERF_REG_X86_SP] = 7, |
| 1564 | [PERF_REG_X86_BP] = 8, |
| 1565 | [PERF_REG_X86_SI] = 9, |
| 1566 | [PERF_REG_X86_DI] = 10, |
| 1567 | [PERF_REG_X86_R8] = 11, |
| 1568 | [PERF_REG_X86_R9] = 12, |
| 1569 | [PERF_REG_X86_R10] = 13, |
| 1570 | [PERF_REG_X86_R11] = 14, |
| 1571 | [PERF_REG_X86_R12] = 15, |
| 1572 | [PERF_REG_X86_R13] = 16, |
| 1573 | [PERF_REG_X86_R14] = 17, |
| 1574 | [PERF_REG_X86_R15] = 18, |
| 1575 | }; |
| 1576 | |
| 1577 | static u64 *intel_pt_add_gp_regs(struct regs_dump *intr_regs, u64 *pos, |
| 1578 | const struct intel_pt_blk_items *items, |
| 1579 | u64 regs_mask) |
| 1580 | { |
| 1581 | const u64 *gp_regs = items->val[INTEL_PT_GP_REGS_POS]; |
| 1582 | u32 mask = items->mask[INTEL_PT_GP_REGS_POS]; |
| 1583 | u32 bit; |
| 1584 | int i; |
| 1585 | |
| 1586 | for (i = 0, bit = 1; i < PERF_REG_X86_64_MAX; i++, bit <<= 1) { |
| 1587 | /* Get the PEBS gp_regs array index */ |
| 1588 | int n = pebs_gp_regs[i] - 1; |
| 1589 | |
| 1590 | if (n < 0) |
| 1591 | continue; |
| 1592 | /* |
| 1593 | * Add only registers that were requested (i.e. 'regs_mask') and |
| 1594 | * that were provided (i.e. 'mask'), and update the resulting |
| 1595 | * mask (i.e. 'intr_regs->mask') accordingly. |
| 1596 | */ |
| 1597 | if (mask & 1 << n && regs_mask & bit) { |
| 1598 | intr_regs->mask |= bit; |
| 1599 | *pos++ = gp_regs[n]; |
| 1600 | } |
| 1601 | } |
| 1602 | |
| 1603 | return pos; |
| 1604 | } |
| 1605 | |
Adrian Hunter | 9d0bc53 | 2019-06-10 10:27:58 +0300 | [diff] [blame] | 1606 | static int intel_pt_synth_pebs_sample(struct intel_pt_queue *ptq) |
Adrian Hunter | e62ca65 | 2019-06-10 10:27:56 +0300 | [diff] [blame] | 1607 | { |
Adrian Hunter | 9d0bc53 | 2019-06-10 10:27:58 +0300 | [diff] [blame] | 1608 | const struct intel_pt_blk_items *items = &ptq->state->items; |
| 1609 | struct perf_sample sample = { .ip = 0, }; |
| 1610 | union perf_event *event = ptq->event_buf; |
| 1611 | struct intel_pt *pt = ptq->pt; |
| 1612 | struct perf_evsel *evsel = pt->pebs_evsel; |
| 1613 | u64 sample_type = evsel->attr.sample_type; |
| 1614 | u64 id = evsel->id[0]; |
| 1615 | u8 cpumode; |
| 1616 | |
| 1617 | if (intel_pt_skip_event(pt)) |
| 1618 | return 0; |
| 1619 | |
| 1620 | intel_pt_prep_a_sample(ptq, event, &sample); |
| 1621 | |
| 1622 | sample.id = id; |
| 1623 | sample.stream_id = id; |
| 1624 | |
| 1625 | if (!evsel->attr.freq) |
| 1626 | sample.period = evsel->attr.sample_period; |
| 1627 | |
| 1628 | /* No support for non-zero CS base */ |
| 1629 | if (items->has_ip) |
| 1630 | sample.ip = items->ip; |
| 1631 | else if (items->has_rip) |
| 1632 | sample.ip = items->rip; |
| 1633 | else |
| 1634 | sample.ip = ptq->state->from_ip; |
| 1635 | |
| 1636 | /* No support for guest mode at this time */ |
| 1637 | cpumode = sample.ip < ptq->pt->kernel_start ? |
| 1638 | PERF_RECORD_MISC_USER : |
| 1639 | PERF_RECORD_MISC_KERNEL; |
| 1640 | |
| 1641 | event->sample.header.misc = cpumode | PERF_RECORD_MISC_EXACT_IP; |
| 1642 | |
| 1643 | sample.cpumode = cpumode; |
| 1644 | |
| 1645 | if (sample_type & PERF_SAMPLE_TIME) { |
| 1646 | u64 timestamp = 0; |
| 1647 | |
| 1648 | if (items->has_timestamp) |
| 1649 | timestamp = items->timestamp; |
| 1650 | else if (!pt->timeless_decoding) |
| 1651 | timestamp = ptq->timestamp; |
| 1652 | if (timestamp) |
| 1653 | sample.time = tsc_to_perf_time(timestamp, &pt->tc); |
| 1654 | } |
| 1655 | |
Adrian Hunter | 9e9a618 | 2019-06-10 10:27:59 +0300 | [diff] [blame^] | 1656 | if (sample_type & PERF_SAMPLE_REGS_INTR && |
| 1657 | items->mask[INTEL_PT_GP_REGS_POS]) { |
| 1658 | u64 regs[sizeof(sample.intr_regs.mask)]; |
| 1659 | u64 regs_mask = evsel->attr.sample_regs_intr; |
| 1660 | |
| 1661 | sample.intr_regs.abi = items->is_32_bit ? |
| 1662 | PERF_SAMPLE_REGS_ABI_32 : |
| 1663 | PERF_SAMPLE_REGS_ABI_64; |
| 1664 | sample.intr_regs.regs = regs; |
| 1665 | |
| 1666 | intel_pt_add_gp_regs(&sample.intr_regs, regs, items, regs_mask); |
| 1667 | } |
| 1668 | |
Adrian Hunter | 9d0bc53 | 2019-06-10 10:27:58 +0300 | [diff] [blame] | 1669 | return intel_pt_deliver_synth_event(pt, ptq, event, &sample, sample_type); |
Adrian Hunter | e62ca65 | 2019-06-10 10:27:56 +0300 | [diff] [blame] | 1670 | } |
| 1671 | |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 1672 | static int intel_pt_synth_error(struct intel_pt *pt, int code, int cpu, |
Adrian Hunter | 16bd432 | 2019-02-06 12:39:47 +0200 | [diff] [blame] | 1673 | pid_t pid, pid_t tid, u64 ip, u64 timestamp) |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 1674 | { |
| 1675 | union perf_event event; |
| 1676 | char msg[MAX_AUXTRACE_ERROR_MSG]; |
| 1677 | int err; |
| 1678 | |
| 1679 | intel_pt__strerror(code, msg, MAX_AUXTRACE_ERROR_MSG); |
| 1680 | |
| 1681 | auxtrace_synth_error(&event.auxtrace_error, PERF_AUXTRACE_ERROR_ITRACE, |
Adrian Hunter | 16bd432 | 2019-02-06 12:39:47 +0200 | [diff] [blame] | 1682 | code, cpu, pid, tid, ip, msg, timestamp); |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 1683 | |
| 1684 | err = perf_session__deliver_synth_event(pt->session, &event, NULL); |
| 1685 | if (err) |
| 1686 | pr_err("Intel Processor Trace: failed to deliver error event, error %d\n", |
| 1687 | err); |
| 1688 | |
| 1689 | return err; |
| 1690 | } |
| 1691 | |
Adrian Hunter | 16bd432 | 2019-02-06 12:39:47 +0200 | [diff] [blame] | 1692 | static int intel_ptq_synth_error(struct intel_pt_queue *ptq, |
| 1693 | const struct intel_pt_state *state) |
| 1694 | { |
| 1695 | struct intel_pt *pt = ptq->pt; |
| 1696 | u64 tm = ptq->timestamp; |
| 1697 | |
| 1698 | tm = pt->timeless_decoding ? 0 : tsc_to_perf_time(tm, &pt->tc); |
| 1699 | |
| 1700 | return intel_pt_synth_error(pt, state->err, ptq->cpu, ptq->pid, |
| 1701 | ptq->tid, state->from_ip, tm); |
| 1702 | } |
| 1703 | |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 1704 | static int intel_pt_next_tid(struct intel_pt *pt, struct intel_pt_queue *ptq) |
| 1705 | { |
| 1706 | struct auxtrace_queue *queue; |
| 1707 | pid_t tid = ptq->next_tid; |
| 1708 | int err; |
| 1709 | |
| 1710 | if (tid == -1) |
| 1711 | return 0; |
| 1712 | |
| 1713 | intel_pt_log("switch: cpu %d tid %d\n", ptq->cpu, tid); |
| 1714 | |
| 1715 | err = machine__set_current_tid(pt->machine, ptq->cpu, -1, tid); |
| 1716 | |
| 1717 | queue = &pt->queues.queue_array[ptq->queue_nr]; |
| 1718 | intel_pt_set_pid_tid_cpu(pt, queue); |
| 1719 | |
| 1720 | ptq->next_tid = -1; |
| 1721 | |
| 1722 | return err; |
| 1723 | } |
| 1724 | |
| 1725 | static inline bool intel_pt_is_switch_ip(struct intel_pt_queue *ptq, u64 ip) |
| 1726 | { |
| 1727 | struct intel_pt *pt = ptq->pt; |
| 1728 | |
| 1729 | return ip == pt->switch_ip && |
| 1730 | (ptq->flags & PERF_IP_FLAG_BRANCH) && |
| 1731 | !(ptq->flags & (PERF_IP_FLAG_CONDITIONAL | PERF_IP_FLAG_ASYNC | |
| 1732 | PERF_IP_FLAG_INTERRUPT | PERF_IP_FLAG_TX_ABORT)); |
| 1733 | } |
| 1734 | |
Adrian Hunter | 3797307 | 2017-06-30 11:36:45 +0300 | [diff] [blame] | 1735 | #define INTEL_PT_PWR_EVT (INTEL_PT_MWAIT_OP | INTEL_PT_PWR_ENTRY | \ |
| 1736 | INTEL_PT_EX_STOP | INTEL_PT_PWR_EXIT | \ |
| 1737 | INTEL_PT_CBR_CHG) |
| 1738 | |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 1739 | static int intel_pt_sample(struct intel_pt_queue *ptq) |
| 1740 | { |
| 1741 | const struct intel_pt_state *state = ptq->state; |
| 1742 | struct intel_pt *pt = ptq->pt; |
| 1743 | int err; |
| 1744 | |
| 1745 | if (!ptq->have_sample) |
| 1746 | return 0; |
| 1747 | |
| 1748 | ptq->have_sample = false; |
| 1749 | |
Adrian Hunter | 5b1dc0f | 2019-05-20 14:37:13 +0300 | [diff] [blame] | 1750 | if (ptq->state->tot_cyc_cnt > ptq->ipc_cyc_cnt) { |
| 1751 | /* |
| 1752 | * Cycle count and instruction count only go together to create |
| 1753 | * a valid IPC ratio when the cycle count changes. |
| 1754 | */ |
| 1755 | ptq->ipc_insn_cnt = ptq->state->tot_insn_cnt; |
| 1756 | ptq->ipc_cyc_cnt = ptq->state->tot_cyc_cnt; |
| 1757 | } |
| 1758 | |
Adrian Hunter | e62ca65 | 2019-06-10 10:27:56 +0300 | [diff] [blame] | 1759 | /* |
| 1760 | * Do PEBS first to allow for the possibility that the PEBS timestamp |
| 1761 | * precedes the current timestamp. |
| 1762 | */ |
| 1763 | if (pt->sample_pebs && state->type & INTEL_PT_BLK_ITEMS) { |
| 1764 | err = intel_pt_synth_pebs_sample(ptq); |
| 1765 | if (err) |
| 1766 | return err; |
| 1767 | } |
| 1768 | |
Adrian Hunter | 3797307 | 2017-06-30 11:36:45 +0300 | [diff] [blame] | 1769 | if (pt->sample_pwr_events && (state->type & INTEL_PT_PWR_EVT)) { |
| 1770 | if (state->type & INTEL_PT_CBR_CHG) { |
| 1771 | err = intel_pt_synth_cbr_sample(ptq); |
| 1772 | if (err) |
| 1773 | return err; |
| 1774 | } |
| 1775 | if (state->type & INTEL_PT_MWAIT_OP) { |
| 1776 | err = intel_pt_synth_mwait_sample(ptq); |
| 1777 | if (err) |
| 1778 | return err; |
| 1779 | } |
| 1780 | if (state->type & INTEL_PT_PWR_ENTRY) { |
| 1781 | err = intel_pt_synth_pwre_sample(ptq); |
| 1782 | if (err) |
| 1783 | return err; |
| 1784 | } |
| 1785 | if (state->type & INTEL_PT_EX_STOP) { |
| 1786 | err = intel_pt_synth_exstop_sample(ptq); |
| 1787 | if (err) |
| 1788 | return err; |
| 1789 | } |
| 1790 | if (state->type & INTEL_PT_PWR_EXIT) { |
| 1791 | err = intel_pt_synth_pwrx_sample(ptq); |
| 1792 | if (err) |
| 1793 | return err; |
| 1794 | } |
| 1795 | } |
| 1796 | |
Adrian Hunter | 406a180 | 2017-05-26 11:17:29 +0300 | [diff] [blame] | 1797 | if (pt->sample_instructions && (state->type & INTEL_PT_INSTRUCTION)) { |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 1798 | err = intel_pt_synth_instruction_sample(ptq); |
| 1799 | if (err) |
| 1800 | return err; |
| 1801 | } |
| 1802 | |
Adrian Hunter | 406a180 | 2017-05-26 11:17:29 +0300 | [diff] [blame] | 1803 | if (pt->sample_transactions && (state->type & INTEL_PT_TRANSACTION)) { |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 1804 | err = intel_pt_synth_transaction_sample(ptq); |
| 1805 | if (err) |
| 1806 | return err; |
| 1807 | } |
| 1808 | |
Adrian Hunter | 3797307 | 2017-06-30 11:36:45 +0300 | [diff] [blame] | 1809 | if (pt->sample_ptwrites && (state->type & INTEL_PT_PTW)) { |
| 1810 | err = intel_pt_synth_ptwrite_sample(ptq); |
| 1811 | if (err) |
| 1812 | return err; |
| 1813 | } |
| 1814 | |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 1815 | if (!(state->type & INTEL_PT_BRANCH)) |
| 1816 | return 0; |
| 1817 | |
Adrian Hunter | 50f73637 | 2016-06-23 16:40:57 +0300 | [diff] [blame] | 1818 | if (pt->synth_opts.callchain || pt->synth_opts.thread_stack) |
Adrian Hunter | 256d92b | 2018-12-21 14:06:19 +0200 | [diff] [blame] | 1819 | thread_stack__event(ptq->thread, ptq->cpu, ptq->flags, state->from_ip, |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 1820 | state->to_ip, ptq->insn_len, |
| 1821 | state->trace_nr); |
| 1822 | else |
Adrian Hunter | 256d92b | 2018-12-21 14:06:19 +0200 | [diff] [blame] | 1823 | thread_stack__set_trace_nr(ptq->thread, ptq->cpu, state->trace_nr); |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 1824 | |
| 1825 | if (pt->sample_branches) { |
| 1826 | err = intel_pt_synth_branch_sample(ptq); |
| 1827 | if (err) |
| 1828 | return err; |
| 1829 | } |
| 1830 | |
Adrian Hunter | f14445e | 2015-09-25 16:15:45 +0300 | [diff] [blame] | 1831 | if (pt->synth_opts.last_branch) |
| 1832 | intel_pt_update_last_branch_rb(ptq); |
| 1833 | |
Adrian Hunter | 63d8e38 | 2018-03-07 16:02:22 +0200 | [diff] [blame] | 1834 | if (!ptq->sync_switch) |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 1835 | return 0; |
| 1836 | |
| 1837 | if (intel_pt_is_switch_ip(ptq, state->to_ip)) { |
| 1838 | switch (ptq->switch_state) { |
Adrian Hunter | dbcb82b | 2018-05-31 13:23:42 +0300 | [diff] [blame] | 1839 | case INTEL_PT_SS_NOT_TRACING: |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 1840 | case INTEL_PT_SS_UNKNOWN: |
| 1841 | case INTEL_PT_SS_EXPECTING_SWITCH_IP: |
| 1842 | err = intel_pt_next_tid(pt, ptq); |
| 1843 | if (err) |
| 1844 | return err; |
| 1845 | ptq->switch_state = INTEL_PT_SS_TRACING; |
| 1846 | break; |
| 1847 | default: |
| 1848 | ptq->switch_state = INTEL_PT_SS_EXPECTING_SWITCH_EVENT; |
| 1849 | return 1; |
| 1850 | } |
| 1851 | } else if (!state->to_ip) { |
| 1852 | ptq->switch_state = INTEL_PT_SS_NOT_TRACING; |
| 1853 | } else if (ptq->switch_state == INTEL_PT_SS_NOT_TRACING) { |
| 1854 | ptq->switch_state = INTEL_PT_SS_UNKNOWN; |
| 1855 | } else if (ptq->switch_state == INTEL_PT_SS_UNKNOWN && |
| 1856 | state->to_ip == pt->ptss_ip && |
| 1857 | (ptq->flags & PERF_IP_FLAG_CALL)) { |
| 1858 | ptq->switch_state = INTEL_PT_SS_TRACING; |
| 1859 | } |
| 1860 | |
| 1861 | return 0; |
| 1862 | } |
| 1863 | |
Adrian Hunter | 86c2786 | 2015-08-13 12:40:57 +0300 | [diff] [blame] | 1864 | static u64 intel_pt_switch_ip(struct intel_pt *pt, u64 *ptss_ip) |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 1865 | { |
Adrian Hunter | 86c2786 | 2015-08-13 12:40:57 +0300 | [diff] [blame] | 1866 | struct machine *machine = pt->machine; |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 1867 | struct map *map; |
| 1868 | struct symbol *sym, *start; |
| 1869 | u64 ip, switch_ip = 0; |
Adrian Hunter | 86c2786 | 2015-08-13 12:40:57 +0300 | [diff] [blame] | 1870 | const char *ptss; |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 1871 | |
| 1872 | if (ptss_ip) |
| 1873 | *ptss_ip = 0; |
| 1874 | |
Arnaldo Carvalho de Melo | a5e813c | 2015-09-30 11:54:04 -0300 | [diff] [blame] | 1875 | map = machine__kernel_map(machine); |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 1876 | if (!map) |
| 1877 | return 0; |
| 1878 | |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame] | 1879 | if (map__load(map)) |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 1880 | return 0; |
| 1881 | |
Arnaldo Carvalho de Melo | 5cf88a6 | 2018-04-25 17:01:46 -0300 | [diff] [blame] | 1882 | start = dso__first_symbol(map->dso); |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 1883 | |
| 1884 | for (sym = start; sym; sym = dso__next_symbol(sym)) { |
| 1885 | if (sym->binding == STB_GLOBAL && |
| 1886 | !strcmp(sym->name, "__switch_to")) { |
| 1887 | ip = map->unmap_ip(map, sym->start); |
| 1888 | if (ip >= map->start && ip < map->end) { |
| 1889 | switch_ip = ip; |
| 1890 | break; |
| 1891 | } |
| 1892 | } |
| 1893 | } |
| 1894 | |
| 1895 | if (!switch_ip || !ptss_ip) |
| 1896 | return 0; |
| 1897 | |
Adrian Hunter | 86c2786 | 2015-08-13 12:40:57 +0300 | [diff] [blame] | 1898 | if (pt->have_sched_switch == 1) |
| 1899 | ptss = "perf_trace_sched_switch"; |
| 1900 | else |
| 1901 | ptss = "__perf_event_task_sched_out"; |
| 1902 | |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 1903 | for (sym = start; sym; sym = dso__next_symbol(sym)) { |
Adrian Hunter | 86c2786 | 2015-08-13 12:40:57 +0300 | [diff] [blame] | 1904 | if (!strcmp(sym->name, ptss)) { |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 1905 | ip = map->unmap_ip(map, sym->start); |
| 1906 | if (ip >= map->start && ip < map->end) { |
| 1907 | *ptss_ip = ip; |
| 1908 | break; |
| 1909 | } |
| 1910 | } |
| 1911 | } |
| 1912 | |
| 1913 | return switch_ip; |
| 1914 | } |
| 1915 | |
Adrian Hunter | 63d8e38 | 2018-03-07 16:02:22 +0200 | [diff] [blame] | 1916 | static void intel_pt_enable_sync_switch(struct intel_pt *pt) |
| 1917 | { |
| 1918 | unsigned int i; |
| 1919 | |
| 1920 | pt->sync_switch = true; |
| 1921 | |
| 1922 | for (i = 0; i < pt->queues.nr_queues; i++) { |
| 1923 | struct auxtrace_queue *queue = &pt->queues.queue_array[i]; |
| 1924 | struct intel_pt_queue *ptq = queue->priv; |
| 1925 | |
| 1926 | if (ptq) |
| 1927 | ptq->sync_switch = true; |
| 1928 | } |
| 1929 | } |
| 1930 | |
Adrian Hunter | 2c47db9 | 2019-06-04 16:00:09 +0300 | [diff] [blame] | 1931 | /* |
| 1932 | * To filter against time ranges, it is only necessary to look at the next start |
| 1933 | * or end time. |
| 1934 | */ |
| 1935 | static bool intel_pt_next_time(struct intel_pt_queue *ptq) |
| 1936 | { |
| 1937 | struct intel_pt *pt = ptq->pt; |
| 1938 | |
| 1939 | if (ptq->sel_start) { |
| 1940 | /* Next time is an end time */ |
| 1941 | ptq->sel_start = false; |
| 1942 | ptq->sel_timestamp = pt->time_ranges[ptq->sel_idx].end; |
| 1943 | return true; |
| 1944 | } else if (ptq->sel_idx + 1 < pt->range_cnt) { |
| 1945 | /* Next time is a start time */ |
| 1946 | ptq->sel_start = true; |
| 1947 | ptq->sel_idx += 1; |
| 1948 | ptq->sel_timestamp = pt->time_ranges[ptq->sel_idx].start; |
| 1949 | return true; |
| 1950 | } |
| 1951 | |
| 1952 | /* No next time */ |
| 1953 | return false; |
| 1954 | } |
| 1955 | |
| 1956 | static int intel_pt_time_filter(struct intel_pt_queue *ptq, u64 *ff_timestamp) |
| 1957 | { |
| 1958 | int err; |
| 1959 | |
| 1960 | while (1) { |
| 1961 | if (ptq->sel_start) { |
| 1962 | if (ptq->timestamp >= ptq->sel_timestamp) { |
| 1963 | /* After start time, so consider next time */ |
| 1964 | intel_pt_next_time(ptq); |
| 1965 | if (!ptq->sel_timestamp) { |
| 1966 | /* No end time */ |
| 1967 | return 0; |
| 1968 | } |
| 1969 | /* Check against end time */ |
| 1970 | continue; |
| 1971 | } |
| 1972 | /* Before start time, so fast forward */ |
| 1973 | ptq->have_sample = false; |
| 1974 | if (ptq->sel_timestamp > *ff_timestamp) { |
| 1975 | if (ptq->sync_switch) { |
| 1976 | intel_pt_next_tid(ptq->pt, ptq); |
| 1977 | ptq->switch_state = INTEL_PT_SS_UNKNOWN; |
| 1978 | } |
| 1979 | *ff_timestamp = ptq->sel_timestamp; |
| 1980 | err = intel_pt_fast_forward(ptq->decoder, |
| 1981 | ptq->sel_timestamp); |
| 1982 | if (err) |
| 1983 | return err; |
| 1984 | } |
| 1985 | return 0; |
| 1986 | } else if (ptq->timestamp > ptq->sel_timestamp) { |
| 1987 | /* After end time, so consider next time */ |
| 1988 | if (!intel_pt_next_time(ptq)) { |
| 1989 | /* No next time range, so stop decoding */ |
| 1990 | ptq->have_sample = false; |
| 1991 | ptq->switch_state = INTEL_PT_SS_NOT_TRACING; |
| 1992 | return 1; |
| 1993 | } |
| 1994 | /* Check against next start time */ |
| 1995 | continue; |
| 1996 | } else { |
| 1997 | /* Before end time */ |
| 1998 | return 0; |
| 1999 | } |
| 2000 | } |
| 2001 | } |
| 2002 | |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 2003 | static int intel_pt_run_decoder(struct intel_pt_queue *ptq, u64 *timestamp) |
| 2004 | { |
| 2005 | const struct intel_pt_state *state = ptq->state; |
| 2006 | struct intel_pt *pt = ptq->pt; |
Adrian Hunter | 2c47db9 | 2019-06-04 16:00:09 +0300 | [diff] [blame] | 2007 | u64 ff_timestamp = 0; |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 2008 | int err; |
| 2009 | |
| 2010 | if (!pt->kernel_start) { |
| 2011 | pt->kernel_start = machine__kernel_start(pt->machine); |
Adrian Hunter | 86c2786 | 2015-08-13 12:40:57 +0300 | [diff] [blame] | 2012 | if (pt->per_cpu_mmaps && |
| 2013 | (pt->have_sched_switch == 1 || pt->have_sched_switch == 3) && |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 2014 | !pt->timeless_decoding && intel_pt_tracing_kernel(pt) && |
| 2015 | !pt->sampling_mode) { |
Adrian Hunter | 86c2786 | 2015-08-13 12:40:57 +0300 | [diff] [blame] | 2016 | pt->switch_ip = intel_pt_switch_ip(pt, &pt->ptss_ip); |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 2017 | if (pt->switch_ip) { |
| 2018 | intel_pt_log("switch_ip: %"PRIx64" ptss_ip: %"PRIx64"\n", |
| 2019 | pt->switch_ip, pt->ptss_ip); |
Adrian Hunter | 63d8e38 | 2018-03-07 16:02:22 +0200 | [diff] [blame] | 2020 | intel_pt_enable_sync_switch(pt); |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 2021 | } |
| 2022 | } |
| 2023 | } |
| 2024 | |
| 2025 | intel_pt_log("queue %u decoding cpu %d pid %d tid %d\n", |
| 2026 | ptq->queue_nr, ptq->cpu, ptq->pid, ptq->tid); |
| 2027 | while (1) { |
| 2028 | err = intel_pt_sample(ptq); |
| 2029 | if (err) |
| 2030 | return err; |
| 2031 | |
| 2032 | state = intel_pt_decode(ptq->decoder); |
| 2033 | if (state->err) { |
| 2034 | if (state->err == INTEL_PT_ERR_NODATA) |
| 2035 | return 1; |
Adrian Hunter | 63d8e38 | 2018-03-07 16:02:22 +0200 | [diff] [blame] | 2036 | if (ptq->sync_switch && |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 2037 | state->from_ip >= pt->kernel_start) { |
Adrian Hunter | 63d8e38 | 2018-03-07 16:02:22 +0200 | [diff] [blame] | 2038 | ptq->sync_switch = false; |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 2039 | intel_pt_next_tid(pt, ptq); |
| 2040 | } |
| 2041 | if (pt->synth_opts.errors) { |
Adrian Hunter | 16bd432 | 2019-02-06 12:39:47 +0200 | [diff] [blame] | 2042 | err = intel_ptq_synth_error(ptq, state); |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 2043 | if (err) |
| 2044 | return err; |
| 2045 | } |
| 2046 | continue; |
| 2047 | } |
| 2048 | |
| 2049 | ptq->state = state; |
| 2050 | ptq->have_sample = true; |
| 2051 | intel_pt_sample_flags(ptq); |
| 2052 | |
| 2053 | /* Use estimated TSC upon return to user space */ |
| 2054 | if (pt->est_tsc && |
| 2055 | (state->from_ip >= pt->kernel_start || !state->from_ip) && |
| 2056 | state->to_ip && state->to_ip < pt->kernel_start) { |
| 2057 | intel_pt_log("TSC %"PRIx64" est. TSC %"PRIx64"\n", |
| 2058 | state->timestamp, state->est_timestamp); |
| 2059 | ptq->timestamp = state->est_timestamp; |
| 2060 | /* Use estimated TSC in unknown switch state */ |
Adrian Hunter | 63d8e38 | 2018-03-07 16:02:22 +0200 | [diff] [blame] | 2061 | } else if (ptq->sync_switch && |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 2062 | ptq->switch_state == INTEL_PT_SS_UNKNOWN && |
| 2063 | intel_pt_is_switch_ip(ptq, state->to_ip) && |
| 2064 | ptq->next_tid == -1) { |
| 2065 | intel_pt_log("TSC %"PRIx64" est. TSC %"PRIx64"\n", |
| 2066 | state->timestamp, state->est_timestamp); |
| 2067 | ptq->timestamp = state->est_timestamp; |
| 2068 | } else if (state->timestamp > ptq->timestamp) { |
| 2069 | ptq->timestamp = state->timestamp; |
| 2070 | } |
| 2071 | |
Adrian Hunter | 2c47db9 | 2019-06-04 16:00:09 +0300 | [diff] [blame] | 2072 | if (ptq->sel_timestamp) { |
| 2073 | err = intel_pt_time_filter(ptq, &ff_timestamp); |
| 2074 | if (err) |
| 2075 | return err; |
| 2076 | } |
| 2077 | |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 2078 | if (!pt->timeless_decoding && ptq->timestamp >= *timestamp) { |
| 2079 | *timestamp = ptq->timestamp; |
| 2080 | return 0; |
| 2081 | } |
| 2082 | } |
| 2083 | return 0; |
| 2084 | } |
| 2085 | |
| 2086 | static inline int intel_pt_update_queues(struct intel_pt *pt) |
| 2087 | { |
| 2088 | if (pt->queues.new_data) { |
| 2089 | pt->queues.new_data = false; |
| 2090 | return intel_pt_setup_queues(pt); |
| 2091 | } |
| 2092 | return 0; |
| 2093 | } |
| 2094 | |
| 2095 | static int intel_pt_process_queues(struct intel_pt *pt, u64 timestamp) |
| 2096 | { |
| 2097 | unsigned int queue_nr; |
| 2098 | u64 ts; |
| 2099 | int ret; |
| 2100 | |
| 2101 | while (1) { |
| 2102 | struct auxtrace_queue *queue; |
| 2103 | struct intel_pt_queue *ptq; |
| 2104 | |
| 2105 | if (!pt->heap.heap_cnt) |
| 2106 | return 0; |
| 2107 | |
| 2108 | if (pt->heap.heap_array[0].ordinal >= timestamp) |
| 2109 | return 0; |
| 2110 | |
| 2111 | queue_nr = pt->heap.heap_array[0].queue_nr; |
| 2112 | queue = &pt->queues.queue_array[queue_nr]; |
| 2113 | ptq = queue->priv; |
| 2114 | |
| 2115 | intel_pt_log("queue %u processing 0x%" PRIx64 " to 0x%" PRIx64 "\n", |
| 2116 | queue_nr, pt->heap.heap_array[0].ordinal, |
| 2117 | timestamp); |
| 2118 | |
| 2119 | auxtrace_heap__pop(&pt->heap); |
| 2120 | |
| 2121 | if (pt->heap.heap_cnt) { |
| 2122 | ts = pt->heap.heap_array[0].ordinal + 1; |
| 2123 | if (ts > timestamp) |
| 2124 | ts = timestamp; |
| 2125 | } else { |
| 2126 | ts = timestamp; |
| 2127 | } |
| 2128 | |
| 2129 | intel_pt_set_pid_tid_cpu(pt, queue); |
| 2130 | |
| 2131 | ret = intel_pt_run_decoder(ptq, &ts); |
| 2132 | |
| 2133 | if (ret < 0) { |
| 2134 | auxtrace_heap__add(&pt->heap, queue_nr, ts); |
| 2135 | return ret; |
| 2136 | } |
| 2137 | |
| 2138 | if (!ret) { |
| 2139 | ret = auxtrace_heap__add(&pt->heap, queue_nr, ts); |
| 2140 | if (ret < 0) |
| 2141 | return ret; |
| 2142 | } else { |
| 2143 | ptq->on_heap = false; |
| 2144 | } |
| 2145 | } |
| 2146 | |
| 2147 | return 0; |
| 2148 | } |
| 2149 | |
| 2150 | static int intel_pt_process_timeless_queues(struct intel_pt *pt, pid_t tid, |
| 2151 | u64 time_) |
| 2152 | { |
| 2153 | struct auxtrace_queues *queues = &pt->queues; |
| 2154 | unsigned int i; |
| 2155 | u64 ts = 0; |
| 2156 | |
| 2157 | for (i = 0; i < queues->nr_queues; i++) { |
| 2158 | struct auxtrace_queue *queue = &pt->queues.queue_array[i]; |
| 2159 | struct intel_pt_queue *ptq = queue->priv; |
| 2160 | |
| 2161 | if (ptq && (tid == -1 || ptq->tid == tid)) { |
| 2162 | ptq->time = time_; |
| 2163 | intel_pt_set_pid_tid_cpu(pt, queue); |
| 2164 | intel_pt_run_decoder(ptq, &ts); |
| 2165 | } |
| 2166 | } |
| 2167 | return 0; |
| 2168 | } |
| 2169 | |
| 2170 | static int intel_pt_lost(struct intel_pt *pt, struct perf_sample *sample) |
| 2171 | { |
| 2172 | return intel_pt_synth_error(pt, INTEL_PT_ERR_LOST, sample->cpu, |
Adrian Hunter | 16bd432 | 2019-02-06 12:39:47 +0200 | [diff] [blame] | 2173 | sample->pid, sample->tid, 0, sample->time); |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 2174 | } |
| 2175 | |
| 2176 | static struct intel_pt_queue *intel_pt_cpu_to_ptq(struct intel_pt *pt, int cpu) |
| 2177 | { |
| 2178 | unsigned i, j; |
| 2179 | |
| 2180 | if (cpu < 0 || !pt->queues.nr_queues) |
| 2181 | return NULL; |
| 2182 | |
| 2183 | if ((unsigned)cpu >= pt->queues.nr_queues) |
| 2184 | i = pt->queues.nr_queues - 1; |
| 2185 | else |
| 2186 | i = cpu; |
| 2187 | |
| 2188 | if (pt->queues.queue_array[i].cpu == cpu) |
| 2189 | return pt->queues.queue_array[i].priv; |
| 2190 | |
| 2191 | for (j = 0; i > 0; j++) { |
| 2192 | if (pt->queues.queue_array[--i].cpu == cpu) |
| 2193 | return pt->queues.queue_array[i].priv; |
| 2194 | } |
| 2195 | |
| 2196 | for (; j < pt->queues.nr_queues; j++) { |
| 2197 | if (pt->queues.queue_array[j].cpu == cpu) |
| 2198 | return pt->queues.queue_array[j].priv; |
| 2199 | } |
| 2200 | |
| 2201 | return NULL; |
| 2202 | } |
| 2203 | |
Adrian Hunter | 86c2786 | 2015-08-13 12:40:57 +0300 | [diff] [blame] | 2204 | static int intel_pt_sync_switch(struct intel_pt *pt, int cpu, pid_t tid, |
| 2205 | u64 timestamp) |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 2206 | { |
| 2207 | struct intel_pt_queue *ptq; |
Adrian Hunter | 86c2786 | 2015-08-13 12:40:57 +0300 | [diff] [blame] | 2208 | int err; |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 2209 | |
| 2210 | if (!pt->sync_switch) |
Adrian Hunter | 86c2786 | 2015-08-13 12:40:57 +0300 | [diff] [blame] | 2211 | return 1; |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 2212 | |
| 2213 | ptq = intel_pt_cpu_to_ptq(pt, cpu); |
Adrian Hunter | 63d8e38 | 2018-03-07 16:02:22 +0200 | [diff] [blame] | 2214 | if (!ptq || !ptq->sync_switch) |
Adrian Hunter | 86c2786 | 2015-08-13 12:40:57 +0300 | [diff] [blame] | 2215 | return 1; |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 2216 | |
| 2217 | switch (ptq->switch_state) { |
| 2218 | case INTEL_PT_SS_NOT_TRACING: |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 2219 | break; |
| 2220 | case INTEL_PT_SS_UNKNOWN: |
| 2221 | case INTEL_PT_SS_TRACING: |
| 2222 | ptq->next_tid = tid; |
| 2223 | ptq->switch_state = INTEL_PT_SS_EXPECTING_SWITCH_IP; |
| 2224 | return 0; |
| 2225 | case INTEL_PT_SS_EXPECTING_SWITCH_EVENT: |
| 2226 | if (!ptq->on_heap) { |
Adrian Hunter | 86c2786 | 2015-08-13 12:40:57 +0300 | [diff] [blame] | 2227 | ptq->timestamp = perf_time_to_tsc(timestamp, |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 2228 | &pt->tc); |
| 2229 | err = auxtrace_heap__add(&pt->heap, ptq->queue_nr, |
| 2230 | ptq->timestamp); |
| 2231 | if (err) |
| 2232 | return err; |
| 2233 | ptq->on_heap = true; |
| 2234 | } |
| 2235 | ptq->switch_state = INTEL_PT_SS_TRACING; |
| 2236 | break; |
| 2237 | case INTEL_PT_SS_EXPECTING_SWITCH_IP: |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 2238 | intel_pt_log("ERROR: cpu %d expecting switch ip\n", cpu); |
| 2239 | break; |
| 2240 | default: |
| 2241 | break; |
| 2242 | } |
Adrian Hunter | 86c2786 | 2015-08-13 12:40:57 +0300 | [diff] [blame] | 2243 | |
Adrian Hunter | 14f1cfd | 2019-04-12 14:38:30 +0300 | [diff] [blame] | 2244 | ptq->next_tid = -1; |
| 2245 | |
Adrian Hunter | 86c2786 | 2015-08-13 12:40:57 +0300 | [diff] [blame] | 2246 | return 1; |
| 2247 | } |
| 2248 | |
| 2249 | static int intel_pt_process_switch(struct intel_pt *pt, |
| 2250 | struct perf_sample *sample) |
| 2251 | { |
| 2252 | struct perf_evsel *evsel; |
| 2253 | pid_t tid; |
| 2254 | int cpu, ret; |
| 2255 | |
| 2256 | evsel = perf_evlist__id2evsel(pt->session->evlist, sample->id); |
| 2257 | if (evsel != pt->switch_evsel) |
| 2258 | return 0; |
| 2259 | |
| 2260 | tid = perf_evsel__intval(evsel, sample, "next_pid"); |
| 2261 | cpu = sample->cpu; |
| 2262 | |
| 2263 | intel_pt_log("sched_switch: cpu %d tid %d time %"PRIu64" tsc %#"PRIx64"\n", |
| 2264 | cpu, tid, sample->time, perf_time_to_tsc(sample->time, |
| 2265 | &pt->tc)); |
| 2266 | |
| 2267 | ret = intel_pt_sync_switch(pt, cpu, tid, sample->time); |
| 2268 | if (ret <= 0) |
| 2269 | return ret; |
| 2270 | |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 2271 | return machine__set_current_tid(pt->machine, cpu, -1, tid); |
| 2272 | } |
| 2273 | |
Adrian Hunter | c7b4f15f | 2019-04-12 14:38:29 +0300 | [diff] [blame] | 2274 | static int intel_pt_context_switch_in(struct intel_pt *pt, |
| 2275 | struct perf_sample *sample) |
| 2276 | { |
| 2277 | pid_t pid = sample->pid; |
| 2278 | pid_t tid = sample->tid; |
| 2279 | int cpu = sample->cpu; |
| 2280 | |
| 2281 | if (pt->sync_switch) { |
| 2282 | struct intel_pt_queue *ptq; |
| 2283 | |
| 2284 | ptq = intel_pt_cpu_to_ptq(pt, cpu); |
| 2285 | if (ptq && ptq->sync_switch) { |
| 2286 | ptq->next_tid = -1; |
| 2287 | switch (ptq->switch_state) { |
| 2288 | case INTEL_PT_SS_NOT_TRACING: |
| 2289 | case INTEL_PT_SS_UNKNOWN: |
| 2290 | case INTEL_PT_SS_TRACING: |
| 2291 | break; |
| 2292 | case INTEL_PT_SS_EXPECTING_SWITCH_EVENT: |
| 2293 | case INTEL_PT_SS_EXPECTING_SWITCH_IP: |
| 2294 | ptq->switch_state = INTEL_PT_SS_TRACING; |
| 2295 | break; |
| 2296 | default: |
| 2297 | break; |
| 2298 | } |
| 2299 | } |
| 2300 | } |
| 2301 | |
| 2302 | /* |
| 2303 | * If the current tid has not been updated yet, ensure it is now that |
| 2304 | * a "switch in" event has occurred. |
| 2305 | */ |
| 2306 | if (machine__get_current_tid(pt->machine, cpu) == tid) |
| 2307 | return 0; |
| 2308 | |
| 2309 | return machine__set_current_tid(pt->machine, cpu, pid, tid); |
| 2310 | } |
| 2311 | |
Adrian Hunter | 86c2786 | 2015-08-13 12:40:57 +0300 | [diff] [blame] | 2312 | static int intel_pt_context_switch(struct intel_pt *pt, union perf_event *event, |
| 2313 | struct perf_sample *sample) |
| 2314 | { |
| 2315 | bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT; |
| 2316 | pid_t pid, tid; |
| 2317 | int cpu, ret; |
| 2318 | |
| 2319 | cpu = sample->cpu; |
| 2320 | |
| 2321 | if (pt->have_sched_switch == 3) { |
| 2322 | if (!out) |
Adrian Hunter | c7b4f15f | 2019-04-12 14:38:29 +0300 | [diff] [blame] | 2323 | return intel_pt_context_switch_in(pt, sample); |
Adrian Hunter | 86c2786 | 2015-08-13 12:40:57 +0300 | [diff] [blame] | 2324 | if (event->header.type != PERF_RECORD_SWITCH_CPU_WIDE) { |
| 2325 | pr_err("Expecting CPU-wide context switch event\n"); |
| 2326 | return -EINVAL; |
| 2327 | } |
| 2328 | pid = event->context_switch.next_prev_pid; |
| 2329 | tid = event->context_switch.next_prev_tid; |
| 2330 | } else { |
| 2331 | if (out) |
| 2332 | return 0; |
| 2333 | pid = sample->pid; |
| 2334 | tid = sample->tid; |
| 2335 | } |
| 2336 | |
| 2337 | if (tid == -1) { |
| 2338 | pr_err("context_switch event has no tid\n"); |
| 2339 | return -EINVAL; |
| 2340 | } |
| 2341 | |
| 2342 | intel_pt_log("context_switch: cpu %d pid %d tid %d time %"PRIu64" tsc %#"PRIx64"\n", |
| 2343 | cpu, pid, tid, sample->time, perf_time_to_tsc(sample->time, |
| 2344 | &pt->tc)); |
| 2345 | |
| 2346 | ret = intel_pt_sync_switch(pt, cpu, tid, sample->time); |
| 2347 | if (ret <= 0) |
| 2348 | return ret; |
| 2349 | |
| 2350 | return machine__set_current_tid(pt->machine, cpu, pid, tid); |
| 2351 | } |
| 2352 | |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 2353 | static int intel_pt_process_itrace_start(struct intel_pt *pt, |
| 2354 | union perf_event *event, |
| 2355 | struct perf_sample *sample) |
| 2356 | { |
| 2357 | if (!pt->per_cpu_mmaps) |
| 2358 | return 0; |
| 2359 | |
| 2360 | intel_pt_log("itrace_start: cpu %d pid %d tid %d time %"PRIu64" tsc %#"PRIx64"\n", |
| 2361 | sample->cpu, event->itrace_start.pid, |
| 2362 | event->itrace_start.tid, sample->time, |
| 2363 | perf_time_to_tsc(sample->time, &pt->tc)); |
| 2364 | |
| 2365 | return machine__set_current_tid(pt->machine, sample->cpu, |
| 2366 | event->itrace_start.pid, |
| 2367 | event->itrace_start.tid); |
| 2368 | } |
| 2369 | |
| 2370 | static int intel_pt_process_event(struct perf_session *session, |
| 2371 | union perf_event *event, |
| 2372 | struct perf_sample *sample, |
| 2373 | struct perf_tool *tool) |
| 2374 | { |
| 2375 | struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt, |
| 2376 | auxtrace); |
| 2377 | u64 timestamp; |
| 2378 | int err = 0; |
| 2379 | |
| 2380 | if (dump_trace) |
| 2381 | return 0; |
| 2382 | |
| 2383 | if (!tool->ordered_events) { |
| 2384 | pr_err("Intel Processor Trace requires ordered events\n"); |
| 2385 | return -EINVAL; |
| 2386 | } |
| 2387 | |
Adrian Hunter | 81cd60c | 2015-08-20 11:51:32 +0300 | [diff] [blame] | 2388 | if (sample->time && sample->time != (u64)-1) |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 2389 | timestamp = perf_time_to_tsc(sample->time, &pt->tc); |
| 2390 | else |
| 2391 | timestamp = 0; |
| 2392 | |
| 2393 | if (timestamp || pt->timeless_decoding) { |
| 2394 | err = intel_pt_update_queues(pt); |
| 2395 | if (err) |
| 2396 | return err; |
| 2397 | } |
| 2398 | |
| 2399 | if (pt->timeless_decoding) { |
| 2400 | if (event->header.type == PERF_RECORD_EXIT) { |
| 2401 | err = intel_pt_process_timeless_queues(pt, |
Adrian Hunter | 53ff6bc | 2015-08-18 12:07:05 +0300 | [diff] [blame] | 2402 | event->fork.tid, |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 2403 | sample->time); |
| 2404 | } |
| 2405 | } else if (timestamp) { |
| 2406 | err = intel_pt_process_queues(pt, timestamp); |
| 2407 | } |
| 2408 | if (err) |
| 2409 | return err; |
| 2410 | |
| 2411 | if (event->header.type == PERF_RECORD_AUX && |
| 2412 | (event->aux.flags & PERF_AUX_FLAG_TRUNCATED) && |
| 2413 | pt->synth_opts.errors) { |
| 2414 | err = intel_pt_lost(pt, sample); |
| 2415 | if (err) |
| 2416 | return err; |
| 2417 | } |
| 2418 | |
| 2419 | if (pt->switch_evsel && event->header.type == PERF_RECORD_SAMPLE) |
| 2420 | err = intel_pt_process_switch(pt, sample); |
| 2421 | else if (event->header.type == PERF_RECORD_ITRACE_START) |
| 2422 | err = intel_pt_process_itrace_start(pt, event, sample); |
Adrian Hunter | 86c2786 | 2015-08-13 12:40:57 +0300 | [diff] [blame] | 2423 | else if (event->header.type == PERF_RECORD_SWITCH || |
| 2424 | event->header.type == PERF_RECORD_SWITCH_CPU_WIDE) |
| 2425 | err = intel_pt_context_switch(pt, event, sample); |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 2426 | |
Adrian Hunter | 93f8be2 | 2018-11-05 09:35:04 +0200 | [diff] [blame] | 2427 | intel_pt_log("event %u: cpu %d time %"PRIu64" tsc %#"PRIx64" ", |
| 2428 | event->header.type, sample->cpu, sample->time, timestamp); |
| 2429 | intel_pt_log_event(event); |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 2430 | |
| 2431 | return err; |
| 2432 | } |
| 2433 | |
| 2434 | static int intel_pt_flush(struct perf_session *session, struct perf_tool *tool) |
| 2435 | { |
| 2436 | struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt, |
| 2437 | auxtrace); |
| 2438 | int ret; |
| 2439 | |
| 2440 | if (dump_trace) |
| 2441 | return 0; |
| 2442 | |
| 2443 | if (!tool->ordered_events) |
| 2444 | return -EINVAL; |
| 2445 | |
| 2446 | ret = intel_pt_update_queues(pt); |
| 2447 | if (ret < 0) |
| 2448 | return ret; |
| 2449 | |
| 2450 | if (pt->timeless_decoding) |
| 2451 | return intel_pt_process_timeless_queues(pt, -1, |
| 2452 | MAX_TIMESTAMP - 1); |
| 2453 | |
| 2454 | return intel_pt_process_queues(pt, MAX_TIMESTAMP); |
| 2455 | } |
| 2456 | |
| 2457 | static void intel_pt_free_events(struct perf_session *session) |
| 2458 | { |
| 2459 | struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt, |
| 2460 | auxtrace); |
| 2461 | struct auxtrace_queues *queues = &pt->queues; |
| 2462 | unsigned int i; |
| 2463 | |
| 2464 | for (i = 0; i < queues->nr_queues; i++) { |
| 2465 | intel_pt_free_queue(queues->queue_array[i].priv); |
| 2466 | queues->queue_array[i].priv = NULL; |
| 2467 | } |
| 2468 | intel_pt_log_disable(); |
| 2469 | auxtrace_queues__free(queues); |
| 2470 | } |
| 2471 | |
| 2472 | static void intel_pt_free(struct perf_session *session) |
| 2473 | { |
| 2474 | struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt, |
| 2475 | auxtrace); |
| 2476 | |
| 2477 | auxtrace_heap__free(&pt->heap); |
| 2478 | intel_pt_free_events(session); |
| 2479 | session->auxtrace = NULL; |
Arnaldo Carvalho de Melo | abd8286 | 2015-12-11 19:11:23 -0300 | [diff] [blame] | 2480 | thread__put(pt->unknown_thread); |
Adrian Hunter | 2acee10 | 2016-09-23 17:38:48 +0300 | [diff] [blame] | 2481 | addr_filters__exit(&pt->filts); |
Adrian Hunter | 2b9e32c | 2016-09-23 17:38:46 +0300 | [diff] [blame] | 2482 | zfree(&pt->filter); |
Adrian Hunter | 2c47db9 | 2019-06-04 16:00:09 +0300 | [diff] [blame] | 2483 | zfree(&pt->time_ranges); |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 2484 | free(pt); |
| 2485 | } |
| 2486 | |
| 2487 | static int intel_pt_process_auxtrace_event(struct perf_session *session, |
| 2488 | union perf_event *event, |
| 2489 | struct perf_tool *tool __maybe_unused) |
| 2490 | { |
| 2491 | struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt, |
| 2492 | auxtrace); |
| 2493 | |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 2494 | if (!pt->data_queued) { |
| 2495 | struct auxtrace_buffer *buffer; |
| 2496 | off_t data_offset; |
Jiri Olsa | 8ceb41d | 2017-01-23 22:07:59 +0100 | [diff] [blame] | 2497 | int fd = perf_data__fd(session->data); |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 2498 | int err; |
| 2499 | |
Jiri Olsa | 8ceb41d | 2017-01-23 22:07:59 +0100 | [diff] [blame] | 2500 | if (perf_data__is_pipe(session->data)) { |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 2501 | data_offset = 0; |
| 2502 | } else { |
| 2503 | data_offset = lseek(fd, 0, SEEK_CUR); |
| 2504 | if (data_offset == -1) |
| 2505 | return -errno; |
| 2506 | } |
| 2507 | |
| 2508 | err = auxtrace_queues__add_event(&pt->queues, session, event, |
| 2509 | data_offset, &buffer); |
| 2510 | if (err) |
| 2511 | return err; |
| 2512 | |
| 2513 | /* Dump here now we have copied a piped trace out of the pipe */ |
| 2514 | if (dump_trace) { |
| 2515 | if (auxtrace_buffer__get_data(buffer, fd)) { |
| 2516 | intel_pt_dump_event(pt, buffer->data, |
| 2517 | buffer->size); |
| 2518 | auxtrace_buffer__put_data(buffer); |
| 2519 | } |
| 2520 | } |
| 2521 | } |
| 2522 | |
| 2523 | return 0; |
| 2524 | } |
| 2525 | |
| 2526 | struct intel_pt_synth { |
| 2527 | struct perf_tool dummy_tool; |
| 2528 | struct perf_session *session; |
| 2529 | }; |
| 2530 | |
| 2531 | static int intel_pt_event_synth(struct perf_tool *tool, |
| 2532 | union perf_event *event, |
| 2533 | struct perf_sample *sample __maybe_unused, |
| 2534 | struct machine *machine __maybe_unused) |
| 2535 | { |
| 2536 | struct intel_pt_synth *intel_pt_synth = |
| 2537 | container_of(tool, struct intel_pt_synth, dummy_tool); |
| 2538 | |
| 2539 | return perf_session__deliver_synth_event(intel_pt_synth->session, event, |
| 2540 | NULL); |
| 2541 | } |
| 2542 | |
Adrian Hunter | 63a22cd | 2017-05-26 11:17:31 +0300 | [diff] [blame] | 2543 | static int intel_pt_synth_event(struct perf_session *session, const char *name, |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 2544 | struct perf_event_attr *attr, u64 id) |
| 2545 | { |
| 2546 | struct intel_pt_synth intel_pt_synth; |
Adrian Hunter | 63a22cd | 2017-05-26 11:17:31 +0300 | [diff] [blame] | 2547 | int err; |
| 2548 | |
| 2549 | pr_debug("Synthesizing '%s' event with id %" PRIu64 " sample type %#" PRIx64 "\n", |
| 2550 | name, id, (u64)attr->sample_type); |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 2551 | |
| 2552 | memset(&intel_pt_synth, 0, sizeof(struct intel_pt_synth)); |
| 2553 | intel_pt_synth.session = session; |
| 2554 | |
Adrian Hunter | 63a22cd | 2017-05-26 11:17:31 +0300 | [diff] [blame] | 2555 | err = perf_event__synthesize_attr(&intel_pt_synth.dummy_tool, attr, 1, |
| 2556 | &id, intel_pt_event_synth); |
| 2557 | if (err) |
| 2558 | pr_err("%s: failed to synthesize '%s' event type\n", |
| 2559 | __func__, name); |
| 2560 | |
| 2561 | return err; |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 2562 | } |
| 2563 | |
Adrian Hunter | bbac88ed | 2017-05-26 11:17:32 +0300 | [diff] [blame] | 2564 | static void intel_pt_set_event_name(struct perf_evlist *evlist, u64 id, |
| 2565 | const char *name) |
| 2566 | { |
| 2567 | struct perf_evsel *evsel; |
| 2568 | |
| 2569 | evlist__for_each_entry(evlist, evsel) { |
| 2570 | if (evsel->id && evsel->id[0] == id) { |
| 2571 | if (evsel->name) |
| 2572 | zfree(&evsel->name); |
| 2573 | evsel->name = strdup(name); |
| 2574 | break; |
| 2575 | } |
| 2576 | } |
| 2577 | } |
| 2578 | |
Adrian Hunter | 85a564d | 2017-05-26 11:17:30 +0300 | [diff] [blame] | 2579 | static struct perf_evsel *intel_pt_evsel(struct intel_pt *pt, |
| 2580 | struct perf_evlist *evlist) |
| 2581 | { |
| 2582 | struct perf_evsel *evsel; |
| 2583 | |
| 2584 | evlist__for_each_entry(evlist, evsel) { |
| 2585 | if (evsel->attr.type == pt->pmu_type && evsel->ids) |
| 2586 | return evsel; |
| 2587 | } |
| 2588 | |
| 2589 | return NULL; |
| 2590 | } |
| 2591 | |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 2592 | static int intel_pt_synth_events(struct intel_pt *pt, |
| 2593 | struct perf_session *session) |
| 2594 | { |
| 2595 | struct perf_evlist *evlist = session->evlist; |
Adrian Hunter | 85a564d | 2017-05-26 11:17:30 +0300 | [diff] [blame] | 2596 | struct perf_evsel *evsel = intel_pt_evsel(pt, evlist); |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 2597 | struct perf_event_attr attr; |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 2598 | u64 id; |
| 2599 | int err; |
| 2600 | |
Adrian Hunter | 85a564d | 2017-05-26 11:17:30 +0300 | [diff] [blame] | 2601 | if (!evsel) { |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 2602 | pr_debug("There are no selected events with Intel Processor Trace data\n"); |
| 2603 | return 0; |
| 2604 | } |
| 2605 | |
| 2606 | memset(&attr, 0, sizeof(struct perf_event_attr)); |
| 2607 | attr.size = sizeof(struct perf_event_attr); |
| 2608 | attr.type = PERF_TYPE_HARDWARE; |
| 2609 | attr.sample_type = evsel->attr.sample_type & PERF_SAMPLE_MASK; |
| 2610 | attr.sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID | |
| 2611 | PERF_SAMPLE_PERIOD; |
| 2612 | if (pt->timeless_decoding) |
| 2613 | attr.sample_type &= ~(u64)PERF_SAMPLE_TIME; |
| 2614 | else |
| 2615 | attr.sample_type |= PERF_SAMPLE_TIME; |
| 2616 | if (!pt->per_cpu_mmaps) |
| 2617 | attr.sample_type &= ~(u64)PERF_SAMPLE_CPU; |
| 2618 | attr.exclude_user = evsel->attr.exclude_user; |
| 2619 | attr.exclude_kernel = evsel->attr.exclude_kernel; |
| 2620 | attr.exclude_hv = evsel->attr.exclude_hv; |
| 2621 | attr.exclude_host = evsel->attr.exclude_host; |
| 2622 | attr.exclude_guest = evsel->attr.exclude_guest; |
| 2623 | attr.sample_id_all = evsel->attr.sample_id_all; |
| 2624 | attr.read_format = evsel->attr.read_format; |
| 2625 | |
| 2626 | id = evsel->id[0] + 1000000000; |
| 2627 | if (!id) |
| 2628 | id = 1; |
| 2629 | |
Adrian Hunter | 4a9fd4e | 2017-05-26 11:17:33 +0300 | [diff] [blame] | 2630 | if (pt->synth_opts.branches) { |
| 2631 | attr.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS; |
| 2632 | attr.sample_period = 1; |
| 2633 | attr.sample_type |= PERF_SAMPLE_ADDR; |
| 2634 | err = intel_pt_synth_event(session, "branches", &attr, id); |
| 2635 | if (err) |
| 2636 | return err; |
| 2637 | pt->sample_branches = true; |
| 2638 | pt->branches_sample_type = attr.sample_type; |
| 2639 | pt->branches_id = id; |
| 2640 | id += 1; |
| 2641 | attr.sample_type &= ~(u64)PERF_SAMPLE_ADDR; |
| 2642 | } |
| 2643 | |
| 2644 | if (pt->synth_opts.callchain) |
| 2645 | attr.sample_type |= PERF_SAMPLE_CALLCHAIN; |
| 2646 | if (pt->synth_opts.last_branch) |
| 2647 | attr.sample_type |= PERF_SAMPLE_BRANCH_STACK; |
| 2648 | |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 2649 | if (pt->synth_opts.instructions) { |
| 2650 | attr.config = PERF_COUNT_HW_INSTRUCTIONS; |
| 2651 | if (pt->synth_opts.period_type == PERF_ITRACE_PERIOD_NANOSECS) |
| 2652 | attr.sample_period = |
| 2653 | intel_pt_ns_to_ticks(pt, pt->synth_opts.period); |
| 2654 | else |
| 2655 | attr.sample_period = pt->synth_opts.period; |
Adrian Hunter | 63a22cd | 2017-05-26 11:17:31 +0300 | [diff] [blame] | 2656 | err = intel_pt_synth_event(session, "instructions", &attr, id); |
| 2657 | if (err) |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 2658 | return err; |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 2659 | pt->sample_instructions = true; |
| 2660 | pt->instructions_sample_type = attr.sample_type; |
| 2661 | pt->instructions_id = id; |
| 2662 | id += 1; |
| 2663 | } |
| 2664 | |
Adrian Hunter | 4a9fd4e | 2017-05-26 11:17:33 +0300 | [diff] [blame] | 2665 | attr.sample_type &= ~(u64)PERF_SAMPLE_PERIOD; |
| 2666 | attr.sample_period = 1; |
| 2667 | |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 2668 | if (pt->synth_opts.transactions) { |
| 2669 | attr.config = PERF_COUNT_HW_INSTRUCTIONS; |
Adrian Hunter | 63a22cd | 2017-05-26 11:17:31 +0300 | [diff] [blame] | 2670 | err = intel_pt_synth_event(session, "transactions", &attr, id); |
| 2671 | if (err) |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 2672 | return err; |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 2673 | pt->sample_transactions = true; |
Adrian Hunter | 2116074 | 2017-05-26 11:17:18 +0300 | [diff] [blame] | 2674 | pt->transactions_sample_type = attr.sample_type; |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 2675 | pt->transactions_id = id; |
Adrian Hunter | bbac88ed | 2017-05-26 11:17:32 +0300 | [diff] [blame] | 2676 | intel_pt_set_event_name(evlist, id, "transactions"); |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 2677 | id += 1; |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 2678 | } |
| 2679 | |
Adrian Hunter | 3797307 | 2017-06-30 11:36:45 +0300 | [diff] [blame] | 2680 | attr.type = PERF_TYPE_SYNTH; |
| 2681 | attr.sample_type |= PERF_SAMPLE_RAW; |
| 2682 | |
| 2683 | if (pt->synth_opts.ptwrites) { |
| 2684 | attr.config = PERF_SYNTH_INTEL_PTWRITE; |
| 2685 | err = intel_pt_synth_event(session, "ptwrite", &attr, id); |
| 2686 | if (err) |
| 2687 | return err; |
| 2688 | pt->sample_ptwrites = true; |
| 2689 | pt->ptwrites_sample_type = attr.sample_type; |
| 2690 | pt->ptwrites_id = id; |
| 2691 | intel_pt_set_event_name(evlist, id, "ptwrite"); |
| 2692 | id += 1; |
| 2693 | } |
| 2694 | |
| 2695 | if (pt->synth_opts.pwr_events) { |
| 2696 | pt->sample_pwr_events = true; |
| 2697 | pt->pwr_events_sample_type = attr.sample_type; |
| 2698 | |
| 2699 | attr.config = PERF_SYNTH_INTEL_CBR; |
| 2700 | err = intel_pt_synth_event(session, "cbr", &attr, id); |
| 2701 | if (err) |
| 2702 | return err; |
| 2703 | pt->cbr_id = id; |
| 2704 | intel_pt_set_event_name(evlist, id, "cbr"); |
| 2705 | id += 1; |
| 2706 | } |
| 2707 | |
| 2708 | if (pt->synth_opts.pwr_events && (evsel->attr.config & 0x10)) { |
| 2709 | attr.config = PERF_SYNTH_INTEL_MWAIT; |
| 2710 | err = intel_pt_synth_event(session, "mwait", &attr, id); |
| 2711 | if (err) |
| 2712 | return err; |
| 2713 | pt->mwait_id = id; |
| 2714 | intel_pt_set_event_name(evlist, id, "mwait"); |
| 2715 | id += 1; |
| 2716 | |
| 2717 | attr.config = PERF_SYNTH_INTEL_PWRE; |
| 2718 | err = intel_pt_synth_event(session, "pwre", &attr, id); |
| 2719 | if (err) |
| 2720 | return err; |
| 2721 | pt->pwre_id = id; |
| 2722 | intel_pt_set_event_name(evlist, id, "pwre"); |
| 2723 | id += 1; |
| 2724 | |
| 2725 | attr.config = PERF_SYNTH_INTEL_EXSTOP; |
| 2726 | err = intel_pt_synth_event(session, "exstop", &attr, id); |
| 2727 | if (err) |
| 2728 | return err; |
| 2729 | pt->exstop_id = id; |
| 2730 | intel_pt_set_event_name(evlist, id, "exstop"); |
| 2731 | id += 1; |
| 2732 | |
| 2733 | attr.config = PERF_SYNTH_INTEL_PWRX; |
| 2734 | err = intel_pt_synth_event(session, "pwrx", &attr, id); |
| 2735 | if (err) |
| 2736 | return err; |
| 2737 | pt->pwrx_id = id; |
| 2738 | intel_pt_set_event_name(evlist, id, "pwrx"); |
| 2739 | id += 1; |
| 2740 | } |
| 2741 | |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 2742 | return 0; |
| 2743 | } |
| 2744 | |
| 2745 | static struct perf_evsel *intel_pt_find_sched_switch(struct perf_evlist *evlist) |
| 2746 | { |
| 2747 | struct perf_evsel *evsel; |
| 2748 | |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 2749 | evlist__for_each_entry_reverse(evlist, evsel) { |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 2750 | const char *name = perf_evsel__name(evsel); |
| 2751 | |
| 2752 | if (!strcmp(name, "sched:sched_switch")) |
| 2753 | return evsel; |
| 2754 | } |
| 2755 | |
| 2756 | return NULL; |
| 2757 | } |
| 2758 | |
Adrian Hunter | 86c2786 | 2015-08-13 12:40:57 +0300 | [diff] [blame] | 2759 | static bool intel_pt_find_switch(struct perf_evlist *evlist) |
| 2760 | { |
| 2761 | struct perf_evsel *evsel; |
| 2762 | |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 2763 | evlist__for_each_entry(evlist, evsel) { |
Adrian Hunter | 86c2786 | 2015-08-13 12:40:57 +0300 | [diff] [blame] | 2764 | if (evsel->attr.context_switch) |
| 2765 | return true; |
| 2766 | } |
| 2767 | |
| 2768 | return false; |
| 2769 | } |
| 2770 | |
Adrian Hunter | ba11ba6 | 2015-09-25 16:15:56 +0300 | [diff] [blame] | 2771 | static int intel_pt_perf_config(const char *var, const char *value, void *data) |
| 2772 | { |
| 2773 | struct intel_pt *pt = data; |
| 2774 | |
| 2775 | if (!strcmp(var, "intel-pt.mispred-all")) |
| 2776 | pt->mispred_all = perf_config_bool(var, value); |
| 2777 | |
| 2778 | return 0; |
| 2779 | } |
| 2780 | |
Adrian Hunter | 2c47db9 | 2019-06-04 16:00:09 +0300 | [diff] [blame] | 2781 | /* Find least TSC which converts to ns or later */ |
| 2782 | static u64 intel_pt_tsc_start(u64 ns, struct intel_pt *pt) |
| 2783 | { |
| 2784 | u64 tsc, tm; |
| 2785 | |
| 2786 | tsc = perf_time_to_tsc(ns, &pt->tc); |
| 2787 | |
| 2788 | while (1) { |
| 2789 | tm = tsc_to_perf_time(tsc, &pt->tc); |
| 2790 | if (tm < ns) |
| 2791 | break; |
| 2792 | tsc -= 1; |
| 2793 | } |
| 2794 | |
| 2795 | while (tm < ns) |
| 2796 | tm = tsc_to_perf_time(++tsc, &pt->tc); |
| 2797 | |
| 2798 | return tsc; |
| 2799 | } |
| 2800 | |
| 2801 | /* Find greatest TSC which converts to ns or earlier */ |
| 2802 | static u64 intel_pt_tsc_end(u64 ns, struct intel_pt *pt) |
| 2803 | { |
| 2804 | u64 tsc, tm; |
| 2805 | |
| 2806 | tsc = perf_time_to_tsc(ns, &pt->tc); |
| 2807 | |
| 2808 | while (1) { |
| 2809 | tm = tsc_to_perf_time(tsc, &pt->tc); |
| 2810 | if (tm > ns) |
| 2811 | break; |
| 2812 | tsc += 1; |
| 2813 | } |
| 2814 | |
| 2815 | while (tm > ns) |
| 2816 | tm = tsc_to_perf_time(--tsc, &pt->tc); |
| 2817 | |
| 2818 | return tsc; |
| 2819 | } |
| 2820 | |
| 2821 | static int intel_pt_setup_time_ranges(struct intel_pt *pt, |
| 2822 | struct itrace_synth_opts *opts) |
| 2823 | { |
| 2824 | struct perf_time_interval *p = opts->ptime_range; |
| 2825 | int n = opts->range_num; |
| 2826 | int i; |
| 2827 | |
| 2828 | if (!n || !p || pt->timeless_decoding) |
| 2829 | return 0; |
| 2830 | |
| 2831 | pt->time_ranges = calloc(n, sizeof(struct range)); |
| 2832 | if (!pt->time_ranges) |
| 2833 | return -ENOMEM; |
| 2834 | |
| 2835 | pt->range_cnt = n; |
| 2836 | |
| 2837 | intel_pt_log("%s: %u range(s)\n", __func__, n); |
| 2838 | |
| 2839 | for (i = 0; i < n; i++) { |
| 2840 | struct range *r = &pt->time_ranges[i]; |
| 2841 | u64 ts = p[i].start; |
| 2842 | u64 te = p[i].end; |
| 2843 | |
| 2844 | /* |
| 2845 | * Take care to ensure the TSC range matches the perf-time range |
| 2846 | * when converted back to perf-time. |
| 2847 | */ |
| 2848 | r->start = ts ? intel_pt_tsc_start(ts, pt) : 0; |
| 2849 | r->end = te ? intel_pt_tsc_end(te, pt) : 0; |
| 2850 | |
| 2851 | intel_pt_log("range %d: perf time interval: %"PRIu64" to %"PRIu64"\n", |
| 2852 | i, ts, te); |
| 2853 | intel_pt_log("range %d: TSC time interval: %#"PRIx64" to %#"PRIx64"\n", |
| 2854 | i, r->start, r->end); |
| 2855 | } |
| 2856 | |
| 2857 | return 0; |
| 2858 | } |
| 2859 | |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 2860 | static const char * const intel_pt_info_fmts[] = { |
Adrian Hunter | 11fa7cb | 2015-07-17 19:33:54 +0300 | [diff] [blame] | 2861 | [INTEL_PT_PMU_TYPE] = " PMU Type %"PRId64"\n", |
| 2862 | [INTEL_PT_TIME_SHIFT] = " Time Shift %"PRIu64"\n", |
| 2863 | [INTEL_PT_TIME_MULT] = " Time Muliplier %"PRIu64"\n", |
| 2864 | [INTEL_PT_TIME_ZERO] = " Time Zero %"PRIu64"\n", |
| 2865 | [INTEL_PT_CAP_USER_TIME_ZERO] = " Cap Time Zero %"PRId64"\n", |
| 2866 | [INTEL_PT_TSC_BIT] = " TSC bit %#"PRIx64"\n", |
| 2867 | [INTEL_PT_NORETCOMP_BIT] = " NoRETComp bit %#"PRIx64"\n", |
| 2868 | [INTEL_PT_HAVE_SCHED_SWITCH] = " Have sched_switch %"PRId64"\n", |
| 2869 | [INTEL_PT_SNAPSHOT_MODE] = " Snapshot mode %"PRId64"\n", |
| 2870 | [INTEL_PT_PER_CPU_MMAPS] = " Per-cpu maps %"PRId64"\n", |
| 2871 | [INTEL_PT_MTC_BIT] = " MTC bit %#"PRIx64"\n", |
| 2872 | [INTEL_PT_TSC_CTC_N] = " TSC:CTC numerator %"PRIu64"\n", |
| 2873 | [INTEL_PT_TSC_CTC_D] = " TSC:CTC denominator %"PRIu64"\n", |
| 2874 | [INTEL_PT_CYC_BIT] = " CYC bit %#"PRIx64"\n", |
Adrian Hunter | fa8025c | 2016-09-23 17:38:42 +0300 | [diff] [blame] | 2875 | [INTEL_PT_MAX_NONTURBO_RATIO] = " Max non-turbo ratio %"PRIu64"\n", |
Adrian Hunter | 2b9e32c | 2016-09-23 17:38:46 +0300 | [diff] [blame] | 2876 | [INTEL_PT_FILTER_STR_LEN] = " Filter string len. %"PRIu64"\n", |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 2877 | }; |
| 2878 | |
| 2879 | static void intel_pt_print_info(u64 *arr, int start, int finish) |
| 2880 | { |
| 2881 | int i; |
| 2882 | |
| 2883 | if (!dump_trace) |
| 2884 | return; |
| 2885 | |
| 2886 | for (i = start; i <= finish; i++) |
| 2887 | fprintf(stdout, intel_pt_info_fmts[i], arr[i]); |
| 2888 | } |
| 2889 | |
Adrian Hunter | 2b9e32c | 2016-09-23 17:38:46 +0300 | [diff] [blame] | 2890 | static void intel_pt_print_info_str(const char *name, const char *str) |
| 2891 | { |
| 2892 | if (!dump_trace) |
| 2893 | return; |
| 2894 | |
| 2895 | fprintf(stdout, " %-20s%s\n", name, str ? str : ""); |
| 2896 | } |
| 2897 | |
Adrian Hunter | 40b746a | 2016-09-23 17:38:44 +0300 | [diff] [blame] | 2898 | static bool intel_pt_has(struct auxtrace_info_event *auxtrace_info, int pos) |
| 2899 | { |
| 2900 | return auxtrace_info->header.size >= |
| 2901 | sizeof(struct auxtrace_info_event) + (sizeof(u64) * (pos + 1)); |
| 2902 | } |
| 2903 | |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 2904 | int intel_pt_process_auxtrace_info(union perf_event *event, |
| 2905 | struct perf_session *session) |
| 2906 | { |
| 2907 | struct auxtrace_info_event *auxtrace_info = &event->auxtrace_info; |
| 2908 | size_t min_sz = sizeof(u64) * INTEL_PT_PER_CPU_MMAPS; |
| 2909 | struct intel_pt *pt; |
Adrian Hunter | 2b9e32c | 2016-09-23 17:38:46 +0300 | [diff] [blame] | 2910 | void *info_end; |
| 2911 | u64 *info; |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 2912 | int err; |
| 2913 | |
| 2914 | if (auxtrace_info->header.size < sizeof(struct auxtrace_info_event) + |
| 2915 | min_sz) |
| 2916 | return -EINVAL; |
| 2917 | |
| 2918 | pt = zalloc(sizeof(struct intel_pt)); |
| 2919 | if (!pt) |
| 2920 | return -ENOMEM; |
| 2921 | |
Adrian Hunter | 2acee10 | 2016-09-23 17:38:48 +0300 | [diff] [blame] | 2922 | addr_filters__init(&pt->filts); |
| 2923 | |
Arnaldo Carvalho de Melo | ecc4c56 | 2017-01-24 13:44:10 -0300 | [diff] [blame] | 2924 | err = perf_config(intel_pt_perf_config, pt); |
| 2925 | if (err) |
| 2926 | goto err_free; |
Adrian Hunter | ba11ba6 | 2015-09-25 16:15:56 +0300 | [diff] [blame] | 2927 | |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 2928 | err = auxtrace_queues__init(&pt->queues); |
| 2929 | if (err) |
| 2930 | goto err_free; |
| 2931 | |
| 2932 | intel_pt_log_set_name(INTEL_PT_PMU_NAME); |
| 2933 | |
| 2934 | pt->session = session; |
| 2935 | pt->machine = &session->machines.host; /* No kvm support */ |
| 2936 | pt->auxtrace_type = auxtrace_info->type; |
| 2937 | pt->pmu_type = auxtrace_info->priv[INTEL_PT_PMU_TYPE]; |
| 2938 | pt->tc.time_shift = auxtrace_info->priv[INTEL_PT_TIME_SHIFT]; |
| 2939 | pt->tc.time_mult = auxtrace_info->priv[INTEL_PT_TIME_MULT]; |
| 2940 | pt->tc.time_zero = auxtrace_info->priv[INTEL_PT_TIME_ZERO]; |
| 2941 | pt->cap_user_time_zero = auxtrace_info->priv[INTEL_PT_CAP_USER_TIME_ZERO]; |
| 2942 | pt->tsc_bit = auxtrace_info->priv[INTEL_PT_TSC_BIT]; |
| 2943 | pt->noretcomp_bit = auxtrace_info->priv[INTEL_PT_NORETCOMP_BIT]; |
| 2944 | pt->have_sched_switch = auxtrace_info->priv[INTEL_PT_HAVE_SCHED_SWITCH]; |
| 2945 | pt->snapshot_mode = auxtrace_info->priv[INTEL_PT_SNAPSHOT_MODE]; |
| 2946 | pt->per_cpu_mmaps = auxtrace_info->priv[INTEL_PT_PER_CPU_MMAPS]; |
| 2947 | intel_pt_print_info(&auxtrace_info->priv[0], INTEL_PT_PMU_TYPE, |
| 2948 | INTEL_PT_PER_CPU_MMAPS); |
| 2949 | |
Adrian Hunter | 40b746a | 2016-09-23 17:38:44 +0300 | [diff] [blame] | 2950 | if (intel_pt_has(auxtrace_info, INTEL_PT_CYC_BIT)) { |
Adrian Hunter | 11fa7cb | 2015-07-17 19:33:54 +0300 | [diff] [blame] | 2951 | pt->mtc_bit = auxtrace_info->priv[INTEL_PT_MTC_BIT]; |
| 2952 | pt->mtc_freq_bits = auxtrace_info->priv[INTEL_PT_MTC_FREQ_BITS]; |
| 2953 | pt->tsc_ctc_ratio_n = auxtrace_info->priv[INTEL_PT_TSC_CTC_N]; |
| 2954 | pt->tsc_ctc_ratio_d = auxtrace_info->priv[INTEL_PT_TSC_CTC_D]; |
| 2955 | pt->cyc_bit = auxtrace_info->priv[INTEL_PT_CYC_BIT]; |
| 2956 | intel_pt_print_info(&auxtrace_info->priv[0], INTEL_PT_MTC_BIT, |
| 2957 | INTEL_PT_CYC_BIT); |
| 2958 | } |
| 2959 | |
Adrian Hunter | 40b746a | 2016-09-23 17:38:44 +0300 | [diff] [blame] | 2960 | if (intel_pt_has(auxtrace_info, INTEL_PT_MAX_NONTURBO_RATIO)) { |
Adrian Hunter | fa8025c | 2016-09-23 17:38:42 +0300 | [diff] [blame] | 2961 | pt->max_non_turbo_ratio = |
| 2962 | auxtrace_info->priv[INTEL_PT_MAX_NONTURBO_RATIO]; |
| 2963 | intel_pt_print_info(&auxtrace_info->priv[0], |
| 2964 | INTEL_PT_MAX_NONTURBO_RATIO, |
| 2965 | INTEL_PT_MAX_NONTURBO_RATIO); |
| 2966 | } |
| 2967 | |
Adrian Hunter | 2b9e32c | 2016-09-23 17:38:46 +0300 | [diff] [blame] | 2968 | info = &auxtrace_info->priv[INTEL_PT_FILTER_STR_LEN] + 1; |
| 2969 | info_end = (void *)info + auxtrace_info->header.size; |
| 2970 | |
| 2971 | if (intel_pt_has(auxtrace_info, INTEL_PT_FILTER_STR_LEN)) { |
| 2972 | size_t len; |
| 2973 | |
| 2974 | len = auxtrace_info->priv[INTEL_PT_FILTER_STR_LEN]; |
| 2975 | intel_pt_print_info(&auxtrace_info->priv[0], |
| 2976 | INTEL_PT_FILTER_STR_LEN, |
| 2977 | INTEL_PT_FILTER_STR_LEN); |
| 2978 | if (len) { |
| 2979 | const char *filter = (const char *)info; |
| 2980 | |
| 2981 | len = roundup(len + 1, 8); |
| 2982 | info += len >> 3; |
| 2983 | if ((void *)info > info_end) { |
| 2984 | pr_err("%s: bad filter string length\n", __func__); |
| 2985 | err = -EINVAL; |
| 2986 | goto err_free_queues; |
| 2987 | } |
| 2988 | pt->filter = memdup(filter, len); |
| 2989 | if (!pt->filter) { |
| 2990 | err = -ENOMEM; |
| 2991 | goto err_free_queues; |
| 2992 | } |
| 2993 | if (session->header.needs_swap) |
| 2994 | mem_bswap_64(pt->filter, len); |
| 2995 | if (pt->filter[len - 1]) { |
| 2996 | pr_err("%s: filter string not null terminated\n", __func__); |
| 2997 | err = -EINVAL; |
| 2998 | goto err_free_queues; |
| 2999 | } |
Adrian Hunter | 2acee10 | 2016-09-23 17:38:48 +0300 | [diff] [blame] | 3000 | err = addr_filters__parse_bare_filter(&pt->filts, |
| 3001 | filter); |
| 3002 | if (err) |
| 3003 | goto err_free_queues; |
Adrian Hunter | 2b9e32c | 2016-09-23 17:38:46 +0300 | [diff] [blame] | 3004 | } |
| 3005 | intel_pt_print_info_str("Filter string", pt->filter); |
| 3006 | } |
| 3007 | |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 3008 | pt->timeless_decoding = intel_pt_timeless_decoding(pt); |
Adrian Hunter | 0763338 | 2019-03-01 12:35:36 +0200 | [diff] [blame] | 3009 | if (pt->timeless_decoding && !pt->tc.time_mult) |
| 3010 | pt->tc.time_mult = 1; |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 3011 | pt->have_tsc = intel_pt_have_tsc(pt); |
| 3012 | pt->sampling_mode = false; |
| 3013 | pt->est_tsc = !pt->timeless_decoding; |
| 3014 | |
| 3015 | pt->unknown_thread = thread__new(999999999, 999999999); |
| 3016 | if (!pt->unknown_thread) { |
| 3017 | err = -ENOMEM; |
| 3018 | goto err_free_queues; |
| 3019 | } |
Adrian Hunter | 3a4acda | 2016-02-01 03:21:04 +0000 | [diff] [blame] | 3020 | |
| 3021 | /* |
| 3022 | * Since this thread will not be kept in any rbtree not in a |
| 3023 | * list, initialize its list node so that at thread__put() the |
| 3024 | * current thread lifetime assuption is kept and we don't segfault |
| 3025 | * at list_del_init(). |
| 3026 | */ |
| 3027 | INIT_LIST_HEAD(&pt->unknown_thread->node); |
| 3028 | |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 3029 | err = thread__set_comm(pt->unknown_thread, "unknown", 0); |
| 3030 | if (err) |
| 3031 | goto err_delete_thread; |
| 3032 | if (thread__init_map_groups(pt->unknown_thread, pt->machine)) { |
| 3033 | err = -ENOMEM; |
| 3034 | goto err_delete_thread; |
| 3035 | } |
| 3036 | |
| 3037 | pt->auxtrace.process_event = intel_pt_process_event; |
| 3038 | pt->auxtrace.process_auxtrace_event = intel_pt_process_auxtrace_event; |
| 3039 | pt->auxtrace.flush_events = intel_pt_flush; |
| 3040 | pt->auxtrace.free_events = intel_pt_free_events; |
| 3041 | pt->auxtrace.free = intel_pt_free; |
| 3042 | session->auxtrace = &pt->auxtrace; |
| 3043 | |
| 3044 | if (dump_trace) |
| 3045 | return 0; |
| 3046 | |
| 3047 | if (pt->have_sched_switch == 1) { |
| 3048 | pt->switch_evsel = intel_pt_find_sched_switch(session->evlist); |
| 3049 | if (!pt->switch_evsel) { |
| 3050 | pr_err("%s: missing sched_switch event\n", __func__); |
Adrian Hunter | 4d34e10 | 2016-09-23 17:38:43 +0300 | [diff] [blame] | 3051 | err = -EINVAL; |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 3052 | goto err_delete_thread; |
| 3053 | } |
Adrian Hunter | 86c2786 | 2015-08-13 12:40:57 +0300 | [diff] [blame] | 3054 | } else if (pt->have_sched_switch == 2 && |
| 3055 | !intel_pt_find_switch(session->evlist)) { |
| 3056 | pr_err("%s: missing context_switch attribute flag\n", __func__); |
Adrian Hunter | 4d34e10 | 2016-09-23 17:38:43 +0300 | [diff] [blame] | 3057 | err = -EINVAL; |
Adrian Hunter | 86c2786 | 2015-08-13 12:40:57 +0300 | [diff] [blame] | 3058 | goto err_delete_thread; |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 3059 | } |
| 3060 | |
| 3061 | if (session->itrace_synth_opts && session->itrace_synth_opts->set) { |
| 3062 | pt->synth_opts = *session->itrace_synth_opts; |
| 3063 | } else { |
Andi Kleen | 4eb0681 | 2018-09-20 11:05:37 -0700 | [diff] [blame] | 3064 | itrace_synth_opts__set_default(&pt->synth_opts, |
| 3065 | session->itrace_synth_opts->default_no_sample); |
Adrian Hunter | 26f19c2 | 2019-05-20 14:37:07 +0300 | [diff] [blame] | 3066 | if (!session->itrace_synth_opts->default_no_sample && |
| 3067 | !session->itrace_synth_opts->inject) { |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 3068 | pt->synth_opts.branches = false; |
| 3069 | pt->synth_opts.callchain = true; |
| 3070 | } |
Adrian Hunter | 50f73637 | 2016-06-23 16:40:57 +0300 | [diff] [blame] | 3071 | if (session->itrace_synth_opts) |
| 3072 | pt->synth_opts.thread_stack = |
| 3073 | session->itrace_synth_opts->thread_stack; |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 3074 | } |
| 3075 | |
| 3076 | if (pt->synth_opts.log) |
| 3077 | intel_pt_log_enable(); |
| 3078 | |
| 3079 | /* Maximum non-turbo ratio is TSC freq / 100 MHz */ |
| 3080 | if (pt->tc.time_mult) { |
| 3081 | u64 tsc_freq = intel_pt_ns_to_ticks(pt, 1000000000); |
| 3082 | |
Adrian Hunter | fa8025c | 2016-09-23 17:38:42 +0300 | [diff] [blame] | 3083 | if (!pt->max_non_turbo_ratio) |
| 3084 | pt->max_non_turbo_ratio = |
| 3085 | (tsc_freq + 50000000) / 100000000; |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 3086 | intel_pt_log("TSC frequency %"PRIu64"\n", tsc_freq); |
| 3087 | intel_pt_log("Maximum non-turbo ratio %u\n", |
| 3088 | pt->max_non_turbo_ratio); |
Adrian Hunter | 3797307 | 2017-06-30 11:36:45 +0300 | [diff] [blame] | 3089 | pt->cbr2khz = tsc_freq / pt->max_non_turbo_ratio / 1000; |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 3090 | } |
| 3091 | |
Adrian Hunter | 2c47db9 | 2019-06-04 16:00:09 +0300 | [diff] [blame] | 3092 | if (session->itrace_synth_opts) { |
| 3093 | err = intel_pt_setup_time_ranges(pt, session->itrace_synth_opts); |
| 3094 | if (err) |
| 3095 | goto err_delete_thread; |
| 3096 | } |
| 3097 | |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 3098 | if (pt->synth_opts.calls) |
| 3099 | pt->branches_filter |= PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC | |
| 3100 | PERF_IP_FLAG_TRACE_END; |
| 3101 | if (pt->synth_opts.returns) |
| 3102 | pt->branches_filter |= PERF_IP_FLAG_RETURN | |
| 3103 | PERF_IP_FLAG_TRACE_BEGIN; |
| 3104 | |
| 3105 | if (pt->synth_opts.callchain && !symbol_conf.use_callchain) { |
| 3106 | symbol_conf.use_callchain = true; |
| 3107 | if (callchain_register_param(&callchain_param) < 0) { |
| 3108 | symbol_conf.use_callchain = false; |
| 3109 | pt->synth_opts.callchain = false; |
| 3110 | } |
| 3111 | } |
| 3112 | |
| 3113 | err = intel_pt_synth_events(pt, session); |
| 3114 | if (err) |
| 3115 | goto err_delete_thread; |
| 3116 | |
| 3117 | err = auxtrace_queues__process_index(&pt->queues, session); |
| 3118 | if (err) |
| 3119 | goto err_delete_thread; |
| 3120 | |
| 3121 | if (pt->queues.populated) |
| 3122 | pt->data_queued = true; |
| 3123 | |
| 3124 | if (pt->timeless_decoding) |
| 3125 | pr_debug2("Intel PT decoding without timestamps\n"); |
| 3126 | |
| 3127 | return 0; |
| 3128 | |
| 3129 | err_delete_thread: |
Arnaldo Carvalho de Melo | abd8286 | 2015-12-11 19:11:23 -0300 | [diff] [blame] | 3130 | thread__zput(pt->unknown_thread); |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 3131 | err_free_queues: |
| 3132 | intel_pt_log_disable(); |
| 3133 | auxtrace_queues__free(&pt->queues); |
| 3134 | session->auxtrace = NULL; |
| 3135 | err_free: |
Adrian Hunter | 2acee10 | 2016-09-23 17:38:48 +0300 | [diff] [blame] | 3136 | addr_filters__exit(&pt->filts); |
Adrian Hunter | 2b9e32c | 2016-09-23 17:38:46 +0300 | [diff] [blame] | 3137 | zfree(&pt->filter); |
Adrian Hunter | 2c47db9 | 2019-06-04 16:00:09 +0300 | [diff] [blame] | 3138 | zfree(&pt->time_ranges); |
Adrian Hunter | 90e457f | 2015-07-17 19:33:41 +0300 | [diff] [blame] | 3139 | free(pt); |
| 3140 | return err; |
| 3141 | } |