blob: e9ed7c7350e2f081f2b39afe08ff2870a05c539b [file] [log] [blame]
Tim Shen7d0bffb2017-02-10 20:30:43 +00001//===-- 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#if defined(__x86_64__)
17#include "xray_x86_64.inc"
Tim Shenc6ce73b2017-02-15 22:40:29 +000018#elif defined(__powerpc64__)
19#include "xray_powerpc64.inc"
Sagar Thakurea831e42017-02-15 10:54:09 +000020#elif defined(__arm__) || defined(__aarch64__) || defined(__mips__)
Tim Shen7d0bffb2017-02-10 20:30:43 +000021// Emulated TSC.
22// There is no instruction like RDTSCP in user mode on ARM. ARM's CP15 does
23// not have a constant frequency like TSC on x86(_64), it may go faster
24// or slower depending on CPU turbo or power saving mode. Furthermore,
25// to read from CP15 on ARM a kernel modification or a driver is needed.
26// We can not require this from users of compiler-rt.
27// So on ARM we use clock_gettime() which gives the result in nanoseconds.
28// To get the measurements per second, we scale this by the number of
29// nanoseconds per second, pretending that the TSC frequency is 1GHz and
30// one TSC tick is 1 nanosecond.
31#include "sanitizer_common/sanitizer_common.h"
32#include "sanitizer_common/sanitizer_internal_defs.h"
33#include "xray_defs.h"
34#include <cerrno>
35#include <cstdint>
36#include <time.h>
37
38namespace __xray {
39
40static constexpr uint64_t NanosecondsPerSecond = 1000ULL * 1000 * 1000;
41
42inline bool probeRequiredCPUFeatures() XRAY_NEVER_INSTRUMENT { return true; }
43
44ALWAYS_INLINE uint64_t readTSC(uint8_t &CPU) XRAY_NEVER_INSTRUMENT {
45 timespec TS;
46 int result = clock_gettime(CLOCK_REALTIME, &TS);
47 if (result != 0) {
48 Report("clock_gettime(2) returned %d, errno=%d.", result, int(errno));
49 TS.tv_sec = 0;
50 TS.tv_nsec = 0;
51 }
52 CPU = 0;
53 return TS.tv_sec * NanosecondsPerSecond + TS.tv_nsec;
54}
55
56inline uint64_t getTSCFrequency() XRAY_NEVER_INSTRUMENT {
57 return NanosecondsPerSecond;
58}
59
60} // namespace __xray
61
62#else
63"Unsupported CPU Architecture"
64#endif // CPU architecture
65
66#endif // XRAY_EMULATE_TSC_H