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" |
30 #include "storage/common/blob_storage/blob_storage_constants.h" | 30 #include "storage/common/blob_storage/blob_storage_constants.h" |
31 | 31 |
32 namespace base { | 32 namespace base { |
33 class TaskRunner; | 33 class TaskRunner; |
34 } | 34 } |
35 | 35 |
36 namespace content { | |
37 class ChromeBlobStorageContext; | |
38 } | |
39 | |
36 namespace storage { | 40 namespace storage { |
37 class ShareableBlobDataItem; | 41 class ShareableBlobDataItem; |
38 class ShareableFileReference; | 42 class ShareableFileReference; |
39 | 43 |
40 // This class's main responsibility is deciding how blob data gets stored. | 44 // This class's main responsibility is deciding how blob data gets stored. |
41 // This encompasses: | 45 // This encompasses: |
42 // * Keeping track of memory & file quota, | 46 // * Keeping track of memory & file quota, |
43 // * How to transport the blob data from the renderer (DetermineStrategy), | 47 // * How to transport the blob data from the renderer (DetermineStrategy), |
44 // * Allocating memory & file quota (ReserveMemoryQuota, ReserveFileQuota) | 48 // * Allocating memory & file quota (ReserveMemoryQuota, ReserveFileQuota) |
45 // * Paging memory quota to disk when we're nearing our memory limit, and | 49 // * Paging memory quota to disk when we're nearing our memory limit, and |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
149 std::vector<scoped_refptr<ShareableBlobDataItem>> unreserved_file_items, | 153 std::vector<scoped_refptr<ShareableBlobDataItem>> unreserved_file_items, |
150 const FileQuotaRequestCallback& done_callback); | 154 const FileQuotaRequestCallback& done_callback); |
151 | 155 |
152 // Called when initially populated or upon later access. | 156 // Called when initially populated or upon later access. |
153 void NotifyMemoryItemsUsed( | 157 void NotifyMemoryItemsUsed( |
154 const std::vector<scoped_refptr<ShareableBlobDataItem>>& items); | 158 const std::vector<scoped_refptr<ShareableBlobDataItem>>& items); |
155 | 159 |
156 size_t memory_usage() const { return blob_memory_used_; } | 160 size_t memory_usage() const { return blob_memory_used_; } |
157 uint64_t disk_usage() const { return disk_used_; } | 161 uint64_t disk_usage() const { return disk_used_; } |
158 | 162 |
163 base::WeakPtr<BlobMemoryController> GetWeakPtr(); | |
164 | |
159 const BlobStorageLimits& limits() const { return limits_; } | 165 const BlobStorageLimits& limits() const { return limits_; } |
160 void set_limits_for_testing(const BlobStorageLimits& limits) { | 166 void set_limits_for_testing(const BlobStorageLimits& limits) { |
167 manual_limits_set_ = true; | |
161 limits_ = limits; | 168 limits_ = limits; |
162 } | 169 } |
163 | 170 |
171 using DiskSpaceFuncPtr = int64_t (*)(const base::FilePath&); | |
172 | |
173 void set_testing_disk_space(DiskSpaceFuncPtr disk_space_function) { | |
174 disk_space_function_ = disk_space_function; | |
175 } | |
176 | |
177 protected: | |
Mark P
2017/01/10 23:57:00
nit: why did you add this?
dmurph
2017/01/11 01:49:22
rmeoved.
| |
164 private: | 178 private: |
165 class FileQuotaAllocationTask; | 179 class FileQuotaAllocationTask; |
166 class MemoryQuotaAllocationTask; | 180 class MemoryQuotaAllocationTask; |
167 | 181 |
182 // So this (and only this) class can call CalculateBlobStorageLimits(). | |
183 friend class content::ChromeBlobStorageContext; | |
184 | |
185 // Schedules a task on the file runner to calculate blob storage quota limits. | |
186 // This should not be called more than once in a browser run as we record UMA | |
Mark P
2017/01/10 23:57:00
"A browser run" sounds to me like something that c
dmurph
2017/01/11 01:49:22
Done.
| |
187 // stats that we expect to only be called once. | |
Mark P
2017/01/10 23:57:00
nit: called -> emitted
(or recorded)
dmurph
2017/01/11 01:49:22
Done.
| |
188 void CalculateBlobStorageLimits(); | |
189 | |
168 using PendingMemoryQuotaTaskList = | 190 using PendingMemoryQuotaTaskList = |
169 std::list<std::unique_ptr<MemoryQuotaAllocationTask>>; | 191 std::list<std::unique_ptr<MemoryQuotaAllocationTask>>; |
170 using PendingFileQuotaTaskList = | 192 using PendingFileQuotaTaskList = |
171 std::list<std::unique_ptr<FileQuotaAllocationTask>>; | 193 std::list<std::unique_ptr<FileQuotaAllocationTask>>; |
172 | 194 |
195 void OnStorageLimitsCalculated(BlobStorageLimits limits); | |
196 | |
197 // Adjusts the effective disk usage based on the available space. We try to | |
198 // keep at least BlobSorageLimits::min_available_disk_space() free. | |
199 void AdjustDiskUsage(uint64_t avail_disk_space); | |
200 | |
173 base::WeakPtr<QuotaAllocationTask> AppendMemoryTask( | 201 base::WeakPtr<QuotaAllocationTask> AppendMemoryTask( |
174 uint64_t total_bytes_needed, | 202 uint64_t total_bytes_needed, |
175 std::vector<scoped_refptr<ShareableBlobDataItem>> unreserved_memory_items, | 203 std::vector<scoped_refptr<ShareableBlobDataItem>> unreserved_memory_items, |
176 const MemoryQuotaRequestCallback& done_callback); | 204 const MemoryQuotaRequestCallback& done_callback); |
177 | 205 |
178 void MaybeGrantPendingMemoryRequests(); | 206 void MaybeGrantPendingMemoryRequests(); |
179 | 207 |
180 size_t CollectItemsForEviction( | 208 size_t CollectItemsForEviction( |
181 std::vector<scoped_refptr<ShareableBlobDataItem>>* output); | 209 std::vector<scoped_refptr<ShareableBlobDataItem>>* output); |
182 | 210 |
183 // Schedule paging until our memory usage is below our memory limit. | 211 // Schedule paging until our memory usage is below our memory limit. |
184 void MaybeScheduleEvictionUntilSystemHealthy(); | 212 void MaybeScheduleEvictionUntilSystemHealthy(); |
185 | 213 |
186 // Called when we've completed evicting a list of items to disk. This is where | 214 // 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. | 215 // we swap the bytes items for file items, and update our bookkeeping. |
188 void OnEvictionComplete( | 216 void OnEvictionComplete( |
189 scoped_refptr<ShareableFileReference> file_reference, | 217 scoped_refptr<ShareableFileReference> file_reference, |
190 std::vector<scoped_refptr<ShareableBlobDataItem>> items, | 218 std::vector<scoped_refptr<ShareableBlobDataItem>> items, |
191 size_t total_items_size, | 219 size_t total_items_size, |
192 FileCreationInfo result); | 220 std::pair<FileCreationInfo, int64_t /* disk_avail */> result); |
193 | 221 |
194 size_t GetAvailableMemoryForBlobs() const; | 222 size_t GetAvailableMemoryForBlobs() const; |
195 uint64_t GetAvailableFileSpaceForBlobs() const; | 223 uint64_t GetAvailableFileSpaceForBlobs() const; |
196 | 224 |
197 void GrantMemoryAllocations( | 225 void GrantMemoryAllocations( |
198 std::vector<scoped_refptr<ShareableBlobDataItem>>* items, | 226 std::vector<scoped_refptr<ShareableBlobDataItem>>* items, |
199 size_t total_bytes); | 227 size_t total_bytes); |
200 void RevokeMemoryAllocation(uint64_t item_id, size_t length); | 228 void RevokeMemoryAllocation(uint64_t item_id, size_t length); |
201 | 229 |
202 // This is registered as a callback for file deletions on the file reference | 230 // This is registered as a callback for file deletions on the file reference |
203 // of our paging files. We decrement the disk space used. | 231 // of our paging files. We decrement the disk space used. |
204 void OnBlobFileDelete(uint64_t size, const base::FilePath& path); | 232 void OnBlobFileDelete(uint64_t size, const base::FilePath& path); |
205 | 233 |
206 base::FilePath GenerateNextPageFileName(); | 234 base::FilePath GenerateNextPageFileName(); |
207 | 235 |
208 // This records diagnostic counters of our memory quotas. Called when usage | 236 // This records diagnostic counters of our memory quotas. Called when usage |
209 // changes. | 237 // changes. |
210 void RecordTracingCounters() const; | 238 void RecordTracingCounters() const; |
211 | 239 |
240 // Store that we set manual limits so we don't accidentally override them with | |
241 // our configuration task. | |
242 bool manual_limits_set_ = false; | |
212 BlobStorageLimits limits_; | 243 BlobStorageLimits limits_; |
213 | 244 |
214 // Memory bookkeeping. These numbers are all disjoint. | 245 // Memory bookkeeping. These numbers are all disjoint. |
215 // This is the amount of memory we're using for blobs in RAM, including the | 246 // This is the amount of memory we're using for blobs in RAM, including the |
216 // in_flight_memory_used_. | 247 // in_flight_memory_used_. |
217 size_t blob_memory_used_ = 0; | 248 size_t blob_memory_used_ = 0; |
218 // This is memory we're temporarily using while we try to write blob items to | 249 // This is memory we're temporarily using while we try to write blob items to |
219 // disk. | 250 // disk. |
220 size_t in_flight_memory_used_ = 0; | 251 size_t in_flight_memory_used_ = 0; |
221 // This is the amount of memory we're using on disk. | 252 // This is the amount of memory we're using on disk. |
222 uint64_t disk_used_ = 0; | 253 uint64_t disk_used_ = 0; |
223 | 254 |
224 // State for GenerateNextPageFileName. | 255 // State for GenerateNextPageFileName. |
225 uint64_t current_file_num_ = 0; | 256 uint64_t current_file_num_ = 0; |
226 | 257 |
227 size_t pending_memory_quota_total_size_ = 0; | 258 size_t pending_memory_quota_total_size_ = 0; |
228 PendingMemoryQuotaTaskList pending_memory_quota_tasks_; | 259 PendingMemoryQuotaTaskList pending_memory_quota_tasks_; |
229 PendingFileQuotaTaskList pending_file_quota_tasks_; | 260 PendingFileQuotaTaskList pending_file_quota_tasks_; |
230 | 261 |
231 int pending_evictions_ = 0; | 262 int pending_evictions_ = 0; |
232 | 263 |
233 bool file_paging_enabled_ = false; | 264 bool file_paging_enabled_ = false; |
234 base::FilePath blob_storage_dir_; | 265 base::FilePath blob_storage_dir_; |
235 scoped_refptr<base::TaskRunner> file_runner_; | 266 scoped_refptr<base::TaskRunner> file_runner_; |
267 // This defaults to calling base::SysInfo::AmountOfFreeDiskSpace. | |
268 DiskSpaceFuncPtr disk_space_function_; | |
236 | 269 |
237 // Lifetime of the ShareableBlobDataItem objects is handled externally in the | 270 // Lifetime of the ShareableBlobDataItem objects is handled externally in the |
238 // BlobStorageContext class. | 271 // BlobStorageContext class. |
239 base::MRUCache<uint64_t, ShareableBlobDataItem*> populated_memory_items_; | 272 base::MRUCache<uint64_t, ShareableBlobDataItem*> populated_memory_items_; |
240 size_t populated_memory_items_bytes_ = 0; | 273 size_t populated_memory_items_bytes_ = 0; |
241 // We need to keep track of items currently being paged to disk so that if | 274 // 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 | 275 // another blob successfully grabs a ref, we can prevent it from adding the |
243 // item to the recent_item_cache_ above. | 276 // item to the recent_item_cache_ above. |
244 std::unordered_set<uint64_t> items_paging_to_file_; | 277 std::unordered_set<uint64_t> items_paging_to_file_; |
245 | 278 |
246 base::WeakPtrFactory<BlobMemoryController> weak_factory_; | 279 base::WeakPtrFactory<BlobMemoryController> weak_factory_; |
247 | 280 |
248 DISALLOW_COPY_AND_ASSIGN(BlobMemoryController); | 281 DISALLOW_COPY_AND_ASSIGN(BlobMemoryController); |
249 }; | 282 }; |
250 } // namespace storage | 283 } // namespace storage |
251 #endif // STORAGE_BROWSER_BLOB_BLOB_MEMORY_CONTROLLER_H_ | 284 #endif // STORAGE_BROWSER_BLOB_BLOB_MEMORY_CONTROLLER_H_ |
OLD | NEW |