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 #ifdef _MSC_VER 11 /* For _alloca */ 12 #include <malloc.h> 13 #endif 14 #include <string.h> 15 16 #include "InstrProfiling.h" 17 #include "InstrProfilingInternal.h" 18 19 #define INSTR_PROF_VALUE_PROF_DATA 20 #include "InstrProfData.inc" 21 22 COMPILER_RT_VISIBILITY void (*FreeHook)(void *) = NULL; 23 static ProfBufferIO TheBufferIO; 24 #define VP_BUFFER_SIZE 8 * 1024 25 static uint8_t BufferIOBuffer[VP_BUFFER_SIZE]; 26 static InstrProfValueData VPDataArray[16]; 27 static uint32_t VPDataArraySize = sizeof(VPDataArray) / sizeof(*VPDataArray); 28 29 COMPILER_RT_VISIBILITY uint8_t *DynamicBufferIOBuffer = 0; 30 COMPILER_RT_VISIBILITY uint32_t VPBufferSize = 0; 31 32 /* The buffer writer is reponsponsible in keeping writer state 33 * across the call. 34 */ 35 COMPILER_RT_VISIBILITY uint32_t lprofBufferWriter(ProfDataWriter *This, 36 ProfDataIOVec *IOVecs, 37 uint32_t NumIOVecs) { 38 uint32_t I; 39 char **Buffer = (char **)&This->WriterCtx; 40 for (I = 0; I < NumIOVecs; I++) { 41 size_t Length = IOVecs[I].ElmSize * IOVecs[I].NumElm; 42 if (IOVecs[I].Data) 43 memcpy(*Buffer, IOVecs[I].Data, Length); 44 *Buffer += Length; 45 } 46 return 0; 47 } 48 49 static void llvmInitBufferIO(ProfBufferIO *BufferIO, ProfDataWriter *FileWriter, 50 uint8_t *Buffer, uint32_t BufferSz) { 51 BufferIO->FileWriter = FileWriter; 52 BufferIO->OwnFileWriter = 0; 53 BufferIO->BufferStart = Buffer; 54 BufferIO->BufferSz = BufferSz; 55 BufferIO->CurOffset = 0; 56 } 57 58 COMPILER_RT_VISIBILITY ProfBufferIO * 59 lprofCreateBufferIO(ProfDataWriter *FileWriter) { 60 uint8_t *Buffer = DynamicBufferIOBuffer; 61 uint32_t BufferSize = VPBufferSize; 62 if (!Buffer) { 63 Buffer = &BufferIOBuffer[0]; 64 BufferSize = sizeof(BufferIOBuffer); 65 } 66 llvmInitBufferIO(&TheBufferIO, FileWriter, Buffer, BufferSize); 67 return &TheBufferIO; 68 } 69 70 COMPILER_RT_VISIBILITY void lprofDeleteBufferIO(ProfBufferIO *BufferIO) { 71 if (BufferIO->OwnFileWriter) 72 FreeHook(BufferIO->FileWriter); 73 if (DynamicBufferIOBuffer) { 74 FreeHook(DynamicBufferIOBuffer); 75 DynamicBufferIOBuffer = 0; 76 VPBufferSize = 0; 77 } 78 } 79 80 COMPILER_RT_VISIBILITY int 81 lprofBufferIOWrite(ProfBufferIO *BufferIO, const uint8_t *Data, uint32_t Size) { 82 /* Buffer is not large enough, it is time to flush. */ 83 if (Size + BufferIO->CurOffset > BufferIO->BufferSz) { 84 if (lprofBufferIOFlush(BufferIO) != 0) 85 return -1; 86 } 87 /* Special case, bypass the buffer completely. */ 88 ProfDataIOVec IO[] = {{Data, sizeof(uint8_t), Size}}; 89 if (Size > BufferIO->BufferSz) { 90 if (BufferIO->FileWriter->Write(BufferIO->FileWriter, IO, 1)) 91 return -1; 92 } else { 93 /* Write the data to buffer */ 94 uint8_t *Buffer = BufferIO->BufferStart + BufferIO->CurOffset; 95 ProfDataWriter BufferWriter; 96 initBufferWriter(&BufferWriter, (char *)Buffer); 97 lprofBufferWriter(&BufferWriter, IO, 1); 98 BufferIO->CurOffset = 99 (uint8_t *)BufferWriter.WriterCtx - BufferIO->BufferStart; 100 } 101 return 0; 102 } 103 104 COMPILER_RT_VISIBILITY int lprofBufferIOFlush(ProfBufferIO *BufferIO) { 105 if (BufferIO->CurOffset) { 106 ProfDataIOVec IO[] = { 107 {BufferIO->BufferStart, sizeof(uint8_t), BufferIO->CurOffset}}; 108 if (BufferIO->FileWriter->Write(BufferIO->FileWriter, IO, 1)) 109 return -1; 110 BufferIO->CurOffset = 0; 111 } 112 return 0; 113 } 114 115 /* Write out value profile data for function specified with \c Data. 116 * The implementation does not use the method \c serializeValueProfData 117 * which depends on dynamic memory allocation. In this implementation, 118 * value profile data is written out to \c BufferIO piecemeal. 119 */ 120 static int writeOneValueProfData(ProfBufferIO *BufferIO, 121 VPDataReaderType *VPDataReader, 122 const __llvm_profile_data *Data) { 123 unsigned I, NumValueKinds = 0; 124 ValueProfData VPHeader; 125 uint8_t *SiteCountArray[IPVK_Last + 1]; 126 127 for (I = 0; I <= IPVK_Last; I++) { 128 if (!Data->NumValueSites[I]) 129 SiteCountArray[I] = 0; 130 else { 131 uint32_t Sz = 132 VPDataReader->GetValueProfRecordHeaderSize(Data->NumValueSites[I]) - 133 offsetof(ValueProfRecord, SiteCountArray); 134 /* Only use alloca for this small byte array to avoid excessive 135 * stack growth. */ 136 SiteCountArray[I] = (uint8_t *)COMPILER_RT_ALLOCA(Sz); 137 memset(SiteCountArray[I], 0, Sz); 138 } 139 } 140 141 /* If NumValueKinds returned is 0, there is nothing to write, report 142 success and return. This should match the raw profile reader's behavior. */ 143 if (!(NumValueKinds = VPDataReader->InitRTRecord(Data, SiteCountArray))) 144 return 0; 145 146 /* First write the header structure. */ 147 VPHeader.TotalSize = VPDataReader->GetValueProfDataSize(); 148 VPHeader.NumValueKinds = NumValueKinds; 149 if (lprofBufferIOWrite(BufferIO, (const uint8_t *)&VPHeader, 150 sizeof(ValueProfData))) 151 return -1; 152 153 /* Make sure nothing else needs to be written before value profile 154 * records. */ 155 if ((void *)VPDataReader->GetFirstValueProfRecord(&VPHeader) != 156 (void *)(&VPHeader + 1)) 157 return -1; 158 159 /* Write out the value profile record for each value kind 160 * one by one. */ 161 for (I = 0; I <= IPVK_Last; I++) { 162 uint32_t J; 163 ValueProfRecord RecordHeader; 164 /* The size of the value prof record header without counting the 165 * site count array .*/ 166 uint32_t RecordHeaderSize = offsetof(ValueProfRecord, SiteCountArray); 167 uint32_t SiteCountArraySize; 168 169 if (!Data->NumValueSites[I]) 170 continue; 171 172 /* Write out the record header. */ 173 RecordHeader.Kind = I; 174 RecordHeader.NumValueSites = Data->NumValueSites[I]; 175 if (lprofBufferIOWrite(BufferIO, (const uint8_t *)&RecordHeader, 176 RecordHeaderSize)) 177 return -1; 178 179 /* Write out the site value count array including padding space. */ 180 SiteCountArraySize = 181 VPDataReader->GetValueProfRecordHeaderSize(Data->NumValueSites[I]) - 182 RecordHeaderSize; 183 if (lprofBufferIOWrite(BufferIO, SiteCountArray[I], SiteCountArraySize)) 184 return -1; 185 186 /* Write out the value profile data for each value site. */ 187 for (J = 0; J < Data->NumValueSites[I]; J++) { 188 uint32_t NRead, NRemain; 189 ValueProfNode *NextStartNode = 0; 190 NRemain = VPDataReader->GetNumValueDataForSite(I, J); 191 if (!NRemain) 192 continue; 193 /* Read and write out value data in small chunks till it is done. */ 194 do { 195 NRead = (NRemain > VPDataArraySize ? VPDataArraySize : NRemain); 196 NextStartNode = 197 VPDataReader->GetValueData(I, /* ValueKind */ 198 J, /* Site */ 199 &VPDataArray[0], NextStartNode, NRead); 200 if (lprofBufferIOWrite(BufferIO, (const uint8_t *)&VPDataArray[0], 201 NRead * sizeof(InstrProfValueData))) 202 return -1; 203 NRemain -= NRead; 204 } while (NRemain != 0); 205 } 206 } 207 /* All done report success. */ 208 return 0; 209 } 210 211 static int writeValueProfData(ProfDataWriter *Writer, 212 VPDataReaderType *VPDataReader, 213 const __llvm_profile_data *DataBegin, 214 const __llvm_profile_data *DataEnd) { 215 ProfBufferIO *BufferIO; 216 const __llvm_profile_data *DI = 0; 217 218 if (!VPDataReader) 219 return 0; 220 221 BufferIO = lprofCreateBufferIO(Writer); 222 223 for (DI = DataBegin; DI < DataEnd; DI++) { 224 if (writeOneValueProfData(BufferIO, VPDataReader, DI)) 225 return -1; 226 } 227 228 if (lprofBufferIOFlush(BufferIO) != 0) 229 return -1; 230 lprofDeleteBufferIO(BufferIO); 231 232 return 0; 233 } 234 235 COMPILER_RT_VISIBILITY int lprofWriteData(ProfDataWriter *Writer, 236 VPDataReaderType *VPDataReader, 237 int SkipNameDataWrite) { 238 /* Match logic in __llvm_profile_write_buffer(). */ 239 const __llvm_profile_data *DataBegin = __llvm_profile_begin_data(); 240 const __llvm_profile_data *DataEnd = __llvm_profile_end_data(); 241 const uint64_t *CountersBegin = __llvm_profile_begin_counters(); 242 const uint64_t *CountersEnd = __llvm_profile_end_counters(); 243 const char *NamesBegin = __llvm_profile_begin_names(); 244 const char *NamesEnd = __llvm_profile_end_names(); 245 return lprofWriteDataImpl(Writer, DataBegin, DataEnd, CountersBegin, 246 CountersEnd, VPDataReader, NamesBegin, NamesEnd, 247 SkipNameDataWrite); 248 } 249 250 COMPILER_RT_VISIBILITY int 251 lprofWriteDataImpl(ProfDataWriter *Writer, const __llvm_profile_data *DataBegin, 252 const __llvm_profile_data *DataEnd, 253 const uint64_t *CountersBegin, const uint64_t *CountersEnd, 254 VPDataReaderType *VPDataReader, const char *NamesBegin, 255 const char *NamesEnd, int SkipNameDataWrite) { 256 257 /* Calculate size of sections. */ 258 const uint64_t DataSize = __llvm_profile_get_data_size(DataBegin, DataEnd); 259 const uint64_t CountersSize = CountersEnd - CountersBegin; 260 const uint64_t NamesSize = NamesEnd - NamesBegin; 261 const uint64_t Padding = __llvm_profile_get_num_padding_bytes(NamesSize); 262 263 /* Enough zeroes for padding. */ 264 const char Zeroes[sizeof(uint64_t)] = {0}; 265 266 /* Create the header. */ 267 __llvm_profile_header Header; 268 269 if (!DataSize) 270 return 0; 271 272 /* Initialize header structure. */ 273 #define INSTR_PROF_RAW_HEADER(Type, Name, Init) Header.Name = Init; 274 #include "InstrProfData.inc" 275 276 /* Write the data. */ 277 ProfDataIOVec IOVec[] = { 278 {&Header, sizeof(__llvm_profile_header), 1}, 279 {DataBegin, sizeof(__llvm_profile_data), DataSize}, 280 {CountersBegin, sizeof(uint64_t), CountersSize}, 281 {SkipNameDataWrite ? NULL : NamesBegin, sizeof(uint8_t), NamesSize}, 282 {Zeroes, sizeof(uint8_t), Padding}}; 283 if (Writer->Write(Writer, IOVec, sizeof(IOVec) / sizeof(*IOVec))) 284 return -1; 285 286 return writeValueProfData(Writer, VPDataReader, DataBegin, DataEnd); 287 } 288