diff options
author | Craig Tiller <ctiller@google.com> | 2017-02-27 07:24:00 -0800 |
---|---|---|
committer | Craig Tiller <ctiller@google.com> | 2017-02-27 07:24:00 -0800 |
commit | edbf2b9d13445221c9d8858c00cea02435570fe0 (patch) | |
tree | 9cb8deb7f5cb9dc4855d4082b6581550fe455529 /test/cpp/microbenchmarks/bm_closure.cc | |
parent | ca8de5c1c3aa47bff5ab83482ed4b33c8cbc3047 (diff) |
Add a spinlock type.
Useful for situations where we need to repeatedly trylock, not useful
for cases where we need to lock (due to spinning).
Add a variant of sync_test to test it (with the same tests we run for
gpr_mu).
Add a benchmark to bm_closure to demonstrate single threaded performance.
Diffstat (limited to 'test/cpp/microbenchmarks/bm_closure.cc')
-rw-r--r-- | test/cpp/microbenchmarks/bm_closure.cc | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/test/cpp/microbenchmarks/bm_closure.cc b/test/cpp/microbenchmarks/bm_closure.cc index 03aede35b2..1f54e8c8b1 100644 --- a/test/cpp/microbenchmarks/bm_closure.cc +++ b/test/cpp/microbenchmarks/bm_closure.cc @@ -39,6 +39,7 @@ extern "C" { #include "src/core/lib/iomgr/closure.h" #include "src/core/lib/iomgr/combiner.h" #include "src/core/lib/iomgr/exec_ctx.h" +#include "src/core/lib/support/spinlock.h" } #include "third_party/benchmark/include/benchmark/benchmark.h" @@ -234,6 +235,55 @@ static void BM_AcquireMutex(benchmark::State& state) { } BENCHMARK(BM_AcquireMutex); +static void BM_TryAcquireMutex(benchmark::State& state) { + TrackCounters track_counters(state); + // for comparison with the combiner stuff below + gpr_mu mu; + gpr_mu_init(&mu); + grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; + while (state.KeepRunning()) { + if (gpr_mu_trylock(&mu)) { + DoNothing(&exec_ctx, NULL, GRPC_ERROR_NONE); + gpr_mu_unlock(&mu); + } else { + abort(); + } + } + grpc_exec_ctx_finish(&exec_ctx); +} +BENCHMARK(BM_TryAcquireMutex); + +static void BM_AcquireSpinlock(benchmark::State& state) { + TrackCounters track_counters(state); + // for comparison with the combiner stuff below + gpr_spinlock mu = GPR_SPINLOCK_INITIALIZER; + grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; + while (state.KeepRunning()) { + gpr_spinlock_lock(&mu); + DoNothing(&exec_ctx, NULL, GRPC_ERROR_NONE); + gpr_spinlock_unlock(&mu); + } + grpc_exec_ctx_finish(&exec_ctx); +} +BENCHMARK(BM_AcquireSpinlock); + +static void BM_TryAcquireSpinlock(benchmark::State& state) { + TrackCounters track_counters(state); + // for comparison with the combiner stuff below + gpr_spinlock mu = GPR_SPINLOCK_INITIALIZER; + grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; + while (state.KeepRunning()) { + if (gpr_spinlock_trylock(&mu)) { + DoNothing(&exec_ctx, NULL, GRPC_ERROR_NONE); + gpr_spinlock_unlock(&mu); + } else { + abort(); + } + } + grpc_exec_ctx_finish(&exec_ctx); +} +BENCHMARK(BM_TryAcquireSpinlock); + static void BM_ClosureSchedOnCombiner(benchmark::State& state) { TrackCounters track_counters(state); grpc_combiner* combiner = grpc_combiner_create(NULL); |