evsel.c 21 KB

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