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

Side by Side Diff: storage/browser/blob/blob_memory_controller.cc

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

Powered by Google App Engine
This is Rietveld 408576698