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