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