evsel.c 72 KB

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