[compiler-rt] r281546 - [asan] add heap_profile=1 to asan to periodically print the heap profile. So far this is a very basic heap-profile functionality

Ahmed Bougacha via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 14 18:16:27 PDT 2016


I hit the same issue on darwin;  I went with a much cruder fix (define
the symbol in sanitizer_mac.cc) in r281567.

Looking around, weak symbols (e.g., _malloc_hook) with a default
definition guarded by !SANITIZER_SUPPORTS_WEAK_HOOKS (which is more
than just SANITIZER_MAC). Maybe this symbol should have the same?

-Ahmed


On Wed, Sep 14, 2016 at 4:19 PM, Kostya Serebryany via llvm-commits
<llvm-commits at lists.llvm.org> wrote:
>
>
> On Wed, Sep 14, 2016 at 4:10 PM, Reid Kleckner <rnk at google.com> wrote:
>>
>> Windows doesn't actually support weak symbols that resolve to 0 if they
>> aren't present. It supports symbols that resolve to some default real symbol
>> if no other definition is found. See lib/asan/asan_win.cc for how we deal
>> with the existing uses of weak symbols. This means I can't fix this in a way
>> that is semantically equivalent.
>>
>> I'll do the quick fix for now, but you should think about other ways that
>> users can provide optional sanitizer hooks that are less platform dependent.
>
>
> Thanks for the quick fix!
>
> --kcc
>
>>
>>
>> On Wed, Sep 14, 2016 at 3:00 PM, Kostya Serebryany via llvm-commits
>> <llvm-commits at lists.llvm.org> wrote:
>>>
>>> Author: kcc
>>> Date: Wed Sep 14 17:00:58 2016
>>> New Revision: 281546
>>>
>>> URL: http://llvm.org/viewvc/llvm-project?rev=281546&view=rev
>>> Log:
>>> [asan] add heap_profile=1 to asan to periodically print the heap profile.
>>> So far this is a very basic heap-profile functionality
>>>
>>> Added:
>>>
>>> compiler-rt/trunk/test/asan/TestCases/Linux/auto_memory_profile_test.cc
>>> Modified:
>>>
>>> compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator_interface.h
>>>     compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_libcdep.cc
>>>     compiler-rt/trunk/lib/sanitizer_common/sanitizer_flags.inc
>>>
>>> Modified:
>>> compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator_interface.h
>>> URL:
>>> http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator_interface.h?rev=281546&r1=281545&r2=281546&view=diff
>>>
>>> ==============================================================================
>>> ---
>>> compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator_interface.h
>>> (original)
>>> +++
>>> compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator_interface.h Wed
>>> Sep 14 17:00:58 2016
>>> @@ -37,6 +37,10 @@ SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_
>>>      /* OPTIONAL */ void __sanitizer_malloc_hook(void *ptr, uptr size);
>>>  SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
>>>      /* OPTIONAL */ void __sanitizer_free_hook(void *ptr);
>>> +
>>> +
>>> +SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
>>> +    void __sanitizer_print_memory_profile(int top_percent);
>>>  }  // extern "C"
>>>
>>>  #endif  // SANITIZER_ALLOCATOR_INTERFACE_H
>>>
>>> Modified:
>>> compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_libcdep.cc
>>> URL:
>>> http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_libcdep.cc?rev=281546&r1=281545&r2=281546&view=diff
>>>
>>> ==============================================================================
>>> --- compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_libcdep.cc
>>> (original)
>>> +++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_libcdep.cc
>>> Wed Sep 14 17:00:58 2016
>>> @@ -13,6 +13,7 @@
>>>
>>>  #include "sanitizer_common.h"
>>>
>>> +#include "sanitizer_allocator_interface.h"
>>>  #include "sanitizer_flags.h"
>>>  #include "sanitizer_stackdepot.h"
>>>  #include "sanitizer_stacktrace.h"
>>> @@ -78,10 +79,12 @@ void SetAllocatorReleaseToOSCallback(All
>>>  void BackgroundThread(void *arg) {
>>>    uptr hard_rss_limit_mb = common_flags()->hard_rss_limit_mb;
>>>    uptr soft_rss_limit_mb = common_flags()->soft_rss_limit_mb;
>>> +  bool heap_profile = common_flags()->heap_profile;
>>>    bool allocator_release_to_os =
>>> common_flags()->allocator_release_to_os;
>>>    uptr prev_reported_rss = 0;
>>>    uptr prev_reported_stack_depot_size = 0;
>>>    bool reached_soft_rss_limit = false;
>>> +  uptr rss_during_last_reported_profile = 0;
>>>    while (true) {
>>>      SleepForMillis(100);
>>>      uptr current_rss_mb = GetRSS() >> 20;
>>> @@ -124,6 +127,12 @@ void BackgroundThread(void *arg) {
>>>        }
>>>      }
>>>      if (allocator_release_to_os && ReleseCallback) ReleseCallback();
>>> +    if (heap_profile &&
>>> +        current_rss_mb > rss_during_last_reported_profile * 1.1) {
>>> +      Printf("\n\nHEAP PROFILE at RSS %zdMb\n", current_rss_mb);
>>> +      __sanitizer_print_memory_profile(90);
>>> +      rss_during_last_reported_profile = current_rss_mb;
>>> +    }
>>>    }
>>>  }
>>>
>>> @@ -151,7 +160,8 @@ void MaybeStartBackgroudThread() {
>>>    // Start the background thread if one of the rss limits is given.
>>>    if (!common_flags()->hard_rss_limit_mb &&
>>>        !common_flags()->soft_rss_limit_mb &&
>>> -      !common_flags()->allocator_release_to_os) return;
>>> +      !common_flags()->allocator_release_to_os &&
>>> +      !common_flags()->heap_profile) return;
>>>    if (!&real_pthread_create) return;  // Can't spawn the thread anyway.
>>>    internal_start_thread(BackgroundThread, nullptr);
>>>  #endif
>>>
>>> Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_flags.inc
>>> URL:
>>> http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_flags.inc?rev=281546&r1=281545&r2=281546&view=diff
>>>
>>> ==============================================================================
>>> --- compiler-rt/trunk/lib/sanitizer_common/sanitizer_flags.inc (original)
>>> +++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_flags.inc Wed Sep 14
>>> 17:00:58 2016
>>> @@ -118,6 +118,7 @@ COMMON_FLAG(uptr, soft_rss_limit_mb, 0,
>>>              " until the RSS goes below the soft limit."
>>>              " This limit does not affect memory allocations other than"
>>>              " malloc/new.")
>>> +COMMON_FLAG(bool, heap_profile, false, "Experimental heap profiler,
>>> asan-only")
>>>  COMMON_FLAG(bool, allocator_release_to_os, false,
>>>              "Experimental. If true, try to periodically release unused"
>>>              " memory to the OS.\n")
>>>
>>> Added:
>>> compiler-rt/trunk/test/asan/TestCases/Linux/auto_memory_profile_test.cc
>>> URL:
>>> http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/asan/TestCases/Linux/auto_memory_profile_test.cc?rev=281546&view=auto
>>>
>>> ==============================================================================
>>> ---
>>> compiler-rt/trunk/test/asan/TestCases/Linux/auto_memory_profile_test.cc
>>> (added)
>>> +++
>>> compiler-rt/trunk/test/asan/TestCases/Linux/auto_memory_profile_test.cc Wed
>>> Sep 14 17:00:58 2016
>>> @@ -0,0 +1,32 @@
>>> +// Tests heap_profile=1.
>>> +// Printing memory profiling only works in the configuration where we
>>> can
>>> +// detect leaks.
>>> +// REQUIRES: leak-detection
>>> +//
>>> +// RUN: %clangxx_asan %s -o %t
>>> +// RUN: %env_asan_opts=heap_profile=1 %run %t 2>&1 | FileCheck %s
>>> +#include <sanitizer/common_interface_defs.h>
>>> +
>>> +#include <stdio.h>
>>> +#include <string.h>
>>> +#include <unistd.h>
>>> +
>>> +char *sink[1000];
>>> +
>>> +int main() {
>>> +
>>> +  for (int i = 0; i < 3; i++) {
>>> +    const size_t  kSize = 13000000;
>>> +    char *x = new char[kSize];
>>> +    memset(x, 0, kSize);
>>> +    sink[i] = x;
>>> +    sleep(1);
>>> +  }
>>> +}
>>> +
>>> +// CHECK: HEAP PROFILE at RSS
>>> +// CHECK: 13000000 byte(s)
>>> +// CHECK: HEAP PROFILE at RSS
>>> +// CHECK: 26000000 byte(s)
>>> +// CHECK: HEAP PROFILE at RSS
>>> +// CHECK: 39000000 byte(s)
>>>
>>>
>>> _______________________________________________
>>> llvm-commits mailing list
>>> llvm-commits at lists.llvm.org
>>> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits
>>
>>
>
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits
>


More information about the llvm-commits mailing list