[compiler-rt] a75b2e8 - [MemProf] Add interface to dump profile
Teresa Johnson via llvm-commits
llvm-commits at lists.llvm.org
Thu Nov 19 10:22:08 PST 2020
Author: Teresa Johnson
Date: 2020-11-19T10:21:53-08:00
New Revision: a75b2e87e6cbfabdfdd0d72c6962acf9d7756dd7
URL: https://github.com/llvm/llvm-project/commit/a75b2e87e6cbfabdfdd0d72c6962acf9d7756dd7
DIFF: https://github.com/llvm/llvm-project/commit/a75b2e87e6cbfabdfdd0d72c6962acf9d7756dd7.diff
LOG: [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
Added:
compiler-rt/test/memprof/TestCases/memprof_profile_dump.cpp
Modified:
compiler-rt/include/sanitizer/memprof_interface.h
compiler-rt/lib/memprof/memprof_allocator.cpp
compiler-rt/lib/memprof/memprof_interface_internal.h
compiler-rt/test/memprof/TestCases/log_path_test.cpp
Removed:
################################################################################
diff --git a/compiler-rt/include/sanitizer/memprof_interface.h b/compiler-rt/include/sanitizer/memprof_interface.h
index a72126051003..76031de4014c 100644
--- a/compiler-rt/include/sanitizer/memprof_interface.h
+++ b/compiler-rt/include/sanitizer/memprof_interface.h
@@ -53,6 +53,11 @@ void __memprof_print_accumulated_stats(void);
/// \returns Default options string.
const char *__memprof_default_options(void);
+/// Prints the memory profile to the current profile file.
+///
+/// \returns 0 on success.
+int __memprof_profile_dump(void);
+
#ifdef __cplusplus
} // extern "C"
#endif
diff --git a/compiler-rt/lib/memprof/memprof_allocator.cpp b/compiler-rt/lib/memprof/memprof_allocator.cpp
index c6a68118f7de..0f9263e66fe0 100644
--- a/compiler-rt/lib/memprof/memprof_allocator.cpp
+++ b/compiler-rt/lib/memprof/memprof_allocator.cpp
@@ -896,3 +896,10 @@ int __sanitizer_get_ownership(const void *p) {
uptr __sanitizer_get_allocated_size(const void *p) {
return memprof_malloc_usable_size(p, 0, 0);
}
+
+int __memprof_profile_dump() {
+ instance.FinishAndPrint();
+ // In the future we may want to return non-zero if there are any errors
+ // detected during the dumping process.
+ return 0;
+}
diff --git a/compiler-rt/lib/memprof/memprof_interface_internal.h b/compiler-rt/lib/memprof/memprof_interface_internal.h
index a51d83a2521c..0aca4afc9afa 100644
--- a/compiler-rt/lib/memprof/memprof_interface_internal.h
+++ b/compiler-rt/lib/memprof/memprof_interface_internal.h
@@ -48,6 +48,7 @@ extern uptr __memprof_shadow_memory_dynamic_address;
SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE extern char
__memprof_profile_filename[1];
+SANITIZER_INTERFACE_ATTRIBUTE int __memprof_profile_dump();
SANITIZER_INTERFACE_ATTRIBUTE void __memprof_load(uptr p);
SANITIZER_INTERFACE_ATTRIBUTE void __memprof_store(uptr p);
diff --git a/compiler-rt/test/memprof/TestCases/log_path_test.cpp b/compiler-rt/test/memprof/TestCases/log_path_test.cpp
index 44fa3781e879..8af937389833 100644
--- a/compiler-rt/test/memprof/TestCases/log_path_test.cpp
+++ b/compiler-rt/test/memprof/TestCases/log_path_test.cpp
@@ -27,6 +27,8 @@
// RUN: %clangxx_memprof %s -o %t -DPROFILE_NAME_VAR="/dev/null/INVALID"
// RUN: not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-INVALID --dump-input=always
+#include <sanitizer/memprof_interface.h>
+
#ifdef PROFILE_NAME_VAR
#define xstr(s) str(s)
#define str(s) #s
@@ -39,6 +41,7 @@ int main(int argc, char **argv) {
char *x = (char *)malloc(10);
memset(x, 0, 10);
free(x);
+ __memprof_profile_dump();
return 0;
}
// CHECK-GOOD: Memory allocation stack id
diff --git a/compiler-rt/test/memprof/TestCases/memprof_profile_dump.cpp b/compiler-rt/test/memprof/TestCases/memprof_profile_dump.cpp
new file mode 100644
index 000000000000..fca1a8af6bf4
--- /dev/null
+++ b/compiler-rt/test/memprof/TestCases/memprof_profile_dump.cpp
@@ -0,0 +1,23 @@
+// RUN: %clangxx_memprof %s -o %t
+
+// RUN: %env_memprof_opts=log_path=stdout %run %t | FileCheck %s
+
+#include <sanitizer/memprof_interface.h>
+#include <stdlib.h>
+#include <string.h>
+int main(int argc, char **argv) {
+ char *x = (char *)malloc(10);
+ memset(x, 0, 10);
+ free(x);
+ __memprof_profile_dump();
+ x = (char *)malloc(10);
+ memset(x, 0, 10);
+ free(x);
+ return 0;
+}
+// We should get 2 rounds of profile info, one from the explicit dump request,
+// and one at exit.
+// CHECK: Memory allocation stack id
+// CHECK: Stack for id
+// CHECK: Memory allocation stack id
+// CHECK: Stack for id
More information about the llvm-commits
mailing list