Skip to content

Commit a75b2e8

Browse files
committed
[MemProf] Add interface to dump profile
Add an interface so that the profile can be dumped on demand. Differential Revision: https://reviews.llvm.org/D91768
1 parent 617e8e5 commit a75b2e8

File tree

5 files changed

+39
-0
lines changed

5 files changed

+39
-0
lines changed

compiler-rt/include/sanitizer/memprof_interface.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ void __memprof_print_accumulated_stats(void);
5353
/// \returns Default options string.
5454
const char *__memprof_default_options(void);
5555

56+
/// Prints the memory profile to the current profile file.
57+
///
58+
/// \returns 0 on success.
59+
int __memprof_profile_dump(void);
60+
5661
#ifdef __cplusplus
5762
} // extern "C"
5863
#endif

compiler-rt/lib/memprof/memprof_allocator.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -896,3 +896,10 @@ int __sanitizer_get_ownership(const void *p) {
896896
uptr __sanitizer_get_allocated_size(const void *p) {
897897
return memprof_malloc_usable_size(p, 0, 0);
898898
}
899+
900+
int __memprof_profile_dump() {
901+
instance.FinishAndPrint();
902+
// In the future we may want to return non-zero if there are any errors
903+
// detected during the dumping process.
904+
return 0;
905+
}

compiler-rt/lib/memprof/memprof_interface_internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ extern uptr __memprof_shadow_memory_dynamic_address;
4848

4949
SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE extern char
5050
__memprof_profile_filename[1];
51+
SANITIZER_INTERFACE_ATTRIBUTE int __memprof_profile_dump();
5152

5253
SANITIZER_INTERFACE_ATTRIBUTE void __memprof_load(uptr p);
5354
SANITIZER_INTERFACE_ATTRIBUTE void __memprof_store(uptr p);

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
// RUN: %clangxx_memprof %s -o %t -DPROFILE_NAME_VAR="/dev/null/INVALID"
2828
// RUN: not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-INVALID --dump-input=always
2929

30+
#include <sanitizer/memprof_interface.h>
31+
3032
#ifdef PROFILE_NAME_VAR
3133
#define xstr(s) str(s)
3234
#define str(s) #s
@@ -39,6 +41,7 @@ int main(int argc, char **argv) {
3941
char *x = (char *)malloc(10);
4042
memset(x, 0, 10);
4143
free(x);
44+
__memprof_profile_dump();
4245
return 0;
4346
}
4447
// CHECK-GOOD: Memory allocation stack id
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// RUN: %clangxx_memprof %s -o %t
2+
3+
// RUN: %env_memprof_opts=log_path=stdout %run %t | FileCheck %s
4+
5+
#include <sanitizer/memprof_interface.h>
6+
#include <stdlib.h>
7+
#include <string.h>
8+
int main(int argc, char **argv) {
9+
char *x = (char *)malloc(10);
10+
memset(x, 0, 10);
11+
free(x);
12+
__memprof_profile_dump();
13+
x = (char *)malloc(10);
14+
memset(x, 0, 10);
15+
free(x);
16+
return 0;
17+
}
18+
// We should get 2 rounds of profile info, one from the explicit dump request,
19+
// and one at exit.
20+
// CHECK: Memory allocation stack id
21+
// CHECK: Stack for id
22+
// CHECK: Memory allocation stack id
23+
// CHECK: Stack for id

0 commit comments

Comments
 (0)