evsel.c 21 KB

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