10b57cec5SDimitry Andric /*===- InstrProfilingBuffer.c - Write instrumentation to a memory 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 #include "InstrProfiling.h" 130b57cec5SDimitry Andric #include "InstrProfilingInternal.h" 14480093f4SDimitry Andric #include "InstrProfilingPort.h" 15480093f4SDimitry Andric 16480093f4SDimitry Andric /* When continuous mode is enabled (%c), this parameter is set to 1. 17480093f4SDimitry Andric * 18480093f4SDimitry Andric * This parameter is defined here in InstrProfilingBuffer.o, instead of in 19480093f4SDimitry Andric * InstrProfilingFile.o, to sequester all libc-dependent code in 20480093f4SDimitry Andric * InstrProfilingFile.o. The test `instrprof-without-libc` will break if this 21480093f4SDimitry Andric * layering is violated. */ 22480093f4SDimitry Andric static int ContinuouslySyncProfile = 0; 23480093f4SDimitry Andric 24e8d8bef9SDimitry Andric /* The system page size. Only valid when non-zero. If 0, the page size is 25e8d8bef9SDimitry Andric * unavailable. */ 26e8d8bef9SDimitry Andric static unsigned PageSize = 0; 27e8d8bef9SDimitry Andric 28480093f4SDimitry Andric COMPILER_RT_VISIBILITY int __llvm_profile_is_continuous_mode_enabled(void) { 29e8d8bef9SDimitry Andric return ContinuouslySyncProfile && PageSize; 30480093f4SDimitry Andric } 31480093f4SDimitry Andric 32480093f4SDimitry Andric COMPILER_RT_VISIBILITY void __llvm_profile_enable_continuous_mode(void) { 33480093f4SDimitry Andric ContinuouslySyncProfile = 1; 34480093f4SDimitry Andric } 350b57cec5SDimitry Andric 365f757f3fSDimitry Andric COMPILER_RT_VISIBILITY void __llvm_profile_disable_continuous_mode(void) { 375f757f3fSDimitry Andric ContinuouslySyncProfile = 0; 385f757f3fSDimitry Andric } 395f757f3fSDimitry Andric 40e8d8bef9SDimitry Andric COMPILER_RT_VISIBILITY void __llvm_profile_set_page_size(unsigned PS) { 41e8d8bef9SDimitry Andric PageSize = PS; 42e8d8bef9SDimitry Andric } 43e8d8bef9SDimitry Andric 440b57cec5SDimitry Andric COMPILER_RT_VISIBILITY 450b57cec5SDimitry Andric uint64_t __llvm_profile_get_size_for_buffer(void) { 460b57cec5SDimitry Andric const __llvm_profile_data *DataBegin = __llvm_profile_begin_data(); 470b57cec5SDimitry Andric const __llvm_profile_data *DataEnd = __llvm_profile_end_data(); 4804eeddc0SDimitry Andric const char *CountersBegin = __llvm_profile_begin_counters(); 4904eeddc0SDimitry Andric const char *CountersEnd = __llvm_profile_end_counters(); 505f757f3fSDimitry Andric const char *BitmapBegin = __llvm_profile_begin_bitmap(); 515f757f3fSDimitry Andric const char *BitmapEnd = __llvm_profile_end_bitmap(); 520b57cec5SDimitry Andric const char *NamesBegin = __llvm_profile_begin_names(); 530b57cec5SDimitry Andric const char *NamesEnd = __llvm_profile_end_names(); 54*0fca6ea1SDimitry Andric const VTableProfData *VTableBegin = __llvm_profile_begin_vtables(); 55*0fca6ea1SDimitry Andric const VTableProfData *VTableEnd = __llvm_profile_end_vtables(); 56*0fca6ea1SDimitry Andric const char *VNamesBegin = __llvm_profile_begin_vtabnames(); 57*0fca6ea1SDimitry Andric const char *VNamesEnd = __llvm_profile_end_vtabnames(); 580b57cec5SDimitry Andric 590b57cec5SDimitry Andric return __llvm_profile_get_size_for_buffer_internal( 605f757f3fSDimitry Andric DataBegin, DataEnd, CountersBegin, CountersEnd, BitmapBegin, BitmapEnd, 61*0fca6ea1SDimitry Andric NamesBegin, NamesEnd, VTableBegin, VTableEnd, VNamesBegin, VNamesEnd); 620b57cec5SDimitry Andric } 630b57cec5SDimitry Andric 64*0fca6ea1SDimitry Andric // NOTE: Caller should guarantee that `Begin` and `End` specifies a half-open 65*0fca6ea1SDimitry Andric // interval [Begin, End). Namely, `End` is one-byte past the end of the array. 660b57cec5SDimitry Andric COMPILER_RT_VISIBILITY 6704eeddc0SDimitry Andric uint64_t __llvm_profile_get_num_data(const __llvm_profile_data *Begin, 680b57cec5SDimitry Andric const __llvm_profile_data *End) { 690b57cec5SDimitry Andric intptr_t BeginI = (intptr_t)Begin, EndI = (intptr_t)End; 700b57cec5SDimitry Andric return ((EndI + sizeof(__llvm_profile_data) - 1) - BeginI) / 710b57cec5SDimitry Andric sizeof(__llvm_profile_data); 720b57cec5SDimitry Andric } 730b57cec5SDimitry Andric 7404eeddc0SDimitry Andric COMPILER_RT_VISIBILITY 7504eeddc0SDimitry Andric uint64_t __llvm_profile_get_data_size(const __llvm_profile_data *Begin, 7604eeddc0SDimitry Andric const __llvm_profile_data *End) { 7704eeddc0SDimitry Andric return __llvm_profile_get_num_data(Begin, End) * sizeof(__llvm_profile_data); 7804eeddc0SDimitry Andric } 7904eeddc0SDimitry Andric 80*0fca6ea1SDimitry Andric // Counts the number of `VTableProfData` elements within the range of [Begin, 81*0fca6ea1SDimitry Andric // End). Caller should guarantee that End points to one byte past the inclusive 82*0fca6ea1SDimitry Andric // range. 83*0fca6ea1SDimitry Andric // FIXME: Add a compiler-rt test to make sure the number of vtables in the 84*0fca6ea1SDimitry Andric // raw profile is the same as the number of vtable elements in the instrumented 85*0fca6ea1SDimitry Andric // binary. 86*0fca6ea1SDimitry Andric COMPILER_RT_VISIBILITY 87*0fca6ea1SDimitry Andric uint64_t __llvm_profile_get_num_vtable(const VTableProfData *Begin, 88*0fca6ea1SDimitry Andric const VTableProfData *End) { 89*0fca6ea1SDimitry Andric // Convert pointers to intptr_t to use integer arithmetic. 90*0fca6ea1SDimitry Andric intptr_t EndI = (intptr_t)End, BeginI = (intptr_t)Begin; 91*0fca6ea1SDimitry Andric return (EndI - BeginI) / sizeof(VTableProfData); 92*0fca6ea1SDimitry Andric } 93*0fca6ea1SDimitry Andric 94*0fca6ea1SDimitry Andric COMPILER_RT_VISIBILITY 95*0fca6ea1SDimitry Andric uint64_t __llvm_profile_get_vtable_section_size(const VTableProfData *Begin, 96*0fca6ea1SDimitry Andric const VTableProfData *End) { 97*0fca6ea1SDimitry Andric return (intptr_t)(End) - (intptr_t)(Begin); 98*0fca6ea1SDimitry Andric } 99*0fca6ea1SDimitry Andric 10004eeddc0SDimitry Andric COMPILER_RT_VISIBILITY size_t __llvm_profile_counter_entry_size(void) { 1011fd87a68SDimitry Andric if (__llvm_profile_get_version() & VARIANT_MASK_BYTE_COVERAGE) 1021fd87a68SDimitry Andric return sizeof(uint8_t); 10304eeddc0SDimitry Andric return sizeof(uint64_t); 10404eeddc0SDimitry Andric } 10504eeddc0SDimitry Andric 10604eeddc0SDimitry Andric COMPILER_RT_VISIBILITY 10704eeddc0SDimitry Andric uint64_t __llvm_profile_get_num_counters(const char *Begin, const char *End) { 10804eeddc0SDimitry Andric intptr_t BeginI = (intptr_t)Begin, EndI = (intptr_t)End; 10904eeddc0SDimitry Andric return ((EndI + __llvm_profile_counter_entry_size() - 1) - BeginI) / 11004eeddc0SDimitry Andric __llvm_profile_counter_entry_size(); 11104eeddc0SDimitry Andric } 11204eeddc0SDimitry Andric 11304eeddc0SDimitry Andric COMPILER_RT_VISIBILITY 11404eeddc0SDimitry Andric uint64_t __llvm_profile_get_counters_size(const char *Begin, const char *End) { 11504eeddc0SDimitry Andric return __llvm_profile_get_num_counters(Begin, End) * 11604eeddc0SDimitry Andric __llvm_profile_counter_entry_size(); 11704eeddc0SDimitry Andric } 11804eeddc0SDimitry Andric 1195f757f3fSDimitry Andric COMPILER_RT_VISIBILITY 1205f757f3fSDimitry Andric uint64_t __llvm_profile_get_num_bitmap_bytes(const char *Begin, 1215f757f3fSDimitry Andric const char *End) { 1225f757f3fSDimitry Andric return (End - Begin); 1235f757f3fSDimitry Andric } 1245f757f3fSDimitry Andric 1255f757f3fSDimitry Andric COMPILER_RT_VISIBILITY 1265f757f3fSDimitry Andric uint64_t __llvm_profile_get_name_size(const char *Begin, const char *End) { 1275f757f3fSDimitry Andric return End - Begin; 1285f757f3fSDimitry Andric } 1295f757f3fSDimitry Andric 130480093f4SDimitry Andric /// Calculate the number of padding bytes needed to add to \p Offset in order 131480093f4SDimitry Andric /// for (\p Offset + Padding) to be page-aligned. 132e8d8bef9SDimitry Andric static uint64_t calculateBytesNeededToPageAlign(uint64_t Offset) { 133480093f4SDimitry Andric uint64_t OffsetModPage = Offset % PageSize; 134480093f4SDimitry Andric if (OffsetModPage > 0) 135480093f4SDimitry Andric return PageSize - OffsetModPage; 136480093f4SDimitry Andric return 0; 137480093f4SDimitry Andric } 138480093f4SDimitry Andric 139fe6060f1SDimitry Andric static int needsCounterPadding(void) { 140fe6060f1SDimitry Andric #if defined(__APPLE__) 141fe6060f1SDimitry Andric return __llvm_profile_is_continuous_mode_enabled(); 142fe6060f1SDimitry Andric #else 143fe6060f1SDimitry Andric return 0; 144fe6060f1SDimitry Andric #endif 145fe6060f1SDimitry Andric } 146fe6060f1SDimitry Andric 147480093f4SDimitry Andric COMPILER_RT_VISIBILITY 148*0fca6ea1SDimitry Andric int __llvm_profile_get_padding_sizes_for_counters( 1495f757f3fSDimitry Andric uint64_t DataSize, uint64_t CountersSize, uint64_t NumBitmapBytes, 150*0fca6ea1SDimitry Andric uint64_t NamesSize, uint64_t VTableSize, uint64_t VNameSize, 151*0fca6ea1SDimitry Andric uint64_t *PaddingBytesBeforeCounters, uint64_t *PaddingBytesAfterCounters, 152*0fca6ea1SDimitry Andric uint64_t *PaddingBytesAfterBitmapBytes, uint64_t *PaddingBytesAfterNames, 153*0fca6ea1SDimitry Andric uint64_t *PaddingBytesAfterVTable, uint64_t *PaddingBytesAfterVName) { 154*0fca6ea1SDimitry Andric // Counter padding is needed only if continuous mode is enabled. 155fe6060f1SDimitry Andric if (!needsCounterPadding()) { 156480093f4SDimitry Andric *PaddingBytesBeforeCounters = 0; 15706c3fb27SDimitry Andric *PaddingBytesAfterCounters = 15806c3fb27SDimitry Andric __llvm_profile_get_num_padding_bytes(CountersSize); 1595f757f3fSDimitry Andric *PaddingBytesAfterBitmapBytes = 1605f757f3fSDimitry Andric __llvm_profile_get_num_padding_bytes(NumBitmapBytes); 161480093f4SDimitry Andric *PaddingBytesAfterNames = __llvm_profile_get_num_padding_bytes(NamesSize); 162*0fca6ea1SDimitry Andric if (PaddingBytesAfterVTable != NULL) 163*0fca6ea1SDimitry Andric *PaddingBytesAfterVTable = 164*0fca6ea1SDimitry Andric __llvm_profile_get_num_padding_bytes(VTableSize); 165*0fca6ea1SDimitry Andric if (PaddingBytesAfterVName != NULL) 166*0fca6ea1SDimitry Andric *PaddingBytesAfterVName = __llvm_profile_get_num_padding_bytes(VNameSize); 167*0fca6ea1SDimitry Andric return 0; 168480093f4SDimitry Andric } 169480093f4SDimitry Andric 170*0fca6ea1SDimitry Andric // Value profiling not supported in continuous mode at profile-write time. 171*0fca6ea1SDimitry Andric // Return -1 to alert the incompatibility. 172*0fca6ea1SDimitry Andric if (VTableSize != 0 || VNameSize != 0) 173*0fca6ea1SDimitry Andric return -1; 174*0fca6ea1SDimitry Andric 175480093f4SDimitry Andric // In continuous mode, the file offsets for headers and for the start of 176480093f4SDimitry Andric // counter sections need to be page-aligned. 17704eeddc0SDimitry Andric *PaddingBytesBeforeCounters = 17804eeddc0SDimitry Andric calculateBytesNeededToPageAlign(sizeof(__llvm_profile_header) + DataSize); 17904eeddc0SDimitry Andric *PaddingBytesAfterCounters = calculateBytesNeededToPageAlign(CountersSize); 1805f757f3fSDimitry Andric *PaddingBytesAfterBitmapBytes = 1815f757f3fSDimitry Andric calculateBytesNeededToPageAlign(NumBitmapBytes); 182e8d8bef9SDimitry Andric *PaddingBytesAfterNames = calculateBytesNeededToPageAlign(NamesSize); 183*0fca6ea1SDimitry Andric // Set these two variables to zero to avoid uninitialized variables 184*0fca6ea1SDimitry Andric // even if VTableSize and VNameSize are known to be zero. 185*0fca6ea1SDimitry Andric if (PaddingBytesAfterVTable != NULL) 186*0fca6ea1SDimitry Andric *PaddingBytesAfterVTable = 0; 187*0fca6ea1SDimitry Andric if (PaddingBytesAfterVName != NULL) 188*0fca6ea1SDimitry Andric *PaddingBytesAfterVName = 0; 189*0fca6ea1SDimitry Andric return 0; 190480093f4SDimitry Andric } 191480093f4SDimitry Andric 1920b57cec5SDimitry Andric COMPILER_RT_VISIBILITY 1930b57cec5SDimitry Andric uint64_t __llvm_profile_get_size_for_buffer_internal( 1940b57cec5SDimitry Andric const __llvm_profile_data *DataBegin, const __llvm_profile_data *DataEnd, 1955f757f3fSDimitry Andric const char *CountersBegin, const char *CountersEnd, const char *BitmapBegin, 196*0fca6ea1SDimitry Andric const char *BitmapEnd, const char *NamesBegin, const char *NamesEnd, 197*0fca6ea1SDimitry Andric const VTableProfData *VTableBegin, const VTableProfData *VTableEnd, 198*0fca6ea1SDimitry Andric const char *VNamesBegin, const char *VNamesEnd) { 1990b57cec5SDimitry Andric /* Match logic in __llvm_profile_write_buffer(). */ 2000b57cec5SDimitry Andric const uint64_t NamesSize = (NamesEnd - NamesBegin) * sizeof(char); 201480093f4SDimitry Andric uint64_t DataSize = __llvm_profile_get_data_size(DataBegin, DataEnd); 20204eeddc0SDimitry Andric uint64_t CountersSize = 20304eeddc0SDimitry Andric __llvm_profile_get_counters_size(CountersBegin, CountersEnd); 2045f757f3fSDimitry Andric const uint64_t NumBitmapBytes = 2055f757f3fSDimitry Andric __llvm_profile_get_num_bitmap_bytes(BitmapBegin, BitmapEnd); 206*0fca6ea1SDimitry Andric const uint64_t VTableSize = 207*0fca6ea1SDimitry Andric __llvm_profile_get_vtable_section_size(VTableBegin, VTableEnd); 208*0fca6ea1SDimitry Andric const uint64_t VNameSize = 209*0fca6ea1SDimitry Andric __llvm_profile_get_name_size(VNamesBegin, VNamesEnd); 210480093f4SDimitry Andric 211480093f4SDimitry Andric /* Determine how much padding is needed before/after the counters and after 212480093f4SDimitry Andric * the names. */ 213480093f4SDimitry Andric uint64_t PaddingBytesBeforeCounters, PaddingBytesAfterCounters, 214*0fca6ea1SDimitry Andric PaddingBytesAfterNames, PaddingBytesAfterBitmapBytes, 215*0fca6ea1SDimitry Andric PaddingBytesAfterVTable, PaddingBytesAfterVNames; 216480093f4SDimitry Andric __llvm_profile_get_padding_sizes_for_counters( 217*0fca6ea1SDimitry Andric DataSize, CountersSize, NumBitmapBytes, NamesSize, 0 /* VTableSize */, 218*0fca6ea1SDimitry Andric 0 /* VNameSize */, &PaddingBytesBeforeCounters, 219*0fca6ea1SDimitry Andric &PaddingBytesAfterCounters, &PaddingBytesAfterBitmapBytes, 220*0fca6ea1SDimitry Andric &PaddingBytesAfterNames, &PaddingBytesAfterVTable, 221*0fca6ea1SDimitry Andric &PaddingBytesAfterVNames); 222480093f4SDimitry Andric 2236e75b2fbSDimitry Andric return sizeof(__llvm_profile_header) + __llvm_write_binary_ids(NULL) + 22404eeddc0SDimitry Andric DataSize + PaddingBytesBeforeCounters + CountersSize + 2255f757f3fSDimitry Andric PaddingBytesAfterCounters + NumBitmapBytes + 226*0fca6ea1SDimitry Andric PaddingBytesAfterBitmapBytes + NamesSize + PaddingBytesAfterNames + 227*0fca6ea1SDimitry Andric VTableSize + PaddingBytesAfterVTable + VNameSize + 228*0fca6ea1SDimitry Andric PaddingBytesAfterVNames; 2290b57cec5SDimitry Andric } 2300b57cec5SDimitry Andric 2310b57cec5SDimitry Andric COMPILER_RT_VISIBILITY 2320b57cec5SDimitry Andric void initBufferWriter(ProfDataWriter *BufferWriter, char *Buffer) { 2330b57cec5SDimitry Andric BufferWriter->Write = lprofBufferWriter; 2340b57cec5SDimitry Andric BufferWriter->WriterCtx = Buffer; 2350b57cec5SDimitry Andric } 2360b57cec5SDimitry Andric 2370b57cec5SDimitry Andric COMPILER_RT_VISIBILITY int __llvm_profile_write_buffer(char *Buffer) { 2380b57cec5SDimitry Andric ProfDataWriter BufferWriter; 2390b57cec5SDimitry Andric initBufferWriter(&BufferWriter, Buffer); 2400b57cec5SDimitry Andric return lprofWriteData(&BufferWriter, 0, 0); 2410b57cec5SDimitry Andric } 2420b57cec5SDimitry Andric 2430b57cec5SDimitry Andric COMPILER_RT_VISIBILITY int __llvm_profile_write_buffer_internal( 2440b57cec5SDimitry Andric char *Buffer, const __llvm_profile_data *DataBegin, 24504eeddc0SDimitry Andric const __llvm_profile_data *DataEnd, const char *CountersBegin, 2465f757f3fSDimitry Andric const char *CountersEnd, const char *BitmapBegin, const char *BitmapEnd, 2475f757f3fSDimitry Andric const char *NamesBegin, const char *NamesEnd) { 2480b57cec5SDimitry Andric ProfDataWriter BufferWriter; 2490b57cec5SDimitry Andric initBufferWriter(&BufferWriter, Buffer); 250*0fca6ea1SDimitry Andric // Set virtual table arguments to NULL since they are not supported yet. 251*0fca6ea1SDimitry Andric return lprofWriteDataImpl( 252*0fca6ea1SDimitry Andric &BufferWriter, DataBegin, DataEnd, CountersBegin, CountersEnd, 253*0fca6ea1SDimitry Andric BitmapBegin, BitmapEnd, /*VPDataReader=*/0, NamesBegin, NamesEnd, 254*0fca6ea1SDimitry Andric /*VTableBegin=*/NULL, /*VTableEnd=*/NULL, /*VNamesBegin=*/NULL, 255*0fca6ea1SDimitry Andric /*VNamesEnd=*/NULL, /*SkipNameDataWrite=*/0); 2560b57cec5SDimitry Andric } 257