evsel.c 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  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), "%s", "unknown attr type");
  268. break;
  269. }
  270. evsel->name = strdup(bf);
  271. return evsel->name ?: "unknown";
  272. }
  273. void perf_evsel__config(struct perf_evsel *evsel, struct perf_record_opts *opts,
  274. struct perf_evsel *first)
  275. {
  276. struct perf_event_attr *attr = &evsel->attr;
  277. int track = !evsel->idx; /* only the first counter needs these */
  278. attr->disabled = 1;
  279. attr->sample_id_all = opts->sample_id_all_missing ? 0 : 1;
  280. attr->inherit = !opts->no_inherit;
  281. attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
  282. PERF_FORMAT_TOTAL_TIME_RUNNING |
  283. PERF_FORMAT_ID;
  284. attr->sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID;
  285. /*
  286. * We default some events to a 1 default interval. But keep
  287. * it a weak assumption overridable by the user.
  288. */
  289. if (!attr->sample_period || (opts->user_freq != UINT_MAX &&
  290. opts->user_interval != ULLONG_MAX)) {
  291. if (opts->freq) {
  292. attr->sample_type |= PERF_SAMPLE_PERIOD;
  293. attr->freq = 1;
  294. attr->sample_freq = opts->freq;
  295. } else {
  296. attr->sample_period = opts->default_interval;
  297. }
  298. }
  299. if (opts->no_samples)
  300. attr->sample_freq = 0;
  301. if (opts->inherit_stat)
  302. attr->inherit_stat = 1;
  303. if (opts->sample_address) {
  304. attr->sample_type |= PERF_SAMPLE_ADDR;
  305. attr->mmap_data = track;
  306. }
  307. if (opts->call_graph) {
  308. attr->sample_type |= PERF_SAMPLE_CALLCHAIN;
  309. if (opts->call_graph == CALLCHAIN_DWARF) {
  310. attr->sample_type |= PERF_SAMPLE_REGS_USER |
  311. PERF_SAMPLE_STACK_USER;
  312. attr->sample_regs_user = PERF_REGS_MASK;
  313. attr->sample_stack_user = opts->stack_dump_size;
  314. attr->exclude_callchain_user = 1;
  315. }
  316. }
  317. if (perf_target__has_cpu(&opts->target))
  318. attr->sample_type |= PERF_SAMPLE_CPU;
  319. if (opts->period)
  320. attr->sample_type |= PERF_SAMPLE_PERIOD;
  321. if (!opts->sample_id_all_missing &&
  322. (opts->sample_time || !opts->no_inherit ||
  323. perf_target__has_cpu(&opts->target)))
  324. attr->sample_type |= PERF_SAMPLE_TIME;
  325. if (opts->raw_samples) {
  326. attr->sample_type |= PERF_SAMPLE_TIME;
  327. attr->sample_type |= PERF_SAMPLE_RAW;
  328. attr->sample_type |= PERF_SAMPLE_CPU;
  329. }
  330. if (opts->no_delay) {
  331. attr->watermark = 0;
  332. attr->wakeup_events = 1;
  333. }
  334. if (opts->branch_stack) {
  335. attr->sample_type |= PERF_SAMPLE_BRANCH_STACK;
  336. attr->branch_sample_type = opts->branch_stack;
  337. }
  338. attr->mmap = track;
  339. attr->comm = track;
  340. if (perf_target__none(&opts->target) &&
  341. (!opts->group || evsel == first)) {
  342. attr->enable_on_exec = 1;
  343. }
  344. }
  345. int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
  346. {
  347. int cpu, thread;
  348. evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
  349. if (evsel->fd) {
  350. for (cpu = 0; cpu < ncpus; cpu++) {
  351. for (thread = 0; thread < nthreads; thread++) {
  352. FD(evsel, cpu, thread) = -1;
  353. }
  354. }
  355. }
  356. return evsel->fd != NULL ? 0 : -ENOMEM;
  357. }
  358. int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
  359. {
  360. evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
  361. if (evsel->sample_id == NULL)
  362. return -ENOMEM;
  363. evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
  364. if (evsel->id == NULL) {
  365. xyarray__delete(evsel->sample_id);
  366. evsel->sample_id = NULL;
  367. return -ENOMEM;
  368. }
  369. return 0;
  370. }
  371. int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
  372. {
  373. evsel->counts = zalloc((sizeof(*evsel->counts) +
  374. (ncpus * sizeof(struct perf_counts_values))));
  375. return evsel->counts != NULL ? 0 : -ENOMEM;
  376. }
  377. void perf_evsel__free_fd(struct perf_evsel *evsel)
  378. {
  379. xyarray__delete(evsel->fd);
  380. evsel->fd = NULL;
  381. }
  382. void perf_evsel__free_id(struct perf_evsel *evsel)
  383. {
  384. xyarray__delete(evsel->sample_id);
  385. evsel->sample_id = NULL;
  386. free(evsel->id);
  387. evsel->id = NULL;
  388. }
  389. void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
  390. {
  391. int cpu, thread;
  392. for (cpu = 0; cpu < ncpus; cpu++)
  393. for (thread = 0; thread < nthreads; ++thread) {
  394. close(FD(evsel, cpu, thread));
  395. FD(evsel, cpu, thread) = -1;
  396. }
  397. }
  398. void perf_evsel__exit(struct perf_evsel *evsel)
  399. {
  400. assert(list_empty(&evsel->node));
  401. xyarray__delete(evsel->fd);
  402. xyarray__delete(evsel->sample_id);
  403. free(evsel->id);
  404. }
  405. void perf_evsel__delete(struct perf_evsel *evsel)
  406. {
  407. perf_evsel__exit(evsel);
  408. close_cgroup(evsel->cgrp);
  409. free(evsel->group_name);
  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 get_group_fd(struct perf_evsel *evsel, int cpu, int thread)
  471. {
  472. struct perf_evsel *leader = evsel->leader;
  473. int fd;
  474. if (!leader)
  475. return -1;
  476. /*
  477. * Leader must be already processed/open,
  478. * if not it's a bug.
  479. */
  480. BUG_ON(!leader->fd);
  481. fd = FD(leader, cpu, thread);
  482. BUG_ON(fd == -1);
  483. return fd;
  484. }
  485. static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
  486. struct thread_map *threads)
  487. {
  488. int cpu, thread;
  489. unsigned long flags = 0;
  490. int pid = -1, err;
  491. if (evsel->fd == NULL &&
  492. perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
  493. return -ENOMEM;
  494. if (evsel->cgrp) {
  495. flags = PERF_FLAG_PID_CGROUP;
  496. pid = evsel->cgrp->fd;
  497. }
  498. for (cpu = 0; cpu < cpus->nr; cpu++) {
  499. for (thread = 0; thread < threads->nr; thread++) {
  500. int group_fd;
  501. if (!evsel->cgrp)
  502. pid = threads->map[thread];
  503. group_fd = get_group_fd(evsel, cpu, thread);
  504. FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
  505. pid,
  506. cpus->map[cpu],
  507. group_fd, flags);
  508. if (FD(evsel, cpu, thread) < 0) {
  509. err = -errno;
  510. goto out_close;
  511. }
  512. }
  513. }
  514. return 0;
  515. out_close:
  516. do {
  517. while (--thread >= 0) {
  518. close(FD(evsel, cpu, thread));
  519. FD(evsel, cpu, thread) = -1;
  520. }
  521. thread = threads->nr;
  522. } while (--cpu >= 0);
  523. return err;
  524. }
  525. void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
  526. {
  527. if (evsel->fd == NULL)
  528. return;
  529. perf_evsel__close_fd(evsel, ncpus, nthreads);
  530. perf_evsel__free_fd(evsel);
  531. evsel->fd = NULL;
  532. }
  533. static struct {
  534. struct cpu_map map;
  535. int cpus[1];
  536. } empty_cpu_map = {
  537. .map.nr = 1,
  538. .cpus = { -1, },
  539. };
  540. static struct {
  541. struct thread_map map;
  542. int threads[1];
  543. } empty_thread_map = {
  544. .map.nr = 1,
  545. .threads = { -1, },
  546. };
  547. int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
  548. struct thread_map *threads)
  549. {
  550. if (cpus == NULL) {
  551. /* Work around old compiler warnings about strict aliasing */
  552. cpus = &empty_cpu_map.map;
  553. }
  554. if (threads == NULL)
  555. threads = &empty_thread_map.map;
  556. return __perf_evsel__open(evsel, cpus, threads);
  557. }
  558. int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
  559. struct cpu_map *cpus)
  560. {
  561. return __perf_evsel__open(evsel, cpus, &empty_thread_map.map);
  562. }
  563. int perf_evsel__open_per_thread(struct perf_evsel *evsel,
  564. struct thread_map *threads)
  565. {
  566. return __perf_evsel__open(evsel, &empty_cpu_map.map, threads);
  567. }
  568. static int perf_event__parse_id_sample(const union perf_event *event, u64 type,
  569. struct perf_sample *sample,
  570. bool swapped)
  571. {
  572. const u64 *array = event->sample.array;
  573. union u64_swap u;
  574. array += ((event->header.size -
  575. sizeof(event->header)) / sizeof(u64)) - 1;
  576. if (type & PERF_SAMPLE_CPU) {
  577. u.val64 = *array;
  578. if (swapped) {
  579. /* undo swap of u64, then swap on individual u32s */
  580. u.val64 = bswap_64(u.val64);
  581. u.val32[0] = bswap_32(u.val32[0]);
  582. }
  583. sample->cpu = u.val32[0];
  584. array--;
  585. }
  586. if (type & PERF_SAMPLE_STREAM_ID) {
  587. sample->stream_id = *array;
  588. array--;
  589. }
  590. if (type & PERF_SAMPLE_ID) {
  591. sample->id = *array;
  592. array--;
  593. }
  594. if (type & PERF_SAMPLE_TIME) {
  595. sample->time = *array;
  596. array--;
  597. }
  598. if (type & PERF_SAMPLE_TID) {
  599. u.val64 = *array;
  600. if (swapped) {
  601. /* undo swap of u64, then swap on individual u32s */
  602. u.val64 = bswap_64(u.val64);
  603. u.val32[0] = bswap_32(u.val32[0]);
  604. u.val32[1] = bswap_32(u.val32[1]);
  605. }
  606. sample->pid = u.val32[0];
  607. sample->tid = u.val32[1];
  608. }
  609. return 0;
  610. }
  611. static bool sample_overlap(const union perf_event *event,
  612. const void *offset, u64 size)
  613. {
  614. const void *base = event;
  615. if (offset + size > base + event->header.size)
  616. return true;
  617. return false;
  618. }
  619. int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
  620. struct perf_sample *data, bool swapped)
  621. {
  622. u64 type = evsel->attr.sample_type;
  623. u64 regs_user = evsel->attr.sample_regs_user;
  624. const u64 *array;
  625. /*
  626. * used for cross-endian analysis. See git commit 65014ab3
  627. * for why this goofiness is needed.
  628. */
  629. union u64_swap u;
  630. memset(data, 0, sizeof(*data));
  631. data->cpu = data->pid = data->tid = -1;
  632. data->stream_id = data->id = data->time = -1ULL;
  633. data->period = 1;
  634. if (event->header.type != PERF_RECORD_SAMPLE) {
  635. if (!evsel->attr.sample_id_all)
  636. return 0;
  637. return perf_event__parse_id_sample(event, type, data, swapped);
  638. }
  639. array = event->sample.array;
  640. if (evsel->sample_size + sizeof(event->header) > event->header.size)
  641. return -EFAULT;
  642. if (type & PERF_SAMPLE_IP) {
  643. data->ip = event->ip.ip;
  644. array++;
  645. }
  646. if (type & PERF_SAMPLE_TID) {
  647. u.val64 = *array;
  648. if (swapped) {
  649. /* undo swap of u64, then swap on individual u32s */
  650. u.val64 = bswap_64(u.val64);
  651. u.val32[0] = bswap_32(u.val32[0]);
  652. u.val32[1] = bswap_32(u.val32[1]);
  653. }
  654. data->pid = u.val32[0];
  655. data->tid = u.val32[1];
  656. array++;
  657. }
  658. if (type & PERF_SAMPLE_TIME) {
  659. data->time = *array;
  660. array++;
  661. }
  662. data->addr = 0;
  663. if (type & PERF_SAMPLE_ADDR) {
  664. data->addr = *array;
  665. array++;
  666. }
  667. data->id = -1ULL;
  668. if (type & PERF_SAMPLE_ID) {
  669. data->id = *array;
  670. array++;
  671. }
  672. if (type & PERF_SAMPLE_STREAM_ID) {
  673. data->stream_id = *array;
  674. array++;
  675. }
  676. if (type & PERF_SAMPLE_CPU) {
  677. u.val64 = *array;
  678. if (swapped) {
  679. /* undo swap of u64, then swap on individual u32s */
  680. u.val64 = bswap_64(u.val64);
  681. u.val32[0] = bswap_32(u.val32[0]);
  682. }
  683. data->cpu = u.val32[0];
  684. array++;
  685. }
  686. if (type & PERF_SAMPLE_PERIOD) {
  687. data->period = *array;
  688. array++;
  689. }
  690. if (type & PERF_SAMPLE_READ) {
  691. fprintf(stderr, "PERF_SAMPLE_READ is unsupported for now\n");
  692. return -1;
  693. }
  694. if (type & PERF_SAMPLE_CALLCHAIN) {
  695. if (sample_overlap(event, array, sizeof(data->callchain->nr)))
  696. return -EFAULT;
  697. data->callchain = (struct ip_callchain *)array;
  698. if (sample_overlap(event, array, data->callchain->nr))
  699. return -EFAULT;
  700. array += 1 + data->callchain->nr;
  701. }
  702. if (type & PERF_SAMPLE_RAW) {
  703. const u64 *pdata;
  704. u.val64 = *array;
  705. if (WARN_ONCE(swapped,
  706. "Endianness of raw data not corrected!\n")) {
  707. /* undo swap of u64, then swap on individual u32s */
  708. u.val64 = bswap_64(u.val64);
  709. u.val32[0] = bswap_32(u.val32[0]);
  710. u.val32[1] = bswap_32(u.val32[1]);
  711. }
  712. if (sample_overlap(event, array, sizeof(u32)))
  713. return -EFAULT;
  714. data->raw_size = u.val32[0];
  715. pdata = (void *) array + sizeof(u32);
  716. if (sample_overlap(event, pdata, data->raw_size))
  717. return -EFAULT;
  718. data->raw_data = (void *) pdata;
  719. array = (void *)array + data->raw_size + sizeof(u32);
  720. }
  721. if (type & PERF_SAMPLE_BRANCH_STACK) {
  722. u64 sz;
  723. data->branch_stack = (struct branch_stack *)array;
  724. array++; /* nr */
  725. sz = data->branch_stack->nr * sizeof(struct branch_entry);
  726. sz /= sizeof(u64);
  727. array += sz;
  728. }
  729. if (type & PERF_SAMPLE_REGS_USER) {
  730. /* First u64 tells us if we have any regs in sample. */
  731. u64 avail = *array++;
  732. if (avail) {
  733. data->user_regs.regs = (u64 *)array;
  734. array += hweight_long(regs_user);
  735. }
  736. }
  737. if (type & PERF_SAMPLE_STACK_USER) {
  738. u64 size = *array++;
  739. data->user_stack.offset = ((char *)(array - 1)
  740. - (char *) event);
  741. if (!size) {
  742. data->user_stack.size = 0;
  743. } else {
  744. data->user_stack.data = (char *)array;
  745. array += size / sizeof(*array);
  746. data->user_stack.size = *array;
  747. }
  748. }
  749. return 0;
  750. }
  751. int perf_event__synthesize_sample(union perf_event *event, u64 type,
  752. const struct perf_sample *sample,
  753. bool swapped)
  754. {
  755. u64 *array;
  756. /*
  757. * used for cross-endian analysis. See git commit 65014ab3
  758. * for why this goofiness is needed.
  759. */
  760. union u64_swap u;
  761. array = event->sample.array;
  762. if (type & PERF_SAMPLE_IP) {
  763. event->ip.ip = sample->ip;
  764. array++;
  765. }
  766. if (type & PERF_SAMPLE_TID) {
  767. u.val32[0] = sample->pid;
  768. u.val32[1] = sample->tid;
  769. if (swapped) {
  770. /*
  771. * Inverse of what is done in perf_evsel__parse_sample
  772. */
  773. u.val32[0] = bswap_32(u.val32[0]);
  774. u.val32[1] = bswap_32(u.val32[1]);
  775. u.val64 = bswap_64(u.val64);
  776. }
  777. *array = u.val64;
  778. array++;
  779. }
  780. if (type & PERF_SAMPLE_TIME) {
  781. *array = sample->time;
  782. array++;
  783. }
  784. if (type & PERF_SAMPLE_ADDR) {
  785. *array = sample->addr;
  786. array++;
  787. }
  788. if (type & PERF_SAMPLE_ID) {
  789. *array = sample->id;
  790. array++;
  791. }
  792. if (type & PERF_SAMPLE_STREAM_ID) {
  793. *array = sample->stream_id;
  794. array++;
  795. }
  796. if (type & PERF_SAMPLE_CPU) {
  797. u.val32[0] = sample->cpu;
  798. if (swapped) {
  799. /*
  800. * Inverse of what is done in perf_evsel__parse_sample
  801. */
  802. u.val32[0] = bswap_32(u.val32[0]);
  803. u.val64 = bswap_64(u.val64);
  804. }
  805. *array = u.val64;
  806. array++;
  807. }
  808. if (type & PERF_SAMPLE_PERIOD) {
  809. *array = sample->period;
  810. array++;
  811. }
  812. return 0;
  813. }