1 //===-- xray_inmemory_log.cc ------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is a part of XRay, a dynamic runtime instrumentation system.
12 // Implementation of a simple in-memory log of XRay events. This defines a
13 // logging function that's compatible with the XRay handler interface, and
14 // routines for exporting data to files.
16 //===----------------------------------------------------------------------===//
22 #include <sys/syscall.h>
23 #include <sys/types.h>
27 #include "sanitizer_common/sanitizer_libc.h"
28 #include "xray/xray_records.h"
29 #include "xray_defs.h"
30 #include "xray_flags.h"
31 #include "xray_interface_internal.h"
33 #include "xray_utils.h"
35 // __xray_InMemoryRawLog will use a thread-local aligned buffer capped to a
36 // certain size (32kb by default) and use it as if it were a circular buffer for
37 // events. We store simple fixed-sized entries in the log for external analysis.
40 void __xray_InMemoryRawLog(int32_t FuncId,
41 XRayEntryType Type) XRAY_NEVER_INSTRUMENT;
48 class ThreadExitFlusher {
54 explicit ThreadExitFlusher(int Fd, XRayRecord *Start,
55 size_t &Offset) XRAY_NEVER_INSTRUMENT
60 ~ThreadExitFlusher() XRAY_NEVER_INSTRUMENT {
61 std::lock_guard<std::mutex> L(LogMutex);
62 if (Fd > 0 && Start != nullptr) {
63 retryingWriteAll(Fd, reinterpret_cast<char *>(Start),
64 reinterpret_cast<char *>(Start + Offset));
65 // Because this thread's exit could be the last one trying to write to the
66 // file and that we're not able to close out the file properly, we sync
67 // instead and hope that the pending writes are flushed as the thread
76 using namespace __xray;
78 static int __xray_OpenLogFile() XRAY_NEVER_INSTRUMENT {
83 // Test for required CPU features and cache the cycle frequency
84 static bool TSCSupported = probeRequiredCPUFeatures();
85 static uint64_t CycleFrequency = TSCSupported ? getTSCFrequency()
86 : __xray::NanosecondsPerSecond;
88 // Since we're here, we get to write the header. We set it up so that the
89 // header will only be written once, at the start, and let the threads
90 // logging do writes which just append.
91 XRayFileHeader Header;
93 Header.Type = FileTypes::NAIVE_LOG;
94 Header.CycleFrequency = CycleFrequency;
96 // FIXME: Actually check whether we have 'constant_tsc' and 'nonstop_tsc'
97 // before setting the values in the header.
98 Header.ConstantTSC = 1;
99 Header.NonstopTSC = 1;
100 retryingWriteAll(F, reinterpret_cast<char *>(&Header),
101 reinterpret_cast<char *>(&Header) + sizeof(Header));
105 template <class RDTSC>
106 void __xray_InMemoryRawLog(int32_t FuncId, XRayEntryType Type,
107 RDTSC ReadTSC) XRAY_NEVER_INSTRUMENT {
109 std::aligned_storage<sizeof(XRayRecord), alignof(XRayRecord)>::type;
110 static constexpr size_t BuffLen = 1024;
111 thread_local static Buffer InMemoryBuffer[BuffLen] = {};
112 thread_local static size_t Offset = 0;
113 static int Fd = __xray_OpenLogFile();
116 thread_local __xray::ThreadExitFlusher Flusher(
117 Fd, reinterpret_cast<__xray::XRayRecord *>(InMemoryBuffer), Offset);
118 thread_local pid_t TId = syscall(SYS_gettid);
120 // First we get the useful data, and stuff it into the already aligned buffer
121 // through a pointer offset.
122 auto &R = reinterpret_cast<__xray::XRayRecord *>(InMemoryBuffer)[Offset];
123 R.RecordType = RecordTypes::NORMAL;
124 R.TSC = ReadTSC(R.CPU);
129 if (Offset == BuffLen) {
130 std::lock_guard<std::mutex> L(LogMutex);
131 auto RecordBuffer = reinterpret_cast<__xray::XRayRecord *>(InMemoryBuffer);
132 retryingWriteAll(Fd, reinterpret_cast<char *>(RecordBuffer),
133 reinterpret_cast<char *>(RecordBuffer + Offset));
138 void __xray_InMemoryRawLogRealTSC(int32_t FuncId,
139 XRayEntryType Type) XRAY_NEVER_INSTRUMENT {
140 __xray_InMemoryRawLog(FuncId, Type, __xray::readTSC);
143 void __xray_InMemoryEmulateTSC(int32_t FuncId,
144 XRayEntryType Type) XRAY_NEVER_INSTRUMENT {
145 __xray_InMemoryRawLog(FuncId, Type, [](uint8_t &CPU) XRAY_NEVER_INSTRUMENT {
147 int result = clock_gettime(CLOCK_REALTIME, &TS);
149 Report("clock_gettimg(2) return %d, errno=%d.", result, int(errno));
153 return TS.tv_sec * __xray::NanosecondsPerSecond + TS.tv_nsec;
157 static auto UNUSED Unused = [] {
158 auto UseRealTSC = probeRequiredCPUFeatures();
160 Report("WARNING: Required CPU features missing for XRay instrumentation, "
161 "using emulation instead.\n");
162 if (flags()->xray_naive_log)
163 __xray_set_handler(UseRealTSC ? __xray_InMemoryRawLogRealTSC
164 : __xray_InMemoryEmulateTSC);