session.c 39 KB

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