OLD | NEW |
(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. |
| 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: |
| 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_; } |
| 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. |
| 92 void CreateTemporaryFileForRenderer( |
| 93 uint64_t size_bytes, |
| 94 const base::Callback<void(FileCreationInfo)>& file_callback); |
| 95 |
| 96 void FreeMemory(size_t memory_size_bytes); |
| 97 |
| 98 // Checks to see if the given size can fit in the system. |
| 99 bool CanFitInSystem(uint64_t size) const; |
| 100 |
| 101 // We assume the user called CanFitInSystem before this and it returned true. |
| 102 // Notifies the caller when a blob of the given size can be stored in our blob |
| 103 // system. If the returned entry is not present, that means the blob can fit |
| 104 // right away and the callback won't be called. |
| 105 // The argument on can_request is true if we can create the blob, and |
| 106 // false if there was an error and the new blob needs to be broken. |
| 107 base::Optional<PendingConstructionEntry> NotifyWhenMemoryCanPopulated( |
| 108 size_t memory_size_bytes, |
| 109 const base::Callback<void(bool)>& can_request); |
| 110 |
| 111 // If we no longer need memory requsted from NotifyWhenMemoryCanPopulated |
| 112 // before |can_request| has been called above, you can call this method to |
| 113 // correctly free the requested memory. |
| 114 void RemovePendingConstructionEntry(const PendingConstructionEntry& entry); |
| 115 |
| 116 // This updates the given item in our LRU lookup table. |
| 117 void UpdateBlobItemInRecents(ShareableBlobDataItem* item); |
| 118 |
| 119 // This removes the given item from our LRU lookup table. |
| 120 void RemoveBlobItemInRecents(const ShareableBlobDataItem& item); |
| 121 |
| 122 size_t memory_usage() const { |
| 123 return blob_memory_used_ + in_flight_memory_used_; |
| 124 } |
| 125 uint64_t disk_usage() const { return disk_used_; } |
| 126 |
| 127 size_t max_ipc_memory_size() const { return max_ipc_memory_size_; } |
| 128 size_t max_shared_memory_size() const { return max_shared_memory_size_; } |
| 129 size_t max_blob_in_memory_size() const { return max_blob_in_memory_size_; } |
| 130 size_t max_blob_memory_space() const { return max_blob_memory_space_; } |
| 131 uint64_t max_blob_disk_space() const { return max_blob_disk_space_; } |
| 132 size_t in_flight_space() const { return in_flight_space_; } |
| 133 uint64_t min_page_file_size() const { return min_page_file_size_; } |
| 134 uint64_t max_file_size() const { return max_file_size_; } |
| 135 |
| 136 void SetMemoryConstantsForTesting(size_t max_ipc_memory_size, |
| 137 size_t max_shared_memory_size, |
| 138 size_t max_blob_in_memory_size, |
| 139 size_t max_blob_memory_space, |
| 140 uint64_t max_blob_disk_space, |
| 141 size_t in_flight_space, |
| 142 uint64_t min_page_file_size, |
| 143 uint64_t max_file_size) { |
| 144 max_ipc_memory_size_ = max_ipc_memory_size; |
| 145 max_shared_memory_size_ = max_shared_memory_size; |
| 146 max_blob_in_memory_size_ = max_blob_in_memory_size; |
| 147 max_blob_memory_space_ = max_blob_memory_space; |
| 148 max_blob_disk_space_ = max_blob_disk_space; |
| 149 in_flight_space_ = in_flight_space; |
| 150 min_page_file_size_ = min_page_file_size; |
| 151 max_file_size_ = max_file_size; |
| 152 } |
| 153 |
| 154 private: |
| 155 using RecentItemsCache = base::MRUCache<uint64_t, ShareableBlobDataItem*>; |
| 156 |
| 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. |
| 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_; |
| 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 size_t max_ipc_memory_size_ = kBlobStorageIPCThresholdBytes; |
| 200 size_t max_shared_memory_size_ = kBlobStorageMaxSharedMemoryBytes; |
| 201 size_t max_blob_in_memory_size_ = kBlobStorageMaxBlobMemorySize; |
| 202 size_t max_blob_memory_space_ = kBlobStorageMaxMemoryUsage; |
| 203 uint64_t max_blob_disk_space_ = kBlobStorageMaxDiskSpace; |
| 204 size_t in_flight_space_ = kBlobStorageInFlightMemory; |
| 205 uint64_t min_page_file_size_ = kBlobStorageMinFileSizeBytes; |
| 206 uint64_t max_file_size_ = kBlobStorageMaxFileSizeBytes; |
| 207 |
| 208 bool enable_disk_ = false; |
| 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 RecentItemsCache 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_ |
OLD | NEW |