evsel.c 48 KB

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