Xinliang David Li | 1d8d46a | 2015-11-18 21:08:03 +0000 | [diff] [blame] | 1 | /*===- 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 Li | 23a66e4 | 2016-05-14 20:12:42 +0000 | [diff] [blame] | 12 | #ifdef _MSC_VER |
| 13 | #include <malloc.h> |
Xinliang David Li | fedb0fd | 2016-05-15 04:26:17 +0000 | [diff] [blame] | 14 | #elif defined(__FreeBSD__) |
| 15 | #include <stdlib.h> |
Xinliang David Li | 23a66e4 | 2016-05-14 20:12:42 +0000 | [diff] [blame] | 16 | #else |
| 17 | #include <alloca.h> |
| 18 | #endif |
Xinliang David Li | baf55d8 | 2015-12-22 18:57:15 +0000 | [diff] [blame] | 19 | #include <string.h> |
| 20 | |
Xinliang David Li | 54dd683 | 2015-12-29 07:13:59 +0000 | [diff] [blame] | 21 | #define INSTR_PROF_VALUE_PROF_DATA |
| 22 | #include "InstrProfData.inc" |
Xinliang David Li | 609fae3 | 2016-05-13 18:26:26 +0000 | [diff] [blame] | 23 | |
Xinliang David Li | b5a2b3a | 2016-05-09 19:01:19 +0000 | [diff] [blame] | 24 | COMPILER_RT_VISIBILITY void (*FreeHook)(void *) = NULL; |
Xinliang David Li | 609fae3 | 2016-05-13 18:26:26 +0000 | [diff] [blame] | 25 | static ProfBufferIO TheBufferIO; |
| 26 | #define VP_BUFFER_SIZE 8 * 1024 |
| 27 | static uint8_t BufferIOBuffer[VP_BUFFER_SIZE]; |
Xinliang David Li | 23a66e4 | 2016-05-14 20:12:42 +0000 | [diff] [blame] | 28 | static InstrProfValueData VPDataArray[16]; |
| 29 | static uint32_t VPDataArraySize = sizeof(VPDataArray) / sizeof(*VPDataArray); |
| 30 | |
Xinliang David Li | 609fae3 | 2016-05-13 18:26:26 +0000 | [diff] [blame] | 31 | COMPILER_RT_VISIBILITY uint8_t *DynamicBufferIOBuffer = 0; |
| 32 | COMPILER_RT_VISIBILITY uint32_t VPBufferSize = 0; |
Xinliang David Li | 54dd683 | 2015-12-29 07:13:59 +0000 | [diff] [blame] | 33 | |
Xinliang David Li | baf55d8 | 2015-12-22 18:57:15 +0000 | [diff] [blame] | 34 | /* The buffer writer is reponsponsible in keeping writer state |
| 35 | * across the call. |
| 36 | */ |
Xinliang David Li | cf1a8d6 | 2016-03-06 04:18:13 +0000 | [diff] [blame] | 37 | COMPILER_RT_VISIBILITY uint32_t lprofBufferWriter(ProfDataIOVec *IOVecs, |
| 38 | uint32_t NumIOVecs, |
| 39 | void **WriterCtx) { |
Xinliang David Li | baf55d8 | 2015-12-22 18:57:15 +0000 | [diff] [blame] | 40 | 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 Li | 1d8d46a | 2015-11-18 21:08:03 +0000 | [diff] [blame] | 49 | |
Xinliang David Li | cda3bc2 | 2015-12-29 23:54:41 +0000 | [diff] [blame] | 50 | static 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 | |
| 59 | COMPILER_RT_VISIBILITY ProfBufferIO * |
Xinliang David Li | 609fae3 | 2016-05-13 18:26:26 +0000 | [diff] [blame] | 60 | lprofCreateBufferIO(WriterCallback FileWriter, void *File) { |
| 61 | uint8_t *Buffer = DynamicBufferIOBuffer; |
| 62 | uint32_t BufferSize = VPBufferSize; |
Xinliang David Li | cda3bc2 | 2015-12-29 23:54:41 +0000 | [diff] [blame] | 63 | if (!Buffer) { |
Xinliang David Li | 609fae3 | 2016-05-13 18:26:26 +0000 | [diff] [blame] | 64 | Buffer = &BufferIOBuffer[0]; |
| 65 | BufferSize = sizeof(BufferIOBuffer); |
Xinliang David Li | cda3bc2 | 2015-12-29 23:54:41 +0000 | [diff] [blame] | 66 | } |
Xinliang David Li | 609fae3 | 2016-05-13 18:26:26 +0000 | [diff] [blame] | 67 | llvmInitBufferIO(&TheBufferIO, FileWriter, File, Buffer, BufferSize); |
| 68 | return &TheBufferIO; |
Xinliang David Li | cda3bc2 | 2015-12-29 23:54:41 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Xinliang David Li | cf1a8d6 | 2016-03-06 04:18:13 +0000 | [diff] [blame] | 71 | COMPILER_RT_VISIBILITY void lprofDeleteBufferIO(ProfBufferIO *BufferIO) { |
Xinliang David Li | a16c754 | 2016-05-14 03:16:47 +0000 | [diff] [blame] | 72 | if (DynamicBufferIOBuffer) { |
| 73 | DynamicBufferIOBuffer = 0; |
| 74 | VPBufferSize = 0; |
Xinliang David Li | 609fae3 | 2016-05-13 18:26:26 +0000 | [diff] [blame] | 75 | FreeHook(DynamicBufferIOBuffer); |
Xinliang David Li | a16c754 | 2016-05-14 03:16:47 +0000 | [diff] [blame] | 76 | } |
Xinliang David Li | cda3bc2 | 2015-12-29 23:54:41 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | COMPILER_RT_VISIBILITY int |
Xinliang David Li | cf1a8d6 | 2016-03-06 04:18:13 +0000 | [diff] [blame] | 80 | lprofBufferIOWrite(ProfBufferIO *BufferIO, const uint8_t *Data, uint32_t Size) { |
Xinliang David Li | cda3bc2 | 2015-12-29 23:54:41 +0000 | [diff] [blame] | 81 | /* Buffer is not large enough, it is time to flush. */ |
| 82 | if (Size + BufferIO->CurOffset > BufferIO->BufferSz) { |
Xinliang David Li | cf1a8d6 | 2016-03-06 04:18:13 +0000 | [diff] [blame] | 83 | if (lprofBufferIOFlush(BufferIO) != 0) |
| 84 | return -1; |
Xinliang David Li | cda3bc2 | 2015-12-29 23:54:41 +0000 | [diff] [blame] | 85 | } |
| 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 Li | cf1a8d6 | 2016-03-06 04:18:13 +0000 | [diff] [blame] | 94 | lprofBufferWriter(IO, 1, (void **)&Buffer); |
Xinliang David Li | cda3bc2 | 2015-12-29 23:54:41 +0000 | [diff] [blame] | 95 | BufferIO->CurOffset = Buffer - BufferIO->BufferStart; |
| 96 | } |
| 97 | return 0; |
| 98 | } |
| 99 | |
Xinliang David Li | cf1a8d6 | 2016-03-06 04:18:13 +0000 | [diff] [blame] | 100 | COMPILER_RT_VISIBILITY int lprofBufferIOFlush(ProfBufferIO *BufferIO) { |
Xinliang David Li | cda3bc2 | 2015-12-29 23:54:41 +0000 | [diff] [blame] | 101 | 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 Li | 23a66e4 | 2016-05-14 20:12:42 +0000 | [diff] [blame] | 111 | /* 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 Li | 911af1c | 2016-05-12 21:18:41 +0000 | [diff] [blame] | 116 | static int writeOneValueProfData(ProfBufferIO *BufferIO, |
Xinliang David Li | 23a66e4 | 2016-05-14 20:12:42 +0000 | [diff] [blame] | 117 | VPDataReaderType *VPDataReader, |
Xinliang David Li | 911af1c | 2016-05-12 21:18:41 +0000 | [diff] [blame] | 118 | const __llvm_profile_data *Data) { |
Xinliang David Li | 23a66e4 | 2016-05-14 20:12:42 +0000 | [diff] [blame] | 119 | 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 Li | 911af1c | 2016-05-12 21:18:41 +0000 | [diff] [blame] | 140 | return 0; |
Xinliang David Li | 23a66e4 | 2016-05-14 20:12:42 +0000 | [diff] [blame] | 141 | |
| 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 Li | 911af1c | 2016-05-12 21:18:41 +0000 | [diff] [blame] | 147 | return -1; |
Xinliang David Li | 23a66e4 | 2016-05-14 20:12:42 +0000 | [diff] [blame] | 148 | |
| 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 Li | 911af1c | 2016-05-12 21:18:41 +0000 | [diff] [blame] | 204 | return 0; |
| 205 | } |
| 206 | |
Xinliang David Li | 54dd683 | 2015-12-29 07:13:59 +0000 | [diff] [blame] | 207 | static int writeValueProfData(WriterCallback Writer, void *WriterCtx, |
Xinliang David Li | 23a66e4 | 2016-05-14 20:12:42 +0000 | [diff] [blame] | 208 | VPDataReaderType *VPDataReader, |
Xinliang David Li | e9a8574d | 2016-05-10 00:17:31 +0000 | [diff] [blame] | 209 | const __llvm_profile_data *DataBegin, |
| 210 | const __llvm_profile_data *DataEnd) { |
Xinliang David Li | cda3bc2 | 2015-12-29 23:54:41 +0000 | [diff] [blame] | 211 | ProfBufferIO *BufferIO; |
Xinliang David Li | e9a8574d | 2016-05-10 00:17:31 +0000 | [diff] [blame] | 212 | const __llvm_profile_data *DI = 0; |
Xinliang David Li | 54dd683 | 2015-12-29 07:13:59 +0000 | [diff] [blame] | 213 | |
Xinliang David Li | 23a66e4 | 2016-05-14 20:12:42 +0000 | [diff] [blame] | 214 | if (!VPDataReader) |
Xinliang David Li | 54dd683 | 2015-12-29 07:13:59 +0000 | [diff] [blame] | 215 | return 0; |
| 216 | |
Xinliang David Li | 609fae3 | 2016-05-13 18:26:26 +0000 | [diff] [blame] | 217 | BufferIO = lprofCreateBufferIO(Writer, WriterCtx); |
Xinliang David Li | 54dd683 | 2015-12-29 07:13:59 +0000 | [diff] [blame] | 218 | |
Xinliang David Li | e9a8574d | 2016-05-10 00:17:31 +0000 | [diff] [blame] | 219 | for (DI = DataBegin; DI < DataEnd; DI++) { |
Xinliang David Li | 23a66e4 | 2016-05-14 20:12:42 +0000 | [diff] [blame] | 220 | if (writeOneValueProfData(BufferIO, VPDataReader, DI)) |
Xinliang David Li | 54dd683 | 2015-12-29 07:13:59 +0000 | [diff] [blame] | 221 | return -1; |
| 222 | } |
| 223 | |
Xinliang David Li | cf1a8d6 | 2016-03-06 04:18:13 +0000 | [diff] [blame] | 224 | if (lprofBufferIOFlush(BufferIO) != 0) |
Xinliang David Li | cda3bc2 | 2015-12-29 23:54:41 +0000 | [diff] [blame] | 225 | return -1; |
Xinliang David Li | cf1a8d6 | 2016-03-06 04:18:13 +0000 | [diff] [blame] | 226 | lprofDeleteBufferIO(BufferIO); |
Xinliang David Li | cda3bc2 | 2015-12-29 23:54:41 +0000 | [diff] [blame] | 227 | |
Xinliang David Li | 54dd683 | 2015-12-29 07:13:59 +0000 | [diff] [blame] | 228 | return 0; |
| 229 | } |
| 230 | |
Xinliang David Li | e9a8574d | 2016-05-10 00:17:31 +0000 | [diff] [blame] | 231 | COMPILER_RT_VISIBILITY int lprofWriteData(WriterCallback Writer, |
| 232 | void *WriterCtx, |
Xinliang David Li | 23a66e4 | 2016-05-14 20:12:42 +0000 | [diff] [blame] | 233 | VPDataReaderType *VPDataReader) { |
Xinliang David Li | e9a8574d | 2016-05-10 00:17:31 +0000 | [diff] [blame] | 234 | /* 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 Li | 23a66e4 | 2016-05-14 20:12:42 +0000 | [diff] [blame] | 242 | CountersBegin, CountersEnd, VPDataReader, |
Xinliang David Li | e9a8574d | 2016-05-10 00:17:31 +0000 | [diff] [blame] | 243 | NamesBegin, NamesEnd); |
| 244 | } |
| 245 | |
Xinliang David Li | cf1a8d6 | 2016-03-06 04:18:13 +0000 | [diff] [blame] | 246 | COMPILER_RT_VISIBILITY int |
| 247 | lprofWriteDataImpl(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 Li | 23a66e4 | 2016-05-14 20:12:42 +0000 | [diff] [blame] | 251 | VPDataReaderType *VPDataReader, const char *NamesBegin, |
Xinliang David Li | e9a8574d | 2016-05-10 00:17:31 +0000 | [diff] [blame] | 252 | const char *NamesEnd) { |
Xinliang David Li | 1d8d46a | 2015-11-18 21:08:03 +0000 | [diff] [blame] | 253 | |
| 254 | /* Calculate size of sections. */ |
Vedant Kumar | b850251 | 2016-02-26 02:49:41 +0000 | [diff] [blame] | 255 | const uint64_t DataSize = __llvm_profile_get_data_size(DataBegin, DataEnd); |
Xinliang David Li | 1d8d46a | 2015-11-18 21:08:03 +0000 | [diff] [blame] | 256 | 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 Li | e9a8574d | 2016-05-10 00:17:31 +0000 | [diff] [blame] | 269 | /* Initialize header structure. */ |
Xinliang David Li | 6376db5 | 2015-11-23 18:36:40 +0000 | [diff] [blame] | 270 | #define INSTR_PROF_RAW_HEADER(Type, Name, Init) Header.Name = Init; |
| 271 | #include "InstrProfData.inc" |
Xinliang David Li | 1d8d46a | 2015-11-18 21:08:03 +0000 | [diff] [blame] | 272 | |
Xinliang David Li | 2d5bf07 | 2015-11-21 04:16:42 +0000 | [diff] [blame] | 273 | /* Write the data. */ |
Xinliang David Li | 54dd683 | 2015-12-29 07:13:59 +0000 | [diff] [blame] | 274 | 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 Sonnenberger | d6487b2 | 2015-12-04 00:40:07 +0000 | [diff] [blame] | 279 | if (Writer(IOVec, sizeof(IOVec) / sizeof(*IOVec), &WriterCtx)) |
Xinliang David Li | 2d5bf07 | 2015-11-21 04:16:42 +0000 | [diff] [blame] | 280 | return -1; |
Xinliang David Li | 54dd683 | 2015-12-29 07:13:59 +0000 | [diff] [blame] | 281 | |
Xinliang David Li | 23a66e4 | 2016-05-14 20:12:42 +0000 | [diff] [blame] | 282 | return writeValueProfData(Writer, WriterCtx, VPDataReader, DataBegin, |
Xinliang David Li | e9a8574d | 2016-05-10 00:17:31 +0000 | [diff] [blame] | 283 | DataEnd); |
Xinliang David Li | 1d8d46a | 2015-11-18 21:08:03 +0000 | [diff] [blame] | 284 | } |