diff --git a/compiler-rt/lib/profile/InstrProfiling.h b/compiler-rt/lib/profile/InstrProfiling.h --- a/compiler-rt/lib/profile/InstrProfiling.h +++ b/compiler-rt/lib/profile/InstrProfiling.h @@ -10,6 +10,7 @@ #define PROFILE_INSTRPROFILING_H_ #include "InstrProfilingPort.h" +#include #define INSTR_PROF_VISIBILITY COMPILER_RT_VISIBILITY #include "InstrProfData.inc" @@ -125,7 +126,7 @@ /*! * \brief this is a wrapper interface to \c __llvm_profile_write_file. * After this interface is invoked, a arleady dumped flag will be set - * so that profile won't be dumped again during program exit. + * so that profile won't be dumped again during program exit. * Invocation of interface __llvm_profile_reset_counters will clear * the flag. This interface is designed to be used to collect profile * data from user selected hot regions. The use model is @@ -157,6 +158,23 @@ */ void __llvm_profile_set_filename(const char *Name); +/*! + * \brief Set the FILE object for writing instrumentation data. + * + * Sets the FILE object to be used for subsequent calls to + * \a __llvm_profile_write_file(). The profile file name set by environment + * variable, command-line option, or calls to \a __llvm_profile_set_filename + * will be ignored. + * + * \c File will not be closed after a call to \a __llvm_profile_write_file() but + * it may be flushed. Passing NULL restores default behavior. + * + * \c If IsExclusive is nonzero, the runtime will assume that it has exclusive + * access to the file, even among other processes, and thus it is safe to use + * the file for online merging if it would normally do so. + */ +void __llvm_profile_set_file_object(FILE *File, int IsExclusive); + /*! \brief Register to write instrumentation data to file at exit. */ int __llvm_profile_register_write_file_atexit(void); diff --git a/compiler-rt/lib/profile/InstrProfilingFile.c b/compiler-rt/lib/profile/InstrProfilingFile.c --- a/compiler-rt/lib/profile/InstrProfilingFile.c +++ b/compiler-rt/lib/profile/InstrProfilingFile.c @@ -37,7 +37,7 @@ /* From where is profile name specified. * The order the enumerators define their * precedence. Re-order them may lead to - * runtime behavior change. */ + * runtime behavior change. */ typedef enum ProfileNameSpecifier { PNS_unknown = 0, PNS_default, @@ -89,9 +89,32 @@ COMPILER_RT_WEAK lprofFilename lprofCurFilename = {0, 0, 0, 0, {0}, {0}, 0, 0, 0, PNS_unknown}; +static int ProfileFileSafeToMerge = 0; +static int lprofIsProfileFileSafeToMerge() { return ProfileFileSafeToMerge; } +static void lprofSetProfileFileSafeToMerge(int IsSafe) { + return ProfileFileSafeToMerge; +} + +static FILE *ProfileFile = NULL; +static FILE *lprofGetProfileFile() { return ProfileFile; } +static void lprofSetProfileFile(FILE *File) { + ProfileFile = File; +} + +COMPILER_RT_VISIBILITY void __llvm_profile_set_file_object(FILE *File, + int IsExclusive) { + lprofSetProfileFile(File); + lprofSetProfileFileSafeToMerge(IsExclusive); +} + static int getCurFilenameLength(); static const char *getCurFilename(char *FilenameBuf, int ForceUseBuf); -static unsigned doMerging() { return lprofCurFilename.MergePoolSize; } +static unsigned doMerging() { + if (lprofGetProfileFile() != NULL && !lprofIsProfileFileSafeToMerge()) { + return 0; + } + return lprofCurFilename.MergePoolSize; +} /* Return 1 if there is an error, otherwise return 0. */ static uint32_t fileWriter(ProfDataWriter *This, ProfDataIOVec *IOVecs, @@ -228,8 +251,11 @@ FILE *ProfileFile; int rc; - createProfileDir(ProfileFileName); - ProfileFile = lprofOpenFileEx(ProfileFileName); + ProfileFile = lprofGetProfileFile(); + if (!ProfileFile) { + createProfileDir(ProfileFileName); + ProfileFile = lprofOpenFileEx(ProfileFileName); + } if (!ProfileFile) return NULL; @@ -244,17 +270,28 @@ return ProfileFile; } +static FILE* GetFileObject(const char *OutputName) { + FILE* File; + File = lprofGetProfileFile(); + if (File != NULL) { + return File; + } + + return fopen(OutputName, "ab"); +} + /* Write profile data to file \c OutputName. */ static int writeFile(const char *OutputName) { int RetVal; FILE *OutputFile; + FILE *ProfileFilePtr; int MergeDone = 0; VPMergeHook = &lprofMergeValueProfData; - if (!doMerging()) - OutputFile = fopen(OutputName, "ab"); - else + if (doMerging()) OutputFile = openFileForMerging(OutputName, &MergeDone); + else + OutputFile = GetFileObject(OutputName); if (!OutputFile) return -1; @@ -265,7 +302,10 @@ initFileWriter(&fileWriter, OutputFile); RetVal = lprofWriteData(&fileWriter, lprofGetVPDataReader(), MergeDone); - fclose(OutputFile); + if (ProfileFilePtr == NULL) + fclose(OutputFile); + else + fflush(OutputFile); return RetVal; } @@ -591,7 +631,7 @@ EnvFilenamePat = getFilenamePatFromEnv(); if (EnvFilenamePat) { - /* Pass CopyFilenamePat = 1, to ensure that the filename would be valid + /* Pass CopyFilenamePat = 1, to ensure that the filename would be valid at the moment when __llvm_profile_write_file() gets executed. */ parseAndSetFilename(EnvFilenamePat, PNS_environment, 1); return; @@ -627,8 +667,7 @@ int PDeathSig = 0; if (lprofProfileDumped()) { - PROF_NOTE("Profile data not written to file: %s.\n", - "already written"); + PROF_NOTE("Profile data not written to file: %s.\n", "already written"); return 0; } diff --git a/compiler-rt/test/profile/instrprof-set-file-object.c b/compiler-rt/test/profile/instrprof-set-file-object.c new file mode 100644 --- /dev/null +++ b/compiler-rt/test/profile/instrprof-set-file-object.c @@ -0,0 +1,18 @@ +// Test that the specified output has profiling data. +// RUN: %clang_profgen -o %t -O3 %s +// RUN: %run %t %t.file.profraw +// RUN: test -f %t.file.profraw +// RUN: llvm-profdata show %t.file.profraw +// RUN: rm %t.file.profraw +#include + +extern void __llvm_profile_set_file_object(FILE*); + +int main(int argc, const char *argv[]) { + if (argc < 2) + return 1; + + FILE* F = fopen(argv[1], "w+b"); + __llvm_profile_set_file_object(F); + return 0; +}