121 lines · c
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-exception6//7//===----------------------------------------------------------------------===//8//9// This file is a part of XRay, a dynamic runtime instrumentation system.10//11//===----------------------------------------------------------------------===//12#ifndef XRAY_EMULATE_TSC_H13#define XRAY_EMULATE_TSC_H14 15#include "sanitizer_common/sanitizer_common.h"16 17namespace __xray {18static constexpr uint64_t NanosecondsPerSecond = 1000ULL * 1000 * 1000;19}20 21#if SANITIZER_FUCHSIA22#include <zircon/syscalls.h>23 24namespace __xray {25 26inline bool probeRequiredCPUFeatures() XRAY_NEVER_INSTRUMENT { return true; }27 28ALWAYS_INLINE uint64_t readTSC(uint8_t &CPU) XRAY_NEVER_INSTRUMENT {29 CPU = 0;30 return _zx_ticks_get();31}32 33inline uint64_t getTSCFrequency() XRAY_NEVER_INSTRUMENT {34 return _zx_ticks_per_second();35}36 37} // namespace __xray38 39#else // SANITIZER_FUCHSIA40 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 defined(__hexagon__) || defined(__loongarch_lp64) || defined(__riscv)47// Emulated TSC.48// There is no instruction like RDTSCP in user mode on ARM. ARM's CP15 does49// not have a constant frequency like TSC on x86(_64), it may go faster50// 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 of55// nanoseconds per second, pretending that the TSC frequency is 1GHz and56// 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 64namespace __xray {65 66inline bool probeRequiredCPUFeatures() XRAY_NEVER_INSTRUMENT { return true; }67 68ALWAYS_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 80inline uint64_t getTSCFrequency() XRAY_NEVER_INSTRUMENT {81 return NanosecondsPerSecond;82}83 84} // namespace __xray85 86#elif defined(__s390x__)87#include "sanitizer_common/sanitizer_common.h"88#include "sanitizer_common/sanitizer_internal_defs.h"89#include "xray_defs.h"90#include <cerrno>91#include <cstdint>92#include <time.h>93 94namespace __xray {95 96inline bool probeRequiredCPUFeatures() XRAY_NEVER_INSTRUMENT { return true; }97 98ALWAYS_INLINE uint64_t readTSC(uint8_t &CPU) XRAY_NEVER_INSTRUMENT {99 CPU = 0;100#if __has_builtin(__builtin_readcyclecounter)101 return __builtin_readcyclecounter();102#else103 uint64_t Cycles;104 asm volatile("stckf %0" : : "Q"(Cycles) : "cc");105 return Cycles;106#endif107}108 109inline uint64_t getTSCFrequency() XRAY_NEVER_INSTRUMENT {110 return NanosecondsPerSecond;111}112 113} // namespace __xray114 115#else116#error Target architecture is not supported.117#endif // CPU architecture118#endif // SANITIZER_FUCHSIA119 120#endif // XRAY_EMULATE_TSC_H121