]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/xray/xray_inmemory_log.cc
Merge OpenSSL 1.0.2m.
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / xray / xray_inmemory_log.cc
1 //===-- xray_inmemory_log.cc ------------------------------------*- 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 // 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.
15 //
16 //===----------------------------------------------------------------------===//
17
18 #include <cassert>
19 #include <fcntl.h>
20 #include <mutex>
21 #include <sys/stat.h>
22 #include <sys/syscall.h>
23 #include <sys/types.h>
24 #include <thread>
25 #include <unistd.h>
26
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"
32 #include "xray_tsc.h"
33 #include "xray_utils.h"
34
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.
38
39 extern "C" {
40 void __xray_InMemoryRawLog(int32_t FuncId,
41                            XRayEntryType Type) XRAY_NEVER_INSTRUMENT;
42 }
43
44 namespace __xray {
45
46 std::mutex LogMutex;
47
48 class ThreadExitFlusher {
49   int Fd;
50   XRayRecord *Start;
51   size_t &Offset;
52
53 public:
54   explicit ThreadExitFlusher(int Fd, XRayRecord *Start,
55                              size_t &Offset) XRAY_NEVER_INSTRUMENT
56       : Fd(Fd),
57         Start(Start),
58         Offset(Offset) {}
59
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
68       // exits.
69       fsync(Fd);
70     }
71   }
72 };
73
74 } // namespace __xray
75
76 using namespace __xray;
77
78 static int __xray_OpenLogFile() XRAY_NEVER_INSTRUMENT {
79   int F = getLogFD();
80   if (F == -1)
81     return -1;
82
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;
87
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;
92   Header.Version = 1;
93   Header.Type = FileTypes::NAIVE_LOG;
94   Header.CycleFrequency = CycleFrequency;
95
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));
102   return F;
103 }
104
105 template <class RDTSC>
106 void __xray_InMemoryRawLog(int32_t FuncId, XRayEntryType Type,
107                            RDTSC ReadTSC) XRAY_NEVER_INSTRUMENT {
108   using Buffer =
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();
114   if (Fd == -1)
115     return;
116   thread_local __xray::ThreadExitFlusher Flusher(
117       Fd, reinterpret_cast<__xray::XRayRecord *>(InMemoryBuffer), Offset);
118   thread_local pid_t TId = syscall(SYS_gettid);
119
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);
125   R.TId = TId;
126   R.Type = Type;
127   R.FuncId = FuncId;
128   ++Offset;
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));
134     Offset = 0;
135   }
136 }
137
138 void __xray_InMemoryRawLogRealTSC(int32_t FuncId,
139                                   XRayEntryType Type) XRAY_NEVER_INSTRUMENT {
140   __xray_InMemoryRawLog(FuncId, Type, __xray::readTSC);
141 }
142
143 void __xray_InMemoryEmulateTSC(int32_t FuncId,
144                                XRayEntryType Type) XRAY_NEVER_INSTRUMENT {
145   __xray_InMemoryRawLog(FuncId, Type, [](uint8_t &CPU) XRAY_NEVER_INSTRUMENT {
146     timespec TS;
147     int result = clock_gettime(CLOCK_REALTIME, &TS);
148     if (result != 0) {
149       Report("clock_gettimg(2) return %d, errno=%d.", result, int(errno));
150       TS = {0, 0};
151     }
152     CPU = 0;
153     return TS.tv_sec * __xray::NanosecondsPerSecond + TS.tv_nsec;
154   });
155 }
156
157 static auto UNUSED Unused = [] {
158   auto UseRealTSC = probeRequiredCPUFeatures();
159   if (!UseRealTSC)
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);
165   return true;
166 }();