Chromium Code Reviews| Index: storage/browser/blob/blob_memory_controller.h |
| diff --git a/storage/browser/blob/blob_memory_controller.h b/storage/browser/blob/blob_memory_controller.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6516902253470e9621f6b42f42b1656660703fd6 |
| --- /dev/null |
| +++ b/storage/browser/blob/blob_memory_controller.h |
| @@ -0,0 +1,227 @@ |
| +// Copyright 2016 The Chromium Authors. All rihts reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef STORAGE_BROWSER_BLOB_BLOB_MEMORY_CONTROLLER_H_ |
| +#define STORAGE_BROWSER_BLOB_BLOB_MEMORY_CONTROLLER_H_ |
| + |
| +#include <stdint.h> |
| + |
| +#include <algorithm> |
| +#include <list> |
| +#include <map> |
| +#include <string> |
| +#include <utility> |
| +#include <vector> |
| + |
| +#include "base/callback.h" |
| +#include "base/containers/mru_cache.h" |
| +#include "base/files/file.h" |
| +#include "base/files/file_path.h" |
| +#include "base/macros.h" |
| +#include "base/memory/ref_counted.h" |
| +#include "base/memory/weak_ptr.h" |
| +#include "base/optional.h" |
| +#include "base/time/time.h" |
| +#include "storage/browser/storage_browser_export.h" |
| +#include "storage/common/blob_storage/blob_storage_constants.h" |
| +#include "storage/common/data_element.h" |
| + |
| +namespace base { |
| +class SingleThreadTaskRunner; |
| +} |
| + |
| +namespace storage { |
| +class ShareableBlobDataItem; |
| +class ShareableFileReference; |
| + |
| +// There should be no memory sizes of 0. |
| +// This class is responsible for |
| +// * our current memory usage for blobs, both on disk and in memory, |
| +// * when we can add a blob w/ a given size, |
| +// * creating temporary files, |
| +// * paging blobs to disk. |
| +class STORAGE_EXPORT BlobMemoryController |
| + : public base::SupportsWeakPtr<BlobMemoryController> { |
| + public: |
| + enum class MemoryStrategyResult { |
| + // We don't have enough memory for this blob. |
| + TOO_LARGE, |
| + // There isn't any memory that needs transporting. |
| + NONE_NEEDED, |
| + // Transportation strategies. |
| + SHORTCUT, |
| + IPC, |
| + SHARED_MEMORY, |
| + FILE |
| + }; |
| + typedef std::list<std::pair<size_t, base::Callback<void(bool)>>> |
| + PendingBlobConstructionList; |
| + typedef PendingBlobConstructionList::iterator PendingContructionEntry; |
| + |
| + struct FileCreationInfo { |
| + FileCreationInfo(); |
| + ~FileCreationInfo(); |
| + FileCreationInfo(FileCreationInfo&& other); |
| + FileCreationInfo& operator=(FileCreationInfo&&); |
| + |
| + base::File::Error error = base::File::FILE_ERROR_FAILED; |
| + scoped_refptr<ShareableFileReference> file_reference; |
| + 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
|
| + base::Time last_modified; |
| + }; |
| + |
| + BlobMemoryController(); |
| + virtual ~BlobMemoryController(); |
| + |
| + 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.
|
| + size_t max_blob_memory_space_bytes, |
| + uint64_t max_blob_disk_space_bytes, |
| + size_t in_flight_space_bytes, |
| + uint64_t min_page_file_size) { |
| + max_blob_in_memory_size_ = max_blob_in_memory_size_bytes; |
| + max_blob_memory_space_ = max_blob_memory_space_bytes; |
| + max_blob_disk_space_ = max_blob_disk_space_bytes; |
| + in_flight_space_ = in_flight_space_bytes; |
| + min_page_file_size_ = min_page_file_size; |
| + } |
| + |
| + 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.
|
| + scoped_refptr<base::SingleThreadTaskRunner> file_runner); |
| + |
| + // Returns if the data in the descriptions is good data (not a bad IPC). If |
| + // SHORTCUT is returned, then the blob can be immediately constructed, and |
| + // the a following call to MaybeFitInMemoryNow will always return true. |
| + bool DecideBlobTransportationMemoryStrategy( |
| + const std::vector<DataElement>& descriptions, |
| + uint64_t* total_bytes, |
| + MemoryStrategyResult* result) const; |
| + |
| + // Creates a temporary file that we store in our blob temporary directory. The |
| + // lifetime of the file is tied to the file_reference. |
| + void CreateTemporaryFileForRenderer( |
| + uint64_t size_bytes, |
| + const base::Callback<void(FileCreationInfo)>& done); |
| + |
| + void FreeMemory(size_t memory_size_bytes); |
| + |
| + enum class MemoryRequestResponse { |
| + SPACE_AVAILABLE, |
| + SPACE_UNAVAILABLE, |
| + PENDING_DISK |
| + }; |
| + |
| + bool CanFitInSystem(uint64_t size) const; |
| + |
| + // We assume the user called CanFitInSystem before this and that the given |
| + // memory size can fit in our system. |
| + // Notifies the caller when a blob of the given size can be stored in our blob |
| + // system. If the returned entry is not present, that means the blob can fit |
| + // right away and the callback won't be called. |
| + // The argument on can_request is true if we can create the blob, and |
| + // false if there was an error and the new blob needs to be broken. |
| + base::Optional<PendingContructionEntry> NotifyWhenMemoryCanPopulated( |
| + size_t memory_size_bytes, |
| + const base::Callback<void(bool)>& can_request); |
| + |
| + void RemovePendingConstructionEntry(const PendingContructionEntry& entry); |
| + |
| + // This updates the given item in our LRU lookup table. |
| + void UpdateBlobItemInRecents(ShareableBlobDataItem* item); |
| + |
| + // This removes the given item from our LRU lookup table. |
| + 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.
|
| + |
| + // Disables the disk. |
| + 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.
|
| + |
| + bool disk_enabled() const { return disk_used_; } |
| + |
| + size_t max_ipc_memory_size() const { return max_ipc_memory_size_; } |
| + size_t max_shared_memory_size() const { return max_shared_memory_size_; } |
| + size_t max_blob_in_memory_size() const { return max_blob_in_memory_size_; } |
| + size_t max_blob_memory_space() const { return max_blob_memory_space_; } |
| + uint64_t max_blob_disk_space() const { return max_blob_disk_space_; } |
| + size_t in_flight_space() const { return in_flight_space_; } |
| + uint64_t min_page_file_size() const { return min_page_file_size_; } |
| + uint64_t max_file_size() const { return max_file_size_; } |
| + |
| + void SetMemoryContantsForTesting(size_t max_ipc_memory_size, |
| + size_t max_shared_memory_size, |
| + size_t max_blob_in_memory_size, |
| + size_t max_blob_memory_space, |
| + uint64_t max_blob_disk_space, |
| + size_t in_flight_space, |
| + uint64_t min_page_file_size, |
| + uint64_t max_file_size) { |
| + max_ipc_memory_size_ = max_ipc_memory_size; |
| + max_shared_memory_size_ = max_shared_memory_size; |
| + max_blob_in_memory_size_ = max_blob_in_memory_size; |
| + max_blob_memory_space_ = max_blob_memory_space; |
| + max_blob_disk_space_ = max_blob_disk_space; |
| + in_flight_space_ = in_flight_space; |
| + min_page_file_size_ = min_page_file_size; |
| + max_file_size_ = max_file_size; |
| + } |
| + |
| + private: |
| + using RecentItemsCache = base::MRUCache<uint64_t, ShareableBlobDataItem*>; |
| + |
| + // We add a delete callback to the file reference to decrement our disk usage. |
| + void OnCreateFile(uint64_t file_size, |
| + const base::Callback<void(FileCreationInfo)>& done, |
| + FileCreationInfo result); |
| + |
| + void MaybeScheduleWaitingBlobs(); |
| + |
| + // Returns the size we'll be freeing. We appropriately increment/decrement |
| + // the disk, memory, and in flight counts. |
| + size_t ScheduleBlobPaging(); |
| + void SchedulePagingUntilWeCanFit(size_t total_memory_needed); |
| + |
| + // Called when we've completed paging a list of items |
| + void OnPagingComplete( |
| + std::unique_ptr<std::vector<scoped_refptr<ShareableBlobDataItem>>> items, |
| + size_t total_items_size, |
| + FileCreationInfo result); |
| + |
| + void RecordTracingCounters(); |
| + |
| + size_t GetAvailableMemoryForBlobs() const; |
| + uint64_t GetAvailableDiskSpaceForBlobs() const; |
| + |
| + // We decrement the disk space used. |
| + void OnBlobFileDelete(uint64_t size, const base::FilePath& path); |
| + |
| + // Memory bookkeeping. |
| + size_t blob_memory_used_ = 0; |
| + size_t in_flight_memory_used_ = 0; |
| + uint64_t disk_used_ = 0; |
| + |
| + scoped_refptr<base::SingleThreadTaskRunner> file_runner_; |
| + |
| + PendingBlobConstructionList blobs_waiting_for_paging_; |
| + size_t blobs_waiting_for_paging_size_ = 0; |
| + |
| + // Constants. |
| + size_t max_ipc_memory_size_ = kBlobStorageIPCThresholdBytes; |
| + size_t max_shared_memory_size_ = kBlobStorageMaxSharedMemoryBytes; |
| + size_t max_blob_in_memory_size_ = kBlobStorageMaxBlobMemorySize; |
| + size_t max_blob_memory_space_ = kBlobStorageMaxMemoryUsage; |
| + uint64_t max_blob_disk_space_ = kBlobStorageMaxDiskSpace; |
| + size_t in_flight_space_ = kBlobStorageInFlightMemory; |
| + uint64_t min_page_file_size_ = kBlobStorageMinFileSizeBytes; |
| + uint64_t max_file_size_ = kBlobStorageMaxFileSizeBytes; |
| + |
| + bool enable_disk_ = false; |
| + base::FilePath blob_storage_dir_; |
| + uint64_t current_file_num_ = 0; |
| + |
| + // Lifetime of the ShareableBlobDataItem objects is handles externally in the |
| + // BlobStorageContext class. |
| + RecentItemsCache recent_item_cache_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(BlobMemoryController); |
| +}; |
| +} // namespace storage |
| +#endif // STORAGE_BROWSER_BLOB_BLOB_MEMORY_CONTROLLER_H_ |