Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1228)

Side by Side Diff: tools/android/heap_profiler/heap_profiler_integrationtest.cc

Issue 323893002: [Android] Introduce libheap_profiler for memory profiling. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix gyp Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <dlfcn.h>
6 #include <fcntl.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <sys/mman.h>
10 #include <unistd.h>
11 #include <map>
12
13 #include "base/compiler_specific.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "tools/android/heap_profiler/heap_profiler.h"
16
17 namespace {
18
19 // The purpose of the four functions below is to create watermarked allocations,
20 // so the test fixture can ascertain that the hooks work end-to-end.
21 __attribute__((noinline)) void* MallocInner(size_t size) {
22 void* ptr = malloc(size);
23 // The memset below is to avoid tail-call elimination optimizations and ensure
24 // that this function will be part of the stack trace.
25 memset(ptr, 0, size);
26 return ptr;
27 }
28
29 __attribute__((noinline)) void* MallocOuter(size_t size) {
30 void* ptr = MallocInner(size);
31 memset(ptr, 0, size);
32 return ptr;
33 }
34
35 __attribute__((noinline)) void* DoMmap(size_t size) {
36 return mmap(
37 0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
38 }
39
40 __attribute__((noinline)) void* MmapInner(size_t size) {
41 void* ptr = DoMmap(size);
42 memset(ptr, 0, size);
43 return ptr;
44 }
45
46 __attribute__((noinline)) void* MmapOuter(size_t size) {
47 void* ptr = MmapInner(size);
48 memset(ptr, 0, size);
49 return ptr;
50 }
51
52 class HeapProfilerIntegrationTest : public testing::Test {
53 public:
54 virtual void SetUp() OVERRIDE {
55 HeapStats* const* stats_ptr = reinterpret_cast<HeapStats* const*>(
56 dlsym(RTLD_DEFAULT, "heap_profiler_stats_for_tests"));
57 EXPECT_TRUE(stats_ptr != NULL);
58 stats_ = *stats_ptr;
59 EXPECT_TRUE(stats_ != NULL);
60 EXPECT_EQ(HEAP_PROFILER_MAGIC_MARKER, stats_->magic_start);
61 }
62
63 protected:
64 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.
65 outer_fn_ = reinterpret_cast<uintptr_t>(outer_fn);
66 inner_fn_ = reinterpret_cast<uintptr_t>(inner_fn);
67 }
68
69 void TestAfterAllAllocatedAnd3Freed() {
70 const StacktraceEntry* st1 = LookupStackTrace(kSize1, inner_fn_);
71 const StacktraceEntry* st2 = LookupStackTrace(kSize2, inner_fn_);
72 const StacktraceEntry* st3 = LookupStackTrace(kSize3, inner_fn_);
73
74 EXPECT_TRUE(st1 != NULL);
75 EXPECT_TRUE(StackTraceContains(st1, outer_fn_));
76 EXPECT_TRUE(StackTraceContains(st1, inner_fn_));
77
78 EXPECT_TRUE(st2 != NULL);
79 EXPECT_FALSE(StackTraceContains(st2, outer_fn_));
80 EXPECT_TRUE(StackTraceContains(st2, inner_fn_));
81
82 EXPECT_EQ(NULL, st3);
83
84 total_alloc_start_ = stats_->total_alloc_bytes;
85 num_stack_traces_start_ = stats_->num_stack_traces;
86 }
87
88 void TestAfterAllFreed() {
89 const size_t total_alloc_end = stats_->total_alloc_bytes;
90 const size_t num_stack_traces_end = stats_->num_stack_traces;
91
92 EXPECT_EQ(kSize1 + kSize2, total_alloc_start_ - total_alloc_end);
93 EXPECT_EQ(2, num_stack_traces_start_ - num_stack_traces_end);
94 EXPECT_EQ(NULL, LookupStackTrace(kSize1, inner_fn_));
95 EXPECT_EQ(NULL, LookupStackTrace(kSize2, inner_fn_));
96 EXPECT_EQ(NULL, LookupStackTrace(kSize3, inner_fn_));
97 }
98
99 static const size_t kSize1 = 499 * PAGE_SIZE;
100 static const size_t kSize2 = 503 * PAGE_SIZE;
101 static const size_t kSize3 = 509 * PAGE_SIZE;
102
103 private:
104 static bool StackTraceContains(const StacktraceEntry* s, uintptr_t fn_addr) {
105 // kExpectedFnLen is a gross estimation of the watermark functions' size.
106 // It tries to address the following problem: the addrs in the unwound stack
107 // stack frames will NOT point to the beginning of the functions, but to the
108 // PC after the call to malloc/mmap.
109 const size_t kExpectedFnLen = 16;
110
111 for (size_t i = 0; i < HEAP_PROFILER_MAX_DEPTH; ++i) {
112 if (s->frames[i] >= fn_addr && s->frames[i] <= fn_addr + kExpectedFnLen)
113 return true;
114 }
115 return false;
116 }
117
118 const StacktraceEntry* LookupStackTrace(size_t size, uintptr_t fn_addr) {
119 for (size_t i = 0; i < stats_->max_stack_traces; ++i) {
120 const StacktraceEntry* st = &stats_->stack_traces[i];
121 if (st->alloc_bytes == size && StackTraceContains(st, fn_addr))
122 return st;
123 }
124 return NULL;
125 }
126
127 const HeapStats* stats_;
128 size_t total_alloc_start_;
129 size_t num_stack_traces_start_;
130 uintptr_t outer_fn_;
131 uintptr_t inner_fn_;
132 };
133
134 TEST_F(HeapProfilerIntegrationTest, TestMallocStackTraces) {
135 Initialize(&MallocOuter, &MallocInner);
136
137 void* m1 = MallocOuter(kSize1);
138 void* m2 = MallocInner(kSize2);
139 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
140 free(m3);
141 TestAfterAllAllocatedAnd3Freed();
142
143 free(m1);
144 free(m2);
145 TestAfterAllFreed();
146 }
147
148 TEST_F(HeapProfilerIntegrationTest, TestMmapStackTraces) {
149 Initialize(&MmapOuter, &MmapInner);
150
151 void* m1 = MmapOuter(kSize1);
152 void* m2 = MmapInner(kSize2);
153 void* m3 = MmapInner(kSize3);
154 munmap(m3, kSize3);
155 TestAfterAllAllocatedAnd3Freed();
156
157 munmap(m1, kSize1);
158 munmap(m2, kSize2);
159 TestAfterAllFreed();
160 }
161
162 // Returns the path of the directory containing the current executable.
163 std::string GetExePath() {
164 char buf[1024];
165 ssize_t len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
166 if (len == -1)
167 return std::string();
168 std::string path(buf, len);
169 size_t sep = path.find_last_of('/');
170 if (sep == std::string::npos)
171 return std::string();
172 path.erase(sep);
173 return path;
174 }
175
176 } // namespace
177
178 int main(int argc, char** argv) {
179 // Re-launch the process itself forcing the preload of the libheap_profiler.
180 if (getenv("LD_PRELOAD") == NULL) {
181 char env_ld_lib_path[256];
182 strlcpy(env_ld_lib_path, "LD_LIBRARY_PATH=", sizeof(env_ld_lib_path));
183 strlcat(env_ld_lib_path, GetExePath().c_str(), sizeof(env_ld_lib_path));
184 char env_ld_preload[] = "LD_PRELOAD=libheap_profiler.so";
185 char* const env[] = {env_ld_preload, env_ld_lib_path, 0};
186 execve("/proc/self/exe", argv, env);
187 // execve() never returns, unless something goes wrong.
188 perror("execve");
189 assert(false);
190 }
191
192 testing::InitGoogleTest(&argc, argv);
193 return RUN_ALL_TESTS();
194 }
OLDNEW
« no previous file with comments | « tools/android/heap_profiler/heap_profiler_hooks_android.c ('k') | tools/android/heap_profiler/heap_profiler_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698