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 size_t NumReservations
= llvm::divideCeil(Reader
.size() - OffsetPtr
, 32U);
55 Records
.reserve(NumReservations
);
57 // Each record after the header will be 32 bytes, in the following format:
59 // (2) uint16 : record type
62 // (4) sint32 : function id
64 // (4) uint32 : thread id
65 // (4) uint32 : process id
67 while (Reader
.isValidOffset(OffsetPtr
)) {
68 if (!Reader
.isValidOffsetForDataOfSize(OffsetPtr
, 32))
69 return createStringError(
70 std::make_error_code(std::errc::executable_format_error
),
71 "Not enough bytes to read a full record at offset %" PRId64
".",
73 auto PreReadOffset
= OffsetPtr
;
74 auto RecordType
= Reader
.getU16(&OffsetPtr
);
75 if (OffsetPtr
== PreReadOffset
)
76 return createStringError(
77 std::make_error_code(std::errc::executable_format_error
),
78 "Failed reading record type at offset %" PRId64
".", OffsetPtr
);
81 case 0: { // Normal records.
82 Records
.emplace_back();
83 auto &Record
= Records
.back();
84 Record
.RecordType
= RecordType
;
86 PreReadOffset
= OffsetPtr
;
87 Record
.CPU
= Reader
.getU8(&OffsetPtr
);
88 if (OffsetPtr
== PreReadOffset
)
89 return createStringError(
90 std::make_error_code(std::errc::executable_format_error
),
91 "Failed reading CPU field at offset %" PRId64
".", OffsetPtr
);
93 PreReadOffset
= OffsetPtr
;
94 auto Type
= Reader
.getU8(&OffsetPtr
);
95 if (OffsetPtr
== PreReadOffset
)
96 return createStringError(
97 std::make_error_code(std::errc::executable_format_error
),
98 "Failed reading record type field at offset %" PRId64
".",
103 Record
.Type
= RecordTypes::ENTER
;
106 Record
.Type
= RecordTypes::EXIT
;
109 Record
.Type
= RecordTypes::TAIL_EXIT
;
112 Record
.Type
= RecordTypes::ENTER_ARG
;
115 return createStringError(
116 std::make_error_code(std::errc::executable_format_error
),
117 "Unknown record type '%d' at offset %" PRId64
".", Type
, OffsetPtr
);
120 PreReadOffset
= OffsetPtr
;
121 Record
.FuncId
= Reader
.getSigned(&OffsetPtr
, sizeof(int32_t));
122 if (OffsetPtr
== PreReadOffset
)
123 return createStringError(
124 std::make_error_code(std::errc::executable_format_error
),
125 "Failed reading function id field at offset %" PRId64
".",
128 PreReadOffset
= OffsetPtr
;
129 Record
.TSC
= Reader
.getU64(&OffsetPtr
);
130 if (OffsetPtr
== PreReadOffset
)
131 return createStringError(
132 std::make_error_code(std::errc::executable_format_error
),
133 "Failed reading TSC field at offset %" PRId64
".", OffsetPtr
);
135 PreReadOffset
= OffsetPtr
;
136 Record
.TId
= Reader
.getU32(&OffsetPtr
);
137 if (OffsetPtr
== PreReadOffset
)
138 return createStringError(
139 std::make_error_code(std::errc::executable_format_error
),
140 "Failed reading thread id field at offset %" PRId64
".", OffsetPtr
);
142 PreReadOffset
= OffsetPtr
;
143 Record
.PId
= Reader
.getU32(&OffsetPtr
);
144 if (OffsetPtr
== PreReadOffset
)
145 return createStringError(
146 std::make_error_code(std::errc::executable_format_error
),
147 "Failed reading process id at offset %" PRId64
".", OffsetPtr
);
151 case 1: { // Arg payload record.
152 auto &Record
= Records
.back();
154 // We skip the next two bytes of the record, because we don't need the
155 // type and the CPU record for arg payloads.
157 PreReadOffset
= OffsetPtr
;
158 int32_t FuncId
= Reader
.getSigned(&OffsetPtr
, sizeof(int32_t));
159 if (OffsetPtr
== PreReadOffset
)
160 return createStringError(
161 std::make_error_code(std::errc::executable_format_error
),
162 "Failed reading function id field at offset %" PRId64
".",
165 PreReadOffset
= OffsetPtr
;
166 auto TId
= Reader
.getU32(&OffsetPtr
);
167 if (OffsetPtr
== PreReadOffset
)
168 return createStringError(
169 std::make_error_code(std::errc::executable_format_error
),
170 "Failed reading thread id field at offset %" PRId64
".", OffsetPtr
);
172 PreReadOffset
= OffsetPtr
;
173 auto PId
= Reader
.getU32(&OffsetPtr
);
174 if (OffsetPtr
== PreReadOffset
)
175 return createStringError(
176 std::make_error_code(std::errc::executable_format_error
),
177 "Failed reading process id field at offset %" PRId64
".",
180 // Make a check for versions above 3 for the Pid field
181 if (Record
.FuncId
!= FuncId
|| Record
.TId
!= TId
||
182 (FileHeader
.Version
>= 3 ? Record
.PId
!= PId
: false))
183 return createStringError(
184 std::make_error_code(std::errc::executable_format_error
),
185 "Corrupted log, found arg payload following non-matching "
186 "function+thread record. Record for function %d != %d at offset "
188 Record
.FuncId
, FuncId
, OffsetPtr
);
190 PreReadOffset
= OffsetPtr
;
191 auto Arg
= Reader
.getU64(&OffsetPtr
);
192 if (OffsetPtr
== PreReadOffset
)
193 return createStringError(
194 std::make_error_code(std::errc::executable_format_error
),
195 "Failed reading argument payload at offset %" PRId64
".",
198 Record
.CallArgs
.push_back(Arg
);
202 return createStringError(
203 std::make_error_code(std::errc::executable_format_error
),
204 "Unknown record type '%d' at offset %" PRId64
".", RecordType
,
207 // Advance the offset pointer enough bytes to align to 32-byte records for
211 return Error::success();
214 /// Reads a log in FDR mode for version 1 of this binary format. FDR mode is
215 /// defined as part of the compiler-rt project in xray_fdr_logging.h, and such
216 /// a log consists of the familiar 32 bit XRayHeader, followed by sequences of
217 /// of interspersed 16 byte Metadata Records and 8 byte Function Records.
219 /// The following is an attempt to document the grammar of the format, which is
220 /// parsed by this function for little-endian machines. Since the format makes
221 /// use of BitFields, when we support big-endian architectures, we will need to
222 /// adjust not only the endianness parameter to llvm's RecordExtractor, but also
223 /// the bit twiddling logic, which is consistent with the little-endian
224 /// convention that BitFields within a struct will first be packed into the
225 /// least significant bits the address they belong to.
227 /// We expect a format complying with the grammar in the following pseudo-EBNF
228 /// in Version 1 of the FDR log.
230 /// FDRLog: XRayFileHeader ThreadBuffer*
231 /// XRayFileHeader: 32 bytes to identify the log as FDR with machine metadata.
232 /// Includes BufferSize
233 /// ThreadBuffer: NewBuffer WallClockTime NewCPUId FunctionSequence EOB
234 /// BufSize: 8 byte unsigned integer indicating how large the buffer is.
235 /// NewBuffer: 16 byte metadata record with Thread Id.
236 /// WallClockTime: 16 byte metadata record with human readable time.
237 /// Pid: 16 byte metadata record with Pid
238 /// NewCPUId: 16 byte metadata record with CPUId and a 64 bit TSC reading.
239 /// EOB: 16 byte record in a thread buffer plus mem garbage to fill BufSize.
240 /// FunctionSequence: NewCPUId | TSCWrap | FunctionRecord
241 /// TSCWrap: 16 byte metadata record with a full 64 bit TSC reading.
242 /// FunctionRecord: 8 byte record with FunctionId, entry/exit, and TSC delta.
244 /// In Version 2, we make the following changes:
246 /// ThreadBuffer: BufferExtents NewBuffer WallClockTime NewCPUId
248 /// BufferExtents: 16 byte metdata record describing how many usable bytes are
249 /// in the buffer. This is measured from the start of the buffer
250 /// and must always be at least 48 (bytes).
252 /// In Version 3, we make the following changes:
254 /// ThreadBuffer: BufferExtents NewBuffer WallClockTime Pid NewCPUId
256 /// EOB: *deprecated*
258 /// In Version 4, we make the following changes:
260 /// CustomEventRecord now includes the CPU data.
262 /// In Version 5, we make the following changes:
264 /// CustomEventRecord and TypedEventRecord now use TSC delta encoding similar to
265 /// what FunctionRecord instances use, and we no longer need to include the CPU
266 /// id in the CustomEventRecord.
268 Error
loadFDRLog(StringRef Data
, bool IsLittleEndian
,
269 XRayFileHeader
&FileHeader
, std::vector
<XRayRecord
> &Records
) {
271 if (Data
.size() < 32)
272 return createStringError(std::make_error_code(std::errc::invalid_argument
),
273 "Not enough bytes for an XRay FDR log.");
274 DataExtractor
DE(Data
, IsLittleEndian
, 8);
276 uint64_t OffsetPtr
= 0;
277 auto FileHeaderOrError
= readBinaryFormatHeader(DE
, OffsetPtr
);
278 if (!FileHeaderOrError
)
279 return FileHeaderOrError
.takeError();
280 FileHeader
= std::move(FileHeaderOrError
.get());
282 // First we load the records into memory.
283 std::vector
<std::unique_ptr
<Record
>> FDRRecords
;
286 FileBasedRecordProducer
P(FileHeader
, DE
, OffsetPtr
);
287 LogBuilderConsumer
C(FDRRecords
);
288 while (DE
.isValidOffsetForDataOfSize(OffsetPtr
, 1)) {
289 auto R
= P
.produce();
291 return R
.takeError();
292 if (auto E
= C
.consume(std::move(R
.get())))
297 // Next we index the records into blocks.
298 BlockIndexer::Index Index
;
300 BlockIndexer
Indexer(Index
);
301 for (auto &R
: FDRRecords
)
302 if (auto E
= R
->apply(Indexer
))
304 if (auto E
= Indexer
.flush())
308 // Then we verify the consistency of the blocks.
310 for (auto &PTB
: Index
) {
311 auto &Blocks
= PTB
.second
;
312 for (auto &B
: Blocks
) {
313 BlockVerifier Verifier
;
314 for (auto *R
: B
.Records
)
315 if (auto E
= R
->apply(Verifier
))
317 if (auto E
= Verifier
.verify())
323 // This is now the meat of the algorithm. Here we sort the blocks according to
324 // the Walltime record in each of the blocks for the same thread. This allows
325 // us to more consistently recreate the execution trace in temporal order.
326 // After the sort, we then reconstitute `Trace` records using a stateful
327 // visitor associated with a single process+thread pair.
329 for (auto &PTB
: Index
) {
330 auto &Blocks
= PTB
.second
;
331 llvm::sort(Blocks
, [](const BlockIndexer::Block
&L
,
332 const BlockIndexer::Block
&R
) {
333 return (L
.WallclockTime
->seconds() < R
.WallclockTime
->seconds() &&
334 L
.WallclockTime
->nanos() < R
.WallclockTime
->nanos());
336 auto Adder
= [&](const XRayRecord
&R
) { Records
.push_back(R
); };
337 TraceExpander
Expander(Adder
, FileHeader
.Version
);
338 for (auto &B
: Blocks
) {
339 for (auto *R
: B
.Records
)
340 if (auto E
= R
->apply(Expander
))
343 if (auto E
= Expander
.flush())
348 return Error::success();
351 Error
loadYAMLLog(StringRef Data
, XRayFileHeader
&FileHeader
,
352 std::vector
<XRayRecord
> &Records
) {
357 return make_error
<StringError
>("Failed loading YAML Data.", In
.error());
359 FileHeader
.Version
= Trace
.Header
.Version
;
360 FileHeader
.Type
= Trace
.Header
.Type
;
361 FileHeader
.ConstantTSC
= Trace
.Header
.ConstantTSC
;
362 FileHeader
.NonstopTSC
= Trace
.Header
.NonstopTSC
;
363 FileHeader
.CycleFrequency
= Trace
.Header
.CycleFrequency
;
365 if (FileHeader
.Version
!= 1)
366 return make_error
<StringError
>(
367 Twine("Unsupported XRay file version: ") + Twine(FileHeader
.Version
),
368 std::make_error_code(std::errc::invalid_argument
));
371 std::transform(Trace
.Records
.begin(), Trace
.Records
.end(),
372 std::back_inserter(Records
), [&](const YAMLXRayRecord
&R
) {
373 return XRayRecord
{R
.RecordType
, R
.CPU
, R
.Type
,
374 R
.FuncId
, R
.TSC
, R
.TId
,
375 R
.PId
, R
.CallArgs
, R
.Data
};
377 return Error::success();
381 Expected
<Trace
> llvm::xray::loadTraceFile(StringRef Filename
, bool Sort
) {
382 Expected
<sys::fs::file_t
> FdOrErr
= sys::fs::openNativeFileForRead(Filename
);
384 return FdOrErr
.takeError();
387 if (auto EC
= sys::fs::file_size(Filename
, FileSize
)) {
388 return make_error
<StringError
>(
389 Twine("Cannot read log from '") + Filename
+ "'", EC
);
392 return make_error
<StringError
>(
393 Twine("File '") + Filename
+ "' too small for XRay.",
394 std::make_error_code(std::errc::executable_format_error
));
397 // Map the opened file into memory and use a StringRef to access it later.
399 sys::fs::mapped_file_region
MappedFile(
400 *FdOrErr
, sys::fs::mapped_file_region::mapmode::readonly
, FileSize
, 0,
402 sys::fs::closeFile(*FdOrErr
);
404 return make_error
<StringError
>(
405 Twine("Cannot read log from '") + Filename
+ "'", EC
);
407 auto Data
= StringRef(MappedFile
.data(), MappedFile
.size());
409 // TODO: Lift the endianness and implementation selection here.
410 DataExtractor
LittleEndianDE(Data
, true, 8);
411 auto TraceOrError
= loadTrace(LittleEndianDE
, Sort
);
413 DataExtractor
BigEndianDE(Data
, false, 8);
414 consumeError(TraceOrError
.takeError());
415 TraceOrError
= loadTrace(BigEndianDE
, Sort
);
420 Expected
<Trace
> llvm::xray::loadTrace(const DataExtractor
&DE
, bool Sort
) {
421 // Attempt to detect the file type using file magic. We have a slight bias
422 // towards the binary format, and we do this by making sure that the first 4
423 // bytes of the binary file is some combination of the following byte
424 // patterns: (observe the code loading them assumes they're little endian)
426 // 0x01 0x00 0x00 0x00 - version 1, "naive" format
427 // 0x01 0x00 0x01 0x00 - version 1, "flight data recorder" format
428 // 0x02 0x00 0x01 0x00 - version 2, "flight data recorder" format
430 // YAML files don't typically have those first four bytes as valid text so we
431 // try loading assuming YAML if we don't find these bytes.
433 // Only if we can't load either the binary or the YAML format will we yield an
435 DataExtractor
HeaderExtractor(DE
.getData(), DE
.isLittleEndian(), 8);
436 uint64_t OffsetPtr
= 0;
437 uint16_t Version
= HeaderExtractor
.getU16(&OffsetPtr
);
438 uint16_t Type
= HeaderExtractor
.getU16(&OffsetPtr
);
440 enum BinaryFormatType
{ NAIVE_FORMAT
= 0, FLIGHT_DATA_RECORDER_FORMAT
= 1 };
445 if (Version
== 1 || Version
== 2 || Version
== 3) {
446 if (auto E
= loadNaiveFormatLog(DE
.getData(), DE
.isLittleEndian(),
447 T
.FileHeader
, T
.Records
))
450 return make_error
<StringError
>(
451 Twine("Unsupported version for Basic/Naive Mode logging: ") +
453 std::make_error_code(std::errc::executable_format_error
));
456 case FLIGHT_DATA_RECORDER_FORMAT
:
457 if (Version
>= 1 && Version
<= 5) {
458 if (auto E
= loadFDRLog(DE
.getData(), DE
.isLittleEndian(), T
.FileHeader
,
462 return make_error
<StringError
>(
463 Twine("Unsupported version for FDR Mode logging: ") + Twine(Version
),
464 std::make_error_code(std::errc::executable_format_error
));
468 if (auto E
= loadYAMLLog(DE
.getData(), T
.FileHeader
, T
.Records
))
473 llvm::stable_sort(T
.Records
, [&](const XRayRecord
&L
, const XRayRecord
&R
) {
474 return L
.TSC
< R
.TSC
;