session.c 40 KB

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