evsel.c 73 KB

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