session.c 42 KB

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