gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/test/perf/linux/clock_gettime_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 <pthread.h> 16 #include <time.h> 17 18 #include "gtest/gtest.h" 19 #include "absl/time/clock.h" 20 #include "absl/time/time.h" 21 #include "benchmark/benchmark.h" 22 23 namespace gvisor { 24 namespace testing { 25 26 namespace { 27 28 void BM_ClockGettimeThreadCPUTime(benchmark::State& state) { 29 clockid_t clockid; 30 ASSERT_EQ(0, pthread_getcpuclockid(pthread_self(), &clockid)); 31 struct timespec tp; 32 33 for (auto _ : state) { 34 clock_gettime(clockid, &tp); 35 } 36 } 37 38 BENCHMARK(BM_ClockGettimeThreadCPUTime); 39 40 void BM_VDSOClockGettime(benchmark::State& state) { 41 const clockid_t clock = state.range(0); 42 struct timespec tp; 43 absl::Time start = absl::Now(); 44 45 // Don't benchmark the calibration phase. 46 while (absl::Now() < start + absl::Milliseconds(2100)) { 47 clock_gettime(clock, &tp); 48 } 49 50 for (auto _ : state) { 51 clock_gettime(clock, &tp); 52 } 53 } 54 55 BENCHMARK(BM_VDSOClockGettime)->Arg(CLOCK_MONOTONIC)->Arg(CLOCK_REALTIME); 56 57 } // namespace 58 59 } // namespace testing 60 } // namespace gvisor