gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/test/perf/linux/poll_benchmark.cc (about) 1 // Copyright 2023 The gVisor Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #include <sys/poll.h> 16 17 #include <cstdint> 18 #include <utility> 19 #include <vector> 20 21 #include "gmock/gmock.h" 22 #include "gtest/gtest.h" 23 #include "benchmark/benchmark.h" 24 #include "test/util/eventfd_util.h" 25 #include "test/util/file_descriptor.h" 26 #include "test/util/posix_error.h" 27 #include "test/util/test_util.h" 28 29 namespace gvisor { 30 namespace testing { 31 32 namespace { 33 34 void Setup(int count, std::vector<FileDescriptor>& event_fds, 35 std::vector<pollfd>& poll_fds) { 36 for (int i = 0; i < count; ++i) { 37 FileDescriptor fd = ASSERT_NO_ERRNO_AND_VALUE(NewEventFD()); 38 poll_fds.push_back(pollfd{.fd = fd.get(), .events = POLLIN}); 39 event_fds.push_back(std::move(fd)); 40 } 41 } 42 43 void BM_PollTimeout(benchmark::State& state) { 44 constexpr int kFDsPerPoll = 3; 45 std::vector<FileDescriptor> event_fds; 46 std::vector<pollfd> poll_fds; 47 ASSERT_NO_FATAL_FAILURE(Setup(kFDsPerPoll, event_fds, poll_fds)); 48 49 const int timeout_ms = state.range(0); 50 for (auto _ : state) { 51 EXPECT_EQ(poll(poll_fds.data(), poll_fds.size(), timeout_ms), 0); 52 } 53 } 54 55 BENCHMARK(BM_PollTimeout)->Range(/*start=*/0, /*limit=*/8); 56 57 void BM_PollAllEvents(benchmark::State& state) { 58 const int fds_per_poll = state.range(0); 59 std::vector<FileDescriptor> event_fds; 60 std::vector<pollfd> poll_fds; 61 ASSERT_NO_FATAL_FAILURE(Setup(fds_per_poll, event_fds, poll_fds)); 62 63 constexpr uint64_t kEventVal = 5; 64 for (const auto& eventfd : event_fds) { 65 ASSERT_THAT(WriteFd(eventfd.get(), &kEventVal, sizeof(kEventVal)), 66 SyscallSucceedsWithValue(sizeof(kEventVal))); 67 } 68 69 constexpr int kTimeoutMs = 0; 70 for (auto _ : state) { 71 EXPECT_EQ(poll(poll_fds.data(), poll_fds.size(), kTimeoutMs), fds_per_poll); 72 } 73 } 74 75 BENCHMARK(BM_PollAllEvents)->Range(/*start=*/2, /*limit=*/1024); 76 77 } // namespace 78 79 } // namespace testing 80 } // namespace gvisor