[compiler-rt] r362676 - [Profile]: Add runtime interface to specify file handle for profile data.
Xinliang David Li via llvm-commits
llvm-commits at lists.llvm.org
Wed Jun 5 23:35:19 PDT 2019
Author: davidxl
Date: Wed Jun 5 23:35:18 2019
New Revision: 362676
URL: http://llvm.org/viewvc/llvm-project?rev=362676&view=rev
Log:
[Profile]: Add runtime interface to specify file handle for profile data.
Author: Sajjad Mirza
Differential Revision: http://reviews.llvm.org/D62541
Modified:
compiler-rt/trunk/lib/profile/InstrProfiling.h
compiler-rt/trunk/lib/profile/InstrProfilingFile.c
compiler-rt/trunk/lib/profile/InstrProfilingUtil.c
compiler-rt/trunk/lib/profile/InstrProfilingUtil.h
Modified: compiler-rt/trunk/lib/profile/InstrProfiling.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/profile/InstrProfiling.h?rev=362676&r1=362675&r2=362676&view=diff
==============================================================================
--- compiler-rt/trunk/lib/profile/InstrProfiling.h (original)
+++ compiler-rt/trunk/lib/profile/InstrProfiling.h Wed Jun 5 23:35:18 2019
@@ -10,6 +10,7 @@
#define PROFILE_INSTRPROFILING_H_
#include "InstrProfilingPort.h"
+#include <stdio.h>
#define INSTR_PROF_VISIBILITY COMPILER_RT_VISIBILITY
#include "InstrProfData.inc"
@@ -125,7 +126,7 @@ int __llvm_orderfile_write_file(void);
/*!
* \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,24 @@ int __llvm_orderfile_dump(void);
*/
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.
+ *
+ * If \c EnableMerge is nonzero, the runtime will always merge profiling data
+ * with the contents of the profiling file. If EnableMerge is zero, the runtime
+ * may still merge the data if it would have merged for another reason (for
+ * example, because of a %m specifier in the file name).
+ */
+void __llvm_profile_set_file_object(FILE *File, int EnableMerge);
+
/*! \brief Register to write instrumentation data to file at exit. */
int __llvm_profile_register_write_file_atexit(void);
Modified: compiler-rt/trunk/lib/profile/InstrProfilingFile.c
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/profile/InstrProfilingFile.c?rev=362676&r1=362675&r2=362676&view=diff
==============================================================================
--- compiler-rt/trunk/lib/profile/InstrProfilingFile.c (original)
+++ compiler-rt/trunk/lib/profile/InstrProfilingFile.c Wed Jun 5 23:35:18 2019
@@ -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,27 @@ typedef struct lprofFilename {
COMPILER_RT_WEAK lprofFilename lprofCurFilename = {0, 0, 0, 0, {0},
{0}, 0, 0, 0, PNS_unknown};
+static int ProfileMergeRequested = 0;
+static int isProfileMergeRequested() { return ProfileMergeRequested; }
+static void setProfileMergeRequested(int EnableMerge) {
+ ProfileMergeRequested = EnableMerge;
+}
+
+static FILE *ProfileFile = NULL;
+static FILE *getProfileFile() { return ProfileFile; }
+static void setProfileFile(FILE *File) { ProfileFile = File; }
+
+COMPILER_RT_VISIBILITY void __llvm_profile_set_file_object(FILE *File,
+ int EnableMerge) {
+ setProfileFile(File);
+ setProfileMergeRequested(EnableMerge);
+}
+
static int getCurFilenameLength();
static const char *getCurFilename(char *FilenameBuf, int ForceUseBuf);
-static unsigned doMerging() { return lprofCurFilename.MergePoolSize; }
+static unsigned doMerging() {
+ return lprofCurFilename.MergePoolSize || isProfileMergeRequested();
+}
/* Return 1 if there is an error, otherwise return 0. */
static uint32_t fileWriter(ProfDataWriter *This, ProfDataIOVec *IOVecs,
@@ -225,11 +243,16 @@ static void createProfileDir(const char
* its instrumented shared libraries dump profile data into their own data file.
*/
static FILE *openFileForMerging(const char *ProfileFileName, int *MergeDone) {
- FILE *ProfileFile;
+ FILE *ProfileFile = NULL;
int rc;
- createProfileDir(ProfileFileName);
- ProfileFile = lprofOpenFileEx(ProfileFileName);
+ ProfileFile = getProfileFile();
+ if (ProfileFile) {
+ lprofLockFileHandle(ProfileFile);
+ } else {
+ createProfileDir(ProfileFileName);
+ ProfileFile = lprofOpenFileEx(ProfileFileName);
+ }
if (!ProfileFile)
return NULL;
@@ -244,6 +267,16 @@ static FILE *openFileForMerging(const ch
return ProfileFile;
}
+static FILE *GetFileObject(const char *OutputName) {
+ FILE *File;
+ File = getProfileFile();
+ if (File != NULL) {
+ return File;
+ }
+
+ return fopen(OutputName, "ab");
+}
+
/* Write profile data to file \c OutputName. */
static int writeFile(const char *OutputName) {
int RetVal;
@@ -251,10 +284,10 @@ static int writeFile(const char *OutputN
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 +298,15 @@ static int writeFile(const char *OutputN
initFileWriter(&fileWriter, OutputFile);
RetVal = lprofWriteData(&fileWriter, lprofGetVPDataReader(), MergeDone);
- fclose(OutputFile);
+ if (doMerging()) {
+ lprofUnlockFileHandle(OutputFile);
+ }
+
+ if (OutputFile == getProfileFile())
+ fflush(OutputFile);
+ else
+ fclose(OutputFile);
+
return RetVal;
}
@@ -591,7 +632,7 @@ void __llvm_profile_initialize_file(void
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 +668,7 @@ int __llvm_profile_write_file(void) {
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;
}
Modified: compiler-rt/trunk/lib/profile/InstrProfilingUtil.c
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/profile/InstrProfilingUtil.c?rev=362676&r1=362675&r2=362676&view=diff
==============================================================================
--- compiler-rt/trunk/lib/profile/InstrProfilingUtil.c (original)
+++ compiler-rt/trunk/lib/profile/InstrProfilingUtil.c Wed Jun 5 23:35:18 2019
@@ -154,6 +154,26 @@ COMPILER_RT_VISIBILITY int lprofUnlockFd
#endif
}
+COMPILER_RT_VISIBILITY int lprofLockFileHandle(FILE *F) {
+ int fd;
+#if defined(_WIN32)
+ fd = _fileno(F);
+#else
+ fd = fileno(F);
+#endif
+ return lprofLockFd(fd);
+}
+
+COMPILER_RT_VISIBILITY int lprofUnlockFileHandle(FILE *F) {
+ int fd;
+#if defined(_WIN32)
+ fd = _fileno(F);
+#else
+ fd = fileno(F);
+#endif
+ return lprofUnlockFd(fd);
+}
+
COMPILER_RT_VISIBILITY FILE *lprofOpenFileEx(const char *ProfileName) {
FILE *f;
int fd;
Modified: compiler-rt/trunk/lib/profile/InstrProfilingUtil.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/profile/InstrProfilingUtil.h?rev=362676&r1=362675&r2=362676&view=diff
==============================================================================
--- compiler-rt/trunk/lib/profile/InstrProfilingUtil.h (original)
+++ compiler-rt/trunk/lib/profile/InstrProfilingUtil.h Wed Jun 5 23:35:18 2019
@@ -23,6 +23,8 @@ unsigned __llvm_profile_get_dir_mode(voi
int lprofLockFd(int fd);
int lprofUnlockFd(int fd);
+int lprofLockFileHandle(FILE *F);
+int lprofUnlockFileHandle(FILE *F);
/*! Open file \c Filename for read+write with write
* lock for exclusive access. The caller will block
More information about the llvm-commits
mailing list