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 "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 13 matching lines...) Expand all Loading... |
24 using Strategy = BlobMemoryController::Strategy; | 24 using Strategy = BlobMemoryController::Strategy; |
25 using FileCreationInfo = BlobMemoryController::FileCreationInfo; | 25 using FileCreationInfo = BlobMemoryController::FileCreationInfo; |
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 float kTestMaxBlobInMemorySpaceUnderPressureRatio = 0.004f; |
34 const uint64_t kTestBlobStorageMaxDiskSpace = 1000; | 35 const uint64_t kTestBlobStorageMaxDiskSpace = 1000; |
35 const uint64_t kTestBlobStorageMinFileSizeBytes = 10; | 36 const uint64_t kTestBlobStorageMinFileSizeBytes = 10; |
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) { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 builder.items_[i], ShareableBlobDataItem::QUOTA_NEEDED))); | 82 builder.items_[i], ShareableBlobDataItem::QUOTA_NEEDED))); |
82 } | 83 } |
83 return result; | 84 return result; |
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; |
| 92 limits.max_blob_in_memory_space_under_pressure_ratio = |
| 93 kTestMaxBlobInMemorySpaceUnderPressureRatio; |
91 limits.desired_max_disk_space = kTestBlobStorageMaxDiskSpace; | 94 limits.desired_max_disk_space = kTestBlobStorageMaxDiskSpace; |
92 limits.effective_max_disk_space = kTestBlobStorageMaxDiskSpace; | 95 limits.effective_max_disk_space = kTestBlobStorageMaxDiskSpace; |
93 limits.min_page_file_size = kTestBlobStorageMinFileSizeBytes; | 96 limits.min_page_file_size = kTestBlobStorageMinFileSizeBytes; |
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; |
| 106 limits.max_blob_in_memory_space_under_pressure_ratio = |
| 107 kTestMaxBlobInMemorySpaceUnderPressureRatio; |
103 limits.desired_max_disk_space = kTestSmallBlobStorageMaxDiskSpace; | 108 limits.desired_max_disk_space = kTestSmallBlobStorageMaxDiskSpace; |
104 limits.effective_max_disk_space = kTestSmallBlobStorageMaxDiskSpace; | 109 limits.effective_max_disk_space = kTestSmallBlobStorageMaxDiskSpace; |
105 limits.min_page_file_size = kTestBlobStorageMinFileSizeBytes; | 110 limits.min_page_file_size = kTestBlobStorageMinFileSizeBytes; |
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) { |
(...skipping 1000 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 kTestMaxBlobInMemorySpaceUnderPressureRatio + |
| 1139 1; |
| 1140 for (size_t i = 0; i < size_to_load; i++) { |
| 1141 BlobDataBuilder builder("fake"); |
| 1142 builder.AppendData(kData, 1); |
| 1143 std::vector<scoped_refptr<ShareableBlobDataItem>> items = |
| 1144 CreateSharedDataItems(builder); |
| 1145 base::WeakPtr<QuotaAllocationTask> memory_task = |
| 1146 controller.ReserveMemoryQuota(items, GetMemoryRequestCallback()); |
| 1147 EXPECT_FALSE(memory_task); |
| 1148 items[0]->set_state(ItemState::POPULATED_WITH_QUOTA); |
| 1149 small_items.insert(small_items.end(), items.begin(), items.end()); |
| 1150 } |
| 1151 controller.NotifyMemoryItemsUsed(small_items); |
| 1152 EXPECT_FALSE(file_runner_->HasPendingTask()); |
| 1153 EXPECT_EQ(size_to_load, controller.memory_usage()); |
| 1154 |
| 1155 controller.OnMemoryPressure( |
| 1156 base::MemoryPressureListener::MemoryPressureLevel:: |
| 1157 MEMORY_PRESSURE_LEVEL_CRITICAL); |
| 1158 |
| 1159 EXPECT_TRUE(file_runner_->HasPendingTask()); |
| 1160 RunFileThreadTasks(); |
| 1161 |
| 1162 base::RunLoop().RunUntilIdle(); |
| 1163 |
| 1164 // 2 page files of size |kTestBlobStorageMaxBlobMemorySize * |
| 1165 // kTestMaxBlobInMemorySpaceUnderPressureRatio| should be evicted with 1 byte |
| 1166 // left in-memory. |
| 1167 EXPECT_EQ(1u, controller.memory_usage()); |
| 1168 EXPECT_EQ(size_to_load - 1, controller.disk_usage()); |
| 1169 return; |
| 1170 } |
| 1171 |
1123 } // namespace storage | 1172 } // namespace storage |
OLD | NEW |