InstrProfilingBuffer.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*===- InstrProfilingBuffer.c - Write instrumentation to a memory buffer --===*\
  2. |*
  3. |* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. |* See https://llvm.org/LICENSE.txt for license information.
  5. |* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. |*
  7. \*===----------------------------------------------------------------------===*/
  8. // Note: This is linked into the Darwin kernel, and must remain compatible
  9. // with freestanding compilation. See `darwin_add_builtin_libraries`.
  10. #include "InstrProfiling.h"
  11. #include "InstrProfilingInternal.h"
  12. #include "InstrProfilingPort.h"
  13. /* When continuous mode is enabled (%c), this parameter is set to 1.
  14. *
  15. * This parameter is defined here in InstrProfilingBuffer.o, instead of in
  16. * InstrProfilingFile.o, to sequester all libc-dependent code in
  17. * InstrProfilingFile.o. The test `instrprof-without-libc` will break if this
  18. * layering is violated. */
  19. static int ContinuouslySyncProfile = 0;
  20. /* The system page size. Only valid when non-zero. If 0, the page size is
  21. * unavailable. */
  22. static unsigned PageSize = 0;
  23. COMPILER_RT_VISIBILITY int __llvm_profile_is_continuous_mode_enabled(void) {
  24. return ContinuouslySyncProfile && PageSize;
  25. }
  26. COMPILER_RT_VISIBILITY void __llvm_profile_enable_continuous_mode(void) {
  27. ContinuouslySyncProfile = 1;
  28. }
  29. COMPILER_RT_VISIBILITY void __llvm_profile_disable_continuous_mode(void) {
  30. ContinuouslySyncProfile = 0;
  31. }
  32. COMPILER_RT_VISIBILITY void __llvm_profile_set_page_size(unsigned PS) {
  33. PageSize = PS;
  34. }
  35. COMPILER_RT_VISIBILITY
  36. uint64_t __llvm_profile_get_size_for_buffer(void) {
  37. const __llvm_profile_data *DataBegin = __llvm_profile_begin_data();
  38. const __llvm_profile_data *DataEnd = __llvm_profile_end_data();
  39. const char *CountersBegin = __llvm_profile_begin_counters();
  40. const char *CountersEnd = __llvm_profile_end_counters();
  41. const char *BitmapBegin = __llvm_profile_begin_bitmap();
  42. const char *BitmapEnd = __llvm_profile_end_bitmap();
  43. const char *NamesBegin = __llvm_profile_begin_names();
  44. const char *NamesEnd = __llvm_profile_end_names();
  45. return __llvm_profile_get_size_for_buffer_internal(
  46. DataBegin, DataEnd, CountersBegin, CountersEnd, BitmapBegin, BitmapEnd,
  47. NamesBegin, NamesEnd);
  48. }
  49. COMPILER_RT_VISIBILITY
  50. uint64_t __llvm_profile_get_num_data(const __llvm_profile_data *Begin,
  51. const __llvm_profile_data *End) {
  52. intptr_t BeginI = (intptr_t)Begin, EndI = (intptr_t)End;
  53. return ((EndI + sizeof(__llvm_profile_data) - 1) - BeginI) /
  54. sizeof(__llvm_profile_data);
  55. }
  56. COMPILER_RT_VISIBILITY
  57. uint64_t __llvm_profile_get_data_size(const __llvm_profile_data *Begin,
  58. const __llvm_profile_data *End) {
  59. return __llvm_profile_get_num_data(Begin, End) * sizeof(__llvm_profile_data);
  60. }
  61. COMPILER_RT_VISIBILITY size_t __llvm_profile_counter_entry_size(void) {
  62. if (__llvm_profile_get_version() & VARIANT_MASK_BYTE_COVERAGE)
  63. return sizeof(uint8_t);
  64. return sizeof(uint64_t);
  65. }
  66. COMPILER_RT_VISIBILITY
  67. uint64_t __llvm_profile_get_num_counters(const char *Begin, const char *End) {
  68. intptr_t BeginI = (intptr_t)Begin, EndI = (intptr_t)End;
  69. return ((EndI + __llvm_profile_counter_entry_size() - 1) - BeginI) /
  70. __llvm_profile_counter_entry_size();
  71. }
  72. COMPILER_RT_VISIBILITY
  73. uint64_t __llvm_profile_get_counters_size(const char *Begin, const char *End) {
  74. return __llvm_profile_get_num_counters(Begin, End) *
  75. __llvm_profile_counter_entry_size();
  76. }
  77. COMPILER_RT_VISIBILITY
  78. uint64_t __llvm_profile_get_num_bitmap_bytes(const char *Begin,
  79. const char *End) {
  80. return (End - Begin);
  81. }
  82. COMPILER_RT_VISIBILITY
  83. uint64_t __llvm_profile_get_name_size(const char *Begin, const char *End) {
  84. return End - Begin;
  85. }
  86. /// Calculate the number of padding bytes needed to add to \p Offset in order
  87. /// for (\p Offset + Padding) to be page-aligned.
  88. static uint64_t calculateBytesNeededToPageAlign(uint64_t Offset) {
  89. uint64_t OffsetModPage = Offset % PageSize;
  90. if (OffsetModPage > 0)
  91. return PageSize - OffsetModPage;
  92. return 0;
  93. }
  94. static int needsCounterPadding(void) {
  95. #if defined(__APPLE__)
  96. return __llvm_profile_is_continuous_mode_enabled();
  97. #else
  98. return 0;
  99. #endif
  100. }
  101. COMPILER_RT_VISIBILITY
  102. void __llvm_profile_get_padding_sizes_for_counters(
  103. uint64_t DataSize, uint64_t CountersSize, uint64_t NumBitmapBytes,
  104. uint64_t NamesSize, uint64_t *PaddingBytesBeforeCounters,
  105. uint64_t *PaddingBytesAfterCounters, uint64_t *PaddingBytesAfterBitmapBytes,
  106. uint64_t *PaddingBytesAfterNames) {
  107. if (!needsCounterPadding()) {
  108. *PaddingBytesBeforeCounters = 0;
  109. *PaddingBytesAfterCounters =
  110. __llvm_profile_get_num_padding_bytes(CountersSize);
  111. *PaddingBytesAfterBitmapBytes =
  112. __llvm_profile_get_num_padding_bytes(NumBitmapBytes);
  113. *PaddingBytesAfterNames = __llvm_profile_get_num_padding_bytes(NamesSize);
  114. return;
  115. }
  116. // In continuous mode, the file offsets for headers and for the start of
  117. // counter sections need to be page-aligned.
  118. *PaddingBytesBeforeCounters =
  119. calculateBytesNeededToPageAlign(sizeof(__llvm_profile_header) + DataSize);
  120. *PaddingBytesAfterCounters = calculateBytesNeededToPageAlign(CountersSize);
  121. *PaddingBytesAfterBitmapBytes =
  122. calculateBytesNeededToPageAlign(NumBitmapBytes);
  123. *PaddingBytesAfterNames = calculateBytesNeededToPageAlign(NamesSize);
  124. }
  125. COMPILER_RT_VISIBILITY
  126. uint64_t __llvm_profile_get_size_for_buffer_internal(
  127. const __llvm_profile_data *DataBegin, const __llvm_profile_data *DataEnd,
  128. const char *CountersBegin, const char *CountersEnd, const char *BitmapBegin,
  129. const char *BitmapEnd, const char *NamesBegin, const char *NamesEnd) {
  130. /* Match logic in __llvm_profile_write_buffer(). */
  131. const uint64_t NamesSize = (NamesEnd - NamesBegin) * sizeof(char);
  132. uint64_t DataSize = __llvm_profile_get_data_size(DataBegin, DataEnd);
  133. uint64_t CountersSize =
  134. __llvm_profile_get_counters_size(CountersBegin, CountersEnd);
  135. const uint64_t NumBitmapBytes =
  136. __llvm_profile_get_num_bitmap_bytes(BitmapBegin, BitmapEnd);
  137. /* Determine how much padding is needed before/after the counters and after
  138. * the names. */
  139. uint64_t PaddingBytesBeforeCounters, PaddingBytesAfterCounters,
  140. PaddingBytesAfterNames, PaddingBytesAfterBitmapBytes;
  141. __llvm_profile_get_padding_sizes_for_counters(
  142. DataSize, CountersSize, NumBitmapBytes, NamesSize,
  143. &PaddingBytesBeforeCounters, &PaddingBytesAfterCounters,
  144. &PaddingBytesAfterBitmapBytes, &PaddingBytesAfterNames);
  145. return sizeof(__llvm_profile_header) + __llvm_write_binary_ids(NULL) +
  146. DataSize + PaddingBytesBeforeCounters + CountersSize +
  147. PaddingBytesAfterCounters + NumBitmapBytes +
  148. PaddingBytesAfterBitmapBytes + NamesSize + PaddingBytesAfterNames;
  149. }
  150. COMPILER_RT_VISIBILITY
  151. void initBufferWriter(ProfDataWriter *BufferWriter, char *Buffer) {
  152. BufferWriter->Write = lprofBufferWriter;
  153. BufferWriter->WriterCtx = Buffer;
  154. }
  155. COMPILER_RT_VISIBILITY int __llvm_profile_write_buffer(char *Buffer) {
  156. ProfDataWriter BufferWriter;
  157. initBufferWriter(&BufferWriter, Buffer);
  158. return lprofWriteData(&BufferWriter, 0, 0);
  159. }
  160. COMPILER_RT_VISIBILITY int __llvm_profile_write_buffer_internal(
  161. char *Buffer, const __llvm_profile_data *DataBegin,
  162. const __llvm_profile_data *DataEnd, const char *CountersBegin,
  163. const char *CountersEnd, const char *BitmapBegin, const char *BitmapEnd,
  164. const char *NamesBegin, const char *NamesEnd) {
  165. ProfDataWriter BufferWriter;
  166. initBufferWriter(&BufferWriter, Buffer);
  167. return lprofWriteDataImpl(&BufferWriter, DataBegin, DataEnd, CountersBegin,
  168. CountersEnd, BitmapBegin, BitmapEnd, 0, NamesBegin,
  169. NamesEnd, 0);
  170. }