intel-pt.c 28 KB

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