evsel.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  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. #include "../../include/linux/perf_event.h"
  18. #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
  19. #define GROUP_FD(group_fd, cpu) (*(int *)xyarray__entry(group_fd, cpu, 0))
  20. int __perf_evsel__sample_size(u64 sample_type)
  21. {
  22. u64 mask = sample_type & PERF_SAMPLE_MASK;
  23. int size = 0;
  24. int i;
  25. for (i = 0; i < 64; i++) {
  26. if (mask & (1ULL << i))
  27. size++;
  28. }
  29. size *= sizeof(u64);
  30. return size;
  31. }
  32. void hists__init(struct hists *hists)
  33. {
  34. memset(hists, 0, sizeof(*hists));
  35. hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
  36. hists->entries_in = &hists->entries_in_array[0];
  37. hists->entries_collapsed = RB_ROOT;
  38. hists->entries = RB_ROOT;
  39. pthread_mutex_init(&hists->lock, NULL);
  40. }
  41. void perf_evsel__init(struct perf_evsel *evsel,
  42. struct perf_event_attr *attr, int idx)
  43. {
  44. evsel->idx = idx;
  45. evsel->attr = *attr;
  46. INIT_LIST_HEAD(&evsel->node);
  47. hists__init(&evsel->hists);
  48. }
  49. struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr, int idx)
  50. {
  51. struct perf_evsel *evsel = zalloc(sizeof(*evsel));
  52. if (evsel != NULL)
  53. perf_evsel__init(evsel, attr, idx);
  54. return evsel;
  55. }
  56. static const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX] = {
  57. "cycles",
  58. "instructions",
  59. "cache-references",
  60. "cache-misses",
  61. "branches",
  62. "branch-misses",
  63. "bus-cycles",
  64. "stalled-cycles-frontend",
  65. "stalled-cycles-backend",
  66. "ref-cycles",
  67. };
  68. const char *__perf_evsel__hw_name(u64 config)
  69. {
  70. if (config < PERF_COUNT_HW_MAX && perf_evsel__hw_names[config])
  71. return perf_evsel__hw_names[config];
  72. return "unknown-hardware";
  73. }
  74. static int perf_evsel__hw_name(struct perf_evsel *evsel, char *bf, size_t size)
  75. {
  76. int colon = 0;
  77. struct perf_event_attr *attr = &evsel->attr;
  78. int r = scnprintf(bf, size, "%s", __perf_evsel__hw_name(attr->config));
  79. bool exclude_guest_default = false;
  80. #define MOD_PRINT(context, mod) do { \
  81. if (!attr->exclude_##context) { \
  82. if (!colon) colon = r++; \
  83. r += scnprintf(bf + r, size - r, "%c", mod); \
  84. } } while(0)
  85. if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) {
  86. MOD_PRINT(kernel, 'k');
  87. MOD_PRINT(user, 'u');
  88. MOD_PRINT(hv, 'h');
  89. exclude_guest_default = true;
  90. }
  91. if (attr->precise_ip) {
  92. if (!colon)
  93. colon = r++;
  94. r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp");
  95. exclude_guest_default = true;
  96. }
  97. if (attr->exclude_host || attr->exclude_guest == exclude_guest_default) {
  98. MOD_PRINT(host, 'H');
  99. MOD_PRINT(guest, 'G');
  100. }
  101. #undef MOD_PRINT
  102. if (colon)
  103. bf[colon] = ':';
  104. return r;
  105. }
  106. int perf_evsel__name(struct perf_evsel *evsel, char *bf, size_t size)
  107. {
  108. int ret;
  109. switch (evsel->attr.type) {
  110. case PERF_TYPE_RAW:
  111. ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->attr.config);
  112. break;
  113. case PERF_TYPE_HARDWARE:
  114. ret = perf_evsel__hw_name(evsel, bf, size);
  115. break;
  116. default:
  117. /*
  118. * FIXME
  119. *
  120. * This is the minimal perf_evsel__name so that we can
  121. * reconstruct event names taking into account event modifiers.
  122. *
  123. * The old event_name uses it now for raw anr hw events, so that
  124. * we don't drag all the parsing stuff into the python binding.
  125. *
  126. * On the next devel cycle the rest of the event naming will be
  127. * brought here.
  128. */
  129. return 0;
  130. }
  131. return ret;
  132. }
  133. void perf_evsel__config(struct perf_evsel *evsel, struct perf_record_opts *opts,
  134. struct perf_evsel *first)
  135. {
  136. struct perf_event_attr *attr = &evsel->attr;
  137. int track = !evsel->idx; /* only the first counter needs these */
  138. attr->disabled = 1;
  139. attr->sample_id_all = opts->sample_id_all_missing ? 0 : 1;
  140. attr->inherit = !opts->no_inherit;
  141. attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
  142. PERF_FORMAT_TOTAL_TIME_RUNNING |
  143. PERF_FORMAT_ID;
  144. attr->sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID;
  145. /*
  146. * We default some events to a 1 default interval. But keep
  147. * it a weak assumption overridable by the user.
  148. */
  149. if (!attr->sample_period || (opts->user_freq != UINT_MAX &&
  150. opts->user_interval != ULLONG_MAX)) {
  151. if (opts->freq) {
  152. attr->sample_type |= PERF_SAMPLE_PERIOD;
  153. attr->freq = 1;
  154. attr->sample_freq = opts->freq;
  155. } else {
  156. attr->sample_period = opts->default_interval;
  157. }
  158. }
  159. if (opts->no_samples)
  160. attr->sample_freq = 0;
  161. if (opts->inherit_stat)
  162. attr->inherit_stat = 1;
  163. if (opts->sample_address) {
  164. attr->sample_type |= PERF_SAMPLE_ADDR;
  165. attr->mmap_data = track;
  166. }
  167. if (opts->call_graph)
  168. attr->sample_type |= PERF_SAMPLE_CALLCHAIN;
  169. if (perf_target__has_cpu(&opts->target))
  170. attr->sample_type |= PERF_SAMPLE_CPU;
  171. if (opts->period)
  172. attr->sample_type |= PERF_SAMPLE_PERIOD;
  173. if (!opts->sample_id_all_missing &&
  174. (opts->sample_time || !opts->no_inherit ||
  175. perf_target__has_cpu(&opts->target)))
  176. attr->sample_type |= PERF_SAMPLE_TIME;
  177. if (opts->raw_samples) {
  178. attr->sample_type |= PERF_SAMPLE_TIME;
  179. attr->sample_type |= PERF_SAMPLE_RAW;
  180. attr->sample_type |= PERF_SAMPLE_CPU;
  181. }
  182. if (opts->no_delay) {
  183. attr->watermark = 0;
  184. attr->wakeup_events = 1;
  185. }
  186. if (opts->branch_stack) {
  187. attr->sample_type |= PERF_SAMPLE_BRANCH_STACK;
  188. attr->branch_sample_type = opts->branch_stack;
  189. }
  190. attr->mmap = track;
  191. attr->comm = track;
  192. if (perf_target__none(&opts->target) &&
  193. (!opts->group || evsel == first)) {
  194. attr->enable_on_exec = 1;
  195. }
  196. }
  197. int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
  198. {
  199. int cpu, thread;
  200. evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
  201. if (evsel->fd) {
  202. for (cpu = 0; cpu < ncpus; cpu++) {
  203. for (thread = 0; thread < nthreads; thread++) {
  204. FD(evsel, cpu, thread) = -1;
  205. }
  206. }
  207. }
  208. return evsel->fd != NULL ? 0 : -ENOMEM;
  209. }
  210. int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
  211. {
  212. evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
  213. if (evsel->sample_id == NULL)
  214. return -ENOMEM;
  215. evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
  216. if (evsel->id == NULL) {
  217. xyarray__delete(evsel->sample_id);
  218. evsel->sample_id = NULL;
  219. return -ENOMEM;
  220. }
  221. return 0;
  222. }
  223. int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
  224. {
  225. evsel->counts = zalloc((sizeof(*evsel->counts) +
  226. (ncpus * sizeof(struct perf_counts_values))));
  227. return evsel->counts != NULL ? 0 : -ENOMEM;
  228. }
  229. void perf_evsel__free_fd(struct perf_evsel *evsel)
  230. {
  231. xyarray__delete(evsel->fd);
  232. evsel->fd = NULL;
  233. }
  234. void perf_evsel__free_id(struct perf_evsel *evsel)
  235. {
  236. xyarray__delete(evsel->sample_id);
  237. evsel->sample_id = NULL;
  238. free(evsel->id);
  239. evsel->id = NULL;
  240. }
  241. void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
  242. {
  243. int cpu, thread;
  244. for (cpu = 0; cpu < ncpus; cpu++)
  245. for (thread = 0; thread < nthreads; ++thread) {
  246. close(FD(evsel, cpu, thread));
  247. FD(evsel, cpu, thread) = -1;
  248. }
  249. }
  250. void perf_evsel__exit(struct perf_evsel *evsel)
  251. {
  252. assert(list_empty(&evsel->node));
  253. xyarray__delete(evsel->fd);
  254. xyarray__delete(evsel->sample_id);
  255. free(evsel->id);
  256. }
  257. void perf_evsel__delete(struct perf_evsel *evsel)
  258. {
  259. perf_evsel__exit(evsel);
  260. close_cgroup(evsel->cgrp);
  261. free(evsel->name);
  262. free(evsel);
  263. }
  264. int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
  265. int cpu, int thread, bool scale)
  266. {
  267. struct perf_counts_values count;
  268. size_t nv = scale ? 3 : 1;
  269. if (FD(evsel, cpu, thread) < 0)
  270. return -EINVAL;
  271. if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
  272. return -ENOMEM;
  273. if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
  274. return -errno;
  275. if (scale) {
  276. if (count.run == 0)
  277. count.val = 0;
  278. else if (count.run < count.ena)
  279. count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
  280. } else
  281. count.ena = count.run = 0;
  282. evsel->counts->cpu[cpu] = count;
  283. return 0;
  284. }
  285. int __perf_evsel__read(struct perf_evsel *evsel,
  286. int ncpus, int nthreads, bool scale)
  287. {
  288. size_t nv = scale ? 3 : 1;
  289. int cpu, thread;
  290. struct perf_counts_values *aggr = &evsel->counts->aggr, count;
  291. aggr->val = aggr->ena = aggr->run = 0;
  292. for (cpu = 0; cpu < ncpus; cpu++) {
  293. for (thread = 0; thread < nthreads; thread++) {
  294. if (FD(evsel, cpu, thread) < 0)
  295. continue;
  296. if (readn(FD(evsel, cpu, thread),
  297. &count, nv * sizeof(u64)) < 0)
  298. return -errno;
  299. aggr->val += count.val;
  300. if (scale) {
  301. aggr->ena += count.ena;
  302. aggr->run += count.run;
  303. }
  304. }
  305. }
  306. evsel->counts->scaled = 0;
  307. if (scale) {
  308. if (aggr->run == 0) {
  309. evsel->counts->scaled = -1;
  310. aggr->val = 0;
  311. return 0;
  312. }
  313. if (aggr->run < aggr->ena) {
  314. evsel->counts->scaled = 1;
  315. aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
  316. }
  317. } else
  318. aggr->ena = aggr->run = 0;
  319. return 0;
  320. }
  321. static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
  322. struct thread_map *threads, bool group,
  323. struct xyarray *group_fds)
  324. {
  325. int cpu, thread;
  326. unsigned long flags = 0;
  327. int pid = -1, err;
  328. if (evsel->fd == NULL &&
  329. perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
  330. return -ENOMEM;
  331. if (evsel->cgrp) {
  332. flags = PERF_FLAG_PID_CGROUP;
  333. pid = evsel->cgrp->fd;
  334. }
  335. for (cpu = 0; cpu < cpus->nr; cpu++) {
  336. int group_fd = group_fds ? GROUP_FD(group_fds, cpu) : -1;
  337. for (thread = 0; thread < threads->nr; thread++) {
  338. if (!evsel->cgrp)
  339. pid = threads->map[thread];
  340. FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
  341. pid,
  342. cpus->map[cpu],
  343. group_fd, flags);
  344. if (FD(evsel, cpu, thread) < 0) {
  345. err = -errno;
  346. goto out_close;
  347. }
  348. if (group && group_fd == -1)
  349. group_fd = FD(evsel, cpu, thread);
  350. }
  351. }
  352. return 0;
  353. out_close:
  354. do {
  355. while (--thread >= 0) {
  356. close(FD(evsel, cpu, thread));
  357. FD(evsel, cpu, thread) = -1;
  358. }
  359. thread = threads->nr;
  360. } while (--cpu >= 0);
  361. return err;
  362. }
  363. void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
  364. {
  365. if (evsel->fd == NULL)
  366. return;
  367. perf_evsel__close_fd(evsel, ncpus, nthreads);
  368. perf_evsel__free_fd(evsel);
  369. evsel->fd = NULL;
  370. }
  371. static struct {
  372. struct cpu_map map;
  373. int cpus[1];
  374. } empty_cpu_map = {
  375. .map.nr = 1,
  376. .cpus = { -1, },
  377. };
  378. static struct {
  379. struct thread_map map;
  380. int threads[1];
  381. } empty_thread_map = {
  382. .map.nr = 1,
  383. .threads = { -1, },
  384. };
  385. int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
  386. struct thread_map *threads, bool group,
  387. struct xyarray *group_fd)
  388. {
  389. if (cpus == NULL) {
  390. /* Work around old compiler warnings about strict aliasing */
  391. cpus = &empty_cpu_map.map;
  392. }
  393. if (threads == NULL)
  394. threads = &empty_thread_map.map;
  395. return __perf_evsel__open(evsel, cpus, threads, group, group_fd);
  396. }
  397. int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
  398. struct cpu_map *cpus, bool group,
  399. struct xyarray *group_fd)
  400. {
  401. return __perf_evsel__open(evsel, cpus, &empty_thread_map.map, group,
  402. group_fd);
  403. }
  404. int perf_evsel__open_per_thread(struct perf_evsel *evsel,
  405. struct thread_map *threads, bool group,
  406. struct xyarray *group_fd)
  407. {
  408. return __perf_evsel__open(evsel, &empty_cpu_map.map, threads, group,
  409. group_fd);
  410. }
  411. static int perf_event__parse_id_sample(const union perf_event *event, u64 type,
  412. struct perf_sample *sample,
  413. bool swapped)
  414. {
  415. const u64 *array = event->sample.array;
  416. union u64_swap u;
  417. array += ((event->header.size -
  418. sizeof(event->header)) / sizeof(u64)) - 1;
  419. if (type & PERF_SAMPLE_CPU) {
  420. u.val64 = *array;
  421. if (swapped) {
  422. /* undo swap of u64, then swap on individual u32s */
  423. u.val64 = bswap_64(u.val64);
  424. u.val32[0] = bswap_32(u.val32[0]);
  425. }
  426. sample->cpu = u.val32[0];
  427. array--;
  428. }
  429. if (type & PERF_SAMPLE_STREAM_ID) {
  430. sample->stream_id = *array;
  431. array--;
  432. }
  433. if (type & PERF_SAMPLE_ID) {
  434. sample->id = *array;
  435. array--;
  436. }
  437. if (type & PERF_SAMPLE_TIME) {
  438. sample->time = *array;
  439. array--;
  440. }
  441. if (type & PERF_SAMPLE_TID) {
  442. u.val64 = *array;
  443. if (swapped) {
  444. /* undo swap of u64, then swap on individual u32s */
  445. u.val64 = bswap_64(u.val64);
  446. u.val32[0] = bswap_32(u.val32[0]);
  447. u.val32[1] = bswap_32(u.val32[1]);
  448. }
  449. sample->pid = u.val32[0];
  450. sample->tid = u.val32[1];
  451. }
  452. return 0;
  453. }
  454. static bool sample_overlap(const union perf_event *event,
  455. const void *offset, u64 size)
  456. {
  457. const void *base = event;
  458. if (offset + size > base + event->header.size)
  459. return true;
  460. return false;
  461. }
  462. int perf_event__parse_sample(const union perf_event *event, u64 type,
  463. int sample_size, bool sample_id_all,
  464. struct perf_sample *data, bool swapped)
  465. {
  466. const u64 *array;
  467. /*
  468. * used for cross-endian analysis. See git commit 65014ab3
  469. * for why this goofiness is needed.
  470. */
  471. union u64_swap u;
  472. memset(data, 0, sizeof(*data));
  473. data->cpu = data->pid = data->tid = -1;
  474. data->stream_id = data->id = data->time = -1ULL;
  475. data->period = 1;
  476. if (event->header.type != PERF_RECORD_SAMPLE) {
  477. if (!sample_id_all)
  478. return 0;
  479. return perf_event__parse_id_sample(event, type, data, swapped);
  480. }
  481. array = event->sample.array;
  482. if (sample_size + sizeof(event->header) > event->header.size)
  483. return -EFAULT;
  484. if (type & PERF_SAMPLE_IP) {
  485. data->ip = event->ip.ip;
  486. array++;
  487. }
  488. if (type & PERF_SAMPLE_TID) {
  489. u.val64 = *array;
  490. if (swapped) {
  491. /* undo swap of u64, then swap on individual u32s */
  492. u.val64 = bswap_64(u.val64);
  493. u.val32[0] = bswap_32(u.val32[0]);
  494. u.val32[1] = bswap_32(u.val32[1]);
  495. }
  496. data->pid = u.val32[0];
  497. data->tid = u.val32[1];
  498. array++;
  499. }
  500. if (type & PERF_SAMPLE_TIME) {
  501. data->time = *array;
  502. array++;
  503. }
  504. data->addr = 0;
  505. if (type & PERF_SAMPLE_ADDR) {
  506. data->addr = *array;
  507. array++;
  508. }
  509. data->id = -1ULL;
  510. if (type & PERF_SAMPLE_ID) {
  511. data->id = *array;
  512. array++;
  513. }
  514. if (type & PERF_SAMPLE_STREAM_ID) {
  515. data->stream_id = *array;
  516. array++;
  517. }
  518. if (type & PERF_SAMPLE_CPU) {
  519. u.val64 = *array;
  520. if (swapped) {
  521. /* undo swap of u64, then swap on individual u32s */
  522. u.val64 = bswap_64(u.val64);
  523. u.val32[0] = bswap_32(u.val32[0]);
  524. }
  525. data->cpu = u.val32[0];
  526. array++;
  527. }
  528. if (type & PERF_SAMPLE_PERIOD) {
  529. data->period = *array;
  530. array++;
  531. }
  532. if (type & PERF_SAMPLE_READ) {
  533. fprintf(stderr, "PERF_SAMPLE_READ is unsupported for now\n");
  534. return -1;
  535. }
  536. if (type & PERF_SAMPLE_CALLCHAIN) {
  537. if (sample_overlap(event, array, sizeof(data->callchain->nr)))
  538. return -EFAULT;
  539. data->callchain = (struct ip_callchain *)array;
  540. if (sample_overlap(event, array, data->callchain->nr))
  541. return -EFAULT;
  542. array += 1 + data->callchain->nr;
  543. }
  544. if (type & PERF_SAMPLE_RAW) {
  545. const u64 *pdata;
  546. u.val64 = *array;
  547. if (WARN_ONCE(swapped,
  548. "Endianness of raw data not corrected!\n")) {
  549. /* undo swap of u64, then swap on individual u32s */
  550. u.val64 = bswap_64(u.val64);
  551. u.val32[0] = bswap_32(u.val32[0]);
  552. u.val32[1] = bswap_32(u.val32[1]);
  553. }
  554. if (sample_overlap(event, array, sizeof(u32)))
  555. return -EFAULT;
  556. data->raw_size = u.val32[0];
  557. pdata = (void *) array + sizeof(u32);
  558. if (sample_overlap(event, pdata, data->raw_size))
  559. return -EFAULT;
  560. data->raw_data = (void *) pdata;
  561. array = (void *)array + data->raw_size + sizeof(u32);
  562. }
  563. if (type & PERF_SAMPLE_BRANCH_STACK) {
  564. u64 sz;
  565. data->branch_stack = (struct branch_stack *)array;
  566. array++; /* nr */
  567. sz = data->branch_stack->nr * sizeof(struct branch_entry);
  568. sz /= sizeof(u64);
  569. array += sz;
  570. }
  571. return 0;
  572. }
  573. int perf_event__synthesize_sample(union perf_event *event, u64 type,
  574. const struct perf_sample *sample,
  575. bool swapped)
  576. {
  577. u64 *array;
  578. /*
  579. * used for cross-endian analysis. See git commit 65014ab3
  580. * for why this goofiness is needed.
  581. */
  582. union u64_swap u;
  583. array = event->sample.array;
  584. if (type & PERF_SAMPLE_IP) {
  585. event->ip.ip = sample->ip;
  586. array++;
  587. }
  588. if (type & PERF_SAMPLE_TID) {
  589. u.val32[0] = sample->pid;
  590. u.val32[1] = sample->tid;
  591. if (swapped) {
  592. /*
  593. * Inverse of what is done in perf_event__parse_sample
  594. */
  595. u.val32[0] = bswap_32(u.val32[0]);
  596. u.val32[1] = bswap_32(u.val32[1]);
  597. u.val64 = bswap_64(u.val64);
  598. }
  599. *array = u.val64;
  600. array++;
  601. }
  602. if (type & PERF_SAMPLE_TIME) {
  603. *array = sample->time;
  604. array++;
  605. }
  606. if (type & PERF_SAMPLE_ADDR) {
  607. *array = sample->addr;
  608. array++;
  609. }
  610. if (type & PERF_SAMPLE_ID) {
  611. *array = sample->id;
  612. array++;
  613. }
  614. if (type & PERF_SAMPLE_STREAM_ID) {
  615. *array = sample->stream_id;
  616. array++;
  617. }
  618. if (type & PERF_SAMPLE_CPU) {
  619. u.val32[0] = sample->cpu;
  620. if (swapped) {
  621. /*
  622. * Inverse of what is done in perf_event__parse_sample
  623. */
  624. u.val32[0] = bswap_32(u.val32[0]);
  625. u.val64 = bswap_64(u.val64);
  626. }
  627. *array = u.val64;
  628. array++;
  629. }
  630. if (type & PERF_SAMPLE_PERIOD) {
  631. *array = sample->period;
  632. array++;
  633. }
  634. return 0;
  635. }