session.c 33 KB

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