gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/test/perf/linux/select_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/select.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  // Populates |event_fds| with new event FDs and |read_fds| with each new event
    35  // FD.
    36  void Setup(int count, std::vector<FileDescriptor>& event_fds, fd_set& read_fds,
    37             int& max_fd) {
    38    max_fd = -1;
    39    FD_ZERO(&read_fds);
    40  
    41    for (int i = 0; i < count; ++i) {
    42      FileDescriptor fd = ASSERT_NO_ERRNO_AND_VALUE(NewEventFD());
    43      if (fd.get() > max_fd) {
    44        max_fd = fd.get();
    45      }
    46      FD_SET(fd.get(), &read_fds);
    47      event_fds.push_back(std::move(fd));
    48    }
    49  
    50    ASSERT_LT(max_fd, FD_SETSIZE);
    51  }
    52  
    53  // Benchmarks a call to select(2) when no FD is "ready" with varying timeout
    54  // values.
    55  void BM_SelectTimeout(benchmark::State& state) {
    56    constexpr int kFDsPerSelect = 3;
    57    std::vector<FileDescriptor> event_fds;
    58    fd_set read_fds;
    59    int max_fd;
    60    ASSERT_NO_FATAL_FAILURE(Setup(kFDsPerSelect, event_fds, read_fds, max_fd));
    61  
    62    const int timeout_ms = state.range(0);
    63    timeval timeout = {
    64        .tv_sec = 0,
    65        .tv_usec = timeout_ms * 1000,
    66    };
    67    for (auto _ : state) {
    68      EXPECT_EQ(select(max_fd + 1, &read_fds, /*writefds=*/nullptr,
    69                       /*exceptfds=*/nullptr, &timeout),
    70                0);
    71    }
    72  }
    73  
    74  BENCHMARK(BM_SelectTimeout)->Range(/*start=*/0, /*limit=*/8);
    75  
    76  // Benchmarks a call to select(2) with a zero timeout and varying number of
    77  // "ready" FDs.
    78  void BM_SelectAllEvents(benchmark::State& state) {
    79    const int fds_per_select = state.range(0);
    80    std::vector<FileDescriptor> event_fds;
    81    fd_set read_fds;
    82    int max_fd;
    83    ASSERT_NO_FATAL_FAILURE(Setup(fds_per_select, event_fds, read_fds, max_fd));
    84  
    85    constexpr uint64_t kEventVal = 5;
    86    for (const auto& eventfd : event_fds) {
    87      ASSERT_THAT(WriteFd(eventfd.get(), &kEventVal, sizeof(kEventVal)),
    88                  SyscallSucceedsWithValue(sizeof(kEventVal)));
    89    }
    90  
    91    timeval timeout = {};
    92    for (auto _ : state) {
    93      EXPECT_EQ(select(max_fd + 1, &read_fds, /*writefds=*/nullptr,
    94                       /*exceptfds=*/nullptr, &timeout),
    95                fds_per_select);
    96    }
    97  }
    98  
    99  BENCHMARK(BM_SelectAllEvents)->Range(/*start=*/2, /*limit=*/512);
   100  
   101  }  // namespace
   102  
   103  }  // namespace testing
   104  }  // namespace gvisor