Chromium Code Reviews| Index: storage/browser/blob/blob_memory_controller.cc |
| diff --git a/storage/browser/blob/blob_memory_controller.cc b/storage/browser/blob/blob_memory_controller.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..92870ac86b37ab13eea46f70c6d2357874a2e809 |
| --- /dev/null |
| +++ b/storage/browser/blob/blob_memory_controller.cc |
| @@ -0,0 +1,652 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "storage/browser/blob/blob_memory_controller.h" |
| + |
| +#include <algorithm> |
| + |
| +#include "base/callback.h" |
| +#include "base/callback_helpers.h" |
| +#include "base/containers/small_map.h" |
| +#include "base/files/file_util.h" |
| +#include "base/location.h" |
| +#include "base/memory/ptr_util.h" |
| +#include "base/metrics/histogram_macros.h" |
| +#include "base/numerics/safe_conversions.h" |
| +#include "base/numerics/safe_math.h" |
| +#include "base/single_thread_task_runner.h" |
| +#include "base/single_thread_task_runner.h" |
| +#include "base/strings/string_number_conversions.h" |
| +#include "base/task_runner.h" |
| +#include "base/task_runner_util.h" |
| +#include "base/time/time.h" |
| +#include "base/trace_event/trace_event.h" |
| +#include "base/tuple.h" |
| +#include "storage/browser/blob/blob_data_builder.h" |
| +#include "storage/browser/blob/blob_data_item.h" |
| +#include "storage/browser/blob/shareable_blob_data_item.h" |
| +#include "storage/browser/blob/shareable_file_reference.h" |
| +#include "storage/common/data_element.h" |
| + |
| +using base::File; |
| +using base::FilePath; |
| +using FileCreationInfo = storage::BlobMemoryController::FileCreationInfo; |
| + |
| +namespace storage { |
| +namespace { |
| + |
| +// Creates a file in the given directory w/ the given filename and size. |
| +std::vector<BlobMemoryController::FileCreationInfo> CreateFiles( |
| + std::vector<scoped_refptr<ShareableFileReference>> file_references) { |
| + LOG(ERROR) << "creating files for renderer"; |
| + std::vector<BlobMemoryController::FileCreationInfo> result; |
| + |
| + for (scoped_refptr<ShareableFileReference>& file_ref : file_references) { |
| + result.push_back(BlobMemoryController::FileCreationInfo()); |
| + BlobMemoryController::FileCreationInfo& creation_info = result.back(); |
| + // Try to open our file. |
| + File file(file_ref->path(), File::FLAG_CREATE_ALWAYS | File::FLAG_WRITE); |
| + creation_info.file_reference = std::move(file_ref); |
| + creation_info.error = file.error_details(); |
| + UMA_HISTOGRAM_ENUMERATION("Storage.Blob.TransportFileCreate", |
| + -creation_info.error, -File::FILE_ERROR_MAX); |
| + if (creation_info.error != File::FILE_OK) |
| + return std::vector<BlobMemoryController::FileCreationInfo>(); |
| + |
| + // Grab the file info to get the "last modified" time and store the file. |
| + File::Info file_info; |
| + bool success = file.GetInfo(&file_info); |
| + UMA_HISTOGRAM_BOOLEAN("Storage.Blob.TransportFileInfoSuccess", success); |
| + creation_info.error = success ? File::FILE_OK : File::FILE_ERROR_FAILED; |
| + if (!success) |
| + return std::vector<BlobMemoryController::FileCreationInfo>(); |
| + creation_info.file = std::move(file); |
| + } |
| + LOG(ERROR) << "Done! Returning " << result.size() << " files."; |
| + return result; |
| +} |
| + |
| +BlobMemoryController::FileCreationInfo WriteItemsToFile( |
| + std::vector<scoped_refptr<ShareableBlobDataItem>>* items, |
| + size_t total_size_bytes, |
| + scoped_refptr<ShareableFileReference> file_reference) { |
|
michaeln
2016/08/23 01:31:54
ShareableFileRefs are not thread-safe so this has
|
| + DCHECK_NE(0u, total_size_bytes); |
| + LOG(ERROR) << "writing to file!"; |
| + UMA_HISTOGRAM_MEMORY_KB("Storage.Blob.PageFileSize", total_size_bytes / 1024); |
| + |
| + // Create our file. |
| + BlobMemoryController::FileCreationInfo creation_info; |
| + File file(file_reference->path(), |
| + File::FLAG_CREATE_ALWAYS | File::FLAG_WRITE); |
| + creation_info.file_reference = std::move(file_reference); |
| + creation_info.error = file.error_details(); |
| + UMA_HISTOGRAM_ENUMERATION("Storage.Blob.PageFileCreate", -creation_info.error, |
| + -File::FILE_ERROR_MAX); |
| + if (creation_info.error != File::FILE_OK) |
| + return creation_info; |
| + |
| + // Write data. |
| + file.SetLength(total_size_bytes); |
| + int bytes_written = 0; |
| + for (const auto& refptr : *items) { |
| + const DataElement& element = refptr->item()->data_element(); |
| + DCHECK_EQ(DataElement::TYPE_BYTES, element.type()); |
| + size_t length = base::checked_cast<size_t>(element.length()); |
| + size_t bytes_left = length; |
| + while (bytes_left > 0) { |
| + bytes_written = |
| + file.WriteAtCurrentPos(element.bytes() + (length - bytes_left), |
| + base::saturated_cast<int>(bytes_left)); |
| + if (bytes_written < 0) |
| + break; |
| + DCHECK_LE(static_cast<size_t>(bytes_written), bytes_left); |
| + bytes_left -= bytes_written; |
| + } |
| + if (bytes_written < 0) |
| + break; |
| + } |
| + UMA_HISTOGRAM_BOOLEAN("Storage.Blob.PageFileWriteSuccess", bytes_written > 0); |
| + |
| + // Grab our modification time and create our SharedFileReference to manage the |
| + // lifetime of the file. |
| + File::Info info; |
| + bool success = file.GetInfo(&info); |
| + UMA_HISTOGRAM_BOOLEAN("Storage.Blob.PageFileInfoSuccess", success); |
| + creation_info.error = |
| + bytes_written < 0 || !success ? File::FILE_ERROR_FAILED : File::FILE_OK; |
| + creation_info.last_modified = info.last_modified; |
| + return creation_info; |
| +} |
| + |
| +// Here so we can destruct files on the correct thread if the user cancels. |
| +void DestructFiles(std::vector<BlobMemoryController::FileCreationInfo> files) {} |
| + |
| +} // namespace |
| + |
| +BlobMemoryController::FileCreationInfo::FileCreationInfo() {} |
| + |
| +BlobMemoryController::FileCreationInfo::~FileCreationInfo() {} |
| + |
| +FileCreationInfo::FileCreationInfo(FileCreationInfo&&) = default; |
| +FileCreationInfo& FileCreationInfo::operator=(FileCreationInfo&&) = default; |
| + |
| +BlobMemoryController::PendingQuotaEntry::PendingQuotaEntry( |
| + uint64_t disk_quota_entry) |
| + : disk_quota_entry(disk_quota_entry) {} |
| +BlobMemoryController::PendingQuotaEntry::PendingQuotaEntry( |
| + PendingBlobConstructionList::iterator memory_quota_entry) |
| + : memory_quota_entry(memory_quota_entry) {} |
| +BlobMemoryController::PendingQuotaEntry::PendingQuotaEntry( |
| + const PendingQuotaEntry& other) = default; |
| + |
| +BlobMemoryController::PendingQuotaEntry::~PendingQuotaEntry() {} |
| + |
| +BlobMemoryController::BlobMemoryController() |
| + : recent_item_cache_( |
| + base::MRUCache<uint64_t, ShareableBlobDataItem*>::NO_AUTO_EVICT), |
| + ptr_factory_(this) {} |
| + |
| +BlobMemoryController::~BlobMemoryController() {} |
| + |
| +void BlobMemoryController::EnableDisk( |
| + const base::FilePath& storage_directory, |
| + scoped_refptr<base::TaskRunner> file_runner) { |
| + LOG(ERROR) << "enabling disk"; |
| + DCHECK(!storage_directory.empty()); |
| + file_runner_ = std::move(file_runner); |
| + blob_storage_dir_ = storage_directory; |
| + disk_enabled_ = true; |
| +} |
| + |
| +void BlobMemoryController::DisableDisk() { |
| + disk_enabled_ = false; |
| + blob_memory_used_ += in_flight_memory_used_; |
| + in_flight_memory_used_ = 0; |
| + pending_file_opens_.clear(); |
| + items_saving_to_disk_.clear(); |
| + for (const auto& size_callback_pair : blobs_waiting_for_paging_) |
| + size_callback_pair.second.Run(false, std::vector<FileCreationInfo>()); |
| + pending_pagings_ = 0; |
| + blobs_waiting_for_paging_.clear(); |
| + blobs_waiting_for_paging_size_ = 0; |
| + recent_item_cache_.Clear(); |
| + recent_item_cache_bytes_ = 0; |
| + RecordTracingCounters(); |
| +} |
| + |
| +BlobMemoryController::MemoryStrategyResult |
| +BlobMemoryController::DecideBlobTransportationMemoryStrategy( |
| + size_t shortcut_bytes, |
| + uint64_t total_transportation_bytes) const { |
| + // Step 1: Handle case where we have no memory to transport. |
|
michaeln
2016/08/23 01:31:54
seems like the the step n comments in this method
|
| + if (total_transportation_bytes == 0) { |
| + return MemoryStrategyResult::NONE_NEEDED; |
| + } |
| + // Step 2: Check if we have enough memory to store the blob. |
| + // We check both individually, as when something is stored straight to disk |
|
Marijn Kruisselbrink
2016/08/05 23:23:54
Not sure what "both" refers to here? You only chec
dmurph
2016/08/19 00:18:33
Done.
|
| + // we only use disk. So it must fit in the available disk space by itself. |
| + if (!CanFitInSystem(total_transportation_bytes)) { |
| + return MemoryStrategyResult::TOO_LARGE; |
| + } |
| + // From here on, we know we can fit the blob in memory or on disk. |
| + // Step 3: Decide if we're using the shortcut method. |
| + if (shortcut_bytes == total_transportation_bytes && |
| + blobs_waiting_for_paging_.empty() && |
| + shortcut_bytes < GetAvailableMemoryForBlobs()) { |
| + return MemoryStrategyResult::NONE_NEEDED; |
| + } |
| + // Step 4: Decide if we're going straight to disk. |
| + if (disk_enabled_ && |
| + (total_transportation_bytes > max_blob_in_memory_size_)) { |
|
michaeln
2016/08/23 01:31:54
should this take shortcut_bytes into account? tota
|
| + return MemoryStrategyResult::FILE; |
| + } |
| + // Step 5: Decide if we're using shared memory. |
| + if (total_transportation_bytes > max_ipc_memory_size_) { |
| + return MemoryStrategyResult::SHARED_MEMORY; |
| + } |
| + // Step 6: We can fit in IPC. |
| + return MemoryStrategyResult::IPC; |
| +} |
| + |
| +bool BlobMemoryController::CanFitInSystem(uint64_t size) const { |
| + // We check each size independently as a blob can't be constructed in both |
| + // disk and memory. |
| + return size <= GetAvailableMemoryForBlobs() || |
| + size <= GetAvailableDiskSpaceForBlobs(); |
| +} |
| + |
| +base::Optional<BlobMemoryController::PendingQuotaEntry> |
| +BlobMemoryController::ReserveQuotaForItems( |
| + std::vector<ShareableBlobDataItem*> items, |
| + const QuotaRequestCallback& success_callback) { |
| + bool files = false; |
|
Marijn Kruisselbrink
2016/08/05 23:23:55
What is this |files| thing, and what is it used fo
dmurph
2016/08/19 00:18:33
Removed.
|
| + uint64_t total_files_size_needed = 0; |
| + uint64_t total_bytes_needed = 0; |
| + base::SmallMap<std::map<std::string, uint64_t>> file_sizes; |
| + std::vector<scoped_refptr<ShareableBlobDataItem>> pending_items; |
| + for (ShareableBlobDataItem* item : items) { |
| + if (item->state() != ShareableBlobDataItem::QUOTA_NEEDED) |
| + continue; |
| + const DataElement& element = item->item()->data_element(); |
| + switch (item->item()->type()) { |
| + case DataElement::TYPE_BYTES_DESCRIPTION: |
| + case DataElement::TYPE_BYTES: { |
| + DCHECK_EQ(0ull, total_files_size_needed) |
| + << "You cannot reserve quota for both files and data"; |
| + total_bytes_needed += element.length(); |
| + break; |
| + } |
| + case DataElement::TYPE_FILE: { |
| + DCHECK_EQ(0ull, total_bytes_needed) |
| + << "You cannot reserve quota for both files and data"; |
| + DCHECK(BlobDataBuilder::IsTemporaryFileItem(element)); |
| + files = true; |
| + std::string file_id = BlobDataBuilder::GetTemporaryFileID(element); |
| + auto it = file_sizes.find(file_id); |
| + if (it != file_sizes.end()) { |
| + it->second = |
| + std::max(it->second, element.offset() + element.length()); |
| + } else { |
| + file_sizes[file_id] = element.offset() + element.length(); |
| + } |
| + total_files_size_needed += element.length(); |
| + break; |
| + } |
| + case DataElement::TYPE_FILE_FILESYSTEM: |
| + case DataElement::TYPE_BLOB: |
| + case DataElement::TYPE_UNKNOWN: |
| + case DataElement::TYPE_DISK_CACHE_ENTRY: |
| + NOTREACHED() << "We only need quota for temporary file and data items"; |
| + break; |
| + } |
| + pending_items.push_back(make_scoped_refptr(item)); |
| + item->state_ = ShareableBlobDataItem::QUOTA_REQUESTED; |
| + } |
| + |
| + if (total_files_size_needed > 0) { |
| + DCHECK_LE(total_files_size_needed, GetAvailableDiskSpaceForBlobs()); |
| + disk_used_ += total_files_size_needed; |
| + std::vector<scoped_refptr<ShareableFileReference>> file_refs; |
| + std::vector<uint64_t> sizes; |
| + for (const auto& size_pair : file_sizes) { |
| + std::string file_name = base::Uint64ToString(current_file_num_++); |
| + file_refs.push_back(ShareableFileReference::GetOrCreate( |
| + blob_storage_dir_.Append(file_name), |
| + ShareableFileReference::DELETE_ON_FINAL_RELEASE, file_runner_.get())); |
| + sizes.push_back(size_pair.second); |
| + } |
| + |
| + uint64_t disk_quota_entry = ++curr_disk_save_entry_; |
| + if (disk_quota_entry == kInvalidDiskQuotaEntry) { |
| + disk_quota_entry = ++curr_disk_save_entry_; |
| + } |
| + pending_file_opens_.insert(disk_quota_entry); |
| + base::PostTaskAndReplyWithResult( |
| + file_runner_.get(), FROM_HERE, |
| + base::Bind(&CreateFiles, base::Passed(&file_refs)), |
| + base::Bind(&BlobMemoryController::OnCreateFiles, |
| + ptr_factory_.GetWeakPtr(), base::Passed(&sizes), |
| + base::Passed(&pending_items), disk_quota_entry, |
| + success_callback)); |
| + RecordTracingCounters(); |
| + return PendingQuotaEntry(disk_quota_entry); |
| + } |
| + |
| + if (total_bytes_needed == 0) { |
| + LOG(ERROR) << "Dont' need bytes"; |
| + success_callback.Run(true, |
| + std::vector<BlobMemoryController::FileCreationInfo>()); |
| + return base::nullopt; |
| + } |
| + |
| + if (!disk_enabled_) { |
| + LOG(ERROR) << "Yes, " << total_bytes_needed << " can fit in memory now."; |
| + blob_memory_used_ += total_bytes_needed; |
| + for (ShareableBlobDataItem* item : items) { |
| + item->state_ = ShareableBlobDataItem::QUOTA_GRANTED; |
| + } |
| + success_callback.Run(true, |
| + std::vector<BlobMemoryController::FileCreationInfo>()); |
| + return base::nullopt; |
| + } |
| + |
| + // If we're currently waiting for blobs to page already, then we add |
| + // ourselves to the end of the queue. Once paging is complete, we'll schedule |
| + // more paging for any more pending blobs. |
| + if (!blobs_waiting_for_paging_.empty()) { |
| + LOG(ERROR) << "putting memory request " << total_bytes_needed |
| + << " in queue."; |
| + blobs_waiting_for_paging_.push_back(std::make_pair( |
| + total_bytes_needed, |
| + base::Bind(&BlobMemoryController::SetStateQuotaGrantedAndCallback, |
| + ptr_factory_.GetWeakPtr(), base::Passed(&pending_items), |
| + success_callback))); |
| + blobs_waiting_for_paging_size_ += total_bytes_needed; |
| + base::Optional<BlobMemoryController::PendingQuotaEntry> entry = |
| + PendingQuotaEntry(--blobs_waiting_for_paging_.end()); |
| + return entry; |
| + } |
| + |
| + // Store right away if we can. |
| + if (total_bytes_needed <= GetAvailableMemoryForBlobs()) { |
| + // If we're past our blob memory limit, then schedule our paging. |
| + LOG(ERROR) << "Yes, " << total_bytes_needed << " can fit in memory now."; |
| + blob_memory_used_ += total_bytes_needed; |
| + for (ShareableBlobDataItem* item : items) { |
| + item->state_ = ShareableBlobDataItem::QUOTA_GRANTED; |
| + } |
| + MaybeSchedulePagingUntilSystemHealthy(); |
| + success_callback.Run(true, |
| + std::vector<BlobMemoryController::FileCreationInfo>()); |
| + return base::nullopt; |
| + } |
| + |
| + // This means we're too big for memory. |
| + LOG(ERROR) << "waiting until " << total_bytes_needed << " can fit."; |
| + DCHECK(blobs_waiting_for_paging_.empty()); |
| + DCHECK_EQ(0u, blobs_waiting_for_paging_size_); |
| + blobs_waiting_for_paging_.push_back(std::make_pair( |
| + total_bytes_needed, |
| + base::Bind(&BlobMemoryController::SetStateQuotaGrantedAndCallback, |
| + ptr_factory_.GetWeakPtr(), base::Passed(&pending_items), |
| + success_callback))); |
| + blobs_waiting_for_paging_size_ = total_bytes_needed; |
| + base::Optional<BlobMemoryController::PendingQuotaEntry> entry = |
| + PendingQuotaEntry(--blobs_waiting_for_paging_.end()); |
| + LOG(ERROR) << "Scheduling paging on first item too big"; |
| + MaybeSchedulePagingUntilSystemHealthy(); |
| + return entry; |
| +} |
| + |
| +void BlobMemoryController::CancelQuotaReservation( |
| + const BlobMemoryController::PendingQuotaEntry& entry) { |
| + if (entry.disk_quota_entry == kInvalidDiskQuotaEntry) { |
| + if (entry.memory_quota_entry == blobs_waiting_for_paging_.end()) |
|
Marijn Kruisselbrink
2016/08/05 23:23:54
Under what circumstances would this comparison be
dmurph
2016/08/19 00:18:33
Simplified this by separating out memory and file
|
| + return; |
| + const std::pair<size_t, QuotaRequestCallback> pair = |
|
Marijn Kruisselbrink
2016/08/05 23:23:54
Not sure if this two-line variable declaration gai
dmurph
2016/08/19 00:18:33
Done.
|
| + *entry.memory_quota_entry; |
| + blobs_waiting_for_paging_size_ -= pair.first; |
| + blobs_waiting_for_paging_.erase(entry.memory_quota_entry); |
| + return; |
| + } |
| + pending_file_opens_.erase(entry.disk_quota_entry); |
| +} |
| + |
| +void BlobMemoryController::MaybeFreeQuotaForItems( |
| + const std::vector<scoped_refptr<ShareableBlobDataItem>>& items) { |
| + uint64_t total_memory_freeing = 0; |
| + base::hash_set<uint64_t> visited_items; |
|
Marijn Kruisselbrink
2016/08/05 23:23:54
base::hash_set is deprecated (and is just an alias
dmurph
2016/08/19 00:18:33
Done.
|
| + for (const scoped_refptr<ShareableBlobDataItem>& item : items) { |
| + switch (item->state()) { |
|
Marijn Kruisselbrink
2016/08/05 23:23:54
It might help make it more immediately obvious wha
dmurph
2016/08/19 00:18:33
Simplified by requiring that the items are all QUO
|
| + case ShareableBlobDataItem::QUOTA_NEEDED: |
| + case ShareableBlobDataItem::QUOTA_REQUESTED: |
| + case ShareableBlobDataItem::POPULATED_WITHOUT_QUOTA: |
| + continue; |
| + case ShareableBlobDataItem::QUOTA_GRANTED: |
| + case ShareableBlobDataItem::POPULATED_WITH_QUOTA: |
| + break; |
| + } |
| + // We only care about bytes items that don't have blobs referencing them, |
| + // and we remove duplicates. |
| + DataElement::Type type = item->item()->type(); |
| + if (!item->referencing_blobs().empty() || |
| + (type != DataElement::TYPE_BYTES && |
| + type != DataElement::TYPE_BYTES_DESCRIPTION) || |
| + visited_items.find(item->item_id()) != visited_items.end()) |
|
Marijn Kruisselbrink
2016/08/05 23:23:54
nit: there is a ContainsKey method in base/stl_uti
dmurph
2016/08/19 00:18:33
Done.
|
| + continue; |
| + visited_items.insert(item->item_id()); |
| + total_memory_freeing += item->item()->length(); |
| + } |
| + if (total_memory_freeing != 0) { |
| + DCHECK_GE(blob_memory_used_, total_memory_freeing); |
| + LOG(ERROR) << "Freeing memory " << total_memory_freeing; |
| + blob_memory_used_ -= total_memory_freeing; |
| + MaybeGrantPendingQuotaRequests(); |
| + } |
| +} |
| + |
| +void BlobMemoryController::FreeQuotaForPagedItemReference( |
| + const scoped_refptr<ShareableBlobDataItem>& item) { |
| + CHECK_EQ(item->state(), ShareableBlobDataItem::QUOTA_GRANTED); |
| + CHECK_EQ(item->item()->type(), DataElement::TYPE_BYTES_DESCRIPTION); |
| + LOG(ERROR) << "Freeing memory " << item->item()->length(); |
| + blob_memory_used_ -= item->item()->length(); |
| + MaybeGrantPendingQuotaRequests(); |
| +} |
| + |
| +void BlobMemoryController::UpdateBlobItemInRecents( |
| + ShareableBlobDataItem* item) { |
| + DCHECK_EQ(DataElement::TYPE_BYTES, item->item()->type()); |
| + DCHECK_EQ(ShareableBlobDataItem::POPULATED_WITH_QUOTA, item->state()); |
| + // We don't want to re-add the item if we're currently paging it to disk. |
| + if (items_saving_to_disk_.find(item->item_id()) != |
| + items_saving_to_disk_.end()) |
| + return; |
| + auto iterator = recent_item_cache_.Get(item->item_id()); |
| + if (iterator == recent_item_cache_.end()) { |
| + recent_item_cache_bytes_ += static_cast<size_t>(item->item()->length()); |
| + recent_item_cache_.Put(item->item_id(), item); |
| + MaybeSchedulePagingUntilSystemHealthy(); |
| + } |
| +} |
| + |
| +void BlobMemoryController::RemoveBlobItemInRecents( |
| + const ShareableBlobDataItem& item) { |
| + auto iterator = recent_item_cache_.Get(item.item_id()); |
| + if (iterator != recent_item_cache_.end()) { |
| + size_t size = static_cast<size_t>(item.item()->length()); |
| + DCHECK_GE(recent_item_cache_bytes_, size); |
| + recent_item_cache_bytes_ -= size; |
| + recent_item_cache_.Erase(iterator); |
| + } |
| +} |
| + |
| +void BlobMemoryController::SetStateQuotaGrantedAndCallback( |
| + std::vector<scoped_refptr<ShareableBlobDataItem>> items, |
| + const BlobMemoryController::QuotaRequestCallback& final_callback, |
| + bool success, |
| + std::vector<FileCreationInfo> files) { |
| + if (!success) { |
| + final_callback.Run(false, std::move(files)); |
| + return; |
| + } |
| + for (const auto& item : items) { |
| + item->state_ = ShareableBlobDataItem::QUOTA_GRANTED; |
| + } |
| + final_callback.Run(true, std::move(files)); |
| + return; |
| +} |
| + |
| +void BlobMemoryController::OnCreateFiles( |
| + std::vector<uint64_t> file_sizes, |
| + std::vector<scoped_refptr<ShareableBlobDataItem>> pending_items, |
| + uint64_t disk_quota_entry, |
| + const QuotaRequestCallback& file_callback, |
| + std::vector<FileCreationInfo> result) { |
| + if (result.empty()) { |
| + LOG(ERROR) << "Error creating files"; |
| + for (uint64_t size : file_sizes) { |
| + disk_used_ -= size; |
| + } |
| + file_callback.Run(false, std::vector<FileCreationInfo>()); |
| + DisableDisk(); |
| + return; |
| + } |
| + if (pending_file_opens_.find(disk_quota_entry) == pending_file_opens_.end()) { |
|
Marijn Kruisselbrink
2016/08/05 23:23:54
nit: rather than first checking if the value exist
dmurph
2016/08/19 00:18:33
Done.
|
| + // The user cancelled this operation. |
| + LOG(ERROR) << "user cancelled file write."; |
| + file_runner_->PostTask(FROM_HERE, |
| + base::Bind(&DestructFiles, base::Passed(&result))); |
| + } |
| + pending_file_opens_.erase(disk_quota_entry); |
| + DCHECK_EQ(file_sizes.size(), result.size()); |
| + for (size_t i = 0; i < result.size(); i++) { |
| + result[i].file_reference->AddFinalReleaseCallback( |
| + base::Bind(&BlobMemoryController::OnBlobFileDelete, |
| + ptr_factory_.GetWeakPtr(), file_sizes[i])); |
| + } |
| + for (const auto& item : pending_items) { |
| + item->state_ = ShareableBlobDataItem::QUOTA_GRANTED; |
| + } |
| + LOG(ERROR) << "Created " << result.size() << " files!"; |
| + file_callback.Run(true, std::move(result)); |
| +} |
| + |
| +void BlobMemoryController::MaybeGrantPendingQuotaRequests() { |
| + size_t space_available = max_blob_in_memory_size_ - blob_memory_used_; |
| + while (!blobs_waiting_for_paging_.empty() && |
| + max_blob_in_memory_size_ - blob_memory_used_ >= |
| + blobs_waiting_for_paging_.front().first) { |
| + auto size_callback_pair = blobs_waiting_for_paging_.front(); |
| + blobs_waiting_for_paging_.pop_front(); |
| + space_available -= size_callback_pair.first; |
| + blobs_waiting_for_paging_size_ -= size_callback_pair.first; |
| + blob_memory_used_ += size_callback_pair.first; |
| + size_callback_pair.second.Run(true, std::vector<FileCreationInfo>()); |
| + } |
| + RecordTracingCounters(); |
| +} |
| + |
| +void BlobMemoryController::MaybeSchedulePagingUntilSystemHealthy() { |
| + // Don't do paging when others are happening, as we don't change our |
| + // blobs_waiting_for_paging_size_ value until after the paging files have |
| + // been written. |
| + if (pending_pagings_ != 0 || !disk_enabled_) |
| + return; |
| + |
| + // We try to page items to disk until our current system size + requested |
| + // memory is below our size limit. |
| + while (blobs_waiting_for_paging_size_ + blob_memory_used_ > |
| + max_blob_in_memory_size_) { |
| + // We only page when we have enough items to fill a while page file. |
| + if (recent_item_cache_bytes_ < min_page_file_size_) |
| + break; |
| + DCHECK_LE(min_page_file_size_, static_cast<uint64_t>(blob_memory_used_)); |
| + size_t total_items_size = 0; |
| + std::unique_ptr<std::vector<scoped_refptr<ShareableBlobDataItem>>> |
| + items_for_disk(new std::vector<scoped_refptr<ShareableBlobDataItem>>()); |
| + // Collect our items. |
| + while (total_items_size < min_page_file_size_ && |
| + !recent_item_cache_.empty()) { |
| + auto iterator = --recent_item_cache_.end(); |
| + ShareableBlobDataItem* item = iterator->second; |
| + DCHECK(item); |
| + DCHECK_EQ(item->item()->type(), DataElement::TYPE_BYTES); |
| + recent_item_cache_.Erase(iterator); |
| + recent_item_cache_bytes_ -= static_cast<size_t>(item->item()->length()); |
| + items_saving_to_disk_.insert(item->item_id()); |
| + size_t size = base::checked_cast<size_t>(item->item()->length()); |
| + total_items_size += size; |
| + items_for_disk->push_back(make_scoped_refptr(item)); |
| + } |
| + if (total_items_size == 0) |
| + break; |
| + |
| + // Update our bookkeeping. |
| + pending_pagings_++; |
| + disk_used_ += total_items_size; |
| + DCHECK_GE(blob_memory_used_, total_items_size); |
| + LOG(ERROR) << "saving " << total_items_size << " to disk."; |
| + blob_memory_used_ -= total_items_size; |
| + in_flight_memory_used_ += total_items_size; |
| + std::string file_name = base::Uint64ToString(current_file_num_++); |
| + // Create our file reference. |
| + scoped_refptr<ShareableFileReference> file_ref = |
| + ShareableFileReference::GetOrCreate( |
| + blob_storage_dir_.Append(file_name), |
| + ShareableFileReference::DELETE_ON_FINAL_RELEASE, |
| + file_runner_.get()); |
| + // Add the release callback so we decrement our disk usage on file deletion. |
| + file_ref->AddFinalReleaseCallback( |
| + base::Bind(&BlobMemoryController::OnBlobFileDelete, |
| + ptr_factory_.GetWeakPtr(), total_items_size)); |
| + // Post the file writing task. |
| + base::PostTaskAndReplyWithResult( |
| + file_runner_.get(), FROM_HERE, |
| + base::Bind(&WriteItemsToFile, items_for_disk.get(), total_items_size, |
| + base::Passed(&file_ref)), |
| + base::Bind(&BlobMemoryController::OnPagingComplete, |
| + ptr_factory_.GetWeakPtr(), base::Passed(&items_for_disk), |
| + total_items_size)); |
| + } |
| + RecordTracingCounters(); |
| +} |
| + |
| +void BlobMemoryController::OnPagingComplete( |
| + std::unique_ptr<std::vector<scoped_refptr<ShareableBlobDataItem>>> items, |
| + size_t total_items_size, |
| + FileCreationInfo result) { |
| + if (!disk_enabled_) |
| + return; |
| + if (result.error != File::FILE_OK) { |
| + UMA_HISTOGRAM_ENUMERATION("Storage.Blob.PagingError", -result.error, |
| + -File::FILE_ERROR_MAX); |
| + disk_used_ -= total_items_size; |
| + DisableDisk(); |
| + return; |
| + } |
| + DCHECK_LT(0u, pending_pagings_); |
| + pending_pagings_--; |
| + |
| + // Switch from the data backing item to a new file backing item. |
| + uint64_t offset = 0; |
| + for (const scoped_refptr<ShareableBlobDataItem>& shareable_item : |
| + *items.get()) { |
| + scoped_refptr<BlobDataItem> new_item(new BlobDataItem( |
| + base::WrapUnique(new DataElement()), result.file_reference)); |
| + new_item->data_element_ptr()->SetToFilePathRange( |
| + result.file_reference->path(), offset, shareable_item->item()->length(), |
| + result.last_modified); |
| + shareable_item->item_ = new_item; |
| + items_saving_to_disk_.erase(shareable_item->item_id()); |
| + offset += shareable_item->item()->length(); |
| + } |
| + in_flight_memory_used_ -= total_items_size; |
| + |
| + // We want callback on blobs up to the amount we've freed. |
| + MaybeGrantPendingQuotaRequests(); |
| + |
| + // If we still have more blobs waiting and we're not waiting on more paging |
| + // operations, schedule more. |
| + MaybeSchedulePagingUntilSystemHealthy(); |
| +} |
| + |
| +void BlobMemoryController::RecordTracingCounters() { |
| + TRACE_COUNTER2("Blob", "MemoryUsage", "RegularStorage", blob_memory_used_, |
| + "InFlightToDisk", in_flight_memory_used_); |
| + TRACE_COUNTER1("Blob", "DiskUsage", disk_used_); |
| + TRACE_COUNTER1("Blob", "TranfersPendingOnDisk", |
| + blobs_waiting_for_paging_.size()); |
| + TRACE_COUNTER1("Blob", "TranfersBytesPendingOnDisk", |
| + blobs_waiting_for_paging_size_); |
| +} |
| + |
| +size_t BlobMemoryController::GetAvailableMemoryForBlobs() const { |
| + // If disk is enabled, then we include |in_flight_memory_used_|. Otherwise we |
| + // combine the in memory and in flight quotas. |
| + if (disk_enabled_) { |
| + if (max_blob_in_memory_size_ + in_flight_space_ < memory_usage()) |
| + return 0; |
| + return max_blob_in_memory_size_ + in_flight_space_ - memory_usage(); |
| + } |
| + if (max_blob_in_memory_size_ + in_flight_space_ < memory_usage()) |
| + return 0; |
| + return max_blob_in_memory_size_ + in_flight_space_ - memory_usage(); |
| +} |
| + |
| +uint64_t BlobMemoryController::GetAvailableDiskSpaceForBlobs() const { |
| + return disk_enabled_ |
| + ? max_blob_disk_space_ - disk_used_ - |
| + blobs_waiting_for_paging_size_ |
| + : 0; |
| +} |
| + |
| +void BlobMemoryController::OnBlobFileDelete(uint64_t size, |
| + const base::FilePath& path) { |
| + DCHECK_LE(size, disk_used_); |
| + LOG(ERROR) << "File deleted " << size; |
| + disk_used_ -= size; |
| +} |
| + |
| +} // namespace storage |