evsel.c 58 KB

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