Skip to content
This repository was archived by the owner on May 21, 2019. It is now read-only.

Commit 324a908

Browse files
committed
[XRay] Refactor TSC related functions into a single header. NFC.
Summary: The implementation, however, is in different arch-specific files, unless it's emulated. Reviewers: dberris, pelikan, javed.absar Subscribers: aemerson, llvm-commits Differential Revision: https://reviews.llvm.org/D29796 git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@294777 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent fc4a9bf commit 324a908

10 files changed

+39
-106
lines changed

lib/xray/xray_AArch64.cc

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
//===----------------------------------------------------------------------===//
1515
#include "sanitizer_common/sanitizer_common.h"
1616
#include "xray_defs.h"
17-
#include "xray_emulate_tsc.h"
1817
#include "xray_interface_internal.h"
1918
#include <atomic>
2019
#include <cassert>
@@ -24,19 +23,6 @@ extern "C" void __clear_cache(void* start, void* end);
2423

2524
namespace __xray {
2625

27-
uint64_t cycleFrequency() XRAY_NEVER_INSTRUMENT {
28-
// There is no instruction like RDTSCP in user mode on ARM. ARM's CP15 does
29-
// not have a constant frequency like TSC on x86[_64]; it may go faster or
30-
// slower depending on CPU's turbo or power saving modes. Furthermore, to
31-
// read from CP15 on ARM a kernel modification or a driver is needed.
32-
// We can not require this from users of compiler-rt.
33-
// So on ARM we use clock_gettime(2) which gives the result in nanoseconds.
34-
// To get the measurements per second, we scale this by the number of
35-
// nanoseconds per second, pretending that the TSC frequency is 1GHz and
36-
// one TSC tick is 1 nanosecond.
37-
return NanosecondsPerSecond;
38-
}
39-
4026
// The machine codes for some instructions used in runtime patching.
4127
enum class PatchOpcodes : uint32_t {
4228
PO_StpX0X30SP_m16e = 0xA9BF7BE0, // STP X0, X30, [SP, #-16]!

lib/xray/xray_arm.cc

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
//===----------------------------------------------------------------------===//
1515
#include "sanitizer_common/sanitizer_common.h"
1616
#include "xray_defs.h"
17-
#include "xray_emulate_tsc.h"
1817
#include "xray_interface_internal.h"
1918
#include <atomic>
2019
#include <cassert>
@@ -23,19 +22,6 @@ extern "C" void __clear_cache(void* start, void* end);
2322

2423
namespace __xray {
2524

26-
uint64_t cycleFrequency() XRAY_NEVER_INSTRUMENT {
27-
// There is no instruction like RDTSCP in user mode on ARM. ARM's CP15 does
28-
// not have a constant frequency like TSC on x86[_64]; it may go faster or
29-
// slower depending on CPU's turbo or power saving modes. Furthermore, to
30-
// read from CP15 on ARM a kernel modification or a driver is needed.
31-
// We can not require this from users of compiler-rt.
32-
// So on ARM we use clock_gettime(2) which gives the result in nanoseconds.
33-
// To get the measurements per second, we scale this by the number of
34-
// nanoseconds per second, pretending that the TSC frequency is 1GHz and
35-
// one TSC tick is 1 nanosecond.
36-
return NanosecondsPerSecond;
37-
}
38-
3925
// The machine codes for some instructions used in runtime patching.
4026
enum class PatchOpcodes : uint32_t {
4127
PO_PushR0Lr = 0xE92D4001, // PUSH {r0, lr}

lib/xray/xray_fdr_logging.cc

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,9 @@
3232
#include "xray_buffer_queue.h"
3333
#include "xray_defs.h"
3434
#include "xray_flags.h"
35+
#include "xray_tsc.h"
3536
#include "xray_utils.h"
3637

37-
#if defined(__x86_64__)
38-
#include "xray_x86_64.h"
39-
#elif defined(__arm__) || defined(__aarch64__)
40-
#include "xray_emulate_tsc.h"
41-
#else
42-
#error "Unsupported CPU Architecture"
43-
#endif /* CPU architecture */
44-
4538
namespace __xray {
4639

4740
// Global BufferQueue.
@@ -123,9 +116,9 @@ XRayLogFlushStatus fdrLoggingFlush() XRAY_NEVER_INSTRUMENT {
123116
XRayFileHeader Header;
124117
Header.Version = 1;
125118
Header.Type = FileTypes::FDR_LOG;
126-
auto CPUFrequency = getCPUFrequency();
119+
auto TSCFrequency = getTSCFrequency();
127120
Header.CycleFrequency =
128-
CPUFrequency == -1 ? 0 : static_cast<uint64_t>(CPUFrequency);
121+
TSCFrequency == -1 ? 0 : static_cast<uint64_t>(TSCFrequency);
129122
// FIXME: Actually check whether we have 'constant_tsc' and 'nonstop_tsc'
130123
// before setting the values in the header.
131124
Header.ConstantTSC = 1;

lib/xray/xray_inmemory_log.cc

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,12 @@
2424
#include <thread>
2525
#include <unistd.h>
2626

27-
#if defined(__x86_64__)
28-
#include "xray_x86_64.h"
29-
#elif defined(__arm__) || defined(__aarch64__)
30-
#include "xray_emulate_tsc.h"
31-
#else
32-
#error "Unsupported CPU Architecture"
33-
#endif /* Architecture-specific inline intrinsics */
34-
3527
#include "sanitizer_common/sanitizer_libc.h"
3628
#include "xray/xray_records.h"
3729
#include "xray_defs.h"
3830
#include "xray_flags.h"
3931
#include "xray_interface_internal.h"
32+
#include "xray_tsc.h"
4033
#include "xray_utils.h"
4134

4235
// __xray_InMemoryRawLog will use a thread-local aligned buffer capped to a
@@ -84,7 +77,7 @@ using namespace __xray;
8477

8578
static int __xray_OpenLogFile() XRAY_NEVER_INSTRUMENT {
8679
int F = getLogFD();
87-
auto CPUFrequency = getCPUFrequency();
80+
auto TSCFrequency = getTSCFrequency();
8881
if (F == -1)
8982
return -1;
9083
// Since we're here, we get to write the header. We set it up so that the
@@ -94,7 +87,7 @@ static int __xray_OpenLogFile() XRAY_NEVER_INSTRUMENT {
9487
Header.Version = 1;
9588
Header.Type = FileTypes::NAIVE_LOG;
9689
Header.CycleFrequency =
97-
CPUFrequency == -1 ? 0 : static_cast<uint64_t>(CPUFrequency);
90+
TSCFrequency == -1 ? 0 : static_cast<uint64_t>(TSCFrequency);
9891

9992
// FIXME: Actually check whether we have 'constant_tsc' and 'nonstop_tsc'
10093
// before setting the values in the header.

lib/xray/xray_interface_internal.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ struct XRaySledMap {
4848
size_t Entries;
4949
};
5050

51-
uint64_t cycleFrequency();
52-
5351
bool patchFunctionEntry(bool Enable, uint32_t FuncId,
5452
const XRaySledEntry &Sled);
5553
bool patchFunctionExit(bool Enable, uint32_t FuncId, const XRaySledEntry &Sled);

lib/xray/xray_emulate_tsc.h renamed to lib/xray/xray_tsc.h

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===-- xray_emulate_tsc.h --------------------------------------*- C++ -*-===//
1+
//===-- xray_tsc.h ----------------------------------------------*- C++ -*-===//
22
//
33
// The LLVM Compiler Infrastructure
44
//
@@ -13,6 +13,19 @@
1313
#ifndef XRAY_EMULATE_TSC_H
1414
#define XRAY_EMULATE_TSC_H
1515

16+
#if defined(__x86_64__)
17+
#include "xray_x86_64.inc"
18+
#elif defined(__arm__) || defined(__aarch64__)
19+
// Emulated TSC.
20+
// There is no instruction like RDTSCP in user mode on ARM. ARM's CP15 does
21+
// not have a constant frequency like TSC on x86(_64), it may go faster
22+
// or slower depending on CPU turbo or power saving mode. Furthermore,
23+
// to read from CP15 on ARM a kernel modification or a driver is needed.
24+
// We can not require this from users of compiler-rt.
25+
// So on ARM we use clock_gettime() which gives the result in nanoseconds.
26+
// To get the measurements per second, we scale this by the number of
27+
// nanoseconds per second, pretending that the TSC frequency is 1GHz and
28+
// one TSC tick is 1 nanosecond.
1629
#include "sanitizer_common/sanitizer_common.h"
1730
#include "sanitizer_common/sanitizer_internal_defs.h"
1831
#include "xray_defs.h"
@@ -24,6 +37,8 @@ namespace __xray {
2437

2538
static constexpr uint64_t NanosecondsPerSecond = 1000ULL * 1000 * 1000;
2639

40+
inline bool probeRequiredCPUFeatures() XRAY_NEVER_INSTRUMENT { return true; }
41+
2742
ALWAYS_INLINE uint64_t readTSC(uint8_t &CPU) XRAY_NEVER_INSTRUMENT {
2843
timespec TS;
2944
int result = clock_gettime(CLOCK_REALTIME, &TS);
@@ -36,8 +51,14 @@ ALWAYS_INLINE uint64_t readTSC(uint8_t &CPU) XRAY_NEVER_INSTRUMENT {
3651
return TS.tv_sec * NanosecondsPerSecond + TS.tv_nsec;
3752
}
3853

39-
bool probeRequiredCPUFeatures();
54+
inline uint64_t getTSCFrequency() XRAY_NEVER_INSTRUMENT {
55+
return NanosecondsPerSecond;
56+
}
4057

4158
} // namespace __xray
4259

60+
#else
61+
"Unsupported CPU Architecture"
62+
#endif // CPU architecture
63+
4364
#endif // XRAY_EMULATE_TSC_H

lib/xray/xray_utils.cc

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,6 @@
2424
#include <unistd.h>
2525
#include <utility>
2626

27-
#if defined(__x86_64__)
28-
#include "xray_x86_64.h"
29-
#elif defined(__arm__) || defined(__aarch64__)
30-
#include "xray_emulate_tsc.h"
31-
#else
32-
#error "Unsupported CPU Architecture"
33-
#endif /* CPU architecture */
34-
3527
namespace __xray {
3628

3729
void printToStdErr(const char *Buffer) XRAY_NEVER_INSTRUMENT {
@@ -99,37 +91,6 @@ bool readValueFromFile(const char *Filename,
9991
return Result;
10092
}
10193

102-
long long getCPUFrequency() XRAY_NEVER_INSTRUMENT {
103-
// Get the cycle frequency from SysFS on Linux.
104-
long long CPUFrequency = -1;
105-
#if defined(__x86_64__)
106-
if (readValueFromFile("/sys/devices/system/cpu/cpu0/tsc_freq_khz",
107-
&CPUFrequency)) {
108-
CPUFrequency *= 1000;
109-
} else if (readValueFromFile(
110-
"/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq",
111-
&CPUFrequency)) {
112-
CPUFrequency *= 1000;
113-
} else {
114-
Report("Unable to determine CPU frequency for TSC accounting.\n");
115-
}
116-
#elif defined(__arm__) || defined(__aarch64__)
117-
// There is no instruction like RDTSCP in user mode on ARM. ARM's CP15 does
118-
// not have a constant frequency like TSC on x86(_64), it may go faster
119-
// or slower depending on CPU turbo or power saving mode. Furthermore,
120-
// to read from CP15 on ARM a kernel modification or a driver is needed.
121-
// We can not require this from users of compiler-rt.
122-
// So on ARM we use clock_gettime() which gives the result in nanoseconds.
123-
// To get the measurements per second, we scale this by the number of
124-
// nanoseconds per second, pretending that the TSC frequency is 1GHz and
125-
// one TSC tick is 1 nanosecond.
126-
CPUFrequency = NanosecondsPerSecond;
127-
#else
128-
#error "Unsupported CPU Architecture"
129-
#endif /* CPU architecture */
130-
return CPUFrequency;
131-
}
132-
13394
int getLogFD() XRAY_NEVER_INSTRUMENT {
13495
// FIXME: Figure out how to make this less stderr-dependent.
13596
SetPrintfAndReportCallback(printToStdErr);

lib/xray/xray_utils.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@ std::pair<ssize_t, bool> retryingReadSome(int Fd, char *Begin, char *End);
3636
// file.
3737
int getLogFD();
3838

39-
// EINTR-safe read of CPU frquency for the current CPU.
40-
long long getCPUFrequency();
41-
4239
} // namespace __xray
4340

4441
#endif // XRAY_UTILS_H

lib/xray/xray_x86_64.cc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,19 +57,19 @@ static bool readValueFromFile(const char *Filename,
5757
return Result;
5858
}
5959

60-
uint64_t cycleFrequency() XRAY_NEVER_INSTRUMENT {
61-
long long CPUFrequency = -1;
60+
uint64_t getTSCFrequency() XRAY_NEVER_INSTRUMENT {
61+
long long TSCFrequency = -1;
6262
if (readValueFromFile("/sys/devices/system/cpu/cpu0/tsc_freq_khz",
63-
&CPUFrequency)) {
64-
CPUFrequency *= 1000;
63+
&TSCFrequency)) {
64+
TSCFrequency *= 1000;
6565
} else if (readValueFromFile(
6666
"/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq",
67-
&CPUFrequency)) {
68-
CPUFrequency *= 1000;
67+
&TSCFrequency)) {
68+
TSCFrequency *= 1000;
6969
} else {
7070
Report("Unable to determine CPU frequency for TSC accounting.\n");
7171
}
72-
return CPUFrequency == -1 ? 0 : static_cast<uint64_t>(CPUFrequency);
72+
return TSCFrequency == -1 ? 0 : static_cast<uint64_t>(TSCFrequency);
7373
}
7474

7575
static constexpr uint8_t CallOpCode = 0xe8;

lib/xray/xray_x86_64.h renamed to lib/xray/xray_x86_64.inc

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===-- xray_x86_64.h -------------------------------------------*- C++ -*-===//
1+
//===-- xray_x86_64.inc -----------------------------------------*- C++ -*-===//
22
//
33
// The LLVM Compiler Infrastructure
44
//
@@ -10,8 +10,6 @@
1010
// This file is a part of XRay, a dynamic runtime instrumentation system.
1111
//
1212
//===----------------------------------------------------------------------===//
13-
#ifndef XRAY_X86_64_H
14-
#define XRAY_X86_64_H
1513

1614
#include <cstdint>
1715
#include <x86intrin.h>
@@ -28,8 +26,8 @@ ALWAYS_INLINE uint64_t readTSC(uint8_t &CPU) XRAY_NEVER_INSTRUMENT {
2826
return TSC;
2927
}
3028

29+
uint64_t getTSCFrequency();
30+
3131
bool probeRequiredCPUFeatures();
3232

3333
} // namespace __xray
34-
35-
#endif // XRAY_X86_64_H

0 commit comments

Comments
 (0)