Skip to content

Commit 5c20d7d

Browse files
committed
[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
1 parent 9ab5362 commit 5c20d7d

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-2
lines changed

compiler-rt/lib/memprof/memprof_interface_internal.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ const char *__memprof_default_options();
4646
SANITIZER_INTERFACE_ATTRIBUTE
4747
extern uptr __memprof_shadow_memory_dynamic_address;
4848

49+
SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE extern char
50+
__memprof_profile_filename[1];
51+
4952
SANITIZER_INTERFACE_ATTRIBUTE void __memprof_load(uptr p);
5053
SANITIZER_INTERFACE_ATTRIBUTE void __memprof_store(uptr p);
5154

compiler-rt/lib/memprof/memprof_rtl.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727

2828
uptr __memprof_shadow_memory_dynamic_address; // Global interface symbol.
2929

30+
// Allow the user to specify a profile output file via the binary.
31+
SANITIZER_WEAK_ATTRIBUTE char __memprof_profile_filename[1];
32+
3033
namespace __memprof {
3134

3235
static void MemprofDie() {
@@ -172,7 +175,12 @@ static void MemprofInitInternal() {
172175
AddDieCallback(MemprofDie);
173176
SetCheckFailedCallback(MemprofCheckFailed);
174177

175-
__sanitizer_set_report_path(common_flags()->log_path);
178+
// Use profile name specified via the binary itself if it exists, and hasn't
179+
// been overrriden by a flag at runtime.
180+
if (__memprof_profile_filename[0] != 0 && !common_flags()->log_path)
181+
__sanitizer_set_report_path(__memprof_profile_filename);
182+
else
183+
__sanitizer_set_report_path(common_flags()->log_path);
176184

177185
__sanitizer::InitializePlatformEarly();
178186

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
___memprof_default_options
1+
___memprof_default_options __memprof_profile_filename

compiler-rt/test/memprof/TestCases/log_path_test.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,24 @@
2121
// RUN: not %run %t 2> %t.out
2222
// RUN: FileCheck %s --check-prefix=CHECK-LONG < %t.out
2323

24+
// Specifying the log name via the __memprof_profile_filename variable.
25+
// RUN: %clangxx_memprof %s -o %t -DPROFILE_NAME_VAR="%t.log2"
26+
// RUN: rm -f %t.log2.*
27+
// RUN: %run %t 2> %t.out
28+
// RUN: FileCheck %s --check-prefix=CHECK-GOOD < %t.log2.*
29+
30+
// Check that the log_path option overrides the log name set via the
31+
// __memprof_profile_filename variable.
32+
// RUN: rm -f %t.log.*
33+
// RUN: %env_memprof_opts=log_path=%t.log %run %t 2> %t.out
34+
// RUN: FileCheck %s --check-prefix=CHECK-GOOD < %t.log.*
35+
36+
#ifdef PROFILE_NAME_VAR
37+
#define xstr(s) str(s)
38+
#define str(s) #s
39+
char __memprof_profile_filename[] = xstr(PROFILE_NAME_VAR);
40+
#endif
41+
2442
#include <stdlib.h>
2543
#include <string.h>
2644
int main(int argc, char **argv) {

0 commit comments

Comments
 (0)