session.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  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 "session.h"
  8. #include "sort.h"
  9. #include "util.h"
  10. static int perf_session__open(struct perf_session *self, bool force)
  11. {
  12. struct stat input_stat;
  13. if (!strcmp(self->filename, "-")) {
  14. self->fd_pipe = true;
  15. self->fd = STDIN_FILENO;
  16. if (perf_header__read(self, self->fd) < 0)
  17. pr_err("incompatible file format");
  18. return 0;
  19. }
  20. self->fd = open(self->filename, O_RDONLY);
  21. if (self->fd < 0) {
  22. int err = errno;
  23. pr_err("failed to open %s: %s", self->filename, strerror(err));
  24. if (err == ENOENT && !strcmp(self->filename, "perf.data"))
  25. pr_err(" (try 'perf record' first)");
  26. pr_err("\n");
  27. return -errno;
  28. }
  29. if (fstat(self->fd, &input_stat) < 0)
  30. goto out_close;
  31. if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) {
  32. pr_err("file %s not owned by current user or root\n",
  33. self->filename);
  34. goto out_close;
  35. }
  36. if (!input_stat.st_size) {
  37. pr_info("zero-sized file (%s), nothing to do!\n",
  38. self->filename);
  39. goto out_close;
  40. }
  41. if (perf_header__read(self, self->fd) < 0) {
  42. pr_err("incompatible file format");
  43. goto out_close;
  44. }
  45. self->size = input_stat.st_size;
  46. return 0;
  47. out_close:
  48. close(self->fd);
  49. self->fd = -1;
  50. return -1;
  51. }
  52. static void perf_session__id_header_size(struct perf_session *session)
  53. {
  54. struct sample_data *data;
  55. u64 sample_type = session->sample_type;
  56. u16 size = 0;
  57. if (!session->sample_id_all)
  58. goto out;
  59. if (sample_type & PERF_SAMPLE_TID)
  60. size += sizeof(data->tid) * 2;
  61. if (sample_type & PERF_SAMPLE_TIME)
  62. size += sizeof(data->time);
  63. if (sample_type & PERF_SAMPLE_ID)
  64. size += sizeof(data->id);
  65. if (sample_type & PERF_SAMPLE_STREAM_ID)
  66. size += sizeof(data->stream_id);
  67. if (sample_type & PERF_SAMPLE_CPU)
  68. size += sizeof(data->cpu) * 2;
  69. out:
  70. session->id_hdr_size = size;
  71. }
  72. void perf_session__set_sample_id_all(struct perf_session *session, bool value)
  73. {
  74. session->sample_id_all = value;
  75. perf_session__id_header_size(session);
  76. }
  77. void perf_session__set_sample_type(struct perf_session *session, u64 type)
  78. {
  79. session->sample_type = type;
  80. }
  81. void perf_session__update_sample_type(struct perf_session *self)
  82. {
  83. self->sample_type = perf_header__sample_type(&self->header);
  84. self->sample_id_all = perf_header__sample_id_all(&self->header);
  85. perf_session__id_header_size(self);
  86. }
  87. int perf_session__create_kernel_maps(struct perf_session *self)
  88. {
  89. int ret = machine__create_kernel_maps(&self->host_machine);
  90. if (ret >= 0)
  91. ret = machines__create_guest_kernel_maps(&self->machines);
  92. return ret;
  93. }
  94. static void perf_session__destroy_kernel_maps(struct perf_session *self)
  95. {
  96. machine__destroy_kernel_maps(&self->host_machine);
  97. machines__destroy_guest_kernel_maps(&self->machines);
  98. }
  99. struct perf_session *perf_session__new(const char *filename, int mode, bool force, bool repipe)
  100. {
  101. size_t len = filename ? strlen(filename) + 1 : 0;
  102. struct perf_session *self = zalloc(sizeof(*self) + len);
  103. if (self == NULL)
  104. goto out;
  105. if (perf_header__init(&self->header) < 0)
  106. goto out_free;
  107. memcpy(self->filename, filename, len);
  108. self->threads = RB_ROOT;
  109. INIT_LIST_HEAD(&self->dead_threads);
  110. self->hists_tree = RB_ROOT;
  111. self->last_match = NULL;
  112. /*
  113. * On 64bit we can mmap the data file in one go. No need for tiny mmap
  114. * slices. On 32bit we use 32MB.
  115. */
  116. #if BITS_PER_LONG == 64
  117. self->mmap_window = ULLONG_MAX;
  118. #else
  119. self->mmap_window = 32 * 1024 * 1024ULL;
  120. #endif
  121. self->machines = RB_ROOT;
  122. self->repipe = repipe;
  123. INIT_LIST_HEAD(&self->ordered_samples.samples);
  124. INIT_LIST_HEAD(&self->ordered_samples.sample_cache);
  125. INIT_LIST_HEAD(&self->ordered_samples.to_free);
  126. machine__init(&self->host_machine, "", HOST_KERNEL_ID);
  127. if (mode == O_RDONLY) {
  128. if (perf_session__open(self, force) < 0)
  129. goto out_delete;
  130. } else if (mode == O_WRONLY) {
  131. /*
  132. * In O_RDONLY mode this will be performed when reading the
  133. * kernel MMAP event, in event__process_mmap().
  134. */
  135. if (perf_session__create_kernel_maps(self) < 0)
  136. goto out_delete;
  137. }
  138. perf_session__update_sample_type(self);
  139. out:
  140. return self;
  141. out_free:
  142. free(self);
  143. return NULL;
  144. out_delete:
  145. perf_session__delete(self);
  146. return NULL;
  147. }
  148. static void perf_session__delete_dead_threads(struct perf_session *self)
  149. {
  150. struct thread *n, *t;
  151. list_for_each_entry_safe(t, n, &self->dead_threads, node) {
  152. list_del(&t->node);
  153. thread__delete(t);
  154. }
  155. }
  156. static void perf_session__delete_threads(struct perf_session *self)
  157. {
  158. struct rb_node *nd = rb_first(&self->threads);
  159. while (nd) {
  160. struct thread *t = rb_entry(nd, struct thread, rb_node);
  161. rb_erase(&t->rb_node, &self->threads);
  162. nd = rb_next(nd);
  163. thread__delete(t);
  164. }
  165. }
  166. void perf_session__delete(struct perf_session *self)
  167. {
  168. perf_header__exit(&self->header);
  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 perf_session__remove_thread(struct perf_session *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. struct map_symbol *perf_session__resolve_callchain(struct perf_session *self,
  193. struct thread *thread,
  194. struct ip_callchain *chain,
  195. struct symbol **parent)
  196. {
  197. u8 cpumode = PERF_RECORD_MISC_USER;
  198. unsigned int i;
  199. struct map_symbol *syms = calloc(chain->nr, sizeof(*syms));
  200. if (!syms)
  201. return NULL;
  202. for (i = 0; i < chain->nr; i++) {
  203. u64 ip = chain->ips[i];
  204. struct addr_location al;
  205. if (ip >= PERF_CONTEXT_MAX) {
  206. switch (ip) {
  207. case PERF_CONTEXT_HV:
  208. cpumode = PERF_RECORD_MISC_HYPERVISOR; break;
  209. case PERF_CONTEXT_KERNEL:
  210. cpumode = PERF_RECORD_MISC_KERNEL; break;
  211. case PERF_CONTEXT_USER:
  212. cpumode = PERF_RECORD_MISC_USER; break;
  213. default:
  214. break;
  215. }
  216. continue;
  217. }
  218. al.filtered = false;
  219. thread__find_addr_location(thread, self, cpumode,
  220. MAP__FUNCTION, thread->pid, ip, &al, NULL);
  221. if (al.sym != NULL) {
  222. if (sort__has_parent && !*parent &&
  223. symbol__match_parent_regex(al.sym))
  224. *parent = al.sym;
  225. if (!symbol_conf.use_callchain)
  226. break;
  227. syms[i].map = al.map;
  228. syms[i].sym = al.sym;
  229. }
  230. }
  231. return syms;
  232. }
  233. static int process_event_synth_stub(event_t *event __used,
  234. struct perf_session *session __used)
  235. {
  236. dump_printf(": unhandled!\n");
  237. return 0;
  238. }
  239. static int process_event_stub(event_t *event __used,
  240. struct sample_data *sample __used,
  241. struct perf_session *session __used)
  242. {
  243. dump_printf(": unhandled!\n");
  244. return 0;
  245. }
  246. static int process_finished_round_stub(event_t *event __used,
  247. struct perf_session *session __used,
  248. struct perf_event_ops *ops __used)
  249. {
  250. dump_printf(": unhandled!\n");
  251. return 0;
  252. }
  253. static int process_finished_round(event_t *event,
  254. struct perf_session *session,
  255. struct perf_event_ops *ops);
  256. static void perf_event_ops__fill_defaults(struct perf_event_ops *handler)
  257. {
  258. if (handler->sample == NULL)
  259. handler->sample = process_event_stub;
  260. if (handler->mmap == NULL)
  261. handler->mmap = process_event_stub;
  262. if (handler->comm == NULL)
  263. handler->comm = process_event_stub;
  264. if (handler->fork == NULL)
  265. handler->fork = process_event_stub;
  266. if (handler->exit == NULL)
  267. handler->exit = process_event_stub;
  268. if (handler->lost == NULL)
  269. handler->lost = event__process_lost;
  270. if (handler->read == NULL)
  271. handler->read = process_event_stub;
  272. if (handler->throttle == NULL)
  273. handler->throttle = process_event_stub;
  274. if (handler->unthrottle == NULL)
  275. handler->unthrottle = process_event_stub;
  276. if (handler->attr == NULL)
  277. handler->attr = process_event_synth_stub;
  278. if (handler->event_type == NULL)
  279. handler->event_type = process_event_synth_stub;
  280. if (handler->tracing_data == NULL)
  281. handler->tracing_data = process_event_synth_stub;
  282. if (handler->build_id == NULL)
  283. handler->build_id = process_event_synth_stub;
  284. if (handler->finished_round == NULL) {
  285. if (handler->ordered_samples)
  286. handler->finished_round = process_finished_round;
  287. else
  288. handler->finished_round = process_finished_round_stub;
  289. }
  290. }
  291. void mem_bswap_64(void *src, int byte_size)
  292. {
  293. u64 *m = src;
  294. while (byte_size > 0) {
  295. *m = bswap_64(*m);
  296. byte_size -= sizeof(u64);
  297. ++m;
  298. }
  299. }
  300. static void event__all64_swap(event_t *self)
  301. {
  302. struct perf_event_header *hdr = &self->header;
  303. mem_bswap_64(hdr + 1, self->header.size - sizeof(*hdr));
  304. }
  305. static void event__comm_swap(event_t *self)
  306. {
  307. self->comm.pid = bswap_32(self->comm.pid);
  308. self->comm.tid = bswap_32(self->comm.tid);
  309. }
  310. static void event__mmap_swap(event_t *self)
  311. {
  312. self->mmap.pid = bswap_32(self->mmap.pid);
  313. self->mmap.tid = bswap_32(self->mmap.tid);
  314. self->mmap.start = bswap_64(self->mmap.start);
  315. self->mmap.len = bswap_64(self->mmap.len);
  316. self->mmap.pgoff = bswap_64(self->mmap.pgoff);
  317. }
  318. static void event__task_swap(event_t *self)
  319. {
  320. self->fork.pid = bswap_32(self->fork.pid);
  321. self->fork.tid = bswap_32(self->fork.tid);
  322. self->fork.ppid = bswap_32(self->fork.ppid);
  323. self->fork.ptid = bswap_32(self->fork.ptid);
  324. self->fork.time = bswap_64(self->fork.time);
  325. }
  326. static void event__read_swap(event_t *self)
  327. {
  328. self->read.pid = bswap_32(self->read.pid);
  329. self->read.tid = bswap_32(self->read.tid);
  330. self->read.value = bswap_64(self->read.value);
  331. self->read.time_enabled = bswap_64(self->read.time_enabled);
  332. self->read.time_running = bswap_64(self->read.time_running);
  333. self->read.id = bswap_64(self->read.id);
  334. }
  335. static void event__attr_swap(event_t *self)
  336. {
  337. size_t size;
  338. self->attr.attr.type = bswap_32(self->attr.attr.type);
  339. self->attr.attr.size = bswap_32(self->attr.attr.size);
  340. self->attr.attr.config = bswap_64(self->attr.attr.config);
  341. self->attr.attr.sample_period = bswap_64(self->attr.attr.sample_period);
  342. self->attr.attr.sample_type = bswap_64(self->attr.attr.sample_type);
  343. self->attr.attr.read_format = bswap_64(self->attr.attr.read_format);
  344. self->attr.attr.wakeup_events = bswap_32(self->attr.attr.wakeup_events);
  345. self->attr.attr.bp_type = bswap_32(self->attr.attr.bp_type);
  346. self->attr.attr.bp_addr = bswap_64(self->attr.attr.bp_addr);
  347. self->attr.attr.bp_len = bswap_64(self->attr.attr.bp_len);
  348. size = self->header.size;
  349. size -= (void *)&self->attr.id - (void *)self;
  350. mem_bswap_64(self->attr.id, size);
  351. }
  352. static void event__event_type_swap(event_t *self)
  353. {
  354. self->event_type.event_type.event_id =
  355. bswap_64(self->event_type.event_type.event_id);
  356. }
  357. static void event__tracing_data_swap(event_t *self)
  358. {
  359. self->tracing_data.size = bswap_32(self->tracing_data.size);
  360. }
  361. typedef void (*event__swap_op)(event_t *self);
  362. static event__swap_op event__swap_ops[] = {
  363. [PERF_RECORD_MMAP] = event__mmap_swap,
  364. [PERF_RECORD_COMM] = event__comm_swap,
  365. [PERF_RECORD_FORK] = event__task_swap,
  366. [PERF_RECORD_EXIT] = event__task_swap,
  367. [PERF_RECORD_LOST] = event__all64_swap,
  368. [PERF_RECORD_READ] = event__read_swap,
  369. [PERF_RECORD_SAMPLE] = event__all64_swap,
  370. [PERF_RECORD_HEADER_ATTR] = event__attr_swap,
  371. [PERF_RECORD_HEADER_EVENT_TYPE] = event__event_type_swap,
  372. [PERF_RECORD_HEADER_TRACING_DATA] = event__tracing_data_swap,
  373. [PERF_RECORD_HEADER_BUILD_ID] = NULL,
  374. [PERF_RECORD_HEADER_MAX] = NULL,
  375. };
  376. struct sample_queue {
  377. u64 timestamp;
  378. event_t *event;
  379. struct list_head list;
  380. };
  381. static void perf_session_free_sample_buffers(struct perf_session *session)
  382. {
  383. struct ordered_samples *os = &session->ordered_samples;
  384. while (!list_empty(&os->to_free)) {
  385. struct sample_queue *sq;
  386. sq = list_entry(os->to_free.next, struct sample_queue, list);
  387. list_del(&sq->list);
  388. free(sq);
  389. }
  390. }
  391. static void flush_sample_queue(struct perf_session *s,
  392. struct perf_event_ops *ops)
  393. {
  394. struct ordered_samples *os = &s->ordered_samples;
  395. struct list_head *head = &os->samples;
  396. struct sample_queue *tmp, *iter;
  397. struct sample_data sample;
  398. u64 limit = os->next_flush;
  399. u64 last_ts = os->last_sample ? os->last_sample->timestamp : 0ULL;
  400. if (!ops->ordered_samples || !limit)
  401. return;
  402. list_for_each_entry_safe(iter, tmp, head, list) {
  403. if (iter->timestamp > limit)
  404. break;
  405. event__parse_sample(iter->event, s, &sample);
  406. ops->sample(iter->event, &sample, s);
  407. os->last_flush = iter->timestamp;
  408. list_del(&iter->list);
  409. list_add(&iter->list, &os->sample_cache);
  410. }
  411. if (list_empty(head)) {
  412. os->last_sample = NULL;
  413. } else if (last_ts <= limit) {
  414. os->last_sample =
  415. list_entry(head->prev, struct sample_queue, list);
  416. }
  417. }
  418. /*
  419. * When perf record finishes a pass on every buffers, it records this pseudo
  420. * event.
  421. * We record the max timestamp t found in the pass n.
  422. * Assuming these timestamps are monotonic across cpus, we know that if
  423. * a buffer still has events with timestamps below t, they will be all
  424. * available and then read in the pass n + 1.
  425. * Hence when we start to read the pass n + 2, we can safely flush every
  426. * events with timestamps below t.
  427. *
  428. * ============ PASS n =================
  429. * CPU 0 | CPU 1
  430. * |
  431. * cnt1 timestamps | cnt2 timestamps
  432. * 1 | 2
  433. * 2 | 3
  434. * - | 4 <--- max recorded
  435. *
  436. * ============ PASS n + 1 ==============
  437. * CPU 0 | CPU 1
  438. * |
  439. * cnt1 timestamps | cnt2 timestamps
  440. * 3 | 5
  441. * 4 | 6
  442. * 5 | 7 <---- max recorded
  443. *
  444. * Flush every events below timestamp 4
  445. *
  446. * ============ PASS n + 2 ==============
  447. * CPU 0 | CPU 1
  448. * |
  449. * cnt1 timestamps | cnt2 timestamps
  450. * 6 | 8
  451. * 7 | 9
  452. * - | 10
  453. *
  454. * Flush every events below timestamp 7
  455. * etc...
  456. */
  457. static int process_finished_round(event_t *event __used,
  458. struct perf_session *session,
  459. struct perf_event_ops *ops)
  460. {
  461. flush_sample_queue(session, ops);
  462. session->ordered_samples.next_flush = session->ordered_samples.max_timestamp;
  463. return 0;
  464. }
  465. /* The queue is ordered by time */
  466. static void __queue_sample_event(struct sample_queue *new,
  467. struct perf_session *s)
  468. {
  469. struct ordered_samples *os = &s->ordered_samples;
  470. struct sample_queue *sample = os->last_sample;
  471. u64 timestamp = new->timestamp;
  472. struct list_head *p;
  473. os->last_sample = new;
  474. if (!sample) {
  475. list_add(&new->list, &os->samples);
  476. os->max_timestamp = timestamp;
  477. return;
  478. }
  479. /*
  480. * last_sample might point to some random place in the list as it's
  481. * the last queued event. We expect that the new event is close to
  482. * this.
  483. */
  484. if (sample->timestamp <= timestamp) {
  485. while (sample->timestamp <= timestamp) {
  486. p = sample->list.next;
  487. if (p == &os->samples) {
  488. list_add_tail(&new->list, &os->samples);
  489. os->max_timestamp = timestamp;
  490. return;
  491. }
  492. sample = list_entry(p, struct sample_queue, list);
  493. }
  494. list_add_tail(&new->list, &sample->list);
  495. } else {
  496. while (sample->timestamp > timestamp) {
  497. p = sample->list.prev;
  498. if (p == &os->samples) {
  499. list_add(&new->list, &os->samples);
  500. return;
  501. }
  502. sample = list_entry(p, struct sample_queue, list);
  503. }
  504. list_add(&new->list, &sample->list);
  505. }
  506. }
  507. #define MAX_SAMPLE_BUFFER (64 * 1024 / sizeof(struct sample_queue))
  508. static int queue_sample_event(event_t *event, struct sample_data *data,
  509. struct perf_session *s)
  510. {
  511. struct ordered_samples *os = &s->ordered_samples;
  512. struct list_head *sc = &os->sample_cache;
  513. u64 timestamp = data->time;
  514. struct sample_queue *new;
  515. if (timestamp < s->ordered_samples.last_flush) {
  516. printf("Warning: Timestamp below last timeslice flush\n");
  517. return -EINVAL;
  518. }
  519. if (!list_empty(sc)) {
  520. new = list_entry(sc->next, struct sample_queue, list);
  521. list_del(&new->list);
  522. } else if (os->sample_buffer) {
  523. new = os->sample_buffer + os->sample_buffer_idx;
  524. if (++os->sample_buffer_idx == MAX_SAMPLE_BUFFER)
  525. os->sample_buffer = NULL;
  526. } else {
  527. os->sample_buffer = malloc(MAX_SAMPLE_BUFFER * sizeof(*new));
  528. if (!os->sample_buffer)
  529. return -ENOMEM;
  530. list_add(&os->sample_buffer->list, &os->to_free);
  531. os->sample_buffer_idx = 2;
  532. new = os->sample_buffer + 1;
  533. }
  534. new->timestamp = timestamp;
  535. new->event = event;
  536. __queue_sample_event(new, s);
  537. return 0;
  538. }
  539. static int perf_session__process_sample(event_t *event,
  540. struct sample_data *sample,
  541. struct perf_session *s,
  542. struct perf_event_ops *ops)
  543. {
  544. if (!ops->ordered_samples)
  545. return ops->sample(event, sample, s);
  546. queue_sample_event(event, sample, s);
  547. return 0;
  548. }
  549. static void callchain__dump(struct sample_data *sample)
  550. {
  551. unsigned int i;
  552. if (!dump_trace)
  553. return;
  554. printf("... chain: nr:%Lu\n", sample->callchain->nr);
  555. for (i = 0; i < sample->callchain->nr; i++)
  556. printf("..... %2d: %016Lx\n", i, sample->callchain->ips[i]);
  557. }
  558. static void perf_session__print_tstamp(struct perf_session *session,
  559. event_t *event,
  560. struct sample_data *sample)
  561. {
  562. if (event->header.type != PERF_RECORD_SAMPLE &&
  563. !session->sample_id_all) {
  564. fputs("-1 -1 ", stdout);
  565. return;
  566. }
  567. if ((session->sample_type & PERF_SAMPLE_CPU))
  568. printf("%u ", sample->cpu);
  569. if (session->sample_type & PERF_SAMPLE_TIME)
  570. printf("%Lu ", sample->time);
  571. }
  572. static int perf_session__process_event(struct perf_session *self,
  573. event_t *event,
  574. struct perf_event_ops *ops,
  575. u64 file_offset)
  576. {
  577. struct sample_data sample;
  578. trace_event(event);
  579. if (self->header.needs_swap && event__swap_ops[event->header.type])
  580. event__swap_ops[event->header.type](event);
  581. if (event->header.type >= PERF_RECORD_MMAP &&
  582. event->header.type <= PERF_RECORD_SAMPLE) {
  583. event__parse_sample(event, self, &sample);
  584. if (dump_trace)
  585. perf_session__print_tstamp(self, event, &sample);
  586. }
  587. if (event->header.type < PERF_RECORD_HEADER_MAX) {
  588. dump_printf("%#Lx [%#x]: PERF_RECORD_%s",
  589. file_offset, event->header.size,
  590. event__name[event->header.type]);
  591. hists__inc_nr_events(&self->hists, event->header.type);
  592. }
  593. switch (event->header.type) {
  594. case PERF_RECORD_SAMPLE:
  595. dump_printf("(IP, %d): %d/%d: %#Lx period: %Ld\n", event->header.misc,
  596. sample.pid, sample.tid, sample.ip, sample.period);
  597. if (self->sample_type & PERF_SAMPLE_CALLCHAIN) {
  598. if (!ip_callchain__valid(sample.callchain, event)) {
  599. pr_debug("call-chain problem with event, "
  600. "skipping it.\n");
  601. ++self->hists.stats.nr_invalid_chains;
  602. self->hists.stats.total_invalid_chains += sample.period;
  603. return 0;
  604. }
  605. callchain__dump(&sample);
  606. }
  607. return perf_session__process_sample(event, &sample, self, ops);
  608. case PERF_RECORD_MMAP:
  609. return ops->mmap(event, &sample, self);
  610. case PERF_RECORD_COMM:
  611. return ops->comm(event, &sample, self);
  612. case PERF_RECORD_FORK:
  613. return ops->fork(event, &sample, self);
  614. case PERF_RECORD_EXIT:
  615. return ops->exit(event, &sample, self);
  616. case PERF_RECORD_LOST:
  617. return ops->lost(event, &sample, self);
  618. case PERF_RECORD_READ:
  619. return ops->read(event, &sample, self);
  620. case PERF_RECORD_THROTTLE:
  621. return ops->throttle(event, &sample, self);
  622. case PERF_RECORD_UNTHROTTLE:
  623. return ops->unthrottle(event, &sample, self);
  624. case PERF_RECORD_HEADER_ATTR:
  625. return ops->attr(event, self);
  626. case PERF_RECORD_HEADER_EVENT_TYPE:
  627. return ops->event_type(event, self);
  628. case PERF_RECORD_HEADER_TRACING_DATA:
  629. /* setup for reading amidst mmap */
  630. lseek(self->fd, file_offset, SEEK_SET);
  631. return ops->tracing_data(event, self);
  632. case PERF_RECORD_HEADER_BUILD_ID:
  633. return ops->build_id(event, self);
  634. case PERF_RECORD_FINISHED_ROUND:
  635. return ops->finished_round(event, self, ops);
  636. default:
  637. ++self->hists.stats.nr_unknown_events;
  638. return -1;
  639. }
  640. }
  641. void perf_event_header__bswap(struct perf_event_header *self)
  642. {
  643. self->type = bswap_32(self->type);
  644. self->misc = bswap_16(self->misc);
  645. self->size = bswap_16(self->size);
  646. }
  647. static struct thread *perf_session__register_idle_thread(struct perf_session *self)
  648. {
  649. struct thread *thread = perf_session__findnew(self, 0);
  650. if (thread == NULL || thread__set_comm(thread, "swapper")) {
  651. pr_err("problem inserting idle task.\n");
  652. thread = NULL;
  653. }
  654. return thread;
  655. }
  656. int do_read(int fd, void *buf, size_t size)
  657. {
  658. void *buf_start = buf;
  659. while (size) {
  660. int ret = read(fd, buf, size);
  661. if (ret <= 0)
  662. return ret;
  663. size -= ret;
  664. buf += ret;
  665. }
  666. return buf - buf_start;
  667. }
  668. #define session_done() (*(volatile int *)(&session_done))
  669. volatile int session_done;
  670. static int __perf_session__process_pipe_events(struct perf_session *self,
  671. struct perf_event_ops *ops)
  672. {
  673. event_t event;
  674. uint32_t size;
  675. int skip = 0;
  676. u64 head;
  677. int err;
  678. void *p;
  679. perf_event_ops__fill_defaults(ops);
  680. head = 0;
  681. more:
  682. err = do_read(self->fd, &event, sizeof(struct perf_event_header));
  683. if (err <= 0) {
  684. if (err == 0)
  685. goto done;
  686. pr_err("failed to read event header\n");
  687. goto out_err;
  688. }
  689. if (self->header.needs_swap)
  690. perf_event_header__bswap(&event.header);
  691. size = event.header.size;
  692. if (size == 0)
  693. size = 8;
  694. p = &event;
  695. p += sizeof(struct perf_event_header);
  696. if (size - sizeof(struct perf_event_header)) {
  697. err = do_read(self->fd, p,
  698. size - sizeof(struct perf_event_header));
  699. if (err <= 0) {
  700. if (err == 0) {
  701. pr_err("unexpected end of event stream\n");
  702. goto done;
  703. }
  704. pr_err("failed to read event data\n");
  705. goto out_err;
  706. }
  707. }
  708. if (size == 0 ||
  709. (skip = perf_session__process_event(self, &event, ops, head)) < 0) {
  710. dump_printf("%#Lx [%#x]: skipping unknown header type: %d\n",
  711. head, event.header.size, event.header.type);
  712. /*
  713. * assume we lost track of the stream, check alignment, and
  714. * increment a single u64 in the hope to catch on again 'soon'.
  715. */
  716. if (unlikely(head & 7))
  717. head &= ~7ULL;
  718. size = 8;
  719. }
  720. head += size;
  721. dump_printf("\n%#Lx [%#x]: event: %d\n",
  722. head, event.header.size, event.header.type);
  723. if (skip > 0)
  724. head += skip;
  725. if (!session_done())
  726. goto more;
  727. done:
  728. err = 0;
  729. out_err:
  730. perf_session_free_sample_buffers(self);
  731. return err;
  732. }
  733. int __perf_session__process_events(struct perf_session *session,
  734. u64 data_offset, u64 data_size,
  735. u64 file_size, struct perf_event_ops *ops)
  736. {
  737. u64 head, page_offset, file_offset, file_pos, progress_next;
  738. int err, mmap_prot, mmap_flags, map_idx = 0;
  739. struct ui_progress *progress;
  740. size_t page_size, mmap_size;
  741. char *buf, *mmaps[8];
  742. event_t *event;
  743. uint32_t size;
  744. perf_event_ops__fill_defaults(ops);
  745. page_size = sysconf(_SC_PAGESIZE);
  746. page_offset = page_size * (data_offset / page_size);
  747. file_offset = page_offset;
  748. head = data_offset - page_offset;
  749. if (data_offset + data_size < file_size)
  750. file_size = data_offset + data_size;
  751. progress_next = file_size / 16;
  752. progress = ui_progress__new("Processing events...", file_size);
  753. if (progress == NULL)
  754. return -1;
  755. mmap_size = session->mmap_window;
  756. if (mmap_size > file_size)
  757. mmap_size = file_size;
  758. memset(mmaps, 0, sizeof(mmaps));
  759. mmap_prot = PROT_READ;
  760. mmap_flags = MAP_SHARED;
  761. if (session->header.needs_swap) {
  762. mmap_prot |= PROT_WRITE;
  763. mmap_flags = MAP_PRIVATE;
  764. }
  765. remap:
  766. buf = mmap(NULL, mmap_size, mmap_prot, mmap_flags, session->fd,
  767. file_offset);
  768. if (buf == MAP_FAILED) {
  769. pr_err("failed to mmap file\n");
  770. err = -errno;
  771. goto out_err;
  772. }
  773. mmaps[map_idx] = buf;
  774. map_idx = (map_idx + 1) & (ARRAY_SIZE(mmaps) - 1);
  775. file_pos = file_offset + head;
  776. more:
  777. event = (event_t *)(buf + head);
  778. if (session->header.needs_swap)
  779. perf_event_header__bswap(&event->header);
  780. size = event->header.size;
  781. if (size == 0)
  782. size = 8;
  783. if (head + event->header.size >= mmap_size) {
  784. if (mmaps[map_idx]) {
  785. munmap(mmaps[map_idx], mmap_size);
  786. mmaps[map_idx] = NULL;
  787. }
  788. page_offset = page_size * (head / page_size);
  789. file_offset += page_offset;
  790. head -= page_offset;
  791. goto remap;
  792. }
  793. size = event->header.size;
  794. dump_printf("\n%#Lx [%#x]: event: %d\n",
  795. file_pos, event->header.size, event->header.type);
  796. if (size == 0 ||
  797. perf_session__process_event(session, event, ops, file_pos) < 0) {
  798. dump_printf("%#Lx [%#x]: skipping unknown header type: %d\n",
  799. file_offset + head, event->header.size,
  800. event->header.type);
  801. /*
  802. * assume we lost track of the stream, check alignment, and
  803. * increment a single u64 in the hope to catch on again 'soon'.
  804. */
  805. if (unlikely(head & 7))
  806. head &= ~7ULL;
  807. size = 8;
  808. }
  809. head += size;
  810. file_pos += size;
  811. if (file_pos >= progress_next) {
  812. progress_next += file_size / 16;
  813. ui_progress__update(progress, file_pos);
  814. }
  815. if (file_pos < file_size)
  816. goto more;
  817. err = 0;
  818. /* do the final flush for ordered samples */
  819. session->ordered_samples.next_flush = ULLONG_MAX;
  820. flush_sample_queue(session, ops);
  821. out_err:
  822. ui_progress__delete(progress);
  823. if (ops->lost == event__process_lost &&
  824. session->hists.stats.total_lost != 0) {
  825. ui__warning("Processed %Lu events and LOST %Lu!\n\n"
  826. "Check IO/CPU overload!\n\n",
  827. session->hists.stats.total_period,
  828. session->hists.stats.total_lost);
  829. }
  830. if (session->hists.stats.nr_unknown_events != 0) {
  831. ui__warning("Found %u unknown events!\n\n"
  832. "Is this an older tool processing a perf.data "
  833. "file generated by a more recent tool?\n\n"
  834. "If that is not the case, consider "
  835. "reporting to linux-kernel@vger.kernel.org.\n\n",
  836. session->hists.stats.nr_unknown_events);
  837. }
  838. if (session->hists.stats.nr_invalid_chains != 0) {
  839. ui__warning("Found invalid callchains!\n\n"
  840. "%u out of %u events were discarded for this reason.\n\n"
  841. "Consider reporting to linux-kernel@vger.kernel.org.\n\n",
  842. session->hists.stats.nr_invalid_chains,
  843. session->hists.stats.nr_events[PERF_RECORD_SAMPLE]);
  844. }
  845. perf_session_free_sample_buffers(session);
  846. return err;
  847. }
  848. int perf_session__process_events(struct perf_session *self,
  849. struct perf_event_ops *ops)
  850. {
  851. int err;
  852. if (perf_session__register_idle_thread(self) == NULL)
  853. return -ENOMEM;
  854. if (!self->fd_pipe)
  855. err = __perf_session__process_events(self,
  856. self->header.data_offset,
  857. self->header.data_size,
  858. self->size, ops);
  859. else
  860. err = __perf_session__process_pipe_events(self, ops);
  861. return err;
  862. }
  863. bool perf_session__has_traces(struct perf_session *self, const char *msg)
  864. {
  865. if (!(self->sample_type & PERF_SAMPLE_RAW)) {
  866. pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg);
  867. return false;
  868. }
  869. return true;
  870. }
  871. int perf_session__set_kallsyms_ref_reloc_sym(struct map **maps,
  872. const char *symbol_name,
  873. u64 addr)
  874. {
  875. char *bracket;
  876. enum map_type i;
  877. struct ref_reloc_sym *ref;
  878. ref = zalloc(sizeof(struct ref_reloc_sym));
  879. if (ref == NULL)
  880. return -ENOMEM;
  881. ref->name = strdup(symbol_name);
  882. if (ref->name == NULL) {
  883. free(ref);
  884. return -ENOMEM;
  885. }
  886. bracket = strchr(ref->name, ']');
  887. if (bracket)
  888. *bracket = '\0';
  889. ref->addr = addr;
  890. for (i = 0; i < MAP__NR_TYPES; ++i) {
  891. struct kmap *kmap = map__kmap(maps[i]);
  892. kmap->ref_reloc_sym = ref;
  893. }
  894. return 0;
  895. }
  896. size_t perf_session__fprintf_dsos(struct perf_session *self, FILE *fp)
  897. {
  898. return __dsos__fprintf(&self->host_machine.kernel_dsos, fp) +
  899. __dsos__fprintf(&self->host_machine.user_dsos, fp) +
  900. machines__fprintf_dsos(&self->machines, fp);
  901. }
  902. size_t perf_session__fprintf_dsos_buildid(struct perf_session *self, FILE *fp,
  903. bool with_hits)
  904. {
  905. size_t ret = machine__fprintf_dsos_buildid(&self->host_machine, fp, with_hits);
  906. return ret + machines__fprintf_dsos_buildid(&self->machines, fp, with_hits);
  907. }