10b57cec5SDimitry Andric /*===- InstrProfilingWriter.c - Write instrumentation to a file or buffer -===*\ 20b57cec5SDimitry Andric |* 30b57cec5SDimitry Andric |* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric |* See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric |* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric |* 70b57cec5SDimitry Andric \*===----------------------------------------------------------------------===*/ 80b57cec5SDimitry Andric 9e8d8bef9SDimitry Andric // Note: This is linked into the Darwin kernel, and must remain compatible 10e8d8bef9SDimitry Andric // with freestanding compilation. See `darwin_add_builtin_libraries`. 11e8d8bef9SDimitry Andric 120b57cec5SDimitry Andric #ifdef _MSC_VER 130b57cec5SDimitry Andric /* For _alloca */ 140b57cec5SDimitry Andric #include <malloc.h> 150b57cec5SDimitry Andric #endif 160b57cec5SDimitry Andric #include <string.h> 170b57cec5SDimitry Andric 180b57cec5SDimitry Andric #include "InstrProfiling.h" 190b57cec5SDimitry Andric #include "InstrProfilingInternal.h" 20480093f4SDimitry Andric #include "InstrProfilingPort.h" 210b57cec5SDimitry Andric 220b57cec5SDimitry Andric #define INSTR_PROF_VALUE_PROF_DATA 23480093f4SDimitry Andric #include "profile/InstrProfData.inc" 240b57cec5SDimitry Andric 250b57cec5SDimitry Andric COMPILER_RT_VISIBILITY void (*FreeHook)(void *) = NULL; 260b57cec5SDimitry Andric static ProfBufferIO TheBufferIO; 270b57cec5SDimitry Andric #define VP_BUFFER_SIZE 8 * 1024 280b57cec5SDimitry Andric static uint8_t BufferIOBuffer[VP_BUFFER_SIZE]; 290b57cec5SDimitry Andric static InstrProfValueData VPDataArray[16]; 300b57cec5SDimitry Andric static uint32_t VPDataArraySize = sizeof(VPDataArray) / sizeof(*VPDataArray); 310b57cec5SDimitry Andric 320b57cec5SDimitry Andric COMPILER_RT_VISIBILITY uint8_t *DynamicBufferIOBuffer = 0; 330b57cec5SDimitry Andric COMPILER_RT_VISIBILITY uint32_t VPBufferSize = 0; 340b57cec5SDimitry Andric 35349cc55cSDimitry Andric /* The buffer writer is responsible in keeping writer state 360b57cec5SDimitry Andric * across the call. 370b57cec5SDimitry Andric */ 380b57cec5SDimitry Andric COMPILER_RT_VISIBILITY uint32_t lprofBufferWriter(ProfDataWriter *This, 390b57cec5SDimitry Andric ProfDataIOVec *IOVecs, 400b57cec5SDimitry Andric uint32_t NumIOVecs) { 410b57cec5SDimitry Andric uint32_t I; 420b57cec5SDimitry Andric char **Buffer = (char **)&This->WriterCtx; 430b57cec5SDimitry Andric for (I = 0; I < NumIOVecs; I++) { 440b57cec5SDimitry Andric size_t Length = IOVecs[I].ElmSize * IOVecs[I].NumElm; 450b57cec5SDimitry Andric if (IOVecs[I].Data) 460b57cec5SDimitry Andric memcpy(*Buffer, IOVecs[I].Data, Length); 47480093f4SDimitry Andric else if (IOVecs[I].UseZeroPadding) { 48480093f4SDimitry Andric /* Allocating the buffer should zero fill. */ 49480093f4SDimitry Andric } 500b57cec5SDimitry Andric *Buffer += Length; 510b57cec5SDimitry Andric } 520b57cec5SDimitry Andric return 0; 530b57cec5SDimitry Andric } 540b57cec5SDimitry Andric 550b57cec5SDimitry Andric static void llvmInitBufferIO(ProfBufferIO *BufferIO, ProfDataWriter *FileWriter, 560b57cec5SDimitry Andric uint8_t *Buffer, uint32_t BufferSz) { 570b57cec5SDimitry Andric BufferIO->FileWriter = FileWriter; 580b57cec5SDimitry Andric BufferIO->OwnFileWriter = 0; 590b57cec5SDimitry Andric BufferIO->BufferStart = Buffer; 600b57cec5SDimitry Andric BufferIO->BufferSz = BufferSz; 610b57cec5SDimitry Andric BufferIO->CurOffset = 0; 620b57cec5SDimitry Andric } 630b57cec5SDimitry Andric 640b57cec5SDimitry Andric COMPILER_RT_VISIBILITY ProfBufferIO * 650b57cec5SDimitry Andric lprofCreateBufferIO(ProfDataWriter *FileWriter) { 660b57cec5SDimitry Andric uint8_t *Buffer = DynamicBufferIOBuffer; 670b57cec5SDimitry Andric uint32_t BufferSize = VPBufferSize; 680b57cec5SDimitry Andric if (!Buffer) { 690b57cec5SDimitry Andric Buffer = &BufferIOBuffer[0]; 700b57cec5SDimitry Andric BufferSize = sizeof(BufferIOBuffer); 710b57cec5SDimitry Andric } 720b57cec5SDimitry Andric llvmInitBufferIO(&TheBufferIO, FileWriter, Buffer, BufferSize); 730b57cec5SDimitry Andric return &TheBufferIO; 740b57cec5SDimitry Andric } 750b57cec5SDimitry Andric 760b57cec5SDimitry Andric COMPILER_RT_VISIBILITY void lprofDeleteBufferIO(ProfBufferIO *BufferIO) { 770b57cec5SDimitry Andric if (BufferIO->OwnFileWriter) 780b57cec5SDimitry Andric FreeHook(BufferIO->FileWriter); 790b57cec5SDimitry Andric if (DynamicBufferIOBuffer) { 800b57cec5SDimitry Andric FreeHook(DynamicBufferIOBuffer); 810b57cec5SDimitry Andric DynamicBufferIOBuffer = 0; 820b57cec5SDimitry Andric VPBufferSize = 0; 830b57cec5SDimitry Andric } 840b57cec5SDimitry Andric } 850b57cec5SDimitry Andric 860b57cec5SDimitry Andric COMPILER_RT_VISIBILITY int 870b57cec5SDimitry Andric lprofBufferIOWrite(ProfBufferIO *BufferIO, const uint8_t *Data, uint32_t Size) { 880b57cec5SDimitry Andric /* Buffer is not large enough, it is time to flush. */ 890b57cec5SDimitry Andric if (Size + BufferIO->CurOffset > BufferIO->BufferSz) { 900b57cec5SDimitry Andric if (lprofBufferIOFlush(BufferIO) != 0) 910b57cec5SDimitry Andric return -1; 920b57cec5SDimitry Andric } 930b57cec5SDimitry Andric /* Special case, bypass the buffer completely. */ 94480093f4SDimitry Andric ProfDataIOVec IO[] = {{Data, sizeof(uint8_t), Size, 0}}; 950b57cec5SDimitry Andric if (Size > BufferIO->BufferSz) { 960b57cec5SDimitry Andric if (BufferIO->FileWriter->Write(BufferIO->FileWriter, IO, 1)) 970b57cec5SDimitry Andric return -1; 980b57cec5SDimitry Andric } else { 990b57cec5SDimitry Andric /* Write the data to buffer */ 1000b57cec5SDimitry Andric uint8_t *Buffer = BufferIO->BufferStart + BufferIO->CurOffset; 1010b57cec5SDimitry Andric ProfDataWriter BufferWriter; 1020b57cec5SDimitry Andric initBufferWriter(&BufferWriter, (char *)Buffer); 1030b57cec5SDimitry Andric lprofBufferWriter(&BufferWriter, IO, 1); 1040b57cec5SDimitry Andric BufferIO->CurOffset = 1050b57cec5SDimitry Andric (uint8_t *)BufferWriter.WriterCtx - BufferIO->BufferStart; 1060b57cec5SDimitry Andric } 1070b57cec5SDimitry Andric return 0; 1080b57cec5SDimitry Andric } 1090b57cec5SDimitry Andric 1100b57cec5SDimitry Andric COMPILER_RT_VISIBILITY int lprofBufferIOFlush(ProfBufferIO *BufferIO) { 1110b57cec5SDimitry Andric if (BufferIO->CurOffset) { 1120b57cec5SDimitry Andric ProfDataIOVec IO[] = { 113480093f4SDimitry Andric {BufferIO->BufferStart, sizeof(uint8_t), BufferIO->CurOffset, 0}}; 1140b57cec5SDimitry Andric if (BufferIO->FileWriter->Write(BufferIO->FileWriter, IO, 1)) 1150b57cec5SDimitry Andric return -1; 1160b57cec5SDimitry Andric BufferIO->CurOffset = 0; 1170b57cec5SDimitry Andric } 1180b57cec5SDimitry Andric return 0; 1190b57cec5SDimitry Andric } 1200b57cec5SDimitry Andric 1210b57cec5SDimitry Andric /* Write out value profile data for function specified with \c Data. 1220b57cec5SDimitry Andric * The implementation does not use the method \c serializeValueProfData 1230b57cec5SDimitry Andric * which depends on dynamic memory allocation. In this implementation, 1240b57cec5SDimitry Andric * value profile data is written out to \c BufferIO piecemeal. 1250b57cec5SDimitry Andric */ 1260b57cec5SDimitry Andric static int writeOneValueProfData(ProfBufferIO *BufferIO, 1270b57cec5SDimitry Andric VPDataReaderType *VPDataReader, 1280b57cec5SDimitry Andric const __llvm_profile_data *Data) { 1290b57cec5SDimitry Andric unsigned I, NumValueKinds = 0; 1300b57cec5SDimitry Andric ValueProfData VPHeader; 1310b57cec5SDimitry Andric uint8_t *SiteCountArray[IPVK_Last + 1]; 1320b57cec5SDimitry Andric 1330b57cec5SDimitry Andric for (I = 0; I <= IPVK_Last; I++) { 1340b57cec5SDimitry Andric if (!Data->NumValueSites[I]) 1350b57cec5SDimitry Andric SiteCountArray[I] = 0; 1360b57cec5SDimitry Andric else { 1370b57cec5SDimitry Andric uint32_t Sz = 1380b57cec5SDimitry Andric VPDataReader->GetValueProfRecordHeaderSize(Data->NumValueSites[I]) - 1390b57cec5SDimitry Andric offsetof(ValueProfRecord, SiteCountArray); 1400b57cec5SDimitry Andric /* Only use alloca for this small byte array to avoid excessive 1410b57cec5SDimitry Andric * stack growth. */ 1420b57cec5SDimitry Andric SiteCountArray[I] = (uint8_t *)COMPILER_RT_ALLOCA(Sz); 1430b57cec5SDimitry Andric memset(SiteCountArray[I], 0, Sz); 1440b57cec5SDimitry Andric } 1450b57cec5SDimitry Andric } 1460b57cec5SDimitry Andric 1470b57cec5SDimitry Andric /* If NumValueKinds returned is 0, there is nothing to write, report 1480b57cec5SDimitry Andric success and return. This should match the raw profile reader's behavior. */ 1490b57cec5SDimitry Andric if (!(NumValueKinds = VPDataReader->InitRTRecord(Data, SiteCountArray))) 1500b57cec5SDimitry Andric return 0; 1510b57cec5SDimitry Andric 1520b57cec5SDimitry Andric /* First write the header structure. */ 1530b57cec5SDimitry Andric VPHeader.TotalSize = VPDataReader->GetValueProfDataSize(); 1540b57cec5SDimitry Andric VPHeader.NumValueKinds = NumValueKinds; 1550b57cec5SDimitry Andric if (lprofBufferIOWrite(BufferIO, (const uint8_t *)&VPHeader, 1560b57cec5SDimitry Andric sizeof(ValueProfData))) 1570b57cec5SDimitry Andric return -1; 1580b57cec5SDimitry Andric 1590b57cec5SDimitry Andric /* Make sure nothing else needs to be written before value profile 1600b57cec5SDimitry Andric * records. */ 1610b57cec5SDimitry Andric if ((void *)VPDataReader->GetFirstValueProfRecord(&VPHeader) != 1620b57cec5SDimitry Andric (void *)(&VPHeader + 1)) 1630b57cec5SDimitry Andric return -1; 1640b57cec5SDimitry Andric 1650b57cec5SDimitry Andric /* Write out the value profile record for each value kind 1660b57cec5SDimitry Andric * one by one. */ 1670b57cec5SDimitry Andric for (I = 0; I <= IPVK_Last; I++) { 1680b57cec5SDimitry Andric uint32_t J; 1690b57cec5SDimitry Andric ValueProfRecord RecordHeader; 1700b57cec5SDimitry Andric /* The size of the value prof record header without counting the 1710b57cec5SDimitry Andric * site count array .*/ 1720b57cec5SDimitry Andric uint32_t RecordHeaderSize = offsetof(ValueProfRecord, SiteCountArray); 1730b57cec5SDimitry Andric uint32_t SiteCountArraySize; 1740b57cec5SDimitry Andric 1750b57cec5SDimitry Andric if (!Data->NumValueSites[I]) 1760b57cec5SDimitry Andric continue; 1770b57cec5SDimitry Andric 1780b57cec5SDimitry Andric /* Write out the record header. */ 1790b57cec5SDimitry Andric RecordHeader.Kind = I; 1800b57cec5SDimitry Andric RecordHeader.NumValueSites = Data->NumValueSites[I]; 1810b57cec5SDimitry Andric if (lprofBufferIOWrite(BufferIO, (const uint8_t *)&RecordHeader, 1820b57cec5SDimitry Andric RecordHeaderSize)) 1830b57cec5SDimitry Andric return -1; 1840b57cec5SDimitry Andric 1850b57cec5SDimitry Andric /* Write out the site value count array including padding space. */ 1860b57cec5SDimitry Andric SiteCountArraySize = 1870b57cec5SDimitry Andric VPDataReader->GetValueProfRecordHeaderSize(Data->NumValueSites[I]) - 1880b57cec5SDimitry Andric RecordHeaderSize; 1890b57cec5SDimitry Andric if (lprofBufferIOWrite(BufferIO, SiteCountArray[I], SiteCountArraySize)) 1900b57cec5SDimitry Andric return -1; 1910b57cec5SDimitry Andric 1920b57cec5SDimitry Andric /* Write out the value profile data for each value site. */ 1930b57cec5SDimitry Andric for (J = 0; J < Data->NumValueSites[I]; J++) { 1940b57cec5SDimitry Andric uint32_t NRead, NRemain; 1950b57cec5SDimitry Andric ValueProfNode *NextStartNode = 0; 1960b57cec5SDimitry Andric NRemain = VPDataReader->GetNumValueDataForSite(I, J); 1970b57cec5SDimitry Andric if (!NRemain) 1980b57cec5SDimitry Andric continue; 1990b57cec5SDimitry Andric /* Read and write out value data in small chunks till it is done. */ 2000b57cec5SDimitry Andric do { 2010b57cec5SDimitry Andric NRead = (NRemain > VPDataArraySize ? VPDataArraySize : NRemain); 2020b57cec5SDimitry Andric NextStartNode = 2030b57cec5SDimitry Andric VPDataReader->GetValueData(I, /* ValueKind */ 2040b57cec5SDimitry Andric J, /* Site */ 2050b57cec5SDimitry Andric &VPDataArray[0], NextStartNode, NRead); 2060b57cec5SDimitry Andric if (lprofBufferIOWrite(BufferIO, (const uint8_t *)&VPDataArray[0], 2070b57cec5SDimitry Andric NRead * sizeof(InstrProfValueData))) 2080b57cec5SDimitry Andric return -1; 2090b57cec5SDimitry Andric NRemain -= NRead; 2100b57cec5SDimitry Andric } while (NRemain != 0); 2110b57cec5SDimitry Andric } 2120b57cec5SDimitry Andric } 2130b57cec5SDimitry Andric /* All done report success. */ 2140b57cec5SDimitry Andric return 0; 2150b57cec5SDimitry Andric } 2160b57cec5SDimitry Andric 2170b57cec5SDimitry Andric static int writeValueProfData(ProfDataWriter *Writer, 2180b57cec5SDimitry Andric VPDataReaderType *VPDataReader, 2190b57cec5SDimitry Andric const __llvm_profile_data *DataBegin, 2200b57cec5SDimitry Andric const __llvm_profile_data *DataEnd) { 2210b57cec5SDimitry Andric ProfBufferIO *BufferIO; 2220b57cec5SDimitry Andric const __llvm_profile_data *DI = 0; 2230b57cec5SDimitry Andric 2240b57cec5SDimitry Andric if (!VPDataReader) 2250b57cec5SDimitry Andric return 0; 2260b57cec5SDimitry Andric 2270b57cec5SDimitry Andric BufferIO = lprofCreateBufferIO(Writer); 2280b57cec5SDimitry Andric 2290b57cec5SDimitry Andric for (DI = DataBegin; DI < DataEnd; DI++) { 2300b57cec5SDimitry Andric if (writeOneValueProfData(BufferIO, VPDataReader, DI)) 2310b57cec5SDimitry Andric return -1; 2320b57cec5SDimitry Andric } 2330b57cec5SDimitry Andric 2340b57cec5SDimitry Andric if (lprofBufferIOFlush(BufferIO) != 0) 2350b57cec5SDimitry Andric return -1; 2360b57cec5SDimitry Andric lprofDeleteBufferIO(BufferIO); 2370b57cec5SDimitry Andric 2380b57cec5SDimitry Andric return 0; 2390b57cec5SDimitry Andric } 2400b57cec5SDimitry Andric 2410b57cec5SDimitry Andric COMPILER_RT_VISIBILITY int lprofWriteData(ProfDataWriter *Writer, 2420b57cec5SDimitry Andric VPDataReaderType *VPDataReader, 2430b57cec5SDimitry Andric int SkipNameDataWrite) { 2440b57cec5SDimitry Andric /* Match logic in __llvm_profile_write_buffer(). */ 2450b57cec5SDimitry Andric const __llvm_profile_data *DataBegin = __llvm_profile_begin_data(); 2460b57cec5SDimitry Andric const __llvm_profile_data *DataEnd = __llvm_profile_end_data(); 24704eeddc0SDimitry Andric const char *CountersBegin = __llvm_profile_begin_counters(); 24804eeddc0SDimitry Andric const char *CountersEnd = __llvm_profile_end_counters(); 2495f757f3fSDimitry Andric const char *BitmapBegin = __llvm_profile_begin_bitmap(); 2505f757f3fSDimitry Andric const char *BitmapEnd = __llvm_profile_end_bitmap(); 2510b57cec5SDimitry Andric const char *NamesBegin = __llvm_profile_begin_names(); 2520b57cec5SDimitry Andric const char *NamesEnd = __llvm_profile_end_names(); 253*0fca6ea1SDimitry Andric const VTableProfData *VTableBegin = __llvm_profile_begin_vtables(); 254*0fca6ea1SDimitry Andric const VTableProfData *VTableEnd = __llvm_profile_end_vtables(); 255*0fca6ea1SDimitry Andric const char *VNamesBegin = __llvm_profile_begin_vtabnames(); 256*0fca6ea1SDimitry Andric const char *VNamesEnd = __llvm_profile_end_vtabnames(); 2570b57cec5SDimitry Andric return lprofWriteDataImpl(Writer, DataBegin, DataEnd, CountersBegin, 2585f757f3fSDimitry Andric CountersEnd, BitmapBegin, BitmapEnd, VPDataReader, 259*0fca6ea1SDimitry Andric NamesBegin, NamesEnd, VTableBegin, VTableEnd, 260*0fca6ea1SDimitry Andric VNamesBegin, VNamesEnd, SkipNameDataWrite); 2610b57cec5SDimitry Andric } 2620b57cec5SDimitry Andric 2630b57cec5SDimitry Andric COMPILER_RT_VISIBILITY int 2640b57cec5SDimitry Andric lprofWriteDataImpl(ProfDataWriter *Writer, const __llvm_profile_data *DataBegin, 2650b57cec5SDimitry Andric const __llvm_profile_data *DataEnd, 26604eeddc0SDimitry Andric const char *CountersBegin, const char *CountersEnd, 2675f757f3fSDimitry Andric const char *BitmapBegin, const char *BitmapEnd, 2680b57cec5SDimitry Andric VPDataReaderType *VPDataReader, const char *NamesBegin, 269*0fca6ea1SDimitry Andric const char *NamesEnd, const VTableProfData *VTableBegin, 270*0fca6ea1SDimitry Andric const VTableProfData *VTableEnd, const char *VNamesBegin, 271*0fca6ea1SDimitry Andric const char *VNamesEnd, int SkipNameDataWrite) { 2720b57cec5SDimitry Andric /* Calculate size of sections. */ 273bdd1243dSDimitry Andric const uint64_t DataSectionSize = 2745f757f3fSDimitry Andric __llvm_profile_get_data_size(DataBegin, DataEnd); 2755f757f3fSDimitry Andric const uint64_t NumData = __llvm_profile_get_num_data(DataBegin, DataEnd); 276bdd1243dSDimitry Andric const uint64_t CountersSectionSize = 27704eeddc0SDimitry Andric __llvm_profile_get_counters_size(CountersBegin, CountersEnd); 27804eeddc0SDimitry Andric const uint64_t NumCounters = 27904eeddc0SDimitry Andric __llvm_profile_get_num_counters(CountersBegin, CountersEnd); 2805f757f3fSDimitry Andric const uint64_t NumBitmapBytes = 2815f757f3fSDimitry Andric __llvm_profile_get_num_bitmap_bytes(BitmapBegin, BitmapEnd); 2825f757f3fSDimitry Andric const uint64_t NamesSize = __llvm_profile_get_name_size(NamesBegin, NamesEnd); 283*0fca6ea1SDimitry Andric const uint64_t NumVTables = 284*0fca6ea1SDimitry Andric __llvm_profile_get_num_vtable(VTableBegin, VTableEnd); 285*0fca6ea1SDimitry Andric const uint64_t VTableSectionSize = 286*0fca6ea1SDimitry Andric __llvm_profile_get_vtable_section_size(VTableBegin, VTableEnd); 287*0fca6ea1SDimitry Andric const uint64_t VNamesSize = 288*0fca6ea1SDimitry Andric __llvm_profile_get_name_size(VNamesBegin, VNamesEnd); 2890b57cec5SDimitry Andric 2900b57cec5SDimitry Andric /* Create the header. */ 2910b57cec5SDimitry Andric __llvm_profile_header Header; 2920b57cec5SDimitry Andric 293480093f4SDimitry Andric /* Determine how much padding is needed before/after the counters and after 294480093f4SDimitry Andric * the names. */ 295480093f4SDimitry Andric uint64_t PaddingBytesBeforeCounters, PaddingBytesAfterCounters, 296*0fca6ea1SDimitry Andric PaddingBytesAfterBitmapBytes, PaddingBytesAfterNames, 297*0fca6ea1SDimitry Andric PaddingBytesAfterVTable, PaddingBytesAfterVNames; 298*0fca6ea1SDimitry Andric if (__llvm_profile_get_padding_sizes_for_counters( 2995f757f3fSDimitry Andric DataSectionSize, CountersSectionSize, NumBitmapBytes, NamesSize, 300*0fca6ea1SDimitry Andric VTableSectionSize, VNamesSize, &PaddingBytesBeforeCounters, 301*0fca6ea1SDimitry Andric &PaddingBytesAfterCounters, &PaddingBytesAfterBitmapBytes, 302*0fca6ea1SDimitry Andric &PaddingBytesAfterNames, &PaddingBytesAfterVTable, 303*0fca6ea1SDimitry Andric &PaddingBytesAfterVNames) == -1) 304*0fca6ea1SDimitry Andric return -1; 305480093f4SDimitry Andric 30604eeddc0SDimitry Andric { 3070b57cec5SDimitry Andric /* Initialize header structure. */ 3080b57cec5SDimitry Andric #define INSTR_PROF_RAW_HEADER(Type, Name, Init) Header.Name = Init; 309480093f4SDimitry Andric #include "profile/InstrProfData.inc" 31004eeddc0SDimitry Andric } 3110b57cec5SDimitry Andric 312349cc55cSDimitry Andric /* On WIN64, label differences are truncated 32-bit values. Truncate 313349cc55cSDimitry Andric * CountersDelta to match. */ 314349cc55cSDimitry Andric #ifdef _WIN64 315349cc55cSDimitry Andric Header.CountersDelta = (uint32_t)Header.CountersDelta; 3165f757f3fSDimitry Andric Header.BitmapDelta = (uint32_t)Header.BitmapDelta; 317349cc55cSDimitry Andric #endif 318349cc55cSDimitry Andric 3190eae32dcSDimitry Andric /* The data and names sections are omitted in lightweight mode. */ 3205f757f3fSDimitry Andric if (NumData == 0 && NamesSize == 0) { 3210eae32dcSDimitry Andric Header.CountersDelta = 0; 3220eae32dcSDimitry Andric Header.NamesDelta = 0; 3230eae32dcSDimitry Andric } 3240eae32dcSDimitry Andric 325fe6060f1SDimitry Andric /* Write the profile header. */ 326fe6060f1SDimitry Andric ProfDataIOVec IOVec[] = {{&Header, sizeof(__llvm_profile_header), 1, 0}}; 327fe6060f1SDimitry Andric if (Writer->Write(Writer, IOVec, sizeof(IOVec) / sizeof(*IOVec))) 328fe6060f1SDimitry Andric return -1; 329fe6060f1SDimitry Andric 330fe6060f1SDimitry Andric /* Write the binary id lengths and data. */ 331fe6060f1SDimitry Andric if (__llvm_write_binary_ids(Writer) == -1) 332fe6060f1SDimitry Andric return -1; 333fe6060f1SDimitry Andric 334fe6060f1SDimitry Andric /* Write the profile data. */ 335fe6060f1SDimitry Andric ProfDataIOVec IOVecData[] = { 3365f757f3fSDimitry Andric {DataBegin, sizeof(uint8_t), DataSectionSize, 0}, 337480093f4SDimitry Andric {NULL, sizeof(uint8_t), PaddingBytesBeforeCounters, 1}, 338bdd1243dSDimitry Andric {CountersBegin, sizeof(uint8_t), CountersSectionSize, 0}, 339480093f4SDimitry Andric {NULL, sizeof(uint8_t), PaddingBytesAfterCounters, 1}, 3405f757f3fSDimitry Andric {BitmapBegin, sizeof(uint8_t), NumBitmapBytes, 0}, 3415f757f3fSDimitry Andric {NULL, sizeof(uint8_t), PaddingBytesAfterBitmapBytes, 1}, 3425f757f3fSDimitry Andric {SkipNameDataWrite ? NULL : NamesBegin, sizeof(uint8_t), NamesSize, 0}, 343*0fca6ea1SDimitry Andric {NULL, sizeof(uint8_t), PaddingBytesAfterNames, 1}, 344*0fca6ea1SDimitry Andric {VTableBegin, sizeof(uint8_t), VTableSectionSize, 0}, 345*0fca6ea1SDimitry Andric {NULL, sizeof(uint8_t), PaddingBytesAfterVTable, 1}, 346*0fca6ea1SDimitry Andric {SkipNameDataWrite ? NULL : VNamesBegin, sizeof(uint8_t), VNamesSize, 0}, 347*0fca6ea1SDimitry Andric {NULL, sizeof(uint8_t), PaddingBytesAfterVNames, 1}}; 348fe6060f1SDimitry Andric if (Writer->Write(Writer, IOVecData, sizeof(IOVecData) / sizeof(*IOVecData))) 3490b57cec5SDimitry Andric return -1; 3500b57cec5SDimitry Andric 3515f757f3fSDimitry Andric /* Value profiling is not yet supported in continuous mode and profile 3525f757f3fSDimitry Andric * correlation mode. */ 3535f757f3fSDimitry Andric if (__llvm_profile_is_continuous_mode_enabled() || 3545f757f3fSDimitry Andric (NumData == 0 && NamesSize == 0)) 355480093f4SDimitry Andric return 0; 356480093f4SDimitry Andric 3570b57cec5SDimitry Andric return writeValueProfData(Writer, VPDataReader, DataBegin, DataEnd); 3580b57cec5SDimitry Andric } 35906c3fb27SDimitry Andric 36006c3fb27SDimitry Andric /* 36106c3fb27SDimitry Andric * Write binary id length and then its data, because binary id does not 36206c3fb27SDimitry Andric * have a fixed length. 36306c3fb27SDimitry Andric */ 36406c3fb27SDimitry Andric COMPILER_RT_VISIBILITY 36506c3fb27SDimitry Andric int lprofWriteOneBinaryId(ProfDataWriter *Writer, uint64_t BinaryIdLen, 36606c3fb27SDimitry Andric const uint8_t *BinaryIdData, 36706c3fb27SDimitry Andric uint64_t BinaryIdPadding) { 36806c3fb27SDimitry Andric ProfDataIOVec BinaryIdIOVec[] = { 36906c3fb27SDimitry Andric {&BinaryIdLen, sizeof(uint64_t), 1, 0}, 37006c3fb27SDimitry Andric {BinaryIdData, sizeof(uint8_t), BinaryIdLen, 0}, 37106c3fb27SDimitry Andric {NULL, sizeof(uint8_t), BinaryIdPadding, 1}, 37206c3fb27SDimitry Andric }; 37306c3fb27SDimitry Andric if (Writer->Write(Writer, BinaryIdIOVec, 37406c3fb27SDimitry Andric sizeof(BinaryIdIOVec) / sizeof(*BinaryIdIOVec))) 37506c3fb27SDimitry Andric return -1; 37606c3fb27SDimitry Andric 37706c3fb27SDimitry Andric /* Successfully wrote binary id, report success. */ 37806c3fb27SDimitry Andric return 0; 37906c3fb27SDimitry Andric } 380