1 /*===- InstrProfilingWriter.c - Write instrumentation to a file or buffer -===*\
3 |* The LLVM Compiler Infrastructure
5 |* This file is distributed under the University of Illinois Open Source
6 |* License. See LICENSE.TXT for details.
8 \*===----------------------------------------------------------------------===*/
16 #include "InstrProfiling.h"
17 #include "InstrProfilingInternal.h"
19 #define INSTR_PROF_VALUE_PROF_DATA
20 #include "InstrProfData.inc"
22 COMPILER_RT_VISIBILITY void (*FreeHook)(void *) = NULL;
23 static ProfBufferIO TheBufferIO;
24 #define VP_BUFFER_SIZE 8 * 1024
25 static uint8_t BufferIOBuffer[VP_BUFFER_SIZE];
26 static InstrProfValueData VPDataArray[16];
27 static uint32_t VPDataArraySize = sizeof(VPDataArray) / sizeof(*VPDataArray);
29 COMPILER_RT_VISIBILITY uint8_t *DynamicBufferIOBuffer = 0;
30 COMPILER_RT_VISIBILITY uint32_t VPBufferSize = 0;
32 /* The buffer writer is reponsponsible in keeping writer state
35 COMPILER_RT_VISIBILITY uint32_t lprofBufferWriter(ProfDataWriter *This,
36 ProfDataIOVec *IOVecs,
39 char **Buffer = (char **)&This->WriterCtx;
40 for (I = 0; I < NumIOVecs; I++) {
41 size_t Length = IOVecs[I].ElmSize * IOVecs[I].NumElm;
43 memcpy(*Buffer, IOVecs[I].Data, Length);
49 static void llvmInitBufferIO(ProfBufferIO *BufferIO, ProfDataWriter *FileWriter,
50 uint8_t *Buffer, uint32_t BufferSz) {
51 BufferIO->FileWriter = FileWriter;
52 BufferIO->OwnFileWriter = 0;
53 BufferIO->BufferStart = Buffer;
54 BufferIO->BufferSz = BufferSz;
55 BufferIO->CurOffset = 0;
58 COMPILER_RT_VISIBILITY ProfBufferIO *
59 lprofCreateBufferIO(ProfDataWriter *FileWriter) {
60 uint8_t *Buffer = DynamicBufferIOBuffer;
61 uint32_t BufferSize = VPBufferSize;
63 Buffer = &BufferIOBuffer[0];
64 BufferSize = sizeof(BufferIOBuffer);
66 llvmInitBufferIO(&TheBufferIO, FileWriter, Buffer, BufferSize);
70 COMPILER_RT_VISIBILITY void lprofDeleteBufferIO(ProfBufferIO *BufferIO) {
71 if (BufferIO->OwnFileWriter)
72 FreeHook(BufferIO->FileWriter);
73 if (DynamicBufferIOBuffer) {
74 FreeHook(DynamicBufferIOBuffer);
75 DynamicBufferIOBuffer = 0;
80 COMPILER_RT_VISIBILITY int
81 lprofBufferIOWrite(ProfBufferIO *BufferIO, const uint8_t *Data, uint32_t Size) {
82 /* Buffer is not large enough, it is time to flush. */
83 if (Size + BufferIO->CurOffset > BufferIO->BufferSz) {
84 if (lprofBufferIOFlush(BufferIO) != 0)
87 /* Special case, bypass the buffer completely. */
88 ProfDataIOVec IO[] = {{Data, sizeof(uint8_t), Size}};
89 if (Size > BufferIO->BufferSz) {
90 if (BufferIO->FileWriter->Write(BufferIO->FileWriter, IO, 1))
93 /* Write the data to buffer */
94 uint8_t *Buffer = BufferIO->BufferStart + BufferIO->CurOffset;
95 ProfDataWriter BufferWriter;
96 initBufferWriter(&BufferWriter, (char *)Buffer);
97 lprofBufferWriter(&BufferWriter, IO, 1);
99 (uint8_t *)BufferWriter.WriterCtx - BufferIO->BufferStart;
104 COMPILER_RT_VISIBILITY int lprofBufferIOFlush(ProfBufferIO *BufferIO) {
105 if (BufferIO->CurOffset) {
106 ProfDataIOVec IO[] = {
107 {BufferIO->BufferStart, sizeof(uint8_t), BufferIO->CurOffset}};
108 if (BufferIO->FileWriter->Write(BufferIO->FileWriter, IO, 1))
110 BufferIO->CurOffset = 0;
115 /* Write out value profile data for function specified with \c Data.
116 * The implementation does not use the method \c serializeValueProfData
117 * which depends on dynamic memory allocation. In this implementation,
118 * value profile data is written out to \c BufferIO piecemeal.
120 static int writeOneValueProfData(ProfBufferIO *BufferIO,
121 VPDataReaderType *VPDataReader,
122 const __llvm_profile_data *Data) {
123 unsigned I, NumValueKinds = 0;
124 ValueProfData VPHeader;
125 uint8_t *SiteCountArray[IPVK_Last + 1];
127 for (I = 0; I <= IPVK_Last; I++) {
128 if (!Data->NumValueSites[I])
129 SiteCountArray[I] = 0;
132 VPDataReader->GetValueProfRecordHeaderSize(Data->NumValueSites[I]) -
133 offsetof(ValueProfRecord, SiteCountArray);
134 /* Only use alloca for this small byte array to avoid excessive
136 SiteCountArray[I] = (uint8_t *)COMPILER_RT_ALLOCA(Sz);
137 memset(SiteCountArray[I], 0, Sz);
141 /* If NumValueKinds returned is 0, there is nothing to write, report
142 success and return. This should match the raw profile reader's behavior. */
143 if (!(NumValueKinds = VPDataReader->InitRTRecord(Data, SiteCountArray)))
146 /* First write the header structure. */
147 VPHeader.TotalSize = VPDataReader->GetValueProfDataSize();
148 VPHeader.NumValueKinds = NumValueKinds;
149 if (lprofBufferIOWrite(BufferIO, (const uint8_t *)&VPHeader,
150 sizeof(ValueProfData)))
153 /* Make sure nothing else needs to be written before value profile
155 if ((void *)VPDataReader->GetFirstValueProfRecord(&VPHeader) !=
156 (void *)(&VPHeader + 1))
159 /* Write out the value profile record for each value kind
161 for (I = 0; I <= IPVK_Last; I++) {
163 ValueProfRecord RecordHeader;
164 /* The size of the value prof record header without counting the
165 * site count array .*/
166 uint32_t RecordHeaderSize = offsetof(ValueProfRecord, SiteCountArray);
167 uint32_t SiteCountArraySize;
169 if (!Data->NumValueSites[I])
172 /* Write out the record header. */
173 RecordHeader.Kind = I;
174 RecordHeader.NumValueSites = Data->NumValueSites[I];
175 if (lprofBufferIOWrite(BufferIO, (const uint8_t *)&RecordHeader,
179 /* Write out the site value count array including padding space. */
181 VPDataReader->GetValueProfRecordHeaderSize(Data->NumValueSites[I]) -
183 if (lprofBufferIOWrite(BufferIO, SiteCountArray[I], SiteCountArraySize))
186 /* Write out the value profile data for each value site. */
187 for (J = 0; J < Data->NumValueSites[I]; J++) {
188 uint32_t NRead, NRemain;
189 ValueProfNode *NextStartNode = 0;
190 NRemain = VPDataReader->GetNumValueDataForSite(I, J);
193 /* Read and write out value data in small chunks till it is done. */
195 NRead = (NRemain > VPDataArraySize ? VPDataArraySize : NRemain);
197 VPDataReader->GetValueData(I, /* ValueKind */
199 &VPDataArray[0], NextStartNode, NRead);
200 if (lprofBufferIOWrite(BufferIO, (const uint8_t *)&VPDataArray[0],
201 NRead * sizeof(InstrProfValueData)))
204 } while (NRemain != 0);
207 /* All done report success. */
211 static int writeValueProfData(ProfDataWriter *Writer,
212 VPDataReaderType *VPDataReader,
213 const __llvm_profile_data *DataBegin,
214 const __llvm_profile_data *DataEnd) {
215 ProfBufferIO *BufferIO;
216 const __llvm_profile_data *DI = 0;
221 BufferIO = lprofCreateBufferIO(Writer);
223 for (DI = DataBegin; DI < DataEnd; DI++) {
224 if (writeOneValueProfData(BufferIO, VPDataReader, DI))
228 if (lprofBufferIOFlush(BufferIO) != 0)
230 lprofDeleteBufferIO(BufferIO);
235 COMPILER_RT_VISIBILITY int lprofWriteData(ProfDataWriter *Writer,
236 VPDataReaderType *VPDataReader,
237 int SkipNameDataWrite) {
238 /* Match logic in __llvm_profile_write_buffer(). */
239 const __llvm_profile_data *DataBegin = __llvm_profile_begin_data();
240 const __llvm_profile_data *DataEnd = __llvm_profile_end_data();
241 const uint64_t *CountersBegin = __llvm_profile_begin_counters();
242 const uint64_t *CountersEnd = __llvm_profile_end_counters();
243 const char *NamesBegin = __llvm_profile_begin_names();
244 const char *NamesEnd = __llvm_profile_end_names();
245 return lprofWriteDataImpl(Writer, DataBegin, DataEnd, CountersBegin,
246 CountersEnd, VPDataReader, NamesBegin, NamesEnd,
250 COMPILER_RT_VISIBILITY int
251 lprofWriteDataImpl(ProfDataWriter *Writer, const __llvm_profile_data *DataBegin,
252 const __llvm_profile_data *DataEnd,
253 const uint64_t *CountersBegin, const uint64_t *CountersEnd,
254 VPDataReaderType *VPDataReader, const char *NamesBegin,
255 const char *NamesEnd, int SkipNameDataWrite) {
257 /* Calculate size of sections. */
258 const uint64_t DataSize = __llvm_profile_get_data_size(DataBegin, DataEnd);
259 const uint64_t CountersSize = CountersEnd - CountersBegin;
260 const uint64_t NamesSize = NamesEnd - NamesBegin;
261 const uint64_t Padding = __llvm_profile_get_num_padding_bytes(NamesSize);
263 /* Enough zeroes for padding. */
264 const char Zeroes[sizeof(uint64_t)] = {0};
266 /* Create the header. */
267 __llvm_profile_header Header;
272 /* Initialize header structure. */
273 #define INSTR_PROF_RAW_HEADER(Type, Name, Init) Header.Name = Init;
274 #include "InstrProfData.inc"
276 /* Write the data. */
277 ProfDataIOVec IOVec[] = {
278 {&Header, sizeof(__llvm_profile_header), 1},
279 {DataBegin, sizeof(__llvm_profile_data), DataSize},
280 {CountersBegin, sizeof(uint64_t), CountersSize},
281 {SkipNameDataWrite ? NULL : NamesBegin, sizeof(uint8_t), NamesSize},
282 {Zeroes, sizeof(uint8_t), Padding}};
283 if (Writer->Write(Writer, IOVec, sizeof(IOVec) / sizeof(*IOVec)))
286 return writeValueProfData(Writer, VPDataReader, DataBegin, DataEnd);