intel-bts.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  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, machine->symbol_filter);
  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. {
  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 (filter && !(filter & btsq->sample_flags))
  382. continue;
  383. err = intel_bts_synth_branch_sample(btsq, branch);
  384. if (err)
  385. break;
  386. }
  387. return err;
  388. }
  389. static int intel_bts_process_queue(struct intel_bts_queue *btsq, u64 *timestamp)
  390. {
  391. struct auxtrace_buffer *buffer = btsq->buffer, *old_buffer = buffer;
  392. struct auxtrace_queue *queue;
  393. struct thread *thread;
  394. int err;
  395. if (btsq->done)
  396. return 1;
  397. if (btsq->pid == -1) {
  398. thread = machine__find_thread(btsq->bts->machine, -1,
  399. btsq->tid);
  400. if (thread)
  401. btsq->pid = thread->pid_;
  402. } else {
  403. thread = machine__findnew_thread(btsq->bts->machine, btsq->pid,
  404. btsq->tid);
  405. }
  406. queue = &btsq->bts->queues.queue_array[btsq->queue_nr];
  407. if (!buffer)
  408. buffer = auxtrace_buffer__next(queue, NULL);
  409. if (!buffer) {
  410. if (!btsq->bts->sampling_mode)
  411. btsq->done = 1;
  412. err = 1;
  413. goto out_put;
  414. }
  415. /* Currently there is no support for split buffers */
  416. if (buffer->consecutive) {
  417. err = -EINVAL;
  418. goto out_put;
  419. }
  420. if (!buffer->data) {
  421. int fd = perf_data_file__fd(btsq->bts->session->file);
  422. buffer->data = auxtrace_buffer__get_data(buffer, fd);
  423. if (!buffer->data) {
  424. err = -ENOMEM;
  425. goto out_put;
  426. }
  427. }
  428. if (btsq->bts->snapshot_mode && !buffer->consecutive &&
  429. intel_bts_do_fix_overlap(queue, buffer)) {
  430. err = -ENOMEM;
  431. goto out_put;
  432. }
  433. if (!btsq->bts->synth_opts.callchain && thread &&
  434. (!old_buffer || btsq->bts->sampling_mode ||
  435. (btsq->bts->snapshot_mode && !buffer->consecutive)))
  436. thread_stack__set_trace_nr(thread, buffer->buffer_nr + 1);
  437. err = intel_bts_process_buffer(btsq, buffer);
  438. auxtrace_buffer__drop_data(buffer);
  439. btsq->buffer = auxtrace_buffer__next(queue, buffer);
  440. if (btsq->buffer) {
  441. if (timestamp)
  442. *timestamp = btsq->buffer->reference;
  443. } else {
  444. if (!btsq->bts->sampling_mode)
  445. btsq->done = 1;
  446. }
  447. out_put:
  448. thread__put(thread);
  449. return err;
  450. }
  451. static int intel_bts_flush_queue(struct intel_bts_queue *btsq)
  452. {
  453. u64 ts = 0;
  454. int ret;
  455. while (1) {
  456. ret = intel_bts_process_queue(btsq, &ts);
  457. if (ret < 0)
  458. return ret;
  459. if (ret)
  460. break;
  461. }
  462. return 0;
  463. }
  464. static int intel_bts_process_tid_exit(struct intel_bts *bts, pid_t tid)
  465. {
  466. struct auxtrace_queues *queues = &bts->queues;
  467. unsigned int i;
  468. for (i = 0; i < queues->nr_queues; i++) {
  469. struct auxtrace_queue *queue = &bts->queues.queue_array[i];
  470. struct intel_bts_queue *btsq = queue->priv;
  471. if (btsq && btsq->tid == tid)
  472. return intel_bts_flush_queue(btsq);
  473. }
  474. return 0;
  475. }
  476. static int intel_bts_process_queues(struct intel_bts *bts, u64 timestamp)
  477. {
  478. while (1) {
  479. unsigned int queue_nr;
  480. struct auxtrace_queue *queue;
  481. struct intel_bts_queue *btsq;
  482. u64 ts = 0;
  483. int ret;
  484. if (!bts->heap.heap_cnt)
  485. return 0;
  486. if (bts->heap.heap_array[0].ordinal > timestamp)
  487. return 0;
  488. queue_nr = bts->heap.heap_array[0].queue_nr;
  489. queue = &bts->queues.queue_array[queue_nr];
  490. btsq = queue->priv;
  491. auxtrace_heap__pop(&bts->heap);
  492. ret = intel_bts_process_queue(btsq, &ts);
  493. if (ret < 0) {
  494. auxtrace_heap__add(&bts->heap, queue_nr, ts);
  495. return ret;
  496. }
  497. if (!ret) {
  498. ret = auxtrace_heap__add(&bts->heap, queue_nr, ts);
  499. if (ret < 0)
  500. return ret;
  501. } else {
  502. btsq->on_heap = false;
  503. }
  504. }
  505. return 0;
  506. }
  507. static int intel_bts_process_event(struct perf_session *session,
  508. union perf_event *event,
  509. struct perf_sample *sample,
  510. struct perf_tool *tool)
  511. {
  512. struct intel_bts *bts = container_of(session->auxtrace, struct intel_bts,
  513. auxtrace);
  514. u64 timestamp;
  515. int err;
  516. if (dump_trace)
  517. return 0;
  518. if (!tool->ordered_events) {
  519. pr_err("Intel BTS requires ordered events\n");
  520. return -EINVAL;
  521. }
  522. if (sample->time && sample->time != (u64)-1)
  523. timestamp = perf_time_to_tsc(sample->time, &bts->tc);
  524. else
  525. timestamp = 0;
  526. err = intel_bts_update_queues(bts);
  527. if (err)
  528. return err;
  529. err = intel_bts_process_queues(bts, timestamp);
  530. if (err)
  531. return err;
  532. if (event->header.type == PERF_RECORD_EXIT) {
  533. err = intel_bts_process_tid_exit(bts, event->fork.tid);
  534. if (err)
  535. return err;
  536. }
  537. if (event->header.type == PERF_RECORD_AUX &&
  538. (event->aux.flags & PERF_AUX_FLAG_TRUNCATED) &&
  539. bts->synth_opts.errors)
  540. err = intel_bts_lost(bts, sample);
  541. return err;
  542. }
  543. static int intel_bts_process_auxtrace_event(struct perf_session *session,
  544. union perf_event *event,
  545. struct perf_tool *tool __maybe_unused)
  546. {
  547. struct intel_bts *bts = container_of(session->auxtrace, struct intel_bts,
  548. auxtrace);
  549. if (bts->sampling_mode)
  550. return 0;
  551. if (!bts->data_queued) {
  552. struct auxtrace_buffer *buffer;
  553. off_t data_offset;
  554. int fd = perf_data_file__fd(session->file);
  555. int err;
  556. if (perf_data_file__is_pipe(session->file)) {
  557. data_offset = 0;
  558. } else {
  559. data_offset = lseek(fd, 0, SEEK_CUR);
  560. if (data_offset == -1)
  561. return -errno;
  562. }
  563. err = auxtrace_queues__add_event(&bts->queues, session, event,
  564. data_offset, &buffer);
  565. if (err)
  566. return err;
  567. /* Dump here now we have copied a piped trace out of the pipe */
  568. if (dump_trace) {
  569. if (auxtrace_buffer__get_data(buffer, fd)) {
  570. intel_bts_dump_event(bts, buffer->data,
  571. buffer->size);
  572. auxtrace_buffer__put_data(buffer);
  573. }
  574. }
  575. }
  576. return 0;
  577. }
  578. static int intel_bts_flush(struct perf_session *session,
  579. struct perf_tool *tool __maybe_unused)
  580. {
  581. struct intel_bts *bts = container_of(session->auxtrace, struct intel_bts,
  582. auxtrace);
  583. int ret;
  584. if (dump_trace || bts->sampling_mode)
  585. return 0;
  586. if (!tool->ordered_events)
  587. return -EINVAL;
  588. ret = intel_bts_update_queues(bts);
  589. if (ret < 0)
  590. return ret;
  591. return intel_bts_process_queues(bts, MAX_TIMESTAMP);
  592. }
  593. static void intel_bts_free_queue(void *priv)
  594. {
  595. struct intel_bts_queue *btsq = priv;
  596. if (!btsq)
  597. return;
  598. free(btsq);
  599. }
  600. static void intel_bts_free_events(struct perf_session *session)
  601. {
  602. struct intel_bts *bts = container_of(session->auxtrace, struct intel_bts,
  603. auxtrace);
  604. struct auxtrace_queues *queues = &bts->queues;
  605. unsigned int i;
  606. for (i = 0; i < queues->nr_queues; i++) {
  607. intel_bts_free_queue(queues->queue_array[i].priv);
  608. queues->queue_array[i].priv = NULL;
  609. }
  610. auxtrace_queues__free(queues);
  611. }
  612. static void intel_bts_free(struct perf_session *session)
  613. {
  614. struct intel_bts *bts = container_of(session->auxtrace, struct intel_bts,
  615. auxtrace);
  616. auxtrace_heap__free(&bts->heap);
  617. intel_bts_free_events(session);
  618. session->auxtrace = NULL;
  619. free(bts);
  620. }
  621. struct intel_bts_synth {
  622. struct perf_tool dummy_tool;
  623. struct perf_session *session;
  624. };
  625. static int intel_bts_event_synth(struct perf_tool *tool,
  626. union perf_event *event,
  627. struct perf_sample *sample __maybe_unused,
  628. struct machine *machine __maybe_unused)
  629. {
  630. struct intel_bts_synth *intel_bts_synth =
  631. container_of(tool, struct intel_bts_synth, dummy_tool);
  632. return perf_session__deliver_synth_event(intel_bts_synth->session,
  633. event, NULL);
  634. }
  635. static int intel_bts_synth_event(struct perf_session *session,
  636. struct perf_event_attr *attr, u64 id)
  637. {
  638. struct intel_bts_synth intel_bts_synth;
  639. memset(&intel_bts_synth, 0, sizeof(struct intel_bts_synth));
  640. intel_bts_synth.session = session;
  641. return perf_event__synthesize_attr(&intel_bts_synth.dummy_tool, attr, 1,
  642. &id, intel_bts_event_synth);
  643. }
  644. static int intel_bts_synth_events(struct intel_bts *bts,
  645. struct perf_session *session)
  646. {
  647. struct perf_evlist *evlist = session->evlist;
  648. struct perf_evsel *evsel;
  649. struct perf_event_attr attr;
  650. bool found = false;
  651. u64 id;
  652. int err;
  653. evlist__for_each(evlist, evsel) {
  654. if (evsel->attr.type == bts->pmu_type && evsel->ids) {
  655. found = true;
  656. break;
  657. }
  658. }
  659. if (!found) {
  660. pr_debug("There are no selected events with Intel BTS data\n");
  661. return 0;
  662. }
  663. memset(&attr, 0, sizeof(struct perf_event_attr));
  664. attr.size = sizeof(struct perf_event_attr);
  665. attr.type = PERF_TYPE_HARDWARE;
  666. attr.sample_type = evsel->attr.sample_type & PERF_SAMPLE_MASK;
  667. attr.sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID |
  668. PERF_SAMPLE_PERIOD;
  669. attr.sample_type &= ~(u64)PERF_SAMPLE_TIME;
  670. attr.sample_type &= ~(u64)PERF_SAMPLE_CPU;
  671. attr.exclude_user = evsel->attr.exclude_user;
  672. attr.exclude_kernel = evsel->attr.exclude_kernel;
  673. attr.exclude_hv = evsel->attr.exclude_hv;
  674. attr.exclude_host = evsel->attr.exclude_host;
  675. attr.exclude_guest = evsel->attr.exclude_guest;
  676. attr.sample_id_all = evsel->attr.sample_id_all;
  677. attr.read_format = evsel->attr.read_format;
  678. id = evsel->id[0] + 1000000000;
  679. if (!id)
  680. id = 1;
  681. if (bts->synth_opts.branches) {
  682. attr.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS;
  683. attr.sample_period = 1;
  684. attr.sample_type |= PERF_SAMPLE_ADDR;
  685. pr_debug("Synthesizing 'branches' event with id %" PRIu64 " sample type %#" PRIx64 "\n",
  686. id, (u64)attr.sample_type);
  687. err = intel_bts_synth_event(session, &attr, id);
  688. if (err) {
  689. pr_err("%s: failed to synthesize 'branches' event type\n",
  690. __func__);
  691. return err;
  692. }
  693. bts->sample_branches = true;
  694. bts->branches_sample_type = attr.sample_type;
  695. bts->branches_id = id;
  696. /*
  697. * We only use sample types from PERF_SAMPLE_MASK so we can use
  698. * __perf_evsel__sample_size() here.
  699. */
  700. bts->branches_event_size = sizeof(struct sample_event) +
  701. __perf_evsel__sample_size(attr.sample_type);
  702. }
  703. bts->synth_needs_swap = evsel->needs_swap;
  704. return 0;
  705. }
  706. static const char * const intel_bts_info_fmts[] = {
  707. [INTEL_BTS_PMU_TYPE] = " PMU Type %"PRId64"\n",
  708. [INTEL_BTS_TIME_SHIFT] = " Time Shift %"PRIu64"\n",
  709. [INTEL_BTS_TIME_MULT] = " Time Muliplier %"PRIu64"\n",
  710. [INTEL_BTS_TIME_ZERO] = " Time Zero %"PRIu64"\n",
  711. [INTEL_BTS_CAP_USER_TIME_ZERO] = " Cap Time Zero %"PRId64"\n",
  712. [INTEL_BTS_SNAPSHOT_MODE] = " Snapshot mode %"PRId64"\n",
  713. };
  714. static void intel_bts_print_info(u64 *arr, int start, int finish)
  715. {
  716. int i;
  717. if (!dump_trace)
  718. return;
  719. for (i = start; i <= finish; i++)
  720. fprintf(stdout, intel_bts_info_fmts[i], arr[i]);
  721. }
  722. u64 intel_bts_auxtrace_info_priv[INTEL_BTS_AUXTRACE_PRIV_SIZE];
  723. int intel_bts_process_auxtrace_info(union perf_event *event,
  724. struct perf_session *session)
  725. {
  726. struct auxtrace_info_event *auxtrace_info = &event->auxtrace_info;
  727. size_t min_sz = sizeof(u64) * INTEL_BTS_SNAPSHOT_MODE;
  728. struct intel_bts *bts;
  729. int err;
  730. if (auxtrace_info->header.size < sizeof(struct auxtrace_info_event) +
  731. min_sz)
  732. return -EINVAL;
  733. bts = zalloc(sizeof(struct intel_bts));
  734. if (!bts)
  735. return -ENOMEM;
  736. err = auxtrace_queues__init(&bts->queues);
  737. if (err)
  738. goto err_free;
  739. bts->session = session;
  740. bts->machine = &session->machines.host; /* No kvm support */
  741. bts->auxtrace_type = auxtrace_info->type;
  742. bts->pmu_type = auxtrace_info->priv[INTEL_BTS_PMU_TYPE];
  743. bts->tc.time_shift = auxtrace_info->priv[INTEL_BTS_TIME_SHIFT];
  744. bts->tc.time_mult = auxtrace_info->priv[INTEL_BTS_TIME_MULT];
  745. bts->tc.time_zero = auxtrace_info->priv[INTEL_BTS_TIME_ZERO];
  746. bts->cap_user_time_zero =
  747. auxtrace_info->priv[INTEL_BTS_CAP_USER_TIME_ZERO];
  748. bts->snapshot_mode = auxtrace_info->priv[INTEL_BTS_SNAPSHOT_MODE];
  749. bts->sampling_mode = false;
  750. bts->auxtrace.process_event = intel_bts_process_event;
  751. bts->auxtrace.process_auxtrace_event = intel_bts_process_auxtrace_event;
  752. bts->auxtrace.flush_events = intel_bts_flush;
  753. bts->auxtrace.free_events = intel_bts_free_events;
  754. bts->auxtrace.free = intel_bts_free;
  755. session->auxtrace = &bts->auxtrace;
  756. intel_bts_print_info(&auxtrace_info->priv[0], INTEL_BTS_PMU_TYPE,
  757. INTEL_BTS_SNAPSHOT_MODE);
  758. if (dump_trace)
  759. return 0;
  760. if (session->itrace_synth_opts && session->itrace_synth_opts->set)
  761. bts->synth_opts = *session->itrace_synth_opts;
  762. else
  763. itrace_synth_opts__set_default(&bts->synth_opts);
  764. if (bts->synth_opts.calls)
  765. bts->branches_filter |= PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC |
  766. PERF_IP_FLAG_TRACE_END;
  767. if (bts->synth_opts.returns)
  768. bts->branches_filter |= PERF_IP_FLAG_RETURN |
  769. PERF_IP_FLAG_TRACE_BEGIN;
  770. err = intel_bts_synth_events(bts, session);
  771. if (err)
  772. goto err_free_queues;
  773. err = auxtrace_queues__process_index(&bts->queues, session);
  774. if (err)
  775. goto err_free_queues;
  776. if (bts->queues.populated)
  777. bts->data_queued = true;
  778. return 0;
  779. err_free_queues:
  780. auxtrace_queues__free(&bts->queues);
  781. session->auxtrace = NULL;
  782. err_free:
  783. free(bts);
  784. return err;
  785. }