evsel.c 48 KB

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