bm_closure.cc
Go to the documentation of this file.
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 /* Test various closure related operations */
20 
21 #include <sstream>
22 
23 #include <benchmark/benchmark.h>
24 
25 #include <grpc/grpc.h>
26 
34 
36  TrackCounters track_counters;
37  for (auto _ : state) {
39  }
40  track_counters.Finish(state);
41 }
43 
45  TrackCounters track_counters;
47  for (auto _ : state) {
49  }
50 
51  track_counters.Finish(state);
52 }
54 
55 static void DoNothing(void* /*arg*/, grpc_error_handle /*error*/) {}
56 
58  TrackCounters track_counters;
60  for (auto _ : state) {
62  GRPC_CLOSURE_INIT(&c, DoNothing, nullptr, grpc_schedule_on_exec_ctx));
63  }
64  track_counters.Finish(state);
65 }
67 
69  TrackCounters track_counters;
73  for (auto _ : state) {
75  GRPC_CLOSURE_INIT(&c, DoNothing, nullptr, nullptr));
76  }
77  GRPC_COMBINER_UNREF(combiner, "finished");
78 
79  track_counters.Finish(state);
80 }
82 
84  TrackCounters track_counters;
86  GRPC_CLOSURE_INIT(&c, DoNothing, nullptr, grpc_schedule_on_exec_ctx);
88  for (auto _ : state) {
90  }
91 
92  track_counters.Finish(state);
93 }
95 
97  TrackCounters track_counters;
99  for (auto _ : state) {
102  GRPC_CLOSURE_CREATE(DoNothing, nullptr, grpc_schedule_on_exec_ctx),
104  }
105 
106  track_counters.Finish(state);
107 }
109 
111  TrackCounters track_counters;
113  grpc_closure c;
114  for (auto _ : state) {
117  GRPC_CLOSURE_INIT(&c, DoNothing, nullptr, grpc_schedule_on_exec_ctx),
119  }
120 
121  track_counters.Finish(state);
122 }
124 
126  TrackCounters track_counters;
127  grpc_closure c;
128  GRPC_CLOSURE_INIT(&c, DoNothing, nullptr, grpc_schedule_on_exec_ctx);
130  for (auto _ : state) {
133  }
134 
135  track_counters.Finish(state);
136 }
138 
140  TrackCounters track_counters;
143  GRPC_CLOSURE_INIT(&c1, DoNothing, nullptr, grpc_schedule_on_exec_ctx);
144  GRPC_CLOSURE_INIT(&c2, DoNothing, nullptr, grpc_schedule_on_exec_ctx);
146  for (auto _ : state) {
150  }
151 
152  track_counters.Finish(state);
153 }
155 
157  TrackCounters track_counters;
160  grpc_closure c3;
161  GRPC_CLOSURE_INIT(&c1, DoNothing, nullptr, grpc_schedule_on_exec_ctx);
162  GRPC_CLOSURE_INIT(&c2, DoNothing, nullptr, grpc_schedule_on_exec_ctx);
163  GRPC_CLOSURE_INIT(&c3, DoNothing, nullptr, grpc_schedule_on_exec_ctx);
165  for (auto _ : state) {
170  }
171 
172  track_counters.Finish(state);
173 }
175 
177  TrackCounters track_counters;
178  // for comparison with the combiner stuff below
179  gpr_mu mu;
180  gpr_mu_init(&mu);
182  for (auto _ : state) {
183  gpr_mu_lock(&mu);
184  DoNothing(nullptr, GRPC_ERROR_NONE);
185  gpr_mu_unlock(&mu);
186  }
187  gpr_mu_destroy(&mu);
188 
189  track_counters.Finish(state);
190 }
192 
194  TrackCounters track_counters;
195  // for comparison with the combiner stuff below
196  gpr_mu mu;
197  gpr_mu_init(&mu);
199  for (auto _ : state) {
200  if (gpr_mu_trylock(&mu)) {
201  DoNothing(nullptr, GRPC_ERROR_NONE);
202  gpr_mu_unlock(&mu);
203  } else {
204  abort();
205  }
206  }
207  gpr_mu_destroy(&mu);
208 
209  track_counters.Finish(state);
210 }
212 
214  TrackCounters track_counters;
215  // for comparison with the combiner stuff below
218  for (auto _ : state) {
220  DoNothing(nullptr, GRPC_ERROR_NONE);
222  }
223 
224  track_counters.Finish(state);
225 }
227 
229  TrackCounters track_counters;
230  // for comparison with the combiner stuff below
233  for (auto _ : state) {
234  if (gpr_spinlock_trylock(&mu)) {
235  DoNothing(nullptr, GRPC_ERROR_NONE);
237  } else {
238  abort();
239  }
240  }
241 
242  track_counters.Finish(state);
243 }
245 
247  TrackCounters track_counters;
249  grpc_closure c;
250  GRPC_CLOSURE_INIT(&c, DoNothing, nullptr, nullptr);
252  for (auto _ : state) {
253  combiner->Run(&c, GRPC_ERROR_NONE);
255  }
256  GRPC_COMBINER_UNREF(combiner, "finished");
257 
258  track_counters.Finish(state);
259 }
261 
263  TrackCounters track_counters;
267  GRPC_CLOSURE_INIT(&c1, DoNothing, nullptr, nullptr);
268  GRPC_CLOSURE_INIT(&c2, DoNothing, nullptr, nullptr);
270  for (auto _ : state) {
271  combiner->Run(&c1, GRPC_ERROR_NONE);
272  combiner->Run(&c2, GRPC_ERROR_NONE);
274  }
275  GRPC_COMBINER_UNREF(combiner, "finished");
276 
277  track_counters.Finish(state);
278 }
280 
282  TrackCounters track_counters;
286  grpc_closure c3;
287  GRPC_CLOSURE_INIT(&c1, DoNothing, nullptr, nullptr);
288  GRPC_CLOSURE_INIT(&c2, DoNothing, nullptr, nullptr);
289  GRPC_CLOSURE_INIT(&c3, DoNothing, nullptr, nullptr);
291  for (auto _ : state) {
292  combiner->Run(&c1, GRPC_ERROR_NONE);
293  combiner->Run(&c2, GRPC_ERROR_NONE);
294  combiner->Run(&c3, GRPC_ERROR_NONE);
296  }
297  GRPC_COMBINER_UNREF(combiner, "finished");
298 
299  track_counters.Finish(state);
300 }
302 
304  TrackCounters track_counters;
309  GRPC_CLOSURE_INIT(&c1, DoNothing, nullptr, nullptr);
310  GRPC_CLOSURE_INIT(&c2, DoNothing, nullptr, nullptr);
312  for (auto _ : state) {
313  combiner1->Run(&c1, GRPC_ERROR_NONE);
314  combiner2->Run(&c2, GRPC_ERROR_NONE);
316  }
317  GRPC_COMBINER_UNREF(combiner1, "finished");
318  GRPC_COMBINER_UNREF(combiner2, "finished");
319 
320  track_counters.Finish(state);
321 }
323 
325  TrackCounters track_counters;
330  grpc_closure c3;
331  grpc_closure c4;
332  GRPC_CLOSURE_INIT(&c1, DoNothing, nullptr, nullptr);
333  GRPC_CLOSURE_INIT(&c2, DoNothing, nullptr, nullptr);
334  GRPC_CLOSURE_INIT(&c3, DoNothing, nullptr, nullptr);
335  GRPC_CLOSURE_INIT(&c4, DoNothing, nullptr, nullptr);
337  for (auto _ : state) {
338  combiner1->Run(&c1, GRPC_ERROR_NONE);
339  combiner2->Run(&c2, GRPC_ERROR_NONE);
340  combiner1->Run(&c3, GRPC_ERROR_NONE);
341  combiner2->Run(&c4, GRPC_ERROR_NONE);
343  }
344  GRPC_COMBINER_UNREF(combiner1, "finished");
345  GRPC_COMBINER_UNREF(combiner2, "finished");
346 
347  track_counters.Finish(state);
348 }
350 
351 // Helper that continuously reschedules the same closure against something until
352 // the benchmark is complete
353 class Rescheduler {
354  public:
356  GRPC_CLOSURE_INIT(&closure_, Step, this, nullptr);
357  }
358 
359  void ScheduleFirst() {
361  }
362 
365  GRPC_CLOSURE_CREATE(Step, this, nullptr),
367  }
368 
369  private:
372 
373  static void Step(void* arg, grpc_error_handle /*error*/) {
374  Rescheduler* self = static_cast<Rescheduler*>(arg);
375  if (self->state_.KeepRunning()) {
377  }
378  }
379 };
380 
382  TrackCounters track_counters;
385  r.ScheduleFirst();
387  track_counters.Finish(state);
388 }
390 
391 // Some distros have RunSpecifiedBenchmarks under the benchmark namespace,
392 // and others do not. This allows us to support both modes.
393 namespace benchmark {
395 } // namespace benchmark
396 
397 int main(int argc, char** argv) {
398  grpc::testing::TestEnvironment env(&argc, argv);
399  LibraryInitializer libInit;
400  ::benchmark::Initialize(&argc, argv);
401  grpc::testing::InitTest(&argc, &argv, false);
403  return 0;
404 }
GRPC_CLOSURE_INIT
#define GRPC_CLOSURE_INIT(closure, cb, cb_arg, scheduler)
Definition: closure.h:115
grpc::testing::InitTest
void InitTest(int *argc, char ***argv, bool remove_flags)
Definition: test_config_cc.cc:28
GPR_SPINLOCK_INITIALIZER
#define GPR_SPINLOCK_INITIALIZER
Definition: src/core/lib/gpr/spinlock.h:35
gpr_mu_unlock
GPRAPI void gpr_mu_unlock(gpr_mu *mu)
GRPC_ERROR_NONE
#define GRPC_ERROR_NONE
Definition: error.h:234
benchmark
Definition: bm_alarm.cc:55
generate.env
env
Definition: generate.py:37
BM_NoOpExecCtx
static void BM_NoOpExecCtx(benchmark::State &state)
Definition: bm_closure.cc:35
Rescheduler::ScheduleFirst
void ScheduleFirst()
Definition: bm_closure.cc:359
Rescheduler::Step
static void Step(void *arg, grpc_error_handle)
Definition: bm_closure.cc:373
gpr_spinlock
Definition: src/core/lib/gpr/spinlock.h:29
BM_ClosureSched3OnCombiner
static void BM_ClosureSched3OnCombiner(benchmark::State &state)
Definition: bm_closure.cc:281
BENCHMARK
BENCHMARK(BM_NoOpExecCtx)
closure.h
benchmark::DoNotOptimize
BENCHMARK_ALWAYS_INLINE void DoNotOptimize(Tp const &value)
Definition: benchmark/include/benchmark/benchmark.h:375
benchmark::RunTheBenchmarksNamespaced
void RunTheBenchmarksNamespaced()
Definition: bm_alarm.cc:56
GRPC_CLOSURE_CREATE
#define GRPC_CLOSURE_CREATE(cb, cb_arg, scheduler)
Definition: closure.h:160
TrackCounters::Finish
virtual void Finish(benchmark::State &state)
Definition: helpers.cc:44
BM_TryAcquireSpinlock
static void BM_TryAcquireSpinlock(benchmark::State &state)
Definition: bm_closure.cc:228
gpr_spinlock_lock
#define gpr_spinlock_lock(lock)
Definition: src/core/lib/gpr/spinlock.h:49
BM_AcquireSpinlock
static void BM_AcquireSpinlock(benchmark::State &state)
Definition: bm_closure.cc:213
gpr_mu_destroy
GPRAPI void gpr_mu_destroy(gpr_mu *mu)
TrackCounters
Definition: helpers.h:51
DEBUG_LOCATION
#define DEBUG_LOCATION
Definition: debug_location.h:41
benchmark::RunSpecifiedBenchmarks
size_t RunSpecifiedBenchmarks()
Definition: benchmark/src/benchmark.cc:437
BM_ClosureInitAgainstCombiner
static void BM_ClosureInitAgainstCombiner(benchmark::State &state)
Definition: bm_closure.cc:68
grpc_combiner_create
grpc_core::Combiner * grpc_combiner_create(void)
Definition: combiner.cc:54
BM_ClosureInitAndRun
static void BM_ClosureInitAndRun(benchmark::State &state)
Definition: bm_closure.cc:110
BM_ClosureSched2OnTwoCombiners
static void BM_ClosureSched2OnTwoCombiners(benchmark::State &state)
Definition: bm_closure.cc:303
c
void c(T a)
Definition: miscompile_with_no_unique_address_test.cc:40
BM_ClosureInitAgainstExecCtx
static void BM_ClosureInitAgainstExecCtx(benchmark::State &state)
Definition: bm_closure.cc:57
grpc_core::ExecCtx::Flush
bool Flush()
Definition: exec_ctx.cc:69
mu
Mutex mu
Definition: server_config_selector_filter.cc:74
BM_ClosureSched2OnCombiner
static void BM_ClosureSched2OnCombiner(benchmark::State &state)
Definition: bm_closure.cc:262
gpr_mu_init
GPRAPI void gpr_mu_init(gpr_mu *mu)
grpc.h
gmock_output_test._
_
Definition: bloaty/third_party/googletest/googlemock/test/gmock_output_test.py:175
arg
Definition: cmdline.cc:40
gpr_spinlock_unlock
#define gpr_spinlock_unlock(lock)
Definition: src/core/lib/gpr/spinlock.h:41
grpc_core::Combiner::Run
void Run(grpc_closure *closure, grpc_error_handle error)
Definition: combiner.cc:343
gpr_mu_lock
GPRAPI void gpr_mu_lock(gpr_mu *mu)
benchmark::Initialize
void Initialize(int *argc, char **argv)
Definition: benchmark/src/benchmark.cc:602
helpers.h
grpc_core::ExecCtx
Definition: exec_ctx.h:97
main
int main(int argc, char **argv)
Definition: bm_closure.cc:397
BM_ClosureSched2OnExecCtx
static void BM_ClosureSched2OnExecCtx(benchmark::State &state)
Definition: bm_closure.cc:139
Rescheduler::state_
benchmark::State & state_
Definition: bm_closure.cc:370
LibraryInitializer
Definition: helpers.h:33
Rescheduler::Rescheduler
Rescheduler(benchmark::State &state)
Definition: bm_closure.cc:355
gpr_spinlock_trylock
#define gpr_spinlock_trylock(lock)
Definition: src/core/lib/gpr/spinlock.h:40
test_config.h
DoNothing
static void DoNothing(void *, grpc_error_handle)
Definition: bm_closure.cc:55
BM_ClosureSched3OnExecCtx
static void BM_ClosureSched3OnExecCtx(benchmark::State &state)
Definition: bm_closure.cc:156
absl::hash_internal::c1
static const uint32_t c1
Definition: abseil-cpp/absl/hash/internal/city.cc:58
BM_AcquireMutex
static void BM_AcquireMutex(benchmark::State &state)
Definition: bm_closure.cc:176
Rescheduler::ScheduleFirstAgainstDifferentScheduler
void ScheduleFirstAgainstDifferentScheduler()
Definition: bm_closure.cc:363
gpr_mu
pthread_mutex_t gpr_mu
Definition: impl/codegen/sync_posix.h:47
exec_ctx
grpc_core::ExecCtx exec_ctx
Definition: end2end_binder_transport_test.cc:75
gpr_mu_trylock
GPRAPI int gpr_mu_trylock(gpr_mu *mu)
benchmark::State
Definition: benchmark/include/benchmark/benchmark.h:503
fix_build_deps.r
r
Definition: fix_build_deps.py:491
BM_ClosureReschedOnExecCtx
static void BM_ClosureReschedOnExecCtx(benchmark::State &state)
Definition: bm_closure.cc:381
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
combiner.h
Rescheduler::closure_
grpc_closure closure_
Definition: bm_closure.cc:371
arg
struct arg arg
state
Definition: bloaty/third_party/zlib/contrib/blast/blast.c:41
exec_ctx.h
BM_ClosureCreateAndRun
static void BM_ClosureCreateAndRun(benchmark::State &state)
Definition: bm_closure.cc:96
test_config.h
grpc_core::ExecCtx::Run
static void Run(const DebugLocation &location, grpc_closure *closure, grpc_error_handle error)
Definition: exec_ctx.cc:98
grpc_core::Combiner
Definition: combiner.h:34
spinlock.h
grpc_error
Definition: error_internal.h:42
BM_ClosureSchedOnCombiner
static void BM_ClosureSchedOnCombiner(benchmark::State &state)
Definition: bm_closure.cc:246
self
PHP_PROTO_OBJECT_FREE_END PHP_PROTO_OBJECT_DTOR_END intern self
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/map.c:543
Rescheduler
Definition: bm_closure.cc:353
BM_WellFlushed
static void BM_WellFlushed(benchmark::State &state)
Definition: bm_closure.cc:44
BM_ClosureSchedOnExecCtx
static void BM_ClosureSchedOnExecCtx(benchmark::State &state)
Definition: bm_closure.cc:125
BM_TryAcquireMutex
static void BM_TryAcquireMutex(benchmark::State &state)
Definition: bm_closure.cc:193
BM_ClosureRun
static void BM_ClosureRun(benchmark::State &state)
Definition: bm_closure.cc:83
GRPC_COMBINER_UNREF
#define GRPC_COMBINER_UNREF(combiner, reason)
Definition: combiner.h:71
BM_ClosureSched4OnTwoCombiners
static void BM_ClosureSched4OnTwoCombiners(benchmark::State &state)
Definition: bm_closure.cc:324
grpc_closure
Definition: closure.h:56
grpc_core::Closure::Run
static void Run(const DebugLocation &location, grpc_closure *closure, grpc_error_handle error)
Definition: closure.h:250
grpc_core::ExecCtx::Get
static ExecCtx * Get()
Definition: exec_ctx.h:205
absl::hash_internal::c2
static const uint32_t c2
Definition: abseil-cpp/absl/hash/internal/city.cc:59


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:48