evsel.c 17 KB

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