session.c 45 KB

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