evsel.c 52 KB

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