evsel.c 72 KB

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