evsel.c 51 KB

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