Chromium Code Reviews| 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 #include "storage/browser/blob/blob_memory_controller.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 | |
| 9 #include "base/callback.h" | |
| 10 #include "base/callback_helpers.h" | |
| 11 #include "base/containers/small_map.h" | |
| 12 #include "base/files/file_util.h" | |
| 13 #include "base/location.h" | |
| 14 #include "base/memory/ptr_util.h" | |
| 15 #include "base/metrics/histogram_macros.h" | |
| 16 #include "base/numerics/safe_conversions.h" | |
| 17 #include "base/numerics/safe_math.h" | |
| 18 #include "base/single_thread_task_runner.h" | |
| 19 #include "base/single_thread_task_runner.h" | |
| 20 #include "base/stl_util.h" | |
| 21 #include "base/strings/string_number_conversions.h" | |
| 22 #include "base/task_runner.h" | |
| 23 #include "base/task_runner_util.h" | |
| 24 #include "base/time/time.h" | |
| 25 #include "base/trace_event/trace_event.h" | |
| 26 #include "base/tuple.h" | |
| 27 #include "storage/browser/blob/blob_data_builder.h" | |
| 28 #include "storage/browser/blob/blob_data_item.h" | |
| 29 #include "storage/browser/blob/shareable_blob_data_item.h" | |
| 30 #include "storage/browser/blob/shareable_file_reference.h" | |
| 31 #include "storage/common/data_element.h" | |
| 32 | |
| 33 using base::File; | |
| 34 using base::FilePath; | |
| 35 using FileCreationInfo = storage::BlobMemoryController::FileCreationInfo; | |
| 36 | |
| 37 namespace storage { | |
| 38 namespace { | |
| 39 using PendingMemoryQuotaRequest = | |
| 40 BlobMemoryController::PendingMemoryQuotaRequest; | |
| 41 using PendingFileQuotaRequest = BlobMemoryController::PendingFileQuotaRequest; | |
| 42 | |
| 43 // Creates a file in the given directory w/ the given filename and size. | |
| 44 std::vector<BlobMemoryController::FileCreationInfo> CreateFiles( | |
| 45 std::vector<scoped_refptr<ShareableFileReference>> file_references) { | |
| 46 std::vector<BlobMemoryController::FileCreationInfo> result; | |
|
pwnall
2016/09/22 21:06:02
Do you need to DCHECK that you're in the File thre
dmurph
2016/09/23 20:15:14
It will, I ran into this earlier. ADding the threa
| |
| 47 | |
| 48 for (scoped_refptr<ShareableFileReference>& file_ref : file_references) { | |
| 49 result.push_back(BlobMemoryController::FileCreationInfo()); | |
| 50 BlobMemoryController::FileCreationInfo& creation_info = result.back(); | |
| 51 // Try to open our file. | |
| 52 File file(file_ref->path(), File::FLAG_CREATE_ALWAYS | File::FLAG_WRITE); | |
| 53 creation_info.file_reference = std::move(file_ref); | |
| 54 creation_info.error = file.error_details(); | |
| 55 UMA_HISTOGRAM_ENUMERATION("Storage.Blob.TransportFileCreate", | |
| 56 -creation_info.error, -File::FILE_ERROR_MAX); | |
| 57 if (creation_info.error != File::FILE_OK) | |
| 58 return std::vector<BlobMemoryController::FileCreationInfo>(); | |
| 59 | |
| 60 // Grab the file info to get the "last modified" time and store the file. | |
| 61 File::Info file_info; | |
| 62 bool success = file.GetInfo(&file_info); | |
| 63 UMA_HISTOGRAM_BOOLEAN("Storage.Blob.TransportFileInfoSuccess", success); | |
| 64 creation_info.error = success ? File::FILE_OK : File::FILE_ERROR_FAILED; | |
| 65 if (!success) | |
| 66 return std::vector<BlobMemoryController::FileCreationInfo>(); | |
| 67 creation_info.file = std::move(file); | |
| 68 } | |
| 69 return result; | |
| 70 } | |
| 71 | |
| 72 BlobMemoryController::FileCreationInfo WriteItemsToFile( | |
| 73 std::vector<scoped_refptr<ShareableBlobDataItem>>* items, | |
| 74 size_t total_size_bytes, | |
| 75 scoped_refptr<ShareableFileReference> file_reference) { | |
| 76 DCHECK_NE(0u, total_size_bytes); | |
| 77 UMA_HISTOGRAM_MEMORY_KB("Storage.Blob.PageFileSize", total_size_bytes / 1024); | |
| 78 | |
| 79 // Create our file. | |
| 80 BlobMemoryController::FileCreationInfo creation_info; | |
| 81 File file(file_reference->path(), | |
| 82 File::FLAG_CREATE_ALWAYS | File::FLAG_WRITE); | |
| 83 creation_info.file_reference = std::move(file_reference); | |
| 84 creation_info.error = file.error_details(); | |
| 85 UMA_HISTOGRAM_ENUMERATION("Storage.Blob.PageFileCreate", -creation_info.error, | |
| 86 -File::FILE_ERROR_MAX); | |
| 87 if (creation_info.error != File::FILE_OK) | |
| 88 return creation_info; | |
| 89 | |
| 90 // Write data. | |
| 91 file.SetLength(total_size_bytes); | |
| 92 int bytes_written = 0; | |
| 93 for (const auto& refptr : *items) { | |
| 94 const DataElement& element = refptr->item()->data_element(); | |
| 95 DCHECK_EQ(DataElement::TYPE_BYTES, element.type()); | |
| 96 size_t length = base::checked_cast<size_t>(element.length()); | |
| 97 size_t bytes_left = length; | |
| 98 while (bytes_left > 0) { | |
| 99 bytes_written = | |
| 100 file.WriteAtCurrentPos(element.bytes() + (length - bytes_left), | |
| 101 base::saturated_cast<int>(bytes_left)); | |
| 102 if (bytes_written < 0) | |
| 103 break; | |
| 104 DCHECK_LE(static_cast<size_t>(bytes_written), bytes_left); | |
| 105 bytes_left -= bytes_written; | |
| 106 } | |
| 107 if (bytes_written < 0) | |
| 108 break; | |
| 109 } | |
| 110 UMA_HISTOGRAM_BOOLEAN("Storage.Blob.PageFileWriteSuccess", bytes_written > 0); | |
| 111 | |
| 112 // Grab our modification time and create our SharedFileReference to manage the | |
| 113 // lifetime of the file. | |
| 114 File::Info info; | |
| 115 bool success = file.GetInfo(&info); | |
| 116 UMA_HISTOGRAM_BOOLEAN("Storage.Blob.PageFileInfoSuccess", success); | |
| 117 creation_info.error = | |
| 118 bytes_written < 0 || !success ? File::FILE_ERROR_FAILED : File::FILE_OK; | |
| 119 creation_info.last_modified = info.last_modified; | |
| 120 return creation_info; | |
| 121 } | |
| 122 | |
| 123 // Here so we can destruct files on the correct thread if the user cancels. | |
| 124 void DestructFiles(std::vector<BlobMemoryController::FileCreationInfo> files) {} | |
| 125 | |
| 126 std::vector<scoped_refptr<ShareableBlobDataItem>> WrapInRefPtrs( | |
| 127 const std::vector<ShareableBlobDataItem*> items_ptrs) { | |
| 128 std::vector<scoped_refptr<ShareableBlobDataItem>> result; | |
| 129 for (ShareableBlobDataItem* item : items_ptrs) { | |
| 130 result.push_back(make_scoped_refptr(item)); | |
| 131 } | |
| 132 return result; | |
| 133 } | |
| 134 | |
| 135 } // namespace | |
| 136 | |
| 137 BlobMemoryController::FileCreationInfo::FileCreationInfo() = default; | |
| 138 BlobMemoryController::FileCreationInfo::~FileCreationInfo() = default; | |
| 139 FileCreationInfo::FileCreationInfo(FileCreationInfo&&) = default; | |
| 140 FileCreationInfo& FileCreationInfo::operator=(FileCreationInfo&&) = default; | |
| 141 | |
| 142 PendingMemoryQuotaRequest BlobMemoryController::GetInvalidMemoryQuotaRequest() { | |
| 143 return blobs_waiting_for_paging_.end(); | |
| 144 } | |
| 145 | |
| 146 BlobMemoryController::BlobMemoryController() | |
| 147 : recent_item_cache_( | |
| 148 base::MRUCache<uint64_t, ShareableBlobDataItem*>::NO_AUTO_EVICT), | |
| 149 ptr_factory_(this) {} | |
| 150 | |
| 151 BlobMemoryController::~BlobMemoryController() {} | |
| 152 | |
| 153 void BlobMemoryController::EnableFilePaging( | |
| 154 const base::FilePath& storage_directory, | |
| 155 scoped_refptr<base::TaskRunner> file_runner) { | |
| 156 DCHECK(!storage_directory.empty()); | |
| 157 file_runner_ = std::move(file_runner); | |
| 158 blob_storage_dir_ = storage_directory; | |
| 159 file_paging_enabled_ = true; | |
| 160 } | |
| 161 | |
| 162 void BlobMemoryController::DisableFilePaging() { | |
|
pwnall
2016/09/22 21:06:02
Should this method also clear file_runner_ (to avo
dmurph
2016/09/23 20:15:14
We do need to cancel them. I believe I fixed this.
| |
| 163 file_paging_enabled_ = false; | |
| 164 blob_memory_used_ += in_flight_memory_used_; | |
| 165 in_flight_memory_used_ = 0; | |
| 166 pending_file_request_sizes_.clear(); | |
| 167 items_saving_to_disk_.clear(); | |
| 168 for (const auto& size_callback_pair : blobs_waiting_for_paging_) | |
| 169 size_callback_pair.second.Run(false); | |
| 170 pending_pagings_ = 0; | |
| 171 blobs_waiting_for_paging_.clear(); | |
| 172 blobs_waiting_for_paging_size_ = 0; | |
| 173 recent_item_cache_.Clear(); | |
| 174 recent_item_cache_bytes_ = 0; | |
| 175 RecordTracingCounters(); | |
| 176 } | |
| 177 | |
| 178 BlobMemoryController::Strategy BlobMemoryController::DetermineStrategy( | |
| 179 size_t preemptive_transported_bytes, | |
| 180 uint64_t total_transportation_bytes) const { | |
| 181 if (total_transportation_bytes == 0) { | |
| 182 return Strategy::NONE_NEEDED; | |
| 183 } | |
| 184 if (!CanReserveQuota(total_transportation_bytes)) { | |
| 185 return Strategy::TOO_LARGE; | |
| 186 } | |
| 187 // Handle the case where we have all the bytes preemptively transported, and | |
| 188 // we can also fit them. | |
| 189 if (preemptive_transported_bytes == total_transportation_bytes && | |
| 190 blobs_waiting_for_paging_.empty() && | |
| 191 preemptive_transported_bytes < GetAvailableMemoryForBlobs()) { | |
| 192 return Strategy::NONE_NEEDED; | |
| 193 } | |
| 194 if (file_paging_enabled_ && | |
| 195 (total_transportation_bytes > limits_.max_blob_in_memory_space)) { | |
| 196 return Strategy::FILE; | |
| 197 } | |
| 198 if (total_transportation_bytes > limits_.max_ipc_memory_size) { | |
| 199 return Strategy::SHARED_MEMORY; | |
| 200 } | |
| 201 return Strategy::IPC; | |
| 202 } | |
| 203 | |
| 204 bool BlobMemoryController::CanReserveQuota(uint64_t size) const { | |
| 205 // We check each size independently as a blob can't be constructed in both | |
| 206 // disk and memory. | |
| 207 return size <= GetAvailableMemoryForBlobs() || | |
| 208 size <= GetAvailableFileSpaceForBlobs(); | |
| 209 } | |
| 210 | |
| 211 PendingMemoryQuotaRequest BlobMemoryController::ReserveMemoryQuota( | |
| 212 std::vector<ShareableBlobDataItem*> unreserved_memory_items, | |
| 213 const MemoryQuotaRequestCallback& success_callback) { | |
| 214 uint64_t total_bytes_needed = 0; | |
| 215 for (ShareableBlobDataItem* item : unreserved_memory_items) { | |
| 216 DCHECK_EQ(ShareableBlobDataItem::QUOTA_NEEDED, item->state()); | |
| 217 DCHECK(item->item()->type() == DataElement::TYPE_BYTES_DESCRIPTION || | |
| 218 item->item()->type() == DataElement::TYPE_BYTES); | |
| 219 total_bytes_needed += item->item()->length(); | |
| 220 item->state_ = ShareableBlobDataItem::QUOTA_REQUESTED; | |
| 221 } | |
| 222 | |
| 223 if (total_bytes_needed == 0) { | |
| 224 success_callback.Run(true); | |
| 225 return blobs_waiting_for_paging_.end(); | |
| 226 } | |
| 227 | |
| 228 if (!file_paging_enabled_) { | |
| 229 blob_memory_used_ += total_bytes_needed; | |
| 230 for (ShareableBlobDataItem* item : unreserved_memory_items) { | |
| 231 item->state_ = ShareableBlobDataItem::QUOTA_GRANTED; | |
| 232 } | |
| 233 success_callback.Run(true); | |
| 234 return blobs_waiting_for_paging_.end(); | |
| 235 } | |
| 236 | |
| 237 // If we're currently waiting for blobs to page already, then we add | |
| 238 // ourselves to the end of the queue. Once paging is complete, we'll schedule | |
| 239 // more paging for any more pending blobs. | |
| 240 if (!blobs_waiting_for_paging_.empty()) { | |
| 241 std::vector<scoped_refptr<ShareableBlobDataItem>> item_handles = | |
| 242 WrapInRefPtrs(unreserved_memory_items); | |
| 243 blobs_waiting_for_paging_.push_back(std::make_pair( | |
| 244 total_bytes_needed, | |
| 245 base::Bind(&BlobMemoryController::SetStateQuotaGrantedAndCallback, | |
| 246 ptr_factory_.GetWeakPtr(), base::Passed(&item_handles), | |
| 247 success_callback))); | |
| 248 blobs_waiting_for_paging_size_ += total_bytes_needed; | |
| 249 return --blobs_waiting_for_paging_.end(); | |
| 250 } | |
| 251 | |
| 252 // Store right away if we can. | |
| 253 if (total_bytes_needed <= GetAvailableMemoryForBlobs()) { | |
| 254 // If we're past our blob memory limit, then schedule our paging. | |
| 255 blob_memory_used_ += total_bytes_needed; | |
| 256 for (ShareableBlobDataItem* item : unreserved_memory_items) { | |
| 257 item->state_ = ShareableBlobDataItem::QUOTA_GRANTED; | |
| 258 } | |
| 259 MaybeSchedulePagingUntilSystemHealthy(); | |
| 260 success_callback.Run(true); | |
| 261 return blobs_waiting_for_paging_.end(); | |
| 262 } | |
| 263 | |
| 264 // This means we're too big for memory. | |
| 265 DCHECK(blobs_waiting_for_paging_.empty()); | |
| 266 DCHECK_EQ(0u, blobs_waiting_for_paging_size_); | |
| 267 std::vector<scoped_refptr<ShareableBlobDataItem>> item_handles = | |
| 268 WrapInRefPtrs(unreserved_memory_items); | |
| 269 blobs_waiting_for_paging_.push_back(std::make_pair( | |
| 270 total_bytes_needed, | |
| 271 base::Bind(&BlobMemoryController::SetStateQuotaGrantedAndCallback, | |
| 272 ptr_factory_.GetWeakPtr(), base::Passed(&item_handles), | |
| 273 success_callback))); | |
| 274 blobs_waiting_for_paging_size_ = total_bytes_needed; | |
| 275 auto it = --blobs_waiting_for_paging_.end(); | |
| 276 MaybeSchedulePagingUntilSystemHealthy(); | |
| 277 return it; | |
| 278 } | |
| 279 | |
| 280 void BlobMemoryController::CancelMemoryQuotaReservation( | |
| 281 const PendingMemoryQuotaRequest& request) { | |
| 282 if (request == blobs_waiting_for_paging_.end()) | |
| 283 return; | |
| 284 blobs_waiting_for_paging_size_ -= request->first; | |
| 285 blobs_waiting_for_paging_.erase(request); | |
| 286 return; | |
| 287 } | |
| 288 | |
| 289 PendingFileQuotaRequest BlobMemoryController::ReserveFileQuota( | |
| 290 std::vector<ShareableBlobDataItem*> unreserved_file_items, | |
| 291 const FileQuotaRequestCallback& success_callback) { | |
| 292 uint64_t total_files_size_needed = 0; | |
| 293 base::SmallMap<std::map<uint64_t, uint64_t>> file_sizes; | |
| 294 std::vector<scoped_refptr<ShareableBlobDataItem>> item_handles; | |
| 295 for (ShareableBlobDataItem* item : unreserved_file_items) { | |
| 296 DCHECK_EQ(ShareableBlobDataItem::QUOTA_NEEDED, item->state()); | |
| 297 DCHECK_EQ(DataElement::TYPE_FILE, item->item()->type()); | |
| 298 | |
| 299 const DataElement& element = item->item()->data_element(); | |
| 300 DCHECK(BlobDataBuilder::IsFutureFileItem(element)); | |
| 301 | |
| 302 uint64_t file_id = BlobDataBuilder::GetFutureFileID(element); | |
| 303 auto it = file_sizes.find(file_id); | |
| 304 if (it != file_sizes.end()) { | |
| 305 it->second = std::max(it->second, element.offset() + element.length()); | |
| 306 } else { | |
| 307 file_sizes[file_id] = element.offset() + element.length(); | |
| 308 } | |
| 309 total_files_size_needed += element.length(); | |
| 310 item->state_ = ShareableBlobDataItem::QUOTA_REQUESTED; | |
| 311 item_handles.push_back(make_scoped_refptr(item)); | |
| 312 } | |
| 313 | |
| 314 DCHECK_LE(total_files_size_needed, GetAvailableFileSpaceForBlobs()); | |
| 315 disk_used_ += total_files_size_needed; | |
| 316 std::vector<scoped_refptr<ShareableFileReference>> file_refs; | |
| 317 std::vector<uint64_t> sizes; | |
| 318 for (const auto& size_pair : file_sizes) { | |
| 319 std::string file_name = base::Uint64ToString(current_file_num_++); | |
| 320 file_refs.push_back(ShareableFileReference::GetOrCreate( | |
| 321 blob_storage_dir_.Append(file_name), | |
| 322 ShareableFileReference::DELETE_ON_FINAL_RELEASE, file_runner_.get())); | |
| 323 sizes.push_back(size_pair.second); | |
| 324 } | |
| 325 | |
| 326 uint64_t disk_quota_entry = ++curr_disk_save_entry_; | |
| 327 if (disk_quota_entry == kInvalidFileQuotaRequest) { | |
| 328 disk_quota_entry = ++curr_disk_save_entry_; | |
| 329 } | |
| 330 pending_file_request_sizes_[disk_quota_entry] = total_files_size_needed; | |
| 331 base::PostTaskAndReplyWithResult( | |
| 332 file_runner_.get(), FROM_HERE, | |
| 333 base::Bind(&CreateFiles, base::Passed(&file_refs)), | |
| 334 base::Bind(&BlobMemoryController::OnCreateFiles, | |
| 335 ptr_factory_.GetWeakPtr(), base::Passed(&sizes), | |
| 336 base::Passed(&item_handles), disk_quota_entry, | |
| 337 success_callback)); | |
| 338 RecordTracingCounters(); | |
| 339 return disk_quota_entry; | |
| 340 } | |
| 341 | |
| 342 void BlobMemoryController::CancelFileQuotaReservation( | |
| 343 const PendingFileQuotaRequest& entry) { | |
| 344 auto it = pending_file_request_sizes_.find(entry); | |
| 345 if (it == pending_file_request_sizes_.end()) | |
| 346 return; | |
| 347 disk_used_ -= it->second; | |
| 348 pending_file_request_sizes_.erase(it); | |
| 349 } | |
| 350 | |
| 351 void BlobMemoryController::MaybeFreeQuotaForItems( | |
| 352 const std::vector<scoped_refptr<ShareableBlobDataItem>>& items) { | |
| 353 uint64_t total_memory_freeing = 0; | |
| 354 std::unordered_set<uint64_t> visited_items; | |
| 355 for (const scoped_refptr<ShareableBlobDataItem>& item : items) { | |
| 356 switch (item->state()) { | |
| 357 case ShareableBlobDataItem::QUOTA_NEEDED: | |
| 358 case ShareableBlobDataItem::QUOTA_REQUESTED: | |
| 359 case ShareableBlobDataItem::POPULATED_WITHOUT_QUOTA: | |
| 360 continue; | |
| 361 case ShareableBlobDataItem::QUOTA_GRANTED: | |
| 362 case ShareableBlobDataItem::POPULATED_WITH_QUOTA: | |
| 363 break; | |
| 364 } | |
| 365 // We only care about bytes items that don't have blobs referencing them, | |
| 366 // and we remove duplicates. | |
| 367 DataElement::Type type = item->item()->type(); | |
| 368 if (!item->referencing_blobs().empty() || | |
| 369 (type != DataElement::TYPE_BYTES && | |
| 370 type != DataElement::TYPE_BYTES_DESCRIPTION) || | |
| 371 base::ContainsKey(visited_items, item->item_id())) | |
| 372 continue; | |
| 373 visited_items.insert(item->item_id()); | |
| 374 total_memory_freeing += item->item()->length(); | |
| 375 } | |
| 376 if (total_memory_freeing != 0) { | |
| 377 DCHECK_GE(blob_memory_used_, total_memory_freeing); | |
| 378 blob_memory_used_ -= total_memory_freeing; | |
| 379 MaybeGrantPendingQuotaRequests(); | |
| 380 } | |
| 381 } | |
| 382 | |
| 383 void BlobMemoryController::FreeQuotaForPagedItemReference( | |
| 384 const scoped_refptr<ShareableBlobDataItem>& item) { | |
| 385 CHECK_EQ(item->state(), ShareableBlobDataItem::QUOTA_GRANTED); | |
| 386 CHECK_EQ(item->item()->type(), DataElement::TYPE_BYTES_DESCRIPTION); | |
| 387 blob_memory_used_ -= item->item()->length(); | |
| 388 MaybeGrantPendingQuotaRequests(); | |
| 389 } | |
| 390 | |
| 391 void BlobMemoryController::UpdateBlobItemInRecents( | |
| 392 ShareableBlobDataItem* item) { | |
| 393 DCHECK_EQ(DataElement::TYPE_BYTES, item->item()->type()); | |
| 394 DCHECK_EQ(ShareableBlobDataItem::POPULATED_WITH_QUOTA, item->state()); | |
| 395 // We don't want to re-add the item if we're currently paging it to disk. | |
| 396 if (items_saving_to_disk_.find(item->item_id()) != | |
| 397 items_saving_to_disk_.end()) | |
| 398 return; | |
| 399 auto iterator = recent_item_cache_.Get(item->item_id()); | |
| 400 if (iterator == recent_item_cache_.end()) { | |
| 401 recent_item_cache_bytes_ += static_cast<size_t>(item->item()->length()); | |
| 402 recent_item_cache_.Put(item->item_id(), item); | |
| 403 MaybeSchedulePagingUntilSystemHealthy(); | |
| 404 } | |
| 405 } | |
| 406 | |
| 407 void BlobMemoryController::RemoveBlobItemInRecents( | |
| 408 const ShareableBlobDataItem& item) { | |
| 409 auto iterator = recent_item_cache_.Get(item.item_id()); | |
| 410 if (iterator != recent_item_cache_.end()) { | |
| 411 size_t size = static_cast<size_t>(item.item()->length()); | |
| 412 DCHECK_GE(recent_item_cache_bytes_, size); | |
| 413 recent_item_cache_bytes_ -= size; | |
| 414 recent_item_cache_.Erase(iterator); | |
| 415 } | |
| 416 } | |
| 417 | |
| 418 void BlobMemoryController::SetStateQuotaGrantedAndCallback( | |
| 419 std::vector<scoped_refptr<ShareableBlobDataItem>> items, | |
| 420 const MemoryQuotaRequestCallback& final_callback, | |
| 421 bool success) { | |
| 422 if (!success) { | |
| 423 final_callback.Run(false); | |
| 424 return; | |
| 425 } | |
| 426 for (const auto& item : items) { | |
| 427 item->state_ = ShareableBlobDataItem::QUOTA_GRANTED; | |
| 428 } | |
| 429 final_callback.Run(true); | |
| 430 return; | |
| 431 } | |
| 432 | |
| 433 void BlobMemoryController::OnCreateFiles( | |
| 434 std::vector<uint64_t> file_sizes, | |
| 435 std::vector<scoped_refptr<ShareableBlobDataItem>> pending_items, | |
| 436 uint64_t disk_quota_entry, | |
| 437 const FileQuotaRequestCallback& file_callback, | |
| 438 std::vector<FileCreationInfo> result) { | |
| 439 if (result.empty()) { | |
| 440 for (uint64_t size : file_sizes) { | |
| 441 disk_used_ -= size; | |
| 442 } | |
| 443 file_callback.Run(false, std::vector<FileCreationInfo>()); | |
| 444 DisableFilePaging(); | |
| 445 return; | |
| 446 } | |
| 447 // Check if the user cancelled the file request. | |
| 448 if (pending_file_request_sizes_.erase(disk_quota_entry) == 0) { | |
| 449 file_runner_->PostTask(FROM_HERE, | |
| 450 base::Bind(&DestructFiles, base::Passed(&result))); | |
| 451 return; | |
| 452 } | |
| 453 DCHECK_EQ(file_sizes.size(), result.size()); | |
| 454 for (size_t i = 0; i < result.size(); i++) { | |
| 455 result[i].file_reference->AddFinalReleaseCallback( | |
| 456 base::Bind(&BlobMemoryController::OnBlobFileDelete, | |
| 457 ptr_factory_.GetWeakPtr(), file_sizes[i])); | |
| 458 } | |
| 459 for (const auto& item : pending_items) { | |
| 460 item->state_ = ShareableBlobDataItem::QUOTA_GRANTED; | |
| 461 } | |
| 462 file_callback.Run(true, std::move(result)); | |
| 463 } | |
| 464 | |
| 465 void BlobMemoryController::MaybeGrantPendingQuotaRequests() { | |
| 466 size_t space_available = limits_.max_blob_in_memory_space - blob_memory_used_; | |
| 467 while (!blobs_waiting_for_paging_.empty() && | |
| 468 limits_.max_blob_in_memory_space - blob_memory_used_ >= | |
| 469 blobs_waiting_for_paging_.front().first) { | |
| 470 auto size_callback_pair = blobs_waiting_for_paging_.front(); | |
| 471 blobs_waiting_for_paging_.pop_front(); | |
| 472 space_available -= size_callback_pair.first; | |
| 473 blobs_waiting_for_paging_size_ -= size_callback_pair.first; | |
| 474 blob_memory_used_ += size_callback_pair.first; | |
| 475 size_callback_pair.second.Run(true); | |
| 476 } | |
| 477 RecordTracingCounters(); | |
| 478 } | |
| 479 | |
| 480 void BlobMemoryController::MaybeSchedulePagingUntilSystemHealthy() { | |
| 481 // Don't do paging when others are happening, as we don't change our | |
| 482 // blobs_waiting_for_paging_size_ value until after the paging files have | |
| 483 // been written. | |
| 484 if (pending_pagings_ != 0 || !file_paging_enabled_) | |
| 485 return; | |
| 486 | |
| 487 // We try to page items to disk until our current system size + requested | |
| 488 // memory is below our size limit. | |
| 489 while (blobs_waiting_for_paging_size_ + blob_memory_used_ > | |
| 490 limits_.max_blob_in_memory_space) { | |
| 491 // We only page when we have enough items to fill a while page file. | |
| 492 if (recent_item_cache_bytes_ < limits_.min_page_file_size) | |
| 493 break; | |
| 494 DCHECK_LE(limits_.min_page_file_size, | |
| 495 static_cast<uint64_t>(blob_memory_used_)); | |
| 496 size_t total_items_size = 0; | |
| 497 std::unique_ptr<std::vector<scoped_refptr<ShareableBlobDataItem>>> | |
| 498 items_for_disk(new std::vector<scoped_refptr<ShareableBlobDataItem>>()); | |
| 499 // Collect our items. | |
| 500 while (total_items_size < limits_.min_page_file_size && | |
| 501 !recent_item_cache_.empty()) { | |
| 502 auto iterator = --recent_item_cache_.end(); | |
| 503 ShareableBlobDataItem* item = iterator->second; | |
| 504 DCHECK(item); | |
| 505 DCHECK_EQ(item->item()->type(), DataElement::TYPE_BYTES); | |
| 506 recent_item_cache_.Erase(iterator); | |
| 507 recent_item_cache_bytes_ -= static_cast<size_t>(item->item()->length()); | |
| 508 items_saving_to_disk_.insert(item->item_id()); | |
| 509 size_t size = base::checked_cast<size_t>(item->item()->length()); | |
| 510 total_items_size += size; | |
| 511 items_for_disk->push_back(make_scoped_refptr(item)); | |
| 512 } | |
| 513 if (total_items_size == 0) | |
| 514 break; | |
| 515 | |
| 516 // Update our bookkeeping. | |
| 517 pending_pagings_++; | |
| 518 disk_used_ += total_items_size; | |
| 519 DCHECK_GE(blob_memory_used_, total_items_size); | |
| 520 blob_memory_used_ -= total_items_size; | |
| 521 in_flight_memory_used_ += total_items_size; | |
| 522 std::string file_name = base::Uint64ToString(current_file_num_++); | |
| 523 // Create our file reference. | |
| 524 scoped_refptr<ShareableFileReference> file_ref = | |
| 525 ShareableFileReference::GetOrCreate( | |
| 526 blob_storage_dir_.Append(file_name), | |
| 527 ShareableFileReference::DELETE_ON_FINAL_RELEASE, | |
| 528 file_runner_.get()); | |
| 529 // Add the release callback so we decrement our disk usage on file deletion. | |
| 530 file_ref->AddFinalReleaseCallback( | |
| 531 base::Bind(&BlobMemoryController::OnBlobFileDelete, | |
| 532 ptr_factory_.GetWeakPtr(), total_items_size)); | |
| 533 // Post the file writing task. | |
| 534 base::PostTaskAndReplyWithResult( | |
| 535 file_runner_.get(), FROM_HERE, | |
| 536 base::Bind(&WriteItemsToFile, items_for_disk.get(), total_items_size, | |
| 537 base::Passed(&file_ref)), | |
| 538 base::Bind(&BlobMemoryController::OnPagingComplete, | |
| 539 ptr_factory_.GetWeakPtr(), base::Passed(&items_for_disk), | |
| 540 total_items_size)); | |
| 541 } | |
| 542 RecordTracingCounters(); | |
| 543 } | |
| 544 | |
| 545 void BlobMemoryController::OnPagingComplete( | |
| 546 std::unique_ptr<std::vector<scoped_refptr<ShareableBlobDataItem>>> items, | |
| 547 size_t total_items_size, | |
| 548 FileCreationInfo result) { | |
| 549 if (!file_paging_enabled_) | |
| 550 return; | |
| 551 if (result.error != File::FILE_OK) { | |
| 552 UMA_HISTOGRAM_ENUMERATION("Storage.Blob.PagingError", -result.error, | |
| 553 -File::FILE_ERROR_MAX); | |
| 554 disk_used_ -= total_items_size; | |
| 555 DisableFilePaging(); | |
| 556 return; | |
| 557 } | |
| 558 DCHECK_LT(0u, pending_pagings_); | |
| 559 pending_pagings_--; | |
| 560 | |
| 561 // Switch from the data backing item to a new file backing item. | |
| 562 uint64_t offset = 0; | |
| 563 for (const scoped_refptr<ShareableBlobDataItem>& shareable_item : | |
| 564 *items.get()) { | |
| 565 scoped_refptr<BlobDataItem> new_item(new BlobDataItem( | |
| 566 base::WrapUnique(new DataElement()), result.file_reference)); | |
| 567 new_item->data_element_ptr()->SetToFilePathRange( | |
| 568 result.file_reference->path(), offset, shareable_item->item()->length(), | |
| 569 result.last_modified); | |
| 570 shareable_item->item_ = new_item; | |
| 571 items_saving_to_disk_.erase(shareable_item->item_id()); | |
| 572 offset += shareable_item->item()->length(); | |
| 573 } | |
| 574 in_flight_memory_used_ -= total_items_size; | |
| 575 | |
| 576 // We want callback on blobs up to the amount we've freed. | |
| 577 MaybeGrantPendingQuotaRequests(); | |
| 578 | |
| 579 // If we still have more blobs waiting and we're not waiting on more paging | |
| 580 // operations, schedule more. | |
| 581 MaybeSchedulePagingUntilSystemHealthy(); | |
| 582 } | |
| 583 | |
| 584 void BlobMemoryController::RecordTracingCounters() { | |
| 585 TRACE_COUNTER2("Blob", "MemoryUsage", "RegularStorage", blob_memory_used_, | |
| 586 "InFlightToDisk", in_flight_memory_used_); | |
| 587 TRACE_COUNTER1("Blob", "DiskUsage", disk_used_); | |
| 588 TRACE_COUNTER1("Blob", "TranfersPendingOnDisk", | |
| 589 blobs_waiting_for_paging_.size()); | |
| 590 TRACE_COUNTER1("Blob", "TranfersBytesPendingOnDisk", | |
| 591 blobs_waiting_for_paging_size_); | |
| 592 } | |
| 593 | |
| 594 size_t BlobMemoryController::GetAvailableMemoryForBlobs() const { | |
| 595 if (limits_.total_memory_space() < memory_usage()) | |
| 596 return 0; | |
| 597 return limits_.total_memory_space() - memory_usage(); | |
| 598 } | |
| 599 | |
| 600 uint64_t BlobMemoryController::GetAvailableFileSpaceForBlobs() const { | |
| 601 if (!file_paging_enabled_) | |
| 602 return 0; | |
| 603 return limits_.max_blob_disk_space - disk_used_ - | |
| 604 blobs_waiting_for_paging_size_; | |
| 605 } | |
| 606 | |
| 607 void BlobMemoryController::OnBlobFileDelete(uint64_t size, | |
| 608 const base::FilePath& path) { | |
| 609 DCHECK_LE(size, disk_used_); | |
| 610 disk_used_ -= size; | |
| 611 } | |
| 612 | |
| 613 } // namespace storage | |
| OLD | NEW |