session.c 45 KB

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