session.c 44 KB

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