evsel.c 59 KB

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