1 //===- Trace.cpp - XRay Trace Loading implementation. ---------------------===//
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-exception
7 //===----------------------------------------------------------------------===//
9 // XRay log reader implementation.
11 //===----------------------------------------------------------------------===//
12 #include "llvm/XRay/Trace.h"
13 #include "llvm/ADT/STLExtras.h"
14 #include "llvm/Support/DataExtractor.h"
15 #include "llvm/Support/Error.h"
16 #include "llvm/Support/FileSystem.h"
17 #include "llvm/XRay/BlockIndexer.h"
18 #include "llvm/XRay/BlockVerifier.h"
19 #include "llvm/XRay/FDRRecordConsumer.h"
20 #include "llvm/XRay/FDRRecordProducer.h"
21 #include "llvm/XRay/FDRRecords.h"
22 #include "llvm/XRay/FDRTraceExpander.h"
23 #include "llvm/XRay/FileHeaderReader.h"
24 #include "llvm/XRay/YAMLXRayRecord.h"
29 using namespace llvm::xray
;
30 using llvm::yaml::Input
;
34 Error
loadNaiveFormatLog(StringRef Data
, bool IsLittleEndian
,
35 XRayFileHeader
&FileHeader
,
36 std::vector
<XRayRecord
> &Records
) {
38 return make_error
<StringError
>(
39 "Not enough bytes for an XRay log.",
40 std::make_error_code(std::errc::invalid_argument
));
42 if (Data
.size() - 32 == 0 || Data
.size() % 32 != 0)
43 return make_error
<StringError
>(
44 "Invalid-sized XRay data.",
45 std::make_error_code(std::errc::invalid_argument
));
47 DataExtractor
Reader(Data
, IsLittleEndian
, 8);
48 uint64_t OffsetPtr
= 0;
49 auto FileHeaderOrError
= readBinaryFormatHeader(Reader
, OffsetPtr
);
50 if (!FileHeaderOrError
)
51 return FileHeaderOrError
.takeError();
52 FileHeader
= std::move(FileHeaderOrError
.get());
54 // Each record after the header will be 32 bytes, in the following format:
56 // (2) uint16 : record type
59 // (4) sint32 : function id
61 // (4) uint32 : thread id
62 // (4) uint32 : process id
64 while (Reader
.isValidOffset(OffsetPtr
)) {
65 if (!Reader
.isValidOffsetForDataOfSize(OffsetPtr
, 32))
66 return createStringError(
67 std::make_error_code(std::errc::executable_format_error
),
68 "Not enough bytes to read a full record at offset %" PRId64
".",
70 auto PreReadOffset
= OffsetPtr
;
71 auto RecordType
= Reader
.getU16(&OffsetPtr
);
72 if (OffsetPtr
== PreReadOffset
)
73 return createStringError(
74 std::make_error_code(std::errc::executable_format_error
),
75 "Failed reading record type at offset %" PRId64
".", OffsetPtr
);
78 case 0: { // Normal records.
79 Records
.emplace_back();
80 auto &Record
= Records
.back();
81 Record
.RecordType
= RecordType
;
83 PreReadOffset
= OffsetPtr
;
84 Record
.CPU
= Reader
.getU8(&OffsetPtr
);
85 if (OffsetPtr
== PreReadOffset
)
86 return createStringError(
87 std::make_error_code(std::errc::executable_format_error
),
88 "Failed reading CPU field at offset %" PRId64
".", OffsetPtr
);
90 PreReadOffset
= OffsetPtr
;
91 auto Type
= Reader
.getU8(&OffsetPtr
);
92 if (OffsetPtr
== PreReadOffset
)
93 return createStringError(
94 std::make_error_code(std::errc::executable_format_error
),
95 "Failed reading record type field at offset %" PRId64
".",
100 Record
.Type
= RecordTypes::ENTER
;
103 Record
.Type
= RecordTypes::EXIT
;
106 Record
.Type
= RecordTypes::TAIL_EXIT
;
109 Record
.Type
= RecordTypes::ENTER_ARG
;
112 return createStringError(
113 std::make_error_code(std::errc::executable_format_error
),
114 "Unknown record type '%d' at offset %" PRId64
".", Type
, OffsetPtr
);
117 PreReadOffset
= OffsetPtr
;
118 Record
.FuncId
= Reader
.getSigned(&OffsetPtr
, sizeof(int32_t));
119 if (OffsetPtr
== PreReadOffset
)
120 return createStringError(
121 std::make_error_code(std::errc::executable_format_error
),
122 "Failed reading function id field at offset %" PRId64
".",
125 PreReadOffset
= OffsetPtr
;
126 Record
.TSC
= Reader
.getU64(&OffsetPtr
);
127 if (OffsetPtr
== PreReadOffset
)
128 return createStringError(
129 std::make_error_code(std::errc::executable_format_error
),
130 "Failed reading TSC field at offset %" PRId64
".", OffsetPtr
);
132 PreReadOffset
= OffsetPtr
;
133 Record
.TId
= Reader
.getU32(&OffsetPtr
);
134 if (OffsetPtr
== PreReadOffset
)
135 return createStringError(
136 std::make_error_code(std::errc::executable_format_error
),
137 "Failed reading thread id field at offset %" PRId64
".", OffsetPtr
);
139 PreReadOffset
= OffsetPtr
;
140 Record
.PId
= Reader
.getU32(&OffsetPtr
);
141 if (OffsetPtr
== PreReadOffset
)
142 return createStringError(
143 std::make_error_code(std::errc::executable_format_error
),
144 "Failed reading process id at offset %" PRId64
".", OffsetPtr
);
148 case 1: { // Arg payload record.
149 auto &Record
= Records
.back();
151 // We skip the next two bytes of the record, because we don't need the
152 // type and the CPU record for arg payloads.
154 PreReadOffset
= OffsetPtr
;
155 int32_t FuncId
= Reader
.getSigned(&OffsetPtr
, sizeof(int32_t));
156 if (OffsetPtr
== PreReadOffset
)
157 return createStringError(
158 std::make_error_code(std::errc::executable_format_error
),
159 "Failed reading function id field at offset %" PRId64
".",
162 PreReadOffset
= OffsetPtr
;
163 auto TId
= Reader
.getU32(&OffsetPtr
);
164 if (OffsetPtr
== PreReadOffset
)
165 return createStringError(
166 std::make_error_code(std::errc::executable_format_error
),
167 "Failed reading thread id field at offset %" PRId64
".", OffsetPtr
);
169 PreReadOffset
= OffsetPtr
;
170 auto PId
= Reader
.getU32(&OffsetPtr
);
171 if (OffsetPtr
== PreReadOffset
)
172 return createStringError(
173 std::make_error_code(std::errc::executable_format_error
),
174 "Failed reading process id field at offset %" PRId64
".",
177 // Make a check for versions above 3 for the Pid field
178 if (Record
.FuncId
!= FuncId
|| Record
.TId
!= TId
||
179 (FileHeader
.Version
>= 3 ? Record
.PId
!= PId
: false))
180 return createStringError(
181 std::make_error_code(std::errc::executable_format_error
),
182 "Corrupted log, found arg payload following non-matching "
183 "function+thread record. Record for function %d != %d at offset "
185 Record
.FuncId
, FuncId
, OffsetPtr
);
187 PreReadOffset
= OffsetPtr
;
188 auto Arg
= Reader
.getU64(&OffsetPtr
);
189 if (OffsetPtr
== PreReadOffset
)
190 return createStringError(
191 std::make_error_code(std::errc::executable_format_error
),
192 "Failed reading argument payload at offset %" PRId64
".",
195 Record
.CallArgs
.push_back(Arg
);
199 return createStringError(
200 std::make_error_code(std::errc::executable_format_error
),
201 "Unknown record type '%d' at offset %" PRId64
".", RecordType
,
204 // Advance the offset pointer enough bytes to align to 32-byte records for
208 return Error::success();
211 /// Reads a log in FDR mode for version 1 of this binary format. FDR mode is
212 /// defined as part of the compiler-rt project in xray_fdr_logging.h, and such
213 /// a log consists of the familiar 32 bit XRayHeader, followed by sequences of
214 /// of interspersed 16 byte Metadata Records and 8 byte Function Records.
216 /// The following is an attempt to document the grammar of the format, which is
217 /// parsed by this function for little-endian machines. Since the format makes
218 /// use of BitFields, when we support big-endian architectures, we will need to
219 /// adjust not only the endianness parameter to llvm's RecordExtractor, but also
220 /// the bit twiddling logic, which is consistent with the little-endian
221 /// convention that BitFields within a struct will first be packed into the
222 /// least significant bits the address they belong to.
224 /// We expect a format complying with the grammar in the following pseudo-EBNF
225 /// in Version 1 of the FDR log.
227 /// FDRLog: XRayFileHeader ThreadBuffer*
228 /// XRayFileHeader: 32 bytes to identify the log as FDR with machine metadata.
229 /// Includes BufferSize
230 /// ThreadBuffer: NewBuffer WallClockTime NewCPUId FunctionSequence EOB
231 /// BufSize: 8 byte unsigned integer indicating how large the buffer is.
232 /// NewBuffer: 16 byte metadata record with Thread Id.
233 /// WallClockTime: 16 byte metadata record with human readable time.
234 /// Pid: 16 byte metadata record with Pid
235 /// NewCPUId: 16 byte metadata record with CPUId and a 64 bit TSC reading.
236 /// EOB: 16 byte record in a thread buffer plus mem garbage to fill BufSize.
237 /// FunctionSequence: NewCPUId | TSCWrap | FunctionRecord
238 /// TSCWrap: 16 byte metadata record with a full 64 bit TSC reading.
239 /// FunctionRecord: 8 byte record with FunctionId, entry/exit, and TSC delta.
241 /// In Version 2, we make the following changes:
243 /// ThreadBuffer: BufferExtents NewBuffer WallClockTime NewCPUId
245 /// BufferExtents: 16 byte metdata record describing how many usable bytes are
246 /// in the buffer. This is measured from the start of the buffer
247 /// and must always be at least 48 (bytes).
249 /// In Version 3, we make the following changes:
251 /// ThreadBuffer: BufferExtents NewBuffer WallClockTime Pid NewCPUId
253 /// EOB: *deprecated*
255 /// In Version 4, we make the following changes:
257 /// CustomEventRecord now includes the CPU data.
259 /// In Version 5, we make the following changes:
261 /// CustomEventRecord and TypedEventRecord now use TSC delta encoding similar to
262 /// what FunctionRecord instances use, and we no longer need to include the CPU
263 /// id in the CustomEventRecord.
265 Error
loadFDRLog(StringRef Data
, bool IsLittleEndian
,
266 XRayFileHeader
&FileHeader
, std::vector
<XRayRecord
> &Records
) {
268 if (Data
.size() < 32)
269 return createStringError(std::make_error_code(std::errc::invalid_argument
),
270 "Not enough bytes for an XRay FDR log.");
271 DataExtractor
DE(Data
, IsLittleEndian
, 8);
273 uint64_t OffsetPtr
= 0;
274 auto FileHeaderOrError
= readBinaryFormatHeader(DE
, OffsetPtr
);
275 if (!FileHeaderOrError
)
276 return FileHeaderOrError
.takeError();
277 FileHeader
= std::move(FileHeaderOrError
.get());
279 // First we load the records into memory.
280 std::vector
<std::unique_ptr
<Record
>> FDRRecords
;
283 FileBasedRecordProducer
P(FileHeader
, DE
, OffsetPtr
);
284 LogBuilderConsumer
C(FDRRecords
);
285 while (DE
.isValidOffsetForDataOfSize(OffsetPtr
, 1)) {
286 auto R
= P
.produce();
288 return R
.takeError();
289 if (auto E
= C
.consume(std::move(R
.get())))
294 // Next we index the records into blocks.
295 BlockIndexer::Index Index
;
297 BlockIndexer
Indexer(Index
);
298 for (auto &R
: FDRRecords
)
299 if (auto E
= R
->apply(Indexer
))
301 if (auto E
= Indexer
.flush())
305 // Then we verify the consistency of the blocks.
307 for (auto &PTB
: Index
) {
308 auto &Blocks
= PTB
.second
;
309 for (auto &B
: Blocks
) {
310 BlockVerifier Verifier
;
311 for (auto *R
: B
.Records
)
312 if (auto E
= R
->apply(Verifier
))
314 if (auto E
= Verifier
.verify())
320 // This is now the meat of the algorithm. Here we sort the blocks according to
321 // the Walltime record in each of the blocks for the same thread. This allows
322 // us to more consistently recreate the execution trace in temporal order.
323 // After the sort, we then reconstitute `Trace` records using a stateful
324 // visitor associated with a single process+thread pair.
326 for (auto &PTB
: Index
) {
327 auto &Blocks
= PTB
.second
;
328 llvm::sort(Blocks
, [](const BlockIndexer::Block
&L
,
329 const BlockIndexer::Block
&R
) {
330 return (L
.WallclockTime
->seconds() < R
.WallclockTime
->seconds() &&
331 L
.WallclockTime
->nanos() < R
.WallclockTime
->nanos());
333 auto Adder
= [&](const XRayRecord
&R
) { Records
.push_back(R
); };
334 TraceExpander
Expander(Adder
, FileHeader
.Version
);
335 for (auto &B
: Blocks
) {
336 for (auto *R
: B
.Records
)
337 if (auto E
= R
->apply(Expander
))
340 if (auto E
= Expander
.flush())
345 return Error::success();
348 Error
loadYAMLLog(StringRef Data
, XRayFileHeader
&FileHeader
,
349 std::vector
<XRayRecord
> &Records
) {
354 return make_error
<StringError
>("Failed loading YAML Data.", In
.error());
356 FileHeader
.Version
= Trace
.Header
.Version
;
357 FileHeader
.Type
= Trace
.Header
.Type
;
358 FileHeader
.ConstantTSC
= Trace
.Header
.ConstantTSC
;
359 FileHeader
.NonstopTSC
= Trace
.Header
.NonstopTSC
;
360 FileHeader
.CycleFrequency
= Trace
.Header
.CycleFrequency
;
362 if (FileHeader
.Version
!= 1)
363 return make_error
<StringError
>(
364 Twine("Unsupported XRay file version: ") + Twine(FileHeader
.Version
),
365 std::make_error_code(std::errc::invalid_argument
));
368 std::transform(Trace
.Records
.begin(), Trace
.Records
.end(),
369 std::back_inserter(Records
), [&](const YAMLXRayRecord
&R
) {
370 return XRayRecord
{R
.RecordType
, R
.CPU
, R
.Type
,
371 R
.FuncId
, R
.TSC
, R
.TId
,
372 R
.PId
, R
.CallArgs
, R
.Data
};
374 return Error::success();
378 Expected
<Trace
> llvm::xray::loadTraceFile(StringRef Filename
, bool Sort
) {
379 Expected
<sys::fs::file_t
> FdOrErr
= sys::fs::openNativeFileForRead(Filename
);
381 return FdOrErr
.takeError();
384 if (auto EC
= sys::fs::file_size(Filename
, FileSize
)) {
385 return make_error
<StringError
>(
386 Twine("Cannot read log from '") + Filename
+ "'", EC
);
389 return make_error
<StringError
>(
390 Twine("File '") + Filename
+ "' too small for XRay.",
391 std::make_error_code(std::errc::executable_format_error
));
394 // Map the opened file into memory and use a StringRef to access it later.
396 sys::fs::mapped_file_region
MappedFile(
397 *FdOrErr
, sys::fs::mapped_file_region::mapmode::readonly
, FileSize
, 0,
399 sys::fs::closeFile(*FdOrErr
);
401 return make_error
<StringError
>(
402 Twine("Cannot read log from '") + Filename
+ "'", EC
);
404 auto Data
= StringRef(MappedFile
.data(), MappedFile
.size());
406 // TODO: Lift the endianness and implementation selection here.
407 DataExtractor
LittleEndianDE(Data
, true, 8);
408 auto TraceOrError
= loadTrace(LittleEndianDE
, Sort
);
410 DataExtractor
BigEndianDE(Data
, false, 8);
411 consumeError(TraceOrError
.takeError());
412 TraceOrError
= loadTrace(BigEndianDE
, Sort
);
417 Expected
<Trace
> llvm::xray::loadTrace(const DataExtractor
&DE
, bool Sort
) {
418 // Attempt to detect the file type using file magic. We have a slight bias
419 // towards the binary format, and we do this by making sure that the first 4
420 // bytes of the binary file is some combination of the following byte
421 // patterns: (observe the code loading them assumes they're little endian)
423 // 0x01 0x00 0x00 0x00 - version 1, "naive" format
424 // 0x01 0x00 0x01 0x00 - version 1, "flight data recorder" format
425 // 0x02 0x00 0x01 0x00 - version 2, "flight data recorder" format
427 // YAML files don't typically have those first four bytes as valid text so we
428 // try loading assuming YAML if we don't find these bytes.
430 // Only if we can't load either the binary or the YAML format will we yield an
432 DataExtractor
HeaderExtractor(DE
.getData(), DE
.isLittleEndian(), 8);
433 uint64_t OffsetPtr
= 0;
434 uint16_t Version
= HeaderExtractor
.getU16(&OffsetPtr
);
435 uint16_t Type
= HeaderExtractor
.getU16(&OffsetPtr
);
437 enum BinaryFormatType
{ NAIVE_FORMAT
= 0, FLIGHT_DATA_RECORDER_FORMAT
= 1 };
442 if (Version
== 1 || Version
== 2 || Version
== 3) {
443 if (auto E
= loadNaiveFormatLog(DE
.getData(), DE
.isLittleEndian(),
444 T
.FileHeader
, T
.Records
))
447 return make_error
<StringError
>(
448 Twine("Unsupported version for Basic/Naive Mode logging: ") +
450 std::make_error_code(std::errc::executable_format_error
));
453 case FLIGHT_DATA_RECORDER_FORMAT
:
454 if (Version
>= 1 && Version
<= 5) {
455 if (auto E
= loadFDRLog(DE
.getData(), DE
.isLittleEndian(), T
.FileHeader
,
459 return make_error
<StringError
>(
460 Twine("Unsupported version for FDR Mode logging: ") + Twine(Version
),
461 std::make_error_code(std::errc::executable_format_error
));
465 if (auto E
= loadYAMLLog(DE
.getData(), T
.FileHeader
, T
.Records
))
470 llvm::stable_sort(T
.Records
, [&](const XRayRecord
&L
, const XRayRecord
&R
) {
471 return L
.TSC
< R
.TSC
;