evsel.c 35 KB

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