evsel.c 22 KB

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