gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/test/perf/linux/signal_benchmark.cc (about)

     1  // Copyright 2020 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 <signal.h>
    16  #include <string.h>
    17  
    18  #include "gtest/gtest.h"
    19  #include "benchmark/benchmark.h"
    20  #include "test/util/logging.h"
    21  #include "test/util/test_util.h"
    22  
    23  namespace gvisor {
    24  namespace testing {
    25  
    26  namespace {
    27  
    28  #ifdef __x86_64__
    29  
    30  void FixupHandler(int sig, siginfo_t* si, void* void_ctx) {
    31    static unsigned int dataval = 0;
    32  
    33    // Skip the offending instruction.
    34    ucontext_t* ctx = reinterpret_cast<ucontext_t*>(void_ctx);
    35    ctx->uc_mcontext.gregs[REG_RAX] = reinterpret_cast<greg_t>(&dataval);
    36  }
    37  
    38  void BM_FaultSignalFixup(benchmark::State& state) {
    39    // Set up the signal handler.
    40    struct sigaction sa = {};
    41    sigemptyset(&sa.sa_mask);
    42    sa.sa_sigaction = FixupHandler;
    43    sa.sa_flags = SA_SIGINFO;
    44    TEST_CHECK(sigaction(SIGSEGV, &sa, nullptr) == 0);
    45  
    46    // Fault, fault, fault.
    47    for (auto _ : state) {
    48      // Trigger the segfault.
    49      asm volatile(
    50          "movq $0, %%rax\n"
    51          "movq $0x77777777, (%%rax)\n"
    52          :
    53          :
    54          : "rax");
    55    }
    56  }
    57  
    58  BENCHMARK(BM_FaultSignalFixup)->UseRealTime();
    59  
    60  #endif  // __x86_64__
    61  
    62  }  // namespace
    63  
    64  }  // namespace testing
    65  }  // namespace gvisor