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

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

Issue 2857283002: Add memory pressure listener to Blob storage (Closed)
Patch Set: set limit in constants. Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef STORAGE_BROWSER_BLOB_BLOB_MEMORY_CONTROLLER_H_ 5 #ifndef STORAGE_BROWSER_BLOB_BLOB_MEMORY_CONTROLLER_H_
6 #define STORAGE_BROWSER_BLOB_BLOB_MEMORY_CONTROLLER_H_ 6 #define STORAGE_BROWSER_BLOB_BLOB_MEMORY_CONTROLLER_H_
7 7
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <list> 10 #include <list>
11 #include <map> 11 #include <map>
12 #include <memory> 12 #include <memory>
13 #include <string> 13 #include <string>
14 #include <unordered_map> 14 #include <unordered_map>
15 #include <unordered_set> 15 #include <unordered_set>
16 #include <utility> 16 #include <utility>
17 #include <vector> 17 #include <vector>
18 18
19 #include "base/callback_forward.h" 19 #include "base/callback_forward.h"
20 #include "base/callback_helpers.h" 20 #include "base/callback_helpers.h"
21 #include "base/containers/mru_cache.h" 21 #include "base/containers/mru_cache.h"
22 #include "base/files/file.h" 22 #include "base/files/file.h"
23 #include "base/files/file_path.h" 23 #include "base/files/file_path.h"
24 #include "base/gtest_prod_util.h"
24 #include "base/macros.h" 25 #include "base/macros.h"
26 #include "base/memory/memory_pressure_listener.h"
25 #include "base/memory/ref_counted.h" 27 #include "base/memory/ref_counted.h"
26 #include "base/memory/weak_ptr.h" 28 #include "base/memory/weak_ptr.h"
27 #include "base/optional.h" 29 #include "base/optional.h"
28 #include "base/time/time.h" 30 #include "base/time/time.h"
29 #include "storage/browser/storage_browser_export.h" 31 #include "storage/browser/storage_browser_export.h"
30 #include "storage/common/blob_storage/blob_storage_constants.h" 32 #include "storage/common/blob_storage/blob_storage_constants.h"
31 33
32 namespace base { 34 namespace base {
33 class TaskRunner; 35 class TaskRunner;
34 } 36 }
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
171 using DiskSpaceFuncPtr = int64_t (*)(const base::FilePath&); 173 using DiskSpaceFuncPtr = int64_t (*)(const base::FilePath&);
172 174
173 void set_testing_disk_space(DiskSpaceFuncPtr disk_space_function) { 175 void set_testing_disk_space(DiskSpaceFuncPtr disk_space_function) {
174 disk_space_function_ = disk_space_function; 176 disk_space_function_ = disk_space_function;
175 } 177 }
176 178
177 private: 179 private:
178 class FileQuotaAllocationTask; 180 class FileQuotaAllocationTask;
179 class MemoryQuotaAllocationTask; 181 class MemoryQuotaAllocationTask;
180 182
183 FRIEND_TEST_ALL_PREFIXES(BlobMemoryControllerTest, OnMemoryPressure);
181 // So this (and only this) class can call CalculateBlobStorageLimits(). 184 // So this (and only this) class can call CalculateBlobStorageLimits().
182 friend class content::ChromeBlobStorageContext; 185 friend class content::ChromeBlobStorageContext;
183 186
184 // Schedules a task on the file runner to calculate blob storage quota limits. 187 // Schedules a task on the file runner to calculate blob storage quota limits.
185 // This should only be called once per storage partition initialization as we 188 // This should only be called once per storage partition initialization as we
186 // emit UMA stats with that expectation. 189 // emit UMA stats with that expectation.
187 void CalculateBlobStorageLimits(); 190 void CalculateBlobStorageLimits();
188 191
189 using PendingMemoryQuotaTaskList = 192 using PendingMemoryQuotaTaskList =
190 std::list<std::unique_ptr<MemoryQuotaAllocationTask>>; 193 std::list<std::unique_ptr<MemoryQuotaAllocationTask>>;
191 using PendingFileQuotaTaskList = 194 using PendingFileQuotaTaskList =
192 std::list<std::unique_ptr<FileQuotaAllocationTask>>; 195 std::list<std::unique_ptr<FileQuotaAllocationTask>>;
193 196
194 void OnStorageLimitsCalculated(BlobStorageLimits limits); 197 void OnStorageLimitsCalculated(BlobStorageLimits limits);
195 198
196 // Adjusts the effective disk usage based on the available space. We try to 199 // Adjusts the effective disk usage based on the available space. We try to
197 // keep at least BlobSorageLimits::min_available_disk_space() free. 200 // keep at least BlobSorageLimits::min_available_disk_space() free.
198 void AdjustDiskUsage(uint64_t avail_disk_space); 201 void AdjustDiskUsage(uint64_t avail_disk_space);
199 202
200 base::WeakPtr<QuotaAllocationTask> AppendMemoryTask( 203 base::WeakPtr<QuotaAllocationTask> AppendMemoryTask(
201 uint64_t total_bytes_needed, 204 uint64_t total_bytes_needed,
202 std::vector<scoped_refptr<ShareableBlobDataItem>> unreserved_memory_items, 205 std::vector<scoped_refptr<ShareableBlobDataItem>> unreserved_memory_items,
203 const MemoryQuotaRequestCallback& done_callback); 206 const MemoryQuotaRequestCallback& done_callback);
204 207
205 void MaybeGrantPendingMemoryRequests(); 208 void MaybeGrantPendingMemoryRequests();
206 209
207 size_t CollectItemsForEviction( 210 size_t CollectItemsForEviction(
208 std::vector<scoped_refptr<ShareableBlobDataItem>>* output); 211 std::vector<scoped_refptr<ShareableBlobDataItem>>* output,
212 uint64_t min_page_file_size);
209 213
210 // Schedule paging until our memory usage is below our memory limit. 214 // Schedule paging until our memory usage is below our memory limit.
211 void MaybeScheduleEvictionUntilSystemHealthy(); 215 void MaybeScheduleEvictionUntilSystemHealthy(bool on_memory_pressure);
dmurph 2017/05/09 21:57:33 Please make the argument MemoryPressureLevel inste
ssid 2017/05/10 00:21:38 Done.
212 216
213 // Called when we've completed evicting a list of items to disk. This is where 217 // Called when we've completed evicting a list of items to disk. This is where
214 // we swap the bytes items for file items, and update our bookkeeping. 218 // we swap the bytes items for file items, and update our bookkeeping.
215 void OnEvictionComplete( 219 void OnEvictionComplete(
216 scoped_refptr<ShareableFileReference> file_reference, 220 scoped_refptr<ShareableFileReference> file_reference,
217 std::vector<scoped_refptr<ShareableBlobDataItem>> items, 221 std::vector<scoped_refptr<ShareableBlobDataItem>> items,
218 size_t total_items_size, 222 size_t total_items_size,
219 std::pair<FileCreationInfo, int64_t /* disk_avail */> result); 223 const char* evict_reason,
224 size_t memory_usage_before_eviction,
225 std::pair<FileCreationInfo, int64_t /* avail_disk */> result);
226
227 void OnMemoryPressure(
228 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level);
220 229
221 size_t GetAvailableMemoryForBlobs() const; 230 size_t GetAvailableMemoryForBlobs() const;
222 uint64_t GetAvailableFileSpaceForBlobs() const; 231 uint64_t GetAvailableFileSpaceForBlobs() const;
223 232
224 void GrantMemoryAllocations( 233 void GrantMemoryAllocations(
225 std::vector<scoped_refptr<ShareableBlobDataItem>>* items, 234 std::vector<scoped_refptr<ShareableBlobDataItem>>* items,
226 size_t total_bytes); 235 size_t total_bytes);
227 void RevokeMemoryAllocation(uint64_t item_id, size_t length); 236 void RevokeMemoryAllocation(uint64_t item_id, size_t length);
228 237
229 // This is registered as a callback for file deletions on the file reference 238 // This is registered as a callback for file deletions on the file reference
(...skipping 28 matching lines...) Expand all
258 PendingMemoryQuotaTaskList pending_memory_quota_tasks_; 267 PendingMemoryQuotaTaskList pending_memory_quota_tasks_;
259 PendingFileQuotaTaskList pending_file_quota_tasks_; 268 PendingFileQuotaTaskList pending_file_quota_tasks_;
260 269
261 int pending_evictions_ = 0; 270 int pending_evictions_ = 0;
262 271
263 bool file_paging_enabled_ = false; 272 bool file_paging_enabled_ = false;
264 base::FilePath blob_storage_dir_; 273 base::FilePath blob_storage_dir_;
265 scoped_refptr<base::TaskRunner> file_runner_; 274 scoped_refptr<base::TaskRunner> file_runner_;
266 // This defaults to calling base::SysInfo::AmountOfFreeDiskSpace. 275 // This defaults to calling base::SysInfo::AmountOfFreeDiskSpace.
267 DiskSpaceFuncPtr disk_space_function_; 276 DiskSpaceFuncPtr disk_space_function_;
277 base::TimeTicks last_eviction_time_;
268 278
269 // Lifetime of the ShareableBlobDataItem objects is handled externally in the 279 // Lifetime of the ShareableBlobDataItem objects is handled externally in the
270 // BlobStorageContext class. 280 // BlobStorageContext class.
271 base::MRUCache<uint64_t, ShareableBlobDataItem*> populated_memory_items_; 281 base::MRUCache<uint64_t, ShareableBlobDataItem*> populated_memory_items_;
272 size_t populated_memory_items_bytes_ = 0; 282 size_t populated_memory_items_bytes_ = 0;
273 // We need to keep track of items currently being paged to disk so that if 283 // We need to keep track of items currently being paged to disk so that if
274 // another blob successfully grabs a ref, we can prevent it from adding the 284 // another blob successfully grabs a ref, we can prevent it from adding the
275 // item to the recent_item_cache_ above. 285 // item to the recent_item_cache_ above.
276 std::unordered_set<uint64_t> items_paging_to_file_; 286 std::unordered_set<uint64_t> items_paging_to_file_;
277 287
288 base::MemoryPressureListener memory_pressure_listener_;
289
278 base::WeakPtrFactory<BlobMemoryController> weak_factory_; 290 base::WeakPtrFactory<BlobMemoryController> weak_factory_;
279 291
280 DISALLOW_COPY_AND_ASSIGN(BlobMemoryController); 292 DISALLOW_COPY_AND_ASSIGN(BlobMemoryController);
281 }; 293 };
282 } // namespace storage 294 } // namespace storage
283 #endif // STORAGE_BROWSER_BLOB_BLOB_MEMORY_CONTROLLER_H_ 295 #endif // STORAGE_BROWSER_BLOB_BLOB_MEMORY_CONTROLLER_H_
OLDNEW
« no previous file with comments | « no previous file | storage/browser/blob/blob_memory_controller.cc » ('j') | storage/browser/blob/blob_memory_controller.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698