evsel.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612
  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. int perf_evsel__set_filter(struct perf_evsel *evsel, int ncpus, int nthreads,
  529. const char *filter)
  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, PERF_EVENT_IOC_SET_FILTER, filter);
  536. if (err)
  537. return err;
  538. }
  539. }
  540. return 0;
  541. }
  542. int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
  543. {
  544. evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
  545. if (evsel->sample_id == NULL)
  546. return -ENOMEM;
  547. evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
  548. if (evsel->id == NULL) {
  549. xyarray__delete(evsel->sample_id);
  550. evsel->sample_id = NULL;
  551. return -ENOMEM;
  552. }
  553. return 0;
  554. }
  555. void perf_evsel__reset_counts(struct perf_evsel *evsel, int ncpus)
  556. {
  557. memset(evsel->counts, 0, (sizeof(*evsel->counts) +
  558. (ncpus * sizeof(struct perf_counts_values))));
  559. }
  560. int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
  561. {
  562. evsel->counts = zalloc((sizeof(*evsel->counts) +
  563. (ncpus * sizeof(struct perf_counts_values))));
  564. return evsel->counts != NULL ? 0 : -ENOMEM;
  565. }
  566. void perf_evsel__free_fd(struct perf_evsel *evsel)
  567. {
  568. xyarray__delete(evsel->fd);
  569. evsel->fd = NULL;
  570. }
  571. void perf_evsel__free_id(struct perf_evsel *evsel)
  572. {
  573. xyarray__delete(evsel->sample_id);
  574. evsel->sample_id = NULL;
  575. free(evsel->id);
  576. evsel->id = NULL;
  577. }
  578. void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
  579. {
  580. int cpu, thread;
  581. for (cpu = 0; cpu < ncpus; cpu++)
  582. for (thread = 0; thread < nthreads; ++thread) {
  583. close(FD(evsel, cpu, thread));
  584. FD(evsel, cpu, thread) = -1;
  585. }
  586. }
  587. void perf_evsel__free_counts(struct perf_evsel *evsel)
  588. {
  589. free(evsel->counts);
  590. }
  591. void perf_evsel__exit(struct perf_evsel *evsel)
  592. {
  593. assert(list_empty(&evsel->node));
  594. perf_evsel__free_fd(evsel);
  595. perf_evsel__free_id(evsel);
  596. }
  597. void perf_evsel__delete(struct perf_evsel *evsel)
  598. {
  599. perf_evsel__exit(evsel);
  600. close_cgroup(evsel->cgrp);
  601. free(evsel->group_name);
  602. if (evsel->tp_format)
  603. pevent_free_format(evsel->tp_format);
  604. free(evsel->name);
  605. free(evsel);
  606. }
  607. static inline void compute_deltas(struct perf_evsel *evsel,
  608. int cpu,
  609. struct perf_counts_values *count)
  610. {
  611. struct perf_counts_values tmp;
  612. if (!evsel->prev_raw_counts)
  613. return;
  614. if (cpu == -1) {
  615. tmp = evsel->prev_raw_counts->aggr;
  616. evsel->prev_raw_counts->aggr = *count;
  617. } else {
  618. tmp = evsel->prev_raw_counts->cpu[cpu];
  619. evsel->prev_raw_counts->cpu[cpu] = *count;
  620. }
  621. count->val = count->val - tmp.val;
  622. count->ena = count->ena - tmp.ena;
  623. count->run = count->run - tmp.run;
  624. }
  625. int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
  626. int cpu, int thread, bool scale)
  627. {
  628. struct perf_counts_values count;
  629. size_t nv = scale ? 3 : 1;
  630. if (FD(evsel, cpu, thread) < 0)
  631. return -EINVAL;
  632. if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
  633. return -ENOMEM;
  634. if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
  635. return -errno;
  636. compute_deltas(evsel, cpu, &count);
  637. if (scale) {
  638. if (count.run == 0)
  639. count.val = 0;
  640. else if (count.run < count.ena)
  641. count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
  642. } else
  643. count.ena = count.run = 0;
  644. evsel->counts->cpu[cpu] = count;
  645. return 0;
  646. }
  647. int __perf_evsel__read(struct perf_evsel *evsel,
  648. int ncpus, int nthreads, bool scale)
  649. {
  650. size_t nv = scale ? 3 : 1;
  651. int cpu, thread;
  652. struct perf_counts_values *aggr = &evsel->counts->aggr, count;
  653. aggr->val = aggr->ena = aggr->run = 0;
  654. for (cpu = 0; cpu < ncpus; cpu++) {
  655. for (thread = 0; thread < nthreads; thread++) {
  656. if (FD(evsel, cpu, thread) < 0)
  657. continue;
  658. if (readn(FD(evsel, cpu, thread),
  659. &count, nv * sizeof(u64)) < 0)
  660. return -errno;
  661. aggr->val += count.val;
  662. if (scale) {
  663. aggr->ena += count.ena;
  664. aggr->run += count.run;
  665. }
  666. }
  667. }
  668. compute_deltas(evsel, -1, aggr);
  669. evsel->counts->scaled = 0;
  670. if (scale) {
  671. if (aggr->run == 0) {
  672. evsel->counts->scaled = -1;
  673. aggr->val = 0;
  674. return 0;
  675. }
  676. if (aggr->run < aggr->ena) {
  677. evsel->counts->scaled = 1;
  678. aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
  679. }
  680. } else
  681. aggr->ena = aggr->run = 0;
  682. return 0;
  683. }
  684. static int get_group_fd(struct perf_evsel *evsel, int cpu, int thread)
  685. {
  686. struct perf_evsel *leader = evsel->leader;
  687. int fd;
  688. if (perf_evsel__is_group_leader(evsel))
  689. return -1;
  690. /*
  691. * Leader must be already processed/open,
  692. * if not it's a bug.
  693. */
  694. BUG_ON(!leader->fd);
  695. fd = FD(leader, cpu, thread);
  696. BUG_ON(fd == -1);
  697. return fd;
  698. }
  699. static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
  700. struct thread_map *threads)
  701. {
  702. int cpu, thread;
  703. unsigned long flags = 0;
  704. int pid = -1, err;
  705. if (evsel->fd == NULL &&
  706. perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
  707. return -ENOMEM;
  708. if (evsel->cgrp) {
  709. flags = PERF_FLAG_PID_CGROUP;
  710. pid = evsel->cgrp->fd;
  711. }
  712. fallback_missing_features:
  713. if (perf_missing_features.exclude_guest)
  714. evsel->attr.exclude_guest = evsel->attr.exclude_host = 0;
  715. retry_sample_id:
  716. if (perf_missing_features.sample_id_all)
  717. evsel->attr.sample_id_all = 0;
  718. for (cpu = 0; cpu < cpus->nr; cpu++) {
  719. for (thread = 0; thread < threads->nr; thread++) {
  720. int group_fd;
  721. if (!evsel->cgrp)
  722. pid = threads->map[thread];
  723. group_fd = get_group_fd(evsel, cpu, thread);
  724. FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
  725. pid,
  726. cpus->map[cpu],
  727. group_fd, flags);
  728. if (FD(evsel, cpu, thread) < 0) {
  729. err = -errno;
  730. goto try_fallback;
  731. }
  732. }
  733. }
  734. return 0;
  735. try_fallback:
  736. if (err != -EINVAL || cpu > 0 || thread > 0)
  737. goto out_close;
  738. if (!perf_missing_features.exclude_guest &&
  739. (evsel->attr.exclude_guest || evsel->attr.exclude_host)) {
  740. perf_missing_features.exclude_guest = true;
  741. goto fallback_missing_features;
  742. } else if (!perf_missing_features.sample_id_all) {
  743. perf_missing_features.sample_id_all = true;
  744. goto retry_sample_id;
  745. }
  746. out_close:
  747. do {
  748. while (--thread >= 0) {
  749. close(FD(evsel, cpu, thread));
  750. FD(evsel, cpu, thread) = -1;
  751. }
  752. thread = threads->nr;
  753. } while (--cpu >= 0);
  754. return err;
  755. }
  756. void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
  757. {
  758. if (evsel->fd == NULL)
  759. return;
  760. perf_evsel__close_fd(evsel, ncpus, nthreads);
  761. perf_evsel__free_fd(evsel);
  762. evsel->fd = NULL;
  763. }
  764. static struct {
  765. struct cpu_map map;
  766. int cpus[1];
  767. } empty_cpu_map = {
  768. .map.nr = 1,
  769. .cpus = { -1, },
  770. };
  771. static struct {
  772. struct thread_map map;
  773. int threads[1];
  774. } empty_thread_map = {
  775. .map.nr = 1,
  776. .threads = { -1, },
  777. };
  778. int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
  779. struct thread_map *threads)
  780. {
  781. if (cpus == NULL) {
  782. /* Work around old compiler warnings about strict aliasing */
  783. cpus = &empty_cpu_map.map;
  784. }
  785. if (threads == NULL)
  786. threads = &empty_thread_map.map;
  787. return __perf_evsel__open(evsel, cpus, threads);
  788. }
  789. int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
  790. struct cpu_map *cpus)
  791. {
  792. return __perf_evsel__open(evsel, cpus, &empty_thread_map.map);
  793. }
  794. int perf_evsel__open_per_thread(struct perf_evsel *evsel,
  795. struct thread_map *threads)
  796. {
  797. return __perf_evsel__open(evsel, &empty_cpu_map.map, threads);
  798. }
  799. static int perf_evsel__parse_id_sample(const struct perf_evsel *evsel,
  800. const union perf_event *event,
  801. struct perf_sample *sample)
  802. {
  803. u64 type = evsel->attr.sample_type;
  804. const u64 *array = event->sample.array;
  805. bool swapped = evsel->needs_swap;
  806. union u64_swap u;
  807. array += ((event->header.size -
  808. sizeof(event->header)) / sizeof(u64)) - 1;
  809. if (type & PERF_SAMPLE_CPU) {
  810. u.val64 = *array;
  811. if (swapped) {
  812. /* undo swap of u64, then swap on individual u32s */
  813. u.val64 = bswap_64(u.val64);
  814. u.val32[0] = bswap_32(u.val32[0]);
  815. }
  816. sample->cpu = u.val32[0];
  817. array--;
  818. }
  819. if (type & PERF_SAMPLE_STREAM_ID) {
  820. sample->stream_id = *array;
  821. array--;
  822. }
  823. if (type & PERF_SAMPLE_ID) {
  824. sample->id = *array;
  825. array--;
  826. }
  827. if (type & PERF_SAMPLE_TIME) {
  828. sample->time = *array;
  829. array--;
  830. }
  831. if (type & PERF_SAMPLE_TID) {
  832. u.val64 = *array;
  833. if (swapped) {
  834. /* undo swap of u64, then swap on individual u32s */
  835. u.val64 = bswap_64(u.val64);
  836. u.val32[0] = bswap_32(u.val32[0]);
  837. u.val32[1] = bswap_32(u.val32[1]);
  838. }
  839. sample->pid = u.val32[0];
  840. sample->tid = u.val32[1];
  841. }
  842. return 0;
  843. }
  844. static bool sample_overlap(const union perf_event *event,
  845. const void *offset, u64 size)
  846. {
  847. const void *base = event;
  848. if (offset + size > base + event->header.size)
  849. return true;
  850. return false;
  851. }
  852. int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
  853. struct perf_sample *data)
  854. {
  855. u64 type = evsel->attr.sample_type;
  856. u64 regs_user = evsel->attr.sample_regs_user;
  857. bool swapped = evsel->needs_swap;
  858. const u64 *array;
  859. /*
  860. * used for cross-endian analysis. See git commit 65014ab3
  861. * for why this goofiness is needed.
  862. */
  863. union u64_swap u;
  864. memset(data, 0, sizeof(*data));
  865. data->cpu = data->pid = data->tid = -1;
  866. data->stream_id = data->id = data->time = -1ULL;
  867. data->period = 1;
  868. data->weight = 0;
  869. if (event->header.type != PERF_RECORD_SAMPLE) {
  870. if (!evsel->attr.sample_id_all)
  871. return 0;
  872. return perf_evsel__parse_id_sample(evsel, event, data);
  873. }
  874. array = event->sample.array;
  875. if (evsel->sample_size + sizeof(event->header) > event->header.size)
  876. return -EFAULT;
  877. if (type & PERF_SAMPLE_IP) {
  878. data->ip = event->ip.ip;
  879. array++;
  880. }
  881. if (type & PERF_SAMPLE_TID) {
  882. u.val64 = *array;
  883. if (swapped) {
  884. /* undo swap of u64, then swap on individual u32s */
  885. u.val64 = bswap_64(u.val64);
  886. u.val32[0] = bswap_32(u.val32[0]);
  887. u.val32[1] = bswap_32(u.val32[1]);
  888. }
  889. data->pid = u.val32[0];
  890. data->tid = u.val32[1];
  891. array++;
  892. }
  893. if (type & PERF_SAMPLE_TIME) {
  894. data->time = *array;
  895. array++;
  896. }
  897. data->addr = 0;
  898. if (type & PERF_SAMPLE_ADDR) {
  899. data->addr = *array;
  900. array++;
  901. }
  902. data->id = -1ULL;
  903. if (type & PERF_SAMPLE_ID) {
  904. data->id = *array;
  905. array++;
  906. }
  907. if (type & PERF_SAMPLE_STREAM_ID) {
  908. data->stream_id = *array;
  909. array++;
  910. }
  911. if (type & PERF_SAMPLE_CPU) {
  912. u.val64 = *array;
  913. if (swapped) {
  914. /* undo swap of u64, then swap on individual u32s */
  915. u.val64 = bswap_64(u.val64);
  916. u.val32[0] = bswap_32(u.val32[0]);
  917. }
  918. data->cpu = u.val32[0];
  919. array++;
  920. }
  921. if (type & PERF_SAMPLE_PERIOD) {
  922. data->period = *array;
  923. array++;
  924. }
  925. if (type & PERF_SAMPLE_READ) {
  926. u64 read_format = evsel->attr.read_format;
  927. if (read_format & PERF_FORMAT_GROUP)
  928. data->read.group.nr = *array;
  929. else
  930. data->read.one.value = *array;
  931. array++;
  932. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
  933. data->read.time_enabled = *array;
  934. array++;
  935. }
  936. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
  937. data->read.time_running = *array;
  938. array++;
  939. }
  940. /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
  941. if (read_format & PERF_FORMAT_GROUP) {
  942. data->read.group.values = (struct sample_read_value *) array;
  943. array = (void *) array + data->read.group.nr *
  944. sizeof(struct sample_read_value);
  945. } else {
  946. data->read.one.id = *array;
  947. array++;
  948. }
  949. }
  950. if (type & PERF_SAMPLE_CALLCHAIN) {
  951. if (sample_overlap(event, array, sizeof(data->callchain->nr)))
  952. return -EFAULT;
  953. data->callchain = (struct ip_callchain *)array;
  954. if (sample_overlap(event, array, data->callchain->nr))
  955. return -EFAULT;
  956. array += 1 + data->callchain->nr;
  957. }
  958. if (type & PERF_SAMPLE_RAW) {
  959. const u64 *pdata;
  960. u.val64 = *array;
  961. if (WARN_ONCE(swapped,
  962. "Endianness of raw data not corrected!\n")) {
  963. /* undo swap of u64, then swap on individual u32s */
  964. u.val64 = bswap_64(u.val64);
  965. u.val32[0] = bswap_32(u.val32[0]);
  966. u.val32[1] = bswap_32(u.val32[1]);
  967. }
  968. if (sample_overlap(event, array, sizeof(u32)))
  969. return -EFAULT;
  970. data->raw_size = u.val32[0];
  971. pdata = (void *) array + sizeof(u32);
  972. if (sample_overlap(event, pdata, data->raw_size))
  973. return -EFAULT;
  974. data->raw_data = (void *) pdata;
  975. array = (void *)array + data->raw_size + sizeof(u32);
  976. }
  977. if (type & PERF_SAMPLE_BRANCH_STACK) {
  978. u64 sz;
  979. data->branch_stack = (struct branch_stack *)array;
  980. array++; /* nr */
  981. sz = data->branch_stack->nr * sizeof(struct branch_entry);
  982. sz /= sizeof(u64);
  983. array += sz;
  984. }
  985. if (type & PERF_SAMPLE_REGS_USER) {
  986. /* First u64 tells us if we have any regs in sample. */
  987. u64 avail = *array++;
  988. if (avail) {
  989. data->user_regs.regs = (u64 *)array;
  990. array += hweight_long(regs_user);
  991. }
  992. }
  993. if (type & PERF_SAMPLE_STACK_USER) {
  994. u64 size = *array++;
  995. data->user_stack.offset = ((char *)(array - 1)
  996. - (char *) event);
  997. if (!size) {
  998. data->user_stack.size = 0;
  999. } else {
  1000. data->user_stack.data = (char *)array;
  1001. array += size / sizeof(*array);
  1002. data->user_stack.size = *array++;
  1003. }
  1004. }
  1005. data->weight = 0;
  1006. if (type & PERF_SAMPLE_WEIGHT) {
  1007. data->weight = *array;
  1008. array++;
  1009. }
  1010. data->data_src = PERF_MEM_DATA_SRC_NONE;
  1011. if (type & PERF_SAMPLE_DATA_SRC) {
  1012. data->data_src = *array;
  1013. array++;
  1014. }
  1015. return 0;
  1016. }
  1017. int perf_event__synthesize_sample(union perf_event *event, u64 type,
  1018. const struct perf_sample *sample,
  1019. bool swapped)
  1020. {
  1021. u64 *array;
  1022. /*
  1023. * used for cross-endian analysis. See git commit 65014ab3
  1024. * for why this goofiness is needed.
  1025. */
  1026. union u64_swap u;
  1027. array = event->sample.array;
  1028. if (type & PERF_SAMPLE_IP) {
  1029. event->ip.ip = sample->ip;
  1030. array++;
  1031. }
  1032. if (type & PERF_SAMPLE_TID) {
  1033. u.val32[0] = sample->pid;
  1034. u.val32[1] = sample->tid;
  1035. if (swapped) {
  1036. /*
  1037. * Inverse of what is done in perf_evsel__parse_sample
  1038. */
  1039. u.val32[0] = bswap_32(u.val32[0]);
  1040. u.val32[1] = bswap_32(u.val32[1]);
  1041. u.val64 = bswap_64(u.val64);
  1042. }
  1043. *array = u.val64;
  1044. array++;
  1045. }
  1046. if (type & PERF_SAMPLE_TIME) {
  1047. *array = sample->time;
  1048. array++;
  1049. }
  1050. if (type & PERF_SAMPLE_ADDR) {
  1051. *array = sample->addr;
  1052. array++;
  1053. }
  1054. if (type & PERF_SAMPLE_ID) {
  1055. *array = sample->id;
  1056. array++;
  1057. }
  1058. if (type & PERF_SAMPLE_STREAM_ID) {
  1059. *array = sample->stream_id;
  1060. array++;
  1061. }
  1062. if (type & PERF_SAMPLE_CPU) {
  1063. u.val32[0] = sample->cpu;
  1064. if (swapped) {
  1065. /*
  1066. * Inverse of what is done in perf_evsel__parse_sample
  1067. */
  1068. u.val32[0] = bswap_32(u.val32[0]);
  1069. u.val64 = bswap_64(u.val64);
  1070. }
  1071. *array = u.val64;
  1072. array++;
  1073. }
  1074. if (type & PERF_SAMPLE_PERIOD) {
  1075. *array = sample->period;
  1076. array++;
  1077. }
  1078. return 0;
  1079. }
  1080. struct format_field *perf_evsel__field(struct perf_evsel *evsel, const char *name)
  1081. {
  1082. return pevent_find_field(evsel->tp_format, name);
  1083. }
  1084. void *perf_evsel__rawptr(struct perf_evsel *evsel, struct perf_sample *sample,
  1085. const char *name)
  1086. {
  1087. struct format_field *field = perf_evsel__field(evsel, name);
  1088. int offset;
  1089. if (!field)
  1090. return NULL;
  1091. offset = field->offset;
  1092. if (field->flags & FIELD_IS_DYNAMIC) {
  1093. offset = *(int *)(sample->raw_data + field->offset);
  1094. offset &= 0xffff;
  1095. }
  1096. return sample->raw_data + offset;
  1097. }
  1098. u64 perf_evsel__intval(struct perf_evsel *evsel, struct perf_sample *sample,
  1099. const char *name)
  1100. {
  1101. struct format_field *field = perf_evsel__field(evsel, name);
  1102. void *ptr;
  1103. u64 value;
  1104. if (!field)
  1105. return 0;
  1106. ptr = sample->raw_data + field->offset;
  1107. switch (field->size) {
  1108. case 1:
  1109. return *(u8 *)ptr;
  1110. case 2:
  1111. value = *(u16 *)ptr;
  1112. break;
  1113. case 4:
  1114. value = *(u32 *)ptr;
  1115. break;
  1116. case 8:
  1117. value = *(u64 *)ptr;
  1118. break;
  1119. default:
  1120. return 0;
  1121. }
  1122. if (!evsel->needs_swap)
  1123. return value;
  1124. switch (field->size) {
  1125. case 2:
  1126. return bswap_16(value);
  1127. case 4:
  1128. return bswap_32(value);
  1129. case 8:
  1130. return bswap_64(value);
  1131. default:
  1132. return 0;
  1133. }
  1134. return 0;
  1135. }
  1136. static int comma_fprintf(FILE *fp, bool *first, const char *fmt, ...)
  1137. {
  1138. va_list args;
  1139. int ret = 0;
  1140. if (!*first) {
  1141. ret += fprintf(fp, ",");
  1142. } else {
  1143. ret += fprintf(fp, ":");
  1144. *first = false;
  1145. }
  1146. va_start(args, fmt);
  1147. ret += vfprintf(fp, fmt, args);
  1148. va_end(args);
  1149. return ret;
  1150. }
  1151. static int __if_fprintf(FILE *fp, bool *first, const char *field, u64 value)
  1152. {
  1153. if (value == 0)
  1154. return 0;
  1155. return comma_fprintf(fp, first, " %s: %" PRIu64, field, value);
  1156. }
  1157. #define if_print(field) printed += __if_fprintf(fp, &first, #field, evsel->attr.field)
  1158. struct bit_names {
  1159. int bit;
  1160. const char *name;
  1161. };
  1162. static int bits__fprintf(FILE *fp, const char *field, u64 value,
  1163. struct bit_names *bits, bool *first)
  1164. {
  1165. int i = 0, printed = comma_fprintf(fp, first, " %s: ", field);
  1166. bool first_bit = true;
  1167. do {
  1168. if (value & bits[i].bit) {
  1169. printed += fprintf(fp, "%s%s", first_bit ? "" : "|", bits[i].name);
  1170. first_bit = false;
  1171. }
  1172. } while (bits[++i].name != NULL);
  1173. return printed;
  1174. }
  1175. static int sample_type__fprintf(FILE *fp, bool *first, u64 value)
  1176. {
  1177. #define bit_name(n) { PERF_SAMPLE_##n, #n }
  1178. struct bit_names bits[] = {
  1179. bit_name(IP), bit_name(TID), bit_name(TIME), bit_name(ADDR),
  1180. bit_name(READ), bit_name(CALLCHAIN), bit_name(ID), bit_name(CPU),
  1181. bit_name(PERIOD), bit_name(STREAM_ID), bit_name(RAW),
  1182. bit_name(BRANCH_STACK), bit_name(REGS_USER), bit_name(STACK_USER),
  1183. { .name = NULL, }
  1184. };
  1185. #undef bit_name
  1186. return bits__fprintf(fp, "sample_type", value, bits, first);
  1187. }
  1188. static int read_format__fprintf(FILE *fp, bool *first, u64 value)
  1189. {
  1190. #define bit_name(n) { PERF_FORMAT_##n, #n }
  1191. struct bit_names bits[] = {
  1192. bit_name(TOTAL_TIME_ENABLED), bit_name(TOTAL_TIME_RUNNING),
  1193. bit_name(ID), bit_name(GROUP),
  1194. { .name = NULL, }
  1195. };
  1196. #undef bit_name
  1197. return bits__fprintf(fp, "read_format", value, bits, first);
  1198. }
  1199. int perf_evsel__fprintf(struct perf_evsel *evsel,
  1200. struct perf_attr_details *details, FILE *fp)
  1201. {
  1202. bool first = true;
  1203. int printed = 0;
  1204. if (details->event_group) {
  1205. struct perf_evsel *pos;
  1206. if (!perf_evsel__is_group_leader(evsel))
  1207. return 0;
  1208. if (evsel->nr_members > 1)
  1209. printed += fprintf(fp, "%s{", evsel->group_name ?: "");
  1210. printed += fprintf(fp, "%s", perf_evsel__name(evsel));
  1211. for_each_group_member(pos, evsel)
  1212. printed += fprintf(fp, ",%s", perf_evsel__name(pos));
  1213. if (evsel->nr_members > 1)
  1214. printed += fprintf(fp, "}");
  1215. goto out;
  1216. }
  1217. printed += fprintf(fp, "%s", perf_evsel__name(evsel));
  1218. if (details->verbose || details->freq) {
  1219. printed += comma_fprintf(fp, &first, " sample_freq=%" PRIu64,
  1220. (u64)evsel->attr.sample_freq);
  1221. }
  1222. if (details->verbose) {
  1223. if_print(type);
  1224. if_print(config);
  1225. if_print(config1);
  1226. if_print(config2);
  1227. if_print(size);
  1228. printed += sample_type__fprintf(fp, &first, evsel->attr.sample_type);
  1229. if (evsel->attr.read_format)
  1230. printed += read_format__fprintf(fp, &first, evsel->attr.read_format);
  1231. if_print(disabled);
  1232. if_print(inherit);
  1233. if_print(pinned);
  1234. if_print(exclusive);
  1235. if_print(exclude_user);
  1236. if_print(exclude_kernel);
  1237. if_print(exclude_hv);
  1238. if_print(exclude_idle);
  1239. if_print(mmap);
  1240. if_print(comm);
  1241. if_print(freq);
  1242. if_print(inherit_stat);
  1243. if_print(enable_on_exec);
  1244. if_print(task);
  1245. if_print(watermark);
  1246. if_print(precise_ip);
  1247. if_print(mmap_data);
  1248. if_print(sample_id_all);
  1249. if_print(exclude_host);
  1250. if_print(exclude_guest);
  1251. if_print(__reserved_1);
  1252. if_print(wakeup_events);
  1253. if_print(bp_type);
  1254. if_print(branch_sample_type);
  1255. }
  1256. out:
  1257. fputc('\n', fp);
  1258. return ++printed;
  1259. }
  1260. bool perf_evsel__fallback(struct perf_evsel *evsel, int err,
  1261. char *msg, size_t msgsize)
  1262. {
  1263. if ((err == ENOENT || err == ENXIO || err == ENODEV) &&
  1264. evsel->attr.type == PERF_TYPE_HARDWARE &&
  1265. evsel->attr.config == PERF_COUNT_HW_CPU_CYCLES) {
  1266. /*
  1267. * If it's cycles then fall back to hrtimer based
  1268. * cpu-clock-tick sw counter, which is always available even if
  1269. * no PMU support.
  1270. *
  1271. * PPC returns ENXIO until 2.6.37 (behavior changed with commit
  1272. * b0a873e).
  1273. */
  1274. scnprintf(msg, msgsize, "%s",
  1275. "The cycles event is not supported, trying to fall back to cpu-clock-ticks");
  1276. evsel->attr.type = PERF_TYPE_SOFTWARE;
  1277. evsel->attr.config = PERF_COUNT_SW_CPU_CLOCK;
  1278. free(evsel->name);
  1279. evsel->name = NULL;
  1280. return true;
  1281. }
  1282. return false;
  1283. }
  1284. int perf_evsel__open_strerror(struct perf_evsel *evsel,
  1285. struct perf_target *target,
  1286. int err, char *msg, size_t size)
  1287. {
  1288. switch (err) {
  1289. case EPERM:
  1290. case EACCES:
  1291. return scnprintf(msg, size,
  1292. "You may not have permission to collect %sstats.\n"
  1293. "Consider tweaking /proc/sys/kernel/perf_event_paranoid:\n"
  1294. " -1 - Not paranoid at all\n"
  1295. " 0 - Disallow raw tracepoint access for unpriv\n"
  1296. " 1 - Disallow cpu events for unpriv\n"
  1297. " 2 - Disallow kernel profiling for unpriv",
  1298. target->system_wide ? "system-wide " : "");
  1299. case ENOENT:
  1300. return scnprintf(msg, size, "The %s event is not supported.",
  1301. perf_evsel__name(evsel));
  1302. case EMFILE:
  1303. return scnprintf(msg, size, "%s",
  1304. "Too many events are opened.\n"
  1305. "Try again after reducing the number of events.");
  1306. case ENODEV:
  1307. if (target->cpu_list)
  1308. return scnprintf(msg, size, "%s",
  1309. "No such device - did you specify an out-of-range profile CPU?\n");
  1310. break;
  1311. case EOPNOTSUPP:
  1312. if (evsel->attr.precise_ip)
  1313. return scnprintf(msg, size, "%s",
  1314. "\'precise\' request may not be supported. Try removing 'p' modifier.");
  1315. #if defined(__i386__) || defined(__x86_64__)
  1316. if (evsel->attr.type == PERF_TYPE_HARDWARE)
  1317. return scnprintf(msg, size, "%s",
  1318. "No hardware sampling interrupt available.\n"
  1319. "No APIC? If so then you can boot the kernel with the \"lapic\" boot parameter to force-enable it.");
  1320. #endif
  1321. break;
  1322. default:
  1323. break;
  1324. }
  1325. return scnprintf(msg, size,
  1326. "The sys_perf_event_open() syscall returned with %d (%s) for event (%s). \n"
  1327. "/bin/dmesg may provide additional information.\n"
  1328. "No CONFIG_PERF_EVENTS=y kernel support configured?\n",
  1329. err, strerror(err), perf_evsel__name(evsel));
  1330. }