evsel.c 48 KB

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