| 1 |
// Copyright 2022 The 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 <atomic> |
| 16 |
#include <cmath> |
| 17 |
#include <memory> |
| 18 |
#include <vector> |
| 19 |
|
| 20 |
#include <benchmark/benchmark.h> |
| 21 |
|
| 22 |
#include "absl/functional/any_invocable.h" |
| 23 |
|
| 24 |
#include <grpcpp/impl/grpc_library.h> |
| 25 |
|
| 26 |
#include "src/core/lib/event_engine/common_closures.h" |
| 27 |
#include "src/core/lib/event_engine/thread_pool.h" |
| 28 |
#include "src/core/lib/gprpp/notification.h" |
| 29 |
#include "test/core/util/test_config.h" |
| 30 |
#include "test/cpp/microbenchmarks/helpers.h" |
| 31 |
#include "test/cpp/util/test_config.h" |
| 32 |
|
| 33 |
namespace { |
| 34 |
|
| 35 |
using ::grpc_event_engine::experimental::AnyInvocableClosure; |
| 36 |
using ::grpc_event_engine::experimental::EventEngine; |
| 37 |
using ::grpc_event_engine::experimental::ThreadPool; |
| 38 |
|
| 39 |
struct FanoutParameters { |
| 40 |
int depth; |
| 41 |
int fanout; |
| 42 |
int limit; |
| 43 |
}; |
| 44 |
|
| 45 |
void BM_ThreadPool_RunSmallLambda(benchmark::State& state) { |
| 46 |
ThreadPool pool; |
| 47 |
const int cb_count = state.range(0); |
| 48 |
std::atomic_int count{0}; |
| 49 |
for (auto _ : state) { |
| 50 |
state.PauseTiming(); |
| 51 |
grpc_core::Notification signal; |
| 52 |
auto cb = [&signal, &count, cb_count]() { |
| 53 |
if (++count == cb_count) signal.Notify(); |
| 54 |
}; |
| 55 |
state.ResumeTiming(); |
| 56 |
for (int i = 0; i < cb_count; i++) { |
| 57 |
pool.Run(cb); |
| 58 |
} |
| 59 |
signal.WaitForNotification(); |
| 60 |
count.store(0); |
| 61 |
} |
| 62 |
state.SetItemsProcessed(cb_count * state.iterations()); |
| 63 |
pool.Quiesce(); |
| 64 |
} |
| 65 |
BENCHMARK(BM_ThreadPool_RunSmallLambda) |
| 66 |
->Range(100, 4096) |
| 67 |
->MeasureProcessCPUTime() |
| 68 |
->UseRealTime(); |
| 69 |
|
| 70 |
void BM_ThreadPool_RunClosure(benchmark::State& state) { |
| 71 |
int cb_count = state.range(0); |
| 72 |
grpc_core::Notification* signal = new grpc_core::Notification(); |
| 73 |
std::atomic_int count{0}; |
| 74 |
AnyInvocableClosure* closure = |
| 75 |
new AnyInvocableClosure([signal_holder = &signal, cb_count, &count]() { |
| 76 |
if (++count == cb_count) { |
| 77 |
(*signal_holder)->Notify(); |
| 78 |
} |
| 79 |
}); |
| 80 |
ThreadPool pool; |
| 81 |
for (auto _ : state) { |
| 82 |
for (int i = 0; i < cb_count; i++) { |
| 83 |
pool.Run(closure); |
| 84 |
} |
| 85 |
signal->WaitForNotification(); |
| 86 |
state.PauseTiming(); |
| 87 |
delete signal; |
| 88 |
signal = new grpc_core::Notification(); |
| 89 |
count.store(0); |
| 90 |
state.ResumeTiming(); |
| 91 |
} |
| 92 |
delete signal; |
| 93 |
state.SetItemsProcessed(cb_count * state.iterations()); |
| 94 |
pool.Quiesce(); |
| 95 |
delete closure; |
| 96 |
} |
| 97 |
BENCHMARK(BM_ThreadPool_RunClosure) |
| 98 |
->Range(100, 4096) |
| 99 |
->MeasureProcessCPUTime() |
| 100 |
->UseRealTime(); |
| 101 |
|
| 102 |
void FanoutTestArguments(benchmark::internal::Benchmark* b) { |
| 103 |
// TODO(hork): enable when the engines are fast enough to run these: |
| 104 |
// ->Args({10000, 1}) // chain of callbacks scheduling callbacks |
| 105 |
// ->Args({1, 10000}) // flat scheduling of callbacks |
| 106 |
// ->Args({5, 6}) // depth 5, fans out to 9,330 callbacks |
| 107 |
// ->Args({2, 100}) // depth 2, fans out 10,101 callbacks |
| 108 |
// ->Args({4, 10}) // depth 4, fans out to 11,110 callbacks |
| 109 |
b->Args({1000, 1}) // chain of callbacks scheduling callbacks |
| 110 |
->Args({100, 1}) // chain of callbacks scheduling callbacks |
| 111 |
->Args({1, 1000}) // flat scheduling of callbacks |
| 112 |
->Args({1, 100}) // flat scheduling of callbacks |
| 113 |
->Args({2, 70}) // depth 2, fans out 4971 |
| 114 |
->Args({4, 8}) // depth 4, fans out 4681 |
| 115 |
->UseRealTime() |
| 116 |
->MeasureProcessCPUTime(); |
| 117 |
} |
| 118 |
|
| 119 |
FanoutParameters GetFanoutParameters(benchmark::State& state) { |
| 120 |
FanoutParameters params; |
| 121 |
params.depth = state.range(0); |
| 122 |
params.fanout = state.range(1); |
| 123 |
if (params.depth == 1 || params.fanout == 1) { |
| 124 |
params.limit = std::max(params.depth, params.fanout) + 1; |
| 125 |
} else { |
| 126 |
// sum of geometric series |
| 127 |
params.limit = |
| 128 |
(1 - std::pow(params.fanout, params.depth + 1)) / (1 - params.fanout); |
| 129 |
} |
| 130 |
// sanity checking |
| 131 |
GPR_ASSERT(params.limit >= params.fanout * params.depth); |
| 132 |
return params; |
| 133 |
} |
| 134 |
|
| 135 |
// Callback for Lambda FanOut tests |
| 136 |
// |
| 137 |
// Note that params are copied each time for 2 reasons: 1) callbacks will |
| 138 |
// inevitably continue to shut down after the end of the test, so a reference |
| 139 |
// parameter will become invalid and crash some callbacks, and 2) in my RBE |
| 140 |
// tests, copies are slightly faster than a shared_ptr<FanoutParams> |
| 141 |
// alternative. |
| 142 |
void FanOutCallback(std::shared_ptr<ThreadPool> pool, |
| 143 |
const FanoutParameters params, |
| 144 |
grpc_core::Notification& signal, std::atomic_int& count, |
| 145 |
int processing_layer) { |
| 146 |
int local_cnt = count.fetch_add(1, std::memory_order_acq_rel) + 1; |
| 147 |
if (local_cnt == params.limit) { |
| 148 |
signal.Notify(); |
| 149 |
return; |
| 150 |
} |
| 151 |
GPR_DEBUG_ASSERT(local_cnt < params.limit); |
| 152 |
if (params.depth == processing_layer) return; |
| 153 |
for (int i = 0; i < params.fanout; i++) { |
| 154 |
pool->Run([pool, params, processing_layer, &count, &signal]() { |
| 155 |
FanOutCallback(pool, params, signal, count, processing_layer + 1); |
| 156 |
}); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
void BM_ThreadPool_Lambda_FanOut(benchmark::State& state) { |
| 161 |
auto params = GetFanoutParameters(state); |
| 162 |
auto pool = std::make_shared<ThreadPool>(); |
| 163 |
for (auto _ : state) { |
| 164 |
std::atomic_int count{0}; |
| 165 |
grpc_core::Notification signal; |
| 166 |
FanOutCallback(pool, params, signal, count, /*processing_layer=*/0); |
| 167 |
do { |
| 168 |
signal.WaitForNotification(); |
| 169 |
} while (count.load() != params.limit); |
| 170 |
} |
| 171 |
state.SetItemsProcessed(params.limit * state.iterations()); |
| 172 |
pool->Quiesce(); |
| 173 |
} |
| 174 |
BENCHMARK(BM_ThreadPool_Lambda_FanOut)->Apply(FanoutTestArguments); |
| 175 |
|
| 176 |
void ClosureFanOutCallback(EventEngine::Closure* child_closure, |
| 177 |
std::shared_ptr<ThreadPool> pool, |
| 178 |
grpc_core::Notification** signal_holder, |
| 179 |
std::atomic_int& count, |
| 180 |
const FanoutParameters params) { |
| 181 |
int local_cnt = count.fetch_add(1, std::memory_order_acq_rel) + 1; |
| 182 |
if (local_cnt == params.limit) { |
| 183 |
(*signal_holder)->Notify(); |
| 184 |
return; |
| 185 |
} |
| 186 |
if (local_cnt > params.limit) { |
| 187 |
gpr_log(GPR_ERROR, "Ran too many closures: %d/%d", local_cnt, params.limit); |
| 188 |
abort(); |
| 189 |
} |
| 190 |
if (child_closure == nullptr) return; |
| 191 |
for (int i = 0; i < params.fanout; i++) { |
| 192 |
pool->Run(child_closure); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
void BM_ThreadPool_Closure_FanOut(benchmark::State& state) { |
| 197 |
auto params = GetFanoutParameters(state); |
| 198 |
auto pool = std::make_shared<ThreadPool>(); |
| 199 |
std::vector<EventEngine::Closure*> closures; |
| 200 |
closures.reserve(params.depth + 2); |
| 201 |
closures.push_back(nullptr); |
| 202 |
grpc_core::Notification* signal = new grpc_core::Notification(); |
| 203 |
std::atomic_int count{0}; |
| 204 |
// prepare a unique closure for each depth |
| 205 |
for (int i = 0; i <= params.depth; i++) { |
| 206 |
// call the previous closure (e.g., closures[2] calls closures[1] during |
| 207 |
// fanout) |
| 208 |
closures.push_back(new AnyInvocableClosure( |
| 209 |
[i, pool, &closures, params, signal_holder = &signal, &count]() { |
| 210 |
ClosureFanOutCallback(closures[i], pool, signal_holder, count, |
| 211 |
params); |
| 212 |
})); |
| 213 |
} |
| 214 |
for (auto _ : state) { |
| 215 |
GPR_DEBUG_ASSERT(count.load(std::memory_order_relaxed) == 0); |
| 216 |
pool->Run(closures[params.depth + 1]); |
| 217 |
do { |
| 218 |
signal->WaitForNotification(); |
| 219 |
} while (count.load() != params.limit); |
| 220 |
// cleanup |
| 221 |
state.PauseTiming(); |
| 222 |
delete signal; |
| 223 |
signal = new grpc_core::Notification(); |
| 224 |
count.store(0); |
| 225 |
state.ResumeTiming(); |
| 226 |
} |
| 227 |
delete signal; |
| 228 |
state.SetItemsProcessed(params.limit * state.iterations()); |
| 229 |
for (auto i : closures) delete i; |
| 230 |
pool->Quiesce(); |
| 231 |
} |
| 232 |
BENCHMARK(BM_ThreadPool_Closure_FanOut)->Apply(FanoutTestArguments); |
| 233 |
|
| 234 |
} // namespace |
| 235 |
|
| 236 |
// Some distros have RunSpecifiedBenchmarks under the benchmark namespace, |
| 237 |
// and others do not. This allows us to support both modes. |
| 238 |
namespace benchmark { |
| 239 |
void RunTheBenchmarksNamespaced() { RunSpecifiedBenchmarks(); } |
| 240 |
} // namespace benchmark |
| 241 |
|
| 242 |
int main(int argc, char** argv) { |
| 243 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 244 |
LibraryInitializer libInit; |
| 245 |
benchmark::Initialize(&argc, argv); |
| 246 |
grpc::testing::InitTest(&argc, &argv, false); |
| 247 |
|
| 248 |
benchmark::RunTheBenchmarksNamespaced(); |
| 249 |
return 0; |
| 250 |
} |