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 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 std::vector<scoped_refptr<ShareableBlobDataItem>> unreserved_file_items, | 150 std::vector<scoped_refptr<ShareableBlobDataItem>> unreserved_file_items, |
151 const FileQuotaRequestCallback& done_callback); | 151 const FileQuotaRequestCallback& done_callback); |
152 | 152 |
153 // Called when initially populated or upon later access. | 153 // Called when initially populated or upon later access. |
154 void NotifyMemoryItemsUsed( | 154 void NotifyMemoryItemsUsed( |
155 const std::vector<scoped_refptr<ShareableBlobDataItem>>& items); | 155 const std::vector<scoped_refptr<ShareableBlobDataItem>>& items); |
156 | 156 |
157 size_t memory_usage() const { return blob_memory_used_; } | 157 size_t memory_usage() const { return blob_memory_used_; } |
158 uint64_t disk_usage() const { return disk_used_; } | 158 uint64_t disk_usage() const { return disk_used_; } |
159 | 159 |
| 160 // Schedules a task on the file runner to calculate blob storage quota limits. |
| 161 void CalculateBlobStorageLimits(); |
| 162 |
| 163 base::WeakPtr<BlobMemoryController> GetWeakPtr(); |
| 164 |
160 const BlobStorageLimits& limits() const { return limits_; } | 165 const BlobStorageLimits& limits() const { return limits_; } |
161 void set_limits_for_testing(const BlobStorageLimits& limits) { | 166 void set_limits_for_testing(const BlobStorageLimits& limits) { |
| 167 manual_limits_set_ = true; |
162 limits_ = limits; | 168 limits_ = limits; |
163 } | 169 } |
164 | 170 |
165 private: | 171 private: |
166 class FileQuotaAllocationTask; | 172 class FileQuotaAllocationTask; |
167 class MemoryQuotaAllocationTask; | 173 class MemoryQuotaAllocationTask; |
168 | 174 |
169 using PendingMemoryQuotaTaskList = | 175 using PendingMemoryQuotaTaskList = |
170 std::list<std::unique_ptr<MemoryQuotaAllocationTask>>; | 176 std::list<std::unique_ptr<MemoryQuotaAllocationTask>>; |
171 using PendingFileQuotaTaskList = | 177 using PendingFileQuotaTaskList = |
172 std::list<std::unique_ptr<FileQuotaAllocationTask>>; | 178 std::list<std::unique_ptr<FileQuotaAllocationTask>>; |
173 | 179 |
| 180 void OnStorageLimitsCalculated(BlobStorageLimits limits); |
| 181 |
| 182 void FreezeDiskUsage(uint64_t avail_disk_space); |
| 183 |
174 base::WeakPtr<QuotaAllocationTask> AppendMemoryTask( | 184 base::WeakPtr<QuotaAllocationTask> AppendMemoryTask( |
175 uint64_t total_bytes_needed, | 185 uint64_t total_bytes_needed, |
176 std::vector<scoped_refptr<ShareableBlobDataItem>> unreserved_memory_items, | 186 std::vector<scoped_refptr<ShareableBlobDataItem>> unreserved_memory_items, |
177 const MemoryQuotaRequestCallback& done_callback); | 187 const MemoryQuotaRequestCallback& done_callback); |
178 | 188 |
179 void MaybeGrantPendingMemoryRequests(); | 189 void MaybeGrantPendingMemoryRequests(); |
180 | 190 |
181 size_t CollectItemsForEviction( | 191 size_t CollectItemsForEviction( |
182 std::vector<scoped_refptr<ShareableBlobDataItem>>* output); | 192 std::vector<scoped_refptr<ShareableBlobDataItem>>* output); |
183 | 193 |
184 // Schedule paging until our memory usage is below our memory limit. | 194 // Schedule paging until our memory usage is below our memory limit. |
185 void MaybeScheduleEvictionUntilSystemHealthy(); | 195 void MaybeScheduleEvictionUntilSystemHealthy(); |
186 | 196 |
187 // Called when we've completed evicting a list of items to disk. This is where | 197 // Called when we've completed evicting a list of items to disk. This is where |
188 // we swap the bytes items for file items, and update our bookkeeping. | 198 // we swap the bytes items for file items, and update our bookkeeping. |
189 void OnEvictionComplete( | 199 void OnEvictionComplete( |
190 scoped_refptr<ShareableFileReference> file_reference, | 200 scoped_refptr<ShareableFileReference> file_reference, |
191 std::vector<scoped_refptr<ShareableBlobDataItem>> items, | 201 std::vector<scoped_refptr<ShareableBlobDataItem>> items, |
192 size_t total_items_size, | 202 size_t total_items_size, |
193 FileCreationInfo result); | 203 std::pair<FileCreationInfo, int64_t> result); |
194 | 204 |
195 size_t GetAvailableMemoryForBlobs() const; | 205 size_t GetAvailableMemoryForBlobs() const; |
196 uint64_t GetAvailableFileSpaceForBlobs() const; | 206 uint64_t GetAvailableFileSpaceForBlobs() const; |
197 | 207 |
198 void GrantMemoryAllocations( | 208 void GrantMemoryAllocations( |
199 std::vector<scoped_refptr<ShareableBlobDataItem>>* items, | 209 std::vector<scoped_refptr<ShareableBlobDataItem>>* items, |
200 size_t total_bytes); | 210 size_t total_bytes); |
201 void RevokeMemoryAllocation(uint64_t item_id, size_t length); | 211 void RevokeMemoryAllocation(uint64_t item_id, size_t length); |
202 | 212 |
203 // This is registered as a callback for file deletions on the file reference | 213 // This is registered as a callback for file deletions on the file reference |
204 // of our paging files. We decrement the disk space used. | 214 // of our paging files. We decrement the disk space used. |
205 void OnBlobFileDelete(uint64_t size, const base::FilePath& path); | 215 void OnBlobFileDelete(uint64_t size, const base::FilePath& path); |
206 | 216 |
207 base::FilePath GenerateNextPageFileName(); | 217 base::FilePath GenerateNextPageFileName(); |
208 | 218 |
209 // This records diagnostic counters of our memory quotas. Called when usage | 219 // This records diagnostic counters of our memory quotas. Called when usage |
210 // changes. | 220 // changes. |
211 void RecordTracingCounters() const; | 221 void RecordTracingCounters() const; |
212 | 222 |
| 223 // Store that we set manual limits so we don't accidentally override them with |
| 224 // our configuration task. |
| 225 bool manual_limits_set_ = false; |
213 BlobStorageLimits limits_; | 226 BlobStorageLimits limits_; |
214 | 227 |
215 // Memory bookkeeping. These numbers are all disjoint. | 228 // Memory bookkeeping. These numbers are all disjoint. |
216 // This is the amount of memory we're using for blobs in RAM, including the | 229 // This is the amount of memory we're using for blobs in RAM, including the |
217 // in_flight_memory_used_. | 230 // in_flight_memory_used_. |
218 size_t blob_memory_used_ = 0; | 231 size_t blob_memory_used_ = 0; |
219 // This is memory we're temporarily using while we try to write blob items to | 232 // This is memory we're temporarily using while we try to write blob items to |
220 // disk. | 233 // disk. |
221 size_t in_flight_memory_used_ = 0; | 234 size_t in_flight_memory_used_ = 0; |
222 // This is the amount of memory we're using on disk. | 235 // This is the amount of memory we're using on disk. |
(...skipping 20 matching lines...) Expand all Loading... |
243 // another blob successfully grabs a ref, we can prevent it from adding the | 256 // another blob successfully grabs a ref, we can prevent it from adding the |
244 // item to the recent_item_cache_ above. | 257 // item to the recent_item_cache_ above. |
245 std::unordered_set<uint64_t> items_paging_to_file_; | 258 std::unordered_set<uint64_t> items_paging_to_file_; |
246 | 259 |
247 base::WeakPtrFactory<BlobMemoryController> weak_factory_; | 260 base::WeakPtrFactory<BlobMemoryController> weak_factory_; |
248 | 261 |
249 DISALLOW_COPY_AND_ASSIGN(BlobMemoryController); | 262 DISALLOW_COPY_AND_ASSIGN(BlobMemoryController); |
250 }; | 263 }; |
251 } // namespace storage | 264 } // namespace storage |
252 #endif // STORAGE_BROWSER_BLOB_BLOB_MEMORY_CONTROLLER_H_ | 265 #endif // STORAGE_BROWSER_BLOB_BLOB_MEMORY_CONTROLLER_H_ |
OLD | NEW |