evsel.c 51 KB

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