evsel.c 37 KB

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