evsel.c 47 KB

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