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