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