bm_pollset.cc 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. /* Test out pollset latencies */
  19. #include <string.h>
  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/gpr/useful.h"
  25. #include "src/core/lib/gprpp/time.h"
  26. #include "src/core/lib/iomgr/ev_posix.h"
  27. #include "src/core/lib/iomgr/pollset.h"
  28. #include "src/core/lib/iomgr/port.h"
  29. #include "src/core/lib/iomgr/wakeup_fd_posix.h"
  30. #include "test/core/util/test_config.h"
  31. #include "test/cpp/microbenchmarks/helpers.h"
  32. #include "test/cpp/util/test_config.h"
  33. #ifdef GRPC_LINUX_MULTIPOLL_WITH_EPOLL
  34. #include <sys/epoll.h>
  35. #include <sys/eventfd.h>
  36. #include <unistd.h>
  37. #endif
  38. static void shutdown_ps(void* ps, grpc_error_handle /*error*/) {
  39. grpc_pollset_destroy(static_cast<grpc_pollset*>(ps));
  40. }
  41. static void BM_CreateDestroyPollset(benchmark::State& state) {
  42. TrackCounters track_counters;
  43. size_t ps_sz = grpc_pollset_size();
  44. grpc_pollset* ps = static_cast<grpc_pollset*>(gpr_malloc(ps_sz));
  45. gpr_mu* mu;
  46. grpc_core::ExecCtx exec_ctx;
  47. grpc_closure shutdown_ps_closure;
  48. GRPC_CLOSURE_INIT(&shutdown_ps_closure, shutdown_ps, ps,
  49. grpc_schedule_on_exec_ctx);
  50. for (auto _ : state) {
  51. memset(ps, 0, ps_sz);
  52. grpc_pollset_init(ps, &mu);
  53. gpr_mu_lock(mu);
  54. grpc_pollset_shutdown(ps, &shutdown_ps_closure);
  55. gpr_mu_unlock(mu);
  56. grpc_core::ExecCtx::Get()->Flush();
  57. }
  58. grpc_core::ExecCtx::Get()->Flush();
  59. gpr_free(ps);
  60. track_counters.Finish(state);
  61. }
  62. BENCHMARK(BM_CreateDestroyPollset);
  63. #ifdef GRPC_LINUX_MULTIPOLL_WITH_EPOLL
  64. static void BM_PollEmptyPollset_SpeedOfLight(benchmark::State& state) {
  65. // equivalent to BM_PollEmptyPollset, but just use the OS primitives to guage
  66. // what the speed of light would be if we abstracted perfectly
  67. TrackCounters track_counters;
  68. int epfd = epoll_create1(0);
  69. GPR_ASSERT(epfd != -1);
  70. size_t nev = state.range(0);
  71. size_t nfd = state.range(1);
  72. epoll_event* ev = new epoll_event[nev];
  73. std::vector<int> fds;
  74. for (size_t i = 0; i < nfd; i++) {
  75. fds.push_back(eventfd(0, 0));
  76. epoll_event ev;
  77. ev.events = EPOLLIN;
  78. epoll_ctl(epfd, EPOLL_CTL_ADD, fds.back(), &ev);
  79. }
  80. for (auto _ : state) {
  81. epoll_wait(epfd, ev, nev, 0);
  82. }
  83. for (auto fd : fds) {
  84. close(fd);
  85. }
  86. close(epfd);
  87. delete[] ev;
  88. track_counters.Finish(state);
  89. }
  90. BENCHMARK(BM_PollEmptyPollset_SpeedOfLight)
  91. ->Args({1, 0})
  92. ->Args({1, 1})
  93. ->Args({1, 10})
  94. ->Args({1, 100})
  95. ->Args({1, 1000})
  96. ->Args({1, 10000})
  97. ->Args({1, 100000})
  98. ->Args({10, 1})
  99. ->Args({100, 1})
  100. ->Args({1000, 1});
  101. #endif
  102. static void BM_PollEmptyPollset(benchmark::State& state) {
  103. TrackCounters track_counters;
  104. size_t ps_sz = grpc_pollset_size();
  105. grpc_pollset* ps = static_cast<grpc_pollset*>(gpr_zalloc(ps_sz));
  106. gpr_mu* mu;
  107. grpc_pollset_init(ps, &mu);
  108. grpc_core::ExecCtx exec_ctx;
  109. gpr_mu_lock(mu);
  110. for (auto _ : state) {
  111. GRPC_ERROR_UNREF(
  112. grpc_pollset_work(ps, nullptr, grpc_core::Timestamp::ProcessEpoch()));
  113. }
  114. grpc_closure shutdown_ps_closure;
  115. GRPC_CLOSURE_INIT(&shutdown_ps_closure, shutdown_ps, ps,
  116. grpc_schedule_on_exec_ctx);
  117. grpc_pollset_shutdown(ps, &shutdown_ps_closure);
  118. gpr_mu_unlock(mu);
  119. grpc_core::ExecCtx::Get()->Flush();
  120. gpr_free(ps);
  121. track_counters.Finish(state);
  122. }
  123. BENCHMARK(BM_PollEmptyPollset);
  124. static void BM_PollAddFd(benchmark::State& state) {
  125. TrackCounters track_counters;
  126. size_t ps_sz = grpc_pollset_size();
  127. grpc_pollset* ps = static_cast<grpc_pollset*>(gpr_zalloc(ps_sz));
  128. gpr_mu* mu;
  129. grpc_pollset_init(ps, &mu);
  130. grpc_core::ExecCtx exec_ctx;
  131. grpc_wakeup_fd wakeup_fd;
  132. GPR_ASSERT(
  133. GRPC_LOG_IF_ERROR("wakeup_fd_init", grpc_wakeup_fd_init(&wakeup_fd)));
  134. grpc_fd* fd = grpc_fd_create(wakeup_fd.read_fd, "xxx", false);
  135. for (auto _ : state) {
  136. grpc_pollset_add_fd(ps, fd);
  137. grpc_core::ExecCtx::Get()->Flush();
  138. }
  139. grpc_fd_orphan(fd, nullptr, nullptr, "xxx");
  140. grpc_closure shutdown_ps_closure;
  141. GRPC_CLOSURE_INIT(&shutdown_ps_closure, shutdown_ps, ps,
  142. grpc_schedule_on_exec_ctx);
  143. gpr_mu_lock(mu);
  144. grpc_pollset_shutdown(ps, &shutdown_ps_closure);
  145. gpr_mu_unlock(mu);
  146. grpc_core::ExecCtx::Get()->Flush();
  147. gpr_free(ps);
  148. track_counters.Finish(state);
  149. }
  150. BENCHMARK(BM_PollAddFd);
  151. class TestClosure : public grpc_closure {
  152. public:
  153. virtual ~TestClosure() {}
  154. };
  155. template <class F>
  156. TestClosure* MakeTestClosure(F f) {
  157. struct C : public TestClosure {
  158. explicit C(F f) : f_(f) { GRPC_CLOSURE_INIT(this, C::cbfn, this, nullptr); }
  159. static void cbfn(void* arg, grpc_error_handle /*error*/) {
  160. C* p = static_cast<C*>(arg);
  161. p->f_();
  162. }
  163. F f_;
  164. };
  165. return new C(f);
  166. }
  167. #ifdef GRPC_LINUX_MULTIPOLL_WITH_EPOLL
  168. static void BM_SingleThreadPollOneFd_SpeedOfLight(benchmark::State& state) {
  169. // equivalent to BM_PollEmptyPollset, but just use the OS primitives to guage
  170. // what the speed of light would be if we abstracted perfectly
  171. TrackCounters track_counters;
  172. int epfd = epoll_create1(0);
  173. GPR_ASSERT(epfd != -1);
  174. epoll_event ev[100];
  175. int fd = eventfd(0, EFD_NONBLOCK);
  176. ev[0].events = EPOLLIN;
  177. epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev[0]);
  178. for (auto _ : state) {
  179. int err;
  180. do {
  181. err = eventfd_write(fd, 1);
  182. } while (err < 0 && errno == EINTR);
  183. GPR_ASSERT(err == 0);
  184. do {
  185. err = epoll_wait(epfd, ev, GPR_ARRAY_SIZE(ev), 0);
  186. } while (err < 0 && errno == EINTR);
  187. GPR_ASSERT(err == 1);
  188. eventfd_t value;
  189. do {
  190. err = eventfd_read(fd, &value);
  191. } while (err < 0 && errno == EINTR);
  192. GPR_ASSERT(err == 0);
  193. }
  194. close(fd);
  195. close(epfd);
  196. track_counters.Finish(state);
  197. }
  198. BENCHMARK(BM_SingleThreadPollOneFd_SpeedOfLight);
  199. #endif
  200. static void BM_SingleThreadPollOneFd(benchmark::State& state) {
  201. TrackCounters track_counters;
  202. size_t ps_sz = grpc_pollset_size();
  203. grpc_pollset* ps = static_cast<grpc_pollset*>(gpr_zalloc(ps_sz));
  204. gpr_mu* mu;
  205. grpc_pollset_init(ps, &mu);
  206. grpc_core::ExecCtx exec_ctx;
  207. grpc_wakeup_fd wakeup_fd;
  208. GRPC_ERROR_UNREF(grpc_wakeup_fd_init(&wakeup_fd));
  209. grpc_fd* wakeup = grpc_fd_create(wakeup_fd.read_fd, "wakeup_read", false);
  210. grpc_pollset_add_fd(ps, wakeup);
  211. bool done = false;
  212. TestClosure* continue_closure = MakeTestClosure([&]() {
  213. GRPC_ERROR_UNREF(grpc_wakeup_fd_consume_wakeup(&wakeup_fd));
  214. if (!state.KeepRunning()) {
  215. done = true;
  216. return;
  217. }
  218. GRPC_ERROR_UNREF(grpc_wakeup_fd_wakeup(&wakeup_fd));
  219. grpc_fd_notify_on_read(wakeup, continue_closure);
  220. });
  221. GRPC_ERROR_UNREF(grpc_wakeup_fd_wakeup(&wakeup_fd));
  222. grpc_fd_notify_on_read(wakeup, continue_closure);
  223. gpr_mu_lock(mu);
  224. while (!done) {
  225. GRPC_ERROR_UNREF(
  226. grpc_pollset_work(ps, nullptr, grpc_core::Timestamp::InfFuture()));
  227. }
  228. grpc_fd_orphan(wakeup, nullptr, nullptr, "done");
  229. wakeup_fd.read_fd = 0;
  230. grpc_closure shutdown_ps_closure;
  231. GRPC_CLOSURE_INIT(&shutdown_ps_closure, shutdown_ps, ps,
  232. grpc_schedule_on_exec_ctx);
  233. grpc_pollset_shutdown(ps, &shutdown_ps_closure);
  234. gpr_mu_unlock(mu);
  235. grpc_core::ExecCtx::Get()->Flush();
  236. grpc_wakeup_fd_destroy(&wakeup_fd);
  237. gpr_free(ps);
  238. track_counters.Finish(state);
  239. delete continue_closure;
  240. }
  241. BENCHMARK(BM_SingleThreadPollOneFd);
  242. // Some distros have RunSpecifiedBenchmarks under the benchmark namespace,
  243. // and others do not. This allows us to support both modes.
  244. namespace benchmark {
  245. void RunTheBenchmarksNamespaced() { RunSpecifiedBenchmarks(); }
  246. } // namespace benchmark
  247. int main(int argc, char** argv) {
  248. grpc::testing::TestEnvironment env(argc, argv);
  249. LibraryInitializer libInit;
  250. ::benchmark::Initialize(&argc, argv);
  251. grpc::testing::InitTest(&argc, &argv, false);
  252. benchmark::RunTheBenchmarksNamespaced();
  253. return 0;
  254. }