evsel.c 23 KB

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