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 SingleThreadTaskRunner; | |
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 base::SupportsWeakPtr<BlobMemoryController> { | |
Marijn Kruisselbrink
2016/07/08 23:37:37
In general using a member base::WeakPtrFactory is
dmurph
2016/07/11 23:33:28
Done.
| |
46 public: | |
47 enum class MemoryStrategyResult { | |
48 // We don't have enough memory for this blob. | |
49 TOO_LARGE, | |
50 // There isn't any memory that needs transporting. | |
51 NONE_NEEDED, | |
52 // Transportation strategies. | |
53 SHORTCUT, | |
54 IPC, | |
55 SHARED_MEMORY, | |
56 FILE | |
57 }; | |
58 typedef std::list<std::pair<size_t, base::Callback<void(bool)>>> | |
59 PendingBlobConstructionList; | |
60 typedef PendingBlobConstructionList::iterator PendingContructionEntry; | |
Marijn Kruisselbrink
2016/07/08 23:37:37
Contruction -> Construction
dmurph
2016/07/11 23:33:28
Done.
| |
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::Optional<base::File> file = base::nullopt; | |
71 base::Time last_modified; | |
72 }; | |
73 | |
74 BlobMemoryController(); | |
75 virtual ~BlobMemoryController(); | |
76 | |
77 void EnableDisk(const base::FilePath storage_directory, | |
78 scoped_refptr<base::SingleThreadTaskRunner> file_runner); | |
79 | |
80 // Returns if the data in the descriptions is good data (not a bad IPC). If | |
81 // SHORTCUT is returned, then the blob can be immediately constructed, and | |
82 // the a following call to MaybeFitInMemoryNow will always return true. | |
83 bool DecideBlobTransportationMemoryStrategy( | |
84 const std::vector<DataElement>& descriptions, | |
85 uint64_t* total_bytes, | |
86 MemoryStrategyResult* result) const; | |
87 | |
88 // Creates a temporary file that we store in our blob temporary directory. The | |
89 // lifetime of the file is tied to the file_reference. | |
90 void CreateTemporaryFileForRenderer( | |
91 uint64_t size_bytes, | |
92 const base::Callback<void(FileCreationInfo)>& done); | |
93 | |
94 void FreeMemory(size_t memory_size_bytes); | |
95 | |
96 enum class MemoryRequestResponse { | |
97 SPACE_AVAILABLE, | |
98 SPACE_UNAVAILABLE, | |
99 PENDING_DISK | |
100 }; | |
101 | |
102 bool CanFitInSystem(uint64_t size) const; | |
103 | |
104 // We assume the user called CanFitInSystem before this and that the given | |
105 // memory size can fit in our system. | |
106 // Notifies the caller when a blob of the given size can be stored in our blob | |
107 // system. If the returned entry is not present, that means the blob can fit | |
108 // right away and the callback won't be called. | |
109 // The argument on can_request is true if we can create the blob, and | |
110 // false if there was an error and the new blob needs to be broken. | |
111 base::Optional<PendingContructionEntry> NotifyWhenMemoryCanPopulated( | |
112 size_t memory_size_bytes, | |
113 const base::Callback<void(bool)>& can_request); | |
114 | |
115 void RemovePendingConstructionEntry(const PendingContructionEntry& entry); | |
116 | |
117 // This updates the given item in our LRU lookup table. | |
118 void UpdateBlobItemInRecents(ShareableBlobDataItem* item); | |
119 | |
120 // This removes the given item from our LRU lookup table. | |
121 void RemoveBlobItemInRecents(uint64_t id); | |
122 | |
123 // Disables the disk. | |
124 void DisableDisk(); | |
125 | |
126 bool disk_enabled() const { return disk_used_; } | |
127 | |
128 size_t max_ipc_memory_size() const { return max_ipc_memory_size_; } | |
129 size_t max_shared_memory_size() const { return max_shared_memory_size_; } | |
130 size_t max_blob_in_memory_size() const { return max_blob_in_memory_size_; } | |
131 size_t max_blob_memory_space() const { return max_blob_memory_space_; } | |
132 uint64_t max_blob_disk_space() const { return max_blob_disk_space_; } | |
133 size_t in_flight_space() const { return in_flight_space_; } | |
134 uint64_t min_page_file_size() const { return min_page_file_size_; } | |
135 uint64_t max_file_size() const { return max_file_size_; } | |
136 | |
137 void SetMemoryContantsForTesting(size_t max_ipc_memory_size, | |
Marijn Kruisselbrink
2016/07/08 23:37:37
Contants -> Constants
dmurph
2016/07/11 23:33:28
Done.
| |
138 size_t max_shared_memory_size, | |
139 size_t max_blob_in_memory_size, | |
140 size_t max_blob_memory_space, | |
141 uint64_t max_blob_disk_space, | |
142 size_t in_flight_space, | |
143 uint64_t min_page_file_size, | |
144 uint64_t max_file_size) { | |
145 max_ipc_memory_size_ = max_ipc_memory_size; | |
146 max_shared_memory_size_ = max_shared_memory_size; | |
147 max_blob_in_memory_size_ = max_blob_in_memory_size; | |
148 max_blob_memory_space_ = max_blob_memory_space; | |
149 max_blob_disk_space_ = max_blob_disk_space; | |
150 in_flight_space_ = in_flight_space; | |
151 min_page_file_size_ = min_page_file_size; | |
152 max_file_size_ = max_file_size; | |
153 } | |
154 | |
155 private: | |
156 using RecentItemsCache = base::MRUCache<uint64_t, ShareableBlobDataItem*>; | |
157 | |
158 // We add a delete callback to the file reference to decrement our disk usage. | |
159 void OnCreateFile(uint64_t file_size, | |
160 const base::Callback<void(FileCreationInfo)>& done, | |
161 FileCreationInfo result); | |
162 | |
163 void MaybeScheduleWaitingBlobs(); | |
164 | |
165 // Returns the size we'll be freeing. We appropriately increment/decrement | |
166 // the disk, memory, and in flight counts. | |
167 size_t ScheduleBlobPaging(); | |
168 void SchedulePagingUntilWeCanFit(size_t total_memory_needed); | |
169 | |
170 // Called when we've completed paging a list of items | |
171 void OnPagingComplete( | |
172 std::unique_ptr<std::vector<scoped_refptr<ShareableBlobDataItem>>> items, | |
173 size_t total_items_size, | |
174 FileCreationInfo result); | |
175 | |
176 void RecordTracingCounters(); | |
177 | |
178 size_t GetAvailableMemoryForBlobs() const; | |
179 uint64_t GetAvailableDiskSpaceForBlobs() const; | |
180 | |
181 // We decrement the disk space used. | |
182 void OnBlobFileDelete(uint64_t size, const base::FilePath& path); | |
183 | |
184 // Memory bookkeeping. | |
185 size_t blob_memory_used_ = 0; | |
186 size_t in_flight_memory_used_ = 0; | |
187 uint64_t disk_used_ = 0; | |
188 | |
189 scoped_refptr<base::SingleThreadTaskRunner> file_runner_; | |
190 | |
191 PendingBlobConstructionList blobs_waiting_for_paging_; | |
192 size_t blobs_waiting_for_paging_size_ = 0; | |
193 | |
194 // Constants. | |
195 size_t max_ipc_memory_size_ = kBlobStorageIPCThresholdBytes; | |
196 size_t max_shared_memory_size_ = kBlobStorageMaxSharedMemoryBytes; | |
197 size_t max_blob_in_memory_size_ = kBlobStorageMaxBlobMemorySize; | |
198 size_t max_blob_memory_space_ = kBlobStorageMaxMemoryUsage; | |
199 uint64_t max_blob_disk_space_ = kBlobStorageMaxDiskSpace; | |
200 size_t in_flight_space_ = kBlobStorageInFlightMemory; | |
201 uint64_t min_page_file_size_ = kBlobStorageMinFileSizeBytes; | |
202 uint64_t max_file_size_ = kBlobStorageMaxFileSizeBytes; | |
203 | |
204 bool enable_disk_ = false; | |
205 base::FilePath blob_storage_dir_; | |
206 uint64_t current_file_num_ = 0; | |
207 | |
208 // Lifetime of the ShareableBlobDataItem objects is handles externally in the | |
Marijn Kruisselbrink
2016/07/08 23:37:37
handles -> handled
dmurph
2016/07/11 23:33:28
Done.
| |
209 // BlobStorageContext class. | |
210 RecentItemsCache recent_item_cache_; | |
211 | |
212 DISALLOW_COPY_AND_ASSIGN(BlobMemoryController); | |
213 }; | |
214 } // namespace storage | |
215 #endif // STORAGE_BROWSER_BLOB_BLOB_MEMORY_CONTROLLER_H_ | |
OLD | NEW |