evsel.c 35 KB

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