intel-bts.c 22 KB

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