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> { | |
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; | |
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 SetMemoryLimits(size_t max_blob_in_memory_size_bytes, | |
78 size_t max_blob_memory_space_bytes, | |
79 uint64_t max_blob_disk_space_bytes, | |
80 size_t in_flight_space_bytes, | |
81 uint64_t min_page_file_size) { | |
82 max_blob_in_memory_size_ = max_blob_in_memory_size_bytes; | |
83 max_blob_memory_space_ = max_blob_memory_space_bytes; | |
84 max_blob_disk_space_ = max_blob_disk_space_bytes; | |
85 in_flight_space_ = in_flight_space_bytes; | |
86 min_page_file_size_ = min_page_file_size; | |
87 } | |
88 | |
89 void EnableDisk(const base::FilePath storage_directory, | |
90 scoped_refptr<base::SingleThreadTaskRunner> file_runner); | |
91 | |
92 // Returns if the data in the descriptions is good data (not a bad IPC). If | |
93 // SHORTCUT is returned, then the blob can be immediately constructed, and | |
94 // the a following call to MaybeFitInMemoryNow will always return true. | |
95 bool DecideBlobTransportationMemoryStrategy( | |
96 const std::vector<DataElement>& descriptions, | |
97 uint64_t* total_bytes, | |
98 MemoryStrategyResult* result) const; | |
99 | |
100 // Creates a temporary file that we store in our blob temporary directory. The | |
101 // lifetime of the file is tied to the file_reference. | |
102 void CreateTemporaryFileForRenderer( | |
103 uint64_t size_bytes, | |
104 const base::Callback<void(FileCreationInfo)>& done); | |
105 | |
106 void FreeMemory(size_t memory_size_bytes); | |
michaeln
2016/07/07 20:05:21
which method(s) allocate(s) the memory?
| |
107 | |
108 bool MaybeFitInMemoryNow(size_t memory_size_bytes); | |
109 | |
110 // Notifies the caller when a blob of the given size can be stored in our blob | |
111 // system. This assumes the caller has called CanNewBlobFit, and does not | |
112 // check whether the size will fit. | |
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 PendingContructionEntry NotifyWhenMemoryCanPopulated( | |
116 size_t memory_size_bytes, | |
117 const base::Callback<void(bool)>& can_request); | |
118 | |
119 void RemovePendingConstructionEntry(const PendingContructionEntry& 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(uint64_t id); | |
126 | |
127 // Disables the disk. | |
128 void DisableDisk(); | |
129 | |
130 bool disk_enabled() const { return disk_used_; } | |
131 | |
132 size_t GetAvailableMemoryForBlobs() const; | |
133 | |
134 size_t max_ipc_memory_size() const { return max_ipc_memory_size_; } | |
135 size_t max_shared_memory_size() const { return max_shared_memory_size_; } | |
136 size_t max_blob_in_memory_size() const { return max_blob_in_memory_size_; } | |
137 size_t max_blob_memory_space() const { return max_blob_memory_space_; } | |
138 uint64_t max_blob_disk_space() const { return max_blob_disk_space_; } | |
139 size_t in_flight_space() const { return in_flight_space_; } | |
140 uint64_t min_page_file_size() const { return min_page_file_size_; } | |
141 uint64_t max_file_size() const { return max_file_size_; } | |
142 | |
143 void SetMemoryContantsForTesting(size_t max_ipc_memory_size, | |
144 size_t max_shared_memory_size, | |
145 size_t max_blob_in_memory_size, | |
146 size_t max_blob_memory_space, | |
147 uint64_t max_blob_disk_space, | |
148 size_t in_flight_space, | |
149 uint64_t min_page_file_size, | |
150 uint64_t max_file_size) { | |
151 max_ipc_memory_size_ = max_ipc_memory_size; | |
152 max_shared_memory_size_ = max_shared_memory_size; | |
153 max_blob_in_memory_size_ = max_blob_in_memory_size; | |
154 max_blob_memory_space_ = max_blob_memory_space; | |
155 max_blob_disk_space_ = max_blob_disk_space; | |
156 in_flight_space_ = in_flight_space; | |
157 min_page_file_size_ = min_page_file_size; | |
158 max_file_size_ = max_file_size; | |
159 } | |
160 | |
161 private: | |
162 using RecentItemsCache = base::MRUCache<uint64_t, ShareableBlobDataItem*>; | |
163 | |
164 // We add a delete callback to the file reference to decrement our disk usage. | |
165 void OnCreateFile(uint64_t file_size, | |
166 const base::Callback<void(FileCreationInfo)>& done, | |
167 FileCreationInfo result); | |
168 | |
169 void MaybeScheduleWaitingBlobs(); | |
170 | |
171 // Returns the size we'll be freeing. We appropriately increment/decrement | |
172 // the disk, memory, and in flight counts. | |
173 size_t ScheduleBlobPaging(); | |
174 void SchedulePagingUntilWeCanFit(size_t total_memory_needed); | |
175 | |
176 // Called when we've completed paging a list of items | |
177 void OnPagingComplete( | |
178 std::unique_ptr<std::vector<scoped_refptr<ShareableBlobDataItem>>> items, | |
179 size_t total_items_size, | |
180 FileCreationInfo result); | |
181 | |
182 void RecordTracingCounters(); | |
183 | |
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::SingleThreadTaskRunner> file_runner_; | |
195 | |
196 PendingBlobConstructionList blobs_waiting_for_paging_; | |
197 size_t blobs_waiting_for_paging_size_ = 0; | |
198 | |
199 // Constants. | |
200 size_t max_ipc_memory_size_ = kBlobStorageIPCThresholdBytes; | |
201 size_t max_shared_memory_size_ = kBlobStorageMaxSharedMemoryBytes; | |
202 size_t max_blob_in_memory_size_ = kBlobStorageMaxBlobMemorySize; | |
203 size_t max_blob_memory_space_ = kBlobStorageMaxMemoryUsage; | |
204 uint64_t max_blob_disk_space_ = kBlobStorageMaxDiskSpace; | |
205 size_t in_flight_space_ = kBlobStorageInFlightMemory; | |
206 uint64_t min_page_file_size_ = kBlobStorageMinFileSizeBytes; | |
207 uint64_t max_file_size_ = kBlobStorageMaxFileSizeBytes; | |
208 | |
209 bool enable_disk_ = false; | |
210 base::FilePath blob_storage_dir_; | |
211 uint64_t current_file_num_ = 0; | |
212 | |
213 // Lifetime of the ShareableBlobDataItem objects is handles externally in the | |
214 // BlobStorageContext class. | |
215 RecentItemsCache recent_item_cache_; | |
216 | |
217 DISALLOW_COPY_AND_ASSIGN(BlobMemoryController); | |
218 }; | |
219 } // namespace storage | |
220 #endif // STORAGE_BROWSER_BLOB_BLOB_MEMORY_CONTROLLER_H_ | |
OLD | NEW |