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 #ifndef BASE_MEMORY_DISCARDABLE_MEMORY_MANAGER_H_ | 5 #ifndef BASE_MEMORY_DISCARDABLE_MEMORY_MANAGER_H_ |
6 #define BASE_MEMORY_DISCARDABLE_MEMORY_MANAGER_H_ | 6 #define BASE_MEMORY_DISCARDABLE_MEMORY_MANAGER_H_ |
7 | 7 |
8 #include "base/base_export.h" | 8 #include "base/base_export.h" |
9 #include "base/containers/hash_tables.h" | 9 #include "base/containers/hash_tables.h" |
10 #include "base/containers/mru_cache.h" | 10 #include "base/containers/mru_cache.h" |
11 #include "base/memory/memory_pressure_listener.h" | 11 #include "base/memory/memory_pressure_listener.h" |
12 #include "base/synchronization/lock.h" | 12 #include "base/synchronization/lock.h" |
| 13 #include "base/time/time.h" |
13 | 14 |
14 namespace base { | 15 namespace base { |
15 namespace internal { | 16 namespace internal { |
16 | 17 |
17 // This interface is used by the DiscardableMemoryManager class to provide some | 18 // This interface is used by the DiscardableMemoryManager class to provide some |
18 // level of userspace control over discardable memory allocations. | 19 // level of userspace control over discardable memory allocations. |
19 class DiscardableMemoryManagerAllocation { | 20 class DiscardableMemoryManagerAllocation { |
20 public: | 21 public: |
21 // Allocate and acquire a lock that prevents the allocation from being purged | 22 // Allocate and acquire a lock that prevents the allocation from being purged |
22 // by the system. Returns true if memory was previously allocated and is still | 23 // by the system. Returns true if memory was previously allocated and is still |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 // reaches the limit, the manager purges the LRU memory. | 62 // reaches the limit, the manager purges the LRU memory. |
62 // | 63 // |
63 // When notified of memory pressure, the manager either purges the LRU memory -- | 64 // When notified of memory pressure, the manager either purges the LRU memory -- |
64 // if the pressure is moderate -- or all discardable memory if the pressure is | 65 // if the pressure is moderate -- or all discardable memory if the pressure is |
65 // critical. | 66 // critical. |
66 class BASE_EXPORT_PRIVATE DiscardableMemoryManager { | 67 class BASE_EXPORT_PRIVATE DiscardableMemoryManager { |
67 public: | 68 public: |
68 typedef DiscardableMemoryManagerAllocation Allocation; | 69 typedef DiscardableMemoryManagerAllocation Allocation; |
69 | 70 |
70 DiscardableMemoryManager(size_t memory_limit, | 71 DiscardableMemoryManager(size_t memory_limit, |
71 size_t bytes_to_keep_under_moderate_pressure); | 72 size_t soft_memory_limit, |
72 ~DiscardableMemoryManager(); | 73 size_t bytes_to_keep_under_moderate_pressure, |
| 74 TimeDelta hard_memory_limit_expiration_time); |
| 75 virtual ~DiscardableMemoryManager(); |
73 | 76 |
74 // Call this to register memory pressure listener. Must be called on a thread | 77 // Call this to register memory pressure listener. Must be called on a thread |
75 // with a MessageLoop current. | 78 // with a MessageLoop current. |
76 void RegisterMemoryPressureListener(); | 79 void RegisterMemoryPressureListener(); |
77 | 80 |
78 // Call this to unregister memory pressure listener. | 81 // Call this to unregister memory pressure listener. |
79 void UnregisterMemoryPressureListener(); | 82 void UnregisterMemoryPressureListener(); |
80 | 83 |
81 // The maximum number of bytes of memory that may be allocated before we force | 84 // The maximum number of bytes of memory that may be allocated before we force |
82 // a purge. If this amount is zero, it is interpreted as having no limit at | 85 // a purge. |
83 // all. | |
84 void SetMemoryLimit(size_t bytes); | 86 void SetMemoryLimit(size_t bytes); |
85 | 87 |
| 88 // The number of bytes of memory that may be allocated but unused for the hard |
| 89 // limit expiration time without getting purged. |
| 90 void SetSoftMemoryLimit(size_t bytes); |
| 91 |
86 // Sets the amount of memory to keep when we're under moderate pressure. | 92 // Sets the amount of memory to keep when we're under moderate pressure. |
87 void SetBytesToKeepUnderModeratePressure(size_t bytes); | 93 void SetBytesToKeepUnderModeratePressure(size_t bytes); |
88 | 94 |
| 95 // Sets the memory usage cutoff time for hard memory limit. |
| 96 void SetHardMemoryLimitExpirationTime( |
| 97 TimeDelta hard_memory_limit_expiration_time); |
| 98 |
| 99 // This will attempt to reduce memory footprint until within soft memory |
| 100 // limit. Returns true if there's no need to call this again until allocations |
| 101 // have been used. |
| 102 bool ReduceMemoryUsage(); |
| 103 |
89 // Adds the given allocation to the manager's collection. | 104 // Adds the given allocation to the manager's collection. |
90 void Register(Allocation* allocation, size_t bytes); | 105 void Register(Allocation* allocation, size_t bytes); |
91 | 106 |
92 // Removes the given allocation from the manager's collection. | 107 // Removes the given allocation from the manager's collection. |
93 void Unregister(Allocation* allocation); | 108 void Unregister(Allocation* allocation); |
94 | 109 |
95 // Returns false if an error occurred. Otherwise, returns true and sets | 110 // Returns false if an error occurred. Otherwise, returns true and sets |
96 // |purged| to indicate whether or not allocation has been purged since last | 111 // |purged| to indicate whether or not allocation has been purged since last |
97 // use. | 112 // use. |
98 bool AcquireLock(Allocation* allocation, bool* purged); | 113 bool AcquireLock(Allocation* allocation, bool* purged); |
(...skipping 16 matching lines...) Expand all Loading... |
115 // Returns total amount of allocated discardable memory. This should only be | 130 // Returns total amount of allocated discardable memory. This should only be |
116 // used by tests. | 131 // used by tests. |
117 size_t GetBytesAllocatedForTest() const; | 132 size_t GetBytesAllocatedForTest() const; |
118 | 133 |
119 private: | 134 private: |
120 struct AllocationInfo { | 135 struct AllocationInfo { |
121 explicit AllocationInfo(size_t bytes) : bytes(bytes), purgable(false) {} | 136 explicit AllocationInfo(size_t bytes) : bytes(bytes), purgable(false) {} |
122 | 137 |
123 const size_t bytes; | 138 const size_t bytes; |
124 bool purgable; | 139 bool purgable; |
| 140 TimeTicks last_usage; |
125 }; | 141 }; |
126 typedef HashingMRUCache<Allocation*, AllocationInfo> AllocationMap; | 142 typedef HashingMRUCache<Allocation*, AllocationInfo> AllocationMap; |
127 | 143 |
128 // This can be called as a hint that the system is under memory pressure. | 144 // This can be called as a hint that the system is under memory pressure. |
129 void OnMemoryPressure( | 145 void OnMemoryPressure( |
130 MemoryPressureListener::MemoryPressureLevel pressure_level); | 146 MemoryPressureListener::MemoryPressureLevel pressure_level); |
131 | 147 |
132 // Purges memory until usage is within | 148 // Purges memory until usage is less or equal to |
133 // |bytes_to_keep_under_moderate_pressure_|. | 149 // |bytes_to_keep_under_moderate_pressure_|. |
134 void Purge(); | 150 void PurgeUntilWithinBytesToKeepUnderModeratePressure(); |
135 | 151 |
136 // Purges least recently used memory until usage is less or equal to |limit|. | 152 // Purges memory not used since |hard_memory_limit_expiration_time_| before |
| 153 // "right now" until usage is less or equal to |soft_memory_limit_|. |
| 154 // Returns true if total amount of memory is less or equal to soft memory |
| 155 // limit. |
| 156 bool PurgeIfNotUsedSinceHardLimitCutoffUntilWithinSoftMemoryLimit(); |
| 157 |
| 158 // Purges memory that has not been used since |timestamp| until usage is less |
| 159 // or equal to |limit|. |
137 // Caller must acquire |lock_| prior to calling this function. | 160 // Caller must acquire |lock_| prior to calling this function. |
138 void PurgeLRUWithLockAcquiredUntilUsageIsWithin(size_t limit); | 161 void PurgeIfNotUsedSinceTimestampUntilUsageIsWithinLimitWithLockAcquired( |
139 | 162 TimeTicks timestamp, |
140 // Ensures that we don't allocate beyond our memory limit. Caller must acquire | 163 size_t limit); |
141 // |lock_| prior to calling this function. | |
142 void EnforcePolicyWithLockAcquired(); | |
143 | 164 |
144 // Called when a change to |bytes_allocated_| has been made. | 165 // Called when a change to |bytes_allocated_| has been made. |
145 void BytesAllocatedChanged() const; | 166 void BytesAllocatedChanged(size_t new_bytes_allocated) const; |
| 167 |
| 168 // Virtual for tests. |
| 169 virtual TimeTicks Now() const; |
146 | 170 |
147 // Needs to be held when accessing members. | 171 // Needs to be held when accessing members. |
148 mutable Lock lock_; | 172 mutable Lock lock_; |
149 | 173 |
150 // A MRU cache of all allocated bits of memory. Used for purging. | 174 // A MRU cache of all allocated bits of memory. Used for purging. |
151 AllocationMap allocations_; | 175 AllocationMap allocations_; |
152 | 176 |
153 // The total amount of allocated memory. | 177 // The total amount of allocated memory. |
154 size_t bytes_allocated_; | 178 size_t bytes_allocated_; |
155 | 179 |
156 // The maximum number of bytes of memory that may be allocated. | 180 // The maximum number of bytes of memory that may be allocated. |
157 size_t memory_limit_; | 181 size_t memory_limit_; |
158 | 182 |
| 183 // The number of bytes of memory that may be allocated but not used for |
| 184 // |hard_memory_limit_expiration_time_| amount of time when receiving an idle |
| 185 // notification. |
| 186 size_t soft_memory_limit_; |
| 187 |
159 // Under moderate memory pressure, we will purge memory until usage is within | 188 // Under moderate memory pressure, we will purge memory until usage is within |
160 // this limit. | 189 // this limit. |
161 size_t bytes_to_keep_under_moderate_pressure_; | 190 size_t bytes_to_keep_under_moderate_pressure_; |
162 | 191 |
163 // Allows us to be respond when the system reports that it is under memory | 192 // Allows us to be respond when the system reports that it is under memory |
164 // pressure. | 193 // pressure. |
165 scoped_ptr<MemoryPressureListener> memory_pressure_listener_; | 194 scoped_ptr<MemoryPressureListener> memory_pressure_listener_; |
166 | 195 |
| 196 // Amount of time it takes for an allocation to become affected by |
| 197 // |soft_memory_limit_|. |
| 198 TimeDelta hard_memory_limit_expiration_time_; |
| 199 |
167 DISALLOW_COPY_AND_ASSIGN(DiscardableMemoryManager); | 200 DISALLOW_COPY_AND_ASSIGN(DiscardableMemoryManager); |
168 }; | 201 }; |
169 | 202 |
170 } // namespace internal | 203 } // namespace internal |
171 } // namespace base | 204 } // namespace base |
172 | 205 |
173 #endif // BASE_MEMORY_DISCARDABLE_MEMORY_MANAGER_H_ | 206 #endif // BASE_MEMORY_DISCARDABLE_MEMORY_MANAGER_H_ |
OLD | NEW |