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