evsel.c 57 KB

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