evsel.c 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486
  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 <api/fs/tracing_path.h>
  12. #include <traceevent/event-parse.h>
  13. #include <linux/hw_breakpoint.h>
  14. #include <linux/perf_event.h>
  15. #include <linux/err.h>
  16. #include <sys/resource.h>
  17. #include "asm/bug.h"
  18. #include "callchain.h"
  19. #include "cgroup.h"
  20. #include "evsel.h"
  21. #include "evlist.h"
  22. #include "util.h"
  23. #include "cpumap.h"
  24. #include "thread_map.h"
  25. #include "target.h"
  26. #include "perf_regs.h"
  27. #include "debug.h"
  28. #include "trace-event.h"
  29. #include "stat.h"
  30. static struct {
  31. bool sample_id_all;
  32. bool exclude_guest;
  33. bool mmap2;
  34. bool cloexec;
  35. bool clockid;
  36. bool clockid_wrong;
  37. bool lbr_flags;
  38. bool write_backward;
  39. } perf_missing_features;
  40. static clockid_t clockid;
  41. static int perf_evsel__no_extra_init(struct perf_evsel *evsel __maybe_unused)
  42. {
  43. return 0;
  44. }
  45. static void perf_evsel__no_extra_fini(struct perf_evsel *evsel __maybe_unused)
  46. {
  47. }
  48. static struct {
  49. size_t size;
  50. int (*init)(struct perf_evsel *evsel);
  51. void (*fini)(struct perf_evsel *evsel);
  52. } perf_evsel__object = {
  53. .size = sizeof(struct perf_evsel),
  54. .init = perf_evsel__no_extra_init,
  55. .fini = perf_evsel__no_extra_fini,
  56. };
  57. int perf_evsel__object_config(size_t object_size,
  58. int (*init)(struct perf_evsel *evsel),
  59. void (*fini)(struct perf_evsel *evsel))
  60. {
  61. if (object_size == 0)
  62. goto set_methods;
  63. if (perf_evsel__object.size > object_size)
  64. return -EINVAL;
  65. perf_evsel__object.size = object_size;
  66. set_methods:
  67. if (init != NULL)
  68. perf_evsel__object.init = init;
  69. if (fini != NULL)
  70. perf_evsel__object.fini = fini;
  71. return 0;
  72. }
  73. #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
  74. int __perf_evsel__sample_size(u64 sample_type)
  75. {
  76. u64 mask = sample_type & PERF_SAMPLE_MASK;
  77. int size = 0;
  78. int i;
  79. for (i = 0; i < 64; i++) {
  80. if (mask & (1ULL << i))
  81. size++;
  82. }
  83. size *= sizeof(u64);
  84. return size;
  85. }
  86. /**
  87. * __perf_evsel__calc_id_pos - calculate id_pos.
  88. * @sample_type: sample type
  89. *
  90. * This function returns the position of the event id (PERF_SAMPLE_ID or
  91. * PERF_SAMPLE_IDENTIFIER) in a sample event i.e. in the array of struct
  92. * sample_event.
  93. */
  94. static int __perf_evsel__calc_id_pos(u64 sample_type)
  95. {
  96. int idx = 0;
  97. if (sample_type & PERF_SAMPLE_IDENTIFIER)
  98. return 0;
  99. if (!(sample_type & PERF_SAMPLE_ID))
  100. return -1;
  101. if (sample_type & PERF_SAMPLE_IP)
  102. idx += 1;
  103. if (sample_type & PERF_SAMPLE_TID)
  104. idx += 1;
  105. if (sample_type & PERF_SAMPLE_TIME)
  106. idx += 1;
  107. if (sample_type & PERF_SAMPLE_ADDR)
  108. idx += 1;
  109. return idx;
  110. }
  111. /**
  112. * __perf_evsel__calc_is_pos - calculate is_pos.
  113. * @sample_type: sample type
  114. *
  115. * This function returns the position (counting backwards) of the event id
  116. * (PERF_SAMPLE_ID or PERF_SAMPLE_IDENTIFIER) in a non-sample event i.e. if
  117. * sample_id_all is used there is an id sample appended to non-sample events.
  118. */
  119. static int __perf_evsel__calc_is_pos(u64 sample_type)
  120. {
  121. int idx = 1;
  122. if (sample_type & PERF_SAMPLE_IDENTIFIER)
  123. return 1;
  124. if (!(sample_type & PERF_SAMPLE_ID))
  125. return -1;
  126. if (sample_type & PERF_SAMPLE_CPU)
  127. idx += 1;
  128. if (sample_type & PERF_SAMPLE_STREAM_ID)
  129. idx += 1;
  130. return idx;
  131. }
  132. void perf_evsel__calc_id_pos(struct perf_evsel *evsel)
  133. {
  134. evsel->id_pos = __perf_evsel__calc_id_pos(evsel->attr.sample_type);
  135. evsel->is_pos = __perf_evsel__calc_is_pos(evsel->attr.sample_type);
  136. }
  137. void __perf_evsel__set_sample_bit(struct perf_evsel *evsel,
  138. enum perf_event_sample_format bit)
  139. {
  140. if (!(evsel->attr.sample_type & bit)) {
  141. evsel->attr.sample_type |= bit;
  142. evsel->sample_size += sizeof(u64);
  143. perf_evsel__calc_id_pos(evsel);
  144. }
  145. }
  146. void __perf_evsel__reset_sample_bit(struct perf_evsel *evsel,
  147. enum perf_event_sample_format bit)
  148. {
  149. if (evsel->attr.sample_type & bit) {
  150. evsel->attr.sample_type &= ~bit;
  151. evsel->sample_size -= sizeof(u64);
  152. perf_evsel__calc_id_pos(evsel);
  153. }
  154. }
  155. void perf_evsel__set_sample_id(struct perf_evsel *evsel,
  156. bool can_sample_identifier)
  157. {
  158. if (can_sample_identifier) {
  159. perf_evsel__reset_sample_bit(evsel, ID);
  160. perf_evsel__set_sample_bit(evsel, IDENTIFIER);
  161. } else {
  162. perf_evsel__set_sample_bit(evsel, ID);
  163. }
  164. evsel->attr.read_format |= PERF_FORMAT_ID;
  165. }
  166. /**
  167. * perf_evsel__is_function_event - Return whether given evsel is a function
  168. * trace event
  169. *
  170. * @evsel - evsel selector to be tested
  171. *
  172. * Return %true if event is function trace event
  173. */
  174. bool perf_evsel__is_function_event(struct perf_evsel *evsel)
  175. {
  176. #define FUNCTION_EVENT "ftrace:function"
  177. return evsel->name &&
  178. !strncmp(FUNCTION_EVENT, evsel->name, sizeof(FUNCTION_EVENT));
  179. #undef FUNCTION_EVENT
  180. }
  181. void perf_evsel__init(struct perf_evsel *evsel,
  182. struct perf_event_attr *attr, int idx)
  183. {
  184. evsel->idx = idx;
  185. evsel->tracking = !idx;
  186. evsel->attr = *attr;
  187. evsel->leader = evsel;
  188. evsel->unit = "";
  189. evsel->scale = 1.0;
  190. evsel->evlist = NULL;
  191. evsel->bpf_fd = -1;
  192. INIT_LIST_HEAD(&evsel->node);
  193. INIT_LIST_HEAD(&evsel->config_terms);
  194. perf_evsel__object.init(evsel);
  195. evsel->sample_size = __perf_evsel__sample_size(attr->sample_type);
  196. perf_evsel__calc_id_pos(evsel);
  197. evsel->cmdline_group_boundary = false;
  198. }
  199. struct perf_evsel *perf_evsel__new_idx(struct perf_event_attr *attr, int idx)
  200. {
  201. struct perf_evsel *evsel = zalloc(perf_evsel__object.size);
  202. if (evsel != NULL)
  203. perf_evsel__init(evsel, attr, idx);
  204. if (perf_evsel__is_bpf_output(evsel)) {
  205. evsel->attr.sample_type |= (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME |
  206. PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD),
  207. evsel->attr.sample_period = 1;
  208. }
  209. return evsel;
  210. }
  211. struct perf_evsel *perf_evsel__new_cycles(void)
  212. {
  213. struct perf_event_attr attr = {
  214. .type = PERF_TYPE_HARDWARE,
  215. .config = PERF_COUNT_HW_CPU_CYCLES,
  216. };
  217. struct perf_evsel *evsel;
  218. event_attr_init(&attr);
  219. perf_event_attr__set_max_precise_ip(&attr);
  220. evsel = perf_evsel__new(&attr);
  221. if (evsel == NULL)
  222. goto out;
  223. /* use asprintf() because free(evsel) assumes name is allocated */
  224. if (asprintf(&evsel->name, "cycles%.*s",
  225. attr.precise_ip ? attr.precise_ip + 1 : 0, ":ppp") < 0)
  226. goto error_free;
  227. out:
  228. return evsel;
  229. error_free:
  230. perf_evsel__delete(evsel);
  231. evsel = NULL;
  232. goto out;
  233. }
  234. /*
  235. * Returns pointer with encoded error via <linux/err.h> interface.
  236. */
  237. struct perf_evsel *perf_evsel__newtp_idx(const char *sys, const char *name, int idx)
  238. {
  239. struct perf_evsel *evsel = zalloc(perf_evsel__object.size);
  240. int err = -ENOMEM;
  241. if (evsel == NULL) {
  242. goto out_err;
  243. } else {
  244. struct perf_event_attr attr = {
  245. .type = PERF_TYPE_TRACEPOINT,
  246. .sample_type = (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME |
  247. PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD),
  248. };
  249. if (asprintf(&evsel->name, "%s:%s", sys, name) < 0)
  250. goto out_free;
  251. evsel->tp_format = trace_event__tp_format(sys, name);
  252. if (IS_ERR(evsel->tp_format)) {
  253. err = PTR_ERR(evsel->tp_format);
  254. goto out_free;
  255. }
  256. event_attr_init(&attr);
  257. attr.config = evsel->tp_format->id;
  258. attr.sample_period = 1;
  259. perf_evsel__init(evsel, &attr, idx);
  260. }
  261. return evsel;
  262. out_free:
  263. zfree(&evsel->name);
  264. free(evsel);
  265. out_err:
  266. return ERR_PTR(err);
  267. }
  268. const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX] = {
  269. "cycles",
  270. "instructions",
  271. "cache-references",
  272. "cache-misses",
  273. "branches",
  274. "branch-misses",
  275. "bus-cycles",
  276. "stalled-cycles-frontend",
  277. "stalled-cycles-backend",
  278. "ref-cycles",
  279. };
  280. static const char *__perf_evsel__hw_name(u64 config)
  281. {
  282. if (config < PERF_COUNT_HW_MAX && perf_evsel__hw_names[config])
  283. return perf_evsel__hw_names[config];
  284. return "unknown-hardware";
  285. }
  286. static int perf_evsel__add_modifiers(struct perf_evsel *evsel, char *bf, size_t size)
  287. {
  288. int colon = 0, r = 0;
  289. struct perf_event_attr *attr = &evsel->attr;
  290. bool exclude_guest_default = false;
  291. #define MOD_PRINT(context, mod) do { \
  292. if (!attr->exclude_##context) { \
  293. if (!colon) colon = ++r; \
  294. r += scnprintf(bf + r, size - r, "%c", mod); \
  295. } } while(0)
  296. if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) {
  297. MOD_PRINT(kernel, 'k');
  298. MOD_PRINT(user, 'u');
  299. MOD_PRINT(hv, 'h');
  300. exclude_guest_default = true;
  301. }
  302. if (attr->precise_ip) {
  303. if (!colon)
  304. colon = ++r;
  305. r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp");
  306. exclude_guest_default = true;
  307. }
  308. if (attr->exclude_host || attr->exclude_guest == exclude_guest_default) {
  309. MOD_PRINT(host, 'H');
  310. MOD_PRINT(guest, 'G');
  311. }
  312. #undef MOD_PRINT
  313. if (colon)
  314. bf[colon - 1] = ':';
  315. return r;
  316. }
  317. static int perf_evsel__hw_name(struct perf_evsel *evsel, char *bf, size_t size)
  318. {
  319. int r = scnprintf(bf, size, "%s", __perf_evsel__hw_name(evsel->attr.config));
  320. return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
  321. }
  322. const char *perf_evsel__sw_names[PERF_COUNT_SW_MAX] = {
  323. "cpu-clock",
  324. "task-clock",
  325. "page-faults",
  326. "context-switches",
  327. "cpu-migrations",
  328. "minor-faults",
  329. "major-faults",
  330. "alignment-faults",
  331. "emulation-faults",
  332. "dummy",
  333. };
  334. static const char *__perf_evsel__sw_name(u64 config)
  335. {
  336. if (config < PERF_COUNT_SW_MAX && perf_evsel__sw_names[config])
  337. return perf_evsel__sw_names[config];
  338. return "unknown-software";
  339. }
  340. static int perf_evsel__sw_name(struct perf_evsel *evsel, char *bf, size_t size)
  341. {
  342. int r = scnprintf(bf, size, "%s", __perf_evsel__sw_name(evsel->attr.config));
  343. return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
  344. }
  345. static int __perf_evsel__bp_name(char *bf, size_t size, u64 addr, u64 type)
  346. {
  347. int r;
  348. r = scnprintf(bf, size, "mem:0x%" PRIx64 ":", addr);
  349. if (type & HW_BREAKPOINT_R)
  350. r += scnprintf(bf + r, size - r, "r");
  351. if (type & HW_BREAKPOINT_W)
  352. r += scnprintf(bf + r, size - r, "w");
  353. if (type & HW_BREAKPOINT_X)
  354. r += scnprintf(bf + r, size - r, "x");
  355. return r;
  356. }
  357. static int perf_evsel__bp_name(struct perf_evsel *evsel, char *bf, size_t size)
  358. {
  359. struct perf_event_attr *attr = &evsel->attr;
  360. int r = __perf_evsel__bp_name(bf, size, attr->bp_addr, attr->bp_type);
  361. return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
  362. }
  363. const char *perf_evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX]
  364. [PERF_EVSEL__MAX_ALIASES] = {
  365. { "L1-dcache", "l1-d", "l1d", "L1-data", },
  366. { "L1-icache", "l1-i", "l1i", "L1-instruction", },
  367. { "LLC", "L2", },
  368. { "dTLB", "d-tlb", "Data-TLB", },
  369. { "iTLB", "i-tlb", "Instruction-TLB", },
  370. { "branch", "branches", "bpu", "btb", "bpc", },
  371. { "node", },
  372. };
  373. const char *perf_evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX]
  374. [PERF_EVSEL__MAX_ALIASES] = {
  375. { "load", "loads", "read", },
  376. { "store", "stores", "write", },
  377. { "prefetch", "prefetches", "speculative-read", "speculative-load", },
  378. };
  379. const char *perf_evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX]
  380. [PERF_EVSEL__MAX_ALIASES] = {
  381. { "refs", "Reference", "ops", "access", },
  382. { "misses", "miss", },
  383. };
  384. #define C(x) PERF_COUNT_HW_CACHE_##x
  385. #define CACHE_READ (1 << C(OP_READ))
  386. #define CACHE_WRITE (1 << C(OP_WRITE))
  387. #define CACHE_PREFETCH (1 << C(OP_PREFETCH))
  388. #define COP(x) (1 << x)
  389. /*
  390. * cache operartion stat
  391. * L1I : Read and prefetch only
  392. * ITLB and BPU : Read-only
  393. */
  394. static unsigned long perf_evsel__hw_cache_stat[C(MAX)] = {
  395. [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
  396. [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
  397. [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
  398. [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
  399. [C(ITLB)] = (CACHE_READ),
  400. [C(BPU)] = (CACHE_READ),
  401. [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
  402. };
  403. bool perf_evsel__is_cache_op_valid(u8 type, u8 op)
  404. {
  405. if (perf_evsel__hw_cache_stat[type] & COP(op))
  406. return true; /* valid */
  407. else
  408. return false; /* invalid */
  409. }
  410. int __perf_evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result,
  411. char *bf, size_t size)
  412. {
  413. if (result) {
  414. return scnprintf(bf, size, "%s-%s-%s", perf_evsel__hw_cache[type][0],
  415. perf_evsel__hw_cache_op[op][0],
  416. perf_evsel__hw_cache_result[result][0]);
  417. }
  418. return scnprintf(bf, size, "%s-%s", perf_evsel__hw_cache[type][0],
  419. perf_evsel__hw_cache_op[op][1]);
  420. }
  421. static int __perf_evsel__hw_cache_name(u64 config, char *bf, size_t size)
  422. {
  423. u8 op, result, type = (config >> 0) & 0xff;
  424. const char *err = "unknown-ext-hardware-cache-type";
  425. if (type >= PERF_COUNT_HW_CACHE_MAX)
  426. goto out_err;
  427. op = (config >> 8) & 0xff;
  428. err = "unknown-ext-hardware-cache-op";
  429. if (op >= PERF_COUNT_HW_CACHE_OP_MAX)
  430. goto out_err;
  431. result = (config >> 16) & 0xff;
  432. err = "unknown-ext-hardware-cache-result";
  433. if (result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
  434. goto out_err;
  435. err = "invalid-cache";
  436. if (!perf_evsel__is_cache_op_valid(type, op))
  437. goto out_err;
  438. return __perf_evsel__hw_cache_type_op_res_name(type, op, result, bf, size);
  439. out_err:
  440. return scnprintf(bf, size, "%s", err);
  441. }
  442. static int perf_evsel__hw_cache_name(struct perf_evsel *evsel, char *bf, size_t size)
  443. {
  444. int ret = __perf_evsel__hw_cache_name(evsel->attr.config, bf, size);
  445. return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
  446. }
  447. static int perf_evsel__raw_name(struct perf_evsel *evsel, char *bf, size_t size)
  448. {
  449. int ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->attr.config);
  450. return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
  451. }
  452. const char *perf_evsel__name(struct perf_evsel *evsel)
  453. {
  454. char bf[128];
  455. if (evsel->name)
  456. return evsel->name;
  457. switch (evsel->attr.type) {
  458. case PERF_TYPE_RAW:
  459. perf_evsel__raw_name(evsel, bf, sizeof(bf));
  460. break;
  461. case PERF_TYPE_HARDWARE:
  462. perf_evsel__hw_name(evsel, bf, sizeof(bf));
  463. break;
  464. case PERF_TYPE_HW_CACHE:
  465. perf_evsel__hw_cache_name(evsel, bf, sizeof(bf));
  466. break;
  467. case PERF_TYPE_SOFTWARE:
  468. perf_evsel__sw_name(evsel, bf, sizeof(bf));
  469. break;
  470. case PERF_TYPE_TRACEPOINT:
  471. scnprintf(bf, sizeof(bf), "%s", "unknown tracepoint");
  472. break;
  473. case PERF_TYPE_BREAKPOINT:
  474. perf_evsel__bp_name(evsel, bf, sizeof(bf));
  475. break;
  476. default:
  477. scnprintf(bf, sizeof(bf), "unknown attr type: %d",
  478. evsel->attr.type);
  479. break;
  480. }
  481. evsel->name = strdup(bf);
  482. return evsel->name ?: "unknown";
  483. }
  484. const char *perf_evsel__group_name(struct perf_evsel *evsel)
  485. {
  486. return evsel->group_name ?: "anon group";
  487. }
  488. int perf_evsel__group_desc(struct perf_evsel *evsel, char *buf, size_t size)
  489. {
  490. int ret;
  491. struct perf_evsel *pos;
  492. const char *group_name = perf_evsel__group_name(evsel);
  493. ret = scnprintf(buf, size, "%s", group_name);
  494. ret += scnprintf(buf + ret, size - ret, " { %s",
  495. perf_evsel__name(evsel));
  496. for_each_group_member(pos, evsel)
  497. ret += scnprintf(buf + ret, size - ret, ", %s",
  498. perf_evsel__name(pos));
  499. ret += scnprintf(buf + ret, size - ret, " }");
  500. return ret;
  501. }
  502. void perf_evsel__config_callchain(struct perf_evsel *evsel,
  503. struct record_opts *opts,
  504. struct callchain_param *param)
  505. {
  506. bool function = perf_evsel__is_function_event(evsel);
  507. struct perf_event_attr *attr = &evsel->attr;
  508. perf_evsel__set_sample_bit(evsel, CALLCHAIN);
  509. attr->sample_max_stack = param->max_stack;
  510. if (param->record_mode == CALLCHAIN_LBR) {
  511. if (!opts->branch_stack) {
  512. if (attr->exclude_user) {
  513. pr_warning("LBR callstack option is only available "
  514. "to get user callchain information. "
  515. "Falling back to framepointers.\n");
  516. } else {
  517. perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
  518. attr->branch_sample_type = PERF_SAMPLE_BRANCH_USER |
  519. PERF_SAMPLE_BRANCH_CALL_STACK |
  520. PERF_SAMPLE_BRANCH_NO_CYCLES |
  521. PERF_SAMPLE_BRANCH_NO_FLAGS;
  522. }
  523. } else
  524. pr_warning("Cannot use LBR callstack with branch stack. "
  525. "Falling back to framepointers.\n");
  526. }
  527. if (param->record_mode == CALLCHAIN_DWARF) {
  528. if (!function) {
  529. perf_evsel__set_sample_bit(evsel, REGS_USER);
  530. perf_evsel__set_sample_bit(evsel, STACK_USER);
  531. attr->sample_regs_user = PERF_REGS_MASK;
  532. attr->sample_stack_user = param->dump_size;
  533. attr->exclude_callchain_user = 1;
  534. } else {
  535. pr_info("Cannot use DWARF unwind for function trace event,"
  536. " falling back to framepointers.\n");
  537. }
  538. }
  539. if (function) {
  540. pr_info("Disabling user space callchains for function trace event.\n");
  541. attr->exclude_callchain_user = 1;
  542. }
  543. }
  544. static void
  545. perf_evsel__reset_callgraph(struct perf_evsel *evsel,
  546. struct callchain_param *param)
  547. {
  548. struct perf_event_attr *attr = &evsel->attr;
  549. perf_evsel__reset_sample_bit(evsel, CALLCHAIN);
  550. if (param->record_mode == CALLCHAIN_LBR) {
  551. perf_evsel__reset_sample_bit(evsel, BRANCH_STACK);
  552. attr->branch_sample_type &= ~(PERF_SAMPLE_BRANCH_USER |
  553. PERF_SAMPLE_BRANCH_CALL_STACK);
  554. }
  555. if (param->record_mode == CALLCHAIN_DWARF) {
  556. perf_evsel__reset_sample_bit(evsel, REGS_USER);
  557. perf_evsel__reset_sample_bit(evsel, STACK_USER);
  558. }
  559. }
  560. static void apply_config_terms(struct perf_evsel *evsel,
  561. struct record_opts *opts)
  562. {
  563. struct perf_evsel_config_term *term;
  564. struct list_head *config_terms = &evsel->config_terms;
  565. struct perf_event_attr *attr = &evsel->attr;
  566. struct callchain_param param;
  567. u32 dump_size = 0;
  568. int max_stack = 0;
  569. const char *callgraph_buf = NULL;
  570. /* callgraph default */
  571. param.record_mode = callchain_param.record_mode;
  572. list_for_each_entry(term, config_terms, list) {
  573. switch (term->type) {
  574. case PERF_EVSEL__CONFIG_TERM_PERIOD:
  575. attr->sample_period = term->val.period;
  576. attr->freq = 0;
  577. break;
  578. case PERF_EVSEL__CONFIG_TERM_FREQ:
  579. attr->sample_freq = term->val.freq;
  580. attr->freq = 1;
  581. break;
  582. case PERF_EVSEL__CONFIG_TERM_TIME:
  583. if (term->val.time)
  584. perf_evsel__set_sample_bit(evsel, TIME);
  585. else
  586. perf_evsel__reset_sample_bit(evsel, TIME);
  587. break;
  588. case PERF_EVSEL__CONFIG_TERM_CALLGRAPH:
  589. callgraph_buf = term->val.callgraph;
  590. break;
  591. case PERF_EVSEL__CONFIG_TERM_STACK_USER:
  592. dump_size = term->val.stack_user;
  593. break;
  594. case PERF_EVSEL__CONFIG_TERM_MAX_STACK:
  595. max_stack = term->val.max_stack;
  596. break;
  597. case PERF_EVSEL__CONFIG_TERM_INHERIT:
  598. /*
  599. * attr->inherit should has already been set by
  600. * perf_evsel__config. If user explicitly set
  601. * inherit using config terms, override global
  602. * opt->no_inherit setting.
  603. */
  604. attr->inherit = term->val.inherit ? 1 : 0;
  605. break;
  606. case PERF_EVSEL__CONFIG_TERM_OVERWRITE:
  607. attr->write_backward = term->val.overwrite ? 1 : 0;
  608. break;
  609. default:
  610. break;
  611. }
  612. }
  613. /* User explicitly set per-event callgraph, clear the old setting and reset. */
  614. if ((callgraph_buf != NULL) || (dump_size > 0) || max_stack) {
  615. if (max_stack) {
  616. param.max_stack = max_stack;
  617. if (callgraph_buf == NULL)
  618. callgraph_buf = "fp";
  619. }
  620. /* parse callgraph parameters */
  621. if (callgraph_buf != NULL) {
  622. if (!strcmp(callgraph_buf, "no")) {
  623. param.enabled = false;
  624. param.record_mode = CALLCHAIN_NONE;
  625. } else {
  626. param.enabled = true;
  627. if (parse_callchain_record(callgraph_buf, &param)) {
  628. pr_err("per-event callgraph setting for %s failed. "
  629. "Apply callgraph global setting for it\n",
  630. evsel->name);
  631. return;
  632. }
  633. }
  634. }
  635. if (dump_size > 0) {
  636. dump_size = round_up(dump_size, sizeof(u64));
  637. param.dump_size = dump_size;
  638. }
  639. /* If global callgraph set, clear it */
  640. if (callchain_param.enabled)
  641. perf_evsel__reset_callgraph(evsel, &callchain_param);
  642. /* set perf-event callgraph */
  643. if (param.enabled)
  644. perf_evsel__config_callchain(evsel, opts, &param);
  645. }
  646. }
  647. /*
  648. * The enable_on_exec/disabled value strategy:
  649. *
  650. * 1) For any type of traced program:
  651. * - all independent events and group leaders are disabled
  652. * - all group members are enabled
  653. *
  654. * Group members are ruled by group leaders. They need to
  655. * be enabled, because the group scheduling relies on that.
  656. *
  657. * 2) For traced programs executed by perf:
  658. * - all independent events and group leaders have
  659. * enable_on_exec set
  660. * - we don't specifically enable or disable any event during
  661. * the record command
  662. *
  663. * Independent events and group leaders are initially disabled
  664. * and get enabled by exec. Group members are ruled by group
  665. * leaders as stated in 1).
  666. *
  667. * 3) For traced programs attached by perf (pid/tid):
  668. * - we specifically enable or disable all events during
  669. * the record command
  670. *
  671. * When attaching events to already running traced we
  672. * enable/disable events specifically, as there's no
  673. * initial traced exec call.
  674. */
  675. void perf_evsel__config(struct perf_evsel *evsel, struct record_opts *opts,
  676. struct callchain_param *callchain)
  677. {
  678. struct perf_evsel *leader = evsel->leader;
  679. struct perf_event_attr *attr = &evsel->attr;
  680. int track = evsel->tracking;
  681. bool per_cpu = opts->target.default_per_cpu && !opts->target.per_thread;
  682. attr->sample_id_all = perf_missing_features.sample_id_all ? 0 : 1;
  683. attr->inherit = !opts->no_inherit;
  684. attr->write_backward = opts->overwrite ? 1 : 0;
  685. perf_evsel__set_sample_bit(evsel, IP);
  686. perf_evsel__set_sample_bit(evsel, TID);
  687. if (evsel->sample_read) {
  688. perf_evsel__set_sample_bit(evsel, READ);
  689. /*
  690. * We need ID even in case of single event, because
  691. * PERF_SAMPLE_READ process ID specific data.
  692. */
  693. perf_evsel__set_sample_id(evsel, false);
  694. /*
  695. * Apply group format only if we belong to group
  696. * with more than one members.
  697. */
  698. if (leader->nr_members > 1) {
  699. attr->read_format |= PERF_FORMAT_GROUP;
  700. attr->inherit = 0;
  701. }
  702. }
  703. /*
  704. * We default some events to have a default interval. But keep
  705. * it a weak assumption overridable by the user.
  706. */
  707. if (!attr->sample_period || (opts->user_freq != UINT_MAX ||
  708. opts->user_interval != ULLONG_MAX)) {
  709. if (opts->freq) {
  710. perf_evsel__set_sample_bit(evsel, PERIOD);
  711. attr->freq = 1;
  712. attr->sample_freq = opts->freq;
  713. } else {
  714. attr->sample_period = opts->default_interval;
  715. }
  716. }
  717. /*
  718. * Disable sampling for all group members other
  719. * than leader in case leader 'leads' the sampling.
  720. */
  721. if ((leader != evsel) && leader->sample_read) {
  722. attr->sample_freq = 0;
  723. attr->sample_period = 0;
  724. }
  725. if (opts->no_samples)
  726. attr->sample_freq = 0;
  727. if (opts->inherit_stat)
  728. attr->inherit_stat = 1;
  729. if (opts->sample_address) {
  730. perf_evsel__set_sample_bit(evsel, ADDR);
  731. attr->mmap_data = track;
  732. }
  733. /*
  734. * We don't allow user space callchains for function trace
  735. * event, due to issues with page faults while tracing page
  736. * fault handler and its overall trickiness nature.
  737. */
  738. if (perf_evsel__is_function_event(evsel))
  739. evsel->attr.exclude_callchain_user = 1;
  740. if (callchain && callchain->enabled && !evsel->no_aux_samples)
  741. perf_evsel__config_callchain(evsel, opts, callchain);
  742. if (opts->sample_intr_regs) {
  743. attr->sample_regs_intr = opts->sample_intr_regs;
  744. perf_evsel__set_sample_bit(evsel, REGS_INTR);
  745. }
  746. if (target__has_cpu(&opts->target) || opts->sample_cpu)
  747. perf_evsel__set_sample_bit(evsel, CPU);
  748. if (opts->period)
  749. perf_evsel__set_sample_bit(evsel, PERIOD);
  750. /*
  751. * When the user explicitly disabled time don't force it here.
  752. */
  753. if (opts->sample_time &&
  754. (!perf_missing_features.sample_id_all &&
  755. (!opts->no_inherit || target__has_cpu(&opts->target) || per_cpu ||
  756. opts->sample_time_set)))
  757. perf_evsel__set_sample_bit(evsel, TIME);
  758. if (opts->raw_samples && !evsel->no_aux_samples) {
  759. perf_evsel__set_sample_bit(evsel, TIME);
  760. perf_evsel__set_sample_bit(evsel, RAW);
  761. perf_evsel__set_sample_bit(evsel, CPU);
  762. }
  763. if (opts->sample_address)
  764. perf_evsel__set_sample_bit(evsel, DATA_SRC);
  765. if (opts->no_buffering) {
  766. attr->watermark = 0;
  767. attr->wakeup_events = 1;
  768. }
  769. if (opts->branch_stack && !evsel->no_aux_samples) {
  770. perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
  771. attr->branch_sample_type = opts->branch_stack;
  772. }
  773. if (opts->sample_weight)
  774. perf_evsel__set_sample_bit(evsel, WEIGHT);
  775. attr->task = track;
  776. attr->mmap = track;
  777. attr->mmap2 = track && !perf_missing_features.mmap2;
  778. attr->comm = track;
  779. if (opts->record_switch_events)
  780. attr->context_switch = track;
  781. if (opts->sample_transaction)
  782. perf_evsel__set_sample_bit(evsel, TRANSACTION);
  783. if (opts->running_time) {
  784. evsel->attr.read_format |=
  785. PERF_FORMAT_TOTAL_TIME_ENABLED |
  786. PERF_FORMAT_TOTAL_TIME_RUNNING;
  787. }
  788. /*
  789. * XXX see the function comment above
  790. *
  791. * Disabling only independent events or group leaders,
  792. * keeping group members enabled.
  793. */
  794. if (perf_evsel__is_group_leader(evsel))
  795. attr->disabled = 1;
  796. /*
  797. * Setting enable_on_exec for independent events and
  798. * group leaders for traced executed by perf.
  799. */
  800. if (target__none(&opts->target) && perf_evsel__is_group_leader(evsel) &&
  801. !opts->initial_delay)
  802. attr->enable_on_exec = 1;
  803. if (evsel->immediate) {
  804. attr->disabled = 0;
  805. attr->enable_on_exec = 0;
  806. }
  807. clockid = opts->clockid;
  808. if (opts->use_clockid) {
  809. attr->use_clockid = 1;
  810. attr->clockid = opts->clockid;
  811. }
  812. if (evsel->precise_max)
  813. perf_event_attr__set_max_precise_ip(attr);
  814. if (opts->all_user) {
  815. attr->exclude_kernel = 1;
  816. attr->exclude_user = 0;
  817. }
  818. if (opts->all_kernel) {
  819. attr->exclude_kernel = 0;
  820. attr->exclude_user = 1;
  821. }
  822. /*
  823. * Apply event specific term settings,
  824. * it overloads any global configuration.
  825. */
  826. apply_config_terms(evsel, opts);
  827. }
  828. static int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
  829. {
  830. if (evsel->system_wide)
  831. nthreads = 1;
  832. evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
  833. if (evsel->fd) {
  834. int cpu, thread;
  835. for (cpu = 0; cpu < ncpus; cpu++) {
  836. for (thread = 0; thread < nthreads; thread++) {
  837. FD(evsel, cpu, thread) = -1;
  838. }
  839. }
  840. }
  841. return evsel->fd != NULL ? 0 : -ENOMEM;
  842. }
  843. static int perf_evsel__run_ioctl(struct perf_evsel *evsel, int ncpus, int nthreads,
  844. int ioc, void *arg)
  845. {
  846. int cpu, thread;
  847. if (evsel->system_wide)
  848. nthreads = 1;
  849. for (cpu = 0; cpu < ncpus; cpu++) {
  850. for (thread = 0; thread < nthreads; thread++) {
  851. int fd = FD(evsel, cpu, thread),
  852. err = ioctl(fd, ioc, arg);
  853. if (err)
  854. return err;
  855. }
  856. }
  857. return 0;
  858. }
  859. int perf_evsel__apply_filter(struct perf_evsel *evsel, int ncpus, int nthreads,
  860. const char *filter)
  861. {
  862. return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
  863. PERF_EVENT_IOC_SET_FILTER,
  864. (void *)filter);
  865. }
  866. int perf_evsel__set_filter(struct perf_evsel *evsel, const char *filter)
  867. {
  868. char *new_filter = strdup(filter);
  869. if (new_filter != NULL) {
  870. free(evsel->filter);
  871. evsel->filter = new_filter;
  872. return 0;
  873. }
  874. return -1;
  875. }
  876. static int perf_evsel__append_filter(struct perf_evsel *evsel,
  877. const char *fmt, const char *filter)
  878. {
  879. char *new_filter;
  880. if (evsel->filter == NULL)
  881. return perf_evsel__set_filter(evsel, filter);
  882. if (asprintf(&new_filter, fmt, evsel->filter, filter) > 0) {
  883. free(evsel->filter);
  884. evsel->filter = new_filter;
  885. return 0;
  886. }
  887. return -1;
  888. }
  889. int perf_evsel__append_tp_filter(struct perf_evsel *evsel, const char *filter)
  890. {
  891. return perf_evsel__append_filter(evsel, "(%s) && (%s)", filter);
  892. }
  893. int perf_evsel__append_addr_filter(struct perf_evsel *evsel, const char *filter)
  894. {
  895. return perf_evsel__append_filter(evsel, "%s,%s", filter);
  896. }
  897. int perf_evsel__enable(struct perf_evsel *evsel)
  898. {
  899. int nthreads = thread_map__nr(evsel->threads);
  900. int ncpus = cpu_map__nr(evsel->cpus);
  901. return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
  902. PERF_EVENT_IOC_ENABLE,
  903. 0);
  904. }
  905. int perf_evsel__disable(struct perf_evsel *evsel)
  906. {
  907. int nthreads = thread_map__nr(evsel->threads);
  908. int ncpus = cpu_map__nr(evsel->cpus);
  909. return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
  910. PERF_EVENT_IOC_DISABLE,
  911. 0);
  912. }
  913. int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
  914. {
  915. if (ncpus == 0 || nthreads == 0)
  916. return 0;
  917. if (evsel->system_wide)
  918. nthreads = 1;
  919. evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
  920. if (evsel->sample_id == NULL)
  921. return -ENOMEM;
  922. evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
  923. if (evsel->id == NULL) {
  924. xyarray__delete(evsel->sample_id);
  925. evsel->sample_id = NULL;
  926. return -ENOMEM;
  927. }
  928. return 0;
  929. }
  930. static void perf_evsel__free_fd(struct perf_evsel *evsel)
  931. {
  932. xyarray__delete(evsel->fd);
  933. evsel->fd = NULL;
  934. }
  935. static void perf_evsel__free_id(struct perf_evsel *evsel)
  936. {
  937. xyarray__delete(evsel->sample_id);
  938. evsel->sample_id = NULL;
  939. zfree(&evsel->id);
  940. }
  941. static void perf_evsel__free_config_terms(struct perf_evsel *evsel)
  942. {
  943. struct perf_evsel_config_term *term, *h;
  944. list_for_each_entry_safe(term, h, &evsel->config_terms, list) {
  945. list_del(&term->list);
  946. free(term);
  947. }
  948. }
  949. void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
  950. {
  951. int cpu, thread;
  952. if (evsel->system_wide)
  953. nthreads = 1;
  954. for (cpu = 0; cpu < ncpus; cpu++)
  955. for (thread = 0; thread < nthreads; ++thread) {
  956. close(FD(evsel, cpu, thread));
  957. FD(evsel, cpu, thread) = -1;
  958. }
  959. }
  960. void perf_evsel__exit(struct perf_evsel *evsel)
  961. {
  962. assert(list_empty(&evsel->node));
  963. assert(evsel->evlist == NULL);
  964. perf_evsel__free_fd(evsel);
  965. perf_evsel__free_id(evsel);
  966. perf_evsel__free_config_terms(evsel);
  967. close_cgroup(evsel->cgrp);
  968. cpu_map__put(evsel->cpus);
  969. cpu_map__put(evsel->own_cpus);
  970. thread_map__put(evsel->threads);
  971. zfree(&evsel->group_name);
  972. zfree(&evsel->name);
  973. perf_evsel__object.fini(evsel);
  974. }
  975. void perf_evsel__delete(struct perf_evsel *evsel)
  976. {
  977. perf_evsel__exit(evsel);
  978. free(evsel);
  979. }
  980. void perf_evsel__compute_deltas(struct perf_evsel *evsel, int cpu, int thread,
  981. struct perf_counts_values *count)
  982. {
  983. struct perf_counts_values tmp;
  984. if (!evsel->prev_raw_counts)
  985. return;
  986. if (cpu == -1) {
  987. tmp = evsel->prev_raw_counts->aggr;
  988. evsel->prev_raw_counts->aggr = *count;
  989. } else {
  990. tmp = *perf_counts(evsel->prev_raw_counts, cpu, thread);
  991. *perf_counts(evsel->prev_raw_counts, cpu, thread) = *count;
  992. }
  993. count->val = count->val - tmp.val;
  994. count->ena = count->ena - tmp.ena;
  995. count->run = count->run - tmp.run;
  996. }
  997. void perf_counts_values__scale(struct perf_counts_values *count,
  998. bool scale, s8 *pscaled)
  999. {
  1000. s8 scaled = 0;
  1001. if (scale) {
  1002. if (count->run == 0) {
  1003. scaled = -1;
  1004. count->val = 0;
  1005. } else if (count->run < count->ena) {
  1006. scaled = 1;
  1007. count->val = (u64)((double) count->val * count->ena / count->run + 0.5);
  1008. }
  1009. } else
  1010. count->ena = count->run = 0;
  1011. if (pscaled)
  1012. *pscaled = scaled;
  1013. }
  1014. int perf_evsel__read(struct perf_evsel *evsel, int cpu, int thread,
  1015. struct perf_counts_values *count)
  1016. {
  1017. memset(count, 0, sizeof(*count));
  1018. if (FD(evsel, cpu, thread) < 0)
  1019. return -EINVAL;
  1020. if (readn(FD(evsel, cpu, thread), count, sizeof(*count)) < 0)
  1021. return -errno;
  1022. return 0;
  1023. }
  1024. int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
  1025. int cpu, int thread, bool scale)
  1026. {
  1027. struct perf_counts_values count;
  1028. size_t nv = scale ? 3 : 1;
  1029. if (FD(evsel, cpu, thread) < 0)
  1030. return -EINVAL;
  1031. if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1, thread + 1) < 0)
  1032. return -ENOMEM;
  1033. if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
  1034. return -errno;
  1035. perf_evsel__compute_deltas(evsel, cpu, thread, &count);
  1036. perf_counts_values__scale(&count, scale, NULL);
  1037. *perf_counts(evsel->counts, cpu, thread) = count;
  1038. return 0;
  1039. }
  1040. static int get_group_fd(struct perf_evsel *evsel, int cpu, int thread)
  1041. {
  1042. struct perf_evsel *leader = evsel->leader;
  1043. int fd;
  1044. if (perf_evsel__is_group_leader(evsel))
  1045. return -1;
  1046. /*
  1047. * Leader must be already processed/open,
  1048. * if not it's a bug.
  1049. */
  1050. BUG_ON(!leader->fd);
  1051. fd = FD(leader, cpu, thread);
  1052. BUG_ON(fd == -1);
  1053. return fd;
  1054. }
  1055. struct bit_names {
  1056. int bit;
  1057. const char *name;
  1058. };
  1059. static void __p_bits(char *buf, size_t size, u64 value, struct bit_names *bits)
  1060. {
  1061. bool first_bit = true;
  1062. int i = 0;
  1063. do {
  1064. if (value & bits[i].bit) {
  1065. buf += scnprintf(buf, size, "%s%s", first_bit ? "" : "|", bits[i].name);
  1066. first_bit = false;
  1067. }
  1068. } while (bits[++i].name != NULL);
  1069. }
  1070. static void __p_sample_type(char *buf, size_t size, u64 value)
  1071. {
  1072. #define bit_name(n) { PERF_SAMPLE_##n, #n }
  1073. struct bit_names bits[] = {
  1074. bit_name(IP), bit_name(TID), bit_name(TIME), bit_name(ADDR),
  1075. bit_name(READ), bit_name(CALLCHAIN), bit_name(ID), bit_name(CPU),
  1076. bit_name(PERIOD), bit_name(STREAM_ID), bit_name(RAW),
  1077. bit_name(BRANCH_STACK), bit_name(REGS_USER), bit_name(STACK_USER),
  1078. bit_name(IDENTIFIER), bit_name(REGS_INTR), bit_name(DATA_SRC),
  1079. bit_name(WEIGHT),
  1080. { .name = NULL, }
  1081. };
  1082. #undef bit_name
  1083. __p_bits(buf, size, value, bits);
  1084. }
  1085. static void __p_branch_sample_type(char *buf, size_t size, u64 value)
  1086. {
  1087. #define bit_name(n) { PERF_SAMPLE_BRANCH_##n, #n }
  1088. struct bit_names bits[] = {
  1089. bit_name(USER), bit_name(KERNEL), bit_name(HV), bit_name(ANY),
  1090. bit_name(ANY_CALL), bit_name(ANY_RETURN), bit_name(IND_CALL),
  1091. bit_name(ABORT_TX), bit_name(IN_TX), bit_name(NO_TX),
  1092. bit_name(COND), bit_name(CALL_STACK), bit_name(IND_JUMP),
  1093. bit_name(CALL), bit_name(NO_FLAGS), bit_name(NO_CYCLES),
  1094. { .name = NULL, }
  1095. };
  1096. #undef bit_name
  1097. __p_bits(buf, size, value, bits);
  1098. }
  1099. static void __p_read_format(char *buf, size_t size, u64 value)
  1100. {
  1101. #define bit_name(n) { PERF_FORMAT_##n, #n }
  1102. struct bit_names bits[] = {
  1103. bit_name(TOTAL_TIME_ENABLED), bit_name(TOTAL_TIME_RUNNING),
  1104. bit_name(ID), bit_name(GROUP),
  1105. { .name = NULL, }
  1106. };
  1107. #undef bit_name
  1108. __p_bits(buf, size, value, bits);
  1109. }
  1110. #define BUF_SIZE 1024
  1111. #define p_hex(val) snprintf(buf, BUF_SIZE, "%#"PRIx64, (uint64_t)(val))
  1112. #define p_unsigned(val) snprintf(buf, BUF_SIZE, "%"PRIu64, (uint64_t)(val))
  1113. #define p_signed(val) snprintf(buf, BUF_SIZE, "%"PRId64, (int64_t)(val))
  1114. #define p_sample_type(val) __p_sample_type(buf, BUF_SIZE, val)
  1115. #define p_branch_sample_type(val) __p_branch_sample_type(buf, BUF_SIZE, val)
  1116. #define p_read_format(val) __p_read_format(buf, BUF_SIZE, val)
  1117. #define PRINT_ATTRn(_n, _f, _p) \
  1118. do { \
  1119. if (attr->_f) { \
  1120. _p(attr->_f); \
  1121. ret += attr__fprintf(fp, _n, buf, priv);\
  1122. } \
  1123. } while (0)
  1124. #define PRINT_ATTRf(_f, _p) PRINT_ATTRn(#_f, _f, _p)
  1125. int perf_event_attr__fprintf(FILE *fp, struct perf_event_attr *attr,
  1126. attr__fprintf_f attr__fprintf, void *priv)
  1127. {
  1128. char buf[BUF_SIZE];
  1129. int ret = 0;
  1130. PRINT_ATTRf(type, p_unsigned);
  1131. PRINT_ATTRf(size, p_unsigned);
  1132. PRINT_ATTRf(config, p_hex);
  1133. PRINT_ATTRn("{ sample_period, sample_freq }", sample_period, p_unsigned);
  1134. PRINT_ATTRf(sample_type, p_sample_type);
  1135. PRINT_ATTRf(read_format, p_read_format);
  1136. PRINT_ATTRf(disabled, p_unsigned);
  1137. PRINT_ATTRf(inherit, p_unsigned);
  1138. PRINT_ATTRf(pinned, p_unsigned);
  1139. PRINT_ATTRf(exclusive, p_unsigned);
  1140. PRINT_ATTRf(exclude_user, p_unsigned);
  1141. PRINT_ATTRf(exclude_kernel, p_unsigned);
  1142. PRINT_ATTRf(exclude_hv, p_unsigned);
  1143. PRINT_ATTRf(exclude_idle, p_unsigned);
  1144. PRINT_ATTRf(mmap, p_unsigned);
  1145. PRINT_ATTRf(comm, p_unsigned);
  1146. PRINT_ATTRf(freq, p_unsigned);
  1147. PRINT_ATTRf(inherit_stat, p_unsigned);
  1148. PRINT_ATTRf(enable_on_exec, p_unsigned);
  1149. PRINT_ATTRf(task, p_unsigned);
  1150. PRINT_ATTRf(watermark, p_unsigned);
  1151. PRINT_ATTRf(precise_ip, p_unsigned);
  1152. PRINT_ATTRf(mmap_data, p_unsigned);
  1153. PRINT_ATTRf(sample_id_all, p_unsigned);
  1154. PRINT_ATTRf(exclude_host, p_unsigned);
  1155. PRINT_ATTRf(exclude_guest, p_unsigned);
  1156. PRINT_ATTRf(exclude_callchain_kernel, p_unsigned);
  1157. PRINT_ATTRf(exclude_callchain_user, p_unsigned);
  1158. PRINT_ATTRf(mmap2, p_unsigned);
  1159. PRINT_ATTRf(comm_exec, p_unsigned);
  1160. PRINT_ATTRf(use_clockid, p_unsigned);
  1161. PRINT_ATTRf(context_switch, p_unsigned);
  1162. PRINT_ATTRf(write_backward, p_unsigned);
  1163. PRINT_ATTRn("{ wakeup_events, wakeup_watermark }", wakeup_events, p_unsigned);
  1164. PRINT_ATTRf(bp_type, p_unsigned);
  1165. PRINT_ATTRn("{ bp_addr, config1 }", bp_addr, p_hex);
  1166. PRINT_ATTRn("{ bp_len, config2 }", bp_len, p_hex);
  1167. PRINT_ATTRf(branch_sample_type, p_branch_sample_type);
  1168. PRINT_ATTRf(sample_regs_user, p_hex);
  1169. PRINT_ATTRf(sample_stack_user, p_unsigned);
  1170. PRINT_ATTRf(clockid, p_signed);
  1171. PRINT_ATTRf(sample_regs_intr, p_hex);
  1172. PRINT_ATTRf(aux_watermark, p_unsigned);
  1173. PRINT_ATTRf(sample_max_stack, p_unsigned);
  1174. return ret;
  1175. }
  1176. static int __open_attr__fprintf(FILE *fp, const char *name, const char *val,
  1177. void *priv __attribute__((unused)))
  1178. {
  1179. return fprintf(fp, " %-32s %s\n", name, val);
  1180. }
  1181. static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
  1182. struct thread_map *threads)
  1183. {
  1184. int cpu, thread, nthreads;
  1185. unsigned long flags = PERF_FLAG_FD_CLOEXEC;
  1186. int pid = -1, err;
  1187. enum { NO_CHANGE, SET_TO_MAX, INCREASED_MAX } set_rlimit = NO_CHANGE;
  1188. if (perf_missing_features.write_backward && evsel->attr.write_backward)
  1189. return -EINVAL;
  1190. if (evsel->system_wide)
  1191. nthreads = 1;
  1192. else
  1193. nthreads = threads->nr;
  1194. if (evsel->fd == NULL &&
  1195. perf_evsel__alloc_fd(evsel, cpus->nr, nthreads) < 0)
  1196. return -ENOMEM;
  1197. if (evsel->cgrp) {
  1198. flags |= PERF_FLAG_PID_CGROUP;
  1199. pid = evsel->cgrp->fd;
  1200. }
  1201. fallback_missing_features:
  1202. if (perf_missing_features.clockid_wrong)
  1203. evsel->attr.clockid = CLOCK_MONOTONIC; /* should always work */
  1204. if (perf_missing_features.clockid) {
  1205. evsel->attr.use_clockid = 0;
  1206. evsel->attr.clockid = 0;
  1207. }
  1208. if (perf_missing_features.cloexec)
  1209. flags &= ~(unsigned long)PERF_FLAG_FD_CLOEXEC;
  1210. if (perf_missing_features.mmap2)
  1211. evsel->attr.mmap2 = 0;
  1212. if (perf_missing_features.exclude_guest)
  1213. evsel->attr.exclude_guest = evsel->attr.exclude_host = 0;
  1214. if (perf_missing_features.lbr_flags)
  1215. evsel->attr.branch_sample_type &= ~(PERF_SAMPLE_BRANCH_NO_FLAGS |
  1216. PERF_SAMPLE_BRANCH_NO_CYCLES);
  1217. retry_sample_id:
  1218. if (perf_missing_features.sample_id_all)
  1219. evsel->attr.sample_id_all = 0;
  1220. if (verbose >= 2) {
  1221. fprintf(stderr, "%.60s\n", graph_dotted_line);
  1222. fprintf(stderr, "perf_event_attr:\n");
  1223. perf_event_attr__fprintf(stderr, &evsel->attr, __open_attr__fprintf, NULL);
  1224. fprintf(stderr, "%.60s\n", graph_dotted_line);
  1225. }
  1226. for (cpu = 0; cpu < cpus->nr; cpu++) {
  1227. for (thread = 0; thread < nthreads; thread++) {
  1228. int group_fd;
  1229. if (!evsel->cgrp && !evsel->system_wide)
  1230. pid = thread_map__pid(threads, thread);
  1231. group_fd = get_group_fd(evsel, cpu, thread);
  1232. retry_open:
  1233. pr_debug2("sys_perf_event_open: pid %d cpu %d group_fd %d flags %#lx\n",
  1234. pid, cpus->map[cpu], group_fd, flags);
  1235. FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
  1236. pid,
  1237. cpus->map[cpu],
  1238. group_fd, flags);
  1239. if (FD(evsel, cpu, thread) < 0) {
  1240. err = -errno;
  1241. pr_debug2("sys_perf_event_open failed, error %d\n",
  1242. err);
  1243. goto try_fallback;
  1244. }
  1245. if (evsel->bpf_fd >= 0) {
  1246. int evt_fd = FD(evsel, cpu, thread);
  1247. int bpf_fd = evsel->bpf_fd;
  1248. err = ioctl(evt_fd,
  1249. PERF_EVENT_IOC_SET_BPF,
  1250. bpf_fd);
  1251. if (err && errno != EEXIST) {
  1252. pr_err("failed to attach bpf fd %d: %s\n",
  1253. bpf_fd, strerror(errno));
  1254. err = -EINVAL;
  1255. goto out_close;
  1256. }
  1257. }
  1258. set_rlimit = NO_CHANGE;
  1259. /*
  1260. * If we succeeded but had to kill clockid, fail and
  1261. * have perf_evsel__open_strerror() print us a nice
  1262. * error.
  1263. */
  1264. if (perf_missing_features.clockid ||
  1265. perf_missing_features.clockid_wrong) {
  1266. err = -EINVAL;
  1267. goto out_close;
  1268. }
  1269. }
  1270. }
  1271. return 0;
  1272. try_fallback:
  1273. /*
  1274. * perf stat needs between 5 and 22 fds per CPU. When we run out
  1275. * of them try to increase the limits.
  1276. */
  1277. if (err == -EMFILE && set_rlimit < INCREASED_MAX) {
  1278. struct rlimit l;
  1279. int old_errno = errno;
  1280. if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
  1281. if (set_rlimit == NO_CHANGE)
  1282. l.rlim_cur = l.rlim_max;
  1283. else {
  1284. l.rlim_cur = l.rlim_max + 1000;
  1285. l.rlim_max = l.rlim_cur;
  1286. }
  1287. if (setrlimit(RLIMIT_NOFILE, &l) == 0) {
  1288. set_rlimit++;
  1289. errno = old_errno;
  1290. goto retry_open;
  1291. }
  1292. }
  1293. errno = old_errno;
  1294. }
  1295. if (err != -EINVAL || cpu > 0 || thread > 0)
  1296. goto out_close;
  1297. /*
  1298. * Must probe features in the order they were added to the
  1299. * perf_event_attr interface.
  1300. */
  1301. if (!perf_missing_features.write_backward && evsel->attr.write_backward) {
  1302. perf_missing_features.write_backward = true;
  1303. goto out_close;
  1304. } else if (!perf_missing_features.clockid_wrong && evsel->attr.use_clockid) {
  1305. perf_missing_features.clockid_wrong = true;
  1306. goto fallback_missing_features;
  1307. } else if (!perf_missing_features.clockid && evsel->attr.use_clockid) {
  1308. perf_missing_features.clockid = true;
  1309. goto fallback_missing_features;
  1310. } else if (!perf_missing_features.cloexec && (flags & PERF_FLAG_FD_CLOEXEC)) {
  1311. perf_missing_features.cloexec = true;
  1312. goto fallback_missing_features;
  1313. } else if (!perf_missing_features.mmap2 && evsel->attr.mmap2) {
  1314. perf_missing_features.mmap2 = true;
  1315. goto fallback_missing_features;
  1316. } else if (!perf_missing_features.exclude_guest &&
  1317. (evsel->attr.exclude_guest || evsel->attr.exclude_host)) {
  1318. perf_missing_features.exclude_guest = true;
  1319. goto fallback_missing_features;
  1320. } else if (!perf_missing_features.sample_id_all) {
  1321. perf_missing_features.sample_id_all = true;
  1322. goto retry_sample_id;
  1323. } else if (!perf_missing_features.lbr_flags &&
  1324. (evsel->attr.branch_sample_type &
  1325. (PERF_SAMPLE_BRANCH_NO_CYCLES |
  1326. PERF_SAMPLE_BRANCH_NO_FLAGS))) {
  1327. perf_missing_features.lbr_flags = true;
  1328. goto fallback_missing_features;
  1329. }
  1330. out_close:
  1331. do {
  1332. while (--thread >= 0) {
  1333. close(FD(evsel, cpu, thread));
  1334. FD(evsel, cpu, thread) = -1;
  1335. }
  1336. thread = nthreads;
  1337. } while (--cpu >= 0);
  1338. return err;
  1339. }
  1340. void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
  1341. {
  1342. if (evsel->fd == NULL)
  1343. return;
  1344. perf_evsel__close_fd(evsel, ncpus, nthreads);
  1345. perf_evsel__free_fd(evsel);
  1346. }
  1347. static struct {
  1348. struct cpu_map map;
  1349. int cpus[1];
  1350. } empty_cpu_map = {
  1351. .map.nr = 1,
  1352. .cpus = { -1, },
  1353. };
  1354. static struct {
  1355. struct thread_map map;
  1356. int threads[1];
  1357. } empty_thread_map = {
  1358. .map.nr = 1,
  1359. .threads = { -1, },
  1360. };
  1361. int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
  1362. struct thread_map *threads)
  1363. {
  1364. if (cpus == NULL) {
  1365. /* Work around old compiler warnings about strict aliasing */
  1366. cpus = &empty_cpu_map.map;
  1367. }
  1368. if (threads == NULL)
  1369. threads = &empty_thread_map.map;
  1370. return __perf_evsel__open(evsel, cpus, threads);
  1371. }
  1372. int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
  1373. struct cpu_map *cpus)
  1374. {
  1375. return __perf_evsel__open(evsel, cpus, &empty_thread_map.map);
  1376. }
  1377. int perf_evsel__open_per_thread(struct perf_evsel *evsel,
  1378. struct thread_map *threads)
  1379. {
  1380. return __perf_evsel__open(evsel, &empty_cpu_map.map, threads);
  1381. }
  1382. static int perf_evsel__parse_id_sample(const struct perf_evsel *evsel,
  1383. const union perf_event *event,
  1384. struct perf_sample *sample)
  1385. {
  1386. u64 type = evsel->attr.sample_type;
  1387. const u64 *array = event->sample.array;
  1388. bool swapped = evsel->needs_swap;
  1389. union u64_swap u;
  1390. array += ((event->header.size -
  1391. sizeof(event->header)) / sizeof(u64)) - 1;
  1392. if (type & PERF_SAMPLE_IDENTIFIER) {
  1393. sample->id = *array;
  1394. array--;
  1395. }
  1396. if (type & PERF_SAMPLE_CPU) {
  1397. u.val64 = *array;
  1398. if (swapped) {
  1399. /* undo swap of u64, then swap on individual u32s */
  1400. u.val64 = bswap_64(u.val64);
  1401. u.val32[0] = bswap_32(u.val32[0]);
  1402. }
  1403. sample->cpu = u.val32[0];
  1404. array--;
  1405. }
  1406. if (type & PERF_SAMPLE_STREAM_ID) {
  1407. sample->stream_id = *array;
  1408. array--;
  1409. }
  1410. if (type & PERF_SAMPLE_ID) {
  1411. sample->id = *array;
  1412. array--;
  1413. }
  1414. if (type & PERF_SAMPLE_TIME) {
  1415. sample->time = *array;
  1416. array--;
  1417. }
  1418. if (type & PERF_SAMPLE_TID) {
  1419. u.val64 = *array;
  1420. if (swapped) {
  1421. /* undo swap of u64, then swap on individual u32s */
  1422. u.val64 = bswap_64(u.val64);
  1423. u.val32[0] = bswap_32(u.val32[0]);
  1424. u.val32[1] = bswap_32(u.val32[1]);
  1425. }
  1426. sample->pid = u.val32[0];
  1427. sample->tid = u.val32[1];
  1428. array--;
  1429. }
  1430. return 0;
  1431. }
  1432. static inline bool overflow(const void *endp, u16 max_size, const void *offset,
  1433. u64 size)
  1434. {
  1435. return size > max_size || offset + size > endp;
  1436. }
  1437. #define OVERFLOW_CHECK(offset, size, max_size) \
  1438. do { \
  1439. if (overflow(endp, (max_size), (offset), (size))) \
  1440. return -EFAULT; \
  1441. } while (0)
  1442. #define OVERFLOW_CHECK_u64(offset) \
  1443. OVERFLOW_CHECK(offset, sizeof(u64), sizeof(u64))
  1444. int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
  1445. struct perf_sample *data)
  1446. {
  1447. u64 type = evsel->attr.sample_type;
  1448. bool swapped = evsel->needs_swap;
  1449. const u64 *array;
  1450. u16 max_size = event->header.size;
  1451. const void *endp = (void *)event + max_size;
  1452. u64 sz;
  1453. /*
  1454. * used for cross-endian analysis. See git commit 65014ab3
  1455. * for why this goofiness is needed.
  1456. */
  1457. union u64_swap u;
  1458. memset(data, 0, sizeof(*data));
  1459. data->cpu = data->pid = data->tid = -1;
  1460. data->stream_id = data->id = data->time = -1ULL;
  1461. data->period = evsel->attr.sample_period;
  1462. data->cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  1463. if (event->header.type != PERF_RECORD_SAMPLE) {
  1464. if (!evsel->attr.sample_id_all)
  1465. return 0;
  1466. return perf_evsel__parse_id_sample(evsel, event, data);
  1467. }
  1468. array = event->sample.array;
  1469. /*
  1470. * The evsel's sample_size is based on PERF_SAMPLE_MASK which includes
  1471. * up to PERF_SAMPLE_PERIOD. After that overflow() must be used to
  1472. * check the format does not go past the end of the event.
  1473. */
  1474. if (evsel->sample_size + sizeof(event->header) > event->header.size)
  1475. return -EFAULT;
  1476. data->id = -1ULL;
  1477. if (type & PERF_SAMPLE_IDENTIFIER) {
  1478. data->id = *array;
  1479. array++;
  1480. }
  1481. if (type & PERF_SAMPLE_IP) {
  1482. data->ip = *array;
  1483. array++;
  1484. }
  1485. if (type & PERF_SAMPLE_TID) {
  1486. u.val64 = *array;
  1487. if (swapped) {
  1488. /* undo swap of u64, then swap on individual u32s */
  1489. u.val64 = bswap_64(u.val64);
  1490. u.val32[0] = bswap_32(u.val32[0]);
  1491. u.val32[1] = bswap_32(u.val32[1]);
  1492. }
  1493. data->pid = u.val32[0];
  1494. data->tid = u.val32[1];
  1495. array++;
  1496. }
  1497. if (type & PERF_SAMPLE_TIME) {
  1498. data->time = *array;
  1499. array++;
  1500. }
  1501. data->addr = 0;
  1502. if (type & PERF_SAMPLE_ADDR) {
  1503. data->addr = *array;
  1504. array++;
  1505. }
  1506. if (type & PERF_SAMPLE_ID) {
  1507. data->id = *array;
  1508. array++;
  1509. }
  1510. if (type & PERF_SAMPLE_STREAM_ID) {
  1511. data->stream_id = *array;
  1512. array++;
  1513. }
  1514. if (type & PERF_SAMPLE_CPU) {
  1515. u.val64 = *array;
  1516. if (swapped) {
  1517. /* undo swap of u64, then swap on individual u32s */
  1518. u.val64 = bswap_64(u.val64);
  1519. u.val32[0] = bswap_32(u.val32[0]);
  1520. }
  1521. data->cpu = u.val32[0];
  1522. array++;
  1523. }
  1524. if (type & PERF_SAMPLE_PERIOD) {
  1525. data->period = *array;
  1526. array++;
  1527. }
  1528. if (type & PERF_SAMPLE_READ) {
  1529. u64 read_format = evsel->attr.read_format;
  1530. OVERFLOW_CHECK_u64(array);
  1531. if (read_format & PERF_FORMAT_GROUP)
  1532. data->read.group.nr = *array;
  1533. else
  1534. data->read.one.value = *array;
  1535. array++;
  1536. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
  1537. OVERFLOW_CHECK_u64(array);
  1538. data->read.time_enabled = *array;
  1539. array++;
  1540. }
  1541. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
  1542. OVERFLOW_CHECK_u64(array);
  1543. data->read.time_running = *array;
  1544. array++;
  1545. }
  1546. /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
  1547. if (read_format & PERF_FORMAT_GROUP) {
  1548. const u64 max_group_nr = UINT64_MAX /
  1549. sizeof(struct sample_read_value);
  1550. if (data->read.group.nr > max_group_nr)
  1551. return -EFAULT;
  1552. sz = data->read.group.nr *
  1553. sizeof(struct sample_read_value);
  1554. OVERFLOW_CHECK(array, sz, max_size);
  1555. data->read.group.values =
  1556. (struct sample_read_value *)array;
  1557. array = (void *)array + sz;
  1558. } else {
  1559. OVERFLOW_CHECK_u64(array);
  1560. data->read.one.id = *array;
  1561. array++;
  1562. }
  1563. }
  1564. if (type & PERF_SAMPLE_CALLCHAIN) {
  1565. const u64 max_callchain_nr = UINT64_MAX / sizeof(u64);
  1566. OVERFLOW_CHECK_u64(array);
  1567. data->callchain = (struct ip_callchain *)array++;
  1568. if (data->callchain->nr > max_callchain_nr)
  1569. return -EFAULT;
  1570. sz = data->callchain->nr * sizeof(u64);
  1571. OVERFLOW_CHECK(array, sz, max_size);
  1572. array = (void *)array + sz;
  1573. }
  1574. if (type & PERF_SAMPLE_RAW) {
  1575. OVERFLOW_CHECK_u64(array);
  1576. u.val64 = *array;
  1577. if (WARN_ONCE(swapped,
  1578. "Endianness of raw data not corrected!\n")) {
  1579. /* undo swap of u64, then swap on individual u32s */
  1580. u.val64 = bswap_64(u.val64);
  1581. u.val32[0] = bswap_32(u.val32[0]);
  1582. u.val32[1] = bswap_32(u.val32[1]);
  1583. }
  1584. data->raw_size = u.val32[0];
  1585. array = (void *)array + sizeof(u32);
  1586. OVERFLOW_CHECK(array, data->raw_size, max_size);
  1587. data->raw_data = (void *)array;
  1588. array = (void *)array + data->raw_size;
  1589. }
  1590. if (type & PERF_SAMPLE_BRANCH_STACK) {
  1591. const u64 max_branch_nr = UINT64_MAX /
  1592. sizeof(struct branch_entry);
  1593. OVERFLOW_CHECK_u64(array);
  1594. data->branch_stack = (struct branch_stack *)array++;
  1595. if (data->branch_stack->nr > max_branch_nr)
  1596. return -EFAULT;
  1597. sz = data->branch_stack->nr * sizeof(struct branch_entry);
  1598. OVERFLOW_CHECK(array, sz, max_size);
  1599. array = (void *)array + sz;
  1600. }
  1601. if (type & PERF_SAMPLE_REGS_USER) {
  1602. OVERFLOW_CHECK_u64(array);
  1603. data->user_regs.abi = *array;
  1604. array++;
  1605. if (data->user_regs.abi) {
  1606. u64 mask = evsel->attr.sample_regs_user;
  1607. sz = hweight_long(mask) * sizeof(u64);
  1608. OVERFLOW_CHECK(array, sz, max_size);
  1609. data->user_regs.mask = mask;
  1610. data->user_regs.regs = (u64 *)array;
  1611. array = (void *)array + sz;
  1612. }
  1613. }
  1614. if (type & PERF_SAMPLE_STACK_USER) {
  1615. OVERFLOW_CHECK_u64(array);
  1616. sz = *array++;
  1617. data->user_stack.offset = ((char *)(array - 1)
  1618. - (char *) event);
  1619. if (!sz) {
  1620. data->user_stack.size = 0;
  1621. } else {
  1622. OVERFLOW_CHECK(array, sz, max_size);
  1623. data->user_stack.data = (char *)array;
  1624. array = (void *)array + sz;
  1625. OVERFLOW_CHECK_u64(array);
  1626. data->user_stack.size = *array++;
  1627. if (WARN_ONCE(data->user_stack.size > sz,
  1628. "user stack dump failure\n"))
  1629. return -EFAULT;
  1630. }
  1631. }
  1632. if (type & PERF_SAMPLE_WEIGHT) {
  1633. OVERFLOW_CHECK_u64(array);
  1634. data->weight = *array;
  1635. array++;
  1636. }
  1637. data->data_src = PERF_MEM_DATA_SRC_NONE;
  1638. if (type & PERF_SAMPLE_DATA_SRC) {
  1639. OVERFLOW_CHECK_u64(array);
  1640. data->data_src = *array;
  1641. array++;
  1642. }
  1643. data->transaction = 0;
  1644. if (type & PERF_SAMPLE_TRANSACTION) {
  1645. OVERFLOW_CHECK_u64(array);
  1646. data->transaction = *array;
  1647. array++;
  1648. }
  1649. data->intr_regs.abi = PERF_SAMPLE_REGS_ABI_NONE;
  1650. if (type & PERF_SAMPLE_REGS_INTR) {
  1651. OVERFLOW_CHECK_u64(array);
  1652. data->intr_regs.abi = *array;
  1653. array++;
  1654. if (data->intr_regs.abi != PERF_SAMPLE_REGS_ABI_NONE) {
  1655. u64 mask = evsel->attr.sample_regs_intr;
  1656. sz = hweight_long(mask) * sizeof(u64);
  1657. OVERFLOW_CHECK(array, sz, max_size);
  1658. data->intr_regs.mask = mask;
  1659. data->intr_regs.regs = (u64 *)array;
  1660. array = (void *)array + sz;
  1661. }
  1662. }
  1663. return 0;
  1664. }
  1665. size_t perf_event__sample_event_size(const struct perf_sample *sample, u64 type,
  1666. u64 read_format)
  1667. {
  1668. size_t sz, result = sizeof(struct sample_event);
  1669. if (type & PERF_SAMPLE_IDENTIFIER)
  1670. result += sizeof(u64);
  1671. if (type & PERF_SAMPLE_IP)
  1672. result += sizeof(u64);
  1673. if (type & PERF_SAMPLE_TID)
  1674. result += sizeof(u64);
  1675. if (type & PERF_SAMPLE_TIME)
  1676. result += sizeof(u64);
  1677. if (type & PERF_SAMPLE_ADDR)
  1678. result += sizeof(u64);
  1679. if (type & PERF_SAMPLE_ID)
  1680. result += sizeof(u64);
  1681. if (type & PERF_SAMPLE_STREAM_ID)
  1682. result += sizeof(u64);
  1683. if (type & PERF_SAMPLE_CPU)
  1684. result += sizeof(u64);
  1685. if (type & PERF_SAMPLE_PERIOD)
  1686. result += sizeof(u64);
  1687. if (type & PERF_SAMPLE_READ) {
  1688. result += sizeof(u64);
  1689. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  1690. result += sizeof(u64);
  1691. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  1692. result += sizeof(u64);
  1693. /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
  1694. if (read_format & PERF_FORMAT_GROUP) {
  1695. sz = sample->read.group.nr *
  1696. sizeof(struct sample_read_value);
  1697. result += sz;
  1698. } else {
  1699. result += sizeof(u64);
  1700. }
  1701. }
  1702. if (type & PERF_SAMPLE_CALLCHAIN) {
  1703. sz = (sample->callchain->nr + 1) * sizeof(u64);
  1704. result += sz;
  1705. }
  1706. if (type & PERF_SAMPLE_RAW) {
  1707. result += sizeof(u32);
  1708. result += sample->raw_size;
  1709. }
  1710. if (type & PERF_SAMPLE_BRANCH_STACK) {
  1711. sz = sample->branch_stack->nr * sizeof(struct branch_entry);
  1712. sz += sizeof(u64);
  1713. result += sz;
  1714. }
  1715. if (type & PERF_SAMPLE_REGS_USER) {
  1716. if (sample->user_regs.abi) {
  1717. result += sizeof(u64);
  1718. sz = hweight_long(sample->user_regs.mask) * sizeof(u64);
  1719. result += sz;
  1720. } else {
  1721. result += sizeof(u64);
  1722. }
  1723. }
  1724. if (type & PERF_SAMPLE_STACK_USER) {
  1725. sz = sample->user_stack.size;
  1726. result += sizeof(u64);
  1727. if (sz) {
  1728. result += sz;
  1729. result += sizeof(u64);
  1730. }
  1731. }
  1732. if (type & PERF_SAMPLE_WEIGHT)
  1733. result += sizeof(u64);
  1734. if (type & PERF_SAMPLE_DATA_SRC)
  1735. result += sizeof(u64);
  1736. if (type & PERF_SAMPLE_TRANSACTION)
  1737. result += sizeof(u64);
  1738. if (type & PERF_SAMPLE_REGS_INTR) {
  1739. if (sample->intr_regs.abi) {
  1740. result += sizeof(u64);
  1741. sz = hweight_long(sample->intr_regs.mask) * sizeof(u64);
  1742. result += sz;
  1743. } else {
  1744. result += sizeof(u64);
  1745. }
  1746. }
  1747. return result;
  1748. }
  1749. int perf_event__synthesize_sample(union perf_event *event, u64 type,
  1750. u64 read_format,
  1751. const struct perf_sample *sample,
  1752. bool swapped)
  1753. {
  1754. u64 *array;
  1755. size_t sz;
  1756. /*
  1757. * used for cross-endian analysis. See git commit 65014ab3
  1758. * for why this goofiness is needed.
  1759. */
  1760. union u64_swap u;
  1761. array = event->sample.array;
  1762. if (type & PERF_SAMPLE_IDENTIFIER) {
  1763. *array = sample->id;
  1764. array++;
  1765. }
  1766. if (type & PERF_SAMPLE_IP) {
  1767. *array = sample->ip;
  1768. array++;
  1769. }
  1770. if (type & PERF_SAMPLE_TID) {
  1771. u.val32[0] = sample->pid;
  1772. u.val32[1] = sample->tid;
  1773. if (swapped) {
  1774. /*
  1775. * Inverse of what is done in perf_evsel__parse_sample
  1776. */
  1777. u.val32[0] = bswap_32(u.val32[0]);
  1778. u.val32[1] = bswap_32(u.val32[1]);
  1779. u.val64 = bswap_64(u.val64);
  1780. }
  1781. *array = u.val64;
  1782. array++;
  1783. }
  1784. if (type & PERF_SAMPLE_TIME) {
  1785. *array = sample->time;
  1786. array++;
  1787. }
  1788. if (type & PERF_SAMPLE_ADDR) {
  1789. *array = sample->addr;
  1790. array++;
  1791. }
  1792. if (type & PERF_SAMPLE_ID) {
  1793. *array = sample->id;
  1794. array++;
  1795. }
  1796. if (type & PERF_SAMPLE_STREAM_ID) {
  1797. *array = sample->stream_id;
  1798. array++;
  1799. }
  1800. if (type & PERF_SAMPLE_CPU) {
  1801. u.val32[0] = sample->cpu;
  1802. if (swapped) {
  1803. /*
  1804. * Inverse of what is done in perf_evsel__parse_sample
  1805. */
  1806. u.val32[0] = bswap_32(u.val32[0]);
  1807. u.val64 = bswap_64(u.val64);
  1808. }
  1809. *array = u.val64;
  1810. array++;
  1811. }
  1812. if (type & PERF_SAMPLE_PERIOD) {
  1813. *array = sample->period;
  1814. array++;
  1815. }
  1816. if (type & PERF_SAMPLE_READ) {
  1817. if (read_format & PERF_FORMAT_GROUP)
  1818. *array = sample->read.group.nr;
  1819. else
  1820. *array = sample->read.one.value;
  1821. array++;
  1822. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
  1823. *array = sample->read.time_enabled;
  1824. array++;
  1825. }
  1826. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
  1827. *array = sample->read.time_running;
  1828. array++;
  1829. }
  1830. /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
  1831. if (read_format & PERF_FORMAT_GROUP) {
  1832. sz = sample->read.group.nr *
  1833. sizeof(struct sample_read_value);
  1834. memcpy(array, sample->read.group.values, sz);
  1835. array = (void *)array + sz;
  1836. } else {
  1837. *array = sample->read.one.id;
  1838. array++;
  1839. }
  1840. }
  1841. if (type & PERF_SAMPLE_CALLCHAIN) {
  1842. sz = (sample->callchain->nr + 1) * sizeof(u64);
  1843. memcpy(array, sample->callchain, sz);
  1844. array = (void *)array + sz;
  1845. }
  1846. if (type & PERF_SAMPLE_RAW) {
  1847. u.val32[0] = sample->raw_size;
  1848. if (WARN_ONCE(swapped,
  1849. "Endianness of raw data not corrected!\n")) {
  1850. /*
  1851. * Inverse of what is done in perf_evsel__parse_sample
  1852. */
  1853. u.val32[0] = bswap_32(u.val32[0]);
  1854. u.val32[1] = bswap_32(u.val32[1]);
  1855. u.val64 = bswap_64(u.val64);
  1856. }
  1857. *array = u.val64;
  1858. array = (void *)array + sizeof(u32);
  1859. memcpy(array, sample->raw_data, sample->raw_size);
  1860. array = (void *)array + sample->raw_size;
  1861. }
  1862. if (type & PERF_SAMPLE_BRANCH_STACK) {
  1863. sz = sample->branch_stack->nr * sizeof(struct branch_entry);
  1864. sz += sizeof(u64);
  1865. memcpy(array, sample->branch_stack, sz);
  1866. array = (void *)array + sz;
  1867. }
  1868. if (type & PERF_SAMPLE_REGS_USER) {
  1869. if (sample->user_regs.abi) {
  1870. *array++ = sample->user_regs.abi;
  1871. sz = hweight_long(sample->user_regs.mask) * sizeof(u64);
  1872. memcpy(array, sample->user_regs.regs, sz);
  1873. array = (void *)array + sz;
  1874. } else {
  1875. *array++ = 0;
  1876. }
  1877. }
  1878. if (type & PERF_SAMPLE_STACK_USER) {
  1879. sz = sample->user_stack.size;
  1880. *array++ = sz;
  1881. if (sz) {
  1882. memcpy(array, sample->user_stack.data, sz);
  1883. array = (void *)array + sz;
  1884. *array++ = sz;
  1885. }
  1886. }
  1887. if (type & PERF_SAMPLE_WEIGHT) {
  1888. *array = sample->weight;
  1889. array++;
  1890. }
  1891. if (type & PERF_SAMPLE_DATA_SRC) {
  1892. *array = sample->data_src;
  1893. array++;
  1894. }
  1895. if (type & PERF_SAMPLE_TRANSACTION) {
  1896. *array = sample->transaction;
  1897. array++;
  1898. }
  1899. if (type & PERF_SAMPLE_REGS_INTR) {
  1900. if (sample->intr_regs.abi) {
  1901. *array++ = sample->intr_regs.abi;
  1902. sz = hweight_long(sample->intr_regs.mask) * sizeof(u64);
  1903. memcpy(array, sample->intr_regs.regs, sz);
  1904. array = (void *)array + sz;
  1905. } else {
  1906. *array++ = 0;
  1907. }
  1908. }
  1909. return 0;
  1910. }
  1911. struct format_field *perf_evsel__field(struct perf_evsel *evsel, const char *name)
  1912. {
  1913. return pevent_find_field(evsel->tp_format, name);
  1914. }
  1915. void *perf_evsel__rawptr(struct perf_evsel *evsel, struct perf_sample *sample,
  1916. const char *name)
  1917. {
  1918. struct format_field *field = perf_evsel__field(evsel, name);
  1919. int offset;
  1920. if (!field)
  1921. return NULL;
  1922. offset = field->offset;
  1923. if (field->flags & FIELD_IS_DYNAMIC) {
  1924. offset = *(int *)(sample->raw_data + field->offset);
  1925. offset &= 0xffff;
  1926. }
  1927. return sample->raw_data + offset;
  1928. }
  1929. u64 format_field__intval(struct format_field *field, struct perf_sample *sample,
  1930. bool needs_swap)
  1931. {
  1932. u64 value;
  1933. void *ptr = sample->raw_data + field->offset;
  1934. switch (field->size) {
  1935. case 1:
  1936. return *(u8 *)ptr;
  1937. case 2:
  1938. value = *(u16 *)ptr;
  1939. break;
  1940. case 4:
  1941. value = *(u32 *)ptr;
  1942. break;
  1943. case 8:
  1944. memcpy(&value, ptr, sizeof(u64));
  1945. break;
  1946. default:
  1947. return 0;
  1948. }
  1949. if (!needs_swap)
  1950. return value;
  1951. switch (field->size) {
  1952. case 2:
  1953. return bswap_16(value);
  1954. case 4:
  1955. return bswap_32(value);
  1956. case 8:
  1957. return bswap_64(value);
  1958. default:
  1959. return 0;
  1960. }
  1961. return 0;
  1962. }
  1963. u64 perf_evsel__intval(struct perf_evsel *evsel, struct perf_sample *sample,
  1964. const char *name)
  1965. {
  1966. struct format_field *field = perf_evsel__field(evsel, name);
  1967. if (!field)
  1968. return 0;
  1969. return field ? format_field__intval(field, sample, evsel->needs_swap) : 0;
  1970. }
  1971. bool perf_evsel__fallback(struct perf_evsel *evsel, int err,
  1972. char *msg, size_t msgsize)
  1973. {
  1974. int paranoid;
  1975. if ((err == ENOENT || err == ENXIO || err == ENODEV) &&
  1976. evsel->attr.type == PERF_TYPE_HARDWARE &&
  1977. evsel->attr.config == PERF_COUNT_HW_CPU_CYCLES) {
  1978. /*
  1979. * If it's cycles then fall back to hrtimer based
  1980. * cpu-clock-tick sw counter, which is always available even if
  1981. * no PMU support.
  1982. *
  1983. * PPC returns ENXIO until 2.6.37 (behavior changed with commit
  1984. * b0a873e).
  1985. */
  1986. scnprintf(msg, msgsize, "%s",
  1987. "The cycles event is not supported, trying to fall back to cpu-clock-ticks");
  1988. evsel->attr.type = PERF_TYPE_SOFTWARE;
  1989. evsel->attr.config = PERF_COUNT_SW_CPU_CLOCK;
  1990. zfree(&evsel->name);
  1991. return true;
  1992. } else if (err == EACCES && !evsel->attr.exclude_kernel &&
  1993. (paranoid = perf_event_paranoid()) > 1) {
  1994. const char *name = perf_evsel__name(evsel);
  1995. char *new_name;
  1996. if (asprintf(&new_name, "%s%su", name, strchr(name, ':') ? "" : ":") < 0)
  1997. return false;
  1998. if (evsel->name)
  1999. free(evsel->name);
  2000. evsel->name = new_name;
  2001. scnprintf(msg, msgsize,
  2002. "kernel.perf_event_paranoid=%d, trying to fall back to excluding kernel samples", paranoid);
  2003. evsel->attr.exclude_kernel = 1;
  2004. return true;
  2005. }
  2006. return false;
  2007. }
  2008. int perf_evsel__open_strerror(struct perf_evsel *evsel, struct target *target,
  2009. int err, char *msg, size_t size)
  2010. {
  2011. char sbuf[STRERR_BUFSIZE];
  2012. switch (err) {
  2013. case EPERM:
  2014. case EACCES:
  2015. return scnprintf(msg, size,
  2016. "You may not have permission to collect %sstats.\n\n"
  2017. "Consider tweaking /proc/sys/kernel/perf_event_paranoid,\n"
  2018. "which controls use of the performance events system by\n"
  2019. "unprivileged users (without CAP_SYS_ADMIN).\n\n"
  2020. "The current value is %d:\n\n"
  2021. " -1: Allow use of (almost) all events by all users\n"
  2022. ">= 0: Disallow raw tracepoint access by users without CAP_IOC_LOCK\n"
  2023. ">= 1: Disallow CPU event access by users without CAP_SYS_ADMIN\n"
  2024. ">= 2: Disallow kernel profiling by users without CAP_SYS_ADMIN",
  2025. target->system_wide ? "system-wide " : "",
  2026. perf_event_paranoid());
  2027. case ENOENT:
  2028. return scnprintf(msg, size, "The %s event is not supported.",
  2029. perf_evsel__name(evsel));
  2030. case EMFILE:
  2031. return scnprintf(msg, size, "%s",
  2032. "Too many events are opened.\n"
  2033. "Probably the maximum number of open file descriptors has been reached.\n"
  2034. "Hint: Try again after reducing the number of events.\n"
  2035. "Hint: Try increasing the limit with 'ulimit -n <limit>'");
  2036. case ENOMEM:
  2037. if ((evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN) != 0 &&
  2038. access("/proc/sys/kernel/perf_event_max_stack", F_OK) == 0)
  2039. return scnprintf(msg, size,
  2040. "Not enough memory to setup event with callchain.\n"
  2041. "Hint: Try tweaking /proc/sys/kernel/perf_event_max_stack\n"
  2042. "Hint: Current value: %d", sysctl_perf_event_max_stack);
  2043. break;
  2044. case ENODEV:
  2045. if (target->cpu_list)
  2046. return scnprintf(msg, size, "%s",
  2047. "No such device - did you specify an out-of-range profile CPU?");
  2048. break;
  2049. case EOPNOTSUPP:
  2050. if (evsel->attr.sample_period != 0)
  2051. return scnprintf(msg, size, "%s",
  2052. "PMU Hardware doesn't support sampling/overflow-interrupts.");
  2053. if (evsel->attr.precise_ip)
  2054. return scnprintf(msg, size, "%s",
  2055. "\'precise\' request may not be supported. Try removing 'p' modifier.");
  2056. #if defined(__i386__) || defined(__x86_64__)
  2057. if (evsel->attr.type == PERF_TYPE_HARDWARE)
  2058. return scnprintf(msg, size, "%s",
  2059. "No hardware sampling interrupt available.\n"
  2060. "No APIC? If so then you can boot the kernel with the \"lapic\" boot parameter to force-enable it.");
  2061. #endif
  2062. break;
  2063. case EBUSY:
  2064. if (find_process("oprofiled"))
  2065. return scnprintf(msg, size,
  2066. "The PMU counters are busy/taken by another profiler.\n"
  2067. "We found oprofile daemon running, please stop it and try again.");
  2068. break;
  2069. case EINVAL:
  2070. if (evsel->attr.write_backward && perf_missing_features.write_backward)
  2071. return scnprintf(msg, size, "Reading from overwrite event is not supported by this kernel.");
  2072. if (perf_missing_features.clockid)
  2073. return scnprintf(msg, size, "clockid feature not supported.");
  2074. if (perf_missing_features.clockid_wrong)
  2075. return scnprintf(msg, size, "wrong clockid (%d).", clockid);
  2076. break;
  2077. default:
  2078. break;
  2079. }
  2080. return scnprintf(msg, size,
  2081. "The sys_perf_event_open() syscall returned with %d (%s) for event (%s).\n"
  2082. "/bin/dmesg may provide additional information.\n"
  2083. "No CONFIG_PERF_EVENTS=y kernel support configured?",
  2084. err, str_error_r(err, sbuf, sizeof(sbuf)),
  2085. perf_evsel__name(evsel));
  2086. }
  2087. char *perf_evsel__env_arch(struct perf_evsel *evsel)
  2088. {
  2089. if (evsel && evsel->evlist && evsel->evlist->env)
  2090. return evsel->evlist->env->arch;
  2091. return NULL;
  2092. }