[llvm-commits] [compiler-rt] r163716 - in /compiler-rt/trunk/lib/asan: asan_malloc_mac.cc asan_stats.h asan_thread_registry.cc asan_thread_registry.h tests/asan_test.cc
Alexander Potapenko
glider at google.com
Wed Sep 12 08:29:51 PDT 2012
Author: glider
Date: Wed Sep 12 10:29:50 2012
New Revision: 163716
URL: http://llvm.org/viewvc/llvm-project?rev=163716&view=rev
Log:
Give more accurate malloc statistics to malloc_zone_statistics().
Fix a warning in macros instantiation.
Modified:
compiler-rt/trunk/lib/asan/asan_malloc_mac.cc
compiler-rt/trunk/lib/asan/asan_stats.h
compiler-rt/trunk/lib/asan/asan_thread_registry.cc
compiler-rt/trunk/lib/asan/asan_thread_registry.h
compiler-rt/trunk/lib/asan/tests/asan_test.cc
Modified: compiler-rt/trunk/lib/asan/asan_malloc_mac.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_malloc_mac.cc?rev=163716&r1=163715&r2=163716&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_malloc_mac.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_malloc_mac.cc Wed Sep 12 10:29:50 2012
@@ -25,6 +25,8 @@
#include "asan_mac.h"
#include "asan_report.h"
#include "asan_stack.h"
+#include "asan_stats.h"
+#include "asan_thread_registry.h"
// Similar code is used in Google Perftools,
// http://code.google.com/p/google-perftools.
@@ -276,7 +278,6 @@
#endif
#endif
-// malloc_introspection callbacks. I'm not clear on what all of these do.
kern_return_t mi_enumerator(task_t task, void *,
unsigned type_mask, vm_address_t zone_address,
memory_reader_t reader,
@@ -313,11 +314,10 @@
}
void mi_statistics(malloc_zone_t *zone, malloc_statistics_t *stats) {
- // TODO(glider): fill these correctly.
- stats->blocks_in_use = 0;
- stats->size_in_use = 0;
- stats->max_size_in_use = 0;
- stats->size_allocated = 0;
+ AsanMallocStats malloc_stats;
+ asanThreadRegistry().FillMallocStatistics(&malloc_stats);
+ CHECK(sizeof(malloc_statistics_t) == sizeof(AsanMallocStats));
+ internal_memcpy(stats, &malloc_stats, sizeof(malloc_statistics_t));
}
#if defined(MAC_OS_X_VERSION_10_6) && \
Modified: compiler-rt/trunk/lib/asan/asan_stats.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_stats.h?rev=163716&r1=163715&r2=163716&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_stats.h (original)
+++ compiler-rt/trunk/lib/asan/asan_stats.h Wed Sep 12 10:29:50 2012
@@ -54,6 +54,14 @@
void Print();
};
+// A cross-platform equivalent of malloc_statistics_t on Mac OS.
+struct AsanMallocStats {
+ uptr blocks_in_use;
+ uptr size_in_use;
+ uptr max_size_in_use;
+ uptr size_allocated;
+};
+
} // namespace __asan
#endif // ASAN_STATS_H
Modified: compiler-rt/trunk/lib/asan/asan_thread_registry.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_thread_registry.cc?rev=163716&r1=163715&r2=163716&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_thread_registry.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_thread_registry.cc Wed Sep 12 10:29:50 2012
@@ -30,6 +30,7 @@
: main_thread_(x),
main_thread_summary_(x),
accumulated_stats_(x),
+ max_malloced_memory_(x),
mu_(x) { }
void AsanThreadRegistry::Init() {
@@ -131,6 +132,17 @@
+ accumulated_stats_.really_freed_redzones;
}
+// Return several stats counters with a single call to
+// UpdateAccumulatedStatsUnlocked().
+void AsanThreadRegistry::FillMallocStatistics(AsanMallocStats *malloc_stats) {
+ ScopedLock lock(&mu_);
+ UpdateAccumulatedStatsUnlocked();
+ malloc_stats->blocks_in_use = accumulated_stats_.mallocs;
+ malloc_stats->size_in_use = accumulated_stats_.malloced;
+ malloc_stats->max_size_in_use = max_malloced_memory_;
+ malloc_stats->size_allocated = accumulated_stats_.mmaped;
+}
+
AsanThreadSummary *AsanThreadRegistry::FindByTid(u32 tid) {
CHECK(tid < n_threads_);
CHECK(thread_summaries_[tid]);
@@ -156,6 +168,12 @@
FlushToAccumulatedStatsUnlocked(&t->stats());
}
}
+ // This is not very accurate: we may miss allocation peaks that happen
+ // between two updates of accumulated_stats_. For more accurate bookkeeping
+ // the maximum should be updated on every malloc(), which is unacceptable.
+ if (max_malloced_memory_ < accumulated_stats_.malloced) {
+ max_malloced_memory_ = accumulated_stats_.malloced;
+ }
}
void AsanThreadRegistry::FlushToAccumulatedStatsUnlocked(AsanStats *stats) {
Modified: compiler-rt/trunk/lib/asan/asan_thread_registry.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_thread_registry.h?rev=163716&r1=163715&r2=163716&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_thread_registry.h (original)
+++ compiler-rt/trunk/lib/asan/asan_thread_registry.h Wed Sep 12 10:29:50 2012
@@ -53,6 +53,7 @@
uptr GetCurrentAllocatedBytes();
uptr GetHeapSize();
uptr GetFreeBytes();
+ void FillMallocStatistics(AsanMallocStats *malloc_stats);
AsanThreadSummary *FindByTid(u32 tid);
AsanThread *FindThreadByStackAddress(uptr addr);
@@ -68,6 +69,9 @@
AsanThread main_thread_;
AsanThreadSummary main_thread_summary_;
AsanStats accumulated_stats_;
+ // Required for malloc_zone_statistics() on OS X. This can't be stored in
+ // per-thread AsanStats.
+ uptr max_malloced_memory_;
u32 n_threads_;
AsanLock mu_;
bool inited_;
Modified: compiler-rt/trunk/lib/asan/tests/asan_test.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/tests/asan_test.cc?rev=163716&r1=163715&r2=163716&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/tests/asan_test.cc (original)
+++ compiler-rt/trunk/lib/asan/tests/asan_test.cc Wed Sep 12 10:29:50 2012
@@ -30,6 +30,7 @@
#ifndef __APPLE__
#include <malloc.h>
#else
+#include <malloc/malloc.h>
#include <AvailabilityMacros.h> // For MAC_OS_X_VERSION_*
#include <CoreFoundation/CFString.h>
#endif // __APPLE__
@@ -2093,6 +2094,19 @@
TEST(AddressSanitizerMac, NSURLDeallocation) {
TestNSURLDeallocation();
}
+
+// See http://code.google.com/p/address-sanitizer/issues/detail?id=109.
+TEST(AddressSanitizerMac, Mstats) {
+ malloc_statistics_t stats1, stats2;
+ malloc_zone_statistics(/*all zones*/NULL, &stats1);
+ const int kMallocSize = 100000;
+ void *alloc = Ident(malloc(kMallocSize));
+ malloc_zone_statistics(/*all zones*/NULL, &stats2);
+ EXPECT_GT(stats2.blocks_in_use, stats1.blocks_in_use);
+ EXPECT_GE(stats2.size_in_use - stats1.size_in_use, kMallocSize);
+ free(alloc);
+ // Even the default OSX allocator may not change the stats after free().
+}
#endif // __APPLE__
// Test that instrumentation of stack allocations takes into account
More information about the llvm-commits
mailing list