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 <algorithm> | |
11 #include <list> | |
12 #include <map> | |
13 #include <string> | |
14 #include <utility> | |
15 #include <vector> | |
16 | |
17 #include "base/callback.h" | |
18 #include "base/containers/hash_tables.h" | |
19 #include "base/containers/mru_cache.h" | |
20 #include "base/files/file.h" | |
21 #include "base/files/file_path.h" | |
22 #include "base/macros.h" | |
23 #include "base/memory/ref_counted.h" | |
24 #include "base/memory/weak_ptr.h" | |
25 #include "base/optional.h" | |
26 #include "base/time/time.h" | |
27 #include "storage/browser/storage_browser_export.h" | |
28 #include "storage/common/blob_storage/blob_storage_constants.h" | |
29 | |
30 namespace base { | |
31 class TaskRunner; | |
32 } | |
33 | |
34 namespace storage { | |
35 class DataElement; | |
36 class ShareableBlobDataItem; | |
37 class ShareableFileReference; | |
38 | |
39 // This class is responsible for all memory, quota, and file operations for the | |
40 // blob system. To determine the transportation strategy for a blob transfer | |
41 // from the renderer, use DecideBlobTransportationMemoryStrategy. To request | |
42 // quota and create files for blob items, you can use: | |
43 // * CanFitInSystem to see if we can fit at all, | |
44 // * ReserveQuotaForItems to ask for quota reservation (for either bytes or | |
45 // files, but not in the same call), and | |
46 // * CancelQuotaReservation to optionally cancel the quota request before it is | |
47 // granted. | |
48 // When destroying a blob, one can call MaybeFreeQuotaForItems to free unused | |
49 // bytes items. Finally, one can use UpdateBlobItemInRecents and | |
50 // RemoveBlobItemInRecents to add/update and remove populated bytes items as | |
51 // candiates for paging memory to disk. This class automatically manages paging | |
52 // memory to disk if we are running low on quota. | |
michaeln
2016/08/15 22:44:44
Please be less verbose in this comment (and in gen
dmurph
2016/08/19 00:18:33
Done.
| |
53 class STORAGE_EXPORT BlobMemoryController { | |
54 public: | |
55 enum class MemoryStrategyResult { | |
michaeln
2016/08/15 22:44:44
Shorter symbols, maybe MemoryStrategy or even just
dmurph
2016/08/19 00:18:33
Done.
| |
56 // We don't have enough memory for this blob. | |
57 TOO_LARGE, | |
58 // There isn't any memory that needs transporting. | |
59 NONE_NEEDED, | |
60 // Transportation strategies. | |
61 IPC, | |
62 SHARED_MEMORY, | |
63 FILE | |
64 }; | |
65 | |
66 struct FileCreationInfo { | |
67 FileCreationInfo(); | |
68 ~FileCreationInfo(); | |
69 FileCreationInfo(FileCreationInfo&& other); | |
70 FileCreationInfo& operator=(FileCreationInfo&&); | |
71 | |
72 base::File::Error error = base::File::FILE_ERROR_FAILED; | |
73 scoped_refptr<ShareableFileReference> file_reference; | |
74 base::File file; | |
75 base::Time last_modified; | |
76 }; | |
77 | |
78 using QuotaRequestCallback = | |
79 base::Callback<void(bool, std::vector<FileCreationInfo>)>; | |
80 using PendingBlobConstructionList = | |
81 std::list<std::pair<size_t, QuotaRequestCallback>>; | |
82 | |
83 static const size_t kInvalidDiskQuotaEntry = 0; | |
84 | |
85 // This struct keeps track of a quota reservation, which the user can use to | |
86 // cancel the quota request with CancelQuotaReservation. This becomes invalid | |
87 // after |success_callback| is called from the ReserveQuotaForItems call. | |
michaeln
2016/08/15 22:44:44
Does this need to be public? would be nice if the
dmurph
2016/08/19 00:18:33
Simplified
| |
88 struct PendingQuotaEntry { | |
89 explicit PendingQuotaEntry(uint64_t disk_quota_entry); | |
90 explicit PendingQuotaEntry( | |
91 PendingBlobConstructionList::iterator memory_quota_entry); | |
92 PendingQuotaEntry(const PendingQuotaEntry& other); | |
93 ~PendingQuotaEntry(); | |
94 | |
95 // We're either saving | |
michaeln
2016/08/15 22:44:44
i don't understand the comment?
dmurph
2016/08/19 00:18:33
Removed.
| |
96 uint64_t disk_quota_entry = kInvalidDiskQuotaEntry; | |
97 PendingBlobConstructionList::iterator memory_quota_entry; | |
98 }; | |
99 | |
100 BlobMemoryController(); | |
101 virtual ~BlobMemoryController(); | |
102 | |
103 void EnableDisk(const base::FilePath& storage_directory, | |
104 scoped_refptr<base::TaskRunner> file_runner); | |
105 | |
106 // Disables the disk. This cancels all pending file creations and paging | |
107 // operations. | |
108 void DisableDisk(); | |
109 | |
110 bool disk_enabled() const { return disk_enabled_; } | |
111 | |
112 // Returns the strategy the transportation layer should use to transport the | |
113 // given memory. |shortcut_bytes| are the number of transport bytes that are | |
114 // already populated for us, so we don't haved to request them from the | |
115 // renderer. | |
116 MemoryStrategyResult DecideBlobTransportationMemoryStrategy( | |
michaeln
2016/08/15 22:44:44
Please use shorter symbols: DetermineStrategy() De
dmurph
2016/08/19 00:18:33
Done.
| |
117 size_t shortcut_bytes, | |
118 uint64_t total_transportation_bytes) const; | |
119 | |
120 // Checks to see if the given size can fit in the system. | |
121 bool CanFitInSystem(uint64_t size) const; | |
michaeln
2016/08/15 22:44:44
Does this need to be public? It's checked when the
dmurph
2016/08/19 00:18:33
When we create a blob by using BlobStorageContext:
| |
122 | |
123 // 'Reserves' space for the given items, and calls |success_callback| when | |
michaeln
2016/08/15 22:44:44
why the quotes?
dmurph
2016/08/19 00:18:33
Simplified.
| |
124 // we're done. This can be done synchronously before the method returns. | |
125 // We only reserve for items ShareableBlobDataItem::state() == QUOTA_NEEDED. | |
126 // We change this to QUOTA_REQUESTED while we allocate space, and | |
127 // QUOTA_GRANTED when we've succeeded and before we call |success_callback|. | |
128 // If the return value is populated, you can use that to cancel the quota | |
129 // reservation using CancelQuotaReservation. | |
130 // NOTE: We assume the user checked CanFitInSystem before this, as we don't | |
131 // inspect quotas. | |
132 // | |
133 // One can only reserve quota for bytes and temporary file items (see | |
michaeln
2016/08/15 22:44:44
Maybe make two methods them, one to ReserveMemoryQ
dmurph
2016/08/19 00:18:33
Great idea!
| |
134 // BlobDataBuilder). You CANNOT ask for quota for both types of items at the | |
135 // same time, and you must split up your requests. When asking for temporary | |
136 // file item quota, the files will be created and the FileCreationInfo vector | |
137 // in the |success_callback| will be populated with the files. | |
138 base::Optional<PendingQuotaEntry> ReserveQuotaForItems( | |
michaeln
2016/08/15 22:44:44
Can this return an integer id type to represent th
dmurph
2016/08/19 00:18:33
Yes, done.
| |
139 std::vector<ShareableBlobDataItem*> items, | |
140 const QuotaRequestCallback& success_callback); | |
141 | |
142 // If we no longer need memory requsted from ReserveQuotaForItems before | |
143 // |success_callback| has been called above, you can call this method to | |
144 // correctly free the requested memory. | |
145 void CancelQuotaReservation(const PendingQuotaEntry& entry); | |
146 | |
147 // This looks through the given items and releases quota for any items that: | |
148 // * Are bytes items, | |
149 // * don't have any blobs referencing them, AND | |
150 // * were allocated quota originally (state is POPULATED_WITH_QUOTA). | |
151 // We don't worry about file items as we use the ShareableFileReference | |
152 // destruction callback feature to detect when those are deleted. | |
153 void MaybeFreeQuotaForItems( | |
154 const std::vector<scoped_refptr<ShareableBlobDataItem>>& items); | |
155 | |
156 // This is used to release quota for an item that was supposed to be filled | |
157 // with a data copy from an item in another blob, but by the time the copy | |
158 // was meant to be performed the original item had been paged to file. This | |
159 // functionally just decrements the memory by the item length, but we do | |
160 // extra checks to make sure our state is correct. | |
161 void FreeQuotaForPagedItemReference( | |
162 const scoped_refptr<ShareableBlobDataItem>& item); | |
163 | |
164 // This adds or updates the given item in our LRU table used for paging items | |
165 // to disk. The item must be a bytes item that is both populated an has it's | |
166 // quota allocated (AKA, in state POPULATED_WITH_QUOTA). | |
167 void UpdateBlobItemInRecents(ShareableBlobDataItem* item); | |
168 | |
169 // This removes the given item from our LRU table used for paging. | |
170 void RemoveBlobItemInRecents(const ShareableBlobDataItem& item); | |
171 | |
172 size_t memory_usage() const { | |
173 return blob_memory_used_ + in_flight_memory_used_; | |
174 } | |
175 | |
176 uint64_t disk_usage() const { return disk_used_; } | |
177 | |
178 size_t max_ipc_memory_size() const { return max_ipc_memory_size_; } | |
179 size_t max_shared_memory_size() const { return max_shared_memory_size_; } | |
180 size_t max_blob_in_memory_size() const { return max_blob_in_memory_size_; } | |
181 uint64_t max_blob_disk_space() const { return max_blob_disk_space_; } | |
182 size_t in_flight_space() const { return in_flight_space_; } | |
183 uint64_t min_page_file_size() const { return min_page_file_size_; } | |
184 uint64_t max_file_size() const { return max_file_size_; } | |
185 | |
186 void SetMemoryConstantsForTesting(size_t max_ipc_memory_size, | |
187 size_t max_shared_memory_size, | |
188 size_t max_blob_in_memory_size, | |
189 uint64_t max_blob_disk_space, | |
190 size_t in_flight_space, | |
191 uint64_t min_page_file_size, | |
192 uint64_t max_file_size) { | |
193 max_ipc_memory_size_ = max_ipc_memory_size; | |
194 max_shared_memory_size_ = max_shared_memory_size; | |
195 max_blob_in_memory_size_ = max_blob_in_memory_size; | |
196 max_blob_disk_space_ = max_blob_disk_space; | |
197 in_flight_space_ = in_flight_space; | |
198 min_page_file_size_ = min_page_file_size; | |
199 max_file_size_ = max_file_size; | |
200 } | |
201 | |
202 private: | |
203 // This is a helper callback method that sets the state of the given |items| | |
204 // to POPULATED_WITH_QUOTA before then forwarding |success| and |files| to the | |
205 // |final_callback|. | |
206 void SetStateQuotaGrantedAndCallback( | |
207 std::vector<scoped_refptr<ShareableBlobDataItem>> items, | |
208 const BlobMemoryController::QuotaRequestCallback& final_callback, | |
209 bool success, | |
210 std::vector<FileCreationInfo> files); | |
211 | |
212 // Called when we've finished creating files for ReserveQuotaForItems when the | |
213 // items are pending file items. We handle error cases, the case where the | |
214 // cancels the quota reservation, and the success case where we add a delete | |
215 // callback to the file reference to decrement our disk usage before fowarding | |
216 // the files and result to the |file_callback|. | |
217 void OnCreateFiles( | |
218 std::vector<uint64_t> file_sizes, | |
219 std::vector<scoped_refptr<ShareableBlobDataItem>> pending_items, | |
220 uint64_t disk_quota_entry, | |
221 const QuotaRequestCallback& file_callback, | |
222 std::vector<FileCreationInfo> result); | |
223 | |
224 void MaybeGrantPendingQuotaRequests(); | |
225 | |
226 // We schedule paging until our memory usage is below our memory limit. We use | |
227 // the lru table to find old items, and we combine them until we reach the | |
228 // min_page_file_size(), and don't schedule paging until we have at least that | |
229 // amount of memory to save to disk. | |
230 void MaybeSchedulePagingUntilSystemHealthy(); | |
231 | |
232 // Called when we've completed paging a list of items to disk. This is where | |
233 // we swap the bytes items for file items, and and update our bookkeeping. | |
234 void OnPagingComplete( | |
235 std::unique_ptr<std::vector<scoped_refptr<ShareableBlobDataItem>>> items, | |
236 size_t total_items_size, | |
237 FileCreationInfo result); | |
238 | |
239 void RecordTracingCounters(); | |
240 | |
241 size_t GetAvailableMemoryForBlobs() const; | |
242 uint64_t GetAvailableDiskSpaceForBlobs() const; | |
243 | |
244 // This is registered as a callback for file deletions on the file reference | |
245 // of our paging files. We decrement the disk space used. | |
246 void OnBlobFileDelete(uint64_t size, const base::FilePath& path); | |
247 | |
248 // Memory bookkeeping. These numbers are all disjoint. | |
249 // This is the amount of memory we're using for blobs in RAM. | |
250 size_t blob_memory_used_ = 0; | |
251 // This is memory we're temporarily using while we try to write blob items to | |
252 // disk. | |
253 size_t in_flight_memory_used_ = 0; | |
254 // This is the amount of memory we're using on disk. | |
255 uint64_t disk_used_ = 0; | |
256 | |
257 size_t pending_pagings_ = 0; | |
258 PendingBlobConstructionList blobs_waiting_for_paging_; | |
259 size_t blobs_waiting_for_paging_size_ = 0; | |
260 | |
261 // We use the same data storage as above to keep the PendingEntry API | |
262 // consistent. | |
263 uint64_t curr_disk_save_entry_ = 0; | |
264 base::hash_set<size_t> pending_file_opens_; | |
Marijn Kruisselbrink
2016/08/05 23:23:55
base::hash_set is deprecated, use std::unordered_s
dmurph
2016/08/19 00:18:33
Done.
| |
265 | |
266 scoped_refptr<base::TaskRunner> file_runner_; | |
267 | |
268 // Constants. | |
269 // TODO(dmurph): consolidate these into a struct. | |
michaeln
2016/08/15 22:44:44
can this be done in this cl?
dmurph
2016/08/19 00:18:33
Done.
| |
270 // This is the maximum amount of memory we can send in an IPC. | |
271 size_t max_ipc_memory_size_ = kBlobStorageIPCThresholdBytes; | |
272 // This is the maximum size of a shared memory handle. | |
273 size_t max_shared_memory_size_ = kBlobStorageMaxSharedMemoryBytes; | |
274 // This is the memory we can allocated towards blobs stored in RAM. | |
275 size_t max_blob_in_memory_size_ = kBlobStorageMaxBlobMemorySpace; | |
276 // This is the maximum amount of disk space we can use. | |
277 uint64_t max_blob_disk_space_ = kBlobStorageMaxDiskSpace; | |
278 // This is the maximum amount of memory we can temporarily allocate towards | |
279 // blob items that we're currently writing to disk. | |
280 size_t in_flight_space_ = kBlobStorageInFlightSpace; | |
281 // This is the minimum file size we can use when paging blob items to disk. | |
282 // We combine items until we reach at least this size. | |
283 uint64_t min_page_file_size_ = kBlobStorageMinFileSizeBytes; | |
284 // This is the maximum file size we can create. We use this is | |
285 // BlobAsyncTransportHost to segment large blobs, and then we create the files | |
286 // using CreateTemporaryFile. | |
287 uint64_t max_file_size_ = kBlobStorageMaxFileSizeBytes; | |
288 | |
289 bool disk_enabled_ = false; | |
290 base::FilePath blob_storage_dir_; | |
291 uint64_t current_file_num_ = 0; | |
292 | |
293 // Lifetime of the ShareableBlobDataItem objects is handled externally in the | |
294 // BlobStorageContext class. | |
295 base::MRUCache<uint64_t, ShareableBlobDataItem*> recent_item_cache_; | |
296 size_t recent_item_cache_bytes_ = 0; | |
297 // We need to keep track of items currently being paged to disk so that if | |
298 // another blob successfully grabs a ref, we can prevent it from adding the | |
299 // item to the recent_item_cache_ above. | |
300 base::hash_set<uint64_t> items_saving_to_disk_; | |
301 | |
302 base::WeakPtrFactory<BlobMemoryController> ptr_factory_; | |
303 | |
304 DISALLOW_COPY_AND_ASSIGN(BlobMemoryController); | |
305 }; | |
306 } // namespace storage | |
307 #endif // STORAGE_BROWSER_BLOB_BLOB_MEMORY_CONTROLLER_H_ | |
OLD | NEW |