evsel.c 52 KB

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