session.c 42 KB

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