session.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752
  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 void 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;
  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. perf_session_deliver_event(s, iter->event, &sample, tool,
  609. iter->file_offset);
  610. os->last_flush = iter->timestamp;
  611. list_del(&iter->list);
  612. list_add(&iter->list, &os->sample_cache);
  613. if (++idx >= progress_next) {
  614. progress_next += os->nr_samples / 16;
  615. ui_progress__update(idx, os->nr_samples,
  616. "Processing time ordered events...");
  617. }
  618. }
  619. if (list_empty(head)) {
  620. os->last_sample = NULL;
  621. } else if (last_ts <= limit) {
  622. os->last_sample =
  623. list_entry(head->prev, struct sample_queue, list);
  624. }
  625. os->nr_samples = 0;
  626. }
  627. /*
  628. * When perf record finishes a pass on every buffers, it records this pseudo
  629. * event.
  630. * We record the max timestamp t found in the pass n.
  631. * Assuming these timestamps are monotonic across cpus, we know that if
  632. * a buffer still has events with timestamps below t, they will be all
  633. * available and then read in the pass n + 1.
  634. * Hence when we start to read the pass n + 2, we can safely flush every
  635. * events with timestamps below t.
  636. *
  637. * ============ PASS n =================
  638. * CPU 0 | CPU 1
  639. * |
  640. * cnt1 timestamps | cnt2 timestamps
  641. * 1 | 2
  642. * 2 | 3
  643. * - | 4 <--- max recorded
  644. *
  645. * ============ PASS n + 1 ==============
  646. * CPU 0 | CPU 1
  647. * |
  648. * cnt1 timestamps | cnt2 timestamps
  649. * 3 | 5
  650. * 4 | 6
  651. * 5 | 7 <---- max recorded
  652. *
  653. * Flush every events below timestamp 4
  654. *
  655. * ============ PASS n + 2 ==============
  656. * CPU 0 | CPU 1
  657. * |
  658. * cnt1 timestamps | cnt2 timestamps
  659. * 6 | 8
  660. * 7 | 9
  661. * - | 10
  662. *
  663. * Flush every events below timestamp 7
  664. * etc...
  665. */
  666. static int process_finished_round(struct perf_tool *tool,
  667. union perf_event *event __used,
  668. struct perf_session *session)
  669. {
  670. flush_sample_queue(session, tool);
  671. session->ordered_samples.next_flush = session->ordered_samples.max_timestamp;
  672. return 0;
  673. }
  674. /* The queue is ordered by time */
  675. static void __queue_event(struct sample_queue *new, struct perf_session *s)
  676. {
  677. struct ordered_samples *os = &s->ordered_samples;
  678. struct sample_queue *sample = os->last_sample;
  679. u64 timestamp = new->timestamp;
  680. struct list_head *p;
  681. ++os->nr_samples;
  682. os->last_sample = new;
  683. if (!sample) {
  684. list_add(&new->list, &os->samples);
  685. os->max_timestamp = timestamp;
  686. return;
  687. }
  688. /*
  689. * last_sample might point to some random place in the list as it's
  690. * the last queued event. We expect that the new event is close to
  691. * this.
  692. */
  693. if (sample->timestamp <= timestamp) {
  694. while (sample->timestamp <= timestamp) {
  695. p = sample->list.next;
  696. if (p == &os->samples) {
  697. list_add_tail(&new->list, &os->samples);
  698. os->max_timestamp = timestamp;
  699. return;
  700. }
  701. sample = list_entry(p, struct sample_queue, list);
  702. }
  703. list_add_tail(&new->list, &sample->list);
  704. } else {
  705. while (sample->timestamp > timestamp) {
  706. p = sample->list.prev;
  707. if (p == &os->samples) {
  708. list_add(&new->list, &os->samples);
  709. return;
  710. }
  711. sample = list_entry(p, struct sample_queue, list);
  712. }
  713. list_add(&new->list, &sample->list);
  714. }
  715. }
  716. #define MAX_SAMPLE_BUFFER (64 * 1024 / sizeof(struct sample_queue))
  717. static int perf_session_queue_event(struct perf_session *s, union perf_event *event,
  718. struct perf_sample *sample, u64 file_offset)
  719. {
  720. struct ordered_samples *os = &s->ordered_samples;
  721. struct list_head *sc = &os->sample_cache;
  722. u64 timestamp = sample->time;
  723. struct sample_queue *new;
  724. if (!timestamp || timestamp == ~0ULL)
  725. return -ETIME;
  726. if (timestamp < s->ordered_samples.last_flush) {
  727. printf("Warning: Timestamp below last timeslice flush\n");
  728. return -EINVAL;
  729. }
  730. if (!list_empty(sc)) {
  731. new = list_entry(sc->next, struct sample_queue, list);
  732. list_del(&new->list);
  733. } else if (os->sample_buffer) {
  734. new = os->sample_buffer + os->sample_buffer_idx;
  735. if (++os->sample_buffer_idx == MAX_SAMPLE_BUFFER)
  736. os->sample_buffer = NULL;
  737. } else {
  738. os->sample_buffer = malloc(MAX_SAMPLE_BUFFER * sizeof(*new));
  739. if (!os->sample_buffer)
  740. return -ENOMEM;
  741. list_add(&os->sample_buffer->list, &os->to_free);
  742. os->sample_buffer_idx = 2;
  743. new = os->sample_buffer + 1;
  744. }
  745. new->timestamp = timestamp;
  746. new->file_offset = file_offset;
  747. new->event = event;
  748. __queue_event(new, s);
  749. return 0;
  750. }
  751. static void callchain__printf(struct perf_sample *sample)
  752. {
  753. unsigned int i;
  754. printf("... chain: nr:%" PRIu64 "\n", sample->callchain->nr);
  755. for (i = 0; i < sample->callchain->nr; i++)
  756. printf("..... %2d: %016" PRIx64 "\n",
  757. i, sample->callchain->ips[i]);
  758. }
  759. static void branch_stack__printf(struct perf_sample *sample)
  760. {
  761. uint64_t i;
  762. printf("... branch stack: nr:%" PRIu64 "\n", sample->branch_stack->nr);
  763. for (i = 0; i < sample->branch_stack->nr; i++)
  764. printf("..... %2"PRIu64": %016" PRIx64 " -> %016" PRIx64 "\n",
  765. i, sample->branch_stack->entries[i].from,
  766. sample->branch_stack->entries[i].to);
  767. }
  768. static void regs_dump__printf(u64 mask, u64 *regs)
  769. {
  770. unsigned rid, i = 0;
  771. for_each_set_bit(rid, (unsigned long *) &mask, sizeof(mask) * 8) {
  772. u64 val = regs[i++];
  773. printf(".... %-5s 0x%" PRIx64 "\n",
  774. perf_reg_name(rid), val);
  775. }
  776. }
  777. static void regs_user__printf(struct perf_sample *sample, u64 mask)
  778. {
  779. struct regs_dump *user_regs = &sample->user_regs;
  780. if (user_regs->regs) {
  781. printf("... user regs: mask 0x%" PRIx64 "\n", mask);
  782. regs_dump__printf(mask, user_regs->regs);
  783. }
  784. }
  785. static void stack_user__printf(struct stack_dump *dump)
  786. {
  787. printf("... ustack: size %" PRIu64 ", offset 0x%x\n",
  788. dump->size, dump->offset);
  789. }
  790. static void perf_session__print_tstamp(struct perf_session *session,
  791. union perf_event *event,
  792. struct perf_sample *sample)
  793. {
  794. u64 sample_type = perf_evlist__sample_type(session->evlist);
  795. if (event->header.type != PERF_RECORD_SAMPLE &&
  796. !perf_evlist__sample_id_all(session->evlist)) {
  797. fputs("-1 -1 ", stdout);
  798. return;
  799. }
  800. if ((sample_type & PERF_SAMPLE_CPU))
  801. printf("%u ", sample->cpu);
  802. if (sample_type & PERF_SAMPLE_TIME)
  803. printf("%" PRIu64 " ", sample->time);
  804. }
  805. static void dump_event(struct perf_session *session, union perf_event *event,
  806. u64 file_offset, struct perf_sample *sample)
  807. {
  808. if (!dump_trace)
  809. return;
  810. printf("\n%#" PRIx64 " [%#x]: event: %d\n",
  811. file_offset, event->header.size, event->header.type);
  812. trace_event(event);
  813. if (sample)
  814. perf_session__print_tstamp(session, event, sample);
  815. printf("%#" PRIx64 " [%#x]: PERF_RECORD_%s", file_offset,
  816. event->header.size, perf_event__name(event->header.type));
  817. }
  818. static void dump_sample(struct perf_evsel *evsel, union perf_event *event,
  819. struct perf_sample *sample)
  820. {
  821. u64 sample_type;
  822. if (!dump_trace)
  823. return;
  824. printf("(IP, %d): %d/%d: %#" PRIx64 " period: %" PRIu64 " addr: %#" PRIx64 "\n",
  825. event->header.misc, sample->pid, sample->tid, sample->ip,
  826. sample->period, sample->addr);
  827. sample_type = evsel->attr.sample_type;
  828. if (sample_type & PERF_SAMPLE_CALLCHAIN)
  829. callchain__printf(sample);
  830. if (sample_type & PERF_SAMPLE_BRANCH_STACK)
  831. branch_stack__printf(sample);
  832. if (sample_type & PERF_SAMPLE_REGS_USER)
  833. regs_user__printf(sample, evsel->attr.sample_regs_user);
  834. if (sample_type & PERF_SAMPLE_STACK_USER)
  835. stack_user__printf(&sample->user_stack);
  836. }
  837. static struct machine *
  838. perf_session__find_machine_for_cpumode(struct perf_session *session,
  839. union perf_event *event)
  840. {
  841. const u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  842. if (perf_guest &&
  843. ((cpumode == PERF_RECORD_MISC_GUEST_KERNEL) ||
  844. (cpumode == PERF_RECORD_MISC_GUEST_USER))) {
  845. u32 pid;
  846. if (event->header.type == PERF_RECORD_MMAP)
  847. pid = event->mmap.pid;
  848. else
  849. pid = event->ip.pid;
  850. return perf_session__findnew_machine(session, pid);
  851. }
  852. return perf_session__find_host_machine(session);
  853. }
  854. static int perf_session_deliver_event(struct perf_session *session,
  855. union perf_event *event,
  856. struct perf_sample *sample,
  857. struct perf_tool *tool,
  858. u64 file_offset)
  859. {
  860. struct perf_evsel *evsel;
  861. struct machine *machine;
  862. dump_event(session, event, file_offset, sample);
  863. evsel = perf_evlist__id2evsel(session->evlist, sample->id);
  864. if (evsel != NULL && event->header.type != PERF_RECORD_SAMPLE) {
  865. /*
  866. * XXX We're leaving PERF_RECORD_SAMPLE unnacounted here
  867. * because the tools right now may apply filters, discarding
  868. * some of the samples. For consistency, in the future we
  869. * should have something like nr_filtered_samples and remove
  870. * the sample->period from total_sample_period, etc, KISS for
  871. * now tho.
  872. *
  873. * Also testing against NULL allows us to handle files without
  874. * attr.sample_id_all and/or without PERF_SAMPLE_ID. In the
  875. * future probably it'll be a good idea to restrict event
  876. * processing via perf_session to files with both set.
  877. */
  878. hists__inc_nr_events(&evsel->hists, event->header.type);
  879. }
  880. machine = perf_session__find_machine_for_cpumode(session, event);
  881. switch (event->header.type) {
  882. case PERF_RECORD_SAMPLE:
  883. dump_sample(evsel, event, sample);
  884. if (evsel == NULL) {
  885. ++session->hists.stats.nr_unknown_id;
  886. return 0;
  887. }
  888. if (machine == NULL) {
  889. ++session->hists.stats.nr_unprocessable_samples;
  890. return 0;
  891. }
  892. return tool->sample(tool, event, sample, evsel, machine);
  893. case PERF_RECORD_MMAP:
  894. return tool->mmap(tool, event, sample, machine);
  895. case PERF_RECORD_COMM:
  896. return tool->comm(tool, event, sample, machine);
  897. case PERF_RECORD_FORK:
  898. return tool->fork(tool, event, sample, machine);
  899. case PERF_RECORD_EXIT:
  900. return tool->exit(tool, event, sample, machine);
  901. case PERF_RECORD_LOST:
  902. if (tool->lost == perf_event__process_lost)
  903. session->hists.stats.total_lost += event->lost.lost;
  904. return tool->lost(tool, event, sample, machine);
  905. case PERF_RECORD_READ:
  906. return tool->read(tool, event, sample, evsel, machine);
  907. case PERF_RECORD_THROTTLE:
  908. return tool->throttle(tool, event, sample, machine);
  909. case PERF_RECORD_UNTHROTTLE:
  910. return tool->unthrottle(tool, event, sample, machine);
  911. default:
  912. ++session->hists.stats.nr_unknown_events;
  913. return -1;
  914. }
  915. }
  916. static int perf_session__preprocess_sample(struct perf_session *session,
  917. union perf_event *event, struct perf_sample *sample)
  918. {
  919. if (event->header.type != PERF_RECORD_SAMPLE ||
  920. !(perf_evlist__sample_type(session->evlist) & PERF_SAMPLE_CALLCHAIN))
  921. return 0;
  922. if (!ip_callchain__valid(sample->callchain, event)) {
  923. pr_debug("call-chain problem with event, skipping it.\n");
  924. ++session->hists.stats.nr_invalid_chains;
  925. session->hists.stats.total_invalid_chains += sample->period;
  926. return -EINVAL;
  927. }
  928. return 0;
  929. }
  930. static int perf_session__process_user_event(struct perf_session *session, union perf_event *event,
  931. struct perf_tool *tool, u64 file_offset)
  932. {
  933. int err;
  934. dump_event(session, event, file_offset, NULL);
  935. /* These events are processed right away */
  936. switch (event->header.type) {
  937. case PERF_RECORD_HEADER_ATTR:
  938. err = tool->attr(event, &session->evlist);
  939. if (err == 0)
  940. perf_session__set_id_hdr_size(session);
  941. return err;
  942. case PERF_RECORD_HEADER_EVENT_TYPE:
  943. return tool->event_type(tool, event);
  944. case PERF_RECORD_HEADER_TRACING_DATA:
  945. /* setup for reading amidst mmap */
  946. lseek(session->fd, file_offset, SEEK_SET);
  947. return tool->tracing_data(event, session);
  948. case PERF_RECORD_HEADER_BUILD_ID:
  949. return tool->build_id(tool, event, session);
  950. case PERF_RECORD_FINISHED_ROUND:
  951. return tool->finished_round(tool, event, session);
  952. default:
  953. return -EINVAL;
  954. }
  955. }
  956. static void event_swap(union perf_event *event, bool sample_id_all)
  957. {
  958. perf_event__swap_op swap;
  959. swap = perf_event__swap_ops[event->header.type];
  960. if (swap)
  961. swap(event, sample_id_all);
  962. }
  963. static int perf_session__process_event(struct perf_session *session,
  964. union perf_event *event,
  965. struct perf_tool *tool,
  966. u64 file_offset)
  967. {
  968. struct perf_sample sample;
  969. int ret;
  970. if (session->header.needs_swap)
  971. event_swap(event, perf_evlist__sample_id_all(session->evlist));
  972. if (event->header.type >= PERF_RECORD_HEADER_MAX)
  973. return -EINVAL;
  974. hists__inc_nr_events(&session->hists, event->header.type);
  975. if (event->header.type >= PERF_RECORD_USER_TYPE_START)
  976. return perf_session__process_user_event(session, event, tool, file_offset);
  977. /*
  978. * For all kernel events we get the sample data
  979. */
  980. ret = perf_evlist__parse_sample(session->evlist, event, &sample,
  981. session->header.needs_swap);
  982. if (ret)
  983. return ret;
  984. /* Preprocess sample records - precheck callchains */
  985. if (perf_session__preprocess_sample(session, event, &sample))
  986. return 0;
  987. if (tool->ordered_samples) {
  988. ret = perf_session_queue_event(session, event, &sample,
  989. file_offset);
  990. if (ret != -ETIME)
  991. return ret;
  992. }
  993. return perf_session_deliver_event(session, event, &sample, tool,
  994. file_offset);
  995. }
  996. void perf_event_header__bswap(struct perf_event_header *self)
  997. {
  998. self->type = bswap_32(self->type);
  999. self->misc = bswap_16(self->misc);
  1000. self->size = bswap_16(self->size);
  1001. }
  1002. struct thread *perf_session__findnew(struct perf_session *session, pid_t pid)
  1003. {
  1004. return machine__findnew_thread(&session->host_machine, pid);
  1005. }
  1006. static struct thread *perf_session__register_idle_thread(struct perf_session *self)
  1007. {
  1008. struct thread *thread = perf_session__findnew(self, 0);
  1009. if (thread == NULL || thread__set_comm(thread, "swapper")) {
  1010. pr_err("problem inserting idle task.\n");
  1011. thread = NULL;
  1012. }
  1013. return thread;
  1014. }
  1015. static void perf_session__warn_about_errors(const struct perf_session *session,
  1016. const struct perf_tool *tool)
  1017. {
  1018. if (tool->lost == perf_event__process_lost &&
  1019. session->hists.stats.nr_events[PERF_RECORD_LOST] != 0) {
  1020. ui__warning("Processed %d events and lost %d chunks!\n\n"
  1021. "Check IO/CPU overload!\n\n",
  1022. session->hists.stats.nr_events[0],
  1023. session->hists.stats.nr_events[PERF_RECORD_LOST]);
  1024. }
  1025. if (session->hists.stats.nr_unknown_events != 0) {
  1026. ui__warning("Found %u unknown events!\n\n"
  1027. "Is this an older tool processing a perf.data "
  1028. "file generated by a more recent tool?\n\n"
  1029. "If that is not the case, consider "
  1030. "reporting to linux-kernel@vger.kernel.org.\n\n",
  1031. session->hists.stats.nr_unknown_events);
  1032. }
  1033. if (session->hists.stats.nr_unknown_id != 0) {
  1034. ui__warning("%u samples with id not present in the header\n",
  1035. session->hists.stats.nr_unknown_id);
  1036. }
  1037. if (session->hists.stats.nr_invalid_chains != 0) {
  1038. ui__warning("Found invalid callchains!\n\n"
  1039. "%u out of %u events were discarded for this reason.\n\n"
  1040. "Consider reporting to linux-kernel@vger.kernel.org.\n\n",
  1041. session->hists.stats.nr_invalid_chains,
  1042. session->hists.stats.nr_events[PERF_RECORD_SAMPLE]);
  1043. }
  1044. if (session->hists.stats.nr_unprocessable_samples != 0) {
  1045. ui__warning("%u unprocessable samples recorded.\n"
  1046. "Do you have a KVM guest running and not using 'perf kvm'?\n",
  1047. session->hists.stats.nr_unprocessable_samples);
  1048. }
  1049. }
  1050. #define session_done() (*(volatile int *)(&session_done))
  1051. volatile int session_done;
  1052. static int __perf_session__process_pipe_events(struct perf_session *self,
  1053. struct perf_tool *tool)
  1054. {
  1055. union perf_event *event;
  1056. uint32_t size, cur_size = 0;
  1057. void *buf = NULL;
  1058. int skip = 0;
  1059. u64 head;
  1060. int err;
  1061. void *p;
  1062. perf_tool__fill_defaults(tool);
  1063. head = 0;
  1064. cur_size = sizeof(union perf_event);
  1065. buf = malloc(cur_size);
  1066. if (!buf)
  1067. return -errno;
  1068. more:
  1069. event = buf;
  1070. err = readn(self->fd, event, sizeof(struct perf_event_header));
  1071. if (err <= 0) {
  1072. if (err == 0)
  1073. goto done;
  1074. pr_err("failed to read event header\n");
  1075. goto out_err;
  1076. }
  1077. if (self->header.needs_swap)
  1078. perf_event_header__bswap(&event->header);
  1079. size = event->header.size;
  1080. if (size == 0)
  1081. size = 8;
  1082. if (size > cur_size) {
  1083. void *new = realloc(buf, size);
  1084. if (!new) {
  1085. pr_err("failed to allocate memory to read event\n");
  1086. goto out_err;
  1087. }
  1088. buf = new;
  1089. cur_size = size;
  1090. event = buf;
  1091. }
  1092. p = event;
  1093. p += sizeof(struct perf_event_header);
  1094. if (size - sizeof(struct perf_event_header)) {
  1095. err = readn(self->fd, p, size - sizeof(struct perf_event_header));
  1096. if (err <= 0) {
  1097. if (err == 0) {
  1098. pr_err("unexpected end of event stream\n");
  1099. goto done;
  1100. }
  1101. pr_err("failed to read event data\n");
  1102. goto out_err;
  1103. }
  1104. }
  1105. if ((skip = perf_session__process_event(self, event, tool, head)) < 0) {
  1106. pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
  1107. head, event->header.size, event->header.type);
  1108. err = -EINVAL;
  1109. goto out_err;
  1110. }
  1111. head += size;
  1112. if (skip > 0)
  1113. head += skip;
  1114. if (!session_done())
  1115. goto more;
  1116. done:
  1117. err = 0;
  1118. out_err:
  1119. free(buf);
  1120. perf_session__warn_about_errors(self, tool);
  1121. perf_session_free_sample_buffers(self);
  1122. return err;
  1123. }
  1124. static union perf_event *
  1125. fetch_mmaped_event(struct perf_session *session,
  1126. u64 head, size_t mmap_size, char *buf)
  1127. {
  1128. union perf_event *event;
  1129. /*
  1130. * Ensure we have enough space remaining to read
  1131. * the size of the event in the headers.
  1132. */
  1133. if (head + sizeof(event->header) > mmap_size)
  1134. return NULL;
  1135. event = (union perf_event *)(buf + head);
  1136. if (session->header.needs_swap)
  1137. perf_event_header__bswap(&event->header);
  1138. if (head + event->header.size > mmap_size)
  1139. return NULL;
  1140. return event;
  1141. }
  1142. int __perf_session__process_events(struct perf_session *session,
  1143. u64 data_offset, u64 data_size,
  1144. u64 file_size, struct perf_tool *tool)
  1145. {
  1146. u64 head, page_offset, file_offset, file_pos, progress_next;
  1147. int err, mmap_prot, mmap_flags, map_idx = 0;
  1148. size_t page_size, mmap_size;
  1149. char *buf, *mmaps[8];
  1150. union perf_event *event;
  1151. uint32_t size;
  1152. perf_tool__fill_defaults(tool);
  1153. page_size = sysconf(_SC_PAGESIZE);
  1154. page_offset = page_size * (data_offset / page_size);
  1155. file_offset = page_offset;
  1156. head = data_offset - page_offset;
  1157. if (data_offset + data_size < file_size)
  1158. file_size = data_offset + data_size;
  1159. progress_next = file_size / 16;
  1160. mmap_size = session->mmap_window;
  1161. if (mmap_size > file_size)
  1162. mmap_size = file_size;
  1163. memset(mmaps, 0, sizeof(mmaps));
  1164. mmap_prot = PROT_READ;
  1165. mmap_flags = MAP_SHARED;
  1166. if (session->header.needs_swap) {
  1167. mmap_prot |= PROT_WRITE;
  1168. mmap_flags = MAP_PRIVATE;
  1169. }
  1170. remap:
  1171. buf = mmap(NULL, mmap_size, mmap_prot, mmap_flags, session->fd,
  1172. file_offset);
  1173. if (buf == MAP_FAILED) {
  1174. pr_err("failed to mmap file\n");
  1175. err = -errno;
  1176. goto out_err;
  1177. }
  1178. mmaps[map_idx] = buf;
  1179. map_idx = (map_idx + 1) & (ARRAY_SIZE(mmaps) - 1);
  1180. file_pos = file_offset + head;
  1181. more:
  1182. event = fetch_mmaped_event(session, head, mmap_size, buf);
  1183. if (!event) {
  1184. if (mmaps[map_idx]) {
  1185. munmap(mmaps[map_idx], mmap_size);
  1186. mmaps[map_idx] = NULL;
  1187. }
  1188. page_offset = page_size * (head / page_size);
  1189. file_offset += page_offset;
  1190. head -= page_offset;
  1191. goto remap;
  1192. }
  1193. size = event->header.size;
  1194. if (size == 0 ||
  1195. perf_session__process_event(session, event, tool, file_pos) < 0) {
  1196. pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
  1197. file_offset + head, event->header.size,
  1198. event->header.type);
  1199. err = -EINVAL;
  1200. goto out_err;
  1201. }
  1202. head += size;
  1203. file_pos += size;
  1204. if (file_pos >= progress_next) {
  1205. progress_next += file_size / 16;
  1206. ui_progress__update(file_pos, file_size,
  1207. "Processing events...");
  1208. }
  1209. if (file_pos < file_size)
  1210. goto more;
  1211. err = 0;
  1212. /* do the final flush for ordered samples */
  1213. session->ordered_samples.next_flush = ULLONG_MAX;
  1214. flush_sample_queue(session, tool);
  1215. out_err:
  1216. perf_session__warn_about_errors(session, tool);
  1217. perf_session_free_sample_buffers(session);
  1218. return err;
  1219. }
  1220. int perf_session__process_events(struct perf_session *self,
  1221. struct perf_tool *tool)
  1222. {
  1223. int err;
  1224. if (perf_session__register_idle_thread(self) == NULL)
  1225. return -ENOMEM;
  1226. if (!self->fd_pipe)
  1227. err = __perf_session__process_events(self,
  1228. self->header.data_offset,
  1229. self->header.data_size,
  1230. self->size, tool);
  1231. else
  1232. err = __perf_session__process_pipe_events(self, tool);
  1233. return err;
  1234. }
  1235. bool perf_session__has_traces(struct perf_session *session, const char *msg)
  1236. {
  1237. if (!(perf_evlist__sample_type(session->evlist) & PERF_SAMPLE_RAW)) {
  1238. pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg);
  1239. return false;
  1240. }
  1241. return true;
  1242. }
  1243. int maps__set_kallsyms_ref_reloc_sym(struct map **maps,
  1244. const char *symbol_name, u64 addr)
  1245. {
  1246. char *bracket;
  1247. enum map_type i;
  1248. struct ref_reloc_sym *ref;
  1249. ref = zalloc(sizeof(struct ref_reloc_sym));
  1250. if (ref == NULL)
  1251. return -ENOMEM;
  1252. ref->name = strdup(symbol_name);
  1253. if (ref->name == NULL) {
  1254. free(ref);
  1255. return -ENOMEM;
  1256. }
  1257. bracket = strchr(ref->name, ']');
  1258. if (bracket)
  1259. *bracket = '\0';
  1260. ref->addr = addr;
  1261. for (i = 0; i < MAP__NR_TYPES; ++i) {
  1262. struct kmap *kmap = map__kmap(maps[i]);
  1263. kmap->ref_reloc_sym = ref;
  1264. }
  1265. return 0;
  1266. }
  1267. size_t perf_session__fprintf_dsos(struct perf_session *self, FILE *fp)
  1268. {
  1269. return __dsos__fprintf(&self->host_machine.kernel_dsos, fp) +
  1270. __dsos__fprintf(&self->host_machine.user_dsos, fp) +
  1271. machines__fprintf_dsos(&self->machines, fp);
  1272. }
  1273. size_t perf_session__fprintf_dsos_buildid(struct perf_session *self, FILE *fp,
  1274. bool with_hits)
  1275. {
  1276. size_t ret = machine__fprintf_dsos_buildid(&self->host_machine, fp, with_hits);
  1277. return ret + machines__fprintf_dsos_buildid(&self->machines, fp, with_hits);
  1278. }
  1279. size_t perf_session__fprintf_nr_events(struct perf_session *session, FILE *fp)
  1280. {
  1281. struct perf_evsel *pos;
  1282. size_t ret = fprintf(fp, "Aggregated stats:\n");
  1283. ret += hists__fprintf_nr_events(&session->hists, fp);
  1284. list_for_each_entry(pos, &session->evlist->entries, node) {
  1285. ret += fprintf(fp, "%s stats:\n", perf_evsel__name(pos));
  1286. ret += hists__fprintf_nr_events(&pos->hists, fp);
  1287. }
  1288. return ret;
  1289. }
  1290. size_t perf_session__fprintf(struct perf_session *session, FILE *fp)
  1291. {
  1292. /*
  1293. * FIXME: Here we have to actually print all the machines in this
  1294. * session, not just the host...
  1295. */
  1296. return machine__fprintf(&session->host_machine, fp);
  1297. }
  1298. void perf_session__remove_thread(struct perf_session *session,
  1299. struct thread *th)
  1300. {
  1301. /*
  1302. * FIXME: This one makes no sense, we need to remove the thread from
  1303. * the machine it belongs to, perf_session can have many machines, so
  1304. * doing it always on ->host_machine is wrong. Fix when auditing all
  1305. * the 'perf kvm' code.
  1306. */
  1307. machine__remove_thread(&session->host_machine, th);
  1308. }
  1309. struct perf_evsel *perf_session__find_first_evtype(struct perf_session *session,
  1310. unsigned int type)
  1311. {
  1312. struct perf_evsel *pos;
  1313. list_for_each_entry(pos, &session->evlist->entries, node) {
  1314. if (pos->attr.type == type)
  1315. return pos;
  1316. }
  1317. return NULL;
  1318. }
  1319. void perf_evsel__print_ip(struct perf_evsel *evsel, union perf_event *event,
  1320. struct perf_sample *sample, struct machine *machine,
  1321. int print_sym, int print_dso, int print_symoffset)
  1322. {
  1323. struct addr_location al;
  1324. struct callchain_cursor_node *node;
  1325. if (perf_event__preprocess_sample(event, machine, &al, sample,
  1326. NULL) < 0) {
  1327. error("problem processing %d event, skipping it.\n",
  1328. event->header.type);
  1329. return;
  1330. }
  1331. if (symbol_conf.use_callchain && sample->callchain) {
  1332. if (machine__resolve_callchain(machine, evsel, al.thread,
  1333. sample, NULL) != 0) {
  1334. if (verbose)
  1335. error("Failed to resolve callchain. Skipping\n");
  1336. return;
  1337. }
  1338. callchain_cursor_commit(&callchain_cursor);
  1339. while (1) {
  1340. node = callchain_cursor_current(&callchain_cursor);
  1341. if (!node)
  1342. break;
  1343. printf("\t%16" PRIx64, node->ip);
  1344. if (print_sym) {
  1345. printf(" ");
  1346. symbol__fprintf_symname(node->sym, stdout);
  1347. }
  1348. if (print_dso) {
  1349. printf(" (");
  1350. map__fprintf_dsoname(node->map, stdout);
  1351. printf(")");
  1352. }
  1353. printf("\n");
  1354. callchain_cursor_advance(&callchain_cursor);
  1355. }
  1356. } else {
  1357. printf("%16" PRIx64, sample->ip);
  1358. if (print_sym) {
  1359. printf(" ");
  1360. if (print_symoffset)
  1361. symbol__fprintf_symname_offs(al.sym, &al,
  1362. stdout);
  1363. else
  1364. symbol__fprintf_symname(al.sym, stdout);
  1365. }
  1366. if (print_dso) {
  1367. printf(" (");
  1368. map__fprintf_dsoname(al.map, stdout);
  1369. printf(")");
  1370. }
  1371. }
  1372. }
  1373. int perf_session__cpu_bitmap(struct perf_session *session,
  1374. const char *cpu_list, unsigned long *cpu_bitmap)
  1375. {
  1376. int i;
  1377. struct cpu_map *map;
  1378. for (i = 0; i < PERF_TYPE_MAX; ++i) {
  1379. struct perf_evsel *evsel;
  1380. evsel = perf_session__find_first_evtype(session, i);
  1381. if (!evsel)
  1382. continue;
  1383. if (!(evsel->attr.sample_type & PERF_SAMPLE_CPU)) {
  1384. pr_err("File does not contain CPU events. "
  1385. "Remove -c option to proceed.\n");
  1386. return -1;
  1387. }
  1388. }
  1389. map = cpu_map__new(cpu_list);
  1390. if (map == NULL) {
  1391. pr_err("Invalid cpu_list\n");
  1392. return -1;
  1393. }
  1394. for (i = 0; i < map->nr; i++) {
  1395. int cpu = map->map[i];
  1396. if (cpu >= MAX_NR_CPUS) {
  1397. pr_err("Requested CPU %d too large. "
  1398. "Consider raising MAX_NR_CPUS\n", cpu);
  1399. return -1;
  1400. }
  1401. set_bit(cpu, cpu_bitmap);
  1402. }
  1403. return 0;
  1404. }
  1405. void perf_session__fprintf_info(struct perf_session *session, FILE *fp,
  1406. bool full)
  1407. {
  1408. struct stat st;
  1409. int ret;
  1410. if (session == NULL || fp == NULL)
  1411. return;
  1412. ret = fstat(session->fd, &st);
  1413. if (ret == -1)
  1414. return;
  1415. fprintf(fp, "# ========\n");
  1416. fprintf(fp, "# captured on: %s", ctime(&st.st_ctime));
  1417. perf_header__fprintf_info(session, fp, full);
  1418. fprintf(fp, "# ========\n#\n");
  1419. }
  1420. int __perf_session__set_tracepoints_handlers(struct perf_session *session,
  1421. const struct perf_evsel_str_handler *assocs,
  1422. size_t nr_assocs)
  1423. {
  1424. struct perf_evlist *evlist = session->evlist;
  1425. struct event_format *format;
  1426. struct perf_evsel *evsel;
  1427. char *tracepoint, *name;
  1428. size_t i;
  1429. int err;
  1430. for (i = 0; i < nr_assocs; i++) {
  1431. err = -ENOMEM;
  1432. tracepoint = strdup(assocs[i].name);
  1433. if (tracepoint == NULL)
  1434. goto out;
  1435. err = -ENOENT;
  1436. name = strchr(tracepoint, ':');
  1437. if (name == NULL)
  1438. goto out_free;
  1439. *name++ = '\0';
  1440. format = pevent_find_event_by_name(session->pevent,
  1441. tracepoint, name);
  1442. if (format == NULL) {
  1443. /*
  1444. * Adding a handler for an event not in the session,
  1445. * just ignore it.
  1446. */
  1447. goto next;
  1448. }
  1449. evsel = perf_evlist__find_tracepoint_by_id(evlist, format->id);
  1450. if (evsel == NULL)
  1451. goto next;
  1452. err = -EEXIST;
  1453. if (evsel->handler.func != NULL)
  1454. goto out_free;
  1455. evsel->handler.func = assocs[i].handler;
  1456. next:
  1457. free(tracepoint);
  1458. }
  1459. err = 0;
  1460. out:
  1461. return err;
  1462. out_free:
  1463. free(tracepoint);
  1464. goto out;
  1465. }