evsel.c 25 KB

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