session.c 47 KB

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