intel-pt.c 27 KB

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