Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "storage/browser/blob/blob_memory_controller.h" | 5 #include "storage/browser/blob/blob_memory_controller.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <numeric> | 8 #include <numeric> |
| 9 | 9 |
| 10 #include "base/bind.h" | |
| 11 #include "base/bind_helpers.h" | |
| 10 #include "base/callback.h" | 12 #include "base/callback.h" |
| 11 #include "base/callback_helpers.h" | 13 #include "base/callback_helpers.h" |
| 12 #include "base/containers/small_map.h" | 14 #include "base/containers/small_map.h" |
| 13 #include "base/files/file_util.h" | 15 #include "base/files/file_util.h" |
| 14 #include "base/guid.h" | 16 #include "base/guid.h" |
| 15 #include "base/location.h" | 17 #include "base/location.h" |
| 16 #include "base/memory/ptr_util.h" | 18 #include "base/memory/ptr_util.h" |
| 17 #include "base/metrics/histogram_macros.h" | 19 #include "base/metrics/histogram_macros.h" |
| 18 #include "base/numerics/safe_conversions.h" | 20 #include "base/numerics/safe_conversions.h" |
| 19 #include "base/numerics/safe_math.h" | 21 #include "base/numerics/safe_math.h" |
| 20 #include "base/single_thread_task_runner.h" | 22 #include "base/single_thread_task_runner.h" |
| 21 #include "base/stl_util.h" | 23 #include "base/stl_util.h" |
| 22 #include "base/strings/string_number_conversions.h" | 24 #include "base/strings/string_number_conversions.h" |
| 25 #include "base/sys_info.h" | |
| 23 #include "base/task_runner.h" | 26 #include "base/task_runner.h" |
| 24 #include "base/task_runner_util.h" | 27 #include "base/task_runner_util.h" |
| 25 #include "base/threading/thread_restrictions.h" | 28 #include "base/threading/thread_restrictions.h" |
| 26 #include "base/time/time.h" | 29 #include "base/time/time.h" |
| 27 #include "base/trace_event/trace_event.h" | 30 #include "base/trace_event/trace_event.h" |
| 28 #include "base/tuple.h" | |
| 29 #include "storage/browser/blob/blob_data_builder.h" | 31 #include "storage/browser/blob/blob_data_builder.h" |
| 30 #include "storage/browser/blob/blob_data_item.h" | 32 #include "storage/browser/blob/blob_data_item.h" |
| 31 #include "storage/browser/blob/shareable_blob_data_item.h" | 33 #include "storage/browser/blob/shareable_blob_data_item.h" |
| 32 #include "storage/browser/blob/shareable_file_reference.h" | 34 #include "storage/browser/blob/shareable_file_reference.h" |
| 33 #include "storage/common/data_element.h" | 35 #include "storage/common/data_element.h" |
| 34 | 36 |
| 35 using base::File; | 37 using base::File; |
| 36 using base::FilePath; | 38 using base::FilePath; |
| 37 | 39 |
| 38 namespace storage { | 40 namespace storage { |
| 39 namespace { | 41 namespace { |
| 42 const int64_t kUnknownDiskAvailability = -1ll; | |
|
pwnall
2016/12/20 23:09:09
Can you make this a constexpr?
dmurph
2016/12/21 22:27:58
Done.
| |
| 43 | |
| 40 using FileCreationInfo = BlobMemoryController::FileCreationInfo; | 44 using FileCreationInfo = BlobMemoryController::FileCreationInfo; |
| 41 using MemoryAllocation = BlobMemoryController::MemoryAllocation; | 45 using MemoryAllocation = BlobMemoryController::MemoryAllocation; |
| 42 using QuotaAllocationTask = BlobMemoryController::QuotaAllocationTask; | 46 using QuotaAllocationTask = BlobMemoryController::QuotaAllocationTask; |
| 47 using DiskSpaceTestGetter = BlobMemoryController::DiskSpaceTestGetter; | |
| 48 | |
| 49 // CrOS: | |
| 50 // * Ram - 20% | |
| 51 // * Disk - 50% | |
| 52 // Note: The disk is the user partition, so the operating system can still | |
| 53 // function if this is full. | |
| 54 // Android: | |
| 55 // * RAM - 20% | |
| 56 // * Disk - 5% | |
| 57 // Desktop: | |
| 58 // * Ram - 20%, or 2 gigs if x64. | |
| 59 // * Disk - 10% | |
| 60 BlobStorageLimits CalculateBlobStorageLimitsImpl(const FilePath& storage_dir, | |
| 61 bool disk_enabled) { | |
| 62 BlobStorageLimits output; | |
|
pwnall
2016/12/20 23:09:09
What are you using this for?
dmurph
2016/12/21 22:27:58
Done.
| |
| 63 | |
| 64 int64_t disk_size = | |
| 65 disk_enabled ? base::SysInfo::AmountOfTotalDiskSpace(storage_dir) : 0ull; | |
| 66 int64_t memory_size = base::SysInfo::AmountOfPhysicalMemory(); | |
| 67 | |
| 68 BlobStorageLimits limits; | |
| 69 | |
| 70 if (memory_size > 0) { | |
| 71 #if !defined(OS_CHROMEOS) && !defined(OS_ANDROID) && defined(ARCH_CPU_64_BITS) | |
| 72 static const size_t kTwoGigabytes = 2ull * 1024 * 1024 * 1024; | |
|
pwnall
2016/12/20 23:09:09
Can you make this a constexpr and remove static? I
dmurph
2016/12/21 22:27:58
Done.
| |
| 73 limits.max_blob_in_memory_space = kTwoGigabytes; | |
| 74 #else | |
| 75 limits.max_blob_in_memory_space = static_cast<size_t>(memory_size / 5ll); | |
| 76 #endif | |
| 77 } | |
| 78 | |
| 79 if (disk_size > 0) { | |
| 80 #if defined(OS_CHROMEOS) | |
| 81 limits.desired_max_disk = static_cast<size_t>(disk_size / 2ll); | |
| 82 #elif defined(OS_ANDROID) | |
| 83 limits.desired_max_disk = static_cast<size_t>(disk_size / 20ll); | |
| 84 #else | |
| 85 limits.desired_max_disk = static_cast<size_t>(disk_size / 10ll); | |
| 86 #endif | |
| 87 } | |
| 88 limits.effective_max_disk = limits.desired_max_disk; | |
| 89 | |
| 90 return limits; | |
| 91 } | |
| 43 | 92 |
| 44 File::Error CreateBlobDirectory(const FilePath& blob_storage_dir) { | 93 File::Error CreateBlobDirectory(const FilePath& blob_storage_dir) { |
| 45 File::Error error = File::FILE_OK; | 94 File::Error error = File::FILE_OK; |
| 46 base::CreateDirectoryAndGetError(blob_storage_dir, &error); | 95 base::CreateDirectoryAndGetError(blob_storage_dir, &error); |
| 47 UMA_HISTOGRAM_ENUMERATION("Storage.Blob.CreateDirectoryResult", -error, | 96 UMA_HISTOGRAM_ENUMERATION("Storage.Blob.CreateDirectoryResult", -error, |
| 48 -File::FILE_ERROR_MAX); | 97 -File::FILE_ERROR_MAX); |
| 49 DLOG_IF(ERROR, error != File::FILE_OK) | 98 DLOG_IF(ERROR, error != File::FILE_OK) |
| 50 << "Error creating blob storage directory: " << error; | 99 << "Error creating blob storage directory: " << error; |
| 51 return error; | 100 return error; |
| 52 } | 101 } |
| 53 | 102 |
| 54 void DestructFile(File infos_without_references) {} | 103 void DestructFile(File infos_without_references) {} |
| 55 | 104 |
| 105 void DeleteFiles(std::vector<FileCreationInfo> files) { | |
| 106 for (FileCreationInfo& file_info : files) { | |
| 107 file_info.file.Close(); | |
| 108 base::DeleteFile(file_info.path, false); | |
| 109 } | |
| 110 } | |
| 111 | |
| 56 // Used for new unpopulated file items. Caller must populate file reference in | 112 // Used for new unpopulated file items. Caller must populate file reference in |
| 57 // returned FileCreationInfos. | 113 // returned FileCreationInfos. Also returns the current available disk space |
|
pwnall
2016/12/20 23:09:09
currently?
dmurph
2016/12/21 22:27:58
Done.
| |
| 58 std::pair<std::vector<FileCreationInfo>, File::Error> CreateEmptyFiles( | 114 // (without the future size of these files). |
| 59 const FilePath& blob_storage_dir, | 115 std::tuple<std::vector<FileCreationInfo>, File::Error, int64_t> |
| 60 scoped_refptr<base::TaskRunner> file_task_runner, | 116 CreateEmptyFiles(const FilePath& blob_storage_dir, |
| 61 std::vector<base::FilePath> file_paths) { | 117 DiskSpaceTestGetter* test_disk_size, |
| 118 scoped_refptr<base::TaskRunner> file_task_runner, | |
| 119 std::vector<base::FilePath> file_paths) { | |
| 62 base::ThreadRestrictions::AssertIOAllowed(); | 120 base::ThreadRestrictions::AssertIOAllowed(); |
| 63 | 121 |
| 64 File::Error dir_create_status = CreateBlobDirectory(blob_storage_dir); | 122 File::Error dir_create_status = CreateBlobDirectory(blob_storage_dir); |
| 65 if (dir_create_status != File::FILE_OK) | 123 if (dir_create_status != File::FILE_OK) { |
| 66 return std::make_pair(std::vector<FileCreationInfo>(), dir_create_status); | 124 return std::make_tuple(std::vector<FileCreationInfo>(), dir_create_status, |
| 125 kUnknownDiskAvailability); | |
| 126 } | |
| 127 | |
| 128 int64_t free_disk_space; | |
| 129 if (test_disk_size) { | |
| 130 free_disk_space = test_disk_size->AmountOfFreeDiskSpace(); | |
| 131 } else { | |
| 132 free_disk_space = base::SysInfo::AmountOfFreeDiskSpace(blob_storage_dir); | |
| 133 } | |
| 67 | 134 |
| 68 std::vector<FileCreationInfo> result; | 135 std::vector<FileCreationInfo> result; |
| 69 for (const base::FilePath& file_path : file_paths) { | 136 for (const base::FilePath& file_path : file_paths) { |
| 70 FileCreationInfo creation_info; | 137 FileCreationInfo creation_info; |
| 71 // Try to open our file. | 138 // Try to open our file. |
| 72 File file(file_path, File::FLAG_CREATE_ALWAYS | File::FLAG_WRITE); | 139 File file(file_path, File::FLAG_CREATE_ALWAYS | File::FLAG_WRITE); |
| 73 creation_info.path = std::move(file_path); | 140 creation_info.path = std::move(file_path); |
| 74 creation_info.file_deletion_runner = file_task_runner; | 141 creation_info.file_deletion_runner = file_task_runner; |
| 75 creation_info.error = file.error_details(); | 142 creation_info.error = file.error_details(); |
| 76 if (creation_info.error != File::FILE_OK) { | 143 if (creation_info.error != File::FILE_OK) { |
| 77 return std::make_pair(std::vector<FileCreationInfo>(), | 144 return std::make_tuple(std::vector<FileCreationInfo>(), |
| 78 creation_info.error); | 145 creation_info.error, free_disk_space); |
| 79 } | 146 } |
| 80 creation_info.file = std::move(file); | 147 creation_info.file = std::move(file); |
| 81 | 148 |
| 82 result.push_back(std::move(creation_info)); | 149 result.push_back(std::move(creation_info)); |
| 83 } | 150 } |
| 84 return std::make_pair(std::move(result), File::FILE_OK); | 151 return std::make_tuple(std::move(result), File::FILE_OK, free_disk_space); |
| 85 } | 152 } |
| 86 | 153 |
| 87 // Used to evict multiple memory items out to a single file. Caller must | 154 // Used to evict multiple memory items out to a single file. Caller must |
| 88 // populate file reference in returned FileCreationInfo. | 155 // populate file reference in returned FileCreationInfo. Also returns the disk |
|
pwnall
2016/12/20 23:09:09
free disk space?
dmurph
2016/12/21 22:27:58
Done.
| |
| 89 FileCreationInfo CreateFileAndWriteItems( | 156 // space AFTER creating this file. |
| 157 std::pair<FileCreationInfo, int64_t> CreateFileAndWriteItems( | |
| 90 const FilePath& blob_storage_dir, | 158 const FilePath& blob_storage_dir, |
| 159 DiskSpaceTestGetter* test_disk_size, | |
| 91 const FilePath& file_path, | 160 const FilePath& file_path, |
| 92 scoped_refptr<base::TaskRunner> file_task_runner, | 161 scoped_refptr<base::TaskRunner> file_task_runner, |
| 93 std::vector<DataElement*> items, | 162 std::vector<DataElement*> items, |
| 94 size_t total_size_bytes) { | 163 size_t total_size_bytes) { |
| 95 DCHECK_NE(0u, total_size_bytes); | 164 DCHECK_NE(0u, total_size_bytes); |
| 96 UMA_HISTOGRAM_MEMORY_KB("Storage.Blob.PageFileSize", total_size_bytes / 1024); | 165 UMA_HISTOGRAM_MEMORY_KB("Storage.Blob.PageFileSize", total_size_bytes / 1024); |
| 97 base::ThreadRestrictions::AssertIOAllowed(); | 166 base::ThreadRestrictions::AssertIOAllowed(); |
| 98 | 167 |
| 99 FileCreationInfo creation_info; | 168 FileCreationInfo creation_info; |
| 100 creation_info.file_deletion_runner = std::move(file_task_runner); | 169 creation_info.file_deletion_runner = std::move(file_task_runner); |
| 101 creation_info.error = CreateBlobDirectory(blob_storage_dir); | 170 creation_info.error = CreateBlobDirectory(blob_storage_dir); |
| 102 if (creation_info.error != File::FILE_OK) | 171 if (creation_info.error != File::FILE_OK) |
| 103 return creation_info; | 172 return std::make_pair(std::move(creation_info), kUnknownDiskAvailability); |
| 173 | |
| 174 int64_t free_disk_space; | |
| 175 if (test_disk_size) { | |
| 176 free_disk_space = test_disk_size->AmountOfFreeDiskSpace(); | |
| 177 } else { | |
| 178 free_disk_space = base::SysInfo::AmountOfFreeDiskSpace(blob_storage_dir); | |
| 179 } | |
| 180 | |
| 181 // Fail early instead of creating the files if we fill the disk. | |
| 182 if (free_disk_space != kUnknownDiskAvailability && | |
| 183 free_disk_space < static_cast<int64_t>(total_size_bytes)) { | |
| 184 creation_info.error = File::FILE_ERROR_NO_SPACE; | |
| 185 return std::make_pair(std::move(creation_info), free_disk_space); | |
| 186 } | |
| 187 int64_t disk_availability = | |
| 188 free_disk_space == kUnknownDiskAvailability | |
| 189 ? kUnknownDiskAvailability | |
| 190 : free_disk_space - static_cast<int64_t>(total_size_bytes); | |
| 104 | 191 |
| 105 // Create the page file. | 192 // Create the page file. |
| 106 File file(file_path, File::FLAG_CREATE_ALWAYS | File::FLAG_WRITE); | 193 File file(file_path, File::FLAG_CREATE_ALWAYS | File::FLAG_WRITE); |
| 107 creation_info.path = file_path; | 194 creation_info.path = file_path; |
| 108 creation_info.error = file.error_details(); | 195 creation_info.error = file.error_details(); |
| 109 if (creation_info.error != File::FILE_OK) | 196 if (creation_info.error != File::FILE_OK) |
| 110 return creation_info; | 197 return std::make_pair(std::move(creation_info), free_disk_space); |
| 111 | 198 |
| 112 // Write data. | 199 // Write data. |
| 113 file.SetLength(total_size_bytes); | 200 file.SetLength(total_size_bytes); |
| 114 int bytes_written = 0; | 201 int bytes_written = 0; |
| 115 for (DataElement* element : items) { | 202 for (DataElement* element : items) { |
| 116 DCHECK_EQ(DataElement::TYPE_BYTES, element->type()); | 203 DCHECK_EQ(DataElement::TYPE_BYTES, element->type()); |
| 117 size_t length = base::checked_cast<size_t>(element->length()); | 204 size_t length = base::checked_cast<size_t>(element->length()); |
| 118 size_t bytes_left = length; | 205 size_t bytes_left = length; |
| 119 while (bytes_left > 0) { | 206 while (bytes_left > 0) { |
| 120 bytes_written = | 207 bytes_written = |
| 121 file.WriteAtCurrentPos(element->bytes() + (length - bytes_left), | 208 file.WriteAtCurrentPos(element->bytes() + (length - bytes_left), |
| 122 base::saturated_cast<int>(bytes_left)); | 209 base::saturated_cast<int>(bytes_left)); |
| 123 if (bytes_written < 0) | 210 if (bytes_written < 0) |
| 124 break; | 211 break; |
| 125 DCHECK_LE(static_cast<size_t>(bytes_written), bytes_left); | 212 DCHECK_LE(static_cast<size_t>(bytes_written), bytes_left); |
| 126 bytes_left -= bytes_written; | 213 bytes_left -= bytes_written; |
| 127 } | 214 } |
| 128 if (bytes_written < 0) | 215 if (bytes_written < 0) |
| 129 break; | 216 break; |
| 130 } | 217 } |
| 131 if (!file.Flush()) { | 218 if (!file.Flush()) { |
| 219 file.Close(); | |
| 220 base::DeleteFile(file_path, false); | |
| 132 creation_info.error = File::FILE_ERROR_FAILED; | 221 creation_info.error = File::FILE_ERROR_FAILED; |
| 133 return creation_info; | 222 return std::make_pair(std::move(creation_info), free_disk_space); |
| 134 } | 223 } |
| 135 | 224 |
| 136 File::Info info; | 225 File::Info info; |
| 137 bool success = file.GetInfo(&info); | 226 bool success = file.GetInfo(&info); |
| 138 creation_info.error = | 227 creation_info.error = |
| 139 bytes_written < 0 || !success ? File::FILE_ERROR_FAILED : File::FILE_OK; | 228 bytes_written < 0 || !success ? File::FILE_ERROR_FAILED : File::FILE_OK; |
| 140 creation_info.last_modified = info.last_modified; | 229 creation_info.last_modified = info.last_modified; |
| 141 return creation_info; | 230 return std::make_pair(std::move(creation_info), disk_availability); |
| 142 } | 231 } |
| 143 | 232 |
| 144 uint64_t GetTotalSizeAndFileSizes( | 233 uint64_t GetTotalSizeAndFileSizes( |
| 145 const std::vector<scoped_refptr<ShareableBlobDataItem>>& | 234 const std::vector<scoped_refptr<ShareableBlobDataItem>>& |
| 146 unreserved_file_items, | 235 unreserved_file_items, |
| 147 std::vector<uint64_t>* file_sizes_output) { | 236 std::vector<uint64_t>* file_sizes_output) { |
| 148 uint64_t total_size_output = 0; | 237 uint64_t total_size_output = 0; |
| 149 base::SmallMap<std::map<uint64_t, uint64_t>> file_id_to_sizes; | 238 base::SmallMap<std::map<uint64_t, uint64_t>> file_id_to_sizes; |
| 150 for (const auto& item : unreserved_file_items) { | 239 for (const auto& item : unreserved_file_items) { |
| 151 const DataElement& element = item->item()->data_element(); | 240 const DataElement& element = item->item()->data_element(); |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 248 base::WeakPtrFactory<MemoryQuotaAllocationTask> weak_factory_; | 337 base::WeakPtrFactory<MemoryQuotaAllocationTask> weak_factory_; |
| 249 DISALLOW_COPY_AND_ASSIGN(MemoryQuotaAllocationTask); | 338 DISALLOW_COPY_AND_ASSIGN(MemoryQuotaAllocationTask); |
| 250 }; | 339 }; |
| 251 | 340 |
| 252 class BlobMemoryController::FileQuotaAllocationTask | 341 class BlobMemoryController::FileQuotaAllocationTask |
| 253 : public BlobMemoryController::QuotaAllocationTask { | 342 : public BlobMemoryController::QuotaAllocationTask { |
| 254 public: | 343 public: |
| 255 // We post a task to create the file for the items right away. | 344 // We post a task to create the file for the items right away. |
| 256 FileQuotaAllocationTask( | 345 FileQuotaAllocationTask( |
| 257 BlobMemoryController* memory_controller, | 346 BlobMemoryController* memory_controller, |
| 347 DiskSpaceTestGetter* test_disk_size, | |
| 258 std::vector<scoped_refptr<ShareableBlobDataItem>> unreserved_file_items, | 348 std::vector<scoped_refptr<ShareableBlobDataItem>> unreserved_file_items, |
| 259 const FileQuotaRequestCallback& done_callback) | 349 const FileQuotaRequestCallback& done_callback) |
| 260 : controller_(memory_controller), | 350 : controller_(memory_controller), |
| 261 done_callback_(done_callback), | 351 done_callback_(done_callback), |
| 262 weak_factory_(this) { | 352 weak_factory_(this) { |
| 263 // Get the file sizes and total size. | 353 // Get the file sizes and total size. |
| 264 std::vector<uint64_t> file_sizes; | |
| 265 uint64_t total_size = | 354 uint64_t total_size = |
| 266 GetTotalSizeAndFileSizes(unreserved_file_items, &file_sizes); | 355 GetTotalSizeAndFileSizes(unreserved_file_items, &file_sizes_); |
| 267 DCHECK_LE(total_size, controller_->GetAvailableFileSpaceForBlobs()); | 356 DCHECK_LE(total_size, controller_->GetAvailableFileSpaceForBlobs()); |
| 268 allocation_size_ = total_size; | 357 allocation_size_ = total_size; |
| 269 | 358 |
| 270 // Check & set our item states. | 359 // Check & set our item states. |
| 271 for (auto& shareable_item : unreserved_file_items) { | 360 for (auto& shareable_item : unreserved_file_items) { |
| 272 DCHECK_EQ(ShareableBlobDataItem::QUOTA_NEEDED, shareable_item->state()); | 361 DCHECK_EQ(ShareableBlobDataItem::QUOTA_NEEDED, shareable_item->state()); |
| 273 DCHECK_EQ(DataElement::TYPE_FILE, shareable_item->item()->type()); | 362 DCHECK_EQ(DataElement::TYPE_FILE, shareable_item->item()->type()); |
| 274 shareable_item->set_state(ShareableBlobDataItem::QUOTA_REQUESTED); | 363 shareable_item->set_state(ShareableBlobDataItem::QUOTA_REQUESTED); |
| 275 } | 364 } |
| 276 pending_items_ = std::move(unreserved_file_items); | 365 pending_items_ = std::move(unreserved_file_items); |
| 277 | 366 |
| 278 // Increment disk usage and create our file references. | 367 // Increment disk usage and create our file references. |
| 279 controller_->disk_used_ += allocation_size_; | 368 controller_->disk_used_ += allocation_size_; |
| 280 std::vector<base::FilePath> file_paths; | 369 std::vector<base::FilePath> file_paths; |
| 281 std::vector<scoped_refptr<ShareableFileReference>> references; | 370 std::vector<scoped_refptr<ShareableFileReference>> references; |
| 282 for (size_t i = 0; i < file_sizes.size(); i++) { | 371 for (size_t i = 0; i < file_sizes_.size(); i++) { |
| 283 file_paths.push_back(controller_->GenerateNextPageFileName()); | 372 file_paths.push_back(controller_->GenerateNextPageFileName()); |
| 284 references.push_back(ShareableFileReference::GetOrCreate( | 373 references.push_back(ShareableFileReference::GetOrCreate( |
| 285 file_paths.back(), ShareableFileReference::DELETE_ON_FINAL_RELEASE, | 374 file_paths.back(), ShareableFileReference::DELETE_ON_FINAL_RELEASE, |
| 286 controller_->file_runner_.get())); | 375 controller_->file_runner_.get())); |
| 287 references.back()->AddFinalReleaseCallback( | |
| 288 base::Bind(&BlobMemoryController::OnBlobFileDelete, | |
| 289 controller_->weak_factory_.GetWeakPtr(), file_sizes[i])); | |
| 290 } | 376 } |
| 291 | |
| 292 // Send file creation task to file thread. | 377 // Send file creation task to file thread. |
| 293 base::PostTaskAndReplyWithResult( | 378 base::PostTaskAndReplyWithResult( |
| 294 controller_->file_runner_.get(), FROM_HERE, | 379 controller_->file_runner_.get(), FROM_HERE, |
| 295 base::Bind(&CreateEmptyFiles, controller_->blob_storage_dir_, | 380 base::Bind(&CreateEmptyFiles, controller_->blob_storage_dir_, |
| 296 controller_->file_runner_, base::Passed(&file_paths)), | 381 test_disk_size, controller_->file_runner_, |
| 382 base::Passed(&file_paths)), | |
| 297 base::Bind(&FileQuotaAllocationTask::OnCreateEmptyFiles, | 383 base::Bind(&FileQuotaAllocationTask::OnCreateEmptyFiles, |
| 298 weak_factory_.GetWeakPtr(), base::Passed(&references))); | 384 weak_factory_.GetWeakPtr(), base::Passed(&references), |
| 385 allocation_size_)); | |
| 299 controller_->RecordTracingCounters(); | 386 controller_->RecordTracingCounters(); |
| 300 } | 387 } |
| 301 ~FileQuotaAllocationTask() override {} | 388 ~FileQuotaAllocationTask() override {} |
| 302 | 389 |
| 303 void RunDoneCallback(std::vector<FileCreationInfo> file_info, bool success) { | 390 void RunDoneCallback(std::vector<FileCreationInfo> file_info, bool success) { |
| 304 // Make sure we clear the weak pointers we gave to the caller beforehand. | 391 // Make sure we clear the weak pointers we gave to the caller beforehand. |
| 305 weak_factory_.InvalidateWeakPtrs(); | 392 weak_factory_.InvalidateWeakPtrs(); |
| 306 | 393 |
| 307 // We want to destroy this object on the exit of this method if we were | 394 // We want to destroy this object on the exit of this method if we were |
| 308 // successful. | 395 // successful. |
| 309 std::unique_ptr<FileQuotaAllocationTask> this_object; | 396 std::unique_ptr<FileQuotaAllocationTask> this_object; |
| 310 if (success) { | 397 if (success) { |
| 398 // Register the disk space accounting callback. | |
| 399 DCHECK_EQ(file_info.size(), file_sizes_.size()); | |
| 400 for (size_t i = 0; i < file_sizes_.size(); i++) { | |
| 401 file_info[i].file_reference->AddFinalReleaseCallback(base::Bind( | |
| 402 &BlobMemoryController::OnBlobFileDelete, | |
| 403 controller_->weak_factory_.GetWeakPtr(), file_sizes_[i])); | |
| 404 } | |
| 311 for (auto& item : pending_items_) { | 405 for (auto& item : pending_items_) { |
| 312 item->set_state(ShareableBlobDataItem::QUOTA_GRANTED); | 406 item->set_state(ShareableBlobDataItem::QUOTA_GRANTED); |
| 313 } | 407 } |
| 314 this_object = std::move(*my_list_position_); | 408 this_object = std::move(*my_list_position_); |
| 315 controller_->pending_file_quota_tasks_.erase(my_list_position_); | 409 controller_->pending_file_quota_tasks_.erase(my_list_position_); |
| 316 } | 410 } |
| 317 | 411 |
| 318 done_callback_.Run(std::move(file_info), success); | 412 done_callback_.Run(std::move(file_info), success); |
| 319 } | 413 } |
| 320 | 414 |
| 321 base::WeakPtr<QuotaAllocationTask> GetWeakPtr() { | 415 base::WeakPtr<QuotaAllocationTask> GetWeakPtr() { |
| 322 return weak_factory_.GetWeakPtr(); | 416 return weak_factory_.GetWeakPtr(); |
| 323 } | 417 } |
| 324 | 418 |
| 325 void Cancel() override { | 419 void Cancel() override { |
| 326 // This call destroys this object. We rely on ShareableFileReference's | 420 DCHECK_GE(controller_->disk_used_, allocation_size_); |
| 327 // final release callback for disk_usage_ accounting. | 421 controller_->disk_used_ -= allocation_size_; |
| 422 // This call destroys this object. | |
| 328 controller_->pending_file_quota_tasks_.erase(my_list_position_); | 423 controller_->pending_file_quota_tasks_.erase(my_list_position_); |
| 329 } | 424 } |
| 330 | 425 |
| 331 void OnCreateEmptyFiles( | 426 void OnCreateEmptyFiles( |
| 332 std::vector<scoped_refptr<ShareableFileReference>> references, | 427 std::vector<scoped_refptr<ShareableFileReference>> references, |
| 333 std::pair<std::vector<FileCreationInfo>, File::Error> files_and_error) { | 428 uint64_t new_files_total_size, |
| 334 auto& files = files_and_error.first; | 429 std::tuple<std::vector<FileCreationInfo>, File::Error, int64_t> result) { |
| 430 std::vector<FileCreationInfo> files = std::move(std::get<0>(result)); | |
| 431 File::Error error = std::get<1>(result); | |
| 432 int64_t avail_disk_space = std::get<2>(result); | |
| 335 if (files.empty()) { | 433 if (files.empty()) { |
| 434 DCHECK_NE(error, File::FILE_OK); | |
| 336 DCHECK_GE(controller_->disk_used_, allocation_size_); | 435 DCHECK_GE(controller_->disk_used_, allocation_size_); |
| 337 controller_->disk_used_ -= allocation_size_; | 436 controller_->disk_used_ -= allocation_size_; |
| 338 // This will call our callback and delete the object correctly. | 437 // This will call our callback and delete the object correctly. |
| 339 controller_->DisableFilePaging(files_and_error.second); | 438 controller_->DisableFilePaging(error); |
| 340 return; | 439 return; |
| 341 } | 440 } |
| 441 // The allocation won't fit at all. Cancel this request. The disk will be | |
| 442 // decremented when the file is deleted through AddFinalReleaseCallback. | |
| 443 if (avail_disk_space != kUnknownDiskAvailability && | |
| 444 base::checked_cast<uint64_t>(avail_disk_space) < new_files_total_size) { | |
| 445 DCHECK_GE(controller_->disk_used_, allocation_size_); | |
| 446 controller_->disk_used_ -= allocation_size_; | |
| 447 controller_->AdjustDiskUsage(static_cast<uint64_t>(avail_disk_space)); | |
| 448 controller_->file_runner_->PostTask( | |
| 449 FROM_HERE, base::Bind(&DeleteFiles, base::Passed(&files))); | |
| 450 std::unique_ptr<FileQuotaAllocationTask> this_object = | |
| 451 std::move(*my_list_position_); | |
| 452 controller_->pending_file_quota_tasks_.erase(my_list_position_); | |
| 453 RunDoneCallback(std::vector<FileCreationInfo>(), false); | |
| 454 return; | |
| 455 } | |
| 456 if (avail_disk_space != kUnknownDiskAvailability) { | |
| 457 controller_->AdjustDiskUsage(base::checked_cast<uint64_t>( | |
| 458 avail_disk_space - new_files_total_size)); | |
| 459 } | |
| 342 DCHECK_EQ(files.size(), references.size()); | 460 DCHECK_EQ(files.size(), references.size()); |
| 343 for (size_t i = 0; i < files.size(); i++) { | 461 for (size_t i = 0; i < files.size(); i++) { |
| 344 files[i].file_reference = std::move(references[i]); | 462 files[i].file_reference = std::move(references[i]); |
| 345 } | 463 } |
| 346 RunDoneCallback(std::move(files), true); | 464 RunDoneCallback(std::move(files), true); |
| 347 } | 465 } |
| 348 | 466 |
| 349 // The my_list_position_ iterator is stored so that we can remove ourself | 467 // The my_list_position_ iterator is stored so that we can remove ourself |
| 350 // from the task list when we are cancelled. | 468 // from the task list when we are cancelled. |
| 351 void set_my_list_position( | 469 void set_my_list_position( |
| 352 PendingFileQuotaTaskList::iterator my_list_position) { | 470 PendingFileQuotaTaskList::iterator my_list_position) { |
| 353 my_list_position_ = my_list_position; | 471 my_list_position_ = my_list_position; |
| 354 } | 472 } |
| 355 | 473 |
| 474 size_t allocation_size() const { return allocation_size_; } | |
| 475 | |
| 356 private: | 476 private: |
| 357 BlobMemoryController* controller_; | 477 BlobMemoryController* controller_; |
| 478 std::vector<uint64_t> file_sizes_; | |
| 358 std::vector<scoped_refptr<ShareableBlobDataItem>> pending_items_; | 479 std::vector<scoped_refptr<ShareableBlobDataItem>> pending_items_; |
| 359 scoped_refptr<base::TaskRunner> file_runner_; | |
| 360 FileQuotaRequestCallback done_callback_; | 480 FileQuotaRequestCallback done_callback_; |
| 361 | 481 |
| 362 uint64_t allocation_size_; | 482 uint64_t allocation_size_; |
| 363 PendingFileQuotaTaskList::iterator my_list_position_; | 483 PendingFileQuotaTaskList::iterator my_list_position_; |
| 364 | 484 |
| 365 base::WeakPtrFactory<FileQuotaAllocationTask> weak_factory_; | 485 base::WeakPtrFactory<FileQuotaAllocationTask> weak_factory_; |
| 366 DISALLOW_COPY_AND_ASSIGN(FileQuotaAllocationTask); | 486 DISALLOW_COPY_AND_ASSIGN(FileQuotaAllocationTask); |
| 367 }; | 487 }; |
| 368 | 488 |
| 369 BlobMemoryController::BlobMemoryController( | 489 BlobMemoryController::BlobMemoryController( |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 394 PendingMemoryQuotaTaskList old_memory_tasks; | 514 PendingMemoryQuotaTaskList old_memory_tasks; |
| 395 PendingFileQuotaTaskList old_file_tasks; | 515 PendingFileQuotaTaskList old_file_tasks; |
| 396 std::swap(old_memory_tasks, pending_memory_quota_tasks_); | 516 std::swap(old_memory_tasks, pending_memory_quota_tasks_); |
| 397 std::swap(old_file_tasks, pending_file_quota_tasks_); | 517 std::swap(old_file_tasks, pending_file_quota_tasks_); |
| 398 | 518 |
| 399 // Don't call the callbacks until we have a consistent state. | 519 // Don't call the callbacks until we have a consistent state. |
| 400 for (auto& memory_request : old_memory_tasks) { | 520 for (auto& memory_request : old_memory_tasks) { |
| 401 memory_request->RunDoneCallback(false); | 521 memory_request->RunDoneCallback(false); |
| 402 } | 522 } |
| 403 for (auto& file_request : old_file_tasks) { | 523 for (auto& file_request : old_file_tasks) { |
| 524 // OnBlobFileDelete is registered when RunDoneCallback is called with | |
| 525 // |true|, so manually do disk accounting. | |
| 526 disk_used_ -= file_request->allocation_size(); | |
| 404 file_request->RunDoneCallback(std::vector<FileCreationInfo>(), false); | 527 file_request->RunDoneCallback(std::vector<FileCreationInfo>(), false); |
| 405 } | 528 } |
| 406 } | 529 } |
| 407 | 530 |
| 408 BlobMemoryController::Strategy BlobMemoryController::DetermineStrategy( | 531 BlobMemoryController::Strategy BlobMemoryController::DetermineStrategy( |
| 409 size_t preemptive_transported_bytes, | 532 size_t preemptive_transported_bytes, |
| 410 uint64_t total_transportation_bytes) const { | 533 uint64_t total_transportation_bytes) const { |
| 411 if (total_transportation_bytes == 0) | 534 if (total_transportation_bytes == 0) |
| 412 return Strategy::NONE_NEEDED; | 535 return Strategy::NONE_NEEDED; |
| 413 if (!CanReserveQuota(total_transportation_bytes)) | 536 if (!CanReserveQuota(total_transportation_bytes)) |
| 414 return Strategy::TOO_LARGE; | 537 return Strategy::TOO_LARGE; |
| 415 | 538 |
| 416 // Handle the case where we have all the bytes preemptively transported, and | 539 // Handle the case where we have all the bytes preemptively transported, and |
| 417 // we can also fit them. | 540 // we can also fit them. |
| 418 if (preemptive_transported_bytes == total_transportation_bytes && | 541 if (preemptive_transported_bytes == total_transportation_bytes && |
| 419 pending_memory_quota_tasks_.empty() && | 542 pending_memory_quota_tasks_.empty() && |
| 420 preemptive_transported_bytes < GetAvailableMemoryForBlobs()) { | 543 preemptive_transported_bytes <= GetAvailableMemoryForBlobs()) { |
| 421 return Strategy::NONE_NEEDED; | 544 return Strategy::NONE_NEEDED; |
| 422 } | 545 } |
| 423 if (file_paging_enabled_ && | 546 if (file_paging_enabled_ && |
| 424 (total_transportation_bytes > limits_.memory_limit_before_paging())) { | 547 total_transportation_bytes <= GetAvailableFileSpaceForBlobs() && |
| 548 total_transportation_bytes > limits_.memory_limit_before_paging()) { | |
| 425 return Strategy::FILE; | 549 return Strategy::FILE; |
| 426 } | 550 } |
| 427 if (total_transportation_bytes > limits_.max_ipc_memory_size) | 551 if (total_transportation_bytes > limits_.max_ipc_memory_size) |
| 428 return Strategy::SHARED_MEMORY; | 552 return Strategy::SHARED_MEMORY; |
| 429 return Strategy::IPC; | 553 return Strategy::IPC; |
| 430 } | 554 } |
| 431 | 555 |
| 432 bool BlobMemoryController::CanReserveQuota(uint64_t size) const { | 556 bool BlobMemoryController::CanReserveQuota(uint64_t size) const { |
| 433 // We check each size independently as a blob can't be constructed in both | 557 // We check each size independently as a blob can't be constructed in both |
| 434 // disk and memory. | 558 // disk and memory. |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 481 auto weak_ptr = AppendMemoryTask( | 605 auto weak_ptr = AppendMemoryTask( |
| 482 total_bytes_needed, std::move(unreserved_memory_items), done_callback); | 606 total_bytes_needed, std::move(unreserved_memory_items), done_callback); |
| 483 MaybeScheduleEvictionUntilSystemHealthy(); | 607 MaybeScheduleEvictionUntilSystemHealthy(); |
| 484 return weak_ptr; | 608 return weak_ptr; |
| 485 } | 609 } |
| 486 | 610 |
| 487 base::WeakPtr<QuotaAllocationTask> BlobMemoryController::ReserveFileQuota( | 611 base::WeakPtr<QuotaAllocationTask> BlobMemoryController::ReserveFileQuota( |
| 488 std::vector<scoped_refptr<ShareableBlobDataItem>> unreserved_file_items, | 612 std::vector<scoped_refptr<ShareableBlobDataItem>> unreserved_file_items, |
| 489 const FileQuotaRequestCallback& done_callback) { | 613 const FileQuotaRequestCallback& done_callback) { |
| 490 pending_file_quota_tasks_.push_back(base::MakeUnique<FileQuotaAllocationTask>( | 614 pending_file_quota_tasks_.push_back(base::MakeUnique<FileQuotaAllocationTask>( |
| 491 this, std::move(unreserved_file_items), done_callback)); | 615 this, disk_space_test_getter_, std::move(unreserved_file_items), |
| 616 done_callback)); | |
| 492 pending_file_quota_tasks_.back()->set_my_list_position( | 617 pending_file_quota_tasks_.back()->set_my_list_position( |
| 493 --pending_file_quota_tasks_.end()); | 618 --pending_file_quota_tasks_.end()); |
| 494 return pending_file_quota_tasks_.back()->GetWeakPtr(); | 619 return pending_file_quota_tasks_.back()->GetWeakPtr(); |
| 495 } | 620 } |
| 496 | 621 |
| 497 void BlobMemoryController::NotifyMemoryItemsUsed( | 622 void BlobMemoryController::NotifyMemoryItemsUsed( |
| 498 const std::vector<scoped_refptr<ShareableBlobDataItem>>& items) { | 623 const std::vector<scoped_refptr<ShareableBlobDataItem>>& items) { |
| 499 for (const auto& item : items) { | 624 for (const auto& item : items) { |
| 500 if (item->item()->type() != DataElement::TYPE_BYTES || | 625 if (item->item()->type() != DataElement::TYPE_BYTES || |
| 501 item->state() != ShareableBlobDataItem::POPULATED_WITH_QUOTA) { | 626 item->state() != ShareableBlobDataItem::POPULATED_WITH_QUOTA) { |
| 502 continue; | 627 continue; |
| 503 } | 628 } |
| 504 // We don't want to re-add the item if we're currently paging it to disk. | 629 // We don't want to re-add the item if we're currently paging it to disk. |
| 505 if (items_paging_to_file_.find(item->item_id()) != | 630 if (items_paging_to_file_.find(item->item_id()) != |
| 506 items_paging_to_file_.end()) { | 631 items_paging_to_file_.end()) { |
| 507 return; | 632 return; |
| 508 } | 633 } |
| 509 auto iterator = populated_memory_items_.Get(item->item_id()); | 634 auto iterator = populated_memory_items_.Get(item->item_id()); |
| 510 if (iterator == populated_memory_items_.end()) { | 635 if (iterator == populated_memory_items_.end()) { |
| 511 populated_memory_items_bytes_ += | 636 populated_memory_items_bytes_ += |
| 512 static_cast<size_t>(item->item()->length()); | 637 static_cast<size_t>(item->item()->length()); |
| 513 populated_memory_items_.Put(item->item_id(), item.get()); | 638 populated_memory_items_.Put(item->item_id(), item.get()); |
| 514 } | 639 } |
| 515 } | 640 } |
| 516 MaybeScheduleEvictionUntilSystemHealthy(); | 641 MaybeScheduleEvictionUntilSystemHealthy(); |
| 517 } | 642 } |
| 518 | 643 |
| 644 void BlobMemoryController::CalculateBlobStorageLimits() { | |
| 645 if (file_runner_) { | |
| 646 PostTaskAndReplyWithResult( | |
| 647 file_runner_.get(), FROM_HERE, | |
| 648 base::Bind(&CalculateBlobStorageLimitsImpl, blob_storage_dir_, true), | |
| 649 base::Bind(&BlobMemoryController::OnStorageLimitsCalculated, | |
| 650 weak_factory_.GetWeakPtr())); | |
| 651 } else { | |
| 652 OnStorageLimitsCalculated( | |
| 653 CalculateBlobStorageLimitsImpl(blob_storage_dir_, false)); | |
| 654 } | |
| 655 } | |
| 656 | |
| 657 base::WeakPtr<BlobMemoryController> BlobMemoryController::GetWeakPtr() { | |
| 658 return weak_factory_.GetWeakPtr(); | |
| 659 } | |
| 660 | |
| 661 void BlobMemoryController::OnStorageLimitsCalculated(BlobStorageLimits limits) { | |
| 662 if (!limits.IsValid() || manual_limits_set_) | |
| 663 return; | |
| 664 limits_ = limits; | |
| 665 } | |
| 666 | |
| 667 void BlobMemoryController::AdjustDiskUsage(uint64_t avail_disk) { | |
| 668 DCHECK_LE(disk_used_, | |
| 669 limits_.desired_max_disk + limits_.min_available_disk_space()); | |
| 670 | |
| 671 if (avail_disk <= limits_.min_available_disk_space()) { | |
| 672 limits_.effective_max_disk = disk_used_; | |
| 673 } else if (avail_disk < | |
| 674 limits_.min_available_disk_space() + limits_.desired_max_disk) { | |
| 675 limits_.effective_max_disk = | |
| 676 avail_disk - limits_.min_available_disk_space() + disk_used_; | |
| 677 } else { | |
| 678 limits_.effective_max_disk = limits_.desired_max_disk; | |
| 679 } | |
| 680 } | |
| 681 | |
| 519 base::WeakPtr<QuotaAllocationTask> BlobMemoryController::AppendMemoryTask( | 682 base::WeakPtr<QuotaAllocationTask> BlobMemoryController::AppendMemoryTask( |
| 520 uint64_t total_bytes_needed, | 683 uint64_t total_bytes_needed, |
| 521 std::vector<scoped_refptr<ShareableBlobDataItem>> unreserved_memory_items, | 684 std::vector<scoped_refptr<ShareableBlobDataItem>> unreserved_memory_items, |
| 522 const MemoryQuotaRequestCallback& done_callback) { | 685 const MemoryQuotaRequestCallback& done_callback) { |
| 523 DCHECK(file_paging_enabled_) | 686 DCHECK(file_paging_enabled_) |
| 524 << "Caller tried to reserve memory when CanReserveQuota(" | 687 << "Caller tried to reserve memory when CanReserveQuota(" |
| 525 << total_bytes_needed << ") would have returned false."; | 688 << total_bytes_needed << ") would have returned false."; |
| 526 | 689 |
| 527 pending_memory_quota_total_size_ += total_bytes_needed; | 690 pending_memory_quota_total_size_ += total_bytes_needed; |
| 528 pending_memory_quota_tasks_.push_back( | 691 pending_memory_quota_tasks_.push_back( |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 567 return total_items_size.ValueOrDie(); | 730 return total_items_size.ValueOrDie(); |
| 568 } | 731 } |
| 569 | 732 |
| 570 void BlobMemoryController::MaybeScheduleEvictionUntilSystemHealthy() { | 733 void BlobMemoryController::MaybeScheduleEvictionUntilSystemHealthy() { |
| 571 // Don't do eviction when others are happening, as we don't change our | 734 // Don't do eviction when others are happening, as we don't change our |
| 572 // pending_memory_quota_total_size_ value until after the paging files have | 735 // pending_memory_quota_total_size_ value until after the paging files have |
| 573 // been written. | 736 // been written. |
| 574 if (pending_evictions_ != 0 || !file_paging_enabled_) | 737 if (pending_evictions_ != 0 || !file_paging_enabled_) |
| 575 return; | 738 return; |
| 576 | 739 |
| 740 uint64_t total_memory_usage = | |
| 741 static_cast<uint64_t>(pending_memory_quota_total_size_) + | |
| 742 blob_memory_used_; | |
| 743 | |
| 577 // We try to page items to disk until our current system size + requested | 744 // We try to page items to disk until our current system size + requested |
| 578 // memory is below our size limit. | 745 // memory is below our size limit. |
| 579 while (pending_memory_quota_total_size_ + blob_memory_used_ > | 746 // Size limit is a lower |memory_limit_before_paging()| if we have disk space. |
| 580 limits_.memory_limit_before_paging()) { | 747 while (total_memory_usage > limits_.effective_max_disk || |
| 748 (disk_used_ < limits_.effective_max_disk && | |
| 749 total_memory_usage > limits_.memory_limit_before_paging())) { | |
| 581 // We only page when we have enough items to fill a whole page file. | 750 // We only page when we have enough items to fill a whole page file. |
| 582 if (populated_memory_items_bytes_ < limits_.min_page_file_size) | 751 if (populated_memory_items_bytes_ < limits_.min_page_file_size) |
| 583 break; | 752 break; |
| 584 DCHECK_LE(limits_.min_page_file_size, | 753 DCHECK_LE(limits_.min_page_file_size, |
| 585 static_cast<uint64_t>(blob_memory_used_)); | 754 static_cast<uint64_t>(blob_memory_used_)); |
| 586 | 755 |
| 587 std::vector<scoped_refptr<ShareableBlobDataItem>> items_to_swap; | 756 std::vector<scoped_refptr<ShareableBlobDataItem>> items_to_swap; |
| 588 size_t total_items_size = CollectItemsForEviction(&items_to_swap); | 757 size_t total_items_size = CollectItemsForEviction(&items_to_swap); |
| 589 if (total_items_size == 0) | 758 if (total_items_size == 0) |
| 590 break; | 759 break; |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 609 file_runner_.get()); | 778 file_runner_.get()); |
| 610 // Add the release callback so we decrement our disk usage on file deletion. | 779 // Add the release callback so we decrement our disk usage on file deletion. |
| 611 file_reference->AddFinalReleaseCallback( | 780 file_reference->AddFinalReleaseCallback( |
| 612 base::Bind(&BlobMemoryController::OnBlobFileDelete, | 781 base::Bind(&BlobMemoryController::OnBlobFileDelete, |
| 613 weak_factory_.GetWeakPtr(), total_items_size)); | 782 weak_factory_.GetWeakPtr(), total_items_size)); |
| 614 | 783 |
| 615 // Post the file writing task. | 784 // Post the file writing task. |
| 616 base::PostTaskAndReplyWithResult( | 785 base::PostTaskAndReplyWithResult( |
| 617 file_runner_.get(), FROM_HERE, | 786 file_runner_.get(), FROM_HERE, |
| 618 base::Bind(&CreateFileAndWriteItems, blob_storage_dir_, | 787 base::Bind(&CreateFileAndWriteItems, blob_storage_dir_, |
| 619 base::Passed(&page_file_path), file_runner_, | 788 disk_space_test_getter_, base::Passed(&page_file_path), |
| 620 base::Passed(&items_for_paging), total_items_size), | 789 file_runner_, base::Passed(&items_for_paging), |
| 790 total_items_size), | |
| 621 base::Bind(&BlobMemoryController::OnEvictionComplete, | 791 base::Bind(&BlobMemoryController::OnEvictionComplete, |
| 622 weak_factory_.GetWeakPtr(), base::Passed(&file_reference), | 792 weak_factory_.GetWeakPtr(), base::Passed(&file_reference), |
| 623 base::Passed(&items_to_swap), total_items_size)); | 793 base::Passed(&items_to_swap), total_items_size)); |
| 624 } | 794 } |
| 625 RecordTracingCounters(); | 795 RecordTracingCounters(); |
| 626 } | 796 } |
| 627 | 797 |
| 628 void BlobMemoryController::OnEvictionComplete( | 798 void BlobMemoryController::OnEvictionComplete( |
| 629 scoped_refptr<ShareableFileReference> file_reference, | 799 scoped_refptr<ShareableFileReference> file_reference, |
| 630 std::vector<scoped_refptr<ShareableBlobDataItem>> items, | 800 std::vector<scoped_refptr<ShareableBlobDataItem>> items, |
| 631 size_t total_items_size, | 801 size_t total_items_size, |
| 632 FileCreationInfo result) { | 802 std::pair<FileCreationInfo, int64_t> result) { |
| 633 if (!file_paging_enabled_) | 803 if (!file_paging_enabled_) |
| 634 return; | 804 return; |
| 635 | 805 |
| 636 if (result.error != File::FILE_OK) { | 806 FileCreationInfo& file_info = std::get<0>(result); |
| 637 DisableFilePaging(result.error); | 807 int64_t avail_disk_space = std::get<1>(result); |
| 808 | |
| 809 if (file_info.error != File::FILE_OK) { | |
| 810 DisableFilePaging(file_info.error); | |
| 638 return; | 811 return; |
| 639 } | 812 } |
| 640 | 813 |
| 814 if (avail_disk_space != kUnknownDiskAvailability) { | |
| 815 AdjustDiskUsage(static_cast<uint64_t>(avail_disk_space)); | |
| 816 } | |
| 817 | |
| 641 DCHECK_LT(0, pending_evictions_); | 818 DCHECK_LT(0, pending_evictions_); |
| 642 pending_evictions_--; | 819 pending_evictions_--; |
| 643 | 820 |
| 644 // Switch item from memory to the new file. | 821 // Switch item from memory to the new file. |
| 645 uint64_t offset = 0; | 822 uint64_t offset = 0; |
| 646 for (const scoped_refptr<ShareableBlobDataItem>& shareable_item : items) { | 823 for (const scoped_refptr<ShareableBlobDataItem>& shareable_item : items) { |
| 647 scoped_refptr<BlobDataItem> new_item( | 824 scoped_refptr<BlobDataItem> new_item( |
| 648 new BlobDataItem(base::WrapUnique(new DataElement()), file_reference)); | 825 new BlobDataItem(base::WrapUnique(new DataElement()), file_reference)); |
| 649 new_item->data_element_ptr()->SetToFilePathRange( | 826 new_item->data_element_ptr()->SetToFilePathRange( |
| 650 file_reference->path(), offset, shareable_item->item()->length(), | 827 file_reference->path(), offset, shareable_item->item()->length(), |
| 651 result.last_modified); | 828 file_info.last_modified); |
| 652 DCHECK(shareable_item->memory_allocation_); | 829 DCHECK(shareable_item->memory_allocation_); |
| 653 shareable_item->set_memory_allocation(nullptr); | 830 shareable_item->set_memory_allocation(nullptr); |
| 654 shareable_item->set_item(new_item); | 831 shareable_item->set_item(new_item); |
| 655 items_paging_to_file_.erase(shareable_item->item_id()); | 832 items_paging_to_file_.erase(shareable_item->item_id()); |
| 656 offset += shareable_item->item()->length(); | 833 offset += shareable_item->item()->length(); |
| 657 } | 834 } |
| 658 in_flight_memory_used_ -= total_items_size; | 835 in_flight_memory_used_ -= total_items_size; |
| 659 | 836 |
| 660 // We want callback on blobs up to the amount we've freed. | 837 // We want callback on blobs up to the amount we've freed. |
| 661 MaybeGrantPendingMemoryRequests(); | 838 MaybeGrantPendingMemoryRequests(); |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 689 uint64_t BlobMemoryController::GetAvailableFileSpaceForBlobs() const { | 866 uint64_t BlobMemoryController::GetAvailableFileSpaceForBlobs() const { |
| 690 if (!file_paging_enabled_) | 867 if (!file_paging_enabled_) |
| 691 return 0; | 868 return 0; |
| 692 // Sometimes we're only paging part of what we need for the new blob, so add | 869 // Sometimes we're only paging part of what we need for the new blob, so add |
| 693 // the rest of the size we need into our disk usage if this is the case. | 870 // the rest of the size we need into our disk usage if this is the case. |
| 694 uint64_t total_disk_used = disk_used_; | 871 uint64_t total_disk_used = disk_used_; |
| 695 if (in_flight_memory_used_ < pending_memory_quota_total_size_) { | 872 if (in_flight_memory_used_ < pending_memory_quota_total_size_) { |
| 696 total_disk_used += | 873 total_disk_used += |
| 697 pending_memory_quota_total_size_ - in_flight_memory_used_; | 874 pending_memory_quota_total_size_ - in_flight_memory_used_; |
| 698 } | 875 } |
| 699 if (limits_.max_blob_disk_space < total_disk_used) | 876 if (limits_.effective_max_disk < total_disk_used) |
| 700 return 0; | 877 return 0; |
| 701 return limits_.max_blob_disk_space - total_disk_used; | 878 return limits_.effective_max_disk - total_disk_used; |
| 702 } | 879 } |
| 703 | 880 |
| 704 void BlobMemoryController::GrantMemoryAllocations( | 881 void BlobMemoryController::GrantMemoryAllocations( |
| 705 std::vector<scoped_refptr<ShareableBlobDataItem>>* items, | 882 std::vector<scoped_refptr<ShareableBlobDataItem>>* items, |
| 706 size_t total_bytes) { | 883 size_t total_bytes) { |
| 707 // These metrics let us calculate the global distribution of blob storage by | 884 // These metrics let us calculate the global distribution of blob storage by |
| 708 // subtracting the histograms. | 885 // subtracting the histograms. |
| 709 UMA_HISTOGRAM_COUNTS("Storage.Blob.StorageSizeBeforeAppend", | 886 UMA_HISTOGRAM_COUNTS("Storage.Blob.StorageSizeBeforeAppend", |
| 710 blob_memory_used_ / 1024); | 887 blob_memory_used_ / 1024); |
| 711 blob_memory_used_ += total_bytes; | 888 blob_memory_used_ += total_bytes; |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 741 MaybeGrantPendingMemoryRequests(); | 918 MaybeGrantPendingMemoryRequests(); |
| 742 } | 919 } |
| 743 | 920 |
| 744 void BlobMemoryController::OnBlobFileDelete(uint64_t size, | 921 void BlobMemoryController::OnBlobFileDelete(uint64_t size, |
| 745 const FilePath& path) { | 922 const FilePath& path) { |
| 746 DCHECK_LE(size, disk_used_); | 923 DCHECK_LE(size, disk_used_); |
| 747 disk_used_ -= size; | 924 disk_used_ -= size; |
| 748 } | 925 } |
| 749 | 926 |
| 750 } // namespace storage | 927 } // namespace storage |
| OLD | NEW |