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

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

Issue 2857283002: Add memory pressure listener to Blob storage (Closed)
Patch Set: ratio and pressue arg. 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 #include "storage/browser/blob/blob_memory_controller.h" 5 #include "storage/browser/blob/blob_memory_controller.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/files/file_util.h" 8 #include "base/files/file_util.h"
9 #include "base/files/scoped_temp_dir.h" 9 #include "base/files/scoped_temp_dir.h"
10 #include "base/message_loop/message_loop.h" 10 #include "base/message_loop/message_loop.h"
(...skipping 15 matching lines...) Expand all
26 using base::TestSimpleTaskRunner; 26 using base::TestSimpleTaskRunner;
27 using ItemState = ShareableBlobDataItem::State; 27 using ItemState = ShareableBlobDataItem::State;
28 using QuotaAllocationTask = BlobMemoryController::QuotaAllocationTask; 28 using QuotaAllocationTask = BlobMemoryController::QuotaAllocationTask;
29 29
30 const std::string kBlobStorageDirectory = "blob_storage"; 30 const std::string kBlobStorageDirectory = "blob_storage";
31 const size_t kTestBlobStorageIPCThresholdBytes = 20; 31 const size_t kTestBlobStorageIPCThresholdBytes = 20;
32 const size_t kTestBlobStorageMaxSharedMemoryBytes = 50; 32 const size_t kTestBlobStorageMaxSharedMemoryBytes = 50;
33 const size_t kTestBlobStorageMaxBlobMemorySize = 500; 33 const size_t kTestBlobStorageMaxBlobMemorySize = 500;
34 const uint64_t kTestBlobStorageMaxDiskSpace = 1000; 34 const uint64_t kTestBlobStorageMaxDiskSpace = 1000;
35 const uint64_t kTestBlobStorageMinFileSizeBytes = 10; 35 const uint64_t kTestBlobStorageMinFileSizeBytes = 10;
36 const float kTestBlobStorageMinFileSizeBytesUnderPressure = 0.004;
36 const uint64_t kTestBlobStorageMaxFileSizeBytes = 100; 37 const uint64_t kTestBlobStorageMaxFileSizeBytes = 100;
37 38
38 const uint64_t kTestSmallBlobStorageMaxDiskSpace = 100; 39 const uint64_t kTestSmallBlobStorageMaxDiskSpace = 100;
39 40
40 static int64_t sFakeDiskSpace = 0; 41 static int64_t sFakeDiskSpace = 0;
41 static bool sFakeDiskSpaceCalled = true; 42 static bool sFakeDiskSpaceCalled = true;
42 43
43 int64_t FakeDiskSpaceMethod(const base::FilePath& path) { 44 int64_t FakeDiskSpaceMethod(const base::FilePath& path) {
44 EXPECT_FALSE(sFakeDiskSpaceCalled); 45 EXPECT_FALSE(sFakeDiskSpaceCalled);
45 sFakeDiskSpaceCalled = true; 46 sFakeDiskSpaceCalled = true;
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
84 } 85 }
85 86
86 void SetTestMemoryLimits(BlobMemoryController* controller) { 87 void SetTestMemoryLimits(BlobMemoryController* controller) {
87 BlobStorageLimits limits; 88 BlobStorageLimits limits;
88 limits.max_ipc_memory_size = kTestBlobStorageIPCThresholdBytes; 89 limits.max_ipc_memory_size = kTestBlobStorageIPCThresholdBytes;
89 limits.max_shared_memory_size = kTestBlobStorageMaxSharedMemoryBytes; 90 limits.max_shared_memory_size = kTestBlobStorageMaxSharedMemoryBytes;
90 limits.max_blob_in_memory_space = kTestBlobStorageMaxBlobMemorySize; 91 limits.max_blob_in_memory_space = kTestBlobStorageMaxBlobMemorySize;
91 limits.desired_max_disk_space = kTestBlobStorageMaxDiskSpace; 92 limits.desired_max_disk_space = kTestBlobStorageMaxDiskSpace;
92 limits.effective_max_disk_space = kTestBlobStorageMaxDiskSpace; 93 limits.effective_max_disk_space = kTestBlobStorageMaxDiskSpace;
93 limits.min_page_file_size = kTestBlobStorageMinFileSizeBytes; 94 limits.min_page_file_size = kTestBlobStorageMinFileSizeBytes;
95 limits.min_page_file_size_ratio_under_pressure =
96 kTestBlobStorageMinFileSizeBytesUnderPressure;
94 limits.max_file_size = kTestBlobStorageMaxFileSizeBytes; 97 limits.max_file_size = kTestBlobStorageMaxFileSizeBytes;
95 controller->set_limits_for_testing(limits); 98 controller->set_limits_for_testing(limits);
96 } 99 }
97 100
98 void SetSmallDiskTestMemoryLimits(BlobMemoryController* controller) { 101 void SetSmallDiskTestMemoryLimits(BlobMemoryController* controller) {
99 BlobStorageLimits limits; 102 BlobStorageLimits limits;
100 limits.max_ipc_memory_size = kTestBlobStorageIPCThresholdBytes; 103 limits.max_ipc_memory_size = kTestBlobStorageIPCThresholdBytes;
101 limits.max_shared_memory_size = kTestBlobStorageMaxSharedMemoryBytes; 104 limits.max_shared_memory_size = kTestBlobStorageMaxSharedMemoryBytes;
102 limits.max_blob_in_memory_space = kTestBlobStorageMaxBlobMemorySize; 105 limits.max_blob_in_memory_space = kTestBlobStorageMaxBlobMemorySize;
103 limits.desired_max_disk_space = kTestSmallBlobStorageMaxDiskSpace; 106 limits.desired_max_disk_space = kTestSmallBlobStorageMaxDiskSpace;
104 limits.effective_max_disk_space = kTestSmallBlobStorageMaxDiskSpace; 107 limits.effective_max_disk_space = kTestSmallBlobStorageMaxDiskSpace;
105 limits.min_page_file_size = kTestBlobStorageMinFileSizeBytes; 108 limits.min_page_file_size = kTestBlobStorageMinFileSizeBytes;
109 limits.min_page_file_size_ratio_under_pressure =
110 kTestBlobStorageMinFileSizeBytesUnderPressure;
106 limits.max_file_size = kTestBlobStorageMaxFileSizeBytes; 111 limits.max_file_size = kTestBlobStorageMaxFileSizeBytes;
107 controller->set_limits_for_testing(limits); 112 controller->set_limits_for_testing(limits);
108 } 113 }
109 114
110 void SaveFileCreationInfo(std::vector<FileCreationInfo> info, bool success) { 115 void SaveFileCreationInfo(std::vector<FileCreationInfo> info, bool success) {
111 file_quota_result_ = success; 116 file_quota_result_ = success;
112 if (success) { 117 if (success) {
113 files_created_.swap(info); 118 files_created_.swap(info);
114 } 119 }
115 } 120 }
(...skipping 997 matching lines...) Expand 10 before | Expand all | Expand 10 after
1113 set_disk_space(-1ll); 1118 set_disk_space(-1ll);
1114 1119
1115 RunFileThreadTasks(); 1120 RunFileThreadTasks();
1116 ExpectDiskSpaceCalled(); 1121 ExpectDiskSpaceCalled();
1117 base::RunLoop().RunUntilIdle(); 1122 base::RunLoop().RunUntilIdle();
1118 1123
1119 // Check the effective limit is constrained. 1124 // Check the effective limit is constrained.
1120 EXPECT_FALSE(controller.limits().IsDiskSpaceConstrained()); 1125 EXPECT_FALSE(controller.limits().IsDiskSpaceConstrained());
1121 } 1126 }
1122 1127
1128 TEST_F(BlobMemoryControllerTest, OnMemoryPressure) {
1129 BlobMemoryController controller(temp_dir_.GetPath(), file_runner_);
1130 SetTestMemoryLimits(&controller);
1131 AssertEnoughDiskSpace();
1132
1133 char kData[1];
1134 kData[0] = 'e';
1135
1136 std::vector<scoped_refptr<ShareableBlobDataItem>> small_items;
1137 size_t size_to_load = 2 * kTestBlobStorageMaxBlobMemorySize *
1138 kTestBlobStorageMinFileSizeBytesUnderPressure +
1139 1;
1140 // 2 * kTestBlobStorageMinFileSizeBytesUnderPressure is less than
1141 // 2 * kTestBlobStorageMinFileSizeBytes.
1142 for (size_t i = 0; i < size_to_load; i++) {
1143 BlobDataBuilder builder("fake");
1144 builder.AppendData(kData, 1);
1145 std::vector<scoped_refptr<ShareableBlobDataItem>> items =
1146 CreateSharedDataItems(builder);
1147 base::WeakPtr<QuotaAllocationTask> memory_task =
1148 controller.ReserveMemoryQuota(items, GetMemoryRequestCallback());
1149 EXPECT_FALSE(memory_task);
1150 items[0]->set_state(ItemState::POPULATED_WITH_QUOTA);
1151 small_items.insert(small_items.end(), items.begin(), items.end());
1152 }
1153 controller.NotifyMemoryItemsUsed(small_items);
1154 EXPECT_FALSE(file_runner_->HasPendingTask());
1155 EXPECT_EQ(size_to_load, controller.memory_usage());
1156
1157 controller.OnMemoryPressure(
1158 base::MemoryPressureListener::MemoryPressureLevel::
1159 MEMORY_PRESSURE_LEVEL_CRITICAL);
1160
1161 EXPECT_TRUE(file_runner_->HasPendingTask());
1162 RunFileThreadTasks();
1163
1164 base::RunLoop().RunUntilIdle();
1165
1166 EXPECT_EQ(1u, controller.memory_usage());
1167 EXPECT_EQ(size_to_load - 1, controller.disk_usage());
1168 return;
1169 }
1170
1123 } // namespace storage 1171 } // namespace storage
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698