evsel.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583
  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 <lk/debugfs.h>
  12. #include <traceevent/event-parse.h>
  13. #include <linux/hw_breakpoint.h>
  14. #include <linux/perf_event.h>
  15. #include "asm/bug.h"
  16. #include "evsel.h"
  17. #include "evlist.h"
  18. #include "util.h"
  19. #include "cpumap.h"
  20. #include "thread_map.h"
  21. #include "target.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, alloc_size - size);
  108. if (n < 0)
  109. goto out_free_bf;
  110. size += n;
  111. } while (n > 0);
  112. pevent_parse_format(&format, bf, size, sys);
  113. out_free_bf:
  114. free(bf);
  115. close(fd);
  116. out_free_filename:
  117. free(filename);
  118. out:
  119. return format;
  120. }
  121. struct perf_evsel *perf_evsel__newtp(const char *sys, const char *name, int idx)
  122. {
  123. struct perf_evsel *evsel = zalloc(sizeof(*evsel));
  124. if (evsel != NULL) {
  125. struct perf_event_attr attr = {
  126. .type = PERF_TYPE_TRACEPOINT,
  127. .sample_type = (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME |
  128. PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD),
  129. };
  130. if (asprintf(&evsel->name, "%s:%s", sys, name) < 0)
  131. goto out_free;
  132. evsel->tp_format = event_format__new(sys, name);
  133. if (evsel->tp_format == NULL)
  134. goto out_free;
  135. event_attr_init(&attr);
  136. attr.config = evsel->tp_format->id;
  137. attr.sample_period = 1;
  138. perf_evsel__init(evsel, &attr, idx);
  139. }
  140. return evsel;
  141. out_free:
  142. free(evsel->name);
  143. free(evsel);
  144. return NULL;
  145. }
  146. const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX] = {
  147. "cycles",
  148. "instructions",
  149. "cache-references",
  150. "cache-misses",
  151. "branches",
  152. "branch-misses",
  153. "bus-cycles",
  154. "stalled-cycles-frontend",
  155. "stalled-cycles-backend",
  156. "ref-cycles",
  157. };
  158. static const char *__perf_evsel__hw_name(u64 config)
  159. {
  160. if (config < PERF_COUNT_HW_MAX && perf_evsel__hw_names[config])
  161. return perf_evsel__hw_names[config];
  162. return "unknown-hardware";
  163. }
  164. static int perf_evsel__add_modifiers(struct perf_evsel *evsel, char *bf, size_t size)
  165. {
  166. int colon = 0, r = 0;
  167. struct perf_event_attr *attr = &evsel->attr;
  168. bool exclude_guest_default = false;
  169. #define MOD_PRINT(context, mod) do { \
  170. if (!attr->exclude_##context) { \
  171. if (!colon) colon = ++r; \
  172. r += scnprintf(bf + r, size - r, "%c", mod); \
  173. } } while(0)
  174. if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) {
  175. MOD_PRINT(kernel, 'k');
  176. MOD_PRINT(user, 'u');
  177. MOD_PRINT(hv, 'h');
  178. exclude_guest_default = true;
  179. }
  180. if (attr->precise_ip) {
  181. if (!colon)
  182. colon = ++r;
  183. r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp");
  184. exclude_guest_default = true;
  185. }
  186. if (attr->exclude_host || attr->exclude_guest == exclude_guest_default) {
  187. MOD_PRINT(host, 'H');
  188. MOD_PRINT(guest, 'G');
  189. }
  190. #undef MOD_PRINT
  191. if (colon)
  192. bf[colon - 1] = ':';
  193. return r;
  194. }
  195. static int perf_evsel__hw_name(struct perf_evsel *evsel, char *bf, size_t size)
  196. {
  197. int r = scnprintf(bf, size, "%s", __perf_evsel__hw_name(evsel->attr.config));
  198. return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
  199. }
  200. const char *perf_evsel__sw_names[PERF_COUNT_SW_MAX] = {
  201. "cpu-clock",
  202. "task-clock",
  203. "page-faults",
  204. "context-switches",
  205. "cpu-migrations",
  206. "minor-faults",
  207. "major-faults",
  208. "alignment-faults",
  209. "emulation-faults",
  210. };
  211. static const char *__perf_evsel__sw_name(u64 config)
  212. {
  213. if (config < PERF_COUNT_SW_MAX && perf_evsel__sw_names[config])
  214. return perf_evsel__sw_names[config];
  215. return "unknown-software";
  216. }
  217. static int perf_evsel__sw_name(struct perf_evsel *evsel, char *bf, size_t size)
  218. {
  219. int r = scnprintf(bf, size, "%s", __perf_evsel__sw_name(evsel->attr.config));
  220. return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
  221. }
  222. static int __perf_evsel__bp_name(char *bf, size_t size, u64 addr, u64 type)
  223. {
  224. int r;
  225. r = scnprintf(bf, size, "mem:0x%" PRIx64 ":", addr);
  226. if (type & HW_BREAKPOINT_R)
  227. r += scnprintf(bf + r, size - r, "r");
  228. if (type & HW_BREAKPOINT_W)
  229. r += scnprintf(bf + r, size - r, "w");
  230. if (type & HW_BREAKPOINT_X)
  231. r += scnprintf(bf + r, size - r, "x");
  232. return r;
  233. }
  234. static int perf_evsel__bp_name(struct perf_evsel *evsel, char *bf, size_t size)
  235. {
  236. struct perf_event_attr *attr = &evsel->attr;
  237. int r = __perf_evsel__bp_name(bf, size, attr->bp_addr, attr->bp_type);
  238. return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
  239. }
  240. const char *perf_evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX]
  241. [PERF_EVSEL__MAX_ALIASES] = {
  242. { "L1-dcache", "l1-d", "l1d", "L1-data", },
  243. { "L1-icache", "l1-i", "l1i", "L1-instruction", },
  244. { "LLC", "L2", },
  245. { "dTLB", "d-tlb", "Data-TLB", },
  246. { "iTLB", "i-tlb", "Instruction-TLB", },
  247. { "branch", "branches", "bpu", "btb", "bpc", },
  248. { "node", },
  249. };
  250. const char *perf_evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX]
  251. [PERF_EVSEL__MAX_ALIASES] = {
  252. { "load", "loads", "read", },
  253. { "store", "stores", "write", },
  254. { "prefetch", "prefetches", "speculative-read", "speculative-load", },
  255. };
  256. const char *perf_evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX]
  257. [PERF_EVSEL__MAX_ALIASES] = {
  258. { "refs", "Reference", "ops", "access", },
  259. { "misses", "miss", },
  260. };
  261. #define C(x) PERF_COUNT_HW_CACHE_##x
  262. #define CACHE_READ (1 << C(OP_READ))
  263. #define CACHE_WRITE (1 << C(OP_WRITE))
  264. #define CACHE_PREFETCH (1 << C(OP_PREFETCH))
  265. #define COP(x) (1 << x)
  266. /*
  267. * cache operartion stat
  268. * L1I : Read and prefetch only
  269. * ITLB and BPU : Read-only
  270. */
  271. static unsigned long perf_evsel__hw_cache_stat[C(MAX)] = {
  272. [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
  273. [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
  274. [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
  275. [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
  276. [C(ITLB)] = (CACHE_READ),
  277. [C(BPU)] = (CACHE_READ),
  278. [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
  279. };
  280. bool perf_evsel__is_cache_op_valid(u8 type, u8 op)
  281. {
  282. if (perf_evsel__hw_cache_stat[type] & COP(op))
  283. return true; /* valid */
  284. else
  285. return false; /* invalid */
  286. }
  287. int __perf_evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result,
  288. char *bf, size_t size)
  289. {
  290. if (result) {
  291. return scnprintf(bf, size, "%s-%s-%s", perf_evsel__hw_cache[type][0],
  292. perf_evsel__hw_cache_op[op][0],
  293. perf_evsel__hw_cache_result[result][0]);
  294. }
  295. return scnprintf(bf, size, "%s-%s", perf_evsel__hw_cache[type][0],
  296. perf_evsel__hw_cache_op[op][1]);
  297. }
  298. static int __perf_evsel__hw_cache_name(u64 config, char *bf, size_t size)
  299. {
  300. u8 op, result, type = (config >> 0) & 0xff;
  301. const char *err = "unknown-ext-hardware-cache-type";
  302. if (type > PERF_COUNT_HW_CACHE_MAX)
  303. goto out_err;
  304. op = (config >> 8) & 0xff;
  305. err = "unknown-ext-hardware-cache-op";
  306. if (op > PERF_COUNT_HW_CACHE_OP_MAX)
  307. goto out_err;
  308. result = (config >> 16) & 0xff;
  309. err = "unknown-ext-hardware-cache-result";
  310. if (result > PERF_COUNT_HW_CACHE_RESULT_MAX)
  311. goto out_err;
  312. err = "invalid-cache";
  313. if (!perf_evsel__is_cache_op_valid(type, op))
  314. goto out_err;
  315. return __perf_evsel__hw_cache_type_op_res_name(type, op, result, bf, size);
  316. out_err:
  317. return scnprintf(bf, size, "%s", err);
  318. }
  319. static int perf_evsel__hw_cache_name(struct perf_evsel *evsel, char *bf, size_t size)
  320. {
  321. int ret = __perf_evsel__hw_cache_name(evsel->attr.config, bf, size);
  322. return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
  323. }
  324. static int perf_evsel__raw_name(struct perf_evsel *evsel, char *bf, size_t size)
  325. {
  326. int ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->attr.config);
  327. return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
  328. }
  329. const char *perf_evsel__name(struct perf_evsel *evsel)
  330. {
  331. char bf[128];
  332. if (evsel->name)
  333. return evsel->name;
  334. switch (evsel->attr.type) {
  335. case PERF_TYPE_RAW:
  336. perf_evsel__raw_name(evsel, bf, sizeof(bf));
  337. break;
  338. case PERF_TYPE_HARDWARE:
  339. perf_evsel__hw_name(evsel, bf, sizeof(bf));
  340. break;
  341. case PERF_TYPE_HW_CACHE:
  342. perf_evsel__hw_cache_name(evsel, bf, sizeof(bf));
  343. break;
  344. case PERF_TYPE_SOFTWARE:
  345. perf_evsel__sw_name(evsel, bf, sizeof(bf));
  346. break;
  347. case PERF_TYPE_TRACEPOINT:
  348. scnprintf(bf, sizeof(bf), "%s", "unknown tracepoint");
  349. break;
  350. case PERF_TYPE_BREAKPOINT:
  351. perf_evsel__bp_name(evsel, bf, sizeof(bf));
  352. break;
  353. default:
  354. scnprintf(bf, sizeof(bf), "unknown attr type: %d",
  355. evsel->attr.type);
  356. break;
  357. }
  358. evsel->name = strdup(bf);
  359. return evsel->name ?: "unknown";
  360. }
  361. const char *perf_evsel__group_name(struct perf_evsel *evsel)
  362. {
  363. return evsel->group_name ?: "anon group";
  364. }
  365. int perf_evsel__group_desc(struct perf_evsel *evsel, char *buf, size_t size)
  366. {
  367. int ret;
  368. struct perf_evsel *pos;
  369. const char *group_name = perf_evsel__group_name(evsel);
  370. ret = scnprintf(buf, size, "%s", group_name);
  371. ret += scnprintf(buf + ret, size - ret, " { %s",
  372. perf_evsel__name(evsel));
  373. for_each_group_member(pos, evsel)
  374. ret += scnprintf(buf + ret, size - ret, ", %s",
  375. perf_evsel__name(pos));
  376. ret += scnprintf(buf + ret, size - ret, " }");
  377. return ret;
  378. }
  379. /*
  380. * The enable_on_exec/disabled value strategy:
  381. *
  382. * 1) For any type of traced program:
  383. * - all independent events and group leaders are disabled
  384. * - all group members are enabled
  385. *
  386. * Group members are ruled by group leaders. They need to
  387. * be enabled, because the group scheduling relies on that.
  388. *
  389. * 2) For traced programs executed by perf:
  390. * - all independent events and group leaders have
  391. * enable_on_exec set
  392. * - we don't specifically enable or disable any event during
  393. * the record command
  394. *
  395. * Independent events and group leaders are initially disabled
  396. * and get enabled by exec. Group members are ruled by group
  397. * leaders as stated in 1).
  398. *
  399. * 3) For traced programs attached by perf (pid/tid):
  400. * - we specifically enable or disable all events during
  401. * the record command
  402. *
  403. * When attaching events to already running traced we
  404. * enable/disable events specifically, as there's no
  405. * initial traced exec call.
  406. */
  407. void perf_evsel__config(struct perf_evsel *evsel,
  408. struct perf_record_opts *opts)
  409. {
  410. struct perf_event_attr *attr = &evsel->attr;
  411. int track = !evsel->idx; /* only the first counter needs these */
  412. attr->sample_id_all = perf_missing_features.sample_id_all ? 0 : 1;
  413. attr->inherit = !opts->no_inherit;
  414. perf_evsel__set_sample_bit(evsel, IP);
  415. perf_evsel__set_sample_bit(evsel, TID);
  416. /*
  417. * We default some events to a 1 default interval. But keep
  418. * it a weak assumption overridable by the user.
  419. */
  420. if (!attr->sample_period || (opts->user_freq != UINT_MAX &&
  421. opts->user_interval != ULLONG_MAX)) {
  422. if (opts->freq) {
  423. perf_evsel__set_sample_bit(evsel, PERIOD);
  424. attr->freq = 1;
  425. attr->sample_freq = opts->freq;
  426. } else {
  427. attr->sample_period = opts->default_interval;
  428. }
  429. }
  430. if (opts->no_samples)
  431. attr->sample_freq = 0;
  432. if (opts->inherit_stat)
  433. attr->inherit_stat = 1;
  434. if (opts->sample_address) {
  435. perf_evsel__set_sample_bit(evsel, ADDR);
  436. attr->mmap_data = track;
  437. }
  438. if (opts->call_graph) {
  439. perf_evsel__set_sample_bit(evsel, CALLCHAIN);
  440. if (opts->call_graph == CALLCHAIN_DWARF) {
  441. perf_evsel__set_sample_bit(evsel, REGS_USER);
  442. perf_evsel__set_sample_bit(evsel, STACK_USER);
  443. attr->sample_regs_user = PERF_REGS_MASK;
  444. attr->sample_stack_user = opts->stack_dump_size;
  445. attr->exclude_callchain_user = 1;
  446. }
  447. }
  448. if (perf_target__has_cpu(&opts->target))
  449. perf_evsel__set_sample_bit(evsel, CPU);
  450. if (opts->period)
  451. perf_evsel__set_sample_bit(evsel, PERIOD);
  452. if (!perf_missing_features.sample_id_all &&
  453. (opts->sample_time || !opts->no_inherit ||
  454. perf_target__has_cpu(&opts->target)))
  455. perf_evsel__set_sample_bit(evsel, TIME);
  456. if (opts->raw_samples) {
  457. perf_evsel__set_sample_bit(evsel, TIME);
  458. perf_evsel__set_sample_bit(evsel, RAW);
  459. perf_evsel__set_sample_bit(evsel, CPU);
  460. }
  461. if (opts->sample_address)
  462. attr->sample_type |= PERF_SAMPLE_DATA_SRC;
  463. if (opts->no_delay) {
  464. attr->watermark = 0;
  465. attr->wakeup_events = 1;
  466. }
  467. if (opts->branch_stack) {
  468. perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
  469. attr->branch_sample_type = opts->branch_stack;
  470. }
  471. if (opts->sample_weight)
  472. attr->sample_type |= PERF_SAMPLE_WEIGHT;
  473. attr->mmap = track;
  474. attr->comm = track;
  475. /*
  476. * XXX see the function comment above
  477. *
  478. * Disabling only independent events or group leaders,
  479. * keeping group members enabled.
  480. */
  481. if (perf_evsel__is_group_leader(evsel))
  482. attr->disabled = 1;
  483. /*
  484. * Setting enable_on_exec for independent events and
  485. * group leaders for traced executed by perf.
  486. */
  487. if (perf_target__none(&opts->target) && perf_evsel__is_group_leader(evsel))
  488. attr->enable_on_exec = 1;
  489. }
  490. int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
  491. {
  492. int cpu, thread;
  493. evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
  494. if (evsel->fd) {
  495. for (cpu = 0; cpu < ncpus; cpu++) {
  496. for (thread = 0; thread < nthreads; thread++) {
  497. FD(evsel, cpu, thread) = -1;
  498. }
  499. }
  500. }
  501. return evsel->fd != NULL ? 0 : -ENOMEM;
  502. }
  503. int perf_evsel__set_filter(struct perf_evsel *evsel, int ncpus, int nthreads,
  504. const char *filter)
  505. {
  506. int cpu, thread;
  507. for (cpu = 0; cpu < ncpus; cpu++) {
  508. for (thread = 0; thread < nthreads; thread++) {
  509. int fd = FD(evsel, cpu, thread),
  510. err = ioctl(fd, PERF_EVENT_IOC_SET_FILTER, filter);
  511. if (err)
  512. return err;
  513. }
  514. }
  515. return 0;
  516. }
  517. int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
  518. {
  519. evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
  520. if (evsel->sample_id == NULL)
  521. return -ENOMEM;
  522. evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
  523. if (evsel->id == NULL) {
  524. xyarray__delete(evsel->sample_id);
  525. evsel->sample_id = NULL;
  526. return -ENOMEM;
  527. }
  528. return 0;
  529. }
  530. void perf_evsel__reset_counts(struct perf_evsel *evsel, int ncpus)
  531. {
  532. memset(evsel->counts, 0, (sizeof(*evsel->counts) +
  533. (ncpus * sizeof(struct perf_counts_values))));
  534. }
  535. int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
  536. {
  537. evsel->counts = zalloc((sizeof(*evsel->counts) +
  538. (ncpus * sizeof(struct perf_counts_values))));
  539. return evsel->counts != NULL ? 0 : -ENOMEM;
  540. }
  541. void perf_evsel__free_fd(struct perf_evsel *evsel)
  542. {
  543. xyarray__delete(evsel->fd);
  544. evsel->fd = NULL;
  545. }
  546. void perf_evsel__free_id(struct perf_evsel *evsel)
  547. {
  548. xyarray__delete(evsel->sample_id);
  549. evsel->sample_id = NULL;
  550. free(evsel->id);
  551. evsel->id = NULL;
  552. }
  553. void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
  554. {
  555. int cpu, thread;
  556. for (cpu = 0; cpu < ncpus; cpu++)
  557. for (thread = 0; thread < nthreads; ++thread) {
  558. close(FD(evsel, cpu, thread));
  559. FD(evsel, cpu, thread) = -1;
  560. }
  561. }
  562. void perf_evsel__free_counts(struct perf_evsel *evsel)
  563. {
  564. free(evsel->counts);
  565. }
  566. void perf_evsel__exit(struct perf_evsel *evsel)
  567. {
  568. assert(list_empty(&evsel->node));
  569. perf_evsel__free_fd(evsel);
  570. perf_evsel__free_id(evsel);
  571. }
  572. void perf_evsel__delete(struct perf_evsel *evsel)
  573. {
  574. perf_evsel__exit(evsel);
  575. close_cgroup(evsel->cgrp);
  576. free(evsel->group_name);
  577. if (evsel->tp_format)
  578. pevent_free_format(evsel->tp_format);
  579. free(evsel->name);
  580. free(evsel);
  581. }
  582. static inline void compute_deltas(struct perf_evsel *evsel,
  583. int cpu,
  584. struct perf_counts_values *count)
  585. {
  586. struct perf_counts_values tmp;
  587. if (!evsel->prev_raw_counts)
  588. return;
  589. if (cpu == -1) {
  590. tmp = evsel->prev_raw_counts->aggr;
  591. evsel->prev_raw_counts->aggr = *count;
  592. } else {
  593. tmp = evsel->prev_raw_counts->cpu[cpu];
  594. evsel->prev_raw_counts->cpu[cpu] = *count;
  595. }
  596. count->val = count->val - tmp.val;
  597. count->ena = count->ena - tmp.ena;
  598. count->run = count->run - tmp.run;
  599. }
  600. int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
  601. int cpu, int thread, bool scale)
  602. {
  603. struct perf_counts_values count;
  604. size_t nv = scale ? 3 : 1;
  605. if (FD(evsel, cpu, thread) < 0)
  606. return -EINVAL;
  607. if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
  608. return -ENOMEM;
  609. if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
  610. return -errno;
  611. compute_deltas(evsel, cpu, &count);
  612. if (scale) {
  613. if (count.run == 0)
  614. count.val = 0;
  615. else if (count.run < count.ena)
  616. count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
  617. } else
  618. count.ena = count.run = 0;
  619. evsel->counts->cpu[cpu] = count;
  620. return 0;
  621. }
  622. int __perf_evsel__read(struct perf_evsel *evsel,
  623. int ncpus, int nthreads, bool scale)
  624. {
  625. size_t nv = scale ? 3 : 1;
  626. int cpu, thread;
  627. struct perf_counts_values *aggr = &evsel->counts->aggr, count;
  628. aggr->val = aggr->ena = aggr->run = 0;
  629. for (cpu = 0; cpu < ncpus; cpu++) {
  630. for (thread = 0; thread < nthreads; thread++) {
  631. if (FD(evsel, cpu, thread) < 0)
  632. continue;
  633. if (readn(FD(evsel, cpu, thread),
  634. &count, nv * sizeof(u64)) < 0)
  635. return -errno;
  636. aggr->val += count.val;
  637. if (scale) {
  638. aggr->ena += count.ena;
  639. aggr->run += count.run;
  640. }
  641. }
  642. }
  643. compute_deltas(evsel, -1, aggr);
  644. evsel->counts->scaled = 0;
  645. if (scale) {
  646. if (aggr->run == 0) {
  647. evsel->counts->scaled = -1;
  648. aggr->val = 0;
  649. return 0;
  650. }
  651. if (aggr->run < aggr->ena) {
  652. evsel->counts->scaled = 1;
  653. aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
  654. }
  655. } else
  656. aggr->ena = aggr->run = 0;
  657. return 0;
  658. }
  659. static int get_group_fd(struct perf_evsel *evsel, int cpu, int thread)
  660. {
  661. struct perf_evsel *leader = evsel->leader;
  662. int fd;
  663. if (perf_evsel__is_group_leader(evsel))
  664. return -1;
  665. /*
  666. * Leader must be already processed/open,
  667. * if not it's a bug.
  668. */
  669. BUG_ON(!leader->fd);
  670. fd = FD(leader, cpu, thread);
  671. BUG_ON(fd == -1);
  672. return fd;
  673. }
  674. static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
  675. struct thread_map *threads)
  676. {
  677. int cpu, thread;
  678. unsigned long flags = 0;
  679. int pid = -1, err;
  680. if (evsel->fd == NULL &&
  681. perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
  682. return -ENOMEM;
  683. if (evsel->cgrp) {
  684. flags = PERF_FLAG_PID_CGROUP;
  685. pid = evsel->cgrp->fd;
  686. }
  687. fallback_missing_features:
  688. if (perf_missing_features.exclude_guest)
  689. evsel->attr.exclude_guest = evsel->attr.exclude_host = 0;
  690. retry_sample_id:
  691. if (perf_missing_features.sample_id_all)
  692. evsel->attr.sample_id_all = 0;
  693. for (cpu = 0; cpu < cpus->nr; cpu++) {
  694. for (thread = 0; thread < threads->nr; thread++) {
  695. int group_fd;
  696. if (!evsel->cgrp)
  697. pid = threads->map[thread];
  698. group_fd = get_group_fd(evsel, cpu, thread);
  699. FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
  700. pid,
  701. cpus->map[cpu],
  702. group_fd, flags);
  703. if (FD(evsel, cpu, thread) < 0) {
  704. err = -errno;
  705. goto try_fallback;
  706. }
  707. }
  708. }
  709. return 0;
  710. try_fallback:
  711. if (err != -EINVAL || cpu > 0 || thread > 0)
  712. goto out_close;
  713. if (!perf_missing_features.exclude_guest &&
  714. (evsel->attr.exclude_guest || evsel->attr.exclude_host)) {
  715. perf_missing_features.exclude_guest = true;
  716. goto fallback_missing_features;
  717. } else if (!perf_missing_features.sample_id_all) {
  718. perf_missing_features.sample_id_all = true;
  719. goto retry_sample_id;
  720. }
  721. out_close:
  722. do {
  723. while (--thread >= 0) {
  724. close(FD(evsel, cpu, thread));
  725. FD(evsel, cpu, thread) = -1;
  726. }
  727. thread = threads->nr;
  728. } while (--cpu >= 0);
  729. return err;
  730. }
  731. void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
  732. {
  733. if (evsel->fd == NULL)
  734. return;
  735. perf_evsel__close_fd(evsel, ncpus, nthreads);
  736. perf_evsel__free_fd(evsel);
  737. evsel->fd = NULL;
  738. }
  739. static struct {
  740. struct cpu_map map;
  741. int cpus[1];
  742. } empty_cpu_map = {
  743. .map.nr = 1,
  744. .cpus = { -1, },
  745. };
  746. static struct {
  747. struct thread_map map;
  748. int threads[1];
  749. } empty_thread_map = {
  750. .map.nr = 1,
  751. .threads = { -1, },
  752. };
  753. int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
  754. struct thread_map *threads)
  755. {
  756. if (cpus == NULL) {
  757. /* Work around old compiler warnings about strict aliasing */
  758. cpus = &empty_cpu_map.map;
  759. }
  760. if (threads == NULL)
  761. threads = &empty_thread_map.map;
  762. return __perf_evsel__open(evsel, cpus, threads);
  763. }
  764. int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
  765. struct cpu_map *cpus)
  766. {
  767. return __perf_evsel__open(evsel, cpus, &empty_thread_map.map);
  768. }
  769. int perf_evsel__open_per_thread(struct perf_evsel *evsel,
  770. struct thread_map *threads)
  771. {
  772. return __perf_evsel__open(evsel, &empty_cpu_map.map, threads);
  773. }
  774. static int perf_evsel__parse_id_sample(const struct perf_evsel *evsel,
  775. const union perf_event *event,
  776. struct perf_sample *sample)
  777. {
  778. u64 type = evsel->attr.sample_type;
  779. const u64 *array = event->sample.array;
  780. bool swapped = evsel->needs_swap;
  781. union u64_swap u;
  782. array += ((event->header.size -
  783. sizeof(event->header)) / sizeof(u64)) - 1;
  784. if (type & PERF_SAMPLE_CPU) {
  785. u.val64 = *array;
  786. if (swapped) {
  787. /* undo swap of u64, then swap on individual u32s */
  788. u.val64 = bswap_64(u.val64);
  789. u.val32[0] = bswap_32(u.val32[0]);
  790. }
  791. sample->cpu = u.val32[0];
  792. array--;
  793. }
  794. if (type & PERF_SAMPLE_STREAM_ID) {
  795. sample->stream_id = *array;
  796. array--;
  797. }
  798. if (type & PERF_SAMPLE_ID) {
  799. sample->id = *array;
  800. array--;
  801. }
  802. if (type & PERF_SAMPLE_TIME) {
  803. sample->time = *array;
  804. array--;
  805. }
  806. if (type & PERF_SAMPLE_TID) {
  807. u.val64 = *array;
  808. if (swapped) {
  809. /* undo swap of u64, then swap on individual u32s */
  810. u.val64 = bswap_64(u.val64);
  811. u.val32[0] = bswap_32(u.val32[0]);
  812. u.val32[1] = bswap_32(u.val32[1]);
  813. }
  814. sample->pid = u.val32[0];
  815. sample->tid = u.val32[1];
  816. }
  817. return 0;
  818. }
  819. static bool sample_overlap(const union perf_event *event,
  820. const void *offset, u64 size)
  821. {
  822. const void *base = event;
  823. if (offset + size > base + event->header.size)
  824. return true;
  825. return false;
  826. }
  827. int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
  828. struct perf_sample *data)
  829. {
  830. u64 type = evsel->attr.sample_type;
  831. u64 regs_user = evsel->attr.sample_regs_user;
  832. bool swapped = evsel->needs_swap;
  833. const u64 *array;
  834. /*
  835. * used for cross-endian analysis. See git commit 65014ab3
  836. * for why this goofiness is needed.
  837. */
  838. union u64_swap u;
  839. memset(data, 0, sizeof(*data));
  840. data->cpu = data->pid = data->tid = -1;
  841. data->stream_id = data->id = data->time = -1ULL;
  842. data->period = 1;
  843. data->weight = 0;
  844. if (event->header.type != PERF_RECORD_SAMPLE) {
  845. if (!evsel->attr.sample_id_all)
  846. return 0;
  847. return perf_evsel__parse_id_sample(evsel, event, data);
  848. }
  849. array = event->sample.array;
  850. if (evsel->sample_size + sizeof(event->header) > event->header.size)
  851. return -EFAULT;
  852. if (type & PERF_SAMPLE_IP) {
  853. data->ip = event->ip.ip;
  854. array++;
  855. }
  856. if (type & PERF_SAMPLE_TID) {
  857. u.val64 = *array;
  858. if (swapped) {
  859. /* undo swap of u64, then swap on individual u32s */
  860. u.val64 = bswap_64(u.val64);
  861. u.val32[0] = bswap_32(u.val32[0]);
  862. u.val32[1] = bswap_32(u.val32[1]);
  863. }
  864. data->pid = u.val32[0];
  865. data->tid = u.val32[1];
  866. array++;
  867. }
  868. if (type & PERF_SAMPLE_TIME) {
  869. data->time = *array;
  870. array++;
  871. }
  872. data->addr = 0;
  873. if (type & PERF_SAMPLE_ADDR) {
  874. data->addr = *array;
  875. array++;
  876. }
  877. data->id = -1ULL;
  878. if (type & PERF_SAMPLE_ID) {
  879. data->id = *array;
  880. array++;
  881. }
  882. if (type & PERF_SAMPLE_STREAM_ID) {
  883. data->stream_id = *array;
  884. array++;
  885. }
  886. if (type & PERF_SAMPLE_CPU) {
  887. u.val64 = *array;
  888. if (swapped) {
  889. /* undo swap of u64, then swap on individual u32s */
  890. u.val64 = bswap_64(u.val64);
  891. u.val32[0] = bswap_32(u.val32[0]);
  892. }
  893. data->cpu = u.val32[0];
  894. array++;
  895. }
  896. if (type & PERF_SAMPLE_PERIOD) {
  897. data->period = *array;
  898. array++;
  899. }
  900. if (type & PERF_SAMPLE_READ) {
  901. u64 read_format = evsel->attr.read_format;
  902. if (read_format & PERF_FORMAT_GROUP)
  903. data->read.group.nr = *array;
  904. else
  905. data->read.one.value = *array;
  906. array++;
  907. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
  908. data->read.time_enabled = *array;
  909. array++;
  910. }
  911. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
  912. data->read.time_running = *array;
  913. array++;
  914. }
  915. /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
  916. if (read_format & PERF_FORMAT_GROUP) {
  917. data->read.group.values = (struct sample_read_value *) array;
  918. array = (void *) array + data->read.group.nr *
  919. sizeof(struct sample_read_value);
  920. } else {
  921. data->read.one.id = *array;
  922. array++;
  923. }
  924. }
  925. if (type & PERF_SAMPLE_CALLCHAIN) {
  926. if (sample_overlap(event, array, sizeof(data->callchain->nr)))
  927. return -EFAULT;
  928. data->callchain = (struct ip_callchain *)array;
  929. if (sample_overlap(event, array, data->callchain->nr))
  930. return -EFAULT;
  931. array += 1 + data->callchain->nr;
  932. }
  933. if (type & PERF_SAMPLE_RAW) {
  934. const u64 *pdata;
  935. u.val64 = *array;
  936. if (WARN_ONCE(swapped,
  937. "Endianness of raw data not corrected!\n")) {
  938. /* undo swap of u64, then swap on individual u32s */
  939. u.val64 = bswap_64(u.val64);
  940. u.val32[0] = bswap_32(u.val32[0]);
  941. u.val32[1] = bswap_32(u.val32[1]);
  942. }
  943. if (sample_overlap(event, array, sizeof(u32)))
  944. return -EFAULT;
  945. data->raw_size = u.val32[0];
  946. pdata = (void *) array + sizeof(u32);
  947. if (sample_overlap(event, pdata, data->raw_size))
  948. return -EFAULT;
  949. data->raw_data = (void *) pdata;
  950. array = (void *)array + data->raw_size + sizeof(u32);
  951. }
  952. if (type & PERF_SAMPLE_BRANCH_STACK) {
  953. u64 sz;
  954. data->branch_stack = (struct branch_stack *)array;
  955. array++; /* nr */
  956. sz = data->branch_stack->nr * sizeof(struct branch_entry);
  957. sz /= sizeof(u64);
  958. array += sz;
  959. }
  960. if (type & PERF_SAMPLE_REGS_USER) {
  961. /* First u64 tells us if we have any regs in sample. */
  962. u64 avail = *array++;
  963. if (avail) {
  964. data->user_regs.regs = (u64 *)array;
  965. array += hweight_long(regs_user);
  966. }
  967. }
  968. if (type & PERF_SAMPLE_STACK_USER) {
  969. u64 size = *array++;
  970. data->user_stack.offset = ((char *)(array - 1)
  971. - (char *) event);
  972. if (!size) {
  973. data->user_stack.size = 0;
  974. } else {
  975. data->user_stack.data = (char *)array;
  976. array += size / sizeof(*array);
  977. data->user_stack.size = *array++;
  978. }
  979. }
  980. data->weight = 0;
  981. if (type & PERF_SAMPLE_WEIGHT) {
  982. data->weight = *array;
  983. array++;
  984. }
  985. data->data_src = PERF_MEM_DATA_SRC_NONE;
  986. if (type & PERF_SAMPLE_DATA_SRC) {
  987. data->data_src = *array;
  988. array++;
  989. }
  990. return 0;
  991. }
  992. int perf_event__synthesize_sample(union perf_event *event, u64 type,
  993. const struct perf_sample *sample,
  994. bool swapped)
  995. {
  996. u64 *array;
  997. /*
  998. * used for cross-endian analysis. See git commit 65014ab3
  999. * for why this goofiness is needed.
  1000. */
  1001. union u64_swap u;
  1002. array = event->sample.array;
  1003. if (type & PERF_SAMPLE_IP) {
  1004. event->ip.ip = sample->ip;
  1005. array++;
  1006. }
  1007. if (type & PERF_SAMPLE_TID) {
  1008. u.val32[0] = sample->pid;
  1009. u.val32[1] = sample->tid;
  1010. if (swapped) {
  1011. /*
  1012. * Inverse of what is done in perf_evsel__parse_sample
  1013. */
  1014. u.val32[0] = bswap_32(u.val32[0]);
  1015. u.val32[1] = bswap_32(u.val32[1]);
  1016. u.val64 = bswap_64(u.val64);
  1017. }
  1018. *array = u.val64;
  1019. array++;
  1020. }
  1021. if (type & PERF_SAMPLE_TIME) {
  1022. *array = sample->time;
  1023. array++;
  1024. }
  1025. if (type & PERF_SAMPLE_ADDR) {
  1026. *array = sample->addr;
  1027. array++;
  1028. }
  1029. if (type & PERF_SAMPLE_ID) {
  1030. *array = sample->id;
  1031. array++;
  1032. }
  1033. if (type & PERF_SAMPLE_STREAM_ID) {
  1034. *array = sample->stream_id;
  1035. array++;
  1036. }
  1037. if (type & PERF_SAMPLE_CPU) {
  1038. u.val32[0] = sample->cpu;
  1039. if (swapped) {
  1040. /*
  1041. * Inverse of what is done in perf_evsel__parse_sample
  1042. */
  1043. u.val32[0] = bswap_32(u.val32[0]);
  1044. u.val64 = bswap_64(u.val64);
  1045. }
  1046. *array = u.val64;
  1047. array++;
  1048. }
  1049. if (type & PERF_SAMPLE_PERIOD) {
  1050. *array = sample->period;
  1051. array++;
  1052. }
  1053. return 0;
  1054. }
  1055. struct format_field *perf_evsel__field(struct perf_evsel *evsel, const char *name)
  1056. {
  1057. return pevent_find_field(evsel->tp_format, name);
  1058. }
  1059. void *perf_evsel__rawptr(struct perf_evsel *evsel, struct perf_sample *sample,
  1060. const char *name)
  1061. {
  1062. struct format_field *field = perf_evsel__field(evsel, name);
  1063. int offset;
  1064. if (!field)
  1065. return NULL;
  1066. offset = field->offset;
  1067. if (field->flags & FIELD_IS_DYNAMIC) {
  1068. offset = *(int *)(sample->raw_data + field->offset);
  1069. offset &= 0xffff;
  1070. }
  1071. return sample->raw_data + offset;
  1072. }
  1073. u64 perf_evsel__intval(struct perf_evsel *evsel, struct perf_sample *sample,
  1074. const char *name)
  1075. {
  1076. struct format_field *field = perf_evsel__field(evsel, name);
  1077. void *ptr;
  1078. u64 value;
  1079. if (!field)
  1080. return 0;
  1081. ptr = sample->raw_data + field->offset;
  1082. switch (field->size) {
  1083. case 1:
  1084. return *(u8 *)ptr;
  1085. case 2:
  1086. value = *(u16 *)ptr;
  1087. break;
  1088. case 4:
  1089. value = *(u32 *)ptr;
  1090. break;
  1091. case 8:
  1092. value = *(u64 *)ptr;
  1093. break;
  1094. default:
  1095. return 0;
  1096. }
  1097. if (!evsel->needs_swap)
  1098. return value;
  1099. switch (field->size) {
  1100. case 2:
  1101. return bswap_16(value);
  1102. case 4:
  1103. return bswap_32(value);
  1104. case 8:
  1105. return bswap_64(value);
  1106. default:
  1107. return 0;
  1108. }
  1109. return 0;
  1110. }
  1111. static int comma_fprintf(FILE *fp, bool *first, const char *fmt, ...)
  1112. {
  1113. va_list args;
  1114. int ret = 0;
  1115. if (!*first) {
  1116. ret += fprintf(fp, ",");
  1117. } else {
  1118. ret += fprintf(fp, ":");
  1119. *first = false;
  1120. }
  1121. va_start(args, fmt);
  1122. ret += vfprintf(fp, fmt, args);
  1123. va_end(args);
  1124. return ret;
  1125. }
  1126. static int __if_fprintf(FILE *fp, bool *first, const char *field, u64 value)
  1127. {
  1128. if (value == 0)
  1129. return 0;
  1130. return comma_fprintf(fp, first, " %s: %" PRIu64, field, value);
  1131. }
  1132. #define if_print(field) printed += __if_fprintf(fp, &first, #field, evsel->attr.field)
  1133. struct bit_names {
  1134. int bit;
  1135. const char *name;
  1136. };
  1137. static int bits__fprintf(FILE *fp, const char *field, u64 value,
  1138. struct bit_names *bits, bool *first)
  1139. {
  1140. int i = 0, printed = comma_fprintf(fp, first, " %s: ", field);
  1141. bool first_bit = true;
  1142. do {
  1143. if (value & bits[i].bit) {
  1144. printed += fprintf(fp, "%s%s", first_bit ? "" : "|", bits[i].name);
  1145. first_bit = false;
  1146. }
  1147. } while (bits[++i].name != NULL);
  1148. return printed;
  1149. }
  1150. static int sample_type__fprintf(FILE *fp, bool *first, u64 value)
  1151. {
  1152. #define bit_name(n) { PERF_SAMPLE_##n, #n }
  1153. struct bit_names bits[] = {
  1154. bit_name(IP), bit_name(TID), bit_name(TIME), bit_name(ADDR),
  1155. bit_name(READ), bit_name(CALLCHAIN), bit_name(ID), bit_name(CPU),
  1156. bit_name(PERIOD), bit_name(STREAM_ID), bit_name(RAW),
  1157. bit_name(BRANCH_STACK), bit_name(REGS_USER), bit_name(STACK_USER),
  1158. { .name = NULL, }
  1159. };
  1160. #undef bit_name
  1161. return bits__fprintf(fp, "sample_type", value, bits, first);
  1162. }
  1163. static int read_format__fprintf(FILE *fp, bool *first, u64 value)
  1164. {
  1165. #define bit_name(n) { PERF_FORMAT_##n, #n }
  1166. struct bit_names bits[] = {
  1167. bit_name(TOTAL_TIME_ENABLED), bit_name(TOTAL_TIME_RUNNING),
  1168. bit_name(ID), bit_name(GROUP),
  1169. { .name = NULL, }
  1170. };
  1171. #undef bit_name
  1172. return bits__fprintf(fp, "read_format", value, bits, first);
  1173. }
  1174. int perf_evsel__fprintf(struct perf_evsel *evsel,
  1175. struct perf_attr_details *details, FILE *fp)
  1176. {
  1177. bool first = true;
  1178. int printed = 0;
  1179. if (details->event_group) {
  1180. struct perf_evsel *pos;
  1181. if (!perf_evsel__is_group_leader(evsel))
  1182. return 0;
  1183. if (evsel->nr_members > 1)
  1184. printed += fprintf(fp, "%s{", evsel->group_name ?: "");
  1185. printed += fprintf(fp, "%s", perf_evsel__name(evsel));
  1186. for_each_group_member(pos, evsel)
  1187. printed += fprintf(fp, ",%s", perf_evsel__name(pos));
  1188. if (evsel->nr_members > 1)
  1189. printed += fprintf(fp, "}");
  1190. goto out;
  1191. }
  1192. printed += fprintf(fp, "%s", perf_evsel__name(evsel));
  1193. if (details->verbose || details->freq) {
  1194. printed += comma_fprintf(fp, &first, " sample_freq=%" PRIu64,
  1195. (u64)evsel->attr.sample_freq);
  1196. }
  1197. if (details->verbose) {
  1198. if_print(type);
  1199. if_print(config);
  1200. if_print(config1);
  1201. if_print(config2);
  1202. if_print(size);
  1203. printed += sample_type__fprintf(fp, &first, evsel->attr.sample_type);
  1204. if (evsel->attr.read_format)
  1205. printed += read_format__fprintf(fp, &first, evsel->attr.read_format);
  1206. if_print(disabled);
  1207. if_print(inherit);
  1208. if_print(pinned);
  1209. if_print(exclusive);
  1210. if_print(exclude_user);
  1211. if_print(exclude_kernel);
  1212. if_print(exclude_hv);
  1213. if_print(exclude_idle);
  1214. if_print(mmap);
  1215. if_print(comm);
  1216. if_print(freq);
  1217. if_print(inherit_stat);
  1218. if_print(enable_on_exec);
  1219. if_print(task);
  1220. if_print(watermark);
  1221. if_print(precise_ip);
  1222. if_print(mmap_data);
  1223. if_print(sample_id_all);
  1224. if_print(exclude_host);
  1225. if_print(exclude_guest);
  1226. if_print(__reserved_1);
  1227. if_print(wakeup_events);
  1228. if_print(bp_type);
  1229. if_print(branch_sample_type);
  1230. }
  1231. out:
  1232. fputc('\n', fp);
  1233. return ++printed;
  1234. }
  1235. bool perf_evsel__fallback(struct perf_evsel *evsel, int err,
  1236. char *msg, size_t msgsize)
  1237. {
  1238. if ((err == ENOENT || err == ENXIO || err == ENODEV) &&
  1239. evsel->attr.type == PERF_TYPE_HARDWARE &&
  1240. evsel->attr.config == PERF_COUNT_HW_CPU_CYCLES) {
  1241. /*
  1242. * If it's cycles then fall back to hrtimer based
  1243. * cpu-clock-tick sw counter, which is always available even if
  1244. * no PMU support.
  1245. *
  1246. * PPC returns ENXIO until 2.6.37 (behavior changed with commit
  1247. * b0a873e).
  1248. */
  1249. scnprintf(msg, msgsize, "%s",
  1250. "The cycles event is not supported, trying to fall back to cpu-clock-ticks");
  1251. evsel->attr.type = PERF_TYPE_SOFTWARE;
  1252. evsel->attr.config = PERF_COUNT_SW_CPU_CLOCK;
  1253. free(evsel->name);
  1254. evsel->name = NULL;
  1255. return true;
  1256. }
  1257. return false;
  1258. }
  1259. int perf_evsel__open_strerror(struct perf_evsel *evsel,
  1260. struct perf_target *target,
  1261. int err, char *msg, size_t size)
  1262. {
  1263. switch (err) {
  1264. case EPERM:
  1265. case EACCES:
  1266. return scnprintf(msg, size,
  1267. "You may not have permission to collect %sstats.\n"
  1268. "Consider tweaking /proc/sys/kernel/perf_event_paranoid:\n"
  1269. " -1 - Not paranoid at all\n"
  1270. " 0 - Disallow raw tracepoint access for unpriv\n"
  1271. " 1 - Disallow cpu events for unpriv\n"
  1272. " 2 - Disallow kernel profiling for unpriv",
  1273. target->system_wide ? "system-wide " : "");
  1274. case ENOENT:
  1275. return scnprintf(msg, size, "The %s event is not supported.",
  1276. perf_evsel__name(evsel));
  1277. case EMFILE:
  1278. return scnprintf(msg, size, "%s",
  1279. "Too many events are opened.\n"
  1280. "Try again after reducing the number of events.");
  1281. case ENODEV:
  1282. if (target->cpu_list)
  1283. return scnprintf(msg, size, "%s",
  1284. "No such device - did you specify an out-of-range profile CPU?\n");
  1285. break;
  1286. case EOPNOTSUPP:
  1287. if (evsel->attr.precise_ip)
  1288. return scnprintf(msg, size, "%s",
  1289. "\'precise\' request may not be supported. Try removing 'p' modifier.");
  1290. #if defined(__i386__) || defined(__x86_64__)
  1291. if (evsel->attr.type == PERF_TYPE_HARDWARE)
  1292. return scnprintf(msg, size, "%s",
  1293. "No hardware sampling interrupt available.\n"
  1294. "No APIC? If so then you can boot the kernel with the \"lapic\" boot parameter to force-enable it.");
  1295. #endif
  1296. break;
  1297. default:
  1298. break;
  1299. }
  1300. return scnprintf(msg, size,
  1301. "The sys_perf_event_open() syscall returned with %d (%s) for event (%s). \n"
  1302. "/bin/dmesg may provide additional information.\n"
  1303. "No CONFIG_PERF_EVENTS=y kernel support configured?\n",
  1304. err, strerror(err), perf_evsel__name(evsel));
  1305. }