Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "storage/browser/blob/blob_memory_controller.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 | |
| 9 #include "base/callback.h" | |
| 10 #include "base/callback_helpers.h" | |
| 11 #include "base/containers/small_map.h" | |
| 12 #include "base/files/file_util.h" | |
| 13 #include "base/location.h" | |
| 14 #include "base/memory/ptr_util.h" | |
| 15 #include "base/metrics/histogram_macros.h" | |
| 16 #include "base/numerics/safe_conversions.h" | |
| 17 #include "base/numerics/safe_math.h" | |
| 18 #include "base/single_thread_task_runner.h" | |
| 19 #include "base/stl_util.h" | |
| 20 #include "base/strings/string_number_conversions.h" | |
| 21 #include "base/task_runner.h" | |
| 22 #include "base/task_runner_util.h" | |
| 23 #include "base/threading/thread_restrictions.h" | |
| 24 #include "base/time/time.h" | |
| 25 #include "base/trace_event/trace_event.h" | |
| 26 #include "base/tuple.h" | |
| 27 #include "storage/browser/blob/blob_data_builder.h" | |
| 28 #include "storage/browser/blob/blob_data_item.h" | |
| 29 #include "storage/browser/blob/shareable_blob_data_item.h" | |
| 30 #include "storage/browser/blob/shareable_file_reference.h" | |
| 31 #include "storage/common/data_element.h" | |
| 32 | |
| 33 using base::File; | |
| 34 using base::FilePath; | |
| 35 using FileCreationInfo = storage::BlobMemoryController::FileCreationInfo; | |
|
michaeln
2016/09/27 00:09:29
looks like most of the usages are fully qualified,
dmurph
2016/09/29 00:44:22
It shortens a lot of code, I unqualified the insta
| |
| 36 | |
| 37 namespace storage { | |
| 38 namespace { | |
| 39 using QuotaAllocationTask = BlobMemoryController::QuotaAllocationTask; | |
| 40 | |
| 41 using MemoryQuotaRequestCallback = | |
| 42 BlobMemoryController::MemoryQuotaRequestCallback; | |
| 43 using MemoryQuotaRequestCallback = | |
| 44 BlobMemoryController::MemoryQuotaRequestCallback; | |
|
michaeln
2016/09/27 00:09:29
lines 41 and 43 are dups
also is the using stateme
dmurph
2016/09/29 00:44:22
Done.
| |
| 45 | |
| 46 // Here so we can destruct files on the correct thread if the user cancels. | |
| 47 void DestructFiles(std::vector<BlobMemoryController::FileCreationInfo> files) {} | |
|
michaeln
2016/09/28 21:19:27
I don't see this being used anywhere?
It might mak
dmurph
2016/09/29 00:44:20
You're right. This is no longer used, so I'll remo
| |
| 48 | |
| 49 // Creates a file in the given directory w/ the given filename and size. | |
|
pwnall
2016/09/26 13:18:08
Why does this comment say "and size"? I understand
| |
| 50 std::vector<BlobMemoryController::FileCreationInfo> CreateFiles( | |
|
michaeln
2016/09/28 01:08:04
It wasn't clear to me that CreateFiles() and Writ
dmurph
2016/09/29 00:44:21
Sounds good. Thanks for the chat about this, I thi
| |
| 51 std::vector<scoped_refptr<ShareableFileReference>> file_references) { | |
| 52 base::ThreadRestrictions::AssertIOAllowed(); | |
| 53 std::vector<BlobMemoryController::FileCreationInfo> result; | |
| 54 | |
| 55 for (scoped_refptr<ShareableFileReference>& file_ref : file_references) { | |
|
pwnall
2016/09/26 13:18:07
You're using file_ref here, but file_reference in
dmurph
2016/09/29 00:44:21
Done.
| |
| 56 result.push_back(BlobMemoryController::FileCreationInfo()); | |
| 57 BlobMemoryController::FileCreationInfo& creation_info = result.back(); | |
|
pwnall
2016/09/26 13:18:08
Is this a common idiom? If not, would it be possib
dmurph
2016/09/29 00:44:21
Yes it's somewhat common, as you don't have redund
| |
| 58 // Try to open our file. | |
| 59 File file(file_ref->path(), File::FLAG_CREATE_ALWAYS | File::FLAG_WRITE); | |
| 60 creation_info.file_reference = std::move(file_ref); | |
| 61 creation_info.error = file.error_details(); | |
| 62 UMA_HISTOGRAM_ENUMERATION("Storage.Blob.TransportFileCreate", | |
| 63 -creation_info.error, -File::FILE_ERROR_MAX); | |
| 64 if (creation_info.error != File::FILE_OK) | |
| 65 return std::vector<BlobMemoryController::FileCreationInfo>(); | |
| 66 | |
| 67 // Grab the file info to get the "last modified" time and store the file. | |
| 68 File::Info file_info; | |
| 69 bool success = file.GetInfo(&file_info); | |
| 70 UMA_HISTOGRAM_BOOLEAN("Storage.Blob.TransportFileInfoSuccess", success); | |
| 71 creation_info.error = success ? File::FILE_OK : File::FILE_ERROR_FAILED; | |
| 72 if (!success) | |
| 73 return std::vector<BlobMemoryController::FileCreationInfo>(); | |
| 74 creation_info.file = std::move(file); | |
| 75 } | |
| 76 return result; | |
| 77 } | |
| 78 | |
| 79 BlobMemoryController::FileCreationInfo WriteItemsToFile( | |
|
pwnall
2016/09/26 13:18:08
Based on the UMA metrics inside, this should only
dmurph
2016/09/29 00:44:21
Done.
| |
| 80 std::vector<scoped_refptr<ShareableBlobDataItem>>* items, | |
|
pwnall
2016/09/26 13:18:08
You don't seem to be changing the vector in this m
dmurph
2016/09/29 00:44:20
I think I can do this now. The callback actually s
| |
| 81 size_t total_size_bytes, | |
| 82 scoped_refptr<ShareableFileReference> file_reference) { | |
| 83 DCHECK_NE(0u, total_size_bytes); | |
| 84 UMA_HISTOGRAM_MEMORY_KB("Storage.Blob.PageFileSize", total_size_bytes / 1024); | |
| 85 base::ThreadRestrictions::AssertIOAllowed(); | |
|
pwnall
2016/09/26 13:18:08
Sorry, I wasn't clear earlier.
What I meant was -
dmurph
2016/09/29 00:44:22
Ah, yeah we don't have access to that here, that's
| |
| 86 | |
| 87 // Create our file. | |
|
pwnall
2016/09/26 13:18:09
our -> the page?
dmurph
2016/09/29 00:44:22
Done.
| |
| 88 BlobMemoryController::FileCreationInfo creation_info; | |
| 89 File file(file_reference->path(), | |
| 90 File::FLAG_CREATE_ALWAYS | File::FLAG_WRITE); | |
| 91 creation_info.file_reference = std::move(file_reference); | |
| 92 creation_info.error = file.error_details(); | |
| 93 UMA_HISTOGRAM_ENUMERATION("Storage.Blob.PageFileCreate", -creation_info.error, | |
| 94 -File::FILE_ERROR_MAX); | |
| 95 if (creation_info.error != File::FILE_OK) | |
| 96 return creation_info; | |
| 97 | |
| 98 // Write data. | |
| 99 file.SetLength(total_size_bytes); | |
| 100 int bytes_written = 0; | |
| 101 for (const auto& refptr : *items) { | |
|
pwnall
2016/09/26 13:18:07
refptr -> shareable_blob_item_reference?
dmurph
2016/09/29 00:44:21
Done.
| |
| 102 const DataElement& element = refptr->item()->data_element(); | |
| 103 DCHECK_EQ(DataElement::TYPE_BYTES, element.type()); | |
| 104 size_t length = base::checked_cast<size_t>(element.length()); | |
| 105 size_t bytes_left = length; | |
| 106 while (bytes_left > 0) { | |
| 107 bytes_written = | |
| 108 file.WriteAtCurrentPos(element.bytes() + (length - bytes_left), | |
| 109 base::saturated_cast<int>(bytes_left)); | |
| 110 if (bytes_written < 0) | |
| 111 break; | |
| 112 DCHECK_LE(static_cast<size_t>(bytes_written), bytes_left); | |
| 113 bytes_left -= bytes_written; | |
| 114 } | |
| 115 if (bytes_written < 0) | |
| 116 break; | |
| 117 } | |
| 118 UMA_HISTOGRAM_BOOLEAN("Storage.Blob.PageFileWriteSuccess", bytes_written > 0); | |
| 119 | |
| 120 // Grab our modification time and create our SharedFileReference to manage the | |
|
pwnall
2016/09/26 13:18:09
Where is the SharedFileReference created?
dmurph
2016/09/29 00:44:22
Ah, this was moved out.
| |
| 121 // lifetime of the file. | |
| 122 File::Info info; | |
| 123 bool success = file.GetInfo(&info); | |
| 124 UMA_HISTOGRAM_BOOLEAN("Storage.Blob.PageFileInfoSuccess", success); | |
| 125 creation_info.error = | |
| 126 bytes_written < 0 || !success ? File::FILE_ERROR_FAILED : File::FILE_OK; | |
| 127 creation_info.last_modified = info.last_modified; | |
| 128 return creation_info; | |
| 129 } | |
| 130 | |
| 131 std::vector<scoped_refptr<ShareableBlobDataItem>> WrapInRefPtrs( | |
| 132 const std::vector<ShareableBlobDataItem*> items_ptrs) { | |
| 133 std::vector<scoped_refptr<ShareableBlobDataItem>> result; | |
| 134 for (ShareableBlobDataItem* item : items_ptrs) { | |
| 135 result.push_back(make_scoped_refptr(item)); | |
| 136 } | |
| 137 return result; | |
| 138 } | |
| 139 | |
| 140 void GetFileSizesAndTotalSize( | |
|
pwnall
2016/09/26 13:18:09
Would it make sense turn total_size_output into th
dmurph
2016/09/29 00:44:22
Sure.
| |
| 141 const std::vector<ShareableBlobDataItem*>& unreserved_file_items, | |
| 142 std::vector<uint64_t>* file_sizes_output, | |
| 143 uint64_t* total_size_output) { | |
| 144 *total_size_output = 0; | |
| 145 base::SmallMap<std::map<uint64_t, uint64_t>> file_sizes; | |
| 146 for (ShareableBlobDataItem* item : unreserved_file_items) { | |
| 147 const DataElement& element = item->item()->data_element(); | |
| 148 uint64_t file_id = BlobDataBuilder::GetFutureFileID(element); | |
| 149 auto it = file_sizes.find(file_id); | |
| 150 if (it != file_sizes.end()) { | |
| 151 it->second = std::max(it->second, element.offset() + element.length()); | |
| 152 } else { | |
| 153 file_sizes[file_id] = element.offset() + element.length(); | |
| 154 } | |
| 155 *total_size_output += element.length(); | |
| 156 } | |
| 157 for (const auto& size_pair : file_sizes) { | |
| 158 file_sizes_output->push_back(size_pair.second); | |
| 159 } | |
| 160 } | |
| 161 | |
| 162 } // namespace | |
| 163 | |
| 164 BlobMemoryController::QuotaAllocationTask::~QuotaAllocationTask() {} | |
| 165 | |
| 166 // Both tasks store a list iterator to their current position in the task list, | |
|
pwnall
2016/09/26 13:18:07
This comment seems to re-state the contents of the
dmurph
2016/09/29 00:44:20
Done.
| |
| 167 // and the total size of their allocation. | |
| 168 template <typename T> | |
| 169 class BlobMemoryController::BaseQuotaAllocationTask | |
| 170 : public BlobMemoryController::QuotaAllocationTask { | |
| 171 public: | |
| 172 BaseQuotaAllocationTask() : allocation_size_(0) {} | |
|
pwnall
2016/09/26 13:18:09
Do you need the default constructor to be able to
dmurph
2016/09/29 00:44:20
No, we need it for FileQuotaAllocationTask, where
| |
| 173 explicit BaseQuotaAllocationTask(uint64_t allocation_size) | |
| 174 : allocation_size_(allocation_size) {} | |
| 175 | |
| 176 ~BaseQuotaAllocationTask() override {} | |
|
pwnall
2016/09/26 13:18:08
would = default work here?
dmurph
2016/09/29 00:44:21
Done.
| |
| 177 | |
| 178 void set_my_list_position(typename T::iterator current_position) { | |
|
pwnall
2016/09/26 13:18:08
The argument name seems odd, given everything else
dmurph
2016/09/29 00:44:22
Done.
| |
| 179 my_list_position_ = current_position; | |
| 180 } | |
| 181 typename T::iterator my_list_position() { return my_list_position_; } | |
| 182 | |
| 183 uint64_t allocation_size() { return allocation_size_; } | |
| 184 void set_allocation_size(uint64_t allocation_size) { | |
| 185 allocation_size_ = allocation_size; | |
| 186 } | |
| 187 | |
| 188 private: | |
| 189 uint64_t allocation_size_; | |
| 190 typename T::iterator my_list_position_; | |
| 191 }; | |
| 192 | |
| 193 class BlobMemoryController::MemoryQuotaAllocationTask | |
| 194 : public BlobMemoryController::BaseQuotaAllocationTask< | |
| 195 PendingMemoryQuotaTaskList> { | |
| 196 public: | |
| 197 MemoryQuotaAllocationTask( | |
| 198 BlobMemoryController* controller, | |
| 199 uint64_t quota_request_size, | |
| 200 std::vector<scoped_refptr<ShareableBlobDataItem>> pending_items, | |
| 201 MemoryQuotaRequestCallback done_callback) | |
| 202 : BaseQuotaAllocationTask(quota_request_size), | |
| 203 controller_(controller), | |
| 204 pending_items_(std::move(pending_items)), | |
| 205 done_callback_(done_callback), | |
| 206 ptr_factory_(this) {} | |
| 207 | |
| 208 ~MemoryQuotaAllocationTask() override {} | |
| 209 | |
| 210 void RunSuccessCallback(bool success) const { | |
|
pwnall
2016/09/26 13:18:08
RunDoneCallback? RunCallback?
dmurph
2016/09/29 00:44:21
Done.
| |
| 211 if (success) { | |
| 212 for (const auto& item : pending_items_) { | |
| 213 item->set_state(ShareableBlobDataItem::QUOTA_GRANTED); | |
| 214 } | |
| 215 } | |
| 216 done_callback_.Run(success); | |
|
pwnall
2016/09/26 13:18:08
Does the task not need to remove itself from the p
dmurph
2016/09/29 00:44:21
No, we do that externally as it'll invalidate the
| |
| 217 } | |
| 218 | |
| 219 void Cancel() override { | |
|
pwnall
2016/09/26 13:18:08
Is it worth DCHECK-ing that Cancel() is called on
michaeln
2016/09/28 01:08:03
i' rather see so very little done on background th
dmurph
2016/09/29 00:44:22
I'll put a comment about IO thread expectations in
| |
| 220 controller_->pending_memory_quota_total_size_ -= allocation_size(); | |
| 221 // This call destorys this object. | |
|
pwnall
2016/09/26 13:18:09
typo destorys -> destroys
dmurph
2016/09/29 00:44:22
Done.
| |
| 222 controller_->pending_memory_quota_tasks_.erase(my_list_position()); | |
| 223 } | |
| 224 | |
| 225 BlobMemoryController* controller_; | |
| 226 std::vector<scoped_refptr<ShareableBlobDataItem>> pending_items_; | |
| 227 MemoryQuotaRequestCallback done_callback_; | |
| 228 | |
| 229 base::WeakPtrFactory<MemoryQuotaAllocationTask> ptr_factory_; | |
| 230 DISALLOW_COPY_AND_ASSIGN(MemoryQuotaAllocationTask); | |
| 231 }; | |
| 232 | |
| 233 // This task | |
|
pwnall
2016/09/26 13:18:09
Eh?
dmurph
2016/09/29 00:44:21
Done.
| |
| 234 class BlobMemoryController::FileQuotaAllocationTask | |
| 235 : public BlobMemoryController::BaseQuotaAllocationTask< | |
| 236 PendingFileQuotaTaskList> { | |
| 237 public: | |
| 238 FileQuotaAllocationTask( | |
| 239 BlobMemoryController* memory_controller, | |
| 240 std::vector<ShareableBlobDataItem*> unreserved_file_items, | |
| 241 const FileQuotaRequestCallback& done_callback) | |
| 242 : controller_(memory_controller), | |
| 243 done_callback_(done_callback), | |
| 244 ptr_factory_(this) { | |
| 245 // Get the file sizes and total size. | |
| 246 uint64_t total_size = 0; | |
| 247 GetFileSizesAndTotalSize(unreserved_file_items, &file_sizes_, &total_size); | |
| 248 DCHECK_LE(total_size, controller_->GetAvailableFileSpaceForBlobs()); | |
| 249 set_allocation_size(total_size); | |
| 250 | |
| 251 // Check our state and set our item states. | |
| 252 for (ShareableBlobDataItem* item : unreserved_file_items) { | |
| 253 DCHECK_EQ(ShareableBlobDataItem::QUOTA_NEEDED, item->state()); | |
| 254 DCHECK_EQ(DataElement::TYPE_FILE, item->item()->type()); | |
| 255 item->set_state(ShareableBlobDataItem::QUOTA_REQUESTED); | |
| 256 pending_items_.push_back(make_scoped_refptr(item)); | |
| 257 } | |
| 258 | |
| 259 // Increment disk usage and create our file references. | |
| 260 controller_->disk_used_ += total_size; | |
| 261 std::vector<scoped_refptr<ShareableFileReference>> file_refs; | |
| 262 for (size_t i = 0; i < file_sizes_.size(); i++) { | |
| 263 std::string file_name = | |
|
pwnall
2016/09/26 13:18:08
I think it'd be nice to have the filename vending
dmurph
2016/09/29 00:44:21
Sounds good. I can't quite do the checks, but I ca
| |
| 264 base::Uint64ToString(controller_->current_file_num_++); | |
| 265 file_refs.push_back(ShareableFileReference::GetOrCreate( | |
| 266 controller_->blob_storage_dir_.Append(file_name), | |
| 267 ShareableFileReference::DELETE_ON_FINAL_RELEASE, | |
| 268 controller_->file_runner_.get())); | |
| 269 } | |
| 270 | |
| 271 // Send file creation task to file thread. | |
| 272 base::PostTaskAndReplyWithResult( | |
| 273 controller_->file_runner_.get(), FROM_HERE, | |
| 274 base::Bind(&CreateFiles, base::Passed(&file_refs)), | |
| 275 base::Bind(&FileQuotaAllocationTask::OnCreateFiles, | |
| 276 ptr_factory_.GetWeakPtr())); | |
| 277 controller_->RecordTracingCounters(); | |
| 278 } | |
| 279 ~FileQuotaAllocationTask() override {} | |
| 280 | |
| 281 void RunSuccessCallback(bool success, | |
|
pwnall
2016/09/26 13:18:08
RunDoneCallback? RunCallback?
dmurph
2016/09/29 00:44:21
Done.
| |
| 282 std::vector<FileCreationInfo> file_info) const { | |
| 283 done_callback_.Run(success, std::move(file_info)); | |
| 284 } | |
| 285 | |
| 286 void Cancel() override { | |
| 287 controller_->disk_used_ -= allocation_size(); | |
| 288 // This call destorys this object. | |
|
pwnall
2016/09/26 13:18:07
typo: "destorys"
dmurph
2016/09/29 00:44:21
Done.
| |
| 289 controller_->pending_file_quota_tasks_.erase(my_list_position()); | |
|
pwnall
2016/09/26 13:18:07
For me, the tricky part here was that destroying t
dmurph
2016/09/29 00:44:20
Yes, this is why I documented that calling 'Cancel
| |
| 290 } | |
| 291 | |
| 292 void OnCreateFiles(std::vector<FileCreationInfo> result) { | |
|
pwnall
2016/09/26 13:18:09
You don't seem to be removing the task from pendin
dmurph
2016/09/29 00:44:21
Good point about sequencing. Fixed.
| |
| 293 if (result.empty()) { | |
| 294 for (uint64_t size : file_sizes_) { | |
| 295 controller_->disk_used_ -= size; | |
| 296 } | |
| 297 done_callback_.Run(false, std::vector<FileCreationInfo>()); | |
|
pwnall
2016/09/26 13:18:08
It seems odd that you have a method wrapping runni
dmurph
2016/09/29 00:44:20
removed.
| |
| 298 controller_->DisableFilePaging(); | |
| 299 return; | |
| 300 } | |
| 301 DCHECK_EQ(file_sizes_.size(), result.size()); | |
| 302 for (size_t i = 0; i < result.size(); i++) { | |
| 303 result[i].file_reference->AddFinalReleaseCallback( | |
| 304 base::Bind(&BlobMemoryController::OnBlobFileDelete, | |
| 305 controller_->ptr_factory_.GetWeakPtr(), file_sizes_[i])); | |
| 306 } | |
| 307 for (const auto& item : pending_items_) { | |
| 308 item->set_state(ShareableBlobDataItem::QUOTA_GRANTED); | |
| 309 } | |
| 310 done_callback_.Run(true, std::move(result)); | |
|
pwnall
2016/09/26 13:18:09
Again, seems odd that you're not using RunSuccessC
dmurph
2016/09/29 00:44:21
Done.
| |
| 311 } | |
| 312 | |
| 313 BlobMemoryController* controller_; | |
| 314 std::vector<uint64_t> file_sizes_; | |
| 315 std::vector<scoped_refptr<ShareableBlobDataItem>> pending_items_; | |
| 316 scoped_refptr<base::TaskRunner> file_runner_; | |
| 317 FileQuotaRequestCallback done_callback_; | |
| 318 | |
| 319 base::WeakPtrFactory<FileQuotaAllocationTask> ptr_factory_; | |
| 320 DISALLOW_COPY_AND_ASSIGN(FileQuotaAllocationTask); | |
| 321 }; | |
| 322 | |
| 323 FileCreationInfo::FileCreationInfo() = default; | |
| 324 FileCreationInfo::~FileCreationInfo() = default; | |
| 325 FileCreationInfo::FileCreationInfo(FileCreationInfo&&) = default; | |
| 326 FileCreationInfo& FileCreationInfo::operator=(FileCreationInfo&&) = default; | |
| 327 | |
| 328 BlobMemoryController::BlobMemoryController() | |
| 329 : recent_item_cache_( | |
| 330 base::MRUCache<uint64_t, ShareableBlobDataItem*>::NO_AUTO_EVICT), | |
| 331 ptr_factory_(this) {} | |
| 332 | |
| 333 BlobMemoryController::~BlobMemoryController() {} | |
| 334 | |
| 335 void BlobMemoryController::EnableFilePaging( | |
| 336 const base::FilePath& storage_directory, | |
| 337 scoped_refptr<base::TaskRunner> file_runner) { | |
| 338 DCHECK(!storage_directory.empty()); | |
|
pwnall
2016/09/26 13:18:07
Should this also DCHECK that file paging was disab
dmurph
2016/09/29 00:44:22
Done.
| |
| 339 file_runner_ = std::move(file_runner); | |
| 340 blob_storage_dir_ = storage_directory; | |
| 341 file_paging_enabled_ = true; | |
| 342 } | |
| 343 | |
| 344 void BlobMemoryController::DisableFilePaging() { | |
| 345 file_paging_enabled_ = false; | |
| 346 blob_memory_used_ += in_flight_memory_used_; | |
| 347 in_flight_memory_used_ = 0; | |
| 348 items_saving_to_disk_.clear(); | |
| 349 for (const auto& memory_request : pending_memory_quota_tasks_) { | |
| 350 memory_request->RunSuccessCallback(false); | |
|
michaeln
2016/09/28 01:08:03
you might want to swap the queues into local varia
dmurph
2016/09/29 00:44:21
Done.
| |
| 351 } | |
| 352 for (const auto& file_request : pending_file_quota_tasks_) { | |
| 353 DCHECK_GE(disk_used_, file_request->allocation_size()); | |
| 354 disk_used_ -= file_request->allocation_size(); | |
| 355 file_request->RunSuccessCallback(false, std::vector<FileCreationInfo>()); | |
| 356 } | |
|
michaeln
2016/09/28 01:08:03
if there are callbacks in flight from the backgrou
dmurph
2016/09/29 00:44:20
Done.
| |
| 357 pending_pagings_ = 0; | |
| 358 pending_memory_quota_total_size_ = 0; | |
| 359 pending_memory_quota_tasks_.clear(); | |
| 360 pending_file_quota_tasks_.clear(); | |
| 361 recent_item_cache_.Clear(); | |
| 362 recent_item_cache_bytes_ = 0; | |
| 363 file_runner_ = nullptr; | |
| 364 RecordTracingCounters(); | |
| 365 } | |
| 366 | |
| 367 BlobMemoryController::Strategy BlobMemoryController::DetermineStrategy( | |
| 368 size_t preemptive_transported_bytes, | |
| 369 uint64_t total_transportation_bytes) const { | |
| 370 if (total_transportation_bytes == 0) { | |
| 371 return Strategy::NONE_NEEDED; | |
| 372 } | |
| 373 if (!CanReserveQuota(total_transportation_bytes)) { | |
| 374 return Strategy::TOO_LARGE; | |
| 375 } | |
| 376 // Handle the case where we have all the bytes preemptively transported, and | |
| 377 // we can also fit them. | |
| 378 if (preemptive_transported_bytes == total_transportation_bytes && | |
| 379 pending_memory_quota_tasks_.empty() && | |
| 380 preemptive_transported_bytes < GetAvailableMemoryForBlobs()) { | |
| 381 return Strategy::NONE_NEEDED; | |
| 382 } | |
| 383 if (file_paging_enabled_ && | |
| 384 (total_transportation_bytes > limits_.max_blob_in_memory_space)) { | |
| 385 return Strategy::FILE; | |
| 386 } | |
| 387 if (total_transportation_bytes > limits_.max_ipc_memory_size) { | |
| 388 return Strategy::SHARED_MEMORY; | |
| 389 } | |
| 390 return Strategy::IPC; | |
|
michaeln
2016/09/28 01:08:04
short and sweet, thnx for neutering the comments :
| |
| 391 } | |
| 392 | |
| 393 bool BlobMemoryController::CanReserveQuota(uint64_t size) const { | |
| 394 // We check each size independently as a blob can't be constructed in both | |
| 395 // disk and memory. | |
| 396 return size <= GetAvailableMemoryForBlobs() || | |
| 397 size <= GetAvailableFileSpaceForBlobs(); | |
| 398 } | |
| 399 | |
| 400 base::WeakPtr<QuotaAllocationTask> BlobMemoryController::ReserveMemoryQuota( | |
| 401 std::vector<ShareableBlobDataItem*> unreserved_memory_items, | |
| 402 const MemoryQuotaRequestCallback& done_callback) { | |
| 403 uint64_t total_bytes_needed = 0; | |
| 404 for (ShareableBlobDataItem* item : unreserved_memory_items) { | |
| 405 DCHECK_EQ(ShareableBlobDataItem::QUOTA_NEEDED, item->state()); | |
| 406 DCHECK(item->item()->type() == DataElement::TYPE_BYTES_DESCRIPTION || | |
| 407 item->item()->type() == DataElement::TYPE_BYTES); | |
|
michaeln
2016/09/28 01:08:04
would DCHECK(length() > 0) be correct here, otherw
dmurph
2016/09/29 00:44:21
Not possible, but good to DCHECK everywhere just i
| |
| 408 total_bytes_needed += item->item()->length(); | |
| 409 item->set_state(ShareableBlobDataItem::QUOTA_REQUESTED); | |
| 410 } | |
| 411 | |
| 412 if (total_bytes_needed == 0) { | |
| 413 done_callback.Run(true); | |
| 414 return base::WeakPtr<QuotaAllocationTask>(); | |
| 415 } | |
| 416 | |
| 417 if (!file_paging_enabled_) { | |
|
pwnall
2016/09/26 13:18:09
It seems like the code in 418-423 mostly duplicate
dmurph
2016/09/29 00:44:21
Nice idea, done.
| |
| 418 blob_memory_used_ += total_bytes_needed; | |
| 419 for (ShareableBlobDataItem* item : unreserved_memory_items) { | |
| 420 item->set_state(ShareableBlobDataItem::QUOTA_GRANTED); | |
| 421 } | |
| 422 done_callback.Run(true); | |
| 423 return base::WeakPtr<QuotaAllocationTask>(); | |
| 424 } | |
| 425 | |
| 426 // If we're currently waiting for blobs to page already, then we add | |
| 427 // ourselves to the end of the queue. Once paging is complete, we'll schedule | |
| 428 // more paging for any more pending blobs. | |
| 429 if (!pending_memory_quota_tasks_.empty()) { | |
| 430 std::vector<scoped_refptr<ShareableBlobDataItem>> item_handles = | |
| 431 WrapInRefPtrs(unreserved_memory_items); | |
| 432 | |
| 433 pending_memory_quota_total_size_ += total_bytes_needed; | |
| 434 pending_memory_quota_tasks_.push_back( | |
| 435 base::MakeUnique<MemoryQuotaAllocationTask>(this, total_bytes_needed, | |
| 436 std::move(item_handles), | |
| 437 done_callback)); | |
| 438 pending_memory_quota_tasks_.back()->set_my_list_position( | |
| 439 --pending_memory_quota_tasks_.end()); | |
| 440 | |
| 441 return pending_memory_quota_tasks_.back()->ptr_factory_.GetWeakPtr(); | |
| 442 } | |
| 443 | |
| 444 // Store right away if we can. | |
| 445 if (total_bytes_needed <= GetAvailableMemoryForBlobs()) { | |
| 446 // If we're past our blob memory limit, then schedule our paging. | |
| 447 blob_memory_used_ += total_bytes_needed; | |
| 448 for (ShareableBlobDataItem* item : unreserved_memory_items) { | |
| 449 item->set_state(ShareableBlobDataItem::QUOTA_GRANTED); | |
| 450 } | |
| 451 MaybeSchedulePagingUntilSystemHealthy(); | |
| 452 done_callback.Run(true); | |
| 453 return base::WeakPtr<QuotaAllocationTask>(); | |
| 454 } | |
| 455 | |
| 456 // This means we're too big for memory. | |
|
pwnall
2016/09/26 13:18:07
"This means" appears to be redundant here.
dmurph
2016/09/29 00:44:22
Done.
| |
| 457 DCHECK(pending_memory_quota_tasks_.empty()); | |
| 458 DCHECK_EQ(0u, pending_memory_quota_total_size_); | |
| 459 std::vector<scoped_refptr<ShareableBlobDataItem>> item_handles = | |
| 460 WrapInRefPtrs(unreserved_memory_items); | |
| 461 | |
| 462 pending_memory_quota_total_size_ = total_bytes_needed; | |
| 463 pending_memory_quota_tasks_.push_back( | |
| 464 base::MakeUnique<MemoryQuotaAllocationTask>( | |
| 465 this, total_bytes_needed, std::move(item_handles), done_callback)); | |
| 466 pending_memory_quota_tasks_.back()->set_my_list_position( | |
| 467 --pending_memory_quota_tasks_.end()); | |
| 468 | |
| 469 auto weak_ptr = pending_memory_quota_tasks_.back()->ptr_factory_.GetWeakPtr(); | |
| 470 MaybeSchedulePagingUntilSystemHealthy(); | |
| 471 return weak_ptr; | |
| 472 } | |
| 473 | |
| 474 base::WeakPtr<QuotaAllocationTask> BlobMemoryController::ReserveFileQuota( | |
| 475 std::vector<ShareableBlobDataItem*> unreserved_file_items, | |
| 476 const FileQuotaRequestCallback& done_callback) { | |
| 477 pending_file_quota_tasks_.push_back(base::MakeUnique<FileQuotaAllocationTask>( | |
| 478 this, std::move(unreserved_file_items), done_callback)); | |
| 479 pending_file_quota_tasks_.back()->set_my_list_position( | |
| 480 --pending_file_quota_tasks_.end()); | |
| 481 return pending_file_quota_tasks_.back()->ptr_factory_.GetWeakPtr(); | |
| 482 } | |
| 483 | |
| 484 void BlobMemoryController::MaybeFreeMemoryQuotaForItems( | |
| 485 const std::vector<scoped_refptr<ShareableBlobDataItem>>& items) { | |
| 486 uint64_t memory_to_be_freed = 0; | |
| 487 std::unordered_set<uint64_t> visited_items; | |
| 488 for (const scoped_refptr<ShareableBlobDataItem>& item : items) { | |
| 489 switch (item->state()) { | |
| 490 case ShareableBlobDataItem::QUOTA_NEEDED: | |
| 491 case ShareableBlobDataItem::QUOTA_REQUESTED: | |
| 492 case ShareableBlobDataItem::POPULATED_WITHOUT_QUOTA: | |
| 493 continue; | |
| 494 case ShareableBlobDataItem::QUOTA_GRANTED: | |
| 495 case ShareableBlobDataItem::POPULATED_WITH_QUOTA: | |
| 496 break; | |
| 497 } | |
| 498 // We only care about bytes items that don't have blobs referencing them, | |
| 499 // and we remove duplicates. | |
| 500 DataElement::Type type = item->item()->type(); | |
| 501 if (!item->referencing_blobs().empty() || | |
| 502 (type != DataElement::TYPE_BYTES && | |
| 503 type != DataElement::TYPE_BYTES_DESCRIPTION) || | |
| 504 base::ContainsKey(visited_items, item->item_id())) | |
| 505 continue; | |
| 506 visited_items.insert(item->item_id()); | |
| 507 memory_to_be_freed += item->item()->length(); | |
| 508 } | |
| 509 if (memory_to_be_freed != 0) { | |
| 510 DCHECK_GE(blob_memory_used_, memory_to_be_freed); | |
| 511 blob_memory_used_ -= memory_to_be_freed; | |
| 512 MaybeGrantPendingQuotaRequests(); | |
| 513 } | |
| 514 } | |
| 515 | |
| 516 void BlobMemoryController::ForceFreeMemoryQuotaForItem( | |
| 517 const scoped_refptr<ShareableBlobDataItem>& item) { | |
| 518 CHECK_EQ(item->state(), ShareableBlobDataItem::QUOTA_GRANTED); | |
| 519 CHECK_EQ(item->item()->type(), DataElement::TYPE_BYTES_DESCRIPTION); | |
| 520 blob_memory_used_ -= item->item()->length(); | |
| 521 MaybeGrantPendingQuotaRequests(); | |
| 522 } | |
| 523 | |
| 524 void BlobMemoryController::UpdateBlobItemInRecents( | |
|
michaeln
2016/09/28 01:08:04
maybe rename the method to make it more clear the
dmurph
2016/09/29 00:44:21
Good idea, thanks.
| |
| 525 ShareableBlobDataItem* item) { | |
| 526 DCHECK_EQ(DataElement::TYPE_BYTES, item->item()->type()); | |
| 527 DCHECK_EQ(ShareableBlobDataItem::POPULATED_WITH_QUOTA, item->state()); | |
| 528 // We don't want to re-add the item if we're currently paging it to disk. | |
| 529 if (items_saving_to_disk_.find(item->item_id()) != | |
| 530 items_saving_to_disk_.end()) | |
| 531 return; | |
| 532 auto iterator = recent_item_cache_.Get(item->item_id()); | |
| 533 if (iterator == recent_item_cache_.end()) { | |
| 534 recent_item_cache_bytes_ += static_cast<size_t>(item->item()->length()); | |
| 535 recent_item_cache_.Put(item->item_id(), item); | |
| 536 MaybeSchedulePagingUntilSystemHealthy(); | |
| 537 } | |
| 538 } | |
| 539 | |
| 540 void BlobMemoryController::RemoveBlobItemInRecents( | |
| 541 const ShareableBlobDataItem& item) { | |
| 542 auto iterator = recent_item_cache_.Get(item.item_id()); | |
| 543 if (iterator != recent_item_cache_.end()) { | |
| 544 size_t size = static_cast<size_t>(item.item()->length()); | |
| 545 DCHECK_GE(recent_item_cache_bytes_, size); | |
| 546 recent_item_cache_bytes_ -= size; | |
| 547 recent_item_cache_.Erase(iterator); | |
| 548 } | |
| 549 } | |
| 550 | |
| 551 void BlobMemoryController::MaybeGrantPendingQuotaRequests() { | |
| 552 while (!pending_memory_quota_tasks_.empty() && | |
| 553 limits_.max_blob_in_memory_space - blob_memory_used_ >= | |
| 554 pending_memory_quota_tasks_.front()->allocation_size()) { | |
| 555 std::unique_ptr<MemoryQuotaAllocationTask> memory_task = | |
| 556 std::move(pending_memory_quota_tasks_.front()); | |
| 557 pending_memory_quota_tasks_.pop_front(); | |
| 558 size_t request_size = memory_task->allocation_size(); | |
| 559 pending_memory_quota_total_size_ -= request_size; | |
| 560 blob_memory_used_ += request_size; | |
| 561 memory_task->RunSuccessCallback(true); | |
| 562 } | |
| 563 RecordTracingCounters(); | |
| 564 } | |
| 565 | |
| 566 void BlobMemoryController::MaybeSchedulePagingUntilSystemHealthy() { | |
| 567 // Don't do paging when others are happening, as we don't change our | |
|
pwnall
2016/09/26 13:18:09
I like eviction more than paging here, both in the
dmurph
2016/09/29 00:44:22
Done.
| |
| 568 // blobs_waiting_for_paging_size_ value until after the paging files have | |
| 569 // been written. | |
| 570 if (pending_pagings_ != 0 || !file_paging_enabled_) | |
| 571 return; | |
| 572 | |
| 573 // We try to page items to disk until our current system size + requested | |
| 574 // memory is below our size limit. | |
| 575 while (pending_memory_quota_total_size_ + blob_memory_used_ > | |
| 576 limits_.max_blob_in_memory_space) { | |
| 577 // We only page when we have enough items to fill a while page file. | |
|
pwnall
2016/09/26 13:18:07
while -> whole
dmurph
2016/09/29 00:44:21
Done.
| |
| 578 if (recent_item_cache_bytes_ < limits_.min_page_file_size) | |
| 579 break; | |
| 580 DCHECK_LE(limits_.min_page_file_size, | |
| 581 static_cast<uint64_t>(blob_memory_used_)); | |
| 582 size_t total_items_size = 0; | |
| 583 std::unique_ptr<std::vector<scoped_refptr<ShareableBlobDataItem>>> | |
| 584 items_for_disk(new std::vector<scoped_refptr<ShareableBlobDataItem>>()); | |
| 585 // Collect our items. | |
|
pwnall
2016/09/26 13:18:09
I think you could extract a method from lines 582-
dmurph
2016/09/29 00:44:20
Done.
| |
| 586 while (total_items_size < limits_.min_page_file_size && | |
| 587 !recent_item_cache_.empty()) { | |
| 588 auto iterator = --recent_item_cache_.end(); | |
| 589 ShareableBlobDataItem* item = iterator->second; | |
| 590 DCHECK(item); | |
| 591 DCHECK_EQ(item->item()->type(), DataElement::TYPE_BYTES); | |
| 592 recent_item_cache_.Erase(iterator); | |
| 593 recent_item_cache_bytes_ -= static_cast<size_t>(item->item()->length()); | |
| 594 items_saving_to_disk_.insert(item->item_id()); | |
| 595 size_t size = base::checked_cast<size_t>(item->item()->length()); | |
| 596 total_items_size += size; | |
| 597 items_for_disk->push_back(make_scoped_refptr(item)); | |
| 598 } | |
| 599 if (total_items_size == 0) | |
| 600 break; | |
| 601 | |
| 602 // Update our bookkeeping. | |
| 603 pending_pagings_++; | |
| 604 disk_used_ += total_items_size; | |
| 605 DCHECK_GE(blob_memory_used_, total_items_size); | |
| 606 blob_memory_used_ -= total_items_size; | |
| 607 in_flight_memory_used_ += total_items_size; | |
| 608 std::string file_name = base::Uint64ToString(current_file_num_++); | |
| 609 // Create our file reference. | |
| 610 scoped_refptr<ShareableFileReference> file_ref = | |
| 611 ShareableFileReference::GetOrCreate( | |
| 612 blob_storage_dir_.Append(file_name), | |
| 613 ShareableFileReference::DELETE_ON_FINAL_RELEASE, | |
| 614 file_runner_.get()); | |
| 615 // Add the release callback so we decrement our disk usage on file deletion. | |
| 616 file_ref->AddFinalReleaseCallback( | |
| 617 base::Bind(&BlobMemoryController::OnBlobFileDelete, | |
| 618 ptr_factory_.GetWeakPtr(), total_items_size)); | |
| 619 // Post the file writing task. | |
| 620 base::PostTaskAndReplyWithResult( | |
| 621 file_runner_.get(), FROM_HERE, | |
| 622 base::Bind(&WriteItemsToFile, items_for_disk.get(), total_items_size, | |
| 623 base::Passed(&file_ref)), | |
| 624 base::Bind(&BlobMemoryController::OnPagingComplete, | |
| 625 ptr_factory_.GetWeakPtr(), base::Passed(&items_for_disk), | |
| 626 total_items_size)); | |
| 627 } | |
| 628 RecordTracingCounters(); | |
| 629 } | |
| 630 | |
| 631 void BlobMemoryController::OnPagingComplete( | |
|
pwnall
2016/09/26 13:18:08
Again, how about Paging -> Eviction?
dmurph
2016/09/29 00:44:21
Done.
| |
| 632 std::unique_ptr<std::vector<scoped_refptr<ShareableBlobDataItem>>> items, | |
| 633 size_t total_items_size, | |
| 634 FileCreationInfo result) { | |
| 635 if (!file_paging_enabled_) | |
| 636 return; | |
| 637 if (result.error != File::FILE_OK) { | |
| 638 UMA_HISTOGRAM_ENUMERATION("Storage.Blob.PagingError", -result.error, | |
| 639 -File::FILE_ERROR_MAX); | |
| 640 disk_used_ -= total_items_size; | |
| 641 DisableFilePaging(); | |
| 642 return; | |
| 643 } | |
| 644 DCHECK_LT(0u, pending_pagings_); | |
| 645 pending_pagings_--; | |
| 646 | |
| 647 // Switch from the data backing item to a new file backing item. | |
|
pwnall
2016/09/26 13:18:08
data-backed items and file-backed items?
dmurph
2016/09/29 00:44:21
Done.
| |
| 648 uint64_t offset = 0; | |
| 649 for (const scoped_refptr<ShareableBlobDataItem>& shareable_item : | |
| 650 *items.get()) { | |
| 651 scoped_refptr<BlobDataItem> new_item(new BlobDataItem( | |
| 652 base::WrapUnique(new DataElement()), result.file_reference)); | |
| 653 new_item->data_element_ptr()->SetToFilePathRange( | |
| 654 result.file_reference->path(), offset, shareable_item->item()->length(), | |
| 655 result.last_modified); | |
| 656 *(shareable_item->item_mutable()) = new_item; | |
|
pwnall
2016/09/26 13:18:09
Why is this better than a set_item()?
dmurph
2016/09/29 00:44:22
Done.
| |
| 657 items_saving_to_disk_.erase(shareable_item->item_id()); | |
| 658 offset += shareable_item->item()->length(); | |
| 659 } | |
| 660 in_flight_memory_used_ -= total_items_size; | |
| 661 | |
| 662 // We want callback on blobs up to the amount we've freed. | |
| 663 MaybeGrantPendingQuotaRequests(); | |
| 664 | |
| 665 // If we still have more blobs waiting and we're not waiting on more paging | |
| 666 // operations, schedule more. | |
| 667 MaybeSchedulePagingUntilSystemHealthy(); | |
| 668 } | |
| 669 | |
| 670 void BlobMemoryController::RecordTracingCounters() const { | |
| 671 TRACE_COUNTER2("Blob", "MemoryUsage", "RegularStorage", blob_memory_used_, | |
| 672 "InFlightToDisk", in_flight_memory_used_); | |
| 673 TRACE_COUNTER1("Blob", "DiskUsage", disk_used_); | |
| 674 TRACE_COUNTER1("Blob", "TranfersPendingOnDisk", | |
| 675 pending_memory_quota_tasks_.size()); | |
| 676 TRACE_COUNTER1("Blob", "TranfersBytesPendingOnDisk", | |
| 677 pending_memory_quota_total_size_); | |
| 678 } | |
| 679 | |
| 680 size_t BlobMemoryController::GetAvailableMemoryForBlobs() const { | |
| 681 if (limits_.total_memory_space() < memory_usage()) | |
| 682 return 0; | |
| 683 return limits_.total_memory_space() - memory_usage(); | |
| 684 } | |
| 685 | |
| 686 uint64_t BlobMemoryController::GetAvailableFileSpaceForBlobs() const { | |
| 687 if (!file_paging_enabled_) | |
| 688 return 0; | |
| 689 return limits_.max_blob_disk_space - disk_used_ - | |
| 690 pending_memory_quota_total_size_; | |
| 691 } | |
| 692 | |
| 693 void BlobMemoryController::OnBlobFileDelete(uint64_t size, | |
| 694 const base::FilePath& path) { | |
| 695 DCHECK_LE(size, disk_used_); | |
| 696 disk_used_ -= size; | |
| 697 } | |
| 698 | |
| 699 } // namespace storage | |
| OLD | NEW |