evsel.c 57 KB

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