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