evsel.c 27 KB

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