evsel.c 69 KB

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