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

Side by Side Diff: base/memory/discardable_memory_provider_unittest.cc

Issue 17106004: Add discardable memory emulation for non-android/mac platforms (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix MemoryAfterUnlock test Created 7 years, 1 month 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 2013 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 "base/memory/discardable_memory_provider.h"
6
7 #include "base/bind.h"
8 #include "base/memory/discardable_memory.h"
9 #include "base/run_loop.h"
10 #include "base/synchronization/waitable_event.h"
11 #include "base/threading/thread.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 using base::internal::DiscardableMemoryProvider;
15
16 namespace base {
17
18 class DiscardableMemoryProviderTestBase {
19 public:
20 DiscardableMemoryProviderTestBase()
21 : message_loop_(MessageLoop::TYPE_IO),
22 provider_(new DiscardableMemoryProvider) {
23 // We set a provider here for two reasons:
24 // 1. It ensures that one test cannot affect the next, and
25 // 2. Since the provider listens for pressure notifications on the thread
26 // it was created on, if we create it on the test thread, we can run
27 // the test thread's message loop until idle when we want to process
28 // one of these notifications.
29 DiscardableMemoryProvider::SetInstanceForTest(provider_.get());
30 }
31
32 virtual ~DiscardableMemoryProviderTestBase() {
33 DiscardableMemoryProvider::SetInstanceForTest(NULL);
34 }
35
36 protected:
37 bool IsRegistered(const DiscardableMemory* discardable) {
38 return DiscardableMemoryProvider::GetInstance()->IsRegisteredForTest(
39 discardable);
40 }
41
42 bool CanBePurged(const DiscardableMemory* discardable) {
43 return DiscardableMemoryProvider::GetInstance()->CanBePurgedForTest(
44 discardable);
45 }
46
47 size_t BytesAllocated() const {
48 return DiscardableMemoryProvider::GetInstance()->
49 GetBytesAllocatedForTest();
50 }
51
52 void* Memory(const DiscardableMemory* discardable) const {
53 return discardable->Memory();
54 }
55
56 void SetDiscardableMemoryLimit(size_t bytes) {
57 DiscardableMemoryProvider::GetInstance()->
58 SetDiscardableMemoryLimit(bytes);
59 }
60
61 void SetBytesToReclaimUnderModeratePressure(size_t bytes) {
62 DiscardableMemoryProvider::GetInstance()->
63 SetBytesToReclaimUnderModeratePressure(bytes);
64 }
65
66 private:
67 MessageLoop message_loop_;
68 scoped_ptr<DiscardableMemoryProvider> provider_;
69 };
70
71 class DiscardableMemoryProviderTest
72 : public DiscardableMemoryProviderTestBase,
73 public testing::Test {
74 public:
75 DiscardableMemoryProviderTest() {}
76 };
77
78 TEST_F(DiscardableMemoryProviderTest, CreateLockedMemory) {
79 size_t size = 1024;
80 const scoped_ptr<DiscardableMemory> discardable(
81 DiscardableMemory::CreateLockedMemory(size));
82 EXPECT_TRUE(IsRegistered(discardable.get()));
83 EXPECT_NE(static_cast<void*>(NULL), Memory(discardable.get()));
84 EXPECT_EQ(1024u, BytesAllocated());
85 EXPECT_FALSE(CanBePurged(discardable.get()));
86 }
87
88 TEST_F(DiscardableMemoryProviderTest, CreateLockedMemoryZeroSize) {
89 size_t size = 0;
90 const scoped_ptr<DiscardableMemory> discardable(
91 DiscardableMemory::CreateLockedMemory(size));
92 EXPECT_FALSE(discardable);
93 EXPECT_FALSE(IsRegistered(discardable.get()));
94 EXPECT_EQ(0u, BytesAllocated());
95 }
96
97 TEST_F(DiscardableMemoryProviderTest, LockAfterUnlock) {
98 size_t size = 1024;
99 const scoped_ptr<DiscardableMemory> discardable(
100 DiscardableMemory::CreateLockedMemory(size));
101 EXPECT_TRUE(IsRegistered(discardable.get()));
102 EXPECT_NE(static_cast<void*>(NULL), Memory(discardable.get()));
103 EXPECT_EQ(1024u, BytesAllocated());
104 EXPECT_FALSE(CanBePurged(discardable.get()));
105
106 // Now unlock so we can lock later.
107 discardable->Unlock();
108 EXPECT_TRUE(CanBePurged(discardable.get()));
109
110 EXPECT_EQ(DISCARDABLE_MEMORY_SUCCESS, discardable->Lock());
111 EXPECT_FALSE(CanBePurged(discardable.get()));
112 }
113
114 TEST_F(DiscardableMemoryProviderTest, LockAfterPurge) {
115 size_t size = 1024;
116 const scoped_ptr<DiscardableMemory> discardable(
117 DiscardableMemory::CreateLockedMemory(size));
118 EXPECT_TRUE(IsRegistered(discardable.get()));
119 EXPECT_NE(static_cast<void*>(NULL), Memory(discardable.get()));
120 EXPECT_EQ(1024u, BytesAllocated());
121 EXPECT_FALSE(CanBePurged(discardable.get()));
122
123 // Now unlock so we can lock later.
124 discardable->Unlock();
125 EXPECT_TRUE(CanBePurged(discardable.get()));
126
127 // Force the system to purge.
128 MemoryPressureListener::NotifyMemoryPressure(
129 MemoryPressureListener::MEMORY_PRESSURE_CRITICAL);
130
131 // Required because ObserverListThreadSafe notifies via PostTask.
132 RunLoop().RunUntilIdle();
133
134 EXPECT_EQ(DISCARDABLE_MEMORY_PURGED, discardable->Lock());
135 EXPECT_FALSE(CanBePurged(discardable.get()));
136 }
137
138 TEST_F(DiscardableMemoryProviderTest, LockAfterPurgeAndCannotReallocate) {
139 size_t size = 1024;
140 const scoped_ptr<DiscardableMemory> discardable(
141 DiscardableMemory::CreateLockedMemory(size));
142 EXPECT_TRUE(IsRegistered(discardable.get()));
143 EXPECT_NE(static_cast<void*>(NULL), Memory(discardable.get()));
144 EXPECT_EQ(1024u, BytesAllocated());
145 EXPECT_FALSE(CanBePurged(discardable.get()));
146
147 // Now unlock so we can lock later.
148 discardable->Unlock();
149 EXPECT_TRUE(CanBePurged(discardable.get()));
150
151 // Set max allowed allocation to 1 byte. This will make cause the memory
152 // to be purged.
153 SetDiscardableMemoryLimit(1);
154
155 EXPECT_EQ(DISCARDABLE_MEMORY_PURGED, discardable->Lock());
156 EXPECT_FALSE(CanBePurged(discardable.get()));
157 }
158
159 class PermutationTestData {
160 public:
161 PermutationTestData(unsigned d0, unsigned d1, unsigned d2) {
162 ordering_[0] = d0;
163 ordering_[1] = d1;
164 ordering_[2] = d2;
165 }
166
167 const unsigned* ordering() const { return ordering_; }
168
169 private:
170 unsigned ordering_[3];
171 };
172
173 class DiscardableMemoryProviderPermutationTest
174 : public DiscardableMemoryProviderTestBase,
175 public testing::TestWithParam<PermutationTestData> {
176 public:
177 DiscardableMemoryProviderPermutationTest() {}
178
179 protected:
180 // Use discardable memory in order specified by ordering parameter.
181 void CreateAndUseDiscardableMemory() {
182 for (int i = 0; i < 3; ++i) {
183 discardables_[i] = DiscardableMemory::CreateLockedMemory(1024);
184 EXPECT_TRUE(discardables_[i]);
185 EXPECT_NE(static_cast<void*>(NULL), Memory(discardables_[i].get()));
186 discardables_[i]->Unlock();
187 }
188 for (int i = 0; i < 3; ++i) {
189 int index = GetParam().ordering()[i];
190 EXPECT_NE(DISCARDABLE_MEMORY_FAILED, discardables_[index]->Lock());
191 // Leave i == 0 locked.
192 if (i > 0)
193 discardables_[index]->Unlock();
194 }
195 }
196
197 DiscardableMemory* discardable(unsigned position) {
198 return discardables_[GetParam().ordering()[position]].get();
199 }
200
201 private:
202 scoped_ptr<DiscardableMemory> discardables_[3];
203 };
204
205 // Verify that memory was discarded in the correct order after applying
206 // memory pressure.
207 TEST_P(DiscardableMemoryProviderPermutationTest, LRUDiscardedModeratePressure) {
208 CreateAndUseDiscardableMemory();
209
210 SetBytesToReclaimUnderModeratePressure(1024);
211 MemoryPressureListener::NotifyMemoryPressure(
212 MemoryPressureListener::MEMORY_PRESSURE_MODERATE);
213 RunLoop().RunUntilIdle();
214
215 EXPECT_NE(DISCARDABLE_MEMORY_FAILED, discardable(2)->Lock());
216 EXPECT_NE(DISCARDABLE_MEMORY_SUCCESS, discardable(1)->Lock());
217 // 0 should still be locked.
218 EXPECT_NE(static_cast<void*>(NULL), Memory(discardable(0)));
219 }
220
221 // Verify that memory was discarded in the correct order after changing
222 // memory limit.
223 TEST_P(DiscardableMemoryProviderPermutationTest, LRUDiscardedExceedLimit) {
224 CreateAndUseDiscardableMemory();
225
226 SetBytesToReclaimUnderModeratePressure(1024);
227 SetDiscardableMemoryLimit(2048);
228
229 EXPECT_NE(DISCARDABLE_MEMORY_FAILED, discardable(2)->Lock());
230 EXPECT_NE(DISCARDABLE_MEMORY_SUCCESS, discardable(1)->Lock());
231 // 0 should still be locked.
232 EXPECT_NE(static_cast<void*>(NULL), Memory(discardable(0)));
233 }
234
235 TEST_P(DiscardableMemoryProviderPermutationTest,
236 CriticalPressureFreesAllUnlocked) {
237 CreateAndUseDiscardableMemory();
238
239 MemoryPressureListener::NotifyMemoryPressure(
240 MemoryPressureListener::MEMORY_PRESSURE_CRITICAL);
241 RunLoop().RunUntilIdle();
242
243 for (int i = 0; i < 3; ++i) {
244 if (i == 0)
245 EXPECT_NE(static_cast<void*>(NULL), Memory(discardable(i)));
246 else
247 EXPECT_EQ(DISCARDABLE_MEMORY_PURGED, discardable(i)->Lock());
248 }
249 }
250
251 INSTANTIATE_TEST_CASE_P(DiscardableMemoryProviderPermutationTests,
252 DiscardableMemoryProviderPermutationTest,
253 ::testing::Values(PermutationTestData(0, 1, 2),
254 PermutationTestData(0, 2, 1),
255 PermutationTestData(1, 0, 2),
256 PermutationTestData(1, 2, 0),
257 PermutationTestData(2, 0, 1),
258 PermutationTestData(2, 1, 0)));
259
260 TEST_F(DiscardableMemoryProviderTest, NormalDestruction) {
261 {
262 size_t size = 1024;
263 const scoped_ptr<DiscardableMemory> discardable(
264 DiscardableMemory::CreateLockedMemory(size));
265 EXPECT_TRUE(IsRegistered(discardable.get()));
266 EXPECT_EQ(1024u, BytesAllocated());
267 }
268 EXPECT_EQ(0u, BytesAllocated());
269 }
270
271 TEST_F(DiscardableMemoryProviderTest, DestructionWhileLocked) {
272 {
273 size_t size = 1024;
274 const scoped_ptr<DiscardableMemory> discardable(
275 DiscardableMemory::CreateLockedMemory(size));
276 EXPECT_TRUE(IsRegistered(discardable.get()));
277 EXPECT_NE(static_cast<void*>(NULL), Memory(discardable.get()));
278 EXPECT_EQ(1024u, BytesAllocated());
279 EXPECT_FALSE(CanBePurged(discardable.get()));
280 }
281 // Should have ignored the "locked" status and freed the discardable memory.
282 EXPECT_EQ(0u, BytesAllocated());
283 }
284
285 #if !defined(NDEBUG) && !defined(OS_ANDROID)
286 // Death tests are not supported with Android APKs.
287 TEST_F(DiscardableMemoryProviderTest, UnlockedMemoryAccessCrashesInDebugMode) {
288 size_t size = 1024;
289 const scoped_ptr<DiscardableMemory> discardable(
290 DiscardableMemory::CreateLockedMemory(size));
291 EXPECT_TRUE(IsRegistered(discardable.get()));
292 EXPECT_NE(static_cast<void*>(NULL), Memory(discardable.get()));
293 EXPECT_EQ(1024u, BytesAllocated());
294 EXPECT_FALSE(CanBePurged(discardable.get()));
295 discardable->Unlock();
296 EXPECT_TRUE(CanBePurged(discardable.get()));
297 // We *must* die if we are asked to vend a pointer to unlocked memory.
298 EXPECT_DEATH(discardable->Memory(), ".*Check failed.*");
299 }
300 #endif
301
302 class ThreadedDiscardableMemoryProviderTest
303 : public DiscardableMemoryProviderTest {
304 public:
305 ThreadedDiscardableMemoryProviderTest()
306 : memory_usage_thread_("memory_usage_thread"),
307 thread_sync_(true, false) {
308 }
309
310 virtual void SetUp() OVERRIDE {
311 memory_usage_thread_.Start();
312 }
313
314 virtual void TearDown() OVERRIDE {
315 memory_usage_thread_.Stop();
316 }
317
318 void UseMemoryHelper() {
319 size_t size = 1024;
320 const scoped_ptr<DiscardableMemory> discardable(
321 DiscardableMemory::CreateLockedMemory(size));
322 EXPECT_TRUE(IsRegistered(discardable.get()));
323 EXPECT_NE(static_cast<void*>(NULL), Memory(discardable.get()));
324 discardable->Unlock();
325 }
326
327 void SignalHelper() {
328 thread_sync_.Signal();
329 }
330
331 Thread memory_usage_thread_;
332 WaitableEvent thread_sync_;
333 };
334
335 TEST_F(ThreadedDiscardableMemoryProviderTest, UseMemoryOnThread) {
336 memory_usage_thread_.message_loop()->PostTask(
337 FROM_HERE,
338 Bind(&ThreadedDiscardableMemoryProviderTest::UseMemoryHelper,
339 Unretained(this)));
340 memory_usage_thread_.message_loop()->PostTask(
341 FROM_HERE,
342 Bind(&ThreadedDiscardableMemoryProviderTest::SignalHelper,
343 Unretained(this)));
344 thread_sync_.Wait();
345 }
346
347 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698