OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 The Chromium Authors. All rihts 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/mru_cache.h" | |
19 #include "base/files/file.h" | |
20 #include "base/files/file_path.h" | |
21 #include "base/macros.h" | |
22 #include "base/memory/ref_counted.h" | |
23 #include "base/memory/weak_ptr.h" | |
24 #include "base/optional.h" | |
25 #include "base/time/time.h" | |
26 #include "storage/browser/storage_browser_export.h" | |
27 #include "storage/common/blob_storage/blob_storage_constants.h" | |
28 #include "storage/common/data_element.h" | |
29 | |
30 namespace base { | |
31 class SequencedWorkerPool; | |
32 } | |
33 | |
34 namespace storage { | |
35 class ShareableBlobDataItem; | |
36 class ShareableFileReference; | |
37 | |
38 // There should be no memory sizes of 0. | |
39 // This class is responsible for | |
40 // * our current memory usage for blobs, both on disk and in memory, | |
41 // * when we can add a blob w/ a given size, | |
42 // * creating temporary files, | |
43 // * paging blobs to disk. | |
44 class STORAGE_EXPORT BlobMemoryController { | |
45 public: | |
46 enum class MemoryStrategyResult { | |
47 // We don't have enough memory for this blob. | |
48 TOO_LARGE, | |
49 // There isn't any memory that needs transporting. | |
50 NONE_NEEDED, | |
Marijn Kruisselbrink
2016/07/12 21:33:06
Is there any meaningful difference between NONE_NE
dmurph
2016/07/14 01:04:31
Good idea, I'll remove shortcut.
| |
51 // Transportation strategies. | |
52 SHORTCUT, | |
53 IPC, | |
54 SHARED_MEMORY, | |
55 FILE | |
56 }; | |
57 typedef std::list<std::pair<size_t, base::Callback<void(bool)>>> | |
Marijn Kruisselbrink
2016/07/12 21:33:06
using instead of typedef (also below)
dmurph
2016/07/14 01:04:31
Done.
| |
58 PendingBlobConstructionList; | |
59 typedef PendingBlobConstructionList::iterator PendingConstructionEntry; | |
michaeln
2016/07/14 01:44:43
This typedef is odd?
dmurph
2016/07/15 02:45:27
I need that entry defined as I return it below.
| |
60 | |
61 struct FileCreationInfo { | |
62 FileCreationInfo(); | |
63 ~FileCreationInfo(); | |
64 FileCreationInfo(FileCreationInfo&& other); | |
65 FileCreationInfo& operator=(FileCreationInfo&&); | |
66 | |
67 base::File::Error error = base::File::FILE_ERROR_FAILED; | |
68 scoped_refptr<ShareableFileReference> file_reference; | |
69 base::File file; | |
70 base::Time last_modified; | |
71 }; | |
72 | |
73 BlobMemoryController(); | |
74 virtual ~BlobMemoryController(); | |
75 | |
76 void EnableDisk(const base::FilePath& storage_directory, | |
77 scoped_refptr<base::SequencedWorkerPool> file_worker_pool); | |
michaeln
2016/07/14 01:44:43
+1 putting these tasks on the worker pool instead
dmurph
2016/07/15 02:45:27
Done.
| |
78 | |
79 // Disables the disk. | |
80 void DisableDisk(); | |
81 | |
82 bool disk_enabled() const { return disk_used_; } | |
83 | |
84 // Returns if the data in the descriptions is good data (not a bad IPC). If | |
85 // SHORTCUT is returned, then the blob can be immediately constructed, and | |
86 // the a following call to MaybeFitInMemoryNow will always return true. | |
Marijn Kruisselbrink
2016/07/12 21:33:06
the a?
dmurph
2016/07/14 01:04:31
Done.
| |
87 bool DecideBlobTransportationMemoryStrategy( | |
88 const std::vector<DataElement>& descriptions, | |
89 uint64_t* total_bytes, | |
90 MemoryStrategyResult* result) const; | |
91 | |
92 // Creates a temporary file that we store in our blob temporary directory. The | |
93 // lifetime of the file is tied to the file_reference. | |
94 void CreateTemporaryFileForRenderer( | |
michaeln
2016/07/14 01:44:43
naming nit: "ForRenderer" isn't really needed
dmurph
2016/07/15 02:45:27
Done.
| |
95 uint64_t size_bytes, | |
96 const base::Callback<void(FileCreationInfo)>& file_callback); | |
97 | |
98 void FreeMemory(size_t memory_size_bytes); | |
99 | |
100 enum class MemoryRequestResponse { | |
Marijn Kruisselbrink
2016/07/12 21:33:06
https://google.github.io/styleguide/cppguide.html#
dmurph
2016/07/14 01:04:31
Removed!
| |
101 SPACE_AVAILABLE, | |
102 SPACE_UNAVAILABLE, | |
103 PENDING_DISK | |
104 }; | |
105 | |
106 bool CanFitInSystem(uint64_t size) const; | |
107 | |
108 // We assume the user called CanFitInSystem before this and that the given | |
109 // memory size can fit in our system. | |
110 // Notifies the caller when a blob of the given size can be stored in our blob | |
111 // system. If the returned entry is not present, that means the blob can fit | |
112 // right away and the callback won't be called. | |
113 // The argument on can_request is true if we can create the blob, and | |
114 // false if there was an error and the new blob needs to be broken. | |
115 base::Optional<PendingConstructionEntry> NotifyWhenMemoryCanPopulated( | |
116 size_t memory_size_bytes, | |
117 const base::Callback<void(bool)>& can_request); | |
118 | |
119 void RemovePendingConstructionEntry(const PendingConstructionEntry& entry); | |
120 | |
121 // This updates the given item in our LRU lookup table. | |
122 void UpdateBlobItemInRecents(ShareableBlobDataItem* item); | |
123 | |
124 // This removes the given item from our LRU lookup table. | |
125 void RemoveBlobItemInRecents(const ShareableBlobDataItem& item); | |
126 | |
127 size_t memory_usage() const { | |
128 return blob_memory_used_ + in_flight_memory_used_; | |
129 } | |
130 uint64_t disk_usage() const { return disk_used_; } | |
131 | |
132 size_t max_ipc_memory_size() const { return max_ipc_memory_size_; } | |
133 size_t max_shared_memory_size() const { return max_shared_memory_size_; } | |
134 size_t max_blob_in_memory_size() const { return max_blob_in_memory_size_; } | |
135 size_t max_blob_memory_space() const { return max_blob_memory_space_; } | |
136 uint64_t max_blob_disk_space() const { return max_blob_disk_space_; } | |
137 size_t in_flight_space() const { return in_flight_space_; } | |
138 uint64_t min_page_file_size() const { return min_page_file_size_; } | |
139 uint64_t max_file_size() const { return max_file_size_; } | |
140 | |
141 void SetMemoryConstantsForTesting(size_t max_ipc_memory_size, | |
142 size_t max_shared_memory_size, | |
143 size_t max_blob_in_memory_size, | |
144 size_t max_blob_memory_space, | |
michaeln
2016/07/14 01:44:43
what's the difference between max_blob_in_memory_s
dmurph
2016/07/15 02:45:27
Removed. One was the addition of max blob size and
| |
145 uint64_t max_blob_disk_space, | |
146 size_t in_flight_space, | |
147 uint64_t min_page_file_size, | |
148 uint64_t max_file_size) { | |
michaeln
2016/07/14 01:44:43
is there a reason the "min" value has "page" in th
dmurph
2016/07/15 02:45:27
The min size is used for paging, and the max size
| |
149 max_ipc_memory_size_ = max_ipc_memory_size; | |
150 max_shared_memory_size_ = max_shared_memory_size; | |
151 max_blob_in_memory_size_ = max_blob_in_memory_size; | |
152 max_blob_memory_space_ = max_blob_memory_space; | |
153 max_blob_disk_space_ = max_blob_disk_space; | |
154 in_flight_space_ = in_flight_space; | |
155 min_page_file_size_ = min_page_file_size; | |
156 max_file_size_ = max_file_size; | |
157 } | |
158 | |
159 private: | |
160 using RecentItemsCache = base::MRUCache<uint64_t, ShareableBlobDataItem*>; | |
161 | |
162 // We add a delete callback to the file reference to decrement our disk usage. | |
163 void OnCreateFile(uint64_t file_size, | |
164 const base::Callback<void(FileCreationInfo)>& done, | |
165 FileCreationInfo result); | |
166 | |
167 void MaybeScheduleWaitingBlobs(); | |
168 | |
169 // Returns the size we'll be freeing. We appropriately increment/decrement | |
170 // the disk, memory, and in flight counts. | |
171 size_t ScheduleBlobPaging(); | |
172 void SchedulePagingUntilWeCanFit(size_t total_memory_needed); | |
173 | |
174 // Called when we've completed paging a list of items | |
175 void OnPagingComplete( | |
176 std::unique_ptr<std::vector<scoped_refptr<ShareableBlobDataItem>>> items, | |
177 size_t total_items_size, | |
178 FileCreationInfo result); | |
179 | |
180 void RecordTracingCounters(); | |
181 | |
182 bool ShouldSchedulePagingForSize(size_t size) const; | |
183 size_t GetAvailableMemoryForBlobs() const; | |
184 uint64_t GetAvailableDiskSpaceForBlobs() const; | |
185 | |
186 // We decrement the disk space used. | |
187 void OnBlobFileDelete(uint64_t size, const base::FilePath& path); | |
188 | |
189 // Memory bookkeeping. | |
190 size_t blob_memory_used_ = 0; | |
191 size_t in_flight_memory_used_ = 0; | |
192 uint64_t disk_used_ = 0; | |
193 | |
194 scoped_refptr<base::SequencedWorkerPool> file_worker_pool_; | |
195 | |
196 size_t pending_pagings_ = 0; | |
197 PendingBlobConstructionList blobs_waiting_for_paging_; | |
198 size_t blobs_waiting_for_paging_size_ = 0; | |
199 | |
200 // Constants. | |
michaeln
2016/07/14 01:44:43
A struct for these constant/config/settings would
dmurph
2016/07/15 02:45:27
put a TODO.
| |
201 size_t max_ipc_memory_size_ = kBlobStorageIPCThresholdBytes; | |
202 size_t max_shared_memory_size_ = kBlobStorageMaxSharedMemoryBytes; | |
203 size_t max_blob_in_memory_size_ = kBlobStorageMaxBlobMemorySize; | |
204 size_t max_blob_memory_space_ = kBlobStorageMaxMemoryUsage; | |
205 uint64_t max_blob_disk_space_ = kBlobStorageMaxDiskSpace; | |
206 size_t in_flight_space_ = kBlobStorageInFlightMemory; | |
207 uint64_t min_page_file_size_ = kBlobStorageMinFileSizeBytes; | |
208 uint64_t max_file_size_ = kBlobStorageMaxFileSizeBytes; | |
209 | |
210 bool enable_disk_ = false; | |
211 base::FilePath blob_storage_dir_; | |
212 uint64_t current_file_num_ = 0; | |
213 | |
214 // Lifetime of the ShareableBlobDataItem objects is handled externally in the | |
215 // BlobStorageContext class. | |
216 RecentItemsCache recent_item_cache_; | |
michaeln
2016/07/14 01:44:43
The using alias for RecentItemsCache isn't needed,
dmurph
2016/07/15 02:45:27
Done.
| |
217 | |
218 base::WeakPtrFactory<BlobMemoryController> ptr_factory_; | |
219 | |
220 DISALLOW_COPY_AND_ASSIGN(BlobMemoryController); | |
221 }; | |
222 } // namespace storage | |
223 #endif // STORAGE_BROWSER_BLOB_BLOB_MEMORY_CONTROLLER_H_ | |
OLD | NEW |