evsel.c 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493
  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. /* callgraph default */
  567. struct callchain_param param = {
  568. .record_mode = callchain_param.record_mode,
  569. };
  570. u32 dump_size = 0;
  571. int max_stack = 0;
  572. const char *callgraph_buf = NULL;
  573. list_for_each_entry(term, config_terms, list) {
  574. switch (term->type) {
  575. case PERF_EVSEL__CONFIG_TERM_PERIOD:
  576. attr->sample_period = term->val.period;
  577. attr->freq = 0;
  578. break;
  579. case PERF_EVSEL__CONFIG_TERM_FREQ:
  580. attr->sample_freq = term->val.freq;
  581. attr->freq = 1;
  582. break;
  583. case PERF_EVSEL__CONFIG_TERM_TIME:
  584. if (term->val.time)
  585. perf_evsel__set_sample_bit(evsel, TIME);
  586. else
  587. perf_evsel__reset_sample_bit(evsel, TIME);
  588. break;
  589. case PERF_EVSEL__CONFIG_TERM_CALLGRAPH:
  590. callgraph_buf = term->val.callgraph;
  591. break;
  592. case PERF_EVSEL__CONFIG_TERM_STACK_USER:
  593. dump_size = term->val.stack_user;
  594. break;
  595. case PERF_EVSEL__CONFIG_TERM_MAX_STACK:
  596. max_stack = term->val.max_stack;
  597. break;
  598. case PERF_EVSEL__CONFIG_TERM_INHERIT:
  599. /*
  600. * attr->inherit should has already been set by
  601. * perf_evsel__config. If user explicitly set
  602. * inherit using config terms, override global
  603. * opt->no_inherit setting.
  604. */
  605. attr->inherit = term->val.inherit ? 1 : 0;
  606. break;
  607. case PERF_EVSEL__CONFIG_TERM_OVERWRITE:
  608. attr->write_backward = term->val.overwrite ? 1 : 0;
  609. break;
  610. default:
  611. break;
  612. }
  613. }
  614. /* User explicitly set per-event callgraph, clear the old setting and reset. */
  615. if ((callgraph_buf != NULL) || (dump_size > 0) || max_stack) {
  616. if (max_stack) {
  617. param.max_stack = max_stack;
  618. if (callgraph_buf == NULL)
  619. callgraph_buf = "fp";
  620. }
  621. /* parse callgraph parameters */
  622. if (callgraph_buf != NULL) {
  623. if (!strcmp(callgraph_buf, "no")) {
  624. param.enabled = false;
  625. param.record_mode = CALLCHAIN_NONE;
  626. } else {
  627. param.enabled = true;
  628. if (parse_callchain_record(callgraph_buf, &param)) {
  629. pr_err("per-event callgraph setting for %s failed. "
  630. "Apply callgraph global setting for it\n",
  631. evsel->name);
  632. return;
  633. }
  634. }
  635. }
  636. if (dump_size > 0) {
  637. dump_size = round_up(dump_size, sizeof(u64));
  638. param.dump_size = dump_size;
  639. }
  640. /* If global callgraph set, clear it */
  641. if (callchain_param.enabled)
  642. perf_evsel__reset_callgraph(evsel, &callchain_param);
  643. /* set perf-event callgraph */
  644. if (param.enabled)
  645. perf_evsel__config_callchain(evsel, opts, &param);
  646. }
  647. }
  648. /*
  649. * The enable_on_exec/disabled value strategy:
  650. *
  651. * 1) For any type of traced program:
  652. * - all independent events and group leaders are disabled
  653. * - all group members are enabled
  654. *
  655. * Group members are ruled by group leaders. They need to
  656. * be enabled, because the group scheduling relies on that.
  657. *
  658. * 2) For traced programs executed by perf:
  659. * - all independent events and group leaders have
  660. * enable_on_exec set
  661. * - we don't specifically enable or disable any event during
  662. * the record command
  663. *
  664. * Independent events and group leaders are initially disabled
  665. * and get enabled by exec. Group members are ruled by group
  666. * leaders as stated in 1).
  667. *
  668. * 3) For traced programs attached by perf (pid/tid):
  669. * - we specifically enable or disable all events during
  670. * the record command
  671. *
  672. * When attaching events to already running traced we
  673. * enable/disable events specifically, as there's no
  674. * initial traced exec call.
  675. */
  676. void perf_evsel__config(struct perf_evsel *evsel, struct record_opts *opts,
  677. struct callchain_param *callchain)
  678. {
  679. struct perf_evsel *leader = evsel->leader;
  680. struct perf_event_attr *attr = &evsel->attr;
  681. int track = evsel->tracking;
  682. bool per_cpu = opts->target.default_per_cpu && !opts->target.per_thread;
  683. attr->sample_id_all = perf_missing_features.sample_id_all ? 0 : 1;
  684. attr->inherit = !opts->no_inherit;
  685. attr->write_backward = opts->overwrite ? 1 : 0;
  686. perf_evsel__set_sample_bit(evsel, IP);
  687. perf_evsel__set_sample_bit(evsel, TID);
  688. if (evsel->sample_read) {
  689. perf_evsel__set_sample_bit(evsel, READ);
  690. /*
  691. * We need ID even in case of single event, because
  692. * PERF_SAMPLE_READ process ID specific data.
  693. */
  694. perf_evsel__set_sample_id(evsel, false);
  695. /*
  696. * Apply group format only if we belong to group
  697. * with more than one members.
  698. */
  699. if (leader->nr_members > 1) {
  700. attr->read_format |= PERF_FORMAT_GROUP;
  701. attr->inherit = 0;
  702. }
  703. }
  704. /*
  705. * We default some events to have a default interval. But keep
  706. * it a weak assumption overridable by the user.
  707. */
  708. if (!attr->sample_period || (opts->user_freq != UINT_MAX ||
  709. opts->user_interval != ULLONG_MAX)) {
  710. if (opts->freq) {
  711. perf_evsel__set_sample_bit(evsel, PERIOD);
  712. attr->freq = 1;
  713. attr->sample_freq = opts->freq;
  714. } else {
  715. attr->sample_period = opts->default_interval;
  716. }
  717. }
  718. /*
  719. * Disable sampling for all group members other
  720. * than leader in case leader 'leads' the sampling.
  721. */
  722. if ((leader != evsel) && leader->sample_read) {
  723. attr->sample_freq = 0;
  724. attr->sample_period = 0;
  725. }
  726. if (opts->no_samples)
  727. attr->sample_freq = 0;
  728. if (opts->inherit_stat)
  729. attr->inherit_stat = 1;
  730. if (opts->sample_address) {
  731. perf_evsel__set_sample_bit(evsel, ADDR);
  732. attr->mmap_data = track;
  733. }
  734. /*
  735. * We don't allow user space callchains for function trace
  736. * event, due to issues with page faults while tracing page
  737. * fault handler and its overall trickiness nature.
  738. */
  739. if (perf_evsel__is_function_event(evsel))
  740. evsel->attr.exclude_callchain_user = 1;
  741. if (callchain && callchain->enabled && !evsel->no_aux_samples)
  742. perf_evsel__config_callchain(evsel, opts, callchain);
  743. if (opts->sample_intr_regs) {
  744. attr->sample_regs_intr = opts->sample_intr_regs;
  745. perf_evsel__set_sample_bit(evsel, REGS_INTR);
  746. }
  747. if (target__has_cpu(&opts->target) || opts->sample_cpu)
  748. perf_evsel__set_sample_bit(evsel, CPU);
  749. if (opts->period)
  750. perf_evsel__set_sample_bit(evsel, PERIOD);
  751. /*
  752. * When the user explicitly disabled time don't force it here.
  753. */
  754. if (opts->sample_time &&
  755. (!perf_missing_features.sample_id_all &&
  756. (!opts->no_inherit || target__has_cpu(&opts->target) || per_cpu ||
  757. opts->sample_time_set)))
  758. perf_evsel__set_sample_bit(evsel, TIME);
  759. if (opts->raw_samples && !evsel->no_aux_samples) {
  760. perf_evsel__set_sample_bit(evsel, TIME);
  761. perf_evsel__set_sample_bit(evsel, RAW);
  762. perf_evsel__set_sample_bit(evsel, CPU);
  763. }
  764. if (opts->sample_address)
  765. perf_evsel__set_sample_bit(evsel, DATA_SRC);
  766. if (opts->no_buffering) {
  767. attr->watermark = 0;
  768. attr->wakeup_events = 1;
  769. }
  770. if (opts->branch_stack && !evsel->no_aux_samples) {
  771. perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
  772. attr->branch_sample_type = opts->branch_stack;
  773. }
  774. if (opts->sample_weight)
  775. perf_evsel__set_sample_bit(evsel, WEIGHT);
  776. attr->task = track;
  777. attr->mmap = track;
  778. attr->mmap2 = track && !perf_missing_features.mmap2;
  779. attr->comm = track;
  780. if (opts->record_switch_events)
  781. attr->context_switch = track;
  782. if (opts->sample_transaction)
  783. perf_evsel__set_sample_bit(evsel, TRANSACTION);
  784. if (opts->running_time) {
  785. evsel->attr.read_format |=
  786. PERF_FORMAT_TOTAL_TIME_ENABLED |
  787. PERF_FORMAT_TOTAL_TIME_RUNNING;
  788. }
  789. /*
  790. * XXX see the function comment above
  791. *
  792. * Disabling only independent events or group leaders,
  793. * keeping group members enabled.
  794. */
  795. if (perf_evsel__is_group_leader(evsel))
  796. attr->disabled = 1;
  797. /*
  798. * Setting enable_on_exec for independent events and
  799. * group leaders for traced executed by perf.
  800. */
  801. if (target__none(&opts->target) && perf_evsel__is_group_leader(evsel) &&
  802. !opts->initial_delay)
  803. attr->enable_on_exec = 1;
  804. if (evsel->immediate) {
  805. attr->disabled = 0;
  806. attr->enable_on_exec = 0;
  807. }
  808. clockid = opts->clockid;
  809. if (opts->use_clockid) {
  810. attr->use_clockid = 1;
  811. attr->clockid = opts->clockid;
  812. }
  813. if (evsel->precise_max)
  814. perf_event_attr__set_max_precise_ip(attr);
  815. if (opts->all_user) {
  816. attr->exclude_kernel = 1;
  817. attr->exclude_user = 0;
  818. }
  819. if (opts->all_kernel) {
  820. attr->exclude_kernel = 0;
  821. attr->exclude_user = 1;
  822. }
  823. /*
  824. * Apply event specific term settings,
  825. * it overloads any global configuration.
  826. */
  827. apply_config_terms(evsel, opts);
  828. }
  829. static int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
  830. {
  831. if (evsel->system_wide)
  832. nthreads = 1;
  833. evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
  834. if (evsel->fd) {
  835. int cpu, thread;
  836. for (cpu = 0; cpu < ncpus; cpu++) {
  837. for (thread = 0; thread < nthreads; thread++) {
  838. FD(evsel, cpu, thread) = -1;
  839. }
  840. }
  841. }
  842. return evsel->fd != NULL ? 0 : -ENOMEM;
  843. }
  844. static int perf_evsel__run_ioctl(struct perf_evsel *evsel, int ncpus, int nthreads,
  845. int ioc, void *arg)
  846. {
  847. int cpu, thread;
  848. if (evsel->system_wide)
  849. nthreads = 1;
  850. for (cpu = 0; cpu < ncpus; cpu++) {
  851. for (thread = 0; thread < nthreads; thread++) {
  852. int fd = FD(evsel, cpu, thread),
  853. err = ioctl(fd, ioc, arg);
  854. if (err)
  855. return err;
  856. }
  857. }
  858. return 0;
  859. }
  860. int perf_evsel__apply_filter(struct perf_evsel *evsel, int ncpus, int nthreads,
  861. const char *filter)
  862. {
  863. return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
  864. PERF_EVENT_IOC_SET_FILTER,
  865. (void *)filter);
  866. }
  867. int perf_evsel__set_filter(struct perf_evsel *evsel, const char *filter)
  868. {
  869. char *new_filter = strdup(filter);
  870. if (new_filter != NULL) {
  871. free(evsel->filter);
  872. evsel->filter = new_filter;
  873. return 0;
  874. }
  875. return -1;
  876. }
  877. static int perf_evsel__append_filter(struct perf_evsel *evsel,
  878. const char *fmt, const char *filter)
  879. {
  880. char *new_filter;
  881. if (evsel->filter == NULL)
  882. return perf_evsel__set_filter(evsel, filter);
  883. if (asprintf(&new_filter, fmt, evsel->filter, filter) > 0) {
  884. free(evsel->filter);
  885. evsel->filter = new_filter;
  886. return 0;
  887. }
  888. return -1;
  889. }
  890. int perf_evsel__append_tp_filter(struct perf_evsel *evsel, const char *filter)
  891. {
  892. return perf_evsel__append_filter(evsel, "(%s) && (%s)", filter);
  893. }
  894. int perf_evsel__append_addr_filter(struct perf_evsel *evsel, const char *filter)
  895. {
  896. return perf_evsel__append_filter(evsel, "%s,%s", filter);
  897. }
  898. int perf_evsel__enable(struct perf_evsel *evsel)
  899. {
  900. int nthreads = thread_map__nr(evsel->threads);
  901. int ncpus = cpu_map__nr(evsel->cpus);
  902. return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
  903. PERF_EVENT_IOC_ENABLE,
  904. 0);
  905. }
  906. int perf_evsel__disable(struct perf_evsel *evsel)
  907. {
  908. int nthreads = thread_map__nr(evsel->threads);
  909. int ncpus = cpu_map__nr(evsel->cpus);
  910. return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
  911. PERF_EVENT_IOC_DISABLE,
  912. 0);
  913. }
  914. int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
  915. {
  916. if (ncpus == 0 || nthreads == 0)
  917. return 0;
  918. if (evsel->system_wide)
  919. nthreads = 1;
  920. evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
  921. if (evsel->sample_id == NULL)
  922. return -ENOMEM;
  923. evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
  924. if (evsel->id == NULL) {
  925. xyarray__delete(evsel->sample_id);
  926. evsel->sample_id = NULL;
  927. return -ENOMEM;
  928. }
  929. return 0;
  930. }
  931. static void perf_evsel__free_fd(struct perf_evsel *evsel)
  932. {
  933. xyarray__delete(evsel->fd);
  934. evsel->fd = NULL;
  935. }
  936. static void perf_evsel__free_id(struct perf_evsel *evsel)
  937. {
  938. xyarray__delete(evsel->sample_id);
  939. evsel->sample_id = NULL;
  940. zfree(&evsel->id);
  941. }
  942. static void perf_evsel__free_config_terms(struct perf_evsel *evsel)
  943. {
  944. struct perf_evsel_config_term *term, *h;
  945. list_for_each_entry_safe(term, h, &evsel->config_terms, list) {
  946. list_del(&term->list);
  947. free(term);
  948. }
  949. }
  950. void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
  951. {
  952. int cpu, thread;
  953. if (evsel->system_wide)
  954. nthreads = 1;
  955. for (cpu = 0; cpu < ncpus; cpu++)
  956. for (thread = 0; thread < nthreads; ++thread) {
  957. close(FD(evsel, cpu, thread));
  958. FD(evsel, cpu, thread) = -1;
  959. }
  960. }
  961. void perf_evsel__exit(struct perf_evsel *evsel)
  962. {
  963. assert(list_empty(&evsel->node));
  964. assert(evsel->evlist == NULL);
  965. perf_evsel__free_fd(evsel);
  966. perf_evsel__free_id(evsel);
  967. perf_evsel__free_config_terms(evsel);
  968. close_cgroup(evsel->cgrp);
  969. cpu_map__put(evsel->cpus);
  970. cpu_map__put(evsel->own_cpus);
  971. thread_map__put(evsel->threads);
  972. zfree(&evsel->group_name);
  973. zfree(&evsel->name);
  974. perf_evsel__object.fini(evsel);
  975. }
  976. void perf_evsel__delete(struct perf_evsel *evsel)
  977. {
  978. perf_evsel__exit(evsel);
  979. free(evsel);
  980. }
  981. void perf_evsel__compute_deltas(struct perf_evsel *evsel, int cpu, int thread,
  982. struct perf_counts_values *count)
  983. {
  984. struct perf_counts_values tmp;
  985. if (!evsel->prev_raw_counts)
  986. return;
  987. if (cpu == -1) {
  988. tmp = evsel->prev_raw_counts->aggr;
  989. evsel->prev_raw_counts->aggr = *count;
  990. } else {
  991. tmp = *perf_counts(evsel->prev_raw_counts, cpu, thread);
  992. *perf_counts(evsel->prev_raw_counts, cpu, thread) = *count;
  993. }
  994. count->val = count->val - tmp.val;
  995. count->ena = count->ena - tmp.ena;
  996. count->run = count->run - tmp.run;
  997. }
  998. void perf_counts_values__scale(struct perf_counts_values *count,
  999. bool scale, s8 *pscaled)
  1000. {
  1001. s8 scaled = 0;
  1002. if (scale) {
  1003. if (count->run == 0) {
  1004. scaled = -1;
  1005. count->val = 0;
  1006. } else if (count->run < count->ena) {
  1007. scaled = 1;
  1008. count->val = (u64)((double) count->val * count->ena / count->run + 0.5);
  1009. }
  1010. } else
  1011. count->ena = count->run = 0;
  1012. if (pscaled)
  1013. *pscaled = scaled;
  1014. }
  1015. int perf_evsel__read(struct perf_evsel *evsel, int cpu, int thread,
  1016. struct perf_counts_values *count)
  1017. {
  1018. memset(count, 0, sizeof(*count));
  1019. if (FD(evsel, cpu, thread) < 0)
  1020. return -EINVAL;
  1021. if (readn(FD(evsel, cpu, thread), count, sizeof(*count)) <= 0)
  1022. return -errno;
  1023. return 0;
  1024. }
  1025. int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
  1026. int cpu, int thread, bool scale)
  1027. {
  1028. struct perf_counts_values count;
  1029. size_t nv = scale ? 3 : 1;
  1030. if (FD(evsel, cpu, thread) < 0)
  1031. return -EINVAL;
  1032. if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1, thread + 1) < 0)
  1033. return -ENOMEM;
  1034. if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) <= 0)
  1035. return -errno;
  1036. perf_evsel__compute_deltas(evsel, cpu, thread, &count);
  1037. perf_counts_values__scale(&count, scale, NULL);
  1038. *perf_counts(evsel->counts, cpu, thread) = count;
  1039. return 0;
  1040. }
  1041. static int get_group_fd(struct perf_evsel *evsel, int cpu, int thread)
  1042. {
  1043. struct perf_evsel *leader = evsel->leader;
  1044. int fd;
  1045. if (perf_evsel__is_group_leader(evsel))
  1046. return -1;
  1047. /*
  1048. * Leader must be already processed/open,
  1049. * if not it's a bug.
  1050. */
  1051. BUG_ON(!leader->fd);
  1052. fd = FD(leader, cpu, thread);
  1053. BUG_ON(fd == -1);
  1054. return fd;
  1055. }
  1056. struct bit_names {
  1057. int bit;
  1058. const char *name;
  1059. };
  1060. static void __p_bits(char *buf, size_t size, u64 value, struct bit_names *bits)
  1061. {
  1062. bool first_bit = true;
  1063. int i = 0;
  1064. do {
  1065. if (value & bits[i].bit) {
  1066. buf += scnprintf(buf, size, "%s%s", first_bit ? "" : "|", bits[i].name);
  1067. first_bit = false;
  1068. }
  1069. } while (bits[++i].name != NULL);
  1070. }
  1071. static void __p_sample_type(char *buf, size_t size, u64 value)
  1072. {
  1073. #define bit_name(n) { PERF_SAMPLE_##n, #n }
  1074. struct bit_names bits[] = {
  1075. bit_name(IP), bit_name(TID), bit_name(TIME), bit_name(ADDR),
  1076. bit_name(READ), bit_name(CALLCHAIN), bit_name(ID), bit_name(CPU),
  1077. bit_name(PERIOD), bit_name(STREAM_ID), bit_name(RAW),
  1078. bit_name(BRANCH_STACK), bit_name(REGS_USER), bit_name(STACK_USER),
  1079. bit_name(IDENTIFIER), bit_name(REGS_INTR), bit_name(DATA_SRC),
  1080. bit_name(WEIGHT),
  1081. { .name = NULL, }
  1082. };
  1083. #undef bit_name
  1084. __p_bits(buf, size, value, bits);
  1085. }
  1086. static void __p_branch_sample_type(char *buf, size_t size, u64 value)
  1087. {
  1088. #define bit_name(n) { PERF_SAMPLE_BRANCH_##n, #n }
  1089. struct bit_names bits[] = {
  1090. bit_name(USER), bit_name(KERNEL), bit_name(HV), bit_name(ANY),
  1091. bit_name(ANY_CALL), bit_name(ANY_RETURN), bit_name(IND_CALL),
  1092. bit_name(ABORT_TX), bit_name(IN_TX), bit_name(NO_TX),
  1093. bit_name(COND), bit_name(CALL_STACK), bit_name(IND_JUMP),
  1094. bit_name(CALL), bit_name(NO_FLAGS), bit_name(NO_CYCLES),
  1095. { .name = NULL, }
  1096. };
  1097. #undef bit_name
  1098. __p_bits(buf, size, value, bits);
  1099. }
  1100. static void __p_read_format(char *buf, size_t size, u64 value)
  1101. {
  1102. #define bit_name(n) { PERF_FORMAT_##n, #n }
  1103. struct bit_names bits[] = {
  1104. bit_name(TOTAL_TIME_ENABLED), bit_name(TOTAL_TIME_RUNNING),
  1105. bit_name(ID), bit_name(GROUP),
  1106. { .name = NULL, }
  1107. };
  1108. #undef bit_name
  1109. __p_bits(buf, size, value, bits);
  1110. }
  1111. #define BUF_SIZE 1024
  1112. #define p_hex(val) snprintf(buf, BUF_SIZE, "%#"PRIx64, (uint64_t)(val))
  1113. #define p_unsigned(val) snprintf(buf, BUF_SIZE, "%"PRIu64, (uint64_t)(val))
  1114. #define p_signed(val) snprintf(buf, BUF_SIZE, "%"PRId64, (int64_t)(val))
  1115. #define p_sample_type(val) __p_sample_type(buf, BUF_SIZE, val)
  1116. #define p_branch_sample_type(val) __p_branch_sample_type(buf, BUF_SIZE, val)
  1117. #define p_read_format(val) __p_read_format(buf, BUF_SIZE, val)
  1118. #define PRINT_ATTRn(_n, _f, _p) \
  1119. do { \
  1120. if (attr->_f) { \
  1121. _p(attr->_f); \
  1122. ret += attr__fprintf(fp, _n, buf, priv);\
  1123. } \
  1124. } while (0)
  1125. #define PRINT_ATTRf(_f, _p) PRINT_ATTRn(#_f, _f, _p)
  1126. int perf_event_attr__fprintf(FILE *fp, struct perf_event_attr *attr,
  1127. attr__fprintf_f attr__fprintf, void *priv)
  1128. {
  1129. char buf[BUF_SIZE];
  1130. int ret = 0;
  1131. PRINT_ATTRf(type, p_unsigned);
  1132. PRINT_ATTRf(size, p_unsigned);
  1133. PRINT_ATTRf(config, p_hex);
  1134. PRINT_ATTRn("{ sample_period, sample_freq }", sample_period, p_unsigned);
  1135. PRINT_ATTRf(sample_type, p_sample_type);
  1136. PRINT_ATTRf(read_format, p_read_format);
  1137. PRINT_ATTRf(disabled, p_unsigned);
  1138. PRINT_ATTRf(inherit, p_unsigned);
  1139. PRINT_ATTRf(pinned, p_unsigned);
  1140. PRINT_ATTRf(exclusive, p_unsigned);
  1141. PRINT_ATTRf(exclude_user, p_unsigned);
  1142. PRINT_ATTRf(exclude_kernel, p_unsigned);
  1143. PRINT_ATTRf(exclude_hv, p_unsigned);
  1144. PRINT_ATTRf(exclude_idle, p_unsigned);
  1145. PRINT_ATTRf(mmap, p_unsigned);
  1146. PRINT_ATTRf(comm, p_unsigned);
  1147. PRINT_ATTRf(freq, p_unsigned);
  1148. PRINT_ATTRf(inherit_stat, p_unsigned);
  1149. PRINT_ATTRf(enable_on_exec, p_unsigned);
  1150. PRINT_ATTRf(task, p_unsigned);
  1151. PRINT_ATTRf(watermark, p_unsigned);
  1152. PRINT_ATTRf(precise_ip, p_unsigned);
  1153. PRINT_ATTRf(mmap_data, p_unsigned);
  1154. PRINT_ATTRf(sample_id_all, p_unsigned);
  1155. PRINT_ATTRf(exclude_host, p_unsigned);
  1156. PRINT_ATTRf(exclude_guest, p_unsigned);
  1157. PRINT_ATTRf(exclude_callchain_kernel, p_unsigned);
  1158. PRINT_ATTRf(exclude_callchain_user, p_unsigned);
  1159. PRINT_ATTRf(mmap2, p_unsigned);
  1160. PRINT_ATTRf(comm_exec, p_unsigned);
  1161. PRINT_ATTRf(use_clockid, p_unsigned);
  1162. PRINT_ATTRf(context_switch, p_unsigned);
  1163. PRINT_ATTRf(write_backward, p_unsigned);
  1164. PRINT_ATTRn("{ wakeup_events, wakeup_watermark }", wakeup_events, p_unsigned);
  1165. PRINT_ATTRf(bp_type, p_unsigned);
  1166. PRINT_ATTRn("{ bp_addr, config1 }", bp_addr, p_hex);
  1167. PRINT_ATTRn("{ bp_len, config2 }", bp_len, p_hex);
  1168. PRINT_ATTRf(branch_sample_type, p_branch_sample_type);
  1169. PRINT_ATTRf(sample_regs_user, p_hex);
  1170. PRINT_ATTRf(sample_stack_user, p_unsigned);
  1171. PRINT_ATTRf(clockid, p_signed);
  1172. PRINT_ATTRf(sample_regs_intr, p_hex);
  1173. PRINT_ATTRf(aux_watermark, p_unsigned);
  1174. PRINT_ATTRf(sample_max_stack, p_unsigned);
  1175. return ret;
  1176. }
  1177. static int __open_attr__fprintf(FILE *fp, const char *name, const char *val,
  1178. void *priv __attribute__((unused)))
  1179. {
  1180. return fprintf(fp, " %-32s %s\n", name, val);
  1181. }
  1182. static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
  1183. struct thread_map *threads)
  1184. {
  1185. int cpu, thread, nthreads;
  1186. unsigned long flags = PERF_FLAG_FD_CLOEXEC;
  1187. int pid = -1, err;
  1188. enum { NO_CHANGE, SET_TO_MAX, INCREASED_MAX } set_rlimit = NO_CHANGE;
  1189. if (perf_missing_features.write_backward && evsel->attr.write_backward)
  1190. return -EINVAL;
  1191. if (evsel->system_wide)
  1192. nthreads = 1;
  1193. else
  1194. nthreads = threads->nr;
  1195. if (evsel->fd == NULL &&
  1196. perf_evsel__alloc_fd(evsel, cpus->nr, nthreads) < 0)
  1197. return -ENOMEM;
  1198. if (evsel->cgrp) {
  1199. flags |= PERF_FLAG_PID_CGROUP;
  1200. pid = evsel->cgrp->fd;
  1201. }
  1202. fallback_missing_features:
  1203. if (perf_missing_features.clockid_wrong)
  1204. evsel->attr.clockid = CLOCK_MONOTONIC; /* should always work */
  1205. if (perf_missing_features.clockid) {
  1206. evsel->attr.use_clockid = 0;
  1207. evsel->attr.clockid = 0;
  1208. }
  1209. if (perf_missing_features.cloexec)
  1210. flags &= ~(unsigned long)PERF_FLAG_FD_CLOEXEC;
  1211. if (perf_missing_features.mmap2)
  1212. evsel->attr.mmap2 = 0;
  1213. if (perf_missing_features.exclude_guest)
  1214. evsel->attr.exclude_guest = evsel->attr.exclude_host = 0;
  1215. if (perf_missing_features.lbr_flags)
  1216. evsel->attr.branch_sample_type &= ~(PERF_SAMPLE_BRANCH_NO_FLAGS |
  1217. PERF_SAMPLE_BRANCH_NO_CYCLES);
  1218. retry_sample_id:
  1219. if (perf_missing_features.sample_id_all)
  1220. evsel->attr.sample_id_all = 0;
  1221. if (verbose >= 2) {
  1222. fprintf(stderr, "%.60s\n", graph_dotted_line);
  1223. fprintf(stderr, "perf_event_attr:\n");
  1224. perf_event_attr__fprintf(stderr, &evsel->attr, __open_attr__fprintf, NULL);
  1225. fprintf(stderr, "%.60s\n", graph_dotted_line);
  1226. }
  1227. for (cpu = 0; cpu < cpus->nr; cpu++) {
  1228. for (thread = 0; thread < nthreads; thread++) {
  1229. int group_fd;
  1230. if (!evsel->cgrp && !evsel->system_wide)
  1231. pid = thread_map__pid(threads, thread);
  1232. group_fd = get_group_fd(evsel, cpu, thread);
  1233. retry_open:
  1234. pr_debug2("sys_perf_event_open: pid %d cpu %d group_fd %d flags %#lx\n",
  1235. pid, cpus->map[cpu], group_fd, flags);
  1236. FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
  1237. pid,
  1238. cpus->map[cpu],
  1239. group_fd, flags);
  1240. if (FD(evsel, cpu, thread) < 0) {
  1241. err = -errno;
  1242. pr_debug2("sys_perf_event_open failed, error %d\n",
  1243. err);
  1244. goto try_fallback;
  1245. }
  1246. if (evsel->bpf_fd >= 0) {
  1247. int evt_fd = FD(evsel, cpu, thread);
  1248. int bpf_fd = evsel->bpf_fd;
  1249. err = ioctl(evt_fd,
  1250. PERF_EVENT_IOC_SET_BPF,
  1251. bpf_fd);
  1252. if (err && errno != EEXIST) {
  1253. pr_err("failed to attach bpf fd %d: %s\n",
  1254. bpf_fd, strerror(errno));
  1255. err = -EINVAL;
  1256. goto out_close;
  1257. }
  1258. }
  1259. set_rlimit = NO_CHANGE;
  1260. /*
  1261. * If we succeeded but had to kill clockid, fail and
  1262. * have perf_evsel__open_strerror() print us a nice
  1263. * error.
  1264. */
  1265. if (perf_missing_features.clockid ||
  1266. perf_missing_features.clockid_wrong) {
  1267. err = -EINVAL;
  1268. goto out_close;
  1269. }
  1270. }
  1271. }
  1272. return 0;
  1273. try_fallback:
  1274. /*
  1275. * perf stat needs between 5 and 22 fds per CPU. When we run out
  1276. * of them try to increase the limits.
  1277. */
  1278. if (err == -EMFILE && set_rlimit < INCREASED_MAX) {
  1279. struct rlimit l;
  1280. int old_errno = errno;
  1281. if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
  1282. if (set_rlimit == NO_CHANGE)
  1283. l.rlim_cur = l.rlim_max;
  1284. else {
  1285. l.rlim_cur = l.rlim_max + 1000;
  1286. l.rlim_max = l.rlim_cur;
  1287. }
  1288. if (setrlimit(RLIMIT_NOFILE, &l) == 0) {
  1289. set_rlimit++;
  1290. errno = old_errno;
  1291. goto retry_open;
  1292. }
  1293. }
  1294. errno = old_errno;
  1295. }
  1296. if (err != -EINVAL || cpu > 0 || thread > 0)
  1297. goto out_close;
  1298. /*
  1299. * Must probe features in the order they were added to the
  1300. * perf_event_attr interface.
  1301. */
  1302. if (!perf_missing_features.write_backward && evsel->attr.write_backward) {
  1303. perf_missing_features.write_backward = true;
  1304. goto out_close;
  1305. } else if (!perf_missing_features.clockid_wrong && evsel->attr.use_clockid) {
  1306. perf_missing_features.clockid_wrong = true;
  1307. goto fallback_missing_features;
  1308. } else if (!perf_missing_features.clockid && evsel->attr.use_clockid) {
  1309. perf_missing_features.clockid = true;
  1310. goto fallback_missing_features;
  1311. } else if (!perf_missing_features.cloexec && (flags & PERF_FLAG_FD_CLOEXEC)) {
  1312. perf_missing_features.cloexec = true;
  1313. goto fallback_missing_features;
  1314. } else if (!perf_missing_features.mmap2 && evsel->attr.mmap2) {
  1315. perf_missing_features.mmap2 = true;
  1316. goto fallback_missing_features;
  1317. } else if (!perf_missing_features.exclude_guest &&
  1318. (evsel->attr.exclude_guest || evsel->attr.exclude_host)) {
  1319. perf_missing_features.exclude_guest = true;
  1320. goto fallback_missing_features;
  1321. } else if (!perf_missing_features.sample_id_all) {
  1322. perf_missing_features.sample_id_all = true;
  1323. goto retry_sample_id;
  1324. } else if (!perf_missing_features.lbr_flags &&
  1325. (evsel->attr.branch_sample_type &
  1326. (PERF_SAMPLE_BRANCH_NO_CYCLES |
  1327. PERF_SAMPLE_BRANCH_NO_FLAGS))) {
  1328. perf_missing_features.lbr_flags = true;
  1329. goto fallback_missing_features;
  1330. }
  1331. out_close:
  1332. do {
  1333. while (--thread >= 0) {
  1334. close(FD(evsel, cpu, thread));
  1335. FD(evsel, cpu, thread) = -1;
  1336. }
  1337. thread = nthreads;
  1338. } while (--cpu >= 0);
  1339. return err;
  1340. }
  1341. void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
  1342. {
  1343. if (evsel->fd == NULL)
  1344. return;
  1345. perf_evsel__close_fd(evsel, ncpus, nthreads);
  1346. perf_evsel__free_fd(evsel);
  1347. }
  1348. static struct {
  1349. struct cpu_map map;
  1350. int cpus[1];
  1351. } empty_cpu_map = {
  1352. .map.nr = 1,
  1353. .cpus = { -1, },
  1354. };
  1355. static struct {
  1356. struct thread_map map;
  1357. int threads[1];
  1358. } empty_thread_map = {
  1359. .map.nr = 1,
  1360. .threads = { -1, },
  1361. };
  1362. int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
  1363. struct thread_map *threads)
  1364. {
  1365. if (cpus == NULL) {
  1366. /* Work around old compiler warnings about strict aliasing */
  1367. cpus = &empty_cpu_map.map;
  1368. }
  1369. if (threads == NULL)
  1370. threads = &empty_thread_map.map;
  1371. return __perf_evsel__open(evsel, cpus, threads);
  1372. }
  1373. int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
  1374. struct cpu_map *cpus)
  1375. {
  1376. return __perf_evsel__open(evsel, cpus, &empty_thread_map.map);
  1377. }
  1378. int perf_evsel__open_per_thread(struct perf_evsel *evsel,
  1379. struct thread_map *threads)
  1380. {
  1381. return __perf_evsel__open(evsel, &empty_cpu_map.map, threads);
  1382. }
  1383. static int perf_evsel__parse_id_sample(const struct perf_evsel *evsel,
  1384. const union perf_event *event,
  1385. struct perf_sample *sample)
  1386. {
  1387. u64 type = evsel->attr.sample_type;
  1388. const u64 *array = event->sample.array;
  1389. bool swapped = evsel->needs_swap;
  1390. union u64_swap u;
  1391. array += ((event->header.size -
  1392. sizeof(event->header)) / sizeof(u64)) - 1;
  1393. if (type & PERF_SAMPLE_IDENTIFIER) {
  1394. sample->id = *array;
  1395. array--;
  1396. }
  1397. if (type & PERF_SAMPLE_CPU) {
  1398. u.val64 = *array;
  1399. if (swapped) {
  1400. /* undo swap of u64, then swap on individual u32s */
  1401. u.val64 = bswap_64(u.val64);
  1402. u.val32[0] = bswap_32(u.val32[0]);
  1403. }
  1404. sample->cpu = u.val32[0];
  1405. array--;
  1406. }
  1407. if (type & PERF_SAMPLE_STREAM_ID) {
  1408. sample->stream_id = *array;
  1409. array--;
  1410. }
  1411. if (type & PERF_SAMPLE_ID) {
  1412. sample->id = *array;
  1413. array--;
  1414. }
  1415. if (type & PERF_SAMPLE_TIME) {
  1416. sample->time = *array;
  1417. array--;
  1418. }
  1419. if (type & PERF_SAMPLE_TID) {
  1420. u.val64 = *array;
  1421. if (swapped) {
  1422. /* undo swap of u64, then swap on individual u32s */
  1423. u.val64 = bswap_64(u.val64);
  1424. u.val32[0] = bswap_32(u.val32[0]);
  1425. u.val32[1] = bswap_32(u.val32[1]);
  1426. }
  1427. sample->pid = u.val32[0];
  1428. sample->tid = u.val32[1];
  1429. array--;
  1430. }
  1431. return 0;
  1432. }
  1433. static inline bool overflow(const void *endp, u16 max_size, const void *offset,
  1434. u64 size)
  1435. {
  1436. return size > max_size || offset + size > endp;
  1437. }
  1438. #define OVERFLOW_CHECK(offset, size, max_size) \
  1439. do { \
  1440. if (overflow(endp, (max_size), (offset), (size))) \
  1441. return -EFAULT; \
  1442. } while (0)
  1443. #define OVERFLOW_CHECK_u64(offset) \
  1444. OVERFLOW_CHECK(offset, sizeof(u64), sizeof(u64))
  1445. int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
  1446. struct perf_sample *data)
  1447. {
  1448. u64 type = evsel->attr.sample_type;
  1449. bool swapped = evsel->needs_swap;
  1450. const u64 *array;
  1451. u16 max_size = event->header.size;
  1452. const void *endp = (void *)event + max_size;
  1453. u64 sz;
  1454. /*
  1455. * used for cross-endian analysis. See git commit 65014ab3
  1456. * for why this goofiness is needed.
  1457. */
  1458. union u64_swap u;
  1459. memset(data, 0, sizeof(*data));
  1460. data->cpu = data->pid = data->tid = -1;
  1461. data->stream_id = data->id = data->time = -1ULL;
  1462. data->period = evsel->attr.sample_period;
  1463. data->cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  1464. if (event->header.type != PERF_RECORD_SAMPLE) {
  1465. if (!evsel->attr.sample_id_all)
  1466. return 0;
  1467. return perf_evsel__parse_id_sample(evsel, event, data);
  1468. }
  1469. array = event->sample.array;
  1470. /*
  1471. * The evsel's sample_size is based on PERF_SAMPLE_MASK which includes
  1472. * up to PERF_SAMPLE_PERIOD. After that overflow() must be used to
  1473. * check the format does not go past the end of the event.
  1474. */
  1475. if (evsel->sample_size + sizeof(event->header) > event->header.size)
  1476. return -EFAULT;
  1477. data->id = -1ULL;
  1478. if (type & PERF_SAMPLE_IDENTIFIER) {
  1479. data->id = *array;
  1480. array++;
  1481. }
  1482. if (type & PERF_SAMPLE_IP) {
  1483. data->ip = *array;
  1484. array++;
  1485. }
  1486. if (type & PERF_SAMPLE_TID) {
  1487. u.val64 = *array;
  1488. if (swapped) {
  1489. /* undo swap of u64, then swap on individual u32s */
  1490. u.val64 = bswap_64(u.val64);
  1491. u.val32[0] = bswap_32(u.val32[0]);
  1492. u.val32[1] = bswap_32(u.val32[1]);
  1493. }
  1494. data->pid = u.val32[0];
  1495. data->tid = u.val32[1];
  1496. array++;
  1497. }
  1498. if (type & PERF_SAMPLE_TIME) {
  1499. data->time = *array;
  1500. array++;
  1501. }
  1502. data->addr = 0;
  1503. if (type & PERF_SAMPLE_ADDR) {
  1504. data->addr = *array;
  1505. array++;
  1506. }
  1507. if (type & PERF_SAMPLE_ID) {
  1508. data->id = *array;
  1509. array++;
  1510. }
  1511. if (type & PERF_SAMPLE_STREAM_ID) {
  1512. data->stream_id = *array;
  1513. array++;
  1514. }
  1515. if (type & PERF_SAMPLE_CPU) {
  1516. u.val64 = *array;
  1517. if (swapped) {
  1518. /* undo swap of u64, then swap on individual u32s */
  1519. u.val64 = bswap_64(u.val64);
  1520. u.val32[0] = bswap_32(u.val32[0]);
  1521. }
  1522. data->cpu = u.val32[0];
  1523. array++;
  1524. }
  1525. if (type & PERF_SAMPLE_PERIOD) {
  1526. data->period = *array;
  1527. array++;
  1528. }
  1529. if (type & PERF_SAMPLE_READ) {
  1530. u64 read_format = evsel->attr.read_format;
  1531. OVERFLOW_CHECK_u64(array);
  1532. if (read_format & PERF_FORMAT_GROUP)
  1533. data->read.group.nr = *array;
  1534. else
  1535. data->read.one.value = *array;
  1536. array++;
  1537. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
  1538. OVERFLOW_CHECK_u64(array);
  1539. data->read.time_enabled = *array;
  1540. array++;
  1541. }
  1542. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
  1543. OVERFLOW_CHECK_u64(array);
  1544. data->read.time_running = *array;
  1545. array++;
  1546. }
  1547. /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
  1548. if (read_format & PERF_FORMAT_GROUP) {
  1549. const u64 max_group_nr = UINT64_MAX /
  1550. sizeof(struct sample_read_value);
  1551. if (data->read.group.nr > max_group_nr)
  1552. return -EFAULT;
  1553. sz = data->read.group.nr *
  1554. sizeof(struct sample_read_value);
  1555. OVERFLOW_CHECK(array, sz, max_size);
  1556. data->read.group.values =
  1557. (struct sample_read_value *)array;
  1558. array = (void *)array + sz;
  1559. } else {
  1560. OVERFLOW_CHECK_u64(array);
  1561. data->read.one.id = *array;
  1562. array++;
  1563. }
  1564. }
  1565. if (type & PERF_SAMPLE_CALLCHAIN) {
  1566. const u64 max_callchain_nr = UINT64_MAX / sizeof(u64);
  1567. OVERFLOW_CHECK_u64(array);
  1568. data->callchain = (struct ip_callchain *)array++;
  1569. if (data->callchain->nr > max_callchain_nr)
  1570. return -EFAULT;
  1571. sz = data->callchain->nr * sizeof(u64);
  1572. OVERFLOW_CHECK(array, sz, max_size);
  1573. array = (void *)array + sz;
  1574. }
  1575. if (type & PERF_SAMPLE_RAW) {
  1576. OVERFLOW_CHECK_u64(array);
  1577. u.val64 = *array;
  1578. if (WARN_ONCE(swapped,
  1579. "Endianness of raw data not corrected!\n")) {
  1580. /* undo swap of u64, then swap on individual u32s */
  1581. u.val64 = bswap_64(u.val64);
  1582. u.val32[0] = bswap_32(u.val32[0]);
  1583. u.val32[1] = bswap_32(u.val32[1]);
  1584. }
  1585. data->raw_size = u.val32[0];
  1586. array = (void *)array + sizeof(u32);
  1587. OVERFLOW_CHECK(array, data->raw_size, max_size);
  1588. data->raw_data = (void *)array;
  1589. array = (void *)array + data->raw_size;
  1590. }
  1591. if (type & PERF_SAMPLE_BRANCH_STACK) {
  1592. const u64 max_branch_nr = UINT64_MAX /
  1593. sizeof(struct branch_entry);
  1594. OVERFLOW_CHECK_u64(array);
  1595. data->branch_stack = (struct branch_stack *)array++;
  1596. if (data->branch_stack->nr > max_branch_nr)
  1597. return -EFAULT;
  1598. sz = data->branch_stack->nr * sizeof(struct branch_entry);
  1599. OVERFLOW_CHECK(array, sz, max_size);
  1600. array = (void *)array + sz;
  1601. }
  1602. if (type & PERF_SAMPLE_REGS_USER) {
  1603. OVERFLOW_CHECK_u64(array);
  1604. data->user_regs.abi = *array;
  1605. array++;
  1606. if (data->user_regs.abi) {
  1607. u64 mask = evsel->attr.sample_regs_user;
  1608. sz = hweight_long(mask) * sizeof(u64);
  1609. OVERFLOW_CHECK(array, sz, max_size);
  1610. data->user_regs.mask = mask;
  1611. data->user_regs.regs = (u64 *)array;
  1612. array = (void *)array + sz;
  1613. }
  1614. }
  1615. if (type & PERF_SAMPLE_STACK_USER) {
  1616. OVERFLOW_CHECK_u64(array);
  1617. sz = *array++;
  1618. data->user_stack.offset = ((char *)(array - 1)
  1619. - (char *) event);
  1620. if (!sz) {
  1621. data->user_stack.size = 0;
  1622. } else {
  1623. OVERFLOW_CHECK(array, sz, max_size);
  1624. data->user_stack.data = (char *)array;
  1625. array = (void *)array + sz;
  1626. OVERFLOW_CHECK_u64(array);
  1627. data->user_stack.size = *array++;
  1628. if (WARN_ONCE(data->user_stack.size > sz,
  1629. "user stack dump failure\n"))
  1630. return -EFAULT;
  1631. }
  1632. }
  1633. if (type & PERF_SAMPLE_WEIGHT) {
  1634. OVERFLOW_CHECK_u64(array);
  1635. data->weight = *array;
  1636. array++;
  1637. }
  1638. data->data_src = PERF_MEM_DATA_SRC_NONE;
  1639. if (type & PERF_SAMPLE_DATA_SRC) {
  1640. OVERFLOW_CHECK_u64(array);
  1641. data->data_src = *array;
  1642. array++;
  1643. }
  1644. data->transaction = 0;
  1645. if (type & PERF_SAMPLE_TRANSACTION) {
  1646. OVERFLOW_CHECK_u64(array);
  1647. data->transaction = *array;
  1648. array++;
  1649. }
  1650. data->intr_regs.abi = PERF_SAMPLE_REGS_ABI_NONE;
  1651. if (type & PERF_SAMPLE_REGS_INTR) {
  1652. OVERFLOW_CHECK_u64(array);
  1653. data->intr_regs.abi = *array;
  1654. array++;
  1655. if (data->intr_regs.abi != PERF_SAMPLE_REGS_ABI_NONE) {
  1656. u64 mask = evsel->attr.sample_regs_intr;
  1657. sz = hweight_long(mask) * sizeof(u64);
  1658. OVERFLOW_CHECK(array, sz, max_size);
  1659. data->intr_regs.mask = mask;
  1660. data->intr_regs.regs = (u64 *)array;
  1661. array = (void *)array + sz;
  1662. }
  1663. }
  1664. return 0;
  1665. }
  1666. size_t perf_event__sample_event_size(const struct perf_sample *sample, u64 type,
  1667. u64 read_format)
  1668. {
  1669. size_t sz, result = sizeof(struct sample_event);
  1670. if (type & PERF_SAMPLE_IDENTIFIER)
  1671. result += sizeof(u64);
  1672. if (type & PERF_SAMPLE_IP)
  1673. result += sizeof(u64);
  1674. if (type & PERF_SAMPLE_TID)
  1675. result += sizeof(u64);
  1676. if (type & PERF_SAMPLE_TIME)
  1677. result += sizeof(u64);
  1678. if (type & PERF_SAMPLE_ADDR)
  1679. result += sizeof(u64);
  1680. if (type & PERF_SAMPLE_ID)
  1681. result += sizeof(u64);
  1682. if (type & PERF_SAMPLE_STREAM_ID)
  1683. result += sizeof(u64);
  1684. if (type & PERF_SAMPLE_CPU)
  1685. result += sizeof(u64);
  1686. if (type & PERF_SAMPLE_PERIOD)
  1687. result += sizeof(u64);
  1688. if (type & PERF_SAMPLE_READ) {
  1689. result += sizeof(u64);
  1690. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  1691. result += sizeof(u64);
  1692. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  1693. result += sizeof(u64);
  1694. /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
  1695. if (read_format & PERF_FORMAT_GROUP) {
  1696. sz = sample->read.group.nr *
  1697. sizeof(struct sample_read_value);
  1698. result += sz;
  1699. } else {
  1700. result += sizeof(u64);
  1701. }
  1702. }
  1703. if (type & PERF_SAMPLE_CALLCHAIN) {
  1704. sz = (sample->callchain->nr + 1) * sizeof(u64);
  1705. result += sz;
  1706. }
  1707. if (type & PERF_SAMPLE_RAW) {
  1708. result += sizeof(u32);
  1709. result += sample->raw_size;
  1710. }
  1711. if (type & PERF_SAMPLE_BRANCH_STACK) {
  1712. sz = sample->branch_stack->nr * sizeof(struct branch_entry);
  1713. sz += sizeof(u64);
  1714. result += sz;
  1715. }
  1716. if (type & PERF_SAMPLE_REGS_USER) {
  1717. if (sample->user_regs.abi) {
  1718. result += sizeof(u64);
  1719. sz = hweight_long(sample->user_regs.mask) * sizeof(u64);
  1720. result += sz;
  1721. } else {
  1722. result += sizeof(u64);
  1723. }
  1724. }
  1725. if (type & PERF_SAMPLE_STACK_USER) {
  1726. sz = sample->user_stack.size;
  1727. result += sizeof(u64);
  1728. if (sz) {
  1729. result += sz;
  1730. result += sizeof(u64);
  1731. }
  1732. }
  1733. if (type & PERF_SAMPLE_WEIGHT)
  1734. result += sizeof(u64);
  1735. if (type & PERF_SAMPLE_DATA_SRC)
  1736. result += sizeof(u64);
  1737. if (type & PERF_SAMPLE_TRANSACTION)
  1738. result += sizeof(u64);
  1739. if (type & PERF_SAMPLE_REGS_INTR) {
  1740. if (sample->intr_regs.abi) {
  1741. result += sizeof(u64);
  1742. sz = hweight_long(sample->intr_regs.mask) * sizeof(u64);
  1743. result += sz;
  1744. } else {
  1745. result += sizeof(u64);
  1746. }
  1747. }
  1748. return result;
  1749. }
  1750. int perf_event__synthesize_sample(union perf_event *event, u64 type,
  1751. u64 read_format,
  1752. const struct perf_sample *sample,
  1753. bool swapped)
  1754. {
  1755. u64 *array;
  1756. size_t sz;
  1757. /*
  1758. * used for cross-endian analysis. See git commit 65014ab3
  1759. * for why this goofiness is needed.
  1760. */
  1761. union u64_swap u;
  1762. array = event->sample.array;
  1763. if (type & PERF_SAMPLE_IDENTIFIER) {
  1764. *array = sample->id;
  1765. array++;
  1766. }
  1767. if (type & PERF_SAMPLE_IP) {
  1768. *array = sample->ip;
  1769. array++;
  1770. }
  1771. if (type & PERF_SAMPLE_TID) {
  1772. u.val32[0] = sample->pid;
  1773. u.val32[1] = sample->tid;
  1774. if (swapped) {
  1775. /*
  1776. * Inverse of what is done in perf_evsel__parse_sample
  1777. */
  1778. u.val32[0] = bswap_32(u.val32[0]);
  1779. u.val32[1] = bswap_32(u.val32[1]);
  1780. u.val64 = bswap_64(u.val64);
  1781. }
  1782. *array = u.val64;
  1783. array++;
  1784. }
  1785. if (type & PERF_SAMPLE_TIME) {
  1786. *array = sample->time;
  1787. array++;
  1788. }
  1789. if (type & PERF_SAMPLE_ADDR) {
  1790. *array = sample->addr;
  1791. array++;
  1792. }
  1793. if (type & PERF_SAMPLE_ID) {
  1794. *array = sample->id;
  1795. array++;
  1796. }
  1797. if (type & PERF_SAMPLE_STREAM_ID) {
  1798. *array = sample->stream_id;
  1799. array++;
  1800. }
  1801. if (type & PERF_SAMPLE_CPU) {
  1802. u.val32[0] = sample->cpu;
  1803. if (swapped) {
  1804. /*
  1805. * Inverse of what is done in perf_evsel__parse_sample
  1806. */
  1807. u.val32[0] = bswap_32(u.val32[0]);
  1808. u.val64 = bswap_64(u.val64);
  1809. }
  1810. *array = u.val64;
  1811. array++;
  1812. }
  1813. if (type & PERF_SAMPLE_PERIOD) {
  1814. *array = sample->period;
  1815. array++;
  1816. }
  1817. if (type & PERF_SAMPLE_READ) {
  1818. if (read_format & PERF_FORMAT_GROUP)
  1819. *array = sample->read.group.nr;
  1820. else
  1821. *array = sample->read.one.value;
  1822. array++;
  1823. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
  1824. *array = sample->read.time_enabled;
  1825. array++;
  1826. }
  1827. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
  1828. *array = sample->read.time_running;
  1829. array++;
  1830. }
  1831. /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
  1832. if (read_format & PERF_FORMAT_GROUP) {
  1833. sz = sample->read.group.nr *
  1834. sizeof(struct sample_read_value);
  1835. memcpy(array, sample->read.group.values, sz);
  1836. array = (void *)array + sz;
  1837. } else {
  1838. *array = sample->read.one.id;
  1839. array++;
  1840. }
  1841. }
  1842. if (type & PERF_SAMPLE_CALLCHAIN) {
  1843. sz = (sample->callchain->nr + 1) * sizeof(u64);
  1844. memcpy(array, sample->callchain, sz);
  1845. array = (void *)array + sz;
  1846. }
  1847. if (type & PERF_SAMPLE_RAW) {
  1848. u.val32[0] = sample->raw_size;
  1849. if (WARN_ONCE(swapped,
  1850. "Endianness of raw data not corrected!\n")) {
  1851. /*
  1852. * Inverse of what is done in perf_evsel__parse_sample
  1853. */
  1854. u.val32[0] = bswap_32(u.val32[0]);
  1855. u.val32[1] = bswap_32(u.val32[1]);
  1856. u.val64 = bswap_64(u.val64);
  1857. }
  1858. *array = u.val64;
  1859. array = (void *)array + sizeof(u32);
  1860. memcpy(array, sample->raw_data, sample->raw_size);
  1861. array = (void *)array + sample->raw_size;
  1862. }
  1863. if (type & PERF_SAMPLE_BRANCH_STACK) {
  1864. sz = sample->branch_stack->nr * sizeof(struct branch_entry);
  1865. sz += sizeof(u64);
  1866. memcpy(array, sample->branch_stack, sz);
  1867. array = (void *)array + sz;
  1868. }
  1869. if (type & PERF_SAMPLE_REGS_USER) {
  1870. if (sample->user_regs.abi) {
  1871. *array++ = sample->user_regs.abi;
  1872. sz = hweight_long(sample->user_regs.mask) * sizeof(u64);
  1873. memcpy(array, sample->user_regs.regs, sz);
  1874. array = (void *)array + sz;
  1875. } else {
  1876. *array++ = 0;
  1877. }
  1878. }
  1879. if (type & PERF_SAMPLE_STACK_USER) {
  1880. sz = sample->user_stack.size;
  1881. *array++ = sz;
  1882. if (sz) {
  1883. memcpy(array, sample->user_stack.data, sz);
  1884. array = (void *)array + sz;
  1885. *array++ = sz;
  1886. }
  1887. }
  1888. if (type & PERF_SAMPLE_WEIGHT) {
  1889. *array = sample->weight;
  1890. array++;
  1891. }
  1892. if (type & PERF_SAMPLE_DATA_SRC) {
  1893. *array = sample->data_src;
  1894. array++;
  1895. }
  1896. if (type & PERF_SAMPLE_TRANSACTION) {
  1897. *array = sample->transaction;
  1898. array++;
  1899. }
  1900. if (type & PERF_SAMPLE_REGS_INTR) {
  1901. if (sample->intr_regs.abi) {
  1902. *array++ = sample->intr_regs.abi;
  1903. sz = hweight_long(sample->intr_regs.mask) * sizeof(u64);
  1904. memcpy(array, sample->intr_regs.regs, sz);
  1905. array = (void *)array + sz;
  1906. } else {
  1907. *array++ = 0;
  1908. }
  1909. }
  1910. return 0;
  1911. }
  1912. struct format_field *perf_evsel__field(struct perf_evsel *evsel, const char *name)
  1913. {
  1914. return pevent_find_field(evsel->tp_format, name);
  1915. }
  1916. void *perf_evsel__rawptr(struct perf_evsel *evsel, struct perf_sample *sample,
  1917. const char *name)
  1918. {
  1919. struct format_field *field = perf_evsel__field(evsel, name);
  1920. int offset;
  1921. if (!field)
  1922. return NULL;
  1923. offset = field->offset;
  1924. if (field->flags & FIELD_IS_DYNAMIC) {
  1925. offset = *(int *)(sample->raw_data + field->offset);
  1926. offset &= 0xffff;
  1927. }
  1928. return sample->raw_data + offset;
  1929. }
  1930. u64 format_field__intval(struct format_field *field, struct perf_sample *sample,
  1931. bool needs_swap)
  1932. {
  1933. u64 value;
  1934. void *ptr = sample->raw_data + field->offset;
  1935. switch (field->size) {
  1936. case 1:
  1937. return *(u8 *)ptr;
  1938. case 2:
  1939. value = *(u16 *)ptr;
  1940. break;
  1941. case 4:
  1942. value = *(u32 *)ptr;
  1943. break;
  1944. case 8:
  1945. memcpy(&value, ptr, sizeof(u64));
  1946. break;
  1947. default:
  1948. return 0;
  1949. }
  1950. if (!needs_swap)
  1951. return value;
  1952. switch (field->size) {
  1953. case 2:
  1954. return bswap_16(value);
  1955. case 4:
  1956. return bswap_32(value);
  1957. case 8:
  1958. return bswap_64(value);
  1959. default:
  1960. return 0;
  1961. }
  1962. return 0;
  1963. }
  1964. u64 perf_evsel__intval(struct perf_evsel *evsel, struct perf_sample *sample,
  1965. const char *name)
  1966. {
  1967. struct format_field *field = perf_evsel__field(evsel, name);
  1968. if (!field)
  1969. return 0;
  1970. return field ? format_field__intval(field, sample, evsel->needs_swap) : 0;
  1971. }
  1972. bool perf_evsel__fallback(struct perf_evsel *evsel, int err,
  1973. char *msg, size_t msgsize)
  1974. {
  1975. int paranoid;
  1976. if ((err == ENOENT || err == ENXIO || err == ENODEV) &&
  1977. evsel->attr.type == PERF_TYPE_HARDWARE &&
  1978. evsel->attr.config == PERF_COUNT_HW_CPU_CYCLES) {
  1979. /*
  1980. * If it's cycles then fall back to hrtimer based
  1981. * cpu-clock-tick sw counter, which is always available even if
  1982. * no PMU support.
  1983. *
  1984. * PPC returns ENXIO until 2.6.37 (behavior changed with commit
  1985. * b0a873e).
  1986. */
  1987. scnprintf(msg, msgsize, "%s",
  1988. "The cycles event is not supported, trying to fall back to cpu-clock-ticks");
  1989. evsel->attr.type = PERF_TYPE_SOFTWARE;
  1990. evsel->attr.config = PERF_COUNT_SW_CPU_CLOCK;
  1991. zfree(&evsel->name);
  1992. return true;
  1993. } else if (err == EACCES && !evsel->attr.exclude_kernel &&
  1994. (paranoid = perf_event_paranoid()) > 1) {
  1995. const char *name = perf_evsel__name(evsel);
  1996. char *new_name;
  1997. if (asprintf(&new_name, "%s%su", name, strchr(name, ':') ? "" : ":") < 0)
  1998. return false;
  1999. if (evsel->name)
  2000. free(evsel->name);
  2001. evsel->name = new_name;
  2002. scnprintf(msg, msgsize,
  2003. "kernel.perf_event_paranoid=%d, trying to fall back to excluding kernel samples", paranoid);
  2004. evsel->attr.exclude_kernel = 1;
  2005. return true;
  2006. }
  2007. return false;
  2008. }
  2009. int perf_evsel__open_strerror(struct perf_evsel *evsel, struct target *target,
  2010. int err, char *msg, size_t size)
  2011. {
  2012. char sbuf[STRERR_BUFSIZE];
  2013. int printed = 0;
  2014. switch (err) {
  2015. case EPERM:
  2016. case EACCES:
  2017. if (err == EPERM)
  2018. printed = scnprintf(msg, size,
  2019. "No permission to enable %s event.\n\n",
  2020. perf_evsel__name(evsel));
  2021. return scnprintf(msg + printed, size - printed,
  2022. "You may not have permission to collect %sstats.\n\n"
  2023. "Consider tweaking /proc/sys/kernel/perf_event_paranoid,\n"
  2024. "which controls use of the performance events system by\n"
  2025. "unprivileged users (without CAP_SYS_ADMIN).\n\n"
  2026. "The current value is %d:\n\n"
  2027. " -1: Allow use of (almost) all events by all users\n"
  2028. ">= 0: Disallow raw tracepoint access by users without CAP_IOC_LOCK\n"
  2029. ">= 1: Disallow CPU event access by users without CAP_SYS_ADMIN\n"
  2030. ">= 2: Disallow kernel profiling by users without CAP_SYS_ADMIN",
  2031. target->system_wide ? "system-wide " : "",
  2032. perf_event_paranoid());
  2033. case ENOENT:
  2034. return scnprintf(msg, size, "The %s event is not supported.",
  2035. perf_evsel__name(evsel));
  2036. case EMFILE:
  2037. return scnprintf(msg, size, "%s",
  2038. "Too many events are opened.\n"
  2039. "Probably the maximum number of open file descriptors has been reached.\n"
  2040. "Hint: Try again after reducing the number of events.\n"
  2041. "Hint: Try increasing the limit with 'ulimit -n <limit>'");
  2042. case ENOMEM:
  2043. if ((evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN) != 0 &&
  2044. access("/proc/sys/kernel/perf_event_max_stack", F_OK) == 0)
  2045. return scnprintf(msg, size,
  2046. "Not enough memory to setup event with callchain.\n"
  2047. "Hint: Try tweaking /proc/sys/kernel/perf_event_max_stack\n"
  2048. "Hint: Current value: %d", sysctl_perf_event_max_stack);
  2049. break;
  2050. case ENODEV:
  2051. if (target->cpu_list)
  2052. return scnprintf(msg, size, "%s",
  2053. "No such device - did you specify an out-of-range profile CPU?");
  2054. break;
  2055. case EOPNOTSUPP:
  2056. if (evsel->attr.sample_period != 0)
  2057. return scnprintf(msg, size, "%s",
  2058. "PMU Hardware doesn't support sampling/overflow-interrupts.");
  2059. if (evsel->attr.precise_ip)
  2060. return scnprintf(msg, size, "%s",
  2061. "\'precise\' request may not be supported. Try removing 'p' modifier.");
  2062. #if defined(__i386__) || defined(__x86_64__)
  2063. if (evsel->attr.type == PERF_TYPE_HARDWARE)
  2064. return scnprintf(msg, size, "%s",
  2065. "No hardware sampling interrupt available.\n"
  2066. "No APIC? If so then you can boot the kernel with the \"lapic\" boot parameter to force-enable it.");
  2067. #endif
  2068. break;
  2069. case EBUSY:
  2070. if (find_process("oprofiled"))
  2071. return scnprintf(msg, size,
  2072. "The PMU counters are busy/taken by another profiler.\n"
  2073. "We found oprofile daemon running, please stop it and try again.");
  2074. break;
  2075. case EINVAL:
  2076. if (evsel->attr.write_backward && perf_missing_features.write_backward)
  2077. return scnprintf(msg, size, "Reading from overwrite event is not supported by this kernel.");
  2078. if (perf_missing_features.clockid)
  2079. return scnprintf(msg, size, "clockid feature not supported.");
  2080. if (perf_missing_features.clockid_wrong)
  2081. return scnprintf(msg, size, "wrong clockid (%d).", clockid);
  2082. break;
  2083. default:
  2084. break;
  2085. }
  2086. return scnprintf(msg, size,
  2087. "The sys_perf_event_open() syscall returned with %d (%s) for event (%s).\n"
  2088. "/bin/dmesg may provide additional information.\n"
  2089. "No CONFIG_PERF_EVENTS=y kernel support configured?",
  2090. err, str_error_r(err, sbuf, sizeof(sbuf)),
  2091. perf_evsel__name(evsel));
  2092. }
  2093. char *perf_evsel__env_arch(struct perf_evsel *evsel)
  2094. {
  2095. if (evsel && evsel->evlist && evsel->evlist->env)
  2096. return evsel->evlist->env->arch;
  2097. return NULL;
  2098. }