session.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602
  1. #define _FILE_OFFSET_BITS 64
  2. #include <linux/kernel.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. static int perf_session__open(struct perf_session *self, bool force)
  15. {
  16. struct stat input_stat;
  17. if (!strcmp(self->filename, "-")) {
  18. self->fd_pipe = true;
  19. self->fd = STDIN_FILENO;
  20. if (perf_session__read_header(self, self->fd) < 0)
  21. pr_err("incompatible file format (rerun with -v to learn more)");
  22. return 0;
  23. }
  24. self->fd = open(self->filename, O_RDONLY);
  25. if (self->fd < 0) {
  26. int err = errno;
  27. pr_err("failed to open %s: %s", self->filename, strerror(err));
  28. if (err == ENOENT && !strcmp(self->filename, "perf.data"))
  29. pr_err(" (try 'perf record' first)");
  30. pr_err("\n");
  31. return -errno;
  32. }
  33. if (fstat(self->fd, &input_stat) < 0)
  34. goto out_close;
  35. if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) {
  36. pr_err("file %s not owned by current user or root\n",
  37. self->filename);
  38. goto out_close;
  39. }
  40. if (!input_stat.st_size) {
  41. pr_info("zero-sized file (%s), nothing to do!\n",
  42. self->filename);
  43. goto out_close;
  44. }
  45. if (perf_session__read_header(self, self->fd) < 0) {
  46. pr_err("incompatible file format (rerun with -v to learn more)");
  47. goto out_close;
  48. }
  49. if (!perf_evlist__valid_sample_type(self->evlist)) {
  50. pr_err("non matching sample_type");
  51. goto out_close;
  52. }
  53. if (!perf_evlist__valid_sample_id_all(self->evlist)) {
  54. pr_err("non matching sample_id_all");
  55. goto out_close;
  56. }
  57. self->size = input_stat.st_size;
  58. return 0;
  59. out_close:
  60. close(self->fd);
  61. self->fd = -1;
  62. return -1;
  63. }
  64. void perf_session__update_sample_type(struct perf_session *self)
  65. {
  66. self->sample_type = perf_evlist__sample_type(self->evlist);
  67. self->sample_size = __perf_evsel__sample_size(self->sample_type);
  68. self->sample_id_all = perf_evlist__sample_id_all(self->evlist);
  69. self->id_hdr_size = perf_evlist__id_hdr_size(self->evlist);
  70. self->host_machine.id_hdr_size = self->id_hdr_size;
  71. }
  72. int perf_session__create_kernel_maps(struct perf_session *self)
  73. {
  74. int ret = machine__create_kernel_maps(&self->host_machine);
  75. if (ret >= 0)
  76. ret = machines__create_guest_kernel_maps(&self->machines);
  77. return ret;
  78. }
  79. static void perf_session__destroy_kernel_maps(struct perf_session *self)
  80. {
  81. machine__destroy_kernel_maps(&self->host_machine);
  82. machines__destroy_guest_kernel_maps(&self->machines);
  83. }
  84. struct perf_session *perf_session__new(const char *filename, int mode,
  85. bool force, bool repipe,
  86. struct perf_tool *tool)
  87. {
  88. struct perf_session *self;
  89. struct stat st;
  90. size_t len;
  91. if (!filename || !strlen(filename)) {
  92. if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
  93. filename = "-";
  94. else
  95. filename = "perf.data";
  96. }
  97. len = strlen(filename);
  98. self = zalloc(sizeof(*self) + len);
  99. if (self == NULL)
  100. goto out;
  101. memcpy(self->filename, filename, len);
  102. /*
  103. * On 64bit we can mmap the data file in one go. No need for tiny mmap
  104. * slices. On 32bit we use 32MB.
  105. */
  106. #if BITS_PER_LONG == 64
  107. self->mmap_window = ULLONG_MAX;
  108. #else
  109. self->mmap_window = 32 * 1024 * 1024ULL;
  110. #endif
  111. self->machines = RB_ROOT;
  112. self->repipe = repipe;
  113. INIT_LIST_HEAD(&self->ordered_samples.samples);
  114. INIT_LIST_HEAD(&self->ordered_samples.sample_cache);
  115. INIT_LIST_HEAD(&self->ordered_samples.to_free);
  116. machine__init(&self->host_machine, "", HOST_KERNEL_ID);
  117. hists__init(&self->hists);
  118. if (mode == O_RDONLY) {
  119. if (perf_session__open(self, force) < 0)
  120. goto out_delete;
  121. perf_session__update_sample_type(self);
  122. } else if (mode == O_WRONLY) {
  123. /*
  124. * In O_RDONLY mode this will be performed when reading the
  125. * kernel MMAP event, in perf_event__process_mmap().
  126. */
  127. if (perf_session__create_kernel_maps(self) < 0)
  128. goto out_delete;
  129. }
  130. if (tool && tool->ordering_requires_timestamps &&
  131. tool->ordered_samples && !self->sample_id_all) {
  132. dump_printf("WARNING: No sample_id_all support, falling back to unordered processing\n");
  133. tool->ordered_samples = false;
  134. }
  135. out:
  136. return self;
  137. out_delete:
  138. perf_session__delete(self);
  139. return NULL;
  140. }
  141. static void machine__delete_dead_threads(struct machine *machine)
  142. {
  143. struct thread *n, *t;
  144. list_for_each_entry_safe(t, n, &machine->dead_threads, node) {
  145. list_del(&t->node);
  146. thread__delete(t);
  147. }
  148. }
  149. static void perf_session__delete_dead_threads(struct perf_session *session)
  150. {
  151. machine__delete_dead_threads(&session->host_machine);
  152. }
  153. static void machine__delete_threads(struct machine *self)
  154. {
  155. struct rb_node *nd = rb_first(&self->threads);
  156. while (nd) {
  157. struct thread *t = rb_entry(nd, struct thread, rb_node);
  158. rb_erase(&t->rb_node, &self->threads);
  159. nd = rb_next(nd);
  160. thread__delete(t);
  161. }
  162. }
  163. static void perf_session__delete_threads(struct perf_session *session)
  164. {
  165. machine__delete_threads(&session->host_machine);
  166. }
  167. void perf_session__delete(struct perf_session *self)
  168. {
  169. perf_session__destroy_kernel_maps(self);
  170. perf_session__delete_dead_threads(self);
  171. perf_session__delete_threads(self);
  172. machine__exit(&self->host_machine);
  173. close(self->fd);
  174. free(self);
  175. }
  176. void machine__remove_thread(struct machine *self, struct thread *th)
  177. {
  178. self->last_match = NULL;
  179. rb_erase(&th->rb_node, &self->threads);
  180. /*
  181. * We may have references to this thread, for instance in some hist_entry
  182. * instances, so just move them to a separate list.
  183. */
  184. list_add_tail(&th->node, &self->dead_threads);
  185. }
  186. static bool symbol__match_parent_regex(struct symbol *sym)
  187. {
  188. if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
  189. return 1;
  190. return 0;
  191. }
  192. static const u8 cpumodes[] = {
  193. PERF_RECORD_MISC_USER,
  194. PERF_RECORD_MISC_KERNEL,
  195. PERF_RECORD_MISC_GUEST_USER,
  196. PERF_RECORD_MISC_GUEST_KERNEL
  197. };
  198. #define NCPUMODES (sizeof(cpumodes)/sizeof(u8))
  199. static void ip__resolve_ams(struct machine *self, struct thread *thread,
  200. struct addr_map_symbol *ams,
  201. u64 ip)
  202. {
  203. struct addr_location al;
  204. size_t i;
  205. u8 m;
  206. memset(&al, 0, sizeof(al));
  207. for (i = 0; i < NCPUMODES; i++) {
  208. m = cpumodes[i];
  209. /*
  210. * We cannot use the header.misc hint to determine whether a
  211. * branch stack address is user, kernel, guest, hypervisor.
  212. * Branches may straddle the kernel/user/hypervisor boundaries.
  213. * Thus, we have to try consecutively until we find a match
  214. * or else, the symbol is unknown
  215. */
  216. thread__find_addr_location(thread, self, m, MAP__FUNCTION,
  217. ip, &al, NULL);
  218. if (al.sym)
  219. goto found;
  220. }
  221. found:
  222. ams->addr = ip;
  223. ams->al_addr = al.addr;
  224. ams->sym = al.sym;
  225. ams->map = al.map;
  226. }
  227. struct branch_info *machine__resolve_bstack(struct machine *self,
  228. struct thread *thr,
  229. struct branch_stack *bs)
  230. {
  231. struct branch_info *bi;
  232. unsigned int i;
  233. bi = calloc(bs->nr, sizeof(struct branch_info));
  234. if (!bi)
  235. return NULL;
  236. for (i = 0; i < bs->nr; i++) {
  237. ip__resolve_ams(self, thr, &bi[i].to, bs->entries[i].to);
  238. ip__resolve_ams(self, thr, &bi[i].from, bs->entries[i].from);
  239. bi[i].flags = bs->entries[i].flags;
  240. }
  241. return bi;
  242. }
  243. int machine__resolve_callchain(struct machine *self,
  244. struct thread *thread,
  245. struct ip_callchain *chain,
  246. struct symbol **parent)
  247. {
  248. u8 cpumode = PERF_RECORD_MISC_USER;
  249. unsigned int i;
  250. int err;
  251. callchain_cursor_reset(&callchain_cursor);
  252. if (chain->nr > PERF_MAX_STACK_DEPTH) {
  253. pr_warning("corrupted callchain. skipping...\n");
  254. return 0;
  255. }
  256. for (i = 0; i < chain->nr; i++) {
  257. u64 ip;
  258. struct addr_location al;
  259. if (callchain_param.order == ORDER_CALLEE)
  260. ip = chain->ips[i];
  261. else
  262. ip = chain->ips[chain->nr - i - 1];
  263. if (ip >= PERF_CONTEXT_MAX) {
  264. switch (ip) {
  265. case PERF_CONTEXT_HV:
  266. cpumode = PERF_RECORD_MISC_HYPERVISOR; break;
  267. case PERF_CONTEXT_KERNEL:
  268. cpumode = PERF_RECORD_MISC_KERNEL; break;
  269. case PERF_CONTEXT_USER:
  270. cpumode = PERF_RECORD_MISC_USER; break;
  271. default:
  272. pr_debug("invalid callchain context: "
  273. "%"PRId64"\n", (s64) ip);
  274. /*
  275. * It seems the callchain is corrupted.
  276. * Discard all.
  277. */
  278. callchain_cursor_reset(&callchain_cursor);
  279. return 0;
  280. }
  281. continue;
  282. }
  283. al.filtered = false;
  284. thread__find_addr_location(thread, self, cpumode,
  285. MAP__FUNCTION, ip, &al, NULL);
  286. if (al.sym != NULL) {
  287. if (sort__has_parent && !*parent &&
  288. symbol__match_parent_regex(al.sym))
  289. *parent = al.sym;
  290. if (!symbol_conf.use_callchain)
  291. break;
  292. }
  293. err = callchain_cursor_append(&callchain_cursor,
  294. ip, al.map, al.sym);
  295. if (err)
  296. return err;
  297. }
  298. return 0;
  299. }
  300. static int process_event_synth_tracing_data_stub(union perf_event *event __used,
  301. struct perf_session *session __used)
  302. {
  303. dump_printf(": unhandled!\n");
  304. return 0;
  305. }
  306. static int process_event_synth_attr_stub(union perf_event *event __used,
  307. struct perf_evlist **pevlist __used)
  308. {
  309. dump_printf(": unhandled!\n");
  310. return 0;
  311. }
  312. static int process_event_sample_stub(struct perf_tool *tool __used,
  313. union perf_event *event __used,
  314. struct perf_sample *sample __used,
  315. struct perf_evsel *evsel __used,
  316. struct machine *machine __used)
  317. {
  318. dump_printf(": unhandled!\n");
  319. return 0;
  320. }
  321. static int process_event_stub(struct perf_tool *tool __used,
  322. union perf_event *event __used,
  323. struct perf_sample *sample __used,
  324. struct machine *machine __used)
  325. {
  326. dump_printf(": unhandled!\n");
  327. return 0;
  328. }
  329. static int process_finished_round_stub(struct perf_tool *tool __used,
  330. union perf_event *event __used,
  331. struct perf_session *perf_session __used)
  332. {
  333. dump_printf(": unhandled!\n");
  334. return 0;
  335. }
  336. static int process_event_type_stub(struct perf_tool *tool __used,
  337. union perf_event *event __used)
  338. {
  339. dump_printf(": unhandled!\n");
  340. return 0;
  341. }
  342. static int process_finished_round(struct perf_tool *tool,
  343. union perf_event *event,
  344. struct perf_session *session);
  345. static void perf_tool__fill_defaults(struct perf_tool *tool)
  346. {
  347. if (tool->sample == NULL)
  348. tool->sample = process_event_sample_stub;
  349. if (tool->mmap == NULL)
  350. tool->mmap = process_event_stub;
  351. if (tool->comm == NULL)
  352. tool->comm = process_event_stub;
  353. if (tool->fork == NULL)
  354. tool->fork = process_event_stub;
  355. if (tool->exit == NULL)
  356. tool->exit = process_event_stub;
  357. if (tool->lost == NULL)
  358. tool->lost = perf_event__process_lost;
  359. if (tool->read == NULL)
  360. tool->read = process_event_sample_stub;
  361. if (tool->throttle == NULL)
  362. tool->throttle = process_event_stub;
  363. if (tool->unthrottle == NULL)
  364. tool->unthrottle = process_event_stub;
  365. if (tool->attr == NULL)
  366. tool->attr = process_event_synth_attr_stub;
  367. if (tool->event_type == NULL)
  368. tool->event_type = process_event_type_stub;
  369. if (tool->tracing_data == NULL)
  370. tool->tracing_data = process_event_synth_tracing_data_stub;
  371. if (tool->build_id == NULL)
  372. tool->build_id = process_finished_round_stub;
  373. if (tool->finished_round == NULL) {
  374. if (tool->ordered_samples)
  375. tool->finished_round = process_finished_round;
  376. else
  377. tool->finished_round = process_finished_round_stub;
  378. }
  379. }
  380. void mem_bswap_64(void *src, int byte_size)
  381. {
  382. u64 *m = src;
  383. while (byte_size > 0) {
  384. *m = bswap_64(*m);
  385. byte_size -= sizeof(u64);
  386. ++m;
  387. }
  388. }
  389. static void swap_sample_id_all(union perf_event *event, void *data)
  390. {
  391. void *end = (void *) event + event->header.size;
  392. int size = end - data;
  393. BUG_ON(size % sizeof(u64));
  394. mem_bswap_64(data, size);
  395. }
  396. static void perf_event__all64_swap(union perf_event *event,
  397. bool sample_id_all __used)
  398. {
  399. struct perf_event_header *hdr = &event->header;
  400. mem_bswap_64(hdr + 1, event->header.size - sizeof(*hdr));
  401. }
  402. static void perf_event__comm_swap(union perf_event *event, bool sample_id_all)
  403. {
  404. event->comm.pid = bswap_32(event->comm.pid);
  405. event->comm.tid = bswap_32(event->comm.tid);
  406. if (sample_id_all) {
  407. void *data = &event->comm.comm;
  408. data += ALIGN(strlen(data) + 1, sizeof(u64));
  409. swap_sample_id_all(event, data);
  410. }
  411. }
  412. static void perf_event__mmap_swap(union perf_event *event,
  413. bool sample_id_all)
  414. {
  415. event->mmap.pid = bswap_32(event->mmap.pid);
  416. event->mmap.tid = bswap_32(event->mmap.tid);
  417. event->mmap.start = bswap_64(event->mmap.start);
  418. event->mmap.len = bswap_64(event->mmap.len);
  419. event->mmap.pgoff = bswap_64(event->mmap.pgoff);
  420. if (sample_id_all) {
  421. void *data = &event->mmap.filename;
  422. data += ALIGN(strlen(data) + 1, sizeof(u64));
  423. swap_sample_id_all(event, data);
  424. }
  425. }
  426. static void perf_event__task_swap(union perf_event *event, bool sample_id_all)
  427. {
  428. event->fork.pid = bswap_32(event->fork.pid);
  429. event->fork.tid = bswap_32(event->fork.tid);
  430. event->fork.ppid = bswap_32(event->fork.ppid);
  431. event->fork.ptid = bswap_32(event->fork.ptid);
  432. event->fork.time = bswap_64(event->fork.time);
  433. if (sample_id_all)
  434. swap_sample_id_all(event, &event->fork + 1);
  435. }
  436. static void perf_event__read_swap(union perf_event *event, bool sample_id_all)
  437. {
  438. event->read.pid = bswap_32(event->read.pid);
  439. event->read.tid = bswap_32(event->read.tid);
  440. event->read.value = bswap_64(event->read.value);
  441. event->read.time_enabled = bswap_64(event->read.time_enabled);
  442. event->read.time_running = bswap_64(event->read.time_running);
  443. event->read.id = bswap_64(event->read.id);
  444. if (sample_id_all)
  445. swap_sample_id_all(event, &event->read + 1);
  446. }
  447. static u8 revbyte(u8 b)
  448. {
  449. int rev = (b >> 4) | ((b & 0xf) << 4);
  450. rev = ((rev & 0xcc) >> 2) | ((rev & 0x33) << 2);
  451. rev = ((rev & 0xaa) >> 1) | ((rev & 0x55) << 1);
  452. return (u8) rev;
  453. }
  454. /*
  455. * XXX this is hack in attempt to carry flags bitfield
  456. * throught endian village. ABI says:
  457. *
  458. * Bit-fields are allocated from right to left (least to most significant)
  459. * on little-endian implementations and from left to right (most to least
  460. * significant) on big-endian implementations.
  461. *
  462. * The above seems to be byte specific, so we need to reverse each
  463. * byte of the bitfield. 'Internet' also says this might be implementation
  464. * specific and we probably need proper fix and carry perf_event_attr
  465. * bitfield flags in separate data file FEAT_ section. Thought this seems
  466. * to work for now.
  467. */
  468. static void swap_bitfield(u8 *p, unsigned len)
  469. {
  470. unsigned i;
  471. for (i = 0; i < len; i++) {
  472. *p = revbyte(*p);
  473. p++;
  474. }
  475. }
  476. /* exported for swapping attributes in file header */
  477. void perf_event__attr_swap(struct perf_event_attr *attr)
  478. {
  479. attr->type = bswap_32(attr->type);
  480. attr->size = bswap_32(attr->size);
  481. attr->config = bswap_64(attr->config);
  482. attr->sample_period = bswap_64(attr->sample_period);
  483. attr->sample_type = bswap_64(attr->sample_type);
  484. attr->read_format = bswap_64(attr->read_format);
  485. attr->wakeup_events = bswap_32(attr->wakeup_events);
  486. attr->bp_type = bswap_32(attr->bp_type);
  487. attr->bp_addr = bswap_64(attr->bp_addr);
  488. attr->bp_len = bswap_64(attr->bp_len);
  489. swap_bitfield((u8 *) (&attr->read_format + 1), sizeof(u64));
  490. }
  491. static void perf_event__hdr_attr_swap(union perf_event *event,
  492. bool sample_id_all __used)
  493. {
  494. size_t size;
  495. perf_event__attr_swap(&event->attr.attr);
  496. size = event->header.size;
  497. size -= (void *)&event->attr.id - (void *)event;
  498. mem_bswap_64(event->attr.id, size);
  499. }
  500. static void perf_event__event_type_swap(union perf_event *event,
  501. bool sample_id_all __used)
  502. {
  503. event->event_type.event_type.event_id =
  504. bswap_64(event->event_type.event_type.event_id);
  505. }
  506. static void perf_event__tracing_data_swap(union perf_event *event,
  507. bool sample_id_all __used)
  508. {
  509. event->tracing_data.size = bswap_32(event->tracing_data.size);
  510. }
  511. typedef void (*perf_event__swap_op)(union perf_event *event,
  512. bool sample_id_all);
  513. static perf_event__swap_op perf_event__swap_ops[] = {
  514. [PERF_RECORD_MMAP] = perf_event__mmap_swap,
  515. [PERF_RECORD_COMM] = perf_event__comm_swap,
  516. [PERF_RECORD_FORK] = perf_event__task_swap,
  517. [PERF_RECORD_EXIT] = perf_event__task_swap,
  518. [PERF_RECORD_LOST] = perf_event__all64_swap,
  519. [PERF_RECORD_READ] = perf_event__read_swap,
  520. [PERF_RECORD_SAMPLE] = perf_event__all64_swap,
  521. [PERF_RECORD_HEADER_ATTR] = perf_event__hdr_attr_swap,
  522. [PERF_RECORD_HEADER_EVENT_TYPE] = perf_event__event_type_swap,
  523. [PERF_RECORD_HEADER_TRACING_DATA] = perf_event__tracing_data_swap,
  524. [PERF_RECORD_HEADER_BUILD_ID] = NULL,
  525. [PERF_RECORD_HEADER_MAX] = NULL,
  526. };
  527. struct sample_queue {
  528. u64 timestamp;
  529. u64 file_offset;
  530. union perf_event *event;
  531. struct list_head list;
  532. };
  533. static void perf_session_free_sample_buffers(struct perf_session *session)
  534. {
  535. struct ordered_samples *os = &session->ordered_samples;
  536. while (!list_empty(&os->to_free)) {
  537. struct sample_queue *sq;
  538. sq = list_entry(os->to_free.next, struct sample_queue, list);
  539. list_del(&sq->list);
  540. free(sq);
  541. }
  542. }
  543. static int perf_session_deliver_event(struct perf_session *session,
  544. union perf_event *event,
  545. struct perf_sample *sample,
  546. struct perf_tool *tool,
  547. u64 file_offset);
  548. static void flush_sample_queue(struct perf_session *s,
  549. struct perf_tool *tool)
  550. {
  551. struct ordered_samples *os = &s->ordered_samples;
  552. struct list_head *head = &os->samples;
  553. struct sample_queue *tmp, *iter;
  554. struct perf_sample sample;
  555. u64 limit = os->next_flush;
  556. u64 last_ts = os->last_sample ? os->last_sample->timestamp : 0ULL;
  557. unsigned idx = 0, progress_next = os->nr_samples / 16;
  558. int ret;
  559. if (!tool->ordered_samples || !limit)
  560. return;
  561. list_for_each_entry_safe(iter, tmp, head, list) {
  562. if (iter->timestamp > limit)
  563. break;
  564. ret = perf_session__parse_sample(s, iter->event, &sample);
  565. if (ret)
  566. pr_err("Can't parse sample, err = %d\n", ret);
  567. else
  568. perf_session_deliver_event(s, iter->event, &sample, tool,
  569. iter->file_offset);
  570. os->last_flush = iter->timestamp;
  571. list_del(&iter->list);
  572. list_add(&iter->list, &os->sample_cache);
  573. if (++idx >= progress_next) {
  574. progress_next += os->nr_samples / 16;
  575. ui_progress__update(idx, os->nr_samples,
  576. "Processing time ordered events...");
  577. }
  578. }
  579. if (list_empty(head)) {
  580. os->last_sample = NULL;
  581. } else if (last_ts <= limit) {
  582. os->last_sample =
  583. list_entry(head->prev, struct sample_queue, list);
  584. }
  585. os->nr_samples = 0;
  586. }
  587. /*
  588. * When perf record finishes a pass on every buffers, it records this pseudo
  589. * event.
  590. * We record the max timestamp t found in the pass n.
  591. * Assuming these timestamps are monotonic across cpus, we know that if
  592. * a buffer still has events with timestamps below t, they will be all
  593. * available and then read in the pass n + 1.
  594. * Hence when we start to read the pass n + 2, we can safely flush every
  595. * events with timestamps below t.
  596. *
  597. * ============ PASS n =================
  598. * CPU 0 | CPU 1
  599. * |
  600. * cnt1 timestamps | cnt2 timestamps
  601. * 1 | 2
  602. * 2 | 3
  603. * - | 4 <--- max recorded
  604. *
  605. * ============ PASS n + 1 ==============
  606. * CPU 0 | CPU 1
  607. * |
  608. * cnt1 timestamps | cnt2 timestamps
  609. * 3 | 5
  610. * 4 | 6
  611. * 5 | 7 <---- max recorded
  612. *
  613. * Flush every events below timestamp 4
  614. *
  615. * ============ PASS n + 2 ==============
  616. * CPU 0 | CPU 1
  617. * |
  618. * cnt1 timestamps | cnt2 timestamps
  619. * 6 | 8
  620. * 7 | 9
  621. * - | 10
  622. *
  623. * Flush every events below timestamp 7
  624. * etc...
  625. */
  626. static int process_finished_round(struct perf_tool *tool,
  627. union perf_event *event __used,
  628. struct perf_session *session)
  629. {
  630. flush_sample_queue(session, tool);
  631. session->ordered_samples.next_flush = session->ordered_samples.max_timestamp;
  632. return 0;
  633. }
  634. /* The queue is ordered by time */
  635. static void __queue_event(struct sample_queue *new, struct perf_session *s)
  636. {
  637. struct ordered_samples *os = &s->ordered_samples;
  638. struct sample_queue *sample = os->last_sample;
  639. u64 timestamp = new->timestamp;
  640. struct list_head *p;
  641. ++os->nr_samples;
  642. os->last_sample = new;
  643. if (!sample) {
  644. list_add(&new->list, &os->samples);
  645. os->max_timestamp = timestamp;
  646. return;
  647. }
  648. /*
  649. * last_sample might point to some random place in the list as it's
  650. * the last queued event. We expect that the new event is close to
  651. * this.
  652. */
  653. if (sample->timestamp <= timestamp) {
  654. while (sample->timestamp <= timestamp) {
  655. p = sample->list.next;
  656. if (p == &os->samples) {
  657. list_add_tail(&new->list, &os->samples);
  658. os->max_timestamp = timestamp;
  659. return;
  660. }
  661. sample = list_entry(p, struct sample_queue, list);
  662. }
  663. list_add_tail(&new->list, &sample->list);
  664. } else {
  665. while (sample->timestamp > timestamp) {
  666. p = sample->list.prev;
  667. if (p == &os->samples) {
  668. list_add(&new->list, &os->samples);
  669. return;
  670. }
  671. sample = list_entry(p, struct sample_queue, list);
  672. }
  673. list_add(&new->list, &sample->list);
  674. }
  675. }
  676. #define MAX_SAMPLE_BUFFER (64 * 1024 / sizeof(struct sample_queue))
  677. static int perf_session_queue_event(struct perf_session *s, union perf_event *event,
  678. struct perf_sample *sample, u64 file_offset)
  679. {
  680. struct ordered_samples *os = &s->ordered_samples;
  681. struct list_head *sc = &os->sample_cache;
  682. u64 timestamp = sample->time;
  683. struct sample_queue *new;
  684. if (!timestamp || timestamp == ~0ULL)
  685. return -ETIME;
  686. if (timestamp < s->ordered_samples.last_flush) {
  687. printf("Warning: Timestamp below last timeslice flush\n");
  688. return -EINVAL;
  689. }
  690. if (!list_empty(sc)) {
  691. new = list_entry(sc->next, struct sample_queue, list);
  692. list_del(&new->list);
  693. } else if (os->sample_buffer) {
  694. new = os->sample_buffer + os->sample_buffer_idx;
  695. if (++os->sample_buffer_idx == MAX_SAMPLE_BUFFER)
  696. os->sample_buffer = NULL;
  697. } else {
  698. os->sample_buffer = malloc(MAX_SAMPLE_BUFFER * sizeof(*new));
  699. if (!os->sample_buffer)
  700. return -ENOMEM;
  701. list_add(&os->sample_buffer->list, &os->to_free);
  702. os->sample_buffer_idx = 2;
  703. new = os->sample_buffer + 1;
  704. }
  705. new->timestamp = timestamp;
  706. new->file_offset = file_offset;
  707. new->event = event;
  708. __queue_event(new, s);
  709. return 0;
  710. }
  711. static void callchain__printf(struct perf_sample *sample)
  712. {
  713. unsigned int i;
  714. printf("... chain: nr:%" PRIu64 "\n", sample->callchain->nr);
  715. for (i = 0; i < sample->callchain->nr; i++)
  716. printf("..... %2d: %016" PRIx64 "\n",
  717. i, sample->callchain->ips[i]);
  718. }
  719. static void branch_stack__printf(struct perf_sample *sample)
  720. {
  721. uint64_t i;
  722. printf("... branch stack: nr:%" PRIu64 "\n", sample->branch_stack->nr);
  723. for (i = 0; i < sample->branch_stack->nr; i++)
  724. printf("..... %2"PRIu64": %016" PRIx64 " -> %016" PRIx64 "\n",
  725. i, sample->branch_stack->entries[i].from,
  726. sample->branch_stack->entries[i].to);
  727. }
  728. static void perf_session__print_tstamp(struct perf_session *session,
  729. union perf_event *event,
  730. struct perf_sample *sample)
  731. {
  732. if (event->header.type != PERF_RECORD_SAMPLE &&
  733. !session->sample_id_all) {
  734. fputs("-1 -1 ", stdout);
  735. return;
  736. }
  737. if ((session->sample_type & PERF_SAMPLE_CPU))
  738. printf("%u ", sample->cpu);
  739. if (session->sample_type & PERF_SAMPLE_TIME)
  740. printf("%" PRIu64 " ", sample->time);
  741. }
  742. static void dump_event(struct perf_session *session, union perf_event *event,
  743. u64 file_offset, struct perf_sample *sample)
  744. {
  745. if (!dump_trace)
  746. return;
  747. printf("\n%#" PRIx64 " [%#x]: event: %d\n",
  748. file_offset, event->header.size, event->header.type);
  749. trace_event(event);
  750. if (sample)
  751. perf_session__print_tstamp(session, event, sample);
  752. printf("%#" PRIx64 " [%#x]: PERF_RECORD_%s", file_offset,
  753. event->header.size, perf_event__name(event->header.type));
  754. }
  755. static void dump_sample(struct perf_session *session, union perf_event *event,
  756. struct perf_sample *sample)
  757. {
  758. if (!dump_trace)
  759. return;
  760. printf("(IP, %d): %d/%d: %#" PRIx64 " period: %" PRIu64 " addr: %#" PRIx64 "\n",
  761. event->header.misc, sample->pid, sample->tid, sample->ip,
  762. sample->period, sample->addr);
  763. if (session->sample_type & PERF_SAMPLE_CALLCHAIN)
  764. callchain__printf(sample);
  765. if (session->sample_type & PERF_SAMPLE_BRANCH_STACK)
  766. branch_stack__printf(sample);
  767. }
  768. static struct machine *
  769. perf_session__find_machine_for_cpumode(struct perf_session *session,
  770. union perf_event *event)
  771. {
  772. const u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  773. if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
  774. u32 pid;
  775. if (event->header.type == PERF_RECORD_MMAP)
  776. pid = event->mmap.pid;
  777. else
  778. pid = event->ip.pid;
  779. return perf_session__find_machine(session, pid);
  780. }
  781. return perf_session__find_host_machine(session);
  782. }
  783. static int perf_session_deliver_event(struct perf_session *session,
  784. union perf_event *event,
  785. struct perf_sample *sample,
  786. struct perf_tool *tool,
  787. u64 file_offset)
  788. {
  789. struct perf_evsel *evsel;
  790. struct machine *machine;
  791. dump_event(session, event, file_offset, sample);
  792. evsel = perf_evlist__id2evsel(session->evlist, sample->id);
  793. if (evsel != NULL && event->header.type != PERF_RECORD_SAMPLE) {
  794. /*
  795. * XXX We're leaving PERF_RECORD_SAMPLE unnacounted here
  796. * because the tools right now may apply filters, discarding
  797. * some of the samples. For consistency, in the future we
  798. * should have something like nr_filtered_samples and remove
  799. * the sample->period from total_sample_period, etc, KISS for
  800. * now tho.
  801. *
  802. * Also testing against NULL allows us to handle files without
  803. * attr.sample_id_all and/or without PERF_SAMPLE_ID. In the
  804. * future probably it'll be a good idea to restrict event
  805. * processing via perf_session to files with both set.
  806. */
  807. hists__inc_nr_events(&evsel->hists, event->header.type);
  808. }
  809. machine = perf_session__find_machine_for_cpumode(session, event);
  810. switch (event->header.type) {
  811. case PERF_RECORD_SAMPLE:
  812. dump_sample(session, event, sample);
  813. if (evsel == NULL) {
  814. ++session->hists.stats.nr_unknown_id;
  815. return 0;
  816. }
  817. if (machine == NULL) {
  818. ++session->hists.stats.nr_unprocessable_samples;
  819. return 0;
  820. }
  821. return tool->sample(tool, event, sample, evsel, machine);
  822. case PERF_RECORD_MMAP:
  823. return tool->mmap(tool, event, sample, machine);
  824. case PERF_RECORD_COMM:
  825. return tool->comm(tool, event, sample, machine);
  826. case PERF_RECORD_FORK:
  827. return tool->fork(tool, event, sample, machine);
  828. case PERF_RECORD_EXIT:
  829. return tool->exit(tool, event, sample, machine);
  830. case PERF_RECORD_LOST:
  831. if (tool->lost == perf_event__process_lost)
  832. session->hists.stats.total_lost += event->lost.lost;
  833. return tool->lost(tool, event, sample, machine);
  834. case PERF_RECORD_READ:
  835. return tool->read(tool, event, sample, evsel, machine);
  836. case PERF_RECORD_THROTTLE:
  837. return tool->throttle(tool, event, sample, machine);
  838. case PERF_RECORD_UNTHROTTLE:
  839. return tool->unthrottle(tool, event, sample, machine);
  840. default:
  841. ++session->hists.stats.nr_unknown_events;
  842. return -1;
  843. }
  844. }
  845. static int perf_session__preprocess_sample(struct perf_session *session,
  846. union perf_event *event, struct perf_sample *sample)
  847. {
  848. if (event->header.type != PERF_RECORD_SAMPLE ||
  849. !(session->sample_type & PERF_SAMPLE_CALLCHAIN))
  850. return 0;
  851. if (!ip_callchain__valid(sample->callchain, event)) {
  852. pr_debug("call-chain problem with event, skipping it.\n");
  853. ++session->hists.stats.nr_invalid_chains;
  854. session->hists.stats.total_invalid_chains += sample->period;
  855. return -EINVAL;
  856. }
  857. return 0;
  858. }
  859. static int perf_session__process_user_event(struct perf_session *session, union perf_event *event,
  860. struct perf_tool *tool, u64 file_offset)
  861. {
  862. int err;
  863. dump_event(session, event, file_offset, NULL);
  864. /* These events are processed right away */
  865. switch (event->header.type) {
  866. case PERF_RECORD_HEADER_ATTR:
  867. err = tool->attr(event, &session->evlist);
  868. if (err == 0)
  869. perf_session__update_sample_type(session);
  870. return err;
  871. case PERF_RECORD_HEADER_EVENT_TYPE:
  872. return tool->event_type(tool, event);
  873. case PERF_RECORD_HEADER_TRACING_DATA:
  874. /* setup for reading amidst mmap */
  875. lseek(session->fd, file_offset, SEEK_SET);
  876. return tool->tracing_data(event, session);
  877. case PERF_RECORD_HEADER_BUILD_ID:
  878. return tool->build_id(tool, event, session);
  879. case PERF_RECORD_FINISHED_ROUND:
  880. return tool->finished_round(tool, event, session);
  881. default:
  882. return -EINVAL;
  883. }
  884. }
  885. static void event_swap(union perf_event *event, bool sample_id_all)
  886. {
  887. perf_event__swap_op swap;
  888. swap = perf_event__swap_ops[event->header.type];
  889. if (swap)
  890. swap(event, sample_id_all);
  891. }
  892. static int perf_session__process_event(struct perf_session *session,
  893. union perf_event *event,
  894. struct perf_tool *tool,
  895. u64 file_offset)
  896. {
  897. struct perf_sample sample;
  898. int ret;
  899. if (session->header.needs_swap)
  900. event_swap(event, session->sample_id_all);
  901. if (event->header.type >= PERF_RECORD_HEADER_MAX)
  902. return -EINVAL;
  903. hists__inc_nr_events(&session->hists, event->header.type);
  904. if (event->header.type >= PERF_RECORD_USER_TYPE_START)
  905. return perf_session__process_user_event(session, event, tool, file_offset);
  906. /*
  907. * For all kernel events we get the sample data
  908. */
  909. ret = perf_session__parse_sample(session, event, &sample);
  910. if (ret)
  911. return ret;
  912. /* Preprocess sample records - precheck callchains */
  913. if (perf_session__preprocess_sample(session, event, &sample))
  914. return 0;
  915. if (tool->ordered_samples) {
  916. ret = perf_session_queue_event(session, event, &sample,
  917. file_offset);
  918. if (ret != -ETIME)
  919. return ret;
  920. }
  921. return perf_session_deliver_event(session, event, &sample, tool,
  922. file_offset);
  923. }
  924. void perf_event_header__bswap(struct perf_event_header *self)
  925. {
  926. self->type = bswap_32(self->type);
  927. self->misc = bswap_16(self->misc);
  928. self->size = bswap_16(self->size);
  929. }
  930. struct thread *perf_session__findnew(struct perf_session *session, pid_t pid)
  931. {
  932. return machine__findnew_thread(&session->host_machine, pid);
  933. }
  934. static struct thread *perf_session__register_idle_thread(struct perf_session *self)
  935. {
  936. struct thread *thread = perf_session__findnew(self, 0);
  937. if (thread == NULL || thread__set_comm(thread, "swapper")) {
  938. pr_err("problem inserting idle task.\n");
  939. thread = NULL;
  940. }
  941. return thread;
  942. }
  943. static void perf_session__warn_about_errors(const struct perf_session *session,
  944. const struct perf_tool *tool)
  945. {
  946. if (tool->lost == perf_event__process_lost &&
  947. session->hists.stats.nr_events[PERF_RECORD_LOST] != 0) {
  948. ui__warning("Processed %d events and lost %d chunks!\n\n"
  949. "Check IO/CPU overload!\n\n",
  950. session->hists.stats.nr_events[0],
  951. session->hists.stats.nr_events[PERF_RECORD_LOST]);
  952. }
  953. if (session->hists.stats.nr_unknown_events != 0) {
  954. ui__warning("Found %u unknown events!\n\n"
  955. "Is this an older tool processing a perf.data "
  956. "file generated by a more recent tool?\n\n"
  957. "If that is not the case, consider "
  958. "reporting to linux-kernel@vger.kernel.org.\n\n",
  959. session->hists.stats.nr_unknown_events);
  960. }
  961. if (session->hists.stats.nr_unknown_id != 0) {
  962. ui__warning("%u samples with id not present in the header\n",
  963. session->hists.stats.nr_unknown_id);
  964. }
  965. if (session->hists.stats.nr_invalid_chains != 0) {
  966. ui__warning("Found invalid callchains!\n\n"
  967. "%u out of %u events were discarded for this reason.\n\n"
  968. "Consider reporting to linux-kernel@vger.kernel.org.\n\n",
  969. session->hists.stats.nr_invalid_chains,
  970. session->hists.stats.nr_events[PERF_RECORD_SAMPLE]);
  971. }
  972. if (session->hists.stats.nr_unprocessable_samples != 0) {
  973. ui__warning("%u unprocessable samples recorded.\n"
  974. "Do you have a KVM guest running and not using 'perf kvm'?\n",
  975. session->hists.stats.nr_unprocessable_samples);
  976. }
  977. }
  978. #define session_done() (*(volatile int *)(&session_done))
  979. volatile int session_done;
  980. static int __perf_session__process_pipe_events(struct perf_session *self,
  981. struct perf_tool *tool)
  982. {
  983. union perf_event *event;
  984. uint32_t size, cur_size = 0;
  985. void *buf = NULL;
  986. int skip = 0;
  987. u64 head;
  988. int err;
  989. void *p;
  990. perf_tool__fill_defaults(tool);
  991. head = 0;
  992. cur_size = sizeof(union perf_event);
  993. buf = malloc(cur_size);
  994. if (!buf)
  995. return -errno;
  996. more:
  997. event = buf;
  998. err = readn(self->fd, event, sizeof(struct perf_event_header));
  999. if (err <= 0) {
  1000. if (err == 0)
  1001. goto done;
  1002. pr_err("failed to read event header\n");
  1003. goto out_err;
  1004. }
  1005. if (self->header.needs_swap)
  1006. perf_event_header__bswap(&event->header);
  1007. size = event->header.size;
  1008. if (size == 0)
  1009. size = 8;
  1010. if (size > cur_size) {
  1011. void *new = realloc(buf, size);
  1012. if (!new) {
  1013. pr_err("failed to allocate memory to read event\n");
  1014. goto out_err;
  1015. }
  1016. buf = new;
  1017. cur_size = size;
  1018. event = buf;
  1019. }
  1020. p = event;
  1021. p += sizeof(struct perf_event_header);
  1022. if (size - sizeof(struct perf_event_header)) {
  1023. err = readn(self->fd, p, size - sizeof(struct perf_event_header));
  1024. if (err <= 0) {
  1025. if (err == 0) {
  1026. pr_err("unexpected end of event stream\n");
  1027. goto done;
  1028. }
  1029. pr_err("failed to read event data\n");
  1030. goto out_err;
  1031. }
  1032. }
  1033. if ((skip = perf_session__process_event(self, event, tool, head)) < 0) {
  1034. pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
  1035. head, event->header.size, event->header.type);
  1036. err = -EINVAL;
  1037. goto out_err;
  1038. }
  1039. head += size;
  1040. if (skip > 0)
  1041. head += skip;
  1042. if (!session_done())
  1043. goto more;
  1044. done:
  1045. err = 0;
  1046. out_err:
  1047. free(buf);
  1048. perf_session__warn_about_errors(self, tool);
  1049. perf_session_free_sample_buffers(self);
  1050. return err;
  1051. }
  1052. static union perf_event *
  1053. fetch_mmaped_event(struct perf_session *session,
  1054. u64 head, size_t mmap_size, char *buf)
  1055. {
  1056. union perf_event *event;
  1057. /*
  1058. * Ensure we have enough space remaining to read
  1059. * the size of the event in the headers.
  1060. */
  1061. if (head + sizeof(event->header) > mmap_size)
  1062. return NULL;
  1063. event = (union perf_event *)(buf + head);
  1064. if (session->header.needs_swap)
  1065. perf_event_header__bswap(&event->header);
  1066. if (head + event->header.size > mmap_size)
  1067. return NULL;
  1068. return event;
  1069. }
  1070. int __perf_session__process_events(struct perf_session *session,
  1071. u64 data_offset, u64 data_size,
  1072. u64 file_size, struct perf_tool *tool)
  1073. {
  1074. u64 head, page_offset, file_offset, file_pos, progress_next;
  1075. int err, mmap_prot, mmap_flags, map_idx = 0;
  1076. size_t page_size, mmap_size;
  1077. char *buf, *mmaps[8];
  1078. union perf_event *event;
  1079. uint32_t size;
  1080. perf_tool__fill_defaults(tool);
  1081. page_size = sysconf(_SC_PAGESIZE);
  1082. page_offset = page_size * (data_offset / page_size);
  1083. file_offset = page_offset;
  1084. head = data_offset - page_offset;
  1085. if (data_offset + data_size < file_size)
  1086. file_size = data_offset + data_size;
  1087. progress_next = file_size / 16;
  1088. mmap_size = session->mmap_window;
  1089. if (mmap_size > file_size)
  1090. mmap_size = file_size;
  1091. memset(mmaps, 0, sizeof(mmaps));
  1092. mmap_prot = PROT_READ;
  1093. mmap_flags = MAP_SHARED;
  1094. if (session->header.needs_swap) {
  1095. mmap_prot |= PROT_WRITE;
  1096. mmap_flags = MAP_PRIVATE;
  1097. }
  1098. remap:
  1099. buf = mmap(NULL, mmap_size, mmap_prot, mmap_flags, session->fd,
  1100. file_offset);
  1101. if (buf == MAP_FAILED) {
  1102. pr_err("failed to mmap file\n");
  1103. err = -errno;
  1104. goto out_err;
  1105. }
  1106. mmaps[map_idx] = buf;
  1107. map_idx = (map_idx + 1) & (ARRAY_SIZE(mmaps) - 1);
  1108. file_pos = file_offset + head;
  1109. more:
  1110. event = fetch_mmaped_event(session, head, mmap_size, buf);
  1111. if (!event) {
  1112. if (mmaps[map_idx]) {
  1113. munmap(mmaps[map_idx], mmap_size);
  1114. mmaps[map_idx] = NULL;
  1115. }
  1116. page_offset = page_size * (head / page_size);
  1117. file_offset += page_offset;
  1118. head -= page_offset;
  1119. goto remap;
  1120. }
  1121. size = event->header.size;
  1122. if (size == 0 ||
  1123. perf_session__process_event(session, event, tool, file_pos) < 0) {
  1124. pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
  1125. file_offset + head, event->header.size,
  1126. event->header.type);
  1127. err = -EINVAL;
  1128. goto out_err;
  1129. }
  1130. head += size;
  1131. file_pos += size;
  1132. if (file_pos >= progress_next) {
  1133. progress_next += file_size / 16;
  1134. ui_progress__update(file_pos, file_size,
  1135. "Processing events...");
  1136. }
  1137. if (file_pos < file_size)
  1138. goto more;
  1139. err = 0;
  1140. /* do the final flush for ordered samples */
  1141. session->ordered_samples.next_flush = ULLONG_MAX;
  1142. flush_sample_queue(session, tool);
  1143. out_err:
  1144. perf_session__warn_about_errors(session, tool);
  1145. perf_session_free_sample_buffers(session);
  1146. return err;
  1147. }
  1148. int perf_session__process_events(struct perf_session *self,
  1149. struct perf_tool *tool)
  1150. {
  1151. int err;
  1152. if (perf_session__register_idle_thread(self) == NULL)
  1153. return -ENOMEM;
  1154. if (!self->fd_pipe)
  1155. err = __perf_session__process_events(self,
  1156. self->header.data_offset,
  1157. self->header.data_size,
  1158. self->size, tool);
  1159. else
  1160. err = __perf_session__process_pipe_events(self, tool);
  1161. return err;
  1162. }
  1163. bool perf_session__has_traces(struct perf_session *self, const char *msg)
  1164. {
  1165. if (!(self->sample_type & PERF_SAMPLE_RAW)) {
  1166. pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg);
  1167. return false;
  1168. }
  1169. return true;
  1170. }
  1171. int maps__set_kallsyms_ref_reloc_sym(struct map **maps,
  1172. const char *symbol_name, u64 addr)
  1173. {
  1174. char *bracket;
  1175. enum map_type i;
  1176. struct ref_reloc_sym *ref;
  1177. ref = zalloc(sizeof(struct ref_reloc_sym));
  1178. if (ref == NULL)
  1179. return -ENOMEM;
  1180. ref->name = strdup(symbol_name);
  1181. if (ref->name == NULL) {
  1182. free(ref);
  1183. return -ENOMEM;
  1184. }
  1185. bracket = strchr(ref->name, ']');
  1186. if (bracket)
  1187. *bracket = '\0';
  1188. ref->addr = addr;
  1189. for (i = 0; i < MAP__NR_TYPES; ++i) {
  1190. struct kmap *kmap = map__kmap(maps[i]);
  1191. kmap->ref_reloc_sym = ref;
  1192. }
  1193. return 0;
  1194. }
  1195. size_t perf_session__fprintf_dsos(struct perf_session *self, FILE *fp)
  1196. {
  1197. return __dsos__fprintf(&self->host_machine.kernel_dsos, fp) +
  1198. __dsos__fprintf(&self->host_machine.user_dsos, fp) +
  1199. machines__fprintf_dsos(&self->machines, fp);
  1200. }
  1201. size_t perf_session__fprintf_dsos_buildid(struct perf_session *self, FILE *fp,
  1202. bool with_hits)
  1203. {
  1204. size_t ret = machine__fprintf_dsos_buildid(&self->host_machine, fp, with_hits);
  1205. return ret + machines__fprintf_dsos_buildid(&self->machines, fp, with_hits);
  1206. }
  1207. size_t perf_session__fprintf_nr_events(struct perf_session *session, FILE *fp)
  1208. {
  1209. struct perf_evsel *pos;
  1210. size_t ret = fprintf(fp, "Aggregated stats:\n");
  1211. ret += hists__fprintf_nr_events(&session->hists, fp);
  1212. list_for_each_entry(pos, &session->evlist->entries, node) {
  1213. ret += fprintf(fp, "%s stats:\n", perf_evsel__name(pos));
  1214. ret += hists__fprintf_nr_events(&pos->hists, fp);
  1215. }
  1216. return ret;
  1217. }
  1218. size_t perf_session__fprintf(struct perf_session *session, FILE *fp)
  1219. {
  1220. /*
  1221. * FIXME: Here we have to actually print all the machines in this
  1222. * session, not just the host...
  1223. */
  1224. return machine__fprintf(&session->host_machine, fp);
  1225. }
  1226. void perf_session__remove_thread(struct perf_session *session,
  1227. struct thread *th)
  1228. {
  1229. /*
  1230. * FIXME: This one makes no sense, we need to remove the thread from
  1231. * the machine it belongs to, perf_session can have many machines, so
  1232. * doing it always on ->host_machine is wrong. Fix when auditing all
  1233. * the 'perf kvm' code.
  1234. */
  1235. machine__remove_thread(&session->host_machine, th);
  1236. }
  1237. struct perf_evsel *perf_session__find_first_evtype(struct perf_session *session,
  1238. unsigned int type)
  1239. {
  1240. struct perf_evsel *pos;
  1241. list_for_each_entry(pos, &session->evlist->entries, node) {
  1242. if (pos->attr.type == type)
  1243. return pos;
  1244. }
  1245. return NULL;
  1246. }
  1247. void perf_event__print_ip(union perf_event *event, struct perf_sample *sample,
  1248. struct machine *machine, int print_sym,
  1249. int print_dso, int print_symoffset)
  1250. {
  1251. struct addr_location al;
  1252. struct callchain_cursor_node *node;
  1253. if (perf_event__preprocess_sample(event, machine, &al, sample,
  1254. NULL) < 0) {
  1255. error("problem processing %d event, skipping it.\n",
  1256. event->header.type);
  1257. return;
  1258. }
  1259. if (symbol_conf.use_callchain && sample->callchain) {
  1260. if (machine__resolve_callchain(machine, al.thread,
  1261. sample->callchain, NULL) != 0) {
  1262. if (verbose)
  1263. error("Failed to resolve callchain. Skipping\n");
  1264. return;
  1265. }
  1266. callchain_cursor_commit(&callchain_cursor);
  1267. while (1) {
  1268. node = callchain_cursor_current(&callchain_cursor);
  1269. if (!node)
  1270. break;
  1271. printf("\t%16" PRIx64, node->ip);
  1272. if (print_sym) {
  1273. printf(" ");
  1274. symbol__fprintf_symname(node->sym, stdout);
  1275. }
  1276. if (print_dso) {
  1277. printf(" (");
  1278. map__fprintf_dsoname(node->map, stdout);
  1279. printf(")");
  1280. }
  1281. printf("\n");
  1282. callchain_cursor_advance(&callchain_cursor);
  1283. }
  1284. } else {
  1285. printf("%16" PRIx64, sample->ip);
  1286. if (print_sym) {
  1287. printf(" ");
  1288. if (print_symoffset)
  1289. symbol__fprintf_symname_offs(al.sym, &al,
  1290. stdout);
  1291. else
  1292. symbol__fprintf_symname(al.sym, stdout);
  1293. }
  1294. if (print_dso) {
  1295. printf(" (");
  1296. map__fprintf_dsoname(al.map, stdout);
  1297. printf(")");
  1298. }
  1299. }
  1300. }
  1301. int perf_session__cpu_bitmap(struct perf_session *session,
  1302. const char *cpu_list, unsigned long *cpu_bitmap)
  1303. {
  1304. int i;
  1305. struct cpu_map *map;
  1306. for (i = 0; i < PERF_TYPE_MAX; ++i) {
  1307. struct perf_evsel *evsel;
  1308. evsel = perf_session__find_first_evtype(session, i);
  1309. if (!evsel)
  1310. continue;
  1311. if (!(evsel->attr.sample_type & PERF_SAMPLE_CPU)) {
  1312. pr_err("File does not contain CPU events. "
  1313. "Remove -c option to proceed.\n");
  1314. return -1;
  1315. }
  1316. }
  1317. map = cpu_map__new(cpu_list);
  1318. if (map == NULL) {
  1319. pr_err("Invalid cpu_list\n");
  1320. return -1;
  1321. }
  1322. for (i = 0; i < map->nr; i++) {
  1323. int cpu = map->map[i];
  1324. if (cpu >= MAX_NR_CPUS) {
  1325. pr_err("Requested CPU %d too large. "
  1326. "Consider raising MAX_NR_CPUS\n", cpu);
  1327. return -1;
  1328. }
  1329. set_bit(cpu, cpu_bitmap);
  1330. }
  1331. return 0;
  1332. }
  1333. void perf_session__fprintf_info(struct perf_session *session, FILE *fp,
  1334. bool full)
  1335. {
  1336. struct stat st;
  1337. int ret;
  1338. if (session == NULL || fp == NULL)
  1339. return;
  1340. ret = fstat(session->fd, &st);
  1341. if (ret == -1)
  1342. return;
  1343. fprintf(fp, "# ========\n");
  1344. fprintf(fp, "# captured on: %s", ctime(&st.st_ctime));
  1345. perf_header__fprintf_info(session, fp, full);
  1346. fprintf(fp, "# ========\n#\n");
  1347. }