session.c 42 KB

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