[profile] Remove dependence on getpagesize from InstrProfilingBuffer.c.o

InstrProfilingBuffer.c.o is generic code that must support compilation
into freestanding projects. This gets rid of its dependence on the
_getpagesize symbol from libc, shifting it to InstrProfilingFile.c.o.

This fixes a build failure seen in a firmware project.

rdar://66249701
This commit is contained in:
Vedant Kumar 2020-07-30 16:19:05 -07:00
parent 57605758b5
commit 896f797b8b
4 changed files with 24 additions and 8 deletions

View File

@ -54,6 +54,15 @@ int __llvm_profile_is_continuous_mode_enabled(void);
*/
void __llvm_profile_enable_continuous_mode(void);
/*!
* \brief Set the page size.
*
* This is a pre-requisite for enabling continuous mode. The buffer size
* calculation code inside of libprofile cannot simply call getpagesize(), as
* it is not allowed to depend on libc.
*/
void __llvm_profile_set_page_size(unsigned PageSize);
/*!
* \brief Get number of bytes necessary to pad the argument to eight
* byte boundary.

View File

@ -21,14 +21,22 @@
* layering is violated. */
static int ContinuouslySyncProfile = 0;
/* The system page size. Only valid when non-zero. If 0, the page size is
* unavailable. */
static unsigned PageSize = 0;
COMPILER_RT_VISIBILITY int __llvm_profile_is_continuous_mode_enabled(void) {
return ContinuouslySyncProfile;
return ContinuouslySyncProfile && PageSize;
}
COMPILER_RT_VISIBILITY void __llvm_profile_enable_continuous_mode(void) {
ContinuouslySyncProfile = 1;
}
COMPILER_RT_VISIBILITY void __llvm_profile_set_page_size(unsigned PS) {
PageSize = PS;
}
COMPILER_RT_VISIBILITY
uint64_t __llvm_profile_get_size_for_buffer(void) {
const __llvm_profile_data *DataBegin = __llvm_profile_begin_data();
@ -52,8 +60,7 @@ uint64_t __llvm_profile_get_data_size(const __llvm_profile_data *Begin,
/// Calculate the number of padding bytes needed to add to \p Offset in order
/// for (\p Offset + Padding) to be page-aligned.
static uint64_t calculateBytesNeededToPageAlign(uint64_t Offset,
unsigned PageSize) {
static uint64_t calculateBytesNeededToPageAlign(uint64_t Offset) {
uint64_t OffsetModPage = Offset % PageSize;
if (OffsetModPage > 0)
return PageSize - OffsetModPage;
@ -75,15 +82,13 @@ void __llvm_profile_get_padding_sizes_for_counters(
// In continuous mode, the file offsets for headers and for the start of
// counter sections need to be page-aligned.
unsigned PageSize = getpagesize();
uint64_t DataSizeInBytes = DataSize * sizeof(__llvm_profile_data);
uint64_t CountersSizeInBytes = CountersSize * sizeof(uint64_t);
*PaddingBytesBeforeCounters = calculateBytesNeededToPageAlign(
sizeof(__llvm_profile_header) + DataSizeInBytes, PageSize);
sizeof(__llvm_profile_header) + DataSizeInBytes);
*PaddingBytesAfterCounters =
calculateBytesNeededToPageAlign(CountersSizeInBytes, PageSize);
*PaddingBytesAfterNames =
calculateBytesNeededToPageAlign(NamesSize, PageSize);
calculateBytesNeededToPageAlign(CountersSizeInBytes);
*PaddingBytesAfterNames = calculateBytesNeededToPageAlign(NamesSize);
}
COMPILER_RT_VISIBILITY

View File

@ -751,6 +751,7 @@ static int parseFilenamePattern(const char *FilenamePat,
return -1;
}
__llvm_profile_set_page_size(getpagesize());
__llvm_profile_enable_continuous_mode();
I++; /* advance to 'c' */
} else {

View File

@ -72,3 +72,4 @@ int main(int argc, const char *argv[]) {
// CHECK-SYMBOLS-NOT: {{ }}_free
// CHECK-SYMBOLS-NOT: {{ }}free
// CHECK-SYMBOLS-NOT: {{ }}_open
// CHECK-SYMBOLS-NOT: {{ }}_getpagesize