Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef STORAGE_BROWSER_BLOB_BLOB_MEMORY_CONTROLLER_H_ | |
| 6 #define STORAGE_BROWSER_BLOB_BLOB_MEMORY_CONTROLLER_H_ | |
| 7 | |
| 8 #include <stdint.h> | |
| 9 | |
| 10 #include <list> | |
| 11 #include <map> | |
| 12 #include <memory> | |
| 13 #include <string> | |
| 14 #include <unordered_map> | |
| 15 #include <unordered_set> | |
| 16 #include <utility> | |
| 17 #include <vector> | |
| 18 | |
| 19 #include "base/callback.h" | |
| 20 #include "base/containers/mru_cache.h" | |
| 21 #include "base/files/file.h" | |
| 22 #include "base/files/file_path.h" | |
| 23 #include "base/macros.h" | |
| 24 #include "base/memory/ref_counted.h" | |
| 25 #include "base/memory/weak_ptr.h" | |
| 26 #include "base/optional.h" | |
| 27 #include "base/time/time.h" | |
| 28 #include "storage/browser/storage_browser_export.h" | |
| 29 #include "storage/common/blob_storage/blob_storage_constants.h" | |
| 30 | |
| 31 namespace base { | |
| 32 class TaskRunner; | |
| 33 } | |
| 34 | |
| 35 namespace storage { | |
| 36 class DataElement; | |
| 37 class ShareableBlobDataItem; | |
| 38 class ShareableFileReference; | |
| 39 | |
| 40 // This class's main responsability is deciding how blob data gets stored. | |
| 41 // This encompasses: | |
| 42 // * Keeping track of memory & file quota, | |
| 43 // * How to transport the blob data from the renderer (DetermineStrategy), | |
| 44 // * Allocating memory & file quota (ReserveMemoryQuota, ReserveFileQuota) | |
| 45 // * Freeing quota (MaybeFreeQuotaForItems) | |
| 46 // * Paging memory quota to disk when we're nearing our memory limit, and | |
| 47 // * Maintaining an LRU of memory items to choose candidates to page to disk | |
| 48 // (UpdateBlobItemInRecents, RemoveBlobItemInRecents). | |
| 49 class STORAGE_EXPORT BlobMemoryController { | |
| 50 public: | |
| 51 enum class Strategy { | |
| 52 // We don't have enough memory for this blob. | |
| 53 TOO_LARGE, | |
| 54 // There isn't any memory that needs transporting. | |
| 55 NONE_NEEDED, | |
| 56 // Transportation strategies. | |
| 57 IPC, | |
| 58 SHARED_MEMORY, | |
| 59 FILE | |
| 60 }; | |
| 61 | |
| 62 struct FileCreationInfo { | |
| 63 FileCreationInfo(); | |
| 64 ~FileCreationInfo(); | |
| 65 FileCreationInfo(FileCreationInfo&& other); | |
| 66 FileCreationInfo& operator=(FileCreationInfo&&); | |
| 67 | |
| 68 base::File::Error error = base::File::FILE_ERROR_FAILED; | |
| 69 scoped_refptr<ShareableFileReference> file_reference; | |
| 70 base::File file; | |
| 71 base::Time last_modified; | |
| 72 }; | |
| 73 | |
| 74 // The bool argument is true if we successfully received memory quota. | |
| 75 using MemoryQuotaRequestCallback = base::Callback<void(bool)>; | |
| 76 // The bool argument is true if we successfully received file quota, and the | |
| 77 // vector argument provides the file info. | |
| 78 using FileQuotaRequestCallback = | |
| 79 base::Callback<void(bool, std::vector<FileCreationInfo>)>; | |
| 80 | |
| 81 class QuotaAllocationTask { | |
|
michaeln
2016/09/27 00:09:29
thnx for adding this abstraction
dmurph
2016/09/29 00:44:22
Acknowledged.
| |
| 82 public: | |
| 83 virtual ~QuotaAllocationTask(); | |
|
michaeln
2016/09/27 00:09:29
looks like the caller should never delete these, i
dmurph
2016/09/29 00:44:22
Done. Making protected so subclasses can call it,
| |
| 84 // Operation is cancelled and the callback will not be called. This object | |
| 85 // will be destroyed by this call. | |
| 86 virtual void Cancel() = 0; | |
| 87 }; | |
| 88 | |
| 89 BlobMemoryController(); | |
| 90 virtual ~BlobMemoryController(); | |
| 91 | |
| 92 void EnableFilePaging(const base::FilePath& storage_directory, | |
|
michaeln
2016/09/27 00:09:30
I mentioned this on the larger cl too, but it got
dmurph
2016/09/29 00:44:22
Sounds good, I put these into the constructor, and
| |
| 93 scoped_refptr<base::TaskRunner> file_runner); | |
| 94 | |
| 95 // Disables the disk. This cancels all pending file creations and paging | |
| 96 // operations. | |
| 97 void DisableFilePaging(); | |
| 98 | |
| 99 bool file_paging_enabled() const { return file_paging_enabled_; } | |
| 100 | |
| 101 // Returns the strategy the transportation layer should use to transport the | |
| 102 // given memory. |preemptive_transported_bytes| are the number of transport | |
| 103 // bytes that are already populated for us, so we don't haved to request them | |
| 104 // from the renderer. | |
| 105 Strategy DetermineStrategy(size_t preemptive_transported_bytes, | |
| 106 uint64_t total_transportation_bytes) const; | |
| 107 | |
| 108 // Checks to see if we can reserve quota (disk or memory) for the given size. | |
| 109 bool CanReserveQuota(uint64_t size) const; | |
| 110 | |
| 111 // Reserves quota for the given |unreserved_memory_items|. The items must be | |
| 112 // bytes items in QUOTA_NEEDED state, which we change to QUOTA_REQUESTED. | |
| 113 // After we reserve memory quota we change their state to QUOTA_GRANTED and | |
| 114 // call |success_callback|. This can happen synchronously. | |
| 115 // Returns a task handle if the request is asynchronous for cancellation. | |
| 116 // NOTE: We don't inspect quota limits and assume the user checked | |
| 117 // CanReserveQuota before calling this. | |
| 118 base::WeakPtr<QuotaAllocationTask> ReserveMemoryQuota( | |
| 119 std::vector<ShareableBlobDataItem*> unreserved_memory_items, | |
| 120 const MemoryQuotaRequestCallback& success_callback); | |
| 121 | |
| 122 // Reserves quota for the given |unreserved_file_items|. The items must be | |
| 123 // temporary file items (BlobDataBuilder::IsTemporaryFileItem returns true) in | |
| 124 // QUOTA_NEEDED state, which we change to QUOTA_REQUESTED. After we reserve | |
| 125 // file quota we change their state to QUOTA_GRANTED and call | |
| 126 // |success_callback|. | |
| 127 // Returns a task handle for cancellation. | |
| 128 // NOTE: We don't inspect quota limits and assume the user checked | |
| 129 // CanReserveQuota before calling this. | |
| 130 base::WeakPtr<QuotaAllocationTask> ReserveFileQuota( | |
| 131 std::vector<ShareableBlobDataItem*> unreserved_file_items, | |
|
michaeln
2016/09/27 00:09:29
should these be vectors of scoped_refptrs?
dmurph
2016/09/29 00:44:22
Hmm.. Maybe we should, as we grab a reference anyw
| |
| 132 const FileQuotaRequestCallback& success_callback); | |
| 133 | |
| 134 // This frees quota for items that don't have any blob references. | |
|
michaeln
2016/09/27 00:09:29
the comment confused me a little, i think you mean
dmurph
2016/09/29 00:44:22
Done.
| |
| 135 void MaybeFreeMemoryQuotaForItems( | |
| 136 const std::vector<scoped_refptr<ShareableBlobDataItem>>& items); | |
| 137 // Forcibly frees memory quota for the given item. | |
| 138 void ForceFreeMemoryQuotaForItem( | |
| 139 const scoped_refptr<ShareableBlobDataItem>& item); | |
| 140 | |
| 141 // This adds or updates a bytes item in our LRU table used for paging items | |
| 142 // to disk. The item must have state POPULATED_WITH_QUOTA. | |
| 143 void UpdateBlobItemInRecents(ShareableBlobDataItem* item); | |
| 144 void RemoveBlobItemInRecents(const ShareableBlobDataItem& item); | |
|
michaeln
2016/09/28 01:08:04
is the remove call needed or could this happen int
dmurph
2016/09/29 00:44:23
Yes it can! woo!
| |
| 145 | |
| 146 size_t memory_usage() const { | |
| 147 return blob_memory_used_ + in_flight_memory_used_; | |
| 148 } | |
| 149 uint64_t disk_usage() const { return disk_used_; } | |
| 150 | |
| 151 const BlobStorageLimits& limits() const { return limits_; } | |
| 152 void SetLimitsForTesting(BlobStorageLimits limits) { limits_ = limits; } | |
| 153 | |
| 154 private: | |
| 155 template <typename T> | |
| 156 class BaseQuotaAllocationTask; | |
| 157 class FileQuotaAllocationTask; | |
| 158 class MemoryQuotaAllocationTask; | |
| 159 | |
| 160 using PendingMemoryQuotaTaskList = | |
| 161 std::list<std::unique_ptr<MemoryQuotaAllocationTask>>; | |
| 162 using PendingFileQuotaTaskList = | |
| 163 std::list<std::unique_ptr<FileQuotaAllocationTask>>; | |
| 164 | |
| 165 void MaybeGrantPendingQuotaRequests(); | |
| 166 | |
| 167 // Schedule paging until our memory usage is below our memory limit. | |
| 168 void MaybeSchedulePagingUntilSystemHealthy(); | |
| 169 | |
| 170 // Called when we've completed paging a list of items to disk. This is where | |
| 171 // we swap the bytes items for file items, and and update our bookkeeping. | |
| 172 void OnPagingComplete( | |
| 173 std::unique_ptr<std::vector<scoped_refptr<ShareableBlobDataItem>>> items, | |
| 174 size_t total_items_size, | |
| 175 FileCreationInfo result); | |
| 176 | |
| 177 size_t GetAvailableMemoryForBlobs() const; | |
| 178 uint64_t GetAvailableFileSpaceForBlobs() const; | |
| 179 | |
| 180 // This is registered as a callback for file deletions on the file reference | |
| 181 // of our paging files. We decrement the disk space used. | |
| 182 void OnBlobFileDelete(uint64_t size, const base::FilePath& path); | |
| 183 | |
| 184 void RecordTracingCounters() const; | |
|
pwnall
2016/09/26 13:18:09
From the perspective of code review / maintenance,
dmurph
2016/09/29 00:44:22
Done.
| |
| 185 | |
| 186 BlobStorageLimits limits_; | |
| 187 | |
| 188 // Memory bookkeeping. These numbers are all disjoint. | |
| 189 // This is the amount of memory we're using for blobs in RAM. | |
| 190 size_t blob_memory_used_ = 0; | |
| 191 // This is memory we're temporarily using while we try to write blob items to | |
| 192 // disk. | |
| 193 size_t in_flight_memory_used_ = 0; | |
| 194 // This is the amount of memory we're using on disk. | |
| 195 uint64_t disk_used_ = 0; | |
| 196 | |
| 197 uint64_t current_file_num_ = 0; | |
| 198 | |
| 199 size_t pending_memory_quota_total_size_ = 0; | |
| 200 PendingMemoryQuotaTaskList pending_memory_quota_tasks_; | |
| 201 PendingFileQuotaTaskList pending_file_quota_tasks_; | |
| 202 | |
| 203 size_t pending_pagings_ = 0; | |
| 204 | |
| 205 scoped_refptr<base::TaskRunner> file_runner_; | |
| 206 | |
| 207 bool file_paging_enabled_ = false; | |
| 208 base::FilePath blob_storage_dir_; | |
| 209 | |
| 210 // Lifetime of the ShareableBlobDataItem objects is handled externally in the | |
| 211 // BlobStorageContext class. | |
| 212 base::MRUCache<uint64_t, ShareableBlobDataItem*> recent_item_cache_; | |
| 213 size_t recent_item_cache_bytes_ = 0; | |
| 214 // We need to keep track of items currently being paged to disk so that if | |
| 215 // another blob successfully grabs a ref, we can prevent it from adding the | |
| 216 // item to the recent_item_cache_ above. | |
| 217 std::unordered_set<uint64_t> items_saving_to_disk_; | |
| 218 | |
| 219 base::WeakPtrFactory<BlobMemoryController> ptr_factory_; | |
|
michaeln
2016/09/28 01:08:04
consistency, typically in storage/browser/blob, th
dmurph
2016/09/29 00:44:22
Done.
| |
| 220 | |
| 221 DISALLOW_COPY_AND_ASSIGN(BlobMemoryController); | |
| 222 }; | |
| 223 } // namespace storage | |
| 224 #endif // STORAGE_BROWSER_BLOB_BLOB_MEMORY_CONTROLLER_H_ | |
| OLD | NEW |