evsel.c 22 KB

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