blob: 30c1b785c72df0032b64ca1c272eb26540719866 [file] [log] [blame]
Xinliang David Li1d8d46a2015-11-18 21:08:03 +00001/*===- InstrProfilingWriter.c - Write instrumentation to a file or buffer -===*\
2|*
3|* The LLVM Compiler Infrastructure
4|*
5|* This file is distributed under the University of Illinois Open Source
6|* License. See LICENSE.TXT for details.
7|*
8\*===----------------------------------------------------------------------===*/
9
10#include "InstrProfiling.h"
11#include "InstrProfilingInternal.h"
Xinliang David Li23a66e42016-05-14 20:12:42 +000012#ifdef _MSC_VER
13#include <malloc.h>
14#else
15#include <alloca.h>
16#endif
Xinliang David Libaf55d82015-12-22 18:57:15 +000017#include <string.h>
18
Xinliang David Li54dd6832015-12-29 07:13:59 +000019#define INSTR_PROF_VALUE_PROF_DATA
20#include "InstrProfData.inc"
Xinliang David Li609fae32016-05-13 18:26:26 +000021
Xinliang David Lib5a2b3a2016-05-09 19:01:19 +000022COMPILER_RT_VISIBILITY void (*FreeHook)(void *) = NULL;
Xinliang David Li609fae32016-05-13 18:26:26 +000023static ProfBufferIO TheBufferIO;
24#define VP_BUFFER_SIZE 8 * 1024
25static uint8_t BufferIOBuffer[VP_BUFFER_SIZE];
Xinliang David Li23a66e42016-05-14 20:12:42 +000026static InstrProfValueData VPDataArray[16];
27static uint32_t VPDataArraySize = sizeof(VPDataArray) / sizeof(*VPDataArray);
28
Xinliang David Li609fae32016-05-13 18:26:26 +000029COMPILER_RT_VISIBILITY uint8_t *DynamicBufferIOBuffer = 0;
30COMPILER_RT_VISIBILITY uint32_t VPBufferSize = 0;
Xinliang David Li54dd6832015-12-29 07:13:59 +000031
Xinliang David Libaf55d82015-12-22 18:57:15 +000032/* The buffer writer is reponsponsible in keeping writer state
33 * across the call.
34 */
Xinliang David Licf1a8d62016-03-06 04:18:13 +000035COMPILER_RT_VISIBILITY uint32_t lprofBufferWriter(ProfDataIOVec *IOVecs,
36 uint32_t NumIOVecs,
37 void **WriterCtx) {
Xinliang David Libaf55d82015-12-22 18:57:15 +000038 uint32_t I;
39 char **Buffer = (char **)WriterCtx;
40 for (I = 0; I < NumIOVecs; I++) {
41 size_t Length = IOVecs[I].ElmSize * IOVecs[I].NumElm;
42 memcpy(*Buffer, IOVecs[I].Data, Length);
43 *Buffer += Length;
44 }
45 return 0;
46}
Xinliang David Li1d8d46a2015-11-18 21:08:03 +000047
Xinliang David Licda3bc22015-12-29 23:54:41 +000048static void llvmInitBufferIO(ProfBufferIO *BufferIO, WriterCallback FileWriter,
49 void *File, uint8_t *Buffer, uint32_t BufferSz) {
50 BufferIO->File = File;
51 BufferIO->FileWriter = FileWriter;
52 BufferIO->BufferStart = Buffer;
53 BufferIO->BufferSz = BufferSz;
54 BufferIO->CurOffset = 0;
55}
56
57COMPILER_RT_VISIBILITY ProfBufferIO *
Xinliang David Li609fae32016-05-13 18:26:26 +000058lprofCreateBufferIO(WriterCallback FileWriter, void *File) {
59 uint8_t *Buffer = DynamicBufferIOBuffer;
60 uint32_t BufferSize = VPBufferSize;
Xinliang David Licda3bc22015-12-29 23:54:41 +000061 if (!Buffer) {
Xinliang David Li609fae32016-05-13 18:26:26 +000062 Buffer = &BufferIOBuffer[0];
63 BufferSize = sizeof(BufferIOBuffer);
Xinliang David Licda3bc22015-12-29 23:54:41 +000064 }
Xinliang David Li609fae32016-05-13 18:26:26 +000065 llvmInitBufferIO(&TheBufferIO, FileWriter, File, Buffer, BufferSize);
66 return &TheBufferIO;
Xinliang David Licda3bc22015-12-29 23:54:41 +000067}
68
Xinliang David Licf1a8d62016-03-06 04:18:13 +000069COMPILER_RT_VISIBILITY void lprofDeleteBufferIO(ProfBufferIO *BufferIO) {
Xinliang David Lia16c7542016-05-14 03:16:47 +000070 if (DynamicBufferIOBuffer) {
71 DynamicBufferIOBuffer = 0;
72 VPBufferSize = 0;
Xinliang David Li609fae32016-05-13 18:26:26 +000073 FreeHook(DynamicBufferIOBuffer);
Xinliang David Lia16c7542016-05-14 03:16:47 +000074 }
Xinliang David Licda3bc22015-12-29 23:54:41 +000075}
76
77COMPILER_RT_VISIBILITY int
Xinliang David Licf1a8d62016-03-06 04:18:13 +000078lprofBufferIOWrite(ProfBufferIO *BufferIO, const uint8_t *Data, uint32_t Size) {
Xinliang David Licda3bc22015-12-29 23:54:41 +000079 /* Buffer is not large enough, it is time to flush. */
80 if (Size + BufferIO->CurOffset > BufferIO->BufferSz) {
Xinliang David Licf1a8d62016-03-06 04:18:13 +000081 if (lprofBufferIOFlush(BufferIO) != 0)
82 return -1;
Xinliang David Licda3bc22015-12-29 23:54:41 +000083 }
84 /* Special case, bypass the buffer completely. */
85 ProfDataIOVec IO[] = {{Data, sizeof(uint8_t), Size}};
86 if (Size > BufferIO->BufferSz) {
87 if (BufferIO->FileWriter(IO, 1, &BufferIO->File))
88 return -1;
89 } else {
90 /* Write the data to buffer */
91 uint8_t *Buffer = BufferIO->BufferStart + BufferIO->CurOffset;
Xinliang David Licf1a8d62016-03-06 04:18:13 +000092 lprofBufferWriter(IO, 1, (void **)&Buffer);
Xinliang David Licda3bc22015-12-29 23:54:41 +000093 BufferIO->CurOffset = Buffer - BufferIO->BufferStart;
94 }
95 return 0;
96}
97
Xinliang David Licf1a8d62016-03-06 04:18:13 +000098COMPILER_RT_VISIBILITY int lprofBufferIOFlush(ProfBufferIO *BufferIO) {
Xinliang David Licda3bc22015-12-29 23:54:41 +000099 if (BufferIO->CurOffset) {
100 ProfDataIOVec IO[] = {
101 {BufferIO->BufferStart, sizeof(uint8_t), BufferIO->CurOffset}};
102 if (BufferIO->FileWriter(IO, 1, &BufferIO->File))
103 return -1;
104 BufferIO->CurOffset = 0;
105 }
106 return 0;
107}
108
Xinliang David Li23a66e42016-05-14 20:12:42 +0000109/* Write out value profile data for function specified with \c Data.
110 * The implementation does not use the method \c serializeValueProfData
111 * which depends on dynamic memory allocation. In this implementation,
112 * value profile data is written out to \c BufferIO piecemeal.
113 */
Xinliang David Li911af1c2016-05-12 21:18:41 +0000114static int writeOneValueProfData(ProfBufferIO *BufferIO,
Xinliang David Li23a66e42016-05-14 20:12:42 +0000115 VPDataReaderType *VPDataReader,
Xinliang David Li911af1c2016-05-12 21:18:41 +0000116 const __llvm_profile_data *Data) {
Xinliang David Li23a66e42016-05-14 20:12:42 +0000117 unsigned I, NumValueKinds = 0;
118 ValueProfData VPHeader;
119 uint8_t *SiteCountArray[IPVK_Last + 1];
120
121 for (I = 0; I <= IPVK_Last; I++) {
122 if (!Data->NumValueSites[I])
123 SiteCountArray[I] = 0;
124 else {
125 uint32_t Sz =
126 VPDataReader->GetValueProfRecordHeaderSize(Data->NumValueSites[I]) -
127 offsetof(ValueProfRecord, SiteCountArray);
128 /* Only use alloca for this small byte array to avoid excessive
129 * stack growth. */
130 SiteCountArray[I] = (uint8_t *)COMPILER_RT_ALLOCA(Sz);
131 memset(SiteCountArray[I], 0, Sz);
132 }
133 }
134
135 /* If NumValueKinds returned is 0, there is nothing to write, report
136 success and return. This should match the raw profile reader's behavior. */
137 if (!(NumValueKinds = VPDataReader->InitRTRecord(Data, SiteCountArray)))
Xinliang David Li911af1c2016-05-12 21:18:41 +0000138 return 0;
Xinliang David Li23a66e42016-05-14 20:12:42 +0000139
140 /* First write the header structure. */
141 VPHeader.TotalSize = VPDataReader->GetValueProfDataSize();
142 VPHeader.NumValueKinds = NumValueKinds;
143 if (lprofBufferIOWrite(BufferIO, (const uint8_t *)&VPHeader,
144 sizeof(ValueProfData)))
Xinliang David Li911af1c2016-05-12 21:18:41 +0000145 return -1;
Xinliang David Li23a66e42016-05-14 20:12:42 +0000146
147 /* Make sure nothing else needs to be written before value profile
148 * records. */
149 if ((void *)VPDataReader->GetFirstValueProfRecord(&VPHeader) !=
150 (void *)(&VPHeader + 1))
151 return -1;
152
153 /* Write out the value profile record for each value kind
154 * one by one. */
155 for (I = 0; I <= IPVK_Last; I++) {
156 uint32_t J;
157 ValueProfRecord RecordHeader;
158 /* The size of the value prof record header without counting the
159 * site count array .*/
160 uint32_t RecordHeaderSize = offsetof(ValueProfRecord, SiteCountArray);
161 uint32_t SiteCountArraySize;
162
163 if (!Data->NumValueSites[I])
164 continue;
165
166 /* Write out the record header. */
167 RecordHeader.Kind = I;
168 RecordHeader.NumValueSites = Data->NumValueSites[I];
169 if (lprofBufferIOWrite(BufferIO, (const uint8_t *)&RecordHeader,
170 RecordHeaderSize))
171 return -1;
172
173 /* Write out the site value count array including padding space. */
174 SiteCountArraySize =
175 VPDataReader->GetValueProfRecordHeaderSize(Data->NumValueSites[I]) -
176 RecordHeaderSize;
177 if (lprofBufferIOWrite(BufferIO, SiteCountArray[I], SiteCountArraySize))
178 return -1;
179
180 /* Write out the value profile data for each value site. */
181 for (J = 0; J < Data->NumValueSites[I]; J++) {
182 uint32_t NRead, NRemain;
183 ValueProfNode *NextStartNode = 0;
184 NRemain = VPDataReader->GetNumValueDataForSite(I, J);
185 if (!NRemain)
186 continue;
187 /* Read and write out value data in small chunks till it is done. */
188 do {
189 NRead = (NRemain > VPDataArraySize ? VPDataArraySize : NRemain);
190 NextStartNode =
191 VPDataReader->GetValueData(I, /* ValueKind */
192 J, /* Site */
193 &VPDataArray[0], NextStartNode, NRead);
194 if (lprofBufferIOWrite(BufferIO, (const uint8_t *)&VPDataArray[0],
195 NRead * sizeof(InstrProfValueData)))
196 return -1;
197 NRemain -= NRead;
198 } while (NRemain != 0);
199 }
200 }
201 /* All done report success. */
Xinliang David Li911af1c2016-05-12 21:18:41 +0000202 return 0;
203}
204
Xinliang David Li54dd6832015-12-29 07:13:59 +0000205static int writeValueProfData(WriterCallback Writer, void *WriterCtx,
Xinliang David Li23a66e42016-05-14 20:12:42 +0000206 VPDataReaderType *VPDataReader,
Xinliang David Lie9a8574d2016-05-10 00:17:31 +0000207 const __llvm_profile_data *DataBegin,
208 const __llvm_profile_data *DataEnd) {
Xinliang David Licda3bc22015-12-29 23:54:41 +0000209 ProfBufferIO *BufferIO;
Xinliang David Lie9a8574d2016-05-10 00:17:31 +0000210 const __llvm_profile_data *DI = 0;
Xinliang David Li54dd6832015-12-29 07:13:59 +0000211
Xinliang David Li23a66e42016-05-14 20:12:42 +0000212 if (!VPDataReader)
Xinliang David Li54dd6832015-12-29 07:13:59 +0000213 return 0;
214
Xinliang David Li609fae32016-05-13 18:26:26 +0000215 BufferIO = lprofCreateBufferIO(Writer, WriterCtx);
Xinliang David Li54dd6832015-12-29 07:13:59 +0000216
Xinliang David Lie9a8574d2016-05-10 00:17:31 +0000217 for (DI = DataBegin; DI < DataEnd; DI++) {
Xinliang David Li23a66e42016-05-14 20:12:42 +0000218 if (writeOneValueProfData(BufferIO, VPDataReader, DI))
Xinliang David Li54dd6832015-12-29 07:13:59 +0000219 return -1;
220 }
221
Xinliang David Licf1a8d62016-03-06 04:18:13 +0000222 if (lprofBufferIOFlush(BufferIO) != 0)
Xinliang David Licda3bc22015-12-29 23:54:41 +0000223 return -1;
Xinliang David Licf1a8d62016-03-06 04:18:13 +0000224 lprofDeleteBufferIO(BufferIO);
Xinliang David Licda3bc22015-12-29 23:54:41 +0000225
Xinliang David Li54dd6832015-12-29 07:13:59 +0000226 return 0;
227}
228
Xinliang David Lie9a8574d2016-05-10 00:17:31 +0000229COMPILER_RT_VISIBILITY int lprofWriteData(WriterCallback Writer,
230 void *WriterCtx,
Xinliang David Li23a66e42016-05-14 20:12:42 +0000231 VPDataReaderType *VPDataReader) {
Xinliang David Lie9a8574d2016-05-10 00:17:31 +0000232 /* Match logic in __llvm_profile_write_buffer(). */
233 const __llvm_profile_data *DataBegin = __llvm_profile_begin_data();
234 const __llvm_profile_data *DataEnd = __llvm_profile_end_data();
235 const uint64_t *CountersBegin = __llvm_profile_begin_counters();
236 const uint64_t *CountersEnd = __llvm_profile_end_counters();
237 const char *NamesBegin = __llvm_profile_begin_names();
238 const char *NamesEnd = __llvm_profile_end_names();
239 return lprofWriteDataImpl(Writer, WriterCtx, DataBegin, DataEnd,
Xinliang David Li23a66e42016-05-14 20:12:42 +0000240 CountersBegin, CountersEnd, VPDataReader,
Xinliang David Lie9a8574d2016-05-10 00:17:31 +0000241 NamesBegin, NamesEnd);
242}
243
Xinliang David Licf1a8d62016-03-06 04:18:13 +0000244COMPILER_RT_VISIBILITY int
245lprofWriteDataImpl(WriterCallback Writer, void *WriterCtx,
246 const __llvm_profile_data *DataBegin,
247 const __llvm_profile_data *DataEnd,
248 const uint64_t *CountersBegin, const uint64_t *CountersEnd,
Xinliang David Li23a66e42016-05-14 20:12:42 +0000249 VPDataReaderType *VPDataReader, const char *NamesBegin,
Xinliang David Lie9a8574d2016-05-10 00:17:31 +0000250 const char *NamesEnd) {
Xinliang David Li1d8d46a2015-11-18 21:08:03 +0000251
252 /* Calculate size of sections. */
Vedant Kumarb8502512016-02-26 02:49:41 +0000253 const uint64_t DataSize = __llvm_profile_get_data_size(DataBegin, DataEnd);
Xinliang David Li1d8d46a2015-11-18 21:08:03 +0000254 const uint64_t CountersSize = CountersEnd - CountersBegin;
255 const uint64_t NamesSize = NamesEnd - NamesBegin;
256 const uint64_t Padding = __llvm_profile_get_num_padding_bytes(NamesSize);
257
258 /* Enough zeroes for padding. */
259 const char Zeroes[sizeof(uint64_t)] = {0};
260
261 /* Create the header. */
262 __llvm_profile_header Header;
263
264 if (!DataSize)
265 return 0;
266
Xinliang David Lie9a8574d2016-05-10 00:17:31 +0000267/* Initialize header structure. */
Xinliang David Li6376db52015-11-23 18:36:40 +0000268#define INSTR_PROF_RAW_HEADER(Type, Name, Init) Header.Name = Init;
269#include "InstrProfData.inc"
Xinliang David Li1d8d46a2015-11-18 21:08:03 +0000270
Xinliang David Li2d5bf072015-11-21 04:16:42 +0000271 /* Write the data. */
Xinliang David Li54dd6832015-12-29 07:13:59 +0000272 ProfDataIOVec IOVec[] = {{&Header, sizeof(__llvm_profile_header), 1},
273 {DataBegin, sizeof(__llvm_profile_data), DataSize},
274 {CountersBegin, sizeof(uint64_t), CountersSize},
275 {NamesBegin, sizeof(uint8_t), NamesSize},
276 {Zeroes, sizeof(uint8_t), Padding}};
Joerg Sonnenbergerd6487b22015-12-04 00:40:07 +0000277 if (Writer(IOVec, sizeof(IOVec) / sizeof(*IOVec), &WriterCtx))
Xinliang David Li2d5bf072015-11-21 04:16:42 +0000278 return -1;
Xinliang David Li54dd6832015-12-29 07:13:59 +0000279
Xinliang David Li23a66e42016-05-14 20:12:42 +0000280 return writeValueProfData(Writer, WriterCtx, VPDataReader, DataBegin,
Xinliang David Lie9a8574d2016-05-10 00:17:31 +0000281 DataEnd);
Xinliang David Li1d8d46a2015-11-18 21:08:03 +0000282}