session.c 42 KB

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