evsel.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015
  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. /*
  413. * The enable_on_exec/disabled value strategy:
  414. *
  415. * 1) For any type of traced program:
  416. * - all independent events and group leaders are disabled
  417. * - all group members are enabled
  418. *
  419. * Group members are ruled by group leaders. They need to
  420. * be enabled, because the group scheduling relies on that.
  421. *
  422. * 2) For traced programs executed by perf:
  423. * - all independent events and group leaders have
  424. * enable_on_exec set
  425. * - we don't specifically enable or disable any event during
  426. * the record command
  427. *
  428. * Independent events and group leaders are initially disabled
  429. * and get enabled by exec. Group members are ruled by group
  430. * leaders as stated in 1).
  431. *
  432. * 3) For traced programs attached by perf (pid/tid):
  433. * - we specifically enable or disable all events during
  434. * the record command
  435. *
  436. * When attaching events to already running traced we
  437. * enable/disable events specifically, as there's no
  438. * initial traced exec call.
  439. */
  440. void perf_evsel__config(struct perf_evsel *evsel, struct record_opts *opts)
  441. {
  442. struct perf_evsel *leader = evsel->leader;
  443. struct perf_event_attr *attr = &evsel->attr;
  444. int track = !evsel->idx; /* only the first counter needs these */
  445. bool per_cpu = opts->target.default_per_cpu && !opts->target.per_thread;
  446. attr->sample_id_all = perf_missing_features.sample_id_all ? 0 : 1;
  447. attr->inherit = !opts->no_inherit;
  448. perf_evsel__set_sample_bit(evsel, IP);
  449. perf_evsel__set_sample_bit(evsel, TID);
  450. if (evsel->sample_read) {
  451. perf_evsel__set_sample_bit(evsel, READ);
  452. /*
  453. * We need ID even in case of single event, because
  454. * PERF_SAMPLE_READ process ID specific data.
  455. */
  456. perf_evsel__set_sample_id(evsel, false);
  457. /*
  458. * Apply group format only if we belong to group
  459. * with more than one members.
  460. */
  461. if (leader->nr_members > 1) {
  462. attr->read_format |= PERF_FORMAT_GROUP;
  463. attr->inherit = 0;
  464. }
  465. }
  466. /*
  467. * We default some events to a 1 default interval. But keep
  468. * it a weak assumption overridable by the user.
  469. */
  470. if (!attr->sample_period || (opts->user_freq != UINT_MAX &&
  471. opts->user_interval != ULLONG_MAX)) {
  472. if (opts->freq) {
  473. perf_evsel__set_sample_bit(evsel, PERIOD);
  474. attr->freq = 1;
  475. attr->sample_freq = opts->freq;
  476. } else {
  477. attr->sample_period = opts->default_interval;
  478. }
  479. }
  480. /*
  481. * Disable sampling for all group members other
  482. * than leader in case leader 'leads' the sampling.
  483. */
  484. if ((leader != evsel) && leader->sample_read) {
  485. attr->sample_freq = 0;
  486. attr->sample_period = 0;
  487. }
  488. if (opts->no_samples)
  489. attr->sample_freq = 0;
  490. if (opts->inherit_stat)
  491. attr->inherit_stat = 1;
  492. if (opts->sample_address) {
  493. perf_evsel__set_sample_bit(evsel, ADDR);
  494. attr->mmap_data = track;
  495. }
  496. if (opts->call_graph) {
  497. perf_evsel__set_sample_bit(evsel, CALLCHAIN);
  498. if (opts->call_graph == CALLCHAIN_DWARF) {
  499. perf_evsel__set_sample_bit(evsel, REGS_USER);
  500. perf_evsel__set_sample_bit(evsel, STACK_USER);
  501. attr->sample_regs_user = PERF_REGS_MASK;
  502. attr->sample_stack_user = opts->stack_dump_size;
  503. attr->exclude_callchain_user = 1;
  504. }
  505. }
  506. if (target__has_cpu(&opts->target))
  507. perf_evsel__set_sample_bit(evsel, CPU);
  508. if (opts->period)
  509. perf_evsel__set_sample_bit(evsel, PERIOD);
  510. if (!perf_missing_features.sample_id_all &&
  511. (opts->sample_time || !opts->no_inherit ||
  512. target__has_cpu(&opts->target) || per_cpu))
  513. perf_evsel__set_sample_bit(evsel, TIME);
  514. if (opts->raw_samples) {
  515. perf_evsel__set_sample_bit(evsel, TIME);
  516. perf_evsel__set_sample_bit(evsel, RAW);
  517. perf_evsel__set_sample_bit(evsel, CPU);
  518. }
  519. if (opts->sample_address)
  520. perf_evsel__set_sample_bit(evsel, DATA_SRC);
  521. if (opts->no_buffering) {
  522. attr->watermark = 0;
  523. attr->wakeup_events = 1;
  524. }
  525. if (opts->branch_stack) {
  526. perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
  527. attr->branch_sample_type = opts->branch_stack;
  528. }
  529. if (opts->sample_weight)
  530. perf_evsel__set_sample_bit(evsel, WEIGHT);
  531. attr->mmap = track;
  532. attr->comm = track;
  533. if (opts->sample_transaction)
  534. perf_evsel__set_sample_bit(evsel, TRANSACTION);
  535. /*
  536. * XXX see the function comment above
  537. *
  538. * Disabling only independent events or group leaders,
  539. * keeping group members enabled.
  540. */
  541. if (perf_evsel__is_group_leader(evsel))
  542. attr->disabled = 1;
  543. /*
  544. * Setting enable_on_exec for independent events and
  545. * group leaders for traced executed by perf.
  546. */
  547. if (target__none(&opts->target) && perf_evsel__is_group_leader(evsel) &&
  548. !opts->initial_delay)
  549. attr->enable_on_exec = 1;
  550. }
  551. int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
  552. {
  553. int cpu, thread;
  554. evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
  555. if (evsel->fd) {
  556. for (cpu = 0; cpu < ncpus; cpu++) {
  557. for (thread = 0; thread < nthreads; thread++) {
  558. FD(evsel, cpu, thread) = -1;
  559. }
  560. }
  561. }
  562. return evsel->fd != NULL ? 0 : -ENOMEM;
  563. }
  564. static int perf_evsel__run_ioctl(struct perf_evsel *evsel, int ncpus, int nthreads,
  565. int ioc, void *arg)
  566. {
  567. int cpu, thread;
  568. for (cpu = 0; cpu < ncpus; cpu++) {
  569. for (thread = 0; thread < nthreads; thread++) {
  570. int fd = FD(evsel, cpu, thread),
  571. err = ioctl(fd, ioc, arg);
  572. if (err)
  573. return err;
  574. }
  575. }
  576. return 0;
  577. }
  578. int perf_evsel__set_filter(struct perf_evsel *evsel, int ncpus, int nthreads,
  579. const char *filter)
  580. {
  581. return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
  582. PERF_EVENT_IOC_SET_FILTER,
  583. (void *)filter);
  584. }
  585. int perf_evsel__enable(struct perf_evsel *evsel, int ncpus, int nthreads)
  586. {
  587. return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
  588. PERF_EVENT_IOC_ENABLE,
  589. 0);
  590. }
  591. int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
  592. {
  593. evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
  594. if (evsel->sample_id == NULL)
  595. return -ENOMEM;
  596. evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
  597. if (evsel->id == NULL) {
  598. xyarray__delete(evsel->sample_id);
  599. evsel->sample_id = NULL;
  600. return -ENOMEM;
  601. }
  602. return 0;
  603. }
  604. void perf_evsel__reset_counts(struct perf_evsel *evsel, int ncpus)
  605. {
  606. memset(evsel->counts, 0, (sizeof(*evsel->counts) +
  607. (ncpus * sizeof(struct perf_counts_values))));
  608. }
  609. int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
  610. {
  611. evsel->counts = zalloc((sizeof(*evsel->counts) +
  612. (ncpus * sizeof(struct perf_counts_values))));
  613. return evsel->counts != NULL ? 0 : -ENOMEM;
  614. }
  615. void perf_evsel__free_fd(struct perf_evsel *evsel)
  616. {
  617. xyarray__delete(evsel->fd);
  618. evsel->fd = NULL;
  619. }
  620. void perf_evsel__free_id(struct perf_evsel *evsel)
  621. {
  622. xyarray__delete(evsel->sample_id);
  623. evsel->sample_id = NULL;
  624. zfree(&evsel->id);
  625. }
  626. void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
  627. {
  628. int cpu, thread;
  629. for (cpu = 0; cpu < ncpus; cpu++)
  630. for (thread = 0; thread < nthreads; ++thread) {
  631. close(FD(evsel, cpu, thread));
  632. FD(evsel, cpu, thread) = -1;
  633. }
  634. }
  635. void perf_evsel__free_counts(struct perf_evsel *evsel)
  636. {
  637. zfree(&evsel->counts);
  638. }
  639. void perf_evsel__exit(struct perf_evsel *evsel)
  640. {
  641. assert(list_empty(&evsel->node));
  642. perf_evsel__free_fd(evsel);
  643. perf_evsel__free_id(evsel);
  644. }
  645. void perf_evsel__delete(struct perf_evsel *evsel)
  646. {
  647. perf_evsel__exit(evsel);
  648. close_cgroup(evsel->cgrp);
  649. zfree(&evsel->group_name);
  650. if (evsel->tp_format)
  651. pevent_free_format(evsel->tp_format);
  652. zfree(&evsel->name);
  653. free(evsel);
  654. }
  655. static inline void compute_deltas(struct perf_evsel *evsel,
  656. int cpu,
  657. struct perf_counts_values *count)
  658. {
  659. struct perf_counts_values tmp;
  660. if (!evsel->prev_raw_counts)
  661. return;
  662. if (cpu == -1) {
  663. tmp = evsel->prev_raw_counts->aggr;
  664. evsel->prev_raw_counts->aggr = *count;
  665. } else {
  666. tmp = evsel->prev_raw_counts->cpu[cpu];
  667. evsel->prev_raw_counts->cpu[cpu] = *count;
  668. }
  669. count->val = count->val - tmp.val;
  670. count->ena = count->ena - tmp.ena;
  671. count->run = count->run - tmp.run;
  672. }
  673. int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
  674. int cpu, int thread, bool scale)
  675. {
  676. struct perf_counts_values count;
  677. size_t nv = scale ? 3 : 1;
  678. if (FD(evsel, cpu, thread) < 0)
  679. return -EINVAL;
  680. if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
  681. return -ENOMEM;
  682. if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
  683. return -errno;
  684. compute_deltas(evsel, cpu, &count);
  685. if (scale) {
  686. if (count.run == 0)
  687. count.val = 0;
  688. else if (count.run < count.ena)
  689. count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
  690. } else
  691. count.ena = count.run = 0;
  692. evsel->counts->cpu[cpu] = count;
  693. return 0;
  694. }
  695. int __perf_evsel__read(struct perf_evsel *evsel,
  696. int ncpus, int nthreads, bool scale)
  697. {
  698. size_t nv = scale ? 3 : 1;
  699. int cpu, thread;
  700. struct perf_counts_values *aggr = &evsel->counts->aggr, count;
  701. aggr->val = aggr->ena = aggr->run = 0;
  702. for (cpu = 0; cpu < ncpus; cpu++) {
  703. for (thread = 0; thread < nthreads; thread++) {
  704. if (FD(evsel, cpu, thread) < 0)
  705. continue;
  706. if (readn(FD(evsel, cpu, thread),
  707. &count, nv * sizeof(u64)) < 0)
  708. return -errno;
  709. aggr->val += count.val;
  710. if (scale) {
  711. aggr->ena += count.ena;
  712. aggr->run += count.run;
  713. }
  714. }
  715. }
  716. compute_deltas(evsel, -1, aggr);
  717. evsel->counts->scaled = 0;
  718. if (scale) {
  719. if (aggr->run == 0) {
  720. evsel->counts->scaled = -1;
  721. aggr->val = 0;
  722. return 0;
  723. }
  724. if (aggr->run < aggr->ena) {
  725. evsel->counts->scaled = 1;
  726. aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
  727. }
  728. } else
  729. aggr->ena = aggr->run = 0;
  730. return 0;
  731. }
  732. static int get_group_fd(struct perf_evsel *evsel, int cpu, int thread)
  733. {
  734. struct perf_evsel *leader = evsel->leader;
  735. int fd;
  736. if (perf_evsel__is_group_leader(evsel))
  737. return -1;
  738. /*
  739. * Leader must be already processed/open,
  740. * if not it's a bug.
  741. */
  742. BUG_ON(!leader->fd);
  743. fd = FD(leader, cpu, thread);
  744. BUG_ON(fd == -1);
  745. return fd;
  746. }
  747. #define __PRINT_ATTR(fmt, cast, field) \
  748. fprintf(fp, " %-19s "fmt"\n", #field, cast attr->field)
  749. #define PRINT_ATTR_U32(field) __PRINT_ATTR("%u" , , field)
  750. #define PRINT_ATTR_X32(field) __PRINT_ATTR("%#x", , field)
  751. #define PRINT_ATTR_U64(field) __PRINT_ATTR("%" PRIu64, (uint64_t), field)
  752. #define PRINT_ATTR_X64(field) __PRINT_ATTR("%#"PRIx64, (uint64_t), field)
  753. #define PRINT_ATTR2N(name1, field1, name2, field2) \
  754. fprintf(fp, " %-19s %u %-19s %u\n", \
  755. name1, attr->field1, name2, attr->field2)
  756. #define PRINT_ATTR2(field1, field2) \
  757. PRINT_ATTR2N(#field1, field1, #field2, field2)
  758. static size_t perf_event_attr__fprintf(struct perf_event_attr *attr, FILE *fp)
  759. {
  760. size_t ret = 0;
  761. ret += fprintf(fp, "%.60s\n", graph_dotted_line);
  762. ret += fprintf(fp, "perf_event_attr:\n");
  763. ret += PRINT_ATTR_U32(type);
  764. ret += PRINT_ATTR_U32(size);
  765. ret += PRINT_ATTR_X64(config);
  766. ret += PRINT_ATTR_U64(sample_period);
  767. ret += PRINT_ATTR_U64(sample_freq);
  768. ret += PRINT_ATTR_X64(sample_type);
  769. ret += PRINT_ATTR_X64(read_format);
  770. ret += PRINT_ATTR2(disabled, inherit);
  771. ret += PRINT_ATTR2(pinned, exclusive);
  772. ret += PRINT_ATTR2(exclude_user, exclude_kernel);
  773. ret += PRINT_ATTR2(exclude_hv, exclude_idle);
  774. ret += PRINT_ATTR2(mmap, comm);
  775. ret += PRINT_ATTR2(freq, inherit_stat);
  776. ret += PRINT_ATTR2(enable_on_exec, task);
  777. ret += PRINT_ATTR2(watermark, precise_ip);
  778. ret += PRINT_ATTR2(mmap_data, sample_id_all);
  779. ret += PRINT_ATTR2(exclude_host, exclude_guest);
  780. ret += PRINT_ATTR2N("excl.callchain_kern", exclude_callchain_kernel,
  781. "excl.callchain_user", exclude_callchain_user);
  782. ret += PRINT_ATTR_U32(mmap2);
  783. ret += PRINT_ATTR_U32(wakeup_events);
  784. ret += PRINT_ATTR_U32(wakeup_watermark);
  785. ret += PRINT_ATTR_X32(bp_type);
  786. ret += PRINT_ATTR_X64(bp_addr);
  787. ret += PRINT_ATTR_X64(config1);
  788. ret += PRINT_ATTR_U64(bp_len);
  789. ret += PRINT_ATTR_X64(config2);
  790. ret += PRINT_ATTR_X64(branch_sample_type);
  791. ret += PRINT_ATTR_X64(sample_regs_user);
  792. ret += PRINT_ATTR_U32(sample_stack_user);
  793. ret += fprintf(fp, "%.60s\n", graph_dotted_line);
  794. return ret;
  795. }
  796. static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
  797. struct thread_map *threads)
  798. {
  799. int cpu, thread;
  800. unsigned long flags = 0;
  801. int pid = -1, err;
  802. enum { NO_CHANGE, SET_TO_MAX, INCREASED_MAX } set_rlimit = NO_CHANGE;
  803. if (evsel->fd == NULL &&
  804. perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
  805. return -ENOMEM;
  806. if (evsel->cgrp) {
  807. flags = PERF_FLAG_PID_CGROUP;
  808. pid = evsel->cgrp->fd;
  809. }
  810. fallback_missing_features:
  811. if (perf_missing_features.mmap2)
  812. evsel->attr.mmap2 = 0;
  813. if (perf_missing_features.exclude_guest)
  814. evsel->attr.exclude_guest = evsel->attr.exclude_host = 0;
  815. retry_sample_id:
  816. if (perf_missing_features.sample_id_all)
  817. evsel->attr.sample_id_all = 0;
  818. if (verbose >= 2)
  819. perf_event_attr__fprintf(&evsel->attr, stderr);
  820. for (cpu = 0; cpu < cpus->nr; cpu++) {
  821. for (thread = 0; thread < threads->nr; thread++) {
  822. int group_fd;
  823. if (!evsel->cgrp)
  824. pid = threads->map[thread];
  825. group_fd = get_group_fd(evsel, cpu, thread);
  826. retry_open:
  827. pr_debug2("perf_event_open: pid %d cpu %d group_fd %d flags %#lx\n",
  828. pid, cpus->map[cpu], group_fd, flags);
  829. FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
  830. pid,
  831. cpus->map[cpu],
  832. group_fd, flags);
  833. if (FD(evsel, cpu, thread) < 0) {
  834. err = -errno;
  835. pr_debug2("perf_event_open failed, error %d\n",
  836. err);
  837. goto try_fallback;
  838. }
  839. set_rlimit = NO_CHANGE;
  840. }
  841. }
  842. return 0;
  843. try_fallback:
  844. /*
  845. * perf stat needs between 5 and 22 fds per CPU. When we run out
  846. * of them try to increase the limits.
  847. */
  848. if (err == -EMFILE && set_rlimit < INCREASED_MAX) {
  849. struct rlimit l;
  850. int old_errno = errno;
  851. if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
  852. if (set_rlimit == NO_CHANGE)
  853. l.rlim_cur = l.rlim_max;
  854. else {
  855. l.rlim_cur = l.rlim_max + 1000;
  856. l.rlim_max = l.rlim_cur;
  857. }
  858. if (setrlimit(RLIMIT_NOFILE, &l) == 0) {
  859. set_rlimit++;
  860. errno = old_errno;
  861. goto retry_open;
  862. }
  863. }
  864. errno = old_errno;
  865. }
  866. if (err != -EINVAL || cpu > 0 || thread > 0)
  867. goto out_close;
  868. if (!perf_missing_features.mmap2 && evsel->attr.mmap2) {
  869. perf_missing_features.mmap2 = true;
  870. goto fallback_missing_features;
  871. } else if (!perf_missing_features.exclude_guest &&
  872. (evsel->attr.exclude_guest || evsel->attr.exclude_host)) {
  873. perf_missing_features.exclude_guest = true;
  874. goto fallback_missing_features;
  875. } else if (!perf_missing_features.sample_id_all) {
  876. perf_missing_features.sample_id_all = true;
  877. goto retry_sample_id;
  878. }
  879. out_close:
  880. do {
  881. while (--thread >= 0) {
  882. close(FD(evsel, cpu, thread));
  883. FD(evsel, cpu, thread) = -1;
  884. }
  885. thread = threads->nr;
  886. } while (--cpu >= 0);
  887. return err;
  888. }
  889. void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
  890. {
  891. if (evsel->fd == NULL)
  892. return;
  893. perf_evsel__close_fd(evsel, ncpus, nthreads);
  894. perf_evsel__free_fd(evsel);
  895. }
  896. static struct {
  897. struct cpu_map map;
  898. int cpus[1];
  899. } empty_cpu_map = {
  900. .map.nr = 1,
  901. .cpus = { -1, },
  902. };
  903. static struct {
  904. struct thread_map map;
  905. int threads[1];
  906. } empty_thread_map = {
  907. .map.nr = 1,
  908. .threads = { -1, },
  909. };
  910. int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
  911. struct thread_map *threads)
  912. {
  913. if (cpus == NULL) {
  914. /* Work around old compiler warnings about strict aliasing */
  915. cpus = &empty_cpu_map.map;
  916. }
  917. if (threads == NULL)
  918. threads = &empty_thread_map.map;
  919. return __perf_evsel__open(evsel, cpus, threads);
  920. }
  921. int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
  922. struct cpu_map *cpus)
  923. {
  924. return __perf_evsel__open(evsel, cpus, &empty_thread_map.map);
  925. }
  926. int perf_evsel__open_per_thread(struct perf_evsel *evsel,
  927. struct thread_map *threads)
  928. {
  929. return __perf_evsel__open(evsel, &empty_cpu_map.map, threads);
  930. }
  931. static int perf_evsel__parse_id_sample(const struct perf_evsel *evsel,
  932. const union perf_event *event,
  933. struct perf_sample *sample)
  934. {
  935. u64 type = evsel->attr.sample_type;
  936. const u64 *array = event->sample.array;
  937. bool swapped = evsel->needs_swap;
  938. union u64_swap u;
  939. array += ((event->header.size -
  940. sizeof(event->header)) / sizeof(u64)) - 1;
  941. if (type & PERF_SAMPLE_IDENTIFIER) {
  942. sample->id = *array;
  943. array--;
  944. }
  945. if (type & PERF_SAMPLE_CPU) {
  946. u.val64 = *array;
  947. if (swapped) {
  948. /* undo swap of u64, then swap on individual u32s */
  949. u.val64 = bswap_64(u.val64);
  950. u.val32[0] = bswap_32(u.val32[0]);
  951. }
  952. sample->cpu = u.val32[0];
  953. array--;
  954. }
  955. if (type & PERF_SAMPLE_STREAM_ID) {
  956. sample->stream_id = *array;
  957. array--;
  958. }
  959. if (type & PERF_SAMPLE_ID) {
  960. sample->id = *array;
  961. array--;
  962. }
  963. if (type & PERF_SAMPLE_TIME) {
  964. sample->time = *array;
  965. array--;
  966. }
  967. if (type & PERF_SAMPLE_TID) {
  968. u.val64 = *array;
  969. if (swapped) {
  970. /* undo swap of u64, then swap on individual u32s */
  971. u.val64 = bswap_64(u.val64);
  972. u.val32[0] = bswap_32(u.val32[0]);
  973. u.val32[1] = bswap_32(u.val32[1]);
  974. }
  975. sample->pid = u.val32[0];
  976. sample->tid = u.val32[1];
  977. array--;
  978. }
  979. return 0;
  980. }
  981. static inline bool overflow(const void *endp, u16 max_size, const void *offset,
  982. u64 size)
  983. {
  984. return size > max_size || offset + size > endp;
  985. }
  986. #define OVERFLOW_CHECK(offset, size, max_size) \
  987. do { \
  988. if (overflow(endp, (max_size), (offset), (size))) \
  989. return -EFAULT; \
  990. } while (0)
  991. #define OVERFLOW_CHECK_u64(offset) \
  992. OVERFLOW_CHECK(offset, sizeof(u64), sizeof(u64))
  993. int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
  994. struct perf_sample *data)
  995. {
  996. u64 type = evsel->attr.sample_type;
  997. bool swapped = evsel->needs_swap;
  998. const u64 *array;
  999. u16 max_size = event->header.size;
  1000. const void *endp = (void *)event + max_size;
  1001. u64 sz;
  1002. /*
  1003. * used for cross-endian analysis. See git commit 65014ab3
  1004. * for why this goofiness is needed.
  1005. */
  1006. union u64_swap u;
  1007. memset(data, 0, sizeof(*data));
  1008. data->cpu = data->pid = data->tid = -1;
  1009. data->stream_id = data->id = data->time = -1ULL;
  1010. data->period = 1;
  1011. data->weight = 0;
  1012. if (event->header.type != PERF_RECORD_SAMPLE) {
  1013. if (!evsel->attr.sample_id_all)
  1014. return 0;
  1015. return perf_evsel__parse_id_sample(evsel, event, data);
  1016. }
  1017. array = event->sample.array;
  1018. /*
  1019. * The evsel's sample_size is based on PERF_SAMPLE_MASK which includes
  1020. * up to PERF_SAMPLE_PERIOD. After that overflow() must be used to
  1021. * check the format does not go past the end of the event.
  1022. */
  1023. if (evsel->sample_size + sizeof(event->header) > event->header.size)
  1024. return -EFAULT;
  1025. data->id = -1ULL;
  1026. if (type & PERF_SAMPLE_IDENTIFIER) {
  1027. data->id = *array;
  1028. array++;
  1029. }
  1030. if (type & PERF_SAMPLE_IP) {
  1031. data->ip = *array;
  1032. array++;
  1033. }
  1034. if (type & PERF_SAMPLE_TID) {
  1035. u.val64 = *array;
  1036. if (swapped) {
  1037. /* undo swap of u64, then swap on individual u32s */
  1038. u.val64 = bswap_64(u.val64);
  1039. u.val32[0] = bswap_32(u.val32[0]);
  1040. u.val32[1] = bswap_32(u.val32[1]);
  1041. }
  1042. data->pid = u.val32[0];
  1043. data->tid = u.val32[1];
  1044. array++;
  1045. }
  1046. if (type & PERF_SAMPLE_TIME) {
  1047. data->time = *array;
  1048. array++;
  1049. }
  1050. data->addr = 0;
  1051. if (type & PERF_SAMPLE_ADDR) {
  1052. data->addr = *array;
  1053. array++;
  1054. }
  1055. if (type & PERF_SAMPLE_ID) {
  1056. data->id = *array;
  1057. array++;
  1058. }
  1059. if (type & PERF_SAMPLE_STREAM_ID) {
  1060. data->stream_id = *array;
  1061. array++;
  1062. }
  1063. if (type & PERF_SAMPLE_CPU) {
  1064. u.val64 = *array;
  1065. if (swapped) {
  1066. /* undo swap of u64, then swap on individual u32s */
  1067. u.val64 = bswap_64(u.val64);
  1068. u.val32[0] = bswap_32(u.val32[0]);
  1069. }
  1070. data->cpu = u.val32[0];
  1071. array++;
  1072. }
  1073. if (type & PERF_SAMPLE_PERIOD) {
  1074. data->period = *array;
  1075. array++;
  1076. }
  1077. if (type & PERF_SAMPLE_READ) {
  1078. u64 read_format = evsel->attr.read_format;
  1079. OVERFLOW_CHECK_u64(array);
  1080. if (read_format & PERF_FORMAT_GROUP)
  1081. data->read.group.nr = *array;
  1082. else
  1083. data->read.one.value = *array;
  1084. array++;
  1085. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
  1086. OVERFLOW_CHECK_u64(array);
  1087. data->read.time_enabled = *array;
  1088. array++;
  1089. }
  1090. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
  1091. OVERFLOW_CHECK_u64(array);
  1092. data->read.time_running = *array;
  1093. array++;
  1094. }
  1095. /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
  1096. if (read_format & PERF_FORMAT_GROUP) {
  1097. const u64 max_group_nr = UINT64_MAX /
  1098. sizeof(struct sample_read_value);
  1099. if (data->read.group.nr > max_group_nr)
  1100. return -EFAULT;
  1101. sz = data->read.group.nr *
  1102. sizeof(struct sample_read_value);
  1103. OVERFLOW_CHECK(array, sz, max_size);
  1104. data->read.group.values =
  1105. (struct sample_read_value *)array;
  1106. array = (void *)array + sz;
  1107. } else {
  1108. OVERFLOW_CHECK_u64(array);
  1109. data->read.one.id = *array;
  1110. array++;
  1111. }
  1112. }
  1113. if (type & PERF_SAMPLE_CALLCHAIN) {
  1114. const u64 max_callchain_nr = UINT64_MAX / sizeof(u64);
  1115. OVERFLOW_CHECK_u64(array);
  1116. data->callchain = (struct ip_callchain *)array++;
  1117. if (data->callchain->nr > max_callchain_nr)
  1118. return -EFAULT;
  1119. sz = data->callchain->nr * sizeof(u64);
  1120. OVERFLOW_CHECK(array, sz, max_size);
  1121. array = (void *)array + sz;
  1122. }
  1123. if (type & PERF_SAMPLE_RAW) {
  1124. OVERFLOW_CHECK_u64(array);
  1125. u.val64 = *array;
  1126. if (WARN_ONCE(swapped,
  1127. "Endianness of raw data not corrected!\n")) {
  1128. /* undo swap of u64, then swap on individual u32s */
  1129. u.val64 = bswap_64(u.val64);
  1130. u.val32[0] = bswap_32(u.val32[0]);
  1131. u.val32[1] = bswap_32(u.val32[1]);
  1132. }
  1133. data->raw_size = u.val32[0];
  1134. array = (void *)array + sizeof(u32);
  1135. OVERFLOW_CHECK(array, data->raw_size, max_size);
  1136. data->raw_data = (void *)array;
  1137. array = (void *)array + data->raw_size;
  1138. }
  1139. if (type & PERF_SAMPLE_BRANCH_STACK) {
  1140. const u64 max_branch_nr = UINT64_MAX /
  1141. sizeof(struct branch_entry);
  1142. OVERFLOW_CHECK_u64(array);
  1143. data->branch_stack = (struct branch_stack *)array++;
  1144. if (data->branch_stack->nr > max_branch_nr)
  1145. return -EFAULT;
  1146. sz = data->branch_stack->nr * sizeof(struct branch_entry);
  1147. OVERFLOW_CHECK(array, sz, max_size);
  1148. array = (void *)array + sz;
  1149. }
  1150. if (type & PERF_SAMPLE_REGS_USER) {
  1151. OVERFLOW_CHECK_u64(array);
  1152. data->user_regs.abi = *array;
  1153. array++;
  1154. if (data->user_regs.abi) {
  1155. u64 regs_user = evsel->attr.sample_regs_user;
  1156. sz = hweight_long(regs_user) * sizeof(u64);
  1157. OVERFLOW_CHECK(array, sz, max_size);
  1158. data->user_regs.regs = (u64 *)array;
  1159. array = (void *)array + sz;
  1160. }
  1161. }
  1162. if (type & PERF_SAMPLE_STACK_USER) {
  1163. OVERFLOW_CHECK_u64(array);
  1164. sz = *array++;
  1165. data->user_stack.offset = ((char *)(array - 1)
  1166. - (char *) event);
  1167. if (!sz) {
  1168. data->user_stack.size = 0;
  1169. } else {
  1170. OVERFLOW_CHECK(array, sz, max_size);
  1171. data->user_stack.data = (char *)array;
  1172. array = (void *)array + sz;
  1173. OVERFLOW_CHECK_u64(array);
  1174. data->user_stack.size = *array++;
  1175. if (WARN_ONCE(data->user_stack.size > sz,
  1176. "user stack dump failure\n"))
  1177. return -EFAULT;
  1178. }
  1179. }
  1180. data->weight = 0;
  1181. if (type & PERF_SAMPLE_WEIGHT) {
  1182. OVERFLOW_CHECK_u64(array);
  1183. data->weight = *array;
  1184. array++;
  1185. }
  1186. data->data_src = PERF_MEM_DATA_SRC_NONE;
  1187. if (type & PERF_SAMPLE_DATA_SRC) {
  1188. OVERFLOW_CHECK_u64(array);
  1189. data->data_src = *array;
  1190. array++;
  1191. }
  1192. data->transaction = 0;
  1193. if (type & PERF_SAMPLE_TRANSACTION) {
  1194. OVERFLOW_CHECK_u64(array);
  1195. data->transaction = *array;
  1196. array++;
  1197. }
  1198. return 0;
  1199. }
  1200. size_t perf_event__sample_event_size(const struct perf_sample *sample, u64 type,
  1201. u64 sample_regs_user, u64 read_format)
  1202. {
  1203. size_t sz, result = sizeof(struct sample_event);
  1204. if (type & PERF_SAMPLE_IDENTIFIER)
  1205. result += sizeof(u64);
  1206. if (type & PERF_SAMPLE_IP)
  1207. result += sizeof(u64);
  1208. if (type & PERF_SAMPLE_TID)
  1209. result += sizeof(u64);
  1210. if (type & PERF_SAMPLE_TIME)
  1211. result += sizeof(u64);
  1212. if (type & PERF_SAMPLE_ADDR)
  1213. result += sizeof(u64);
  1214. if (type & PERF_SAMPLE_ID)
  1215. result += sizeof(u64);
  1216. if (type & PERF_SAMPLE_STREAM_ID)
  1217. result += sizeof(u64);
  1218. if (type & PERF_SAMPLE_CPU)
  1219. result += sizeof(u64);
  1220. if (type & PERF_SAMPLE_PERIOD)
  1221. result += sizeof(u64);
  1222. if (type & PERF_SAMPLE_READ) {
  1223. result += sizeof(u64);
  1224. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  1225. result += sizeof(u64);
  1226. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  1227. result += sizeof(u64);
  1228. /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
  1229. if (read_format & PERF_FORMAT_GROUP) {
  1230. sz = sample->read.group.nr *
  1231. sizeof(struct sample_read_value);
  1232. result += sz;
  1233. } else {
  1234. result += sizeof(u64);
  1235. }
  1236. }
  1237. if (type & PERF_SAMPLE_CALLCHAIN) {
  1238. sz = (sample->callchain->nr + 1) * sizeof(u64);
  1239. result += sz;
  1240. }
  1241. if (type & PERF_SAMPLE_RAW) {
  1242. result += sizeof(u32);
  1243. result += sample->raw_size;
  1244. }
  1245. if (type & PERF_SAMPLE_BRANCH_STACK) {
  1246. sz = sample->branch_stack->nr * sizeof(struct branch_entry);
  1247. sz += sizeof(u64);
  1248. result += sz;
  1249. }
  1250. if (type & PERF_SAMPLE_REGS_USER) {
  1251. if (sample->user_regs.abi) {
  1252. result += sizeof(u64);
  1253. sz = hweight_long(sample_regs_user) * sizeof(u64);
  1254. result += sz;
  1255. } else {
  1256. result += sizeof(u64);
  1257. }
  1258. }
  1259. if (type & PERF_SAMPLE_STACK_USER) {
  1260. sz = sample->user_stack.size;
  1261. result += sizeof(u64);
  1262. if (sz) {
  1263. result += sz;
  1264. result += sizeof(u64);
  1265. }
  1266. }
  1267. if (type & PERF_SAMPLE_WEIGHT)
  1268. result += sizeof(u64);
  1269. if (type & PERF_SAMPLE_DATA_SRC)
  1270. result += sizeof(u64);
  1271. if (type & PERF_SAMPLE_TRANSACTION)
  1272. result += sizeof(u64);
  1273. return result;
  1274. }
  1275. int perf_event__synthesize_sample(union perf_event *event, u64 type,
  1276. u64 sample_regs_user, u64 read_format,
  1277. const struct perf_sample *sample,
  1278. bool swapped)
  1279. {
  1280. u64 *array;
  1281. size_t sz;
  1282. /*
  1283. * used for cross-endian analysis. See git commit 65014ab3
  1284. * for why this goofiness is needed.
  1285. */
  1286. union u64_swap u;
  1287. array = event->sample.array;
  1288. if (type & PERF_SAMPLE_IDENTIFIER) {
  1289. *array = sample->id;
  1290. array++;
  1291. }
  1292. if (type & PERF_SAMPLE_IP) {
  1293. *array = sample->ip;
  1294. array++;
  1295. }
  1296. if (type & PERF_SAMPLE_TID) {
  1297. u.val32[0] = sample->pid;
  1298. u.val32[1] = sample->tid;
  1299. if (swapped) {
  1300. /*
  1301. * Inverse of what is done in perf_evsel__parse_sample
  1302. */
  1303. u.val32[0] = bswap_32(u.val32[0]);
  1304. u.val32[1] = bswap_32(u.val32[1]);
  1305. u.val64 = bswap_64(u.val64);
  1306. }
  1307. *array = u.val64;
  1308. array++;
  1309. }
  1310. if (type & PERF_SAMPLE_TIME) {
  1311. *array = sample->time;
  1312. array++;
  1313. }
  1314. if (type & PERF_SAMPLE_ADDR) {
  1315. *array = sample->addr;
  1316. array++;
  1317. }
  1318. if (type & PERF_SAMPLE_ID) {
  1319. *array = sample->id;
  1320. array++;
  1321. }
  1322. if (type & PERF_SAMPLE_STREAM_ID) {
  1323. *array = sample->stream_id;
  1324. array++;
  1325. }
  1326. if (type & PERF_SAMPLE_CPU) {
  1327. u.val32[0] = sample->cpu;
  1328. if (swapped) {
  1329. /*
  1330. * Inverse of what is done in perf_evsel__parse_sample
  1331. */
  1332. u.val32[0] = bswap_32(u.val32[0]);
  1333. u.val64 = bswap_64(u.val64);
  1334. }
  1335. *array = u.val64;
  1336. array++;
  1337. }
  1338. if (type & PERF_SAMPLE_PERIOD) {
  1339. *array = sample->period;
  1340. array++;
  1341. }
  1342. if (type & PERF_SAMPLE_READ) {
  1343. if (read_format & PERF_FORMAT_GROUP)
  1344. *array = sample->read.group.nr;
  1345. else
  1346. *array = sample->read.one.value;
  1347. array++;
  1348. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
  1349. *array = sample->read.time_enabled;
  1350. array++;
  1351. }
  1352. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
  1353. *array = sample->read.time_running;
  1354. array++;
  1355. }
  1356. /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
  1357. if (read_format & PERF_FORMAT_GROUP) {
  1358. sz = sample->read.group.nr *
  1359. sizeof(struct sample_read_value);
  1360. memcpy(array, sample->read.group.values, sz);
  1361. array = (void *)array + sz;
  1362. } else {
  1363. *array = sample->read.one.id;
  1364. array++;
  1365. }
  1366. }
  1367. if (type & PERF_SAMPLE_CALLCHAIN) {
  1368. sz = (sample->callchain->nr + 1) * sizeof(u64);
  1369. memcpy(array, sample->callchain, sz);
  1370. array = (void *)array + sz;
  1371. }
  1372. if (type & PERF_SAMPLE_RAW) {
  1373. u.val32[0] = sample->raw_size;
  1374. if (WARN_ONCE(swapped,
  1375. "Endianness of raw data not corrected!\n")) {
  1376. /*
  1377. * Inverse of what is done in perf_evsel__parse_sample
  1378. */
  1379. u.val32[0] = bswap_32(u.val32[0]);
  1380. u.val32[1] = bswap_32(u.val32[1]);
  1381. u.val64 = bswap_64(u.val64);
  1382. }
  1383. *array = u.val64;
  1384. array = (void *)array + sizeof(u32);
  1385. memcpy(array, sample->raw_data, sample->raw_size);
  1386. array = (void *)array + sample->raw_size;
  1387. }
  1388. if (type & PERF_SAMPLE_BRANCH_STACK) {
  1389. sz = sample->branch_stack->nr * sizeof(struct branch_entry);
  1390. sz += sizeof(u64);
  1391. memcpy(array, sample->branch_stack, sz);
  1392. array = (void *)array + sz;
  1393. }
  1394. if (type & PERF_SAMPLE_REGS_USER) {
  1395. if (sample->user_regs.abi) {
  1396. *array++ = sample->user_regs.abi;
  1397. sz = hweight_long(sample_regs_user) * sizeof(u64);
  1398. memcpy(array, sample->user_regs.regs, sz);
  1399. array = (void *)array + sz;
  1400. } else {
  1401. *array++ = 0;
  1402. }
  1403. }
  1404. if (type & PERF_SAMPLE_STACK_USER) {
  1405. sz = sample->user_stack.size;
  1406. *array++ = sz;
  1407. if (sz) {
  1408. memcpy(array, sample->user_stack.data, sz);
  1409. array = (void *)array + sz;
  1410. *array++ = sz;
  1411. }
  1412. }
  1413. if (type & PERF_SAMPLE_WEIGHT) {
  1414. *array = sample->weight;
  1415. array++;
  1416. }
  1417. if (type & PERF_SAMPLE_DATA_SRC) {
  1418. *array = sample->data_src;
  1419. array++;
  1420. }
  1421. if (type & PERF_SAMPLE_TRANSACTION) {
  1422. *array = sample->transaction;
  1423. array++;
  1424. }
  1425. return 0;
  1426. }
  1427. struct format_field *perf_evsel__field(struct perf_evsel *evsel, const char *name)
  1428. {
  1429. return pevent_find_field(evsel->tp_format, name);
  1430. }
  1431. void *perf_evsel__rawptr(struct perf_evsel *evsel, struct perf_sample *sample,
  1432. const char *name)
  1433. {
  1434. struct format_field *field = perf_evsel__field(evsel, name);
  1435. int offset;
  1436. if (!field)
  1437. return NULL;
  1438. offset = field->offset;
  1439. if (field->flags & FIELD_IS_DYNAMIC) {
  1440. offset = *(int *)(sample->raw_data + field->offset);
  1441. offset &= 0xffff;
  1442. }
  1443. return sample->raw_data + offset;
  1444. }
  1445. u64 perf_evsel__intval(struct perf_evsel *evsel, struct perf_sample *sample,
  1446. const char *name)
  1447. {
  1448. struct format_field *field = perf_evsel__field(evsel, name);
  1449. void *ptr;
  1450. u64 value;
  1451. if (!field)
  1452. return 0;
  1453. ptr = sample->raw_data + field->offset;
  1454. switch (field->size) {
  1455. case 1:
  1456. return *(u8 *)ptr;
  1457. case 2:
  1458. value = *(u16 *)ptr;
  1459. break;
  1460. case 4:
  1461. value = *(u32 *)ptr;
  1462. break;
  1463. case 8:
  1464. value = *(u64 *)ptr;
  1465. break;
  1466. default:
  1467. return 0;
  1468. }
  1469. if (!evsel->needs_swap)
  1470. return value;
  1471. switch (field->size) {
  1472. case 2:
  1473. return bswap_16(value);
  1474. case 4:
  1475. return bswap_32(value);
  1476. case 8:
  1477. return bswap_64(value);
  1478. default:
  1479. return 0;
  1480. }
  1481. return 0;
  1482. }
  1483. static int comma_fprintf(FILE *fp, bool *first, const char *fmt, ...)
  1484. {
  1485. va_list args;
  1486. int ret = 0;
  1487. if (!*first) {
  1488. ret += fprintf(fp, ",");
  1489. } else {
  1490. ret += fprintf(fp, ":");
  1491. *first = false;
  1492. }
  1493. va_start(args, fmt);
  1494. ret += vfprintf(fp, fmt, args);
  1495. va_end(args);
  1496. return ret;
  1497. }
  1498. static int __if_fprintf(FILE *fp, bool *first, const char *field, u64 value)
  1499. {
  1500. if (value == 0)
  1501. return 0;
  1502. return comma_fprintf(fp, first, " %s: %" PRIu64, field, value);
  1503. }
  1504. #define if_print(field) printed += __if_fprintf(fp, &first, #field, evsel->attr.field)
  1505. struct bit_names {
  1506. int bit;
  1507. const char *name;
  1508. };
  1509. static int bits__fprintf(FILE *fp, const char *field, u64 value,
  1510. struct bit_names *bits, bool *first)
  1511. {
  1512. int i = 0, printed = comma_fprintf(fp, first, " %s: ", field);
  1513. bool first_bit = true;
  1514. do {
  1515. if (value & bits[i].bit) {
  1516. printed += fprintf(fp, "%s%s", first_bit ? "" : "|", bits[i].name);
  1517. first_bit = false;
  1518. }
  1519. } while (bits[++i].name != NULL);
  1520. return printed;
  1521. }
  1522. static int sample_type__fprintf(FILE *fp, bool *first, u64 value)
  1523. {
  1524. #define bit_name(n) { PERF_SAMPLE_##n, #n }
  1525. struct bit_names bits[] = {
  1526. bit_name(IP), bit_name(TID), bit_name(TIME), bit_name(ADDR),
  1527. bit_name(READ), bit_name(CALLCHAIN), bit_name(ID), bit_name(CPU),
  1528. bit_name(PERIOD), bit_name(STREAM_ID), bit_name(RAW),
  1529. bit_name(BRANCH_STACK), bit_name(REGS_USER), bit_name(STACK_USER),
  1530. bit_name(IDENTIFIER),
  1531. { .name = NULL, }
  1532. };
  1533. #undef bit_name
  1534. return bits__fprintf(fp, "sample_type", value, bits, first);
  1535. }
  1536. static int read_format__fprintf(FILE *fp, bool *first, u64 value)
  1537. {
  1538. #define bit_name(n) { PERF_FORMAT_##n, #n }
  1539. struct bit_names bits[] = {
  1540. bit_name(TOTAL_TIME_ENABLED), bit_name(TOTAL_TIME_RUNNING),
  1541. bit_name(ID), bit_name(GROUP),
  1542. { .name = NULL, }
  1543. };
  1544. #undef bit_name
  1545. return bits__fprintf(fp, "read_format", value, bits, first);
  1546. }
  1547. int perf_evsel__fprintf(struct perf_evsel *evsel,
  1548. struct perf_attr_details *details, FILE *fp)
  1549. {
  1550. bool first = true;
  1551. int printed = 0;
  1552. if (details->event_group) {
  1553. struct perf_evsel *pos;
  1554. if (!perf_evsel__is_group_leader(evsel))
  1555. return 0;
  1556. if (evsel->nr_members > 1)
  1557. printed += fprintf(fp, "%s{", evsel->group_name ?: "");
  1558. printed += fprintf(fp, "%s", perf_evsel__name(evsel));
  1559. for_each_group_member(pos, evsel)
  1560. printed += fprintf(fp, ",%s", perf_evsel__name(pos));
  1561. if (evsel->nr_members > 1)
  1562. printed += fprintf(fp, "}");
  1563. goto out;
  1564. }
  1565. printed += fprintf(fp, "%s", perf_evsel__name(evsel));
  1566. if (details->verbose || details->freq) {
  1567. printed += comma_fprintf(fp, &first, " sample_freq=%" PRIu64,
  1568. (u64)evsel->attr.sample_freq);
  1569. }
  1570. if (details->verbose) {
  1571. if_print(type);
  1572. if_print(config);
  1573. if_print(config1);
  1574. if_print(config2);
  1575. if_print(size);
  1576. printed += sample_type__fprintf(fp, &first, evsel->attr.sample_type);
  1577. if (evsel->attr.read_format)
  1578. printed += read_format__fprintf(fp, &first, evsel->attr.read_format);
  1579. if_print(disabled);
  1580. if_print(inherit);
  1581. if_print(pinned);
  1582. if_print(exclusive);
  1583. if_print(exclude_user);
  1584. if_print(exclude_kernel);
  1585. if_print(exclude_hv);
  1586. if_print(exclude_idle);
  1587. if_print(mmap);
  1588. if_print(mmap2);
  1589. if_print(comm);
  1590. if_print(freq);
  1591. if_print(inherit_stat);
  1592. if_print(enable_on_exec);
  1593. if_print(task);
  1594. if_print(watermark);
  1595. if_print(precise_ip);
  1596. if_print(mmap_data);
  1597. if_print(sample_id_all);
  1598. if_print(exclude_host);
  1599. if_print(exclude_guest);
  1600. if_print(__reserved_1);
  1601. if_print(wakeup_events);
  1602. if_print(bp_type);
  1603. if_print(branch_sample_type);
  1604. }
  1605. out:
  1606. fputc('\n', fp);
  1607. return ++printed;
  1608. }
  1609. bool perf_evsel__fallback(struct perf_evsel *evsel, int err,
  1610. char *msg, size_t msgsize)
  1611. {
  1612. if ((err == ENOENT || err == ENXIO || err == ENODEV) &&
  1613. evsel->attr.type == PERF_TYPE_HARDWARE &&
  1614. evsel->attr.config == PERF_COUNT_HW_CPU_CYCLES) {
  1615. /*
  1616. * If it's cycles then fall back to hrtimer based
  1617. * cpu-clock-tick sw counter, which is always available even if
  1618. * no PMU support.
  1619. *
  1620. * PPC returns ENXIO until 2.6.37 (behavior changed with commit
  1621. * b0a873e).
  1622. */
  1623. scnprintf(msg, msgsize, "%s",
  1624. "The cycles event is not supported, trying to fall back to cpu-clock-ticks");
  1625. evsel->attr.type = PERF_TYPE_SOFTWARE;
  1626. evsel->attr.config = PERF_COUNT_SW_CPU_CLOCK;
  1627. zfree(&evsel->name);
  1628. return true;
  1629. }
  1630. return false;
  1631. }
  1632. int perf_evsel__open_strerror(struct perf_evsel *evsel, struct target *target,
  1633. int err, char *msg, size_t size)
  1634. {
  1635. switch (err) {
  1636. case EPERM:
  1637. case EACCES:
  1638. return scnprintf(msg, size,
  1639. "You may not have permission to collect %sstats.\n"
  1640. "Consider tweaking /proc/sys/kernel/perf_event_paranoid:\n"
  1641. " -1 - Not paranoid at all\n"
  1642. " 0 - Disallow raw tracepoint access for unpriv\n"
  1643. " 1 - Disallow cpu events for unpriv\n"
  1644. " 2 - Disallow kernel profiling for unpriv",
  1645. target->system_wide ? "system-wide " : "");
  1646. case ENOENT:
  1647. return scnprintf(msg, size, "The %s event is not supported.",
  1648. perf_evsel__name(evsel));
  1649. case EMFILE:
  1650. return scnprintf(msg, size, "%s",
  1651. "Too many events are opened.\n"
  1652. "Try again after reducing the number of events.");
  1653. case ENODEV:
  1654. if (target->cpu_list)
  1655. return scnprintf(msg, size, "%s",
  1656. "No such device - did you specify an out-of-range profile CPU?\n");
  1657. break;
  1658. case EOPNOTSUPP:
  1659. if (evsel->attr.precise_ip)
  1660. return scnprintf(msg, size, "%s",
  1661. "\'precise\' request may not be supported. Try removing 'p' modifier.");
  1662. #if defined(__i386__) || defined(__x86_64__)
  1663. if (evsel->attr.type == PERF_TYPE_HARDWARE)
  1664. return scnprintf(msg, size, "%s",
  1665. "No hardware sampling interrupt available.\n"
  1666. "No APIC? If so then you can boot the kernel with the \"lapic\" boot parameter to force-enable it.");
  1667. #endif
  1668. break;
  1669. default:
  1670. break;
  1671. }
  1672. return scnprintf(msg, size,
  1673. "The sys_perf_event_open() syscall returned with %d (%s) for event (%s). \n"
  1674. "/bin/dmesg may provide additional information.\n"
  1675. "No CONFIG_PERF_EVENTS=y kernel support configured?\n",
  1676. err, strerror(err), perf_evsel__name(evsel));
  1677. }