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