1e6c84f82SJakob Johnson //===-- PerfTests.cpp -----------------------------------------------------===//
2e6c84f82SJakob Johnson //
3e6c84f82SJakob Johnson // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6c84f82SJakob Johnson // See https://llvm.org/LICENSE.txt for license information.
5e6c84f82SJakob Johnson // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e6c84f82SJakob Johnson //
7e6c84f82SJakob Johnson //===----------------------------------------------------------------------===//
8e6c84f82SJakob Johnson 
9b97b082cSWalter Erquinigo #ifdef __x86_64__
10b97b082cSWalter Erquinigo 
11e6c84f82SJakob Johnson #include "Perf.h"
12e6c84f82SJakob Johnson 
13e6c84f82SJakob Johnson #include "llvm/Support/Error.h"
14e6c84f82SJakob Johnson 
15e6c84f82SJakob Johnson #include "gtest/gtest.h"
165de0a3e9SWalter Erquinigo 
17e6c84f82SJakob Johnson #include <chrono>
18e6c84f82SJakob Johnson #include <cstdint>
19e6c84f82SJakob Johnson 
20e6c84f82SJakob Johnson using namespace lldb_private;
21e6c84f82SJakob Johnson using namespace process_linux;
22e6c84f82SJakob Johnson using namespace llvm;
23e6c84f82SJakob Johnson 
24e6c84f82SJakob Johnson /// Helper function to read current TSC value.
25e6c84f82SJakob Johnson ///
26e6c84f82SJakob Johnson /// This code is based on llvm/xray.
readTsc()27e6c84f82SJakob Johnson static Expected<uint64_t> readTsc() {
28e6c84f82SJakob Johnson 
29e6c84f82SJakob Johnson   unsigned int eax, ebx, ecx, edx;
30e6c84f82SJakob Johnson 
31e6c84f82SJakob Johnson   // We check whether rdtscp support is enabled. According to the x86_64 manual,
32e6c84f82SJakob Johnson   // level should be set at 0x80000001, and we should have a look at bit 27 in
33e6c84f82SJakob Johnson   // EDX. That's 0x8000000 (or 1u << 27).
34e6c84f82SJakob Johnson   __asm__ __volatile__("cpuid"
35e6c84f82SJakob Johnson                        : "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx)
36e6c84f82SJakob Johnson                        : "0"(0x80000001));
37e6c84f82SJakob Johnson   if (!(edx & (1u << 27))) {
38e6c84f82SJakob Johnson     return createStringError(inconvertibleErrorCode(),
39e6c84f82SJakob Johnson                              "Missing rdtscp support.");
40e6c84f82SJakob Johnson   }
41e6c84f82SJakob Johnson 
42e6c84f82SJakob Johnson   unsigned cpu;
43e6c84f82SJakob Johnson   unsigned long rax, rdx;
44e6c84f82SJakob Johnson 
45e6c84f82SJakob Johnson   __asm__ __volatile__("rdtscp\n" : "=a"(rax), "=d"(rdx), "=c"(cpu)::);
46e6c84f82SJakob Johnson 
47e6c84f82SJakob Johnson   return (rdx << 32) + rax;
48e6c84f82SJakob Johnson }
49e6c84f82SJakob Johnson 
50e6c84f82SJakob Johnson // Test TSC to walltime conversion based on perf conversion values.
TEST(Perf,TscConversion)51e6c84f82SJakob Johnson TEST(Perf, TscConversion) {
52e6c84f82SJakob Johnson   // This test works by first reading the TSC value directly before
53e6c84f82SJakob Johnson   // and after sleeping, then converting these values to nanoseconds, and
54e6c84f82SJakob Johnson   // finally ensuring the difference is approximately equal to the sleep time.
55e6c84f82SJakob Johnson   //
56e6c84f82SJakob Johnson   // There will be slight overhead associated with the sleep call, so it isn't
57e6c84f82SJakob Johnson   // reasonable to expect the difference to be exactly equal to the sleep time.
58e6c84f82SJakob Johnson 
59e6c84f82SJakob Johnson   const int SLEEP_SECS = 1;
60e6c84f82SJakob Johnson   std::chrono::nanoseconds SLEEP_NANOS{std::chrono::seconds(SLEEP_SECS)};
61e6c84f82SJakob Johnson 
629b79187cSJakob Johnson   Expected<LinuxPerfZeroTscConversion> params =
639b79187cSJakob Johnson       LoadPerfTscConversionParameters();
64e6c84f82SJakob Johnson 
65e6c84f82SJakob Johnson   // Skip the test if the conversion parameters aren't available.
66e6c84f82SJakob Johnson   if (!params)
67d1375285SJakob Johnson     GTEST_SKIP() << toString(params.takeError());
68e6c84f82SJakob Johnson 
69e6c84f82SJakob Johnson   Expected<uint64_t> tsc_before_sleep = readTsc();
70e6c84f82SJakob Johnson   sleep(SLEEP_SECS);
71e6c84f82SJakob Johnson   Expected<uint64_t> tsc_after_sleep = readTsc();
72e6c84f82SJakob Johnson 
73e6c84f82SJakob Johnson   // Skip the test if we are unable to read the TSC value.
74e6c84f82SJakob Johnson   if (!tsc_before_sleep)
75d1375285SJakob Johnson     GTEST_SKIP() << toString(tsc_before_sleep.takeError());
76e6c84f82SJakob Johnson   if (!tsc_after_sleep)
77d1375285SJakob Johnson     GTEST_SKIP() << toString(tsc_after_sleep.takeError());
78e6c84f82SJakob Johnson 
79*a19fcc2bSWalter Erquinigo   uint64_t converted_tsc_diff =
801f2d49a8SWalter Erquinigo       params->ToNanos(*tsc_after_sleep) - params->ToNanos(*tsc_before_sleep);
81e6c84f82SJakob Johnson 
82e6c84f82SJakob Johnson   std::chrono::microseconds acceptable_overhead(500);
83e6c84f82SJakob Johnson 
84*a19fcc2bSWalter Erquinigo   ASSERT_GE(converted_tsc_diff, static_cast<uint64_t>(SLEEP_NANOS.count()));
85*a19fcc2bSWalter Erquinigo   ASSERT_LT(converted_tsc_diff,
86*a19fcc2bSWalter Erquinigo             static_cast<uint64_t>((SLEEP_NANOS + acceptable_overhead).count()));
87e6c84f82SJakob Johnson }
88b97b082cSWalter Erquinigo 
89b97b082cSWalter Erquinigo #endif // __x86_64__
90