session.c 43 KB

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