evsel.c 72 KB

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