Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[XRay] Add the
llvm-xray fdr-dump
implementation
Summary: In this change, we implement a `BlockPrinter` which orders records in a Block that's been indexed by the `BlockIndexer`. This is used in the `llvm-xray fdr-dump` tool which ties together the various types and utilities we've been working on, to allow for inspection of XRay FDR mode traces both with and without verification. This change is the final step of the refactoring of D50441. Reviewers: mboerger, eizan Subscribers: mgorny, hiraditya, llvm-commits Differential Revision: https://reviews.llvm.org/D51846 llvm-svn: 341887
- Loading branch information
1 parent
cec7d3a
commit dd01efc
Showing
9 changed files
with
476 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
//===- BlockPrinter.h - FDR Block Pretty Printer -------------------------===// | ||
// | ||
// The LLVM Compiler Infrastructure | ||
// | ||
// This file is distributed under the University of Illinois Open Source | ||
// License. See LICENSE.TXT for details. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// An implementation of the RecordVisitor which formats a block of records for | ||
// easier human consumption. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
#ifndef LLVM_INCLUDE_LLVM_XRAY_BLOCKPRINTER_H_ | ||
#define LLVM_INCLUDE_LLVM_XRAY_BLOCKPRINTER_H_ | ||
|
||
#include "llvm/Support/raw_ostream.h" | ||
#include "llvm/XRay/FDRRecords.h" | ||
#include "llvm/XRay/RecordPrinter.h" | ||
|
||
namespace llvm { | ||
namespace xray { | ||
|
||
class BlockPrinter : public RecordVisitor { | ||
enum class State { | ||
Start, | ||
Preamble, | ||
Metadata, | ||
Function, | ||
Arg, | ||
CustomEvent, | ||
End, | ||
}; | ||
|
||
raw_ostream &OS; | ||
RecordPrinter &RP; | ||
State CurrentState = State::Start; | ||
|
||
public: | ||
explicit BlockPrinter(raw_ostream &O, RecordPrinter &P) | ||
: RecordVisitor(), OS(O), RP(P) {} | ||
|
||
Error visit(BufferExtents &) override; | ||
Error visit(WallclockRecord &) override; | ||
Error visit(NewCPUIDRecord &) override; | ||
Error visit(TSCWrapRecord &) override; | ||
Error visit(CustomEventRecord &) override; | ||
Error visit(CallArgRecord &) override; | ||
Error visit(PIDRecord &) override; | ||
Error visit(NewBufferRecord &) override; | ||
Error visit(EndBufferRecord &) override; | ||
Error visit(FunctionRecord &) override; | ||
|
||
void reset() { CurrentState = State::Start; } | ||
}; | ||
|
||
} // namespace xray | ||
} // namespace llvm | ||
|
||
#endif // LLVM_INCLUDE_LLVM_XRAY_BLOCKPRINTER_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
//===- BlockPrinter.cpp - FDR Block Pretty Printer Implementation --------===// | ||
// | ||
// The LLVM Compiler Infrastructure | ||
// | ||
// This file is distributed under the University of Illinois Open Source | ||
// License. See LICENSE.TXT for details. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
#include "llvm/XRay/BlockPrinter.h" | ||
|
||
namespace llvm { | ||
namespace xray { | ||
|
||
Error BlockPrinter::visit(BufferExtents &R) { | ||
OS << "\n[New Block]\n"; | ||
CurrentState = State::Preamble; | ||
return RP.visit(R); | ||
} | ||
|
||
// Preamble printing. | ||
Error BlockPrinter::visit(NewBufferRecord &R) { | ||
if (CurrentState == State::Start) | ||
OS << "\n[New Block]\n"; | ||
|
||
OS << "Preamble: \n"; | ||
CurrentState = State::Preamble; | ||
return RP.visit(R); | ||
} | ||
|
||
Error BlockPrinter::visit(WallclockRecord &R) { | ||
CurrentState = State::Preamble; | ||
return RP.visit(R); | ||
} | ||
|
||
Error BlockPrinter::visit(PIDRecord &R) { | ||
CurrentState = State::Preamble; | ||
return RP.visit(R); | ||
} | ||
|
||
// Metadata printing. | ||
Error BlockPrinter::visit(NewCPUIDRecord &R) { | ||
if (CurrentState == State::Preamble) | ||
OS << "\nBody:\n"; | ||
if (CurrentState == State::Function) | ||
OS << "\nMetadata: "; | ||
CurrentState = State::Metadata; | ||
OS << " "; | ||
auto E = RP.visit(R); | ||
return E; | ||
} | ||
|
||
Error BlockPrinter::visit(TSCWrapRecord &R) { | ||
if (CurrentState == State::Function) | ||
OS << "\nMetadata:"; | ||
CurrentState = State::Metadata; | ||
OS << " "; | ||
auto E = RP.visit(R); | ||
return E; | ||
} | ||
|
||
// Custom events will be rendered like "function" events. | ||
Error BlockPrinter::visit(CustomEventRecord &R) { | ||
if (CurrentState == State::Metadata) | ||
OS << "\n"; | ||
CurrentState = State::CustomEvent; | ||
OS << "* "; | ||
auto E = RP.visit(R); | ||
return E; | ||
} | ||
|
||
// Function call printing. | ||
Error BlockPrinter::visit(FunctionRecord &R) { | ||
if (CurrentState == State::Metadata) | ||
OS << "\n"; | ||
CurrentState = State::Function; | ||
OS << "- "; | ||
auto E = RP.visit(R); | ||
return E; | ||
} | ||
|
||
Error BlockPrinter::visit(CallArgRecord &R) { | ||
CurrentState = State::Arg; | ||
OS << " : "; | ||
auto E = RP.visit(R); | ||
return E; | ||
} | ||
|
||
Error BlockPrinter::visit(EndBufferRecord &R) { | ||
CurrentState = State::End; | ||
OS << " *** "; | ||
auto E = RP.visit(R); | ||
return E; | ||
} | ||
|
||
} // namespace xray | ||
} // namespace llvm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
; RUN: llvm-xray fdr-dump -verify %S/Inputs/fdr-log-arg1-version-3.xray \ | ||
; RUN: | FileCheck %s | ||
|
||
; CHECK: [New Block] | ||
; CHECK-NEXT: Preamble: | ||
; CHECK-NEXT: <Thread ID: 2631> | ||
; CHECK-NEXT: <Wall Time: seconds = 599605.032403> | ||
; CHECK-NEXT: <PID: 2631> | ||
; CHECK-EMPTY: | ||
; CHECK-NEXT: Body: | ||
; CHECK-NEXT: <CPU ID: 6> | ||
; CHECK-NEXT: <CPU ID: 6> | ||
; CHECK-NEXT: <TSC Wrap: base = 2034042117104344> | ||
; CHECK-EMPTY: | ||
; CHECK-NEXT: - <Function Enter: #3 delta = +3> | ||
; CHECK-NEXT: - <Function Exit: #3 delta = +3> | ||
; CHECK-NEXT: - <Function Enter: #2 delta = +2> | ||
; CHECK-NEXT: - <Function Exit: #2 delta = +2> | ||
; CHECK-EMPTY: | ||
; CHECK-NEXT: Metadata: <TSC Wrap: base = 2034049739853430> | ||
; CHECK-EMPTY: | ||
; CHECK-NEXT: - <Function Enter: #1 delta = +1> | ||
; CHECK-NEXT: : <Call Argument: data = 67 (hex = 0x43)> | ||
; CHECK-NEXT: - <Function Exit: #1 delta = +1> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
; RUN: llvm-xray fdr-dump -verify %S/Inputs/fdr-log-arg1.xray | FileCheck %s | ||
|
||
; CHECK: [New Block] | ||
; CHECK-NEXT: Preamble: | ||
; CHECK-NEXT: <Thread ID: 14648> | ||
; CHECK-NEXT: <Wall Time: seconds = 1452786.250689> | ||
; CHECK-EMPTY: | ||
; CHECK-NEXT: Body: | ||
; CHECK-NEXT: <CPU ID: 49> | ||
; CHECK-NEXT: <TSC Wrap: base = 18828908666540172> | ||
; CHECK-EMPTY: | ||
; CHECK-NEXT: - <Function Enter: #1 delta = +1> | ||
; CHECK-NEXT: : <Call Argument: data = 1 (hex = 0x1)> | ||
; CHECK-NEXT: - <Function Exit: #1 delta = +1> | ||
; CHECK-NEXT: *** <End of Buffer> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
//===- xray-fdr-dump.cpp: XRay FDR Trace Dump Tool ------------------------===// | ||
// | ||
// The LLVM Compiler Infrastructure | ||
// | ||
// This file is distributed under the University of Illinois Open Source | ||
// License. See LICENSE.TXT for details. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Implements the FDR trace dumping tool, using the libraries for handling FDR | ||
// mode traces specifically. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
#include "xray-registry.h" | ||
#include "llvm/Support/CommandLine.h" | ||
#include "llvm/Support/FileSystem.h" | ||
#include "llvm/XRay/BlockIndexer.h" | ||
#include "llvm/XRay/BlockPrinter.h" | ||
#include "llvm/XRay/BlockVerifier.h" | ||
#include "llvm/XRay/FDRRecordConsumer.h" | ||
#include "llvm/XRay/FDRRecordProducer.h" | ||
#include "llvm/XRay/FDRRecords.h" | ||
#include "llvm/XRay/FileHeaderReader.h" | ||
#include "llvm/XRay/RecordPrinter.h" | ||
|
||
using namespace llvm; | ||
using namespace xray; | ||
|
||
static cl::SubCommand Dump("fdr-dump", "FDR Trace Dump"); | ||
static cl::opt<std::string> DumpInput(cl::Positional, | ||
cl::desc("<xray fdr mode log>"), | ||
cl::Required, cl::sub(Dump)); | ||
static cl::opt<bool> DumpVerify("verify", | ||
cl::desc("verify structure of the log"), | ||
cl::init(false), cl::sub(Dump)); | ||
|
||
static CommandRegistration Unused(&Dump, []() -> Error { | ||
// Open the file provided. | ||
int Fd; | ||
if (auto EC = sys::fs::openFileForRead(DumpInput, Fd)) | ||
return createStringError(EC, "Cannot open file '%s' for read.", | ||
DumpInput.c_str()); | ||
|
||
uint64_t FileSize; | ||
if (auto EC = sys::fs::file_size(DumpInput, FileSize)) | ||
return createStringError(EC, "Failed to get file size for '%s'.", | ||
DumpInput.c_str()); | ||
|
||
std::error_code EC; | ||
sys::fs::mapped_file_region MappedFile( | ||
Fd, sys::fs::mapped_file_region::mapmode::readonly, FileSize, 0, EC); | ||
|
||
DataExtractor DE(StringRef(MappedFile.data(), MappedFile.size()), true, 8); | ||
uint32_t OffsetPtr = 0; | ||
|
||
auto FileHeaderOrError = readBinaryFormatHeader(DE, OffsetPtr); | ||
if (!FileHeaderOrError) | ||
return FileHeaderOrError.takeError(); | ||
auto &H = FileHeaderOrError.get(); | ||
|
||
FileBasedRecordProducer P(H, DE, OffsetPtr); | ||
|
||
RecordPrinter RP(outs(), "\n"); | ||
if (!DumpVerify) { | ||
PipelineConsumer C({&RP}); | ||
while (DE.isValidOffsetForDataOfSize(OffsetPtr, 1)) { | ||
auto R = P.produce(); | ||
if (!R) | ||
return R.takeError(); | ||
if (auto E = C.consume(std::move(R.get()))) | ||
return E; | ||
} | ||
return Error::success(); | ||
} | ||
|
||
BlockPrinter BP(outs(), RP); | ||
std::vector<std::unique_ptr<Record>> Records; | ||
LogBuilderConsumer C(Records); | ||
while (DE.isValidOffsetForDataOfSize(OffsetPtr, 1)) { | ||
auto R = P.produce(); | ||
if (!R) { | ||
// Print records we've found so far. | ||
for (auto &Ptr : Records) | ||
if (auto E = Ptr->apply(RP)) | ||
return joinErrors(std::move(E), R.takeError()); | ||
return R.takeError(); | ||
} | ||
if (auto E = C.consume(std::move(R.get()))) | ||
return E; | ||
} | ||
|
||
// Once we have a trace, we then index the blocks. | ||
BlockIndexer::Index Index; | ||
BlockIndexer BI(Index); | ||
for (auto &Ptr : Records) | ||
if (auto E = Ptr->apply(BI)) | ||
return E; | ||
|
||
if (auto E = BI.flush()) | ||
return E; | ||
|
||
// Then we validate while printing each block. | ||
BlockVerifier BV; | ||
for (auto ProcessThreadBlocks : Index) { | ||
auto &Blocks = ProcessThreadBlocks.second; | ||
for (auto &B : Blocks) { | ||
for (auto *R : B.Records) { | ||
if (auto E = R->apply(BV)) | ||
return E; | ||
if (auto E = R->apply(BP)) | ||
return E; | ||
} | ||
BV.reset(); | ||
BP.reset(); | ||
} | ||
} | ||
outs().flush(); | ||
return Error::success(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.