session.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670
  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 (perf_guest &&
  784. ((cpumode == PERF_RECORD_MISC_GUEST_KERNEL) ||
  785. (cpumode == PERF_RECORD_MISC_GUEST_USER))) {
  786. u32 pid;
  787. if (event->header.type == PERF_RECORD_MMAP)
  788. pid = event->mmap.pid;
  789. else
  790. pid = event->ip.pid;
  791. return perf_session__findnew_machine(session, pid);
  792. }
  793. return perf_session__find_host_machine(session);
  794. }
  795. static int perf_session_deliver_event(struct perf_session *session,
  796. union perf_event *event,
  797. struct perf_sample *sample,
  798. struct perf_tool *tool,
  799. u64 file_offset)
  800. {
  801. struct perf_evsel *evsel;
  802. struct machine *machine;
  803. dump_event(session, event, file_offset, sample);
  804. evsel = perf_evlist__id2evsel(session->evlist, sample->id);
  805. if (evsel != NULL && event->header.type != PERF_RECORD_SAMPLE) {
  806. /*
  807. * XXX We're leaving PERF_RECORD_SAMPLE unnacounted here
  808. * because the tools right now may apply filters, discarding
  809. * some of the samples. For consistency, in the future we
  810. * should have something like nr_filtered_samples and remove
  811. * the sample->period from total_sample_period, etc, KISS for
  812. * now tho.
  813. *
  814. * Also testing against NULL allows us to handle files without
  815. * attr.sample_id_all and/or without PERF_SAMPLE_ID. In the
  816. * future probably it'll be a good idea to restrict event
  817. * processing via perf_session to files with both set.
  818. */
  819. hists__inc_nr_events(&evsel->hists, event->header.type);
  820. }
  821. machine = perf_session__find_machine_for_cpumode(session, event);
  822. switch (event->header.type) {
  823. case PERF_RECORD_SAMPLE:
  824. dump_sample(session, event, sample);
  825. if (evsel == NULL) {
  826. ++session->hists.stats.nr_unknown_id;
  827. return 0;
  828. }
  829. if (machine == NULL) {
  830. ++session->hists.stats.nr_unprocessable_samples;
  831. return 0;
  832. }
  833. return tool->sample(tool, event, sample, evsel, machine);
  834. case PERF_RECORD_MMAP:
  835. return tool->mmap(tool, event, sample, machine);
  836. case PERF_RECORD_COMM:
  837. return tool->comm(tool, event, sample, machine);
  838. case PERF_RECORD_FORK:
  839. return tool->fork(tool, event, sample, machine);
  840. case PERF_RECORD_EXIT:
  841. return tool->exit(tool, event, sample, machine);
  842. case PERF_RECORD_LOST:
  843. if (tool->lost == perf_event__process_lost)
  844. session->hists.stats.total_lost += event->lost.lost;
  845. return tool->lost(tool, event, sample, machine);
  846. case PERF_RECORD_READ:
  847. return tool->read(tool, event, sample, evsel, machine);
  848. case PERF_RECORD_THROTTLE:
  849. return tool->throttle(tool, event, sample, machine);
  850. case PERF_RECORD_UNTHROTTLE:
  851. return tool->unthrottle(tool, event, sample, machine);
  852. default:
  853. ++session->hists.stats.nr_unknown_events;
  854. return -1;
  855. }
  856. }
  857. static int perf_session__preprocess_sample(struct perf_session *session,
  858. union perf_event *event, struct perf_sample *sample)
  859. {
  860. if (event->header.type != PERF_RECORD_SAMPLE ||
  861. !(session->sample_type & PERF_SAMPLE_CALLCHAIN))
  862. return 0;
  863. if (!ip_callchain__valid(sample->callchain, event)) {
  864. pr_debug("call-chain problem with event, skipping it.\n");
  865. ++session->hists.stats.nr_invalid_chains;
  866. session->hists.stats.total_invalid_chains += sample->period;
  867. return -EINVAL;
  868. }
  869. return 0;
  870. }
  871. static int perf_session__process_user_event(struct perf_session *session, union perf_event *event,
  872. struct perf_tool *tool, u64 file_offset)
  873. {
  874. int err;
  875. dump_event(session, event, file_offset, NULL);
  876. /* These events are processed right away */
  877. switch (event->header.type) {
  878. case PERF_RECORD_HEADER_ATTR:
  879. err = tool->attr(event, &session->evlist);
  880. if (err == 0)
  881. perf_session__update_sample_type(session);
  882. return err;
  883. case PERF_RECORD_HEADER_EVENT_TYPE:
  884. return tool->event_type(tool, event);
  885. case PERF_RECORD_HEADER_TRACING_DATA:
  886. /* setup for reading amidst mmap */
  887. lseek(session->fd, file_offset, SEEK_SET);
  888. return tool->tracing_data(event, session);
  889. case PERF_RECORD_HEADER_BUILD_ID:
  890. return tool->build_id(tool, event, session);
  891. case PERF_RECORD_FINISHED_ROUND:
  892. return tool->finished_round(tool, event, session);
  893. default:
  894. return -EINVAL;
  895. }
  896. }
  897. static void event_swap(union perf_event *event, bool sample_id_all)
  898. {
  899. perf_event__swap_op swap;
  900. swap = perf_event__swap_ops[event->header.type];
  901. if (swap)
  902. swap(event, sample_id_all);
  903. }
  904. static int perf_session__process_event(struct perf_session *session,
  905. union perf_event *event,
  906. struct perf_tool *tool,
  907. u64 file_offset)
  908. {
  909. struct perf_sample sample;
  910. int ret;
  911. if (session->header.needs_swap)
  912. event_swap(event, session->sample_id_all);
  913. if (event->header.type >= PERF_RECORD_HEADER_MAX)
  914. return -EINVAL;
  915. hists__inc_nr_events(&session->hists, event->header.type);
  916. if (event->header.type >= PERF_RECORD_USER_TYPE_START)
  917. return perf_session__process_user_event(session, event, tool, file_offset);
  918. /*
  919. * For all kernel events we get the sample data
  920. */
  921. ret = perf_session__parse_sample(session, event, &sample);
  922. if (ret)
  923. return ret;
  924. /* Preprocess sample records - precheck callchains */
  925. if (perf_session__preprocess_sample(session, event, &sample))
  926. return 0;
  927. if (tool->ordered_samples) {
  928. ret = perf_session_queue_event(session, event, &sample,
  929. file_offset);
  930. if (ret != -ETIME)
  931. return ret;
  932. }
  933. return perf_session_deliver_event(session, event, &sample, tool,
  934. file_offset);
  935. }
  936. void perf_event_header__bswap(struct perf_event_header *self)
  937. {
  938. self->type = bswap_32(self->type);
  939. self->misc = bswap_16(self->misc);
  940. self->size = bswap_16(self->size);
  941. }
  942. struct thread *perf_session__findnew(struct perf_session *session, pid_t pid)
  943. {
  944. return machine__findnew_thread(&session->host_machine, pid);
  945. }
  946. static struct thread *perf_session__register_idle_thread(struct perf_session *self)
  947. {
  948. struct thread *thread = perf_session__findnew(self, 0);
  949. if (thread == NULL || thread__set_comm(thread, "swapper")) {
  950. pr_err("problem inserting idle task.\n");
  951. thread = NULL;
  952. }
  953. return thread;
  954. }
  955. static void perf_session__warn_about_errors(const struct perf_session *session,
  956. const struct perf_tool *tool)
  957. {
  958. if (tool->lost == perf_event__process_lost &&
  959. session->hists.stats.nr_events[PERF_RECORD_LOST] != 0) {
  960. ui__warning("Processed %d events and lost %d chunks!\n\n"
  961. "Check IO/CPU overload!\n\n",
  962. session->hists.stats.nr_events[0],
  963. session->hists.stats.nr_events[PERF_RECORD_LOST]);
  964. }
  965. if (session->hists.stats.nr_unknown_events != 0) {
  966. ui__warning("Found %u unknown events!\n\n"
  967. "Is this an older tool processing a perf.data "
  968. "file generated by a more recent tool?\n\n"
  969. "If that is not the case, consider "
  970. "reporting to linux-kernel@vger.kernel.org.\n\n",
  971. session->hists.stats.nr_unknown_events);
  972. }
  973. if (session->hists.stats.nr_unknown_id != 0) {
  974. ui__warning("%u samples with id not present in the header\n",
  975. session->hists.stats.nr_unknown_id);
  976. }
  977. if (session->hists.stats.nr_invalid_chains != 0) {
  978. ui__warning("Found invalid callchains!\n\n"
  979. "%u out of %u events were discarded for this reason.\n\n"
  980. "Consider reporting to linux-kernel@vger.kernel.org.\n\n",
  981. session->hists.stats.nr_invalid_chains,
  982. session->hists.stats.nr_events[PERF_RECORD_SAMPLE]);
  983. }
  984. if (session->hists.stats.nr_unprocessable_samples != 0) {
  985. ui__warning("%u unprocessable samples recorded.\n"
  986. "Do you have a KVM guest running and not using 'perf kvm'?\n",
  987. session->hists.stats.nr_unprocessable_samples);
  988. }
  989. }
  990. #define session_done() (*(volatile int *)(&session_done))
  991. volatile int session_done;
  992. static int __perf_session__process_pipe_events(struct perf_session *self,
  993. struct perf_tool *tool)
  994. {
  995. union perf_event *event;
  996. uint32_t size, cur_size = 0;
  997. void *buf = NULL;
  998. int skip = 0;
  999. u64 head;
  1000. int err;
  1001. void *p;
  1002. perf_tool__fill_defaults(tool);
  1003. head = 0;
  1004. cur_size = sizeof(union perf_event);
  1005. buf = malloc(cur_size);
  1006. if (!buf)
  1007. return -errno;
  1008. more:
  1009. event = buf;
  1010. err = readn(self->fd, event, sizeof(struct perf_event_header));
  1011. if (err <= 0) {
  1012. if (err == 0)
  1013. goto done;
  1014. pr_err("failed to read event header\n");
  1015. goto out_err;
  1016. }
  1017. if (self->header.needs_swap)
  1018. perf_event_header__bswap(&event->header);
  1019. size = event->header.size;
  1020. if (size == 0)
  1021. size = 8;
  1022. if (size > cur_size) {
  1023. void *new = realloc(buf, size);
  1024. if (!new) {
  1025. pr_err("failed to allocate memory to read event\n");
  1026. goto out_err;
  1027. }
  1028. buf = new;
  1029. cur_size = size;
  1030. event = buf;
  1031. }
  1032. p = event;
  1033. p += sizeof(struct perf_event_header);
  1034. if (size - sizeof(struct perf_event_header)) {
  1035. err = readn(self->fd, p, size - sizeof(struct perf_event_header));
  1036. if (err <= 0) {
  1037. if (err == 0) {
  1038. pr_err("unexpected end of event stream\n");
  1039. goto done;
  1040. }
  1041. pr_err("failed to read event data\n");
  1042. goto out_err;
  1043. }
  1044. }
  1045. if ((skip = perf_session__process_event(self, event, tool, head)) < 0) {
  1046. pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
  1047. head, event->header.size, event->header.type);
  1048. err = -EINVAL;
  1049. goto out_err;
  1050. }
  1051. head += size;
  1052. if (skip > 0)
  1053. head += skip;
  1054. if (!session_done())
  1055. goto more;
  1056. done:
  1057. err = 0;
  1058. out_err:
  1059. free(buf);
  1060. perf_session__warn_about_errors(self, tool);
  1061. perf_session_free_sample_buffers(self);
  1062. return err;
  1063. }
  1064. static union perf_event *
  1065. fetch_mmaped_event(struct perf_session *session,
  1066. u64 head, size_t mmap_size, char *buf)
  1067. {
  1068. union perf_event *event;
  1069. /*
  1070. * Ensure we have enough space remaining to read
  1071. * the size of the event in the headers.
  1072. */
  1073. if (head + sizeof(event->header) > mmap_size)
  1074. return NULL;
  1075. event = (union perf_event *)(buf + head);
  1076. if (session->header.needs_swap)
  1077. perf_event_header__bswap(&event->header);
  1078. if (head + event->header.size > mmap_size)
  1079. return NULL;
  1080. return event;
  1081. }
  1082. int __perf_session__process_events(struct perf_session *session,
  1083. u64 data_offset, u64 data_size,
  1084. u64 file_size, struct perf_tool *tool)
  1085. {
  1086. u64 head, page_offset, file_offset, file_pos, progress_next;
  1087. int err, mmap_prot, mmap_flags, map_idx = 0;
  1088. size_t page_size, mmap_size;
  1089. char *buf, *mmaps[8];
  1090. union perf_event *event;
  1091. uint32_t size;
  1092. perf_tool__fill_defaults(tool);
  1093. page_size = sysconf(_SC_PAGESIZE);
  1094. page_offset = page_size * (data_offset / page_size);
  1095. file_offset = page_offset;
  1096. head = data_offset - page_offset;
  1097. if (data_offset + data_size < file_size)
  1098. file_size = data_offset + data_size;
  1099. progress_next = file_size / 16;
  1100. mmap_size = session->mmap_window;
  1101. if (mmap_size > file_size)
  1102. mmap_size = file_size;
  1103. memset(mmaps, 0, sizeof(mmaps));
  1104. mmap_prot = PROT_READ;
  1105. mmap_flags = MAP_SHARED;
  1106. if (session->header.needs_swap) {
  1107. mmap_prot |= PROT_WRITE;
  1108. mmap_flags = MAP_PRIVATE;
  1109. }
  1110. remap:
  1111. buf = mmap(NULL, mmap_size, mmap_prot, mmap_flags, session->fd,
  1112. file_offset);
  1113. if (buf == MAP_FAILED) {
  1114. pr_err("failed to mmap file\n");
  1115. err = -errno;
  1116. goto out_err;
  1117. }
  1118. mmaps[map_idx] = buf;
  1119. map_idx = (map_idx + 1) & (ARRAY_SIZE(mmaps) - 1);
  1120. file_pos = file_offset + head;
  1121. more:
  1122. event = fetch_mmaped_event(session, head, mmap_size, buf);
  1123. if (!event) {
  1124. if (mmaps[map_idx]) {
  1125. munmap(mmaps[map_idx], mmap_size);
  1126. mmaps[map_idx] = NULL;
  1127. }
  1128. page_offset = page_size * (head / page_size);
  1129. file_offset += page_offset;
  1130. head -= page_offset;
  1131. goto remap;
  1132. }
  1133. size = event->header.size;
  1134. if (size == 0 ||
  1135. perf_session__process_event(session, event, tool, file_pos) < 0) {
  1136. pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
  1137. file_offset + head, event->header.size,
  1138. event->header.type);
  1139. err = -EINVAL;
  1140. goto out_err;
  1141. }
  1142. head += size;
  1143. file_pos += size;
  1144. if (file_pos >= progress_next) {
  1145. progress_next += file_size / 16;
  1146. ui_progress__update(file_pos, file_size,
  1147. "Processing events...");
  1148. }
  1149. if (file_pos < file_size)
  1150. goto more;
  1151. err = 0;
  1152. /* do the final flush for ordered samples */
  1153. session->ordered_samples.next_flush = ULLONG_MAX;
  1154. flush_sample_queue(session, tool);
  1155. out_err:
  1156. perf_session__warn_about_errors(session, tool);
  1157. perf_session_free_sample_buffers(session);
  1158. return err;
  1159. }
  1160. int perf_session__process_events(struct perf_session *self,
  1161. struct perf_tool *tool)
  1162. {
  1163. int err;
  1164. if (perf_session__register_idle_thread(self) == NULL)
  1165. return -ENOMEM;
  1166. if (!self->fd_pipe)
  1167. err = __perf_session__process_events(self,
  1168. self->header.data_offset,
  1169. self->header.data_size,
  1170. self->size, tool);
  1171. else
  1172. err = __perf_session__process_pipe_events(self, tool);
  1173. return err;
  1174. }
  1175. bool perf_session__has_traces(struct perf_session *self, const char *msg)
  1176. {
  1177. if (!(self->sample_type & PERF_SAMPLE_RAW)) {
  1178. pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg);
  1179. return false;
  1180. }
  1181. return true;
  1182. }
  1183. int maps__set_kallsyms_ref_reloc_sym(struct map **maps,
  1184. const char *symbol_name, u64 addr)
  1185. {
  1186. char *bracket;
  1187. enum map_type i;
  1188. struct ref_reloc_sym *ref;
  1189. ref = zalloc(sizeof(struct ref_reloc_sym));
  1190. if (ref == NULL)
  1191. return -ENOMEM;
  1192. ref->name = strdup(symbol_name);
  1193. if (ref->name == NULL) {
  1194. free(ref);
  1195. return -ENOMEM;
  1196. }
  1197. bracket = strchr(ref->name, ']');
  1198. if (bracket)
  1199. *bracket = '\0';
  1200. ref->addr = addr;
  1201. for (i = 0; i < MAP__NR_TYPES; ++i) {
  1202. struct kmap *kmap = map__kmap(maps[i]);
  1203. kmap->ref_reloc_sym = ref;
  1204. }
  1205. return 0;
  1206. }
  1207. size_t perf_session__fprintf_dsos(struct perf_session *self, FILE *fp)
  1208. {
  1209. return __dsos__fprintf(&self->host_machine.kernel_dsos, fp) +
  1210. __dsos__fprintf(&self->host_machine.user_dsos, fp) +
  1211. machines__fprintf_dsos(&self->machines, fp);
  1212. }
  1213. size_t perf_session__fprintf_dsos_buildid(struct perf_session *self, FILE *fp,
  1214. bool with_hits)
  1215. {
  1216. size_t ret = machine__fprintf_dsos_buildid(&self->host_machine, fp, with_hits);
  1217. return ret + machines__fprintf_dsos_buildid(&self->machines, fp, with_hits);
  1218. }
  1219. size_t perf_session__fprintf_nr_events(struct perf_session *session, FILE *fp)
  1220. {
  1221. struct perf_evsel *pos;
  1222. size_t ret = fprintf(fp, "Aggregated stats:\n");
  1223. ret += hists__fprintf_nr_events(&session->hists, fp);
  1224. list_for_each_entry(pos, &session->evlist->entries, node) {
  1225. ret += fprintf(fp, "%s stats:\n", perf_evsel__name(pos));
  1226. ret += hists__fprintf_nr_events(&pos->hists, fp);
  1227. }
  1228. return ret;
  1229. }
  1230. size_t perf_session__fprintf(struct perf_session *session, FILE *fp)
  1231. {
  1232. /*
  1233. * FIXME: Here we have to actually print all the machines in this
  1234. * session, not just the host...
  1235. */
  1236. return machine__fprintf(&session->host_machine, fp);
  1237. }
  1238. void perf_session__remove_thread(struct perf_session *session,
  1239. struct thread *th)
  1240. {
  1241. /*
  1242. * FIXME: This one makes no sense, we need to remove the thread from
  1243. * the machine it belongs to, perf_session can have many machines, so
  1244. * doing it always on ->host_machine is wrong. Fix when auditing all
  1245. * the 'perf kvm' code.
  1246. */
  1247. machine__remove_thread(&session->host_machine, th);
  1248. }
  1249. struct perf_evsel *perf_session__find_first_evtype(struct perf_session *session,
  1250. unsigned int type)
  1251. {
  1252. struct perf_evsel *pos;
  1253. list_for_each_entry(pos, &session->evlist->entries, node) {
  1254. if (pos->attr.type == type)
  1255. return pos;
  1256. }
  1257. return NULL;
  1258. }
  1259. void perf_event__print_ip(union perf_event *event, struct perf_sample *sample,
  1260. struct machine *machine, int print_sym,
  1261. int print_dso, int print_symoffset)
  1262. {
  1263. struct addr_location al;
  1264. struct callchain_cursor_node *node;
  1265. if (perf_event__preprocess_sample(event, machine, &al, sample,
  1266. NULL) < 0) {
  1267. error("problem processing %d event, skipping it.\n",
  1268. event->header.type);
  1269. return;
  1270. }
  1271. if (symbol_conf.use_callchain && sample->callchain) {
  1272. if (machine__resolve_callchain(machine, al.thread,
  1273. sample->callchain, NULL) != 0) {
  1274. if (verbose)
  1275. error("Failed to resolve callchain. Skipping\n");
  1276. return;
  1277. }
  1278. callchain_cursor_commit(&callchain_cursor);
  1279. while (1) {
  1280. node = callchain_cursor_current(&callchain_cursor);
  1281. if (!node)
  1282. break;
  1283. printf("\t%16" PRIx64, node->ip);
  1284. if (print_sym) {
  1285. printf(" ");
  1286. symbol__fprintf_symname(node->sym, stdout);
  1287. }
  1288. if (print_dso) {
  1289. printf(" (");
  1290. map__fprintf_dsoname(node->map, stdout);
  1291. printf(")");
  1292. }
  1293. printf("\n");
  1294. callchain_cursor_advance(&callchain_cursor);
  1295. }
  1296. } else {
  1297. printf("%16" PRIx64, sample->ip);
  1298. if (print_sym) {
  1299. printf(" ");
  1300. if (print_symoffset)
  1301. symbol__fprintf_symname_offs(al.sym, &al,
  1302. stdout);
  1303. else
  1304. symbol__fprintf_symname(al.sym, stdout);
  1305. }
  1306. if (print_dso) {
  1307. printf(" (");
  1308. map__fprintf_dsoname(al.map, stdout);
  1309. printf(")");
  1310. }
  1311. }
  1312. }
  1313. int perf_session__cpu_bitmap(struct perf_session *session,
  1314. const char *cpu_list, unsigned long *cpu_bitmap)
  1315. {
  1316. int i;
  1317. struct cpu_map *map;
  1318. for (i = 0; i < PERF_TYPE_MAX; ++i) {
  1319. struct perf_evsel *evsel;
  1320. evsel = perf_session__find_first_evtype(session, i);
  1321. if (!evsel)
  1322. continue;
  1323. if (!(evsel->attr.sample_type & PERF_SAMPLE_CPU)) {
  1324. pr_err("File does not contain CPU events. "
  1325. "Remove -c option to proceed.\n");
  1326. return -1;
  1327. }
  1328. }
  1329. map = cpu_map__new(cpu_list);
  1330. if (map == NULL) {
  1331. pr_err("Invalid cpu_list\n");
  1332. return -1;
  1333. }
  1334. for (i = 0; i < map->nr; i++) {
  1335. int cpu = map->map[i];
  1336. if (cpu >= MAX_NR_CPUS) {
  1337. pr_err("Requested CPU %d too large. "
  1338. "Consider raising MAX_NR_CPUS\n", cpu);
  1339. return -1;
  1340. }
  1341. set_bit(cpu, cpu_bitmap);
  1342. }
  1343. return 0;
  1344. }
  1345. void perf_session__fprintf_info(struct perf_session *session, FILE *fp,
  1346. bool full)
  1347. {
  1348. struct stat st;
  1349. int ret;
  1350. if (session == NULL || fp == NULL)
  1351. return;
  1352. ret = fstat(session->fd, &st);
  1353. if (ret == -1)
  1354. return;
  1355. fprintf(fp, "# ========\n");
  1356. fprintf(fp, "# captured on: %s", ctime(&st.st_ctime));
  1357. perf_header__fprintf_info(session, fp, full);
  1358. fprintf(fp, "# ========\n#\n");
  1359. }
  1360. int __perf_session__set_tracepoints_handlers(struct perf_session *session,
  1361. const struct perf_evsel_str_handler *assocs,
  1362. size_t nr_assocs)
  1363. {
  1364. struct perf_evlist *evlist = session->evlist;
  1365. struct event_format *format;
  1366. struct perf_evsel *evsel;
  1367. char *tracepoint, *name;
  1368. size_t i;
  1369. int err;
  1370. for (i = 0; i < nr_assocs; i++) {
  1371. err = -ENOMEM;
  1372. tracepoint = strdup(assocs[i].name);
  1373. if (tracepoint == NULL)
  1374. goto out;
  1375. err = -ENOENT;
  1376. name = strchr(tracepoint, ':');
  1377. if (name == NULL)
  1378. goto out_free;
  1379. *name++ = '\0';
  1380. format = pevent_find_event_by_name(session->pevent,
  1381. tracepoint, name);
  1382. if (format == NULL) {
  1383. /*
  1384. * Adding a handler for an event not in the session,
  1385. * just ignore it.
  1386. */
  1387. goto next;
  1388. }
  1389. evsel = perf_evlist__find_tracepoint_by_id(evlist, format->id);
  1390. if (evsel == NULL)
  1391. goto next;
  1392. err = -EEXIST;
  1393. if (evsel->handler.func != NULL)
  1394. goto out_free;
  1395. evsel->handler.func = assocs[i].handler;
  1396. next:
  1397. free(tracepoint);
  1398. }
  1399. err = 0;
  1400. out:
  1401. return err;
  1402. out_free:
  1403. free(tracepoint);
  1404. goto out;
  1405. }