evsel.c 59 KB

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