Chromium Code Reviews| 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; | |
|
kinuko
2016/07/07 16:11:44
base::File has invalid/empty status, is using Opti
michaeln
2016/07/07 20:05:23
The base::File object is inexpensive to construct
dmurph
2016/07/11 23:33:27
We don't always need it. Changed back to regular f
| |
| 71 base::Time last_modified; | |
| 72 }; | |
| 73 | |
| 74 BlobMemoryController(); | |
| 75 virtual ~BlobMemoryController(); | |
| 76 | |
| 77 void SetMemoryLimits(size_t max_blob_in_memory_size_bytes, | |
|
kinuko
2016/07/07 16:11:44
Do we need this in addition to / separately from S
dmurph
2016/07/11 23:33:27
Done.
| |
| 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, | |
|
kinuko
2016/07/07 16:11:44
nit: const ref?
dmurph
2016/07/11 23:33:27
Done.
| |
| 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); | |
| 107 | |
| 108 enum class MemoryRequestResponse { | |
| 109 SPACE_AVAILABLE, | |
| 110 SPACE_UNAVAILABLE, | |
| 111 PENDING_DISK | |
| 112 }; | |
| 113 | |
| 114 bool CanFitInSystem(uint64_t size) const; | |
| 115 | |
| 116 // We assume the user called CanFitInSystem before this and that the given | |
| 117 // memory size can fit in our system. | |
| 118 // Notifies the caller when a blob of the given size can be stored in our blob | |
| 119 // system. If the returned entry is not present, that means the blob can fit | |
| 120 // right away and the callback won't be called. | |
| 121 // The argument on can_request is true if we can create the blob, and | |
| 122 // false if there was an error and the new blob needs to be broken. | |
| 123 base::Optional<PendingContructionEntry> NotifyWhenMemoryCanPopulated( | |
| 124 size_t memory_size_bytes, | |
| 125 const base::Callback<void(bool)>& can_request); | |
| 126 | |
| 127 void RemovePendingConstructionEntry(const PendingContructionEntry& entry); | |
| 128 | |
| 129 // This updates the given item in our LRU lookup table. | |
| 130 void UpdateBlobItemInRecents(ShareableBlobDataItem* item); | |
| 131 | |
| 132 // This removes the given item from our LRU lookup table. | |
| 133 void RemoveBlobItemInRecents(uint64_t id); | |
|
michaeln
2016/07/07 20:05:23
for symmetry, maybe have this method take the |ite
dmurph
2016/07/11 23:33:27
Done.
| |
| 134 | |
| 135 // Disables the disk. | |
| 136 void DisableDisk(); | |
|
kinuko
2016/07/07 16:11:44
nit: I feel this and disk_enabled() should be plac
dmurph
2016/07/11 23:33:27
Done.
| |
| 137 | |
| 138 bool disk_enabled() const { return disk_used_; } | |
| 139 | |
| 140 size_t max_ipc_memory_size() const { return max_ipc_memory_size_; } | |
| 141 size_t max_shared_memory_size() const { return max_shared_memory_size_; } | |
| 142 size_t max_blob_in_memory_size() const { return max_blob_in_memory_size_; } | |
| 143 size_t max_blob_memory_space() const { return max_blob_memory_space_; } | |
| 144 uint64_t max_blob_disk_space() const { return max_blob_disk_space_; } | |
| 145 size_t in_flight_space() const { return in_flight_space_; } | |
| 146 uint64_t min_page_file_size() const { return min_page_file_size_; } | |
| 147 uint64_t max_file_size() const { return max_file_size_; } | |
| 148 | |
| 149 void SetMemoryContantsForTesting(size_t max_ipc_memory_size, | |
| 150 size_t max_shared_memory_size, | |
| 151 size_t max_blob_in_memory_size, | |
| 152 size_t max_blob_memory_space, | |
| 153 uint64_t max_blob_disk_space, | |
| 154 size_t in_flight_space, | |
| 155 uint64_t min_page_file_size, | |
| 156 uint64_t max_file_size) { | |
| 157 max_ipc_memory_size_ = max_ipc_memory_size; | |
| 158 max_shared_memory_size_ = max_shared_memory_size; | |
| 159 max_blob_in_memory_size_ = max_blob_in_memory_size; | |
| 160 max_blob_memory_space_ = max_blob_memory_space; | |
| 161 max_blob_disk_space_ = max_blob_disk_space; | |
| 162 in_flight_space_ = in_flight_space; | |
| 163 min_page_file_size_ = min_page_file_size; | |
| 164 max_file_size_ = max_file_size; | |
| 165 } | |
| 166 | |
| 167 private: | |
| 168 using RecentItemsCache = base::MRUCache<uint64_t, ShareableBlobDataItem*>; | |
| 169 | |
| 170 // We add a delete callback to the file reference to decrement our disk usage. | |
| 171 void OnCreateFile(uint64_t file_size, | |
| 172 const base::Callback<void(FileCreationInfo)>& done, | |
| 173 FileCreationInfo result); | |
| 174 | |
| 175 void MaybeScheduleWaitingBlobs(); | |
| 176 | |
| 177 // Returns the size we'll be freeing. We appropriately increment/decrement | |
| 178 // the disk, memory, and in flight counts. | |
| 179 size_t ScheduleBlobPaging(); | |
| 180 void SchedulePagingUntilWeCanFit(size_t total_memory_needed); | |
| 181 | |
| 182 // Called when we've completed paging a list of items | |
| 183 void OnPagingComplete( | |
| 184 std::unique_ptr<std::vector<scoped_refptr<ShareableBlobDataItem>>> items, | |
| 185 size_t total_items_size, | |
| 186 FileCreationInfo result); | |
| 187 | |
| 188 void RecordTracingCounters(); | |
| 189 | |
| 190 size_t GetAvailableMemoryForBlobs() const; | |
| 191 uint64_t GetAvailableDiskSpaceForBlobs() const; | |
| 192 | |
| 193 // We decrement the disk space used. | |
| 194 void OnBlobFileDelete(uint64_t size, const base::FilePath& path); | |
| 195 | |
| 196 // Memory bookkeeping. | |
| 197 size_t blob_memory_used_ = 0; | |
| 198 size_t in_flight_memory_used_ = 0; | |
| 199 uint64_t disk_used_ = 0; | |
| 200 | |
| 201 scoped_refptr<base::SingleThreadTaskRunner> file_runner_; | |
| 202 | |
| 203 PendingBlobConstructionList blobs_waiting_for_paging_; | |
| 204 size_t blobs_waiting_for_paging_size_ = 0; | |
| 205 | |
| 206 // Constants. | |
| 207 size_t max_ipc_memory_size_ = kBlobStorageIPCThresholdBytes; | |
| 208 size_t max_shared_memory_size_ = kBlobStorageMaxSharedMemoryBytes; | |
| 209 size_t max_blob_in_memory_size_ = kBlobStorageMaxBlobMemorySize; | |
| 210 size_t max_blob_memory_space_ = kBlobStorageMaxMemoryUsage; | |
| 211 uint64_t max_blob_disk_space_ = kBlobStorageMaxDiskSpace; | |
| 212 size_t in_flight_space_ = kBlobStorageInFlightMemory; | |
| 213 uint64_t min_page_file_size_ = kBlobStorageMinFileSizeBytes; | |
| 214 uint64_t max_file_size_ = kBlobStorageMaxFileSizeBytes; | |
| 215 | |
| 216 bool enable_disk_ = false; | |
| 217 base::FilePath blob_storage_dir_; | |
| 218 uint64_t current_file_num_ = 0; | |
| 219 | |
| 220 // Lifetime of the ShareableBlobDataItem objects is handles externally in the | |
| 221 // BlobStorageContext class. | |
| 222 RecentItemsCache recent_item_cache_; | |
| 223 | |
| 224 DISALLOW_COPY_AND_ASSIGN(BlobMemoryController); | |
| 225 }; | |
| 226 } // namespace storage | |
| 227 #endif // STORAGE_BROWSER_BLOB_BLOB_MEMORY_CONTROLLER_H_ | |
| OLD | NEW |