From 5c20d7db9f2791367b9311130eb44afecb16829c Mon Sep 17 00:00:00 2001 From: Teresa Johnson Date: Tue, 29 Sep 2020 15:31:11 -0700 Subject: [PATCH] [MemProf] Allow the binary to specify the profile output filename 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 | 3 +++ compiler-rt/lib/memprof/memprof_rtl.cpp | 10 +++++++++- compiler-rt/lib/memprof/weak_symbols.txt | 2 +- compiler-rt/test/memprof/TestCases/log_path_test.cpp | 18 ++++++++++++++++++ 4 files changed, 31 insertions(+), 2 deletions(-) diff --git a/compiler-rt/lib/memprof/memprof_interface_internal.h b/compiler-rt/lib/memprof/memprof_interface_internal.h index ea410f5..a51d83a 100644 --- a/compiler-rt/lib/memprof/memprof_interface_internal.h +++ b/compiler-rt/lib/memprof/memprof_interface_internal.h @@ -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); diff --git a/compiler-rt/lib/memprof/memprof_rtl.cpp b/compiler-rt/lib/memprof/memprof_rtl.cpp index 6127040..93264dd 100644 --- a/compiler-rt/lib/memprof/memprof_rtl.cpp +++ b/compiler-rt/lib/memprof/memprof_rtl.cpp @@ -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(); diff --git a/compiler-rt/lib/memprof/weak_symbols.txt b/compiler-rt/lib/memprof/weak_symbols.txt index bb2dea8..2718136 100644 --- a/compiler-rt/lib/memprof/weak_symbols.txt +++ b/compiler-rt/lib/memprof/weak_symbols.txt @@ -1 +1 @@ -___memprof_default_options +___memprof_default_options __memprof_profile_filename diff --git a/compiler-rt/test/memprof/TestCases/log_path_test.cpp b/compiler-rt/test/memprof/TestCases/log_path_test.cpp index 0b1d498..fbc0518 100644 --- a/compiler-rt/test/memprof/TestCases/log_path_test.cpp +++ b/compiler-rt/test/memprof/TestCases/log_path_test.cpp @@ -21,6 +21,24 @@ // 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 #include int main(int argc, char **argv) { -- 2.7.4