evsel.c 25 KB

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