OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/memory/discardable_memory_manager.h" | 5 #include "base/memory/discardable_memory_manager.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/run_loop.h" |
8 #include "base/synchronization/waitable_event.h" | 9 #include "base/synchronization/waitable_event.h" |
9 #include "base/threading/thread.h" | 10 #include "base/threading/thread.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
11 | 12 |
12 namespace base { | 13 namespace base { |
13 namespace { | 14 namespace { |
14 | 15 |
15 class TestAllocationImpl : public internal::DiscardableMemoryManagerAllocation { | 16 class TestAllocationImpl : public internal::DiscardableMemoryManagerAllocation { |
16 public: | 17 public: |
17 TestAllocationImpl() : is_allocated_(false), is_locked_(false) {} | 18 TestAllocationImpl() : is_allocated_(false), is_locked_(false) {} |
(...skipping 20 matching lines...) Expand all Loading... |
38 | 39 |
39 private: | 40 private: |
40 bool is_allocated_; | 41 bool is_allocated_; |
41 bool is_locked_; | 42 bool is_locked_; |
42 }; | 43 }; |
43 | 44 |
44 // Tests can assume that the default limit is at least 1024. Tests that rely on | 45 // Tests can assume that the default limit is at least 1024. Tests that rely on |
45 // something else needs to explicit set the limit. | 46 // something else needs to explicit set the limit. |
46 const size_t kDefaultMemoryLimit = 1024; | 47 const size_t kDefaultMemoryLimit = 1024; |
47 const size_t kDefaultSoftMemoryLimit = kDefaultMemoryLimit; | 48 const size_t kDefaultSoftMemoryLimit = kDefaultMemoryLimit; |
| 49 const size_t kDefaultBytesToKeepUnderModeratePressure = kDefaultMemoryLimit; |
48 | 50 |
49 class TestDiscardableMemoryManagerImpl | 51 class TestDiscardableMemoryManagerImpl |
50 : public internal::DiscardableMemoryManager { | 52 : public internal::DiscardableMemoryManager { |
51 public: | 53 public: |
52 TestDiscardableMemoryManagerImpl() | 54 TestDiscardableMemoryManagerImpl() |
53 : DiscardableMemoryManager(kDefaultMemoryLimit, | 55 : DiscardableMemoryManager(kDefaultMemoryLimit, |
54 kDefaultSoftMemoryLimit, | 56 kDefaultSoftMemoryLimit, |
| 57 kDefaultBytesToKeepUnderModeratePressure, |
55 TimeDelta::Max()) {} | 58 TimeDelta::Max()) {} |
56 | 59 |
57 void SetNow(TimeTicks now) { now_ = now; } | 60 void SetNow(TimeTicks now) { now_ = now; } |
58 | 61 |
59 private: | 62 private: |
60 // Overriden from internal::DiscardableMemoryManager: | 63 // Overriden from internal::DiscardableMemoryManager: |
61 virtual TimeTicks Now() const OVERRIDE { return now_; } | 64 virtual TimeTicks Now() const OVERRIDE { return now_; } |
62 | 65 |
63 TimeTicks now_; | 66 TimeTicks now_; |
64 }; | 67 }; |
65 | 68 |
66 class DiscardableMemoryManagerTestBase { | 69 class DiscardableMemoryManagerTestBase { |
67 public: | 70 public: |
68 DiscardableMemoryManagerTestBase() {} | 71 DiscardableMemoryManagerTestBase() { |
| 72 manager_.RegisterMemoryPressureListener(); |
| 73 } |
69 | 74 |
70 protected: | 75 protected: |
71 enum LockStatus { | 76 enum LockStatus { |
72 LOCK_STATUS_FAILED, | 77 LOCK_STATUS_FAILED, |
73 LOCK_STATUS_PURGED, | 78 LOCK_STATUS_PURGED, |
74 LOCK_STATUS_SUCCESS | 79 LOCK_STATUS_SUCCESS |
75 }; | 80 }; |
76 | 81 |
77 size_t BytesAllocated() const { return manager_.GetBytesAllocatedForTest(); } | 82 size_t BytesAllocated() const { return manager_.GetBytesAllocatedForTest(); } |
78 | 83 |
79 void SetMemoryLimit(size_t bytes) { manager_.SetMemoryLimit(bytes); } | 84 void SetMemoryLimit(size_t bytes) { manager_.SetMemoryLimit(bytes); } |
80 | 85 |
81 void SetSoftMemoryLimit(size_t bytes) { manager_.SetSoftMemoryLimit(bytes); } | 86 void SetSoftMemoryLimit(size_t bytes) { manager_.SetSoftMemoryLimit(bytes); } |
82 | 87 |
| 88 void SetBytesToKeepUnderModeratePressure(size_t bytes) { |
| 89 manager_.SetBytesToKeepUnderModeratePressure(bytes); |
| 90 } |
| 91 |
83 void SetHardMemoryLimitExpirationTime(TimeDelta time) { | 92 void SetHardMemoryLimitExpirationTime(TimeDelta time) { |
84 manager_.SetHardMemoryLimitExpirationTime(time); | 93 manager_.SetHardMemoryLimitExpirationTime(time); |
85 } | 94 } |
86 | 95 |
87 void Register(TestAllocationImpl* allocation, size_t bytes) { | 96 void Register(TestAllocationImpl* allocation, size_t bytes) { |
88 manager_.Register(allocation, bytes); | 97 manager_.Register(allocation, bytes); |
89 } | 98 } |
90 | 99 |
91 void Unregister(TestAllocationImpl* allocation) { | 100 void Unregister(TestAllocationImpl* allocation) { |
92 manager_.Unregister(allocation); | 101 manager_.Unregister(allocation); |
(...skipping 18 matching lines...) Expand all Loading... |
111 manager_.Register(allocation, bytes); | 120 manager_.Register(allocation, bytes); |
112 return Lock(allocation); | 121 return Lock(allocation); |
113 } | 122 } |
114 | 123 |
115 bool CanBePurged(TestAllocationImpl* allocation) const { | 124 bool CanBePurged(TestAllocationImpl* allocation) const { |
116 return manager_.CanBePurgedForTest(allocation); | 125 return manager_.CanBePurgedForTest(allocation); |
117 } | 126 } |
118 | 127 |
119 void SetNow(TimeTicks now) { manager_.SetNow(now); } | 128 void SetNow(TimeTicks now) { manager_.SetNow(now); } |
120 | 129 |
121 void PurgeAll() { return manager_.PurgeAll(); } | |
122 | |
123 bool ReduceMemoryUsage() { return manager_.ReduceMemoryUsage(); } | 130 bool ReduceMemoryUsage() { return manager_.ReduceMemoryUsage(); } |
124 | 131 |
125 void ReduceMemoryUsageUntilWithinLimit(size_t bytes) { | |
126 manager_.ReduceMemoryUsageUntilWithinLimit(bytes); | |
127 } | |
128 | |
129 private: | 132 private: |
| 133 MessageLoopForIO message_loop_; |
130 TestDiscardableMemoryManagerImpl manager_; | 134 TestDiscardableMemoryManagerImpl manager_; |
131 }; | 135 }; |
132 | 136 |
133 class DiscardableMemoryManagerTest : public DiscardableMemoryManagerTestBase, | 137 class DiscardableMemoryManagerTest : public DiscardableMemoryManagerTestBase, |
134 public testing::Test { | 138 public testing::Test { |
135 public: | 139 public: |
136 DiscardableMemoryManagerTest() {} | 140 DiscardableMemoryManagerTest() {} |
137 }; | 141 }; |
138 | 142 |
139 TEST_F(DiscardableMemoryManagerTest, CreateAndLock) { | 143 TEST_F(DiscardableMemoryManagerTest, CreateAndLock) { |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
181 TestAllocationImpl allocation; | 185 TestAllocationImpl allocation; |
182 RegisterAndLock(&allocation, size); | 186 RegisterAndLock(&allocation, size); |
183 EXPECT_EQ(1024u, BytesAllocated()); | 187 EXPECT_EQ(1024u, BytesAllocated()); |
184 EXPECT_FALSE(CanBePurged(&allocation)); | 188 EXPECT_FALSE(CanBePurged(&allocation)); |
185 | 189 |
186 // Now unlock so we can lock later. | 190 // Now unlock so we can lock later. |
187 Unlock(&allocation); | 191 Unlock(&allocation); |
188 EXPECT_TRUE(CanBePurged(&allocation)); | 192 EXPECT_TRUE(CanBePurged(&allocation)); |
189 | 193 |
190 // Force the system to purge. | 194 // Force the system to purge. |
191 PurgeAll(); | 195 MemoryPressureListener::NotifyMemoryPressure( |
| 196 MemoryPressureListener::MEMORY_PRESSURE_CRITICAL); |
| 197 |
| 198 // Required because ObserverListThreadSafe notifies via PostTask. |
| 199 RunLoop().RunUntilIdle(); |
192 | 200 |
193 EXPECT_EQ(LOCK_STATUS_PURGED, Lock(&allocation)); | 201 EXPECT_EQ(LOCK_STATUS_PURGED, Lock(&allocation)); |
194 EXPECT_FALSE(CanBePurged(&allocation)); | 202 EXPECT_FALSE(CanBePurged(&allocation)); |
195 | 203 |
196 Unlock(&allocation); | 204 Unlock(&allocation); |
197 Unregister(&allocation); | 205 Unregister(&allocation); |
198 } | 206 } |
199 | 207 |
200 TEST_F(DiscardableMemoryManagerTest, LockAfterPurgeAndCannotReallocate) { | 208 TEST_F(DiscardableMemoryManagerTest, LockAfterPurgeAndCannotReallocate) { |
201 size_t size = 1024; | 209 size_t size = 1024; |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
286 if (allocation_[i].is_locked()) | 294 if (allocation_[i].is_locked()) |
287 Unlock(&allocation_[i]); | 295 Unlock(&allocation_[i]); |
288 Unregister(&allocation_[i]); | 296 Unregister(&allocation_[i]); |
289 } | 297 } |
290 } | 298 } |
291 | 299 |
292 private: | 300 private: |
293 TestAllocationImpl allocation_[3]; | 301 TestAllocationImpl allocation_[3]; |
294 }; | 302 }; |
295 | 303 |
296 // Verify that memory was discarded in the correct order after reducing usage to | 304 // Verify that memory was discarded in the correct order after applying |
297 // limit. | 305 // memory pressure. |
298 TEST_P(DiscardableMemoryManagerPermutationTest, LRUDiscarded) { | 306 TEST_P(DiscardableMemoryManagerPermutationTest, LRUDiscardedModeratePressure) { |
299 RegisterAndUseAllocations(); | 307 RegisterAndUseAllocations(); |
300 | 308 |
| 309 SetBytesToKeepUnderModeratePressure(1024); |
301 SetMemoryLimit(2048); | 310 SetMemoryLimit(2048); |
302 | 311 |
303 ReduceMemoryUsageUntilWithinLimit(1024); | 312 MemoryPressureListener::NotifyMemoryPressure( |
| 313 MemoryPressureListener::MEMORY_PRESSURE_MODERATE); |
| 314 RunLoop().RunUntilIdle(); |
304 | 315 |
305 EXPECT_NE(LOCK_STATUS_FAILED, Lock(allocation(2))); | 316 EXPECT_NE(LOCK_STATUS_FAILED, Lock(allocation(2))); |
306 EXPECT_EQ(LOCK_STATUS_PURGED, Lock(allocation(1))); | 317 EXPECT_EQ(LOCK_STATUS_PURGED, Lock(allocation(1))); |
307 // 0 should still be locked. | 318 // 0 should still be locked. |
308 EXPECT_TRUE(allocation(0)->is_locked()); | 319 EXPECT_TRUE(allocation(0)->is_locked()); |
309 | 320 |
310 UnlockAndUnregisterAllocations(); | 321 UnlockAndUnregisterAllocations(); |
311 } | 322 } |
312 | 323 |
313 // Verify that memory was discarded in the correct order after changing | 324 // Verify that memory was discarded in the correct order after changing |
314 // memory limit. | 325 // memory limit. |
315 TEST_P(DiscardableMemoryManagerPermutationTest, LRUDiscardedExceedLimit) { | 326 TEST_P(DiscardableMemoryManagerPermutationTest, LRUDiscardedExceedLimit) { |
316 RegisterAndUseAllocations(); | 327 RegisterAndUseAllocations(); |
317 | 328 |
| 329 SetBytesToKeepUnderModeratePressure(1024); |
318 SetMemoryLimit(2048); | 330 SetMemoryLimit(2048); |
319 | 331 |
320 EXPECT_NE(LOCK_STATUS_FAILED, Lock(allocation(2))); | 332 EXPECT_NE(LOCK_STATUS_FAILED, Lock(allocation(2))); |
321 EXPECT_EQ(LOCK_STATUS_PURGED, Lock(allocation(1))); | 333 EXPECT_EQ(LOCK_STATUS_PURGED, Lock(allocation(1))); |
322 // 0 should still be locked. | 334 // 0 should still be locked. |
323 EXPECT_TRUE(allocation(0)->is_locked()); | 335 EXPECT_TRUE(allocation(0)->is_locked()); |
324 | 336 |
325 UnlockAndUnregisterAllocations(); | 337 UnlockAndUnregisterAllocations(); |
326 } | 338 } |
327 | 339 |
328 // Verify that no more memory than necessary was discarded after changing | 340 // Verify that no more memory than necessary was discarded after changing |
329 // memory limit. | 341 // memory limit. |
330 TEST_P(DiscardableMemoryManagerPermutationTest, LRUDiscardedAmount) { | 342 TEST_P(DiscardableMemoryManagerPermutationTest, LRUDiscardedAmount) { |
| 343 SetBytesToKeepUnderModeratePressure(2048); |
331 SetMemoryLimit(4096); | 344 SetMemoryLimit(4096); |
332 | 345 |
333 RegisterAndUseAllocations(); | 346 RegisterAndUseAllocations(); |
334 | 347 |
335 SetMemoryLimit(2048); | 348 SetMemoryLimit(2048); |
336 | 349 |
337 EXPECT_EQ(LOCK_STATUS_SUCCESS, Lock(allocation(2))); | 350 EXPECT_EQ(LOCK_STATUS_SUCCESS, Lock(allocation(2))); |
338 EXPECT_EQ(LOCK_STATUS_PURGED, Lock(allocation(1))); | 351 EXPECT_EQ(LOCK_STATUS_PURGED, Lock(allocation(1))); |
339 // 0 should still be locked. | 352 // 0 should still be locked. |
340 EXPECT_TRUE(allocation(0)->is_locked()); | 353 EXPECT_TRUE(allocation(0)->is_locked()); |
341 | 354 |
342 UnlockAndUnregisterAllocations(); | 355 UnlockAndUnregisterAllocations(); |
343 } | 356 } |
344 | 357 |
345 TEST_P(DiscardableMemoryManagerPermutationTest, PurgeFreesAllUnlocked) { | 358 TEST_P(DiscardableMemoryManagerPermutationTest, PurgeFreesAllUnlocked) { |
346 RegisterAndUseAllocations(); | 359 RegisterAndUseAllocations(); |
347 | 360 |
348 PurgeAll(); | 361 MemoryPressureListener::NotifyMemoryPressure( |
| 362 MemoryPressureListener::MEMORY_PRESSURE_CRITICAL); |
| 363 RunLoop().RunUntilIdle(); |
349 | 364 |
350 for (int i = 0; i < 3; ++i) { | 365 for (int i = 0; i < 3; ++i) { |
351 if (i == 0) | 366 if (i == 0) |
352 EXPECT_TRUE(allocation(i)->is_locked()); | 367 EXPECT_TRUE(allocation(i)->is_locked()); |
353 else | 368 else |
354 EXPECT_EQ(LOCK_STATUS_PURGED, Lock(allocation(i))); | 369 EXPECT_EQ(LOCK_STATUS_PURGED, Lock(allocation(i))); |
355 } | 370 } |
356 | 371 |
357 UnlockAndUnregisterAllocations(); | 372 UnlockAndUnregisterAllocations(); |
358 } | 373 } |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
481 Unretained(this))); | 496 Unretained(this))); |
482 memory_usage_thread_.message_loop()->PostTask( | 497 memory_usage_thread_.message_loop()->PostTask( |
483 FROM_HERE, | 498 FROM_HERE, |
484 Bind(&ThreadedDiscardableMemoryManagerTest::SignalHelper, | 499 Bind(&ThreadedDiscardableMemoryManagerTest::SignalHelper, |
485 Unretained(this))); | 500 Unretained(this))); |
486 thread_sync_.Wait(); | 501 thread_sync_.Wait(); |
487 } | 502 } |
488 | 503 |
489 } // namespace | 504 } // namespace |
490 } // namespace base | 505 } // namespace base |
OLD | NEW |