Skip to content

Commit ea1826e

Browse files
committed
[MemProf] Defer profile file setup until dump time
With all of the writing of the memprof profile consolidated into one place, there is no need to set up the profile file (which creates the file and also redirects all printing from the runtime to it) until we are ready to dump the profile. This allows errors and other messages to be dumped to stderr instead of the profile file, which by default is in a binary format. Additionally, reset the output file to stderr after dumping the profile so that any requested memprof allocator statistics are printed to stderr. Differential Revision: https://reviews.llvm.org/D138175
1 parent af029d3 commit ea1826e

File tree

4 files changed

+21
-10
lines changed

4 files changed

+21
-10
lines changed

compiler-rt/lib/memprof/memprof_allocator.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "sanitizer_common/sanitizer_errno.h"
2727
#include "sanitizer_common/sanitizer_file.h"
2828
#include "sanitizer_common/sanitizer_flags.h"
29+
#include "sanitizer_common/sanitizer_interface_internal.h"
2930
#include "sanitizer_common/sanitizer_internal_defs.h"
3031
#include "sanitizer_common/sanitizer_list.h"
3132
#include "sanitizer_common/sanitizer_procmaps.h"
@@ -35,6 +36,9 @@
3536
#include <sched.h>
3637
#include <time.h>
3738

39+
// Allow the user to specify a profile output file via the binary.
40+
SANITIZER_WEAK_ATTRIBUTE char __memprof_profile_filename[1];
41+
3842
namespace __memprof {
3943
namespace {
4044
using ::llvm::memprof::MemInfoBlock;
@@ -280,6 +284,12 @@ struct Allocator {
280284
}
281285

282286
void FinishAndWrite() {
287+
// Use profile name specified via the binary itself if it exists, and hasn't
288+
// been overrriden by a flag at runtime.
289+
if (__memprof_profile_filename[0] != 0 && !common_flags()->log_path)
290+
__sanitizer_set_report_path(__memprof_profile_filename);
291+
else
292+
__sanitizer_set_report_path(common_flags()->log_path);
283293
if (print_text && common_flags()->print_module_map)
284294
DumpProcessMap();
285295

@@ -304,6 +314,11 @@ struct Allocator {
304314
}
305315

306316
allocator.ForceUnlock();
317+
318+
// Set the report back to the default stderr now that we have dumped the
319+
// profile, in case there are later errors or stats dumping on exit has been
320+
// enabled.
321+
__sanitizer_set_report_path("stderr");
307322
}
308323

309324
// Inserts any blocks which have been allocated but not yet deallocated.

compiler-rt/lib/memprof/memprof_rtl.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@
2929

3030
uptr __memprof_shadow_memory_dynamic_address; // Global interface symbol.
3131

32-
// Allow the user to specify a profile output file via the binary.
33-
SANITIZER_WEAK_ATTRIBUTE char __memprof_profile_filename[1];
34-
3532
namespace __memprof {
3633

3734
static void MemprofDie() {
@@ -169,13 +166,6 @@ static void MemprofInitInternal() {
169166
AddDieCallback(MemprofDie);
170167
SetCheckUnwindCallback(CheckUnwind);
171168

172-
// Use profile name specified via the binary itself if it exists, and hasn't
173-
// been overrriden by a flag at runtime.
174-
if (__memprof_profile_filename[0] != 0 && !common_flags()->log_path)
175-
__sanitizer_set_report_path(__memprof_profile_filename);
176-
else
177-
__sanitizer_set_report_path(common_flags()->log_path);
178-
179169
__sanitizer::InitializePlatformEarly();
180170

181171
// Setup internal allocator callback.

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
// RUN: %clangxx_memprof -O0 %s -o %t
44
// RUN: %env_memprof_opts=print_text=true:log_path=stderr:atexit=1 %run %t 2>&1 | FileCheck %s
5+
// Stats should be dumped to stderr even if the profile log path set to a file.
6+
// RUN: rm -f %t.log.*
7+
// RUN: %env_memprof_opts=print_text=true:log_path=%t.log:atexit=1 %run %t 2>&1 | FileCheck %s
58
// RUN: %env_memprof_opts=print_text=true:log_path=stderr:atexit=0 %run %t 2>&1 | FileCheck %s --check-prefix=NOATEXIT
69

710
// CHECK: MemProfiler exit stats:

compiler-rt/test/memprof/TestCases/malloc-size-too-big.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
// RUN: %clangxx_memprof -O0 %s -o %t
22
// RUN: %env_memprof_opts=print_text=true:log_path=stderr:allocator_may_return_null=0 not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK --check-prefix=CHECK-SUMMARY
3+
// Errors should be printed to stderr even if the log_path set to a file.
4+
// RUN: rm -f %t.log.*
5+
// RUN: %env_memprof_opts=print_text=true:log_path=%t.log:allocator_may_return_null=0 not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK --check-prefix=CHECK-SUMMARY
36
// RUN: %env_memprof_opts=print_text=true:log_path=stderr:allocator_may_return_null=1 %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-NULL
47
// Test print_summary
58
// RUN: %env_memprof_opts=print_text=true:log_path=stderr:allocator_may_return_null=0:print_summary=0 not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK --check-prefix=CHECK-NOSUMMARY

0 commit comments

Comments
 (0)