xref: /freebsd/contrib/llvm-project/llvm/lib/XRay/Trace.cpp (revision 7a6dacaca14b62ca4b74406814becb87a3fefac0)
10b57cec5SDimitry Andric //===- Trace.cpp - XRay Trace Loading implementation. ---------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // XRay log reader implementation.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric #include "llvm/XRay/Trace.h"
130b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
140b57cec5SDimitry Andric #include "llvm/Support/DataExtractor.h"
150b57cec5SDimitry Andric #include "llvm/Support/Error.h"
160b57cec5SDimitry Andric #include "llvm/Support/FileSystem.h"
170b57cec5SDimitry Andric #include "llvm/XRay/BlockIndexer.h"
180b57cec5SDimitry Andric #include "llvm/XRay/BlockVerifier.h"
190b57cec5SDimitry Andric #include "llvm/XRay/FDRRecordConsumer.h"
200b57cec5SDimitry Andric #include "llvm/XRay/FDRRecordProducer.h"
210b57cec5SDimitry Andric #include "llvm/XRay/FDRRecords.h"
220b57cec5SDimitry Andric #include "llvm/XRay/FDRTraceExpander.h"
230b57cec5SDimitry Andric #include "llvm/XRay/FileHeaderReader.h"
240b57cec5SDimitry Andric #include "llvm/XRay/YAMLXRayRecord.h"
250b57cec5SDimitry Andric #include <memory>
260b57cec5SDimitry Andric #include <vector>
270b57cec5SDimitry Andric 
280b57cec5SDimitry Andric using namespace llvm;
290b57cec5SDimitry Andric using namespace llvm::xray;
300b57cec5SDimitry Andric using llvm::yaml::Input;
310b57cec5SDimitry Andric 
320b57cec5SDimitry Andric namespace {
330b57cec5SDimitry Andric 
340b57cec5SDimitry Andric Error loadNaiveFormatLog(StringRef Data, bool IsLittleEndian,
350b57cec5SDimitry Andric                          XRayFileHeader &FileHeader,
360b57cec5SDimitry Andric                          std::vector<XRayRecord> &Records) {
370b57cec5SDimitry Andric   if (Data.size() < 32)
380b57cec5SDimitry Andric     return make_error<StringError>(
390b57cec5SDimitry Andric         "Not enough bytes for an XRay log.",
400b57cec5SDimitry Andric         std::make_error_code(std::errc::invalid_argument));
410b57cec5SDimitry Andric 
420b57cec5SDimitry Andric   if (Data.size() - 32 == 0 || Data.size() % 32 != 0)
430b57cec5SDimitry Andric     return make_error<StringError>(
440b57cec5SDimitry Andric         "Invalid-sized XRay data.",
450b57cec5SDimitry Andric         std::make_error_code(std::errc::invalid_argument));
460b57cec5SDimitry Andric 
470b57cec5SDimitry Andric   DataExtractor Reader(Data, IsLittleEndian, 8);
488bcb0991SDimitry Andric   uint64_t OffsetPtr = 0;
490b57cec5SDimitry Andric   auto FileHeaderOrError = readBinaryFormatHeader(Reader, OffsetPtr);
500b57cec5SDimitry Andric   if (!FileHeaderOrError)
510b57cec5SDimitry Andric     return FileHeaderOrError.takeError();
520b57cec5SDimitry Andric   FileHeader = std::move(FileHeaderOrError.get());
530b57cec5SDimitry Andric 
54*7a6dacacSDimitry Andric   size_t NumReservations = llvm::divideCeil(Reader.size() - OffsetPtr, 32U);
55*7a6dacacSDimitry Andric   Records.reserve(NumReservations);
56*7a6dacacSDimitry Andric 
570b57cec5SDimitry Andric   // Each record after the header will be 32 bytes, in the following format:
580b57cec5SDimitry Andric   //
590b57cec5SDimitry Andric   //   (2)   uint16 : record type
600b57cec5SDimitry Andric   //   (1)   uint8  : cpu id
610b57cec5SDimitry Andric   //   (1)   uint8  : type
620b57cec5SDimitry Andric   //   (4)   sint32 : function id
630b57cec5SDimitry Andric   //   (8)   uint64 : tsc
640b57cec5SDimitry Andric   //   (4)   uint32 : thread id
650b57cec5SDimitry Andric   //   (4)   uint32 : process id
660b57cec5SDimitry Andric   //   (8)   -      : padding
670b57cec5SDimitry Andric   while (Reader.isValidOffset(OffsetPtr)) {
680b57cec5SDimitry Andric     if (!Reader.isValidOffsetForDataOfSize(OffsetPtr, 32))
690b57cec5SDimitry Andric       return createStringError(
700b57cec5SDimitry Andric           std::make_error_code(std::errc::executable_format_error),
718bcb0991SDimitry Andric           "Not enough bytes to read a full record at offset %" PRId64 ".",
728bcb0991SDimitry Andric           OffsetPtr);
730b57cec5SDimitry Andric     auto PreReadOffset = OffsetPtr;
740b57cec5SDimitry Andric     auto RecordType = Reader.getU16(&OffsetPtr);
750b57cec5SDimitry Andric     if (OffsetPtr == PreReadOffset)
760b57cec5SDimitry Andric       return createStringError(
770b57cec5SDimitry Andric           std::make_error_code(std::errc::executable_format_error),
788bcb0991SDimitry Andric           "Failed reading record type at offset %" PRId64 ".", OffsetPtr);
790b57cec5SDimitry Andric 
800b57cec5SDimitry Andric     switch (RecordType) {
810b57cec5SDimitry Andric     case 0: { // Normal records.
820b57cec5SDimitry Andric       Records.emplace_back();
830b57cec5SDimitry Andric       auto &Record = Records.back();
840b57cec5SDimitry Andric       Record.RecordType = RecordType;
850b57cec5SDimitry Andric 
860b57cec5SDimitry Andric       PreReadOffset = OffsetPtr;
870b57cec5SDimitry Andric       Record.CPU = Reader.getU8(&OffsetPtr);
880b57cec5SDimitry Andric       if (OffsetPtr == PreReadOffset)
890b57cec5SDimitry Andric         return createStringError(
900b57cec5SDimitry Andric             std::make_error_code(std::errc::executable_format_error),
918bcb0991SDimitry Andric             "Failed reading CPU field at offset %" PRId64 ".", OffsetPtr);
920b57cec5SDimitry Andric 
930b57cec5SDimitry Andric       PreReadOffset = OffsetPtr;
940b57cec5SDimitry Andric       auto Type = Reader.getU8(&OffsetPtr);
950b57cec5SDimitry Andric       if (OffsetPtr == PreReadOffset)
960b57cec5SDimitry Andric         return createStringError(
970b57cec5SDimitry Andric             std::make_error_code(std::errc::executable_format_error),
988bcb0991SDimitry Andric             "Failed reading record type field at offset %" PRId64 ".",
998bcb0991SDimitry Andric             OffsetPtr);
1000b57cec5SDimitry Andric 
1010b57cec5SDimitry Andric       switch (Type) {
1020b57cec5SDimitry Andric       case 0:
1030b57cec5SDimitry Andric         Record.Type = RecordTypes::ENTER;
1040b57cec5SDimitry Andric         break;
1050b57cec5SDimitry Andric       case 1:
1060b57cec5SDimitry Andric         Record.Type = RecordTypes::EXIT;
1070b57cec5SDimitry Andric         break;
1080b57cec5SDimitry Andric       case 2:
1090b57cec5SDimitry Andric         Record.Type = RecordTypes::TAIL_EXIT;
1100b57cec5SDimitry Andric         break;
1110b57cec5SDimitry Andric       case 3:
1120b57cec5SDimitry Andric         Record.Type = RecordTypes::ENTER_ARG;
1130b57cec5SDimitry Andric         break;
1140b57cec5SDimitry Andric       default:
1150b57cec5SDimitry Andric         return createStringError(
1160b57cec5SDimitry Andric             std::make_error_code(std::errc::executable_format_error),
1178bcb0991SDimitry Andric             "Unknown record type '%d' at offset %" PRId64 ".", Type, OffsetPtr);
1180b57cec5SDimitry Andric       }
1190b57cec5SDimitry Andric 
1200b57cec5SDimitry Andric       PreReadOffset = OffsetPtr;
1210b57cec5SDimitry Andric       Record.FuncId = Reader.getSigned(&OffsetPtr, sizeof(int32_t));
1220b57cec5SDimitry Andric       if (OffsetPtr == PreReadOffset)
1230b57cec5SDimitry Andric         return createStringError(
1240b57cec5SDimitry Andric             std::make_error_code(std::errc::executable_format_error),
1258bcb0991SDimitry Andric             "Failed reading function id field at offset %" PRId64 ".",
1268bcb0991SDimitry Andric             OffsetPtr);
1270b57cec5SDimitry Andric 
1280b57cec5SDimitry Andric       PreReadOffset = OffsetPtr;
1290b57cec5SDimitry Andric       Record.TSC = Reader.getU64(&OffsetPtr);
1300b57cec5SDimitry Andric       if (OffsetPtr == PreReadOffset)
1310b57cec5SDimitry Andric         return createStringError(
1320b57cec5SDimitry Andric             std::make_error_code(std::errc::executable_format_error),
1338bcb0991SDimitry Andric             "Failed reading TSC field at offset %" PRId64 ".", OffsetPtr);
1340b57cec5SDimitry Andric 
1350b57cec5SDimitry Andric       PreReadOffset = OffsetPtr;
1360b57cec5SDimitry Andric       Record.TId = Reader.getU32(&OffsetPtr);
1370b57cec5SDimitry Andric       if (OffsetPtr == PreReadOffset)
1380b57cec5SDimitry Andric         return createStringError(
1390b57cec5SDimitry Andric             std::make_error_code(std::errc::executable_format_error),
1408bcb0991SDimitry Andric             "Failed reading thread id field at offset %" PRId64 ".", OffsetPtr);
1410b57cec5SDimitry Andric 
1420b57cec5SDimitry Andric       PreReadOffset = OffsetPtr;
1430b57cec5SDimitry Andric       Record.PId = Reader.getU32(&OffsetPtr);
1440b57cec5SDimitry Andric       if (OffsetPtr == PreReadOffset)
1450b57cec5SDimitry Andric         return createStringError(
1460b57cec5SDimitry Andric             std::make_error_code(std::errc::executable_format_error),
1478bcb0991SDimitry Andric             "Failed reading process id at offset %" PRId64 ".", OffsetPtr);
1480b57cec5SDimitry Andric 
1490b57cec5SDimitry Andric       break;
1500b57cec5SDimitry Andric     }
1510b57cec5SDimitry Andric     case 1: { // Arg payload record.
1520b57cec5SDimitry Andric       auto &Record = Records.back();
1530b57cec5SDimitry Andric 
1540b57cec5SDimitry Andric       // We skip the next two bytes of the record, because we don't need the
1550b57cec5SDimitry Andric       // type and the CPU record for arg payloads.
1560b57cec5SDimitry Andric       OffsetPtr += 2;
1570b57cec5SDimitry Andric       PreReadOffset = OffsetPtr;
1580b57cec5SDimitry Andric       int32_t FuncId = Reader.getSigned(&OffsetPtr, sizeof(int32_t));
1590b57cec5SDimitry Andric       if (OffsetPtr == PreReadOffset)
1600b57cec5SDimitry Andric         return createStringError(
1610b57cec5SDimitry Andric             std::make_error_code(std::errc::executable_format_error),
1628bcb0991SDimitry Andric             "Failed reading function id field at offset %" PRId64 ".",
1638bcb0991SDimitry Andric             OffsetPtr);
1640b57cec5SDimitry Andric 
1650b57cec5SDimitry Andric       PreReadOffset = OffsetPtr;
1660b57cec5SDimitry Andric       auto TId = Reader.getU32(&OffsetPtr);
1670b57cec5SDimitry Andric       if (OffsetPtr == PreReadOffset)
1680b57cec5SDimitry Andric         return createStringError(
1690b57cec5SDimitry Andric             std::make_error_code(std::errc::executable_format_error),
1708bcb0991SDimitry Andric             "Failed reading thread id field at offset %" PRId64 ".", OffsetPtr);
1710b57cec5SDimitry Andric 
1720b57cec5SDimitry Andric       PreReadOffset = OffsetPtr;
1730b57cec5SDimitry Andric       auto PId = Reader.getU32(&OffsetPtr);
1740b57cec5SDimitry Andric       if (OffsetPtr == PreReadOffset)
1750b57cec5SDimitry Andric         return createStringError(
1760b57cec5SDimitry Andric             std::make_error_code(std::errc::executable_format_error),
1778bcb0991SDimitry Andric             "Failed reading process id field at offset %" PRId64 ".",
1788bcb0991SDimitry Andric             OffsetPtr);
1790b57cec5SDimitry Andric 
1800b57cec5SDimitry Andric       // Make a check for versions above 3 for the Pid field
1810b57cec5SDimitry Andric       if (Record.FuncId != FuncId || Record.TId != TId ||
1820b57cec5SDimitry Andric           (FileHeader.Version >= 3 ? Record.PId != PId : false))
1830b57cec5SDimitry Andric         return createStringError(
1840b57cec5SDimitry Andric             std::make_error_code(std::errc::executable_format_error),
1850b57cec5SDimitry Andric             "Corrupted log, found arg payload following non-matching "
1860b57cec5SDimitry Andric             "function+thread record. Record for function %d != %d at offset "
1878bcb0991SDimitry Andric             "%" PRId64 ".",
1880b57cec5SDimitry Andric             Record.FuncId, FuncId, OffsetPtr);
1890b57cec5SDimitry Andric 
1900b57cec5SDimitry Andric       PreReadOffset = OffsetPtr;
1910b57cec5SDimitry Andric       auto Arg = Reader.getU64(&OffsetPtr);
1920b57cec5SDimitry Andric       if (OffsetPtr == PreReadOffset)
1930b57cec5SDimitry Andric         return createStringError(
1940b57cec5SDimitry Andric             std::make_error_code(std::errc::executable_format_error),
1958bcb0991SDimitry Andric             "Failed reading argument payload at offset %" PRId64 ".",
1968bcb0991SDimitry Andric             OffsetPtr);
1970b57cec5SDimitry Andric 
1980b57cec5SDimitry Andric       Record.CallArgs.push_back(Arg);
1990b57cec5SDimitry Andric       break;
2000b57cec5SDimitry Andric     }
2010b57cec5SDimitry Andric     default:
2020b57cec5SDimitry Andric       return createStringError(
2030b57cec5SDimitry Andric           std::make_error_code(std::errc::executable_format_error),
2048bcb0991SDimitry Andric           "Unknown record type '%d' at offset %" PRId64 ".", RecordType,
2058bcb0991SDimitry Andric           OffsetPtr);
2060b57cec5SDimitry Andric     }
2070b57cec5SDimitry Andric     // Advance the offset pointer enough bytes to align to 32-byte records for
2080b57cec5SDimitry Andric     // basic mode logs.
2090b57cec5SDimitry Andric     OffsetPtr += 8;
2100b57cec5SDimitry Andric   }
2110b57cec5SDimitry Andric   return Error::success();
2120b57cec5SDimitry Andric }
2130b57cec5SDimitry Andric 
2140b57cec5SDimitry Andric /// Reads a log in FDR mode for version 1 of this binary format. FDR mode is
2150b57cec5SDimitry Andric /// defined as part of the compiler-rt project in xray_fdr_logging.h, and such
2160b57cec5SDimitry Andric /// a log consists of the familiar 32 bit XRayHeader, followed by sequences of
2170b57cec5SDimitry Andric /// of interspersed 16 byte Metadata Records and 8 byte Function Records.
2180b57cec5SDimitry Andric ///
2190b57cec5SDimitry Andric /// The following is an attempt to document the grammar of the format, which is
2200b57cec5SDimitry Andric /// parsed by this function for little-endian machines. Since the format makes
2210b57cec5SDimitry Andric /// use of BitFields, when we support big-endian architectures, we will need to
2220b57cec5SDimitry Andric /// adjust not only the endianness parameter to llvm's RecordExtractor, but also
2230b57cec5SDimitry Andric /// the bit twiddling logic, which is consistent with the little-endian
2240b57cec5SDimitry Andric /// convention that BitFields within a struct will first be packed into the
2250b57cec5SDimitry Andric /// least significant bits the address they belong to.
2260b57cec5SDimitry Andric ///
2270b57cec5SDimitry Andric /// We expect a format complying with the grammar in the following pseudo-EBNF
2280b57cec5SDimitry Andric /// in Version 1 of the FDR log.
2290b57cec5SDimitry Andric ///
2300b57cec5SDimitry Andric /// FDRLog: XRayFileHeader ThreadBuffer*
2310b57cec5SDimitry Andric /// XRayFileHeader: 32 bytes to identify the log as FDR with machine metadata.
2320b57cec5SDimitry Andric ///     Includes BufferSize
2330b57cec5SDimitry Andric /// ThreadBuffer: NewBuffer WallClockTime NewCPUId FunctionSequence EOB
2340b57cec5SDimitry Andric /// BufSize: 8 byte unsigned integer indicating how large the buffer is.
2350b57cec5SDimitry Andric /// NewBuffer: 16 byte metadata record with Thread Id.
2360b57cec5SDimitry Andric /// WallClockTime: 16 byte metadata record with human readable time.
2370b57cec5SDimitry Andric /// Pid: 16 byte metadata record with Pid
2380b57cec5SDimitry Andric /// NewCPUId: 16 byte metadata record with CPUId and a 64 bit TSC reading.
2390b57cec5SDimitry Andric /// EOB: 16 byte record in a thread buffer plus mem garbage to fill BufSize.
2400b57cec5SDimitry Andric /// FunctionSequence: NewCPUId | TSCWrap | FunctionRecord
2410b57cec5SDimitry Andric /// TSCWrap: 16 byte metadata record with a full 64 bit TSC reading.
2420b57cec5SDimitry Andric /// FunctionRecord: 8 byte record with FunctionId, entry/exit, and TSC delta.
2430b57cec5SDimitry Andric ///
2440b57cec5SDimitry Andric /// In Version 2, we make the following changes:
2450b57cec5SDimitry Andric ///
2460b57cec5SDimitry Andric /// ThreadBuffer: BufferExtents NewBuffer WallClockTime NewCPUId
2470b57cec5SDimitry Andric ///               FunctionSequence
2480b57cec5SDimitry Andric /// BufferExtents: 16 byte metdata record describing how many usable bytes are
2490b57cec5SDimitry Andric ///                in the buffer. This is measured from the start of the buffer
2500b57cec5SDimitry Andric ///                and must always be at least 48 (bytes).
2510b57cec5SDimitry Andric ///
2520b57cec5SDimitry Andric /// In Version 3, we make the following changes:
2530b57cec5SDimitry Andric ///
2540b57cec5SDimitry Andric /// ThreadBuffer: BufferExtents NewBuffer WallClockTime Pid NewCPUId
2550b57cec5SDimitry Andric ///               FunctionSequence
2560b57cec5SDimitry Andric /// EOB: *deprecated*
2570b57cec5SDimitry Andric ///
2580b57cec5SDimitry Andric /// In Version 4, we make the following changes:
2590b57cec5SDimitry Andric ///
2600b57cec5SDimitry Andric /// CustomEventRecord now includes the CPU data.
2610b57cec5SDimitry Andric ///
2620b57cec5SDimitry Andric /// In Version 5, we make the following changes:
2630b57cec5SDimitry Andric ///
2640b57cec5SDimitry Andric /// CustomEventRecord and TypedEventRecord now use TSC delta encoding similar to
2650b57cec5SDimitry Andric /// what FunctionRecord instances use, and we no longer need to include the CPU
2660b57cec5SDimitry Andric /// id in the CustomEventRecord.
2670b57cec5SDimitry Andric ///
2680b57cec5SDimitry Andric Error loadFDRLog(StringRef Data, bool IsLittleEndian,
2690b57cec5SDimitry Andric                  XRayFileHeader &FileHeader, std::vector<XRayRecord> &Records) {
2700b57cec5SDimitry Andric 
2710b57cec5SDimitry Andric   if (Data.size() < 32)
2720b57cec5SDimitry Andric     return createStringError(std::make_error_code(std::errc::invalid_argument),
2730b57cec5SDimitry Andric                              "Not enough bytes for an XRay FDR log.");
2740b57cec5SDimitry Andric   DataExtractor DE(Data, IsLittleEndian, 8);
2750b57cec5SDimitry Andric 
2768bcb0991SDimitry Andric   uint64_t OffsetPtr = 0;
2770b57cec5SDimitry Andric   auto FileHeaderOrError = readBinaryFormatHeader(DE, OffsetPtr);
2780b57cec5SDimitry Andric   if (!FileHeaderOrError)
2790b57cec5SDimitry Andric     return FileHeaderOrError.takeError();
2800b57cec5SDimitry Andric   FileHeader = std::move(FileHeaderOrError.get());
2810b57cec5SDimitry Andric 
2820b57cec5SDimitry Andric   // First we load the records into memory.
2830b57cec5SDimitry Andric   std::vector<std::unique_ptr<Record>> FDRRecords;
2840b57cec5SDimitry Andric 
2850b57cec5SDimitry Andric   {
2860b57cec5SDimitry Andric     FileBasedRecordProducer P(FileHeader, DE, OffsetPtr);
2870b57cec5SDimitry Andric     LogBuilderConsumer C(FDRRecords);
2880b57cec5SDimitry Andric     while (DE.isValidOffsetForDataOfSize(OffsetPtr, 1)) {
2890b57cec5SDimitry Andric       auto R = P.produce();
2900b57cec5SDimitry Andric       if (!R)
2910b57cec5SDimitry Andric         return R.takeError();
2920b57cec5SDimitry Andric       if (auto E = C.consume(std::move(R.get())))
2930b57cec5SDimitry Andric         return E;
2940b57cec5SDimitry Andric     }
2950b57cec5SDimitry Andric   }
2960b57cec5SDimitry Andric 
2970b57cec5SDimitry Andric   // Next we index the records into blocks.
2980b57cec5SDimitry Andric   BlockIndexer::Index Index;
2990b57cec5SDimitry Andric   {
3000b57cec5SDimitry Andric     BlockIndexer Indexer(Index);
3010b57cec5SDimitry Andric     for (auto &R : FDRRecords)
3020b57cec5SDimitry Andric       if (auto E = R->apply(Indexer))
3030b57cec5SDimitry Andric         return E;
3040b57cec5SDimitry Andric     if (auto E = Indexer.flush())
3050b57cec5SDimitry Andric       return E;
3060b57cec5SDimitry Andric   }
3070b57cec5SDimitry Andric 
3080b57cec5SDimitry Andric   // Then we verify the consistency of the blocks.
3090b57cec5SDimitry Andric   {
3100b57cec5SDimitry Andric     for (auto &PTB : Index) {
3110b57cec5SDimitry Andric       auto &Blocks = PTB.second;
3120b57cec5SDimitry Andric       for (auto &B : Blocks) {
3130b57cec5SDimitry Andric         BlockVerifier Verifier;
3140b57cec5SDimitry Andric         for (auto *R : B.Records)
3150b57cec5SDimitry Andric           if (auto E = R->apply(Verifier))
3160b57cec5SDimitry Andric             return E;
3170b57cec5SDimitry Andric         if (auto E = Verifier.verify())
3180b57cec5SDimitry Andric           return E;
3190b57cec5SDimitry Andric       }
3200b57cec5SDimitry Andric     }
3210b57cec5SDimitry Andric   }
3220b57cec5SDimitry Andric 
3230b57cec5SDimitry Andric   // This is now the meat of the algorithm. Here we sort the blocks according to
3240b57cec5SDimitry Andric   // the Walltime record in each of the blocks for the same thread. This allows
3250b57cec5SDimitry Andric   // us to more consistently recreate the execution trace in temporal order.
3260b57cec5SDimitry Andric   // After the sort, we then reconstitute `Trace` records using a stateful
3270b57cec5SDimitry Andric   // visitor associated with a single process+thread pair.
3280b57cec5SDimitry Andric   {
3290b57cec5SDimitry Andric     for (auto &PTB : Index) {
3300b57cec5SDimitry Andric       auto &Blocks = PTB.second;
3310b57cec5SDimitry Andric       llvm::sort(Blocks, [](const BlockIndexer::Block &L,
3320b57cec5SDimitry Andric                             const BlockIndexer::Block &R) {
3330b57cec5SDimitry Andric         return (L.WallclockTime->seconds() < R.WallclockTime->seconds() &&
3340b57cec5SDimitry Andric                 L.WallclockTime->nanos() < R.WallclockTime->nanos());
3350b57cec5SDimitry Andric       });
3360b57cec5SDimitry Andric       auto Adder = [&](const XRayRecord &R) { Records.push_back(R); };
3370b57cec5SDimitry Andric       TraceExpander Expander(Adder, FileHeader.Version);
3380b57cec5SDimitry Andric       for (auto &B : Blocks) {
3390b57cec5SDimitry Andric         for (auto *R : B.Records)
3400b57cec5SDimitry Andric           if (auto E = R->apply(Expander))
3410b57cec5SDimitry Andric             return E;
3420b57cec5SDimitry Andric       }
3430b57cec5SDimitry Andric       if (auto E = Expander.flush())
3440b57cec5SDimitry Andric         return E;
3450b57cec5SDimitry Andric     }
3460b57cec5SDimitry Andric   }
3470b57cec5SDimitry Andric 
3480b57cec5SDimitry Andric   return Error::success();
3490b57cec5SDimitry Andric }
3500b57cec5SDimitry Andric 
3510b57cec5SDimitry Andric Error loadYAMLLog(StringRef Data, XRayFileHeader &FileHeader,
3520b57cec5SDimitry Andric                   std::vector<XRayRecord> &Records) {
3530b57cec5SDimitry Andric   YAMLXRayTrace Trace;
3540b57cec5SDimitry Andric   Input In(Data);
3550b57cec5SDimitry Andric   In >> Trace;
3560b57cec5SDimitry Andric   if (In.error())
3570b57cec5SDimitry Andric     return make_error<StringError>("Failed loading YAML Data.", In.error());
3580b57cec5SDimitry Andric 
3590b57cec5SDimitry Andric   FileHeader.Version = Trace.Header.Version;
3600b57cec5SDimitry Andric   FileHeader.Type = Trace.Header.Type;
3610b57cec5SDimitry Andric   FileHeader.ConstantTSC = Trace.Header.ConstantTSC;
3620b57cec5SDimitry Andric   FileHeader.NonstopTSC = Trace.Header.NonstopTSC;
3630b57cec5SDimitry Andric   FileHeader.CycleFrequency = Trace.Header.CycleFrequency;
3640b57cec5SDimitry Andric 
3650b57cec5SDimitry Andric   if (FileHeader.Version != 1)
3660b57cec5SDimitry Andric     return make_error<StringError>(
3670b57cec5SDimitry Andric         Twine("Unsupported XRay file version: ") + Twine(FileHeader.Version),
3680b57cec5SDimitry Andric         std::make_error_code(std::errc::invalid_argument));
3690b57cec5SDimitry Andric 
3700b57cec5SDimitry Andric   Records.clear();
3710b57cec5SDimitry Andric   std::transform(Trace.Records.begin(), Trace.Records.end(),
3720b57cec5SDimitry Andric                  std::back_inserter(Records), [&](const YAMLXRayRecord &R) {
3730b57cec5SDimitry Andric                    return XRayRecord{R.RecordType, R.CPU,      R.Type,
3740b57cec5SDimitry Andric                                      R.FuncId,     R.TSC,      R.TId,
3750b57cec5SDimitry Andric                                      R.PId,        R.CallArgs, R.Data};
3760b57cec5SDimitry Andric                  });
3770b57cec5SDimitry Andric   return Error::success();
3780b57cec5SDimitry Andric }
3790b57cec5SDimitry Andric } // namespace
3800b57cec5SDimitry Andric 
3810b57cec5SDimitry Andric Expected<Trace> llvm::xray::loadTraceFile(StringRef Filename, bool Sort) {
3820b57cec5SDimitry Andric   Expected<sys::fs::file_t> FdOrErr = sys::fs::openNativeFileForRead(Filename);
3830b57cec5SDimitry Andric   if (!FdOrErr)
3840b57cec5SDimitry Andric     return FdOrErr.takeError();
3850b57cec5SDimitry Andric 
3860b57cec5SDimitry Andric   uint64_t FileSize;
3870b57cec5SDimitry Andric   if (auto EC = sys::fs::file_size(Filename, FileSize)) {
3880b57cec5SDimitry Andric     return make_error<StringError>(
3890b57cec5SDimitry Andric         Twine("Cannot read log from '") + Filename + "'", EC);
3900b57cec5SDimitry Andric   }
3910b57cec5SDimitry Andric   if (FileSize < 4) {
3920b57cec5SDimitry Andric     return make_error<StringError>(
3930b57cec5SDimitry Andric         Twine("File '") + Filename + "' too small for XRay.",
3940b57cec5SDimitry Andric         std::make_error_code(std::errc::executable_format_error));
3950b57cec5SDimitry Andric   }
3960b57cec5SDimitry Andric 
3970b57cec5SDimitry Andric   // Map the opened file into memory and use a StringRef to access it later.
3980b57cec5SDimitry Andric   std::error_code EC;
3990b57cec5SDimitry Andric   sys::fs::mapped_file_region MappedFile(
4000b57cec5SDimitry Andric       *FdOrErr, sys::fs::mapped_file_region::mapmode::readonly, FileSize, 0,
4010b57cec5SDimitry Andric       EC);
4020b57cec5SDimitry Andric   sys::fs::closeFile(*FdOrErr);
4030b57cec5SDimitry Andric   if (EC) {
4040b57cec5SDimitry Andric     return make_error<StringError>(
4050b57cec5SDimitry Andric         Twine("Cannot read log from '") + Filename + "'", EC);
4060b57cec5SDimitry Andric   }
4070b57cec5SDimitry Andric   auto Data = StringRef(MappedFile.data(), MappedFile.size());
4080b57cec5SDimitry Andric 
4090b57cec5SDimitry Andric   // TODO: Lift the endianness and implementation selection here.
4100b57cec5SDimitry Andric   DataExtractor LittleEndianDE(Data, true, 8);
4110b57cec5SDimitry Andric   auto TraceOrError = loadTrace(LittleEndianDE, Sort);
4120b57cec5SDimitry Andric   if (!TraceOrError) {
4130b57cec5SDimitry Andric     DataExtractor BigEndianDE(Data, false, 8);
4145ffd83dbSDimitry Andric     consumeError(TraceOrError.takeError());
4150b57cec5SDimitry Andric     TraceOrError = loadTrace(BigEndianDE, Sort);
4160b57cec5SDimitry Andric   }
4170b57cec5SDimitry Andric   return TraceOrError;
4180b57cec5SDimitry Andric }
4190b57cec5SDimitry Andric 
4200b57cec5SDimitry Andric Expected<Trace> llvm::xray::loadTrace(const DataExtractor &DE, bool Sort) {
4210b57cec5SDimitry Andric   // Attempt to detect the file type using file magic. We have a slight bias
4220b57cec5SDimitry Andric   // towards the binary format, and we do this by making sure that the first 4
4230b57cec5SDimitry Andric   // bytes of the binary file is some combination of the following byte
4240b57cec5SDimitry Andric   // patterns: (observe the code loading them assumes they're little endian)
4250b57cec5SDimitry Andric   //
4260b57cec5SDimitry Andric   //   0x01 0x00 0x00 0x00 - version 1, "naive" format
4270b57cec5SDimitry Andric   //   0x01 0x00 0x01 0x00 - version 1, "flight data recorder" format
4280b57cec5SDimitry Andric   //   0x02 0x00 0x01 0x00 - version 2, "flight data recorder" format
4290b57cec5SDimitry Andric   //
4300b57cec5SDimitry Andric   // YAML files don't typically have those first four bytes as valid text so we
4310b57cec5SDimitry Andric   // try loading assuming YAML if we don't find these bytes.
4320b57cec5SDimitry Andric   //
4330b57cec5SDimitry Andric   // Only if we can't load either the binary or the YAML format will we yield an
4340b57cec5SDimitry Andric   // error.
4350b57cec5SDimitry Andric   DataExtractor HeaderExtractor(DE.getData(), DE.isLittleEndian(), 8);
4368bcb0991SDimitry Andric   uint64_t OffsetPtr = 0;
4370b57cec5SDimitry Andric   uint16_t Version = HeaderExtractor.getU16(&OffsetPtr);
4380b57cec5SDimitry Andric   uint16_t Type = HeaderExtractor.getU16(&OffsetPtr);
4390b57cec5SDimitry Andric 
4400b57cec5SDimitry Andric   enum BinaryFormatType { NAIVE_FORMAT = 0, FLIGHT_DATA_RECORDER_FORMAT = 1 };
4410b57cec5SDimitry Andric 
4420b57cec5SDimitry Andric   Trace T;
4430b57cec5SDimitry Andric   switch (Type) {
4440b57cec5SDimitry Andric   case NAIVE_FORMAT:
4450b57cec5SDimitry Andric     if (Version == 1 || Version == 2 || Version == 3) {
4460b57cec5SDimitry Andric       if (auto E = loadNaiveFormatLog(DE.getData(), DE.isLittleEndian(),
4470b57cec5SDimitry Andric                                       T.FileHeader, T.Records))
4480b57cec5SDimitry Andric         return std::move(E);
4490b57cec5SDimitry Andric     } else {
4500b57cec5SDimitry Andric       return make_error<StringError>(
4510b57cec5SDimitry Andric           Twine("Unsupported version for Basic/Naive Mode logging: ") +
4520b57cec5SDimitry Andric               Twine(Version),
4530b57cec5SDimitry Andric           std::make_error_code(std::errc::executable_format_error));
4540b57cec5SDimitry Andric     }
4550b57cec5SDimitry Andric     break;
4560b57cec5SDimitry Andric   case FLIGHT_DATA_RECORDER_FORMAT:
4570b57cec5SDimitry Andric     if (Version >= 1 && Version <= 5) {
4580b57cec5SDimitry Andric       if (auto E = loadFDRLog(DE.getData(), DE.isLittleEndian(), T.FileHeader,
4590b57cec5SDimitry Andric                               T.Records))
4600b57cec5SDimitry Andric         return std::move(E);
4610b57cec5SDimitry Andric     } else {
4620b57cec5SDimitry Andric       return make_error<StringError>(
4630b57cec5SDimitry Andric           Twine("Unsupported version for FDR Mode logging: ") + Twine(Version),
4640b57cec5SDimitry Andric           std::make_error_code(std::errc::executable_format_error));
4650b57cec5SDimitry Andric     }
4660b57cec5SDimitry Andric     break;
4670b57cec5SDimitry Andric   default:
4680b57cec5SDimitry Andric     if (auto E = loadYAMLLog(DE.getData(), T.FileHeader, T.Records))
4690b57cec5SDimitry Andric       return std::move(E);
4700b57cec5SDimitry Andric   }
4710b57cec5SDimitry Andric 
4720b57cec5SDimitry Andric   if (Sort)
4730b57cec5SDimitry Andric     llvm::stable_sort(T.Records, [&](const XRayRecord &L, const XRayRecord &R) {
4740b57cec5SDimitry Andric       return L.TSC < R.TSC;
4750b57cec5SDimitry Andric     });
4760b57cec5SDimitry Andric 
4770b57cec5SDimitry Andric   return std::move(T);
4780b57cec5SDimitry Andric }
479