intel-pt.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  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 <subcmd/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_terms__delete(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_entry(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 mtc, mtc_periods = 0, mtc_period;
  162. int psb_cyc, psb_periods, psb_period;
  163. int pos = 0;
  164. u64 config;
  165. pos += scnprintf(buf + pos, sizeof(buf) - pos, "tsc");
  166. if (perf_pmu__scan_file(intel_pt_pmu, "caps/mtc", "%d",
  167. &mtc) != 1)
  168. mtc = 1;
  169. if (mtc) {
  170. if (perf_pmu__scan_file(intel_pt_pmu, "caps/mtc_periods", "%x",
  171. &mtc_periods) != 1)
  172. mtc_periods = 0;
  173. if (mtc_periods) {
  174. mtc_period = intel_pt_pick_bit(mtc_periods, 3);
  175. pos += scnprintf(buf + pos, sizeof(buf) - pos,
  176. ",mtc,mtc_period=%d", mtc_period);
  177. }
  178. }
  179. if (perf_pmu__scan_file(intel_pt_pmu, "caps/psb_cyc", "%d",
  180. &psb_cyc) != 1)
  181. psb_cyc = 1;
  182. if (psb_cyc && mtc_periods) {
  183. if (perf_pmu__scan_file(intel_pt_pmu, "caps/psb_periods", "%x",
  184. &psb_periods) != 1)
  185. psb_periods = 0;
  186. if (psb_periods) {
  187. psb_period = intel_pt_pick_bit(psb_periods, 3);
  188. pos += scnprintf(buf + pos, sizeof(buf) - pos,
  189. ",psb_period=%d", psb_period);
  190. }
  191. }
  192. pr_debug2("%s default config: %s\n", intel_pt_pmu->name, buf);
  193. intel_pt_parse_terms(&intel_pt_pmu->format, buf, &config);
  194. return config;
  195. }
  196. static int intel_pt_parse_snapshot_options(struct auxtrace_record *itr,
  197. struct record_opts *opts,
  198. const char *str)
  199. {
  200. struct intel_pt_recording *ptr =
  201. container_of(itr, struct intel_pt_recording, itr);
  202. unsigned long long snapshot_size = 0;
  203. char *endptr;
  204. if (str) {
  205. snapshot_size = strtoull(str, &endptr, 0);
  206. if (*endptr || snapshot_size > SIZE_MAX)
  207. return -1;
  208. }
  209. opts->auxtrace_snapshot_mode = true;
  210. opts->auxtrace_snapshot_size = snapshot_size;
  211. ptr->snapshot_size = snapshot_size;
  212. return 0;
  213. }
  214. struct perf_event_attr *
  215. intel_pt_pmu_default_config(struct perf_pmu *intel_pt_pmu)
  216. {
  217. struct perf_event_attr *attr;
  218. attr = zalloc(sizeof(struct perf_event_attr));
  219. if (!attr)
  220. return NULL;
  221. attr->config = intel_pt_default_config(intel_pt_pmu);
  222. intel_pt_pmu->selectable = true;
  223. return attr;
  224. }
  225. static size_t
  226. intel_pt_info_priv_size(struct auxtrace_record *itr __maybe_unused,
  227. struct perf_evlist *evlist __maybe_unused)
  228. {
  229. return INTEL_PT_AUXTRACE_PRIV_SIZE;
  230. }
  231. static void intel_pt_tsc_ctc_ratio(u32 *n, u32 *d)
  232. {
  233. unsigned int eax = 0, ebx = 0, ecx = 0, edx = 0;
  234. __get_cpuid(0x15, &eax, &ebx, &ecx, &edx);
  235. *n = ebx;
  236. *d = eax;
  237. }
  238. static int intel_pt_info_fill(struct auxtrace_record *itr,
  239. struct perf_session *session,
  240. struct auxtrace_info_event *auxtrace_info,
  241. size_t priv_size)
  242. {
  243. struct intel_pt_recording *ptr =
  244. container_of(itr, struct intel_pt_recording, itr);
  245. struct perf_pmu *intel_pt_pmu = ptr->intel_pt_pmu;
  246. struct perf_event_mmap_page *pc;
  247. struct perf_tsc_conversion tc = { .time_mult = 0, };
  248. bool cap_user_time_zero = false, per_cpu_mmaps;
  249. u64 tsc_bit, mtc_bit, mtc_freq_bits, cyc_bit, noretcomp_bit;
  250. u32 tsc_ctc_ratio_n, tsc_ctc_ratio_d;
  251. int err;
  252. if (priv_size != INTEL_PT_AUXTRACE_PRIV_SIZE)
  253. return -EINVAL;
  254. intel_pt_parse_terms(&intel_pt_pmu->format, "tsc", &tsc_bit);
  255. intel_pt_parse_terms(&intel_pt_pmu->format, "noretcomp",
  256. &noretcomp_bit);
  257. intel_pt_parse_terms(&intel_pt_pmu->format, "mtc", &mtc_bit);
  258. mtc_freq_bits = perf_pmu__format_bits(&intel_pt_pmu->format,
  259. "mtc_period");
  260. intel_pt_parse_terms(&intel_pt_pmu->format, "cyc", &cyc_bit);
  261. intel_pt_tsc_ctc_ratio(&tsc_ctc_ratio_n, &tsc_ctc_ratio_d);
  262. if (!session->evlist->nr_mmaps)
  263. return -EINVAL;
  264. pc = session->evlist->mmap[0].base;
  265. if (pc) {
  266. err = perf_read_tsc_conversion(pc, &tc);
  267. if (err) {
  268. if (err != -EOPNOTSUPP)
  269. return err;
  270. } else {
  271. cap_user_time_zero = tc.time_mult != 0;
  272. }
  273. if (!cap_user_time_zero)
  274. ui__warning("Intel Processor Trace: TSC not available\n");
  275. }
  276. per_cpu_mmaps = !cpu_map__empty(session->evlist->cpus);
  277. auxtrace_info->type = PERF_AUXTRACE_INTEL_PT;
  278. auxtrace_info->priv[INTEL_PT_PMU_TYPE] = intel_pt_pmu->type;
  279. auxtrace_info->priv[INTEL_PT_TIME_SHIFT] = tc.time_shift;
  280. auxtrace_info->priv[INTEL_PT_TIME_MULT] = tc.time_mult;
  281. auxtrace_info->priv[INTEL_PT_TIME_ZERO] = tc.time_zero;
  282. auxtrace_info->priv[INTEL_PT_CAP_USER_TIME_ZERO] = cap_user_time_zero;
  283. auxtrace_info->priv[INTEL_PT_TSC_BIT] = tsc_bit;
  284. auxtrace_info->priv[INTEL_PT_NORETCOMP_BIT] = noretcomp_bit;
  285. auxtrace_info->priv[INTEL_PT_HAVE_SCHED_SWITCH] = ptr->have_sched_switch;
  286. auxtrace_info->priv[INTEL_PT_SNAPSHOT_MODE] = ptr->snapshot_mode;
  287. auxtrace_info->priv[INTEL_PT_PER_CPU_MMAPS] = per_cpu_mmaps;
  288. auxtrace_info->priv[INTEL_PT_MTC_BIT] = mtc_bit;
  289. auxtrace_info->priv[INTEL_PT_MTC_FREQ_BITS] = mtc_freq_bits;
  290. auxtrace_info->priv[INTEL_PT_TSC_CTC_N] = tsc_ctc_ratio_n;
  291. auxtrace_info->priv[INTEL_PT_TSC_CTC_D] = tsc_ctc_ratio_d;
  292. auxtrace_info->priv[INTEL_PT_CYC_BIT] = cyc_bit;
  293. return 0;
  294. }
  295. static int intel_pt_track_switches(struct perf_evlist *evlist)
  296. {
  297. const char *sched_switch = "sched:sched_switch";
  298. struct perf_evsel *evsel;
  299. int err;
  300. if (!perf_evlist__can_select_event(evlist, sched_switch))
  301. return -EPERM;
  302. err = parse_events(evlist, sched_switch, NULL);
  303. if (err) {
  304. pr_debug2("%s: failed to parse %s, error %d\n",
  305. __func__, sched_switch, err);
  306. return err;
  307. }
  308. evsel = perf_evlist__last(evlist);
  309. perf_evsel__set_sample_bit(evsel, CPU);
  310. perf_evsel__set_sample_bit(evsel, TIME);
  311. evsel->system_wide = true;
  312. evsel->no_aux_samples = true;
  313. evsel->immediate = true;
  314. return 0;
  315. }
  316. static void intel_pt_valid_str(char *str, size_t len, u64 valid)
  317. {
  318. unsigned int val, last = 0, state = 1;
  319. int p = 0;
  320. str[0] = '\0';
  321. for (val = 0; val <= 64; val++, valid >>= 1) {
  322. if (valid & 1) {
  323. last = val;
  324. switch (state) {
  325. case 0:
  326. p += scnprintf(str + p, len - p, ",");
  327. /* Fall through */
  328. case 1:
  329. p += scnprintf(str + p, len - p, "%u", val);
  330. state = 2;
  331. break;
  332. case 2:
  333. state = 3;
  334. break;
  335. case 3:
  336. state = 4;
  337. break;
  338. default:
  339. break;
  340. }
  341. } else {
  342. switch (state) {
  343. case 3:
  344. p += scnprintf(str + p, len - p, ",%u", last);
  345. state = 0;
  346. break;
  347. case 4:
  348. p += scnprintf(str + p, len - p, "-%u", last);
  349. state = 0;
  350. break;
  351. default:
  352. break;
  353. }
  354. if (state != 1)
  355. state = 0;
  356. }
  357. }
  358. }
  359. static int intel_pt_val_config_term(struct perf_pmu *intel_pt_pmu,
  360. const char *caps, const char *name,
  361. const char *supported, u64 config)
  362. {
  363. char valid_str[256];
  364. unsigned int shift;
  365. unsigned long long valid;
  366. u64 bits;
  367. int ok;
  368. if (perf_pmu__scan_file(intel_pt_pmu, caps, "%llx", &valid) != 1)
  369. valid = 0;
  370. if (supported &&
  371. perf_pmu__scan_file(intel_pt_pmu, supported, "%d", &ok) == 1 && !ok)
  372. valid = 0;
  373. valid |= 1;
  374. bits = perf_pmu__format_bits(&intel_pt_pmu->format, name);
  375. config &= bits;
  376. for (shift = 0; bits && !(bits & 1); shift++)
  377. bits >>= 1;
  378. config >>= shift;
  379. if (config > 63)
  380. goto out_err;
  381. if (valid & (1 << config))
  382. return 0;
  383. out_err:
  384. intel_pt_valid_str(valid_str, sizeof(valid_str), valid);
  385. pr_err("Invalid %s for %s. Valid values are: %s\n",
  386. name, INTEL_PT_PMU_NAME, valid_str);
  387. return -EINVAL;
  388. }
  389. static int intel_pt_validate_config(struct perf_pmu *intel_pt_pmu,
  390. struct perf_evsel *evsel)
  391. {
  392. int err;
  393. if (!evsel)
  394. return 0;
  395. err = intel_pt_val_config_term(intel_pt_pmu, "caps/cycle_thresholds",
  396. "cyc_thresh", "caps/psb_cyc",
  397. evsel->attr.config);
  398. if (err)
  399. return err;
  400. err = intel_pt_val_config_term(intel_pt_pmu, "caps/mtc_periods",
  401. "mtc_period", "caps/mtc",
  402. evsel->attr.config);
  403. if (err)
  404. return err;
  405. return intel_pt_val_config_term(intel_pt_pmu, "caps/psb_periods",
  406. "psb_period", "caps/psb_cyc",
  407. evsel->attr.config);
  408. }
  409. static int intel_pt_recording_options(struct auxtrace_record *itr,
  410. struct perf_evlist *evlist,
  411. struct record_opts *opts)
  412. {
  413. struct intel_pt_recording *ptr =
  414. container_of(itr, struct intel_pt_recording, itr);
  415. struct perf_pmu *intel_pt_pmu = ptr->intel_pt_pmu;
  416. bool have_timing_info, need_immediate = false;
  417. struct perf_evsel *evsel, *intel_pt_evsel = NULL;
  418. const struct cpu_map *cpus = evlist->cpus;
  419. bool privileged = geteuid() == 0 || perf_event_paranoid() < 0;
  420. u64 tsc_bit;
  421. int err;
  422. ptr->evlist = evlist;
  423. ptr->snapshot_mode = opts->auxtrace_snapshot_mode;
  424. evlist__for_each_entry(evlist, evsel) {
  425. if (evsel->attr.type == intel_pt_pmu->type) {
  426. if (intel_pt_evsel) {
  427. pr_err("There may be only one " INTEL_PT_PMU_NAME " event\n");
  428. return -EINVAL;
  429. }
  430. evsel->attr.freq = 0;
  431. evsel->attr.sample_period = 1;
  432. intel_pt_evsel = evsel;
  433. opts->full_auxtrace = true;
  434. }
  435. }
  436. if (opts->auxtrace_snapshot_mode && !opts->full_auxtrace) {
  437. pr_err("Snapshot mode (-S option) requires " INTEL_PT_PMU_NAME " PMU event (-e " INTEL_PT_PMU_NAME ")\n");
  438. return -EINVAL;
  439. }
  440. if (opts->use_clockid) {
  441. pr_err("Cannot use clockid (-k option) with " INTEL_PT_PMU_NAME "\n");
  442. return -EINVAL;
  443. }
  444. if (!opts->full_auxtrace)
  445. return 0;
  446. err = intel_pt_validate_config(intel_pt_pmu, intel_pt_evsel);
  447. if (err)
  448. return err;
  449. /* Set default sizes for snapshot mode */
  450. if (opts->auxtrace_snapshot_mode) {
  451. size_t psb_period = intel_pt_psb_period(intel_pt_pmu, evlist);
  452. if (!opts->auxtrace_snapshot_size && !opts->auxtrace_mmap_pages) {
  453. if (privileged) {
  454. opts->auxtrace_mmap_pages = MiB(4) / page_size;
  455. } else {
  456. opts->auxtrace_mmap_pages = KiB(128) / page_size;
  457. if (opts->mmap_pages == UINT_MAX)
  458. opts->mmap_pages = KiB(256) / page_size;
  459. }
  460. } else if (!opts->auxtrace_mmap_pages && !privileged &&
  461. opts->mmap_pages == UINT_MAX) {
  462. opts->mmap_pages = KiB(256) / page_size;
  463. }
  464. if (!opts->auxtrace_snapshot_size)
  465. opts->auxtrace_snapshot_size =
  466. opts->auxtrace_mmap_pages * (size_t)page_size;
  467. if (!opts->auxtrace_mmap_pages) {
  468. size_t sz = opts->auxtrace_snapshot_size;
  469. sz = round_up(sz, page_size) / page_size;
  470. opts->auxtrace_mmap_pages = roundup_pow_of_two(sz);
  471. }
  472. if (opts->auxtrace_snapshot_size >
  473. opts->auxtrace_mmap_pages * (size_t)page_size) {
  474. pr_err("Snapshot size %zu must not be greater than AUX area tracing mmap size %zu\n",
  475. opts->auxtrace_snapshot_size,
  476. opts->auxtrace_mmap_pages * (size_t)page_size);
  477. return -EINVAL;
  478. }
  479. if (!opts->auxtrace_snapshot_size || !opts->auxtrace_mmap_pages) {
  480. pr_err("Failed to calculate default snapshot size and/or AUX area tracing mmap pages\n");
  481. return -EINVAL;
  482. }
  483. pr_debug2("Intel PT snapshot size: %zu\n",
  484. opts->auxtrace_snapshot_size);
  485. if (psb_period &&
  486. opts->auxtrace_snapshot_size <= psb_period +
  487. INTEL_PT_PSB_PERIOD_NEAR)
  488. ui__warning("Intel PT snapshot size (%zu) may be too small for PSB period (%zu)\n",
  489. opts->auxtrace_snapshot_size, psb_period);
  490. }
  491. /* Set default sizes for full trace mode */
  492. if (opts->full_auxtrace && !opts->auxtrace_mmap_pages) {
  493. if (privileged) {
  494. opts->auxtrace_mmap_pages = MiB(4) / page_size;
  495. } else {
  496. opts->auxtrace_mmap_pages = KiB(128) / page_size;
  497. if (opts->mmap_pages == UINT_MAX)
  498. opts->mmap_pages = KiB(256) / page_size;
  499. }
  500. }
  501. /* Validate auxtrace_mmap_pages */
  502. if (opts->auxtrace_mmap_pages) {
  503. size_t sz = opts->auxtrace_mmap_pages * (size_t)page_size;
  504. size_t min_sz;
  505. if (opts->auxtrace_snapshot_mode)
  506. min_sz = KiB(4);
  507. else
  508. min_sz = KiB(8);
  509. if (sz < min_sz || !is_power_of_2(sz)) {
  510. pr_err("Invalid mmap size for Intel Processor Trace: must be at least %zuKiB and a power of 2\n",
  511. min_sz / 1024);
  512. return -EINVAL;
  513. }
  514. }
  515. intel_pt_parse_terms(&intel_pt_pmu->format, "tsc", &tsc_bit);
  516. if (opts->full_auxtrace && (intel_pt_evsel->attr.config & tsc_bit))
  517. have_timing_info = true;
  518. else
  519. have_timing_info = false;
  520. /*
  521. * Per-cpu recording needs sched_switch events to distinguish different
  522. * threads.
  523. */
  524. if (have_timing_info && !cpu_map__empty(cpus)) {
  525. if (perf_can_record_switch_events()) {
  526. bool cpu_wide = !target__none(&opts->target) &&
  527. !target__has_task(&opts->target);
  528. if (!cpu_wide && perf_can_record_cpu_wide()) {
  529. struct perf_evsel *switch_evsel;
  530. err = parse_events(evlist, "dummy:u", NULL);
  531. if (err)
  532. return err;
  533. switch_evsel = perf_evlist__last(evlist);
  534. switch_evsel->attr.freq = 0;
  535. switch_evsel->attr.sample_period = 1;
  536. switch_evsel->attr.context_switch = 1;
  537. switch_evsel->system_wide = true;
  538. switch_evsel->no_aux_samples = true;
  539. switch_evsel->immediate = true;
  540. perf_evsel__set_sample_bit(switch_evsel, TID);
  541. perf_evsel__set_sample_bit(switch_evsel, TIME);
  542. perf_evsel__set_sample_bit(switch_evsel, CPU);
  543. opts->record_switch_events = false;
  544. ptr->have_sched_switch = 3;
  545. } else {
  546. opts->record_switch_events = true;
  547. need_immediate = true;
  548. if (cpu_wide)
  549. ptr->have_sched_switch = 3;
  550. else
  551. ptr->have_sched_switch = 2;
  552. }
  553. } else {
  554. err = intel_pt_track_switches(evlist);
  555. if (err == -EPERM)
  556. pr_debug2("Unable to select sched:sched_switch\n");
  557. else if (err)
  558. return err;
  559. else
  560. ptr->have_sched_switch = 1;
  561. }
  562. }
  563. if (intel_pt_evsel) {
  564. /*
  565. * To obtain the auxtrace buffer file descriptor, the auxtrace
  566. * event must come first.
  567. */
  568. perf_evlist__to_front(evlist, intel_pt_evsel);
  569. /*
  570. * In the case of per-cpu mmaps, we need the CPU on the
  571. * AUX event.
  572. */
  573. if (!cpu_map__empty(cpus))
  574. perf_evsel__set_sample_bit(intel_pt_evsel, CPU);
  575. }
  576. /* Add dummy event to keep tracking */
  577. if (opts->full_auxtrace) {
  578. struct perf_evsel *tracking_evsel;
  579. err = parse_events(evlist, "dummy:u", NULL);
  580. if (err)
  581. return err;
  582. tracking_evsel = perf_evlist__last(evlist);
  583. perf_evlist__set_tracking_event(evlist, tracking_evsel);
  584. tracking_evsel->attr.freq = 0;
  585. tracking_evsel->attr.sample_period = 1;
  586. if (need_immediate)
  587. tracking_evsel->immediate = true;
  588. /* In per-cpu case, always need the time of mmap events etc */
  589. if (!cpu_map__empty(cpus)) {
  590. perf_evsel__set_sample_bit(tracking_evsel, TIME);
  591. /* And the CPU for switch events */
  592. perf_evsel__set_sample_bit(tracking_evsel, CPU);
  593. }
  594. }
  595. /*
  596. * Warn the user when we do not have enough information to decode i.e.
  597. * per-cpu with no sched_switch (except workload-only).
  598. */
  599. if (!ptr->have_sched_switch && !cpu_map__empty(cpus) &&
  600. !target__none(&opts->target))
  601. ui__warning("Intel Processor Trace decoding will not be possible except for kernel tracing!\n");
  602. return 0;
  603. }
  604. static int intel_pt_snapshot_start(struct auxtrace_record *itr)
  605. {
  606. struct intel_pt_recording *ptr =
  607. container_of(itr, struct intel_pt_recording, itr);
  608. struct perf_evsel *evsel;
  609. evlist__for_each_entry(ptr->evlist, evsel) {
  610. if (evsel->attr.type == ptr->intel_pt_pmu->type)
  611. return perf_evsel__disable(evsel);
  612. }
  613. return -EINVAL;
  614. }
  615. static int intel_pt_snapshot_finish(struct auxtrace_record *itr)
  616. {
  617. struct intel_pt_recording *ptr =
  618. container_of(itr, struct intel_pt_recording, itr);
  619. struct perf_evsel *evsel;
  620. evlist__for_each_entry(ptr->evlist, evsel) {
  621. if (evsel->attr.type == ptr->intel_pt_pmu->type)
  622. return perf_evsel__enable(evsel);
  623. }
  624. return -EINVAL;
  625. }
  626. static int intel_pt_alloc_snapshot_refs(struct intel_pt_recording *ptr, int idx)
  627. {
  628. const size_t sz = sizeof(struct intel_pt_snapshot_ref);
  629. int cnt = ptr->snapshot_ref_cnt, new_cnt = cnt * 2;
  630. struct intel_pt_snapshot_ref *refs;
  631. if (!new_cnt)
  632. new_cnt = 16;
  633. while (new_cnt <= idx)
  634. new_cnt *= 2;
  635. refs = calloc(new_cnt, sz);
  636. if (!refs)
  637. return -ENOMEM;
  638. memcpy(refs, ptr->snapshot_refs, cnt * sz);
  639. ptr->snapshot_refs = refs;
  640. ptr->snapshot_ref_cnt = new_cnt;
  641. return 0;
  642. }
  643. static void intel_pt_free_snapshot_refs(struct intel_pt_recording *ptr)
  644. {
  645. int i;
  646. for (i = 0; i < ptr->snapshot_ref_cnt; i++)
  647. zfree(&ptr->snapshot_refs[i].ref_buf);
  648. zfree(&ptr->snapshot_refs);
  649. }
  650. static void intel_pt_recording_free(struct auxtrace_record *itr)
  651. {
  652. struct intel_pt_recording *ptr =
  653. container_of(itr, struct intel_pt_recording, itr);
  654. intel_pt_free_snapshot_refs(ptr);
  655. free(ptr);
  656. }
  657. static int intel_pt_alloc_snapshot_ref(struct intel_pt_recording *ptr, int idx,
  658. size_t snapshot_buf_size)
  659. {
  660. size_t ref_buf_size = ptr->snapshot_ref_buf_size;
  661. void *ref_buf;
  662. ref_buf = zalloc(ref_buf_size);
  663. if (!ref_buf)
  664. return -ENOMEM;
  665. ptr->snapshot_refs[idx].ref_buf = ref_buf;
  666. ptr->snapshot_refs[idx].ref_offset = snapshot_buf_size - ref_buf_size;
  667. return 0;
  668. }
  669. static size_t intel_pt_snapshot_ref_buf_size(struct intel_pt_recording *ptr,
  670. size_t snapshot_buf_size)
  671. {
  672. const size_t max_size = 256 * 1024;
  673. size_t buf_size = 0, psb_period;
  674. if (ptr->snapshot_size <= 64 * 1024)
  675. return 0;
  676. psb_period = intel_pt_psb_period(ptr->intel_pt_pmu, ptr->evlist);
  677. if (psb_period)
  678. buf_size = psb_period * 2;
  679. if (!buf_size || buf_size > max_size)
  680. buf_size = max_size;
  681. if (buf_size >= snapshot_buf_size)
  682. return 0;
  683. if (buf_size >= ptr->snapshot_size / 2)
  684. return 0;
  685. return buf_size;
  686. }
  687. static int intel_pt_snapshot_init(struct intel_pt_recording *ptr,
  688. size_t snapshot_buf_size)
  689. {
  690. if (ptr->snapshot_init_done)
  691. return 0;
  692. ptr->snapshot_init_done = true;
  693. ptr->snapshot_ref_buf_size = intel_pt_snapshot_ref_buf_size(ptr,
  694. snapshot_buf_size);
  695. return 0;
  696. }
  697. /**
  698. * intel_pt_compare_buffers - compare bytes in a buffer to a circular buffer.
  699. * @buf1: first buffer
  700. * @compare_size: number of bytes to compare
  701. * @buf2: second buffer (a circular buffer)
  702. * @offs2: offset in second buffer
  703. * @buf2_size: size of second buffer
  704. *
  705. * The comparison allows for the possibility that the bytes to compare in the
  706. * circular buffer are not contiguous. It is assumed that @compare_size <=
  707. * @buf2_size. This function returns %false if the bytes are identical, %true
  708. * otherwise.
  709. */
  710. static bool intel_pt_compare_buffers(void *buf1, size_t compare_size,
  711. void *buf2, size_t offs2, size_t buf2_size)
  712. {
  713. size_t end2 = offs2 + compare_size, part_size;
  714. if (end2 <= buf2_size)
  715. return memcmp(buf1, buf2 + offs2, compare_size);
  716. part_size = end2 - buf2_size;
  717. if (memcmp(buf1, buf2 + offs2, part_size))
  718. return true;
  719. compare_size -= part_size;
  720. return memcmp(buf1 + part_size, buf2, compare_size);
  721. }
  722. static bool intel_pt_compare_ref(void *ref_buf, size_t ref_offset,
  723. size_t ref_size, size_t buf_size,
  724. void *data, size_t head)
  725. {
  726. size_t ref_end = ref_offset + ref_size;
  727. if (ref_end > buf_size) {
  728. if (head > ref_offset || head < ref_end - buf_size)
  729. return true;
  730. } else if (head > ref_offset && head < ref_end) {
  731. return true;
  732. }
  733. return intel_pt_compare_buffers(ref_buf, ref_size, data, ref_offset,
  734. buf_size);
  735. }
  736. static void intel_pt_copy_ref(void *ref_buf, size_t ref_size, size_t buf_size,
  737. void *data, size_t head)
  738. {
  739. if (head >= ref_size) {
  740. memcpy(ref_buf, data + head - ref_size, ref_size);
  741. } else {
  742. memcpy(ref_buf, data, head);
  743. ref_size -= head;
  744. memcpy(ref_buf + head, data + buf_size - ref_size, ref_size);
  745. }
  746. }
  747. static bool intel_pt_wrapped(struct intel_pt_recording *ptr, int idx,
  748. struct auxtrace_mmap *mm, unsigned char *data,
  749. u64 head)
  750. {
  751. struct intel_pt_snapshot_ref *ref = &ptr->snapshot_refs[idx];
  752. bool wrapped;
  753. wrapped = intel_pt_compare_ref(ref->ref_buf, ref->ref_offset,
  754. ptr->snapshot_ref_buf_size, mm->len,
  755. data, head);
  756. intel_pt_copy_ref(ref->ref_buf, ptr->snapshot_ref_buf_size, mm->len,
  757. data, head);
  758. return wrapped;
  759. }
  760. static bool intel_pt_first_wrap(u64 *data, size_t buf_size)
  761. {
  762. int i, a, b;
  763. b = buf_size >> 3;
  764. a = b - 512;
  765. if (a < 0)
  766. a = 0;
  767. for (i = a; i < b; i++) {
  768. if (data[i])
  769. return true;
  770. }
  771. return false;
  772. }
  773. static int intel_pt_find_snapshot(struct auxtrace_record *itr, int idx,
  774. struct auxtrace_mmap *mm, unsigned char *data,
  775. u64 *head, u64 *old)
  776. {
  777. struct intel_pt_recording *ptr =
  778. container_of(itr, struct intel_pt_recording, itr);
  779. bool wrapped;
  780. int err;
  781. pr_debug3("%s: mmap index %d old head %zu new head %zu\n",
  782. __func__, idx, (size_t)*old, (size_t)*head);
  783. err = intel_pt_snapshot_init(ptr, mm->len);
  784. if (err)
  785. goto out_err;
  786. if (idx >= ptr->snapshot_ref_cnt) {
  787. err = intel_pt_alloc_snapshot_refs(ptr, idx);
  788. if (err)
  789. goto out_err;
  790. }
  791. if (ptr->snapshot_ref_buf_size) {
  792. if (!ptr->snapshot_refs[idx].ref_buf) {
  793. err = intel_pt_alloc_snapshot_ref(ptr, idx, mm->len);
  794. if (err)
  795. goto out_err;
  796. }
  797. wrapped = intel_pt_wrapped(ptr, idx, mm, data, *head);
  798. } else {
  799. wrapped = ptr->snapshot_refs[idx].wrapped;
  800. if (!wrapped && intel_pt_first_wrap((u64 *)data, mm->len)) {
  801. ptr->snapshot_refs[idx].wrapped = true;
  802. wrapped = true;
  803. }
  804. }
  805. /*
  806. * In full trace mode 'head' continually increases. However in snapshot
  807. * mode 'head' is an offset within the buffer. Here 'old' and 'head'
  808. * are adjusted to match the full trace case which expects that 'old' is
  809. * always less than 'head'.
  810. */
  811. if (wrapped) {
  812. *old = *head;
  813. *head += mm->len;
  814. } else {
  815. if (mm->mask)
  816. *old &= mm->mask;
  817. else
  818. *old %= mm->len;
  819. if (*old > *head)
  820. *head += mm->len;
  821. }
  822. pr_debug3("%s: wrap-around %sdetected, adjusted old head %zu adjusted new head %zu\n",
  823. __func__, wrapped ? "" : "not ", (size_t)*old, (size_t)*head);
  824. return 0;
  825. out_err:
  826. pr_err("%s: failed, error %d\n", __func__, err);
  827. return err;
  828. }
  829. static u64 intel_pt_reference(struct auxtrace_record *itr __maybe_unused)
  830. {
  831. return rdtsc();
  832. }
  833. static int intel_pt_read_finish(struct auxtrace_record *itr, int idx)
  834. {
  835. struct intel_pt_recording *ptr =
  836. container_of(itr, struct intel_pt_recording, itr);
  837. struct perf_evsel *evsel;
  838. evlist__for_each_entry(ptr->evlist, evsel) {
  839. if (evsel->attr.type == ptr->intel_pt_pmu->type)
  840. return perf_evlist__enable_event_idx(ptr->evlist, evsel,
  841. idx);
  842. }
  843. return -EINVAL;
  844. }
  845. struct auxtrace_record *intel_pt_recording_init(int *err)
  846. {
  847. struct perf_pmu *intel_pt_pmu = perf_pmu__find(INTEL_PT_PMU_NAME);
  848. struct intel_pt_recording *ptr;
  849. if (!intel_pt_pmu)
  850. return NULL;
  851. if (setenv("JITDUMP_USE_ARCH_TIMESTAMP", "1", 1)) {
  852. *err = -errno;
  853. return NULL;
  854. }
  855. ptr = zalloc(sizeof(struct intel_pt_recording));
  856. if (!ptr) {
  857. *err = -ENOMEM;
  858. return NULL;
  859. }
  860. ptr->intel_pt_pmu = intel_pt_pmu;
  861. ptr->itr.recording_options = intel_pt_recording_options;
  862. ptr->itr.info_priv_size = intel_pt_info_priv_size;
  863. ptr->itr.info_fill = intel_pt_info_fill;
  864. ptr->itr.free = intel_pt_recording_free;
  865. ptr->itr.snapshot_start = intel_pt_snapshot_start;
  866. ptr->itr.snapshot_finish = intel_pt_snapshot_finish;
  867. ptr->itr.find_snapshot = intel_pt_find_snapshot;
  868. ptr->itr.parse_snapshot_options = intel_pt_parse_snapshot_options;
  869. ptr->itr.reference = intel_pt_reference;
  870. ptr->itr.read_finish = intel_pt_read_finish;
  871. return &ptr->itr;
  872. }