evsel.c 27 KB

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