evsel.c 48 KB

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