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