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

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: fix mac build Created 6 years, 5 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
« no previous file with comments | « base/memory/discardable_memory_manager.cc ('k') | base/memory/discardable_memory_win.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 void SetNow(TimeTicks now) { now_ = now; }
61
62 private:
63 // Overriden from internal::DiscardableMemoryManager:
64 virtual TimeTicks Now() const OVERRIDE { return now_; }
65
66 TimeTicks now_;
67 };
68
50 class DiscardableMemoryManagerTestBase { 69 class DiscardableMemoryManagerTestBase {
51 public: 70 public:
52 DiscardableMemoryManagerTestBase() 71 DiscardableMemoryManagerTestBase() {
53 : manager_(kDefaultMemoryLimit,
54 kDefaultBytesToKeepUnderModeratePressure) {
55 manager_.RegisterMemoryPressureListener(); 72 manager_.RegisterMemoryPressureListener();
56 } 73 }
57 74
58 protected: 75 protected:
59 enum LockStatus { 76 enum LockStatus {
60 LOCK_STATUS_FAILED, 77 LOCK_STATUS_FAILED,
61 LOCK_STATUS_PURGED, 78 LOCK_STATUS_PURGED,
62 LOCK_STATUS_SUCCESS 79 LOCK_STATUS_SUCCESS
63 }; 80 };
64 81
65 size_t BytesAllocated() const { return manager_.GetBytesAllocatedForTest(); } 82 size_t BytesAllocated() const { return manager_.GetBytesAllocatedForTest(); }
66 83
67 void SetMemoryLimit(size_t bytes) { manager_.SetMemoryLimit(bytes); } 84 void SetMemoryLimit(size_t bytes) { manager_.SetMemoryLimit(bytes); }
68 85
86 void SetSoftMemoryLimit(size_t bytes) { manager_.SetSoftMemoryLimit(bytes); }
87
69 void SetBytesToKeepUnderModeratePressure(size_t bytes) { 88 void SetBytesToKeepUnderModeratePressure(size_t bytes) {
70 manager_.SetBytesToKeepUnderModeratePressure(bytes); 89 manager_.SetBytesToKeepUnderModeratePressure(bytes);
71 } 90 }
72 91
92 void SetHardMemoryLimitExpirationTime(TimeDelta time) {
93 manager_.SetHardMemoryLimitExpirationTime(time);
94 }
95
73 void Register(TestAllocationImpl* allocation, size_t bytes) { 96 void Register(TestAllocationImpl* allocation, size_t bytes) {
74 manager_.Register(allocation, bytes); 97 manager_.Register(allocation, bytes);
75 } 98 }
76 99
77 void Unregister(TestAllocationImpl* allocation) { 100 void Unregister(TestAllocationImpl* allocation) {
78 manager_.Unregister(allocation); 101 manager_.Unregister(allocation);
79 } 102 }
80 103
81 bool IsRegistered(TestAllocationImpl* allocation) const { 104 bool IsRegistered(TestAllocationImpl* allocation) const {
82 return manager_.IsRegisteredForTest(allocation); 105 return manager_.IsRegisteredForTest(allocation);
(...skipping 12 matching lines...) Expand all
95 118
96 LockStatus RegisterAndLock(TestAllocationImpl* allocation, size_t bytes) { 119 LockStatus RegisterAndLock(TestAllocationImpl* allocation, size_t bytes) {
97 manager_.Register(allocation, bytes); 120 manager_.Register(allocation, bytes);
98 return Lock(allocation); 121 return Lock(allocation);
99 } 122 }
100 123
101 bool CanBePurged(TestAllocationImpl* allocation) const { 124 bool CanBePurged(TestAllocationImpl* allocation) const {
102 return manager_.CanBePurgedForTest(allocation); 125 return manager_.CanBePurgedForTest(allocation);
103 } 126 }
104 127
128 void SetNow(TimeTicks now) { manager_.SetNow(now); }
129
130 bool ReduceMemoryUsage() { return manager_.ReduceMemoryUsage(); }
131
105 private: 132 private:
106 MessageLoopForIO message_loop_; 133 MessageLoopForIO message_loop_;
107 internal::DiscardableMemoryManager manager_; 134 TestDiscardableMemoryManagerImpl manager_;
108 }; 135 };
109 136
110 class DiscardableMemoryManagerTest : public DiscardableMemoryManagerTestBase, 137 class DiscardableMemoryManagerTest : public DiscardableMemoryManagerTestBase,
111 public testing::Test { 138 public testing::Test {
112 public: 139 public:
113 DiscardableMemoryManagerTest() {} 140 DiscardableMemoryManagerTest() {}
114 }; 141 };
115 142
116 TEST_F(DiscardableMemoryManagerTest, CreateAndLock) { 143 TEST_F(DiscardableMemoryManagerTest, CreateAndLock) {
117 size_t size = 1024; 144 size_t size = 1024;
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after
385 EXPECT_EQ(1024u, BytesAllocated()); 412 EXPECT_EQ(1024u, BytesAllocated());
386 Unlock(&allocation); 413 Unlock(&allocation);
387 EXPECT_TRUE(CanBePurged(&allocation)); 414 EXPECT_TRUE(CanBePurged(&allocation));
388 SetMemoryLimit(0); 415 SetMemoryLimit(0);
389 EXPECT_EQ(0u, BytesAllocated()); 416 EXPECT_EQ(0u, BytesAllocated());
390 Unregister(&allocation); 417 Unregister(&allocation);
391 } 418 }
392 EXPECT_EQ(0u, BytesAllocated()); 419 EXPECT_EQ(0u, BytesAllocated());
393 } 420 }
394 421
422 TEST_F(DiscardableMemoryManagerTest, ReduceMemoryUsage) {
423 SetMemoryLimit(3072);
424 SetSoftMemoryLimit(1024);
425 SetHardMemoryLimitExpirationTime(TimeDelta::FromInternalValue(1));
426
427 size_t size = 1024;
428 TestAllocationImpl allocation[3];
429 RegisterAndLock(&allocation[0], size);
430 RegisterAndLock(&allocation[1], size);
431 RegisterAndLock(&allocation[2], size);
432 EXPECT_EQ(3072u, BytesAllocated());
433
434 // Above soft limit but nothing that can be purged.
435 EXPECT_FALSE(ReduceMemoryUsage());
436
437 SetNow(TimeTicks::FromInternalValue(0));
438 Unlock(&allocation[0]);
439
440 // Above soft limit but still nothing that can be purged as all unlocked
441 // allocations are within the hard limit cutoff time.
442 EXPECT_FALSE(ReduceMemoryUsage());
443
444 SetNow(TimeTicks::FromInternalValue(1));
445 Unlock(&allocation[1]);
446
447 // One unlocked allocation is no longer within the hard limit cutoff time. It
448 // should be purged and ReduceMemoryUsage() should return false as we're not
449 // yet within the soft memory limit.
450 EXPECT_FALSE(ReduceMemoryUsage());
451 EXPECT_EQ(2048u, BytesAllocated());
452
453 // One more unlocked allocation is no longer within the hard limit cutoff
454 // time. It should be purged and ReduceMemoryUsage() should return true as
455 // we're now within the soft memory limit.
456 SetNow(TimeTicks::FromInternalValue(2));
457 EXPECT_TRUE(ReduceMemoryUsage());
458 EXPECT_EQ(1024u, BytesAllocated());
459
460 Unlock(&allocation[2]);
461
462 Unregister(&allocation[0]);
463 Unregister(&allocation[1]);
464 Unregister(&allocation[2]);
465 }
466
395 class ThreadedDiscardableMemoryManagerTest 467 class ThreadedDiscardableMemoryManagerTest
396 : public DiscardableMemoryManagerTest { 468 : public DiscardableMemoryManagerTest {
397 public: 469 public:
398 ThreadedDiscardableMemoryManagerTest() 470 ThreadedDiscardableMemoryManagerTest()
399 : memory_usage_thread_("memory_usage_thread"), 471 : memory_usage_thread_("memory_usage_thread"),
400 thread_sync_(true, false) {} 472 thread_sync_(true, false) {}
401 473
402 virtual void SetUp() OVERRIDE { memory_usage_thread_.Start(); } 474 virtual void SetUp() OVERRIDE { memory_usage_thread_.Start(); }
403 475
404 virtual void TearDown() OVERRIDE { memory_usage_thread_.Stop(); } 476 virtual void TearDown() OVERRIDE { memory_usage_thread_.Stop(); }
(...skipping 19 matching lines...) Expand all
424 Unretained(this))); 496 Unretained(this)));
425 memory_usage_thread_.message_loop()->PostTask( 497 memory_usage_thread_.message_loop()->PostTask(
426 FROM_HERE, 498 FROM_HERE,
427 Bind(&ThreadedDiscardableMemoryManagerTest::SignalHelper, 499 Bind(&ThreadedDiscardableMemoryManagerTest::SignalHelper,
428 Unretained(this))); 500 Unretained(this)));
429 thread_sync_.Wait(); 501 thread_sync_.Wait();
430 } 502 }
431 503
432 } // namespace 504 } // namespace
433 } // namespace base 505 } // namespace base
OLDNEW
« no previous file with comments | « base/memory/discardable_memory_manager.cc ('k') | base/memory/discardable_memory_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698