evsel.c 22 KB

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