session.c 39 KB

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