evsel.c 47 KB

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