[PATCH] D91768: [MemProf] Add interface to dump profile

Teresa Johnson via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 18 23:15:40 PST 2020


tejohnson created this revision.
tejohnson added a reviewer: vitalybuka.
Herald added a project: Sanitizers.
Herald added a subscriber: Sanitizers.
tejohnson requested review of this revision.

Add an interface so that the profile can be dumped on demand.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D91768

Files:
  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
  compiler-rt/test/memprof/TestCases/memprof_profile_dump.cpp


Index: compiler-rt/test/memprof/TestCases/memprof_profile_dump.cpp
===================================================================
--- /dev/null
+++ 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
Index: compiler-rt/test/memprof/TestCases/log_path_test.cpp
===================================================================
--- compiler-rt/test/memprof/TestCases/log_path_test.cpp
+++ 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 @@
   char *x = (char *)malloc(10);
   memset(x, 0, 10);
   free(x);
+  __memprof_profile_dump();
   return 0;
 }
 // CHECK-GOOD: Memory allocation stack id
Index: compiler-rt/lib/memprof/memprof_interface_internal.h
===================================================================
--- compiler-rt/lib/memprof/memprof_interface_internal.h
+++ compiler-rt/lib/memprof/memprof_interface_internal.h
@@ -48,6 +48,7 @@
 
 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);
Index: compiler-rt/lib/memprof/memprof_allocator.cpp
===================================================================
--- compiler-rt/lib/memprof/memprof_allocator.cpp
+++ compiler-rt/lib/memprof/memprof_allocator.cpp
@@ -896,3 +896,10 @@
 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;
+}
Index: compiler-rt/include/sanitizer/memprof_interface.h
===================================================================
--- compiler-rt/include/sanitizer/memprof_interface.h
+++ compiler-rt/include/sanitizer/memprof_interface.h
@@ -53,6 +53,11 @@
 /// \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


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D91768.306319.patch
Type: text/x-patch
Size: 3169 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20201119/645bc975/attachment.bin>


More information about the llvm-commits mailing list