session.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096
  1. #include <linux/kernel.h>
  2. #include <traceevent/event-parse.h>
  3. #include <byteswap.h>
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6. #include <sys/mman.h>
  7. #include "evlist.h"
  8. #include "evsel.h"
  9. #include "session.h"
  10. #include "tool.h"
  11. #include "sort.h"
  12. #include "util.h"
  13. #include "cpumap.h"
  14. #include "perf_regs.h"
  15. #include "asm/bug.h"
  16. #include "auxtrace.h"
  17. #include "thread-stack.h"
  18. static int perf_session__deliver_event(struct perf_session *session,
  19. union perf_event *event,
  20. struct perf_sample *sample,
  21. struct perf_tool *tool,
  22. u64 file_offset);
  23. static int perf_session__open(struct perf_session *session)
  24. {
  25. struct perf_data_file *file = session->file;
  26. if (perf_session__read_header(session) < 0) {
  27. pr_err("incompatible file format (rerun with -v to learn more)\n");
  28. return -1;
  29. }
  30. if (perf_data_file__is_pipe(file))
  31. return 0;
  32. if (!perf_evlist__valid_sample_type(session->evlist)) {
  33. pr_err("non matching sample_type\n");
  34. return -1;
  35. }
  36. if (!perf_evlist__valid_sample_id_all(session->evlist)) {
  37. pr_err("non matching sample_id_all\n");
  38. return -1;
  39. }
  40. if (!perf_evlist__valid_read_format(session->evlist)) {
  41. pr_err("non matching read_format\n");
  42. return -1;
  43. }
  44. return 0;
  45. }
  46. void perf_session__set_id_hdr_size(struct perf_session *session)
  47. {
  48. u16 id_hdr_size = perf_evlist__id_hdr_size(session->evlist);
  49. machines__set_id_hdr_size(&session->machines, id_hdr_size);
  50. }
  51. int perf_session__create_kernel_maps(struct perf_session *session)
  52. {
  53. int ret = machine__create_kernel_maps(&session->machines.host);
  54. if (ret >= 0)
  55. ret = machines__create_guest_kernel_maps(&session->machines);
  56. return ret;
  57. }
  58. static void perf_session__destroy_kernel_maps(struct perf_session *session)
  59. {
  60. machines__destroy_kernel_maps(&session->machines);
  61. }
  62. static bool perf_session__has_comm_exec(struct perf_session *session)
  63. {
  64. struct perf_evsel *evsel;
  65. evlist__for_each(session->evlist, evsel) {
  66. if (evsel->attr.comm_exec)
  67. return true;
  68. }
  69. return false;
  70. }
  71. static void perf_session__set_comm_exec(struct perf_session *session)
  72. {
  73. bool comm_exec = perf_session__has_comm_exec(session);
  74. machines__set_comm_exec(&session->machines, comm_exec);
  75. }
  76. static int ordered_events__deliver_event(struct ordered_events *oe,
  77. struct ordered_event *event)
  78. {
  79. struct perf_sample sample;
  80. struct perf_session *session = container_of(oe, struct perf_session,
  81. ordered_events);
  82. int ret = perf_evlist__parse_sample(session->evlist, event->event, &sample);
  83. if (ret) {
  84. pr_err("Can't parse sample, err = %d\n", ret);
  85. return ret;
  86. }
  87. return perf_session__deliver_event(session, event->event, &sample,
  88. session->tool, event->file_offset);
  89. }
  90. struct perf_session *perf_session__new(struct perf_data_file *file,
  91. bool repipe, struct perf_tool *tool)
  92. {
  93. struct perf_session *session = zalloc(sizeof(*session));
  94. if (!session)
  95. goto out;
  96. session->repipe = repipe;
  97. session->tool = tool;
  98. INIT_LIST_HEAD(&session->auxtrace_index);
  99. machines__init(&session->machines);
  100. ordered_events__init(&session->ordered_events, ordered_events__deliver_event);
  101. if (file) {
  102. if (perf_data_file__open(file))
  103. goto out_delete;
  104. session->file = file;
  105. if (perf_data_file__is_read(file)) {
  106. if (perf_session__open(session) < 0)
  107. goto out_close;
  108. /*
  109. * set session attributes that are present in perf.data
  110. * but not in pipe-mode.
  111. */
  112. if (!file->is_pipe) {
  113. perf_session__set_id_hdr_size(session);
  114. perf_session__set_comm_exec(session);
  115. }
  116. }
  117. } else {
  118. session->machines.host.env = &perf_env;
  119. }
  120. if (!file || perf_data_file__is_write(file)) {
  121. /*
  122. * In O_RDONLY mode this will be performed when reading the
  123. * kernel MMAP event, in perf_event__process_mmap().
  124. */
  125. if (perf_session__create_kernel_maps(session) < 0)
  126. pr_warning("Cannot read kernel map\n");
  127. }
  128. /*
  129. * In pipe-mode, evlist is empty until PERF_RECORD_HEADER_ATTR is
  130. * processed, so perf_evlist__sample_id_all is not meaningful here.
  131. */
  132. if ((!file || !file->is_pipe) && tool && tool->ordering_requires_timestamps &&
  133. tool->ordered_events && !perf_evlist__sample_id_all(session->evlist)) {
  134. dump_printf("WARNING: No sample_id_all support, falling back to unordered processing\n");
  135. tool->ordered_events = false;
  136. }
  137. return session;
  138. out_close:
  139. perf_data_file__close(file);
  140. out_delete:
  141. perf_session__delete(session);
  142. out:
  143. return NULL;
  144. }
  145. static void perf_session__delete_threads(struct perf_session *session)
  146. {
  147. machine__delete_threads(&session->machines.host);
  148. }
  149. void perf_session__delete(struct perf_session *session)
  150. {
  151. auxtrace__free(session);
  152. auxtrace_index__free(&session->auxtrace_index);
  153. perf_session__destroy_kernel_maps(session);
  154. perf_session__delete_threads(session);
  155. perf_env__exit(&session->header.env);
  156. machines__exit(&session->machines);
  157. if (session->file)
  158. perf_data_file__close(session->file);
  159. free(session);
  160. }
  161. static int process_event_synth_tracing_data_stub(struct perf_tool *tool
  162. __maybe_unused,
  163. union perf_event *event
  164. __maybe_unused,
  165. struct perf_session *session
  166. __maybe_unused)
  167. {
  168. dump_printf(": unhandled!\n");
  169. return 0;
  170. }
  171. static int process_event_synth_attr_stub(struct perf_tool *tool __maybe_unused,
  172. union perf_event *event __maybe_unused,
  173. struct perf_evlist **pevlist
  174. __maybe_unused)
  175. {
  176. dump_printf(": unhandled!\n");
  177. return 0;
  178. }
  179. static int process_event_sample_stub(struct perf_tool *tool __maybe_unused,
  180. union perf_event *event __maybe_unused,
  181. struct perf_sample *sample __maybe_unused,
  182. struct perf_evsel *evsel __maybe_unused,
  183. struct machine *machine __maybe_unused)
  184. {
  185. dump_printf(": unhandled!\n");
  186. return 0;
  187. }
  188. static int process_event_stub(struct perf_tool *tool __maybe_unused,
  189. union perf_event *event __maybe_unused,
  190. struct perf_sample *sample __maybe_unused,
  191. struct machine *machine __maybe_unused)
  192. {
  193. dump_printf(": unhandled!\n");
  194. return 0;
  195. }
  196. static int process_build_id_stub(struct perf_tool *tool __maybe_unused,
  197. union perf_event *event __maybe_unused,
  198. struct perf_session *session __maybe_unused)
  199. {
  200. dump_printf(": unhandled!\n");
  201. return 0;
  202. }
  203. static int process_finished_round_stub(struct perf_tool *tool __maybe_unused,
  204. union perf_event *event __maybe_unused,
  205. struct ordered_events *oe __maybe_unused)
  206. {
  207. dump_printf(": unhandled!\n");
  208. return 0;
  209. }
  210. static int process_finished_round(struct perf_tool *tool,
  211. union perf_event *event,
  212. struct ordered_events *oe);
  213. static int process_id_index_stub(struct perf_tool *tool __maybe_unused,
  214. union perf_event *event __maybe_unused,
  215. struct perf_session *perf_session
  216. __maybe_unused)
  217. {
  218. dump_printf(": unhandled!\n");
  219. return 0;
  220. }
  221. static int process_event_auxtrace_info_stub(struct perf_tool *tool __maybe_unused,
  222. union perf_event *event __maybe_unused,
  223. struct perf_session *session __maybe_unused)
  224. {
  225. dump_printf(": unhandled!\n");
  226. return 0;
  227. }
  228. static int skipn(int fd, off_t n)
  229. {
  230. char buf[4096];
  231. ssize_t ret;
  232. while (n > 0) {
  233. ret = read(fd, buf, min(n, (off_t)sizeof(buf)));
  234. if (ret <= 0)
  235. return ret;
  236. n -= ret;
  237. }
  238. return 0;
  239. }
  240. static s64 process_event_auxtrace_stub(struct perf_tool *tool __maybe_unused,
  241. union perf_event *event,
  242. struct perf_session *session
  243. __maybe_unused)
  244. {
  245. dump_printf(": unhandled!\n");
  246. if (perf_data_file__is_pipe(session->file))
  247. skipn(perf_data_file__fd(session->file), event->auxtrace.size);
  248. return event->auxtrace.size;
  249. }
  250. static
  251. int process_event_auxtrace_error_stub(struct perf_tool *tool __maybe_unused,
  252. union perf_event *event __maybe_unused,
  253. struct perf_session *session __maybe_unused)
  254. {
  255. dump_printf(": unhandled!\n");
  256. return 0;
  257. }
  258. void perf_tool__fill_defaults(struct perf_tool *tool)
  259. {
  260. if (tool->sample == NULL)
  261. tool->sample = process_event_sample_stub;
  262. if (tool->mmap == NULL)
  263. tool->mmap = process_event_stub;
  264. if (tool->mmap2 == NULL)
  265. tool->mmap2 = process_event_stub;
  266. if (tool->comm == NULL)
  267. tool->comm = process_event_stub;
  268. if (tool->fork == NULL)
  269. tool->fork = process_event_stub;
  270. if (tool->exit == NULL)
  271. tool->exit = process_event_stub;
  272. if (tool->lost == NULL)
  273. tool->lost = perf_event__process_lost;
  274. if (tool->lost_samples == NULL)
  275. tool->lost_samples = perf_event__process_lost_samples;
  276. if (tool->aux == NULL)
  277. tool->aux = perf_event__process_aux;
  278. if (tool->itrace_start == NULL)
  279. tool->itrace_start = perf_event__process_itrace_start;
  280. if (tool->context_switch == NULL)
  281. tool->context_switch = perf_event__process_switch;
  282. if (tool->read == NULL)
  283. tool->read = process_event_sample_stub;
  284. if (tool->throttle == NULL)
  285. tool->throttle = process_event_stub;
  286. if (tool->unthrottle == NULL)
  287. tool->unthrottle = process_event_stub;
  288. if (tool->attr == NULL)
  289. tool->attr = process_event_synth_attr_stub;
  290. if (tool->tracing_data == NULL)
  291. tool->tracing_data = process_event_synth_tracing_data_stub;
  292. if (tool->build_id == NULL)
  293. tool->build_id = process_build_id_stub;
  294. if (tool->finished_round == NULL) {
  295. if (tool->ordered_events)
  296. tool->finished_round = process_finished_round;
  297. else
  298. tool->finished_round = process_finished_round_stub;
  299. }
  300. if (tool->id_index == NULL)
  301. tool->id_index = process_id_index_stub;
  302. if (tool->auxtrace_info == NULL)
  303. tool->auxtrace_info = process_event_auxtrace_info_stub;
  304. if (tool->auxtrace == NULL)
  305. tool->auxtrace = process_event_auxtrace_stub;
  306. if (tool->auxtrace_error == NULL)
  307. tool->auxtrace_error = process_event_auxtrace_error_stub;
  308. }
  309. static void swap_sample_id_all(union perf_event *event, void *data)
  310. {
  311. void *end = (void *) event + event->header.size;
  312. int size = end - data;
  313. BUG_ON(size % sizeof(u64));
  314. mem_bswap_64(data, size);
  315. }
  316. static void perf_event__all64_swap(union perf_event *event,
  317. bool sample_id_all __maybe_unused)
  318. {
  319. struct perf_event_header *hdr = &event->header;
  320. mem_bswap_64(hdr + 1, event->header.size - sizeof(*hdr));
  321. }
  322. static void perf_event__comm_swap(union perf_event *event, bool sample_id_all)
  323. {
  324. event->comm.pid = bswap_32(event->comm.pid);
  325. event->comm.tid = bswap_32(event->comm.tid);
  326. if (sample_id_all) {
  327. void *data = &event->comm.comm;
  328. data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
  329. swap_sample_id_all(event, data);
  330. }
  331. }
  332. static void perf_event__mmap_swap(union perf_event *event,
  333. bool sample_id_all)
  334. {
  335. event->mmap.pid = bswap_32(event->mmap.pid);
  336. event->mmap.tid = bswap_32(event->mmap.tid);
  337. event->mmap.start = bswap_64(event->mmap.start);
  338. event->mmap.len = bswap_64(event->mmap.len);
  339. event->mmap.pgoff = bswap_64(event->mmap.pgoff);
  340. if (sample_id_all) {
  341. void *data = &event->mmap.filename;
  342. data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
  343. swap_sample_id_all(event, data);
  344. }
  345. }
  346. static void perf_event__mmap2_swap(union perf_event *event,
  347. bool sample_id_all)
  348. {
  349. event->mmap2.pid = bswap_32(event->mmap2.pid);
  350. event->mmap2.tid = bswap_32(event->mmap2.tid);
  351. event->mmap2.start = bswap_64(event->mmap2.start);
  352. event->mmap2.len = bswap_64(event->mmap2.len);
  353. event->mmap2.pgoff = bswap_64(event->mmap2.pgoff);
  354. event->mmap2.maj = bswap_32(event->mmap2.maj);
  355. event->mmap2.min = bswap_32(event->mmap2.min);
  356. event->mmap2.ino = bswap_64(event->mmap2.ino);
  357. if (sample_id_all) {
  358. void *data = &event->mmap2.filename;
  359. data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
  360. swap_sample_id_all(event, data);
  361. }
  362. }
  363. static void perf_event__task_swap(union perf_event *event, bool sample_id_all)
  364. {
  365. event->fork.pid = bswap_32(event->fork.pid);
  366. event->fork.tid = bswap_32(event->fork.tid);
  367. event->fork.ppid = bswap_32(event->fork.ppid);
  368. event->fork.ptid = bswap_32(event->fork.ptid);
  369. event->fork.time = bswap_64(event->fork.time);
  370. if (sample_id_all)
  371. swap_sample_id_all(event, &event->fork + 1);
  372. }
  373. static void perf_event__read_swap(union perf_event *event, bool sample_id_all)
  374. {
  375. event->read.pid = bswap_32(event->read.pid);
  376. event->read.tid = bswap_32(event->read.tid);
  377. event->read.value = bswap_64(event->read.value);
  378. event->read.time_enabled = bswap_64(event->read.time_enabled);
  379. event->read.time_running = bswap_64(event->read.time_running);
  380. event->read.id = bswap_64(event->read.id);
  381. if (sample_id_all)
  382. swap_sample_id_all(event, &event->read + 1);
  383. }
  384. static void perf_event__aux_swap(union perf_event *event, bool sample_id_all)
  385. {
  386. event->aux.aux_offset = bswap_64(event->aux.aux_offset);
  387. event->aux.aux_size = bswap_64(event->aux.aux_size);
  388. event->aux.flags = bswap_64(event->aux.flags);
  389. if (sample_id_all)
  390. swap_sample_id_all(event, &event->aux + 1);
  391. }
  392. static void perf_event__itrace_start_swap(union perf_event *event,
  393. bool sample_id_all)
  394. {
  395. event->itrace_start.pid = bswap_32(event->itrace_start.pid);
  396. event->itrace_start.tid = bswap_32(event->itrace_start.tid);
  397. if (sample_id_all)
  398. swap_sample_id_all(event, &event->itrace_start + 1);
  399. }
  400. static void perf_event__switch_swap(union perf_event *event, bool sample_id_all)
  401. {
  402. if (event->header.type == PERF_RECORD_SWITCH_CPU_WIDE) {
  403. event->context_switch.next_prev_pid =
  404. bswap_32(event->context_switch.next_prev_pid);
  405. event->context_switch.next_prev_tid =
  406. bswap_32(event->context_switch.next_prev_tid);
  407. }
  408. if (sample_id_all)
  409. swap_sample_id_all(event, &event->context_switch + 1);
  410. }
  411. static void perf_event__throttle_swap(union perf_event *event,
  412. bool sample_id_all)
  413. {
  414. event->throttle.time = bswap_64(event->throttle.time);
  415. event->throttle.id = bswap_64(event->throttle.id);
  416. event->throttle.stream_id = bswap_64(event->throttle.stream_id);
  417. if (sample_id_all)
  418. swap_sample_id_all(event, &event->throttle + 1);
  419. }
  420. static u8 revbyte(u8 b)
  421. {
  422. int rev = (b >> 4) | ((b & 0xf) << 4);
  423. rev = ((rev & 0xcc) >> 2) | ((rev & 0x33) << 2);
  424. rev = ((rev & 0xaa) >> 1) | ((rev & 0x55) << 1);
  425. return (u8) rev;
  426. }
  427. /*
  428. * XXX this is hack in attempt to carry flags bitfield
  429. * throught endian village. ABI says:
  430. *
  431. * Bit-fields are allocated from right to left (least to most significant)
  432. * on little-endian implementations and from left to right (most to least
  433. * significant) on big-endian implementations.
  434. *
  435. * The above seems to be byte specific, so we need to reverse each
  436. * byte of the bitfield. 'Internet' also says this might be implementation
  437. * specific and we probably need proper fix and carry perf_event_attr
  438. * bitfield flags in separate data file FEAT_ section. Thought this seems
  439. * to work for now.
  440. */
  441. static void swap_bitfield(u8 *p, unsigned len)
  442. {
  443. unsigned i;
  444. for (i = 0; i < len; i++) {
  445. *p = revbyte(*p);
  446. p++;
  447. }
  448. }
  449. /* exported for swapping attributes in file header */
  450. void perf_event__attr_swap(struct perf_event_attr *attr)
  451. {
  452. attr->type = bswap_32(attr->type);
  453. attr->size = bswap_32(attr->size);
  454. #define bswap_safe(f, n) \
  455. (attr->size > (offsetof(struct perf_event_attr, f) + \
  456. sizeof(attr->f) * (n)))
  457. #define bswap_field(f, sz) \
  458. do { \
  459. if (bswap_safe(f, 0)) \
  460. attr->f = bswap_##sz(attr->f); \
  461. } while(0)
  462. #define bswap_field_32(f) bswap_field(f, 32)
  463. #define bswap_field_64(f) bswap_field(f, 64)
  464. bswap_field_64(config);
  465. bswap_field_64(sample_period);
  466. bswap_field_64(sample_type);
  467. bswap_field_64(read_format);
  468. bswap_field_32(wakeup_events);
  469. bswap_field_32(bp_type);
  470. bswap_field_64(bp_addr);
  471. bswap_field_64(bp_len);
  472. bswap_field_64(branch_sample_type);
  473. bswap_field_64(sample_regs_user);
  474. bswap_field_32(sample_stack_user);
  475. bswap_field_32(aux_watermark);
  476. /*
  477. * After read_format are bitfields. Check read_format because
  478. * we are unable to use offsetof on bitfield.
  479. */
  480. if (bswap_safe(read_format, 1))
  481. swap_bitfield((u8 *) (&attr->read_format + 1),
  482. sizeof(u64));
  483. #undef bswap_field_64
  484. #undef bswap_field_32
  485. #undef bswap_field
  486. #undef bswap_safe
  487. }
  488. static void perf_event__hdr_attr_swap(union perf_event *event,
  489. bool sample_id_all __maybe_unused)
  490. {
  491. size_t size;
  492. perf_event__attr_swap(&event->attr.attr);
  493. size = event->header.size;
  494. size -= (void *)&event->attr.id - (void *)event;
  495. mem_bswap_64(event->attr.id, size);
  496. }
  497. static void perf_event__event_type_swap(union perf_event *event,
  498. bool sample_id_all __maybe_unused)
  499. {
  500. event->event_type.event_type.event_id =
  501. bswap_64(event->event_type.event_type.event_id);
  502. }
  503. static void perf_event__tracing_data_swap(union perf_event *event,
  504. bool sample_id_all __maybe_unused)
  505. {
  506. event->tracing_data.size = bswap_32(event->tracing_data.size);
  507. }
  508. static void perf_event__auxtrace_info_swap(union perf_event *event,
  509. bool sample_id_all __maybe_unused)
  510. {
  511. size_t size;
  512. event->auxtrace_info.type = bswap_32(event->auxtrace_info.type);
  513. size = event->header.size;
  514. size -= (void *)&event->auxtrace_info.priv - (void *)event;
  515. mem_bswap_64(event->auxtrace_info.priv, size);
  516. }
  517. static void perf_event__auxtrace_swap(union perf_event *event,
  518. bool sample_id_all __maybe_unused)
  519. {
  520. event->auxtrace.size = bswap_64(event->auxtrace.size);
  521. event->auxtrace.offset = bswap_64(event->auxtrace.offset);
  522. event->auxtrace.reference = bswap_64(event->auxtrace.reference);
  523. event->auxtrace.idx = bswap_32(event->auxtrace.idx);
  524. event->auxtrace.tid = bswap_32(event->auxtrace.tid);
  525. event->auxtrace.cpu = bswap_32(event->auxtrace.cpu);
  526. }
  527. static void perf_event__auxtrace_error_swap(union perf_event *event,
  528. bool sample_id_all __maybe_unused)
  529. {
  530. event->auxtrace_error.type = bswap_32(event->auxtrace_error.type);
  531. event->auxtrace_error.code = bswap_32(event->auxtrace_error.code);
  532. event->auxtrace_error.cpu = bswap_32(event->auxtrace_error.cpu);
  533. event->auxtrace_error.pid = bswap_32(event->auxtrace_error.pid);
  534. event->auxtrace_error.tid = bswap_32(event->auxtrace_error.tid);
  535. event->auxtrace_error.ip = bswap_64(event->auxtrace_error.ip);
  536. }
  537. typedef void (*perf_event__swap_op)(union perf_event *event,
  538. bool sample_id_all);
  539. static perf_event__swap_op perf_event__swap_ops[] = {
  540. [PERF_RECORD_MMAP] = perf_event__mmap_swap,
  541. [PERF_RECORD_MMAP2] = perf_event__mmap2_swap,
  542. [PERF_RECORD_COMM] = perf_event__comm_swap,
  543. [PERF_RECORD_FORK] = perf_event__task_swap,
  544. [PERF_RECORD_EXIT] = perf_event__task_swap,
  545. [PERF_RECORD_LOST] = perf_event__all64_swap,
  546. [PERF_RECORD_READ] = perf_event__read_swap,
  547. [PERF_RECORD_THROTTLE] = perf_event__throttle_swap,
  548. [PERF_RECORD_UNTHROTTLE] = perf_event__throttle_swap,
  549. [PERF_RECORD_SAMPLE] = perf_event__all64_swap,
  550. [PERF_RECORD_AUX] = perf_event__aux_swap,
  551. [PERF_RECORD_ITRACE_START] = perf_event__itrace_start_swap,
  552. [PERF_RECORD_LOST_SAMPLES] = perf_event__all64_swap,
  553. [PERF_RECORD_SWITCH] = perf_event__switch_swap,
  554. [PERF_RECORD_SWITCH_CPU_WIDE] = perf_event__switch_swap,
  555. [PERF_RECORD_HEADER_ATTR] = perf_event__hdr_attr_swap,
  556. [PERF_RECORD_HEADER_EVENT_TYPE] = perf_event__event_type_swap,
  557. [PERF_RECORD_HEADER_TRACING_DATA] = perf_event__tracing_data_swap,
  558. [PERF_RECORD_HEADER_BUILD_ID] = NULL,
  559. [PERF_RECORD_ID_INDEX] = perf_event__all64_swap,
  560. [PERF_RECORD_AUXTRACE_INFO] = perf_event__auxtrace_info_swap,
  561. [PERF_RECORD_AUXTRACE] = perf_event__auxtrace_swap,
  562. [PERF_RECORD_AUXTRACE_ERROR] = perf_event__auxtrace_error_swap,
  563. [PERF_RECORD_HEADER_MAX] = NULL,
  564. };
  565. /*
  566. * When perf record finishes a pass on every buffers, it records this pseudo
  567. * event.
  568. * We record the max timestamp t found in the pass n.
  569. * Assuming these timestamps are monotonic across cpus, we know that if
  570. * a buffer still has events with timestamps below t, they will be all
  571. * available and then read in the pass n + 1.
  572. * Hence when we start to read the pass n + 2, we can safely flush every
  573. * events with timestamps below t.
  574. *
  575. * ============ PASS n =================
  576. * CPU 0 | CPU 1
  577. * |
  578. * cnt1 timestamps | cnt2 timestamps
  579. * 1 | 2
  580. * 2 | 3
  581. * - | 4 <--- max recorded
  582. *
  583. * ============ PASS n + 1 ==============
  584. * CPU 0 | CPU 1
  585. * |
  586. * cnt1 timestamps | cnt2 timestamps
  587. * 3 | 5
  588. * 4 | 6
  589. * 5 | 7 <---- max recorded
  590. *
  591. * Flush every events below timestamp 4
  592. *
  593. * ============ PASS n + 2 ==============
  594. * CPU 0 | CPU 1
  595. * |
  596. * cnt1 timestamps | cnt2 timestamps
  597. * 6 | 8
  598. * 7 | 9
  599. * - | 10
  600. *
  601. * Flush every events below timestamp 7
  602. * etc...
  603. */
  604. static int process_finished_round(struct perf_tool *tool __maybe_unused,
  605. union perf_event *event __maybe_unused,
  606. struct ordered_events *oe)
  607. {
  608. if (dump_trace)
  609. fprintf(stdout, "\n");
  610. return ordered_events__flush(oe, OE_FLUSH__ROUND);
  611. }
  612. int perf_session__queue_event(struct perf_session *s, union perf_event *event,
  613. struct perf_sample *sample, u64 file_offset)
  614. {
  615. return ordered_events__queue(&s->ordered_events, event, sample, file_offset);
  616. }
  617. static void callchain__lbr_callstack_printf(struct perf_sample *sample)
  618. {
  619. struct ip_callchain *callchain = sample->callchain;
  620. struct branch_stack *lbr_stack = sample->branch_stack;
  621. u64 kernel_callchain_nr = callchain->nr;
  622. unsigned int i;
  623. for (i = 0; i < kernel_callchain_nr; i++) {
  624. if (callchain->ips[i] == PERF_CONTEXT_USER)
  625. break;
  626. }
  627. if ((i != kernel_callchain_nr) && lbr_stack->nr) {
  628. u64 total_nr;
  629. /*
  630. * LBR callstack can only get user call chain,
  631. * i is kernel call chain number,
  632. * 1 is PERF_CONTEXT_USER.
  633. *
  634. * The user call chain is stored in LBR registers.
  635. * LBR are pair registers. The caller is stored
  636. * in "from" register, while the callee is stored
  637. * in "to" register.
  638. * For example, there is a call stack
  639. * "A"->"B"->"C"->"D".
  640. * The LBR registers will recorde like
  641. * "C"->"D", "B"->"C", "A"->"B".
  642. * So only the first "to" register and all "from"
  643. * registers are needed to construct the whole stack.
  644. */
  645. total_nr = i + 1 + lbr_stack->nr + 1;
  646. kernel_callchain_nr = i + 1;
  647. printf("... LBR call chain: nr:%" PRIu64 "\n", total_nr);
  648. for (i = 0; i < kernel_callchain_nr; i++)
  649. printf("..... %2d: %016" PRIx64 "\n",
  650. i, callchain->ips[i]);
  651. printf("..... %2d: %016" PRIx64 "\n",
  652. (int)(kernel_callchain_nr), lbr_stack->entries[0].to);
  653. for (i = 0; i < lbr_stack->nr; i++)
  654. printf("..... %2d: %016" PRIx64 "\n",
  655. (int)(i + kernel_callchain_nr + 1), lbr_stack->entries[i].from);
  656. }
  657. }
  658. static void callchain__printf(struct perf_evsel *evsel,
  659. struct perf_sample *sample)
  660. {
  661. unsigned int i;
  662. struct ip_callchain *callchain = sample->callchain;
  663. if (has_branch_callstack(evsel))
  664. callchain__lbr_callstack_printf(sample);
  665. printf("... FP chain: nr:%" PRIu64 "\n", callchain->nr);
  666. for (i = 0; i < callchain->nr; i++)
  667. printf("..... %2d: %016" PRIx64 "\n",
  668. i, callchain->ips[i]);
  669. }
  670. static void branch_stack__printf(struct perf_sample *sample)
  671. {
  672. uint64_t i;
  673. printf("... branch stack: nr:%" PRIu64 "\n", sample->branch_stack->nr);
  674. for (i = 0; i < sample->branch_stack->nr; i++) {
  675. struct branch_entry *e = &sample->branch_stack->entries[i];
  676. printf("..... %2"PRIu64": %016" PRIx64 " -> %016" PRIx64 " %hu cycles %s%s%s%s %x\n",
  677. i, e->from, e->to,
  678. e->flags.cycles,
  679. e->flags.mispred ? "M" : " ",
  680. e->flags.predicted ? "P" : " ",
  681. e->flags.abort ? "A" : " ",
  682. e->flags.in_tx ? "T" : " ",
  683. (unsigned)e->flags.reserved);
  684. }
  685. }
  686. static void regs_dump__printf(u64 mask, u64 *regs)
  687. {
  688. unsigned rid, i = 0;
  689. for_each_set_bit(rid, (unsigned long *) &mask, sizeof(mask) * 8) {
  690. u64 val = regs[i++];
  691. printf(".... %-5s 0x%" PRIx64 "\n",
  692. perf_reg_name(rid), val);
  693. }
  694. }
  695. static const char *regs_abi[] = {
  696. [PERF_SAMPLE_REGS_ABI_NONE] = "none",
  697. [PERF_SAMPLE_REGS_ABI_32] = "32-bit",
  698. [PERF_SAMPLE_REGS_ABI_64] = "64-bit",
  699. };
  700. static inline const char *regs_dump_abi(struct regs_dump *d)
  701. {
  702. if (d->abi > PERF_SAMPLE_REGS_ABI_64)
  703. return "unknown";
  704. return regs_abi[d->abi];
  705. }
  706. static void regs__printf(const char *type, struct regs_dump *regs)
  707. {
  708. u64 mask = regs->mask;
  709. printf("... %s regs: mask 0x%" PRIx64 " ABI %s\n",
  710. type,
  711. mask,
  712. regs_dump_abi(regs));
  713. regs_dump__printf(mask, regs->regs);
  714. }
  715. static void regs_user__printf(struct perf_sample *sample)
  716. {
  717. struct regs_dump *user_regs = &sample->user_regs;
  718. if (user_regs->regs)
  719. regs__printf("user", user_regs);
  720. }
  721. static void regs_intr__printf(struct perf_sample *sample)
  722. {
  723. struct regs_dump *intr_regs = &sample->intr_regs;
  724. if (intr_regs->regs)
  725. regs__printf("intr", intr_regs);
  726. }
  727. static void stack_user__printf(struct stack_dump *dump)
  728. {
  729. printf("... ustack: size %" PRIu64 ", offset 0x%x\n",
  730. dump->size, dump->offset);
  731. }
  732. static void perf_evlist__print_tstamp(struct perf_evlist *evlist,
  733. union perf_event *event,
  734. struct perf_sample *sample)
  735. {
  736. u64 sample_type = __perf_evlist__combined_sample_type(evlist);
  737. if (event->header.type != PERF_RECORD_SAMPLE &&
  738. !perf_evlist__sample_id_all(evlist)) {
  739. fputs("-1 -1 ", stdout);
  740. return;
  741. }
  742. if ((sample_type & PERF_SAMPLE_CPU))
  743. printf("%u ", sample->cpu);
  744. if (sample_type & PERF_SAMPLE_TIME)
  745. printf("%" PRIu64 " ", sample->time);
  746. }
  747. static void sample_read__printf(struct perf_sample *sample, u64 read_format)
  748. {
  749. printf("... sample_read:\n");
  750. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  751. printf("...... time enabled %016" PRIx64 "\n",
  752. sample->read.time_enabled);
  753. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  754. printf("...... time running %016" PRIx64 "\n",
  755. sample->read.time_running);
  756. if (read_format & PERF_FORMAT_GROUP) {
  757. u64 i;
  758. printf(".... group nr %" PRIu64 "\n", sample->read.group.nr);
  759. for (i = 0; i < sample->read.group.nr; i++) {
  760. struct sample_read_value *value;
  761. value = &sample->read.group.values[i];
  762. printf("..... id %016" PRIx64
  763. ", value %016" PRIx64 "\n",
  764. value->id, value->value);
  765. }
  766. } else
  767. printf("..... id %016" PRIx64 ", value %016" PRIx64 "\n",
  768. sample->read.one.id, sample->read.one.value);
  769. }
  770. static void dump_event(struct perf_evlist *evlist, union perf_event *event,
  771. u64 file_offset, struct perf_sample *sample)
  772. {
  773. if (!dump_trace)
  774. return;
  775. printf("\n%#" PRIx64 " [%#x]: event: %d\n",
  776. file_offset, event->header.size, event->header.type);
  777. trace_event(event);
  778. if (sample)
  779. perf_evlist__print_tstamp(evlist, event, sample);
  780. printf("%#" PRIx64 " [%#x]: PERF_RECORD_%s", file_offset,
  781. event->header.size, perf_event__name(event->header.type));
  782. }
  783. static void dump_sample(struct perf_evsel *evsel, union perf_event *event,
  784. struct perf_sample *sample)
  785. {
  786. u64 sample_type;
  787. if (!dump_trace)
  788. return;
  789. printf("(IP, 0x%x): %d/%d: %#" PRIx64 " period: %" PRIu64 " addr: %#" PRIx64 "\n",
  790. event->header.misc, sample->pid, sample->tid, sample->ip,
  791. sample->period, sample->addr);
  792. sample_type = evsel->attr.sample_type;
  793. if (sample_type & PERF_SAMPLE_CALLCHAIN)
  794. callchain__printf(evsel, sample);
  795. if ((sample_type & PERF_SAMPLE_BRANCH_STACK) && !has_branch_callstack(evsel))
  796. branch_stack__printf(sample);
  797. if (sample_type & PERF_SAMPLE_REGS_USER)
  798. regs_user__printf(sample);
  799. if (sample_type & PERF_SAMPLE_REGS_INTR)
  800. regs_intr__printf(sample);
  801. if (sample_type & PERF_SAMPLE_STACK_USER)
  802. stack_user__printf(&sample->user_stack);
  803. if (sample_type & PERF_SAMPLE_WEIGHT)
  804. printf("... weight: %" PRIu64 "\n", sample->weight);
  805. if (sample_type & PERF_SAMPLE_DATA_SRC)
  806. printf(" . data_src: 0x%"PRIx64"\n", sample->data_src);
  807. if (sample_type & PERF_SAMPLE_TRANSACTION)
  808. printf("... transaction: %" PRIx64 "\n", sample->transaction);
  809. if (sample_type & PERF_SAMPLE_READ)
  810. sample_read__printf(sample, evsel->attr.read_format);
  811. }
  812. static struct machine *machines__find_for_cpumode(struct machines *machines,
  813. union perf_event *event,
  814. struct perf_sample *sample)
  815. {
  816. const u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  817. struct machine *machine;
  818. if (perf_guest &&
  819. ((cpumode == PERF_RECORD_MISC_GUEST_KERNEL) ||
  820. (cpumode == PERF_RECORD_MISC_GUEST_USER))) {
  821. u32 pid;
  822. if (event->header.type == PERF_RECORD_MMAP
  823. || event->header.type == PERF_RECORD_MMAP2)
  824. pid = event->mmap.pid;
  825. else
  826. pid = sample->pid;
  827. machine = machines__find(machines, pid);
  828. if (!machine)
  829. machine = machines__findnew(machines, DEFAULT_GUEST_KERNEL_ID);
  830. return machine;
  831. }
  832. return &machines->host;
  833. }
  834. static int deliver_sample_value(struct perf_evlist *evlist,
  835. struct perf_tool *tool,
  836. union perf_event *event,
  837. struct perf_sample *sample,
  838. struct sample_read_value *v,
  839. struct machine *machine)
  840. {
  841. struct perf_sample_id *sid = perf_evlist__id2sid(evlist, v->id);
  842. if (sid) {
  843. sample->id = v->id;
  844. sample->period = v->value - sid->period;
  845. sid->period = v->value;
  846. }
  847. if (!sid || sid->evsel == NULL) {
  848. ++evlist->stats.nr_unknown_id;
  849. return 0;
  850. }
  851. return tool->sample(tool, event, sample, sid->evsel, machine);
  852. }
  853. static int deliver_sample_group(struct perf_evlist *evlist,
  854. struct perf_tool *tool,
  855. union perf_event *event,
  856. struct perf_sample *sample,
  857. struct machine *machine)
  858. {
  859. int ret = -EINVAL;
  860. u64 i;
  861. for (i = 0; i < sample->read.group.nr; i++) {
  862. ret = deliver_sample_value(evlist, tool, event, sample,
  863. &sample->read.group.values[i],
  864. machine);
  865. if (ret)
  866. break;
  867. }
  868. return ret;
  869. }
  870. static int
  871. perf_evlist__deliver_sample(struct perf_evlist *evlist,
  872. struct perf_tool *tool,
  873. union perf_event *event,
  874. struct perf_sample *sample,
  875. struct perf_evsel *evsel,
  876. struct machine *machine)
  877. {
  878. /* We know evsel != NULL. */
  879. u64 sample_type = evsel->attr.sample_type;
  880. u64 read_format = evsel->attr.read_format;
  881. /* Standard sample delievery. */
  882. if (!(sample_type & PERF_SAMPLE_READ))
  883. return tool->sample(tool, event, sample, evsel, machine);
  884. /* For PERF_SAMPLE_READ we have either single or group mode. */
  885. if (read_format & PERF_FORMAT_GROUP)
  886. return deliver_sample_group(evlist, tool, event, sample,
  887. machine);
  888. else
  889. return deliver_sample_value(evlist, tool, event, sample,
  890. &sample->read.one, machine);
  891. }
  892. static int machines__deliver_event(struct machines *machines,
  893. struct perf_evlist *evlist,
  894. union perf_event *event,
  895. struct perf_sample *sample,
  896. struct perf_tool *tool, u64 file_offset)
  897. {
  898. struct perf_evsel *evsel;
  899. struct machine *machine;
  900. dump_event(evlist, event, file_offset, sample);
  901. evsel = perf_evlist__id2evsel(evlist, sample->id);
  902. machine = machines__find_for_cpumode(machines, event, sample);
  903. switch (event->header.type) {
  904. case PERF_RECORD_SAMPLE:
  905. if (evsel == NULL) {
  906. ++evlist->stats.nr_unknown_id;
  907. return 0;
  908. }
  909. dump_sample(evsel, event, sample);
  910. if (machine == NULL) {
  911. ++evlist->stats.nr_unprocessable_samples;
  912. return 0;
  913. }
  914. return perf_evlist__deliver_sample(evlist, tool, event, sample, evsel, machine);
  915. case PERF_RECORD_MMAP:
  916. return tool->mmap(tool, event, sample, machine);
  917. case PERF_RECORD_MMAP2:
  918. if (event->header.misc & PERF_RECORD_MISC_PROC_MAP_PARSE_TIMEOUT)
  919. ++evlist->stats.nr_proc_map_timeout;
  920. return tool->mmap2(tool, event, sample, machine);
  921. case PERF_RECORD_COMM:
  922. return tool->comm(tool, event, sample, machine);
  923. case PERF_RECORD_FORK:
  924. return tool->fork(tool, event, sample, machine);
  925. case PERF_RECORD_EXIT:
  926. return tool->exit(tool, event, sample, machine);
  927. case PERF_RECORD_LOST:
  928. if (tool->lost == perf_event__process_lost)
  929. evlist->stats.total_lost += event->lost.lost;
  930. return tool->lost(tool, event, sample, machine);
  931. case PERF_RECORD_LOST_SAMPLES:
  932. if (tool->lost_samples == perf_event__process_lost_samples)
  933. evlist->stats.total_lost_samples += event->lost_samples.lost;
  934. return tool->lost_samples(tool, event, sample, machine);
  935. case PERF_RECORD_READ:
  936. return tool->read(tool, event, sample, evsel, machine);
  937. case PERF_RECORD_THROTTLE:
  938. return tool->throttle(tool, event, sample, machine);
  939. case PERF_RECORD_UNTHROTTLE:
  940. return tool->unthrottle(tool, event, sample, machine);
  941. case PERF_RECORD_AUX:
  942. if (tool->aux == perf_event__process_aux &&
  943. (event->aux.flags & PERF_AUX_FLAG_TRUNCATED))
  944. evlist->stats.total_aux_lost += 1;
  945. return tool->aux(tool, event, sample, machine);
  946. case PERF_RECORD_ITRACE_START:
  947. return tool->itrace_start(tool, event, sample, machine);
  948. case PERF_RECORD_SWITCH:
  949. case PERF_RECORD_SWITCH_CPU_WIDE:
  950. return tool->context_switch(tool, event, sample, machine);
  951. default:
  952. ++evlist->stats.nr_unknown_events;
  953. return -1;
  954. }
  955. }
  956. static int perf_session__deliver_event(struct perf_session *session,
  957. union perf_event *event,
  958. struct perf_sample *sample,
  959. struct perf_tool *tool,
  960. u64 file_offset)
  961. {
  962. int ret;
  963. ret = auxtrace__process_event(session, event, sample, tool);
  964. if (ret < 0)
  965. return ret;
  966. if (ret > 0)
  967. return 0;
  968. return machines__deliver_event(&session->machines, session->evlist,
  969. event, sample, tool, file_offset);
  970. }
  971. static s64 perf_session__process_user_event(struct perf_session *session,
  972. union perf_event *event,
  973. u64 file_offset)
  974. {
  975. struct ordered_events *oe = &session->ordered_events;
  976. struct perf_tool *tool = session->tool;
  977. int fd = perf_data_file__fd(session->file);
  978. int err;
  979. dump_event(session->evlist, event, file_offset, NULL);
  980. /* These events are processed right away */
  981. switch (event->header.type) {
  982. case PERF_RECORD_HEADER_ATTR:
  983. err = tool->attr(tool, event, &session->evlist);
  984. if (err == 0) {
  985. perf_session__set_id_hdr_size(session);
  986. perf_session__set_comm_exec(session);
  987. }
  988. return err;
  989. case PERF_RECORD_HEADER_EVENT_TYPE:
  990. /*
  991. * Depreceated, but we need to handle it for sake
  992. * of old data files create in pipe mode.
  993. */
  994. return 0;
  995. case PERF_RECORD_HEADER_TRACING_DATA:
  996. /* setup for reading amidst mmap */
  997. lseek(fd, file_offset, SEEK_SET);
  998. return tool->tracing_data(tool, event, session);
  999. case PERF_RECORD_HEADER_BUILD_ID:
  1000. return tool->build_id(tool, event, session);
  1001. case PERF_RECORD_FINISHED_ROUND:
  1002. return tool->finished_round(tool, event, oe);
  1003. case PERF_RECORD_ID_INDEX:
  1004. return tool->id_index(tool, event, session);
  1005. case PERF_RECORD_AUXTRACE_INFO:
  1006. return tool->auxtrace_info(tool, event, session);
  1007. case PERF_RECORD_AUXTRACE:
  1008. /* setup for reading amidst mmap */
  1009. lseek(fd, file_offset + event->header.size, SEEK_SET);
  1010. return tool->auxtrace(tool, event, session);
  1011. case PERF_RECORD_AUXTRACE_ERROR:
  1012. perf_session__auxtrace_error_inc(session, event);
  1013. return tool->auxtrace_error(tool, event, session);
  1014. default:
  1015. return -EINVAL;
  1016. }
  1017. }
  1018. int perf_session__deliver_synth_event(struct perf_session *session,
  1019. union perf_event *event,
  1020. struct perf_sample *sample)
  1021. {
  1022. struct perf_evlist *evlist = session->evlist;
  1023. struct perf_tool *tool = session->tool;
  1024. events_stats__inc(&evlist->stats, event->header.type);
  1025. if (event->header.type >= PERF_RECORD_USER_TYPE_START)
  1026. return perf_session__process_user_event(session, event, 0);
  1027. return machines__deliver_event(&session->machines, evlist, event, sample, tool, 0);
  1028. }
  1029. static void event_swap(union perf_event *event, bool sample_id_all)
  1030. {
  1031. perf_event__swap_op swap;
  1032. swap = perf_event__swap_ops[event->header.type];
  1033. if (swap)
  1034. swap(event, sample_id_all);
  1035. }
  1036. int perf_session__peek_event(struct perf_session *session, off_t file_offset,
  1037. void *buf, size_t buf_sz,
  1038. union perf_event **event_ptr,
  1039. struct perf_sample *sample)
  1040. {
  1041. union perf_event *event;
  1042. size_t hdr_sz, rest;
  1043. int fd;
  1044. if (session->one_mmap && !session->header.needs_swap) {
  1045. event = file_offset - session->one_mmap_offset +
  1046. session->one_mmap_addr;
  1047. goto out_parse_sample;
  1048. }
  1049. if (perf_data_file__is_pipe(session->file))
  1050. return -1;
  1051. fd = perf_data_file__fd(session->file);
  1052. hdr_sz = sizeof(struct perf_event_header);
  1053. if (buf_sz < hdr_sz)
  1054. return -1;
  1055. if (lseek(fd, file_offset, SEEK_SET) == (off_t)-1 ||
  1056. readn(fd, buf, hdr_sz) != (ssize_t)hdr_sz)
  1057. return -1;
  1058. event = (union perf_event *)buf;
  1059. if (session->header.needs_swap)
  1060. perf_event_header__bswap(&event->header);
  1061. if (event->header.size < hdr_sz || event->header.size > buf_sz)
  1062. return -1;
  1063. rest = event->header.size - hdr_sz;
  1064. if (readn(fd, buf, rest) != (ssize_t)rest)
  1065. return -1;
  1066. if (session->header.needs_swap)
  1067. event_swap(event, perf_evlist__sample_id_all(session->evlist));
  1068. out_parse_sample:
  1069. if (sample && event->header.type < PERF_RECORD_USER_TYPE_START &&
  1070. perf_evlist__parse_sample(session->evlist, event, sample))
  1071. return -1;
  1072. *event_ptr = event;
  1073. return 0;
  1074. }
  1075. static s64 perf_session__process_event(struct perf_session *session,
  1076. union perf_event *event, u64 file_offset)
  1077. {
  1078. struct perf_evlist *evlist = session->evlist;
  1079. struct perf_tool *tool = session->tool;
  1080. struct perf_sample sample;
  1081. int ret;
  1082. if (session->header.needs_swap)
  1083. event_swap(event, perf_evlist__sample_id_all(evlist));
  1084. if (event->header.type >= PERF_RECORD_HEADER_MAX)
  1085. return -EINVAL;
  1086. events_stats__inc(&evlist->stats, event->header.type);
  1087. if (event->header.type >= PERF_RECORD_USER_TYPE_START)
  1088. return perf_session__process_user_event(session, event, file_offset);
  1089. /*
  1090. * For all kernel events we get the sample data
  1091. */
  1092. ret = perf_evlist__parse_sample(evlist, event, &sample);
  1093. if (ret)
  1094. return ret;
  1095. if (tool->ordered_events) {
  1096. ret = perf_session__queue_event(session, event, &sample, file_offset);
  1097. if (ret != -ETIME)
  1098. return ret;
  1099. }
  1100. return perf_session__deliver_event(session, event, &sample, tool,
  1101. file_offset);
  1102. }
  1103. void perf_event_header__bswap(struct perf_event_header *hdr)
  1104. {
  1105. hdr->type = bswap_32(hdr->type);
  1106. hdr->misc = bswap_16(hdr->misc);
  1107. hdr->size = bswap_16(hdr->size);
  1108. }
  1109. struct thread *perf_session__findnew(struct perf_session *session, pid_t pid)
  1110. {
  1111. return machine__findnew_thread(&session->machines.host, -1, pid);
  1112. }
  1113. struct thread *perf_session__register_idle_thread(struct perf_session *session)
  1114. {
  1115. struct thread *thread;
  1116. thread = machine__findnew_thread(&session->machines.host, 0, 0);
  1117. if (thread == NULL || thread__set_comm(thread, "swapper", 0)) {
  1118. pr_err("problem inserting idle task.\n");
  1119. thread = NULL;
  1120. }
  1121. return thread;
  1122. }
  1123. static void perf_session__warn_about_errors(const struct perf_session *session)
  1124. {
  1125. const struct events_stats *stats = &session->evlist->stats;
  1126. const struct ordered_events *oe = &session->ordered_events;
  1127. if (session->tool->lost == perf_event__process_lost &&
  1128. stats->nr_events[PERF_RECORD_LOST] != 0) {
  1129. ui__warning("Processed %d events and lost %d chunks!\n\n"
  1130. "Check IO/CPU overload!\n\n",
  1131. stats->nr_events[0],
  1132. stats->nr_events[PERF_RECORD_LOST]);
  1133. }
  1134. if (session->tool->lost_samples == perf_event__process_lost_samples) {
  1135. double drop_rate;
  1136. drop_rate = (double)stats->total_lost_samples /
  1137. (double) (stats->nr_events[PERF_RECORD_SAMPLE] + stats->total_lost_samples);
  1138. if (drop_rate > 0.05) {
  1139. ui__warning("Processed %" PRIu64 " samples and lost %3.2f%% samples!\n\n",
  1140. stats->nr_events[PERF_RECORD_SAMPLE] + stats->total_lost_samples,
  1141. drop_rate * 100.0);
  1142. }
  1143. }
  1144. if (session->tool->aux == perf_event__process_aux &&
  1145. stats->total_aux_lost != 0) {
  1146. ui__warning("AUX data lost %" PRIu64 " times out of %u!\n\n",
  1147. stats->total_aux_lost,
  1148. stats->nr_events[PERF_RECORD_AUX]);
  1149. }
  1150. if (stats->nr_unknown_events != 0) {
  1151. ui__warning("Found %u unknown events!\n\n"
  1152. "Is this an older tool processing a perf.data "
  1153. "file generated by a more recent tool?\n\n"
  1154. "If that is not the case, consider "
  1155. "reporting to linux-kernel@vger.kernel.org.\n\n",
  1156. stats->nr_unknown_events);
  1157. }
  1158. if (stats->nr_unknown_id != 0) {
  1159. ui__warning("%u samples with id not present in the header\n",
  1160. stats->nr_unknown_id);
  1161. }
  1162. if (stats->nr_invalid_chains != 0) {
  1163. ui__warning("Found invalid callchains!\n\n"
  1164. "%u out of %u events were discarded for this reason.\n\n"
  1165. "Consider reporting to linux-kernel@vger.kernel.org.\n\n",
  1166. stats->nr_invalid_chains,
  1167. stats->nr_events[PERF_RECORD_SAMPLE]);
  1168. }
  1169. if (stats->nr_unprocessable_samples != 0) {
  1170. ui__warning("%u unprocessable samples recorded.\n"
  1171. "Do you have a KVM guest running and not using 'perf kvm'?\n",
  1172. stats->nr_unprocessable_samples);
  1173. }
  1174. if (oe->nr_unordered_events != 0)
  1175. ui__warning("%u out of order events recorded.\n", oe->nr_unordered_events);
  1176. events_stats__auxtrace_error_warn(stats);
  1177. if (stats->nr_proc_map_timeout != 0) {
  1178. ui__warning("%d map information files for pre-existing threads were\n"
  1179. "not processed, if there are samples for addresses they\n"
  1180. "will not be resolved, you may find out which are these\n"
  1181. "threads by running with -v and redirecting the output\n"
  1182. "to a file.\n"
  1183. "The time limit to process proc map is too short?\n"
  1184. "Increase it by --proc-map-timeout\n",
  1185. stats->nr_proc_map_timeout);
  1186. }
  1187. }
  1188. static int perf_session__flush_thread_stack(struct thread *thread,
  1189. void *p __maybe_unused)
  1190. {
  1191. return thread_stack__flush(thread);
  1192. }
  1193. static int perf_session__flush_thread_stacks(struct perf_session *session)
  1194. {
  1195. return machines__for_each_thread(&session->machines,
  1196. perf_session__flush_thread_stack,
  1197. NULL);
  1198. }
  1199. volatile int session_done;
  1200. static int __perf_session__process_pipe_events(struct perf_session *session)
  1201. {
  1202. struct ordered_events *oe = &session->ordered_events;
  1203. struct perf_tool *tool = session->tool;
  1204. int fd = perf_data_file__fd(session->file);
  1205. union perf_event *event;
  1206. uint32_t size, cur_size = 0;
  1207. void *buf = NULL;
  1208. s64 skip = 0;
  1209. u64 head;
  1210. ssize_t err;
  1211. void *p;
  1212. perf_tool__fill_defaults(tool);
  1213. head = 0;
  1214. cur_size = sizeof(union perf_event);
  1215. buf = malloc(cur_size);
  1216. if (!buf)
  1217. return -errno;
  1218. ordered_events__set_copy_on_queue(oe, true);
  1219. more:
  1220. event = buf;
  1221. err = readn(fd, event, sizeof(struct perf_event_header));
  1222. if (err <= 0) {
  1223. if (err == 0)
  1224. goto done;
  1225. pr_err("failed to read event header\n");
  1226. goto out_err;
  1227. }
  1228. if (session->header.needs_swap)
  1229. perf_event_header__bswap(&event->header);
  1230. size = event->header.size;
  1231. if (size < sizeof(struct perf_event_header)) {
  1232. pr_err("bad event header size\n");
  1233. goto out_err;
  1234. }
  1235. if (size > cur_size) {
  1236. void *new = realloc(buf, size);
  1237. if (!new) {
  1238. pr_err("failed to allocate memory to read event\n");
  1239. goto out_err;
  1240. }
  1241. buf = new;
  1242. cur_size = size;
  1243. event = buf;
  1244. }
  1245. p = event;
  1246. p += sizeof(struct perf_event_header);
  1247. if (size - sizeof(struct perf_event_header)) {
  1248. err = readn(fd, p, size - sizeof(struct perf_event_header));
  1249. if (err <= 0) {
  1250. if (err == 0) {
  1251. pr_err("unexpected end of event stream\n");
  1252. goto done;
  1253. }
  1254. pr_err("failed to read event data\n");
  1255. goto out_err;
  1256. }
  1257. }
  1258. if ((skip = perf_session__process_event(session, event, head)) < 0) {
  1259. pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
  1260. head, event->header.size, event->header.type);
  1261. err = -EINVAL;
  1262. goto out_err;
  1263. }
  1264. head += size;
  1265. if (skip > 0)
  1266. head += skip;
  1267. if (!session_done())
  1268. goto more;
  1269. done:
  1270. /* do the final flush for ordered samples */
  1271. err = ordered_events__flush(oe, OE_FLUSH__FINAL);
  1272. if (err)
  1273. goto out_err;
  1274. err = auxtrace__flush_events(session, tool);
  1275. if (err)
  1276. goto out_err;
  1277. err = perf_session__flush_thread_stacks(session);
  1278. out_err:
  1279. free(buf);
  1280. perf_session__warn_about_errors(session);
  1281. ordered_events__free(&session->ordered_events);
  1282. auxtrace__free_events(session);
  1283. return err;
  1284. }
  1285. static union perf_event *
  1286. fetch_mmaped_event(struct perf_session *session,
  1287. u64 head, size_t mmap_size, char *buf)
  1288. {
  1289. union perf_event *event;
  1290. /*
  1291. * Ensure we have enough space remaining to read
  1292. * the size of the event in the headers.
  1293. */
  1294. if (head + sizeof(event->header) > mmap_size)
  1295. return NULL;
  1296. event = (union perf_event *)(buf + head);
  1297. if (session->header.needs_swap)
  1298. perf_event_header__bswap(&event->header);
  1299. if (head + event->header.size > mmap_size) {
  1300. /* We're not fetching the event so swap back again */
  1301. if (session->header.needs_swap)
  1302. perf_event_header__bswap(&event->header);
  1303. return NULL;
  1304. }
  1305. return event;
  1306. }
  1307. /*
  1308. * On 64bit we can mmap the data file in one go. No need for tiny mmap
  1309. * slices. On 32bit we use 32MB.
  1310. */
  1311. #if BITS_PER_LONG == 64
  1312. #define MMAP_SIZE ULLONG_MAX
  1313. #define NUM_MMAPS 1
  1314. #else
  1315. #define MMAP_SIZE (32 * 1024 * 1024ULL)
  1316. #define NUM_MMAPS 128
  1317. #endif
  1318. static int __perf_session__process_events(struct perf_session *session,
  1319. u64 data_offset, u64 data_size,
  1320. u64 file_size)
  1321. {
  1322. struct ordered_events *oe = &session->ordered_events;
  1323. struct perf_tool *tool = session->tool;
  1324. int fd = perf_data_file__fd(session->file);
  1325. u64 head, page_offset, file_offset, file_pos, size;
  1326. int err, mmap_prot, mmap_flags, map_idx = 0;
  1327. size_t mmap_size;
  1328. char *buf, *mmaps[NUM_MMAPS];
  1329. union perf_event *event;
  1330. struct ui_progress prog;
  1331. s64 skip;
  1332. perf_tool__fill_defaults(tool);
  1333. page_offset = page_size * (data_offset / page_size);
  1334. file_offset = page_offset;
  1335. head = data_offset - page_offset;
  1336. if (data_size == 0)
  1337. goto out;
  1338. if (data_offset + data_size < file_size)
  1339. file_size = data_offset + data_size;
  1340. ui_progress__init(&prog, file_size, "Processing events...");
  1341. mmap_size = MMAP_SIZE;
  1342. if (mmap_size > file_size) {
  1343. mmap_size = file_size;
  1344. session->one_mmap = true;
  1345. }
  1346. memset(mmaps, 0, sizeof(mmaps));
  1347. mmap_prot = PROT_READ;
  1348. mmap_flags = MAP_SHARED;
  1349. if (session->header.needs_swap) {
  1350. mmap_prot |= PROT_WRITE;
  1351. mmap_flags = MAP_PRIVATE;
  1352. }
  1353. remap:
  1354. buf = mmap(NULL, mmap_size, mmap_prot, mmap_flags, fd,
  1355. file_offset);
  1356. if (buf == MAP_FAILED) {
  1357. pr_err("failed to mmap file\n");
  1358. err = -errno;
  1359. goto out_err;
  1360. }
  1361. mmaps[map_idx] = buf;
  1362. map_idx = (map_idx + 1) & (ARRAY_SIZE(mmaps) - 1);
  1363. file_pos = file_offset + head;
  1364. if (session->one_mmap) {
  1365. session->one_mmap_addr = buf;
  1366. session->one_mmap_offset = file_offset;
  1367. }
  1368. more:
  1369. event = fetch_mmaped_event(session, head, mmap_size, buf);
  1370. if (!event) {
  1371. if (mmaps[map_idx]) {
  1372. munmap(mmaps[map_idx], mmap_size);
  1373. mmaps[map_idx] = NULL;
  1374. }
  1375. page_offset = page_size * (head / page_size);
  1376. file_offset += page_offset;
  1377. head -= page_offset;
  1378. goto remap;
  1379. }
  1380. size = event->header.size;
  1381. if (size < sizeof(struct perf_event_header) ||
  1382. (skip = perf_session__process_event(session, event, file_pos)) < 0) {
  1383. pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
  1384. file_offset + head, event->header.size,
  1385. event->header.type);
  1386. err = -EINVAL;
  1387. goto out_err;
  1388. }
  1389. if (skip)
  1390. size += skip;
  1391. head += size;
  1392. file_pos += size;
  1393. ui_progress__update(&prog, size);
  1394. if (session_done())
  1395. goto out;
  1396. if (file_pos < file_size)
  1397. goto more;
  1398. out:
  1399. /* do the final flush for ordered samples */
  1400. err = ordered_events__flush(oe, OE_FLUSH__FINAL);
  1401. if (err)
  1402. goto out_err;
  1403. err = auxtrace__flush_events(session, tool);
  1404. if (err)
  1405. goto out_err;
  1406. err = perf_session__flush_thread_stacks(session);
  1407. out_err:
  1408. ui_progress__finish();
  1409. perf_session__warn_about_errors(session);
  1410. ordered_events__free(&session->ordered_events);
  1411. auxtrace__free_events(session);
  1412. session->one_mmap = false;
  1413. return err;
  1414. }
  1415. int perf_session__process_events(struct perf_session *session)
  1416. {
  1417. u64 size = perf_data_file__size(session->file);
  1418. int err;
  1419. if (perf_session__register_idle_thread(session) == NULL)
  1420. return -ENOMEM;
  1421. if (!perf_data_file__is_pipe(session->file))
  1422. err = __perf_session__process_events(session,
  1423. session->header.data_offset,
  1424. session->header.data_size, size);
  1425. else
  1426. err = __perf_session__process_pipe_events(session);
  1427. return err;
  1428. }
  1429. bool perf_session__has_traces(struct perf_session *session, const char *msg)
  1430. {
  1431. struct perf_evsel *evsel;
  1432. evlist__for_each(session->evlist, evsel) {
  1433. if (evsel->attr.type == PERF_TYPE_TRACEPOINT)
  1434. return true;
  1435. }
  1436. pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg);
  1437. return false;
  1438. }
  1439. int maps__set_kallsyms_ref_reloc_sym(struct map **maps,
  1440. const char *symbol_name, u64 addr)
  1441. {
  1442. char *bracket;
  1443. enum map_type i;
  1444. struct ref_reloc_sym *ref;
  1445. ref = zalloc(sizeof(struct ref_reloc_sym));
  1446. if (ref == NULL)
  1447. return -ENOMEM;
  1448. ref->name = strdup(symbol_name);
  1449. if (ref->name == NULL) {
  1450. free(ref);
  1451. return -ENOMEM;
  1452. }
  1453. bracket = strchr(ref->name, ']');
  1454. if (bracket)
  1455. *bracket = '\0';
  1456. ref->addr = addr;
  1457. for (i = 0; i < MAP__NR_TYPES; ++i) {
  1458. struct kmap *kmap = map__kmap(maps[i]);
  1459. if (!kmap)
  1460. continue;
  1461. kmap->ref_reloc_sym = ref;
  1462. }
  1463. return 0;
  1464. }
  1465. size_t perf_session__fprintf_dsos(struct perf_session *session, FILE *fp)
  1466. {
  1467. return machines__fprintf_dsos(&session->machines, fp);
  1468. }
  1469. size_t perf_session__fprintf_dsos_buildid(struct perf_session *session, FILE *fp,
  1470. bool (skip)(struct dso *dso, int parm), int parm)
  1471. {
  1472. return machines__fprintf_dsos_buildid(&session->machines, fp, skip, parm);
  1473. }
  1474. size_t perf_session__fprintf_nr_events(struct perf_session *session, FILE *fp)
  1475. {
  1476. size_t ret;
  1477. const char *msg = "";
  1478. if (perf_header__has_feat(&session->header, HEADER_AUXTRACE))
  1479. msg = " (excludes AUX area (e.g. instruction trace) decoded / synthesized events)";
  1480. ret = fprintf(fp, "\nAggregated stats:%s\n", msg);
  1481. ret += events_stats__fprintf(&session->evlist->stats, fp);
  1482. return ret;
  1483. }
  1484. size_t perf_session__fprintf(struct perf_session *session, FILE *fp)
  1485. {
  1486. /*
  1487. * FIXME: Here we have to actually print all the machines in this
  1488. * session, not just the host...
  1489. */
  1490. return machine__fprintf(&session->machines.host, fp);
  1491. }
  1492. struct perf_evsel *perf_session__find_first_evtype(struct perf_session *session,
  1493. unsigned int type)
  1494. {
  1495. struct perf_evsel *pos;
  1496. evlist__for_each(session->evlist, pos) {
  1497. if (pos->attr.type == type)
  1498. return pos;
  1499. }
  1500. return NULL;
  1501. }
  1502. void perf_evsel__print_ip(struct perf_evsel *evsel, struct perf_sample *sample,
  1503. struct addr_location *al,
  1504. unsigned int print_opts, unsigned int stack_depth)
  1505. {
  1506. struct callchain_cursor_node *node;
  1507. int print_ip = print_opts & PRINT_IP_OPT_IP;
  1508. int print_sym = print_opts & PRINT_IP_OPT_SYM;
  1509. int print_dso = print_opts & PRINT_IP_OPT_DSO;
  1510. int print_symoffset = print_opts & PRINT_IP_OPT_SYMOFFSET;
  1511. int print_oneline = print_opts & PRINT_IP_OPT_ONELINE;
  1512. int print_srcline = print_opts & PRINT_IP_OPT_SRCLINE;
  1513. char s = print_oneline ? ' ' : '\t';
  1514. if (symbol_conf.use_callchain && sample->callchain) {
  1515. struct addr_location node_al;
  1516. if (thread__resolve_callchain(al->thread, evsel,
  1517. sample, NULL, NULL,
  1518. stack_depth) != 0) {
  1519. if (verbose)
  1520. error("Failed to resolve callchain. Skipping\n");
  1521. return;
  1522. }
  1523. callchain_cursor_commit(&callchain_cursor);
  1524. if (print_symoffset)
  1525. node_al = *al;
  1526. while (stack_depth) {
  1527. u64 addr = 0;
  1528. node = callchain_cursor_current(&callchain_cursor);
  1529. if (!node)
  1530. break;
  1531. if (node->sym && node->sym->ignore)
  1532. goto next;
  1533. if (print_ip)
  1534. printf("%c%16" PRIx64, s, node->ip);
  1535. if (node->map)
  1536. addr = node->map->map_ip(node->map, node->ip);
  1537. if (print_sym) {
  1538. printf(" ");
  1539. if (print_symoffset) {
  1540. node_al.addr = addr;
  1541. node_al.map = node->map;
  1542. symbol__fprintf_symname_offs(node->sym, &node_al, stdout);
  1543. } else
  1544. symbol__fprintf_symname(node->sym, stdout);
  1545. }
  1546. if (print_dso) {
  1547. printf(" (");
  1548. map__fprintf_dsoname(node->map, stdout);
  1549. printf(")");
  1550. }
  1551. if (print_srcline)
  1552. map__fprintf_srcline(node->map, addr, "\n ",
  1553. stdout);
  1554. if (!print_oneline)
  1555. printf("\n");
  1556. stack_depth--;
  1557. next:
  1558. callchain_cursor_advance(&callchain_cursor);
  1559. }
  1560. } else {
  1561. if (al->sym && al->sym->ignore)
  1562. return;
  1563. if (print_ip)
  1564. printf("%16" PRIx64, sample->ip);
  1565. if (print_sym) {
  1566. printf(" ");
  1567. if (print_symoffset)
  1568. symbol__fprintf_symname_offs(al->sym, al,
  1569. stdout);
  1570. else
  1571. symbol__fprintf_symname(al->sym, stdout);
  1572. }
  1573. if (print_dso) {
  1574. printf(" (");
  1575. map__fprintf_dsoname(al->map, stdout);
  1576. printf(")");
  1577. }
  1578. if (print_srcline)
  1579. map__fprintf_srcline(al->map, al->addr, "\n ", stdout);
  1580. }
  1581. }
  1582. int perf_session__cpu_bitmap(struct perf_session *session,
  1583. const char *cpu_list, unsigned long *cpu_bitmap)
  1584. {
  1585. int i, err = -1;
  1586. struct cpu_map *map;
  1587. for (i = 0; i < PERF_TYPE_MAX; ++i) {
  1588. struct perf_evsel *evsel;
  1589. evsel = perf_session__find_first_evtype(session, i);
  1590. if (!evsel)
  1591. continue;
  1592. if (!(evsel->attr.sample_type & PERF_SAMPLE_CPU)) {
  1593. pr_err("File does not contain CPU events. "
  1594. "Remove -c option to proceed.\n");
  1595. return -1;
  1596. }
  1597. }
  1598. map = cpu_map__new(cpu_list);
  1599. if (map == NULL) {
  1600. pr_err("Invalid cpu_list\n");
  1601. return -1;
  1602. }
  1603. for (i = 0; i < map->nr; i++) {
  1604. int cpu = map->map[i];
  1605. if (cpu >= MAX_NR_CPUS) {
  1606. pr_err("Requested CPU %d too large. "
  1607. "Consider raising MAX_NR_CPUS\n", cpu);
  1608. goto out_delete_map;
  1609. }
  1610. set_bit(cpu, cpu_bitmap);
  1611. }
  1612. err = 0;
  1613. out_delete_map:
  1614. cpu_map__put(map);
  1615. return err;
  1616. }
  1617. void perf_session__fprintf_info(struct perf_session *session, FILE *fp,
  1618. bool full)
  1619. {
  1620. struct stat st;
  1621. int fd, ret;
  1622. if (session == NULL || fp == NULL)
  1623. return;
  1624. fd = perf_data_file__fd(session->file);
  1625. ret = fstat(fd, &st);
  1626. if (ret == -1)
  1627. return;
  1628. fprintf(fp, "# ========\n");
  1629. fprintf(fp, "# captured on: %s", ctime(&st.st_ctime));
  1630. perf_header__fprintf_info(session, fp, full);
  1631. fprintf(fp, "# ========\n#\n");
  1632. }
  1633. int __perf_session__set_tracepoints_handlers(struct perf_session *session,
  1634. const struct perf_evsel_str_handler *assocs,
  1635. size_t nr_assocs)
  1636. {
  1637. struct perf_evsel *evsel;
  1638. size_t i;
  1639. int err;
  1640. for (i = 0; i < nr_assocs; i++) {
  1641. /*
  1642. * Adding a handler for an event not in the session,
  1643. * just ignore it.
  1644. */
  1645. evsel = perf_evlist__find_tracepoint_by_name(session->evlist, assocs[i].name);
  1646. if (evsel == NULL)
  1647. continue;
  1648. err = -EEXIST;
  1649. if (evsel->handler != NULL)
  1650. goto out;
  1651. evsel->handler = assocs[i].handler;
  1652. }
  1653. err = 0;
  1654. out:
  1655. return err;
  1656. }
  1657. int perf_event__process_id_index(struct perf_tool *tool __maybe_unused,
  1658. union perf_event *event,
  1659. struct perf_session *session)
  1660. {
  1661. struct perf_evlist *evlist = session->evlist;
  1662. struct id_index_event *ie = &event->id_index;
  1663. size_t i, nr, max_nr;
  1664. max_nr = (ie->header.size - sizeof(struct id_index_event)) /
  1665. sizeof(struct id_index_entry);
  1666. nr = ie->nr;
  1667. if (nr > max_nr)
  1668. return -EINVAL;
  1669. if (dump_trace)
  1670. fprintf(stdout, " nr: %zu\n", nr);
  1671. for (i = 0; i < nr; i++) {
  1672. struct id_index_entry *e = &ie->entries[i];
  1673. struct perf_sample_id *sid;
  1674. if (dump_trace) {
  1675. fprintf(stdout, " ... id: %"PRIu64, e->id);
  1676. fprintf(stdout, " idx: %"PRIu64, e->idx);
  1677. fprintf(stdout, " cpu: %"PRId64, e->cpu);
  1678. fprintf(stdout, " tid: %"PRId64"\n", e->tid);
  1679. }
  1680. sid = perf_evlist__id2sid(evlist, e->id);
  1681. if (!sid)
  1682. return -ENOENT;
  1683. sid->idx = e->idx;
  1684. sid->cpu = e->cpu;
  1685. sid->tid = e->tid;
  1686. }
  1687. return 0;
  1688. }
  1689. int perf_event__synthesize_id_index(struct perf_tool *tool,
  1690. perf_event__handler_t process,
  1691. struct perf_evlist *evlist,
  1692. struct machine *machine)
  1693. {
  1694. union perf_event *ev;
  1695. struct perf_evsel *evsel;
  1696. size_t nr = 0, i = 0, sz, max_nr, n;
  1697. int err;
  1698. pr_debug2("Synthesizing id index\n");
  1699. max_nr = (UINT16_MAX - sizeof(struct id_index_event)) /
  1700. sizeof(struct id_index_entry);
  1701. evlist__for_each(evlist, evsel)
  1702. nr += evsel->ids;
  1703. n = nr > max_nr ? max_nr : nr;
  1704. sz = sizeof(struct id_index_event) + n * sizeof(struct id_index_entry);
  1705. ev = zalloc(sz);
  1706. if (!ev)
  1707. return -ENOMEM;
  1708. ev->id_index.header.type = PERF_RECORD_ID_INDEX;
  1709. ev->id_index.header.size = sz;
  1710. ev->id_index.nr = n;
  1711. evlist__for_each(evlist, evsel) {
  1712. u32 j;
  1713. for (j = 0; j < evsel->ids; j++) {
  1714. struct id_index_entry *e;
  1715. struct perf_sample_id *sid;
  1716. if (i >= n) {
  1717. err = process(tool, ev, NULL, machine);
  1718. if (err)
  1719. goto out_err;
  1720. nr -= n;
  1721. i = 0;
  1722. }
  1723. e = &ev->id_index.entries[i++];
  1724. e->id = evsel->id[j];
  1725. sid = perf_evlist__id2sid(evlist, e->id);
  1726. if (!sid) {
  1727. free(ev);
  1728. return -ENOENT;
  1729. }
  1730. e->idx = sid->idx;
  1731. e->cpu = sid->cpu;
  1732. e->tid = sid->tid;
  1733. }
  1734. }
  1735. sz = sizeof(struct id_index_event) + nr * sizeof(struct id_index_entry);
  1736. ev->id_index.header.size = sz;
  1737. ev->id_index.nr = nr;
  1738. err = process(tool, ev, NULL, machine);
  1739. out_err:
  1740. free(ev);
  1741. return err;
  1742. }