evsel.c 25 KB

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