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