evsel.c 35 KB

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