xref: /freebsd/contrib/llvm-project/compiler-rt/lib/xray/xray_tsc.h (revision c66ec88fed842fbaad62c30d510644ceb7bd2d71)
1 //===-- xray_tsc.h ----------------------------------------------*- C++ -*-===//
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 // This file is a part of XRay, a dynamic runtime instrumentation system.
10 //
11 //===----------------------------------------------------------------------===//
12 #ifndef XRAY_EMULATE_TSC_H
13 #define XRAY_EMULATE_TSC_H
14 
15 #include "sanitizer_common/sanitizer_common.h"
16 
17 namespace __xray {
18 static constexpr uint64_t NanosecondsPerSecond = 1000ULL * 1000 * 1000;
19 }
20 
21 #if SANITIZER_FUCHSIA
22 #include <zircon/syscalls.h>
23 
24 namespace __xray {
25 
26 inline bool probeRequiredCPUFeatures() XRAY_NEVER_INSTRUMENT { return true; }
27 
28 ALWAYS_INLINE uint64_t readTSC(uint8_t &CPU) XRAY_NEVER_INSTRUMENT {
29   CPU = 0;
30   return _zx_ticks_get();
31 }
32 
33 inline uint64_t getTSCFrequency() XRAY_NEVER_INSTRUMENT {
34   return _zx_ticks_per_second();
35 }
36 
37 } // namespace __xray
38 
39 #else // SANITIZER_FUCHSIA
40 
41 #if defined(__x86_64__)
42 #include "xray_x86_64.inc"
43 #elif defined(__powerpc64__)
44 #include "xray_powerpc64.inc"
45 #elif defined(__arm__) || defined(__aarch64__) || defined(__mips__)
46 // Emulated TSC.
47 // There is no instruction like RDTSCP in user mode on ARM. ARM's CP15 does
48 //   not have a constant frequency like TSC on x86(_64), it may go faster
49 //   or slower depending on CPU turbo or power saving mode. Furthermore,
50 //   to read from CP15 on ARM a kernel modification or a driver is needed.
51 //   We can not require this from users of compiler-rt.
52 // So on ARM we use clock_gettime() which gives the result in nanoseconds.
53 //   To get the measurements per second, we scale this by the number of
54 //   nanoseconds per second, pretending that the TSC frequency is 1GHz and
55 //   one TSC tick is 1 nanosecond.
56 #include "sanitizer_common/sanitizer_common.h"
57 #include "sanitizer_common/sanitizer_internal_defs.h"
58 #include "xray_defs.h"
59 #include <cerrno>
60 #include <cstdint>
61 #include <time.h>
62 
63 namespace __xray {
64 
65 inline bool probeRequiredCPUFeatures() XRAY_NEVER_INSTRUMENT { return true; }
66 
67 ALWAYS_INLINE uint64_t readTSC(uint8_t &CPU) XRAY_NEVER_INSTRUMENT {
68   timespec TS;
69   int result = clock_gettime(CLOCK_REALTIME, &TS);
70   if (result != 0) {
71     Report("clock_gettime(2) returned %d, errno=%d.", result, int(errno));
72     TS.tv_sec = 0;
73     TS.tv_nsec = 0;
74   }
75   CPU = 0;
76   return TS.tv_sec * NanosecondsPerSecond + TS.tv_nsec;
77 }
78 
79 inline uint64_t getTSCFrequency() XRAY_NEVER_INSTRUMENT {
80   return NanosecondsPerSecond;
81 }
82 
83 } // namespace __xray
84 
85 #else
86 #error Target architecture is not supported.
87 #endif // CPU architecture
88 #endif // SANITIZER_FUCHSIA
89 
90 #endif // XRAY_EMULATE_TSC_H
91