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