session.c 45 KB

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