auxtrace.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370
  1. /*
  2. * auxtrace.c: AUX area trace support
  3. * Copyright (c) 2013-2015, Intel Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. */
  15. #include <sys/types.h>
  16. #include <sys/mman.h>
  17. #include <stdbool.h>
  18. #include <linux/kernel.h>
  19. #include <linux/perf_event.h>
  20. #include <linux/types.h>
  21. #include <linux/bitops.h>
  22. #include <linux/log2.h>
  23. #include <linux/string.h>
  24. #include <sys/param.h>
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <limits.h>
  29. #include <errno.h>
  30. #include <linux/list.h>
  31. #include "../perf.h"
  32. #include "util.h"
  33. #include "evlist.h"
  34. #include "cpumap.h"
  35. #include "thread_map.h"
  36. #include "asm/bug.h"
  37. #include "auxtrace.h"
  38. #include <linux/hash.h>
  39. #include "event.h"
  40. #include "session.h"
  41. #include "debug.h"
  42. #include "parse-options.h"
  43. #include "intel-pt.h"
  44. #include "intel-bts.h"
  45. int auxtrace_mmap__mmap(struct auxtrace_mmap *mm,
  46. struct auxtrace_mmap_params *mp,
  47. void *userpg, int fd)
  48. {
  49. struct perf_event_mmap_page *pc = userpg;
  50. WARN_ONCE(mm->base, "Uninitialized auxtrace_mmap\n");
  51. mm->userpg = userpg;
  52. mm->mask = mp->mask;
  53. mm->len = mp->len;
  54. mm->prev = 0;
  55. mm->idx = mp->idx;
  56. mm->tid = mp->tid;
  57. mm->cpu = mp->cpu;
  58. if (!mp->len) {
  59. mm->base = NULL;
  60. return 0;
  61. }
  62. #if BITS_PER_LONG != 64 && !defined(HAVE_SYNC_COMPARE_AND_SWAP_SUPPORT)
  63. pr_err("Cannot use AUX area tracing mmaps\n");
  64. return -1;
  65. #endif
  66. pc->aux_offset = mp->offset;
  67. pc->aux_size = mp->len;
  68. mm->base = mmap(NULL, mp->len, mp->prot, MAP_SHARED, fd, mp->offset);
  69. if (mm->base == MAP_FAILED) {
  70. pr_debug2("failed to mmap AUX area\n");
  71. mm->base = NULL;
  72. return -1;
  73. }
  74. return 0;
  75. }
  76. void auxtrace_mmap__munmap(struct auxtrace_mmap *mm)
  77. {
  78. if (mm->base) {
  79. munmap(mm->base, mm->len);
  80. mm->base = NULL;
  81. }
  82. }
  83. void auxtrace_mmap_params__init(struct auxtrace_mmap_params *mp,
  84. off_t auxtrace_offset,
  85. unsigned int auxtrace_pages,
  86. bool auxtrace_overwrite)
  87. {
  88. if (auxtrace_pages) {
  89. mp->offset = auxtrace_offset;
  90. mp->len = auxtrace_pages * (size_t)page_size;
  91. mp->mask = is_power_of_2(mp->len) ? mp->len - 1 : 0;
  92. mp->prot = PROT_READ | (auxtrace_overwrite ? 0 : PROT_WRITE);
  93. pr_debug2("AUX area mmap length %zu\n", mp->len);
  94. } else {
  95. mp->len = 0;
  96. }
  97. }
  98. void auxtrace_mmap_params__set_idx(struct auxtrace_mmap_params *mp,
  99. struct perf_evlist *evlist, int idx,
  100. bool per_cpu)
  101. {
  102. mp->idx = idx;
  103. if (per_cpu) {
  104. mp->cpu = evlist->cpus->map[idx];
  105. if (evlist->threads)
  106. mp->tid = thread_map__pid(evlist->threads, 0);
  107. else
  108. mp->tid = -1;
  109. } else {
  110. mp->cpu = -1;
  111. mp->tid = thread_map__pid(evlist->threads, idx);
  112. }
  113. }
  114. #define AUXTRACE_INIT_NR_QUEUES 32
  115. static struct auxtrace_queue *auxtrace_alloc_queue_array(unsigned int nr_queues)
  116. {
  117. struct auxtrace_queue *queue_array;
  118. unsigned int max_nr_queues, i;
  119. max_nr_queues = UINT_MAX / sizeof(struct auxtrace_queue);
  120. if (nr_queues > max_nr_queues)
  121. return NULL;
  122. queue_array = calloc(nr_queues, sizeof(struct auxtrace_queue));
  123. if (!queue_array)
  124. return NULL;
  125. for (i = 0; i < nr_queues; i++) {
  126. INIT_LIST_HEAD(&queue_array[i].head);
  127. queue_array[i].priv = NULL;
  128. }
  129. return queue_array;
  130. }
  131. int auxtrace_queues__init(struct auxtrace_queues *queues)
  132. {
  133. queues->nr_queues = AUXTRACE_INIT_NR_QUEUES;
  134. queues->queue_array = auxtrace_alloc_queue_array(queues->nr_queues);
  135. if (!queues->queue_array)
  136. return -ENOMEM;
  137. return 0;
  138. }
  139. static int auxtrace_queues__grow(struct auxtrace_queues *queues,
  140. unsigned int new_nr_queues)
  141. {
  142. unsigned int nr_queues = queues->nr_queues;
  143. struct auxtrace_queue *queue_array;
  144. unsigned int i;
  145. if (!nr_queues)
  146. nr_queues = AUXTRACE_INIT_NR_QUEUES;
  147. while (nr_queues && nr_queues < new_nr_queues)
  148. nr_queues <<= 1;
  149. if (nr_queues < queues->nr_queues || nr_queues < new_nr_queues)
  150. return -EINVAL;
  151. queue_array = auxtrace_alloc_queue_array(nr_queues);
  152. if (!queue_array)
  153. return -ENOMEM;
  154. for (i = 0; i < queues->nr_queues; i++) {
  155. list_splice_tail(&queues->queue_array[i].head,
  156. &queue_array[i].head);
  157. queue_array[i].priv = queues->queue_array[i].priv;
  158. }
  159. queues->nr_queues = nr_queues;
  160. queues->queue_array = queue_array;
  161. return 0;
  162. }
  163. static void *auxtrace_copy_data(u64 size, struct perf_session *session)
  164. {
  165. int fd = perf_data_file__fd(session->file);
  166. void *p;
  167. ssize_t ret;
  168. if (size > SSIZE_MAX)
  169. return NULL;
  170. p = malloc(size);
  171. if (!p)
  172. return NULL;
  173. ret = readn(fd, p, size);
  174. if (ret != (ssize_t)size) {
  175. free(p);
  176. return NULL;
  177. }
  178. return p;
  179. }
  180. static int auxtrace_queues__add_buffer(struct auxtrace_queues *queues,
  181. unsigned int idx,
  182. struct auxtrace_buffer *buffer)
  183. {
  184. struct auxtrace_queue *queue;
  185. int err;
  186. if (idx >= queues->nr_queues) {
  187. err = auxtrace_queues__grow(queues, idx + 1);
  188. if (err)
  189. return err;
  190. }
  191. queue = &queues->queue_array[idx];
  192. if (!queue->set) {
  193. queue->set = true;
  194. queue->tid = buffer->tid;
  195. queue->cpu = buffer->cpu;
  196. } else if (buffer->cpu != queue->cpu || buffer->tid != queue->tid) {
  197. pr_err("auxtrace queue conflict: cpu %d, tid %d vs cpu %d, tid %d\n",
  198. queue->cpu, queue->tid, buffer->cpu, buffer->tid);
  199. return -EINVAL;
  200. }
  201. buffer->buffer_nr = queues->next_buffer_nr++;
  202. list_add_tail(&buffer->list, &queue->head);
  203. queues->new_data = true;
  204. queues->populated = true;
  205. return 0;
  206. }
  207. /* Limit buffers to 32MiB on 32-bit */
  208. #define BUFFER_LIMIT_FOR_32_BIT (32 * 1024 * 1024)
  209. static int auxtrace_queues__split_buffer(struct auxtrace_queues *queues,
  210. unsigned int idx,
  211. struct auxtrace_buffer *buffer)
  212. {
  213. u64 sz = buffer->size;
  214. bool consecutive = false;
  215. struct auxtrace_buffer *b;
  216. int err;
  217. while (sz > BUFFER_LIMIT_FOR_32_BIT) {
  218. b = memdup(buffer, sizeof(struct auxtrace_buffer));
  219. if (!b)
  220. return -ENOMEM;
  221. b->size = BUFFER_LIMIT_FOR_32_BIT;
  222. b->consecutive = consecutive;
  223. err = auxtrace_queues__add_buffer(queues, idx, b);
  224. if (err) {
  225. auxtrace_buffer__free(b);
  226. return err;
  227. }
  228. buffer->data_offset += BUFFER_LIMIT_FOR_32_BIT;
  229. sz -= BUFFER_LIMIT_FOR_32_BIT;
  230. consecutive = true;
  231. }
  232. buffer->size = sz;
  233. buffer->consecutive = consecutive;
  234. return 0;
  235. }
  236. static int auxtrace_queues__add_event_buffer(struct auxtrace_queues *queues,
  237. struct perf_session *session,
  238. unsigned int idx,
  239. struct auxtrace_buffer *buffer)
  240. {
  241. if (session->one_mmap) {
  242. buffer->data = buffer->data_offset - session->one_mmap_offset +
  243. session->one_mmap_addr;
  244. } else if (perf_data_file__is_pipe(session->file)) {
  245. buffer->data = auxtrace_copy_data(buffer->size, session);
  246. if (!buffer->data)
  247. return -ENOMEM;
  248. buffer->data_needs_freeing = true;
  249. } else if (BITS_PER_LONG == 32 &&
  250. buffer->size > BUFFER_LIMIT_FOR_32_BIT) {
  251. int err;
  252. err = auxtrace_queues__split_buffer(queues, idx, buffer);
  253. if (err)
  254. return err;
  255. }
  256. return auxtrace_queues__add_buffer(queues, idx, buffer);
  257. }
  258. int auxtrace_queues__add_event(struct auxtrace_queues *queues,
  259. struct perf_session *session,
  260. union perf_event *event, off_t data_offset,
  261. struct auxtrace_buffer **buffer_ptr)
  262. {
  263. struct auxtrace_buffer *buffer;
  264. unsigned int idx;
  265. int err;
  266. buffer = zalloc(sizeof(struct auxtrace_buffer));
  267. if (!buffer)
  268. return -ENOMEM;
  269. buffer->pid = -1;
  270. buffer->tid = event->auxtrace.tid;
  271. buffer->cpu = event->auxtrace.cpu;
  272. buffer->data_offset = data_offset;
  273. buffer->offset = event->auxtrace.offset;
  274. buffer->reference = event->auxtrace.reference;
  275. buffer->size = event->auxtrace.size;
  276. idx = event->auxtrace.idx;
  277. err = auxtrace_queues__add_event_buffer(queues, session, idx, buffer);
  278. if (err)
  279. goto out_err;
  280. if (buffer_ptr)
  281. *buffer_ptr = buffer;
  282. return 0;
  283. out_err:
  284. auxtrace_buffer__free(buffer);
  285. return err;
  286. }
  287. static int auxtrace_queues__add_indexed_event(struct auxtrace_queues *queues,
  288. struct perf_session *session,
  289. off_t file_offset, size_t sz)
  290. {
  291. union perf_event *event;
  292. int err;
  293. char buf[PERF_SAMPLE_MAX_SIZE];
  294. err = perf_session__peek_event(session, file_offset, buf,
  295. PERF_SAMPLE_MAX_SIZE, &event, NULL);
  296. if (err)
  297. return err;
  298. if (event->header.type == PERF_RECORD_AUXTRACE) {
  299. if (event->header.size < sizeof(struct auxtrace_event) ||
  300. event->header.size != sz) {
  301. err = -EINVAL;
  302. goto out;
  303. }
  304. file_offset += event->header.size;
  305. err = auxtrace_queues__add_event(queues, session, event,
  306. file_offset, NULL);
  307. }
  308. out:
  309. return err;
  310. }
  311. void auxtrace_queues__free(struct auxtrace_queues *queues)
  312. {
  313. unsigned int i;
  314. for (i = 0; i < queues->nr_queues; i++) {
  315. while (!list_empty(&queues->queue_array[i].head)) {
  316. struct auxtrace_buffer *buffer;
  317. buffer = list_entry(queues->queue_array[i].head.next,
  318. struct auxtrace_buffer, list);
  319. list_del(&buffer->list);
  320. auxtrace_buffer__free(buffer);
  321. }
  322. }
  323. zfree(&queues->queue_array);
  324. queues->nr_queues = 0;
  325. }
  326. static void auxtrace_heapify(struct auxtrace_heap_item *heap_array,
  327. unsigned int pos, unsigned int queue_nr,
  328. u64 ordinal)
  329. {
  330. unsigned int parent;
  331. while (pos) {
  332. parent = (pos - 1) >> 1;
  333. if (heap_array[parent].ordinal <= ordinal)
  334. break;
  335. heap_array[pos] = heap_array[parent];
  336. pos = parent;
  337. }
  338. heap_array[pos].queue_nr = queue_nr;
  339. heap_array[pos].ordinal = ordinal;
  340. }
  341. int auxtrace_heap__add(struct auxtrace_heap *heap, unsigned int queue_nr,
  342. u64 ordinal)
  343. {
  344. struct auxtrace_heap_item *heap_array;
  345. if (queue_nr >= heap->heap_sz) {
  346. unsigned int heap_sz = AUXTRACE_INIT_NR_QUEUES;
  347. while (heap_sz <= queue_nr)
  348. heap_sz <<= 1;
  349. heap_array = realloc(heap->heap_array,
  350. heap_sz * sizeof(struct auxtrace_heap_item));
  351. if (!heap_array)
  352. return -ENOMEM;
  353. heap->heap_array = heap_array;
  354. heap->heap_sz = heap_sz;
  355. }
  356. auxtrace_heapify(heap->heap_array, heap->heap_cnt++, queue_nr, ordinal);
  357. return 0;
  358. }
  359. void auxtrace_heap__free(struct auxtrace_heap *heap)
  360. {
  361. zfree(&heap->heap_array);
  362. heap->heap_cnt = 0;
  363. heap->heap_sz = 0;
  364. }
  365. void auxtrace_heap__pop(struct auxtrace_heap *heap)
  366. {
  367. unsigned int pos, last, heap_cnt = heap->heap_cnt;
  368. struct auxtrace_heap_item *heap_array;
  369. if (!heap_cnt)
  370. return;
  371. heap->heap_cnt -= 1;
  372. heap_array = heap->heap_array;
  373. pos = 0;
  374. while (1) {
  375. unsigned int left, right;
  376. left = (pos << 1) + 1;
  377. if (left >= heap_cnt)
  378. break;
  379. right = left + 1;
  380. if (right >= heap_cnt) {
  381. heap_array[pos] = heap_array[left];
  382. return;
  383. }
  384. if (heap_array[left].ordinal < heap_array[right].ordinal) {
  385. heap_array[pos] = heap_array[left];
  386. pos = left;
  387. } else {
  388. heap_array[pos] = heap_array[right];
  389. pos = right;
  390. }
  391. }
  392. last = heap_cnt - 1;
  393. auxtrace_heapify(heap_array, pos, heap_array[last].queue_nr,
  394. heap_array[last].ordinal);
  395. }
  396. size_t auxtrace_record__info_priv_size(struct auxtrace_record *itr)
  397. {
  398. if (itr)
  399. return itr->info_priv_size(itr);
  400. return 0;
  401. }
  402. static int auxtrace_not_supported(void)
  403. {
  404. pr_err("AUX area tracing is not supported on this architecture\n");
  405. return -EINVAL;
  406. }
  407. int auxtrace_record__info_fill(struct auxtrace_record *itr,
  408. struct perf_session *session,
  409. struct auxtrace_info_event *auxtrace_info,
  410. size_t priv_size)
  411. {
  412. if (itr)
  413. return itr->info_fill(itr, session, auxtrace_info, priv_size);
  414. return auxtrace_not_supported();
  415. }
  416. void auxtrace_record__free(struct auxtrace_record *itr)
  417. {
  418. if (itr)
  419. itr->free(itr);
  420. }
  421. int auxtrace_record__snapshot_start(struct auxtrace_record *itr)
  422. {
  423. if (itr && itr->snapshot_start)
  424. return itr->snapshot_start(itr);
  425. return 0;
  426. }
  427. int auxtrace_record__snapshot_finish(struct auxtrace_record *itr)
  428. {
  429. if (itr && itr->snapshot_finish)
  430. return itr->snapshot_finish(itr);
  431. return 0;
  432. }
  433. int auxtrace_record__find_snapshot(struct auxtrace_record *itr, int idx,
  434. struct auxtrace_mmap *mm,
  435. unsigned char *data, u64 *head, u64 *old)
  436. {
  437. if (itr && itr->find_snapshot)
  438. return itr->find_snapshot(itr, idx, mm, data, head, old);
  439. return 0;
  440. }
  441. int auxtrace_record__options(struct auxtrace_record *itr,
  442. struct perf_evlist *evlist,
  443. struct record_opts *opts)
  444. {
  445. if (itr)
  446. return itr->recording_options(itr, evlist, opts);
  447. return 0;
  448. }
  449. u64 auxtrace_record__reference(struct auxtrace_record *itr)
  450. {
  451. if (itr)
  452. return itr->reference(itr);
  453. return 0;
  454. }
  455. int auxtrace_parse_snapshot_options(struct auxtrace_record *itr,
  456. struct record_opts *opts, const char *str)
  457. {
  458. if (!str)
  459. return 0;
  460. if (itr)
  461. return itr->parse_snapshot_options(itr, opts, str);
  462. pr_err("No AUX area tracing to snapshot\n");
  463. return -EINVAL;
  464. }
  465. struct auxtrace_record *__weak
  466. auxtrace_record__init(struct perf_evlist *evlist __maybe_unused, int *err)
  467. {
  468. *err = 0;
  469. return NULL;
  470. }
  471. static int auxtrace_index__alloc(struct list_head *head)
  472. {
  473. struct auxtrace_index *auxtrace_index;
  474. auxtrace_index = malloc(sizeof(struct auxtrace_index));
  475. if (!auxtrace_index)
  476. return -ENOMEM;
  477. auxtrace_index->nr = 0;
  478. INIT_LIST_HEAD(&auxtrace_index->list);
  479. list_add_tail(&auxtrace_index->list, head);
  480. return 0;
  481. }
  482. void auxtrace_index__free(struct list_head *head)
  483. {
  484. struct auxtrace_index *auxtrace_index, *n;
  485. list_for_each_entry_safe(auxtrace_index, n, head, list) {
  486. list_del(&auxtrace_index->list);
  487. free(auxtrace_index);
  488. }
  489. }
  490. static struct auxtrace_index *auxtrace_index__last(struct list_head *head)
  491. {
  492. struct auxtrace_index *auxtrace_index;
  493. int err;
  494. if (list_empty(head)) {
  495. err = auxtrace_index__alloc(head);
  496. if (err)
  497. return NULL;
  498. }
  499. auxtrace_index = list_entry(head->prev, struct auxtrace_index, list);
  500. if (auxtrace_index->nr >= PERF_AUXTRACE_INDEX_ENTRY_COUNT) {
  501. err = auxtrace_index__alloc(head);
  502. if (err)
  503. return NULL;
  504. auxtrace_index = list_entry(head->prev, struct auxtrace_index,
  505. list);
  506. }
  507. return auxtrace_index;
  508. }
  509. int auxtrace_index__auxtrace_event(struct list_head *head,
  510. union perf_event *event, off_t file_offset)
  511. {
  512. struct auxtrace_index *auxtrace_index;
  513. size_t nr;
  514. auxtrace_index = auxtrace_index__last(head);
  515. if (!auxtrace_index)
  516. return -ENOMEM;
  517. nr = auxtrace_index->nr;
  518. auxtrace_index->entries[nr].file_offset = file_offset;
  519. auxtrace_index->entries[nr].sz = event->header.size;
  520. auxtrace_index->nr += 1;
  521. return 0;
  522. }
  523. static int auxtrace_index__do_write(int fd,
  524. struct auxtrace_index *auxtrace_index)
  525. {
  526. struct auxtrace_index_entry ent;
  527. size_t i;
  528. for (i = 0; i < auxtrace_index->nr; i++) {
  529. ent.file_offset = auxtrace_index->entries[i].file_offset;
  530. ent.sz = auxtrace_index->entries[i].sz;
  531. if (writen(fd, &ent, sizeof(ent)) != sizeof(ent))
  532. return -errno;
  533. }
  534. return 0;
  535. }
  536. int auxtrace_index__write(int fd, struct list_head *head)
  537. {
  538. struct auxtrace_index *auxtrace_index;
  539. u64 total = 0;
  540. int err;
  541. list_for_each_entry(auxtrace_index, head, list)
  542. total += auxtrace_index->nr;
  543. if (writen(fd, &total, sizeof(total)) != sizeof(total))
  544. return -errno;
  545. list_for_each_entry(auxtrace_index, head, list) {
  546. err = auxtrace_index__do_write(fd, auxtrace_index);
  547. if (err)
  548. return err;
  549. }
  550. return 0;
  551. }
  552. static int auxtrace_index__process_entry(int fd, struct list_head *head,
  553. bool needs_swap)
  554. {
  555. struct auxtrace_index *auxtrace_index;
  556. struct auxtrace_index_entry ent;
  557. size_t nr;
  558. if (readn(fd, &ent, sizeof(ent)) != sizeof(ent))
  559. return -1;
  560. auxtrace_index = auxtrace_index__last(head);
  561. if (!auxtrace_index)
  562. return -1;
  563. nr = auxtrace_index->nr;
  564. if (needs_swap) {
  565. auxtrace_index->entries[nr].file_offset =
  566. bswap_64(ent.file_offset);
  567. auxtrace_index->entries[nr].sz = bswap_64(ent.sz);
  568. } else {
  569. auxtrace_index->entries[nr].file_offset = ent.file_offset;
  570. auxtrace_index->entries[nr].sz = ent.sz;
  571. }
  572. auxtrace_index->nr = nr + 1;
  573. return 0;
  574. }
  575. int auxtrace_index__process(int fd, u64 size, struct perf_session *session,
  576. bool needs_swap)
  577. {
  578. struct list_head *head = &session->auxtrace_index;
  579. u64 nr;
  580. if (readn(fd, &nr, sizeof(u64)) != sizeof(u64))
  581. return -1;
  582. if (needs_swap)
  583. nr = bswap_64(nr);
  584. if (sizeof(u64) + nr * sizeof(struct auxtrace_index_entry) > size)
  585. return -1;
  586. while (nr--) {
  587. int err;
  588. err = auxtrace_index__process_entry(fd, head, needs_swap);
  589. if (err)
  590. return -1;
  591. }
  592. return 0;
  593. }
  594. static int auxtrace_queues__process_index_entry(struct auxtrace_queues *queues,
  595. struct perf_session *session,
  596. struct auxtrace_index_entry *ent)
  597. {
  598. return auxtrace_queues__add_indexed_event(queues, session,
  599. ent->file_offset, ent->sz);
  600. }
  601. int auxtrace_queues__process_index(struct auxtrace_queues *queues,
  602. struct perf_session *session)
  603. {
  604. struct auxtrace_index *auxtrace_index;
  605. struct auxtrace_index_entry *ent;
  606. size_t i;
  607. int err;
  608. list_for_each_entry(auxtrace_index, &session->auxtrace_index, list) {
  609. for (i = 0; i < auxtrace_index->nr; i++) {
  610. ent = &auxtrace_index->entries[i];
  611. err = auxtrace_queues__process_index_entry(queues,
  612. session,
  613. ent);
  614. if (err)
  615. return err;
  616. }
  617. }
  618. return 0;
  619. }
  620. struct auxtrace_buffer *auxtrace_buffer__next(struct auxtrace_queue *queue,
  621. struct auxtrace_buffer *buffer)
  622. {
  623. if (buffer) {
  624. if (list_is_last(&buffer->list, &queue->head))
  625. return NULL;
  626. return list_entry(buffer->list.next, struct auxtrace_buffer,
  627. list);
  628. } else {
  629. if (list_empty(&queue->head))
  630. return NULL;
  631. return list_entry(queue->head.next, struct auxtrace_buffer,
  632. list);
  633. }
  634. }
  635. void *auxtrace_buffer__get_data(struct auxtrace_buffer *buffer, int fd)
  636. {
  637. size_t adj = buffer->data_offset & (page_size - 1);
  638. size_t size = buffer->size + adj;
  639. off_t file_offset = buffer->data_offset - adj;
  640. void *addr;
  641. if (buffer->data)
  642. return buffer->data;
  643. addr = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, file_offset);
  644. if (addr == MAP_FAILED)
  645. return NULL;
  646. buffer->mmap_addr = addr;
  647. buffer->mmap_size = size;
  648. buffer->data = addr + adj;
  649. return buffer->data;
  650. }
  651. void auxtrace_buffer__put_data(struct auxtrace_buffer *buffer)
  652. {
  653. if (!buffer->data || !buffer->mmap_addr)
  654. return;
  655. munmap(buffer->mmap_addr, buffer->mmap_size);
  656. buffer->mmap_addr = NULL;
  657. buffer->mmap_size = 0;
  658. buffer->data = NULL;
  659. buffer->use_data = NULL;
  660. }
  661. void auxtrace_buffer__drop_data(struct auxtrace_buffer *buffer)
  662. {
  663. auxtrace_buffer__put_data(buffer);
  664. if (buffer->data_needs_freeing) {
  665. buffer->data_needs_freeing = false;
  666. zfree(&buffer->data);
  667. buffer->use_data = NULL;
  668. buffer->size = 0;
  669. }
  670. }
  671. void auxtrace_buffer__free(struct auxtrace_buffer *buffer)
  672. {
  673. auxtrace_buffer__drop_data(buffer);
  674. free(buffer);
  675. }
  676. void auxtrace_synth_error(struct auxtrace_error_event *auxtrace_error, int type,
  677. int code, int cpu, pid_t pid, pid_t tid, u64 ip,
  678. const char *msg)
  679. {
  680. size_t size;
  681. memset(auxtrace_error, 0, sizeof(struct auxtrace_error_event));
  682. auxtrace_error->header.type = PERF_RECORD_AUXTRACE_ERROR;
  683. auxtrace_error->type = type;
  684. auxtrace_error->code = code;
  685. auxtrace_error->cpu = cpu;
  686. auxtrace_error->pid = pid;
  687. auxtrace_error->tid = tid;
  688. auxtrace_error->ip = ip;
  689. strlcpy(auxtrace_error->msg, msg, MAX_AUXTRACE_ERROR_MSG);
  690. size = (void *)auxtrace_error->msg - (void *)auxtrace_error +
  691. strlen(auxtrace_error->msg) + 1;
  692. auxtrace_error->header.size = PERF_ALIGN(size, sizeof(u64));
  693. }
  694. int perf_event__synthesize_auxtrace_info(struct auxtrace_record *itr,
  695. struct perf_tool *tool,
  696. struct perf_session *session,
  697. perf_event__handler_t process)
  698. {
  699. union perf_event *ev;
  700. size_t priv_size;
  701. int err;
  702. pr_debug2("Synthesizing auxtrace information\n");
  703. priv_size = auxtrace_record__info_priv_size(itr);
  704. ev = zalloc(sizeof(struct auxtrace_info_event) + priv_size);
  705. if (!ev)
  706. return -ENOMEM;
  707. ev->auxtrace_info.header.type = PERF_RECORD_AUXTRACE_INFO;
  708. ev->auxtrace_info.header.size = sizeof(struct auxtrace_info_event) +
  709. priv_size;
  710. err = auxtrace_record__info_fill(itr, session, &ev->auxtrace_info,
  711. priv_size);
  712. if (err)
  713. goto out_free;
  714. err = process(tool, ev, NULL, NULL);
  715. out_free:
  716. free(ev);
  717. return err;
  718. }
  719. static bool auxtrace__dont_decode(struct perf_session *session)
  720. {
  721. return !session->itrace_synth_opts ||
  722. session->itrace_synth_opts->dont_decode;
  723. }
  724. int perf_event__process_auxtrace_info(struct perf_tool *tool __maybe_unused,
  725. union perf_event *event,
  726. struct perf_session *session)
  727. {
  728. enum auxtrace_type type = event->auxtrace_info.type;
  729. if (dump_trace)
  730. fprintf(stdout, " type: %u\n", type);
  731. switch (type) {
  732. case PERF_AUXTRACE_INTEL_PT:
  733. return intel_pt_process_auxtrace_info(event, session);
  734. case PERF_AUXTRACE_INTEL_BTS:
  735. return intel_bts_process_auxtrace_info(event, session);
  736. case PERF_AUXTRACE_UNKNOWN:
  737. default:
  738. return -EINVAL;
  739. }
  740. }
  741. s64 perf_event__process_auxtrace(struct perf_tool *tool,
  742. union perf_event *event,
  743. struct perf_session *session)
  744. {
  745. s64 err;
  746. if (dump_trace)
  747. fprintf(stdout, " size: %#"PRIx64" offset: %#"PRIx64" ref: %#"PRIx64" idx: %u tid: %d cpu: %d\n",
  748. event->auxtrace.size, event->auxtrace.offset,
  749. event->auxtrace.reference, event->auxtrace.idx,
  750. event->auxtrace.tid, event->auxtrace.cpu);
  751. if (auxtrace__dont_decode(session))
  752. return event->auxtrace.size;
  753. if (!session->auxtrace || event->header.type != PERF_RECORD_AUXTRACE)
  754. return -EINVAL;
  755. err = session->auxtrace->process_auxtrace_event(session, event, tool);
  756. if (err < 0)
  757. return err;
  758. return event->auxtrace.size;
  759. }
  760. #define PERF_ITRACE_DEFAULT_PERIOD_TYPE PERF_ITRACE_PERIOD_NANOSECS
  761. #define PERF_ITRACE_DEFAULT_PERIOD 100000
  762. #define PERF_ITRACE_DEFAULT_CALLCHAIN_SZ 16
  763. #define PERF_ITRACE_MAX_CALLCHAIN_SZ 1024
  764. void itrace_synth_opts__set_default(struct itrace_synth_opts *synth_opts)
  765. {
  766. synth_opts->instructions = true;
  767. synth_opts->branches = true;
  768. synth_opts->transactions = true;
  769. synth_opts->errors = true;
  770. synth_opts->period_type = PERF_ITRACE_DEFAULT_PERIOD_TYPE;
  771. synth_opts->period = PERF_ITRACE_DEFAULT_PERIOD;
  772. synth_opts->callchain_sz = PERF_ITRACE_DEFAULT_CALLCHAIN_SZ;
  773. }
  774. /*
  775. * Please check tools/perf/Documentation/perf-script.txt for information
  776. * about the options parsed here, which is introduced after this cset,
  777. * when support in 'perf script' for these options is introduced.
  778. */
  779. int itrace_parse_synth_opts(const struct option *opt, const char *str,
  780. int unset)
  781. {
  782. struct itrace_synth_opts *synth_opts = opt->value;
  783. const char *p;
  784. char *endptr;
  785. bool period_type_set = false;
  786. synth_opts->set = true;
  787. if (unset) {
  788. synth_opts->dont_decode = true;
  789. return 0;
  790. }
  791. if (!str) {
  792. itrace_synth_opts__set_default(synth_opts);
  793. return 0;
  794. }
  795. for (p = str; *p;) {
  796. switch (*p++) {
  797. case 'i':
  798. synth_opts->instructions = true;
  799. while (*p == ' ' || *p == ',')
  800. p += 1;
  801. if (isdigit(*p)) {
  802. synth_opts->period = strtoull(p, &endptr, 10);
  803. p = endptr;
  804. while (*p == ' ' || *p == ',')
  805. p += 1;
  806. switch (*p++) {
  807. case 'i':
  808. synth_opts->period_type =
  809. PERF_ITRACE_PERIOD_INSTRUCTIONS;
  810. period_type_set = true;
  811. break;
  812. case 't':
  813. synth_opts->period_type =
  814. PERF_ITRACE_PERIOD_TICKS;
  815. period_type_set = true;
  816. break;
  817. case 'm':
  818. synth_opts->period *= 1000;
  819. /* Fall through */
  820. case 'u':
  821. synth_opts->period *= 1000;
  822. /* Fall through */
  823. case 'n':
  824. if (*p++ != 's')
  825. goto out_err;
  826. synth_opts->period_type =
  827. PERF_ITRACE_PERIOD_NANOSECS;
  828. period_type_set = true;
  829. break;
  830. case '\0':
  831. goto out;
  832. default:
  833. goto out_err;
  834. }
  835. }
  836. break;
  837. case 'b':
  838. synth_opts->branches = true;
  839. break;
  840. case 'x':
  841. synth_opts->transactions = true;
  842. break;
  843. case 'e':
  844. synth_opts->errors = true;
  845. break;
  846. case 'd':
  847. synth_opts->log = true;
  848. break;
  849. case 'c':
  850. synth_opts->branches = true;
  851. synth_opts->calls = true;
  852. break;
  853. case 'r':
  854. synth_opts->branches = true;
  855. synth_opts->returns = true;
  856. break;
  857. case 'g':
  858. synth_opts->callchain = true;
  859. synth_opts->callchain_sz =
  860. PERF_ITRACE_DEFAULT_CALLCHAIN_SZ;
  861. while (*p == ' ' || *p == ',')
  862. p += 1;
  863. if (isdigit(*p)) {
  864. unsigned int val;
  865. val = strtoul(p, &endptr, 10);
  866. p = endptr;
  867. if (!val || val > PERF_ITRACE_MAX_CALLCHAIN_SZ)
  868. goto out_err;
  869. synth_opts->callchain_sz = val;
  870. }
  871. break;
  872. case ' ':
  873. case ',':
  874. break;
  875. default:
  876. goto out_err;
  877. }
  878. }
  879. out:
  880. if (synth_opts->instructions) {
  881. if (!period_type_set)
  882. synth_opts->period_type =
  883. PERF_ITRACE_DEFAULT_PERIOD_TYPE;
  884. if (!synth_opts->period)
  885. synth_opts->period = PERF_ITRACE_DEFAULT_PERIOD;
  886. }
  887. return 0;
  888. out_err:
  889. pr_err("Bad Instruction Tracing options '%s'\n", str);
  890. return -EINVAL;
  891. }
  892. static const char * const auxtrace_error_type_name[] = {
  893. [PERF_AUXTRACE_ERROR_ITRACE] = "instruction trace",
  894. };
  895. static const char *auxtrace_error_name(int type)
  896. {
  897. const char *error_type_name = NULL;
  898. if (type < PERF_AUXTRACE_ERROR_MAX)
  899. error_type_name = auxtrace_error_type_name[type];
  900. if (!error_type_name)
  901. error_type_name = "unknown AUX";
  902. return error_type_name;
  903. }
  904. size_t perf_event__fprintf_auxtrace_error(union perf_event *event, FILE *fp)
  905. {
  906. struct auxtrace_error_event *e = &event->auxtrace_error;
  907. int ret;
  908. ret = fprintf(fp, " %s error type %u",
  909. auxtrace_error_name(e->type), e->type);
  910. ret += fprintf(fp, " cpu %d pid %d tid %d ip %#"PRIx64" code %u: %s\n",
  911. e->cpu, e->pid, e->tid, e->ip, e->code, e->msg);
  912. return ret;
  913. }
  914. void perf_session__auxtrace_error_inc(struct perf_session *session,
  915. union perf_event *event)
  916. {
  917. struct auxtrace_error_event *e = &event->auxtrace_error;
  918. if (e->type < PERF_AUXTRACE_ERROR_MAX)
  919. session->evlist->stats.nr_auxtrace_errors[e->type] += 1;
  920. }
  921. void events_stats__auxtrace_error_warn(const struct events_stats *stats)
  922. {
  923. int i;
  924. for (i = 0; i < PERF_AUXTRACE_ERROR_MAX; i++) {
  925. if (!stats->nr_auxtrace_errors[i])
  926. continue;
  927. ui__warning("%u %s errors\n",
  928. stats->nr_auxtrace_errors[i],
  929. auxtrace_error_name(i));
  930. }
  931. }
  932. int perf_event__process_auxtrace_error(struct perf_tool *tool __maybe_unused,
  933. union perf_event *event,
  934. struct perf_session *session)
  935. {
  936. if (auxtrace__dont_decode(session))
  937. return 0;
  938. perf_event__fprintf_auxtrace_error(event, stdout);
  939. return 0;
  940. }
  941. static int __auxtrace_mmap__read(struct auxtrace_mmap *mm,
  942. struct auxtrace_record *itr,
  943. struct perf_tool *tool, process_auxtrace_t fn,
  944. bool snapshot, size_t snapshot_size)
  945. {
  946. u64 head, old = mm->prev, offset, ref;
  947. unsigned char *data = mm->base;
  948. size_t size, head_off, old_off, len1, len2, padding;
  949. union perf_event ev;
  950. void *data1, *data2;
  951. if (snapshot) {
  952. head = auxtrace_mmap__read_snapshot_head(mm);
  953. if (auxtrace_record__find_snapshot(itr, mm->idx, mm, data,
  954. &head, &old))
  955. return -1;
  956. } else {
  957. head = auxtrace_mmap__read_head(mm);
  958. }
  959. if (old == head)
  960. return 0;
  961. pr_debug3("auxtrace idx %d old %#"PRIx64" head %#"PRIx64" diff %#"PRIx64"\n",
  962. mm->idx, old, head, head - old);
  963. if (mm->mask) {
  964. head_off = head & mm->mask;
  965. old_off = old & mm->mask;
  966. } else {
  967. head_off = head % mm->len;
  968. old_off = old % mm->len;
  969. }
  970. if (head_off > old_off)
  971. size = head_off - old_off;
  972. else
  973. size = mm->len - (old_off - head_off);
  974. if (snapshot && size > snapshot_size)
  975. size = snapshot_size;
  976. ref = auxtrace_record__reference(itr);
  977. if (head > old || size <= head || mm->mask) {
  978. offset = head - size;
  979. } else {
  980. /*
  981. * When the buffer size is not a power of 2, 'head' wraps at the
  982. * highest multiple of the buffer size, so we have to subtract
  983. * the remainder here.
  984. */
  985. u64 rem = (0ULL - mm->len) % mm->len;
  986. offset = head - size - rem;
  987. }
  988. if (size > head_off) {
  989. len1 = size - head_off;
  990. data1 = &data[mm->len - len1];
  991. len2 = head_off;
  992. data2 = &data[0];
  993. } else {
  994. len1 = size;
  995. data1 = &data[head_off - len1];
  996. len2 = 0;
  997. data2 = NULL;
  998. }
  999. if (itr->alignment) {
  1000. unsigned int unwanted = len1 % itr->alignment;
  1001. len1 -= unwanted;
  1002. size -= unwanted;
  1003. }
  1004. /* padding must be written by fn() e.g. record__process_auxtrace() */
  1005. padding = size & 7;
  1006. if (padding)
  1007. padding = 8 - padding;
  1008. memset(&ev, 0, sizeof(ev));
  1009. ev.auxtrace.header.type = PERF_RECORD_AUXTRACE;
  1010. ev.auxtrace.header.size = sizeof(ev.auxtrace);
  1011. ev.auxtrace.size = size + padding;
  1012. ev.auxtrace.offset = offset;
  1013. ev.auxtrace.reference = ref;
  1014. ev.auxtrace.idx = mm->idx;
  1015. ev.auxtrace.tid = mm->tid;
  1016. ev.auxtrace.cpu = mm->cpu;
  1017. if (fn(tool, &ev, data1, len1, data2, len2))
  1018. return -1;
  1019. mm->prev = head;
  1020. if (!snapshot) {
  1021. auxtrace_mmap__write_tail(mm, head);
  1022. if (itr->read_finish) {
  1023. int err;
  1024. err = itr->read_finish(itr, mm->idx);
  1025. if (err < 0)
  1026. return err;
  1027. }
  1028. }
  1029. return 1;
  1030. }
  1031. int auxtrace_mmap__read(struct auxtrace_mmap *mm, struct auxtrace_record *itr,
  1032. struct perf_tool *tool, process_auxtrace_t fn)
  1033. {
  1034. return __auxtrace_mmap__read(mm, itr, tool, fn, false, 0);
  1035. }
  1036. int auxtrace_mmap__read_snapshot(struct auxtrace_mmap *mm,
  1037. struct auxtrace_record *itr,
  1038. struct perf_tool *tool, process_auxtrace_t fn,
  1039. size_t snapshot_size)
  1040. {
  1041. return __auxtrace_mmap__read(mm, itr, tool, fn, true, snapshot_size);
  1042. }
  1043. /**
  1044. * struct auxtrace_cache - hash table to implement a cache
  1045. * @hashtable: the hashtable
  1046. * @sz: hashtable size (number of hlists)
  1047. * @entry_size: size of an entry
  1048. * @limit: limit the number of entries to this maximum, when reached the cache
  1049. * is dropped and caching begins again with an empty cache
  1050. * @cnt: current number of entries
  1051. * @bits: hashtable size (@sz = 2^@bits)
  1052. */
  1053. struct auxtrace_cache {
  1054. struct hlist_head *hashtable;
  1055. size_t sz;
  1056. size_t entry_size;
  1057. size_t limit;
  1058. size_t cnt;
  1059. unsigned int bits;
  1060. };
  1061. struct auxtrace_cache *auxtrace_cache__new(unsigned int bits, size_t entry_size,
  1062. unsigned int limit_percent)
  1063. {
  1064. struct auxtrace_cache *c;
  1065. struct hlist_head *ht;
  1066. size_t sz, i;
  1067. c = zalloc(sizeof(struct auxtrace_cache));
  1068. if (!c)
  1069. return NULL;
  1070. sz = 1UL << bits;
  1071. ht = calloc(sz, sizeof(struct hlist_head));
  1072. if (!ht)
  1073. goto out_free;
  1074. for (i = 0; i < sz; i++)
  1075. INIT_HLIST_HEAD(&ht[i]);
  1076. c->hashtable = ht;
  1077. c->sz = sz;
  1078. c->entry_size = entry_size;
  1079. c->limit = (c->sz * limit_percent) / 100;
  1080. c->bits = bits;
  1081. return c;
  1082. out_free:
  1083. free(c);
  1084. return NULL;
  1085. }
  1086. static void auxtrace_cache__drop(struct auxtrace_cache *c)
  1087. {
  1088. struct auxtrace_cache_entry *entry;
  1089. struct hlist_node *tmp;
  1090. size_t i;
  1091. if (!c)
  1092. return;
  1093. for (i = 0; i < c->sz; i++) {
  1094. hlist_for_each_entry_safe(entry, tmp, &c->hashtable[i], hash) {
  1095. hlist_del(&entry->hash);
  1096. auxtrace_cache__free_entry(c, entry);
  1097. }
  1098. }
  1099. c->cnt = 0;
  1100. }
  1101. void auxtrace_cache__free(struct auxtrace_cache *c)
  1102. {
  1103. if (!c)
  1104. return;
  1105. auxtrace_cache__drop(c);
  1106. free(c->hashtable);
  1107. free(c);
  1108. }
  1109. void *auxtrace_cache__alloc_entry(struct auxtrace_cache *c)
  1110. {
  1111. return malloc(c->entry_size);
  1112. }
  1113. void auxtrace_cache__free_entry(struct auxtrace_cache *c __maybe_unused,
  1114. void *entry)
  1115. {
  1116. free(entry);
  1117. }
  1118. int auxtrace_cache__add(struct auxtrace_cache *c, u32 key,
  1119. struct auxtrace_cache_entry *entry)
  1120. {
  1121. if (c->limit && ++c->cnt > c->limit)
  1122. auxtrace_cache__drop(c);
  1123. entry->key = key;
  1124. hlist_add_head(&entry->hash, &c->hashtable[hash_32(key, c->bits)]);
  1125. return 0;
  1126. }
  1127. void *auxtrace_cache__lookup(struct auxtrace_cache *c, u32 key)
  1128. {
  1129. struct auxtrace_cache_entry *entry;
  1130. struct hlist_head *hlist;
  1131. if (!c)
  1132. return NULL;
  1133. hlist = &c->hashtable[hash_32(key, c->bits)];
  1134. hlist_for_each_entry(entry, hlist, hash) {
  1135. if (entry->key == key)
  1136. return entry;
  1137. }
  1138. return NULL;
  1139. }