From 12bc9994b427b60befac8bfd6a0c062963dc57e7 Mon Sep 17 00:00:00 2001 From: Ghanan Gowripalan Date: Mon, 9 Oct 2023 10:15:33 -0700 Subject: [PATCH] Introduce select_benchmark This benchmark performs similar measurements as epoll_benchmark. PiperOrigin-RevId: 571972206 --- test/perf/BUILD | 5 ++ test/perf/linux/BUILD | 18 +++++ test/perf/linux/select_benchmark.cc | 100 ++++++++++++++++++++++++++++ 3 files changed, 123 insertions(+) create mode 100644 test/perf/linux/select_benchmark.cc diff --git a/test/perf/BUILD b/test/perf/BUILD index dff5241b5..2fb269307 100644 --- a/test/perf/BUILD +++ b/test/perf/BUILD @@ -31,6 +31,11 @@ syscall_test( test = "//test/perf/linux:poll_benchmark", ) +syscall_test( + debug = False, + test = "//test/perf/linux:select_benchmark", +) + syscall_test( size = "large", debug = False, diff --git a/test/perf/linux/BUILD b/test/perf/linux/BUILD index 1a7d1f062..a4a6502a0 100644 --- a/test/perf/linux/BUILD +++ b/test/perf/linux/BUILD @@ -287,6 +287,24 @@ cc_binary( ], ) +cc_binary( + name = "select_benchmark", + testonly = 1, + srcs = [ + "select_benchmark.cc", + ], + deps = [ + gbenchmark, + gtest, + "//test/util:eventfd_util", + "//test/util:file_descriptor", + "//test/util:posix_error", + "//test/util:test_main", + "//test/util:test_util", + "@com_google_absl//absl/time", + ], +) + cc_binary( name = "death_benchmark", testonly = 1, diff --git a/test/perf/linux/select_benchmark.cc b/test/perf/linux/select_benchmark.cc new file mode 100644 index 000000000..1b2a92ae6 --- /dev/null +++ b/test/perf/linux/select_benchmark.cc @@ -0,0 +1,100 @@ +// Copyright 2023 The gVisor Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include +#include +#include + +#include "gmock/gmock.h" +#include "gtest/gtest.h" +#include "benchmark/benchmark.h" +#include "test/util/eventfd_util.h" +#include "test/util/file_descriptor.h" +#include "test/util/posix_error.h" +#include "test/util/test_util.h" + +namespace gvisor { +namespace testing { + +namespace { + +// Populates |event_fds| with new event FDs and |read_fds| with each new event +// FD. +void Setup(int count, std::vector& event_fds, fd_set& read_fds, + int& max_fd) { + max_fd = -1; + for (int i = 0; i < count; ++i) { + FileDescriptor fd = ASSERT_NO_ERRNO_AND_VALUE(NewEventFD()); + if (fd.get() > max_fd) { + max_fd = fd.get(); + } + FD_SET(fd.get(), &read_fds); + event_fds.push_back(std::move(fd)); + } +} + +// Benchmarks a call to select(2) when no FD is "ready" with varying timeout +// values. +void BM_SelectTimeout(benchmark::State& state) { + constexpr int kFDsPerSelect = 3; + std::vector event_fds; + fd_set read_fds; + int max_fd; + ASSERT_NO_FATAL_FAILURE(Setup(kFDsPerSelect, event_fds, read_fds, max_fd)); + + const int timeout_ms = state.range(0); + timeval timeout = { + .tv_sec = 0, + .tv_usec = timeout_ms * 1000, + }; + for (auto _ : state) { + EXPECT_EQ(select(max_fd + 1, &read_fds, /*writefds=*/nullptr, + /*exceptfds=*/nullptr, &timeout), + 0); + } +} + +BENCHMARK(BM_SelectTimeout)->Range(/*start=*/0, /*limit=*/8); + +// Benchmarks a call to select(2) with a zero timeout and varying number of +// "ready" FDs. +void BM_SelectAllEvents(benchmark::State& state) { + const int fds_per_select = state.range(0); + std::vector event_fds; + fd_set read_fds; + int max_fd; + ASSERT_NO_FATAL_FAILURE(Setup(fds_per_select, event_fds, read_fds, max_fd)); + + constexpr uint64_t kEventVal = 5; + for (const auto& eventfd : event_fds) { + ASSERT_THAT(WriteFd(eventfd.get(), &kEventVal, sizeof(kEventVal)), + SyscallSucceedsWithValue(sizeof(kEventVal))); + } + + timeval timeout = {}; + for (auto _ : state) { + EXPECT_EQ(select(max_fd + 1, &read_fds, /*writefds=*/nullptr, + /*exceptfds=*/nullptr, &timeout), + fds_per_select); + } +} + +BENCHMARK(BM_SelectAllEvents)->Range(/*start=*/2, /*limit=*/1024); + +} // namespace + +} // namespace testing +} // namespace gvisor