| 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 <stddef.h> |
| 16 |
|
| 17 |
#include <algorithm> |
| 18 |
#include <atomic> |
| 19 |
#include <chrono> |
| 20 |
#include <initializer_list> |
| 21 |
#include <memory> |
| 22 |
#include <random> |
| 23 |
#include <thread> |
| 24 |
#include <utility> |
| 25 |
#include <vector> |
| 26 |
|
| 27 |
#include "absl/base/thread_annotations.h" |
| 28 |
#include "absl/strings/str_cat.h" |
| 29 |
#include "absl/types/optional.h" |
| 30 |
#include "gtest/gtest.h" |
| 31 |
|
| 32 |
#include <grpc/event_engine/memory_allocator.h> |
| 33 |
#include <grpc/event_engine/memory_request.h> |
| 34 |
#include <grpc/support/log.h> |
| 35 |
|
| 36 |
#include "src/core/lib/gprpp/sync.h" |
| 37 |
#include "src/core/lib/iomgr/exec_ctx.h" |
| 38 |
#include "src/core/lib/resource_quota/memory_quota.h" |
| 39 |
#include "test/core/util/test_config.h" |
| 40 |
|
| 41 |
namespace grpc_core { |
| 42 |
|
| 43 |
namespace { |
| 44 |
class StressTest { |
| 45 |
public: |
| 46 |
// Create a stress test with some size. |
| 47 |
StressTest(size_t num_quotas, size_t num_allocators) { |
| 48 |
for (size_t i = 0; i < num_quotas; ++i) { |
| 49 |
quotas_.emplace_back(absl::StrCat("quota[", i, "]")); |
| 50 |
} |
| 51 |
std::random_device g; |
| 52 |
std::uniform_int_distribution<size_t> dist(0, num_quotas - 1); |
| 53 |
for (size_t i = 0; i < num_allocators; ++i) { |
| 54 |
allocators_.emplace_back(quotas_[dist(g)].CreateMemoryOwner( |
| 55 |
absl::StrCat("allocator[", i, "]"))); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
~StressTest() { |
| 60 |
ExecCtx exec_ctx; |
| 61 |
allocators_.clear(); |
| 62 |
quotas_.clear(); |
| 63 |
} |
| 64 |
|
| 65 |
// Run the thread for some period of time. |
| 66 |
void Run(int seconds) { |
| 67 |
std::vector<std::thread> threads; |
| 68 |
|
| 69 |
// And another few threads constantly resizing quotas. |
| 70 |
threads.reserve(2 + allocators_.size()); |
| 71 |
for (int i = 0; i < 2; i++) threads.push_back(Run(Resizer)); |
| 72 |
|
| 73 |
// For each (allocator, pass), start a thread continuously allocating from |
| 74 |
// that allocator. Whenever the first allocation is made, schedule a |
| 75 |
// reclaimer for that pass. |
| 76 |
for (size_t i = 0; i < allocators_.size(); i++) { |
| 77 |
auto* allocator = &allocators_[i]; |
| 78 |
for (ReclamationPass pass : |
| 79 |
{ReclamationPass::kBenign, ReclamationPass::kIdle, |
| 80 |
ReclamationPass::kDestructive}) { |
| 81 |
threads.push_back(Run([allocator, pass](StatePtr st) mutable { |
| 82 |
if (st->RememberReservation( |
| 83 |
allocator->MakeReservation(st->RandomRequest()))) { |
| 84 |
allocator->PostReclaimer( |
| 85 |
pass, [st](absl::optional<ReclamationSweep> sweep) { |
| 86 |
if (!sweep.has_value()) return; |
| 87 |
st->ForgetReservations(); |
| 88 |
}); |
| 89 |
} |
| 90 |
})); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
// All threads started, wait for the alloted time. |
| 95 |
std::this_thread::sleep_for(std::chrono::seconds(seconds)); |
| 96 |
|
| 97 |
// Toggle the completion bit, and then wait for the threads. |
| 98 |
done_.store(true, std::memory_order_relaxed); |
| 99 |
while (!threads.empty()) { |
| 100 |
threads.back().join(); |
| 101 |
threads.pop_back(); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
private: |
| 106 |
// Per-thread state. |
| 107 |
// Not everything is used on every thread, but it's not terrible having the |
| 108 |
// extra state around and it does simplify things somewhat. |
| 109 |
class State { |
| 110 |
public: |
| 111 |
explicit State(StressTest* test) |
| 112 |
: test_(test), |
| 113 |
quotas_distribution_(0, test_->quotas_.size() - 1), |
| 114 |
allocators_distribution_(0, test_->allocators_.size() - 1), |
| 115 |
size_distribution_(1, 4 * 1024 * 1024), |
| 116 |
quota_size_distribution_(1024 * 1024, size_t(8) * 1024 * 1024 * 1024), |
| 117 |
choose_variable_size_(1, 100) {} |
| 118 |
|
| 119 |
// Choose a random quota, and return an owned pointer to it. |
| 120 |
// Not thread-safe, only callable from the owning thread. |
| 121 |
MemoryQuota* RandomQuota() { |
| 122 |
return &test_->quotas_[quotas_distribution_(g_)]; |
| 123 |
} |
| 124 |
|
| 125 |
// Choose a random allocator, and return a borrowed pointer to it. |
| 126 |
// Not thread-safe, only callable from the owning thread. |
| 127 |
MemoryOwner* RandomAllocator() { |
| 128 |
return &test_->allocators_[allocators_distribution_(g_)]; |
| 129 |
} |
| 130 |
|
| 131 |
// Random memory request size - 1% of allocations are chosen to be variable |
| 132 |
// sized - the rest are fixed (since variable sized create some contention |
| 133 |
// problems between allocator threads of different passes on the same |
| 134 |
// allocator). |
| 135 |
// Not thread-safe, only callable from the owning thread. |
| 136 |
MemoryRequest RandomRequest() { |
| 137 |
size_t a = size_distribution_(g_); |
| 138 |
if (choose_variable_size_(g_) == 1) { |
| 139 |
size_t b = size_distribution_(g_); |
| 140 |
return MemoryRequest(std::min(a, b), std::max(a, b)); |
| 141 |
} |
| 142 |
return MemoryRequest(a); |
| 143 |
} |
| 144 |
|
| 145 |
// Choose a new size for a backing quota. |
| 146 |
// Not thread-safe, only callable from the owning thread. |
| 147 |
size_t RandomQuotaSize() { return quota_size_distribution_(g_); } |
| 148 |
|
| 149 |
// Remember a reservation, return true if it's the first remembered since |
| 150 |
// the last reclamation. |
| 151 |
// Thread-safe. |
| 152 |
bool RememberReservation(MemoryAllocator::Reservation reservation) |
| 153 |
ABSL_LOCKS_EXCLUDED(mu_) { |
| 154 |
MutexLock lock(&mu_); |
| 155 |
bool was_empty = reservations_.empty(); |
| 156 |
reservations_.emplace_back(std::move(reservation)); |
| 157 |
return was_empty; |
| 158 |
} |
| 159 |
|
| 160 |
// Return all reservations made until this moment, so that they can be |
| 161 |
// dropped. |
| 162 |
std::vector<MemoryAllocator::Reservation> ForgetReservations() |
| 163 |
ABSL_LOCKS_EXCLUDED(mu_) { |
| 164 |
MutexLock lock(&mu_); |
| 165 |
return std::move(reservations_); |
| 166 |
} |
| 167 |
|
| 168 |
private: |
| 169 |
// Owning test. |
| 170 |
StressTest* const test_; |
| 171 |
// Random number generator. |
| 172 |
std::mt19937 g_{std::random_device()()}; |
| 173 |
// Distribution to choose a quota. |
| 174 |
std::uniform_int_distribution<size_t> quotas_distribution_; |
| 175 |
// Distribution to choose an allocator. |
| 176 |
std::uniform_int_distribution<size_t> allocators_distribution_; |
| 177 |
// Distribution to choose an allocation size. |
| 178 |
std::uniform_int_distribution<size_t> size_distribution_; |
| 179 |
// Distribution to choose a quota size. |
| 180 |
std::uniform_int_distribution<size_t> quota_size_distribution_; |
| 181 |
// Distribution to choose whether to make a variable-sized allocation. |
| 182 |
std::uniform_int_distribution<size_t> choose_variable_size_; |
| 183 |
|
| 184 |
// Mutex to protect the reservation list. |
| 185 |
Mutex mu_; |
| 186 |
// Reservations remembered by this thread. |
| 187 |
std::vector<MemoryAllocator::Reservation> reservations_ |
| 188 |
ABSL_GUARDED_BY(mu_); |
| 189 |
}; |
| 190 |
// Type alias since we always pass around these shared pointers. |
| 191 |
using StatePtr = std::shared_ptr<State>; |
| 192 |
|
| 193 |
// Choose one allocator, resize it to a randomly chosen size. |
| 194 |
static void Resizer(StatePtr st) { |
| 195 |
auto* quota = st->RandomQuota(); |
| 196 |
size_t size = st->RandomQuotaSize(); |
| 197 |
quota->SetSize(size); |
| 198 |
} |
| 199 |
|
| 200 |
// Create a thread that repeatedly runs a function until the test is done. |
| 201 |
// We create one instance of State that we pass as a StatePtr to said |
| 202 |
// function as the current overall state for this thread. |
| 203 |
// Monitors done_ to see when we should stop. |
| 204 |
// Ensures there's an ExecCtx for each iteration of the loop. |
| 205 |
template <typename Fn> |
| 206 |
std::thread Run(Fn fn) { |
| 207 |
return std::thread([this, fn]() mutable { |
| 208 |
auto state = std::make_shared<State>(this); |
| 209 |
while (!done_.load(std::memory_order_relaxed)) { |
| 210 |
ExecCtx exec_ctx; |
| 211 |
fn(state); |
| 212 |
} |
| 213 |
}); |
| 214 |
} |
| 215 |
|
| 216 |
// Flag for when the test is completed. |
| 217 |
std::atomic<bool> done_{false}; |
| 218 |
|
| 219 |
// Memory quotas to test against. We build this up at construction time, but |
| 220 |
// then don't resize, so we can load from it continuously from all of the |
| 221 |
// threads. |
| 222 |
std::vector<MemoryQuota> quotas_; |
| 223 |
// Memory allocators to test against. Similarly, built at construction time, |
| 224 |
// and then the shape of this vector is not changed. |
| 225 |
std::vector<MemoryOwner> allocators_; |
| 226 |
}; |
| 227 |
} // namespace |
| 228 |
|
| 229 |
} // namespace grpc_core |
| 230 |
|
| 231 |
TEST(MemoryQuotaStressTest, MainTest) { |
| 232 |
if (sizeof(void*) != 8) { |
| 233 |
gpr_log( |
| 234 |
GPR_ERROR, |
| 235 |
"This test assumes 64-bit processors in the values it uses for sizes. " |
| 236 |
"Since this test is mostly aimed at TSAN coverage, and that's mostly " |
| 237 |
"platform independent, we simply skip this test in 32-bit builds."); |
| 238 |
GTEST_SKIP(); |
| 239 |
} |
| 240 |
grpc_core::StressTest(16, 20).Run(8); |
| 241 |
} |
| 242 |
|
| 243 |
int main(int argc, char** argv) { |
| 244 |
grpc::testing::TestEnvironment give_me_a_name(&argc, argv); |
| 245 |
::testing::InitGoogleTest(&argc, argv); |
| 246 |
return RUN_ALL_TESTS(); |
| 247 |
} |