intel-bts.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * intel-bts.c: Intel Processor Trace support
  4. * Copyright (c) 2013-2015, Intel Corporation.
  5. */
  6. #include <endian.h>
  7. #include <errno.h>
  8. #include <byteswap.h>
  9. #include <inttypes.h>
  10. #include <linux/kernel.h>
  11. #include <linux/types.h>
  12. #include <linux/bitops.h>
  13. #include <linux/log2.h>
  14. #include <linux/zalloc.h>
  15. #include "color.h"
  16. #include "evsel.h"
  17. #include "evlist.h"
  18. #include "machine.h"
  19. #include "symbol.h"
  20. #include "session.h"
  21. #include "tool.h"
  22. #include "thread.h"
  23. #include "thread-stack.h"
  24. #include "debug.h"
  25. #include "tsc.h"
  26. #include "auxtrace.h"
  27. #include "intel-pt-decoder/intel-pt-insn-decoder.h"
  28. #include "intel-bts.h"
  29. #include "util/synthetic-events.h"
  30. #define MAX_TIMESTAMP (~0ULL)
  31. #define INTEL_BTS_ERR_NOINSN 5
  32. #define INTEL_BTS_ERR_LOST 9
  33. #if __BYTE_ORDER == __BIG_ENDIAN
  34. #define le64_to_cpu bswap_64
  35. #else
  36. #define le64_to_cpu
  37. #endif
  38. struct intel_bts {
  39. struct auxtrace auxtrace;
  40. struct auxtrace_queues queues;
  41. struct auxtrace_heap heap;
  42. u32 auxtrace_type;
  43. struct perf_session *session;
  44. struct machine *machine;
  45. bool sampling_mode;
  46. bool snapshot_mode;
  47. bool data_queued;
  48. u32 pmu_type;
  49. struct perf_tsc_conversion tc;
  50. bool cap_user_time_zero;
  51. struct itrace_synth_opts synth_opts;
  52. bool sample_branches;
  53. u32 branches_filter;
  54. u64 branches_sample_type;
  55. u64 branches_id;
  56. size_t branches_event_size;
  57. unsigned long num_events;
  58. };
  59. struct intel_bts_queue {
  60. struct intel_bts *bts;
  61. unsigned int queue_nr;
  62. struct auxtrace_buffer *buffer;
  63. bool on_heap;
  64. bool done;
  65. pid_t pid;
  66. pid_t tid;
  67. int cpu;
  68. u64 time;
  69. struct intel_pt_insn intel_pt_insn;
  70. u32 sample_flags;
  71. };
  72. struct branch {
  73. u64 from;
  74. u64 to;
  75. u64 misc;
  76. };
  77. static void intel_bts_dump(struct intel_bts *bts __maybe_unused,
  78. unsigned char *buf, size_t len)
  79. {
  80. struct branch *branch;
  81. size_t i, pos = 0, br_sz = sizeof(struct branch), sz;
  82. const char *color = PERF_COLOR_BLUE;
  83. color_fprintf(stdout, color,
  84. ". ... Intel BTS data: size %zu bytes\n",
  85. len);
  86. while (len) {
  87. if (len >= br_sz)
  88. sz = br_sz;
  89. else
  90. sz = len;
  91. printf(".");
  92. color_fprintf(stdout, color, " %08x: ", pos);
  93. for (i = 0; i < sz; i++)
  94. color_fprintf(stdout, color, " %02x", buf[i]);
  95. for (; i < br_sz; i++)
  96. color_fprintf(stdout, color, " ");
  97. if (len >= br_sz) {
  98. branch = (struct branch *)buf;
  99. color_fprintf(stdout, color, " %"PRIx64" -> %"PRIx64" %s\n",
  100. le64_to_cpu(branch->from),
  101. le64_to_cpu(branch->to),
  102. le64_to_cpu(branch->misc) & 0x10 ?
  103. "pred" : "miss");
  104. } else {
  105. color_fprintf(stdout, color, " Bad record!\n");
  106. }
  107. pos += sz;
  108. buf += sz;
  109. len -= sz;
  110. }
  111. }
  112. static void intel_bts_dump_event(struct intel_bts *bts, unsigned char *buf,
  113. size_t len)
  114. {
  115. printf(".\n");
  116. intel_bts_dump(bts, buf, len);
  117. }
  118. static int intel_bts_lost(struct intel_bts *bts, struct perf_sample *sample)
  119. {
  120. union perf_event event;
  121. int err;
  122. auxtrace_synth_error(&event.auxtrace_error, PERF_AUXTRACE_ERROR_ITRACE,
  123. INTEL_BTS_ERR_LOST, sample->cpu, sample->pid,
  124. sample->tid, 0, "Lost trace data", sample->time);
  125. err = perf_session__deliver_synth_event(bts->session, &event, NULL);
  126. if (err)
  127. pr_err("Intel BTS: failed to deliver error event, error %d\n",
  128. err);
  129. return err;
  130. }
  131. static struct intel_bts_queue *intel_bts_alloc_queue(struct intel_bts *bts,
  132. unsigned int queue_nr)
  133. {
  134. struct intel_bts_queue *btsq;
  135. btsq = zalloc(sizeof(struct intel_bts_queue));
  136. if (!btsq)
  137. return NULL;
  138. btsq->bts = bts;
  139. btsq->queue_nr = queue_nr;
  140. btsq->pid = -1;
  141. btsq->tid = -1;
  142. btsq->cpu = -1;
  143. return btsq;
  144. }
  145. static int intel_bts_setup_queue(struct intel_bts *bts,
  146. struct auxtrace_queue *queue,
  147. unsigned int queue_nr)
  148. {
  149. struct intel_bts_queue *btsq = queue->priv;
  150. if (list_empty(&queue->head))
  151. return 0;
  152. if (!btsq) {
  153. btsq = intel_bts_alloc_queue(bts, queue_nr);
  154. if (!btsq)
  155. return -ENOMEM;
  156. queue->priv = btsq;
  157. if (queue->cpu != -1)
  158. btsq->cpu = queue->cpu;
  159. btsq->tid = queue->tid;
  160. }
  161. if (bts->sampling_mode)
  162. return 0;
  163. if (!btsq->on_heap && !btsq->buffer) {
  164. int ret;
  165. btsq->buffer = auxtrace_buffer__next(queue, NULL);
  166. if (!btsq->buffer)
  167. return 0;
  168. ret = auxtrace_heap__add(&bts->heap, queue_nr,
  169. btsq->buffer->reference);
  170. if (ret)
  171. return ret;
  172. btsq->on_heap = true;
  173. }
  174. return 0;
  175. }
  176. static int intel_bts_setup_queues(struct intel_bts *bts)
  177. {
  178. unsigned int i;
  179. int ret;
  180. for (i = 0; i < bts->queues.nr_queues; i++) {
  181. ret = intel_bts_setup_queue(bts, &bts->queues.queue_array[i],
  182. i);
  183. if (ret)
  184. return ret;
  185. }
  186. return 0;
  187. }
  188. static inline int intel_bts_update_queues(struct intel_bts *bts)
  189. {
  190. if (bts->queues.new_data) {
  191. bts->queues.new_data = false;
  192. return intel_bts_setup_queues(bts);
  193. }
  194. return 0;
  195. }
  196. static unsigned char *intel_bts_find_overlap(unsigned char *buf_a, size_t len_a,
  197. unsigned char *buf_b, size_t len_b)
  198. {
  199. size_t offs, len;
  200. if (len_a > len_b)
  201. offs = len_a - len_b;
  202. else
  203. offs = 0;
  204. for (; offs < len_a; offs += sizeof(struct branch)) {
  205. len = len_a - offs;
  206. if (!memcmp(buf_a + offs, buf_b, len))
  207. return buf_b + len;
  208. }
  209. return buf_b;
  210. }
  211. static int intel_bts_do_fix_overlap(struct auxtrace_queue *queue,
  212. struct auxtrace_buffer *b)
  213. {
  214. struct auxtrace_buffer *a;
  215. void *start;
  216. if (b->list.prev == &queue->head)
  217. return 0;
  218. a = list_entry(b->list.prev, struct auxtrace_buffer, list);
  219. start = intel_bts_find_overlap(a->data, a->size, b->data, b->size);
  220. if (!start)
  221. return -EINVAL;
  222. b->use_size = b->data + b->size - start;
  223. b->use_data = start;
  224. return 0;
  225. }
  226. static inline u8 intel_bts_cpumode(struct intel_bts *bts, uint64_t ip)
  227. {
  228. return machine__kernel_ip(bts->machine, ip) ?
  229. PERF_RECORD_MISC_KERNEL :
  230. PERF_RECORD_MISC_USER;
  231. }
  232. static int intel_bts_synth_branch_sample(struct intel_bts_queue *btsq,
  233. struct branch *branch)
  234. {
  235. int ret;
  236. struct intel_bts *bts = btsq->bts;
  237. union perf_event event;
  238. struct perf_sample sample = { .ip = 0, };
  239. if (bts->synth_opts.initial_skip &&
  240. bts->num_events++ <= bts->synth_opts.initial_skip)
  241. return 0;
  242. sample.ip = le64_to_cpu(branch->from);
  243. sample.cpumode = intel_bts_cpumode(bts, sample.ip);
  244. sample.pid = btsq->pid;
  245. sample.tid = btsq->tid;
  246. sample.addr = le64_to_cpu(branch->to);
  247. sample.id = btsq->bts->branches_id;
  248. sample.stream_id = btsq->bts->branches_id;
  249. sample.period = 1;
  250. sample.cpu = btsq->cpu;
  251. sample.flags = btsq->sample_flags;
  252. sample.insn_len = btsq->intel_pt_insn.length;
  253. memcpy(sample.insn, btsq->intel_pt_insn.buf, INTEL_PT_INSN_BUF_SZ);
  254. event.sample.header.type = PERF_RECORD_SAMPLE;
  255. event.sample.header.misc = sample.cpumode;
  256. event.sample.header.size = sizeof(struct perf_event_header);
  257. if (bts->synth_opts.inject) {
  258. event.sample.header.size = bts->branches_event_size;
  259. ret = perf_event__synthesize_sample(&event,
  260. bts->branches_sample_type,
  261. 0, &sample);
  262. if (ret)
  263. return ret;
  264. }
  265. ret = perf_session__deliver_synth_event(bts->session, &event, &sample);
  266. if (ret)
  267. pr_err("Intel BTS: failed to deliver branch event, error %d\n",
  268. ret);
  269. return ret;
  270. }
  271. static int intel_bts_get_next_insn(struct intel_bts_queue *btsq, u64 ip)
  272. {
  273. struct machine *machine = btsq->bts->machine;
  274. struct thread *thread;
  275. unsigned char buf[INTEL_PT_INSN_BUF_SZ];
  276. ssize_t len;
  277. bool x86_64;
  278. int err = -1;
  279. thread = machine__find_thread(machine, -1, btsq->tid);
  280. if (!thread)
  281. return -1;
  282. len = thread__memcpy(thread, machine, buf, ip, INTEL_PT_INSN_BUF_SZ, &x86_64);
  283. if (len <= 0)
  284. goto out_put;
  285. if (intel_pt_get_insn(buf, len, x86_64, &btsq->intel_pt_insn))
  286. goto out_put;
  287. err = 0;
  288. out_put:
  289. thread__put(thread);
  290. return err;
  291. }
  292. static int intel_bts_synth_error(struct intel_bts *bts, int cpu, pid_t pid,
  293. pid_t tid, u64 ip)
  294. {
  295. union perf_event event;
  296. int err;
  297. auxtrace_synth_error(&event.auxtrace_error, PERF_AUXTRACE_ERROR_ITRACE,
  298. INTEL_BTS_ERR_NOINSN, cpu, pid, tid, ip,
  299. "Failed to get instruction", 0);
  300. err = perf_session__deliver_synth_event(bts->session, &event, NULL);
  301. if (err)
  302. pr_err("Intel BTS: failed to deliver error event, error %d\n",
  303. err);
  304. return err;
  305. }
  306. static int intel_bts_get_branch_type(struct intel_bts_queue *btsq,
  307. struct branch *branch)
  308. {
  309. int err;
  310. if (!branch->from) {
  311. if (branch->to)
  312. btsq->sample_flags = PERF_IP_FLAG_BRANCH |
  313. PERF_IP_FLAG_TRACE_BEGIN;
  314. else
  315. btsq->sample_flags = 0;
  316. btsq->intel_pt_insn.length = 0;
  317. } else if (!branch->to) {
  318. btsq->sample_flags = PERF_IP_FLAG_BRANCH |
  319. PERF_IP_FLAG_TRACE_END;
  320. btsq->intel_pt_insn.length = 0;
  321. } else {
  322. err = intel_bts_get_next_insn(btsq, branch->from);
  323. if (err) {
  324. btsq->sample_flags = 0;
  325. btsq->intel_pt_insn.length = 0;
  326. if (!btsq->bts->synth_opts.errors)
  327. return 0;
  328. err = intel_bts_synth_error(btsq->bts, btsq->cpu,
  329. btsq->pid, btsq->tid,
  330. branch->from);
  331. return err;
  332. }
  333. btsq->sample_flags = intel_pt_insn_type(btsq->intel_pt_insn.op);
  334. /* Check for an async branch into the kernel */
  335. if (!machine__kernel_ip(btsq->bts->machine, branch->from) &&
  336. machine__kernel_ip(btsq->bts->machine, branch->to) &&
  337. btsq->sample_flags != (PERF_IP_FLAG_BRANCH |
  338. PERF_IP_FLAG_CALL |
  339. PERF_IP_FLAG_SYSCALLRET))
  340. btsq->sample_flags = PERF_IP_FLAG_BRANCH |
  341. PERF_IP_FLAG_CALL |
  342. PERF_IP_FLAG_ASYNC |
  343. PERF_IP_FLAG_INTERRUPT;
  344. }
  345. return 0;
  346. }
  347. static int intel_bts_process_buffer(struct intel_bts_queue *btsq,
  348. struct auxtrace_buffer *buffer,
  349. struct thread *thread)
  350. {
  351. struct branch *branch;
  352. size_t sz, bsz = sizeof(struct branch);
  353. u32 filter = btsq->bts->branches_filter;
  354. int err = 0;
  355. if (buffer->use_data) {
  356. sz = buffer->use_size;
  357. branch = buffer->use_data;
  358. } else {
  359. sz = buffer->size;
  360. branch = buffer->data;
  361. }
  362. if (!btsq->bts->sample_branches)
  363. return 0;
  364. for (; sz > bsz; branch += 1, sz -= bsz) {
  365. if (!branch->from && !branch->to)
  366. continue;
  367. intel_bts_get_branch_type(btsq, branch);
  368. if (btsq->bts->synth_opts.thread_stack)
  369. thread_stack__event(thread, btsq->cpu, btsq->sample_flags,
  370. le64_to_cpu(branch->from),
  371. le64_to_cpu(branch->to),
  372. btsq->intel_pt_insn.length,
  373. buffer->buffer_nr + 1, true, 0, 0);
  374. if (filter && !(filter & btsq->sample_flags))
  375. continue;
  376. err = intel_bts_synth_branch_sample(btsq, branch);
  377. if (err)
  378. break;
  379. }
  380. return err;
  381. }
  382. static int intel_bts_process_queue(struct intel_bts_queue *btsq, u64 *timestamp)
  383. {
  384. struct auxtrace_buffer *buffer = btsq->buffer, *old_buffer = buffer;
  385. struct auxtrace_queue *queue;
  386. struct thread *thread;
  387. int err;
  388. if (btsq->done)
  389. return 1;
  390. if (btsq->pid == -1) {
  391. thread = machine__find_thread(btsq->bts->machine, -1,
  392. btsq->tid);
  393. if (thread)
  394. btsq->pid = thread->pid_;
  395. } else {
  396. thread = machine__findnew_thread(btsq->bts->machine, btsq->pid,
  397. btsq->tid);
  398. }
  399. queue = &btsq->bts->queues.queue_array[btsq->queue_nr];
  400. if (!buffer)
  401. buffer = auxtrace_buffer__next(queue, NULL);
  402. if (!buffer) {
  403. if (!btsq->bts->sampling_mode)
  404. btsq->done = 1;
  405. err = 1;
  406. goto out_put;
  407. }
  408. /* Currently there is no support for split buffers */
  409. if (buffer->consecutive) {
  410. err = -EINVAL;
  411. goto out_put;
  412. }
  413. if (!buffer->data) {
  414. int fd = perf_data__fd(btsq->bts->session->data);
  415. buffer->data = auxtrace_buffer__get_data(buffer, fd);
  416. if (!buffer->data) {
  417. err = -ENOMEM;
  418. goto out_put;
  419. }
  420. }
  421. if (btsq->bts->snapshot_mode && !buffer->consecutive &&
  422. intel_bts_do_fix_overlap(queue, buffer)) {
  423. err = -ENOMEM;
  424. goto out_put;
  425. }
  426. if (!btsq->bts->synth_opts.callchain &&
  427. !btsq->bts->synth_opts.thread_stack && thread &&
  428. (!old_buffer || btsq->bts->sampling_mode ||
  429. (btsq->bts->snapshot_mode && !buffer->consecutive)))
  430. thread_stack__set_trace_nr(thread, btsq->cpu, buffer->buffer_nr + 1);
  431. err = intel_bts_process_buffer(btsq, buffer, thread);
  432. auxtrace_buffer__drop_data(buffer);
  433. btsq->buffer = auxtrace_buffer__next(queue, buffer);
  434. if (btsq->buffer) {
  435. if (timestamp)
  436. *timestamp = btsq->buffer->reference;
  437. } else {
  438. if (!btsq->bts->sampling_mode)
  439. btsq->done = 1;
  440. }
  441. out_put:
  442. thread__put(thread);
  443. return err;
  444. }
  445. static int intel_bts_flush_queue(struct intel_bts_queue *btsq)
  446. {
  447. u64 ts = 0;
  448. int ret;
  449. while (1) {
  450. ret = intel_bts_process_queue(btsq, &ts);
  451. if (ret < 0)
  452. return ret;
  453. if (ret)
  454. break;
  455. }
  456. return 0;
  457. }
  458. static int intel_bts_process_tid_exit(struct intel_bts *bts, pid_t tid)
  459. {
  460. struct auxtrace_queues *queues = &bts->queues;
  461. unsigned int i;
  462. for (i = 0; i < queues->nr_queues; i++) {
  463. struct auxtrace_queue *queue = &bts->queues.queue_array[i];
  464. struct intel_bts_queue *btsq = queue->priv;
  465. if (btsq && btsq->tid == tid)
  466. return intel_bts_flush_queue(btsq);
  467. }
  468. return 0;
  469. }
  470. static int intel_bts_process_queues(struct intel_bts *bts, u64 timestamp)
  471. {
  472. while (1) {
  473. unsigned int queue_nr;
  474. struct auxtrace_queue *queue;
  475. struct intel_bts_queue *btsq;
  476. u64 ts = 0;
  477. int ret;
  478. if (!bts->heap.heap_cnt)
  479. return 0;
  480. if (bts->heap.heap_array[0].ordinal > timestamp)
  481. return 0;
  482. queue_nr = bts->heap.heap_array[0].queue_nr;
  483. queue = &bts->queues.queue_array[queue_nr];
  484. btsq = queue->priv;
  485. auxtrace_heap__pop(&bts->heap);
  486. ret = intel_bts_process_queue(btsq, &ts);
  487. if (ret < 0) {
  488. auxtrace_heap__add(&bts->heap, queue_nr, ts);
  489. return ret;
  490. }
  491. if (!ret) {
  492. ret = auxtrace_heap__add(&bts->heap, queue_nr, ts);
  493. if (ret < 0)
  494. return ret;
  495. } else {
  496. btsq->on_heap = false;
  497. }
  498. }
  499. return 0;
  500. }
  501. static int intel_bts_process_event(struct perf_session *session,
  502. union perf_event *event,
  503. struct perf_sample *sample,
  504. struct perf_tool *tool)
  505. {
  506. struct intel_bts *bts = container_of(session->auxtrace, struct intel_bts,
  507. auxtrace);
  508. u64 timestamp;
  509. int err;
  510. if (dump_trace)
  511. return 0;
  512. if (!tool->ordered_events) {
  513. pr_err("Intel BTS requires ordered events\n");
  514. return -EINVAL;
  515. }
  516. if (sample->time && sample->time != (u64)-1)
  517. timestamp = perf_time_to_tsc(sample->time, &bts->tc);
  518. else
  519. timestamp = 0;
  520. err = intel_bts_update_queues(bts);
  521. if (err)
  522. return err;
  523. err = intel_bts_process_queues(bts, timestamp);
  524. if (err)
  525. return err;
  526. if (event->header.type == PERF_RECORD_EXIT) {
  527. err = intel_bts_process_tid_exit(bts, event->fork.tid);
  528. if (err)
  529. return err;
  530. }
  531. if (event->header.type == PERF_RECORD_AUX &&
  532. (event->aux.flags & PERF_AUX_FLAG_TRUNCATED) &&
  533. bts->synth_opts.errors)
  534. err = intel_bts_lost(bts, sample);
  535. return err;
  536. }
  537. static int intel_bts_process_auxtrace_event(struct perf_session *session,
  538. union perf_event *event,
  539. struct perf_tool *tool __maybe_unused)
  540. {
  541. struct intel_bts *bts = container_of(session->auxtrace, struct intel_bts,
  542. auxtrace);
  543. if (bts->sampling_mode)
  544. return 0;
  545. if (!bts->data_queued) {
  546. struct auxtrace_buffer *buffer;
  547. off_t data_offset;
  548. int fd = perf_data__fd(session->data);
  549. int err;
  550. if (perf_data__is_pipe(session->data)) {
  551. data_offset = 0;
  552. } else {
  553. data_offset = lseek(fd, 0, SEEK_CUR);
  554. if (data_offset == -1)
  555. return -errno;
  556. }
  557. err = auxtrace_queues__add_event(&bts->queues, session, event,
  558. data_offset, &buffer);
  559. if (err)
  560. return err;
  561. /* Dump here now we have copied a piped trace out of the pipe */
  562. if (dump_trace) {
  563. if (auxtrace_buffer__get_data(buffer, fd)) {
  564. intel_bts_dump_event(bts, buffer->data,
  565. buffer->size);
  566. auxtrace_buffer__put_data(buffer);
  567. }
  568. }
  569. }
  570. return 0;
  571. }
  572. static int intel_bts_flush(struct perf_session *session,
  573. struct perf_tool *tool __maybe_unused)
  574. {
  575. struct intel_bts *bts = container_of(session->auxtrace, struct intel_bts,
  576. auxtrace);
  577. int ret;
  578. if (dump_trace || bts->sampling_mode)
  579. return 0;
  580. if (!tool->ordered_events)
  581. return -EINVAL;
  582. ret = intel_bts_update_queues(bts);
  583. if (ret < 0)
  584. return ret;
  585. return intel_bts_process_queues(bts, MAX_TIMESTAMP);
  586. }
  587. static void intel_bts_free_queue(void *priv)
  588. {
  589. struct intel_bts_queue *btsq = priv;
  590. if (!btsq)
  591. return;
  592. free(btsq);
  593. }
  594. static void intel_bts_free_events(struct perf_session *session)
  595. {
  596. struct intel_bts *bts = container_of(session->auxtrace, struct intel_bts,
  597. auxtrace);
  598. struct auxtrace_queues *queues = &bts->queues;
  599. unsigned int i;
  600. for (i = 0; i < queues->nr_queues; i++) {
  601. intel_bts_free_queue(queues->queue_array[i].priv);
  602. queues->queue_array[i].priv = NULL;
  603. }
  604. auxtrace_queues__free(queues);
  605. }
  606. static void intel_bts_free(struct perf_session *session)
  607. {
  608. struct intel_bts *bts = container_of(session->auxtrace, struct intel_bts,
  609. auxtrace);
  610. auxtrace_heap__free(&bts->heap);
  611. intel_bts_free_events(session);
  612. session->auxtrace = NULL;
  613. free(bts);
  614. }
  615. static bool intel_bts_evsel_is_auxtrace(struct perf_session *session,
  616. struct evsel *evsel)
  617. {
  618. struct intel_bts *bts = container_of(session->auxtrace, struct intel_bts,
  619. auxtrace);
  620. return evsel->core.attr.type == bts->pmu_type;
  621. }
  622. struct intel_bts_synth {
  623. struct perf_tool dummy_tool;
  624. struct perf_session *session;
  625. };
  626. static int intel_bts_event_synth(struct perf_tool *tool,
  627. union perf_event *event,
  628. struct perf_sample *sample __maybe_unused,
  629. struct machine *machine __maybe_unused)
  630. {
  631. struct intel_bts_synth *intel_bts_synth =
  632. container_of(tool, struct intel_bts_synth, dummy_tool);
  633. return perf_session__deliver_synth_event(intel_bts_synth->session,
  634. event, NULL);
  635. }
  636. static int intel_bts_synth_event(struct perf_session *session,
  637. struct perf_event_attr *attr, u64 id)
  638. {
  639. struct intel_bts_synth intel_bts_synth;
  640. memset(&intel_bts_synth, 0, sizeof(struct intel_bts_synth));
  641. intel_bts_synth.session = session;
  642. return perf_event__synthesize_attr(&intel_bts_synth.dummy_tool, attr, 1,
  643. &id, intel_bts_event_synth);
  644. }
  645. static int intel_bts_synth_events(struct intel_bts *bts,
  646. struct perf_session *session)
  647. {
  648. struct evlist *evlist = session->evlist;
  649. struct evsel *evsel;
  650. struct perf_event_attr attr;
  651. bool found = false;
  652. u64 id;
  653. int err;
  654. evlist__for_each_entry(evlist, evsel) {
  655. if (evsel->core.attr.type == bts->pmu_type && evsel->core.ids) {
  656. found = true;
  657. break;
  658. }
  659. }
  660. if (!found) {
  661. pr_debug("There are no selected events with Intel BTS data\n");
  662. return 0;
  663. }
  664. memset(&attr, 0, sizeof(struct perf_event_attr));
  665. attr.size = sizeof(struct perf_event_attr);
  666. attr.type = PERF_TYPE_HARDWARE;
  667. attr.sample_type = evsel->core.attr.sample_type & PERF_SAMPLE_MASK;
  668. attr.sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID |
  669. PERF_SAMPLE_PERIOD;
  670. attr.sample_type &= ~(u64)PERF_SAMPLE_TIME;
  671. attr.sample_type &= ~(u64)PERF_SAMPLE_CPU;
  672. attr.exclude_user = evsel->core.attr.exclude_user;
  673. attr.exclude_kernel = evsel->core.attr.exclude_kernel;
  674. attr.exclude_hv = evsel->core.attr.exclude_hv;
  675. attr.exclude_host = evsel->core.attr.exclude_host;
  676. attr.exclude_guest = evsel->core.attr.exclude_guest;
  677. attr.sample_id_all = evsel->core.attr.sample_id_all;
  678. attr.read_format = evsel->core.attr.read_format;
  679. id = evsel->core.id[0] + 1000000000;
  680. if (!id)
  681. id = 1;
  682. if (bts->synth_opts.branches) {
  683. attr.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS;
  684. attr.sample_period = 1;
  685. attr.sample_type |= PERF_SAMPLE_ADDR;
  686. pr_debug("Synthesizing 'branches' event with id %" PRIu64 " sample type %#" PRIx64 "\n",
  687. id, (u64)attr.sample_type);
  688. err = intel_bts_synth_event(session, &attr, id);
  689. if (err) {
  690. pr_err("%s: failed to synthesize 'branches' event type\n",
  691. __func__);
  692. return err;
  693. }
  694. bts->sample_branches = true;
  695. bts->branches_sample_type = attr.sample_type;
  696. bts->branches_id = id;
  697. /*
  698. * We only use sample types from PERF_SAMPLE_MASK so we can use
  699. * __evsel__sample_size() here.
  700. */
  701. bts->branches_event_size = sizeof(struct perf_record_sample) +
  702. __evsel__sample_size(attr.sample_type);
  703. }
  704. return 0;
  705. }
  706. static const char * const intel_bts_info_fmts[] = {
  707. [INTEL_BTS_PMU_TYPE] = " PMU Type %"PRId64"\n",
  708. [INTEL_BTS_TIME_SHIFT] = " Time Shift %"PRIu64"\n",
  709. [INTEL_BTS_TIME_MULT] = " Time Muliplier %"PRIu64"\n",
  710. [INTEL_BTS_TIME_ZERO] = " Time Zero %"PRIu64"\n",
  711. [INTEL_BTS_CAP_USER_TIME_ZERO] = " Cap Time Zero %"PRId64"\n",
  712. [INTEL_BTS_SNAPSHOT_MODE] = " Snapshot mode %"PRId64"\n",
  713. };
  714. static void intel_bts_print_info(__u64 *arr, int start, int finish)
  715. {
  716. int i;
  717. if (!dump_trace)
  718. return;
  719. for (i = start; i <= finish; i++)
  720. fprintf(stdout, intel_bts_info_fmts[i], arr[i]);
  721. }
  722. int intel_bts_process_auxtrace_info(union perf_event *event,
  723. struct perf_session *session)
  724. {
  725. struct perf_record_auxtrace_info *auxtrace_info = &event->auxtrace_info;
  726. size_t min_sz = sizeof(u64) * INTEL_BTS_SNAPSHOT_MODE;
  727. struct intel_bts *bts;
  728. int err;
  729. if (auxtrace_info->header.size < sizeof(struct perf_record_auxtrace_info) +
  730. min_sz)
  731. return -EINVAL;
  732. bts = zalloc(sizeof(struct intel_bts));
  733. if (!bts)
  734. return -ENOMEM;
  735. err = auxtrace_queues__init(&bts->queues);
  736. if (err)
  737. goto err_free;
  738. bts->session = session;
  739. bts->machine = &session->machines.host; /* No kvm support */
  740. bts->auxtrace_type = auxtrace_info->type;
  741. bts->pmu_type = auxtrace_info->priv[INTEL_BTS_PMU_TYPE];
  742. bts->tc.time_shift = auxtrace_info->priv[INTEL_BTS_TIME_SHIFT];
  743. bts->tc.time_mult = auxtrace_info->priv[INTEL_BTS_TIME_MULT];
  744. bts->tc.time_zero = auxtrace_info->priv[INTEL_BTS_TIME_ZERO];
  745. bts->cap_user_time_zero =
  746. auxtrace_info->priv[INTEL_BTS_CAP_USER_TIME_ZERO];
  747. bts->snapshot_mode = auxtrace_info->priv[INTEL_BTS_SNAPSHOT_MODE];
  748. bts->sampling_mode = false;
  749. bts->auxtrace.process_event = intel_bts_process_event;
  750. bts->auxtrace.process_auxtrace_event = intel_bts_process_auxtrace_event;
  751. bts->auxtrace.flush_events = intel_bts_flush;
  752. bts->auxtrace.free_events = intel_bts_free_events;
  753. bts->auxtrace.free = intel_bts_free;
  754. bts->auxtrace.evsel_is_auxtrace = intel_bts_evsel_is_auxtrace;
  755. session->auxtrace = &bts->auxtrace;
  756. intel_bts_print_info(&auxtrace_info->priv[0], INTEL_BTS_PMU_TYPE,
  757. INTEL_BTS_SNAPSHOT_MODE);
  758. if (dump_trace)
  759. return 0;
  760. if (session->itrace_synth_opts->set) {
  761. bts->synth_opts = *session->itrace_synth_opts;
  762. } else {
  763. itrace_synth_opts__set_default(&bts->synth_opts,
  764. session->itrace_synth_opts->default_no_sample);
  765. bts->synth_opts.thread_stack =
  766. session->itrace_synth_opts->thread_stack;
  767. }
  768. if (bts->synth_opts.calls)
  769. bts->branches_filter |= PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC |
  770. PERF_IP_FLAG_TRACE_END;
  771. if (bts->synth_opts.returns)
  772. bts->branches_filter |= PERF_IP_FLAG_RETURN |
  773. PERF_IP_FLAG_TRACE_BEGIN;
  774. err = intel_bts_synth_events(bts, session);
  775. if (err)
  776. goto err_free_queues;
  777. err = auxtrace_queues__process_index(&bts->queues, session);
  778. if (err)
  779. goto err_free_queues;
  780. if (bts->queues.populated)
  781. bts->data_queued = true;
  782. return 0;
  783. err_free_queues:
  784. auxtrace_queues__free(&bts->queues);
  785. session->auxtrace = NULL;
  786. err_free:
  787. free(bts);
  788. return err;
  789. }