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

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

Issue 336273003: base: Add soft memory limit to DiscardableMemoryManager. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: v2 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 | Annotate | Revision Log
OLDNEW
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
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
61 void SetNow(TimeTicks now) { now_ = now; }
62
63 protected:
64 // Overriden from internal::DiscardableMemoryManager:
65 virtual TimeTicks Now() const OVERRIDE {
66 return now_;
67 }
68
69 private:
70 TimeTicks now_;
71 };
72
50 class DiscardableMemoryManagerTestBase { 73 class DiscardableMemoryManagerTestBase {
51 public: 74 public:
52 DiscardableMemoryManagerTestBase() 75 DiscardableMemoryManagerTestBase() {
53 : manager_(kDefaultMemoryLimit,
54 kDefaultBytesToKeepUnderModeratePressure) {
55 manager_.RegisterMemoryPressureListener(); 76 manager_.RegisterMemoryPressureListener();
56 } 77 }
57 78
58 protected: 79 protected:
59 enum LockStatus { 80 enum LockStatus {
60 LOCK_STATUS_FAILED, 81 LOCK_STATUS_FAILED,
61 LOCK_STATUS_PURGED, 82 LOCK_STATUS_PURGED,
62 LOCK_STATUS_SUCCESS 83 LOCK_STATUS_SUCCESS
63 }; 84 };
64 85
65 size_t BytesAllocated() const { return manager_.GetBytesAllocatedForTest(); } 86 size_t BytesAllocated() const { return manager_.GetBytesAllocatedForTest(); }
66 87
67 void SetMemoryLimit(size_t bytes) { manager_.SetMemoryLimit(bytes); } 88 void SetMemoryLimit(size_t bytes) { manager_.SetMemoryLimit(bytes); }
68 89
90 void SetSoftMemoryLimit(size_t bytes) { manager_.SetSoftMemoryLimit(bytes); }
91
69 void SetBytesToKeepUnderModeratePressure(size_t bytes) { 92 void SetBytesToKeepUnderModeratePressure(size_t bytes) {
70 manager_.SetBytesToKeepUnderModeratePressure(bytes); 93 manager_.SetBytesToKeepUnderModeratePressure(bytes);
71 } 94 }
72 95
96 void SetHardMemoryLimitExpirationTime(TimeDelta time) {
97 manager_.SetHardMemoryLimitExpirationTime(time);
98 }
99
73 void Register(TestAllocationImpl* allocation, size_t bytes) { 100 void Register(TestAllocationImpl* allocation, size_t bytes) {
74 manager_.Register(allocation, bytes); 101 manager_.Register(allocation, bytes);
75 } 102 }
76 103
77 void Unregister(TestAllocationImpl* allocation) { 104 void Unregister(TestAllocationImpl* allocation) {
78 manager_.Unregister(allocation); 105 manager_.Unregister(allocation);
79 } 106 }
80 107
81 bool IsRegistered(TestAllocationImpl* allocation) const { 108 bool IsRegistered(TestAllocationImpl* allocation) const {
82 return manager_.IsRegisteredForTest(allocation); 109 return manager_.IsRegisteredForTest(allocation);
(...skipping 12 matching lines...) Expand all
95 122
96 LockStatus RegisterAndLock(TestAllocationImpl* allocation, size_t bytes) { 123 LockStatus RegisterAndLock(TestAllocationImpl* allocation, size_t bytes) {
97 manager_.Register(allocation, bytes); 124 manager_.Register(allocation, bytes);
98 return Lock(allocation); 125 return Lock(allocation);
99 } 126 }
100 127
101 bool CanBePurged(TestAllocationImpl* allocation) const { 128 bool CanBePurged(TestAllocationImpl* allocation) const {
102 return manager_.CanBePurgedForTest(allocation); 129 return manager_.CanBePurgedForTest(allocation);
103 } 130 }
104 131
132 void SetNow(TimeTicks now) {
133 manager_.SetNow(now);
134 }
135
136 bool IdleNotification() {
137 return manager_.IdleNotification();
138 }
139
105 private: 140 private:
106 MessageLoopForIO message_loop_; 141 MessageLoopForIO message_loop_;
107 internal::DiscardableMemoryManager manager_; 142 TestDiscardableMemoryManagerImpl manager_;
108 }; 143 };
109 144
110 class DiscardableMemoryManagerTest : public DiscardableMemoryManagerTestBase, 145 class DiscardableMemoryManagerTest : public DiscardableMemoryManagerTestBase,
111 public testing::Test { 146 public testing::Test {
112 public: 147 public:
113 DiscardableMemoryManagerTest() {} 148 DiscardableMemoryManagerTest() {}
114 }; 149 };
115 150
116 TEST_F(DiscardableMemoryManagerTest, CreateAndLock) { 151 TEST_F(DiscardableMemoryManagerTest, CreateAndLock) {
117 size_t size = 1024; 152 size_t size = 1024;
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after
385 EXPECT_EQ(1024u, BytesAllocated()); 420 EXPECT_EQ(1024u, BytesAllocated());
386 Unlock(&allocation); 421 Unlock(&allocation);
387 EXPECT_TRUE(CanBePurged(&allocation)); 422 EXPECT_TRUE(CanBePurged(&allocation));
388 SetMemoryLimit(0); 423 SetMemoryLimit(0);
389 EXPECT_EQ(0u, BytesAllocated()); 424 EXPECT_EQ(0u, BytesAllocated());
390 Unregister(&allocation); 425 Unregister(&allocation);
391 } 426 }
392 EXPECT_EQ(0u, BytesAllocated()); 427 EXPECT_EQ(0u, BytesAllocated());
393 } 428 }
394 429
430 TEST_F(DiscardableMemoryManagerTest, IdleNotification) {
431 SetMemoryLimit(3072);
432 SetSoftMemoryLimit(1024);
433 SetHardMemoryLimitExpirationTime(TimeDelta::FromInternalValue(1));
434
435 size_t size = 1024;
436 TestAllocationImpl allocation[3];
437 RegisterAndLock(&allocation[0], size);
438 RegisterAndLock(&allocation[1], size);
439 RegisterAndLock(&allocation[2], size);
440 EXPECT_EQ(3072u, BytesAllocated());
441
442 // Above soft limit but nothing that can be purged.
443 EXPECT_FALSE(IdleNotification());
444
445 SetNow(TimeTicks::FromInternalValue(0));
446 Unlock(&allocation[0]);
447
448 // Above soft limit but still nothing that can be purged as all unlocked
449 // allocation is within the hard limit cutoff time.
450 EXPECT_FALSE(IdleNotification());
451
452 SetNow(TimeTicks::FromInternalValue(1));
453 Unlock(&allocation[1]);
454
455 // One unlocked allocation is no longer within the hard limit cutoff time. It
456 // should be purged. IdleNotification() should return false as we're not yet
457 // within the soft memory limit.
458 EXPECT_FALSE(IdleNotification());
459 EXPECT_EQ(2048u, BytesAllocated());
460
461 // One more unlocked allocation is no longer within the hard limit cutoff
462 // time. It should be purged. IdleNotification() should return true as we're
463 // now within the soft memory limit.
464 SetNow(TimeTicks::FromInternalValue(2));
465 EXPECT_TRUE(IdleNotification());
466 EXPECT_EQ(1024u, BytesAllocated());
467
468 Unlock(&allocation[2]);
469
470 Unregister(&allocation[0]);
471 Unregister(&allocation[1]);
472 Unregister(&allocation[2]);
473 }
474
395 class ThreadedDiscardableMemoryManagerTest 475 class ThreadedDiscardableMemoryManagerTest
396 : public DiscardableMemoryManagerTest { 476 : public DiscardableMemoryManagerTest {
397 public: 477 public:
398 ThreadedDiscardableMemoryManagerTest() 478 ThreadedDiscardableMemoryManagerTest()
399 : memory_usage_thread_("memory_usage_thread"), 479 : memory_usage_thread_("memory_usage_thread"),
400 thread_sync_(true, false) {} 480 thread_sync_(true, false) {}
401 481
402 virtual void SetUp() OVERRIDE { memory_usage_thread_.Start(); } 482 virtual void SetUp() OVERRIDE { memory_usage_thread_.Start(); }
403 483
404 virtual void TearDown() OVERRIDE { memory_usage_thread_.Stop(); } 484 virtual void TearDown() OVERRIDE { memory_usage_thread_.Stop(); }
(...skipping 19 matching lines...) Expand all
424 Unretained(this))); 504 Unretained(this)));
425 memory_usage_thread_.message_loop()->PostTask( 505 memory_usage_thread_.message_loop()->PostTask(
426 FROM_HERE, 506 FROM_HERE,
427 Bind(&ThreadedDiscardableMemoryManagerTest::SignalHelper, 507 Bind(&ThreadedDiscardableMemoryManagerTest::SignalHelper,
428 Unretained(this))); 508 Unretained(this)));
429 thread_sync_.Wait(); 509 thread_sync_.Wait();
430 } 510 }
431 511
432 } // namespace 512 } // namespace
433 } // namespace base 513 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698