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 | |
164 private: | 177 private: |
165 class FileQuotaAllocationTask; | 178 class FileQuotaAllocationTask; |
166 class MemoryQuotaAllocationTask; | 179 class MemoryQuotaAllocationTask; |
167 | 180 |
181 // So this (and only this) class can call CalculateBlobStorageLimits(). | |
182 friend class content::ChromeBlobStorageContext; | |
183 | |
184 // Schedules a task on the file runner to calculate blob storage quota limits. | |
185 // This should only be called once per storage partition initialization as we | |
Mark P
2017/01/11 17:49:14
This comment isn't consistent with histograms.xml,
dmurph
2017/01/11 18:25:29
That's for "MaxDiskSpaceAdjustment". "MaxDiskSpace
Mark P
2017/01/11 20:45:24
Acknowledged.
Oops, sorry I mixed them up. /me s
| |
186 // emit UMA stats with that expectation. | |
187 void CalculateBlobStorageLimits(); | |
188 | |
168 using PendingMemoryQuotaTaskList = | 189 using PendingMemoryQuotaTaskList = |
169 std::list<std::unique_ptr<MemoryQuotaAllocationTask>>; | 190 std::list<std::unique_ptr<MemoryQuotaAllocationTask>>; |
170 using PendingFileQuotaTaskList = | 191 using PendingFileQuotaTaskList = |
171 std::list<std::unique_ptr<FileQuotaAllocationTask>>; | 192 std::list<std::unique_ptr<FileQuotaAllocationTask>>; |
172 | 193 |
194 void OnStorageLimitsCalculated(BlobStorageLimits limits); | |
195 | |
196 // Adjusts the effective disk usage based on the available space. We try to | |
197 // keep at least BlobSorageLimits::min_available_disk_space() free. | |
198 void AdjustDiskUsage(uint64_t avail_disk_space); | |
199 | |
173 base::WeakPtr<QuotaAllocationTask> AppendMemoryTask( | 200 base::WeakPtr<QuotaAllocationTask> AppendMemoryTask( |
174 uint64_t total_bytes_needed, | 201 uint64_t total_bytes_needed, |
175 std::vector<scoped_refptr<ShareableBlobDataItem>> unreserved_memory_items, | 202 std::vector<scoped_refptr<ShareableBlobDataItem>> unreserved_memory_items, |
176 const MemoryQuotaRequestCallback& done_callback); | 203 const MemoryQuotaRequestCallback& done_callback); |
177 | 204 |
178 void MaybeGrantPendingMemoryRequests(); | 205 void MaybeGrantPendingMemoryRequests(); |
179 | 206 |
180 size_t CollectItemsForEviction( | 207 size_t CollectItemsForEviction( |
181 std::vector<scoped_refptr<ShareableBlobDataItem>>* output); | 208 std::vector<scoped_refptr<ShareableBlobDataItem>>* output); |
182 | 209 |
183 // Schedule paging until our memory usage is below our memory limit. | 210 // Schedule paging until our memory usage is below our memory limit. |
184 void MaybeScheduleEvictionUntilSystemHealthy(); | 211 void MaybeScheduleEvictionUntilSystemHealthy(); |
185 | 212 |
186 // Called when we've completed evicting a list of items to disk. This is where | 213 // 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. | 214 // we swap the bytes items for file items, and update our bookkeeping. |
188 void OnEvictionComplete( | 215 void OnEvictionComplete( |
189 scoped_refptr<ShareableFileReference> file_reference, | 216 scoped_refptr<ShareableFileReference> file_reference, |
190 std::vector<scoped_refptr<ShareableBlobDataItem>> items, | 217 std::vector<scoped_refptr<ShareableBlobDataItem>> items, |
191 size_t total_items_size, | 218 size_t total_items_size, |
192 FileCreationInfo result); | 219 std::pair<FileCreationInfo, int64_t /* disk_avail */> result); |
193 | 220 |
194 size_t GetAvailableMemoryForBlobs() const; | 221 size_t GetAvailableMemoryForBlobs() const; |
195 uint64_t GetAvailableFileSpaceForBlobs() const; | 222 uint64_t GetAvailableFileSpaceForBlobs() const; |
196 | 223 |
197 void GrantMemoryAllocations( | 224 void GrantMemoryAllocations( |
198 std::vector<scoped_refptr<ShareableBlobDataItem>>* items, | 225 std::vector<scoped_refptr<ShareableBlobDataItem>>* items, |
199 size_t total_bytes); | 226 size_t total_bytes); |
200 void RevokeMemoryAllocation(uint64_t item_id, size_t length); | 227 void RevokeMemoryAllocation(uint64_t item_id, size_t length); |
201 | 228 |
202 // This is registered as a callback for file deletions on the file reference | 229 // This is registered as a callback for file deletions on the file reference |
203 // of our paging files. We decrement the disk space used. | 230 // of our paging files. We decrement the disk space used. |
204 void OnBlobFileDelete(uint64_t size, const base::FilePath& path); | 231 void OnBlobFileDelete(uint64_t size, const base::FilePath& path); |
205 | 232 |
206 base::FilePath GenerateNextPageFileName(); | 233 base::FilePath GenerateNextPageFileName(); |
207 | 234 |
208 // This records diagnostic counters of our memory quotas. Called when usage | 235 // This records diagnostic counters of our memory quotas. Called when usage |
209 // changes. | 236 // changes. |
210 void RecordTracingCounters() const; | 237 void RecordTracingCounters() const; |
211 | 238 |
239 // Store that we set manual limits so we don't accidentally override them with | |
240 // our configuration task. | |
241 bool manual_limits_set_ = false; | |
212 BlobStorageLimits limits_; | 242 BlobStorageLimits limits_; |
213 | 243 |
214 // Memory bookkeeping. These numbers are all disjoint. | 244 // Memory bookkeeping. These numbers are all disjoint. |
215 // This is the amount of memory we're using for blobs in RAM, including the | 245 // This is the amount of memory we're using for blobs in RAM, including the |
216 // in_flight_memory_used_. | 246 // in_flight_memory_used_. |
217 size_t blob_memory_used_ = 0; | 247 size_t blob_memory_used_ = 0; |
218 // This is memory we're temporarily using while we try to write blob items to | 248 // This is memory we're temporarily using while we try to write blob items to |
219 // disk. | 249 // disk. |
220 size_t in_flight_memory_used_ = 0; | 250 size_t in_flight_memory_used_ = 0; |
221 // This is the amount of memory we're using on disk. | 251 // This is the amount of memory we're using on disk. |
222 uint64_t disk_used_ = 0; | 252 uint64_t disk_used_ = 0; |
223 | 253 |
224 // State for GenerateNextPageFileName. | 254 // State for GenerateNextPageFileName. |
225 uint64_t current_file_num_ = 0; | 255 uint64_t current_file_num_ = 0; |
226 | 256 |
227 size_t pending_memory_quota_total_size_ = 0; | 257 size_t pending_memory_quota_total_size_ = 0; |
228 PendingMemoryQuotaTaskList pending_memory_quota_tasks_; | 258 PendingMemoryQuotaTaskList pending_memory_quota_tasks_; |
229 PendingFileQuotaTaskList pending_file_quota_tasks_; | 259 PendingFileQuotaTaskList pending_file_quota_tasks_; |
230 | 260 |
231 int pending_evictions_ = 0; | 261 int pending_evictions_ = 0; |
232 | 262 |
233 bool file_paging_enabled_ = false; | 263 bool file_paging_enabled_ = false; |
234 base::FilePath blob_storage_dir_; | 264 base::FilePath blob_storage_dir_; |
235 scoped_refptr<base::TaskRunner> file_runner_; | 265 scoped_refptr<base::TaskRunner> file_runner_; |
266 // This defaults to calling base::SysInfo::AmountOfFreeDiskSpace. | |
267 DiskSpaceFuncPtr disk_space_function_; | |
236 | 268 |
237 // Lifetime of the ShareableBlobDataItem objects is handled externally in the | 269 // Lifetime of the ShareableBlobDataItem objects is handled externally in the |
238 // BlobStorageContext class. | 270 // BlobStorageContext class. |
239 base::MRUCache<uint64_t, ShareableBlobDataItem*> populated_memory_items_; | 271 base::MRUCache<uint64_t, ShareableBlobDataItem*> populated_memory_items_; |
240 size_t populated_memory_items_bytes_ = 0; | 272 size_t populated_memory_items_bytes_ = 0; |
241 // We need to keep track of items currently being paged to disk so that if | 273 // 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 | 274 // another blob successfully grabs a ref, we can prevent it from adding the |
243 // item to the recent_item_cache_ above. | 275 // item to the recent_item_cache_ above. |
244 std::unordered_set<uint64_t> items_paging_to_file_; | 276 std::unordered_set<uint64_t> items_paging_to_file_; |
245 | 277 |
246 base::WeakPtrFactory<BlobMemoryController> weak_factory_; | 278 base::WeakPtrFactory<BlobMemoryController> weak_factory_; |
247 | 279 |
248 DISALLOW_COPY_AND_ASSIGN(BlobMemoryController); | 280 DISALLOW_COPY_AND_ASSIGN(BlobMemoryController); |
249 }; | 281 }; |
250 } // namespace storage | 282 } // namespace storage |
251 #endif // STORAGE_BROWSER_BLOB_BLOB_MEMORY_CONTROLLER_H_ | 283 #endif // STORAGE_BROWSER_BLOB_BLOB_MEMORY_CONTROLLER_H_ |
OLD | NEW |