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