evsel.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166
  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->mmap = track;
  588. attr->mmap2 = track && !perf_missing_features.mmap2;
  589. attr->comm = track;
  590. if (opts->sample_transaction)
  591. perf_evsel__set_sample_bit(evsel, TRANSACTION);
  592. /*
  593. * XXX see the function comment above
  594. *
  595. * Disabling only independent events or group leaders,
  596. * keeping group members enabled.
  597. */
  598. if (perf_evsel__is_group_leader(evsel))
  599. attr->disabled = 1;
  600. /*
  601. * Setting enable_on_exec for independent events and
  602. * group leaders for traced executed by perf.
  603. */
  604. if (target__none(&opts->target) && perf_evsel__is_group_leader(evsel) &&
  605. !opts->initial_delay)
  606. attr->enable_on_exec = 1;
  607. if (evsel->immediate) {
  608. attr->disabled = 0;
  609. attr->enable_on_exec = 0;
  610. }
  611. }
  612. static int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
  613. {
  614. int cpu, thread;
  615. if (evsel->system_wide)
  616. nthreads = 1;
  617. evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
  618. if (evsel->fd) {
  619. for (cpu = 0; cpu < ncpus; cpu++) {
  620. for (thread = 0; thread < nthreads; thread++) {
  621. FD(evsel, cpu, thread) = -1;
  622. }
  623. }
  624. }
  625. return evsel->fd != NULL ? 0 : -ENOMEM;
  626. }
  627. static int perf_evsel__run_ioctl(struct perf_evsel *evsel, int ncpus, int nthreads,
  628. int ioc, void *arg)
  629. {
  630. int cpu, thread;
  631. if (evsel->system_wide)
  632. nthreads = 1;
  633. for (cpu = 0; cpu < ncpus; cpu++) {
  634. for (thread = 0; thread < nthreads; thread++) {
  635. int fd = FD(evsel, cpu, thread),
  636. err = ioctl(fd, ioc, arg);
  637. if (err)
  638. return err;
  639. }
  640. }
  641. return 0;
  642. }
  643. int perf_evsel__set_filter(struct perf_evsel *evsel, int ncpus, int nthreads,
  644. const char *filter)
  645. {
  646. return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
  647. PERF_EVENT_IOC_SET_FILTER,
  648. (void *)filter);
  649. }
  650. int perf_evsel__enable(struct perf_evsel *evsel, int ncpus, int nthreads)
  651. {
  652. return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
  653. PERF_EVENT_IOC_ENABLE,
  654. 0);
  655. }
  656. int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
  657. {
  658. if (evsel->system_wide)
  659. nthreads = 1;
  660. evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
  661. if (evsel->sample_id == NULL)
  662. return -ENOMEM;
  663. evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
  664. if (evsel->id == NULL) {
  665. xyarray__delete(evsel->sample_id);
  666. evsel->sample_id = NULL;
  667. return -ENOMEM;
  668. }
  669. return 0;
  670. }
  671. void perf_evsel__reset_counts(struct perf_evsel *evsel, int ncpus)
  672. {
  673. memset(evsel->counts, 0, (sizeof(*evsel->counts) +
  674. (ncpus * sizeof(struct perf_counts_values))));
  675. }
  676. int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
  677. {
  678. evsel->counts = zalloc((sizeof(*evsel->counts) +
  679. (ncpus * sizeof(struct perf_counts_values))));
  680. return evsel->counts != NULL ? 0 : -ENOMEM;
  681. }
  682. static void perf_evsel__free_fd(struct perf_evsel *evsel)
  683. {
  684. xyarray__delete(evsel->fd);
  685. evsel->fd = NULL;
  686. }
  687. static void perf_evsel__free_id(struct perf_evsel *evsel)
  688. {
  689. xyarray__delete(evsel->sample_id);
  690. evsel->sample_id = NULL;
  691. zfree(&evsel->id);
  692. }
  693. void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
  694. {
  695. int cpu, thread;
  696. if (evsel->system_wide)
  697. nthreads = 1;
  698. for (cpu = 0; cpu < ncpus; cpu++)
  699. for (thread = 0; thread < nthreads; ++thread) {
  700. close(FD(evsel, cpu, thread));
  701. FD(evsel, cpu, thread) = -1;
  702. }
  703. }
  704. void perf_evsel__free_counts(struct perf_evsel *evsel)
  705. {
  706. zfree(&evsel->counts);
  707. }
  708. void perf_evsel__exit(struct perf_evsel *evsel)
  709. {
  710. assert(list_empty(&evsel->node));
  711. perf_evsel__free_fd(evsel);
  712. perf_evsel__free_id(evsel);
  713. close_cgroup(evsel->cgrp);
  714. zfree(&evsel->group_name);
  715. zfree(&evsel->name);
  716. perf_evsel__object.fini(evsel);
  717. }
  718. void perf_evsel__delete(struct perf_evsel *evsel)
  719. {
  720. perf_evsel__exit(evsel);
  721. free(evsel);
  722. }
  723. static inline void compute_deltas(struct perf_evsel *evsel,
  724. int cpu,
  725. struct perf_counts_values *count)
  726. {
  727. struct perf_counts_values tmp;
  728. if (!evsel->prev_raw_counts)
  729. return;
  730. if (cpu == -1) {
  731. tmp = evsel->prev_raw_counts->aggr;
  732. evsel->prev_raw_counts->aggr = *count;
  733. } else {
  734. tmp = evsel->prev_raw_counts->cpu[cpu];
  735. evsel->prev_raw_counts->cpu[cpu] = *count;
  736. }
  737. count->val = count->val - tmp.val;
  738. count->ena = count->ena - tmp.ena;
  739. count->run = count->run - tmp.run;
  740. }
  741. int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
  742. int cpu, int thread, bool scale)
  743. {
  744. struct perf_counts_values count;
  745. size_t nv = scale ? 3 : 1;
  746. if (FD(evsel, cpu, thread) < 0)
  747. return -EINVAL;
  748. if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
  749. return -ENOMEM;
  750. if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
  751. return -errno;
  752. compute_deltas(evsel, cpu, &count);
  753. if (scale) {
  754. if (count.run == 0)
  755. count.val = 0;
  756. else if (count.run < count.ena)
  757. count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
  758. } else
  759. count.ena = count.run = 0;
  760. evsel->counts->cpu[cpu] = count;
  761. return 0;
  762. }
  763. int __perf_evsel__read(struct perf_evsel *evsel,
  764. int ncpus, int nthreads, bool scale)
  765. {
  766. size_t nv = scale ? 3 : 1;
  767. int cpu, thread;
  768. struct perf_counts_values *aggr = &evsel->counts->aggr, count;
  769. if (evsel->system_wide)
  770. nthreads = 1;
  771. aggr->val = aggr->ena = aggr->run = 0;
  772. for (cpu = 0; cpu < ncpus; cpu++) {
  773. for (thread = 0; thread < nthreads; thread++) {
  774. if (FD(evsel, cpu, thread) < 0)
  775. continue;
  776. if (readn(FD(evsel, cpu, thread),
  777. &count, nv * sizeof(u64)) < 0)
  778. return -errno;
  779. aggr->val += count.val;
  780. if (scale) {
  781. aggr->ena += count.ena;
  782. aggr->run += count.run;
  783. }
  784. }
  785. }
  786. compute_deltas(evsel, -1, aggr);
  787. evsel->counts->scaled = 0;
  788. if (scale) {
  789. if (aggr->run == 0) {
  790. evsel->counts->scaled = -1;
  791. aggr->val = 0;
  792. return 0;
  793. }
  794. if (aggr->run < aggr->ena) {
  795. evsel->counts->scaled = 1;
  796. aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
  797. }
  798. } else
  799. aggr->ena = aggr->run = 0;
  800. return 0;
  801. }
  802. static int get_group_fd(struct perf_evsel *evsel, int cpu, int thread)
  803. {
  804. struct perf_evsel *leader = evsel->leader;
  805. int fd;
  806. if (perf_evsel__is_group_leader(evsel))
  807. return -1;
  808. /*
  809. * Leader must be already processed/open,
  810. * if not it's a bug.
  811. */
  812. BUG_ON(!leader->fd);
  813. fd = FD(leader, cpu, thread);
  814. BUG_ON(fd == -1);
  815. return fd;
  816. }
  817. #define __PRINT_ATTR(fmt, cast, field) \
  818. fprintf(fp, " %-19s "fmt"\n", #field, cast attr->field)
  819. #define PRINT_ATTR_U32(field) __PRINT_ATTR("%u" , , field)
  820. #define PRINT_ATTR_X32(field) __PRINT_ATTR("%#x", , field)
  821. #define PRINT_ATTR_U64(field) __PRINT_ATTR("%" PRIu64, (uint64_t), field)
  822. #define PRINT_ATTR_X64(field) __PRINT_ATTR("%#"PRIx64, (uint64_t), field)
  823. #define PRINT_ATTR2N(name1, field1, name2, field2) \
  824. fprintf(fp, " %-19s %u %-19s %u\n", \
  825. name1, attr->field1, name2, attr->field2)
  826. #define PRINT_ATTR2(field1, field2) \
  827. PRINT_ATTR2N(#field1, field1, #field2, field2)
  828. static size_t perf_event_attr__fprintf(struct perf_event_attr *attr, FILE *fp)
  829. {
  830. size_t ret = 0;
  831. ret += fprintf(fp, "%.60s\n", graph_dotted_line);
  832. ret += fprintf(fp, "perf_event_attr:\n");
  833. ret += PRINT_ATTR_U32(type);
  834. ret += PRINT_ATTR_U32(size);
  835. ret += PRINT_ATTR_X64(config);
  836. ret += PRINT_ATTR_U64(sample_period);
  837. ret += PRINT_ATTR_U64(sample_freq);
  838. ret += PRINT_ATTR_X64(sample_type);
  839. ret += PRINT_ATTR_X64(read_format);
  840. ret += PRINT_ATTR2(disabled, inherit);
  841. ret += PRINT_ATTR2(pinned, exclusive);
  842. ret += PRINT_ATTR2(exclude_user, exclude_kernel);
  843. ret += PRINT_ATTR2(exclude_hv, exclude_idle);
  844. ret += PRINT_ATTR2(mmap, comm);
  845. ret += PRINT_ATTR2(mmap2, comm_exec);
  846. ret += PRINT_ATTR2(freq, inherit_stat);
  847. ret += PRINT_ATTR2(enable_on_exec, task);
  848. ret += PRINT_ATTR2(watermark, precise_ip);
  849. ret += PRINT_ATTR2(mmap_data, sample_id_all);
  850. ret += PRINT_ATTR2(exclude_host, exclude_guest);
  851. ret += PRINT_ATTR2N("excl.callchain_kern", exclude_callchain_kernel,
  852. "excl.callchain_user", exclude_callchain_user);
  853. ret += PRINT_ATTR_U32(wakeup_events);
  854. ret += PRINT_ATTR_U32(wakeup_watermark);
  855. ret += PRINT_ATTR_X32(bp_type);
  856. ret += PRINT_ATTR_X64(bp_addr);
  857. ret += PRINT_ATTR_X64(config1);
  858. ret += PRINT_ATTR_U64(bp_len);
  859. ret += PRINT_ATTR_X64(config2);
  860. ret += PRINT_ATTR_X64(branch_sample_type);
  861. ret += PRINT_ATTR_X64(sample_regs_user);
  862. ret += PRINT_ATTR_U32(sample_stack_user);
  863. ret += PRINT_ATTR_X64(sample_regs_intr);
  864. ret += fprintf(fp, "%.60s\n", graph_dotted_line);
  865. return ret;
  866. }
  867. static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
  868. struct thread_map *threads)
  869. {
  870. int cpu, thread, nthreads;
  871. unsigned long flags = PERF_FLAG_FD_CLOEXEC;
  872. int pid = -1, err;
  873. enum { NO_CHANGE, SET_TO_MAX, INCREASED_MAX } set_rlimit = NO_CHANGE;
  874. if (evsel->system_wide)
  875. nthreads = 1;
  876. else
  877. nthreads = threads->nr;
  878. if (evsel->fd == NULL &&
  879. perf_evsel__alloc_fd(evsel, cpus->nr, nthreads) < 0)
  880. return -ENOMEM;
  881. if (evsel->cgrp) {
  882. flags |= PERF_FLAG_PID_CGROUP;
  883. pid = evsel->cgrp->fd;
  884. }
  885. fallback_missing_features:
  886. if (perf_missing_features.cloexec)
  887. flags &= ~(unsigned long)PERF_FLAG_FD_CLOEXEC;
  888. if (perf_missing_features.mmap2)
  889. evsel->attr.mmap2 = 0;
  890. if (perf_missing_features.exclude_guest)
  891. evsel->attr.exclude_guest = evsel->attr.exclude_host = 0;
  892. retry_sample_id:
  893. if (perf_missing_features.sample_id_all)
  894. evsel->attr.sample_id_all = 0;
  895. if (verbose >= 2)
  896. perf_event_attr__fprintf(&evsel->attr, stderr);
  897. for (cpu = 0; cpu < cpus->nr; cpu++) {
  898. for (thread = 0; thread < nthreads; thread++) {
  899. int group_fd;
  900. if (!evsel->cgrp && !evsel->system_wide)
  901. pid = threads->map[thread];
  902. group_fd = get_group_fd(evsel, cpu, thread);
  903. retry_open:
  904. pr_debug2("sys_perf_event_open: pid %d cpu %d group_fd %d flags %#lx\n",
  905. pid, cpus->map[cpu], group_fd, flags);
  906. FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
  907. pid,
  908. cpus->map[cpu],
  909. group_fd, flags);
  910. if (FD(evsel, cpu, thread) < 0) {
  911. err = -errno;
  912. pr_debug2("sys_perf_event_open failed, error %d\n",
  913. err);
  914. goto try_fallback;
  915. }
  916. set_rlimit = NO_CHANGE;
  917. }
  918. }
  919. return 0;
  920. try_fallback:
  921. /*
  922. * perf stat needs between 5 and 22 fds per CPU. When we run out
  923. * of them try to increase the limits.
  924. */
  925. if (err == -EMFILE && set_rlimit < INCREASED_MAX) {
  926. struct rlimit l;
  927. int old_errno = errno;
  928. if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
  929. if (set_rlimit == NO_CHANGE)
  930. l.rlim_cur = l.rlim_max;
  931. else {
  932. l.rlim_cur = l.rlim_max + 1000;
  933. l.rlim_max = l.rlim_cur;
  934. }
  935. if (setrlimit(RLIMIT_NOFILE, &l) == 0) {
  936. set_rlimit++;
  937. errno = old_errno;
  938. goto retry_open;
  939. }
  940. }
  941. errno = old_errno;
  942. }
  943. if (err != -EINVAL || cpu > 0 || thread > 0)
  944. goto out_close;
  945. if (!perf_missing_features.cloexec && (flags & PERF_FLAG_FD_CLOEXEC)) {
  946. perf_missing_features.cloexec = true;
  947. goto fallback_missing_features;
  948. } else if (!perf_missing_features.mmap2 && evsel->attr.mmap2) {
  949. perf_missing_features.mmap2 = true;
  950. goto fallback_missing_features;
  951. } else if (!perf_missing_features.exclude_guest &&
  952. (evsel->attr.exclude_guest || evsel->attr.exclude_host)) {
  953. perf_missing_features.exclude_guest = true;
  954. goto fallback_missing_features;
  955. } else if (!perf_missing_features.sample_id_all) {
  956. perf_missing_features.sample_id_all = true;
  957. goto retry_sample_id;
  958. }
  959. out_close:
  960. do {
  961. while (--thread >= 0) {
  962. close(FD(evsel, cpu, thread));
  963. FD(evsel, cpu, thread) = -1;
  964. }
  965. thread = nthreads;
  966. } while (--cpu >= 0);
  967. return err;
  968. }
  969. void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
  970. {
  971. if (evsel->fd == NULL)
  972. return;
  973. perf_evsel__close_fd(evsel, ncpus, nthreads);
  974. perf_evsel__free_fd(evsel);
  975. }
  976. static struct {
  977. struct cpu_map map;
  978. int cpus[1];
  979. } empty_cpu_map = {
  980. .map.nr = 1,
  981. .cpus = { -1, },
  982. };
  983. static struct {
  984. struct thread_map map;
  985. int threads[1];
  986. } empty_thread_map = {
  987. .map.nr = 1,
  988. .threads = { -1, },
  989. };
  990. int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
  991. struct thread_map *threads)
  992. {
  993. if (cpus == NULL) {
  994. /* Work around old compiler warnings about strict aliasing */
  995. cpus = &empty_cpu_map.map;
  996. }
  997. if (threads == NULL)
  998. threads = &empty_thread_map.map;
  999. return __perf_evsel__open(evsel, cpus, threads);
  1000. }
  1001. int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
  1002. struct cpu_map *cpus)
  1003. {
  1004. return __perf_evsel__open(evsel, cpus, &empty_thread_map.map);
  1005. }
  1006. int perf_evsel__open_per_thread(struct perf_evsel *evsel,
  1007. struct thread_map *threads)
  1008. {
  1009. return __perf_evsel__open(evsel, &empty_cpu_map.map, threads);
  1010. }
  1011. static int perf_evsel__parse_id_sample(const struct perf_evsel *evsel,
  1012. const union perf_event *event,
  1013. struct perf_sample *sample)
  1014. {
  1015. u64 type = evsel->attr.sample_type;
  1016. const u64 *array = event->sample.array;
  1017. bool swapped = evsel->needs_swap;
  1018. union u64_swap u;
  1019. array += ((event->header.size -
  1020. sizeof(event->header)) / sizeof(u64)) - 1;
  1021. if (type & PERF_SAMPLE_IDENTIFIER) {
  1022. sample->id = *array;
  1023. array--;
  1024. }
  1025. if (type & PERF_SAMPLE_CPU) {
  1026. u.val64 = *array;
  1027. if (swapped) {
  1028. /* undo swap of u64, then swap on individual u32s */
  1029. u.val64 = bswap_64(u.val64);
  1030. u.val32[0] = bswap_32(u.val32[0]);
  1031. }
  1032. sample->cpu = u.val32[0];
  1033. array--;
  1034. }
  1035. if (type & PERF_SAMPLE_STREAM_ID) {
  1036. sample->stream_id = *array;
  1037. array--;
  1038. }
  1039. if (type & PERF_SAMPLE_ID) {
  1040. sample->id = *array;
  1041. array--;
  1042. }
  1043. if (type & PERF_SAMPLE_TIME) {
  1044. sample->time = *array;
  1045. array--;
  1046. }
  1047. if (type & PERF_SAMPLE_TID) {
  1048. u.val64 = *array;
  1049. if (swapped) {
  1050. /* undo swap of u64, then swap on individual u32s */
  1051. u.val64 = bswap_64(u.val64);
  1052. u.val32[0] = bswap_32(u.val32[0]);
  1053. u.val32[1] = bswap_32(u.val32[1]);
  1054. }
  1055. sample->pid = u.val32[0];
  1056. sample->tid = u.val32[1];
  1057. array--;
  1058. }
  1059. return 0;
  1060. }
  1061. static inline bool overflow(const void *endp, u16 max_size, const void *offset,
  1062. u64 size)
  1063. {
  1064. return size > max_size || offset + size > endp;
  1065. }
  1066. #define OVERFLOW_CHECK(offset, size, max_size) \
  1067. do { \
  1068. if (overflow(endp, (max_size), (offset), (size))) \
  1069. return -EFAULT; \
  1070. } while (0)
  1071. #define OVERFLOW_CHECK_u64(offset) \
  1072. OVERFLOW_CHECK(offset, sizeof(u64), sizeof(u64))
  1073. int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
  1074. struct perf_sample *data)
  1075. {
  1076. u64 type = evsel->attr.sample_type;
  1077. bool swapped = evsel->needs_swap;
  1078. const u64 *array;
  1079. u16 max_size = event->header.size;
  1080. const void *endp = (void *)event + max_size;
  1081. u64 sz;
  1082. /*
  1083. * used for cross-endian analysis. See git commit 65014ab3
  1084. * for why this goofiness is needed.
  1085. */
  1086. union u64_swap u;
  1087. memset(data, 0, sizeof(*data));
  1088. data->cpu = data->pid = data->tid = -1;
  1089. data->stream_id = data->id = data->time = -1ULL;
  1090. data->period = evsel->attr.sample_period;
  1091. data->weight = 0;
  1092. if (event->header.type != PERF_RECORD_SAMPLE) {
  1093. if (!evsel->attr.sample_id_all)
  1094. return 0;
  1095. return perf_evsel__parse_id_sample(evsel, event, data);
  1096. }
  1097. array = event->sample.array;
  1098. /*
  1099. * The evsel's sample_size is based on PERF_SAMPLE_MASK which includes
  1100. * up to PERF_SAMPLE_PERIOD. After that overflow() must be used to
  1101. * check the format does not go past the end of the event.
  1102. */
  1103. if (evsel->sample_size + sizeof(event->header) > event->header.size)
  1104. return -EFAULT;
  1105. data->id = -1ULL;
  1106. if (type & PERF_SAMPLE_IDENTIFIER) {
  1107. data->id = *array;
  1108. array++;
  1109. }
  1110. if (type & PERF_SAMPLE_IP) {
  1111. data->ip = *array;
  1112. array++;
  1113. }
  1114. if (type & PERF_SAMPLE_TID) {
  1115. u.val64 = *array;
  1116. if (swapped) {
  1117. /* undo swap of u64, then swap on individual u32s */
  1118. u.val64 = bswap_64(u.val64);
  1119. u.val32[0] = bswap_32(u.val32[0]);
  1120. u.val32[1] = bswap_32(u.val32[1]);
  1121. }
  1122. data->pid = u.val32[0];
  1123. data->tid = u.val32[1];
  1124. array++;
  1125. }
  1126. if (type & PERF_SAMPLE_TIME) {
  1127. data->time = *array;
  1128. array++;
  1129. }
  1130. data->addr = 0;
  1131. if (type & PERF_SAMPLE_ADDR) {
  1132. data->addr = *array;
  1133. array++;
  1134. }
  1135. if (type & PERF_SAMPLE_ID) {
  1136. data->id = *array;
  1137. array++;
  1138. }
  1139. if (type & PERF_SAMPLE_STREAM_ID) {
  1140. data->stream_id = *array;
  1141. array++;
  1142. }
  1143. if (type & PERF_SAMPLE_CPU) {
  1144. u.val64 = *array;
  1145. if (swapped) {
  1146. /* undo swap of u64, then swap on individual u32s */
  1147. u.val64 = bswap_64(u.val64);
  1148. u.val32[0] = bswap_32(u.val32[0]);
  1149. }
  1150. data->cpu = u.val32[0];
  1151. array++;
  1152. }
  1153. if (type & PERF_SAMPLE_PERIOD) {
  1154. data->period = *array;
  1155. array++;
  1156. }
  1157. if (type & PERF_SAMPLE_READ) {
  1158. u64 read_format = evsel->attr.read_format;
  1159. OVERFLOW_CHECK_u64(array);
  1160. if (read_format & PERF_FORMAT_GROUP)
  1161. data->read.group.nr = *array;
  1162. else
  1163. data->read.one.value = *array;
  1164. array++;
  1165. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
  1166. OVERFLOW_CHECK_u64(array);
  1167. data->read.time_enabled = *array;
  1168. array++;
  1169. }
  1170. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
  1171. OVERFLOW_CHECK_u64(array);
  1172. data->read.time_running = *array;
  1173. array++;
  1174. }
  1175. /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
  1176. if (read_format & PERF_FORMAT_GROUP) {
  1177. const u64 max_group_nr = UINT64_MAX /
  1178. sizeof(struct sample_read_value);
  1179. if (data->read.group.nr > max_group_nr)
  1180. return -EFAULT;
  1181. sz = data->read.group.nr *
  1182. sizeof(struct sample_read_value);
  1183. OVERFLOW_CHECK(array, sz, max_size);
  1184. data->read.group.values =
  1185. (struct sample_read_value *)array;
  1186. array = (void *)array + sz;
  1187. } else {
  1188. OVERFLOW_CHECK_u64(array);
  1189. data->read.one.id = *array;
  1190. array++;
  1191. }
  1192. }
  1193. if (type & PERF_SAMPLE_CALLCHAIN) {
  1194. const u64 max_callchain_nr = UINT64_MAX / sizeof(u64);
  1195. OVERFLOW_CHECK_u64(array);
  1196. data->callchain = (struct ip_callchain *)array++;
  1197. if (data->callchain->nr > max_callchain_nr)
  1198. return -EFAULT;
  1199. sz = data->callchain->nr * sizeof(u64);
  1200. OVERFLOW_CHECK(array, sz, max_size);
  1201. array = (void *)array + sz;
  1202. }
  1203. if (type & PERF_SAMPLE_RAW) {
  1204. OVERFLOW_CHECK_u64(array);
  1205. u.val64 = *array;
  1206. if (WARN_ONCE(swapped,
  1207. "Endianness of raw data not corrected!\n")) {
  1208. /* undo swap of u64, then swap on individual u32s */
  1209. u.val64 = bswap_64(u.val64);
  1210. u.val32[0] = bswap_32(u.val32[0]);
  1211. u.val32[1] = bswap_32(u.val32[1]);
  1212. }
  1213. data->raw_size = u.val32[0];
  1214. array = (void *)array + sizeof(u32);
  1215. OVERFLOW_CHECK(array, data->raw_size, max_size);
  1216. data->raw_data = (void *)array;
  1217. array = (void *)array + data->raw_size;
  1218. }
  1219. if (type & PERF_SAMPLE_BRANCH_STACK) {
  1220. const u64 max_branch_nr = UINT64_MAX /
  1221. sizeof(struct branch_entry);
  1222. OVERFLOW_CHECK_u64(array);
  1223. data->branch_stack = (struct branch_stack *)array++;
  1224. if (data->branch_stack->nr > max_branch_nr)
  1225. return -EFAULT;
  1226. sz = data->branch_stack->nr * sizeof(struct branch_entry);
  1227. OVERFLOW_CHECK(array, sz, max_size);
  1228. array = (void *)array + sz;
  1229. }
  1230. if (type & PERF_SAMPLE_REGS_USER) {
  1231. OVERFLOW_CHECK_u64(array);
  1232. data->user_regs.abi = *array;
  1233. array++;
  1234. if (data->user_regs.abi) {
  1235. u64 mask = evsel->attr.sample_regs_user;
  1236. sz = hweight_long(mask) * sizeof(u64);
  1237. OVERFLOW_CHECK(array, sz, max_size);
  1238. data->user_regs.mask = mask;
  1239. data->user_regs.regs = (u64 *)array;
  1240. array = (void *)array + sz;
  1241. }
  1242. }
  1243. if (type & PERF_SAMPLE_STACK_USER) {
  1244. OVERFLOW_CHECK_u64(array);
  1245. sz = *array++;
  1246. data->user_stack.offset = ((char *)(array - 1)
  1247. - (char *) event);
  1248. if (!sz) {
  1249. data->user_stack.size = 0;
  1250. } else {
  1251. OVERFLOW_CHECK(array, sz, max_size);
  1252. data->user_stack.data = (char *)array;
  1253. array = (void *)array + sz;
  1254. OVERFLOW_CHECK_u64(array);
  1255. data->user_stack.size = *array++;
  1256. if (WARN_ONCE(data->user_stack.size > sz,
  1257. "user stack dump failure\n"))
  1258. return -EFAULT;
  1259. }
  1260. }
  1261. data->weight = 0;
  1262. if (type & PERF_SAMPLE_WEIGHT) {
  1263. OVERFLOW_CHECK_u64(array);
  1264. data->weight = *array;
  1265. array++;
  1266. }
  1267. data->data_src = PERF_MEM_DATA_SRC_NONE;
  1268. if (type & PERF_SAMPLE_DATA_SRC) {
  1269. OVERFLOW_CHECK_u64(array);
  1270. data->data_src = *array;
  1271. array++;
  1272. }
  1273. data->transaction = 0;
  1274. if (type & PERF_SAMPLE_TRANSACTION) {
  1275. OVERFLOW_CHECK_u64(array);
  1276. data->transaction = *array;
  1277. array++;
  1278. }
  1279. data->intr_regs.abi = PERF_SAMPLE_REGS_ABI_NONE;
  1280. if (type & PERF_SAMPLE_REGS_INTR) {
  1281. OVERFLOW_CHECK_u64(array);
  1282. data->intr_regs.abi = *array;
  1283. array++;
  1284. if (data->intr_regs.abi != PERF_SAMPLE_REGS_ABI_NONE) {
  1285. u64 mask = evsel->attr.sample_regs_intr;
  1286. sz = hweight_long(mask) * sizeof(u64);
  1287. OVERFLOW_CHECK(array, sz, max_size);
  1288. data->intr_regs.mask = mask;
  1289. data->intr_regs.regs = (u64 *)array;
  1290. array = (void *)array + sz;
  1291. }
  1292. }
  1293. return 0;
  1294. }
  1295. size_t perf_event__sample_event_size(const struct perf_sample *sample, u64 type,
  1296. u64 read_format)
  1297. {
  1298. size_t sz, result = sizeof(struct sample_event);
  1299. if (type & PERF_SAMPLE_IDENTIFIER)
  1300. result += sizeof(u64);
  1301. if (type & PERF_SAMPLE_IP)
  1302. result += sizeof(u64);
  1303. if (type & PERF_SAMPLE_TID)
  1304. result += sizeof(u64);
  1305. if (type & PERF_SAMPLE_TIME)
  1306. result += sizeof(u64);
  1307. if (type & PERF_SAMPLE_ADDR)
  1308. result += sizeof(u64);
  1309. if (type & PERF_SAMPLE_ID)
  1310. result += sizeof(u64);
  1311. if (type & PERF_SAMPLE_STREAM_ID)
  1312. result += sizeof(u64);
  1313. if (type & PERF_SAMPLE_CPU)
  1314. result += sizeof(u64);
  1315. if (type & PERF_SAMPLE_PERIOD)
  1316. result += sizeof(u64);
  1317. if (type & PERF_SAMPLE_READ) {
  1318. result += sizeof(u64);
  1319. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  1320. result += sizeof(u64);
  1321. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  1322. result += sizeof(u64);
  1323. /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
  1324. if (read_format & PERF_FORMAT_GROUP) {
  1325. sz = sample->read.group.nr *
  1326. sizeof(struct sample_read_value);
  1327. result += sz;
  1328. } else {
  1329. result += sizeof(u64);
  1330. }
  1331. }
  1332. if (type & PERF_SAMPLE_CALLCHAIN) {
  1333. sz = (sample->callchain->nr + 1) * sizeof(u64);
  1334. result += sz;
  1335. }
  1336. if (type & PERF_SAMPLE_RAW) {
  1337. result += sizeof(u32);
  1338. result += sample->raw_size;
  1339. }
  1340. if (type & PERF_SAMPLE_BRANCH_STACK) {
  1341. sz = sample->branch_stack->nr * sizeof(struct branch_entry);
  1342. sz += sizeof(u64);
  1343. result += sz;
  1344. }
  1345. if (type & PERF_SAMPLE_REGS_USER) {
  1346. if (sample->user_regs.abi) {
  1347. result += sizeof(u64);
  1348. sz = hweight_long(sample->user_regs.mask) * sizeof(u64);
  1349. result += sz;
  1350. } else {
  1351. result += sizeof(u64);
  1352. }
  1353. }
  1354. if (type & PERF_SAMPLE_STACK_USER) {
  1355. sz = sample->user_stack.size;
  1356. result += sizeof(u64);
  1357. if (sz) {
  1358. result += sz;
  1359. result += sizeof(u64);
  1360. }
  1361. }
  1362. if (type & PERF_SAMPLE_WEIGHT)
  1363. result += sizeof(u64);
  1364. if (type & PERF_SAMPLE_DATA_SRC)
  1365. result += sizeof(u64);
  1366. if (type & PERF_SAMPLE_TRANSACTION)
  1367. result += sizeof(u64);
  1368. if (type & PERF_SAMPLE_REGS_INTR) {
  1369. if (sample->intr_regs.abi) {
  1370. result += sizeof(u64);
  1371. sz = hweight_long(sample->intr_regs.mask) * sizeof(u64);
  1372. result += sz;
  1373. } else {
  1374. result += sizeof(u64);
  1375. }
  1376. }
  1377. return result;
  1378. }
  1379. int perf_event__synthesize_sample(union perf_event *event, u64 type,
  1380. u64 read_format,
  1381. const struct perf_sample *sample,
  1382. bool swapped)
  1383. {
  1384. u64 *array;
  1385. size_t sz;
  1386. /*
  1387. * used for cross-endian analysis. See git commit 65014ab3
  1388. * for why this goofiness is needed.
  1389. */
  1390. union u64_swap u;
  1391. array = event->sample.array;
  1392. if (type & PERF_SAMPLE_IDENTIFIER) {
  1393. *array = sample->id;
  1394. array++;
  1395. }
  1396. if (type & PERF_SAMPLE_IP) {
  1397. *array = sample->ip;
  1398. array++;
  1399. }
  1400. if (type & PERF_SAMPLE_TID) {
  1401. u.val32[0] = sample->pid;
  1402. u.val32[1] = sample->tid;
  1403. if (swapped) {
  1404. /*
  1405. * Inverse of what is done in perf_evsel__parse_sample
  1406. */
  1407. u.val32[0] = bswap_32(u.val32[0]);
  1408. u.val32[1] = bswap_32(u.val32[1]);
  1409. u.val64 = bswap_64(u.val64);
  1410. }
  1411. *array = u.val64;
  1412. array++;
  1413. }
  1414. if (type & PERF_SAMPLE_TIME) {
  1415. *array = sample->time;
  1416. array++;
  1417. }
  1418. if (type & PERF_SAMPLE_ADDR) {
  1419. *array = sample->addr;
  1420. array++;
  1421. }
  1422. if (type & PERF_SAMPLE_ID) {
  1423. *array = sample->id;
  1424. array++;
  1425. }
  1426. if (type & PERF_SAMPLE_STREAM_ID) {
  1427. *array = sample->stream_id;
  1428. array++;
  1429. }
  1430. if (type & PERF_SAMPLE_CPU) {
  1431. u.val32[0] = sample->cpu;
  1432. if (swapped) {
  1433. /*
  1434. * Inverse of what is done in perf_evsel__parse_sample
  1435. */
  1436. u.val32[0] = bswap_32(u.val32[0]);
  1437. u.val64 = bswap_64(u.val64);
  1438. }
  1439. *array = u.val64;
  1440. array++;
  1441. }
  1442. if (type & PERF_SAMPLE_PERIOD) {
  1443. *array = sample->period;
  1444. array++;
  1445. }
  1446. if (type & PERF_SAMPLE_READ) {
  1447. if (read_format & PERF_FORMAT_GROUP)
  1448. *array = sample->read.group.nr;
  1449. else
  1450. *array = sample->read.one.value;
  1451. array++;
  1452. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
  1453. *array = sample->read.time_enabled;
  1454. array++;
  1455. }
  1456. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
  1457. *array = sample->read.time_running;
  1458. array++;
  1459. }
  1460. /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
  1461. if (read_format & PERF_FORMAT_GROUP) {
  1462. sz = sample->read.group.nr *
  1463. sizeof(struct sample_read_value);
  1464. memcpy(array, sample->read.group.values, sz);
  1465. array = (void *)array + sz;
  1466. } else {
  1467. *array = sample->read.one.id;
  1468. array++;
  1469. }
  1470. }
  1471. if (type & PERF_SAMPLE_CALLCHAIN) {
  1472. sz = (sample->callchain->nr + 1) * sizeof(u64);
  1473. memcpy(array, sample->callchain, sz);
  1474. array = (void *)array + sz;
  1475. }
  1476. if (type & PERF_SAMPLE_RAW) {
  1477. u.val32[0] = sample->raw_size;
  1478. if (WARN_ONCE(swapped,
  1479. "Endianness of raw data not corrected!\n")) {
  1480. /*
  1481. * Inverse of what is done in perf_evsel__parse_sample
  1482. */
  1483. u.val32[0] = bswap_32(u.val32[0]);
  1484. u.val32[1] = bswap_32(u.val32[1]);
  1485. u.val64 = bswap_64(u.val64);
  1486. }
  1487. *array = u.val64;
  1488. array = (void *)array + sizeof(u32);
  1489. memcpy(array, sample->raw_data, sample->raw_size);
  1490. array = (void *)array + sample->raw_size;
  1491. }
  1492. if (type & PERF_SAMPLE_BRANCH_STACK) {
  1493. sz = sample->branch_stack->nr * sizeof(struct branch_entry);
  1494. sz += sizeof(u64);
  1495. memcpy(array, sample->branch_stack, sz);
  1496. array = (void *)array + sz;
  1497. }
  1498. if (type & PERF_SAMPLE_REGS_USER) {
  1499. if (sample->user_regs.abi) {
  1500. *array++ = sample->user_regs.abi;
  1501. sz = hweight_long(sample->user_regs.mask) * sizeof(u64);
  1502. memcpy(array, sample->user_regs.regs, sz);
  1503. array = (void *)array + sz;
  1504. } else {
  1505. *array++ = 0;
  1506. }
  1507. }
  1508. if (type & PERF_SAMPLE_STACK_USER) {
  1509. sz = sample->user_stack.size;
  1510. *array++ = sz;
  1511. if (sz) {
  1512. memcpy(array, sample->user_stack.data, sz);
  1513. array = (void *)array + sz;
  1514. *array++ = sz;
  1515. }
  1516. }
  1517. if (type & PERF_SAMPLE_WEIGHT) {
  1518. *array = sample->weight;
  1519. array++;
  1520. }
  1521. if (type & PERF_SAMPLE_DATA_SRC) {
  1522. *array = sample->data_src;
  1523. array++;
  1524. }
  1525. if (type & PERF_SAMPLE_TRANSACTION) {
  1526. *array = sample->transaction;
  1527. array++;
  1528. }
  1529. if (type & PERF_SAMPLE_REGS_INTR) {
  1530. if (sample->intr_regs.abi) {
  1531. *array++ = sample->intr_regs.abi;
  1532. sz = hweight_long(sample->intr_regs.mask) * sizeof(u64);
  1533. memcpy(array, sample->intr_regs.regs, sz);
  1534. array = (void *)array + sz;
  1535. } else {
  1536. *array++ = 0;
  1537. }
  1538. }
  1539. return 0;
  1540. }
  1541. struct format_field *perf_evsel__field(struct perf_evsel *evsel, const char *name)
  1542. {
  1543. return pevent_find_field(evsel->tp_format, name);
  1544. }
  1545. void *perf_evsel__rawptr(struct perf_evsel *evsel, struct perf_sample *sample,
  1546. const char *name)
  1547. {
  1548. struct format_field *field = perf_evsel__field(evsel, name);
  1549. int offset;
  1550. if (!field)
  1551. return NULL;
  1552. offset = field->offset;
  1553. if (field->flags & FIELD_IS_DYNAMIC) {
  1554. offset = *(int *)(sample->raw_data + field->offset);
  1555. offset &= 0xffff;
  1556. }
  1557. return sample->raw_data + offset;
  1558. }
  1559. u64 perf_evsel__intval(struct perf_evsel *evsel, struct perf_sample *sample,
  1560. const char *name)
  1561. {
  1562. struct format_field *field = perf_evsel__field(evsel, name);
  1563. void *ptr;
  1564. u64 value;
  1565. if (!field)
  1566. return 0;
  1567. ptr = sample->raw_data + field->offset;
  1568. switch (field->size) {
  1569. case 1:
  1570. return *(u8 *)ptr;
  1571. case 2:
  1572. value = *(u16 *)ptr;
  1573. break;
  1574. case 4:
  1575. value = *(u32 *)ptr;
  1576. break;
  1577. case 8:
  1578. value = *(u64 *)ptr;
  1579. break;
  1580. default:
  1581. return 0;
  1582. }
  1583. if (!evsel->needs_swap)
  1584. return value;
  1585. switch (field->size) {
  1586. case 2:
  1587. return bswap_16(value);
  1588. case 4:
  1589. return bswap_32(value);
  1590. case 8:
  1591. return bswap_64(value);
  1592. default:
  1593. return 0;
  1594. }
  1595. return 0;
  1596. }
  1597. static int comma_fprintf(FILE *fp, bool *first, const char *fmt, ...)
  1598. {
  1599. va_list args;
  1600. int ret = 0;
  1601. if (!*first) {
  1602. ret += fprintf(fp, ",");
  1603. } else {
  1604. ret += fprintf(fp, ":");
  1605. *first = false;
  1606. }
  1607. va_start(args, fmt);
  1608. ret += vfprintf(fp, fmt, args);
  1609. va_end(args);
  1610. return ret;
  1611. }
  1612. static int __if_fprintf(FILE *fp, bool *first, const char *field, u64 value)
  1613. {
  1614. if (value == 0)
  1615. return 0;
  1616. return comma_fprintf(fp, first, " %s: %" PRIu64, field, value);
  1617. }
  1618. #define if_print(field) printed += __if_fprintf(fp, &first, #field, evsel->attr.field)
  1619. struct bit_names {
  1620. int bit;
  1621. const char *name;
  1622. };
  1623. static int bits__fprintf(FILE *fp, const char *field, u64 value,
  1624. struct bit_names *bits, bool *first)
  1625. {
  1626. int i = 0, printed = comma_fprintf(fp, first, " %s: ", field);
  1627. bool first_bit = true;
  1628. do {
  1629. if (value & bits[i].bit) {
  1630. printed += fprintf(fp, "%s%s", first_bit ? "" : "|", bits[i].name);
  1631. first_bit = false;
  1632. }
  1633. } while (bits[++i].name != NULL);
  1634. return printed;
  1635. }
  1636. static int sample_type__fprintf(FILE *fp, bool *first, u64 value)
  1637. {
  1638. #define bit_name(n) { PERF_SAMPLE_##n, #n }
  1639. struct bit_names bits[] = {
  1640. bit_name(IP), bit_name(TID), bit_name(TIME), bit_name(ADDR),
  1641. bit_name(READ), bit_name(CALLCHAIN), bit_name(ID), bit_name(CPU),
  1642. bit_name(PERIOD), bit_name(STREAM_ID), bit_name(RAW),
  1643. bit_name(BRANCH_STACK), bit_name(REGS_USER), bit_name(STACK_USER),
  1644. bit_name(IDENTIFIER), bit_name(REGS_INTR),
  1645. { .name = NULL, }
  1646. };
  1647. #undef bit_name
  1648. return bits__fprintf(fp, "sample_type", value, bits, first);
  1649. }
  1650. static int read_format__fprintf(FILE *fp, bool *first, u64 value)
  1651. {
  1652. #define bit_name(n) { PERF_FORMAT_##n, #n }
  1653. struct bit_names bits[] = {
  1654. bit_name(TOTAL_TIME_ENABLED), bit_name(TOTAL_TIME_RUNNING),
  1655. bit_name(ID), bit_name(GROUP),
  1656. { .name = NULL, }
  1657. };
  1658. #undef bit_name
  1659. return bits__fprintf(fp, "read_format", value, bits, first);
  1660. }
  1661. int perf_evsel__fprintf(struct perf_evsel *evsel,
  1662. struct perf_attr_details *details, FILE *fp)
  1663. {
  1664. bool first = true;
  1665. int printed = 0;
  1666. if (details->event_group) {
  1667. struct perf_evsel *pos;
  1668. if (!perf_evsel__is_group_leader(evsel))
  1669. return 0;
  1670. if (evsel->nr_members > 1)
  1671. printed += fprintf(fp, "%s{", evsel->group_name ?: "");
  1672. printed += fprintf(fp, "%s", perf_evsel__name(evsel));
  1673. for_each_group_member(pos, evsel)
  1674. printed += fprintf(fp, ",%s", perf_evsel__name(pos));
  1675. if (evsel->nr_members > 1)
  1676. printed += fprintf(fp, "}");
  1677. goto out;
  1678. }
  1679. printed += fprintf(fp, "%s", perf_evsel__name(evsel));
  1680. if (details->verbose || details->freq) {
  1681. printed += comma_fprintf(fp, &first, " sample_freq=%" PRIu64,
  1682. (u64)evsel->attr.sample_freq);
  1683. }
  1684. if (details->verbose) {
  1685. if_print(type);
  1686. if_print(config);
  1687. if_print(config1);
  1688. if_print(config2);
  1689. if_print(size);
  1690. printed += sample_type__fprintf(fp, &first, evsel->attr.sample_type);
  1691. if (evsel->attr.read_format)
  1692. printed += read_format__fprintf(fp, &first, evsel->attr.read_format);
  1693. if_print(disabled);
  1694. if_print(inherit);
  1695. if_print(pinned);
  1696. if_print(exclusive);
  1697. if_print(exclude_user);
  1698. if_print(exclude_kernel);
  1699. if_print(exclude_hv);
  1700. if_print(exclude_idle);
  1701. if_print(mmap);
  1702. if_print(mmap2);
  1703. if_print(comm);
  1704. if_print(comm_exec);
  1705. if_print(freq);
  1706. if_print(inherit_stat);
  1707. if_print(enable_on_exec);
  1708. if_print(task);
  1709. if_print(watermark);
  1710. if_print(precise_ip);
  1711. if_print(mmap_data);
  1712. if_print(sample_id_all);
  1713. if_print(exclude_host);
  1714. if_print(exclude_guest);
  1715. if_print(__reserved_1);
  1716. if_print(wakeup_events);
  1717. if_print(bp_type);
  1718. if_print(branch_sample_type);
  1719. }
  1720. out:
  1721. fputc('\n', fp);
  1722. return ++printed;
  1723. }
  1724. bool perf_evsel__fallback(struct perf_evsel *evsel, int err,
  1725. char *msg, size_t msgsize)
  1726. {
  1727. if ((err == ENOENT || err == ENXIO || err == ENODEV) &&
  1728. evsel->attr.type == PERF_TYPE_HARDWARE &&
  1729. evsel->attr.config == PERF_COUNT_HW_CPU_CYCLES) {
  1730. /*
  1731. * If it's cycles then fall back to hrtimer based
  1732. * cpu-clock-tick sw counter, which is always available even if
  1733. * no PMU support.
  1734. *
  1735. * PPC returns ENXIO until 2.6.37 (behavior changed with commit
  1736. * b0a873e).
  1737. */
  1738. scnprintf(msg, msgsize, "%s",
  1739. "The cycles event is not supported, trying to fall back to cpu-clock-ticks");
  1740. evsel->attr.type = PERF_TYPE_SOFTWARE;
  1741. evsel->attr.config = PERF_COUNT_SW_CPU_CLOCK;
  1742. zfree(&evsel->name);
  1743. return true;
  1744. }
  1745. return false;
  1746. }
  1747. int perf_evsel__open_strerror(struct perf_evsel *evsel, struct target *target,
  1748. int err, char *msg, size_t size)
  1749. {
  1750. char sbuf[STRERR_BUFSIZE];
  1751. switch (err) {
  1752. case EPERM:
  1753. case EACCES:
  1754. return scnprintf(msg, size,
  1755. "You may not have permission to collect %sstats.\n"
  1756. "Consider tweaking /proc/sys/kernel/perf_event_paranoid:\n"
  1757. " -1 - Not paranoid at all\n"
  1758. " 0 - Disallow raw tracepoint access for unpriv\n"
  1759. " 1 - Disallow cpu events for unpriv\n"
  1760. " 2 - Disallow kernel profiling for unpriv",
  1761. target->system_wide ? "system-wide " : "");
  1762. case ENOENT:
  1763. return scnprintf(msg, size, "The %s event is not supported.",
  1764. perf_evsel__name(evsel));
  1765. case EMFILE:
  1766. return scnprintf(msg, size, "%s",
  1767. "Too many events are opened.\n"
  1768. "Try again after reducing the number of events.");
  1769. case ENODEV:
  1770. if (target->cpu_list)
  1771. return scnprintf(msg, size, "%s",
  1772. "No such device - did you specify an out-of-range profile CPU?\n");
  1773. break;
  1774. case EOPNOTSUPP:
  1775. if (evsel->attr.precise_ip)
  1776. return scnprintf(msg, size, "%s",
  1777. "\'precise\' request may not be supported. Try removing 'p' modifier.");
  1778. #if defined(__i386__) || defined(__x86_64__)
  1779. if (evsel->attr.type == PERF_TYPE_HARDWARE)
  1780. return scnprintf(msg, size, "%s",
  1781. "No hardware sampling interrupt available.\n"
  1782. "No APIC? If so then you can boot the kernel with the \"lapic\" boot parameter to force-enable it.");
  1783. #endif
  1784. break;
  1785. case EBUSY:
  1786. if (find_process("oprofiled"))
  1787. return scnprintf(msg, size,
  1788. "The PMU counters are busy/taken by another profiler.\n"
  1789. "We found oprofile daemon running, please stop it and try again.");
  1790. break;
  1791. default:
  1792. break;
  1793. }
  1794. return scnprintf(msg, size,
  1795. "The sys_perf_event_open() syscall returned with %d (%s) for event (%s).\n"
  1796. "/bin/dmesg may provide additional information.\n"
  1797. "No CONFIG_PERF_EVENTS=y kernel support configured?\n",
  1798. err, strerror_r(err, sbuf, sizeof(sbuf)),
  1799. perf_evsel__name(evsel));
  1800. }