evsel.c 62 KB

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