session.c 41 KB

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