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/callback_helpers.h" | |
21 #include "base/containers/mru_cache.h" | |
22 #include "base/files/file.h" | |
23 #include "base/files/file_path.h" | |
24 #include "base/macros.h" | |
25 #include "base/memory/ref_counted.h" | |
26 #include "base/memory/weak_ptr.h" | |
27 #include "base/optional.h" | |
28 #include "base/time/time.h" | |
29 #include "storage/browser/storage_browser_export.h" | |
30 #include "storage/common/blob_storage/blob_storage_constants.h" | |
31 | |
32 namespace base { | |
33 class TaskRunner; | |
34 } | |
35 | |
36 namespace storage { | |
37 class DataElement; | |
38 class ShareableBlobDataItem; | |
39 class ShareableFileReference; | |
40 | |
41 // This class's main responsibility is deciding how blob data gets stored. | |
42 // This encompasses: | |
43 // * Keeping track of memory & file quota, | |
44 // * How to transport the blob data from the renderer (DetermineStrategy), | |
45 // * Allocating memory & file quota (ReserveMemoryQuota, ReserveFileQuota) | |
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 // (NotifyMemoryItemUsed). | |
49 // This class can only be interacted with on the IO thread. | |
50 class STORAGE_EXPORT BlobMemoryController { | |
51 public: | |
52 enum class Strategy { | |
53 // We don't have enough memory for this blob. | |
54 TOO_LARGE, | |
55 // There isn't any memory that needs transporting. | |
56 NONE_NEEDED, | |
57 // Transportation strategies. | |
58 IPC, | |
59 SHARED_MEMORY, | |
60 FILE | |
61 }; | |
62 | |
63 struct STORAGE_EXPORT FileCreationInfo { | |
64 FileCreationInfo(); | |
65 ~FileCreationInfo(); | |
66 FileCreationInfo(FileCreationInfo&& other); | |
67 FileCreationInfo& operator=(FileCreationInfo&&); | |
68 | |
69 base::File::Error error = base::File::FILE_ERROR_FAILED; | |
70 base::File file; | |
71 scoped_refptr<base::TaskRunner> file_deletion_runner; | |
72 base::FilePath path; | |
73 scoped_refptr<ShareableFileReference> file_reference; | |
74 base::Time last_modified; | |
75 }; | |
76 | |
77 struct MemoryAllocation { | |
78 MemoryAllocation(base::WeakPtr<BlobMemoryController> controller, | |
79 uint64_t item_id, | |
80 size_t length); | |
81 ~MemoryAllocation(); | |
82 | |
83 private: | |
84 base::WeakPtr<BlobMemoryController> controller; | |
85 uint64_t item_id; | |
86 size_t length; | |
87 | |
88 DISALLOW_COPY_AND_ASSIGN(MemoryAllocation); | |
89 }; | |
90 | |
91 class QuotaAllocationTask { | |
92 public: | |
93 // Operation is cancelled and the callback will NOT be called. This object | |
94 // will be destroyed by this call. | |
95 virtual void Cancel() = 0; | |
96 | |
97 protected: | |
98 virtual ~QuotaAllocationTask(); | |
99 }; | |
100 | |
101 // The bool argument is true if we successfully received memory quota. | |
102 using MemoryQuotaRequestCallback = base::Callback<void(bool)>; | |
103 // The bool argument is true if we successfully received file quota, and the | |
104 // vector argument provides the file info. | |
105 using FileQuotaRequestCallback = | |
106 base::Callback<void(bool, std::vector<FileCreationInfo>)>; | |
107 | |
108 // We enable file paging if |file_runner| isn't a nullptr. | |
109 BlobMemoryController(const base::FilePath& storage_directory, | |
110 scoped_refptr<base::TaskRunner> file_runner); | |
111 virtual ~BlobMemoryController(); | |
michaeln
2016/10/18 00:56:24
does this need to be virtual?
dmurph
2016/10/18 20:24:45
Done.
| |
112 | |
113 // Disables file paging. This cancels all pending file creations and paging | |
114 // operations. Reason is recorded in UMA. | |
115 void DisableFilePaging(base::File::Error reason); | |
116 | |
117 bool file_paging_enabled() const { return file_paging_enabled_; } | |
118 | |
119 // Returns the strategy the transportation layer should use to transport the | |
120 // given memory. |preemptive_transported_bytes| are the number of transport | |
121 // bytes that are already populated for us, so we don't haved to request them | |
122 // from the renderer. | |
123 Strategy DetermineStrategy(size_t preemptive_transported_bytes, | |
124 uint64_t total_transportation_bytes) const; | |
125 | |
126 // Checks to see if we can reserve quota (disk or memory) for the given size. | |
127 bool CanReserveQuota(uint64_t size) const; | |
128 | |
129 // Reserves quota for the given |unreserved_memory_items|. The items must be | |
130 // bytes items in QUOTA_NEEDED state, which we change to QUOTA_REQUESTED. | |
131 // After we reserve memory quota we change their state to QUOTA_GRANTED and | |
132 // call |done_callback|. This can happen synchronously. | |
michaeln
2016/10/18 00:56:24
might mention passing ownership of a MemoryAllocat
dmurph
2016/10/18 20:24:45
Done.
| |
133 // Returns a task handle if the request is asynchronous for cancellation. | |
134 // NOTE: We don't inspect quota limits and assume the user checked | |
135 // CanReserveQuota before calling this. | |
136 base::WeakPtr<QuotaAllocationTask> ReserveMemoryQuota( | |
137 std::vector<scoped_refptr<ShareableBlobDataItem>> unreserved_memory_items, | |
138 const MemoryQuotaRequestCallback& done_callback); | |
139 | |
140 // Reserves quota for the given |unreserved_file_items|. The items must be | |
141 // temporary file items (BlobDataBuilder::IsTemporaryFileItem returns true) in | |
142 // QUOTA_NEEDED state, which we change to QUOTA_REQUESTED. After we reserve | |
143 // file quota we change their state to QUOTA_GRANTED and call | |
144 // |done_callback|. | |
145 // Returns a task handle for cancellation. | |
146 // NOTE: We don't inspect quota limits and assume the user checked | |
147 // CanReserveQuota before calling this. | |
148 base::WeakPtr<QuotaAllocationTask> ReserveFileQuota( | |
149 std::vector<scoped_refptr<ShareableBlobDataItem>> unreserved_file_items, | |
150 const FileQuotaRequestCallback& done_callback); | |
151 | |
152 // Called when initially populated or upon later access. | |
153 void NotifyMemoryItemsUsed( | |
154 std::vector<scoped_refptr<ShareableBlobDataItem>>& items); | |
155 | |
156 size_t memory_usage() const { return blob_memory_used_; } | |
157 uint64_t disk_usage() const { return disk_used_; } | |
158 | |
159 const BlobStorageLimits& limits() const { return limits_; } | |
160 void set_limits_for_testing(const BlobStorageLimits& limits) { | |
161 limits_ = limits; | |
162 } | |
163 | |
164 private: | |
165 template <typename T> | |
166 class BaseQuotaAllocationTask; | |
167 class FileQuotaAllocationTask; | |
168 class MemoryQuotaAllocationTask; | |
169 | |
170 using PendingMemoryQuotaTaskList = | |
171 std::list<std::unique_ptr<MemoryQuotaAllocationTask>>; | |
172 using PendingFileQuotaTaskList = | |
173 std::list<std::unique_ptr<FileQuotaAllocationTask>>; | |
174 | |
175 base::WeakPtr<QuotaAllocationTask> AppendMemoryTask( | |
176 uint64_t total_bytes_needed, | |
177 std::vector<scoped_refptr<ShareableBlobDataItem>> unreserved_memory_items, | |
178 const MemoryQuotaRequestCallback& done_callback); | |
179 | |
180 void MaybeGrantPendingMemoryRequests(); | |
181 | |
182 size_t CollectItemsForEviction( | |
183 std::vector<scoped_refptr<ShareableBlobDataItem>>* output); | |
184 | |
185 // Schedule paging until our memory usage is below our memory limit. | |
186 void MaybeScheduleEvictionUntilSystemHealthy(); | |
187 | |
188 // Called when we've completed evicting a list of items to disk. This is where | |
189 // we swap the bytes items for file items, and and update our bookkeeping. | |
190 void OnEvictionComplete( | |
191 scoped_refptr<ShareableFileReference> file_reference, | |
192 std::vector<scoped_refptr<ShareableBlobDataItem>> items, | |
193 size_t total_items_size, | |
194 FileCreationInfo result); | |
195 | |
196 size_t GetAvailableMemoryForBlobs() const; | |
197 uint64_t GetAvailableFileSpaceForBlobs() const; | |
198 | |
199 void GrantMemoryAllocations( | |
200 std::vector<scoped_refptr<ShareableBlobDataItem>>* items); | |
201 void RevokeMemoryAllocation(uint64_t item_id, size_t length); | |
202 | |
203 // This is registered as a callback for file deletions on the file reference | |
204 // of our paging files. We decrement the disk space used. | |
205 void OnBlobFileDelete(uint64_t size, const base::FilePath& path); | |
206 | |
207 base::FilePath GenerateNextPageFileName(); | |
208 | |
209 // This records diagnostic counters of our memory quotas. Called when usage | |
210 // changes. | |
211 void RecordTracingCounters() const; | |
212 | |
213 BlobStorageLimits limits_; | |
214 | |
215 // Memory bookkeeping. These numbers are all disjoint. | |
216 // This is the amount of memory we're using for blobs in RAM, including the | |
217 // in_flight_memory_used_. | |
218 size_t blob_memory_used_ = 0; | |
219 // This is memory we're temporarily using while we try to write blob items to | |
220 // disk. | |
221 size_t in_flight_memory_used_ = 0; | |
222 // This is the amount of memory we're using on disk. | |
223 uint64_t disk_used_ = 0; | |
224 | |
225 // State for GenerateNextPageFileName. | |
226 uint64_t current_file_num_ = 0; | |
227 | |
228 size_t pending_memory_quota_total_size_ = 0; | |
229 PendingMemoryQuotaTaskList pending_memory_quota_tasks_; | |
230 PendingFileQuotaTaskList pending_file_quota_tasks_; | |
231 | |
232 int pending_evictions_ = 0; | |
233 | |
234 bool file_paging_enabled_ = false; | |
235 base::FilePath blob_storage_dir_; | |
236 scoped_refptr<base::TaskRunner> file_runner_; | |
237 | |
238 // Lifetime of the ShareableBlobDataItem objects is handled externally in the | |
239 // BlobStorageContext class. | |
240 base::MRUCache<uint64_t, ShareableBlobDataItem*> populated_memory_items_; | |
241 size_t populated_memory_items_bytes_ = 0; | |
242 // We need to keep track of items currently being paged to disk so that if | |
243 // another blob successfully grabs a ref, we can prevent it from adding the | |
244 // item to the recent_item_cache_ above. | |
245 std::unordered_set<uint64_t> items_paging_to_file_; | |
246 | |
247 // We clear this when disabling paging to disable pending file tasks. | |
248 base::WeakPtrFactory<BlobMemoryController> file_task_factory_; | |
249 // Used for bookkeeping tasks, where we decrement our memory or disk usage. | |
250 base::WeakPtrFactory<BlobMemoryController> quota_bookkeeping_factory_; | |
251 | |
252 DISALLOW_COPY_AND_ASSIGN(BlobMemoryController); | |
253 }; | |
254 } // namespace storage | |
255 #endif // STORAGE_BROWSER_BLOB_BLOB_MEMORY_CONTROLLER_H_ | |
OLD | NEW |