evsel.c 71 KB

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