intel-bts.c 23 KB

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