21 using namespace llvm::xray;
25 std::aligned_storage<sizeof(XRayRecord), alignof(XRayRecord)>::type;
44 FileHeader.
Type = HeaderExtractor.
getU16(&OffsetPtr);
51 return make_error<StringError>(
58 std::vector<XRayRecord> &Records) {
61 return make_error<StringError>(
62 "Not enough bytes for an XRay log.",
65 if (Data.
size() - 32 == 0 || Data.
size() % 32 != 0)
66 return make_error<StringError>(
67 "Invalid-sized XRay data.",
85 Records.emplace_back();
86 auto &
Record = Records.back();
89 auto Type = RecordExtractor.
getU8(&OffsetPtr);
98 return make_error<StringError>(
102 Record.FuncId = RecordExtractor.
getSigned(&OffsetPtr,
sizeof(int32_t));
122 NEW_BUFFER_RECORD_OR_EOF,
126 SCAN_TO_END_OF_THREAD_BUF,
140 return "NEW_BUFFER_RECORD_OR_EOF";
142 return "WALLCLOCK_RECORD";
144 return "NEW_CPU_ID_RECORD";
146 return "FUNCTION_SEQUENCE";
148 return "SCAN_TO_END_OF_THREAD_BUF";
150 return "CUSTOM_EVENT_DATA";
160 return make_error<StringError>(
161 "Malformed log. Read New Buffer record kind out of sequence",
173 return make_error<StringError>(
174 "Malformed log. Received EOB message without current buffer.",
185 return make_error<StringError>(
186 "Malformed log. Read NewCPUId record kind out of sequence",
199 return make_error<StringError>(
200 "Malformed log. Read TSCWrap record kind out of sequence",
211 return make_error<StringError>(
212 "Malformed log. Read Wallclock record kind out of sequence",
223 size_t &RecordSize) {
229 uint64_t TSC = RecordExtractor.
getU64(&OffsetPtr);
234 RecordSize = 16 + DataSize;
245 size_t &RecordSize) {
247 uint8_t RecordKind = RecordFirstByte >> 1;
248 switch (RecordKind) {
276 RecordExtractor, RecordSize))
281 return make_error<StringError>(
282 Twine(
"Illegal metadata record type: ")
298 std::vector<XRayRecord> &Records) {
301 return make_error<StringError>(
302 "Malformed log. Received Function Record before new buffer setup.",
305 return make_error<StringError>(
306 "Malformed log. Received Function Record when expecting wallclock.",
309 return make_error<StringError>(
310 "Malformed log. Received Function Record before first CPU record.",
313 Records.emplace_back();
314 auto &
Record = Records.back();
317 uint8_t RecordType = (RecordFirstByte >> 1) & 0x07;
318 switch (RecordType) {
329 return make_error<StringError>(
330 Twine(
"Illegal function record type: ")
345 Record.FuncId = FuncIdBitField >> 4;
349 uint64_t new_tsc = State.
BaseTSC + RecordExtractor.
getU32(&OffsetPtr);
383 std::vector<XRayRecord> &Records) {
384 if (Data.
size() < 32)
385 return make_error<StringError>(
386 "Not enough bytes for an XRay log.",
392 if (Data.
size() % 8 != 0)
393 return make_error<StringError>(
394 "Invalid-sized XRay data.",
400 uint64_t BufferSize = 0;
405 BufferSize = ExtraDataExtractor.
getU64(&ExtraDataOffset);
411 size_t RecordSize = 0;
412 for (
auto S = Data.
drop_front(32); !S.
empty(); S = S.drop_front(RecordSize)) {
416 RecordSize = State.CurrentBufferSize - State.CurrentBufferConsumed;
417 if (S.size() < State.CurrentBufferSize - State.CurrentBufferConsumed) {
418 return make_error<StringError>(
419 Twine(
"Incomplete thread buffer. Expected ") +
420 Twine(State.CurrentBufferSize - State.CurrentBufferConsumed) +
421 " remaining bytes but found " +
Twine(S.size()),
424 State.CurrentBufferConsumed = 0;
428 uint8_t BitField = RecordExtractor.
getU8(&OffsetPtr);
429 bool isMetadataRecord = BitField & 0x01uL;
430 if (isMetadataRecord) {
435 State.CurrentBufferConsumed += RecordSize;
441 State.CurrentBufferConsumed += RecordSize;
447 State.CurrentBufferSize == State.CurrentBufferConsumed))
448 return make_error<StringError>(
449 Twine(
"Encountered EOF with unexpected state expectation ") +
451 ". Remaining expected bytes in thread buffer total " +
452 Twine(State.CurrentBufferSize - State.CurrentBufferConsumed),
459 std::vector<XRayRecord> &Records) {
465 return make_error<StringError>(
"Failed loading YAML Data.", In.
error());
467 FileHeader.
Version = Trace.Header.Version;
468 FileHeader.
Type = Trace.Header.Type;
470 FileHeader.
NonstopTSC = Trace.Header.NonstopTSC;
474 return make_error<StringError>(
482 R.FuncId, R.TSC, R.TId};
490 return make_error<StringError>(
491 Twine(
"Cannot read log from '") + Filename +
"'", EC);
497 return make_error<StringError>(
498 Twine(
"Cannot read log from '") + Filename +
"'", EC);
501 return make_error<StringError>(
502 Twine(
"File '") + Filename +
"' too small for XRay.",
509 Fd, sys::fs::mapped_file_region::mapmode::readonly, FileSize, 0, EC);
511 return make_error<StringError>(
512 Twine(
"Cannot read log from '") + Filename +
"'", EC);
531 uint16_t
Version = HeaderExtractor.getU16(&OffsetPtr);
532 uint16_t
Type = HeaderExtractor.getU16(&OffsetPtr);
534 enum BinaryFormatType { NAIVE_FORMAT = 0, FLIGHT_DATA_RECORDER_FORMAT = 1 };
537 if (Version == 1 && Type == NAIVE_FORMAT) {
540 T.FileHeader, T.Records))
542 }
else if (Version == 1 && Type == FLIGHT_DATA_RECORDER_FORMAT) {
544 T.FileHeader, T.Records))
548 T.FileHeader, T.Records))
553 std::sort(T.Records.begin(), T.Records.end(),
555 return L.
TSC < R.TSC;
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef drop_front(size_t N=1) const
Return a StringRef equal to 'this' but with the first N elements dropped.
Error processFDRNewCPUIdRecord(FDRState &State, uint8_t RecordFirstByte, DataExtractor &RecordExtractor)
State transition when a NewCPUIdRecord is encountered.
uint16_t RecordType
The type of record.
uint64_t CurrentBufferConsumed
std::error_code openFileForRead(const Twine &Name, int &ResultFD, SmallVectorImpl< char > *RealPath=nullptr)
This class represents a memory mapped file.
Expected< Trace > loadTraceFile(StringRef Filename, bool Sort=false)
This function will attempt to load XRay trace records from the provided |Filename|.
Error loadYAMLLog(StringRef Data, XRayFileHeader &FileHeader, std::vector< XRayRecord > &Records)
Error processCustomEventMarker(FDRState &State, uint8_t RecordFirstByte, DataExtractor &RecordExtractor, size_t &RecordSize)
State transition when a CustomEventMarker is encountered.
Error readBinaryFormatHeader(StringRef Data, XRayFileHeader &FileHeader)
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
std::error_code make_error_code(BitcodeError E)
std::error_code file_size(const Twine &Path, uint64_t &Result)
Get file size.
Tagged union holding either a T or a Error.
Error processFDRWallTimeRecord(FDRState &State, uint8_t RecordFirstByte, DataExtractor &RecordExtractor)
State transition when a WallTimeMarkerRecord is encountered.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
A Trace object represents the records that have been loaded from XRay log files generated by instrume...
Error processFDRMetadataRecord(FDRState &State, uint8_t RecordFirstByte, DataExtractor &RecordExtractor, size_t &RecordSize)
Advances the state machine for reading the FDR record type by reading one Metadata Record and updatin...
The instances of the Type class are immutable: once they are created, they are never changed...
Error processFDRTSCWrapRecord(FDRState &State, uint8_t RecordFirstByte, DataExtractor &RecordExtractor)
State transition when a TSCWrapRecord (overflow detection) is encountered.
Error processFDRNewBufferRecord(FDRState &State, uint8_t RecordFirstByte, DataExtractor &RecordExtractor)
State transition when a NewBufferRecord is encountered.
Error loadFDRLog(StringRef Data, XRayFileHeader &FileHeader, std::vector< XRayRecord > &Records)
Reads a log in FDR mode for version 1 of this binary format.
When reading from a Flight Data Recorder mode log, metadata records are sparse compared to packed fun...
static const char *const Magic
Twine concat(const Twine &Suffix) const
uint64_t CurrentBufferSize
Twine fdrStateToTwine(const FDRState::Token &state)
static ErrorSuccess success()
Create a success value.
Error processFDREndOfBufferRecord(FDRState &State, uint8_t RecordFirstByte, DataExtractor &RecordExtractor)
State transition when an EndOfBufferRecord is encountered.
uint64_t TSC
Get the full 8 bytes of the TSC when we get the log record.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
OutputIt transform(R &&Range, OutputIt d_first, UnaryPredicate P)
Wrapper function around std::transform to apply a function to a range and store the result elsewhere...
const unsigned char * bytes_begin() const
Lightweight error class with error context and mandatory checking.
Error processFDRFunctionRecord(FDRState &State, uint8_t RecordFirstByte, DataExtractor &RecordExtractor, std::vector< XRayRecord > &Records)
Reads a function record from an FDR format log, appending a new XRayRecord to the vector being popula...
StringRef - Represent a constant reference to a string, i.e.
Token
Encode some of the state transitions for the FDR log reader as explicit checks.
void sort(Policy policy, RandomAccessIterator Start, RandomAccessIterator End, const Comparator &Comp=Comparator())
Error loadNaiveFormatLog(StringRef Data, XRayFileHeader &FileHeader, std::vector< XRayRecord > &Records)
std::aligned_storage< sizeof(XRayRecord), alignof(XRayRecord)>::type XRayRecordStorage