evsel.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931
  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 <sys/resource.h>
  16. #include "asm/bug.h"
  17. #include "evsel.h"
  18. #include "evlist.h"
  19. #include "util.h"
  20. #include "cpumap.h"
  21. #include "thread_map.h"
  22. #include "target.h"
  23. #include "perf_regs.h"
  24. #include "debug.h"
  25. static struct {
  26. bool sample_id_all;
  27. bool exclude_guest;
  28. } perf_missing_features;
  29. #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
  30. int __perf_evsel__sample_size(u64 sample_type)
  31. {
  32. u64 mask = sample_type & PERF_SAMPLE_MASK;
  33. int size = 0;
  34. int i;
  35. for (i = 0; i < 64; i++) {
  36. if (mask & (1ULL << i))
  37. size++;
  38. }
  39. size *= sizeof(u64);
  40. return size;
  41. }
  42. /**
  43. * __perf_evsel__calc_id_pos - calculate id_pos.
  44. * @sample_type: sample type
  45. *
  46. * This function returns the position of the event id (PERF_SAMPLE_ID or
  47. * PERF_SAMPLE_IDENTIFIER) in a sample event i.e. in the array of struct
  48. * sample_event.
  49. */
  50. static int __perf_evsel__calc_id_pos(u64 sample_type)
  51. {
  52. int idx = 0;
  53. if (sample_type & PERF_SAMPLE_IDENTIFIER)
  54. return 0;
  55. if (!(sample_type & PERF_SAMPLE_ID))
  56. return -1;
  57. if (sample_type & PERF_SAMPLE_IP)
  58. idx += 1;
  59. if (sample_type & PERF_SAMPLE_TID)
  60. idx += 1;
  61. if (sample_type & PERF_SAMPLE_TIME)
  62. idx += 1;
  63. if (sample_type & PERF_SAMPLE_ADDR)
  64. idx += 1;
  65. return idx;
  66. }
  67. /**
  68. * __perf_evsel__calc_is_pos - calculate is_pos.
  69. * @sample_type: sample type
  70. *
  71. * This function returns the position (counting backwards) of the event id
  72. * (PERF_SAMPLE_ID or PERF_SAMPLE_IDENTIFIER) in a non-sample event i.e. if
  73. * sample_id_all is used there is an id sample appended to non-sample events.
  74. */
  75. static int __perf_evsel__calc_is_pos(u64 sample_type)
  76. {
  77. int idx = 1;
  78. if (sample_type & PERF_SAMPLE_IDENTIFIER)
  79. return 1;
  80. if (!(sample_type & PERF_SAMPLE_ID))
  81. return -1;
  82. if (sample_type & PERF_SAMPLE_CPU)
  83. idx += 1;
  84. if (sample_type & PERF_SAMPLE_STREAM_ID)
  85. idx += 1;
  86. return idx;
  87. }
  88. void perf_evsel__calc_id_pos(struct perf_evsel *evsel)
  89. {
  90. evsel->id_pos = __perf_evsel__calc_id_pos(evsel->attr.sample_type);
  91. evsel->is_pos = __perf_evsel__calc_is_pos(evsel->attr.sample_type);
  92. }
  93. void hists__init(struct hists *hists)
  94. {
  95. memset(hists, 0, sizeof(*hists));
  96. hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
  97. hists->entries_in = &hists->entries_in_array[0];
  98. hists->entries_collapsed = RB_ROOT;
  99. hists->entries = RB_ROOT;
  100. pthread_mutex_init(&hists->lock, NULL);
  101. }
  102. void __perf_evsel__set_sample_bit(struct perf_evsel *evsel,
  103. enum perf_event_sample_format bit)
  104. {
  105. if (!(evsel->attr.sample_type & bit)) {
  106. evsel->attr.sample_type |= bit;
  107. evsel->sample_size += sizeof(u64);
  108. perf_evsel__calc_id_pos(evsel);
  109. }
  110. }
  111. void __perf_evsel__reset_sample_bit(struct perf_evsel *evsel,
  112. enum perf_event_sample_format bit)
  113. {
  114. if (evsel->attr.sample_type & bit) {
  115. evsel->attr.sample_type &= ~bit;
  116. evsel->sample_size -= sizeof(u64);
  117. perf_evsel__calc_id_pos(evsel);
  118. }
  119. }
  120. void perf_evsel__set_sample_id(struct perf_evsel *evsel,
  121. bool can_sample_identifier)
  122. {
  123. if (can_sample_identifier) {
  124. perf_evsel__reset_sample_bit(evsel, ID);
  125. perf_evsel__set_sample_bit(evsel, IDENTIFIER);
  126. } else {
  127. perf_evsel__set_sample_bit(evsel, ID);
  128. }
  129. evsel->attr.read_format |= PERF_FORMAT_ID;
  130. }
  131. void perf_evsel__init(struct perf_evsel *evsel,
  132. struct perf_event_attr *attr, int idx)
  133. {
  134. evsel->idx = idx;
  135. evsel->attr = *attr;
  136. evsel->leader = evsel;
  137. INIT_LIST_HEAD(&evsel->node);
  138. hists__init(&evsel->hists);
  139. evsel->sample_size = __perf_evsel__sample_size(attr->sample_type);
  140. perf_evsel__calc_id_pos(evsel);
  141. }
  142. struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr, int idx)
  143. {
  144. struct perf_evsel *evsel = zalloc(sizeof(*evsel));
  145. if (evsel != NULL)
  146. perf_evsel__init(evsel, attr, idx);
  147. return evsel;
  148. }
  149. struct event_format *event_format__new(const char *sys, const char *name)
  150. {
  151. int fd, n;
  152. char *filename;
  153. void *bf = NULL, *nbf;
  154. size_t size = 0, alloc_size = 0;
  155. struct event_format *format = NULL;
  156. if (asprintf(&filename, "%s/%s/%s/format", tracing_events_path, sys, name) < 0)
  157. goto out;
  158. fd = open(filename, O_RDONLY);
  159. if (fd < 0)
  160. goto out_free_filename;
  161. do {
  162. if (size == alloc_size) {
  163. alloc_size += BUFSIZ;
  164. nbf = realloc(bf, alloc_size);
  165. if (nbf == NULL)
  166. goto out_free_bf;
  167. bf = nbf;
  168. }
  169. n = read(fd, bf + size, alloc_size - size);
  170. if (n < 0)
  171. goto out_free_bf;
  172. size += n;
  173. } while (n > 0);
  174. pevent_parse_format(&format, bf, size, sys);
  175. out_free_bf:
  176. free(bf);
  177. close(fd);
  178. out_free_filename:
  179. free(filename);
  180. out:
  181. return format;
  182. }
  183. struct perf_evsel *perf_evsel__newtp(const char *sys, const char *name, int idx)
  184. {
  185. struct perf_evsel *evsel = zalloc(sizeof(*evsel));
  186. if (evsel != NULL) {
  187. struct perf_event_attr attr = {
  188. .type = PERF_TYPE_TRACEPOINT,
  189. .sample_type = (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME |
  190. PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD),
  191. };
  192. if (asprintf(&evsel->name, "%s:%s", sys, name) < 0)
  193. goto out_free;
  194. evsel->tp_format = event_format__new(sys, name);
  195. if (evsel->tp_format == NULL)
  196. goto out_free;
  197. event_attr_init(&attr);
  198. attr.config = evsel->tp_format->id;
  199. attr.sample_period = 1;
  200. perf_evsel__init(evsel, &attr, idx);
  201. }
  202. return evsel;
  203. out_free:
  204. free(evsel->name);
  205. free(evsel);
  206. return NULL;
  207. }
  208. const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX] = {
  209. "cycles",
  210. "instructions",
  211. "cache-references",
  212. "cache-misses",
  213. "branches",
  214. "branch-misses",
  215. "bus-cycles",
  216. "stalled-cycles-frontend",
  217. "stalled-cycles-backend",
  218. "ref-cycles",
  219. };
  220. static const char *__perf_evsel__hw_name(u64 config)
  221. {
  222. if (config < PERF_COUNT_HW_MAX && perf_evsel__hw_names[config])
  223. return perf_evsel__hw_names[config];
  224. return "unknown-hardware";
  225. }
  226. static int perf_evsel__add_modifiers(struct perf_evsel *evsel, char *bf, size_t size)
  227. {
  228. int colon = 0, r = 0;
  229. struct perf_event_attr *attr = &evsel->attr;
  230. bool exclude_guest_default = false;
  231. #define MOD_PRINT(context, mod) do { \
  232. if (!attr->exclude_##context) { \
  233. if (!colon) colon = ++r; \
  234. r += scnprintf(bf + r, size - r, "%c", mod); \
  235. } } while(0)
  236. if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) {
  237. MOD_PRINT(kernel, 'k');
  238. MOD_PRINT(user, 'u');
  239. MOD_PRINT(hv, 'h');
  240. exclude_guest_default = true;
  241. }
  242. if (attr->precise_ip) {
  243. if (!colon)
  244. colon = ++r;
  245. r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp");
  246. exclude_guest_default = true;
  247. }
  248. if (attr->exclude_host || attr->exclude_guest == exclude_guest_default) {
  249. MOD_PRINT(host, 'H');
  250. MOD_PRINT(guest, 'G');
  251. }
  252. #undef MOD_PRINT
  253. if (colon)
  254. bf[colon - 1] = ':';
  255. return r;
  256. }
  257. static int perf_evsel__hw_name(struct perf_evsel *evsel, char *bf, size_t size)
  258. {
  259. int r = scnprintf(bf, size, "%s", __perf_evsel__hw_name(evsel->attr.config));
  260. return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
  261. }
  262. const char *perf_evsel__sw_names[PERF_COUNT_SW_MAX] = {
  263. "cpu-clock",
  264. "task-clock",
  265. "page-faults",
  266. "context-switches",
  267. "cpu-migrations",
  268. "minor-faults",
  269. "major-faults",
  270. "alignment-faults",
  271. "emulation-faults",
  272. };
  273. static const char *__perf_evsel__sw_name(u64 config)
  274. {
  275. if (config < PERF_COUNT_SW_MAX && perf_evsel__sw_names[config])
  276. return perf_evsel__sw_names[config];
  277. return "unknown-software";
  278. }
  279. static int perf_evsel__sw_name(struct perf_evsel *evsel, char *bf, size_t size)
  280. {
  281. int r = scnprintf(bf, size, "%s", __perf_evsel__sw_name(evsel->attr.config));
  282. return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
  283. }
  284. static int __perf_evsel__bp_name(char *bf, size_t size, u64 addr, u64 type)
  285. {
  286. int r;
  287. r = scnprintf(bf, size, "mem:0x%" PRIx64 ":", addr);
  288. if (type & HW_BREAKPOINT_R)
  289. r += scnprintf(bf + r, size - r, "r");
  290. if (type & HW_BREAKPOINT_W)
  291. r += scnprintf(bf + r, size - r, "w");
  292. if (type & HW_BREAKPOINT_X)
  293. r += scnprintf(bf + r, size - r, "x");
  294. return r;
  295. }
  296. static int perf_evsel__bp_name(struct perf_evsel *evsel, char *bf, size_t size)
  297. {
  298. struct perf_event_attr *attr = &evsel->attr;
  299. int r = __perf_evsel__bp_name(bf, size, attr->bp_addr, attr->bp_type);
  300. return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
  301. }
  302. const char *perf_evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX]
  303. [PERF_EVSEL__MAX_ALIASES] = {
  304. { "L1-dcache", "l1-d", "l1d", "L1-data", },
  305. { "L1-icache", "l1-i", "l1i", "L1-instruction", },
  306. { "LLC", "L2", },
  307. { "dTLB", "d-tlb", "Data-TLB", },
  308. { "iTLB", "i-tlb", "Instruction-TLB", },
  309. { "branch", "branches", "bpu", "btb", "bpc", },
  310. { "node", },
  311. };
  312. const char *perf_evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX]
  313. [PERF_EVSEL__MAX_ALIASES] = {
  314. { "load", "loads", "read", },
  315. { "store", "stores", "write", },
  316. { "prefetch", "prefetches", "speculative-read", "speculative-load", },
  317. };
  318. const char *perf_evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX]
  319. [PERF_EVSEL__MAX_ALIASES] = {
  320. { "refs", "Reference", "ops", "access", },
  321. { "misses", "miss", },
  322. };
  323. #define C(x) PERF_COUNT_HW_CACHE_##x
  324. #define CACHE_READ (1 << C(OP_READ))
  325. #define CACHE_WRITE (1 << C(OP_WRITE))
  326. #define CACHE_PREFETCH (1 << C(OP_PREFETCH))
  327. #define COP(x) (1 << x)
  328. /*
  329. * cache operartion stat
  330. * L1I : Read and prefetch only
  331. * ITLB and BPU : Read-only
  332. */
  333. static unsigned long perf_evsel__hw_cache_stat[C(MAX)] = {
  334. [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
  335. [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
  336. [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
  337. [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
  338. [C(ITLB)] = (CACHE_READ),
  339. [C(BPU)] = (CACHE_READ),
  340. [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
  341. };
  342. bool perf_evsel__is_cache_op_valid(u8 type, u8 op)
  343. {
  344. if (perf_evsel__hw_cache_stat[type] & COP(op))
  345. return true; /* valid */
  346. else
  347. return false; /* invalid */
  348. }
  349. int __perf_evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result,
  350. char *bf, size_t size)
  351. {
  352. if (result) {
  353. return scnprintf(bf, size, "%s-%s-%s", perf_evsel__hw_cache[type][0],
  354. perf_evsel__hw_cache_op[op][0],
  355. perf_evsel__hw_cache_result[result][0]);
  356. }
  357. return scnprintf(bf, size, "%s-%s", perf_evsel__hw_cache[type][0],
  358. perf_evsel__hw_cache_op[op][1]);
  359. }
  360. static int __perf_evsel__hw_cache_name(u64 config, char *bf, size_t size)
  361. {
  362. u8 op, result, type = (config >> 0) & 0xff;
  363. const char *err = "unknown-ext-hardware-cache-type";
  364. if (type > PERF_COUNT_HW_CACHE_MAX)
  365. goto out_err;
  366. op = (config >> 8) & 0xff;
  367. err = "unknown-ext-hardware-cache-op";
  368. if (op > PERF_COUNT_HW_CACHE_OP_MAX)
  369. goto out_err;
  370. result = (config >> 16) & 0xff;
  371. err = "unknown-ext-hardware-cache-result";
  372. if (result > PERF_COUNT_HW_CACHE_RESULT_MAX)
  373. goto out_err;
  374. err = "invalid-cache";
  375. if (!perf_evsel__is_cache_op_valid(type, op))
  376. goto out_err;
  377. return __perf_evsel__hw_cache_type_op_res_name(type, op, result, bf, size);
  378. out_err:
  379. return scnprintf(bf, size, "%s", err);
  380. }
  381. static int perf_evsel__hw_cache_name(struct perf_evsel *evsel, char *bf, size_t size)
  382. {
  383. int ret = __perf_evsel__hw_cache_name(evsel->attr.config, bf, size);
  384. return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
  385. }
  386. static int perf_evsel__raw_name(struct perf_evsel *evsel, char *bf, size_t size)
  387. {
  388. int ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->attr.config);
  389. return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
  390. }
  391. const char *perf_evsel__name(struct perf_evsel *evsel)
  392. {
  393. char bf[128];
  394. if (evsel->name)
  395. return evsel->name;
  396. switch (evsel->attr.type) {
  397. case PERF_TYPE_RAW:
  398. perf_evsel__raw_name(evsel, bf, sizeof(bf));
  399. break;
  400. case PERF_TYPE_HARDWARE:
  401. perf_evsel__hw_name(evsel, bf, sizeof(bf));
  402. break;
  403. case PERF_TYPE_HW_CACHE:
  404. perf_evsel__hw_cache_name(evsel, bf, sizeof(bf));
  405. break;
  406. case PERF_TYPE_SOFTWARE:
  407. perf_evsel__sw_name(evsel, bf, sizeof(bf));
  408. break;
  409. case PERF_TYPE_TRACEPOINT:
  410. scnprintf(bf, sizeof(bf), "%s", "unknown tracepoint");
  411. break;
  412. case PERF_TYPE_BREAKPOINT:
  413. perf_evsel__bp_name(evsel, bf, sizeof(bf));
  414. break;
  415. default:
  416. scnprintf(bf, sizeof(bf), "unknown attr type: %d",
  417. evsel->attr.type);
  418. break;
  419. }
  420. evsel->name = strdup(bf);
  421. return evsel->name ?: "unknown";
  422. }
  423. const char *perf_evsel__group_name(struct perf_evsel *evsel)
  424. {
  425. return evsel->group_name ?: "anon group";
  426. }
  427. int perf_evsel__group_desc(struct perf_evsel *evsel, char *buf, size_t size)
  428. {
  429. int ret;
  430. struct perf_evsel *pos;
  431. const char *group_name = perf_evsel__group_name(evsel);
  432. ret = scnprintf(buf, size, "%s", group_name);
  433. ret += scnprintf(buf + ret, size - ret, " { %s",
  434. perf_evsel__name(evsel));
  435. for_each_group_member(pos, evsel)
  436. ret += scnprintf(buf + ret, size - ret, ", %s",
  437. perf_evsel__name(pos));
  438. ret += scnprintf(buf + ret, size - ret, " }");
  439. return ret;
  440. }
  441. /*
  442. * The enable_on_exec/disabled value strategy:
  443. *
  444. * 1) For any type of traced program:
  445. * - all independent events and group leaders are disabled
  446. * - all group members are enabled
  447. *
  448. * Group members are ruled by group leaders. They need to
  449. * be enabled, because the group scheduling relies on that.
  450. *
  451. * 2) For traced programs executed by perf:
  452. * - all independent events and group leaders have
  453. * enable_on_exec set
  454. * - we don't specifically enable or disable any event during
  455. * the record command
  456. *
  457. * Independent events and group leaders are initially disabled
  458. * and get enabled by exec. Group members are ruled by group
  459. * leaders as stated in 1).
  460. *
  461. * 3) For traced programs attached by perf (pid/tid):
  462. * - we specifically enable or disable all events during
  463. * the record command
  464. *
  465. * When attaching events to already running traced we
  466. * enable/disable events specifically, as there's no
  467. * initial traced exec call.
  468. */
  469. void perf_evsel__config(struct perf_evsel *evsel,
  470. struct perf_record_opts *opts)
  471. {
  472. struct perf_evsel *leader = evsel->leader;
  473. struct perf_event_attr *attr = &evsel->attr;
  474. int track = !evsel->idx; /* only the first counter needs these */
  475. attr->sample_id_all = perf_missing_features.sample_id_all ? 0 : 1;
  476. attr->inherit = !opts->no_inherit;
  477. perf_evsel__set_sample_bit(evsel, IP);
  478. perf_evsel__set_sample_bit(evsel, TID);
  479. if (evsel->sample_read) {
  480. perf_evsel__set_sample_bit(evsel, READ);
  481. /*
  482. * We need ID even in case of single event, because
  483. * PERF_SAMPLE_READ process ID specific data.
  484. */
  485. perf_evsel__set_sample_id(evsel, false);
  486. /*
  487. * Apply group format only if we belong to group
  488. * with more than one members.
  489. */
  490. if (leader->nr_members > 1) {
  491. attr->read_format |= PERF_FORMAT_GROUP;
  492. attr->inherit = 0;
  493. }
  494. }
  495. /*
  496. * We default some events to a 1 default interval. But keep
  497. * it a weak assumption overridable by the user.
  498. */
  499. if (!attr->sample_period || (opts->user_freq != UINT_MAX &&
  500. opts->user_interval != ULLONG_MAX)) {
  501. if (opts->freq) {
  502. perf_evsel__set_sample_bit(evsel, PERIOD);
  503. attr->freq = 1;
  504. attr->sample_freq = opts->freq;
  505. } else {
  506. attr->sample_period = opts->default_interval;
  507. }
  508. }
  509. /*
  510. * Disable sampling for all group members other
  511. * than leader in case leader 'leads' the sampling.
  512. */
  513. if ((leader != evsel) && leader->sample_read) {
  514. attr->sample_freq = 0;
  515. attr->sample_period = 0;
  516. }
  517. if (opts->no_samples)
  518. attr->sample_freq = 0;
  519. if (opts->inherit_stat)
  520. attr->inherit_stat = 1;
  521. if (opts->sample_address) {
  522. perf_evsel__set_sample_bit(evsel, ADDR);
  523. attr->mmap_data = track;
  524. }
  525. if (opts->call_graph) {
  526. perf_evsel__set_sample_bit(evsel, CALLCHAIN);
  527. if (opts->call_graph == CALLCHAIN_DWARF) {
  528. perf_evsel__set_sample_bit(evsel, REGS_USER);
  529. perf_evsel__set_sample_bit(evsel, STACK_USER);
  530. attr->sample_regs_user = PERF_REGS_MASK;
  531. attr->sample_stack_user = opts->stack_dump_size;
  532. attr->exclude_callchain_user = 1;
  533. }
  534. }
  535. if (perf_target__has_cpu(&opts->target))
  536. perf_evsel__set_sample_bit(evsel, CPU);
  537. if (opts->period)
  538. perf_evsel__set_sample_bit(evsel, PERIOD);
  539. if (!perf_missing_features.sample_id_all &&
  540. (opts->sample_time || !opts->no_inherit ||
  541. perf_target__has_cpu(&opts->target)))
  542. perf_evsel__set_sample_bit(evsel, TIME);
  543. if (opts->raw_samples) {
  544. perf_evsel__set_sample_bit(evsel, TIME);
  545. perf_evsel__set_sample_bit(evsel, RAW);
  546. perf_evsel__set_sample_bit(evsel, CPU);
  547. }
  548. if (opts->sample_address)
  549. attr->sample_type |= PERF_SAMPLE_DATA_SRC;
  550. if (opts->no_delay) {
  551. attr->watermark = 0;
  552. attr->wakeup_events = 1;
  553. }
  554. if (opts->branch_stack) {
  555. perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
  556. attr->branch_sample_type = opts->branch_stack;
  557. }
  558. if (opts->sample_weight)
  559. attr->sample_type |= PERF_SAMPLE_WEIGHT;
  560. attr->mmap = track;
  561. attr->comm = track;
  562. /*
  563. * XXX see the function comment above
  564. *
  565. * Disabling only independent events or group leaders,
  566. * keeping group members enabled.
  567. */
  568. if (perf_evsel__is_group_leader(evsel))
  569. attr->disabled = 1;
  570. /*
  571. * Setting enable_on_exec for independent events and
  572. * group leaders for traced executed by perf.
  573. */
  574. if (perf_target__none(&opts->target) && perf_evsel__is_group_leader(evsel))
  575. attr->enable_on_exec = 1;
  576. }
  577. int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
  578. {
  579. int cpu, thread;
  580. evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
  581. if (evsel->fd) {
  582. for (cpu = 0; cpu < ncpus; cpu++) {
  583. for (thread = 0; thread < nthreads; thread++) {
  584. FD(evsel, cpu, thread) = -1;
  585. }
  586. }
  587. }
  588. return evsel->fd != NULL ? 0 : -ENOMEM;
  589. }
  590. static int perf_evsel__run_ioctl(struct perf_evsel *evsel, int ncpus, int nthreads,
  591. int ioc, void *arg)
  592. {
  593. int cpu, thread;
  594. for (cpu = 0; cpu < ncpus; cpu++) {
  595. for (thread = 0; thread < nthreads; thread++) {
  596. int fd = FD(evsel, cpu, thread),
  597. err = ioctl(fd, ioc, arg);
  598. if (err)
  599. return err;
  600. }
  601. }
  602. return 0;
  603. }
  604. int perf_evsel__set_filter(struct perf_evsel *evsel, int ncpus, int nthreads,
  605. const char *filter)
  606. {
  607. return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
  608. PERF_EVENT_IOC_SET_FILTER,
  609. (void *)filter);
  610. }
  611. int perf_evsel__enable(struct perf_evsel *evsel, int ncpus, int nthreads)
  612. {
  613. return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
  614. PERF_EVENT_IOC_ENABLE,
  615. 0);
  616. }
  617. int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
  618. {
  619. evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
  620. if (evsel->sample_id == NULL)
  621. return -ENOMEM;
  622. evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
  623. if (evsel->id == NULL) {
  624. xyarray__delete(evsel->sample_id);
  625. evsel->sample_id = NULL;
  626. return -ENOMEM;
  627. }
  628. return 0;
  629. }
  630. void perf_evsel__reset_counts(struct perf_evsel *evsel, int ncpus)
  631. {
  632. memset(evsel->counts, 0, (sizeof(*evsel->counts) +
  633. (ncpus * sizeof(struct perf_counts_values))));
  634. }
  635. int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
  636. {
  637. evsel->counts = zalloc((sizeof(*evsel->counts) +
  638. (ncpus * sizeof(struct perf_counts_values))));
  639. return evsel->counts != NULL ? 0 : -ENOMEM;
  640. }
  641. void perf_evsel__free_fd(struct perf_evsel *evsel)
  642. {
  643. xyarray__delete(evsel->fd);
  644. evsel->fd = NULL;
  645. }
  646. void perf_evsel__free_id(struct perf_evsel *evsel)
  647. {
  648. xyarray__delete(evsel->sample_id);
  649. evsel->sample_id = NULL;
  650. free(evsel->id);
  651. evsel->id = NULL;
  652. }
  653. void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
  654. {
  655. int cpu, thread;
  656. for (cpu = 0; cpu < ncpus; cpu++)
  657. for (thread = 0; thread < nthreads; ++thread) {
  658. close(FD(evsel, cpu, thread));
  659. FD(evsel, cpu, thread) = -1;
  660. }
  661. }
  662. void perf_evsel__free_counts(struct perf_evsel *evsel)
  663. {
  664. free(evsel->counts);
  665. }
  666. void perf_evsel__exit(struct perf_evsel *evsel)
  667. {
  668. assert(list_empty(&evsel->node));
  669. perf_evsel__free_fd(evsel);
  670. perf_evsel__free_id(evsel);
  671. }
  672. void perf_evsel__delete(struct perf_evsel *evsel)
  673. {
  674. perf_evsel__exit(evsel);
  675. close_cgroup(evsel->cgrp);
  676. free(evsel->group_name);
  677. if (evsel->tp_format)
  678. pevent_free_format(evsel->tp_format);
  679. free(evsel->name);
  680. free(evsel);
  681. }
  682. static inline void compute_deltas(struct perf_evsel *evsel,
  683. int cpu,
  684. struct perf_counts_values *count)
  685. {
  686. struct perf_counts_values tmp;
  687. if (!evsel->prev_raw_counts)
  688. return;
  689. if (cpu == -1) {
  690. tmp = evsel->prev_raw_counts->aggr;
  691. evsel->prev_raw_counts->aggr = *count;
  692. } else {
  693. tmp = evsel->prev_raw_counts->cpu[cpu];
  694. evsel->prev_raw_counts->cpu[cpu] = *count;
  695. }
  696. count->val = count->val - tmp.val;
  697. count->ena = count->ena - tmp.ena;
  698. count->run = count->run - tmp.run;
  699. }
  700. int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
  701. int cpu, int thread, bool scale)
  702. {
  703. struct perf_counts_values count;
  704. size_t nv = scale ? 3 : 1;
  705. if (FD(evsel, cpu, thread) < 0)
  706. return -EINVAL;
  707. if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
  708. return -ENOMEM;
  709. if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
  710. return -errno;
  711. compute_deltas(evsel, cpu, &count);
  712. if (scale) {
  713. if (count.run == 0)
  714. count.val = 0;
  715. else if (count.run < count.ena)
  716. count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
  717. } else
  718. count.ena = count.run = 0;
  719. evsel->counts->cpu[cpu] = count;
  720. return 0;
  721. }
  722. int __perf_evsel__read(struct perf_evsel *evsel,
  723. int ncpus, int nthreads, bool scale)
  724. {
  725. size_t nv = scale ? 3 : 1;
  726. int cpu, thread;
  727. struct perf_counts_values *aggr = &evsel->counts->aggr, count;
  728. aggr->val = aggr->ena = aggr->run = 0;
  729. for (cpu = 0; cpu < ncpus; cpu++) {
  730. for (thread = 0; thread < nthreads; thread++) {
  731. if (FD(evsel, cpu, thread) < 0)
  732. continue;
  733. if (readn(FD(evsel, cpu, thread),
  734. &count, nv * sizeof(u64)) < 0)
  735. return -errno;
  736. aggr->val += count.val;
  737. if (scale) {
  738. aggr->ena += count.ena;
  739. aggr->run += count.run;
  740. }
  741. }
  742. }
  743. compute_deltas(evsel, -1, aggr);
  744. evsel->counts->scaled = 0;
  745. if (scale) {
  746. if (aggr->run == 0) {
  747. evsel->counts->scaled = -1;
  748. aggr->val = 0;
  749. return 0;
  750. }
  751. if (aggr->run < aggr->ena) {
  752. evsel->counts->scaled = 1;
  753. aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
  754. }
  755. } else
  756. aggr->ena = aggr->run = 0;
  757. return 0;
  758. }
  759. static int get_group_fd(struct perf_evsel *evsel, int cpu, int thread)
  760. {
  761. struct perf_evsel *leader = evsel->leader;
  762. int fd;
  763. if (perf_evsel__is_group_leader(evsel))
  764. return -1;
  765. /*
  766. * Leader must be already processed/open,
  767. * if not it's a bug.
  768. */
  769. BUG_ON(!leader->fd);
  770. fd = FD(leader, cpu, thread);
  771. BUG_ON(fd == -1);
  772. return fd;
  773. }
  774. #define __PRINT_ATTR(fmt, cast, field) \
  775. fprintf(fp, " %-19s "fmt"\n", #field, cast attr->field)
  776. #define PRINT_ATTR_U32(field) __PRINT_ATTR("%u" , , field)
  777. #define PRINT_ATTR_X32(field) __PRINT_ATTR("%#x", , field)
  778. #define PRINT_ATTR_U64(field) __PRINT_ATTR("%" PRIu64, (uint64_t), field)
  779. #define PRINT_ATTR_X64(field) __PRINT_ATTR("%#"PRIx64, (uint64_t), field)
  780. #define PRINT_ATTR2N(name1, field1, name2, field2) \
  781. fprintf(fp, " %-19s %u %-19s %u\n", \
  782. name1, attr->field1, name2, attr->field2)
  783. #define PRINT_ATTR2(field1, field2) \
  784. PRINT_ATTR2N(#field1, field1, #field2, field2)
  785. static size_t perf_event_attr__fprintf(struct perf_event_attr *attr, FILE *fp)
  786. {
  787. size_t ret = 0;
  788. ret += fprintf(fp, "%.60s\n", graph_dotted_line);
  789. ret += fprintf(fp, "perf_event_attr:\n");
  790. ret += PRINT_ATTR_U32(type);
  791. ret += PRINT_ATTR_U32(size);
  792. ret += PRINT_ATTR_X64(config);
  793. ret += PRINT_ATTR_U64(sample_period);
  794. ret += PRINT_ATTR_U64(sample_freq);
  795. ret += PRINT_ATTR_X64(sample_type);
  796. ret += PRINT_ATTR_X64(read_format);
  797. ret += PRINT_ATTR2(disabled, inherit);
  798. ret += PRINT_ATTR2(pinned, exclusive);
  799. ret += PRINT_ATTR2(exclude_user, exclude_kernel);
  800. ret += PRINT_ATTR2(exclude_hv, exclude_idle);
  801. ret += PRINT_ATTR2(mmap, comm);
  802. ret += PRINT_ATTR2(freq, inherit_stat);
  803. ret += PRINT_ATTR2(enable_on_exec, task);
  804. ret += PRINT_ATTR2(watermark, precise_ip);
  805. ret += PRINT_ATTR2(mmap_data, sample_id_all);
  806. ret += PRINT_ATTR2(exclude_host, exclude_guest);
  807. ret += PRINT_ATTR2N("excl.callchain_kern", exclude_callchain_kernel,
  808. "excl.callchain_user", exclude_callchain_user);
  809. ret += PRINT_ATTR_U32(wakeup_events);
  810. ret += PRINT_ATTR_U32(wakeup_watermark);
  811. ret += PRINT_ATTR_X32(bp_type);
  812. ret += PRINT_ATTR_X64(bp_addr);
  813. ret += PRINT_ATTR_X64(config1);
  814. ret += PRINT_ATTR_U64(bp_len);
  815. ret += PRINT_ATTR_X64(config2);
  816. ret += PRINT_ATTR_X64(branch_sample_type);
  817. ret += PRINT_ATTR_X64(sample_regs_user);
  818. ret += PRINT_ATTR_U32(sample_stack_user);
  819. ret += fprintf(fp, "%.60s\n", graph_dotted_line);
  820. return ret;
  821. }
  822. static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
  823. struct thread_map *threads)
  824. {
  825. int cpu, thread;
  826. unsigned long flags = 0;
  827. int pid = -1, err;
  828. enum { NO_CHANGE, SET_TO_MAX, INCREASED_MAX } set_rlimit = NO_CHANGE;
  829. if (evsel->fd == NULL &&
  830. perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
  831. return -ENOMEM;
  832. if (evsel->cgrp) {
  833. flags = PERF_FLAG_PID_CGROUP;
  834. pid = evsel->cgrp->fd;
  835. }
  836. fallback_missing_features:
  837. if (perf_missing_features.exclude_guest)
  838. evsel->attr.exclude_guest = evsel->attr.exclude_host = 0;
  839. retry_sample_id:
  840. if (perf_missing_features.sample_id_all)
  841. evsel->attr.sample_id_all = 0;
  842. if (verbose >= 2)
  843. perf_event_attr__fprintf(&evsel->attr, stderr);
  844. for (cpu = 0; cpu < cpus->nr; cpu++) {
  845. for (thread = 0; thread < threads->nr; thread++) {
  846. int group_fd;
  847. if (!evsel->cgrp)
  848. pid = threads->map[thread];
  849. group_fd = get_group_fd(evsel, cpu, thread);
  850. retry_open:
  851. pr_debug2("perf_event_open: pid %d cpu %d group_fd %d flags %#lx\n",
  852. pid, cpus->map[cpu], group_fd, flags);
  853. FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
  854. pid,
  855. cpus->map[cpu],
  856. group_fd, flags);
  857. if (FD(evsel, cpu, thread) < 0) {
  858. err = -errno;
  859. goto try_fallback;
  860. }
  861. set_rlimit = NO_CHANGE;
  862. }
  863. }
  864. return 0;
  865. try_fallback:
  866. /*
  867. * perf stat needs between 5 and 22 fds per CPU. When we run out
  868. * of them try to increase the limits.
  869. */
  870. if (err == -EMFILE && set_rlimit < INCREASED_MAX) {
  871. struct rlimit l;
  872. int old_errno = errno;
  873. if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
  874. if (set_rlimit == NO_CHANGE)
  875. l.rlim_cur = l.rlim_max;
  876. else {
  877. l.rlim_cur = l.rlim_max + 1000;
  878. l.rlim_max = l.rlim_cur;
  879. }
  880. if (setrlimit(RLIMIT_NOFILE, &l) == 0) {
  881. set_rlimit++;
  882. errno = old_errno;
  883. goto retry_open;
  884. }
  885. }
  886. errno = old_errno;
  887. }
  888. if (err != -EINVAL || cpu > 0 || thread > 0)
  889. goto out_close;
  890. if (!perf_missing_features.exclude_guest &&
  891. (evsel->attr.exclude_guest || evsel->attr.exclude_host)) {
  892. perf_missing_features.exclude_guest = true;
  893. goto fallback_missing_features;
  894. } else if (!perf_missing_features.sample_id_all) {
  895. perf_missing_features.sample_id_all = true;
  896. goto retry_sample_id;
  897. }
  898. out_close:
  899. do {
  900. while (--thread >= 0) {
  901. close(FD(evsel, cpu, thread));
  902. FD(evsel, cpu, thread) = -1;
  903. }
  904. thread = threads->nr;
  905. } while (--cpu >= 0);
  906. return err;
  907. }
  908. void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
  909. {
  910. if (evsel->fd == NULL)
  911. return;
  912. perf_evsel__close_fd(evsel, ncpus, nthreads);
  913. perf_evsel__free_fd(evsel);
  914. evsel->fd = NULL;
  915. }
  916. static struct {
  917. struct cpu_map map;
  918. int cpus[1];
  919. } empty_cpu_map = {
  920. .map.nr = 1,
  921. .cpus = { -1, },
  922. };
  923. static struct {
  924. struct thread_map map;
  925. int threads[1];
  926. } empty_thread_map = {
  927. .map.nr = 1,
  928. .threads = { -1, },
  929. };
  930. int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
  931. struct thread_map *threads)
  932. {
  933. if (cpus == NULL) {
  934. /* Work around old compiler warnings about strict aliasing */
  935. cpus = &empty_cpu_map.map;
  936. }
  937. if (threads == NULL)
  938. threads = &empty_thread_map.map;
  939. return __perf_evsel__open(evsel, cpus, threads);
  940. }
  941. int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
  942. struct cpu_map *cpus)
  943. {
  944. return __perf_evsel__open(evsel, cpus, &empty_thread_map.map);
  945. }
  946. int perf_evsel__open_per_thread(struct perf_evsel *evsel,
  947. struct thread_map *threads)
  948. {
  949. return __perf_evsel__open(evsel, &empty_cpu_map.map, threads);
  950. }
  951. static int perf_evsel__parse_id_sample(const struct perf_evsel *evsel,
  952. const union perf_event *event,
  953. struct perf_sample *sample)
  954. {
  955. u64 type = evsel->attr.sample_type;
  956. const u64 *array = event->sample.array;
  957. bool swapped = evsel->needs_swap;
  958. union u64_swap u;
  959. array += ((event->header.size -
  960. sizeof(event->header)) / sizeof(u64)) - 1;
  961. if (type & PERF_SAMPLE_IDENTIFIER) {
  962. sample->id = *array;
  963. array--;
  964. }
  965. if (type & PERF_SAMPLE_CPU) {
  966. u.val64 = *array;
  967. if (swapped) {
  968. /* undo swap of u64, then swap on individual u32s */
  969. u.val64 = bswap_64(u.val64);
  970. u.val32[0] = bswap_32(u.val32[0]);
  971. }
  972. sample->cpu = u.val32[0];
  973. array--;
  974. }
  975. if (type & PERF_SAMPLE_STREAM_ID) {
  976. sample->stream_id = *array;
  977. array--;
  978. }
  979. if (type & PERF_SAMPLE_ID) {
  980. sample->id = *array;
  981. array--;
  982. }
  983. if (type & PERF_SAMPLE_TIME) {
  984. sample->time = *array;
  985. array--;
  986. }
  987. if (type & PERF_SAMPLE_TID) {
  988. u.val64 = *array;
  989. if (swapped) {
  990. /* undo swap of u64, then swap on individual u32s */
  991. u.val64 = bswap_64(u.val64);
  992. u.val32[0] = bswap_32(u.val32[0]);
  993. u.val32[1] = bswap_32(u.val32[1]);
  994. }
  995. sample->pid = u.val32[0];
  996. sample->tid = u.val32[1];
  997. }
  998. return 0;
  999. }
  1000. static inline bool overflow(const void *endp, u16 max_size, const void *offset,
  1001. u64 size)
  1002. {
  1003. return size > max_size || offset + size > endp;
  1004. }
  1005. #define OVERFLOW_CHECK(offset, size, max_size) \
  1006. do { \
  1007. if (overflow(endp, (max_size), (offset), (size))) \
  1008. return -EFAULT; \
  1009. } while (0)
  1010. #define OVERFLOW_CHECK_u64(offset) \
  1011. OVERFLOW_CHECK(offset, sizeof(u64), sizeof(u64))
  1012. int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
  1013. struct perf_sample *data)
  1014. {
  1015. u64 type = evsel->attr.sample_type;
  1016. bool swapped = evsel->needs_swap;
  1017. const u64 *array;
  1018. u16 max_size = event->header.size;
  1019. const void *endp = (void *)event + max_size;
  1020. u64 sz;
  1021. /*
  1022. * used for cross-endian analysis. See git commit 65014ab3
  1023. * for why this goofiness is needed.
  1024. */
  1025. union u64_swap u;
  1026. memset(data, 0, sizeof(*data));
  1027. data->cpu = data->pid = data->tid = -1;
  1028. data->stream_id = data->id = data->time = -1ULL;
  1029. data->period = 1;
  1030. data->weight = 0;
  1031. if (event->header.type != PERF_RECORD_SAMPLE) {
  1032. if (!evsel->attr.sample_id_all)
  1033. return 0;
  1034. return perf_evsel__parse_id_sample(evsel, event, data);
  1035. }
  1036. array = event->sample.array;
  1037. /*
  1038. * The evsel's sample_size is based on PERF_SAMPLE_MASK which includes
  1039. * up to PERF_SAMPLE_PERIOD. After that overflow() must be used to
  1040. * check the format does not go past the end of the event.
  1041. */
  1042. if (evsel->sample_size + sizeof(event->header) > event->header.size)
  1043. return -EFAULT;
  1044. data->id = -1ULL;
  1045. if (type & PERF_SAMPLE_IDENTIFIER) {
  1046. data->id = *array;
  1047. array++;
  1048. }
  1049. if (type & PERF_SAMPLE_IP) {
  1050. data->ip = *array;
  1051. array++;
  1052. }
  1053. if (type & PERF_SAMPLE_TID) {
  1054. u.val64 = *array;
  1055. if (swapped) {
  1056. /* undo swap of u64, then swap on individual u32s */
  1057. u.val64 = bswap_64(u.val64);
  1058. u.val32[0] = bswap_32(u.val32[0]);
  1059. u.val32[1] = bswap_32(u.val32[1]);
  1060. }
  1061. data->pid = u.val32[0];
  1062. data->tid = u.val32[1];
  1063. array++;
  1064. }
  1065. if (type & PERF_SAMPLE_TIME) {
  1066. data->time = *array;
  1067. array++;
  1068. }
  1069. data->addr = 0;
  1070. if (type & PERF_SAMPLE_ADDR) {
  1071. data->addr = *array;
  1072. array++;
  1073. }
  1074. if (type & PERF_SAMPLE_ID) {
  1075. data->id = *array;
  1076. array++;
  1077. }
  1078. if (type & PERF_SAMPLE_STREAM_ID) {
  1079. data->stream_id = *array;
  1080. array++;
  1081. }
  1082. if (type & PERF_SAMPLE_CPU) {
  1083. u.val64 = *array;
  1084. if (swapped) {
  1085. /* undo swap of u64, then swap on individual u32s */
  1086. u.val64 = bswap_64(u.val64);
  1087. u.val32[0] = bswap_32(u.val32[0]);
  1088. }
  1089. data->cpu = u.val32[0];
  1090. array++;
  1091. }
  1092. if (type & PERF_SAMPLE_PERIOD) {
  1093. data->period = *array;
  1094. array++;
  1095. }
  1096. if (type & PERF_SAMPLE_READ) {
  1097. u64 read_format = evsel->attr.read_format;
  1098. OVERFLOW_CHECK_u64(array);
  1099. if (read_format & PERF_FORMAT_GROUP)
  1100. data->read.group.nr = *array;
  1101. else
  1102. data->read.one.value = *array;
  1103. array++;
  1104. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
  1105. OVERFLOW_CHECK_u64(array);
  1106. data->read.time_enabled = *array;
  1107. array++;
  1108. }
  1109. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
  1110. OVERFLOW_CHECK_u64(array);
  1111. data->read.time_running = *array;
  1112. array++;
  1113. }
  1114. /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
  1115. if (read_format & PERF_FORMAT_GROUP) {
  1116. const u64 max_group_nr = UINT64_MAX /
  1117. sizeof(struct sample_read_value);
  1118. if (data->read.group.nr > max_group_nr)
  1119. return -EFAULT;
  1120. sz = data->read.group.nr *
  1121. sizeof(struct sample_read_value);
  1122. OVERFLOW_CHECK(array, sz, max_size);
  1123. data->read.group.values =
  1124. (struct sample_read_value *)array;
  1125. array = (void *)array + sz;
  1126. } else {
  1127. OVERFLOW_CHECK_u64(array);
  1128. data->read.one.id = *array;
  1129. array++;
  1130. }
  1131. }
  1132. if (type & PERF_SAMPLE_CALLCHAIN) {
  1133. const u64 max_callchain_nr = UINT64_MAX / sizeof(u64);
  1134. OVERFLOW_CHECK_u64(array);
  1135. data->callchain = (struct ip_callchain *)array++;
  1136. if (data->callchain->nr > max_callchain_nr)
  1137. return -EFAULT;
  1138. sz = data->callchain->nr * sizeof(u64);
  1139. OVERFLOW_CHECK(array, sz, max_size);
  1140. array = (void *)array + sz;
  1141. }
  1142. if (type & PERF_SAMPLE_RAW) {
  1143. OVERFLOW_CHECK_u64(array);
  1144. u.val64 = *array;
  1145. if (WARN_ONCE(swapped,
  1146. "Endianness of raw data not corrected!\n")) {
  1147. /* undo swap of u64, then swap on individual u32s */
  1148. u.val64 = bswap_64(u.val64);
  1149. u.val32[0] = bswap_32(u.val32[0]);
  1150. u.val32[1] = bswap_32(u.val32[1]);
  1151. }
  1152. data->raw_size = u.val32[0];
  1153. array = (void *)array + sizeof(u32);
  1154. OVERFLOW_CHECK(array, data->raw_size, max_size);
  1155. data->raw_data = (void *)array;
  1156. array = (void *)array + data->raw_size;
  1157. }
  1158. if (type & PERF_SAMPLE_BRANCH_STACK) {
  1159. const u64 max_branch_nr = UINT64_MAX /
  1160. sizeof(struct branch_entry);
  1161. OVERFLOW_CHECK_u64(array);
  1162. data->branch_stack = (struct branch_stack *)array++;
  1163. if (data->branch_stack->nr > max_branch_nr)
  1164. return -EFAULT;
  1165. sz = data->branch_stack->nr * sizeof(struct branch_entry);
  1166. OVERFLOW_CHECK(array, sz, max_size);
  1167. array = (void *)array + sz;
  1168. }
  1169. if (type & PERF_SAMPLE_REGS_USER) {
  1170. OVERFLOW_CHECK_u64(array);
  1171. data->user_regs.abi = *array;
  1172. array++;
  1173. if (data->user_regs.abi) {
  1174. u64 regs_user = evsel->attr.sample_regs_user;
  1175. sz = hweight_long(regs_user) * sizeof(u64);
  1176. OVERFLOW_CHECK(array, sz, max_size);
  1177. data->user_regs.regs = (u64 *)array;
  1178. array = (void *)array + sz;
  1179. }
  1180. }
  1181. if (type & PERF_SAMPLE_STACK_USER) {
  1182. OVERFLOW_CHECK_u64(array);
  1183. sz = *array++;
  1184. data->user_stack.offset = ((char *)(array - 1)
  1185. - (char *) event);
  1186. if (!sz) {
  1187. data->user_stack.size = 0;
  1188. } else {
  1189. OVERFLOW_CHECK(array, sz, max_size);
  1190. data->user_stack.data = (char *)array;
  1191. array = (void *)array + sz;
  1192. OVERFLOW_CHECK_u64(array);
  1193. data->user_stack.size = *array++;
  1194. }
  1195. }
  1196. data->weight = 0;
  1197. if (type & PERF_SAMPLE_WEIGHT) {
  1198. OVERFLOW_CHECK_u64(array);
  1199. data->weight = *array;
  1200. array++;
  1201. }
  1202. data->data_src = PERF_MEM_DATA_SRC_NONE;
  1203. if (type & PERF_SAMPLE_DATA_SRC) {
  1204. OVERFLOW_CHECK_u64(array);
  1205. data->data_src = *array;
  1206. array++;
  1207. }
  1208. return 0;
  1209. }
  1210. int perf_event__synthesize_sample(union perf_event *event, u64 type,
  1211. u64 sample_regs_user, u64 read_format,
  1212. const struct perf_sample *sample,
  1213. bool swapped)
  1214. {
  1215. u64 *array;
  1216. size_t sz;
  1217. /*
  1218. * used for cross-endian analysis. See git commit 65014ab3
  1219. * for why this goofiness is needed.
  1220. */
  1221. union u64_swap u;
  1222. array = event->sample.array;
  1223. if (type & PERF_SAMPLE_IDENTIFIER) {
  1224. *array = sample->id;
  1225. array++;
  1226. }
  1227. if (type & PERF_SAMPLE_IP) {
  1228. *array = sample->ip;
  1229. array++;
  1230. }
  1231. if (type & PERF_SAMPLE_TID) {
  1232. u.val32[0] = sample->pid;
  1233. u.val32[1] = sample->tid;
  1234. if (swapped) {
  1235. /*
  1236. * Inverse of what is done in perf_evsel__parse_sample
  1237. */
  1238. u.val32[0] = bswap_32(u.val32[0]);
  1239. u.val32[1] = bswap_32(u.val32[1]);
  1240. u.val64 = bswap_64(u.val64);
  1241. }
  1242. *array = u.val64;
  1243. array++;
  1244. }
  1245. if (type & PERF_SAMPLE_TIME) {
  1246. *array = sample->time;
  1247. array++;
  1248. }
  1249. if (type & PERF_SAMPLE_ADDR) {
  1250. *array = sample->addr;
  1251. array++;
  1252. }
  1253. if (type & PERF_SAMPLE_ID) {
  1254. *array = sample->id;
  1255. array++;
  1256. }
  1257. if (type & PERF_SAMPLE_STREAM_ID) {
  1258. *array = sample->stream_id;
  1259. array++;
  1260. }
  1261. if (type & PERF_SAMPLE_CPU) {
  1262. u.val32[0] = sample->cpu;
  1263. if (swapped) {
  1264. /*
  1265. * Inverse of what is done in perf_evsel__parse_sample
  1266. */
  1267. u.val32[0] = bswap_32(u.val32[0]);
  1268. u.val64 = bswap_64(u.val64);
  1269. }
  1270. *array = u.val64;
  1271. array++;
  1272. }
  1273. if (type & PERF_SAMPLE_PERIOD) {
  1274. *array = sample->period;
  1275. array++;
  1276. }
  1277. if (type & PERF_SAMPLE_READ) {
  1278. if (read_format & PERF_FORMAT_GROUP)
  1279. *array = sample->read.group.nr;
  1280. else
  1281. *array = sample->read.one.value;
  1282. array++;
  1283. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
  1284. *array = sample->read.time_enabled;
  1285. array++;
  1286. }
  1287. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
  1288. *array = sample->read.time_running;
  1289. array++;
  1290. }
  1291. /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
  1292. if (read_format & PERF_FORMAT_GROUP) {
  1293. sz = sample->read.group.nr *
  1294. sizeof(struct sample_read_value);
  1295. memcpy(array, sample->read.group.values, sz);
  1296. array = (void *)array + sz;
  1297. } else {
  1298. *array = sample->read.one.id;
  1299. array++;
  1300. }
  1301. }
  1302. if (type & PERF_SAMPLE_CALLCHAIN) {
  1303. sz = (sample->callchain->nr + 1) * sizeof(u64);
  1304. memcpy(array, sample->callchain, sz);
  1305. array = (void *)array + sz;
  1306. }
  1307. if (type & PERF_SAMPLE_RAW) {
  1308. u.val32[0] = sample->raw_size;
  1309. if (WARN_ONCE(swapped,
  1310. "Endianness of raw data not corrected!\n")) {
  1311. /*
  1312. * Inverse of what is done in perf_evsel__parse_sample
  1313. */
  1314. u.val32[0] = bswap_32(u.val32[0]);
  1315. u.val32[1] = bswap_32(u.val32[1]);
  1316. u.val64 = bswap_64(u.val64);
  1317. }
  1318. *array = u.val64;
  1319. array = (void *)array + sizeof(u32);
  1320. memcpy(array, sample->raw_data, sample->raw_size);
  1321. array = (void *)array + sample->raw_size;
  1322. }
  1323. if (type & PERF_SAMPLE_BRANCH_STACK) {
  1324. sz = sample->branch_stack->nr * sizeof(struct branch_entry);
  1325. sz += sizeof(u64);
  1326. memcpy(array, sample->branch_stack, sz);
  1327. array = (void *)array + sz;
  1328. }
  1329. if (type & PERF_SAMPLE_REGS_USER) {
  1330. if (sample->user_regs.abi) {
  1331. *array++ = sample->user_regs.abi;
  1332. sz = hweight_long(sample_regs_user) * sizeof(u64);
  1333. memcpy(array, sample->user_regs.regs, sz);
  1334. array = (void *)array + sz;
  1335. } else {
  1336. *array++ = 0;
  1337. }
  1338. }
  1339. if (type & PERF_SAMPLE_STACK_USER) {
  1340. sz = sample->user_stack.size;
  1341. *array++ = sz;
  1342. if (sz) {
  1343. memcpy(array, sample->user_stack.data, sz);
  1344. array = (void *)array + sz;
  1345. *array++ = sz;
  1346. }
  1347. }
  1348. if (type & PERF_SAMPLE_WEIGHT) {
  1349. *array = sample->weight;
  1350. array++;
  1351. }
  1352. if (type & PERF_SAMPLE_DATA_SRC) {
  1353. *array = sample->data_src;
  1354. array++;
  1355. }
  1356. return 0;
  1357. }
  1358. struct format_field *perf_evsel__field(struct perf_evsel *evsel, const char *name)
  1359. {
  1360. return pevent_find_field(evsel->tp_format, name);
  1361. }
  1362. void *perf_evsel__rawptr(struct perf_evsel *evsel, struct perf_sample *sample,
  1363. const char *name)
  1364. {
  1365. struct format_field *field = perf_evsel__field(evsel, name);
  1366. int offset;
  1367. if (!field)
  1368. return NULL;
  1369. offset = field->offset;
  1370. if (field->flags & FIELD_IS_DYNAMIC) {
  1371. offset = *(int *)(sample->raw_data + field->offset);
  1372. offset &= 0xffff;
  1373. }
  1374. return sample->raw_data + offset;
  1375. }
  1376. u64 perf_evsel__intval(struct perf_evsel *evsel, struct perf_sample *sample,
  1377. const char *name)
  1378. {
  1379. struct format_field *field = perf_evsel__field(evsel, name);
  1380. void *ptr;
  1381. u64 value;
  1382. if (!field)
  1383. return 0;
  1384. ptr = sample->raw_data + field->offset;
  1385. switch (field->size) {
  1386. case 1:
  1387. return *(u8 *)ptr;
  1388. case 2:
  1389. value = *(u16 *)ptr;
  1390. break;
  1391. case 4:
  1392. value = *(u32 *)ptr;
  1393. break;
  1394. case 8:
  1395. value = *(u64 *)ptr;
  1396. break;
  1397. default:
  1398. return 0;
  1399. }
  1400. if (!evsel->needs_swap)
  1401. return value;
  1402. switch (field->size) {
  1403. case 2:
  1404. return bswap_16(value);
  1405. case 4:
  1406. return bswap_32(value);
  1407. case 8:
  1408. return bswap_64(value);
  1409. default:
  1410. return 0;
  1411. }
  1412. return 0;
  1413. }
  1414. static int comma_fprintf(FILE *fp, bool *first, const char *fmt, ...)
  1415. {
  1416. va_list args;
  1417. int ret = 0;
  1418. if (!*first) {
  1419. ret += fprintf(fp, ",");
  1420. } else {
  1421. ret += fprintf(fp, ":");
  1422. *first = false;
  1423. }
  1424. va_start(args, fmt);
  1425. ret += vfprintf(fp, fmt, args);
  1426. va_end(args);
  1427. return ret;
  1428. }
  1429. static int __if_fprintf(FILE *fp, bool *first, const char *field, u64 value)
  1430. {
  1431. if (value == 0)
  1432. return 0;
  1433. return comma_fprintf(fp, first, " %s: %" PRIu64, field, value);
  1434. }
  1435. #define if_print(field) printed += __if_fprintf(fp, &first, #field, evsel->attr.field)
  1436. struct bit_names {
  1437. int bit;
  1438. const char *name;
  1439. };
  1440. static int bits__fprintf(FILE *fp, const char *field, u64 value,
  1441. struct bit_names *bits, bool *first)
  1442. {
  1443. int i = 0, printed = comma_fprintf(fp, first, " %s: ", field);
  1444. bool first_bit = true;
  1445. do {
  1446. if (value & bits[i].bit) {
  1447. printed += fprintf(fp, "%s%s", first_bit ? "" : "|", bits[i].name);
  1448. first_bit = false;
  1449. }
  1450. } while (bits[++i].name != NULL);
  1451. return printed;
  1452. }
  1453. static int sample_type__fprintf(FILE *fp, bool *first, u64 value)
  1454. {
  1455. #define bit_name(n) { PERF_SAMPLE_##n, #n }
  1456. struct bit_names bits[] = {
  1457. bit_name(IP), bit_name(TID), bit_name(TIME), bit_name(ADDR),
  1458. bit_name(READ), bit_name(CALLCHAIN), bit_name(ID), bit_name(CPU),
  1459. bit_name(PERIOD), bit_name(STREAM_ID), bit_name(RAW),
  1460. bit_name(BRANCH_STACK), bit_name(REGS_USER), bit_name(STACK_USER),
  1461. bit_name(IDENTIFIER),
  1462. { .name = NULL, }
  1463. };
  1464. #undef bit_name
  1465. return bits__fprintf(fp, "sample_type", value, bits, first);
  1466. }
  1467. static int read_format__fprintf(FILE *fp, bool *first, u64 value)
  1468. {
  1469. #define bit_name(n) { PERF_FORMAT_##n, #n }
  1470. struct bit_names bits[] = {
  1471. bit_name(TOTAL_TIME_ENABLED), bit_name(TOTAL_TIME_RUNNING),
  1472. bit_name(ID), bit_name(GROUP),
  1473. { .name = NULL, }
  1474. };
  1475. #undef bit_name
  1476. return bits__fprintf(fp, "read_format", value, bits, first);
  1477. }
  1478. int perf_evsel__fprintf(struct perf_evsel *evsel,
  1479. struct perf_attr_details *details, FILE *fp)
  1480. {
  1481. bool first = true;
  1482. int printed = 0;
  1483. if (details->event_group) {
  1484. struct perf_evsel *pos;
  1485. if (!perf_evsel__is_group_leader(evsel))
  1486. return 0;
  1487. if (evsel->nr_members > 1)
  1488. printed += fprintf(fp, "%s{", evsel->group_name ?: "");
  1489. printed += fprintf(fp, "%s", perf_evsel__name(evsel));
  1490. for_each_group_member(pos, evsel)
  1491. printed += fprintf(fp, ",%s", perf_evsel__name(pos));
  1492. if (evsel->nr_members > 1)
  1493. printed += fprintf(fp, "}");
  1494. goto out;
  1495. }
  1496. printed += fprintf(fp, "%s", perf_evsel__name(evsel));
  1497. if (details->verbose || details->freq) {
  1498. printed += comma_fprintf(fp, &first, " sample_freq=%" PRIu64,
  1499. (u64)evsel->attr.sample_freq);
  1500. }
  1501. if (details->verbose) {
  1502. if_print(type);
  1503. if_print(config);
  1504. if_print(config1);
  1505. if_print(config2);
  1506. if_print(size);
  1507. printed += sample_type__fprintf(fp, &first, evsel->attr.sample_type);
  1508. if (evsel->attr.read_format)
  1509. printed += read_format__fprintf(fp, &first, evsel->attr.read_format);
  1510. if_print(disabled);
  1511. if_print(inherit);
  1512. if_print(pinned);
  1513. if_print(exclusive);
  1514. if_print(exclude_user);
  1515. if_print(exclude_kernel);
  1516. if_print(exclude_hv);
  1517. if_print(exclude_idle);
  1518. if_print(mmap);
  1519. if_print(comm);
  1520. if_print(freq);
  1521. if_print(inherit_stat);
  1522. if_print(enable_on_exec);
  1523. if_print(task);
  1524. if_print(watermark);
  1525. if_print(precise_ip);
  1526. if_print(mmap_data);
  1527. if_print(sample_id_all);
  1528. if_print(exclude_host);
  1529. if_print(exclude_guest);
  1530. if_print(__reserved_1);
  1531. if_print(wakeup_events);
  1532. if_print(bp_type);
  1533. if_print(branch_sample_type);
  1534. }
  1535. out:
  1536. fputc('\n', fp);
  1537. return ++printed;
  1538. }
  1539. bool perf_evsel__fallback(struct perf_evsel *evsel, int err,
  1540. char *msg, size_t msgsize)
  1541. {
  1542. if ((err == ENOENT || err == ENXIO || err == ENODEV) &&
  1543. evsel->attr.type == PERF_TYPE_HARDWARE &&
  1544. evsel->attr.config == PERF_COUNT_HW_CPU_CYCLES) {
  1545. /*
  1546. * If it's cycles then fall back to hrtimer based
  1547. * cpu-clock-tick sw counter, which is always available even if
  1548. * no PMU support.
  1549. *
  1550. * PPC returns ENXIO until 2.6.37 (behavior changed with commit
  1551. * b0a873e).
  1552. */
  1553. scnprintf(msg, msgsize, "%s",
  1554. "The cycles event is not supported, trying to fall back to cpu-clock-ticks");
  1555. evsel->attr.type = PERF_TYPE_SOFTWARE;
  1556. evsel->attr.config = PERF_COUNT_SW_CPU_CLOCK;
  1557. free(evsel->name);
  1558. evsel->name = NULL;
  1559. return true;
  1560. }
  1561. return false;
  1562. }
  1563. int perf_evsel__open_strerror(struct perf_evsel *evsel,
  1564. struct perf_target *target,
  1565. int err, char *msg, size_t size)
  1566. {
  1567. switch (err) {
  1568. case EPERM:
  1569. case EACCES:
  1570. return scnprintf(msg, size,
  1571. "You may not have permission to collect %sstats.\n"
  1572. "Consider tweaking /proc/sys/kernel/perf_event_paranoid:\n"
  1573. " -1 - Not paranoid at all\n"
  1574. " 0 - Disallow raw tracepoint access for unpriv\n"
  1575. " 1 - Disallow cpu events for unpriv\n"
  1576. " 2 - Disallow kernel profiling for unpriv",
  1577. target->system_wide ? "system-wide " : "");
  1578. case ENOENT:
  1579. return scnprintf(msg, size, "The %s event is not supported.",
  1580. perf_evsel__name(evsel));
  1581. case EMFILE:
  1582. return scnprintf(msg, size, "%s",
  1583. "Too many events are opened.\n"
  1584. "Try again after reducing the number of events.");
  1585. case ENODEV:
  1586. if (target->cpu_list)
  1587. return scnprintf(msg, size, "%s",
  1588. "No such device - did you specify an out-of-range profile CPU?\n");
  1589. break;
  1590. case EOPNOTSUPP:
  1591. if (evsel->attr.precise_ip)
  1592. return scnprintf(msg, size, "%s",
  1593. "\'precise\' request may not be supported. Try removing 'p' modifier.");
  1594. #if defined(__i386__) || defined(__x86_64__)
  1595. if (evsel->attr.type == PERF_TYPE_HARDWARE)
  1596. return scnprintf(msg, size, "%s",
  1597. "No hardware sampling interrupt available.\n"
  1598. "No APIC? If so then you can boot the kernel with the \"lapic\" boot parameter to force-enable it.");
  1599. #endif
  1600. break;
  1601. default:
  1602. break;
  1603. }
  1604. return scnprintf(msg, size,
  1605. "The sys_perf_event_open() syscall returned with %d (%s) for event (%s). \n"
  1606. "/bin/dmesg may provide additional information.\n"
  1607. "No CONFIG_PERF_EVENTS=y kernel support configured?\n",
  1608. err, strerror(err), perf_evsel__name(evsel));
  1609. }