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

Unified Diff: storage/browser/blob/blob_memory_controller.cc

Issue 2339933004: [BlobStorage] BlobMemoryController & tests (Closed)
Patch Set: rebase Created 4 years, 3 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 side-by-side diff with in-line comments
Download patch
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..68b17d33dce9aaedbcdab225bf1e9008cb431b0e
--- /dev/null
+++ b/storage/browser/blob/blob_memory_controller.cc
@@ -0,0 +1,613 @@
+// 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/stl_util.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 {
+using PendingMemoryQuotaRequest =
+ BlobMemoryController::PendingMemoryQuotaRequest;
+using PendingFileQuotaRequest = BlobMemoryController::PendingFileQuotaRequest;
+
+// 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) {
+ 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);
+ }
+ return result;
+}
+
+BlobMemoryController::FileCreationInfo WriteItemsToFile(
+ std::vector<scoped_refptr<ShareableBlobDataItem>>* items,
+ size_t total_size_bytes,
+ scoped_refptr<ShareableFileReference> file_reference) {
+ DCHECK_NE(0u, total_size_bytes);
+ 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) {}
+
+std::vector<scoped_refptr<ShareableBlobDataItem>> WrapInRefPtrs(
+ const std::vector<ShareableBlobDataItem*> items_ptrs) {
+ std::vector<scoped_refptr<ShareableBlobDataItem>> result;
+ for (ShareableBlobDataItem* item : items_ptrs) {
+ result.push_back(make_scoped_refptr(item));
+ }
+ return result;
+}
+
+} // namespace
+
+BlobMemoryController::FileCreationInfo::FileCreationInfo() = default;
+BlobMemoryController::FileCreationInfo::~FileCreationInfo() = default;
+FileCreationInfo::FileCreationInfo(FileCreationInfo&&) = default;
+FileCreationInfo& FileCreationInfo::operator=(FileCreationInfo&&) = default;
+
+PendingMemoryQuotaRequest BlobMemoryController::GetInvalidMemoryQuotaRequest() {
+ return blobs_waiting_for_paging_.end();
+}
+
+BlobMemoryController::BlobMemoryController()
+ : recent_item_cache_(
+ base::MRUCache<uint64_t, ShareableBlobDataItem*>::NO_AUTO_EVICT),
+ ptr_factory_(this) {}
+
+BlobMemoryController::~BlobMemoryController() {}
+
+void BlobMemoryController::EnableFilePaging(
+ const base::FilePath& storage_directory,
+ scoped_refptr<base::TaskRunner> file_runner) {
+ DCHECK(!storage_directory.empty());
+ file_runner_ = std::move(file_runner);
+ blob_storage_dir_ = storage_directory;
+ file_paging_enabled_ = true;
+}
+
+void BlobMemoryController::DisableFilePaging() {
+ file_paging_enabled_ = false;
+ blob_memory_used_ += in_flight_memory_used_;
+ in_flight_memory_used_ = 0;
+ pending_file_request_sizes_.clear();
+ items_saving_to_disk_.clear();
+ for (const auto& size_callback_pair : blobs_waiting_for_paging_)
+ size_callback_pair.second.Run(false);
+ 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::Strategy BlobMemoryController::DetermineStrategy(
+ size_t preemptive_transported_bytes,
+ uint64_t total_transportation_bytes) const {
+ if (total_transportation_bytes == 0) {
+ return Strategy::NONE_NEEDED;
+ }
+ if (!CanReserveQuota(total_transportation_bytes)) {
+ return Strategy::TOO_LARGE;
+ }
+ // Handle the case where we have all the bytes preemptively transported, and
+ // we can also fit them.
+ if (preemptive_transported_bytes == total_transportation_bytes &&
+ blobs_waiting_for_paging_.empty() &&
+ preemptive_transported_bytes < GetAvailableMemoryForBlobs()) {
+ return Strategy::NONE_NEEDED;
+ }
+ if (file_paging_enabled_ &&
+ (total_transportation_bytes > limits_.max_blob_in_memory_space)) {
+ return Strategy::FILE;
+ }
+ if (total_transportation_bytes > limits_.max_ipc_memory_size) {
+ return Strategy::SHARED_MEMORY;
+ }
+ return Strategy::IPC;
+}
+
+bool BlobMemoryController::CanReserveQuota(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 <= GetAvailableFileSpaceForBlobs();
+}
+
+PendingMemoryQuotaRequest BlobMemoryController::ReserveMemoryQuota(
+ std::vector<ShareableBlobDataItem*> unreserved_memory_items,
+ const MemoryQuotaRequestCallback& success_callback) {
+ uint64_t total_bytes_needed = 0;
+ for (ShareableBlobDataItem* item : unreserved_memory_items) {
+ DCHECK_EQ(ShareableBlobDataItem::QUOTA_NEEDED, item->state());
+ DCHECK(item->item()->type() == DataElement::TYPE_BYTES_DESCRIPTION ||
+ item->item()->type() == DataElement::TYPE_BYTES);
+ total_bytes_needed += item->item()->length();
+ item->state_ = ShareableBlobDataItem::QUOTA_REQUESTED;
+ }
+
+ if (total_bytes_needed == 0) {
+ success_callback.Run(true);
+ return blobs_waiting_for_paging_.end();
+ }
+
+ if (!file_paging_enabled_) {
+ blob_memory_used_ += total_bytes_needed;
+ for (ShareableBlobDataItem* item : unreserved_memory_items) {
+ item->state_ = ShareableBlobDataItem::QUOTA_GRANTED;
+ }
+ success_callback.Run(true);
+ return blobs_waiting_for_paging_.end();
+ }
+
+ // 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()) {
+ std::vector<scoped_refptr<ShareableBlobDataItem>> item_handles =
+ WrapInRefPtrs(unreserved_memory_items);
+ blobs_waiting_for_paging_.push_back(std::make_pair(
+ total_bytes_needed,
+ base::Bind(&BlobMemoryController::SetStateQuotaGrantedAndCallback,
+ ptr_factory_.GetWeakPtr(), base::Passed(&item_handles),
+ success_callback)));
+ blobs_waiting_for_paging_size_ += total_bytes_needed;
+ return --blobs_waiting_for_paging_.end();
+ }
+
+ // Store right away if we can.
+ if (total_bytes_needed <= GetAvailableMemoryForBlobs()) {
+ // If we're past our blob memory limit, then schedule our paging.
+ blob_memory_used_ += total_bytes_needed;
+ for (ShareableBlobDataItem* item : unreserved_memory_items) {
+ item->state_ = ShareableBlobDataItem::QUOTA_GRANTED;
+ }
+ MaybeSchedulePagingUntilSystemHealthy();
+ success_callback.Run(true);
+ return blobs_waiting_for_paging_.end();
+ }
+
+ // This means we're too big for memory.
+ DCHECK(blobs_waiting_for_paging_.empty());
+ DCHECK_EQ(0u, blobs_waiting_for_paging_size_);
+ std::vector<scoped_refptr<ShareableBlobDataItem>> item_handles =
+ WrapInRefPtrs(unreserved_memory_items);
+ blobs_waiting_for_paging_.push_back(std::make_pair(
+ total_bytes_needed,
+ base::Bind(&BlobMemoryController::SetStateQuotaGrantedAndCallback,
+ ptr_factory_.GetWeakPtr(), base::Passed(&item_handles),
+ success_callback)));
+ blobs_waiting_for_paging_size_ = total_bytes_needed;
+ auto it = --blobs_waiting_for_paging_.end();
+ MaybeSchedulePagingUntilSystemHealthy();
+ return it;
+}
+
+void BlobMemoryController::CancelMemoryQuotaReservation(
+ const PendingMemoryQuotaRequest& request) {
+ if (request == blobs_waiting_for_paging_.end())
+ return;
+ blobs_waiting_for_paging_size_ -= request->first;
+ blobs_waiting_for_paging_.erase(request);
+ return;
+}
+
+PendingFileQuotaRequest BlobMemoryController::ReserveFileQuota(
+ std::vector<ShareableBlobDataItem*> unreserved_file_items,
+ const FileQuotaRequestCallback& success_callback) {
+ uint64_t total_files_size_needed = 0;
+ base::SmallMap<std::map<uint64_t, uint64_t>> file_sizes;
+ std::vector<scoped_refptr<ShareableBlobDataItem>> item_handles;
+ for (ShareableBlobDataItem* item : unreserved_file_items) {
+ DCHECK_EQ(ShareableBlobDataItem::QUOTA_NEEDED, item->state());
+ DCHECK_EQ(DataElement::TYPE_FILE, item->item()->type());
+
+ const DataElement& element = item->item()->data_element();
+ DCHECK(BlobDataBuilder::IsFutureFileItem(element));
+
+ uint64_t file_id = BlobDataBuilder::GetFutureFileID(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();
+ item->state_ = ShareableBlobDataItem::QUOTA_REQUESTED;
+ item_handles.push_back(make_scoped_refptr(item));
+ }
+
+ DCHECK_LE(total_files_size_needed, GetAvailableFileSpaceForBlobs());
+ 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 == kInvalidFileQuotaRequest) {
+ disk_quota_entry = ++curr_disk_save_entry_;
+ }
+ pending_file_request_sizes_[disk_quota_entry] = total_files_size_needed;
+ 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(&item_handles), disk_quota_entry,
+ success_callback));
+ RecordTracingCounters();
+ return disk_quota_entry;
+}
+
+void BlobMemoryController::CancelFileQuotaReservation(
+ const PendingFileQuotaRequest& entry) {
+ auto it = pending_file_request_sizes_.find(entry);
+ if (it == pending_file_request_sizes_.end())
+ return;
+ disk_used_ -= it->second;
+ pending_file_request_sizes_.erase(it);
+}
+
+void BlobMemoryController::MaybeFreeQuotaForItems(
+ const std::vector<scoped_refptr<ShareableBlobDataItem>>& items) {
+ uint64_t total_memory_freeing = 0;
+ std::unordered_set<uint64_t> visited_items;
+ for (const scoped_refptr<ShareableBlobDataItem>& item : items) {
+ switch (item->state()) {
+ 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) ||
+ base::ContainsKey(visited_items, item->item_id()))
+ continue;
+ visited_items.insert(item->item_id());
pwnall 2016/09/21 22:56:58 Do these items need to be released somehow? Grant
dmurph 2016/09/23 20:15:13 Not here, this class doesn't manage the lifetime o
+ total_memory_freeing += item->item()->length();
pwnall 2016/09/21 22:56:58 memory_to_be_freed?
dmurph 2016/09/23 20:15:14 Done.
+ }
+ if (total_memory_freeing != 0) {
+ DCHECK_GE(blob_memory_used_, 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);
+ 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 MemoryQuotaRequestCallback& final_callback,
+ bool success) {
+ if (!success) {
+ final_callback.Run(false);
+ return;
+ }
+ for (const auto& item : items) {
+ item->state_ = ShareableBlobDataItem::QUOTA_GRANTED;
+ }
+ final_callback.Run(true);
+ return;
+}
+
+void BlobMemoryController::OnCreateFiles(
+ std::vector<uint64_t> file_sizes,
+ std::vector<scoped_refptr<ShareableBlobDataItem>> pending_items,
+ uint64_t disk_quota_entry,
+ const FileQuotaRequestCallback& file_callback,
+ std::vector<FileCreationInfo> result) {
+ if (result.empty()) {
+ for (uint64_t size : file_sizes) {
+ disk_used_ -= size;
+ }
+ file_callback.Run(false, std::vector<FileCreationInfo>());
+ DisableFilePaging();
+ return;
+ }
+ // Check if the user cancelled the file request.
+ if (pending_file_request_sizes_.erase(disk_quota_entry) == 0) {
+ file_runner_->PostTask(FROM_HERE,
+ base::Bind(&DestructFiles, base::Passed(&result)));
+ return;
+ }
+ 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;
+ }
+ file_callback.Run(true, std::move(result));
+}
+
+void BlobMemoryController::MaybeGrantPendingQuotaRequests() {
+ size_t space_available = limits_.max_blob_in_memory_space - blob_memory_used_;
+ while (!blobs_waiting_for_paging_.empty() &&
+ limits_.max_blob_in_memory_space - 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);
+ }
+ 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 || !file_paging_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_ >
+ limits_.max_blob_in_memory_space) {
+ // We only page when we have enough items to fill a while page file.
+ if (recent_item_cache_bytes_ < limits_.min_page_file_size)
+ break;
+ DCHECK_LE(limits_.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 < limits_.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);
+ 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 (!file_paging_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;
+ DisableFilePaging();
+ 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 (limits_.total_memory_space() < memory_usage())
+ return 0;
+ return limits_.total_memory_space() - memory_usage();
+}
+
+uint64_t BlobMemoryController::GetAvailableFileSpaceForBlobs() const {
+ if (!file_paging_enabled_)
+ return 0;
+ return limits_.max_blob_disk_space - disk_used_ -
+ blobs_waiting_for_paging_size_;
+}
+
+void BlobMemoryController::OnBlobFileDelete(uint64_t size,
+ const base::FilePath& path) {
+ DCHECK_LE(size, disk_used_);
+ disk_used_ -= size;
+}
+
+} // namespace storage

Powered by Google App Engine
This is Rietveld 408576698