From 07b9077bfdcc14d27dd29b378d4fdf7914ebd28c Mon Sep 17 00:00:00 2001 From: Ghanan Gowripalan Date: Fri, 6 Oct 2023 13:01:43 -0700 Subject: [PATCH] Introduce poll_benchmark This benchmark performs similar measurements as epoll_benchmark. PiperOrigin-RevId: 571414062 --- test/perf/BUILD | 7 ++- test/perf/linux/BUILD | 21 +++++++- test/perf/linux/epoll_benchmark.cc | 22 ++------ test/perf/linux/poll_benchmark.cc | 80 ++++++++++++++++++++++++++++++ 4 files changed, 110 insertions(+), 20 deletions(-) create mode 100644 test/perf/linux/poll_benchmark.cc diff --git a/test/perf/BUILD b/test/perf/BUILD index 3c5bb3e4e..dff5241b5 100644 --- a/test/perf/BUILD +++ b/test/perf/BUILD @@ -1,5 +1,5 @@ -load("//tools:defs.bzl", "more_shards") load("//test/runner:defs.bzl", "syscall_test") +load("//tools:defs.bzl", "more_shards") package( default_applicable_licenses = ["//:license"], @@ -26,6 +26,11 @@ syscall_test( test = "//test/perf/linux:epoll_benchmark", ) +syscall_test( + debug = False, + test = "//test/perf/linux:poll_benchmark", +) + syscall_test( size = "large", debug = False, diff --git a/test/perf/linux/BUILD b/test/perf/linux/BUILD index bf7545cb8..1a7d1f062 100644 --- a/test/perf/linux/BUILD +++ b/test/perf/linux/BUILD @@ -260,10 +260,29 @@ cc_binary( gbenchmark, gtest, "//test/util:epoll_util", + "//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 = "poll_benchmark", + testonly = 1, + srcs = [ + "poll_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", - "//test/util:thread_util", "@com_google_absl//absl/time", ], ) diff --git a/test/perf/linux/epoll_benchmark.cc b/test/perf/linux/epoll_benchmark.cc index 0b121338a..d4d5fa1c3 100644 --- a/test/perf/linux/epoll_benchmark.cc +++ b/test/perf/linux/epoll_benchmark.cc @@ -13,38 +13,24 @@ // limitations under the License. #include -#include -#include -#include #include -#include -#include -#include +#include +#include "gmock/gmock.h" #include "gtest/gtest.h" -#include "absl/time/time.h" #include "benchmark/benchmark.h" #include "test/util/epoll_util.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" -#include "test/util/thread_util.h" namespace gvisor { namespace testing { namespace { -// Returns a new eventfd. -PosixErrorOr NewEventFD() { - int fd = eventfd(0, /* flags = */ 0); - MaybeSave(); - if (fd < 0) { - return PosixError(errno, "eventfd"); - } - return FileDescriptor(fd); -} - // Also stolen from epoll.cc unit tests. void BM_EpollTimeout(benchmark::State& state) { constexpr int kFDsPerEpoll = 3; diff --git a/test/perf/linux/poll_benchmark.cc b/test/perf/linux/poll_benchmark.cc new file mode 100644 index 000000000..0eec232b4 --- /dev/null +++ b/test/perf/linux/poll_benchmark.cc @@ -0,0 +1,80 @@ +// 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 { + +void Setup(int count, std::vector& event_fds, + std::vector& poll_fds) { + for (int i = 0; i < count; ++i) { + FileDescriptor fd = ASSERT_NO_ERRNO_AND_VALUE(NewEventFD()); + poll_fds.push_back(pollfd{.fd = fd.get(), .events = POLLIN}); + event_fds.push_back(std::move(fd)); + } +} + +void BM_PollTimeout(benchmark::State& state) { + constexpr int kFDsPerPoll = 3; + std::vector event_fds; + std::vector poll_fds; + ASSERT_NO_FATAL_FAILURE(Setup(kFDsPerPoll, event_fds, poll_fds)); + + const int timeout_ms = state.range(0); + for (auto _ : state) { + EXPECT_EQ(poll(poll_fds.data(), poll_fds.size(), timeout_ms), 0); + } +} + +BENCHMARK(BM_PollTimeout)->Range(/*start=*/0, /*limit=*/8); + +void BM_PollAllEvents(benchmark::State& state) { + const int fds_per_poll = state.range(0); + std::vector event_fds; + std::vector poll_fds; + ASSERT_NO_FATAL_FAILURE(Setup(fds_per_poll, event_fds, poll_fds)); + + constexpr uint64_t kEventVal = 5; + for (const auto& eventfd : event_fds) { + ASSERT_THAT(WriteFd(eventfd.get(), &kEventVal, sizeof(kEventVal)), + SyscallSucceedsWithValue(sizeof(kEventVal))); + } + + constexpr int kTimeoutMs = 0; + for (auto _ : state) { + EXPECT_EQ(poll(poll_fds.data(), poll_fds.size(), kTimeoutMs), fds_per_poll); + } +} + +BENCHMARK(BM_PollAllEvents)->Range(/*start=*/2, /*limit=*/1024); + +} // namespace + +} // namespace testing +} // namespace gvisor