evsel.c 36 KB

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