OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 STORAGE_BROWSER_BLOB_BLOB_MEMORY_CONTROLLER_H_ | 5 #ifndef STORAGE_BROWSER_BLOB_BLOB_MEMORY_CONTROLLER_H_ |
6 #define STORAGE_BROWSER_BLOB_BLOB_MEMORY_CONTROLLER_H_ | 6 #define STORAGE_BROWSER_BLOB_BLOB_MEMORY_CONTROLLER_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <list> | 10 #include <list> |
11 #include <map> | 11 #include <map> |
12 #include <memory> | 12 #include <memory> |
13 #include <string> | 13 #include <string> |
14 #include <unordered_map> | 14 #include <unordered_map> |
15 #include <unordered_set> | 15 #include <unordered_set> |
16 #include <utility> | 16 #include <utility> |
17 #include <vector> | 17 #include <vector> |
18 | 18 |
19 #include "base/callback.h" | 19 #include "base/callback_forward.h" |
20 #include "base/callback_helpers.h" | 20 #include "base/callback_helpers.h" |
21 #include "base/containers/mru_cache.h" | 21 #include "base/containers/mru_cache.h" |
22 #include "base/files/file.h" | 22 #include "base/files/file.h" |
23 #include "base/files/file_path.h" | 23 #include "base/files/file_path.h" |
24 #include "base/macros.h" | 24 #include "base/macros.h" |
25 #include "base/memory/ref_counted.h" | 25 #include "base/memory/ref_counted.h" |
26 #include "base/memory/weak_ptr.h" | 26 #include "base/memory/weak_ptr.h" |
27 #include "base/optional.h" | 27 #include "base/optional.h" |
28 #include "base/time/time.h" | 28 #include "base/time/time.h" |
29 #include "storage/browser/storage_browser_export.h" | 29 #include "storage/browser/storage_browser_export.h" |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 std::vector<scoped_refptr<ShareableBlobDataItem>> unreserved_file_items, | 149 std::vector<scoped_refptr<ShareableBlobDataItem>> unreserved_file_items, |
150 const FileQuotaRequestCallback& done_callback); | 150 const FileQuotaRequestCallback& done_callback); |
151 | 151 |
152 // Called when initially populated or upon later access. | 152 // Called when initially populated or upon later access. |
153 void NotifyMemoryItemsUsed( | 153 void NotifyMemoryItemsUsed( |
154 const std::vector<scoped_refptr<ShareableBlobDataItem>>& items); | 154 const std::vector<scoped_refptr<ShareableBlobDataItem>>& items); |
155 | 155 |
156 size_t memory_usage() const { return blob_memory_used_; } | 156 size_t memory_usage() const { return blob_memory_used_; } |
157 uint64_t disk_usage() const { return disk_used_; } | 157 uint64_t disk_usage() const { return disk_used_; } |
158 | 158 |
| 159 // Schedules a task on the file runner to calculate blob storage quota limits. |
| 160 void CalculateBlobStorageLimits(); |
| 161 |
| 162 base::WeakPtr<BlobMemoryController> GetWeakPtr(); |
| 163 |
159 const BlobStorageLimits& limits() const { return limits_; } | 164 const BlobStorageLimits& limits() const { return limits_; } |
160 void set_limits_for_testing(const BlobStorageLimits& limits) { | 165 void set_limits_for_testing(const BlobStorageLimits& limits) { |
| 166 manual_limits_set_ = true; |
161 limits_ = limits; | 167 limits_ = limits; |
162 } | 168 } |
163 | 169 |
| 170 using DiskSpaceFuncPtr = int64_t(*)(const base::FilePath&); |
| 171 |
| 172 void set_testing_disk_space(DiskSpaceFuncPtr disk_space_function) { |
| 173 disk_space_function_ = disk_space_function; |
| 174 } |
| 175 |
164 private: | 176 private: |
165 class FileQuotaAllocationTask; | 177 class FileQuotaAllocationTask; |
166 class MemoryQuotaAllocationTask; | 178 class MemoryQuotaAllocationTask; |
167 | 179 |
168 using PendingMemoryQuotaTaskList = | 180 using PendingMemoryQuotaTaskList = |
169 std::list<std::unique_ptr<MemoryQuotaAllocationTask>>; | 181 std::list<std::unique_ptr<MemoryQuotaAllocationTask>>; |
170 using PendingFileQuotaTaskList = | 182 using PendingFileQuotaTaskList = |
171 std::list<std::unique_ptr<FileQuotaAllocationTask>>; | 183 std::list<std::unique_ptr<FileQuotaAllocationTask>>; |
172 | 184 |
| 185 void OnStorageLimitsCalculated(BlobStorageLimits limits); |
| 186 |
| 187 // Adjusts the effective disk usage based on the available space. We try to |
| 188 // keep at least BlobSorageLimits::min_available_disk_space() free. |
| 189 void AdjustDiskUsage(uint64_t avail_disk_space); |
| 190 |
173 base::WeakPtr<QuotaAllocationTask> AppendMemoryTask( | 191 base::WeakPtr<QuotaAllocationTask> AppendMemoryTask( |
174 uint64_t total_bytes_needed, | 192 uint64_t total_bytes_needed, |
175 std::vector<scoped_refptr<ShareableBlobDataItem>> unreserved_memory_items, | 193 std::vector<scoped_refptr<ShareableBlobDataItem>> unreserved_memory_items, |
176 const MemoryQuotaRequestCallback& done_callback); | 194 const MemoryQuotaRequestCallback& done_callback); |
177 | 195 |
178 void MaybeGrantPendingMemoryRequests(); | 196 void MaybeGrantPendingMemoryRequests(); |
179 | 197 |
180 size_t CollectItemsForEviction( | 198 size_t CollectItemsForEviction( |
181 std::vector<scoped_refptr<ShareableBlobDataItem>>* output); | 199 std::vector<scoped_refptr<ShareableBlobDataItem>>* output); |
182 | 200 |
183 // Schedule paging until our memory usage is below our memory limit. | 201 // Schedule paging until our memory usage is below our memory limit. |
184 void MaybeScheduleEvictionUntilSystemHealthy(); | 202 void MaybeScheduleEvictionUntilSystemHealthy(); |
185 | 203 |
186 // Called when we've completed evicting a list of items to disk. This is where | 204 // Called when we've completed evicting a list of items to disk. This is where |
187 // we swap the bytes items for file items, and update our bookkeeping. | 205 // we swap the bytes items for file items, and update our bookkeeping. |
188 void OnEvictionComplete( | 206 void OnEvictionComplete( |
189 scoped_refptr<ShareableFileReference> file_reference, | 207 scoped_refptr<ShareableFileReference> file_reference, |
190 std::vector<scoped_refptr<ShareableBlobDataItem>> items, | 208 std::vector<scoped_refptr<ShareableBlobDataItem>> items, |
191 size_t total_items_size, | 209 size_t total_items_size, |
192 FileCreationInfo result); | 210 std::pair<FileCreationInfo, int64_t> result); |
193 | 211 |
194 size_t GetAvailableMemoryForBlobs() const; | 212 size_t GetAvailableMemoryForBlobs() const; |
195 uint64_t GetAvailableFileSpaceForBlobs() const; | 213 uint64_t GetAvailableFileSpaceForBlobs() const; |
196 | 214 |
197 void GrantMemoryAllocations( | 215 void GrantMemoryAllocations( |
198 std::vector<scoped_refptr<ShareableBlobDataItem>>* items, | 216 std::vector<scoped_refptr<ShareableBlobDataItem>>* items, |
199 size_t total_bytes); | 217 size_t total_bytes); |
200 void RevokeMemoryAllocation(uint64_t item_id, size_t length); | 218 void RevokeMemoryAllocation(uint64_t item_id, size_t length); |
201 | 219 |
202 // This is registered as a callback for file deletions on the file reference | 220 // This is registered as a callback for file deletions on the file reference |
203 // of our paging files. We decrement the disk space used. | 221 // of our paging files. We decrement the disk space used. |
204 void OnBlobFileDelete(uint64_t size, const base::FilePath& path); | 222 void OnBlobFileDelete(uint64_t size, const base::FilePath& path); |
205 | 223 |
206 base::FilePath GenerateNextPageFileName(); | 224 base::FilePath GenerateNextPageFileName(); |
207 | 225 |
208 // This records diagnostic counters of our memory quotas. Called when usage | 226 // This records diagnostic counters of our memory quotas. Called when usage |
209 // changes. | 227 // changes. |
210 void RecordTracingCounters() const; | 228 void RecordTracingCounters() const; |
211 | 229 |
| 230 // Store that we set manual limits so we don't accidentally override them with |
| 231 // our configuration task. |
| 232 bool manual_limits_set_ = false; |
212 BlobStorageLimits limits_; | 233 BlobStorageLimits limits_; |
213 | 234 |
214 // Memory bookkeeping. These numbers are all disjoint. | 235 // Memory bookkeeping. These numbers are all disjoint. |
215 // This is the amount of memory we're using for blobs in RAM, including the | 236 // This is the amount of memory we're using for blobs in RAM, including the |
216 // in_flight_memory_used_. | 237 // in_flight_memory_used_. |
217 size_t blob_memory_used_ = 0; | 238 size_t blob_memory_used_ = 0; |
218 // This is memory we're temporarily using while we try to write blob items to | 239 // This is memory we're temporarily using while we try to write blob items to |
219 // disk. | 240 // disk. |
220 size_t in_flight_memory_used_ = 0; | 241 size_t in_flight_memory_used_ = 0; |
221 // This is the amount of memory we're using on disk. | 242 // This is the amount of memory we're using on disk. |
222 uint64_t disk_used_ = 0; | 243 uint64_t disk_used_ = 0; |
223 | 244 |
224 // State for GenerateNextPageFileName. | 245 // State for GenerateNextPageFileName. |
225 uint64_t current_file_num_ = 0; | 246 uint64_t current_file_num_ = 0; |
226 | 247 |
227 size_t pending_memory_quota_total_size_ = 0; | 248 size_t pending_memory_quota_total_size_ = 0; |
228 PendingMemoryQuotaTaskList pending_memory_quota_tasks_; | 249 PendingMemoryQuotaTaskList pending_memory_quota_tasks_; |
229 PendingFileQuotaTaskList pending_file_quota_tasks_; | 250 PendingFileQuotaTaskList pending_file_quota_tasks_; |
230 | 251 |
231 int pending_evictions_ = 0; | 252 int pending_evictions_ = 0; |
232 | 253 |
233 bool file_paging_enabled_ = false; | 254 bool file_paging_enabled_ = false; |
234 base::FilePath blob_storage_dir_; | 255 base::FilePath blob_storage_dir_; |
235 scoped_refptr<base::TaskRunner> file_runner_; | 256 scoped_refptr<base::TaskRunner> file_runner_; |
| 257 // This defaults to calling base::SysInfo::AmountOfFreeDiskSpace. |
| 258 DiskSpaceFuncPtr disk_space_function_; |
236 | 259 |
237 // Lifetime of the ShareableBlobDataItem objects is handled externally in the | 260 // Lifetime of the ShareableBlobDataItem objects is handled externally in the |
238 // BlobStorageContext class. | 261 // BlobStorageContext class. |
239 base::MRUCache<uint64_t, ShareableBlobDataItem*> populated_memory_items_; | 262 base::MRUCache<uint64_t, ShareableBlobDataItem*> populated_memory_items_; |
240 size_t populated_memory_items_bytes_ = 0; | 263 size_t populated_memory_items_bytes_ = 0; |
241 // We need to keep track of items currently being paged to disk so that if | 264 // We need to keep track of items currently being paged to disk so that if |
242 // another blob successfully grabs a ref, we can prevent it from adding the | 265 // another blob successfully grabs a ref, we can prevent it from adding the |
243 // item to the recent_item_cache_ above. | 266 // item to the recent_item_cache_ above. |
244 std::unordered_set<uint64_t> items_paging_to_file_; | 267 std::unordered_set<uint64_t> items_paging_to_file_; |
245 | 268 |
246 base::WeakPtrFactory<BlobMemoryController> weak_factory_; | 269 base::WeakPtrFactory<BlobMemoryController> weak_factory_; |
247 | 270 |
248 DISALLOW_COPY_AND_ASSIGN(BlobMemoryController); | 271 DISALLOW_COPY_AND_ASSIGN(BlobMemoryController); |
249 }; | 272 }; |
250 } // namespace storage | 273 } // namespace storage |
251 #endif // STORAGE_BROWSER_BLOB_BLOB_MEMORY_CONTROLLER_H_ | 274 #endif // STORAGE_BROWSER_BLOB_BLOB_MEMORY_CONTROLLER_H_ |
OLD | NEW |