Chromium Code Reviews| Index: tools/android/heap_profiler/heap_profiler_integrationtest.cc |
| diff --git a/tools/android/heap_profiler/heap_profiler_integrationtest.cc b/tools/android/heap_profiler/heap_profiler_integrationtest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..006c461bd45993906d7c01ae0510cdda0d9d47a3 |
| --- /dev/null |
| +++ b/tools/android/heap_profiler/heap_profiler_integrationtest.cc |
| @@ -0,0 +1,194 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include <dlfcn.h> |
| +#include <fcntl.h> |
| +#include <stdlib.h> |
| +#include <string.h> |
| +#include <sys/mman.h> |
| +#include <unistd.h> |
| +#include <map> |
| + |
| +#include "base/compiler_specific.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| +#include "tools/android/heap_profiler/heap_profiler.h" |
| + |
| +namespace { |
| + |
| +// The purpose of the four functions below is to create watermarked allocations, |
| +// so the test fixture can ascertain that the hooks work end-to-end. |
| +__attribute__((noinline)) void* MallocInner(size_t size) { |
| + void* ptr = malloc(size); |
| + // The memset below is to avoid tail-call elimination optimizations and ensure |
| + // that this function will be part of the stack trace. |
| + memset(ptr, 0, size); |
| + return ptr; |
| +} |
| + |
| +__attribute__((noinline)) void* MallocOuter(size_t size) { |
| + void* ptr = MallocInner(size); |
| + memset(ptr, 0, size); |
| + return ptr; |
| +} |
| + |
| +__attribute__((noinline)) void* DoMmap(size_t size) { |
| + return mmap( |
| + 0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); |
| +} |
| + |
| +__attribute__((noinline)) void* MmapInner(size_t size) { |
| + void* ptr = DoMmap(size); |
| + memset(ptr, 0, size); |
| + return ptr; |
| +} |
| + |
| +__attribute__((noinline)) void* MmapOuter(size_t size) { |
| + void* ptr = MmapInner(size); |
| + memset(ptr, 0, size); |
| + return ptr; |
| +} |
| + |
| +class HeapProfilerIntegrationTest : public testing::Test { |
| + public: |
| + virtual void SetUp() OVERRIDE { |
| + HeapStats* const* stats_ptr = reinterpret_cast<HeapStats* const*>( |
| + dlsym(RTLD_DEFAULT, "heap_profiler_stats_for_tests")); |
| + EXPECT_TRUE(stats_ptr != NULL); |
| + stats_ = *stats_ptr; |
| + EXPECT_TRUE(stats_ != NULL); |
| + EXPECT_EQ(HEAP_PROFILER_MAGIC_MARKER, stats_->magic_start); |
| + } |
| + |
| + protected: |
| + void Initialize(void* (*outer_fn)(size_t), void* (*inner_fn)(size_t)) { |
|
bulach
2014/06/26 12:49:38
nit: could do with a typedef void* (*AllocatorFn)(
Primiano Tucci (use gerrit)
2014/06/26 13:38:17
Done.
|
| + outer_fn_ = reinterpret_cast<uintptr_t>(outer_fn); |
| + inner_fn_ = reinterpret_cast<uintptr_t>(inner_fn); |
| + } |
| + |
| + void TestAfterAllAllocatedAnd3Freed() { |
| + const StacktraceEntry* st1 = LookupStackTrace(kSize1, inner_fn_); |
| + const StacktraceEntry* st2 = LookupStackTrace(kSize2, inner_fn_); |
| + const StacktraceEntry* st3 = LookupStackTrace(kSize3, inner_fn_); |
| + |
| + EXPECT_TRUE(st1 != NULL); |
| + EXPECT_TRUE(StackTraceContains(st1, outer_fn_)); |
| + EXPECT_TRUE(StackTraceContains(st1, inner_fn_)); |
| + |
| + EXPECT_TRUE(st2 != NULL); |
| + EXPECT_FALSE(StackTraceContains(st2, outer_fn_)); |
| + EXPECT_TRUE(StackTraceContains(st2, inner_fn_)); |
| + |
| + EXPECT_EQ(NULL, st3); |
| + |
| + total_alloc_start_ = stats_->total_alloc_bytes; |
| + num_stack_traces_start_ = stats_->num_stack_traces; |
| + } |
| + |
| + void TestAfterAllFreed() { |
| + const size_t total_alloc_end = stats_->total_alloc_bytes; |
| + const size_t num_stack_traces_end = stats_->num_stack_traces; |
| + |
| + EXPECT_EQ(kSize1 + kSize2, total_alloc_start_ - total_alloc_end); |
| + EXPECT_EQ(2, num_stack_traces_start_ - num_stack_traces_end); |
| + EXPECT_EQ(NULL, LookupStackTrace(kSize1, inner_fn_)); |
| + EXPECT_EQ(NULL, LookupStackTrace(kSize2, inner_fn_)); |
| + EXPECT_EQ(NULL, LookupStackTrace(kSize3, inner_fn_)); |
| + } |
| + |
| + static const size_t kSize1 = 499 * PAGE_SIZE; |
| + static const size_t kSize2 = 503 * PAGE_SIZE; |
| + static const size_t kSize3 = 509 * PAGE_SIZE; |
| + |
| + private: |
| + static bool StackTraceContains(const StacktraceEntry* s, uintptr_t fn_addr) { |
| + // kExpectedFnLen is a gross estimation of the watermark functions' size. |
| + // It tries to address the following problem: the addrs in the unwound stack |
| + // stack frames will NOT point to the beginning of the functions, but to the |
| + // PC after the call to malloc/mmap. |
| + const size_t kExpectedFnLen = 16; |
| + |
| + for (size_t i = 0; i < HEAP_PROFILER_MAX_DEPTH; ++i) { |
| + if (s->frames[i] >= fn_addr && s->frames[i] <= fn_addr + kExpectedFnLen) |
| + return true; |
| + } |
| + return false; |
| + } |
| + |
| + const StacktraceEntry* LookupStackTrace(size_t size, uintptr_t fn_addr) { |
| + for (size_t i = 0; i < stats_->max_stack_traces; ++i) { |
| + const StacktraceEntry* st = &stats_->stack_traces[i]; |
| + if (st->alloc_bytes == size && StackTraceContains(st, fn_addr)) |
| + return st; |
| + } |
| + return NULL; |
| + } |
| + |
| + const HeapStats* stats_; |
| + size_t total_alloc_start_; |
| + size_t num_stack_traces_start_; |
| + uintptr_t outer_fn_; |
| + uintptr_t inner_fn_; |
| +}; |
| + |
| +TEST_F(HeapProfilerIntegrationTest, TestMallocStackTraces) { |
| + Initialize(&MallocOuter, &MallocInner); |
| + |
| + void* m1 = MallocOuter(kSize1); |
| + void* m2 = MallocInner(kSize2); |
| + void* m3 = MallocInner(kSize3); |
|
bulach
2014/06/26 12:49:38
nit: could move these three and 151-153 to Initial
Primiano Tucci (use gerrit)
2014/06/26 13:38:17
Hmm I tried but it looks odd. I can move the alloc
|
| + free(m3); |
| + TestAfterAllAllocatedAnd3Freed(); |
| + |
| + free(m1); |
| + free(m2); |
| + TestAfterAllFreed(); |
| +} |
| + |
| +TEST_F(HeapProfilerIntegrationTest, TestMmapStackTraces) { |
| + Initialize(&MmapOuter, &MmapInner); |
| + |
| + void* m1 = MmapOuter(kSize1); |
| + void* m2 = MmapInner(kSize2); |
| + void* m3 = MmapInner(kSize3); |
| + munmap(m3, kSize3); |
| + TestAfterAllAllocatedAnd3Freed(); |
| + |
| + munmap(m1, kSize1); |
| + munmap(m2, kSize2); |
| + TestAfterAllFreed(); |
| +} |
| + |
| +// Returns the path of the directory containing the current executable. |
| +std::string GetExePath() { |
| + char buf[1024]; |
| + ssize_t len = readlink("/proc/self/exe", buf, sizeof(buf) - 1); |
| + if (len == -1) |
| + return std::string(); |
| + std::string path(buf, len); |
| + size_t sep = path.find_last_of('/'); |
| + if (sep == std::string::npos) |
| + return std::string(); |
| + path.erase(sep); |
| + return path; |
| +} |
| + |
| +} // namespace |
| + |
| +int main(int argc, char** argv) { |
| + // Re-launch the process itself forcing the preload of the libheap_profiler. |
| + if (getenv("LD_PRELOAD") == NULL) { |
| + char env_ld_lib_path[256]; |
| + strlcpy(env_ld_lib_path, "LD_LIBRARY_PATH=", sizeof(env_ld_lib_path)); |
| + strlcat(env_ld_lib_path, GetExePath().c_str(), sizeof(env_ld_lib_path)); |
| + char env_ld_preload[] = "LD_PRELOAD=libheap_profiler.so"; |
| + char* const env[] = {env_ld_preload, env_ld_lib_path, 0}; |
| + execve("/proc/self/exe", argv, env); |
| + // execve() never returns, unless something goes wrong. |
| + perror("execve"); |
| + assert(false); |
| + } |
| + |
| + testing::InitGoogleTest(&argc, argv); |
| + return RUN_ALL_TESTS(); |
| +} |