intel-pt.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  1. /*
  2. * intel_pt.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 <stdbool.h>
  16. #include <linux/kernel.h>
  17. #include <linux/types.h>
  18. #include <linux/bitops.h>
  19. #include <linux/log2.h>
  20. #include <cpuid.h>
  21. #include "../../perf.h"
  22. #include "../../util/session.h"
  23. #include "../../util/event.h"
  24. #include "../../util/evlist.h"
  25. #include "../../util/evsel.h"
  26. #include "../../util/cpumap.h"
  27. #include "../../util/parse-options.h"
  28. #include "../../util/parse-events.h"
  29. #include "../../util/pmu.h"
  30. #include "../../util/debug.h"
  31. #include "../../util/auxtrace.h"
  32. #include "../../util/tsc.h"
  33. #include "../../util/intel-pt.h"
  34. #define KiB(x) ((x) * 1024)
  35. #define MiB(x) ((x) * 1024 * 1024)
  36. #define KiB_MASK(x) (KiB(x) - 1)
  37. #define MiB_MASK(x) (MiB(x) - 1)
  38. #define INTEL_PT_DEFAULT_SAMPLE_SIZE KiB(4)
  39. #define INTEL_PT_MAX_SAMPLE_SIZE KiB(60)
  40. #define INTEL_PT_PSB_PERIOD_NEAR 256
  41. struct intel_pt_snapshot_ref {
  42. void *ref_buf;
  43. size_t ref_offset;
  44. bool wrapped;
  45. };
  46. struct intel_pt_recording {
  47. struct auxtrace_record itr;
  48. struct perf_pmu *intel_pt_pmu;
  49. int have_sched_switch;
  50. struct perf_evlist *evlist;
  51. bool snapshot_mode;
  52. bool snapshot_init_done;
  53. size_t snapshot_size;
  54. size_t snapshot_ref_buf_size;
  55. int snapshot_ref_cnt;
  56. struct intel_pt_snapshot_ref *snapshot_refs;
  57. };
  58. static int intel_pt_parse_terms_with_default(struct list_head *formats,
  59. const char *str,
  60. u64 *config)
  61. {
  62. struct list_head *terms;
  63. struct perf_event_attr attr = { .size = 0, };
  64. int err;
  65. terms = malloc(sizeof(struct list_head));
  66. if (!terms)
  67. return -ENOMEM;
  68. INIT_LIST_HEAD(terms);
  69. err = parse_events_terms(terms, str);
  70. if (err)
  71. goto out_free;
  72. attr.config = *config;
  73. err = perf_pmu__config_terms(formats, &attr, terms, true, NULL);
  74. if (err)
  75. goto out_free;
  76. *config = attr.config;
  77. out_free:
  78. parse_events__free_terms(terms);
  79. return err;
  80. }
  81. static int intel_pt_parse_terms(struct list_head *formats, const char *str,
  82. u64 *config)
  83. {
  84. *config = 0;
  85. return intel_pt_parse_terms_with_default(formats, str, config);
  86. }
  87. static u64 intel_pt_masked_bits(u64 mask, u64 bits)
  88. {
  89. const u64 top_bit = 1ULL << 63;
  90. u64 res = 0;
  91. int i;
  92. for (i = 0; i < 64; i++) {
  93. if (mask & top_bit) {
  94. res <<= 1;
  95. if (bits & top_bit)
  96. res |= 1;
  97. }
  98. mask <<= 1;
  99. bits <<= 1;
  100. }
  101. return res;
  102. }
  103. static int intel_pt_read_config(struct perf_pmu *intel_pt_pmu, const char *str,
  104. struct perf_evlist *evlist, u64 *res)
  105. {
  106. struct perf_evsel *evsel;
  107. u64 mask;
  108. *res = 0;
  109. mask = perf_pmu__format_bits(&intel_pt_pmu->format, str);
  110. if (!mask)
  111. return -EINVAL;
  112. evlist__for_each(evlist, evsel) {
  113. if (evsel->attr.type == intel_pt_pmu->type) {
  114. *res = intel_pt_masked_bits(mask, evsel->attr.config);
  115. return 0;
  116. }
  117. }
  118. return -EINVAL;
  119. }
  120. static size_t intel_pt_psb_period(struct perf_pmu *intel_pt_pmu,
  121. struct perf_evlist *evlist)
  122. {
  123. u64 val;
  124. int err, topa_multiple_entries;
  125. size_t psb_period;
  126. if (perf_pmu__scan_file(intel_pt_pmu, "caps/topa_multiple_entries",
  127. "%d", &topa_multiple_entries) != 1)
  128. topa_multiple_entries = 0;
  129. /*
  130. * Use caps/topa_multiple_entries to indicate early hardware that had
  131. * extra frequent PSBs.
  132. */
  133. if (!topa_multiple_entries) {
  134. psb_period = 256;
  135. goto out;
  136. }
  137. err = intel_pt_read_config(intel_pt_pmu, "psb_period", evlist, &val);
  138. if (err)
  139. val = 0;
  140. psb_period = 1 << (val + 11);
  141. out:
  142. pr_debug2("%s psb_period %zu\n", intel_pt_pmu->name, psb_period);
  143. return psb_period;
  144. }
  145. static int intel_pt_pick_bit(int bits, int target)
  146. {
  147. int pos, pick = -1;
  148. for (pos = 0; bits; bits >>= 1, pos++) {
  149. if (bits & 1) {
  150. if (pos <= target || pick < 0)
  151. pick = pos;
  152. if (pos >= target)
  153. break;
  154. }
  155. }
  156. return pick;
  157. }
  158. static u64 intel_pt_default_config(struct perf_pmu *intel_pt_pmu)
  159. {
  160. char buf[256];
  161. int psb_cyc, psb_periods, psb_period;
  162. int pos = 0;
  163. u64 config;
  164. pos += scnprintf(buf + pos, sizeof(buf) - pos, "tsc");
  165. if (perf_pmu__scan_file(intel_pt_pmu, "caps/psb_cyc", "%d",
  166. &psb_cyc) != 1)
  167. psb_cyc = 1;
  168. if (psb_cyc) {
  169. if (perf_pmu__scan_file(intel_pt_pmu, "caps/psb_periods", "%x",
  170. &psb_periods) != 1)
  171. psb_periods = 0;
  172. if (psb_periods) {
  173. psb_period = intel_pt_pick_bit(psb_periods, 3);
  174. pos += scnprintf(buf + pos, sizeof(buf) - pos,
  175. ",psb_period=%d", psb_period);
  176. }
  177. }
  178. pr_debug2("%s default config: %s\n", intel_pt_pmu->name, buf);
  179. intel_pt_parse_terms(&intel_pt_pmu->format, buf, &config);
  180. return config;
  181. }
  182. static int intel_pt_parse_snapshot_options(struct auxtrace_record *itr,
  183. struct record_opts *opts,
  184. const char *str)
  185. {
  186. struct intel_pt_recording *ptr =
  187. container_of(itr, struct intel_pt_recording, itr);
  188. unsigned long long snapshot_size = 0;
  189. char *endptr;
  190. if (str) {
  191. snapshot_size = strtoull(str, &endptr, 0);
  192. if (*endptr || snapshot_size > SIZE_MAX)
  193. return -1;
  194. }
  195. opts->auxtrace_snapshot_mode = true;
  196. opts->auxtrace_snapshot_size = snapshot_size;
  197. ptr->snapshot_size = snapshot_size;
  198. return 0;
  199. }
  200. struct perf_event_attr *
  201. intel_pt_pmu_default_config(struct perf_pmu *intel_pt_pmu)
  202. {
  203. struct perf_event_attr *attr;
  204. attr = zalloc(sizeof(struct perf_event_attr));
  205. if (!attr)
  206. return NULL;
  207. attr->config = intel_pt_default_config(intel_pt_pmu);
  208. intel_pt_pmu->selectable = true;
  209. return attr;
  210. }
  211. static size_t intel_pt_info_priv_size(struct auxtrace_record *itr __maybe_unused)
  212. {
  213. return INTEL_PT_AUXTRACE_PRIV_SIZE;
  214. }
  215. static void intel_pt_tsc_ctc_ratio(u32 *n, u32 *d)
  216. {
  217. unsigned int eax = 0, ebx = 0, ecx = 0, edx = 0;
  218. __get_cpuid(0x15, &eax, &ebx, &ecx, &edx);
  219. *n = ebx;
  220. *d = eax;
  221. }
  222. static int intel_pt_info_fill(struct auxtrace_record *itr,
  223. struct perf_session *session,
  224. struct auxtrace_info_event *auxtrace_info,
  225. size_t priv_size)
  226. {
  227. struct intel_pt_recording *ptr =
  228. container_of(itr, struct intel_pt_recording, itr);
  229. struct perf_pmu *intel_pt_pmu = ptr->intel_pt_pmu;
  230. struct perf_event_mmap_page *pc;
  231. struct perf_tsc_conversion tc = { .time_mult = 0, };
  232. bool cap_user_time_zero = false, per_cpu_mmaps;
  233. u64 tsc_bit, mtc_bit, mtc_freq_bits, cyc_bit, noretcomp_bit;
  234. u32 tsc_ctc_ratio_n, tsc_ctc_ratio_d;
  235. int err;
  236. if (priv_size != INTEL_PT_AUXTRACE_PRIV_SIZE)
  237. return -EINVAL;
  238. intel_pt_parse_terms(&intel_pt_pmu->format, "tsc", &tsc_bit);
  239. intel_pt_parse_terms(&intel_pt_pmu->format, "noretcomp",
  240. &noretcomp_bit);
  241. intel_pt_parse_terms(&intel_pt_pmu->format, "mtc", &mtc_bit);
  242. mtc_freq_bits = perf_pmu__format_bits(&intel_pt_pmu->format,
  243. "mtc_period");
  244. intel_pt_parse_terms(&intel_pt_pmu->format, "cyc", &cyc_bit);
  245. intel_pt_tsc_ctc_ratio(&tsc_ctc_ratio_n, &tsc_ctc_ratio_d);
  246. if (!session->evlist->nr_mmaps)
  247. return -EINVAL;
  248. pc = session->evlist->mmap[0].base;
  249. if (pc) {
  250. err = perf_read_tsc_conversion(pc, &tc);
  251. if (err) {
  252. if (err != -EOPNOTSUPP)
  253. return err;
  254. } else {
  255. cap_user_time_zero = tc.time_mult != 0;
  256. }
  257. if (!cap_user_time_zero)
  258. ui__warning("Intel Processor Trace: TSC not available\n");
  259. }
  260. per_cpu_mmaps = !cpu_map__empty(session->evlist->cpus);
  261. auxtrace_info->type = PERF_AUXTRACE_INTEL_PT;
  262. auxtrace_info->priv[INTEL_PT_PMU_TYPE] = intel_pt_pmu->type;
  263. auxtrace_info->priv[INTEL_PT_TIME_SHIFT] = tc.time_shift;
  264. auxtrace_info->priv[INTEL_PT_TIME_MULT] = tc.time_mult;
  265. auxtrace_info->priv[INTEL_PT_TIME_ZERO] = tc.time_zero;
  266. auxtrace_info->priv[INTEL_PT_CAP_USER_TIME_ZERO] = cap_user_time_zero;
  267. auxtrace_info->priv[INTEL_PT_TSC_BIT] = tsc_bit;
  268. auxtrace_info->priv[INTEL_PT_NORETCOMP_BIT] = noretcomp_bit;
  269. auxtrace_info->priv[INTEL_PT_HAVE_SCHED_SWITCH] = ptr->have_sched_switch;
  270. auxtrace_info->priv[INTEL_PT_SNAPSHOT_MODE] = ptr->snapshot_mode;
  271. auxtrace_info->priv[INTEL_PT_PER_CPU_MMAPS] = per_cpu_mmaps;
  272. auxtrace_info->priv[INTEL_PT_MTC_BIT] = mtc_bit;
  273. auxtrace_info->priv[INTEL_PT_MTC_FREQ_BITS] = mtc_freq_bits;
  274. auxtrace_info->priv[INTEL_PT_TSC_CTC_N] = tsc_ctc_ratio_n;
  275. auxtrace_info->priv[INTEL_PT_TSC_CTC_D] = tsc_ctc_ratio_d;
  276. auxtrace_info->priv[INTEL_PT_CYC_BIT] = cyc_bit;
  277. return 0;
  278. }
  279. static int intel_pt_track_switches(struct perf_evlist *evlist)
  280. {
  281. const char *sched_switch = "sched:sched_switch";
  282. struct perf_evsel *evsel;
  283. int err;
  284. if (!perf_evlist__can_select_event(evlist, sched_switch))
  285. return -EPERM;
  286. err = parse_events(evlist, sched_switch, NULL);
  287. if (err) {
  288. pr_debug2("%s: failed to parse %s, error %d\n",
  289. __func__, sched_switch, err);
  290. return err;
  291. }
  292. evsel = perf_evlist__last(evlist);
  293. perf_evsel__set_sample_bit(evsel, CPU);
  294. perf_evsel__set_sample_bit(evsel, TIME);
  295. evsel->system_wide = true;
  296. evsel->no_aux_samples = true;
  297. evsel->immediate = true;
  298. return 0;
  299. }
  300. static void intel_pt_valid_str(char *str, size_t len, u64 valid)
  301. {
  302. unsigned int val, last = 0, state = 1;
  303. int p = 0;
  304. str[0] = '\0';
  305. for (val = 0; val <= 64; val++, valid >>= 1) {
  306. if (valid & 1) {
  307. last = val;
  308. switch (state) {
  309. case 0:
  310. p += scnprintf(str + p, len - p, ",");
  311. /* Fall through */
  312. case 1:
  313. p += scnprintf(str + p, len - p, "%u", val);
  314. state = 2;
  315. break;
  316. case 2:
  317. state = 3;
  318. break;
  319. case 3:
  320. state = 4;
  321. break;
  322. default:
  323. break;
  324. }
  325. } else {
  326. switch (state) {
  327. case 3:
  328. p += scnprintf(str + p, len - p, ",%u", last);
  329. state = 0;
  330. break;
  331. case 4:
  332. p += scnprintf(str + p, len - p, "-%u", last);
  333. state = 0;
  334. break;
  335. default:
  336. break;
  337. }
  338. if (state != 1)
  339. state = 0;
  340. }
  341. }
  342. }
  343. static int intel_pt_val_config_term(struct perf_pmu *intel_pt_pmu,
  344. const char *caps, const char *name,
  345. const char *supported, u64 config)
  346. {
  347. char valid_str[256];
  348. unsigned int shift;
  349. unsigned long long valid;
  350. u64 bits;
  351. int ok;
  352. if (perf_pmu__scan_file(intel_pt_pmu, caps, "%llx", &valid) != 1)
  353. valid = 0;
  354. if (supported &&
  355. perf_pmu__scan_file(intel_pt_pmu, supported, "%d", &ok) == 1 && !ok)
  356. valid = 0;
  357. valid |= 1;
  358. bits = perf_pmu__format_bits(&intel_pt_pmu->format, name);
  359. config &= bits;
  360. for (shift = 0; bits && !(bits & 1); shift++)
  361. bits >>= 1;
  362. config >>= shift;
  363. if (config > 63)
  364. goto out_err;
  365. if (valid & (1 << config))
  366. return 0;
  367. out_err:
  368. intel_pt_valid_str(valid_str, sizeof(valid_str), valid);
  369. pr_err("Invalid %s for %s. Valid values are: %s\n",
  370. name, INTEL_PT_PMU_NAME, valid_str);
  371. return -EINVAL;
  372. }
  373. static int intel_pt_validate_config(struct perf_pmu *intel_pt_pmu,
  374. struct perf_evsel *evsel)
  375. {
  376. if (!evsel)
  377. return 0;
  378. return intel_pt_val_config_term(intel_pt_pmu, "caps/psb_periods",
  379. "psb_period", "caps/psb_cyc",
  380. evsel->attr.config);
  381. }
  382. static int intel_pt_recording_options(struct auxtrace_record *itr,
  383. struct perf_evlist *evlist,
  384. struct record_opts *opts)
  385. {
  386. struct intel_pt_recording *ptr =
  387. container_of(itr, struct intel_pt_recording, itr);
  388. struct perf_pmu *intel_pt_pmu = ptr->intel_pt_pmu;
  389. bool have_timing_info;
  390. struct perf_evsel *evsel, *intel_pt_evsel = NULL;
  391. const struct cpu_map *cpus = evlist->cpus;
  392. bool privileged = geteuid() == 0 || perf_event_paranoid() < 0;
  393. u64 tsc_bit;
  394. int err;
  395. ptr->evlist = evlist;
  396. ptr->snapshot_mode = opts->auxtrace_snapshot_mode;
  397. evlist__for_each(evlist, evsel) {
  398. if (evsel->attr.type == intel_pt_pmu->type) {
  399. if (intel_pt_evsel) {
  400. pr_err("There may be only one " INTEL_PT_PMU_NAME " event\n");
  401. return -EINVAL;
  402. }
  403. evsel->attr.freq = 0;
  404. evsel->attr.sample_period = 1;
  405. intel_pt_evsel = evsel;
  406. opts->full_auxtrace = true;
  407. }
  408. }
  409. if (opts->auxtrace_snapshot_mode && !opts->full_auxtrace) {
  410. pr_err("Snapshot mode (-S option) requires " INTEL_PT_PMU_NAME " PMU event (-e " INTEL_PT_PMU_NAME ")\n");
  411. return -EINVAL;
  412. }
  413. if (opts->use_clockid) {
  414. pr_err("Cannot use clockid (-k option) with " INTEL_PT_PMU_NAME "\n");
  415. return -EINVAL;
  416. }
  417. if (!opts->full_auxtrace)
  418. return 0;
  419. err = intel_pt_validate_config(intel_pt_pmu, intel_pt_evsel);
  420. if (err)
  421. return err;
  422. /* Set default sizes for snapshot mode */
  423. if (opts->auxtrace_snapshot_mode) {
  424. size_t psb_period = intel_pt_psb_period(intel_pt_pmu, evlist);
  425. if (!opts->auxtrace_snapshot_size && !opts->auxtrace_mmap_pages) {
  426. if (privileged) {
  427. opts->auxtrace_mmap_pages = MiB(4) / page_size;
  428. } else {
  429. opts->auxtrace_mmap_pages = KiB(128) / page_size;
  430. if (opts->mmap_pages == UINT_MAX)
  431. opts->mmap_pages = KiB(256) / page_size;
  432. }
  433. } else if (!opts->auxtrace_mmap_pages && !privileged &&
  434. opts->mmap_pages == UINT_MAX) {
  435. opts->mmap_pages = KiB(256) / page_size;
  436. }
  437. if (!opts->auxtrace_snapshot_size)
  438. opts->auxtrace_snapshot_size =
  439. opts->auxtrace_mmap_pages * (size_t)page_size;
  440. if (!opts->auxtrace_mmap_pages) {
  441. size_t sz = opts->auxtrace_snapshot_size;
  442. sz = round_up(sz, page_size) / page_size;
  443. opts->auxtrace_mmap_pages = roundup_pow_of_two(sz);
  444. }
  445. if (opts->auxtrace_snapshot_size >
  446. opts->auxtrace_mmap_pages * (size_t)page_size) {
  447. pr_err("Snapshot size %zu must not be greater than AUX area tracing mmap size %zu\n",
  448. opts->auxtrace_snapshot_size,
  449. opts->auxtrace_mmap_pages * (size_t)page_size);
  450. return -EINVAL;
  451. }
  452. if (!opts->auxtrace_snapshot_size || !opts->auxtrace_mmap_pages) {
  453. pr_err("Failed to calculate default snapshot size and/or AUX area tracing mmap pages\n");
  454. return -EINVAL;
  455. }
  456. pr_debug2("Intel PT snapshot size: %zu\n",
  457. opts->auxtrace_snapshot_size);
  458. if (psb_period &&
  459. opts->auxtrace_snapshot_size <= psb_period +
  460. INTEL_PT_PSB_PERIOD_NEAR)
  461. ui__warning("Intel PT snapshot size (%zu) may be too small for PSB period (%zu)\n",
  462. opts->auxtrace_snapshot_size, psb_period);
  463. }
  464. /* Set default sizes for full trace mode */
  465. if (opts->full_auxtrace && !opts->auxtrace_mmap_pages) {
  466. if (privileged) {
  467. opts->auxtrace_mmap_pages = MiB(4) / page_size;
  468. } else {
  469. opts->auxtrace_mmap_pages = KiB(128) / page_size;
  470. if (opts->mmap_pages == UINT_MAX)
  471. opts->mmap_pages = KiB(256) / page_size;
  472. }
  473. }
  474. /* Validate auxtrace_mmap_pages */
  475. if (opts->auxtrace_mmap_pages) {
  476. size_t sz = opts->auxtrace_mmap_pages * (size_t)page_size;
  477. size_t min_sz;
  478. if (opts->auxtrace_snapshot_mode)
  479. min_sz = KiB(4);
  480. else
  481. min_sz = KiB(8);
  482. if (sz < min_sz || !is_power_of_2(sz)) {
  483. pr_err("Invalid mmap size for Intel Processor Trace: must be at least %zuKiB and a power of 2\n",
  484. min_sz / 1024);
  485. return -EINVAL;
  486. }
  487. }
  488. intel_pt_parse_terms(&intel_pt_pmu->format, "tsc", &tsc_bit);
  489. if (opts->full_auxtrace && (intel_pt_evsel->attr.config & tsc_bit))
  490. have_timing_info = true;
  491. else
  492. have_timing_info = false;
  493. /*
  494. * Per-cpu recording needs sched_switch events to distinguish different
  495. * threads.
  496. */
  497. if (have_timing_info && !cpu_map__empty(cpus)) {
  498. err = intel_pt_track_switches(evlist);
  499. if (err == -EPERM)
  500. pr_debug2("Unable to select sched:sched_switch\n");
  501. else if (err)
  502. return err;
  503. else
  504. ptr->have_sched_switch = 1;
  505. }
  506. if (intel_pt_evsel) {
  507. /*
  508. * To obtain the auxtrace buffer file descriptor, the auxtrace
  509. * event must come first.
  510. */
  511. perf_evlist__to_front(evlist, intel_pt_evsel);
  512. /*
  513. * In the case of per-cpu mmaps, we need the CPU on the
  514. * AUX event.
  515. */
  516. if (!cpu_map__empty(cpus))
  517. perf_evsel__set_sample_bit(intel_pt_evsel, CPU);
  518. }
  519. /* Add dummy event to keep tracking */
  520. if (opts->full_auxtrace) {
  521. struct perf_evsel *tracking_evsel;
  522. err = parse_events(evlist, "dummy:u", NULL);
  523. if (err)
  524. return err;
  525. tracking_evsel = perf_evlist__last(evlist);
  526. perf_evlist__set_tracking_event(evlist, tracking_evsel);
  527. tracking_evsel->attr.freq = 0;
  528. tracking_evsel->attr.sample_period = 1;
  529. /* In per-cpu case, always need the time of mmap events etc */
  530. if (!cpu_map__empty(cpus))
  531. perf_evsel__set_sample_bit(tracking_evsel, TIME);
  532. }
  533. /*
  534. * Warn the user when we do not have enough information to decode i.e.
  535. * per-cpu with no sched_switch (except workload-only).
  536. */
  537. if (!ptr->have_sched_switch && !cpu_map__empty(cpus) &&
  538. !target__none(&opts->target))
  539. ui__warning("Intel Processor Trace decoding will not be possible except for kernel tracing!\n");
  540. return 0;
  541. }
  542. static int intel_pt_snapshot_start(struct auxtrace_record *itr)
  543. {
  544. struct intel_pt_recording *ptr =
  545. container_of(itr, struct intel_pt_recording, itr);
  546. struct perf_evsel *evsel;
  547. evlist__for_each(ptr->evlist, evsel) {
  548. if (evsel->attr.type == ptr->intel_pt_pmu->type)
  549. return perf_evlist__disable_event(ptr->evlist, evsel);
  550. }
  551. return -EINVAL;
  552. }
  553. static int intel_pt_snapshot_finish(struct auxtrace_record *itr)
  554. {
  555. struct intel_pt_recording *ptr =
  556. container_of(itr, struct intel_pt_recording, itr);
  557. struct perf_evsel *evsel;
  558. evlist__for_each(ptr->evlist, evsel) {
  559. if (evsel->attr.type == ptr->intel_pt_pmu->type)
  560. return perf_evlist__enable_event(ptr->evlist, evsel);
  561. }
  562. return -EINVAL;
  563. }
  564. static int intel_pt_alloc_snapshot_refs(struct intel_pt_recording *ptr, int idx)
  565. {
  566. const size_t sz = sizeof(struct intel_pt_snapshot_ref);
  567. int cnt = ptr->snapshot_ref_cnt, new_cnt = cnt * 2;
  568. struct intel_pt_snapshot_ref *refs;
  569. if (!new_cnt)
  570. new_cnt = 16;
  571. while (new_cnt <= idx)
  572. new_cnt *= 2;
  573. refs = calloc(new_cnt, sz);
  574. if (!refs)
  575. return -ENOMEM;
  576. memcpy(refs, ptr->snapshot_refs, cnt * sz);
  577. ptr->snapshot_refs = refs;
  578. ptr->snapshot_ref_cnt = new_cnt;
  579. return 0;
  580. }
  581. static void intel_pt_free_snapshot_refs(struct intel_pt_recording *ptr)
  582. {
  583. int i;
  584. for (i = 0; i < ptr->snapshot_ref_cnt; i++)
  585. zfree(&ptr->snapshot_refs[i].ref_buf);
  586. zfree(&ptr->snapshot_refs);
  587. }
  588. static void intel_pt_recording_free(struct auxtrace_record *itr)
  589. {
  590. struct intel_pt_recording *ptr =
  591. container_of(itr, struct intel_pt_recording, itr);
  592. intel_pt_free_snapshot_refs(ptr);
  593. free(ptr);
  594. }
  595. static int intel_pt_alloc_snapshot_ref(struct intel_pt_recording *ptr, int idx,
  596. size_t snapshot_buf_size)
  597. {
  598. size_t ref_buf_size = ptr->snapshot_ref_buf_size;
  599. void *ref_buf;
  600. ref_buf = zalloc(ref_buf_size);
  601. if (!ref_buf)
  602. return -ENOMEM;
  603. ptr->snapshot_refs[idx].ref_buf = ref_buf;
  604. ptr->snapshot_refs[idx].ref_offset = snapshot_buf_size - ref_buf_size;
  605. return 0;
  606. }
  607. static size_t intel_pt_snapshot_ref_buf_size(struct intel_pt_recording *ptr,
  608. size_t snapshot_buf_size)
  609. {
  610. const size_t max_size = 256 * 1024;
  611. size_t buf_size = 0, psb_period;
  612. if (ptr->snapshot_size <= 64 * 1024)
  613. return 0;
  614. psb_period = intel_pt_psb_period(ptr->intel_pt_pmu, ptr->evlist);
  615. if (psb_period)
  616. buf_size = psb_period * 2;
  617. if (!buf_size || buf_size > max_size)
  618. buf_size = max_size;
  619. if (buf_size >= snapshot_buf_size)
  620. return 0;
  621. if (buf_size >= ptr->snapshot_size / 2)
  622. return 0;
  623. return buf_size;
  624. }
  625. static int intel_pt_snapshot_init(struct intel_pt_recording *ptr,
  626. size_t snapshot_buf_size)
  627. {
  628. if (ptr->snapshot_init_done)
  629. return 0;
  630. ptr->snapshot_init_done = true;
  631. ptr->snapshot_ref_buf_size = intel_pt_snapshot_ref_buf_size(ptr,
  632. snapshot_buf_size);
  633. return 0;
  634. }
  635. /**
  636. * intel_pt_compare_buffers - compare bytes in a buffer to a circular buffer.
  637. * @buf1: first buffer
  638. * @compare_size: number of bytes to compare
  639. * @buf2: second buffer (a circular buffer)
  640. * @offs2: offset in second buffer
  641. * @buf2_size: size of second buffer
  642. *
  643. * The comparison allows for the possibility that the bytes to compare in the
  644. * circular buffer are not contiguous. It is assumed that @compare_size <=
  645. * @buf2_size. This function returns %false if the bytes are identical, %true
  646. * otherwise.
  647. */
  648. static bool intel_pt_compare_buffers(void *buf1, size_t compare_size,
  649. void *buf2, size_t offs2, size_t buf2_size)
  650. {
  651. size_t end2 = offs2 + compare_size, part_size;
  652. if (end2 <= buf2_size)
  653. return memcmp(buf1, buf2 + offs2, compare_size);
  654. part_size = end2 - buf2_size;
  655. if (memcmp(buf1, buf2 + offs2, part_size))
  656. return true;
  657. compare_size -= part_size;
  658. return memcmp(buf1 + part_size, buf2, compare_size);
  659. }
  660. static bool intel_pt_compare_ref(void *ref_buf, size_t ref_offset,
  661. size_t ref_size, size_t buf_size,
  662. void *data, size_t head)
  663. {
  664. size_t ref_end = ref_offset + ref_size;
  665. if (ref_end > buf_size) {
  666. if (head > ref_offset || head < ref_end - buf_size)
  667. return true;
  668. } else if (head > ref_offset && head < ref_end) {
  669. return true;
  670. }
  671. return intel_pt_compare_buffers(ref_buf, ref_size, data, ref_offset,
  672. buf_size);
  673. }
  674. static void intel_pt_copy_ref(void *ref_buf, size_t ref_size, size_t buf_size,
  675. void *data, size_t head)
  676. {
  677. if (head >= ref_size) {
  678. memcpy(ref_buf, data + head - ref_size, ref_size);
  679. } else {
  680. memcpy(ref_buf, data, head);
  681. ref_size -= head;
  682. memcpy(ref_buf + head, data + buf_size - ref_size, ref_size);
  683. }
  684. }
  685. static bool intel_pt_wrapped(struct intel_pt_recording *ptr, int idx,
  686. struct auxtrace_mmap *mm, unsigned char *data,
  687. u64 head)
  688. {
  689. struct intel_pt_snapshot_ref *ref = &ptr->snapshot_refs[idx];
  690. bool wrapped;
  691. wrapped = intel_pt_compare_ref(ref->ref_buf, ref->ref_offset,
  692. ptr->snapshot_ref_buf_size, mm->len,
  693. data, head);
  694. intel_pt_copy_ref(ref->ref_buf, ptr->snapshot_ref_buf_size, mm->len,
  695. data, head);
  696. return wrapped;
  697. }
  698. static bool intel_pt_first_wrap(u64 *data, size_t buf_size)
  699. {
  700. int i, a, b;
  701. b = buf_size >> 3;
  702. a = b - 512;
  703. if (a < 0)
  704. a = 0;
  705. for (i = a; i < b; i++) {
  706. if (data[i])
  707. return true;
  708. }
  709. return false;
  710. }
  711. static int intel_pt_find_snapshot(struct auxtrace_record *itr, int idx,
  712. struct auxtrace_mmap *mm, unsigned char *data,
  713. u64 *head, u64 *old)
  714. {
  715. struct intel_pt_recording *ptr =
  716. container_of(itr, struct intel_pt_recording, itr);
  717. bool wrapped;
  718. int err;
  719. pr_debug3("%s: mmap index %d old head %zu new head %zu\n",
  720. __func__, idx, (size_t)*old, (size_t)*head);
  721. err = intel_pt_snapshot_init(ptr, mm->len);
  722. if (err)
  723. goto out_err;
  724. if (idx >= ptr->snapshot_ref_cnt) {
  725. err = intel_pt_alloc_snapshot_refs(ptr, idx);
  726. if (err)
  727. goto out_err;
  728. }
  729. if (ptr->snapshot_ref_buf_size) {
  730. if (!ptr->snapshot_refs[idx].ref_buf) {
  731. err = intel_pt_alloc_snapshot_ref(ptr, idx, mm->len);
  732. if (err)
  733. goto out_err;
  734. }
  735. wrapped = intel_pt_wrapped(ptr, idx, mm, data, *head);
  736. } else {
  737. wrapped = ptr->snapshot_refs[idx].wrapped;
  738. if (!wrapped && intel_pt_first_wrap((u64 *)data, mm->len)) {
  739. ptr->snapshot_refs[idx].wrapped = true;
  740. wrapped = true;
  741. }
  742. }
  743. /*
  744. * In full trace mode 'head' continually increases. However in snapshot
  745. * mode 'head' is an offset within the buffer. Here 'old' and 'head'
  746. * are adjusted to match the full trace case which expects that 'old' is
  747. * always less than 'head'.
  748. */
  749. if (wrapped) {
  750. *old = *head;
  751. *head += mm->len;
  752. } else {
  753. if (mm->mask)
  754. *old &= mm->mask;
  755. else
  756. *old %= mm->len;
  757. if (*old > *head)
  758. *head += mm->len;
  759. }
  760. pr_debug3("%s: wrap-around %sdetected, adjusted old head %zu adjusted new head %zu\n",
  761. __func__, wrapped ? "" : "not ", (size_t)*old, (size_t)*head);
  762. return 0;
  763. out_err:
  764. pr_err("%s: failed, error %d\n", __func__, err);
  765. return err;
  766. }
  767. static u64 intel_pt_reference(struct auxtrace_record *itr __maybe_unused)
  768. {
  769. return rdtsc();
  770. }
  771. static int intel_pt_read_finish(struct auxtrace_record *itr, int idx)
  772. {
  773. struct intel_pt_recording *ptr =
  774. container_of(itr, struct intel_pt_recording, itr);
  775. struct perf_evsel *evsel;
  776. evlist__for_each(ptr->evlist, evsel) {
  777. if (evsel->attr.type == ptr->intel_pt_pmu->type)
  778. return perf_evlist__enable_event_idx(ptr->evlist, evsel,
  779. idx);
  780. }
  781. return -EINVAL;
  782. }
  783. struct auxtrace_record *intel_pt_recording_init(int *err)
  784. {
  785. struct perf_pmu *intel_pt_pmu = perf_pmu__find(INTEL_PT_PMU_NAME);
  786. struct intel_pt_recording *ptr;
  787. if (!intel_pt_pmu)
  788. return NULL;
  789. ptr = zalloc(sizeof(struct intel_pt_recording));
  790. if (!ptr) {
  791. *err = -ENOMEM;
  792. return NULL;
  793. }
  794. ptr->intel_pt_pmu = intel_pt_pmu;
  795. ptr->itr.recording_options = intel_pt_recording_options;
  796. ptr->itr.info_priv_size = intel_pt_info_priv_size;
  797. ptr->itr.info_fill = intel_pt_info_fill;
  798. ptr->itr.free = intel_pt_recording_free;
  799. ptr->itr.snapshot_start = intel_pt_snapshot_start;
  800. ptr->itr.snapshot_finish = intel_pt_snapshot_finish;
  801. ptr->itr.find_snapshot = intel_pt_find_snapshot;
  802. ptr->itr.parse_snapshot_options = intel_pt_parse_snapshot_options;
  803. ptr->itr.reference = intel_pt_reference;
  804. ptr->itr.read_finish = intel_pt_read_finish;
  805. return &ptr->itr;
  806. }