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