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