session.c 41 KB

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