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

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: Finished comments, added new pending enum state Created 4 years, 5 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 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/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
29 namespace base {
30 class TaskRunner;
31 }
32
33 namespace storage {
34 class DataElement;
35 class ShareableBlobDataItem;
36 class ShareableFileReference;
37
38 // There should be no memory sizes of 0.
kinuko 2016/07/17 16:15:46 This sentence doesn't make a lot sense without con
dmurph 2016/07/19 02:26:27 Done.
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,
kinuko 2016/07/17 16:15:46 nit: now that we started to have a real 'temporary
dmurph 2016/07/19 02:26:27 Done.
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,
51 // Transportation strategies.
52 IPC,
53 SHARED_MEMORY,
54 FILE
55 };
56 using PendingBlobConstructionList =
57 std::list<std::pair<size_t, base::Callback<void(bool)>>>;
58 using PendingConstructionEntry = PendingBlobConstructionList::iterator;
59
60 struct FileCreationInfo {
61 FileCreationInfo();
62 ~FileCreationInfo();
63 FileCreationInfo(FileCreationInfo&& other);
64 FileCreationInfo& operator=(FileCreationInfo&&);
65
66 base::File::Error error = base::File::FILE_ERROR_FAILED;
67 scoped_refptr<ShareableFileReference> file_reference;
68 base::File file;
69 base::Time last_modified;
70 };
71
72 BlobMemoryController();
73 virtual ~BlobMemoryController();
74
75 void EnableDisk(const base::FilePath& storage_directory,
76 scoped_refptr<base::TaskRunner> file_runner);
77
78 // Disables the disk. This cancels all pending file creations and paging
79 // operations.
80 void DisableDisk();
81
82 bool disk_enabled() const { return disk_used_; }
kinuko 2016/07/17 16:15:46 should this return enable_disk_ ??
dmurph 2016/07/19 02:26:27 oops! Thanks.
83
84 // Returns if the data in the descriptions is good data (not a bad IPC).
85 bool DecideBlobTransportationMemoryStrategy(
86 const std::vector<DataElement>& descriptions,
87 uint64_t* total_bytes,
88 MemoryStrategyResult* result) const;
89
90 // Creates a temporary file that we store in our blob temporary directory. The
91 // lifetime of the file is tied to the file_reference. This method 'allocates'
92 // the disk memory right away.
93 // NOTE: We assume the user checked CanFitInSystem before this, as we don't
94 // inspect quotas.
95 void CreateTemporaryFile(
96 uint64_t size_bytes,
97 const base::Callback<void(FileCreationInfo)>& file_callback);
98
99 void FreeMemory(size_t memory_size_bytes);
100
101 // Checks to see if the given size can fit in the system.
102 bool CanFitInSystem(uint64_t size) const;
103
104 // This method 'allocates' memory for the user, and we notify the caller when
105 // a blob of the given size can be stored in our blob system. If the returned
106 // entry is not present, that means the blob can fit right away and the
107 // callback won't be called. If we return a pending entry, then the memory
108 // isn't 'allocated' until |can_request| is called with true. IF the argument
109 // is false then there was an error and the new blob needs to be broken.
110 // NOTE: We assume the user checked CanFitInSystem before this, as we don't
111 // inspect quotas.
112 base::Optional<PendingConstructionEntry> NotifyWhenMemoryCanPopulated(
113 size_t memory_size_bytes,
114 const base::Callback<void(bool)>& can_request);
115
116 // If we no longer need memory requsted from NotifyWhenMemoryCanPopulated
117 // before |can_request| has been called above, you can call this method to
118 // correctly free the requested memory.
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 uint64_t max_blob_disk_space() const { return max_blob_disk_space_; }
136 size_t in_flight_space() const { return in_flight_space_; }
137 uint64_t min_page_file_size() const { return min_page_file_size_; }
138 uint64_t max_file_size() const { return max_file_size_; }
139
140 void SetMemoryConstantsForTesting(size_t max_ipc_memory_size,
141 size_t max_shared_memory_size,
142 size_t max_blob_in_memory_size,
143 uint64_t max_blob_disk_space,
144 size_t in_flight_space,
145 uint64_t min_page_file_size,
146 uint64_t max_file_size) {
147 max_ipc_memory_size_ = max_ipc_memory_size;
148 max_shared_memory_size_ = max_shared_memory_size;
149 max_blob_in_memory_size_ = max_blob_in_memory_size;
150 max_blob_disk_space_ = max_blob_disk_space;
151 in_flight_space_ = in_flight_space;
152 min_page_file_size_ = min_page_file_size;
153 max_file_size_ = max_file_size;
154 }
155
156 private:
157 // We add a delete callback to the file reference to decrement our disk usage.
158 void OnCreateFile(uint64_t file_size,
159 const base::Callback<void(FileCreationInfo)>& done,
160 FileCreationInfo result);
161
162 void MaybeScheduleWaitingBlobs();
163
164 bool HasEnoughMemoryToPage() {
165 return recent_item_cache_bytes_ >= min_page_file_size_;
166 }
167
168 // We schedule paging for all memory over our in memory limit, including
169 // any memory waiting to be allocated. We appropriately increment/decrement
170 // the disk, memory, and in flight counts.
171 void MaybeSchedulePagingUntilSystemHealthy();
172
173 // Called when we've completed paging a list of items
174 void OnPagingComplete(
175 std::unique_ptr<std::vector<scoped_refptr<ShareableBlobDataItem>>> items,
176 size_t total_items_size,
177 FileCreationInfo result);
178
179 void RecordTracingCounters();
180
181 size_t GetAvailableMemoryForBlobs() const;
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.
kinuko 2016/07/17 16:15:46 Please add more comments for bookkeeping fields.
dmurph 2016/07/19 02:26:27 Done.
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::TaskRunner> file_runner_;
kinuko 2016/07/17 16:15:46 Move this above or below other memory bookkeeping
dmurph 2016/07/19 02:26:27 Done.
193
194 size_t pending_pagings_ = 0;
195 PendingBlobConstructionList blobs_waiting_for_paging_;
196 size_t blobs_waiting_for_paging_size_ = 0;
197
198 // Constants.
199 // TODO(dmurph): consolidate these into a struct.
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 uint64_t max_blob_disk_space_ = kBlobStorageMaxDiskSpace;
204 size_t in_flight_space_ = kBlobStorageInFlightMemory;
kinuko 2016/07/17 16:15:46 nit: why some are called 'space' while others are
dmurph 2016/07/19 02:26:27 Done.
205 uint64_t min_page_file_size_ = kBlobStorageMinFileSizeBytes;
206 uint64_t max_file_size_ = kBlobStorageMaxFileSizeBytes;
207
208 bool enable_disk_ = false;
kinuko 2016/07/17 16:15:46 disk_enabled_ ??
dmurph 2016/07/19 02:26:27 Done.
209 base::FilePath blob_storage_dir_;
210 uint64_t current_file_num_ = 0;
211
212 // Lifetime of the ShareableBlobDataItem objects is handled externally in the
213 // BlobStorageContext class.
214 base::MRUCache<uint64_t, ShareableBlobDataItem*> recent_item_cache_;
215 size_t recent_item_cache_bytes_ = 0;
216
217 base::WeakPtrFactory<BlobMemoryController> ptr_factory_;
218
219 DISALLOW_COPY_AND_ASSIGN(BlobMemoryController);
220 };
221 } // namespace storage
222 #endif // STORAGE_BROWSER_BLOB_BLOB_MEMORY_CONTROLLER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698