evsel.c 30 KB

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