blob: da0b58df082149163134097613936400021be699 [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>
Xinliang David Lifedb0fd2016-05-15 04:26:17 +000014#elif defined(__FreeBSD__)
15#include <stdlib.h>
Xinliang David Li23a66e42016-05-14 20:12:42 +000016#else
17#include <alloca.h>
18#endif
Xinliang David Libaf55d82015-12-22 18:57:15 +000019#include <string.h>
20
Xinliang David Li54dd6832015-12-29 07:13:59 +000021#define INSTR_PROF_VALUE_PROF_DATA
22#include "InstrProfData.inc"
Xinliang David Li609fae32016-05-13 18:26:26 +000023
Xinliang David Lib5a2b3a2016-05-09 19:01:19 +000024COMPILER_RT_VISIBILITY void (*FreeHook)(void *) = NULL;
Xinliang David Li609fae32016-05-13 18:26:26 +000025static ProfBufferIO TheBufferIO;
26#define VP_BUFFER_SIZE 8 * 1024
27static uint8_t BufferIOBuffer[VP_BUFFER_SIZE];
Xinliang David Li23a66e42016-05-14 20:12:42 +000028static InstrProfValueData VPDataArray[16];
29static uint32_t VPDataArraySize = sizeof(VPDataArray) / sizeof(*VPDataArray);
30
Xinliang David Li609fae32016-05-13 18:26:26 +000031COMPILER_RT_VISIBILITY uint8_t *DynamicBufferIOBuffer = 0;
32COMPILER_RT_VISIBILITY uint32_t VPBufferSize = 0;
Xinliang David Li54dd6832015-12-29 07:13:59 +000033
Xinliang David Libaf55d82015-12-22 18:57:15 +000034/* The buffer writer is reponsponsible in keeping writer state
35 * across the call.
36 */
Xinliang David Licf1a8d62016-03-06 04:18:13 +000037COMPILER_RT_VISIBILITY uint32_t lprofBufferWriter(ProfDataIOVec *IOVecs,
38 uint32_t NumIOVecs,
39 void **WriterCtx) {
Xinliang David Libaf55d82015-12-22 18:57:15 +000040 uint32_t I;
41 char **Buffer = (char **)WriterCtx;
42 for (I = 0; I < NumIOVecs; I++) {
43 size_t Length = IOVecs[I].ElmSize * IOVecs[I].NumElm;
44 memcpy(*Buffer, IOVecs[I].Data, Length);
45 *Buffer += Length;
46 }
47 return 0;
48}
Xinliang David Li1d8d46a2015-11-18 21:08:03 +000049
Xinliang David Licda3bc22015-12-29 23:54:41 +000050static void llvmInitBufferIO(ProfBufferIO *BufferIO, WriterCallback FileWriter,
51 void *File, uint8_t *Buffer, uint32_t BufferSz) {
52 BufferIO->File = File;
53 BufferIO->FileWriter = FileWriter;
54 BufferIO->BufferStart = Buffer;
55 BufferIO->BufferSz = BufferSz;
56 BufferIO->CurOffset = 0;
57}
58
59COMPILER_RT_VISIBILITY ProfBufferIO *
Xinliang David Li609fae32016-05-13 18:26:26 +000060lprofCreateBufferIO(WriterCallback FileWriter, void *File) {
61 uint8_t *Buffer = DynamicBufferIOBuffer;
62 uint32_t BufferSize = VPBufferSize;
Xinliang David Licda3bc22015-12-29 23:54:41 +000063 if (!Buffer) {
Xinliang David Li609fae32016-05-13 18:26:26 +000064 Buffer = &BufferIOBuffer[0];
65 BufferSize = sizeof(BufferIOBuffer);
Xinliang David Licda3bc22015-12-29 23:54:41 +000066 }
Xinliang David Li609fae32016-05-13 18:26:26 +000067 llvmInitBufferIO(&TheBufferIO, FileWriter, File, Buffer, BufferSize);
68 return &TheBufferIO;
Xinliang David Licda3bc22015-12-29 23:54:41 +000069}
70
Xinliang David Licf1a8d62016-03-06 04:18:13 +000071COMPILER_RT_VISIBILITY void lprofDeleteBufferIO(ProfBufferIO *BufferIO) {
Xinliang David Lia16c7542016-05-14 03:16:47 +000072 if (DynamicBufferIOBuffer) {
73 DynamicBufferIOBuffer = 0;
74 VPBufferSize = 0;
Xinliang David Li609fae32016-05-13 18:26:26 +000075 FreeHook(DynamicBufferIOBuffer);
Xinliang David Lia16c7542016-05-14 03:16:47 +000076 }
Xinliang David Licda3bc22015-12-29 23:54:41 +000077}
78
79COMPILER_RT_VISIBILITY int
Xinliang David Licf1a8d62016-03-06 04:18:13 +000080lprofBufferIOWrite(ProfBufferIO *BufferIO, const uint8_t *Data, uint32_t Size) {
Xinliang David Licda3bc22015-12-29 23:54:41 +000081 /* Buffer is not large enough, it is time to flush. */
82 if (Size + BufferIO->CurOffset > BufferIO->BufferSz) {
Xinliang David Licf1a8d62016-03-06 04:18:13 +000083 if (lprofBufferIOFlush(BufferIO) != 0)
84 return -1;
Xinliang David Licda3bc22015-12-29 23:54:41 +000085 }
86 /* Special case, bypass the buffer completely. */
87 ProfDataIOVec IO[] = {{Data, sizeof(uint8_t), Size}};
88 if (Size > BufferIO->BufferSz) {
89 if (BufferIO->FileWriter(IO, 1, &BufferIO->File))
90 return -1;
91 } else {
92 /* Write the data to buffer */
93 uint8_t *Buffer = BufferIO->BufferStart + BufferIO->CurOffset;
Xinliang David Licf1a8d62016-03-06 04:18:13 +000094 lprofBufferWriter(IO, 1, (void **)&Buffer);
Xinliang David Licda3bc22015-12-29 23:54:41 +000095 BufferIO->CurOffset = Buffer - BufferIO->BufferStart;
96 }
97 return 0;
98}
99
Xinliang David Licf1a8d62016-03-06 04:18:13 +0000100COMPILER_RT_VISIBILITY int lprofBufferIOFlush(ProfBufferIO *BufferIO) {
Xinliang David Licda3bc22015-12-29 23:54:41 +0000101 if (BufferIO->CurOffset) {
102 ProfDataIOVec IO[] = {
103 {BufferIO->BufferStart, sizeof(uint8_t), BufferIO->CurOffset}};
104 if (BufferIO->FileWriter(IO, 1, &BufferIO->File))
105 return -1;
106 BufferIO->CurOffset = 0;
107 }
108 return 0;
109}
110
Xinliang David Li23a66e42016-05-14 20:12:42 +0000111/* Write out value profile data for function specified with \c Data.
112 * The implementation does not use the method \c serializeValueProfData
113 * which depends on dynamic memory allocation. In this implementation,
114 * value profile data is written out to \c BufferIO piecemeal.
115 */
Xinliang David Li911af1c2016-05-12 21:18:41 +0000116static int writeOneValueProfData(ProfBufferIO *BufferIO,
Xinliang David Li23a66e42016-05-14 20:12:42 +0000117 VPDataReaderType *VPDataReader,
Xinliang David Li911af1c2016-05-12 21:18:41 +0000118 const __llvm_profile_data *Data) {
Xinliang David Li23a66e42016-05-14 20:12:42 +0000119 unsigned I, NumValueKinds = 0;
120 ValueProfData VPHeader;
121 uint8_t *SiteCountArray[IPVK_Last + 1];
122
123 for (I = 0; I <= IPVK_Last; I++) {
124 if (!Data->NumValueSites[I])
125 SiteCountArray[I] = 0;
126 else {
127 uint32_t Sz =
128 VPDataReader->GetValueProfRecordHeaderSize(Data->NumValueSites[I]) -
129 offsetof(ValueProfRecord, SiteCountArray);
130 /* Only use alloca for this small byte array to avoid excessive
131 * stack growth. */
132 SiteCountArray[I] = (uint8_t *)COMPILER_RT_ALLOCA(Sz);
133 memset(SiteCountArray[I], 0, Sz);
134 }
135 }
136
137 /* If NumValueKinds returned is 0, there is nothing to write, report
138 success and return. This should match the raw profile reader's behavior. */
139 if (!(NumValueKinds = VPDataReader->InitRTRecord(Data, SiteCountArray)))
Xinliang David Li911af1c2016-05-12 21:18:41 +0000140 return 0;
Xinliang David Li23a66e42016-05-14 20:12:42 +0000141
142 /* First write the header structure. */
143 VPHeader.TotalSize = VPDataReader->GetValueProfDataSize();
144 VPHeader.NumValueKinds = NumValueKinds;
145 if (lprofBufferIOWrite(BufferIO, (const uint8_t *)&VPHeader,
146 sizeof(ValueProfData)))
Xinliang David Li911af1c2016-05-12 21:18:41 +0000147 return -1;
Xinliang David Li23a66e42016-05-14 20:12:42 +0000148
149 /* Make sure nothing else needs to be written before value profile
150 * records. */
151 if ((void *)VPDataReader->GetFirstValueProfRecord(&VPHeader) !=
152 (void *)(&VPHeader + 1))
153 return -1;
154
155 /* Write out the value profile record for each value kind
156 * one by one. */
157 for (I = 0; I <= IPVK_Last; I++) {
158 uint32_t J;
159 ValueProfRecord RecordHeader;
160 /* The size of the value prof record header without counting the
161 * site count array .*/
162 uint32_t RecordHeaderSize = offsetof(ValueProfRecord, SiteCountArray);
163 uint32_t SiteCountArraySize;
164
165 if (!Data->NumValueSites[I])
166 continue;
167
168 /* Write out the record header. */
169 RecordHeader.Kind = I;
170 RecordHeader.NumValueSites = Data->NumValueSites[I];
171 if (lprofBufferIOWrite(BufferIO, (const uint8_t *)&RecordHeader,
172 RecordHeaderSize))
173 return -1;
174
175 /* Write out the site value count array including padding space. */
176 SiteCountArraySize =
177 VPDataReader->GetValueProfRecordHeaderSize(Data->NumValueSites[I]) -
178 RecordHeaderSize;
179 if (lprofBufferIOWrite(BufferIO, SiteCountArray[I], SiteCountArraySize))
180 return -1;
181
182 /* Write out the value profile data for each value site. */
183 for (J = 0; J < Data->NumValueSites[I]; J++) {
184 uint32_t NRead, NRemain;
185 ValueProfNode *NextStartNode = 0;
186 NRemain = VPDataReader->GetNumValueDataForSite(I, J);
187 if (!NRemain)
188 continue;
189 /* Read and write out value data in small chunks till it is done. */
190 do {
191 NRead = (NRemain > VPDataArraySize ? VPDataArraySize : NRemain);
192 NextStartNode =
193 VPDataReader->GetValueData(I, /* ValueKind */
194 J, /* Site */
195 &VPDataArray[0], NextStartNode, NRead);
196 if (lprofBufferIOWrite(BufferIO, (const uint8_t *)&VPDataArray[0],
197 NRead * sizeof(InstrProfValueData)))
198 return -1;
199 NRemain -= NRead;
200 } while (NRemain != 0);
201 }
202 }
203 /* All done report success. */
Xinliang David Li911af1c2016-05-12 21:18:41 +0000204 return 0;
205}
206
Xinliang David Li54dd6832015-12-29 07:13:59 +0000207static int writeValueProfData(WriterCallback Writer, void *WriterCtx,
Xinliang David Li23a66e42016-05-14 20:12:42 +0000208 VPDataReaderType *VPDataReader,
Xinliang David Lie9a8574d2016-05-10 00:17:31 +0000209 const __llvm_profile_data *DataBegin,
210 const __llvm_profile_data *DataEnd) {
Xinliang David Licda3bc22015-12-29 23:54:41 +0000211 ProfBufferIO *BufferIO;
Xinliang David Lie9a8574d2016-05-10 00:17:31 +0000212 const __llvm_profile_data *DI = 0;
Xinliang David Li54dd6832015-12-29 07:13:59 +0000213
Xinliang David Li23a66e42016-05-14 20:12:42 +0000214 if (!VPDataReader)
Xinliang David Li54dd6832015-12-29 07:13:59 +0000215 return 0;
216
Xinliang David Li609fae32016-05-13 18:26:26 +0000217 BufferIO = lprofCreateBufferIO(Writer, WriterCtx);
Xinliang David Li54dd6832015-12-29 07:13:59 +0000218
Xinliang David Lie9a8574d2016-05-10 00:17:31 +0000219 for (DI = DataBegin; DI < DataEnd; DI++) {
Xinliang David Li23a66e42016-05-14 20:12:42 +0000220 if (writeOneValueProfData(BufferIO, VPDataReader, DI))
Xinliang David Li54dd6832015-12-29 07:13:59 +0000221 return -1;
222 }
223
Xinliang David Licf1a8d62016-03-06 04:18:13 +0000224 if (lprofBufferIOFlush(BufferIO) != 0)
Xinliang David Licda3bc22015-12-29 23:54:41 +0000225 return -1;
Xinliang David Licf1a8d62016-03-06 04:18:13 +0000226 lprofDeleteBufferIO(BufferIO);
Xinliang David Licda3bc22015-12-29 23:54:41 +0000227
Xinliang David Li54dd6832015-12-29 07:13:59 +0000228 return 0;
229}
230
Xinliang David Lie9a8574d2016-05-10 00:17:31 +0000231COMPILER_RT_VISIBILITY int lprofWriteData(WriterCallback Writer,
232 void *WriterCtx,
Xinliang David Li23a66e42016-05-14 20:12:42 +0000233 VPDataReaderType *VPDataReader) {
Xinliang David Lie9a8574d2016-05-10 00:17:31 +0000234 /* Match logic in __llvm_profile_write_buffer(). */
235 const __llvm_profile_data *DataBegin = __llvm_profile_begin_data();
236 const __llvm_profile_data *DataEnd = __llvm_profile_end_data();
237 const uint64_t *CountersBegin = __llvm_profile_begin_counters();
238 const uint64_t *CountersEnd = __llvm_profile_end_counters();
239 const char *NamesBegin = __llvm_profile_begin_names();
240 const char *NamesEnd = __llvm_profile_end_names();
241 return lprofWriteDataImpl(Writer, WriterCtx, DataBegin, DataEnd,
Xinliang David Li23a66e42016-05-14 20:12:42 +0000242 CountersBegin, CountersEnd, VPDataReader,
Xinliang David Lie9a8574d2016-05-10 00:17:31 +0000243 NamesBegin, NamesEnd);
244}
245
Xinliang David Licf1a8d62016-03-06 04:18:13 +0000246COMPILER_RT_VISIBILITY int
247lprofWriteDataImpl(WriterCallback Writer, void *WriterCtx,
248 const __llvm_profile_data *DataBegin,
249 const __llvm_profile_data *DataEnd,
250 const uint64_t *CountersBegin, const uint64_t *CountersEnd,
Xinliang David Li23a66e42016-05-14 20:12:42 +0000251 VPDataReaderType *VPDataReader, const char *NamesBegin,
Xinliang David Lie9a8574d2016-05-10 00:17:31 +0000252 const char *NamesEnd) {
Xinliang David Li1d8d46a2015-11-18 21:08:03 +0000253
254 /* Calculate size of sections. */
Vedant Kumarb8502512016-02-26 02:49:41 +0000255 const uint64_t DataSize = __llvm_profile_get_data_size(DataBegin, DataEnd);
Xinliang David Li1d8d46a2015-11-18 21:08:03 +0000256 const uint64_t CountersSize = CountersEnd - CountersBegin;
257 const uint64_t NamesSize = NamesEnd - NamesBegin;
258 const uint64_t Padding = __llvm_profile_get_num_padding_bytes(NamesSize);
259
260 /* Enough zeroes for padding. */
261 const char Zeroes[sizeof(uint64_t)] = {0};
262
263 /* Create the header. */
264 __llvm_profile_header Header;
265
266 if (!DataSize)
267 return 0;
268
Xinliang David Lie9a8574d2016-05-10 00:17:31 +0000269/* Initialize header structure. */
Xinliang David Li6376db52015-11-23 18:36:40 +0000270#define INSTR_PROF_RAW_HEADER(Type, Name, Init) Header.Name = Init;
271#include "InstrProfData.inc"
Xinliang David Li1d8d46a2015-11-18 21:08:03 +0000272
Xinliang David Li2d5bf072015-11-21 04:16:42 +0000273 /* Write the data. */
Xinliang David Li54dd6832015-12-29 07:13:59 +0000274 ProfDataIOVec IOVec[] = {{&Header, sizeof(__llvm_profile_header), 1},
275 {DataBegin, sizeof(__llvm_profile_data), DataSize},
276 {CountersBegin, sizeof(uint64_t), CountersSize},
277 {NamesBegin, sizeof(uint8_t), NamesSize},
278 {Zeroes, sizeof(uint8_t), Padding}};
Joerg Sonnenbergerd6487b22015-12-04 00:40:07 +0000279 if (Writer(IOVec, sizeof(IOVec) / sizeof(*IOVec), &WriterCtx))
Xinliang David Li2d5bf072015-11-21 04:16:42 +0000280 return -1;
Xinliang David Li54dd6832015-12-29 07:13:59 +0000281
Xinliang David Li23a66e42016-05-14 20:12:42 +0000282 return writeValueProfData(Writer, WriterCtx, VPDataReader, DataBegin,
Xinliang David Lie9a8574d2016-05-10 00:17:31 +0000283 DataEnd);
Xinliang David Li1d8d46a2015-11-18 21:08:03 +0000284}