summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTeresa Johnson <tejohnson@google.com>2018-07-19 19:03:50 +0000
committerTeresa Johnson <tejohnson@google.com>2018-07-19 19:03:50 +0000
commit3640f73c3f31b32f2eed19dead7b4eb53db2107f (patch)
tree1beb4410a0866755e012d0765ee4ea6399678b59
parent2ae0202ef135697860741f591c41433a3d329c9e (diff)
[profile] Add interface to get profile filename
Summary: Add __llvm_profile_get_filename interface to get the profile filename, which can be used for identifying which profile file belongs to an app when multiple binaries are instrumented and dumping profiles into the same directory. The filename includes the path. Reviewers: davidxl Subscribers: delcypher, #sanitizers, llvm-commits Differential Revision: https://reviews.llvm.org/D49529 git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@337482 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--lib/profile/InstrProfiling.h8
-rw-r--r--lib/profile/InstrProfilingFile.c59
-rw-r--r--test/profile/instrprof-get-filename.c39
3 files changed, 94 insertions, 12 deletions
diff --git a/lib/profile/InstrProfiling.h b/lib/profile/InstrProfiling.h
index 740e2d4d5..3ef7eacf0 100644
--- a/lib/profile/InstrProfiling.h
+++ b/lib/profile/InstrProfiling.h
@@ -170,6 +170,14 @@ void __llvm_profile_initialize_file(void);
*/
const char *__llvm_profile_get_path_prefix();
+/*!
+ * \brief Return filename (including path) of the profile data. Note that if the
+ * user calls __llvm_profile_set_filename later after invoking this interface,
+ * the actual file name may differ from what is returned here.
+ * Side-effect: this API call will invoke malloc with dynamic memory allocation.
+ */
+const char *__llvm_profile_get_filename();
+
/*! \brief Get the magic token for the file format. */
uint64_t __llvm_profile_get_magic(void);
diff --git a/lib/profile/InstrProfilingFile.c b/lib/profile/InstrProfilingFile.c
index 1299a6d8c..f6013ad22 100644
--- a/lib/profile/InstrProfilingFile.c
+++ b/lib/profile/InstrProfilingFile.c
@@ -69,6 +69,7 @@ typedef struct lprofFilename {
* by runtime. */
unsigned OwnsFilenamePat;
const char *ProfilePathPrefix;
+ const char *Filename;
char PidChars[MAX_PID_SIZE];
char Hostname[COMPILER_RT_MAX_HOSTLEN];
unsigned NumPids;
@@ -84,11 +85,11 @@ typedef struct lprofFilename {
ProfileNameSpecifier PNS;
} lprofFilename;
-COMPILER_RT_WEAK lprofFilename lprofCurFilename = {0, 0, 0, {0}, {0},
- 0, 0, 0, PNS_unknown};
+COMPILER_RT_WEAK lprofFilename lprofCurFilename = {0, 0, 0, 0, {0},
+ {0}, 0, 0, 0, PNS_unknown};
static int getCurFilenameLength();
-static const char *getCurFilename(char *FilenameBuf);
+static const char *getCurFilename(char *FilenameBuf, int ForceUseBuf);
static unsigned doMerging() { return lprofCurFilename.MergePoolSize; }
/* Return 1 if there is an error, otherwise return 0. */
@@ -266,7 +267,7 @@ static void truncateCurrentFile(void) {
Length = getCurFilenameLength();
FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1);
- Filename = getCurFilename(FilenameBuf);
+ Filename = getCurFilename(FilenameBuf, 0);
if (!Filename)
return;
@@ -311,9 +312,12 @@ static int parseFilenamePattern(const char *FilenamePat,
char *Hostname = &lprofCurFilename.Hostname[0];
int MergingEnabled = 0;
- /* Clean up cached prefix. */
+ /* Clean up cached prefix and filename. */
if (lprofCurFilename.ProfilePathPrefix)
free((void *)lprofCurFilename.ProfilePathPrefix);
+ if (lprofCurFilename.Filename)
+ free((void *)lprofCurFilename.Filename);
+
memset(&lprofCurFilename, 0, sizeof(lprofCurFilename));
if (lprofCurFilename.FilenamePat && lprofCurFilename.OwnsFilenamePat) {
@@ -428,17 +432,25 @@ static int getCurFilenameLength() {
/* Return the pointer to the current profile file name (after substituting
* PIDs and Hostnames in filename pattern. \p FilenameBuf is the buffer
* to store the resulting filename. If no substitution is needed, the
- * current filename pattern string is directly returned. */
-static const char *getCurFilename(char *FilenameBuf) {
- int I, J, PidLength, HostNameLength;
+ * current filename pattern string is directly returned, unless ForceUseBuf
+ * is enabled. */
+static const char *getCurFilename(char *FilenameBuf, int ForceUseBuf) {
+ int I, J, PidLength, HostNameLength, FilenamePatLength;
const char *FilenamePat = lprofCurFilename.FilenamePat;
if (!lprofCurFilename.FilenamePat || !lprofCurFilename.FilenamePat[0])
return 0;
if (!(lprofCurFilename.NumPids || lprofCurFilename.NumHosts ||
- lprofCurFilename.MergePoolSize))
- return lprofCurFilename.FilenamePat;
+ lprofCurFilename.MergePoolSize)) {
+ if (!ForceUseBuf)
+ return lprofCurFilename.FilenamePat;
+
+ FilenamePatLength = strlen(lprofCurFilename.FilenamePat);
+ memcpy(FilenameBuf, lprofCurFilename.FilenamePat, FilenamePatLength);
+ FilenameBuf[FilenamePatLength] = '\0';
+ return FilenameBuf;
+ }
PidLength = strlen(lprofCurFilename.PidChars);
HostNameLength = strlen(lprofCurFilename.Hostname);
@@ -492,7 +504,7 @@ const char *__llvm_profile_get_path_prefix(void) {
Length = getCurFilenameLength();
FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1);
- Filename = getCurFilename(FilenameBuf);
+ Filename = getCurFilename(FilenameBuf, 0);
if (!Filename)
return "\0";
@@ -512,6 +524,29 @@ const char *__llvm_profile_get_path_prefix(void) {
return Prefix;
}
+COMPILER_RT_VISIBILITY
+const char *__llvm_profile_get_filename(void) {
+ int Length;
+ char *FilenameBuf;
+ const char *Filename;
+
+ if (lprofCurFilename.Filename)
+ return lprofCurFilename.Filename;
+
+ Length = getCurFilenameLength();
+ FilenameBuf = (char *)malloc(Length + 1);
+ if (!FilenameBuf) {
+ PROF_ERR("Failed to %s\n", "allocate memory.");
+ return "\0";
+ }
+ Filename = getCurFilename(FilenameBuf, 1);
+ if (!Filename)
+ return "\0";
+
+ lprofCurFilename.Filename = FilenameBuf;
+ return FilenameBuf;
+}
+
/* This method is invoked by the runtime initialization hook
* InstrProfilingRuntime.o if it is linked in. Both user specified
* profile path via -fprofile-instr-generate= and LLVM_PROFILE_FILE
@@ -568,7 +603,7 @@ int __llvm_profile_write_file(void) {
Length = getCurFilenameLength();
FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1);
- Filename = getCurFilename(FilenameBuf);
+ Filename = getCurFilename(FilenameBuf, 0);
/* Check the filename. */
if (!Filename) {
diff --git a/test/profile/instrprof-get-filename.c b/test/profile/instrprof-get-filename.c
new file mode 100644
index 000000000..031b75f12
--- /dev/null
+++ b/test/profile/instrprof-get-filename.c
@@ -0,0 +1,39 @@
+// Test __llvm_profile_get_filename.
+// RUN: %clang_pgogen -O2 -o %t %s
+// RUN: %run %t
+
+#include <stdio.h>
+#include <string.h>
+
+const char *__llvm_profile_get_filename();
+void __llvm_profile_set_filename(const char *);
+
+int main(int argc, const char *argv[]) {
+ int i;
+ const char *filename;
+ const char *new_filename = "/path/to/test.profraw";
+
+ filename = __llvm_profile_get_filename();
+ if (strncmp(filename, "default_", 8)) {
+ fprintf(stderr,
+ "Error: got filename %s, expected it to start with 'default_'\n",
+ filename);
+ return 1;
+ }
+ if (strcmp(filename + strlen(filename) - strlen(".profraw"), ".profraw")) {
+ fprintf(stderr,
+ "Error: got filename %s, expected it to end with '.profraw'\n",
+ filename);
+ return 1;
+ }
+
+ __llvm_profile_set_filename(new_filename);
+ filename = __llvm_profile_get_filename();
+ if (strcmp(filename, new_filename)) {
+ fprintf(stderr, "Error: got filename %s, expected '%s'\n", filename,
+ new_filename);
+ return 1;
+ }
+
+ return 0;
+}