evsel.c 50 KB

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