From 4f6e4ae44a6ecaae12081dc611a5ab8093ee3f1a Mon Sep 17 00:00:00 2001 From: Kostya Serebryany Date: Thu, 10 Jan 2013 12:34:12 +0000 Subject: [PATCH] [sanitizer] add standalone_malloc_test llvm-svn: 172061 --- .../tests/standalone_malloc_test.cc | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 compiler-rt/lib/sanitizer_common/tests/standalone_malloc_test.cc diff --git a/compiler-rt/lib/sanitizer_common/tests/standalone_malloc_test.cc b/compiler-rt/lib/sanitizer_common/tests/standalone_malloc_test.cc new file mode 100644 index 000000000000..c65a6139a782 --- /dev/null +++ b/compiler-rt/lib/sanitizer_common/tests/standalone_malloc_test.cc @@ -0,0 +1,59 @@ +#include +#include +#include +#include +#include + +using namespace std; + +const size_t kNumThreds = 16; +const size_t kNumIters = 1 << 20; + + +void *MallocThread(void *t) { + size_t total_malloced = 0, total_freed = 0; + size_t max_in_use = 0; + size_t tid = reinterpret_cast(t); + vector > allocated; + allocated.reserve(kNumIters); + for (size_t i = 1; i < kNumIters; i++) { + if ((i % (kNumIters / 2)) == 0 && tid == 0) + fprintf(stderr, " T[%ld] iter %ld\n", tid, i); + if ((i % 5) <= 2) { // 0, 1, 2 + size_t size = 1 + (i % 200); + if ((i % 10001) == 0) + size *= 4096; + total_malloced += size; + char *x = new char[size]; + x[0] = x[size - 1] = x[size / 2] = 0; + allocated.push_back(make_pair(x, size)); + max_in_use = max(max_in_use, total_malloced - total_freed); + } else { // 3, 4 + if (allocated.empty()) continue; + size_t slot = i % allocated.size(); + char *p = allocated[slot].first; + size_t size = allocated[slot].second; + total_freed += size; + swap(allocated[slot], allocated.back()); + allocated.pop_back(); + delete [] p; + } + } + if (tid == 0) + fprintf(stderr, " T[%ld] total_malloced: %ldM in use %ldM max %ldM\n", + tid, total_malloced >> 20, (total_malloced - total_freed) >> 20, + max_in_use >> 20); + for (size_t i = 0; i < allocated.size(); i++) + delete [] allocated[i].first; + return 0; +} + + +int main() { + pthread_t t[kNumThreds]; + for (size_t i = 0; i < kNumThreds; i++) + pthread_create(&t[i], 0, MallocThread, reinterpret_cast(i)); + for (size_t i = 0; i < kNumThreds; i++) + pthread_join(t[i], 0); + malloc_stats(); +}