blob: 95f37e8e9b4fd25ec7fd1e1219ab3775c873d853 [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
Xinliang David Li6c7bddb2016-05-15 16:41:58 +000013/* For _alloca */
Xinliang David Li23a66e42016-05-14 20:12:42 +000014#include <malloc.h>
Xinliang David Li23a66e42016-05-14 20:12:42 +000015#endif
Xinliang David Libaf55d82015-12-22 18:57:15 +000016#include <string.h>
17
Xinliang David Li54dd6832015-12-29 07:13:59 +000018#define INSTR_PROF_VALUE_PROF_DATA
19#include "InstrProfData.inc"
Xinliang David Li609fae32016-05-13 18:26:26 +000020
Xinliang David Lib5a2b3a2016-05-09 19:01:19 +000021COMPILER_RT_VISIBILITY void (*FreeHook)(void *) = NULL;
Xinliang David Li609fae32016-05-13 18:26:26 +000022static ProfBufferIO TheBufferIO;
23#define VP_BUFFER_SIZE 8 * 1024
24static uint8_t BufferIOBuffer[VP_BUFFER_SIZE];
Xinliang David Li23a66e42016-05-14 20:12:42 +000025static InstrProfValueData VPDataArray[16];
26static uint32_t VPDataArraySize = sizeof(VPDataArray) / sizeof(*VPDataArray);
27
Xinliang David Li609fae32016-05-13 18:26:26 +000028COMPILER_RT_VISIBILITY uint8_t *DynamicBufferIOBuffer = 0;
29COMPILER_RT_VISIBILITY uint32_t VPBufferSize = 0;
Xinliang David Li54dd6832015-12-29 07:13:59 +000030
Xinliang David Libaf55d82015-12-22 18:57:15 +000031/* The buffer writer is reponsponsible in keeping writer state
32 * across the call.
33 */
Xinliang David Licf1a8d62016-03-06 04:18:13 +000034COMPILER_RT_VISIBILITY uint32_t lprofBufferWriter(ProfDataIOVec *IOVecs,
35 uint32_t NumIOVecs,
36 void **WriterCtx) {
Xinliang David Libaf55d82015-12-22 18:57:15 +000037 uint32_t I;
38 char **Buffer = (char **)WriterCtx;
39 for (I = 0; I < NumIOVecs; I++) {
40 size_t Length = IOVecs[I].ElmSize * IOVecs[I].NumElm;
41 memcpy(*Buffer, IOVecs[I].Data, Length);
42 *Buffer += Length;
43 }
44 return 0;
45}
Xinliang David Li1d8d46a2015-11-18 21:08:03 +000046
Xinliang David Licda3bc22015-12-29 23:54:41 +000047static void llvmInitBufferIO(ProfBufferIO *BufferIO, WriterCallback FileWriter,
48 void *File, uint8_t *Buffer, uint32_t BufferSz) {
49 BufferIO->File = File;
50 BufferIO->FileWriter = FileWriter;
51 BufferIO->BufferStart = Buffer;
52 BufferIO->BufferSz = BufferSz;
53 BufferIO->CurOffset = 0;
54}
55
56COMPILER_RT_VISIBILITY ProfBufferIO *
Xinliang David Li609fae32016-05-13 18:26:26 +000057lprofCreateBufferIO(WriterCallback FileWriter, void *File) {
58 uint8_t *Buffer = DynamicBufferIOBuffer;
59 uint32_t BufferSize = VPBufferSize;
Xinliang David Licda3bc22015-12-29 23:54:41 +000060 if (!Buffer) {
Xinliang David Li609fae32016-05-13 18:26:26 +000061 Buffer = &BufferIOBuffer[0];
62 BufferSize = sizeof(BufferIOBuffer);
Xinliang David Licda3bc22015-12-29 23:54:41 +000063 }
Xinliang David Li609fae32016-05-13 18:26:26 +000064 llvmInitBufferIO(&TheBufferIO, FileWriter, File, Buffer, BufferSize);
65 return &TheBufferIO;
Xinliang David Licda3bc22015-12-29 23:54:41 +000066}
67
Xinliang David Licf1a8d62016-03-06 04:18:13 +000068COMPILER_RT_VISIBILITY void lprofDeleteBufferIO(ProfBufferIO *BufferIO) {
Xinliang David Lia16c7542016-05-14 03:16:47 +000069 if (DynamicBufferIOBuffer) {
Sean Silvacfec6c62016-05-16 23:28:35 +000070 FreeHook(DynamicBufferIOBuffer);
Xinliang David Lia16c7542016-05-14 03:16:47 +000071 DynamicBufferIOBuffer = 0;
72 VPBufferSize = 0;
Xinliang David Lia16c7542016-05-14 03:16:47 +000073 }
Xinliang David Licda3bc22015-12-29 23:54:41 +000074}
75
76COMPILER_RT_VISIBILITY int
Xinliang David Licf1a8d62016-03-06 04:18:13 +000077lprofBufferIOWrite(ProfBufferIO *BufferIO, const uint8_t *Data, uint32_t Size) {
Xinliang David Licda3bc22015-12-29 23:54:41 +000078 /* Buffer is not large enough, it is time to flush. */
79 if (Size + BufferIO->CurOffset > BufferIO->BufferSz) {
Xinliang David Licf1a8d62016-03-06 04:18:13 +000080 if (lprofBufferIOFlush(BufferIO) != 0)
81 return -1;
Xinliang David Licda3bc22015-12-29 23:54:41 +000082 }
83 /* Special case, bypass the buffer completely. */
84 ProfDataIOVec IO[] = {{Data, sizeof(uint8_t), Size}};
85 if (Size > BufferIO->BufferSz) {
86 if (BufferIO->FileWriter(IO, 1, &BufferIO->File))
87 return -1;
88 } else {
89 /* Write the data to buffer */
90 uint8_t *Buffer = BufferIO->BufferStart + BufferIO->CurOffset;
Xinliang David Licf1a8d62016-03-06 04:18:13 +000091 lprofBufferWriter(IO, 1, (void **)&Buffer);
Xinliang David Licda3bc22015-12-29 23:54:41 +000092 BufferIO->CurOffset = Buffer - BufferIO->BufferStart;
93 }
94 return 0;
95}
96
Xinliang David Licf1a8d62016-03-06 04:18:13 +000097COMPILER_RT_VISIBILITY int lprofBufferIOFlush(ProfBufferIO *BufferIO) {
Xinliang David Licda3bc22015-12-29 23:54:41 +000098 if (BufferIO->CurOffset) {
99 ProfDataIOVec IO[] = {
100 {BufferIO->BufferStart, sizeof(uint8_t), BufferIO->CurOffset}};
101 if (BufferIO->FileWriter(IO, 1, &BufferIO->File))
102 return -1;
103 BufferIO->CurOffset = 0;
104 }
105 return 0;
106}
107
Xinliang David Li23a66e42016-05-14 20:12:42 +0000108/* Write out value profile data for function specified with \c Data.
109 * The implementation does not use the method \c serializeValueProfData
110 * which depends on dynamic memory allocation. In this implementation,
111 * value profile data is written out to \c BufferIO piecemeal.
112 */
Xinliang David Li911af1c2016-05-12 21:18:41 +0000113static int writeOneValueProfData(ProfBufferIO *BufferIO,
Xinliang David Li23a66e42016-05-14 20:12:42 +0000114 VPDataReaderType *VPDataReader,
Xinliang David Li911af1c2016-05-12 21:18:41 +0000115 const __llvm_profile_data *Data) {
Xinliang David Li23a66e42016-05-14 20:12:42 +0000116 unsigned I, NumValueKinds = 0;
117 ValueProfData VPHeader;
118 uint8_t *SiteCountArray[IPVK_Last + 1];
119
120 for (I = 0; I <= IPVK_Last; I++) {
121 if (!Data->NumValueSites[I])
122 SiteCountArray[I] = 0;
123 else {
124 uint32_t Sz =
125 VPDataReader->GetValueProfRecordHeaderSize(Data->NumValueSites[I]) -
126 offsetof(ValueProfRecord, SiteCountArray);
127 /* Only use alloca for this small byte array to avoid excessive
128 * stack growth. */
129 SiteCountArray[I] = (uint8_t *)COMPILER_RT_ALLOCA(Sz);
130 memset(SiteCountArray[I], 0, Sz);
131 }
132 }
133
134 /* If NumValueKinds returned is 0, there is nothing to write, report
135 success and return. This should match the raw profile reader's behavior. */
136 if (!(NumValueKinds = VPDataReader->InitRTRecord(Data, SiteCountArray)))
Xinliang David Li911af1c2016-05-12 21:18:41 +0000137 return 0;
Xinliang David Li23a66e42016-05-14 20:12:42 +0000138
139 /* First write the header structure. */
140 VPHeader.TotalSize = VPDataReader->GetValueProfDataSize();
141 VPHeader.NumValueKinds = NumValueKinds;
142 if (lprofBufferIOWrite(BufferIO, (const uint8_t *)&VPHeader,
143 sizeof(ValueProfData)))
Xinliang David Li911af1c2016-05-12 21:18:41 +0000144 return -1;
Xinliang David Li23a66e42016-05-14 20:12:42 +0000145
146 /* Make sure nothing else needs to be written before value profile
147 * records. */
148 if ((void *)VPDataReader->GetFirstValueProfRecord(&VPHeader) !=
149 (void *)(&VPHeader + 1))
150 return -1;
151
152 /* Write out the value profile record for each value kind
153 * one by one. */
154 for (I = 0; I <= IPVK_Last; I++) {
155 uint32_t J;
156 ValueProfRecord RecordHeader;
157 /* The size of the value prof record header without counting the
158 * site count array .*/
159 uint32_t RecordHeaderSize = offsetof(ValueProfRecord, SiteCountArray);
160 uint32_t SiteCountArraySize;
161
162 if (!Data->NumValueSites[I])
163 continue;
164
165 /* Write out the record header. */
166 RecordHeader.Kind = I;
167 RecordHeader.NumValueSites = Data->NumValueSites[I];
168 if (lprofBufferIOWrite(BufferIO, (const uint8_t *)&RecordHeader,
169 RecordHeaderSize))
170 return -1;
171
172 /* Write out the site value count array including padding space. */
173 SiteCountArraySize =
174 VPDataReader->GetValueProfRecordHeaderSize(Data->NumValueSites[I]) -
175 RecordHeaderSize;
176 if (lprofBufferIOWrite(BufferIO, SiteCountArray[I], SiteCountArraySize))
177 return -1;
178
179 /* Write out the value profile data for each value site. */
180 for (J = 0; J < Data->NumValueSites[I]; J++) {
181 uint32_t NRead, NRemain;
182 ValueProfNode *NextStartNode = 0;
183 NRemain = VPDataReader->GetNumValueDataForSite(I, J);
184 if (!NRemain)
185 continue;
186 /* Read and write out value data in small chunks till it is done. */
187 do {
188 NRead = (NRemain > VPDataArraySize ? VPDataArraySize : NRemain);
189 NextStartNode =
190 VPDataReader->GetValueData(I, /* ValueKind */
191 J, /* Site */
192 &VPDataArray[0], NextStartNode, NRead);
193 if (lprofBufferIOWrite(BufferIO, (const uint8_t *)&VPDataArray[0],
194 NRead * sizeof(InstrProfValueData)))
195 return -1;
196 NRemain -= NRead;
197 } while (NRemain != 0);
198 }
199 }
200 /* All done report success. */
Xinliang David Li911af1c2016-05-12 21:18:41 +0000201 return 0;
202}
203
Xinliang David Li54dd6832015-12-29 07:13:59 +0000204static int writeValueProfData(WriterCallback Writer, void *WriterCtx,
Xinliang David Li23a66e42016-05-14 20:12:42 +0000205 VPDataReaderType *VPDataReader,
Xinliang David Lie9a8574d2016-05-10 00:17:31 +0000206 const __llvm_profile_data *DataBegin,
207 const __llvm_profile_data *DataEnd) {
Xinliang David Licda3bc22015-12-29 23:54:41 +0000208 ProfBufferIO *BufferIO;
Xinliang David Lie9a8574d2016-05-10 00:17:31 +0000209 const __llvm_profile_data *DI = 0;
Xinliang David Li54dd6832015-12-29 07:13:59 +0000210
Xinliang David Li23a66e42016-05-14 20:12:42 +0000211 if (!VPDataReader)
Xinliang David Li54dd6832015-12-29 07:13:59 +0000212 return 0;
213
Xinliang David Li609fae32016-05-13 18:26:26 +0000214 BufferIO = lprofCreateBufferIO(Writer, WriterCtx);
Xinliang David Li54dd6832015-12-29 07:13:59 +0000215
Xinliang David Lie9a8574d2016-05-10 00:17:31 +0000216 for (DI = DataBegin; DI < DataEnd; DI++) {
Xinliang David Li23a66e42016-05-14 20:12:42 +0000217 if (writeOneValueProfData(BufferIO, VPDataReader, DI))
Xinliang David Li54dd6832015-12-29 07:13:59 +0000218 return -1;
219 }
220
Xinliang David Licf1a8d62016-03-06 04:18:13 +0000221 if (lprofBufferIOFlush(BufferIO) != 0)
Xinliang David Licda3bc22015-12-29 23:54:41 +0000222 return -1;
Xinliang David Licf1a8d62016-03-06 04:18:13 +0000223 lprofDeleteBufferIO(BufferIO);
Xinliang David Licda3bc22015-12-29 23:54:41 +0000224
Xinliang David Li54dd6832015-12-29 07:13:59 +0000225 return 0;
226}
227
Xinliang David Lie9a8574d2016-05-10 00:17:31 +0000228COMPILER_RT_VISIBILITY int lprofWriteData(WriterCallback Writer,
229 void *WriterCtx,
Xinliang David Li23a66e42016-05-14 20:12:42 +0000230 VPDataReaderType *VPDataReader) {
Xinliang David Lie9a8574d2016-05-10 00:17:31 +0000231 /* Match logic in __llvm_profile_write_buffer(). */
232 const __llvm_profile_data *DataBegin = __llvm_profile_begin_data();
233 const __llvm_profile_data *DataEnd = __llvm_profile_end_data();
234 const uint64_t *CountersBegin = __llvm_profile_begin_counters();
235 const uint64_t *CountersEnd = __llvm_profile_end_counters();
236 const char *NamesBegin = __llvm_profile_begin_names();
237 const char *NamesEnd = __llvm_profile_end_names();
238 return lprofWriteDataImpl(Writer, WriterCtx, DataBegin, DataEnd,
Xinliang David Li23a66e42016-05-14 20:12:42 +0000239 CountersBegin, CountersEnd, VPDataReader,
Xinliang David Lie9a8574d2016-05-10 00:17:31 +0000240 NamesBegin, NamesEnd);
241}
242
Xinliang David Licf1a8d62016-03-06 04:18:13 +0000243COMPILER_RT_VISIBILITY int
244lprofWriteDataImpl(WriterCallback Writer, void *WriterCtx,
245 const __llvm_profile_data *DataBegin,
246 const __llvm_profile_data *DataEnd,
247 const uint64_t *CountersBegin, const uint64_t *CountersEnd,
Xinliang David Li23a66e42016-05-14 20:12:42 +0000248 VPDataReaderType *VPDataReader, const char *NamesBegin,
Xinliang David Lie9a8574d2016-05-10 00:17:31 +0000249 const char *NamesEnd) {
Xinliang David Li1d8d46a2015-11-18 21:08:03 +0000250
251 /* Calculate size of sections. */
Vedant Kumarb8502512016-02-26 02:49:41 +0000252 const uint64_t DataSize = __llvm_profile_get_data_size(DataBegin, DataEnd);
Xinliang David Li1d8d46a2015-11-18 21:08:03 +0000253 const uint64_t CountersSize = CountersEnd - CountersBegin;
254 const uint64_t NamesSize = NamesEnd - NamesBegin;
255 const uint64_t Padding = __llvm_profile_get_num_padding_bytes(NamesSize);
256
257 /* Enough zeroes for padding. */
258 const char Zeroes[sizeof(uint64_t)] = {0};
259
260 /* Create the header. */
261 __llvm_profile_header Header;
262
263 if (!DataSize)
264 return 0;
265
Xinliang David Lie9a8574d2016-05-10 00:17:31 +0000266/* Initialize header structure. */
Xinliang David Li6376db52015-11-23 18:36:40 +0000267#define INSTR_PROF_RAW_HEADER(Type, Name, Init) Header.Name = Init;
268#include "InstrProfData.inc"
Xinliang David Li1d8d46a2015-11-18 21:08:03 +0000269
Xinliang David Li2d5bf072015-11-21 04:16:42 +0000270 /* Write the data. */
Xinliang David Li54dd6832015-12-29 07:13:59 +0000271 ProfDataIOVec IOVec[] = {{&Header, sizeof(__llvm_profile_header), 1},
272 {DataBegin, sizeof(__llvm_profile_data), DataSize},
273 {CountersBegin, sizeof(uint64_t), CountersSize},
274 {NamesBegin, sizeof(uint8_t), NamesSize},
275 {Zeroes, sizeof(uint8_t), Padding}};
Joerg Sonnenbergerd6487b22015-12-04 00:40:07 +0000276 if (Writer(IOVec, sizeof(IOVec) / sizeof(*IOVec), &WriterCtx))
Xinliang David Li2d5bf072015-11-21 04:16:42 +0000277 return -1;
Xinliang David Li54dd6832015-12-29 07:13:59 +0000278
Xinliang David Li23a66e42016-05-14 20:12:42 +0000279 return writeValueProfData(Writer, WriterCtx, VPDataReader, DataBegin,
Xinliang David Lie9a8574d2016-05-10 00:17:31 +0000280 DataEnd);
Xinliang David Li1d8d46a2015-11-18 21:08:03 +0000281}