evsel.c 27 KB

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