evsel.c 21 KB

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