bm_cq_multiple_threads.cc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. #include <string.h>
  19. #include <atomic>
  20. #include <benchmark/benchmark.h>
  21. #include <grpc/grpc.h>
  22. #include <grpc/support/alloc.h>
  23. #include <grpc/support/log.h>
  24. #include "src/core/lib/gprpp/time.h"
  25. #include "src/core/lib/iomgr/ev_posix.h"
  26. #include "src/core/lib/iomgr/port.h"
  27. #include "src/core/lib/surface/completion_queue.h"
  28. #include "test/core/util/test_config.h"
  29. #include "test/cpp/microbenchmarks/helpers.h"
  30. #include "test/cpp/util/test_config.h"
  31. struct grpc_pollset {
  32. gpr_mu mu;
  33. };
  34. static gpr_mu g_mu;
  35. static gpr_cv g_cv;
  36. static int g_threads_active;
  37. static bool g_active;
  38. namespace grpc {
  39. namespace testing {
  40. static grpc_completion_queue* g_cq;
  41. static grpc_event_engine_vtable g_vtable;
  42. static void pollset_shutdown(grpc_pollset* /*ps*/, grpc_closure* closure) {
  43. grpc_core::ExecCtx::Run(DEBUG_LOCATION, closure, GRPC_ERROR_NONE);
  44. }
  45. static void pollset_init(grpc_pollset* ps, gpr_mu** mu) {
  46. gpr_mu_init(&ps->mu);
  47. *mu = &ps->mu;
  48. }
  49. static void pollset_destroy(grpc_pollset* ps) { gpr_mu_destroy(&ps->mu); }
  50. static grpc_error_handle pollset_kick(grpc_pollset* /*p*/,
  51. grpc_pollset_worker* /*worker*/) {
  52. return GRPC_ERROR_NONE;
  53. }
  54. /* Callback when the tag is dequeued from the completion queue. Does nothing */
  55. static void cq_done_cb(void* /*done_arg*/, grpc_cq_completion* cq_completion) {
  56. gpr_free(cq_completion);
  57. }
  58. /* Queues a completion tag if deadline is > 0.
  59. * Does nothing if deadline is 0 (i.e gpr_time_0(GPR_CLOCK_MONOTONIC)) */
  60. static grpc_error_handle pollset_work(grpc_pollset* ps,
  61. grpc_pollset_worker** /*worker*/,
  62. grpc_core::Timestamp deadline) {
  63. if (deadline == grpc_core::Timestamp::ProcessEpoch()) {
  64. gpr_log(GPR_DEBUG, "no-op");
  65. return GRPC_ERROR_NONE;
  66. }
  67. gpr_mu_unlock(&ps->mu);
  68. void* tag = reinterpret_cast<void*>(10); // Some random number
  69. GPR_ASSERT(grpc_cq_begin_op(g_cq, tag));
  70. grpc_cq_end_op(
  71. g_cq, tag, GRPC_ERROR_NONE, cq_done_cb, nullptr,
  72. static_cast<grpc_cq_completion*>(gpr_malloc(sizeof(grpc_cq_completion))));
  73. grpc_core::ExecCtx::Get()->Flush();
  74. gpr_mu_lock(&ps->mu);
  75. return GRPC_ERROR_NONE;
  76. }
  77. static const grpc_event_engine_vtable* init_engine_vtable(bool) {
  78. memset(&g_vtable, 0, sizeof(g_vtable));
  79. g_vtable.pollset_size = sizeof(grpc_pollset);
  80. g_vtable.pollset_init = pollset_init;
  81. g_vtable.pollset_shutdown = pollset_shutdown;
  82. g_vtable.pollset_destroy = pollset_destroy;
  83. g_vtable.pollset_work = pollset_work;
  84. g_vtable.pollset_kick = pollset_kick;
  85. g_vtable.is_any_background_poller_thread = [] { return false; };
  86. g_vtable.add_closure_to_background_poller = [](grpc_closure* /*closure*/,
  87. grpc_error_handle /*error*/) {
  88. return false;
  89. };
  90. g_vtable.shutdown_background_closure = [] {};
  91. g_vtable.shutdown_engine = [] {};
  92. return &g_vtable;
  93. }
  94. static void setup() {
  95. // This test should only ever be run with a non or any polling engine
  96. // Override the polling engine for the non-polling engine
  97. // and add a custom polling engine
  98. grpc_register_event_engine_factory("none", init_engine_vtable, false);
  99. grpc_register_event_engine_factory("bm_cq_multiple_threads",
  100. init_engine_vtable, true);
  101. grpc_init();
  102. GPR_ASSERT(strcmp(grpc_get_poll_strategy_name(), "none") == 0 ||
  103. strcmp(grpc_get_poll_strategy_name(), "bm_cq_multiple_threads") ==
  104. 0);
  105. g_cq = grpc_completion_queue_create_for_next(nullptr);
  106. }
  107. static void teardown() {
  108. grpc_completion_queue_shutdown(g_cq);
  109. /* Drain any events */
  110. gpr_timespec deadline = gpr_time_0(GPR_CLOCK_MONOTONIC);
  111. while (grpc_completion_queue_next(g_cq, deadline, nullptr).type !=
  112. GRPC_QUEUE_SHUTDOWN) {
  113. /* Do nothing */
  114. }
  115. grpc_completion_queue_destroy(g_cq);
  116. grpc_shutdown();
  117. }
  118. /* A few notes about Multi-threaded benchmarks:
  119. Setup:
  120. The benchmark framework ensures that none of the threads proceed beyond the
  121. state.KeepRunning() call unless all the threads have called state.keepRunning
  122. at least once. So it is safe to do the initialization in one of the threads
  123. before state.KeepRunning() is called.
  124. Teardown:
  125. The benchmark framework also ensures that no thread is running the benchmark
  126. code (i.e the code between two successive calls of state.KeepRunning()) if
  127. state.KeepRunning() returns false. So it is safe to do the teardown in one
  128. of the threads after state.keepRunning() returns false.
  129. However, our use requires synchronization because we do additional work at
  130. each thread that requires specific ordering (TrackCounters must be constructed
  131. after grpc_init because it needs the number of cores, initialized by grpc,
  132. and its Finish call must take place before grpc_shutdown so that it can use
  133. grpc_stats).
  134. */
  135. static void BM_Cq_Throughput(benchmark::State& state) {
  136. gpr_timespec deadline = gpr_inf_future(GPR_CLOCK_MONOTONIC);
  137. auto thd_idx = state.thread_index();
  138. gpr_mu_lock(&g_mu);
  139. g_threads_active++;
  140. if (thd_idx == 0) {
  141. setup();
  142. g_active = true;
  143. gpr_cv_broadcast(&g_cv);
  144. } else {
  145. while (!g_active) {
  146. gpr_cv_wait(&g_cv, &g_mu, deadline);
  147. }
  148. }
  149. gpr_mu_unlock(&g_mu);
  150. // Use a TrackCounters object to monitor the gRPC performance statistics
  151. // (optionally including low-level counters) before and after the test
  152. TrackCounters track_counters;
  153. for (auto _ : state) {
  154. GPR_ASSERT(grpc_completion_queue_next(g_cq, deadline, nullptr).type ==
  155. GRPC_OP_COMPLETE);
  156. }
  157. state.SetItemsProcessed(state.iterations());
  158. track_counters.Finish(state);
  159. gpr_mu_lock(&g_mu);
  160. g_threads_active--;
  161. if (g_threads_active == 0) {
  162. gpr_cv_broadcast(&g_cv);
  163. } else {
  164. while (g_threads_active > 0) {
  165. gpr_cv_wait(&g_cv, &g_mu, deadline);
  166. }
  167. }
  168. gpr_mu_unlock(&g_mu);
  169. if (thd_idx == 0) {
  170. teardown();
  171. g_active = false;
  172. }
  173. }
  174. BENCHMARK(BM_Cq_Throughput)->ThreadRange(1, 16)->UseRealTime();
  175. } // namespace testing
  176. } // namespace grpc
  177. // Some distros have RunSpecifiedBenchmarks under the benchmark namespace,
  178. // and others do not. This allows us to support both modes.
  179. namespace benchmark {
  180. void RunTheBenchmarksNamespaced() { RunSpecifiedBenchmarks(); }
  181. } // namespace benchmark
  182. int main(int argc, char** argv) {
  183. grpc::testing::TestEnvironment env(argc, argv);
  184. gpr_mu_init(&g_mu);
  185. gpr_cv_init(&g_cv);
  186. ::benchmark::Initialize(&argc, argv);
  187. grpc::testing::InitTest(&argc, &argv, false);
  188. benchmark::RunTheBenchmarksNamespaced();
  189. return 0;
  190. }