evsel.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750
  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 <lk/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 "evsel.h"
  18. #include "evlist.h"
  19. #include "util.h"
  20. #include "cpumap.h"
  21. #include "thread_map.h"
  22. #include "target.h"
  23. #include "perf_regs.h"
  24. #include "debug.h"
  25. static struct {
  26. bool sample_id_all;
  27. bool exclude_guest;
  28. } perf_missing_features;
  29. #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
  30. static int __perf_evsel__sample_size(u64 sample_type)
  31. {
  32. u64 mask = sample_type & PERF_SAMPLE_MASK;
  33. int size = 0;
  34. int i;
  35. for (i = 0; i < 64; i++) {
  36. if (mask & (1ULL << i))
  37. size++;
  38. }
  39. size *= sizeof(u64);
  40. return size;
  41. }
  42. void hists__init(struct hists *hists)
  43. {
  44. memset(hists, 0, sizeof(*hists));
  45. hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
  46. hists->entries_in = &hists->entries_in_array[0];
  47. hists->entries_collapsed = RB_ROOT;
  48. hists->entries = RB_ROOT;
  49. pthread_mutex_init(&hists->lock, NULL);
  50. }
  51. void __perf_evsel__set_sample_bit(struct perf_evsel *evsel,
  52. enum perf_event_sample_format bit)
  53. {
  54. if (!(evsel->attr.sample_type & bit)) {
  55. evsel->attr.sample_type |= bit;
  56. evsel->sample_size += sizeof(u64);
  57. }
  58. }
  59. void __perf_evsel__reset_sample_bit(struct perf_evsel *evsel,
  60. enum perf_event_sample_format bit)
  61. {
  62. if (evsel->attr.sample_type & bit) {
  63. evsel->attr.sample_type &= ~bit;
  64. evsel->sample_size -= sizeof(u64);
  65. }
  66. }
  67. void perf_evsel__set_sample_id(struct perf_evsel *evsel)
  68. {
  69. perf_evsel__set_sample_bit(evsel, ID);
  70. evsel->attr.read_format |= PERF_FORMAT_ID;
  71. }
  72. void perf_evsel__init(struct perf_evsel *evsel,
  73. struct perf_event_attr *attr, int idx)
  74. {
  75. evsel->idx = idx;
  76. evsel->attr = *attr;
  77. evsel->leader = evsel;
  78. INIT_LIST_HEAD(&evsel->node);
  79. hists__init(&evsel->hists);
  80. evsel->sample_size = __perf_evsel__sample_size(attr->sample_type);
  81. }
  82. struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr, int idx)
  83. {
  84. struct perf_evsel *evsel = zalloc(sizeof(*evsel));
  85. if (evsel != NULL)
  86. perf_evsel__init(evsel, attr, idx);
  87. return evsel;
  88. }
  89. struct event_format *event_format__new(const char *sys, const char *name)
  90. {
  91. int fd, n;
  92. char *filename;
  93. void *bf = NULL, *nbf;
  94. size_t size = 0, alloc_size = 0;
  95. struct event_format *format = NULL;
  96. if (asprintf(&filename, "%s/%s/%s/format", tracing_events_path, sys, name) < 0)
  97. goto out;
  98. fd = open(filename, O_RDONLY);
  99. if (fd < 0)
  100. goto out_free_filename;
  101. do {
  102. if (size == alloc_size) {
  103. alloc_size += BUFSIZ;
  104. nbf = realloc(bf, alloc_size);
  105. if (nbf == NULL)
  106. goto out_free_bf;
  107. bf = nbf;
  108. }
  109. n = read(fd, bf + size, alloc_size - size);
  110. if (n < 0)
  111. goto out_free_bf;
  112. size += n;
  113. } while (n > 0);
  114. pevent_parse_format(&format, bf, size, sys);
  115. out_free_bf:
  116. free(bf);
  117. close(fd);
  118. out_free_filename:
  119. free(filename);
  120. out:
  121. return format;
  122. }
  123. struct perf_evsel *perf_evsel__newtp(const char *sys, const char *name, int idx)
  124. {
  125. struct perf_evsel *evsel = zalloc(sizeof(*evsel));
  126. if (evsel != NULL) {
  127. struct perf_event_attr attr = {
  128. .type = PERF_TYPE_TRACEPOINT,
  129. .sample_type = (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME |
  130. PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD),
  131. };
  132. if (asprintf(&evsel->name, "%s:%s", sys, name) < 0)
  133. goto out_free;
  134. evsel->tp_format = event_format__new(sys, name);
  135. if (evsel->tp_format == NULL)
  136. goto out_free;
  137. event_attr_init(&attr);
  138. attr.config = evsel->tp_format->id;
  139. attr.sample_period = 1;
  140. perf_evsel__init(evsel, &attr, idx);
  141. }
  142. return evsel;
  143. out_free:
  144. free(evsel->name);
  145. free(evsel);
  146. return NULL;
  147. }
  148. const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX] = {
  149. "cycles",
  150. "instructions",
  151. "cache-references",
  152. "cache-misses",
  153. "branches",
  154. "branch-misses",
  155. "bus-cycles",
  156. "stalled-cycles-frontend",
  157. "stalled-cycles-backend",
  158. "ref-cycles",
  159. };
  160. static const char *__perf_evsel__hw_name(u64 config)
  161. {
  162. if (config < PERF_COUNT_HW_MAX && perf_evsel__hw_names[config])
  163. return perf_evsel__hw_names[config];
  164. return "unknown-hardware";
  165. }
  166. static int perf_evsel__add_modifiers(struct perf_evsel *evsel, char *bf, size_t size)
  167. {
  168. int colon = 0, r = 0;
  169. struct perf_event_attr *attr = &evsel->attr;
  170. bool exclude_guest_default = false;
  171. #define MOD_PRINT(context, mod) do { \
  172. if (!attr->exclude_##context) { \
  173. if (!colon) colon = ++r; \
  174. r += scnprintf(bf + r, size - r, "%c", mod); \
  175. } } while(0)
  176. if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) {
  177. MOD_PRINT(kernel, 'k');
  178. MOD_PRINT(user, 'u');
  179. MOD_PRINT(hv, 'h');
  180. exclude_guest_default = true;
  181. }
  182. if (attr->precise_ip) {
  183. if (!colon)
  184. colon = ++r;
  185. r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp");
  186. exclude_guest_default = true;
  187. }
  188. if (attr->exclude_host || attr->exclude_guest == exclude_guest_default) {
  189. MOD_PRINT(host, 'H');
  190. MOD_PRINT(guest, 'G');
  191. }
  192. #undef MOD_PRINT
  193. if (colon)
  194. bf[colon - 1] = ':';
  195. return r;
  196. }
  197. static int perf_evsel__hw_name(struct perf_evsel *evsel, char *bf, size_t size)
  198. {
  199. int r = scnprintf(bf, size, "%s", __perf_evsel__hw_name(evsel->attr.config));
  200. return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
  201. }
  202. const char *perf_evsel__sw_names[PERF_COUNT_SW_MAX] = {
  203. "cpu-clock",
  204. "task-clock",
  205. "page-faults",
  206. "context-switches",
  207. "cpu-migrations",
  208. "minor-faults",
  209. "major-faults",
  210. "alignment-faults",
  211. "emulation-faults",
  212. };
  213. static const char *__perf_evsel__sw_name(u64 config)
  214. {
  215. if (config < PERF_COUNT_SW_MAX && perf_evsel__sw_names[config])
  216. return perf_evsel__sw_names[config];
  217. return "unknown-software";
  218. }
  219. static int perf_evsel__sw_name(struct perf_evsel *evsel, char *bf, size_t size)
  220. {
  221. int r = scnprintf(bf, size, "%s", __perf_evsel__sw_name(evsel->attr.config));
  222. return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
  223. }
  224. static int __perf_evsel__bp_name(char *bf, size_t size, u64 addr, u64 type)
  225. {
  226. int r;
  227. r = scnprintf(bf, size, "mem:0x%" PRIx64 ":", addr);
  228. if (type & HW_BREAKPOINT_R)
  229. r += scnprintf(bf + r, size - r, "r");
  230. if (type & HW_BREAKPOINT_W)
  231. r += scnprintf(bf + r, size - r, "w");
  232. if (type & HW_BREAKPOINT_X)
  233. r += scnprintf(bf + r, size - r, "x");
  234. return r;
  235. }
  236. static int perf_evsel__bp_name(struct perf_evsel *evsel, char *bf, size_t size)
  237. {
  238. struct perf_event_attr *attr = &evsel->attr;
  239. int r = __perf_evsel__bp_name(bf, size, attr->bp_addr, attr->bp_type);
  240. return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
  241. }
  242. const char *perf_evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX]
  243. [PERF_EVSEL__MAX_ALIASES] = {
  244. { "L1-dcache", "l1-d", "l1d", "L1-data", },
  245. { "L1-icache", "l1-i", "l1i", "L1-instruction", },
  246. { "LLC", "L2", },
  247. { "dTLB", "d-tlb", "Data-TLB", },
  248. { "iTLB", "i-tlb", "Instruction-TLB", },
  249. { "branch", "branches", "bpu", "btb", "bpc", },
  250. { "node", },
  251. };
  252. const char *perf_evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX]
  253. [PERF_EVSEL__MAX_ALIASES] = {
  254. { "load", "loads", "read", },
  255. { "store", "stores", "write", },
  256. { "prefetch", "prefetches", "speculative-read", "speculative-load", },
  257. };
  258. const char *perf_evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX]
  259. [PERF_EVSEL__MAX_ALIASES] = {
  260. { "refs", "Reference", "ops", "access", },
  261. { "misses", "miss", },
  262. };
  263. #define C(x) PERF_COUNT_HW_CACHE_##x
  264. #define CACHE_READ (1 << C(OP_READ))
  265. #define CACHE_WRITE (1 << C(OP_WRITE))
  266. #define CACHE_PREFETCH (1 << C(OP_PREFETCH))
  267. #define COP(x) (1 << x)
  268. /*
  269. * cache operartion stat
  270. * L1I : Read and prefetch only
  271. * ITLB and BPU : Read-only
  272. */
  273. static unsigned long perf_evsel__hw_cache_stat[C(MAX)] = {
  274. [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
  275. [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
  276. [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
  277. [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
  278. [C(ITLB)] = (CACHE_READ),
  279. [C(BPU)] = (CACHE_READ),
  280. [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
  281. };
  282. bool perf_evsel__is_cache_op_valid(u8 type, u8 op)
  283. {
  284. if (perf_evsel__hw_cache_stat[type] & COP(op))
  285. return true; /* valid */
  286. else
  287. return false; /* invalid */
  288. }
  289. int __perf_evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result,
  290. char *bf, size_t size)
  291. {
  292. if (result) {
  293. return scnprintf(bf, size, "%s-%s-%s", perf_evsel__hw_cache[type][0],
  294. perf_evsel__hw_cache_op[op][0],
  295. perf_evsel__hw_cache_result[result][0]);
  296. }
  297. return scnprintf(bf, size, "%s-%s", perf_evsel__hw_cache[type][0],
  298. perf_evsel__hw_cache_op[op][1]);
  299. }
  300. static int __perf_evsel__hw_cache_name(u64 config, char *bf, size_t size)
  301. {
  302. u8 op, result, type = (config >> 0) & 0xff;
  303. const char *err = "unknown-ext-hardware-cache-type";
  304. if (type > PERF_COUNT_HW_CACHE_MAX)
  305. goto out_err;
  306. op = (config >> 8) & 0xff;
  307. err = "unknown-ext-hardware-cache-op";
  308. if (op > PERF_COUNT_HW_CACHE_OP_MAX)
  309. goto out_err;
  310. result = (config >> 16) & 0xff;
  311. err = "unknown-ext-hardware-cache-result";
  312. if (result > PERF_COUNT_HW_CACHE_RESULT_MAX)
  313. goto out_err;
  314. err = "invalid-cache";
  315. if (!perf_evsel__is_cache_op_valid(type, op))
  316. goto out_err;
  317. return __perf_evsel__hw_cache_type_op_res_name(type, op, result, bf, size);
  318. out_err:
  319. return scnprintf(bf, size, "%s", err);
  320. }
  321. static int perf_evsel__hw_cache_name(struct perf_evsel *evsel, char *bf, size_t size)
  322. {
  323. int ret = __perf_evsel__hw_cache_name(evsel->attr.config, bf, size);
  324. return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
  325. }
  326. static int perf_evsel__raw_name(struct perf_evsel *evsel, char *bf, size_t size)
  327. {
  328. int ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->attr.config);
  329. return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
  330. }
  331. const char *perf_evsel__name(struct perf_evsel *evsel)
  332. {
  333. char bf[128];
  334. if (evsel->name)
  335. return evsel->name;
  336. switch (evsel->attr.type) {
  337. case PERF_TYPE_RAW:
  338. perf_evsel__raw_name(evsel, bf, sizeof(bf));
  339. break;
  340. case PERF_TYPE_HARDWARE:
  341. perf_evsel__hw_name(evsel, bf, sizeof(bf));
  342. break;
  343. case PERF_TYPE_HW_CACHE:
  344. perf_evsel__hw_cache_name(evsel, bf, sizeof(bf));
  345. break;
  346. case PERF_TYPE_SOFTWARE:
  347. perf_evsel__sw_name(evsel, bf, sizeof(bf));
  348. break;
  349. case PERF_TYPE_TRACEPOINT:
  350. scnprintf(bf, sizeof(bf), "%s", "unknown tracepoint");
  351. break;
  352. case PERF_TYPE_BREAKPOINT:
  353. perf_evsel__bp_name(evsel, bf, sizeof(bf));
  354. break;
  355. default:
  356. scnprintf(bf, sizeof(bf), "unknown attr type: %d",
  357. evsel->attr.type);
  358. break;
  359. }
  360. evsel->name = strdup(bf);
  361. return evsel->name ?: "unknown";
  362. }
  363. const char *perf_evsel__group_name(struct perf_evsel *evsel)
  364. {
  365. return evsel->group_name ?: "anon group";
  366. }
  367. int perf_evsel__group_desc(struct perf_evsel *evsel, char *buf, size_t size)
  368. {
  369. int ret;
  370. struct perf_evsel *pos;
  371. const char *group_name = perf_evsel__group_name(evsel);
  372. ret = scnprintf(buf, size, "%s", group_name);
  373. ret += scnprintf(buf + ret, size - ret, " { %s",
  374. perf_evsel__name(evsel));
  375. for_each_group_member(pos, evsel)
  376. ret += scnprintf(buf + ret, size - ret, ", %s",
  377. perf_evsel__name(pos));
  378. ret += scnprintf(buf + ret, size - ret, " }");
  379. return ret;
  380. }
  381. /*
  382. * The enable_on_exec/disabled value strategy:
  383. *
  384. * 1) For any type of traced program:
  385. * - all independent events and group leaders are disabled
  386. * - all group members are enabled
  387. *
  388. * Group members are ruled by group leaders. They need to
  389. * be enabled, because the group scheduling relies on that.
  390. *
  391. * 2) For traced programs executed by perf:
  392. * - all independent events and group leaders have
  393. * enable_on_exec set
  394. * - we don't specifically enable or disable any event during
  395. * the record command
  396. *
  397. * Independent events and group leaders are initially disabled
  398. * and get enabled by exec. Group members are ruled by group
  399. * leaders as stated in 1).
  400. *
  401. * 3) For traced programs attached by perf (pid/tid):
  402. * - we specifically enable or disable all events during
  403. * the record command
  404. *
  405. * When attaching events to already running traced we
  406. * enable/disable events specifically, as there's no
  407. * initial traced exec call.
  408. */
  409. void perf_evsel__config(struct perf_evsel *evsel,
  410. struct perf_record_opts *opts)
  411. {
  412. struct perf_evsel *leader = evsel->leader;
  413. struct perf_event_attr *attr = &evsel->attr;
  414. int track = !evsel->idx; /* only the first counter needs these */
  415. attr->sample_id_all = perf_missing_features.sample_id_all ? 0 : 1;
  416. attr->inherit = !opts->no_inherit;
  417. perf_evsel__set_sample_bit(evsel, IP);
  418. perf_evsel__set_sample_bit(evsel, TID);
  419. if (evsel->sample_read) {
  420. perf_evsel__set_sample_bit(evsel, READ);
  421. /*
  422. * We need ID even in case of single event, because
  423. * PERF_SAMPLE_READ process ID specific data.
  424. */
  425. perf_evsel__set_sample_id(evsel);
  426. /*
  427. * Apply group format only if we belong to group
  428. * with more than one members.
  429. */
  430. if (leader->nr_members > 1) {
  431. attr->read_format |= PERF_FORMAT_GROUP;
  432. attr->inherit = 0;
  433. }
  434. }
  435. /*
  436. * We default some events to a 1 default interval. But keep
  437. * it a weak assumption overridable by the user.
  438. */
  439. if (!attr->sample_period || (opts->user_freq != UINT_MAX &&
  440. opts->user_interval != ULLONG_MAX)) {
  441. if (opts->freq) {
  442. perf_evsel__set_sample_bit(evsel, PERIOD);
  443. attr->freq = 1;
  444. attr->sample_freq = opts->freq;
  445. } else {
  446. attr->sample_period = opts->default_interval;
  447. }
  448. }
  449. /*
  450. * Disable sampling for all group members other
  451. * than leader in case leader 'leads' the sampling.
  452. */
  453. if ((leader != evsel) && leader->sample_read) {
  454. attr->sample_freq = 0;
  455. attr->sample_period = 0;
  456. }
  457. if (opts->no_samples)
  458. attr->sample_freq = 0;
  459. if (opts->inherit_stat)
  460. attr->inherit_stat = 1;
  461. if (opts->sample_address) {
  462. perf_evsel__set_sample_bit(evsel, ADDR);
  463. attr->mmap_data = track;
  464. }
  465. if (opts->call_graph) {
  466. perf_evsel__set_sample_bit(evsel, CALLCHAIN);
  467. if (opts->call_graph == CALLCHAIN_DWARF) {
  468. perf_evsel__set_sample_bit(evsel, REGS_USER);
  469. perf_evsel__set_sample_bit(evsel, STACK_USER);
  470. attr->sample_regs_user = PERF_REGS_MASK;
  471. attr->sample_stack_user = opts->stack_dump_size;
  472. attr->exclude_callchain_user = 1;
  473. }
  474. }
  475. if (perf_target__has_cpu(&opts->target))
  476. perf_evsel__set_sample_bit(evsel, CPU);
  477. if (opts->period)
  478. perf_evsel__set_sample_bit(evsel, PERIOD);
  479. if (!perf_missing_features.sample_id_all &&
  480. (opts->sample_time || !opts->no_inherit ||
  481. perf_target__has_cpu(&opts->target)))
  482. perf_evsel__set_sample_bit(evsel, TIME);
  483. if (opts->raw_samples) {
  484. perf_evsel__set_sample_bit(evsel, TIME);
  485. perf_evsel__set_sample_bit(evsel, RAW);
  486. perf_evsel__set_sample_bit(evsel, CPU);
  487. }
  488. if (opts->sample_address)
  489. attr->sample_type |= PERF_SAMPLE_DATA_SRC;
  490. if (opts->no_delay) {
  491. attr->watermark = 0;
  492. attr->wakeup_events = 1;
  493. }
  494. if (opts->branch_stack) {
  495. perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
  496. attr->branch_sample_type = opts->branch_stack;
  497. }
  498. if (opts->sample_weight)
  499. attr->sample_type |= PERF_SAMPLE_WEIGHT;
  500. attr->mmap = track;
  501. attr->comm = track;
  502. /*
  503. * XXX see the function comment above
  504. *
  505. * Disabling only independent events or group leaders,
  506. * keeping group members enabled.
  507. */
  508. if (perf_evsel__is_group_leader(evsel))
  509. attr->disabled = 1;
  510. /*
  511. * Setting enable_on_exec for independent events and
  512. * group leaders for traced executed by perf.
  513. */
  514. if (perf_target__none(&opts->target) && perf_evsel__is_group_leader(evsel))
  515. attr->enable_on_exec = 1;
  516. }
  517. int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
  518. {
  519. int cpu, thread;
  520. evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
  521. if (evsel->fd) {
  522. for (cpu = 0; cpu < ncpus; cpu++) {
  523. for (thread = 0; thread < nthreads; thread++) {
  524. FD(evsel, cpu, thread) = -1;
  525. }
  526. }
  527. }
  528. return evsel->fd != NULL ? 0 : -ENOMEM;
  529. }
  530. static int perf_evsel__run_ioctl(struct perf_evsel *evsel, int ncpus, int nthreads,
  531. int ioc, void *arg)
  532. {
  533. int cpu, thread;
  534. for (cpu = 0; cpu < ncpus; cpu++) {
  535. for (thread = 0; thread < nthreads; thread++) {
  536. int fd = FD(evsel, cpu, thread),
  537. err = ioctl(fd, ioc, arg);
  538. if (err)
  539. return err;
  540. }
  541. }
  542. return 0;
  543. }
  544. int perf_evsel__set_filter(struct perf_evsel *evsel, int ncpus, int nthreads,
  545. const char *filter)
  546. {
  547. return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
  548. PERF_EVENT_IOC_SET_FILTER,
  549. (void *)filter);
  550. }
  551. int perf_evsel__enable(struct perf_evsel *evsel, int ncpus, int nthreads)
  552. {
  553. return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
  554. PERF_EVENT_IOC_ENABLE,
  555. 0);
  556. }
  557. int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
  558. {
  559. evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
  560. if (evsel->sample_id == NULL)
  561. return -ENOMEM;
  562. evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
  563. if (evsel->id == NULL) {
  564. xyarray__delete(evsel->sample_id);
  565. evsel->sample_id = NULL;
  566. return -ENOMEM;
  567. }
  568. return 0;
  569. }
  570. void perf_evsel__reset_counts(struct perf_evsel *evsel, int ncpus)
  571. {
  572. memset(evsel->counts, 0, (sizeof(*evsel->counts) +
  573. (ncpus * sizeof(struct perf_counts_values))));
  574. }
  575. int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
  576. {
  577. evsel->counts = zalloc((sizeof(*evsel->counts) +
  578. (ncpus * sizeof(struct perf_counts_values))));
  579. return evsel->counts != NULL ? 0 : -ENOMEM;
  580. }
  581. void perf_evsel__free_fd(struct perf_evsel *evsel)
  582. {
  583. xyarray__delete(evsel->fd);
  584. evsel->fd = NULL;
  585. }
  586. void perf_evsel__free_id(struct perf_evsel *evsel)
  587. {
  588. xyarray__delete(evsel->sample_id);
  589. evsel->sample_id = NULL;
  590. free(evsel->id);
  591. evsel->id = NULL;
  592. }
  593. void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
  594. {
  595. int cpu, thread;
  596. for (cpu = 0; cpu < ncpus; cpu++)
  597. for (thread = 0; thread < nthreads; ++thread) {
  598. close(FD(evsel, cpu, thread));
  599. FD(evsel, cpu, thread) = -1;
  600. }
  601. }
  602. void perf_evsel__free_counts(struct perf_evsel *evsel)
  603. {
  604. free(evsel->counts);
  605. }
  606. void perf_evsel__exit(struct perf_evsel *evsel)
  607. {
  608. assert(list_empty(&evsel->node));
  609. perf_evsel__free_fd(evsel);
  610. perf_evsel__free_id(evsel);
  611. }
  612. void perf_evsel__delete(struct perf_evsel *evsel)
  613. {
  614. perf_evsel__exit(evsel);
  615. close_cgroup(evsel->cgrp);
  616. free(evsel->group_name);
  617. if (evsel->tp_format)
  618. pevent_free_format(evsel->tp_format);
  619. free(evsel->name);
  620. free(evsel);
  621. }
  622. static inline void compute_deltas(struct perf_evsel *evsel,
  623. int cpu,
  624. struct perf_counts_values *count)
  625. {
  626. struct perf_counts_values tmp;
  627. if (!evsel->prev_raw_counts)
  628. return;
  629. if (cpu == -1) {
  630. tmp = evsel->prev_raw_counts->aggr;
  631. evsel->prev_raw_counts->aggr = *count;
  632. } else {
  633. tmp = evsel->prev_raw_counts->cpu[cpu];
  634. evsel->prev_raw_counts->cpu[cpu] = *count;
  635. }
  636. count->val = count->val - tmp.val;
  637. count->ena = count->ena - tmp.ena;
  638. count->run = count->run - tmp.run;
  639. }
  640. int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
  641. int cpu, int thread, bool scale)
  642. {
  643. struct perf_counts_values count;
  644. size_t nv = scale ? 3 : 1;
  645. if (FD(evsel, cpu, thread) < 0)
  646. return -EINVAL;
  647. if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
  648. return -ENOMEM;
  649. if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
  650. return -errno;
  651. compute_deltas(evsel, cpu, &count);
  652. if (scale) {
  653. if (count.run == 0)
  654. count.val = 0;
  655. else if (count.run < count.ena)
  656. count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
  657. } else
  658. count.ena = count.run = 0;
  659. evsel->counts->cpu[cpu] = count;
  660. return 0;
  661. }
  662. int __perf_evsel__read(struct perf_evsel *evsel,
  663. int ncpus, int nthreads, bool scale)
  664. {
  665. size_t nv = scale ? 3 : 1;
  666. int cpu, thread;
  667. struct perf_counts_values *aggr = &evsel->counts->aggr, count;
  668. aggr->val = aggr->ena = aggr->run = 0;
  669. for (cpu = 0; cpu < ncpus; cpu++) {
  670. for (thread = 0; thread < nthreads; thread++) {
  671. if (FD(evsel, cpu, thread) < 0)
  672. continue;
  673. if (readn(FD(evsel, cpu, thread),
  674. &count, nv * sizeof(u64)) < 0)
  675. return -errno;
  676. aggr->val += count.val;
  677. if (scale) {
  678. aggr->ena += count.ena;
  679. aggr->run += count.run;
  680. }
  681. }
  682. }
  683. compute_deltas(evsel, -1, aggr);
  684. evsel->counts->scaled = 0;
  685. if (scale) {
  686. if (aggr->run == 0) {
  687. evsel->counts->scaled = -1;
  688. aggr->val = 0;
  689. return 0;
  690. }
  691. if (aggr->run < aggr->ena) {
  692. evsel->counts->scaled = 1;
  693. aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
  694. }
  695. } else
  696. aggr->ena = aggr->run = 0;
  697. return 0;
  698. }
  699. static int get_group_fd(struct perf_evsel *evsel, int cpu, int thread)
  700. {
  701. struct perf_evsel *leader = evsel->leader;
  702. int fd;
  703. if (perf_evsel__is_group_leader(evsel))
  704. return -1;
  705. /*
  706. * Leader must be already processed/open,
  707. * if not it's a bug.
  708. */
  709. BUG_ON(!leader->fd);
  710. fd = FD(leader, cpu, thread);
  711. BUG_ON(fd == -1);
  712. return fd;
  713. }
  714. #define __PRINT_ATTR(fmt, cast, field) \
  715. fprintf(fp, " %-19s "fmt"\n", #field, cast attr->field)
  716. #define PRINT_ATTR_U32(field) __PRINT_ATTR("%u" , , field)
  717. #define PRINT_ATTR_X32(field) __PRINT_ATTR("%#x", , field)
  718. #define PRINT_ATTR_U64(field) __PRINT_ATTR("%" PRIu64, (uint64_t), field)
  719. #define PRINT_ATTR_X64(field) __PRINT_ATTR("%#"PRIx64, (uint64_t), field)
  720. #define PRINT_ATTR2N(name1, field1, name2, field2) \
  721. fprintf(fp, " %-19s %u %-19s %u\n", \
  722. name1, attr->field1, name2, attr->field2)
  723. #define PRINT_ATTR2(field1, field2) \
  724. PRINT_ATTR2N(#field1, field1, #field2, field2)
  725. static size_t perf_event_attr__fprintf(struct perf_event_attr *attr, FILE *fp)
  726. {
  727. size_t ret = 0;
  728. ret += fprintf(fp, "%.60s\n", graph_dotted_line);
  729. ret += fprintf(fp, "perf_event_attr:\n");
  730. ret += PRINT_ATTR_U32(type);
  731. ret += PRINT_ATTR_U32(size);
  732. ret += PRINT_ATTR_X64(config);
  733. ret += PRINT_ATTR_U64(sample_period);
  734. ret += PRINT_ATTR_U64(sample_freq);
  735. ret += PRINT_ATTR_X64(sample_type);
  736. ret += PRINT_ATTR_X64(read_format);
  737. ret += PRINT_ATTR2(disabled, inherit);
  738. ret += PRINT_ATTR2(pinned, exclusive);
  739. ret += PRINT_ATTR2(exclude_user, exclude_kernel);
  740. ret += PRINT_ATTR2(exclude_hv, exclude_idle);
  741. ret += PRINT_ATTR2(mmap, comm);
  742. ret += PRINT_ATTR2(freq, inherit_stat);
  743. ret += PRINT_ATTR2(enable_on_exec, task);
  744. ret += PRINT_ATTR2(watermark, precise_ip);
  745. ret += PRINT_ATTR2(mmap_data, sample_id_all);
  746. ret += PRINT_ATTR2(exclude_host, exclude_guest);
  747. ret += PRINT_ATTR2N("excl.callchain_kern", exclude_callchain_kernel,
  748. "excl.callchain_user", exclude_callchain_user);
  749. ret += PRINT_ATTR_U32(wakeup_events);
  750. ret += PRINT_ATTR_U32(wakeup_watermark);
  751. ret += PRINT_ATTR_X32(bp_type);
  752. ret += PRINT_ATTR_X64(bp_addr);
  753. ret += PRINT_ATTR_X64(config1);
  754. ret += PRINT_ATTR_U64(bp_len);
  755. ret += PRINT_ATTR_X64(config2);
  756. ret += PRINT_ATTR_X64(branch_sample_type);
  757. ret += PRINT_ATTR_X64(sample_regs_user);
  758. ret += PRINT_ATTR_U32(sample_stack_user);
  759. ret += fprintf(fp, "%.60s\n", graph_dotted_line);
  760. return ret;
  761. }
  762. static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
  763. struct thread_map *threads)
  764. {
  765. int cpu, thread;
  766. unsigned long flags = 0;
  767. int pid = -1, err;
  768. enum { NO_CHANGE, SET_TO_MAX, INCREASED_MAX } set_rlimit = NO_CHANGE;
  769. if (evsel->fd == NULL &&
  770. perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
  771. return -ENOMEM;
  772. if (evsel->cgrp) {
  773. flags = PERF_FLAG_PID_CGROUP;
  774. pid = evsel->cgrp->fd;
  775. }
  776. fallback_missing_features:
  777. if (perf_missing_features.exclude_guest)
  778. evsel->attr.exclude_guest = evsel->attr.exclude_host = 0;
  779. retry_sample_id:
  780. if (perf_missing_features.sample_id_all)
  781. evsel->attr.sample_id_all = 0;
  782. if (verbose >= 2)
  783. perf_event_attr__fprintf(&evsel->attr, stderr);
  784. for (cpu = 0; cpu < cpus->nr; cpu++) {
  785. for (thread = 0; thread < threads->nr; thread++) {
  786. int group_fd;
  787. if (!evsel->cgrp)
  788. pid = threads->map[thread];
  789. group_fd = get_group_fd(evsel, cpu, thread);
  790. retry_open:
  791. pr_debug2("perf_event_open: pid %d cpu %d group_fd %d flags %#lx\n",
  792. pid, cpus->map[cpu], group_fd, flags);
  793. FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
  794. pid,
  795. cpus->map[cpu],
  796. group_fd, flags);
  797. if (FD(evsel, cpu, thread) < 0) {
  798. err = -errno;
  799. goto try_fallback;
  800. }
  801. set_rlimit = NO_CHANGE;
  802. }
  803. }
  804. return 0;
  805. try_fallback:
  806. /*
  807. * perf stat needs between 5 and 22 fds per CPU. When we run out
  808. * of them try to increase the limits.
  809. */
  810. if (err == -EMFILE && set_rlimit < INCREASED_MAX) {
  811. struct rlimit l;
  812. int old_errno = errno;
  813. if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
  814. if (set_rlimit == NO_CHANGE)
  815. l.rlim_cur = l.rlim_max;
  816. else {
  817. l.rlim_cur = l.rlim_max + 1000;
  818. l.rlim_max = l.rlim_cur;
  819. }
  820. if (setrlimit(RLIMIT_NOFILE, &l) == 0) {
  821. set_rlimit++;
  822. errno = old_errno;
  823. goto retry_open;
  824. }
  825. }
  826. errno = old_errno;
  827. }
  828. if (err != -EINVAL || cpu > 0 || thread > 0)
  829. goto out_close;
  830. if (!perf_missing_features.exclude_guest &&
  831. (evsel->attr.exclude_guest || evsel->attr.exclude_host)) {
  832. perf_missing_features.exclude_guest = true;
  833. goto fallback_missing_features;
  834. } else if (!perf_missing_features.sample_id_all) {
  835. perf_missing_features.sample_id_all = true;
  836. goto retry_sample_id;
  837. }
  838. out_close:
  839. do {
  840. while (--thread >= 0) {
  841. close(FD(evsel, cpu, thread));
  842. FD(evsel, cpu, thread) = -1;
  843. }
  844. thread = threads->nr;
  845. } while (--cpu >= 0);
  846. return err;
  847. }
  848. void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
  849. {
  850. if (evsel->fd == NULL)
  851. return;
  852. perf_evsel__close_fd(evsel, ncpus, nthreads);
  853. perf_evsel__free_fd(evsel);
  854. evsel->fd = NULL;
  855. }
  856. static struct {
  857. struct cpu_map map;
  858. int cpus[1];
  859. } empty_cpu_map = {
  860. .map.nr = 1,
  861. .cpus = { -1, },
  862. };
  863. static struct {
  864. struct thread_map map;
  865. int threads[1];
  866. } empty_thread_map = {
  867. .map.nr = 1,
  868. .threads = { -1, },
  869. };
  870. int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
  871. struct thread_map *threads)
  872. {
  873. if (cpus == NULL) {
  874. /* Work around old compiler warnings about strict aliasing */
  875. cpus = &empty_cpu_map.map;
  876. }
  877. if (threads == NULL)
  878. threads = &empty_thread_map.map;
  879. return __perf_evsel__open(evsel, cpus, threads);
  880. }
  881. int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
  882. struct cpu_map *cpus)
  883. {
  884. return __perf_evsel__open(evsel, cpus, &empty_thread_map.map);
  885. }
  886. int perf_evsel__open_per_thread(struct perf_evsel *evsel,
  887. struct thread_map *threads)
  888. {
  889. return __perf_evsel__open(evsel, &empty_cpu_map.map, threads);
  890. }
  891. static int perf_evsel__parse_id_sample(const struct perf_evsel *evsel,
  892. const union perf_event *event,
  893. struct perf_sample *sample)
  894. {
  895. u64 type = evsel->attr.sample_type;
  896. const u64 *array = event->sample.array;
  897. bool swapped = evsel->needs_swap;
  898. union u64_swap u;
  899. array += ((event->header.size -
  900. sizeof(event->header)) / sizeof(u64)) - 1;
  901. if (type & PERF_SAMPLE_CPU) {
  902. u.val64 = *array;
  903. if (swapped) {
  904. /* undo swap of u64, then swap on individual u32s */
  905. u.val64 = bswap_64(u.val64);
  906. u.val32[0] = bswap_32(u.val32[0]);
  907. }
  908. sample->cpu = u.val32[0];
  909. array--;
  910. }
  911. if (type & PERF_SAMPLE_STREAM_ID) {
  912. sample->stream_id = *array;
  913. array--;
  914. }
  915. if (type & PERF_SAMPLE_ID) {
  916. sample->id = *array;
  917. array--;
  918. }
  919. if (type & PERF_SAMPLE_TIME) {
  920. sample->time = *array;
  921. array--;
  922. }
  923. if (type & PERF_SAMPLE_TID) {
  924. u.val64 = *array;
  925. if (swapped) {
  926. /* undo swap of u64, then swap on individual u32s */
  927. u.val64 = bswap_64(u.val64);
  928. u.val32[0] = bswap_32(u.val32[0]);
  929. u.val32[1] = bswap_32(u.val32[1]);
  930. }
  931. sample->pid = u.val32[0];
  932. sample->tid = u.val32[1];
  933. }
  934. return 0;
  935. }
  936. static inline bool overflow(const void *endp, u16 max_size, const void *offset,
  937. u64 size)
  938. {
  939. return size > max_size || offset + size > endp;
  940. }
  941. #define OVERFLOW_CHECK(offset, size, max_size) \
  942. do { \
  943. if (overflow(endp, (max_size), (offset), (size))) \
  944. return -EFAULT; \
  945. } while (0)
  946. #define OVERFLOW_CHECK_u64(offset) \
  947. OVERFLOW_CHECK(offset, sizeof(u64), sizeof(u64))
  948. int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
  949. struct perf_sample *data)
  950. {
  951. u64 type = evsel->attr.sample_type;
  952. bool swapped = evsel->needs_swap;
  953. const u64 *array;
  954. u16 max_size = event->header.size;
  955. const void *endp = (void *)event + max_size;
  956. u64 sz;
  957. /*
  958. * used for cross-endian analysis. See git commit 65014ab3
  959. * for why this goofiness is needed.
  960. */
  961. union u64_swap u;
  962. memset(data, 0, sizeof(*data));
  963. data->cpu = data->pid = data->tid = -1;
  964. data->stream_id = data->id = data->time = -1ULL;
  965. data->period = 1;
  966. data->weight = 0;
  967. if (event->header.type != PERF_RECORD_SAMPLE) {
  968. if (!evsel->attr.sample_id_all)
  969. return 0;
  970. return perf_evsel__parse_id_sample(evsel, event, data);
  971. }
  972. array = event->sample.array;
  973. /*
  974. * The evsel's sample_size is based on PERF_SAMPLE_MASK which includes
  975. * up to PERF_SAMPLE_PERIOD. After that overflow() must be used to
  976. * check the format does not go past the end of the event.
  977. */
  978. if (evsel->sample_size + sizeof(event->header) > event->header.size)
  979. return -EFAULT;
  980. if (type & PERF_SAMPLE_IP) {
  981. data->ip = *array;
  982. array++;
  983. }
  984. if (type & PERF_SAMPLE_TID) {
  985. u.val64 = *array;
  986. if (swapped) {
  987. /* undo swap of u64, then swap on individual u32s */
  988. u.val64 = bswap_64(u.val64);
  989. u.val32[0] = bswap_32(u.val32[0]);
  990. u.val32[1] = bswap_32(u.val32[1]);
  991. }
  992. data->pid = u.val32[0];
  993. data->tid = u.val32[1];
  994. array++;
  995. }
  996. if (type & PERF_SAMPLE_TIME) {
  997. data->time = *array;
  998. array++;
  999. }
  1000. data->addr = 0;
  1001. if (type & PERF_SAMPLE_ADDR) {
  1002. data->addr = *array;
  1003. array++;
  1004. }
  1005. data->id = -1ULL;
  1006. if (type & PERF_SAMPLE_ID) {
  1007. data->id = *array;
  1008. array++;
  1009. }
  1010. if (type & PERF_SAMPLE_STREAM_ID) {
  1011. data->stream_id = *array;
  1012. array++;
  1013. }
  1014. if (type & PERF_SAMPLE_CPU) {
  1015. u.val64 = *array;
  1016. if (swapped) {
  1017. /* undo swap of u64, then swap on individual u32s */
  1018. u.val64 = bswap_64(u.val64);
  1019. u.val32[0] = bswap_32(u.val32[0]);
  1020. }
  1021. data->cpu = u.val32[0];
  1022. array++;
  1023. }
  1024. if (type & PERF_SAMPLE_PERIOD) {
  1025. data->period = *array;
  1026. array++;
  1027. }
  1028. if (type & PERF_SAMPLE_READ) {
  1029. u64 read_format = evsel->attr.read_format;
  1030. OVERFLOW_CHECK_u64(array);
  1031. if (read_format & PERF_FORMAT_GROUP)
  1032. data->read.group.nr = *array;
  1033. else
  1034. data->read.one.value = *array;
  1035. array++;
  1036. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
  1037. OVERFLOW_CHECK_u64(array);
  1038. data->read.time_enabled = *array;
  1039. array++;
  1040. }
  1041. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
  1042. OVERFLOW_CHECK_u64(array);
  1043. data->read.time_running = *array;
  1044. array++;
  1045. }
  1046. /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
  1047. if (read_format & PERF_FORMAT_GROUP) {
  1048. const u64 max_group_nr = UINT64_MAX /
  1049. sizeof(struct sample_read_value);
  1050. if (data->read.group.nr > max_group_nr)
  1051. return -EFAULT;
  1052. sz = data->read.group.nr *
  1053. sizeof(struct sample_read_value);
  1054. OVERFLOW_CHECK(array, sz, max_size);
  1055. data->read.group.values =
  1056. (struct sample_read_value *)array;
  1057. array = (void *)array + sz;
  1058. } else {
  1059. OVERFLOW_CHECK_u64(array);
  1060. data->read.one.id = *array;
  1061. array++;
  1062. }
  1063. }
  1064. if (type & PERF_SAMPLE_CALLCHAIN) {
  1065. const u64 max_callchain_nr = UINT64_MAX / sizeof(u64);
  1066. OVERFLOW_CHECK_u64(array);
  1067. data->callchain = (struct ip_callchain *)array++;
  1068. if (data->callchain->nr > max_callchain_nr)
  1069. return -EFAULT;
  1070. sz = data->callchain->nr * sizeof(u64);
  1071. OVERFLOW_CHECK(array, sz, max_size);
  1072. array = (void *)array + sz;
  1073. }
  1074. if (type & PERF_SAMPLE_RAW) {
  1075. OVERFLOW_CHECK_u64(array);
  1076. u.val64 = *array;
  1077. if (WARN_ONCE(swapped,
  1078. "Endianness of raw data not corrected!\n")) {
  1079. /* undo swap of u64, then swap on individual u32s */
  1080. u.val64 = bswap_64(u.val64);
  1081. u.val32[0] = bswap_32(u.val32[0]);
  1082. u.val32[1] = bswap_32(u.val32[1]);
  1083. }
  1084. data->raw_size = u.val32[0];
  1085. array = (void *)array + sizeof(u32);
  1086. OVERFLOW_CHECK(array, data->raw_size, max_size);
  1087. data->raw_data = (void *)array;
  1088. array = (void *)array + data->raw_size;
  1089. }
  1090. if (type & PERF_SAMPLE_BRANCH_STACK) {
  1091. const u64 max_branch_nr = UINT64_MAX /
  1092. sizeof(struct branch_entry);
  1093. OVERFLOW_CHECK_u64(array);
  1094. data->branch_stack = (struct branch_stack *)array++;
  1095. if (data->branch_stack->nr > max_branch_nr)
  1096. return -EFAULT;
  1097. sz = data->branch_stack->nr * sizeof(struct branch_entry);
  1098. OVERFLOW_CHECK(array, sz, max_size);
  1099. array = (void *)array + sz;
  1100. }
  1101. if (type & PERF_SAMPLE_REGS_USER) {
  1102. u64 avail;
  1103. /* First u64 tells us if we have any regs in sample. */
  1104. OVERFLOW_CHECK_u64(array);
  1105. avail = *array++;
  1106. if (avail) {
  1107. u64 regs_user = evsel->attr.sample_regs_user;
  1108. sz = hweight_long(regs_user) * sizeof(u64);
  1109. OVERFLOW_CHECK(array, sz, max_size);
  1110. data->user_regs.regs = (u64 *)array;
  1111. array = (void *)array + sz;
  1112. }
  1113. }
  1114. if (type & PERF_SAMPLE_STACK_USER) {
  1115. OVERFLOW_CHECK_u64(array);
  1116. sz = *array++;
  1117. data->user_stack.offset = ((char *)(array - 1)
  1118. - (char *) event);
  1119. if (!sz) {
  1120. data->user_stack.size = 0;
  1121. } else {
  1122. OVERFLOW_CHECK(array, sz, max_size);
  1123. data->user_stack.data = (char *)array;
  1124. array = (void *)array + sz;
  1125. OVERFLOW_CHECK_u64(array);
  1126. data->user_stack.size = *array++;
  1127. }
  1128. }
  1129. data->weight = 0;
  1130. if (type & PERF_SAMPLE_WEIGHT) {
  1131. OVERFLOW_CHECK_u64(array);
  1132. data->weight = *array;
  1133. array++;
  1134. }
  1135. data->data_src = PERF_MEM_DATA_SRC_NONE;
  1136. if (type & PERF_SAMPLE_DATA_SRC) {
  1137. OVERFLOW_CHECK_u64(array);
  1138. data->data_src = *array;
  1139. array++;
  1140. }
  1141. return 0;
  1142. }
  1143. int perf_event__synthesize_sample(union perf_event *event, u64 type,
  1144. const struct perf_sample *sample,
  1145. bool swapped)
  1146. {
  1147. u64 *array;
  1148. /*
  1149. * used for cross-endian analysis. See git commit 65014ab3
  1150. * for why this goofiness is needed.
  1151. */
  1152. union u64_swap u;
  1153. array = event->sample.array;
  1154. if (type & PERF_SAMPLE_IP) {
  1155. *array = sample->ip;
  1156. array++;
  1157. }
  1158. if (type & PERF_SAMPLE_TID) {
  1159. u.val32[0] = sample->pid;
  1160. u.val32[1] = sample->tid;
  1161. if (swapped) {
  1162. /*
  1163. * Inverse of what is done in perf_evsel__parse_sample
  1164. */
  1165. u.val32[0] = bswap_32(u.val32[0]);
  1166. u.val32[1] = bswap_32(u.val32[1]);
  1167. u.val64 = bswap_64(u.val64);
  1168. }
  1169. *array = u.val64;
  1170. array++;
  1171. }
  1172. if (type & PERF_SAMPLE_TIME) {
  1173. *array = sample->time;
  1174. array++;
  1175. }
  1176. if (type & PERF_SAMPLE_ADDR) {
  1177. *array = sample->addr;
  1178. array++;
  1179. }
  1180. if (type & PERF_SAMPLE_ID) {
  1181. *array = sample->id;
  1182. array++;
  1183. }
  1184. if (type & PERF_SAMPLE_STREAM_ID) {
  1185. *array = sample->stream_id;
  1186. array++;
  1187. }
  1188. if (type & PERF_SAMPLE_CPU) {
  1189. u.val32[0] = sample->cpu;
  1190. if (swapped) {
  1191. /*
  1192. * Inverse of what is done in perf_evsel__parse_sample
  1193. */
  1194. u.val32[0] = bswap_32(u.val32[0]);
  1195. u.val64 = bswap_64(u.val64);
  1196. }
  1197. *array = u.val64;
  1198. array++;
  1199. }
  1200. if (type & PERF_SAMPLE_PERIOD) {
  1201. *array = sample->period;
  1202. array++;
  1203. }
  1204. return 0;
  1205. }
  1206. struct format_field *perf_evsel__field(struct perf_evsel *evsel, const char *name)
  1207. {
  1208. return pevent_find_field(evsel->tp_format, name);
  1209. }
  1210. void *perf_evsel__rawptr(struct perf_evsel *evsel, struct perf_sample *sample,
  1211. const char *name)
  1212. {
  1213. struct format_field *field = perf_evsel__field(evsel, name);
  1214. int offset;
  1215. if (!field)
  1216. return NULL;
  1217. offset = field->offset;
  1218. if (field->flags & FIELD_IS_DYNAMIC) {
  1219. offset = *(int *)(sample->raw_data + field->offset);
  1220. offset &= 0xffff;
  1221. }
  1222. return sample->raw_data + offset;
  1223. }
  1224. u64 perf_evsel__intval(struct perf_evsel *evsel, struct perf_sample *sample,
  1225. const char *name)
  1226. {
  1227. struct format_field *field = perf_evsel__field(evsel, name);
  1228. void *ptr;
  1229. u64 value;
  1230. if (!field)
  1231. return 0;
  1232. ptr = sample->raw_data + field->offset;
  1233. switch (field->size) {
  1234. case 1:
  1235. return *(u8 *)ptr;
  1236. case 2:
  1237. value = *(u16 *)ptr;
  1238. break;
  1239. case 4:
  1240. value = *(u32 *)ptr;
  1241. break;
  1242. case 8:
  1243. value = *(u64 *)ptr;
  1244. break;
  1245. default:
  1246. return 0;
  1247. }
  1248. if (!evsel->needs_swap)
  1249. return value;
  1250. switch (field->size) {
  1251. case 2:
  1252. return bswap_16(value);
  1253. case 4:
  1254. return bswap_32(value);
  1255. case 8:
  1256. return bswap_64(value);
  1257. default:
  1258. return 0;
  1259. }
  1260. return 0;
  1261. }
  1262. static int comma_fprintf(FILE *fp, bool *first, const char *fmt, ...)
  1263. {
  1264. va_list args;
  1265. int ret = 0;
  1266. if (!*first) {
  1267. ret += fprintf(fp, ",");
  1268. } else {
  1269. ret += fprintf(fp, ":");
  1270. *first = false;
  1271. }
  1272. va_start(args, fmt);
  1273. ret += vfprintf(fp, fmt, args);
  1274. va_end(args);
  1275. return ret;
  1276. }
  1277. static int __if_fprintf(FILE *fp, bool *first, const char *field, u64 value)
  1278. {
  1279. if (value == 0)
  1280. return 0;
  1281. return comma_fprintf(fp, first, " %s: %" PRIu64, field, value);
  1282. }
  1283. #define if_print(field) printed += __if_fprintf(fp, &first, #field, evsel->attr.field)
  1284. struct bit_names {
  1285. int bit;
  1286. const char *name;
  1287. };
  1288. static int bits__fprintf(FILE *fp, const char *field, u64 value,
  1289. struct bit_names *bits, bool *first)
  1290. {
  1291. int i = 0, printed = comma_fprintf(fp, first, " %s: ", field);
  1292. bool first_bit = true;
  1293. do {
  1294. if (value & bits[i].bit) {
  1295. printed += fprintf(fp, "%s%s", first_bit ? "" : "|", bits[i].name);
  1296. first_bit = false;
  1297. }
  1298. } while (bits[++i].name != NULL);
  1299. return printed;
  1300. }
  1301. static int sample_type__fprintf(FILE *fp, bool *first, u64 value)
  1302. {
  1303. #define bit_name(n) { PERF_SAMPLE_##n, #n }
  1304. struct bit_names bits[] = {
  1305. bit_name(IP), bit_name(TID), bit_name(TIME), bit_name(ADDR),
  1306. bit_name(READ), bit_name(CALLCHAIN), bit_name(ID), bit_name(CPU),
  1307. bit_name(PERIOD), bit_name(STREAM_ID), bit_name(RAW),
  1308. bit_name(BRANCH_STACK), bit_name(REGS_USER), bit_name(STACK_USER),
  1309. { .name = NULL, }
  1310. };
  1311. #undef bit_name
  1312. return bits__fprintf(fp, "sample_type", value, bits, first);
  1313. }
  1314. static int read_format__fprintf(FILE *fp, bool *first, u64 value)
  1315. {
  1316. #define bit_name(n) { PERF_FORMAT_##n, #n }
  1317. struct bit_names bits[] = {
  1318. bit_name(TOTAL_TIME_ENABLED), bit_name(TOTAL_TIME_RUNNING),
  1319. bit_name(ID), bit_name(GROUP),
  1320. { .name = NULL, }
  1321. };
  1322. #undef bit_name
  1323. return bits__fprintf(fp, "read_format", value, bits, first);
  1324. }
  1325. int perf_evsel__fprintf(struct perf_evsel *evsel,
  1326. struct perf_attr_details *details, FILE *fp)
  1327. {
  1328. bool first = true;
  1329. int printed = 0;
  1330. if (details->event_group) {
  1331. struct perf_evsel *pos;
  1332. if (!perf_evsel__is_group_leader(evsel))
  1333. return 0;
  1334. if (evsel->nr_members > 1)
  1335. printed += fprintf(fp, "%s{", evsel->group_name ?: "");
  1336. printed += fprintf(fp, "%s", perf_evsel__name(evsel));
  1337. for_each_group_member(pos, evsel)
  1338. printed += fprintf(fp, ",%s", perf_evsel__name(pos));
  1339. if (evsel->nr_members > 1)
  1340. printed += fprintf(fp, "}");
  1341. goto out;
  1342. }
  1343. printed += fprintf(fp, "%s", perf_evsel__name(evsel));
  1344. if (details->verbose || details->freq) {
  1345. printed += comma_fprintf(fp, &first, " sample_freq=%" PRIu64,
  1346. (u64)evsel->attr.sample_freq);
  1347. }
  1348. if (details->verbose) {
  1349. if_print(type);
  1350. if_print(config);
  1351. if_print(config1);
  1352. if_print(config2);
  1353. if_print(size);
  1354. printed += sample_type__fprintf(fp, &first, evsel->attr.sample_type);
  1355. if (evsel->attr.read_format)
  1356. printed += read_format__fprintf(fp, &first, evsel->attr.read_format);
  1357. if_print(disabled);
  1358. if_print(inherit);
  1359. if_print(pinned);
  1360. if_print(exclusive);
  1361. if_print(exclude_user);
  1362. if_print(exclude_kernel);
  1363. if_print(exclude_hv);
  1364. if_print(exclude_idle);
  1365. if_print(mmap);
  1366. if_print(comm);
  1367. if_print(freq);
  1368. if_print(inherit_stat);
  1369. if_print(enable_on_exec);
  1370. if_print(task);
  1371. if_print(watermark);
  1372. if_print(precise_ip);
  1373. if_print(mmap_data);
  1374. if_print(sample_id_all);
  1375. if_print(exclude_host);
  1376. if_print(exclude_guest);
  1377. if_print(__reserved_1);
  1378. if_print(wakeup_events);
  1379. if_print(bp_type);
  1380. if_print(branch_sample_type);
  1381. }
  1382. out:
  1383. fputc('\n', fp);
  1384. return ++printed;
  1385. }
  1386. bool perf_evsel__fallback(struct perf_evsel *evsel, int err,
  1387. char *msg, size_t msgsize)
  1388. {
  1389. if ((err == ENOENT || err == ENXIO || err == ENODEV) &&
  1390. evsel->attr.type == PERF_TYPE_HARDWARE &&
  1391. evsel->attr.config == PERF_COUNT_HW_CPU_CYCLES) {
  1392. /*
  1393. * If it's cycles then fall back to hrtimer based
  1394. * cpu-clock-tick sw counter, which is always available even if
  1395. * no PMU support.
  1396. *
  1397. * PPC returns ENXIO until 2.6.37 (behavior changed with commit
  1398. * b0a873e).
  1399. */
  1400. scnprintf(msg, msgsize, "%s",
  1401. "The cycles event is not supported, trying to fall back to cpu-clock-ticks");
  1402. evsel->attr.type = PERF_TYPE_SOFTWARE;
  1403. evsel->attr.config = PERF_COUNT_SW_CPU_CLOCK;
  1404. free(evsel->name);
  1405. evsel->name = NULL;
  1406. return true;
  1407. }
  1408. return false;
  1409. }
  1410. int perf_evsel__open_strerror(struct perf_evsel *evsel,
  1411. struct perf_target *target,
  1412. int err, char *msg, size_t size)
  1413. {
  1414. switch (err) {
  1415. case EPERM:
  1416. case EACCES:
  1417. return scnprintf(msg, size,
  1418. "You may not have permission to collect %sstats.\n"
  1419. "Consider tweaking /proc/sys/kernel/perf_event_paranoid:\n"
  1420. " -1 - Not paranoid at all\n"
  1421. " 0 - Disallow raw tracepoint access for unpriv\n"
  1422. " 1 - Disallow cpu events for unpriv\n"
  1423. " 2 - Disallow kernel profiling for unpriv",
  1424. target->system_wide ? "system-wide " : "");
  1425. case ENOENT:
  1426. return scnprintf(msg, size, "The %s event is not supported.",
  1427. perf_evsel__name(evsel));
  1428. case EMFILE:
  1429. return scnprintf(msg, size, "%s",
  1430. "Too many events are opened.\n"
  1431. "Try again after reducing the number of events.");
  1432. case ENODEV:
  1433. if (target->cpu_list)
  1434. return scnprintf(msg, size, "%s",
  1435. "No such device - did you specify an out-of-range profile CPU?\n");
  1436. break;
  1437. case EOPNOTSUPP:
  1438. if (evsel->attr.precise_ip)
  1439. return scnprintf(msg, size, "%s",
  1440. "\'precise\' request may not be supported. Try removing 'p' modifier.");
  1441. #if defined(__i386__) || defined(__x86_64__)
  1442. if (evsel->attr.type == PERF_TYPE_HARDWARE)
  1443. return scnprintf(msg, size, "%s",
  1444. "No hardware sampling interrupt available.\n"
  1445. "No APIC? If so then you can boot the kernel with the \"lapic\" boot parameter to force-enable it.");
  1446. #endif
  1447. break;
  1448. default:
  1449. break;
  1450. }
  1451. return scnprintf(msg, size,
  1452. "The sys_perf_event_open() syscall returned with %d (%s) for event (%s). \n"
  1453. "/bin/dmesg may provide additional information.\n"
  1454. "No CONFIG_PERF_EVENTS=y kernel support configured?\n",
  1455. err, strerror(err), perf_evsel__name(evsel));
  1456. }