1 //===- Trace.cpp - XRay Trace Loading implementation. ---------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // XRay log reader implementation. 11 // 12 //===----------------------------------------------------------------------===// 13 #include "llvm/XRay/Trace.h" 14 #include "llvm/ADT/STLExtras.h" 15 #include "llvm/Support/DataExtractor.h" 16 #include "llvm/Support/Error.h" 17 #include "llvm/Support/FileSystem.h" 18 #include "llvm/XRay/BlockIndexer.h" 19 #include "llvm/XRay/BlockVerifier.h" 20 #include "llvm/XRay/FDRRecordConsumer.h" 21 #include "llvm/XRay/FDRRecordProducer.h" 22 #include "llvm/XRay/FDRRecords.h" 23 #include "llvm/XRay/FDRTraceExpander.h" 24 #include "llvm/XRay/FileHeaderReader.h" 25 #include "llvm/XRay/YAMLXRayRecord.h" 26 #include <memory> 27 #include <vector> 28 29 using namespace llvm; 30 using namespace llvm::xray; 31 using llvm::yaml::Input; 32 33 namespace { 34 using XRayRecordStorage = 35 std::aligned_storage<sizeof(XRayRecord), alignof(XRayRecord)>::type; 36 37 Error loadNaiveFormatLog(StringRef Data, bool IsLittleEndian, 38 XRayFileHeader &FileHeader, 39 std::vector<XRayRecord> &Records) { 40 if (Data.size() < 32) 41 return make_error<StringError>( 42 "Not enough bytes for an XRay log.", 43 std::make_error_code(std::errc::invalid_argument)); 44 45 if (Data.size() - 32 == 0 || Data.size() % 32 != 0) 46 return make_error<StringError>( 47 "Invalid-sized XRay data.", 48 std::make_error_code(std::errc::invalid_argument)); 49 50 DataExtractor Reader(Data, IsLittleEndian, 8); 51 uint32_t OffsetPtr = 0; 52 auto FileHeaderOrError = readBinaryFormatHeader(Reader, OffsetPtr); 53 if (!FileHeaderOrError) 54 return FileHeaderOrError.takeError(); 55 FileHeader = std::move(FileHeaderOrError.get()); 56 57 // Each record after the header will be 32 bytes, in the following format: 58 // 59 // (2) uint16 : record type 60 // (1) uint8 : cpu id 61 // (1) uint8 : type 62 // (4) sint32 : function id 63 // (8) uint64 : tsc 64 // (4) uint32 : thread id 65 // (4) uint32 : process id 66 // (8) - : padding 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 %d.", OffsetPtr); 72 auto PreReadOffset = OffsetPtr; 73 auto RecordType = Reader.getU16(&OffsetPtr); 74 if (OffsetPtr == PreReadOffset) 75 return createStringError( 76 std::make_error_code(std::errc::executable_format_error), 77 "Failed reading record type at offset %d.", OffsetPtr); 78 79 switch (RecordType) { 80 case 0: { // Normal records. 81 Records.emplace_back(); 82 auto &Record = Records.back(); 83 Record.RecordType = RecordType; 84 85 PreReadOffset = OffsetPtr; 86 Record.CPU = Reader.getU8(&OffsetPtr); 87 if (OffsetPtr == PreReadOffset) 88 return createStringError( 89 std::make_error_code(std::errc::executable_format_error), 90 "Failed reading CPU field at offset %d.", OffsetPtr); 91 92 PreReadOffset = OffsetPtr; 93 auto Type = Reader.getU8(&OffsetPtr); 94 if (OffsetPtr == PreReadOffset) 95 return createStringError( 96 std::make_error_code(std::errc::executable_format_error), 97 "Failed reading record type field at offset %d.", OffsetPtr); 98 99 switch (Type) { 100 case 0: 101 Record.Type = RecordTypes::ENTER; 102 break; 103 case 1: 104 Record.Type = RecordTypes::EXIT; 105 break; 106 case 2: 107 Record.Type = RecordTypes::TAIL_EXIT; 108 break; 109 case 3: 110 Record.Type = RecordTypes::ENTER_ARG; 111 break; 112 default: 113 return createStringError( 114 std::make_error_code(std::errc::executable_format_error), 115 "Unknown record type '%d' at offset %d.", Type, OffsetPtr); 116 } 117 118 PreReadOffset = OffsetPtr; 119 Record.FuncId = Reader.getSigned(&OffsetPtr, sizeof(int32_t)); 120 if (OffsetPtr == PreReadOffset) 121 return createStringError( 122 std::make_error_code(std::errc::executable_format_error), 123 "Failed reading function id field at offset %d.", OffsetPtr); 124 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 %d.", OffsetPtr); 131 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 %d.", OffsetPtr); 138 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 %d.", OffsetPtr); 145 146 break; 147 } 148 case 1: { // Arg payload record. 149 auto &Record = Records.back(); 150 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. 153 OffsetPtr += 2; 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 %d.", OffsetPtr); 160 161 PreReadOffset = OffsetPtr; 162 auto TId = Reader.getU32(&OffsetPtr); 163 if (OffsetPtr == PreReadOffset) 164 return createStringError( 165 std::make_error_code(std::errc::executable_format_error), 166 "Failed reading thread id field at offset %d.", OffsetPtr); 167 168 PreReadOffset = OffsetPtr; 169 auto PId = Reader.getU32(&OffsetPtr); 170 if (OffsetPtr == PreReadOffset) 171 return createStringError( 172 std::make_error_code(std::errc::executable_format_error), 173 "Failed reading process id field at offset %d.", OffsetPtr); 174 175 // Make a check for versions above 3 for the Pid field 176 if (Record.FuncId != FuncId || Record.TId != TId || 177 (FileHeader.Version >= 3 ? Record.PId != PId : false)) 178 return createStringError( 179 std::make_error_code(std::errc::executable_format_error), 180 "Corrupted log, found arg payload following non-matching " 181 "function+thread record. Record for function %d != %d at offset " 182 "%d", 183 Record.FuncId, FuncId, OffsetPtr); 184 185 PreReadOffset = OffsetPtr; 186 auto Arg = Reader.getU64(&OffsetPtr); 187 if (OffsetPtr == PreReadOffset) 188 return createStringError( 189 std::make_error_code(std::errc::executable_format_error), 190 "Failed reading argument payload at offset %d.", OffsetPtr); 191 192 Record.CallArgs.push_back(Arg); 193 break; 194 } 195 default: 196 return createStringError( 197 std::make_error_code(std::errc::executable_format_error), 198 "Unknown record type '%d' at offset %d.", RecordType, OffsetPtr); 199 } 200 // Advance the offset pointer enough bytes to align to 32-byte records for 201 // basic mode logs. 202 OffsetPtr += 8; 203 } 204 return Error::success(); 205 } 206 207 /// Reads a log in FDR mode for version 1 of this binary format. FDR mode is 208 /// defined as part of the compiler-rt project in xray_fdr_logging.h, and such 209 /// a log consists of the familiar 32 bit XRayHeader, followed by sequences of 210 /// of interspersed 16 byte Metadata Records and 8 byte Function Records. 211 /// 212 /// The following is an attempt to document the grammar of the format, which is 213 /// parsed by this function for little-endian machines. Since the format makes 214 /// use of BitFields, when we support big-endian architectures, we will need to 215 /// adjust not only the endianness parameter to llvm's RecordExtractor, but also 216 /// the bit twiddling logic, which is consistent with the little-endian 217 /// convention that BitFields within a struct will first be packed into the 218 /// least significant bits the address they belong to. 219 /// 220 /// We expect a format complying with the grammar in the following pseudo-EBNF 221 /// in Version 1 of the FDR log. 222 /// 223 /// FDRLog: XRayFileHeader ThreadBuffer* 224 /// XRayFileHeader: 32 bytes to identify the log as FDR with machine metadata. 225 /// Includes BufferSize 226 /// ThreadBuffer: NewBuffer WallClockTime NewCPUId FunctionSequence EOB 227 /// BufSize: 8 byte unsigned integer indicating how large the buffer is. 228 /// NewBuffer: 16 byte metadata record with Thread Id. 229 /// WallClockTime: 16 byte metadata record with human readable time. 230 /// Pid: 16 byte metadata record with Pid 231 /// NewCPUId: 16 byte metadata record with CPUId and a 64 bit TSC reading. 232 /// EOB: 16 byte record in a thread buffer plus mem garbage to fill BufSize. 233 /// FunctionSequence: NewCPUId | TSCWrap | FunctionRecord 234 /// TSCWrap: 16 byte metadata record with a full 64 bit TSC reading. 235 /// FunctionRecord: 8 byte record with FunctionId, entry/exit, and TSC delta. 236 /// 237 /// In Version 2, we make the following changes: 238 /// 239 /// ThreadBuffer: BufferExtents NewBuffer WallClockTime NewCPUId 240 /// FunctionSequence 241 /// BufferExtents: 16 byte metdata record describing how many usable bytes are 242 /// in the buffer. This is measured from the start of the buffer 243 /// and must always be at least 48 (bytes). 244 /// 245 /// In Version 3, we make the following changes: 246 /// 247 /// ThreadBuffer: BufferExtents NewBuffer WallClockTime Pid NewCPUId 248 /// FunctionSequence 249 /// EOB: *deprecated* 250 Error loadFDRLog(StringRef Data, bool IsLittleEndian, 251 XRayFileHeader &FileHeader, std::vector<XRayRecord> &Records) { 252 253 if (Data.size() < 32) 254 return createStringError(std::make_error_code(std::errc::invalid_argument), 255 "Not enough bytes for an XRay FDR log."); 256 DataExtractor DE(Data, IsLittleEndian, 8); 257 258 uint32_t OffsetPtr = 0; 259 auto FileHeaderOrError = readBinaryFormatHeader(DE, OffsetPtr); 260 if (!FileHeaderOrError) 261 return FileHeaderOrError.takeError(); 262 FileHeader = std::move(FileHeaderOrError.get()); 263 264 // First we load the records into memory. 265 std::vector<std::unique_ptr<Record>> FDRRecords; 266 267 { 268 FileBasedRecordProducer P(FileHeader, DE, OffsetPtr); 269 LogBuilderConsumer C(FDRRecords); 270 while (DE.isValidOffsetForDataOfSize(OffsetPtr, 1)) { 271 auto R = P.produce(); 272 if (!R) 273 return R.takeError(); 274 if (auto E = C.consume(std::move(R.get()))) 275 return E; 276 } 277 } 278 279 // Next we index the records into blocks. 280 BlockIndexer::Index Index; 281 { 282 BlockIndexer Indexer(Index); 283 for (auto &R : FDRRecords) 284 if (auto E = R->apply(Indexer)) 285 return E; 286 if (auto E = Indexer.flush()) 287 return E; 288 } 289 290 // Then we verify the consistency of the blocks. 291 { 292 BlockVerifier Verifier; 293 for (auto &PTB : Index) { 294 auto &Blocks = PTB.second; 295 for (auto &B : Blocks) { 296 for (auto *R : B.Records) 297 if (auto E = R->apply(Verifier)) 298 return E; 299 if (auto E = Verifier.verify()) 300 return E; 301 Verifier.reset(); 302 } 303 } 304 } 305 306 // This is now the meat of the algorithm. Here we sort the blocks according to 307 // the Walltime record in each of the blocks for the same thread. This allows 308 // us to more consistently recreate the execution trace in temporal order. 309 // After the sort, we then reconstitute `Trace` records using a stateful 310 // visitor associated with a single process+thread pair. 311 { 312 for (auto &PTB : Index) { 313 auto &Blocks = PTB.second; 314 llvm::sort( 315 Blocks.begin(), Blocks.end(), 316 [](const BlockIndexer::Block &L, const BlockIndexer::Block &R) { 317 return (L.WallclockTime->seconds() < R.WallclockTime->seconds() && 318 L.WallclockTime->nanos() < R.WallclockTime->nanos()); 319 }); 320 TraceExpander Expander([&](const XRayRecord &R) { Records.push_back(R); }, 321 FileHeader.Version); 322 for (auto &B : Blocks) { 323 for (auto *R : B.Records) 324 if (auto E = R->apply(Expander)) 325 return E; 326 } 327 if (auto E = Expander.flush()) 328 return E; 329 } 330 } 331 332 return Error::success(); 333 } 334 335 Error loadYAMLLog(StringRef Data, XRayFileHeader &FileHeader, 336 std::vector<XRayRecord> &Records) { 337 YAMLXRayTrace Trace; 338 Input In(Data); 339 In >> Trace; 340 if (In.error()) 341 return make_error<StringError>("Failed loading YAML Data.", In.error()); 342 343 FileHeader.Version = Trace.Header.Version; 344 FileHeader.Type = Trace.Header.Type; 345 FileHeader.ConstantTSC = Trace.Header.ConstantTSC; 346 FileHeader.NonstopTSC = Trace.Header.NonstopTSC; 347 FileHeader.CycleFrequency = Trace.Header.CycleFrequency; 348 349 if (FileHeader.Version != 1) 350 return make_error<StringError>( 351 Twine("Unsupported XRay file version: ") + Twine(FileHeader.Version), 352 std::make_error_code(std::errc::invalid_argument)); 353 354 Records.clear(); 355 std::transform(Trace.Records.begin(), Trace.Records.end(), 356 std::back_inserter(Records), [&](const YAMLXRayRecord &R) { 357 return XRayRecord{R.RecordType, R.CPU, R.Type, R.FuncId, 358 R.TSC, R.TId, R.PId, R.CallArgs}; 359 }); 360 return Error::success(); 361 } 362 } // namespace 363 364 Expected<Trace> llvm::xray::loadTraceFile(StringRef Filename, bool Sort) { 365 int Fd; 366 if (auto EC = sys::fs::openFileForRead(Filename, Fd)) { 367 return make_error<StringError>( 368 Twine("Cannot read log from '") + Filename + "'", EC); 369 } 370 371 uint64_t FileSize; 372 if (auto EC = sys::fs::file_size(Filename, FileSize)) { 373 return make_error<StringError>( 374 Twine("Cannot read log from '") + Filename + "'", EC); 375 } 376 if (FileSize < 4) { 377 return make_error<StringError>( 378 Twine("File '") + Filename + "' too small for XRay.", 379 std::make_error_code(std::errc::executable_format_error)); 380 } 381 382 // Map the opened file into memory and use a StringRef to access it later. 383 std::error_code EC; 384 sys::fs::mapped_file_region MappedFile( 385 Fd, sys::fs::mapped_file_region::mapmode::readonly, FileSize, 0, EC); 386 if (EC) { 387 return make_error<StringError>( 388 Twine("Cannot read log from '") + Filename + "'", EC); 389 } 390 auto Data = StringRef(MappedFile.data(), MappedFile.size()); 391 392 // TODO: Lift the endianness and implementation selection here. 393 DataExtractor LittleEndianDE(Data, true, 8); 394 auto TraceOrError = loadTrace(LittleEndianDE, Sort); 395 if (!TraceOrError) { 396 DataExtractor BigEndianDE(Data, false, 8); 397 TraceOrError = loadTrace(BigEndianDE, Sort); 398 } 399 return TraceOrError; 400 } 401 402 Expected<Trace> llvm::xray::loadTrace(const DataExtractor &DE, bool Sort) { 403 // Attempt to detect the file type using file magic. We have a slight bias 404 // towards the binary format, and we do this by making sure that the first 4 405 // bytes of the binary file is some combination of the following byte 406 // patterns: (observe the code loading them assumes they're little endian) 407 // 408 // 0x01 0x00 0x00 0x00 - version 1, "naive" format 409 // 0x01 0x00 0x01 0x00 - version 1, "flight data recorder" format 410 // 0x02 0x00 0x01 0x00 - version 2, "flight data recorder" format 411 // 412 // YAML files don't typically have those first four bytes as valid text so we 413 // try loading assuming YAML if we don't find these bytes. 414 // 415 // Only if we can't load either the binary or the YAML format will we yield an 416 // error. 417 DataExtractor HeaderExtractor(DE.getData(), DE.isLittleEndian(), 8); 418 uint32_t OffsetPtr = 0; 419 uint16_t Version = HeaderExtractor.getU16(&OffsetPtr); 420 uint16_t Type = HeaderExtractor.getU16(&OffsetPtr); 421 422 enum BinaryFormatType { NAIVE_FORMAT = 0, FLIGHT_DATA_RECORDER_FORMAT = 1 }; 423 424 Trace T; 425 switch (Type) { 426 case NAIVE_FORMAT: 427 if (Version == 1 || Version == 2 || Version == 3) { 428 if (auto E = loadNaiveFormatLog(DE.getData(), DE.isLittleEndian(), 429 T.FileHeader, T.Records)) 430 return std::move(E); 431 } else { 432 return make_error<StringError>( 433 Twine("Unsupported version for Basic/Naive Mode logging: ") + 434 Twine(Version), 435 std::make_error_code(std::errc::executable_format_error)); 436 } 437 break; 438 case FLIGHT_DATA_RECORDER_FORMAT: 439 if (Version == 1 || Version == 2 || Version == 3) { 440 if (auto E = loadFDRLog(DE.getData(), DE.isLittleEndian(), T.FileHeader, 441 T.Records)) 442 return std::move(E); 443 } else { 444 return make_error<StringError>( 445 Twine("Unsupported version for FDR Mode logging: ") + Twine(Version), 446 std::make_error_code(std::errc::executable_format_error)); 447 } 448 break; 449 default: 450 if (auto E = loadYAMLLog(DE.getData(), T.FileHeader, T.Records)) 451 return std::move(E); 452 } 453 454 if (Sort) 455 std::stable_sort(T.Records.begin(), T.Records.end(), 456 [&](const XRayRecord &L, const XRayRecord &R) { 457 return L.TSC < R.TSC; 458 }); 459 460 return std::move(T); 461 } 462