Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(6)

Side by Side Diff: storage/browser/blob/blob_memory_controller.h

Issue 2055053003: [BlobAsync] Disk support for blob storage (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fixes, working w/ Layout tests Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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;
michaeln 2016/07/07 20:05:21 Is the base::Optional wrapper really needed? The d
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).
93 bool DecideBlobTransportationMemoryStrategy(
94 const std::vector<DataElement>& descriptions,
95 uint64_t* total_bytes,
96 MemoryStrategyResult* result) const;
97
98 // Creates a temporary file that we store in our blob temporary directory. The
99 // lifetime of the file is tied to the file_reference.
100 void CreateTemporaryFileForRenderer(
101 uint64_t size_bytes,
102 const base::Callback<void(FileCreationInfo)>& done);
103
104 void FreeMemory(size_t memory_size_bytes);
105
106 bool MaybeFitInMemoryNow(size_t memory_size_bytes);
107
108 // Notifies the caller when a blob of the given size can be stored in our blob
109 // system. This assumes the caller has called CanNewBlobFit, and does not
110 // check whether the size will fit.
111 // The argument on can_request is true if we can create the blob, and
112 // false if there was an error and the new blob needs to be broken.
113 PendingContructionEntry NotifyWhenMemoryCanPopulated(
114 size_t memory_size_bytes,
115 const base::Callback<void(bool)>& can_request);
116
117 void RemovePendingConstructionEntry(const PendingContructionEntry& entry);
118
119 // This updates the given item in our LRU lookup table.
120 void UpdateBlobItemInRecents(ShareableBlobDataItem* item);
121
122 // This removes the given item from our LRU lookup table.
123 void RemoveBlobItemInRecents(uint64_t id);
124
125 // Disables the disk.
126 void DisableDisk();
127
128 bool disk_enabled() const { return disk_used_; }
129
130 size_t GetAvailableMemoryForBlobs() const;
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 SetMemoryContantsForTesting(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,
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) {
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 uint64_t GetAvailableDiskSpaceForBlobs() const;
183
184 // We decrement the disk space used.
185 void OnBlobFileDelete(uint64_t size, const base::FilePath& path);
186
187 // Memory bookkeeping.
188 size_t blob_memory_used_ = 0;
189 size_t in_flight_memory_used_ = 0;
190 uint64_t disk_used_ = 0;
191
192 scoped_refptr<base::SingleThreadTaskRunner> file_runner_;
193
194 PendingBlobConstructionList blobs_waiting_for_paging_;
195 size_t blobs_waiting_for_paging_size_ = 0;
196
197 // Constants.
198 size_t max_ipc_memory_size_ = kBlobStorageIPCThresholdBytes;
199 size_t max_shared_memory_size_ = kBlobStorageMaxSharedMemoryBytes;
200 size_t max_blob_in_memory_size_ = kBlobStorageMaxBlobMemorySize;
201 size_t max_blob_memory_space_ = kBlobStorageMaxMemoryUsage;
202 uint64_t max_blob_disk_space_ = kBlobStorageMaxDiskSpace;
203 size_t in_flight_space_ = kBlobStorageInFlightMemory;
204 uint64_t min_page_file_size_ = kBlobStorageMinFileSizeBytes;
205 uint64_t max_file_size_ = kBlobStorageMaxFileSizeBytes;
206
207 bool enable_disk_ = false;
208 base::FilePath blob_storage_dir_;
209 uint64_t current_file_num_ = 0;
210
211 // Lifetime of the ShareableBlobDataItem objects is handles externally in the
212 // BlobStorageContext class.
213 RecentItemsCache recent_item_cache_;
214
215 DISALLOW_COPY_AND_ASSIGN(BlobMemoryController);
216 };
217 } // namespace storage
218 #endif // STORAGE_BROWSER_BLOB_BLOB_MEMORY_CONTROLLER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698