evsel.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  1. /*
  2. * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
  3. *
  4. * Parts came from builtin-{top,stat,record}.c, see those files for further
  5. * copyright notes.
  6. *
  7. * Released under the GPL v2. (and only v2, not any later version)
  8. */
  9. #include <byteswap.h>
  10. #include "asm/bug.h"
  11. #include "evsel.h"
  12. #include "evlist.h"
  13. #include "util.h"
  14. #include "cpumap.h"
  15. #include "thread_map.h"
  16. #include "target.h"
  17. #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
  18. #define GROUP_FD(group_fd, cpu) (*(int *)xyarray__entry(group_fd, cpu, 0))
  19. int __perf_evsel__sample_size(u64 sample_type)
  20. {
  21. u64 mask = sample_type & PERF_SAMPLE_MASK;
  22. int size = 0;
  23. int i;
  24. for (i = 0; i < 64; i++) {
  25. if (mask & (1ULL << i))
  26. size++;
  27. }
  28. size *= sizeof(u64);
  29. return size;
  30. }
  31. void hists__init(struct hists *hists)
  32. {
  33. memset(hists, 0, sizeof(*hists));
  34. hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
  35. hists->entries_in = &hists->entries_in_array[0];
  36. hists->entries_collapsed = RB_ROOT;
  37. hists->entries = RB_ROOT;
  38. pthread_mutex_init(&hists->lock, NULL);
  39. }
  40. void perf_evsel__init(struct perf_evsel *evsel,
  41. struct perf_event_attr *attr, int idx)
  42. {
  43. evsel->idx = idx;
  44. evsel->attr = *attr;
  45. INIT_LIST_HEAD(&evsel->node);
  46. hists__init(&evsel->hists);
  47. }
  48. struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr, int idx)
  49. {
  50. struct perf_evsel *evsel = zalloc(sizeof(*evsel));
  51. if (evsel != NULL)
  52. perf_evsel__init(evsel, attr, idx);
  53. return evsel;
  54. }
  55. static const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX] = {
  56. "cycles",
  57. "instructions",
  58. "cache-references",
  59. "cache-misses",
  60. "branches",
  61. "branch-misses",
  62. "bus-cycles",
  63. "stalled-cycles-frontend",
  64. "stalled-cycles-backend",
  65. "ref-cycles",
  66. };
  67. static const char *__perf_evsel__hw_name(u64 config)
  68. {
  69. if (config < PERF_COUNT_HW_MAX && perf_evsel__hw_names[config])
  70. return perf_evsel__hw_names[config];
  71. return "unknown-hardware";
  72. }
  73. static int perf_evsel__add_modifiers(struct perf_evsel *evsel, char *bf, size_t size)
  74. {
  75. int colon = 0, r = 0;
  76. struct perf_event_attr *attr = &evsel->attr;
  77. bool exclude_guest_default = false;
  78. #define MOD_PRINT(context, mod) do { \
  79. if (!attr->exclude_##context) { \
  80. if (!colon) colon = ++r; \
  81. r += scnprintf(bf + r, size - r, "%c", mod); \
  82. } } while(0)
  83. if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) {
  84. MOD_PRINT(kernel, 'k');
  85. MOD_PRINT(user, 'u');
  86. MOD_PRINT(hv, 'h');
  87. exclude_guest_default = true;
  88. }
  89. if (attr->precise_ip) {
  90. if (!colon)
  91. colon = ++r;
  92. r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp");
  93. exclude_guest_default = true;
  94. }
  95. if (attr->exclude_host || attr->exclude_guest == exclude_guest_default) {
  96. MOD_PRINT(host, 'H');
  97. MOD_PRINT(guest, 'G');
  98. }
  99. #undef MOD_PRINT
  100. if (colon)
  101. bf[colon - 1] = ':';
  102. return r;
  103. }
  104. static int perf_evsel__hw_name(struct perf_evsel *evsel, char *bf, size_t size)
  105. {
  106. int r = scnprintf(bf, size, "%s", __perf_evsel__hw_name(evsel->attr.config));
  107. return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
  108. }
  109. static const char *perf_evsel__sw_names[PERF_COUNT_SW_MAX] = {
  110. "cpu-clock",
  111. "task-clock",
  112. "page-faults",
  113. "context-switches",
  114. "CPU-migrations",
  115. "minor-faults",
  116. "major-faults",
  117. "alignment-faults",
  118. "emulation-faults",
  119. };
  120. static const char *__perf_evsel__sw_name(u64 config)
  121. {
  122. if (config < PERF_COUNT_SW_MAX && perf_evsel__sw_names[config])
  123. return perf_evsel__sw_names[config];
  124. return "unknown-software";
  125. }
  126. static int perf_evsel__sw_name(struct perf_evsel *evsel, char *bf, size_t size)
  127. {
  128. int r = scnprintf(bf, size, "%s", __perf_evsel__sw_name(evsel->attr.config));
  129. return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
  130. }
  131. const char *perf_evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX]
  132. [PERF_EVSEL__MAX_ALIASES] = {
  133. { "L1-dcache", "l1-d", "l1d", "L1-data", },
  134. { "L1-icache", "l1-i", "l1i", "L1-instruction", },
  135. { "LLC", "L2", },
  136. { "dTLB", "d-tlb", "Data-TLB", },
  137. { "iTLB", "i-tlb", "Instruction-TLB", },
  138. { "branch", "branches", "bpu", "btb", "bpc", },
  139. { "node", },
  140. };
  141. const char *perf_evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX]
  142. [PERF_EVSEL__MAX_ALIASES] = {
  143. { "load", "loads", "read", },
  144. { "store", "stores", "write", },
  145. { "prefetch", "prefetches", "speculative-read", "speculative-load", },
  146. };
  147. const char *perf_evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX]
  148. [PERF_EVSEL__MAX_ALIASES] = {
  149. { "refs", "Reference", "ops", "access", },
  150. { "misses", "miss", },
  151. };
  152. #define C(x) PERF_COUNT_HW_CACHE_##x
  153. #define CACHE_READ (1 << C(OP_READ))
  154. #define CACHE_WRITE (1 << C(OP_WRITE))
  155. #define CACHE_PREFETCH (1 << C(OP_PREFETCH))
  156. #define COP(x) (1 << x)
  157. /*
  158. * cache operartion stat
  159. * L1I : Read and prefetch only
  160. * ITLB and BPU : Read-only
  161. */
  162. static unsigned long perf_evsel__hw_cache_stat[C(MAX)] = {
  163. [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
  164. [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
  165. [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
  166. [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
  167. [C(ITLB)] = (CACHE_READ),
  168. [C(BPU)] = (CACHE_READ),
  169. [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
  170. };
  171. bool perf_evsel__is_cache_op_valid(u8 type, u8 op)
  172. {
  173. if (perf_evsel__hw_cache_stat[type] & COP(op))
  174. return true; /* valid */
  175. else
  176. return false; /* invalid */
  177. }
  178. int __perf_evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result,
  179. char *bf, size_t size)
  180. {
  181. if (result) {
  182. return scnprintf(bf, size, "%s-%s-%s", perf_evsel__hw_cache[type][0],
  183. perf_evsel__hw_cache_op[op][0],
  184. perf_evsel__hw_cache_result[result][0]);
  185. }
  186. return scnprintf(bf, size, "%s-%s", perf_evsel__hw_cache[type][0],
  187. perf_evsel__hw_cache_op[op][1]);
  188. }
  189. static int __perf_evsel__hw_cache_name(u64 config, char *bf, size_t size)
  190. {
  191. u8 op, result, type = (config >> 0) & 0xff;
  192. const char *err = "unknown-ext-hardware-cache-type";
  193. if (type > PERF_COUNT_HW_CACHE_MAX)
  194. goto out_err;
  195. op = (config >> 8) & 0xff;
  196. err = "unknown-ext-hardware-cache-op";
  197. if (op > PERF_COUNT_HW_CACHE_OP_MAX)
  198. goto out_err;
  199. result = (config >> 16) & 0xff;
  200. err = "unknown-ext-hardware-cache-result";
  201. if (result > PERF_COUNT_HW_CACHE_RESULT_MAX)
  202. goto out_err;
  203. err = "invalid-cache";
  204. if (!perf_evsel__is_cache_op_valid(type, op))
  205. goto out_err;
  206. return __perf_evsel__hw_cache_type_op_res_name(type, op, result, bf, size);
  207. out_err:
  208. return scnprintf(bf, size, "%s", err);
  209. }
  210. static int perf_evsel__hw_cache_name(struct perf_evsel *evsel, char *bf, size_t size)
  211. {
  212. int ret = __perf_evsel__hw_cache_name(evsel->attr.config, bf, size);
  213. return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
  214. }
  215. static int perf_evsel__raw_name(struct perf_evsel *evsel, char *bf, size_t size)
  216. {
  217. int ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->attr.config);
  218. return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
  219. }
  220. const char *perf_evsel__name(struct perf_evsel *evsel)
  221. {
  222. char bf[128];
  223. if (evsel->name)
  224. return evsel->name;
  225. switch (evsel->attr.type) {
  226. case PERF_TYPE_RAW:
  227. perf_evsel__raw_name(evsel, bf, sizeof(bf));
  228. break;
  229. case PERF_TYPE_HARDWARE:
  230. perf_evsel__hw_name(evsel, bf, sizeof(bf));
  231. break;
  232. case PERF_TYPE_HW_CACHE:
  233. perf_evsel__hw_cache_name(evsel, bf, sizeof(bf));
  234. break;
  235. case PERF_TYPE_SOFTWARE:
  236. perf_evsel__sw_name(evsel, bf, sizeof(bf));
  237. break;
  238. case PERF_TYPE_TRACEPOINT:
  239. scnprintf(bf, sizeof(bf), "%s", "unknown tracepoint");
  240. break;
  241. default:
  242. scnprintf(bf, sizeof(bf), "%s", "unknown attr type");
  243. break;
  244. }
  245. evsel->name = strdup(bf);
  246. return evsel->name ?: "unknown";
  247. }
  248. void perf_evsel__config(struct perf_evsel *evsel, struct perf_record_opts *opts,
  249. struct perf_evsel *first)
  250. {
  251. struct perf_event_attr *attr = &evsel->attr;
  252. int track = !evsel->idx; /* only the first counter needs these */
  253. attr->disabled = 1;
  254. attr->sample_id_all = opts->sample_id_all_missing ? 0 : 1;
  255. attr->inherit = !opts->no_inherit;
  256. attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
  257. PERF_FORMAT_TOTAL_TIME_RUNNING |
  258. PERF_FORMAT_ID;
  259. attr->sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID;
  260. /*
  261. * We default some events to a 1 default interval. But keep
  262. * it a weak assumption overridable by the user.
  263. */
  264. if (!attr->sample_period || (opts->user_freq != UINT_MAX &&
  265. opts->user_interval != ULLONG_MAX)) {
  266. if (opts->freq) {
  267. attr->sample_type |= PERF_SAMPLE_PERIOD;
  268. attr->freq = 1;
  269. attr->sample_freq = opts->freq;
  270. } else {
  271. attr->sample_period = opts->default_interval;
  272. }
  273. }
  274. if (opts->no_samples)
  275. attr->sample_freq = 0;
  276. if (opts->inherit_stat)
  277. attr->inherit_stat = 1;
  278. if (opts->sample_address) {
  279. attr->sample_type |= PERF_SAMPLE_ADDR;
  280. attr->mmap_data = track;
  281. }
  282. if (opts->call_graph)
  283. attr->sample_type |= PERF_SAMPLE_CALLCHAIN;
  284. if (perf_target__has_cpu(&opts->target))
  285. attr->sample_type |= PERF_SAMPLE_CPU;
  286. if (opts->period)
  287. attr->sample_type |= PERF_SAMPLE_PERIOD;
  288. if (!opts->sample_id_all_missing &&
  289. (opts->sample_time || !opts->no_inherit ||
  290. perf_target__has_cpu(&opts->target)))
  291. attr->sample_type |= PERF_SAMPLE_TIME;
  292. if (opts->raw_samples) {
  293. attr->sample_type |= PERF_SAMPLE_TIME;
  294. attr->sample_type |= PERF_SAMPLE_RAW;
  295. attr->sample_type |= PERF_SAMPLE_CPU;
  296. }
  297. if (opts->no_delay) {
  298. attr->watermark = 0;
  299. attr->wakeup_events = 1;
  300. }
  301. if (opts->branch_stack) {
  302. attr->sample_type |= PERF_SAMPLE_BRANCH_STACK;
  303. attr->branch_sample_type = opts->branch_stack;
  304. }
  305. attr->mmap = track;
  306. attr->comm = track;
  307. if (perf_target__none(&opts->target) &&
  308. (!opts->group || evsel == first)) {
  309. attr->enable_on_exec = 1;
  310. }
  311. }
  312. int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
  313. {
  314. int cpu, thread;
  315. evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
  316. if (evsel->fd) {
  317. for (cpu = 0; cpu < ncpus; cpu++) {
  318. for (thread = 0; thread < nthreads; thread++) {
  319. FD(evsel, cpu, thread) = -1;
  320. }
  321. }
  322. }
  323. return evsel->fd != NULL ? 0 : -ENOMEM;
  324. }
  325. int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
  326. {
  327. evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
  328. if (evsel->sample_id == NULL)
  329. return -ENOMEM;
  330. evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
  331. if (evsel->id == NULL) {
  332. xyarray__delete(evsel->sample_id);
  333. evsel->sample_id = NULL;
  334. return -ENOMEM;
  335. }
  336. return 0;
  337. }
  338. int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
  339. {
  340. evsel->counts = zalloc((sizeof(*evsel->counts) +
  341. (ncpus * sizeof(struct perf_counts_values))));
  342. return evsel->counts != NULL ? 0 : -ENOMEM;
  343. }
  344. void perf_evsel__free_fd(struct perf_evsel *evsel)
  345. {
  346. xyarray__delete(evsel->fd);
  347. evsel->fd = NULL;
  348. }
  349. void perf_evsel__free_id(struct perf_evsel *evsel)
  350. {
  351. xyarray__delete(evsel->sample_id);
  352. evsel->sample_id = NULL;
  353. free(evsel->id);
  354. evsel->id = NULL;
  355. }
  356. void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
  357. {
  358. int cpu, thread;
  359. for (cpu = 0; cpu < ncpus; cpu++)
  360. for (thread = 0; thread < nthreads; ++thread) {
  361. close(FD(evsel, cpu, thread));
  362. FD(evsel, cpu, thread) = -1;
  363. }
  364. }
  365. void perf_evsel__exit(struct perf_evsel *evsel)
  366. {
  367. assert(list_empty(&evsel->node));
  368. xyarray__delete(evsel->fd);
  369. xyarray__delete(evsel->sample_id);
  370. free(evsel->id);
  371. }
  372. void perf_evsel__delete(struct perf_evsel *evsel)
  373. {
  374. perf_evsel__exit(evsel);
  375. close_cgroup(evsel->cgrp);
  376. free(evsel->name);
  377. free(evsel);
  378. }
  379. int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
  380. int cpu, int thread, bool scale)
  381. {
  382. struct perf_counts_values count;
  383. size_t nv = scale ? 3 : 1;
  384. if (FD(evsel, cpu, thread) < 0)
  385. return -EINVAL;
  386. if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
  387. return -ENOMEM;
  388. if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
  389. return -errno;
  390. if (scale) {
  391. if (count.run == 0)
  392. count.val = 0;
  393. else if (count.run < count.ena)
  394. count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
  395. } else
  396. count.ena = count.run = 0;
  397. evsel->counts->cpu[cpu] = count;
  398. return 0;
  399. }
  400. int __perf_evsel__read(struct perf_evsel *evsel,
  401. int ncpus, int nthreads, bool scale)
  402. {
  403. size_t nv = scale ? 3 : 1;
  404. int cpu, thread;
  405. struct perf_counts_values *aggr = &evsel->counts->aggr, count;
  406. aggr->val = aggr->ena = aggr->run = 0;
  407. for (cpu = 0; cpu < ncpus; cpu++) {
  408. for (thread = 0; thread < nthreads; thread++) {
  409. if (FD(evsel, cpu, thread) < 0)
  410. continue;
  411. if (readn(FD(evsel, cpu, thread),
  412. &count, nv * sizeof(u64)) < 0)
  413. return -errno;
  414. aggr->val += count.val;
  415. if (scale) {
  416. aggr->ena += count.ena;
  417. aggr->run += count.run;
  418. }
  419. }
  420. }
  421. evsel->counts->scaled = 0;
  422. if (scale) {
  423. if (aggr->run == 0) {
  424. evsel->counts->scaled = -1;
  425. aggr->val = 0;
  426. return 0;
  427. }
  428. if (aggr->run < aggr->ena) {
  429. evsel->counts->scaled = 1;
  430. aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
  431. }
  432. } else
  433. aggr->ena = aggr->run = 0;
  434. return 0;
  435. }
  436. static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
  437. struct thread_map *threads, bool group,
  438. struct xyarray *group_fds)
  439. {
  440. int cpu, thread;
  441. unsigned long flags = 0;
  442. int pid = -1, err;
  443. if (evsel->fd == NULL &&
  444. perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
  445. return -ENOMEM;
  446. if (evsel->cgrp) {
  447. flags = PERF_FLAG_PID_CGROUP;
  448. pid = evsel->cgrp->fd;
  449. }
  450. for (cpu = 0; cpu < cpus->nr; cpu++) {
  451. int group_fd = group_fds ? GROUP_FD(group_fds, cpu) : -1;
  452. for (thread = 0; thread < threads->nr; thread++) {
  453. if (!evsel->cgrp)
  454. pid = threads->map[thread];
  455. FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
  456. pid,
  457. cpus->map[cpu],
  458. group_fd, flags);
  459. if (FD(evsel, cpu, thread) < 0) {
  460. err = -errno;
  461. goto out_close;
  462. }
  463. if (group && group_fd == -1)
  464. group_fd = FD(evsel, cpu, thread);
  465. }
  466. }
  467. return 0;
  468. out_close:
  469. do {
  470. while (--thread >= 0) {
  471. close(FD(evsel, cpu, thread));
  472. FD(evsel, cpu, thread) = -1;
  473. }
  474. thread = threads->nr;
  475. } while (--cpu >= 0);
  476. return err;
  477. }
  478. void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
  479. {
  480. if (evsel->fd == NULL)
  481. return;
  482. perf_evsel__close_fd(evsel, ncpus, nthreads);
  483. perf_evsel__free_fd(evsel);
  484. evsel->fd = NULL;
  485. }
  486. static struct {
  487. struct cpu_map map;
  488. int cpus[1];
  489. } empty_cpu_map = {
  490. .map.nr = 1,
  491. .cpus = { -1, },
  492. };
  493. static struct {
  494. struct thread_map map;
  495. int threads[1];
  496. } empty_thread_map = {
  497. .map.nr = 1,
  498. .threads = { -1, },
  499. };
  500. int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
  501. struct thread_map *threads, bool group,
  502. struct xyarray *group_fd)
  503. {
  504. if (cpus == NULL) {
  505. /* Work around old compiler warnings about strict aliasing */
  506. cpus = &empty_cpu_map.map;
  507. }
  508. if (threads == NULL)
  509. threads = &empty_thread_map.map;
  510. return __perf_evsel__open(evsel, cpus, threads, group, group_fd);
  511. }
  512. int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
  513. struct cpu_map *cpus, bool group,
  514. struct xyarray *group_fd)
  515. {
  516. return __perf_evsel__open(evsel, cpus, &empty_thread_map.map, group,
  517. group_fd);
  518. }
  519. int perf_evsel__open_per_thread(struct perf_evsel *evsel,
  520. struct thread_map *threads, bool group,
  521. struct xyarray *group_fd)
  522. {
  523. return __perf_evsel__open(evsel, &empty_cpu_map.map, threads, group,
  524. group_fd);
  525. }
  526. static int perf_event__parse_id_sample(const union perf_event *event, u64 type,
  527. struct perf_sample *sample,
  528. bool swapped)
  529. {
  530. const u64 *array = event->sample.array;
  531. union u64_swap u;
  532. array += ((event->header.size -
  533. sizeof(event->header)) / sizeof(u64)) - 1;
  534. if (type & PERF_SAMPLE_CPU) {
  535. u.val64 = *array;
  536. if (swapped) {
  537. /* undo swap of u64, then swap on individual u32s */
  538. u.val64 = bswap_64(u.val64);
  539. u.val32[0] = bswap_32(u.val32[0]);
  540. }
  541. sample->cpu = u.val32[0];
  542. array--;
  543. }
  544. if (type & PERF_SAMPLE_STREAM_ID) {
  545. sample->stream_id = *array;
  546. array--;
  547. }
  548. if (type & PERF_SAMPLE_ID) {
  549. sample->id = *array;
  550. array--;
  551. }
  552. if (type & PERF_SAMPLE_TIME) {
  553. sample->time = *array;
  554. array--;
  555. }
  556. if (type & PERF_SAMPLE_TID) {
  557. u.val64 = *array;
  558. if (swapped) {
  559. /* undo swap of u64, then swap on individual u32s */
  560. u.val64 = bswap_64(u.val64);
  561. u.val32[0] = bswap_32(u.val32[0]);
  562. u.val32[1] = bswap_32(u.val32[1]);
  563. }
  564. sample->pid = u.val32[0];
  565. sample->tid = u.val32[1];
  566. }
  567. return 0;
  568. }
  569. static bool sample_overlap(const union perf_event *event,
  570. const void *offset, u64 size)
  571. {
  572. const void *base = event;
  573. if (offset + size > base + event->header.size)
  574. return true;
  575. return false;
  576. }
  577. int perf_event__parse_sample(const union perf_event *event, u64 type,
  578. int sample_size, bool sample_id_all,
  579. struct perf_sample *data, bool swapped)
  580. {
  581. const u64 *array;
  582. /*
  583. * used for cross-endian analysis. See git commit 65014ab3
  584. * for why this goofiness is needed.
  585. */
  586. union u64_swap u;
  587. memset(data, 0, sizeof(*data));
  588. data->cpu = data->pid = data->tid = -1;
  589. data->stream_id = data->id = data->time = -1ULL;
  590. data->period = 1;
  591. if (event->header.type != PERF_RECORD_SAMPLE) {
  592. if (!sample_id_all)
  593. return 0;
  594. return perf_event__parse_id_sample(event, type, data, swapped);
  595. }
  596. array = event->sample.array;
  597. if (sample_size + sizeof(event->header) > event->header.size)
  598. return -EFAULT;
  599. if (type & PERF_SAMPLE_IP) {
  600. data->ip = event->ip.ip;
  601. array++;
  602. }
  603. if (type & PERF_SAMPLE_TID) {
  604. u.val64 = *array;
  605. if (swapped) {
  606. /* undo swap of u64, then swap on individual u32s */
  607. u.val64 = bswap_64(u.val64);
  608. u.val32[0] = bswap_32(u.val32[0]);
  609. u.val32[1] = bswap_32(u.val32[1]);
  610. }
  611. data->pid = u.val32[0];
  612. data->tid = u.val32[1];
  613. array++;
  614. }
  615. if (type & PERF_SAMPLE_TIME) {
  616. data->time = *array;
  617. array++;
  618. }
  619. data->addr = 0;
  620. if (type & PERF_SAMPLE_ADDR) {
  621. data->addr = *array;
  622. array++;
  623. }
  624. data->id = -1ULL;
  625. if (type & PERF_SAMPLE_ID) {
  626. data->id = *array;
  627. array++;
  628. }
  629. if (type & PERF_SAMPLE_STREAM_ID) {
  630. data->stream_id = *array;
  631. array++;
  632. }
  633. if (type & PERF_SAMPLE_CPU) {
  634. u.val64 = *array;
  635. if (swapped) {
  636. /* undo swap of u64, then swap on individual u32s */
  637. u.val64 = bswap_64(u.val64);
  638. u.val32[0] = bswap_32(u.val32[0]);
  639. }
  640. data->cpu = u.val32[0];
  641. array++;
  642. }
  643. if (type & PERF_SAMPLE_PERIOD) {
  644. data->period = *array;
  645. array++;
  646. }
  647. if (type & PERF_SAMPLE_READ) {
  648. fprintf(stderr, "PERF_SAMPLE_READ is unsupported for now\n");
  649. return -1;
  650. }
  651. if (type & PERF_SAMPLE_CALLCHAIN) {
  652. if (sample_overlap(event, array, sizeof(data->callchain->nr)))
  653. return -EFAULT;
  654. data->callchain = (struct ip_callchain *)array;
  655. if (sample_overlap(event, array, data->callchain->nr))
  656. return -EFAULT;
  657. array += 1 + data->callchain->nr;
  658. }
  659. if (type & PERF_SAMPLE_RAW) {
  660. const u64 *pdata;
  661. u.val64 = *array;
  662. if (WARN_ONCE(swapped,
  663. "Endianness of raw data not corrected!\n")) {
  664. /* undo swap of u64, then swap on individual u32s */
  665. u.val64 = bswap_64(u.val64);
  666. u.val32[0] = bswap_32(u.val32[0]);
  667. u.val32[1] = bswap_32(u.val32[1]);
  668. }
  669. if (sample_overlap(event, array, sizeof(u32)))
  670. return -EFAULT;
  671. data->raw_size = u.val32[0];
  672. pdata = (void *) array + sizeof(u32);
  673. if (sample_overlap(event, pdata, data->raw_size))
  674. return -EFAULT;
  675. data->raw_data = (void *) pdata;
  676. array = (void *)array + data->raw_size + sizeof(u32);
  677. }
  678. if (type & PERF_SAMPLE_BRANCH_STACK) {
  679. u64 sz;
  680. data->branch_stack = (struct branch_stack *)array;
  681. array++; /* nr */
  682. sz = data->branch_stack->nr * sizeof(struct branch_entry);
  683. sz /= sizeof(u64);
  684. array += sz;
  685. }
  686. return 0;
  687. }
  688. int perf_event__synthesize_sample(union perf_event *event, u64 type,
  689. const struct perf_sample *sample,
  690. bool swapped)
  691. {
  692. u64 *array;
  693. /*
  694. * used for cross-endian analysis. See git commit 65014ab3
  695. * for why this goofiness is needed.
  696. */
  697. union u64_swap u;
  698. array = event->sample.array;
  699. if (type & PERF_SAMPLE_IP) {
  700. event->ip.ip = sample->ip;
  701. array++;
  702. }
  703. if (type & PERF_SAMPLE_TID) {
  704. u.val32[0] = sample->pid;
  705. u.val32[1] = sample->tid;
  706. if (swapped) {
  707. /*
  708. * Inverse of what is done in perf_event__parse_sample
  709. */
  710. u.val32[0] = bswap_32(u.val32[0]);
  711. u.val32[1] = bswap_32(u.val32[1]);
  712. u.val64 = bswap_64(u.val64);
  713. }
  714. *array = u.val64;
  715. array++;
  716. }
  717. if (type & PERF_SAMPLE_TIME) {
  718. *array = sample->time;
  719. array++;
  720. }
  721. if (type & PERF_SAMPLE_ADDR) {
  722. *array = sample->addr;
  723. array++;
  724. }
  725. if (type & PERF_SAMPLE_ID) {
  726. *array = sample->id;
  727. array++;
  728. }
  729. if (type & PERF_SAMPLE_STREAM_ID) {
  730. *array = sample->stream_id;
  731. array++;
  732. }
  733. if (type & PERF_SAMPLE_CPU) {
  734. u.val32[0] = sample->cpu;
  735. if (swapped) {
  736. /*
  737. * Inverse of what is done in perf_event__parse_sample
  738. */
  739. u.val32[0] = bswap_32(u.val32[0]);
  740. u.val64 = bswap_64(u.val64);
  741. }
  742. *array = u.val64;
  743. array++;
  744. }
  745. if (type & PERF_SAMPLE_PERIOD) {
  746. *array = sample->period;
  747. array++;
  748. }
  749. return 0;
  750. }