evsel.c 71 KB

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