diff options
author | Xinliang David Li <davidxl@google.com> | 2015-12-29 07:13:59 +0000 |
---|---|---|
committer | Xinliang David Li <davidxl@google.com> | 2015-12-29 07:13:59 +0000 |
commit | 0bd4e92177255526ef663792aca8a1f9674d566b (patch) | |
tree | 64badaa85b408ea2984fa0efa5e2281a1289cf06 /lib/profile/InstrProfilingWriter.c | |
parent | aafd7e5eeea4d59ef434c81975c744709809187b (diff) | |
download | compiler-rt-0bd4e92177255526ef663792aca8a1f9674d566b.tar.gz |
[PGO]: Do not update Data->Value field during profile write.
The profile reader no longer depends on this field to be updated and point
to owning func's vp data. The VP data also no longer needs to be allocated
in a contiguous memory space.
Differential Revision: http://reviews.llvm.org/D15258
git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@256543 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/profile/InstrProfilingWriter.c')
-rw-r--r-- | lib/profile/InstrProfilingWriter.c | 96 |
1 files changed, 81 insertions, 15 deletions
diff --git a/lib/profile/InstrProfilingWriter.c b/lib/profile/InstrProfilingWriter.c index 8519891f2..6153a0050 100644 --- a/lib/profile/InstrProfilingWriter.c +++ b/lib/profile/InstrProfilingWriter.c @@ -11,6 +11,12 @@ #include "InstrProfilingInternal.h" #include <string.h> +#define INSTR_PROF_VALUE_PROF_DATA +#include "InstrProfData.inc" +void (*FreeHook)(void *) = NULL; +void* (*CallocHook)(size_t, size_t) = NULL; +uint32_t VPBufferSize = 0; + /* The buffer writer is reponsponsible in keeping writer state * across the call. */ @@ -29,7 +35,7 @@ COMPILER_RT_VISIBILITY uint32_t llvmBufferWriter(ProfDataIOVec *IOVecs, COMPILER_RT_VISIBILITY int llvmWriteProfData(WriterCallback Writer, void *WriterCtx, - const uint8_t *ValueDataBegin, + ValueProfData **ValueDataArray, const uint64_t ValueDataSize) { /* Match logic in __llvm_profile_write_buffer(). */ const __llvm_profile_data *DataBegin = __llvm_profile_begin_data(); @@ -39,15 +45,80 @@ COMPILER_RT_VISIBILITY int llvmWriteProfData(WriterCallback Writer, const char *NamesBegin = __llvm_profile_begin_names(); const char *NamesEnd = __llvm_profile_end_names(); return llvmWriteProfDataImpl(Writer, WriterCtx, DataBegin, DataEnd, - CountersBegin, CountersEnd, ValueDataBegin, + CountersBegin, CountersEnd, ValueDataArray, ValueDataSize, NamesBegin, NamesEnd); } +#define VP_BUFFER_SIZE 8 * 1024 +static int writeValueProfData(WriterCallback Writer, void *WriterCtx, + ValueProfData **ValueDataBegin, + uint64_t NumVData) { + ValueProfData **ValueDataArray = ValueDataBegin; + char *BufferStart = 0, *Buffer; + ValueProfData *CurVData; + uint32_t I = 0, BufferSz; + + if (!ValueDataBegin) + return 0; + + BufferSz = VPBufferSize ? VPBufferSize : VP_BUFFER_SIZE; + BufferStart = (char *)CallocHook(BufferSz, sizeof(uint8_t)); + if (!BufferStart) + return -1; + + uint32_t WriteSize = 0; + Buffer = BufferStart; + do { + CurVData = ValueDataArray[I]; + if (!CurVData) { + I++; + continue; + } + + /* Buffer is full or not large enough, it is time to flush. */ + if (CurVData->TotalSize + WriteSize > BufferSz) { + if (WriteSize) { + ProfDataIOVec IO[] = {{BufferStart, sizeof(uint8_t), WriteSize}}; + if (Writer(IO, 1, &WriterCtx)) + return -1; + WriteSize = 0; + Buffer = BufferStart; + } + /* Special case, bypass the buffer completely. */ + if (CurVData->TotalSize > BufferSz) { + ProfDataIOVec IO[] = {{CurVData, sizeof(uint8_t), CurVData->TotalSize}}; + if (Writer(IO, 1, &WriterCtx)) + return -1; + FreeHook(ValueDataArray[I]); + I++; + } + } else { + /* Write the data to buffer */ + ProfDataIOVec IO[] = {{CurVData, sizeof(uint8_t), CurVData->TotalSize}}; + llvmBufferWriter(IO, 1, (void **)&Buffer); + WriteSize += CurVData->TotalSize; + FreeHook(ValueDataArray[I]); + I++; + } + } while (I < NumVData); + + /* Final flush. */ + if (WriteSize) { + ProfDataIOVec IO[] = {{BufferStart, sizeof(uint8_t), WriteSize}}; + if (Writer(IO, 1, &WriterCtx)) + return -1; + } + + FreeHook(ValueDataBegin); + FreeHook(BufferStart); + return 0; +} + COMPILER_RT_VISIBILITY int llvmWriteProfDataImpl( WriterCallback Writer, void *WriterCtx, const __llvm_profile_data *DataBegin, const __llvm_profile_data *DataEnd, const uint64_t *CountersBegin, const uint64_t *CountersEnd, - const uint8_t *ValueDataBegin, const uint64_t ValueDataSize, + ValueProfData **ValueDataBegin, const uint64_t ValueDataSize, const char *NamesBegin, const char *NamesEnd) { /* Calculate size of sections. */ @@ -70,18 +141,13 @@ COMPILER_RT_VISIBILITY int llvmWriteProfDataImpl( #include "InstrProfData.inc" /* Write the data. */ - ProfDataIOVec IOVec[] = { - {&Header, sizeof(__llvm_profile_header), 1}, - {DataBegin, sizeof(__llvm_profile_data), DataSize}, - {CountersBegin, sizeof(uint64_t), CountersSize}, - {NamesBegin, sizeof(char), NamesSize}, - {Zeroes, sizeof(char), Padding}}; + ProfDataIOVec IOVec[] = {{&Header, sizeof(__llvm_profile_header), 1}, + {DataBegin, sizeof(__llvm_profile_data), DataSize}, + {CountersBegin, sizeof(uint64_t), CountersSize}, + {NamesBegin, sizeof(uint8_t), NamesSize}, + {Zeroes, sizeof(uint8_t), Padding}}; if (Writer(IOVec, sizeof(IOVec) / sizeof(*IOVec), &WriterCtx)) return -1; - if (ValueDataBegin) { - ProfDataIOVec IOVec2[] = {{ValueDataBegin, sizeof(char), ValueDataSize}}; - if (Writer(IOVec2, sizeof(IOVec2) / sizeof(*IOVec2), &WriterCtx)) - return -1; - } - return 0; + + return writeValueProfData(Writer, WriterCtx, ValueDataBegin, DataSize); } |