| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2017 gRPC authors. |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
* |
| 17 |
*/ |
| 18 |
|
| 19 |
#include <string.h> |
| 20 |
|
| 21 |
#include <atomic> |
| 22 |
|
| 23 |
#include <benchmark/benchmark.h> |
| 24 |
|
| 25 |
#include <grpc/grpc.h> |
| 26 |
#include <grpc/support/alloc.h> |
| 27 |
#include <grpc/support/log.h> |
| 28 |
|
| 29 |
#include "src/core/lib/gprpp/time.h" |
| 30 |
#include "src/core/lib/iomgr/ev_posix.h" |
| 31 |
#include "src/core/lib/iomgr/port.h" |
| 32 |
#include "src/core/lib/surface/completion_queue.h" |
| 33 |
#include "test/core/util/test_config.h" |
| 34 |
#include "test/cpp/microbenchmarks/helpers.h" |
| 35 |
#include "test/cpp/util/test_config.h" |
| 36 |
|
| 37 |
struct grpc_pollset { |
| 38 |
gpr_mu mu; |
| 39 |
}; |
| 40 |
|
| 41 |
static gpr_mu g_mu; |
| 42 |
static gpr_cv g_cv; |
| 43 |
static int g_threads_active; |
| 44 |
static bool g_active; |
| 45 |
|
| 46 |
namespace grpc { |
| 47 |
namespace testing { |
| 48 |
static grpc_completion_queue* g_cq; |
| 49 |
|
| 50 |
static void pollset_shutdown(grpc_pollset* /*ps*/, grpc_closure* closure) { |
| 51 |
grpc_core::ExecCtx::Run(DEBUG_LOCATION, closure, absl::OkStatus()); |
| 52 |
} |
| 53 |
|
| 54 |
static void pollset_init(grpc_pollset* ps, gpr_mu** mu) { |
| 55 |
gpr_mu_init(&ps->mu); |
| 56 |
*mu = &ps->mu; |
| 57 |
} |
| 58 |
|
| 59 |
static void pollset_destroy(grpc_pollset* ps) { gpr_mu_destroy(&ps->mu); } |
| 60 |
|
| 61 |
static grpc_error_handle pollset_kick(grpc_pollset* /*p*/, |
| 62 |
grpc_pollset_worker* /*worker*/) { |
| 63 |
return absl::OkStatus(); |
| 64 |
} |
| 65 |
|
| 66 |
/* Callback when the tag is dequeued from the completion queue. Does nothing */ |
| 67 |
static void cq_done_cb(void* /*done_arg*/, grpc_cq_completion* cq_completion) { |
| 68 |
gpr_free(cq_completion); |
| 69 |
} |
| 70 |
|
| 71 |
/* Queues a completion tag if deadline is > 0. |
| 72 |
* Does nothing if deadline is 0 (i.e gpr_time_0(GPR_CLOCK_MONOTONIC)) */ |
| 73 |
static grpc_error_handle pollset_work(grpc_pollset* ps, |
| 74 |
grpc_pollset_worker** /*worker*/, |
| 75 |
grpc_core::Timestamp deadline) { |
| 76 |
if (deadline == grpc_core::Timestamp::ProcessEpoch()) { |
| 77 |
gpr_log(GPR_DEBUG, "no-op"); |
| 78 |
return absl::OkStatus(); |
| 79 |
} |
| 80 |
|
| 81 |
gpr_mu_unlock(&ps->mu); |
| 82 |
|
| 83 |
void* tag = reinterpret_cast<void*>(10); // Some random number |
| 84 |
GPR_ASSERT(grpc_cq_begin_op(g_cq, tag)); |
| 85 |
grpc_cq_end_op( |
| 86 |
g_cq, tag, absl::OkStatus(), cq_done_cb, nullptr, |
| 87 |
static_cast<grpc_cq_completion*>(gpr_malloc(sizeof(grpc_cq_completion)))); |
| 88 |
grpc_core::ExecCtx::Get()->Flush(); |
| 89 |
gpr_mu_lock(&ps->mu); |
| 90 |
return absl::OkStatus(); |
| 91 |
} |
| 92 |
|
| 93 |
static grpc_event_engine_vtable make_engine_vtable(const char* name) { |
| 94 |
grpc_event_engine_vtable vtable; |
| 95 |
memset(&vtable, 0, sizeof(vtable)); |
| 96 |
|
| 97 |
vtable.pollset_size = sizeof(grpc_pollset); |
| 98 |
vtable.pollset_init = pollset_init; |
| 99 |
vtable.pollset_shutdown = pollset_shutdown; |
| 100 |
vtable.pollset_destroy = pollset_destroy; |
| 101 |
vtable.pollset_work = pollset_work; |
| 102 |
vtable.pollset_kick = pollset_kick; |
| 103 |
vtable.is_any_background_poller_thread = [] { return false; }; |
| 104 |
vtable.add_closure_to_background_poller = [](grpc_closure* /*closure*/, |
| 105 |
grpc_error_handle /*error*/) { |
| 106 |
return false; |
| 107 |
}; |
| 108 |
vtable.shutdown_background_closure = [] {}; |
| 109 |
vtable.shutdown_engine = [] {}; |
| 110 |
vtable.check_engine_available = [](bool) { return true; }; |
| 111 |
vtable.init_engine = [] {}; |
| 112 |
vtable.name = name; |
| 113 |
|
| 114 |
return vtable; |
| 115 |
} |
| 116 |
|
| 117 |
static void setup() { |
| 118 |
grpc_init(); |
| 119 |
GPR_ASSERT(strcmp(grpc_get_poll_strategy_name(), "none") == 0 || |
| 120 |
strcmp(grpc_get_poll_strategy_name(), "bm_cq_multiple_threads") == |
| 121 |
0); |
| 122 |
|
| 123 |
g_cq = grpc_completion_queue_create_for_next(nullptr); |
| 124 |
} |
| 125 |
|
| 126 |
static void teardown() { |
| 127 |
grpc_completion_queue_shutdown(g_cq); |
| 128 |
|
| 129 |
/* Drain any events */ |
| 130 |
gpr_timespec deadline = gpr_time_0(GPR_CLOCK_MONOTONIC); |
| 131 |
while (grpc_completion_queue_next(g_cq, deadline, nullptr).type != |
| 132 |
GRPC_QUEUE_SHUTDOWN) { |
| 133 |
/* Do nothing */ |
| 134 |
} |
| 135 |
|
| 136 |
grpc_completion_queue_destroy(g_cq); |
| 137 |
grpc_shutdown(); |
| 138 |
} |
| 139 |
|
| 140 |
/* A few notes about Multi-threaded benchmarks: |
| 141 |
|
| 142 |
Setup: |
| 143 |
The benchmark framework ensures that none of the threads proceed beyond the |
| 144 |
state.KeepRunning() call unless all the threads have called state.keepRunning |
| 145 |
at least once. So it is safe to do the initialization in one of the threads |
| 146 |
before state.KeepRunning() is called. |
| 147 |
|
| 148 |
Teardown: |
| 149 |
The benchmark framework also ensures that no thread is running the benchmark |
| 150 |
code (i.e the code between two successive calls of state.KeepRunning()) if |
| 151 |
state.KeepRunning() returns false. So it is safe to do the teardown in one |
| 152 |
of the threads after state.keepRunning() returns false. |
| 153 |
|
| 154 |
However, our use requires synchronization because we do additional work at |
| 155 |
each thread that requires specific ordering (TrackCounters must be constructed |
| 156 |
after grpc_init because it needs the number of cores, initialized by grpc, |
| 157 |
and its Finish call must take place before grpc_shutdown so that it can use |
| 158 |
grpc_stats). |
| 159 |
*/ |
| 160 |
static void BM_Cq_Throughput(benchmark::State& state) { |
| 161 |
gpr_timespec deadline = gpr_inf_future(GPR_CLOCK_MONOTONIC); |
| 162 |
auto thd_idx = state.thread_index(); |
| 163 |
|
| 164 |
gpr_mu_lock(&g_mu); |
| 165 |
g_threads_active++; |
| 166 |
if (thd_idx == 0) { |
| 167 |
setup(); |
| 168 |
g_active = true; |
| 169 |
gpr_cv_broadcast(&g_cv); |
| 170 |
} else { |
| 171 |
while (!g_active) { |
| 172 |
gpr_cv_wait(&g_cv, &g_mu, deadline); |
| 173 |
} |
| 174 |
} |
| 175 |
gpr_mu_unlock(&g_mu); |
| 176 |
|
| 177 |
for (auto _ : state) { |
| 178 |
GPR_ASSERT(grpc_completion_queue_next(g_cq, deadline, nullptr).type == |
| 179 |
GRPC_OP_COMPLETE); |
| 180 |
} |
| 181 |
|
| 182 |
state.SetItemsProcessed(state.iterations()); |
| 183 |
|
| 184 |
gpr_mu_lock(&g_mu); |
| 185 |
g_threads_active--; |
| 186 |
if (g_threads_active == 0) { |
| 187 |
gpr_cv_broadcast(&g_cv); |
| 188 |
} else { |
| 189 |
while (g_threads_active > 0) { |
| 190 |
gpr_cv_wait(&g_cv, &g_mu, deadline); |
| 191 |
} |
| 192 |
} |
| 193 |
gpr_mu_unlock(&g_mu); |
| 194 |
|
| 195 |
if (thd_idx == 0) { |
| 196 |
teardown(); |
| 197 |
g_active = false; |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
BENCHMARK(BM_Cq_Throughput)->ThreadRange(1, 16)->UseRealTime(); |
| 202 |
|
| 203 |
namespace { |
| 204 |
const grpc_event_engine_vtable g_none_vtable = |
| 205 |
grpc::testing::make_engine_vtable("none"); |
| 206 |
const grpc_event_engine_vtable g_bm_vtable = |
| 207 |
grpc::testing::make_engine_vtable("bm_cq_multiple_threads"); |
| 208 |
} // namespace |
| 209 |
|
| 210 |
} // namespace testing |
| 211 |
} // namespace grpc |
| 212 |
|
| 213 |
// Some distros have RunSpecifiedBenchmarks under the benchmark namespace, |
| 214 |
// and others do not. This allows us to support both modes. |
| 215 |
namespace benchmark { |
| 216 |
void RunTheBenchmarksNamespaced() { RunSpecifiedBenchmarks(); } |
| 217 |
} // namespace benchmark |
| 218 |
|
| 219 |
int main(int argc, char** argv) { |
| 220 |
// This test should only ever be run with a non or any polling engine |
| 221 |
// Override the polling engine for the non-polling engine |
| 222 |
// and add a custom polling engine |
| 223 |
grpc_register_event_engine_factory(&grpc::testing::g_none_vtable, false); |
| 224 |
grpc_register_event_engine_factory(&grpc::testing::g_bm_vtable, true); |
| 225 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 226 |
gpr_mu_init(&g_mu); |
| 227 |
gpr_cv_init(&g_cv); |
| 228 |
::benchmark::Initialize(&argc, argv); |
| 229 |
grpc::testing::InitTest(&argc, &argv, false); |
| 230 |
benchmark::RunTheBenchmarksNamespaced(); |
| 231 |
return 0; |
| 232 |
} |