intel-bts.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  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 <inttypes.h>
  18. #include <linux/kernel.h>
  19. #include <linux/types.h>
  20. #include <linux/bitops.h>
  21. #include <linux/log2.h>
  22. #include "cpumap.h"
  23. #include "color.h"
  24. #include "evsel.h"
  25. #include "evlist.h"
  26. #include "machine.h"
  27. #include "session.h"
  28. #include "util.h"
  29. #include "thread.h"
  30. #include "thread-stack.h"
  31. #include "debug.h"
  32. #include "tsc.h"
  33. #include "auxtrace.h"
  34. #include "intel-pt-decoder/intel-pt-insn-decoder.h"
  35. #include "intel-bts.h"
  36. #define MAX_TIMESTAMP (~0ULL)
  37. #define INTEL_BTS_ERR_NOINSN 5
  38. #define INTEL_BTS_ERR_LOST 9
  39. #if __BYTE_ORDER == __BIG_ENDIAN
  40. #define le64_to_cpu bswap_64
  41. #else
  42. #define le64_to_cpu
  43. #endif
  44. struct intel_bts {
  45. struct auxtrace auxtrace;
  46. struct auxtrace_queues queues;
  47. struct auxtrace_heap heap;
  48. u32 auxtrace_type;
  49. struct perf_session *session;
  50. struct machine *machine;
  51. bool sampling_mode;
  52. bool snapshot_mode;
  53. bool data_queued;
  54. u32 pmu_type;
  55. struct perf_tsc_conversion tc;
  56. bool cap_user_time_zero;
  57. struct itrace_synth_opts synth_opts;
  58. bool sample_branches;
  59. u32 branches_filter;
  60. u64 branches_sample_type;
  61. u64 branches_id;
  62. size_t branches_event_size;
  63. bool synth_needs_swap;
  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. bts->synth_needs_swap);
  264. if (ret)
  265. return ret;
  266. }
  267. ret = perf_session__deliver_synth_event(bts->session, &event, &sample);
  268. if (ret)
  269. pr_err("Intel BTS: failed to deliver branch event, error %d\n",
  270. ret);
  271. return ret;
  272. }
  273. static int intel_bts_get_next_insn(struct intel_bts_queue *btsq, u64 ip)
  274. {
  275. struct machine *machine = btsq->bts->machine;
  276. struct thread *thread;
  277. struct addr_location al;
  278. unsigned char buf[INTEL_PT_INSN_BUF_SZ];
  279. ssize_t len;
  280. int x86_64;
  281. uint8_t cpumode;
  282. int err = -1;
  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,
  294. INTEL_PT_INSN_BUF_SZ);
  295. if (len <= 0)
  296. goto out_put;
  297. /* Load maps to ensure dso->is_64_bit has been updated */
  298. map__load(al.map);
  299. x86_64 = al.map->dso->is_64_bit;
  300. if (intel_pt_get_insn(buf, len, x86_64, &btsq->intel_pt_insn))
  301. goto out_put;
  302. err = 0;
  303. out_put:
  304. thread__put(thread);
  305. return err;
  306. }
  307. static int intel_bts_synth_error(struct intel_bts *bts, int cpu, pid_t pid,
  308. pid_t tid, u64 ip)
  309. {
  310. union perf_event event;
  311. int err;
  312. auxtrace_synth_error(&event.auxtrace_error, PERF_AUXTRACE_ERROR_ITRACE,
  313. INTEL_BTS_ERR_NOINSN, cpu, pid, tid, ip,
  314. "Failed to get instruction");
  315. err = perf_session__deliver_synth_event(bts->session, &event, NULL);
  316. if (err)
  317. pr_err("Intel BTS: failed to deliver error event, error %d\n",
  318. err);
  319. return err;
  320. }
  321. static int intel_bts_get_branch_type(struct intel_bts_queue *btsq,
  322. struct branch *branch)
  323. {
  324. int err;
  325. if (!branch->from) {
  326. if (branch->to)
  327. btsq->sample_flags = PERF_IP_FLAG_BRANCH |
  328. PERF_IP_FLAG_TRACE_BEGIN;
  329. else
  330. btsq->sample_flags = 0;
  331. btsq->intel_pt_insn.length = 0;
  332. } else if (!branch->to) {
  333. btsq->sample_flags = PERF_IP_FLAG_BRANCH |
  334. PERF_IP_FLAG_TRACE_END;
  335. btsq->intel_pt_insn.length = 0;
  336. } else {
  337. err = intel_bts_get_next_insn(btsq, branch->from);
  338. if (err) {
  339. btsq->sample_flags = 0;
  340. btsq->intel_pt_insn.length = 0;
  341. if (!btsq->bts->synth_opts.errors)
  342. return 0;
  343. err = intel_bts_synth_error(btsq->bts, btsq->cpu,
  344. btsq->pid, btsq->tid,
  345. branch->from);
  346. return err;
  347. }
  348. btsq->sample_flags = intel_pt_insn_type(btsq->intel_pt_insn.op);
  349. /* Check for an async branch into the kernel */
  350. if (!machine__kernel_ip(btsq->bts->machine, branch->from) &&
  351. machine__kernel_ip(btsq->bts->machine, branch->to) &&
  352. btsq->sample_flags != (PERF_IP_FLAG_BRANCH |
  353. PERF_IP_FLAG_CALL |
  354. PERF_IP_FLAG_SYSCALLRET))
  355. btsq->sample_flags = PERF_IP_FLAG_BRANCH |
  356. PERF_IP_FLAG_CALL |
  357. PERF_IP_FLAG_ASYNC |
  358. PERF_IP_FLAG_INTERRUPT;
  359. }
  360. return 0;
  361. }
  362. static int intel_bts_process_buffer(struct intel_bts_queue *btsq,
  363. struct auxtrace_buffer *buffer,
  364. struct thread *thread)
  365. {
  366. struct branch *branch;
  367. size_t sz, bsz = sizeof(struct branch);
  368. u32 filter = btsq->bts->branches_filter;
  369. int err = 0;
  370. if (buffer->use_data) {
  371. sz = buffer->use_size;
  372. branch = buffer->use_data;
  373. } else {
  374. sz = buffer->size;
  375. branch = buffer->data;
  376. }
  377. if (!btsq->bts->sample_branches)
  378. return 0;
  379. for (; sz > bsz; branch += 1, sz -= bsz) {
  380. if (!branch->from && !branch->to)
  381. continue;
  382. intel_bts_get_branch_type(btsq, branch);
  383. if (btsq->bts->synth_opts.thread_stack)
  384. thread_stack__event(thread, btsq->sample_flags,
  385. le64_to_cpu(branch->from),
  386. le64_to_cpu(branch->to),
  387. btsq->intel_pt_insn.length,
  388. buffer->buffer_nr + 1);
  389. if (filter && !(filter & btsq->sample_flags))
  390. continue;
  391. err = intel_bts_synth_branch_sample(btsq, branch);
  392. if (err)
  393. break;
  394. }
  395. return err;
  396. }
  397. static int intel_bts_process_queue(struct intel_bts_queue *btsq, u64 *timestamp)
  398. {
  399. struct auxtrace_buffer *buffer = btsq->buffer, *old_buffer = buffer;
  400. struct auxtrace_queue *queue;
  401. struct thread *thread;
  402. int err;
  403. if (btsq->done)
  404. return 1;
  405. if (btsq->pid == -1) {
  406. thread = machine__find_thread(btsq->bts->machine, -1,
  407. btsq->tid);
  408. if (thread)
  409. btsq->pid = thread->pid_;
  410. } else {
  411. thread = machine__findnew_thread(btsq->bts->machine, btsq->pid,
  412. btsq->tid);
  413. }
  414. queue = &btsq->bts->queues.queue_array[btsq->queue_nr];
  415. if (!buffer)
  416. buffer = auxtrace_buffer__next(queue, NULL);
  417. if (!buffer) {
  418. if (!btsq->bts->sampling_mode)
  419. btsq->done = 1;
  420. err = 1;
  421. goto out_put;
  422. }
  423. /* Currently there is no support for split buffers */
  424. if (buffer->consecutive) {
  425. err = -EINVAL;
  426. goto out_put;
  427. }
  428. if (!buffer->data) {
  429. int fd = perf_data_file__fd(btsq->bts->session->file);
  430. buffer->data = auxtrace_buffer__get_data(buffer, fd);
  431. if (!buffer->data) {
  432. err = -ENOMEM;
  433. goto out_put;
  434. }
  435. }
  436. if (btsq->bts->snapshot_mode && !buffer->consecutive &&
  437. intel_bts_do_fix_overlap(queue, buffer)) {
  438. err = -ENOMEM;
  439. goto out_put;
  440. }
  441. if (!btsq->bts->synth_opts.callchain &&
  442. !btsq->bts->synth_opts.thread_stack && thread &&
  443. (!old_buffer || btsq->bts->sampling_mode ||
  444. (btsq->bts->snapshot_mode && !buffer->consecutive)))
  445. thread_stack__set_trace_nr(thread, buffer->buffer_nr + 1);
  446. err = intel_bts_process_buffer(btsq, buffer, thread);
  447. auxtrace_buffer__drop_data(buffer);
  448. btsq->buffer = auxtrace_buffer__next(queue, buffer);
  449. if (btsq->buffer) {
  450. if (timestamp)
  451. *timestamp = btsq->buffer->reference;
  452. } else {
  453. if (!btsq->bts->sampling_mode)
  454. btsq->done = 1;
  455. }
  456. out_put:
  457. thread__put(thread);
  458. return err;
  459. }
  460. static int intel_bts_flush_queue(struct intel_bts_queue *btsq)
  461. {
  462. u64 ts = 0;
  463. int ret;
  464. while (1) {
  465. ret = intel_bts_process_queue(btsq, &ts);
  466. if (ret < 0)
  467. return ret;
  468. if (ret)
  469. break;
  470. }
  471. return 0;
  472. }
  473. static int intel_bts_process_tid_exit(struct intel_bts *bts, pid_t tid)
  474. {
  475. struct auxtrace_queues *queues = &bts->queues;
  476. unsigned int i;
  477. for (i = 0; i < queues->nr_queues; i++) {
  478. struct auxtrace_queue *queue = &bts->queues.queue_array[i];
  479. struct intel_bts_queue *btsq = queue->priv;
  480. if (btsq && btsq->tid == tid)
  481. return intel_bts_flush_queue(btsq);
  482. }
  483. return 0;
  484. }
  485. static int intel_bts_process_queues(struct intel_bts *bts, u64 timestamp)
  486. {
  487. while (1) {
  488. unsigned int queue_nr;
  489. struct auxtrace_queue *queue;
  490. struct intel_bts_queue *btsq;
  491. u64 ts = 0;
  492. int ret;
  493. if (!bts->heap.heap_cnt)
  494. return 0;
  495. if (bts->heap.heap_array[0].ordinal > timestamp)
  496. return 0;
  497. queue_nr = bts->heap.heap_array[0].queue_nr;
  498. queue = &bts->queues.queue_array[queue_nr];
  499. btsq = queue->priv;
  500. auxtrace_heap__pop(&bts->heap);
  501. ret = intel_bts_process_queue(btsq, &ts);
  502. if (ret < 0) {
  503. auxtrace_heap__add(&bts->heap, queue_nr, ts);
  504. return ret;
  505. }
  506. if (!ret) {
  507. ret = auxtrace_heap__add(&bts->heap, queue_nr, ts);
  508. if (ret < 0)
  509. return ret;
  510. } else {
  511. btsq->on_heap = false;
  512. }
  513. }
  514. return 0;
  515. }
  516. static int intel_bts_process_event(struct perf_session *session,
  517. union perf_event *event,
  518. struct perf_sample *sample,
  519. struct perf_tool *tool)
  520. {
  521. struct intel_bts *bts = container_of(session->auxtrace, struct intel_bts,
  522. auxtrace);
  523. u64 timestamp;
  524. int err;
  525. if (dump_trace)
  526. return 0;
  527. if (!tool->ordered_events) {
  528. pr_err("Intel BTS requires ordered events\n");
  529. return -EINVAL;
  530. }
  531. if (sample->time && sample->time != (u64)-1)
  532. timestamp = perf_time_to_tsc(sample->time, &bts->tc);
  533. else
  534. timestamp = 0;
  535. err = intel_bts_update_queues(bts);
  536. if (err)
  537. return err;
  538. err = intel_bts_process_queues(bts, timestamp);
  539. if (err)
  540. return err;
  541. if (event->header.type == PERF_RECORD_EXIT) {
  542. err = intel_bts_process_tid_exit(bts, event->fork.tid);
  543. if (err)
  544. return err;
  545. }
  546. if (event->header.type == PERF_RECORD_AUX &&
  547. (event->aux.flags & PERF_AUX_FLAG_TRUNCATED) &&
  548. bts->synth_opts.errors)
  549. err = intel_bts_lost(bts, sample);
  550. return err;
  551. }
  552. static int intel_bts_process_auxtrace_event(struct perf_session *session,
  553. union perf_event *event,
  554. struct perf_tool *tool __maybe_unused)
  555. {
  556. struct intel_bts *bts = container_of(session->auxtrace, struct intel_bts,
  557. auxtrace);
  558. if (bts->sampling_mode)
  559. return 0;
  560. if (!bts->data_queued) {
  561. struct auxtrace_buffer *buffer;
  562. off_t data_offset;
  563. int fd = perf_data_file__fd(session->file);
  564. int err;
  565. if (perf_data_file__is_pipe(session->file)) {
  566. data_offset = 0;
  567. } else {
  568. data_offset = lseek(fd, 0, SEEK_CUR);
  569. if (data_offset == -1)
  570. return -errno;
  571. }
  572. err = auxtrace_queues__add_event(&bts->queues, session, event,
  573. data_offset, &buffer);
  574. if (err)
  575. return err;
  576. /* Dump here now we have copied a piped trace out of the pipe */
  577. if (dump_trace) {
  578. if (auxtrace_buffer__get_data(buffer, fd)) {
  579. intel_bts_dump_event(bts, buffer->data,
  580. buffer->size);
  581. auxtrace_buffer__put_data(buffer);
  582. }
  583. }
  584. }
  585. return 0;
  586. }
  587. static int intel_bts_flush(struct perf_session *session,
  588. struct perf_tool *tool __maybe_unused)
  589. {
  590. struct intel_bts *bts = container_of(session->auxtrace, struct intel_bts,
  591. auxtrace);
  592. int ret;
  593. if (dump_trace || bts->sampling_mode)
  594. return 0;
  595. if (!tool->ordered_events)
  596. return -EINVAL;
  597. ret = intel_bts_update_queues(bts);
  598. if (ret < 0)
  599. return ret;
  600. return intel_bts_process_queues(bts, MAX_TIMESTAMP);
  601. }
  602. static void intel_bts_free_queue(void *priv)
  603. {
  604. struct intel_bts_queue *btsq = priv;
  605. if (!btsq)
  606. return;
  607. free(btsq);
  608. }
  609. static void intel_bts_free_events(struct perf_session *session)
  610. {
  611. struct intel_bts *bts = container_of(session->auxtrace, struct intel_bts,
  612. auxtrace);
  613. struct auxtrace_queues *queues = &bts->queues;
  614. unsigned int i;
  615. for (i = 0; i < queues->nr_queues; i++) {
  616. intel_bts_free_queue(queues->queue_array[i].priv);
  617. queues->queue_array[i].priv = NULL;
  618. }
  619. auxtrace_queues__free(queues);
  620. }
  621. static void intel_bts_free(struct perf_session *session)
  622. {
  623. struct intel_bts *bts = container_of(session->auxtrace, struct intel_bts,
  624. auxtrace);
  625. auxtrace_heap__free(&bts->heap);
  626. intel_bts_free_events(session);
  627. session->auxtrace = NULL;
  628. free(bts);
  629. }
  630. struct intel_bts_synth {
  631. struct perf_tool dummy_tool;
  632. struct perf_session *session;
  633. };
  634. static int intel_bts_event_synth(struct perf_tool *tool,
  635. union perf_event *event,
  636. struct perf_sample *sample __maybe_unused,
  637. struct machine *machine __maybe_unused)
  638. {
  639. struct intel_bts_synth *intel_bts_synth =
  640. container_of(tool, struct intel_bts_synth, dummy_tool);
  641. return perf_session__deliver_synth_event(intel_bts_synth->session,
  642. event, NULL);
  643. }
  644. static int intel_bts_synth_event(struct perf_session *session,
  645. struct perf_event_attr *attr, u64 id)
  646. {
  647. struct intel_bts_synth intel_bts_synth;
  648. memset(&intel_bts_synth, 0, sizeof(struct intel_bts_synth));
  649. intel_bts_synth.session = session;
  650. return perf_event__synthesize_attr(&intel_bts_synth.dummy_tool, attr, 1,
  651. &id, intel_bts_event_synth);
  652. }
  653. static int intel_bts_synth_events(struct intel_bts *bts,
  654. struct perf_session *session)
  655. {
  656. struct perf_evlist *evlist = session->evlist;
  657. struct perf_evsel *evsel;
  658. struct perf_event_attr attr;
  659. bool found = false;
  660. u64 id;
  661. int err;
  662. evlist__for_each_entry(evlist, evsel) {
  663. if (evsel->attr.type == bts->pmu_type && evsel->ids) {
  664. found = true;
  665. break;
  666. }
  667. }
  668. if (!found) {
  669. pr_debug("There are no selected events with Intel BTS data\n");
  670. return 0;
  671. }
  672. memset(&attr, 0, sizeof(struct perf_event_attr));
  673. attr.size = sizeof(struct perf_event_attr);
  674. attr.type = PERF_TYPE_HARDWARE;
  675. attr.sample_type = evsel->attr.sample_type & PERF_SAMPLE_MASK;
  676. attr.sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID |
  677. PERF_SAMPLE_PERIOD;
  678. attr.sample_type &= ~(u64)PERF_SAMPLE_TIME;
  679. attr.sample_type &= ~(u64)PERF_SAMPLE_CPU;
  680. attr.exclude_user = evsel->attr.exclude_user;
  681. attr.exclude_kernel = evsel->attr.exclude_kernel;
  682. attr.exclude_hv = evsel->attr.exclude_hv;
  683. attr.exclude_host = evsel->attr.exclude_host;
  684. attr.exclude_guest = evsel->attr.exclude_guest;
  685. attr.sample_id_all = evsel->attr.sample_id_all;
  686. attr.read_format = evsel->attr.read_format;
  687. id = evsel->id[0] + 1000000000;
  688. if (!id)
  689. id = 1;
  690. if (bts->synth_opts.branches) {
  691. attr.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS;
  692. attr.sample_period = 1;
  693. attr.sample_type |= PERF_SAMPLE_ADDR;
  694. pr_debug("Synthesizing 'branches' event with id %" PRIu64 " sample type %#" PRIx64 "\n",
  695. id, (u64)attr.sample_type);
  696. err = intel_bts_synth_event(session, &attr, id);
  697. if (err) {
  698. pr_err("%s: failed to synthesize 'branches' event type\n",
  699. __func__);
  700. return err;
  701. }
  702. bts->sample_branches = true;
  703. bts->branches_sample_type = attr.sample_type;
  704. bts->branches_id = id;
  705. /*
  706. * We only use sample types from PERF_SAMPLE_MASK so we can use
  707. * __perf_evsel__sample_size() here.
  708. */
  709. bts->branches_event_size = sizeof(struct sample_event) +
  710. __perf_evsel__sample_size(attr.sample_type);
  711. }
  712. bts->synth_needs_swap = evsel->needs_swap;
  713. return 0;
  714. }
  715. static const char * const intel_bts_info_fmts[] = {
  716. [INTEL_BTS_PMU_TYPE] = " PMU Type %"PRId64"\n",
  717. [INTEL_BTS_TIME_SHIFT] = " Time Shift %"PRIu64"\n",
  718. [INTEL_BTS_TIME_MULT] = " Time Muliplier %"PRIu64"\n",
  719. [INTEL_BTS_TIME_ZERO] = " Time Zero %"PRIu64"\n",
  720. [INTEL_BTS_CAP_USER_TIME_ZERO] = " Cap Time Zero %"PRId64"\n",
  721. [INTEL_BTS_SNAPSHOT_MODE] = " Snapshot mode %"PRId64"\n",
  722. };
  723. static void intel_bts_print_info(u64 *arr, int start, int finish)
  724. {
  725. int i;
  726. if (!dump_trace)
  727. return;
  728. for (i = start; i <= finish; i++)
  729. fprintf(stdout, intel_bts_info_fmts[i], arr[i]);
  730. }
  731. u64 intel_bts_auxtrace_info_priv[INTEL_BTS_AUXTRACE_PRIV_SIZE];
  732. int intel_bts_process_auxtrace_info(union perf_event *event,
  733. struct perf_session *session)
  734. {
  735. struct auxtrace_info_event *auxtrace_info = &event->auxtrace_info;
  736. size_t min_sz = sizeof(u64) * INTEL_BTS_SNAPSHOT_MODE;
  737. struct intel_bts *bts;
  738. int err;
  739. if (auxtrace_info->header.size < sizeof(struct auxtrace_info_event) +
  740. min_sz)
  741. return -EINVAL;
  742. bts = zalloc(sizeof(struct intel_bts));
  743. if (!bts)
  744. return -ENOMEM;
  745. err = auxtrace_queues__init(&bts->queues);
  746. if (err)
  747. goto err_free;
  748. bts->session = session;
  749. bts->machine = &session->machines.host; /* No kvm support */
  750. bts->auxtrace_type = auxtrace_info->type;
  751. bts->pmu_type = auxtrace_info->priv[INTEL_BTS_PMU_TYPE];
  752. bts->tc.time_shift = auxtrace_info->priv[INTEL_BTS_TIME_SHIFT];
  753. bts->tc.time_mult = auxtrace_info->priv[INTEL_BTS_TIME_MULT];
  754. bts->tc.time_zero = auxtrace_info->priv[INTEL_BTS_TIME_ZERO];
  755. bts->cap_user_time_zero =
  756. auxtrace_info->priv[INTEL_BTS_CAP_USER_TIME_ZERO];
  757. bts->snapshot_mode = auxtrace_info->priv[INTEL_BTS_SNAPSHOT_MODE];
  758. bts->sampling_mode = false;
  759. bts->auxtrace.process_event = intel_bts_process_event;
  760. bts->auxtrace.process_auxtrace_event = intel_bts_process_auxtrace_event;
  761. bts->auxtrace.flush_events = intel_bts_flush;
  762. bts->auxtrace.free_events = intel_bts_free_events;
  763. bts->auxtrace.free = intel_bts_free;
  764. session->auxtrace = &bts->auxtrace;
  765. intel_bts_print_info(&auxtrace_info->priv[0], INTEL_BTS_PMU_TYPE,
  766. INTEL_BTS_SNAPSHOT_MODE);
  767. if (dump_trace)
  768. return 0;
  769. if (session->itrace_synth_opts && session->itrace_synth_opts->set) {
  770. bts->synth_opts = *session->itrace_synth_opts;
  771. } else {
  772. itrace_synth_opts__set_default(&bts->synth_opts);
  773. if (session->itrace_synth_opts)
  774. bts->synth_opts.thread_stack =
  775. session->itrace_synth_opts->thread_stack;
  776. }
  777. if (bts->synth_opts.calls)
  778. bts->branches_filter |= PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC |
  779. PERF_IP_FLAG_TRACE_END;
  780. if (bts->synth_opts.returns)
  781. bts->branches_filter |= PERF_IP_FLAG_RETURN |
  782. PERF_IP_FLAG_TRACE_BEGIN;
  783. err = intel_bts_synth_events(bts, session);
  784. if (err)
  785. goto err_free_queues;
  786. err = auxtrace_queues__process_index(&bts->queues, session);
  787. if (err)
  788. goto err_free_queues;
  789. if (bts->queues.populated)
  790. bts->data_queued = true;
  791. return 0;
  792. err_free_queues:
  793. auxtrace_queues__free(&bts->queues);
  794. session->auxtrace = NULL;
  795. err_free:
  796. free(bts);
  797. return err;
  798. }