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 36e8d8bef9SDimitry Andric COMPILER_RT_VISIBILITY void __llvm_profile_set_page_size(unsigned PS) { 37e8d8bef9SDimitry Andric PageSize = PS; 38e8d8bef9SDimitry Andric } 39e8d8bef9SDimitry Andric 400b57cec5SDimitry Andric COMPILER_RT_VISIBILITY 410b57cec5SDimitry Andric uint64_t __llvm_profile_get_size_for_buffer(void) { 420b57cec5SDimitry Andric const __llvm_profile_data *DataBegin = __llvm_profile_begin_data(); 430b57cec5SDimitry Andric const __llvm_profile_data *DataEnd = __llvm_profile_end_data(); 4404eeddc0SDimitry Andric const char *CountersBegin = __llvm_profile_begin_counters(); 4504eeddc0SDimitry Andric const char *CountersEnd = __llvm_profile_end_counters(); 460b57cec5SDimitry Andric const char *NamesBegin = __llvm_profile_begin_names(); 470b57cec5SDimitry Andric const char *NamesEnd = __llvm_profile_end_names(); 480b57cec5SDimitry Andric 490b57cec5SDimitry Andric return __llvm_profile_get_size_for_buffer_internal( 500b57cec5SDimitry Andric DataBegin, DataEnd, CountersBegin, CountersEnd, NamesBegin, NamesEnd); 510b57cec5SDimitry Andric } 520b57cec5SDimitry Andric 530b57cec5SDimitry Andric COMPILER_RT_VISIBILITY 5404eeddc0SDimitry Andric uint64_t __llvm_profile_get_num_data(const __llvm_profile_data *Begin, 550b57cec5SDimitry Andric const __llvm_profile_data *End) { 560b57cec5SDimitry Andric intptr_t BeginI = (intptr_t)Begin, EndI = (intptr_t)End; 570b57cec5SDimitry Andric return ((EndI + sizeof(__llvm_profile_data) - 1) - BeginI) / 580b57cec5SDimitry Andric sizeof(__llvm_profile_data); 590b57cec5SDimitry Andric } 600b57cec5SDimitry Andric 6104eeddc0SDimitry Andric COMPILER_RT_VISIBILITY 6204eeddc0SDimitry Andric uint64_t __llvm_profile_get_data_size(const __llvm_profile_data *Begin, 6304eeddc0SDimitry Andric const __llvm_profile_data *End) { 6404eeddc0SDimitry Andric return __llvm_profile_get_num_data(Begin, End) * sizeof(__llvm_profile_data); 6504eeddc0SDimitry Andric } 6604eeddc0SDimitry Andric 6704eeddc0SDimitry Andric COMPILER_RT_VISIBILITY size_t __llvm_profile_counter_entry_size(void) { 681fd87a68SDimitry Andric if (__llvm_profile_get_version() & VARIANT_MASK_BYTE_COVERAGE) 691fd87a68SDimitry Andric return sizeof(uint8_t); 7004eeddc0SDimitry Andric return sizeof(uint64_t); 7104eeddc0SDimitry Andric } 7204eeddc0SDimitry Andric 7304eeddc0SDimitry Andric COMPILER_RT_VISIBILITY 7404eeddc0SDimitry Andric uint64_t __llvm_profile_get_num_counters(const char *Begin, const char *End) { 7504eeddc0SDimitry Andric intptr_t BeginI = (intptr_t)Begin, EndI = (intptr_t)End; 7604eeddc0SDimitry Andric return ((EndI + __llvm_profile_counter_entry_size() - 1) - BeginI) / 7704eeddc0SDimitry Andric __llvm_profile_counter_entry_size(); 7804eeddc0SDimitry Andric } 7904eeddc0SDimitry Andric 8004eeddc0SDimitry Andric COMPILER_RT_VISIBILITY 8104eeddc0SDimitry Andric uint64_t __llvm_profile_get_counters_size(const char *Begin, const char *End) { 8204eeddc0SDimitry Andric return __llvm_profile_get_num_counters(Begin, End) * 8304eeddc0SDimitry Andric __llvm_profile_counter_entry_size(); 8404eeddc0SDimitry Andric } 8504eeddc0SDimitry Andric 86480093f4SDimitry Andric /// Calculate the number of padding bytes needed to add to \p Offset in order 87480093f4SDimitry Andric /// for (\p Offset + Padding) to be page-aligned. 88e8d8bef9SDimitry Andric static uint64_t calculateBytesNeededToPageAlign(uint64_t Offset) { 89480093f4SDimitry Andric uint64_t OffsetModPage = Offset % PageSize; 90480093f4SDimitry Andric if (OffsetModPage > 0) 91480093f4SDimitry Andric return PageSize - OffsetModPage; 92480093f4SDimitry Andric return 0; 93480093f4SDimitry Andric } 94480093f4SDimitry Andric 95fe6060f1SDimitry Andric static int needsCounterPadding(void) { 96fe6060f1SDimitry Andric #if defined(__APPLE__) 97fe6060f1SDimitry Andric return __llvm_profile_is_continuous_mode_enabled(); 98fe6060f1SDimitry Andric #else 99fe6060f1SDimitry Andric return 0; 100fe6060f1SDimitry Andric #endif 101fe6060f1SDimitry Andric } 102fe6060f1SDimitry Andric 103480093f4SDimitry Andric COMPILER_RT_VISIBILITY 104480093f4SDimitry Andric void __llvm_profile_get_padding_sizes_for_counters( 105480093f4SDimitry Andric uint64_t DataSize, uint64_t CountersSize, uint64_t NamesSize, 106480093f4SDimitry Andric uint64_t *PaddingBytesBeforeCounters, uint64_t *PaddingBytesAfterCounters, 107480093f4SDimitry Andric uint64_t *PaddingBytesAfterNames) { 108fe6060f1SDimitry Andric if (!needsCounterPadding()) { 109480093f4SDimitry Andric *PaddingBytesBeforeCounters = 0; 110*06c3fb27SDimitry Andric *PaddingBytesAfterCounters = 111*06c3fb27SDimitry Andric __llvm_profile_get_num_padding_bytes(CountersSize); 112480093f4SDimitry Andric *PaddingBytesAfterNames = __llvm_profile_get_num_padding_bytes(NamesSize); 113480093f4SDimitry Andric return; 114480093f4SDimitry Andric } 115480093f4SDimitry Andric 116480093f4SDimitry Andric // In continuous mode, the file offsets for headers and for the start of 117480093f4SDimitry Andric // counter sections need to be page-aligned. 11804eeddc0SDimitry Andric *PaddingBytesBeforeCounters = 11904eeddc0SDimitry Andric calculateBytesNeededToPageAlign(sizeof(__llvm_profile_header) + DataSize); 12004eeddc0SDimitry Andric *PaddingBytesAfterCounters = calculateBytesNeededToPageAlign(CountersSize); 121e8d8bef9SDimitry Andric *PaddingBytesAfterNames = calculateBytesNeededToPageAlign(NamesSize); 122480093f4SDimitry Andric } 123480093f4SDimitry Andric 1240b57cec5SDimitry Andric COMPILER_RT_VISIBILITY 1250b57cec5SDimitry Andric uint64_t __llvm_profile_get_size_for_buffer_internal( 1260b57cec5SDimitry Andric const __llvm_profile_data *DataBegin, const __llvm_profile_data *DataEnd, 12704eeddc0SDimitry Andric const char *CountersBegin, const char *CountersEnd, const char *NamesBegin, 12804eeddc0SDimitry Andric const char *NamesEnd) { 1290b57cec5SDimitry Andric /* Match logic in __llvm_profile_write_buffer(). */ 1300b57cec5SDimitry Andric const uint64_t NamesSize = (NamesEnd - NamesBegin) * sizeof(char); 131480093f4SDimitry Andric uint64_t DataSize = __llvm_profile_get_data_size(DataBegin, DataEnd); 13204eeddc0SDimitry Andric uint64_t CountersSize = 13304eeddc0SDimitry Andric __llvm_profile_get_counters_size(CountersBegin, CountersEnd); 134480093f4SDimitry Andric 135480093f4SDimitry Andric /* Determine how much padding is needed before/after the counters and after 136480093f4SDimitry Andric * the names. */ 137480093f4SDimitry Andric uint64_t PaddingBytesBeforeCounters, PaddingBytesAfterCounters, 138480093f4SDimitry Andric PaddingBytesAfterNames; 139480093f4SDimitry Andric __llvm_profile_get_padding_sizes_for_counters( 140480093f4SDimitry Andric DataSize, CountersSize, NamesSize, &PaddingBytesBeforeCounters, 141480093f4SDimitry Andric &PaddingBytesAfterCounters, &PaddingBytesAfterNames); 142480093f4SDimitry Andric 1436e75b2fbSDimitry Andric return sizeof(__llvm_profile_header) + __llvm_write_binary_ids(NULL) + 14404eeddc0SDimitry Andric DataSize + PaddingBytesBeforeCounters + CountersSize + 14504eeddc0SDimitry Andric PaddingBytesAfterCounters + NamesSize + PaddingBytesAfterNames; 1460b57cec5SDimitry Andric } 1470b57cec5SDimitry Andric 1480b57cec5SDimitry Andric COMPILER_RT_VISIBILITY 1490b57cec5SDimitry Andric void initBufferWriter(ProfDataWriter *BufferWriter, char *Buffer) { 1500b57cec5SDimitry Andric BufferWriter->Write = lprofBufferWriter; 1510b57cec5SDimitry Andric BufferWriter->WriterCtx = Buffer; 1520b57cec5SDimitry Andric } 1530b57cec5SDimitry Andric 1540b57cec5SDimitry Andric COMPILER_RT_VISIBILITY int __llvm_profile_write_buffer(char *Buffer) { 1550b57cec5SDimitry Andric ProfDataWriter BufferWriter; 1560b57cec5SDimitry Andric initBufferWriter(&BufferWriter, Buffer); 1570b57cec5SDimitry Andric return lprofWriteData(&BufferWriter, 0, 0); 1580b57cec5SDimitry Andric } 1590b57cec5SDimitry Andric 1600b57cec5SDimitry Andric COMPILER_RT_VISIBILITY int __llvm_profile_write_buffer_internal( 1610b57cec5SDimitry Andric char *Buffer, const __llvm_profile_data *DataBegin, 16204eeddc0SDimitry Andric const __llvm_profile_data *DataEnd, const char *CountersBegin, 16304eeddc0SDimitry Andric const char *CountersEnd, const char *NamesBegin, const char *NamesEnd) { 1640b57cec5SDimitry Andric ProfDataWriter BufferWriter; 1650b57cec5SDimitry Andric initBufferWriter(&BufferWriter, Buffer); 1660b57cec5SDimitry Andric return lprofWriteDataImpl(&BufferWriter, DataBegin, DataEnd, CountersBegin, 1670b57cec5SDimitry Andric CountersEnd, 0, NamesBegin, NamesEnd, 0); 1680b57cec5SDimitry Andric } 169