intel-bts.c 23 KB

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