Skip to content

Commit

Permalink
[XRay] Add the llvm-xray fdr-dump implementation
Browse files Browse the repository at this point in the history
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
deanberris committed Sep 11, 2018
1 parent cec7d3a commit dd01efc
Show file tree
Hide file tree
Showing 9 changed files with 476 additions and 1 deletion.
60 changes: 60 additions & 0 deletions llvm/include/llvm/XRay/BlockPrinter.h
@@ -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_
96 changes: 96 additions & 0 deletions llvm/lib/XRay/BlockPrinter.cpp
@@ -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
1 change: 1 addition & 0 deletions llvm/lib/XRay/CMakeLists.txt
@@ -1,5 +1,6 @@
add_llvm_library(LLVMXRay
BlockIndexer.cpp
BlockPrinter.cpp
BlockVerifier.cpp
FDRRecordProducer.cpp
FDRRecords.cpp
Expand Down
25 changes: 25 additions & 0 deletions llvm/test/tools/llvm-xray/X86/fdr-dump-arg1-version-3.txt
@@ -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>

16 changes: 16 additions & 0 deletions llvm/test/tools/llvm-xray/X86/fdr-dump-arg1.txt
@@ -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>

1 change: 1 addition & 0 deletions llvm/tools/llvm-xray/CMakeLists.txt
Expand Up @@ -14,6 +14,7 @@ add_llvm_tool(llvm-xray
xray-color-helper.cpp
xray-converter.cpp
xray-extract.cpp
xray-fdr-dump.cpp
xray-graph-diff.cpp
xray-graph.cpp
xray-registry.cpp
Expand Down
119 changes: 119 additions & 0 deletions llvm/tools/llvm-xray/xray-fdr-dump.cpp
@@ -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();
});
3 changes: 2 additions & 1 deletion llvm/unittests/XRay/CMakeLists.txt
Expand Up @@ -5,13 +5,14 @@ set(LLVM_LINK_COMPONENTS
)

add_llvm_unittest(XRayTests
ProfileTest.cpp
FDRBlockIndexerTest.cpp
FDRBlockVerifierTest.cpp
FDRProducerConsumerTest.cpp
FDRRecordPrinterTest.cpp
FDRRecordsTest.cpp
FDRTraceWriterTest.cpp
GraphTest.cpp
ProfileTest.cpp
)

add_dependencies(XRayTests intrinsics_gen)

0 comments on commit dd01efc

Please sign in to comment.