session.c 58 KB

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