evsel.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408
  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. /*
  362. * The enable_on_exec/disabled value strategy:
  363. *
  364. * 1) For any type of traced program:
  365. * - all independent events and group leaders are disabled
  366. * - all group members are enabled
  367. *
  368. * Group members are ruled by group leaders. They need to
  369. * be enabled, because the group scheduling relies on that.
  370. *
  371. * 2) For traced programs executed by perf:
  372. * - all independent events and group leaders have
  373. * enable_on_exec set
  374. * - we don't specifically enable or disable any event during
  375. * the record command
  376. *
  377. * Independent events and group leaders are initially disabled
  378. * and get enabled by exec. Group members are ruled by group
  379. * leaders as stated in 1).
  380. *
  381. * 3) For traced programs attached by perf (pid/tid):
  382. * - we specifically enable or disable all events during
  383. * the record command
  384. *
  385. * When attaching events to already running traced we
  386. * enable/disable events specifically, as there's no
  387. * initial traced exec call.
  388. */
  389. void perf_evsel__config(struct perf_evsel *evsel,
  390. struct perf_record_opts *opts)
  391. {
  392. struct perf_event_attr *attr = &evsel->attr;
  393. int track = !evsel->idx; /* only the first counter needs these */
  394. attr->sample_id_all = perf_missing_features.sample_id_all ? 0 : 1;
  395. attr->inherit = !opts->no_inherit;
  396. perf_evsel__set_sample_bit(evsel, IP);
  397. perf_evsel__set_sample_bit(evsel, TID);
  398. /*
  399. * We default some events to a 1 default interval. But keep
  400. * it a weak assumption overridable by the user.
  401. */
  402. if (!attr->sample_period || (opts->user_freq != UINT_MAX &&
  403. opts->user_interval != ULLONG_MAX)) {
  404. if (opts->freq) {
  405. perf_evsel__set_sample_bit(evsel, PERIOD);
  406. attr->freq = 1;
  407. attr->sample_freq = opts->freq;
  408. } else {
  409. attr->sample_period = opts->default_interval;
  410. }
  411. }
  412. if (opts->no_samples)
  413. attr->sample_freq = 0;
  414. if (opts->inherit_stat)
  415. attr->inherit_stat = 1;
  416. if (opts->sample_address) {
  417. perf_evsel__set_sample_bit(evsel, ADDR);
  418. attr->mmap_data = track;
  419. }
  420. if (opts->call_graph) {
  421. perf_evsel__set_sample_bit(evsel, CALLCHAIN);
  422. if (opts->call_graph == CALLCHAIN_DWARF) {
  423. perf_evsel__set_sample_bit(evsel, REGS_USER);
  424. perf_evsel__set_sample_bit(evsel, STACK_USER);
  425. attr->sample_regs_user = PERF_REGS_MASK;
  426. attr->sample_stack_user = opts->stack_dump_size;
  427. attr->exclude_callchain_user = 1;
  428. }
  429. }
  430. if (perf_target__has_cpu(&opts->target))
  431. perf_evsel__set_sample_bit(evsel, CPU);
  432. if (opts->period)
  433. perf_evsel__set_sample_bit(evsel, PERIOD);
  434. if (!perf_missing_features.sample_id_all &&
  435. (opts->sample_time || !opts->no_inherit ||
  436. perf_target__has_cpu(&opts->target)))
  437. perf_evsel__set_sample_bit(evsel, TIME);
  438. if (opts->raw_samples) {
  439. perf_evsel__set_sample_bit(evsel, TIME);
  440. perf_evsel__set_sample_bit(evsel, RAW);
  441. perf_evsel__set_sample_bit(evsel, CPU);
  442. }
  443. if (opts->no_delay) {
  444. attr->watermark = 0;
  445. attr->wakeup_events = 1;
  446. }
  447. if (opts->branch_stack) {
  448. perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
  449. attr->branch_sample_type = opts->branch_stack;
  450. }
  451. attr->mmap = track;
  452. attr->comm = track;
  453. /*
  454. * XXX see the function comment above
  455. *
  456. * Disabling only independent events or group leaders,
  457. * keeping group members enabled.
  458. */
  459. if (perf_evsel__is_group_leader(evsel))
  460. attr->disabled = 1;
  461. /*
  462. * Setting enable_on_exec for independent events and
  463. * group leaders for traced executed by perf.
  464. */
  465. if (perf_target__none(&opts->target) && perf_evsel__is_group_leader(evsel))
  466. attr->enable_on_exec = 1;
  467. }
  468. int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
  469. {
  470. int cpu, thread;
  471. evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
  472. if (evsel->fd) {
  473. for (cpu = 0; cpu < ncpus; cpu++) {
  474. for (thread = 0; thread < nthreads; thread++) {
  475. FD(evsel, cpu, thread) = -1;
  476. }
  477. }
  478. }
  479. return evsel->fd != NULL ? 0 : -ENOMEM;
  480. }
  481. int perf_evsel__set_filter(struct perf_evsel *evsel, int ncpus, int nthreads,
  482. const char *filter)
  483. {
  484. int cpu, thread;
  485. for (cpu = 0; cpu < ncpus; cpu++) {
  486. for (thread = 0; thread < nthreads; thread++) {
  487. int fd = FD(evsel, cpu, thread),
  488. err = ioctl(fd, PERF_EVENT_IOC_SET_FILTER, filter);
  489. if (err)
  490. return err;
  491. }
  492. }
  493. return 0;
  494. }
  495. int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
  496. {
  497. evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
  498. if (evsel->sample_id == NULL)
  499. return -ENOMEM;
  500. evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
  501. if (evsel->id == NULL) {
  502. xyarray__delete(evsel->sample_id);
  503. evsel->sample_id = NULL;
  504. return -ENOMEM;
  505. }
  506. return 0;
  507. }
  508. int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
  509. {
  510. evsel->counts = zalloc((sizeof(*evsel->counts) +
  511. (ncpus * sizeof(struct perf_counts_values))));
  512. return evsel->counts != NULL ? 0 : -ENOMEM;
  513. }
  514. void perf_evsel__free_fd(struct perf_evsel *evsel)
  515. {
  516. xyarray__delete(evsel->fd);
  517. evsel->fd = NULL;
  518. }
  519. void perf_evsel__free_id(struct perf_evsel *evsel)
  520. {
  521. xyarray__delete(evsel->sample_id);
  522. evsel->sample_id = NULL;
  523. free(evsel->id);
  524. evsel->id = NULL;
  525. }
  526. void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
  527. {
  528. int cpu, thread;
  529. for (cpu = 0; cpu < ncpus; cpu++)
  530. for (thread = 0; thread < nthreads; ++thread) {
  531. close(FD(evsel, cpu, thread));
  532. FD(evsel, cpu, thread) = -1;
  533. }
  534. }
  535. void perf_evsel__exit(struct perf_evsel *evsel)
  536. {
  537. assert(list_empty(&evsel->node));
  538. xyarray__delete(evsel->fd);
  539. xyarray__delete(evsel->sample_id);
  540. free(evsel->id);
  541. }
  542. void perf_evsel__delete(struct perf_evsel *evsel)
  543. {
  544. perf_evsel__exit(evsel);
  545. close_cgroup(evsel->cgrp);
  546. free(evsel->group_name);
  547. if (evsel->tp_format)
  548. pevent_free_format(evsel->tp_format);
  549. free(evsel->name);
  550. free(evsel);
  551. }
  552. int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
  553. int cpu, int thread, bool scale)
  554. {
  555. struct perf_counts_values count;
  556. size_t nv = scale ? 3 : 1;
  557. if (FD(evsel, cpu, thread) < 0)
  558. return -EINVAL;
  559. if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
  560. return -ENOMEM;
  561. if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
  562. return -errno;
  563. if (scale) {
  564. if (count.run == 0)
  565. count.val = 0;
  566. else if (count.run < count.ena)
  567. count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
  568. } else
  569. count.ena = count.run = 0;
  570. evsel->counts->cpu[cpu] = count;
  571. return 0;
  572. }
  573. int __perf_evsel__read(struct perf_evsel *evsel,
  574. int ncpus, int nthreads, bool scale)
  575. {
  576. size_t nv = scale ? 3 : 1;
  577. int cpu, thread;
  578. struct perf_counts_values *aggr = &evsel->counts->aggr, count;
  579. aggr->val = aggr->ena = aggr->run = 0;
  580. for (cpu = 0; cpu < ncpus; cpu++) {
  581. for (thread = 0; thread < nthreads; thread++) {
  582. if (FD(evsel, cpu, thread) < 0)
  583. continue;
  584. if (readn(FD(evsel, cpu, thread),
  585. &count, nv * sizeof(u64)) < 0)
  586. return -errno;
  587. aggr->val += count.val;
  588. if (scale) {
  589. aggr->ena += count.ena;
  590. aggr->run += count.run;
  591. }
  592. }
  593. }
  594. evsel->counts->scaled = 0;
  595. if (scale) {
  596. if (aggr->run == 0) {
  597. evsel->counts->scaled = -1;
  598. aggr->val = 0;
  599. return 0;
  600. }
  601. if (aggr->run < aggr->ena) {
  602. evsel->counts->scaled = 1;
  603. aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
  604. }
  605. } else
  606. aggr->ena = aggr->run = 0;
  607. return 0;
  608. }
  609. static int get_group_fd(struct perf_evsel *evsel, int cpu, int thread)
  610. {
  611. struct perf_evsel *leader = evsel->leader;
  612. int fd;
  613. if (perf_evsel__is_group_leader(evsel))
  614. return -1;
  615. /*
  616. * Leader must be already processed/open,
  617. * if not it's a bug.
  618. */
  619. BUG_ON(!leader->fd);
  620. fd = FD(leader, cpu, thread);
  621. BUG_ON(fd == -1);
  622. return fd;
  623. }
  624. static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
  625. struct thread_map *threads)
  626. {
  627. int cpu, thread;
  628. unsigned long flags = 0;
  629. int pid = -1, err;
  630. if (evsel->fd == NULL &&
  631. perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
  632. return -ENOMEM;
  633. if (evsel->cgrp) {
  634. flags = PERF_FLAG_PID_CGROUP;
  635. pid = evsel->cgrp->fd;
  636. }
  637. fallback_missing_features:
  638. if (perf_missing_features.exclude_guest)
  639. evsel->attr.exclude_guest = evsel->attr.exclude_host = 0;
  640. retry_sample_id:
  641. if (perf_missing_features.sample_id_all)
  642. evsel->attr.sample_id_all = 0;
  643. for (cpu = 0; cpu < cpus->nr; cpu++) {
  644. for (thread = 0; thread < threads->nr; thread++) {
  645. int group_fd;
  646. if (!evsel->cgrp)
  647. pid = threads->map[thread];
  648. group_fd = get_group_fd(evsel, cpu, thread);
  649. FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
  650. pid,
  651. cpus->map[cpu],
  652. group_fd, flags);
  653. if (FD(evsel, cpu, thread) < 0) {
  654. err = -errno;
  655. goto try_fallback;
  656. }
  657. }
  658. }
  659. return 0;
  660. try_fallback:
  661. if (err != -EINVAL || cpu > 0 || thread > 0)
  662. goto out_close;
  663. if (!perf_missing_features.exclude_guest &&
  664. (evsel->attr.exclude_guest || evsel->attr.exclude_host)) {
  665. perf_missing_features.exclude_guest = true;
  666. goto fallback_missing_features;
  667. } else if (!perf_missing_features.sample_id_all) {
  668. perf_missing_features.sample_id_all = true;
  669. goto retry_sample_id;
  670. }
  671. out_close:
  672. do {
  673. while (--thread >= 0) {
  674. close(FD(evsel, cpu, thread));
  675. FD(evsel, cpu, thread) = -1;
  676. }
  677. thread = threads->nr;
  678. } while (--cpu >= 0);
  679. return err;
  680. }
  681. void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
  682. {
  683. if (evsel->fd == NULL)
  684. return;
  685. perf_evsel__close_fd(evsel, ncpus, nthreads);
  686. perf_evsel__free_fd(evsel);
  687. evsel->fd = NULL;
  688. }
  689. static struct {
  690. struct cpu_map map;
  691. int cpus[1];
  692. } empty_cpu_map = {
  693. .map.nr = 1,
  694. .cpus = { -1, },
  695. };
  696. static struct {
  697. struct thread_map map;
  698. int threads[1];
  699. } empty_thread_map = {
  700. .map.nr = 1,
  701. .threads = { -1, },
  702. };
  703. int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
  704. struct thread_map *threads)
  705. {
  706. if (cpus == NULL) {
  707. /* Work around old compiler warnings about strict aliasing */
  708. cpus = &empty_cpu_map.map;
  709. }
  710. if (threads == NULL)
  711. threads = &empty_thread_map.map;
  712. return __perf_evsel__open(evsel, cpus, threads);
  713. }
  714. int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
  715. struct cpu_map *cpus)
  716. {
  717. return __perf_evsel__open(evsel, cpus, &empty_thread_map.map);
  718. }
  719. int perf_evsel__open_per_thread(struct perf_evsel *evsel,
  720. struct thread_map *threads)
  721. {
  722. return __perf_evsel__open(evsel, &empty_cpu_map.map, threads);
  723. }
  724. static int perf_evsel__parse_id_sample(const struct perf_evsel *evsel,
  725. const union perf_event *event,
  726. struct perf_sample *sample)
  727. {
  728. u64 type = evsel->attr.sample_type;
  729. const u64 *array = event->sample.array;
  730. bool swapped = evsel->needs_swap;
  731. union u64_swap u;
  732. array += ((event->header.size -
  733. sizeof(event->header)) / sizeof(u64)) - 1;
  734. if (type & PERF_SAMPLE_CPU) {
  735. u.val64 = *array;
  736. if (swapped) {
  737. /* undo swap of u64, then swap on individual u32s */
  738. u.val64 = bswap_64(u.val64);
  739. u.val32[0] = bswap_32(u.val32[0]);
  740. }
  741. sample->cpu = u.val32[0];
  742. array--;
  743. }
  744. if (type & PERF_SAMPLE_STREAM_ID) {
  745. sample->stream_id = *array;
  746. array--;
  747. }
  748. if (type & PERF_SAMPLE_ID) {
  749. sample->id = *array;
  750. array--;
  751. }
  752. if (type & PERF_SAMPLE_TIME) {
  753. sample->time = *array;
  754. array--;
  755. }
  756. if (type & PERF_SAMPLE_TID) {
  757. u.val64 = *array;
  758. if (swapped) {
  759. /* undo swap of u64, then swap on individual u32s */
  760. u.val64 = bswap_64(u.val64);
  761. u.val32[0] = bswap_32(u.val32[0]);
  762. u.val32[1] = bswap_32(u.val32[1]);
  763. }
  764. sample->pid = u.val32[0];
  765. sample->tid = u.val32[1];
  766. }
  767. return 0;
  768. }
  769. static bool sample_overlap(const union perf_event *event,
  770. const void *offset, u64 size)
  771. {
  772. const void *base = event;
  773. if (offset + size > base + event->header.size)
  774. return true;
  775. return false;
  776. }
  777. int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
  778. struct perf_sample *data)
  779. {
  780. u64 type = evsel->attr.sample_type;
  781. u64 regs_user = evsel->attr.sample_regs_user;
  782. bool swapped = evsel->needs_swap;
  783. const u64 *array;
  784. /*
  785. * used for cross-endian analysis. See git commit 65014ab3
  786. * for why this goofiness is needed.
  787. */
  788. union u64_swap u;
  789. memset(data, 0, sizeof(*data));
  790. data->cpu = data->pid = data->tid = -1;
  791. data->stream_id = data->id = data->time = -1ULL;
  792. data->period = 1;
  793. if (event->header.type != PERF_RECORD_SAMPLE) {
  794. if (!evsel->attr.sample_id_all)
  795. return 0;
  796. return perf_evsel__parse_id_sample(evsel, event, data);
  797. }
  798. array = event->sample.array;
  799. if (evsel->sample_size + sizeof(event->header) > event->header.size)
  800. return -EFAULT;
  801. if (type & PERF_SAMPLE_IP) {
  802. data->ip = event->ip.ip;
  803. array++;
  804. }
  805. if (type & PERF_SAMPLE_TID) {
  806. u.val64 = *array;
  807. if (swapped) {
  808. /* undo swap of u64, then swap on individual u32s */
  809. u.val64 = bswap_64(u.val64);
  810. u.val32[0] = bswap_32(u.val32[0]);
  811. u.val32[1] = bswap_32(u.val32[1]);
  812. }
  813. data->pid = u.val32[0];
  814. data->tid = u.val32[1];
  815. array++;
  816. }
  817. if (type & PERF_SAMPLE_TIME) {
  818. data->time = *array;
  819. array++;
  820. }
  821. data->addr = 0;
  822. if (type & PERF_SAMPLE_ADDR) {
  823. data->addr = *array;
  824. array++;
  825. }
  826. data->id = -1ULL;
  827. if (type & PERF_SAMPLE_ID) {
  828. data->id = *array;
  829. array++;
  830. }
  831. if (type & PERF_SAMPLE_STREAM_ID) {
  832. data->stream_id = *array;
  833. array++;
  834. }
  835. if (type & PERF_SAMPLE_CPU) {
  836. u.val64 = *array;
  837. if (swapped) {
  838. /* undo swap of u64, then swap on individual u32s */
  839. u.val64 = bswap_64(u.val64);
  840. u.val32[0] = bswap_32(u.val32[0]);
  841. }
  842. data->cpu = u.val32[0];
  843. array++;
  844. }
  845. if (type & PERF_SAMPLE_PERIOD) {
  846. data->period = *array;
  847. array++;
  848. }
  849. if (type & PERF_SAMPLE_READ) {
  850. fprintf(stderr, "PERF_SAMPLE_READ is unsupported for now\n");
  851. return -1;
  852. }
  853. if (type & PERF_SAMPLE_CALLCHAIN) {
  854. if (sample_overlap(event, array, sizeof(data->callchain->nr)))
  855. return -EFAULT;
  856. data->callchain = (struct ip_callchain *)array;
  857. if (sample_overlap(event, array, data->callchain->nr))
  858. return -EFAULT;
  859. array += 1 + data->callchain->nr;
  860. }
  861. if (type & PERF_SAMPLE_RAW) {
  862. const u64 *pdata;
  863. u.val64 = *array;
  864. if (WARN_ONCE(swapped,
  865. "Endianness of raw data not corrected!\n")) {
  866. /* undo swap of u64, then swap on individual u32s */
  867. u.val64 = bswap_64(u.val64);
  868. u.val32[0] = bswap_32(u.val32[0]);
  869. u.val32[1] = bswap_32(u.val32[1]);
  870. }
  871. if (sample_overlap(event, array, sizeof(u32)))
  872. return -EFAULT;
  873. data->raw_size = u.val32[0];
  874. pdata = (void *) array + sizeof(u32);
  875. if (sample_overlap(event, pdata, data->raw_size))
  876. return -EFAULT;
  877. data->raw_data = (void *) pdata;
  878. array = (void *)array + data->raw_size + sizeof(u32);
  879. }
  880. if (type & PERF_SAMPLE_BRANCH_STACK) {
  881. u64 sz;
  882. data->branch_stack = (struct branch_stack *)array;
  883. array++; /* nr */
  884. sz = data->branch_stack->nr * sizeof(struct branch_entry);
  885. sz /= sizeof(u64);
  886. array += sz;
  887. }
  888. if (type & PERF_SAMPLE_REGS_USER) {
  889. /* First u64 tells us if we have any regs in sample. */
  890. u64 avail = *array++;
  891. if (avail) {
  892. data->user_regs.regs = (u64 *)array;
  893. array += hweight_long(regs_user);
  894. }
  895. }
  896. if (type & PERF_SAMPLE_STACK_USER) {
  897. u64 size = *array++;
  898. data->user_stack.offset = ((char *)(array - 1)
  899. - (char *) event);
  900. if (!size) {
  901. data->user_stack.size = 0;
  902. } else {
  903. data->user_stack.data = (char *)array;
  904. array += size / sizeof(*array);
  905. data->user_stack.size = *array;
  906. }
  907. }
  908. return 0;
  909. }
  910. int perf_event__synthesize_sample(union perf_event *event, u64 type,
  911. const struct perf_sample *sample,
  912. bool swapped)
  913. {
  914. u64 *array;
  915. /*
  916. * used for cross-endian analysis. See git commit 65014ab3
  917. * for why this goofiness is needed.
  918. */
  919. union u64_swap u;
  920. array = event->sample.array;
  921. if (type & PERF_SAMPLE_IP) {
  922. event->ip.ip = sample->ip;
  923. array++;
  924. }
  925. if (type & PERF_SAMPLE_TID) {
  926. u.val32[0] = sample->pid;
  927. u.val32[1] = sample->tid;
  928. if (swapped) {
  929. /*
  930. * Inverse of what is done in perf_evsel__parse_sample
  931. */
  932. u.val32[0] = bswap_32(u.val32[0]);
  933. u.val32[1] = bswap_32(u.val32[1]);
  934. u.val64 = bswap_64(u.val64);
  935. }
  936. *array = u.val64;
  937. array++;
  938. }
  939. if (type & PERF_SAMPLE_TIME) {
  940. *array = sample->time;
  941. array++;
  942. }
  943. if (type & PERF_SAMPLE_ADDR) {
  944. *array = sample->addr;
  945. array++;
  946. }
  947. if (type & PERF_SAMPLE_ID) {
  948. *array = sample->id;
  949. array++;
  950. }
  951. if (type & PERF_SAMPLE_STREAM_ID) {
  952. *array = sample->stream_id;
  953. array++;
  954. }
  955. if (type & PERF_SAMPLE_CPU) {
  956. u.val32[0] = sample->cpu;
  957. if (swapped) {
  958. /*
  959. * Inverse of what is done in perf_evsel__parse_sample
  960. */
  961. u.val32[0] = bswap_32(u.val32[0]);
  962. u.val64 = bswap_64(u.val64);
  963. }
  964. *array = u.val64;
  965. array++;
  966. }
  967. if (type & PERF_SAMPLE_PERIOD) {
  968. *array = sample->period;
  969. array++;
  970. }
  971. return 0;
  972. }
  973. struct format_field *perf_evsel__field(struct perf_evsel *evsel, const char *name)
  974. {
  975. return pevent_find_field(evsel->tp_format, name);
  976. }
  977. void *perf_evsel__rawptr(struct perf_evsel *evsel, struct perf_sample *sample,
  978. const char *name)
  979. {
  980. struct format_field *field = perf_evsel__field(evsel, name);
  981. int offset;
  982. if (!field)
  983. return NULL;
  984. offset = field->offset;
  985. if (field->flags & FIELD_IS_DYNAMIC) {
  986. offset = *(int *)(sample->raw_data + field->offset);
  987. offset &= 0xffff;
  988. }
  989. return sample->raw_data + offset;
  990. }
  991. u64 perf_evsel__intval(struct perf_evsel *evsel, struct perf_sample *sample,
  992. const char *name)
  993. {
  994. struct format_field *field = perf_evsel__field(evsel, name);
  995. void *ptr;
  996. u64 value;
  997. if (!field)
  998. return 0;
  999. ptr = sample->raw_data + field->offset;
  1000. switch (field->size) {
  1001. case 1:
  1002. return *(u8 *)ptr;
  1003. case 2:
  1004. value = *(u16 *)ptr;
  1005. break;
  1006. case 4:
  1007. value = *(u32 *)ptr;
  1008. break;
  1009. case 8:
  1010. value = *(u64 *)ptr;
  1011. break;
  1012. default:
  1013. return 0;
  1014. }
  1015. if (!evsel->needs_swap)
  1016. return value;
  1017. switch (field->size) {
  1018. case 2:
  1019. return bswap_16(value);
  1020. case 4:
  1021. return bswap_32(value);
  1022. case 8:
  1023. return bswap_64(value);
  1024. default:
  1025. return 0;
  1026. }
  1027. return 0;
  1028. }
  1029. static int comma_fprintf(FILE *fp, bool *first, const char *fmt, ...)
  1030. {
  1031. va_list args;
  1032. int ret = 0;
  1033. if (!*first) {
  1034. ret += fprintf(fp, ",");
  1035. } else {
  1036. ret += fprintf(fp, ":");
  1037. *first = false;
  1038. }
  1039. va_start(args, fmt);
  1040. ret += vfprintf(fp, fmt, args);
  1041. va_end(args);
  1042. return ret;
  1043. }
  1044. static int __if_fprintf(FILE *fp, bool *first, const char *field, u64 value)
  1045. {
  1046. if (value == 0)
  1047. return 0;
  1048. return comma_fprintf(fp, first, " %s: %" PRIu64, field, value);
  1049. }
  1050. #define if_print(field) printed += __if_fprintf(fp, &first, #field, evsel->attr.field)
  1051. struct bit_names {
  1052. int bit;
  1053. const char *name;
  1054. };
  1055. static int bits__fprintf(FILE *fp, const char *field, u64 value,
  1056. struct bit_names *bits, bool *first)
  1057. {
  1058. int i = 0, printed = comma_fprintf(fp, first, " %s: ", field);
  1059. bool first_bit = true;
  1060. do {
  1061. if (value & bits[i].bit) {
  1062. printed += fprintf(fp, "%s%s", first_bit ? "" : "|", bits[i].name);
  1063. first_bit = false;
  1064. }
  1065. } while (bits[++i].name != NULL);
  1066. return printed;
  1067. }
  1068. static int sample_type__fprintf(FILE *fp, bool *first, u64 value)
  1069. {
  1070. #define bit_name(n) { PERF_SAMPLE_##n, #n }
  1071. struct bit_names bits[] = {
  1072. bit_name(IP), bit_name(TID), bit_name(TIME), bit_name(ADDR),
  1073. bit_name(READ), bit_name(CALLCHAIN), bit_name(ID), bit_name(CPU),
  1074. bit_name(PERIOD), bit_name(STREAM_ID), bit_name(RAW),
  1075. bit_name(BRANCH_STACK), bit_name(REGS_USER), bit_name(STACK_USER),
  1076. { .name = NULL, }
  1077. };
  1078. #undef bit_name
  1079. return bits__fprintf(fp, "sample_type", value, bits, first);
  1080. }
  1081. static int read_format__fprintf(FILE *fp, bool *first, u64 value)
  1082. {
  1083. #define bit_name(n) { PERF_FORMAT_##n, #n }
  1084. struct bit_names bits[] = {
  1085. bit_name(TOTAL_TIME_ENABLED), bit_name(TOTAL_TIME_RUNNING),
  1086. bit_name(ID), bit_name(GROUP),
  1087. { .name = NULL, }
  1088. };
  1089. #undef bit_name
  1090. return bits__fprintf(fp, "read_format", value, bits, first);
  1091. }
  1092. int perf_evsel__fprintf(struct perf_evsel *evsel,
  1093. struct perf_attr_details *details, FILE *fp)
  1094. {
  1095. bool first = true;
  1096. int printed = fprintf(fp, "%s", perf_evsel__name(evsel));
  1097. if (details->verbose || details->freq) {
  1098. printed += comma_fprintf(fp, &first, " sample_freq=%" PRIu64,
  1099. (u64)evsel->attr.sample_freq);
  1100. }
  1101. if (details->verbose) {
  1102. if_print(type);
  1103. if_print(config);
  1104. if_print(config1);
  1105. if_print(config2);
  1106. if_print(size);
  1107. printed += sample_type__fprintf(fp, &first, evsel->attr.sample_type);
  1108. if (evsel->attr.read_format)
  1109. printed += read_format__fprintf(fp, &first, evsel->attr.read_format);
  1110. if_print(disabled);
  1111. if_print(inherit);
  1112. if_print(pinned);
  1113. if_print(exclusive);
  1114. if_print(exclude_user);
  1115. if_print(exclude_kernel);
  1116. if_print(exclude_hv);
  1117. if_print(exclude_idle);
  1118. if_print(mmap);
  1119. if_print(comm);
  1120. if_print(freq);
  1121. if_print(inherit_stat);
  1122. if_print(enable_on_exec);
  1123. if_print(task);
  1124. if_print(watermark);
  1125. if_print(precise_ip);
  1126. if_print(mmap_data);
  1127. if_print(sample_id_all);
  1128. if_print(exclude_host);
  1129. if_print(exclude_guest);
  1130. if_print(__reserved_1);
  1131. if_print(wakeup_events);
  1132. if_print(bp_type);
  1133. if_print(branch_sample_type);
  1134. }
  1135. fputc('\n', fp);
  1136. return ++printed;
  1137. }
  1138. bool perf_evsel__fallback(struct perf_evsel *evsel, int err,
  1139. char *msg, size_t msgsize)
  1140. {
  1141. if ((err == ENOENT || err == ENXIO) &&
  1142. evsel->attr.type == PERF_TYPE_HARDWARE &&
  1143. evsel->attr.config == PERF_COUNT_HW_CPU_CYCLES) {
  1144. /*
  1145. * If it's cycles then fall back to hrtimer based
  1146. * cpu-clock-tick sw counter, which is always available even if
  1147. * no PMU support.
  1148. *
  1149. * PPC returns ENXIO until 2.6.37 (behavior changed with commit
  1150. * b0a873e).
  1151. */
  1152. scnprintf(msg, msgsize, "%s",
  1153. "The cycles event is not supported, trying to fall back to cpu-clock-ticks");
  1154. evsel->attr.type = PERF_TYPE_SOFTWARE;
  1155. evsel->attr.config = PERF_COUNT_SW_CPU_CLOCK;
  1156. free(evsel->name);
  1157. evsel->name = NULL;
  1158. return true;
  1159. }
  1160. return false;
  1161. }