Chromium Code Reviews| 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/run_loop.h" |
| 9 #include "base/synchronization/waitable_event.h" | 9 #include "base/synchronization/waitable_event.h" |
| 10 #include "base/threading/thread.h" | 10 #include "base/threading/thread.h" |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 38 bool is_locked() const { return is_locked_; } | 38 bool is_locked() const { return is_locked_; } |
| 39 | 39 |
| 40 private: | 40 private: |
| 41 bool is_allocated_; | 41 bool is_allocated_; |
| 42 bool is_locked_; | 42 bool is_locked_; |
| 43 }; | 43 }; |
| 44 | 44 |
| 45 // 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 |
| 46 // something else needs to explicit set the limit. | 46 // something else needs to explicit set the limit. |
| 47 const size_t kDefaultMemoryLimit = 1024; | 47 const size_t kDefaultMemoryLimit = 1024; |
| 48 const size_t kDefaultSoftMemoryLimit = kDefaultMemoryLimit; | |
| 48 const size_t kDefaultBytesToKeepUnderModeratePressure = kDefaultMemoryLimit; | 49 const size_t kDefaultBytesToKeepUnderModeratePressure = kDefaultMemoryLimit; |
| 49 | 50 |
| 51 class TestDiscardableMemoryManagerImpl | |
| 52 : public internal::DiscardableMemoryManager { | |
| 53 public: | |
| 54 TestDiscardableMemoryManagerImpl() | |
| 55 : DiscardableMemoryManager(kDefaultMemoryLimit, | |
| 56 kDefaultSoftMemoryLimit, | |
| 57 kDefaultBytesToKeepUnderModeratePressure, | |
| 58 TimeDelta::Max()) {} | |
| 59 | |
| 60 void SetNow(TimeTicks now) { now_ = now; } | |
| 61 | |
| 62 protected: | |
| 63 // Overriden from internal::DiscardableMemoryManager: | |
| 64 virtual TimeTicks Now() const OVERRIDE { return now_; } | |
|
willchan no longer on Chromium
2014/07/07 22:28:08
Likewise, this doesn't have to be protected. It's
reveman
2014/07/07 23:49:03
Done.
| |
| 65 | |
| 66 private: | |
| 67 TimeTicks now_; | |
| 68 }; | |
| 69 | |
| 50 class DiscardableMemoryManagerTestBase { | 70 class DiscardableMemoryManagerTestBase { |
| 51 public: | 71 public: |
| 52 DiscardableMemoryManagerTestBase() | 72 DiscardableMemoryManagerTestBase() { |
| 53 : manager_(kDefaultMemoryLimit, | |
| 54 kDefaultBytesToKeepUnderModeratePressure) { | |
| 55 manager_.RegisterMemoryPressureListener(); | 73 manager_.RegisterMemoryPressureListener(); |
| 56 } | 74 } |
| 57 | 75 |
| 58 protected: | 76 protected: |
| 59 enum LockStatus { | 77 enum LockStatus { |
| 60 LOCK_STATUS_FAILED, | 78 LOCK_STATUS_FAILED, |
| 61 LOCK_STATUS_PURGED, | 79 LOCK_STATUS_PURGED, |
| 62 LOCK_STATUS_SUCCESS | 80 LOCK_STATUS_SUCCESS |
| 63 }; | 81 }; |
| 64 | 82 |
| 65 size_t BytesAllocated() const { return manager_.GetBytesAllocatedForTest(); } | 83 size_t BytesAllocated() const { return manager_.GetBytesAllocatedForTest(); } |
| 66 | 84 |
| 67 void SetMemoryLimit(size_t bytes) { manager_.SetMemoryLimit(bytes); } | 85 void SetMemoryLimit(size_t bytes) { manager_.SetMemoryLimit(bytes); } |
| 68 | 86 |
| 87 void SetSoftMemoryLimit(size_t bytes) { manager_.SetSoftMemoryLimit(bytes); } | |
| 88 | |
| 69 void SetBytesToKeepUnderModeratePressure(size_t bytes) { | 89 void SetBytesToKeepUnderModeratePressure(size_t bytes) { |
| 70 manager_.SetBytesToKeepUnderModeratePressure(bytes); | 90 manager_.SetBytesToKeepUnderModeratePressure(bytes); |
| 71 } | 91 } |
| 72 | 92 |
| 93 void SetHardMemoryLimitExpirationTime(TimeDelta time) { | |
| 94 manager_.SetHardMemoryLimitExpirationTime(time); | |
| 95 } | |
| 96 | |
| 73 void Register(TestAllocationImpl* allocation, size_t bytes) { | 97 void Register(TestAllocationImpl* allocation, size_t bytes) { |
| 74 manager_.Register(allocation, bytes); | 98 manager_.Register(allocation, bytes); |
| 75 } | 99 } |
| 76 | 100 |
| 77 void Unregister(TestAllocationImpl* allocation) { | 101 void Unregister(TestAllocationImpl* allocation) { |
| 78 manager_.Unregister(allocation); | 102 manager_.Unregister(allocation); |
| 79 } | 103 } |
| 80 | 104 |
| 81 bool IsRegistered(TestAllocationImpl* allocation) const { | 105 bool IsRegistered(TestAllocationImpl* allocation) const { |
| 82 return manager_.IsRegisteredForTest(allocation); | 106 return manager_.IsRegisteredForTest(allocation); |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 95 | 119 |
| 96 LockStatus RegisterAndLock(TestAllocationImpl* allocation, size_t bytes) { | 120 LockStatus RegisterAndLock(TestAllocationImpl* allocation, size_t bytes) { |
| 97 manager_.Register(allocation, bytes); | 121 manager_.Register(allocation, bytes); |
| 98 return Lock(allocation); | 122 return Lock(allocation); |
| 99 } | 123 } |
| 100 | 124 |
| 101 bool CanBePurged(TestAllocationImpl* allocation) const { | 125 bool CanBePurged(TestAllocationImpl* allocation) const { |
| 102 return manager_.CanBePurgedForTest(allocation); | 126 return manager_.CanBePurgedForTest(allocation); |
| 103 } | 127 } |
| 104 | 128 |
| 129 void SetNow(TimeTicks now) { manager_.SetNow(now); } | |
| 130 | |
| 131 bool ReduceMemoryUsage() { return manager_.ReduceMemoryUsage(); } | |
| 132 | |
| 105 private: | 133 private: |
| 106 MessageLoopForIO message_loop_; | 134 MessageLoopForIO message_loop_; |
| 107 internal::DiscardableMemoryManager manager_; | 135 TestDiscardableMemoryManagerImpl manager_; |
| 108 }; | 136 }; |
| 109 | 137 |
| 110 class DiscardableMemoryManagerTest : public DiscardableMemoryManagerTestBase, | 138 class DiscardableMemoryManagerTest : public DiscardableMemoryManagerTestBase, |
| 111 public testing::Test { | 139 public testing::Test { |
| 112 public: | 140 public: |
| 113 DiscardableMemoryManagerTest() {} | 141 DiscardableMemoryManagerTest() {} |
| 114 }; | 142 }; |
| 115 | 143 |
| 116 TEST_F(DiscardableMemoryManagerTest, CreateAndLock) { | 144 TEST_F(DiscardableMemoryManagerTest, CreateAndLock) { |
| 117 size_t size = 1024; | 145 size_t size = 1024; |
| (...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 385 EXPECT_EQ(1024u, BytesAllocated()); | 413 EXPECT_EQ(1024u, BytesAllocated()); |
| 386 Unlock(&allocation); | 414 Unlock(&allocation); |
| 387 EXPECT_TRUE(CanBePurged(&allocation)); | 415 EXPECT_TRUE(CanBePurged(&allocation)); |
| 388 SetMemoryLimit(0); | 416 SetMemoryLimit(0); |
| 389 EXPECT_EQ(0u, BytesAllocated()); | 417 EXPECT_EQ(0u, BytesAllocated()); |
| 390 Unregister(&allocation); | 418 Unregister(&allocation); |
| 391 } | 419 } |
| 392 EXPECT_EQ(0u, BytesAllocated()); | 420 EXPECT_EQ(0u, BytesAllocated()); |
| 393 } | 421 } |
| 394 | 422 |
| 423 TEST_F(DiscardableMemoryManagerTest, ReduceMemoryUsage) { | |
| 424 SetMemoryLimit(3072); | |
| 425 SetSoftMemoryLimit(1024); | |
| 426 SetHardMemoryLimitExpirationTime(TimeDelta::FromInternalValue(1)); | |
| 427 | |
| 428 size_t size = 1024; | |
| 429 TestAllocationImpl allocation[3]; | |
| 430 RegisterAndLock(&allocation[0], size); | |
| 431 RegisterAndLock(&allocation[1], size); | |
| 432 RegisterAndLock(&allocation[2], size); | |
| 433 EXPECT_EQ(3072u, BytesAllocated()); | |
| 434 | |
| 435 // Above soft limit but nothing that can be purged. | |
| 436 EXPECT_FALSE(ReduceMemoryUsage()); | |
| 437 | |
| 438 SetNow(TimeTicks::FromInternalValue(0)); | |
| 439 Unlock(&allocation[0]); | |
| 440 | |
| 441 // Above soft limit but still nothing that can be purged as all unlocked | |
| 442 // allocations are within the hard limit cutoff time. | |
| 443 EXPECT_FALSE(ReduceMemoryUsage()); | |
| 444 | |
| 445 SetNow(TimeTicks::FromInternalValue(1)); | |
| 446 Unlock(&allocation[1]); | |
| 447 | |
| 448 // One unlocked allocation is no longer within the hard limit cutoff time. It | |
| 449 // should be purged and ReduceMemoryUsage() should return false as we're not | |
| 450 // yet within the soft memory limit. | |
| 451 EXPECT_FALSE(ReduceMemoryUsage()); | |
| 452 EXPECT_EQ(2048u, BytesAllocated()); | |
| 453 | |
| 454 // One more unlocked allocation is no longer within the hard limit cutoff | |
| 455 // time. It should be purged and ReduceMemoryUsage() should return true as | |
| 456 // we're now within the soft memory limit. | |
| 457 SetNow(TimeTicks::FromInternalValue(2)); | |
| 458 EXPECT_TRUE(ReduceMemoryUsage()); | |
| 459 EXPECT_EQ(1024u, BytesAllocated()); | |
| 460 | |
| 461 Unlock(&allocation[2]); | |
| 462 | |
| 463 Unregister(&allocation[0]); | |
| 464 Unregister(&allocation[1]); | |
| 465 Unregister(&allocation[2]); | |
| 466 } | |
| 467 | |
| 395 class ThreadedDiscardableMemoryManagerTest | 468 class ThreadedDiscardableMemoryManagerTest |
| 396 : public DiscardableMemoryManagerTest { | 469 : public DiscardableMemoryManagerTest { |
| 397 public: | 470 public: |
| 398 ThreadedDiscardableMemoryManagerTest() | 471 ThreadedDiscardableMemoryManagerTest() |
| 399 : memory_usage_thread_("memory_usage_thread"), | 472 : memory_usage_thread_("memory_usage_thread"), |
| 400 thread_sync_(true, false) {} | 473 thread_sync_(true, false) {} |
| 401 | 474 |
| 402 virtual void SetUp() OVERRIDE { memory_usage_thread_.Start(); } | 475 virtual void SetUp() OVERRIDE { memory_usage_thread_.Start(); } |
| 403 | 476 |
| 404 virtual void TearDown() OVERRIDE { memory_usage_thread_.Stop(); } | 477 virtual void TearDown() OVERRIDE { memory_usage_thread_.Stop(); } |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 424 Unretained(this))); | 497 Unretained(this))); |
| 425 memory_usage_thread_.message_loop()->PostTask( | 498 memory_usage_thread_.message_loop()->PostTask( |
| 426 FROM_HERE, | 499 FROM_HERE, |
| 427 Bind(&ThreadedDiscardableMemoryManagerTest::SignalHelper, | 500 Bind(&ThreadedDiscardableMemoryManagerTest::SignalHelper, |
| 428 Unretained(this))); | 501 Unretained(this))); |
| 429 thread_sync_.Wait(); | 502 thread_sync_.Wait(); |
| 430 } | 503 } |
| 431 | 504 |
| 432 } // namespace | 505 } // namespace |
| 433 } // namespace base | 506 } // namespace base |
| OLD | NEW |