[profile] Make atexit hook a no-op on Fuchsia

On Fuchsia, we always use the continuous mode with runtime counter
relocation, so there's no need for atexit hook or support for dumping
the profile manually.

Differential Revision: https://reviews.llvm.org/D76556
This commit is contained in:
Petr Hosek 2020-03-21 16:41:00 -07:00
parent d6fb02b196
commit 32bddad37b
6 changed files with 56 additions and 69 deletions

View File

@ -218,6 +218,9 @@ int __llvm_profile_register_write_file_atexit(void);
/*! \brief Initialize file handling. */ /*! \brief Initialize file handling. */
void __llvm_profile_initialize_file(void); void __llvm_profile_initialize_file(void);
/*! \brief Initialize the profile runtime. */
void __llvm_profile_initialize(void);
/*! /*!
* \brief Return path prefix (excluding the base filename) of the profile data. * \brief Return path prefix (excluding the base filename) of the profile data.
* This is useful for users using \c -fprofile-generate=./path_prefix who do * This is useful for users using \c -fprofile-generate=./path_prefix who do

View File

@ -10,9 +10,6 @@
#include "InstrProfilingInternal.h" #include "InstrProfilingInternal.h"
#include "InstrProfilingPort.h" #include "InstrProfilingPort.h"
/* When counters are being relocated at runtime, this parameter is set to 1. */
COMPILER_RT_VISIBILITY int RuntimeCounterRelocation = 0;
/* When continuous mode is enabled (%c), this parameter is set to 1. /* When continuous mode is enabled (%c), this parameter is set to 1.
* *
* This parameter is defined here in InstrProfilingBuffer.o, instead of in * This parameter is defined here in InstrProfilingBuffer.o, instead of in
@ -66,7 +63,7 @@ void __llvm_profile_get_padding_sizes_for_counters(
uint64_t *PaddingBytesBeforeCounters, uint64_t *PaddingBytesAfterCounters, uint64_t *PaddingBytesBeforeCounters, uint64_t *PaddingBytesAfterCounters,
uint64_t *PaddingBytesAfterNames) { uint64_t *PaddingBytesAfterNames) {
if (!__llvm_profile_is_continuous_mode_enabled() || if (!__llvm_profile_is_continuous_mode_enabled() ||
RuntimeCounterRelocation) { lprofRuntimeCounterRelocation()) {
*PaddingBytesBeforeCounters = 0; *PaddingBytesBeforeCounters = 0;
*PaddingBytesAfterCounters = 0; *PaddingBytesAfterCounters = 0;
*PaddingBytesAfterNames = __llvm_profile_get_num_padding_bytes(NamesSize); *PaddingBytesAfterNames = __llvm_profile_get_num_padding_bytes(NamesSize);

View File

@ -35,6 +35,16 @@
#include "InstrProfilingPort.h" #include "InstrProfilingPort.h"
#include "InstrProfilingUtil.h" #include "InstrProfilingUtil.h"
static int RuntimeCounterRelocation = 0;
COMPILER_RT_VISIBILITY unsigned lprofRuntimeCounterRelocation(void) {
return RuntimeCounterRelocation;
}
COMPILER_RT_VISIBILITY void lprofSetRuntimeCounterRelocation(void) {
RuntimeCounterRelocation = 1;
}
/* From where is profile name specified. /* From where is profile name specified.
* The order the enumerators define their * The order the enumerators define their
* precedence. Re-order them may lead to * precedence. Re-order them may lead to
@ -477,7 +487,8 @@ static int writeMMappedFile(FILE *OutputFile, char **Profile) {
} }
static void relocateCounters(void) { static void relocateCounters(void) {
if (!__llvm_profile_is_continuous_mode_enabled() || !RuntimeCounterRelocation) if (!__llvm_profile_is_continuous_mode_enabled() ||
!lprofRuntimeCounterRelocation())
return; return;
/* Get the sizes of various profile data sections. Taken from /* Get the sizes of various profile data sections. Taken from
@ -808,7 +819,7 @@ static void parseAndSetFilename(const char *FilenamePat,
truncateCurrentFile(); truncateCurrentFile();
if (__llvm_profile_is_continuous_mode_enabled()) { if (__llvm_profile_is_continuous_mode_enabled()) {
if (RuntimeCounterRelocation) if (lprofRuntimeCounterRelocation())
relocateCounters(); relocateCounters();
else else
initializeProfileForContinuousMode(); initializeProfileForContinuousMode();
@ -951,10 +962,10 @@ const char *__llvm_profile_get_filename(void) {
return FilenameBuf; return FilenameBuf;
} }
/* This method is invoked by the runtime initialization hook /* This API initializes the file handling, both user specified
* InstrProfilingRuntime.o if it is linked in. Both user specified
* profile path via -fprofile-instr-generate= and LLVM_PROFILE_FILE * profile path via -fprofile-instr-generate= and LLVM_PROFILE_FILE
* environment variable can override this default value. */ * environment variable can override this default value.
*/
COMPILER_RT_VISIBILITY COMPILER_RT_VISIBILITY
void __llvm_profile_initialize_file(void) { void __llvm_profile_initialize_file(void) {
const char *EnvFilenamePat; const char *EnvFilenamePat;
@ -963,7 +974,7 @@ void __llvm_profile_initialize_file(void) {
int hasCommandLineOverrider = (INSTR_PROF_PROFILE_NAME_VAR[0] != 0); int hasCommandLineOverrider = (INSTR_PROF_PROFILE_NAME_VAR[0] != 0);
if (__llvm_profile_counter_bias != -1) if (__llvm_profile_counter_bias != -1)
RuntimeCounterRelocation = 1; lprofSetRuntimeCounterRelocation();
EnvFilenamePat = getFilenamePatFromEnv(); EnvFilenamePat = getFilenamePatFromEnv();
if (EnvFilenamePat) { if (EnvFilenamePat) {
@ -982,6 +993,16 @@ void __llvm_profile_initialize_file(void) {
parseAndSetFilename(SelectedPat, PNS, 0); parseAndSetFilename(SelectedPat, PNS, 0);
} }
/* This method is invoked by the runtime initialization hook
* InstrProfilingRuntime.o if it is linked in.
*/
COMPILER_RT_VISIBILITY
void __llvm_profile_initialize(void) {
__llvm_profile_initialize_file();
if (!__llvm_profile_is_continuous_mode_enabled())
__llvm_profile_register_write_file_atexit();
}
/* This API is directly called by the user application code. It has the /* This API is directly called by the user application code. It has the
* highest precedence compared with LLVM_PROFILE_FILE environment variable * highest precedence compared with LLVM_PROFILE_FILE environment variable
* and command line option -fprofile-instr-generate=<profile_name>. * and command line option -fprofile-instr-generate=<profile_name>.

View File

@ -184,7 +184,10 @@ uint64_t lprofGetLoadModuleSignature();
unsigned lprofProfileDumped(); unsigned lprofProfileDumped();
void lprofSetProfileDumped(); void lprofSetProfileDumped();
COMPILER_RT_VISIBILITY extern int RuntimeCounterRelocation; /* Return non zero value if counters are being relocated at runtime. */
unsigned lprofRuntimeCounterRelocation(void);
void lprofSetRuntimeCounterRelocation(void);
COMPILER_RT_VISIBILITY extern void (*FreeHook)(void *); COMPILER_RT_VISIBILITY extern void (*FreeHook)(void *);
COMPILER_RT_VISIBILITY extern uint8_t *DynamicBufferIOBuffer; COMPILER_RT_VISIBILITY extern uint8_t *DynamicBufferIOBuffer;
COMPILER_RT_VISIBILITY extern uint32_t VPBufferSize; COMPILER_RT_VISIBILITY extern uint32_t VPBufferSize;

View File

@ -34,7 +34,11 @@
#include "InstrProfilingInternal.h" #include "InstrProfilingInternal.h"
#include "InstrProfilingUtil.h" #include "InstrProfilingUtil.h"
/* VMO that contains the coverage data shared across all modules. */ COMPILER_RT_VISIBILITY unsigned lprofRuntimeCounterRelocation(void) {
return 1;
}
/* VMO that contains the profile data for this module. */
static zx_handle_t __llvm_profile_vmo; static zx_handle_t __llvm_profile_vmo;
/* Current offset within the VMO where data should be written next. */ /* Current offset within the VMO where data should be written next. */
static uint64_t __llvm_profile_offset; static uint64_t __llvm_profile_offset;
@ -92,43 +96,23 @@ static void initVMOWriter(ProfDataWriter *This) {
This->WriterCtx = NULL; This->WriterCtx = NULL;
} }
static int dump(void) { /* This method is invoked by the runtime initialization hook
if (lprofProfileDumped()) { * InstrProfilingRuntime.o if it is linked in. */
lprofWrite("LLVM Profile: data not published: already written.\n");
return 0;
}
/* Check if there is llvm/runtime version mismatch. */
if (GET_VERSION(__llvm_profile_get_version()) != INSTR_PROF_RAW_VERSION) {
lprofWrite("LLVM Profile: runtime and instrumentation version mismatch: "
"expected %d, but got %d\n",
INSTR_PROF_RAW_VERSION,
(int)GET_VERSION(__llvm_profile_get_version()));
return -1;
}
/* Write the profile data into the mapped region. */
ProfDataWriter VMOWriter;
initVMOWriter(&VMOWriter);
if (lprofWriteData(&VMOWriter, lprofGetVPDataReader(), 0) != 0)
return -1;
return 0;
}
COMPILER_RT_VISIBILITY COMPILER_RT_VISIBILITY
int __llvm_profile_dump(void) { void __llvm_profile_initialize(void) {
int rc = dump(); /* This symbol is defined as weak and initialized to -1 by the runtimer, but
lprofSetProfileDumped(); * compiler will generate a strong definition initialized to 0 when runtime
return rc; * counter relocation is used. */
} if (__llvm_profile_counter_bias == -1) {
lprofWrite("LLVM Profile: counter relocation at runtime is required\n");
static void dumpWithoutReturn(void) { dump(); }
static void createVMO(void) {
/* Don't create VMO if it has been alread created. */
if (__llvm_profile_vmo != ZX_HANDLE_INVALID)
return; return;
}
/* Don't create VMO if it has been alread created. */
if (__llvm_profile_vmo != ZX_HANDLE_INVALID) {
lprofWrite("LLVM Profile: VMO has already been created\n");
return;
}
const __llvm_profile_data *DataBegin = __llvm_profile_begin_data(); const __llvm_profile_data *DataBegin = __llvm_profile_begin_data();
const __llvm_profile_data *DataEnd = __llvm_profile_end_data(); const __llvm_profile_data *DataEnd = __llvm_profile_end_data();
@ -211,23 +195,4 @@ static void createVMO(void) {
(uintptr_t)__llvm_profile_begin_counters() + CountersOffset; (uintptr_t)__llvm_profile_begin_counters() + CountersOffset;
} }
/* This method is invoked by the runtime initialization hook
* InstrProfilingRuntime.o if it is linked in.
*/
COMPILER_RT_VISIBILITY
void __llvm_profile_initialize_file(void) { createVMO(); }
COMPILER_RT_VISIBILITY
int __llvm_profile_register_write_file_atexit(void) {
static bool HasBeenRegistered = false;
if (HasBeenRegistered)
return 0;
lprofSetupValueProfiler();
HasBeenRegistered = true;
return atexit(dumpWithoutReturn);
}
#endif #endif

View File

@ -19,9 +19,7 @@ namespace {
class RegisterRuntime { class RegisterRuntime {
public: public:
RegisterRuntime() { RegisterRuntime() {
__llvm_profile_initialize_file(); __llvm_profile_initialize();
if (!__llvm_profile_is_continuous_mode_enabled())
__llvm_profile_register_write_file_atexit();
} }
}; };