[MemProf] Allow the binary to specify the profile output filename
authorTeresa Johnson <tejohnson@google.com>
Tue, 29 Sep 2020 22:31:11 +0000 (15:31 -0700)
committerTeresa Johnson <tejohnson@google.com>
Thu, 22 Oct 2020 15:30:19 +0000 (08:30 -0700)
This will allow the output directory to be specified by a build time
option, similar to the directory specified for regular PGO profiles via
-fprofile-generate=. The memory profiling instrumentation pass will
set up the variable. This is the same mechanism used by the PGO
instrumentation and runtime.

Depends on D87120 and D89629.

Differential Revision: https://reviews.llvm.org/D89086

compiler-rt/lib/memprof/memprof_interface_internal.h
compiler-rt/lib/memprof/memprof_rtl.cpp
compiler-rt/lib/memprof/weak_symbols.txt
compiler-rt/test/memprof/TestCases/log_path_test.cpp

index ea410f5..a51d83a 100644 (file)
@@ -46,6 +46,9 @@ const char *__memprof_default_options();
 SANITIZER_INTERFACE_ATTRIBUTE
 extern uptr __memprof_shadow_memory_dynamic_address;
 
+SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE extern char
+    __memprof_profile_filename[1];
+
 SANITIZER_INTERFACE_ATTRIBUTE void __memprof_load(uptr p);
 SANITIZER_INTERFACE_ATTRIBUTE void __memprof_store(uptr p);
 
index 6127040..93264dd 100644 (file)
@@ -27,6 +27,9 @@
 
 uptr __memprof_shadow_memory_dynamic_address; // Global interface symbol.
 
+// Allow the user to specify a profile output file via the binary.
+SANITIZER_WEAK_ATTRIBUTE char __memprof_profile_filename[1];
+
 namespace __memprof {
 
 static void MemprofDie() {
@@ -172,7 +175,12 @@ static void MemprofInitInternal() {
   AddDieCallback(MemprofDie);
   SetCheckFailedCallback(MemprofCheckFailed);
 
-  __sanitizer_set_report_path(common_flags()->log_path);
+  // Use profile name specified via the binary itself if it exists, and hasn't
+  // been overrriden by a flag at runtime.
+  if (__memprof_profile_filename[0] != 0 && !common_flags()->log_path)
+    __sanitizer_set_report_path(__memprof_profile_filename);
+  else
+    __sanitizer_set_report_path(common_flags()->log_path);
 
   __sanitizer::InitializePlatformEarly();
 
index bb2dea8..2718136 100644 (file)
@@ -1 +1 @@
-___memprof_default_options
+___memprof_default_options __memprof_profile_filename
index 0b1d498..fbc0518 100644 (file)
 // RUN:   not %run %t 2> %t.out
 // RUN: FileCheck %s --check-prefix=CHECK-LONG < %t.out
 
+// Specifying the log name via the __memprof_profile_filename variable.
+// RUN: %clangxx_memprof  %s -o %t -DPROFILE_NAME_VAR="%t.log2"
+// RUN: rm -f %t.log2.*
+// RUN: %run %t 2> %t.out
+// RUN: FileCheck %s --check-prefix=CHECK-GOOD < %t.log2.*
+
+// Check that the log_path option overrides the log name set via the
+// __memprof_profile_filename variable.
+// RUN: rm -f %t.log.*
+// RUN: %env_memprof_opts=log_path=%t.log %run %t 2> %t.out
+// RUN: FileCheck %s --check-prefix=CHECK-GOOD < %t.log.*
+
+#ifdef PROFILE_NAME_VAR
+#define xstr(s) str(s)
+#define str(s) #s
+char __memprof_profile_filename[] = xstr(PROFILE_NAME_VAR);
+#endif
+
 #include <stdlib.h>
 #include <string.h>
 int main(int argc, char **argv) {