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 | 9 |
9 #include "base/callback.h" | 10 #include "base/callback.h" |
10 #include "base/callback_helpers.h" | 11 #include "base/callback_helpers.h" |
11 #include "base/containers/small_map.h" | 12 #include "base/containers/small_map.h" |
12 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
13 #include "base/guid.h" | 14 #include "base/guid.h" |
14 #include "base/location.h" | 15 #include "base/location.h" |
15 #include "base/memory/ptr_util.h" | 16 #include "base/memory/ptr_util.h" |
16 #include "base/metrics/histogram_macros.h" | 17 #include "base/metrics/histogram_macros.h" |
17 #include "base/numerics/safe_conversions.h" | 18 #include "base/numerics/safe_conversions.h" |
(...skipping 20 matching lines...) Expand all Loading... |
38 namespace { | 39 namespace { |
39 using FileCreationInfo = BlobMemoryController::FileCreationInfo; | 40 using FileCreationInfo = BlobMemoryController::FileCreationInfo; |
40 using MemoryAllocation = BlobMemoryController::MemoryAllocation; | 41 using MemoryAllocation = BlobMemoryController::MemoryAllocation; |
41 using QuotaAllocationTask = BlobMemoryController::QuotaAllocationTask; | 42 using QuotaAllocationTask = BlobMemoryController::QuotaAllocationTask; |
42 | 43 |
43 File::Error CreateBlobDirectory(const FilePath& blob_storage_dir) { | 44 File::Error CreateBlobDirectory(const FilePath& blob_storage_dir) { |
44 File::Error error = File::FILE_OK; | 45 File::Error error = File::FILE_OK; |
45 base::CreateDirectoryAndGetError(blob_storage_dir, &error); | 46 base::CreateDirectoryAndGetError(blob_storage_dir, &error); |
46 UMA_HISTOGRAM_ENUMERATION("Storage.Blob.CreateDirectoryResult", -error, | 47 UMA_HISTOGRAM_ENUMERATION("Storage.Blob.CreateDirectoryResult", -error, |
47 -File::FILE_ERROR_MAX); | 48 -File::FILE_ERROR_MAX); |
| 49 DLOG_IF(ERROR, error != File::FILE_OK) |
| 50 << "Error creating blob storage directory: " << error; |
48 return error; | 51 return error; |
49 } | 52 } |
50 | 53 |
51 void DestructFile(File infos_without_references) {} | 54 void DestructFile(File infos_without_references) {} |
52 | 55 |
53 // Used for new unpopulated file items. Caller must populate file reference in | 56 // Used for new unpopulated file items. Caller must populate file reference in |
54 // returned FileCreationInfos. | 57 // returned FileCreationInfos. |
55 std::pair<std::vector<FileCreationInfo>, File::Error> CreateEmptyFiles( | 58 std::pair<std::vector<FileCreationInfo>, File::Error> CreateEmptyFiles( |
56 const FilePath& blob_storage_dir, | 59 const FilePath& blob_storage_dir, |
57 scoped_refptr<base::TaskRunner> file_task_runner, | 60 scoped_refptr<base::TaskRunner> file_task_runner, |
58 std::vector<base::FilePath> file_paths) { | 61 std::vector<base::FilePath> file_paths) { |
59 base::ThreadRestrictions::AssertIOAllowed(); | 62 base::ThreadRestrictions::AssertIOAllowed(); |
60 | 63 |
61 File::Error dir_create_status = CreateBlobDirectory(blob_storage_dir); | 64 File::Error dir_create_status = CreateBlobDirectory(blob_storage_dir); |
62 if (dir_create_status != File::FILE_OK) | 65 if (dir_create_status != File::FILE_OK) |
63 return std::make_pair(std::vector<FileCreationInfo>(), dir_create_status); | 66 return std::make_pair(std::vector<FileCreationInfo>(), dir_create_status); |
64 | 67 |
65 std::vector<FileCreationInfo> result; | 68 std::vector<FileCreationInfo> result; |
66 for (const base::FilePath& file_path : file_paths) { | 69 for (const base::FilePath& file_path : file_paths) { |
67 FileCreationInfo creation_info; | 70 FileCreationInfo creation_info; |
68 // Try to open our file. | 71 // Try to open our file. |
69 File file(file_path, File::FLAG_CREATE_ALWAYS | File::FLAG_WRITE); | 72 File file(file_path, File::FLAG_CREATE_ALWAYS | File::FLAG_WRITE); |
70 creation_info.path = std::move(file_path); | 73 creation_info.path = std::move(file_path); |
71 creation_info.file_deletion_runner = file_task_runner; | 74 creation_info.file_deletion_runner = file_task_runner; |
72 creation_info.error = file.error_details(); | 75 creation_info.error = file.error_details(); |
73 if (creation_info.error != File::FILE_OK) { | 76 if (creation_info.error != File::FILE_OK) { |
74 return std::make_pair(std::vector<FileCreationInfo>(), | 77 return std::make_pair(std::vector<FileCreationInfo>(), |
75 creation_info.error); | 78 creation_info.error); |
76 } | 79 } |
77 | |
78 // Grab the file info to get the "last modified" time and store the file. | |
79 File::Info file_info; | |
80 bool success = file.GetInfo(&file_info); | |
81 creation_info.error = success ? File::FILE_OK : File::FILE_ERROR_FAILED; | |
82 if (!success) { | |
83 return std::make_pair(std::vector<FileCreationInfo>(), | |
84 creation_info.error); | |
85 } | |
86 creation_info.file = std::move(file); | 80 creation_info.file = std::move(file); |
87 | 81 |
88 result.push_back(std::move(creation_info)); | 82 result.push_back(std::move(creation_info)); |
89 } | 83 } |
90 return std::make_pair(std::move(result), File::FILE_OK); | 84 return std::make_pair(std::move(result), File::FILE_OK); |
91 } | 85 } |
92 | 86 |
93 // Used to evict multiple memory items out to a single file. Caller must | 87 // Used to evict multiple memory items out to a single file. Caller must |
94 // populate file reference in returned FileCreationInfo. | 88 // populate file reference in returned FileCreationInfo. |
95 FileCreationInfo CreateFileAndWriteItems( | 89 FileCreationInfo CreateFileAndWriteItems( |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 file.WriteAtCurrentPos(element->bytes() + (length - bytes_left), | 121 file.WriteAtCurrentPos(element->bytes() + (length - bytes_left), |
128 base::saturated_cast<int>(bytes_left)); | 122 base::saturated_cast<int>(bytes_left)); |
129 if (bytes_written < 0) | 123 if (bytes_written < 0) |
130 break; | 124 break; |
131 DCHECK_LE(static_cast<size_t>(bytes_written), bytes_left); | 125 DCHECK_LE(static_cast<size_t>(bytes_written), bytes_left); |
132 bytes_left -= bytes_written; | 126 bytes_left -= bytes_written; |
133 } | 127 } |
134 if (bytes_written < 0) | 128 if (bytes_written < 0) |
135 break; | 129 break; |
136 } | 130 } |
| 131 if (!file.Flush()) { |
| 132 creation_info.error = File::FILE_ERROR_FAILED; |
| 133 return creation_info; |
| 134 } |
137 | 135 |
138 File::Info info; | 136 File::Info info; |
139 bool success = file.GetInfo(&info); | 137 bool success = file.GetInfo(&info); |
140 creation_info.error = | 138 creation_info.error = |
141 bytes_written < 0 || !success ? File::FILE_ERROR_FAILED : File::FILE_OK; | 139 bytes_written < 0 || !success ? File::FILE_ERROR_FAILED : File::FILE_OK; |
142 creation_info.last_modified = info.last_modified; | 140 creation_info.last_modified = info.last_modified; |
143 return creation_info; | 141 return creation_info; |
144 } | 142 } |
145 | 143 |
146 uint64_t GetTotalSizeAndFileSizes( | 144 uint64_t GetTotalSizeAndFileSizes( |
147 const std::vector<scoped_refptr<ShareableBlobDataItem>>& | 145 const std::vector<scoped_refptr<ShareableBlobDataItem>>& |
148 unreserved_file_items, | 146 unreserved_file_items, |
149 std::vector<uint64_t>* file_sizes_output) { | 147 std::vector<uint64_t>* file_sizes_output) { |
150 uint64_t total_size_output = 0; | 148 uint64_t total_size_output = 0; |
151 base::SmallMap<std::map<uint64_t, uint64_t>> file_id_to_sizes; | 149 base::SmallMap<std::map<uint64_t, uint64_t>> file_id_to_sizes; |
152 for (const auto& item : unreserved_file_items) { | 150 for (const auto& item : unreserved_file_items) { |
153 const DataElement& element = item->item()->data_element(); | 151 const DataElement& element = item->item()->data_element(); |
154 uint64_t file_id = BlobDataBuilder::GetFutureFileID(element); | 152 uint64_t file_id = BlobDataBuilder::GetFutureFileID(element); |
155 auto it = file_id_to_sizes.find(file_id); | 153 auto it = file_id_to_sizes.find(file_id); |
156 if (it != file_id_to_sizes.end()) | 154 if (it != file_id_to_sizes.end()) |
157 it->second = std::max(it->second, element.offset() + element.length()); | 155 it->second = std::max(it->second, element.offset() + element.length()); |
158 else | 156 else |
159 file_id_to_sizes[file_id] = element.offset() + element.length(); | 157 file_id_to_sizes[file_id] = element.offset() + element.length(); |
160 total_size_output += element.length(); | 158 total_size_output += element.length(); |
161 } | 159 } |
162 for (const auto& size_pair : file_id_to_sizes) { | 160 for (const auto& size_pair : file_id_to_sizes) { |
163 file_sizes_output->push_back(size_pair.second); | 161 file_sizes_output->push_back(size_pair.second); |
164 } | 162 } |
| 163 DCHECK_EQ(std::accumulate(file_sizes_output->begin(), |
| 164 file_sizes_output->end(), 0ull), |
| 165 total_size_output) |
| 166 << "Illegal builder configuration, temporary files must be totally used."; |
165 return total_size_output; | 167 return total_size_output; |
166 } | 168 } |
167 | 169 |
168 } // namespace | 170 } // namespace |
169 | 171 |
170 FileCreationInfo::FileCreationInfo() {} | 172 FileCreationInfo::FileCreationInfo() {} |
171 FileCreationInfo::~FileCreationInfo() { | 173 FileCreationInfo::~FileCreationInfo() { |
172 if (file.IsValid()) { | 174 if (file.IsValid()) { |
173 DCHECK(file_deletion_runner); | 175 DCHECK(file_deletion_runner); |
174 file_deletion_runner->PostTask( | 176 file_deletion_runner->PostTask( |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
291 base::PostTaskAndReplyWithResult( | 293 base::PostTaskAndReplyWithResult( |
292 controller_->file_runner_.get(), FROM_HERE, | 294 controller_->file_runner_.get(), FROM_HERE, |
293 base::Bind(&CreateEmptyFiles, controller_->blob_storage_dir_, | 295 base::Bind(&CreateEmptyFiles, controller_->blob_storage_dir_, |
294 controller_->file_runner_, base::Passed(&file_paths)), | 296 controller_->file_runner_, base::Passed(&file_paths)), |
295 base::Bind(&FileQuotaAllocationTask::OnCreateEmptyFiles, | 297 base::Bind(&FileQuotaAllocationTask::OnCreateEmptyFiles, |
296 weak_factory_.GetWeakPtr(), base::Passed(&references))); | 298 weak_factory_.GetWeakPtr(), base::Passed(&references))); |
297 controller_->RecordTracingCounters(); | 299 controller_->RecordTracingCounters(); |
298 } | 300 } |
299 ~FileQuotaAllocationTask() override {} | 301 ~FileQuotaAllocationTask() override {} |
300 | 302 |
301 void RunDoneCallback(bool success, std::vector<FileCreationInfo> file_info) { | 303 void RunDoneCallback(std::vector<FileCreationInfo> file_info, bool success) { |
302 // Make sure we clear the weak pointers we gave to the caller beforehand. | 304 // Make sure we clear the weak pointers we gave to the caller beforehand. |
303 weak_factory_.InvalidateWeakPtrs(); | 305 weak_factory_.InvalidateWeakPtrs(); |
304 | 306 |
305 // We want to destroy this object on the exit of this method if we were | 307 // We want to destroy this object on the exit of this method if we were |
306 // successful. | 308 // successful. |
307 std::unique_ptr<FileQuotaAllocationTask> this_object; | 309 std::unique_ptr<FileQuotaAllocationTask> this_object; |
308 if (success) { | 310 if (success) { |
309 for (auto& item : pending_items_) { | 311 for (auto& item : pending_items_) { |
310 item->set_state(ShareableBlobDataItem::QUOTA_GRANTED); | 312 item->set_state(ShareableBlobDataItem::QUOTA_GRANTED); |
311 } | 313 } |
312 this_object = std::move(*my_list_position_); | 314 this_object = std::move(*my_list_position_); |
313 controller_->pending_file_quota_tasks_.erase(my_list_position_); | 315 controller_->pending_file_quota_tasks_.erase(my_list_position_); |
314 } | 316 } |
315 | 317 |
316 done_callback_.Run(success, std::move(file_info)); | 318 done_callback_.Run(std::move(file_info), success); |
317 } | 319 } |
318 | 320 |
319 base::WeakPtr<QuotaAllocationTask> GetWeakPtr() { | 321 base::WeakPtr<QuotaAllocationTask> GetWeakPtr() { |
320 return weak_factory_.GetWeakPtr(); | 322 return weak_factory_.GetWeakPtr(); |
321 } | 323 } |
322 | 324 |
323 void Cancel() override { | 325 void Cancel() override { |
324 // This call destroys this object. We rely on ShareableFileReference's | 326 // This call destroys this object. We rely on ShareableFileReference's |
325 // final release callback for disk_usage_ accounting. | 327 // final release callback for disk_usage_ accounting. |
326 controller_->pending_file_quota_tasks_.erase(my_list_position_); | 328 controller_->pending_file_quota_tasks_.erase(my_list_position_); |
327 } | 329 } |
328 | 330 |
329 void OnCreateEmptyFiles( | 331 void OnCreateEmptyFiles( |
330 std::vector<scoped_refptr<ShareableFileReference>> references, | 332 std::vector<scoped_refptr<ShareableFileReference>> references, |
331 std::pair<std::vector<FileCreationInfo>, File::Error> files_and_error) { | 333 std::pair<std::vector<FileCreationInfo>, File::Error> files_and_error) { |
332 auto& files = files_and_error.first; | 334 auto& files = files_and_error.first; |
333 if (files.empty()) { | 335 if (files.empty()) { |
334 DCHECK_GE(controller_->disk_used_, allocation_size_); | 336 DCHECK_GE(controller_->disk_used_, allocation_size_); |
335 controller_->disk_used_ -= allocation_size_; | 337 controller_->disk_used_ -= allocation_size_; |
336 // This will call our callback and delete the object correctly. | 338 // This will call our callback and delete the object correctly. |
337 controller_->DisableFilePaging(files_and_error.second); | 339 controller_->DisableFilePaging(files_and_error.second); |
338 return; | 340 return; |
339 } | 341 } |
340 DCHECK_EQ(files.size(), references.size()); | 342 DCHECK_EQ(files.size(), references.size()); |
341 for (size_t i = 0; i < files.size(); i++) { | 343 for (size_t i = 0; i < files.size(); i++) { |
342 files[i].file_reference = std::move(references[i]); | 344 files[i].file_reference = std::move(references[i]); |
343 } | 345 } |
344 RunDoneCallback(true, std::move(files)); | 346 RunDoneCallback(std::move(files), true); |
345 } | 347 } |
346 | 348 |
347 // The my_list_position_ iterator is stored so that we can remove ourself | 349 // The my_list_position_ iterator is stored so that we can remove ourself |
348 // from the task list when we are cancelled. | 350 // from the task list when we are cancelled. |
349 void set_my_list_position( | 351 void set_my_list_position( |
350 PendingFileQuotaTaskList::iterator my_list_position) { | 352 PendingFileQuotaTaskList::iterator my_list_position) { |
351 my_list_position_ = my_list_position; | 353 my_list_position_ = my_list_position; |
352 } | 354 } |
353 | 355 |
354 private: | 356 private: |
(...skipping 17 matching lines...) Expand all Loading... |
372 file_runner_(std::move(file_runner)), | 374 file_runner_(std::move(file_runner)), |
373 populated_memory_items_( | 375 populated_memory_items_( |
374 base::MRUCache<uint64_t, ShareableBlobDataItem*>::NO_AUTO_EVICT), | 376 base::MRUCache<uint64_t, ShareableBlobDataItem*>::NO_AUTO_EVICT), |
375 weak_factory_(this) {} | 377 weak_factory_(this) {} |
376 | 378 |
377 BlobMemoryController::~BlobMemoryController() {} | 379 BlobMemoryController::~BlobMemoryController() {} |
378 | 380 |
379 void BlobMemoryController::DisableFilePaging(base::File::Error reason) { | 381 void BlobMemoryController::DisableFilePaging(base::File::Error reason) { |
380 UMA_HISTOGRAM_ENUMERATION("Storage.Blob.PagingDisabled", -reason, | 382 UMA_HISTOGRAM_ENUMERATION("Storage.Blob.PagingDisabled", -reason, |
381 -File::FILE_ERROR_MAX); | 383 -File::FILE_ERROR_MAX); |
| 384 DLOG(ERROR) << "Blob storage paging disabled, reason: " << reason; |
382 file_paging_enabled_ = false; | 385 file_paging_enabled_ = false; |
383 in_flight_memory_used_ = 0; | 386 in_flight_memory_used_ = 0; |
384 items_paging_to_file_.clear(); | 387 items_paging_to_file_.clear(); |
385 pending_evictions_ = 0; | 388 pending_evictions_ = 0; |
386 pending_memory_quota_total_size_ = 0; | 389 pending_memory_quota_total_size_ = 0; |
387 populated_memory_items_.Clear(); | 390 populated_memory_items_.Clear(); |
388 populated_memory_items_bytes_ = 0; | 391 populated_memory_items_bytes_ = 0; |
389 file_runner_ = nullptr; | 392 file_runner_ = nullptr; |
390 | 393 |
391 PendingMemoryQuotaTaskList old_memory_tasks; | 394 PendingMemoryQuotaTaskList old_memory_tasks; |
392 PendingFileQuotaTaskList old_file_tasks; | 395 PendingFileQuotaTaskList old_file_tasks; |
393 std::swap(old_memory_tasks, pending_memory_quota_tasks_); | 396 std::swap(old_memory_tasks, pending_memory_quota_tasks_); |
394 std::swap(old_file_tasks, pending_file_quota_tasks_); | 397 std::swap(old_file_tasks, pending_file_quota_tasks_); |
395 | 398 |
396 // Don't call the callbacks until we have a consistent state. | 399 // Don't call the callbacks until we have a consistent state. |
397 for (auto& memory_request : old_memory_tasks) { | 400 for (auto& memory_request : old_memory_tasks) { |
398 memory_request->RunDoneCallback(false); | 401 memory_request->RunDoneCallback(false); |
399 } | 402 } |
400 for (auto& file_request : old_file_tasks) { | 403 for (auto& file_request : old_file_tasks) { |
401 file_request->RunDoneCallback(false, std::vector<FileCreationInfo>()); | 404 file_request->RunDoneCallback(std::vector<FileCreationInfo>(), false); |
402 } | 405 } |
403 } | 406 } |
404 | 407 |
405 BlobMemoryController::Strategy BlobMemoryController::DetermineStrategy( | 408 BlobMemoryController::Strategy BlobMemoryController::DetermineStrategy( |
406 size_t preemptive_transported_bytes, | 409 size_t preemptive_transported_bytes, |
407 uint64_t total_transportation_bytes) const { | 410 uint64_t total_transportation_bytes) const { |
408 if (total_transportation_bytes == 0) | 411 if (total_transportation_bytes == 0) |
409 return Strategy::NONE_NEEDED; | 412 return Strategy::NONE_NEEDED; |
410 if (!CanReserveQuota(total_transportation_bytes)) | 413 if (!CanReserveQuota(total_transportation_bytes)) |
411 return Strategy::TOO_LARGE; | 414 return Strategy::TOO_LARGE; |
(...skipping 326 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
738 MaybeGrantPendingMemoryRequests(); | 741 MaybeGrantPendingMemoryRequests(); |
739 } | 742 } |
740 | 743 |
741 void BlobMemoryController::OnBlobFileDelete(uint64_t size, | 744 void BlobMemoryController::OnBlobFileDelete(uint64_t size, |
742 const FilePath& path) { | 745 const FilePath& path) { |
743 DCHECK_LE(size, disk_used_); | 746 DCHECK_LE(size, disk_used_); |
744 disk_used_ -= size; | 747 disk_used_ -= size; |
745 } | 748 } |
746 | 749 |
747 } // namespace storage | 750 } // namespace storage |
OLD | NEW |