| 1 |
// Copyright 2021 gRPC authors. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
#include "src/core/lib/resource_quota/memory_quota.h" |
| 16 |
|
| 17 |
#include <algorithm> |
| 18 |
#include <atomic> |
| 19 |
#include <chrono> |
| 20 |
#include <random> |
| 21 |
#include <thread> |
| 22 |
#include <vector> |
| 23 |
|
| 24 |
#include "gtest/gtest.h" |
| 25 |
|
| 26 |
#include <grpc/slice.h> |
| 27 |
|
| 28 |
#include "src/core/lib/iomgr/exec_ctx.h" |
| 29 |
#include "test/core/resource_quota/call_checker.h" |
| 30 |
#include "test/core/util/test_config.h" |
| 31 |
|
| 32 |
namespace grpc_core { |
| 33 |
namespace testing { |
| 34 |
|
| 35 |
// |
| 36 |
// Helpers |
| 37 |
// |
| 38 |
|
| 39 |
template <size_t kSize> |
| 40 |
struct Sized { |
| 41 |
char blah[kSize]; |
| 42 |
virtual ~Sized() {} |
| 43 |
}; |
| 44 |
|
| 45 |
// |
| 46 |
// MemoryRequestTest |
| 47 |
// |
| 48 |
|
| 49 |
TEST(MemoryRequestTest, ConversionFromSize) { |
| 50 |
MemoryRequest request = 3; |
| 51 |
EXPECT_EQ(request.min(), 3); |
| 52 |
EXPECT_EQ(request.max(), 3); |
| 53 |
} |
| 54 |
|
| 55 |
TEST(MemoryRequestTest, MinMax) { |
| 56 |
MemoryRequest request(3, 7); |
| 57 |
EXPECT_EQ(request.min(), 3); |
| 58 |
EXPECT_EQ(request.max(), 7); |
| 59 |
} |
| 60 |
|
| 61 |
// |
| 62 |
// MemoryQuotaTest |
| 63 |
// |
| 64 |
|
| 65 |
TEST(MemoryQuotaTest, NoOp) { MemoryQuota("foo"); } |
| 66 |
|
| 67 |
TEST(MemoryQuotaTest, CreateAllocatorNoOp) { |
| 68 |
MemoryQuota memory_quota("foo"); |
| 69 |
auto memory_allocator = memory_quota.CreateMemoryAllocator("bar"); |
| 70 |
} |
| 71 |
|
| 72 |
TEST(MemoryQuotaTest, CreateObjectFromAllocator) { |
| 73 |
ExecCtx exec_ctx; |
| 74 |
MemoryQuota memory_quota("foo"); |
| 75 |
auto memory_allocator = memory_quota.CreateMemoryAllocator("bar"); |
| 76 |
auto object = memory_allocator.MakeUnique<Sized<4096>>(); |
| 77 |
} |
| 78 |
|
| 79 |
TEST(MemoryQuotaTest, CreateSomeObjectsAndExpectReclamation) { |
| 80 |
ExecCtx exec_ctx; |
| 81 |
|
| 82 |
MemoryQuota memory_quota("foo"); |
| 83 |
memory_quota.SetSize(4096); |
| 84 |
auto memory_allocator = memory_quota.CreateMemoryOwner("bar"); |
| 85 |
auto object = memory_allocator.MakeUnique<Sized<2048>>(); |
| 86 |
|
| 87 |
auto checker1 = CallChecker::Make(); |
| 88 |
memory_allocator.PostReclaimer( |
| 89 |
ReclamationPass::kDestructive, |
| 90 |
[&object, checker1](absl::optional<ReclamationSweep> sweep) { |
| 91 |
checker1->Called(); |
| 92 |
EXPECT_TRUE(sweep.has_value()); |
| 93 |
object.reset(); |
| 94 |
}); |
| 95 |
auto object2 = memory_allocator.MakeUnique<Sized<2048>>(); |
| 96 |
exec_ctx.Flush(); |
| 97 |
EXPECT_EQ(object.get(), nullptr); |
| 98 |
|
| 99 |
auto checker2 = CallChecker::Make(); |
| 100 |
memory_allocator.PostReclaimer( |
| 101 |
ReclamationPass::kDestructive, |
| 102 |
[&object2, checker2](absl::optional<ReclamationSweep> sweep) { |
| 103 |
checker2->Called(); |
| 104 |
EXPECT_TRUE(sweep.has_value()); |
| 105 |
object2.reset(); |
| 106 |
}); |
| 107 |
auto object3 = memory_allocator.MakeUnique<Sized<2048>>(); |
| 108 |
exec_ctx.Flush(); |
| 109 |
EXPECT_EQ(object2.get(), nullptr); |
| 110 |
} |
| 111 |
|
| 112 |
TEST(MemoryQuotaTest, ReserveRangeNoPressure) { |
| 113 |
MemoryQuota memory_quota("foo"); |
| 114 |
auto memory_allocator = memory_quota.CreateMemoryAllocator("bar"); |
| 115 |
size_t total = 0; |
| 116 |
for (int i = 0; i < 10000; i++) { |
| 117 |
ExecCtx exec_ctx; |
| 118 |
auto n = memory_allocator.Reserve(MemoryRequest(100, 40000)); |
| 119 |
EXPECT_EQ(n, 40000); |
| 120 |
total += n; |
| 121 |
} |
| 122 |
memory_allocator.Release(total); |
| 123 |
} |
| 124 |
|
| 125 |
TEST(MemoryQuotaTest, MakeSlice) { |
| 126 |
MemoryQuota memory_quota("foo"); |
| 127 |
auto memory_allocator = memory_quota.CreateMemoryAllocator("bar"); |
| 128 |
std::vector<grpc_slice> slices; |
| 129 |
for (int i = 1; i < 1000; i++) { |
| 130 |
ExecCtx exec_ctx; |
| 131 |
int min = i; |
| 132 |
int max = 10 * i - 9; |
| 133 |
slices.push_back(memory_allocator.MakeSlice(MemoryRequest(min, max))); |
| 134 |
} |
| 135 |
ExecCtx exec_ctx; |
| 136 |
for (grpc_slice slice : slices) { |
| 137 |
grpc_slice_unref(slice); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
TEST(MemoryQuotaTest, ContainerAllocator) { |
| 142 |
ExecCtx exec_ctx; |
| 143 |
MemoryQuota memory_quota("foo"); |
| 144 |
auto memory_allocator = memory_quota.CreateMemoryAllocator("bar"); |
| 145 |
Vector<int> vec(&memory_allocator); |
| 146 |
for (int i = 0; i < 100000; i++) { |
| 147 |
vec.push_back(i); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
TEST(MemoryQuotaTest, NoBunchingIfIdle) { |
| 152 |
// Ensure that we don't queue up useless reclamations even if there are no |
| 153 |
// memory reclamations needed. |
| 154 |
MemoryQuota memory_quota("foo"); |
| 155 |
std::atomic<size_t> count_reclaimers_called{0}; |
| 156 |
|
| 157 |
for (size_t i = 0; i < 10000; i++) { |
| 158 |
ExecCtx exec_ctx; |
| 159 |
auto memory_owner = memory_quota.CreateMemoryOwner("bar"); |
| 160 |
memory_owner.PostReclaimer( |
| 161 |
ReclamationPass::kDestructive, |
| 162 |
[&count_reclaimers_called](absl::optional<ReclamationSweep> sweep) { |
| 163 |
EXPECT_FALSE(sweep.has_value()); |
| 164 |
count_reclaimers_called.fetch_add(1, std::memory_order_relaxed); |
| 165 |
}); |
| 166 |
auto object = memory_owner.MakeUnique<Sized<2048>>(); |
| 167 |
} |
| 168 |
|
| 169 |
EXPECT_GE(count_reclaimers_called.load(std::memory_order_relaxed), 8000); |
| 170 |
} |
| 171 |
|
| 172 |
} // namespace testing |
| 173 |
|
| 174 |
namespace memory_quota_detail { |
| 175 |
namespace testing { |
| 176 |
|
| 177 |
// |
| 178 |
// PressureControllerTest |
| 179 |
// |
| 180 |
|
| 181 |
TEST(PressureControllerTest, Init) { |
| 182 |
PressureController c{100, 3}; |
| 183 |
EXPECT_EQ(c.Update(-1.0), 0.0); |
| 184 |
EXPECT_EQ(c.Update(1.0), 1.0); |
| 185 |
} |
| 186 |
|
| 187 |
TEST(PressureControllerTest, LowDecays) { |
| 188 |
PressureController c{100, 3}; |
| 189 |
EXPECT_EQ(c.Update(1.0), 1.0); |
| 190 |
double last = 1.0; |
| 191 |
while (last > 1e-30) { |
| 192 |
double x = c.Update(-1.0); |
| 193 |
EXPECT_LE(x, last); |
| 194 |
last = x; |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
// |
| 199 |
// PressureTrackerTest |
| 200 |
// |
| 201 |
|
| 202 |
TEST(PressureTrackerTest, NoOp) { PressureTracker(); } |
| 203 |
|
| 204 |
TEST(PressureTrackerTest, Decays) { |
| 205 |
PressureTracker tracker; |
| 206 |
int cur_ms = 0; |
| 207 |
auto step_time = [&] { |
| 208 |
++cur_ms; |
| 209 |
return Timestamp::ProcessEpoch() + Duration::Seconds(1) + |
| 210 |
Duration::Milliseconds(cur_ms); |
| 211 |
}; |
| 212 |
// At start pressure is zero and we should be reading zero back. |
| 213 |
{ |
| 214 |
ExecCtx exec_ctx; |
| 215 |
exec_ctx.TestOnlySetNow(step_time()); |
| 216 |
EXPECT_EQ(tracker.AddSampleAndGetControlValue(0.0), 0.0); |
| 217 |
} |
| 218 |
// If memory pressure goes to 100% or higher, we should *immediately* snap to |
| 219 |
// reporting 100%. |
| 220 |
{ |
| 221 |
ExecCtx exec_ctx; |
| 222 |
exec_ctx.TestOnlySetNow(step_time()); |
| 223 |
EXPECT_EQ(tracker.AddSampleAndGetControlValue(1.0), 1.0); |
| 224 |
} |
| 225 |
// Once memory pressure reduces, we should *eventually* get back to reporting |
| 226 |
// close to zero, and monotonically decrease. |
| 227 |
const int got_full = cur_ms; |
| 228 |
double last_reported = 1.0; |
| 229 |
while (true) { |
| 230 |
ExecCtx exec_ctx; |
| 231 |
exec_ctx.TestOnlySetNow(step_time()); |
| 232 |
double new_reported = tracker.AddSampleAndGetControlValue(0.0); |
| 233 |
EXPECT_LE(new_reported, last_reported); |
| 234 |
last_reported = new_reported; |
| 235 |
if (new_reported < 0.1) break; |
| 236 |
} |
| 237 |
// Verify the above happened in a somewhat reasonable time. |
| 238 |
ASSERT_LE(cur_ms, got_full + 1000000); |
| 239 |
} |
| 240 |
|
| 241 |
TEST(PressureTrackerTest, ManyThreads) { |
| 242 |
PressureTracker tracker; |
| 243 |
std::vector<std::thread> threads; |
| 244 |
std::atomic<bool> shutdown{false}; |
| 245 |
threads.reserve(10); |
| 246 |
for (int i = 0; i < 10; i++) { |
| 247 |
threads.emplace_back([&tracker, &shutdown] { |
| 248 |
std::random_device rng; |
| 249 |
std::uniform_real_distribution<double> dist(0.0, 1.0); |
| 250 |
while (!shutdown.load(std::memory_order_relaxed)) { |
| 251 |
ExecCtx exec_ctx; |
| 252 |
tracker.AddSampleAndGetControlValue(dist(rng)); |
| 253 |
} |
| 254 |
}); |
| 255 |
} |
| 256 |
std::this_thread::sleep_for(std::chrono::seconds(5)); |
| 257 |
shutdown.store(true, std::memory_order_relaxed); |
| 258 |
for (auto& thread : threads) { |
| 259 |
thread.join(); |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
} // namespace testing |
| 264 |
} // namespace memory_quota_detail |
| 265 |
|
| 266 |
} // namespace grpc_core |
| 267 |
|
| 268 |
int main(int argc, char** argv) { |
| 269 |
grpc::testing::TestEnvironment give_me_a_name(&argc, argv); |
| 270 |
::testing::InitGoogleTest(&argc, argv); |
| 271 |
gpr_log_verbosity_init(); |
| 272 |
return RUN_ALL_TESTS(); |
| 273 |
} |