Index: llvm/trunk/include/llvm/XRay/RecordPrinter.h =================================================================== --- llvm/trunk/include/llvm/XRay/RecordPrinter.h +++ llvm/trunk/include/llvm/XRay/RecordPrinter.h @@ -0,0 +1,48 @@ +//===- RecordPrinter.h - FDR Record 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 prints an individual record's +// data in an adhoc format, suitable for human inspection. +// +//===----------------------------------------------------------------------===// +#ifndef LLVM_INCLUDE_LLVM_XRAY_RECORDPRINTER_H_ +#define LLVM_INCLUDE_LLVM_XRAY_RECORDPRINTER_H_ + +#include "llvm/Support/raw_ostream.h" +#include "llvm/XRay/FDRRecords.h" + +namespace llvm { +namespace xray { + +class RecordPrinter : public RecordVisitor { + raw_ostream &OS; + std::string Delim; + +public: + explicit RecordPrinter(raw_ostream &O, std::string D) + : RecordVisitor(), OS(O), Delim(std::move(D)) {} + + explicit RecordPrinter(raw_ostream &O) : RecordPrinter(O, ""){}; + + 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; +}; + +} // namespace xray +} // namespace llvm + +#endif // LLVM_INCLUDE_LLVM_XRAY_RECORDPRINTER_H Index: llvm/trunk/lib/XRay/CMakeLists.txt =================================================================== --- llvm/trunk/lib/XRay/CMakeLists.txt +++ llvm/trunk/lib/XRay/CMakeLists.txt @@ -7,6 +7,7 @@ LogBuilderConsumer.cpp Profile.cpp RecordInitializer.cpp + RecordPrinter.cpp Trace.cpp ADDITIONAL_HEADER_DIRS Index: llvm/trunk/lib/XRay/RecordPrinter.cpp =================================================================== --- llvm/trunk/lib/XRay/RecordPrinter.cpp +++ llvm/trunk/lib/XRay/RecordPrinter.cpp @@ -0,0 +1,89 @@ +//===- RecordPrinter.cpp - FDR Record Printer -----------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +#include "llvm/XRay/RecordPrinter.h" + +#include "llvm/Support/FormatVariadic.h" + +namespace llvm { +namespace xray { + +Error RecordPrinter::visit(BufferExtents &R) { + OS << formatv("", R.size()) << Delim; + return Error::success(); +} + +Error RecordPrinter::visit(WallclockRecord &R) { + OS << formatv("", R.seconds(), R.nanos()) + << Delim; + return Error::success(); +} + +Error RecordPrinter::visit(NewCPUIDRecord &R) { + OS << formatv("", R.cpuid()) << Delim; + return Error::success(); +} + +Error RecordPrinter::visit(TSCWrapRecord &R) { + OS << formatv("", R.tsc()) << Delim; + return Error::success(); +} + +Error RecordPrinter::visit(CustomEventRecord &R) { + OS << formatv("", R.tsc(), + R.size(), R.data()) + << Delim; + return Error::success(); +} + +Error RecordPrinter::visit(CallArgRecord &R) { + OS << formatv("", R.arg()) << Delim; + return Error::success(); +} + +Error RecordPrinter::visit(PIDRecord &R) { + OS << formatv("", R.pid()) << Delim; + return Error::success(); +} + +Error RecordPrinter::visit(NewBufferRecord &R) { + OS << formatv("", R.tid()) << Delim; + return Error::success(); +} + +Error RecordPrinter::visit(EndBufferRecord &R) { + OS << "" << Delim; + return Error::success(); +} + +Error RecordPrinter::visit(FunctionRecord &R) { + // FIXME: Support symbolization here? + switch (R.recordType()) { + case RecordTypes::ENTER: + OS << formatv("", R.functionId(), + R.delta()); + break; + case RecordTypes::ENTER_ARG: + OS << formatv("", + R.functionId(), R.delta()); + break; + case RecordTypes::EXIT: + OS << formatv("", R.functionId(), + R.delta()); + break; + case RecordTypes::TAIL_EXIT: + OS << formatv("", R.functionId(), + R.delta()); + break; + } + OS << Delim; + return Error::success(); +} + +} // namespace xray +} // namespace llvm Index: llvm/trunk/unittests/XRay/CMakeLists.txt =================================================================== --- llvm/trunk/unittests/XRay/CMakeLists.txt +++ llvm/trunk/unittests/XRay/CMakeLists.txt @@ -4,10 +4,11 @@ ) add_llvm_unittest(XRayTests + ProfileTest.cpp FDRProducerConsumerTest.cpp + FDRRecordPrinterTest.cpp FDRTraceWriterTest.cpp GraphTest.cpp - ProfileTest.cpp ) add_dependencies(XRayTests intrinsics_gen) Index: llvm/trunk/unittests/XRay/FDRRecordPrinterTest.cpp =================================================================== --- llvm/trunk/unittests/XRay/FDRRecordPrinterTest.cpp +++ llvm/trunk/unittests/XRay/FDRRecordPrinterTest.cpp @@ -0,0 +1,176 @@ +//===- llvm/unittest/XRay/FDRRecordPrinterTest.cpp --------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +#include "llvm/Support/raw_ostream.h" +#include "llvm/XRay/FDRRecords.h" +#include "llvm/XRay/RecordPrinter.h" +#include "gmock/gmock.h" +#include "gtest/gtest.h" +#include + +namespace llvm { +namespace xray { +namespace { + +using ::testing::Eq; + +template struct Helper {}; + +template <> struct Helper { + static std::unique_ptr construct() { + return make_unique(1); + } + + static constexpr char Expected[] = ""; +}; +constexpr char Helper::Expected[]; + +template <> struct Helper { + static std::unique_ptr construct() { + return make_unique(1, 2); + } + + static constexpr char Expected[] = ""; +}; +constexpr char Helper::Expected[]; + +template <> struct Helper { + static std::unique_ptr construct() { + return make_unique(1); + } + + static constexpr char Expected[] = ""; +}; +constexpr char Helper::Expected[]; + +template <> struct Helper { + static std::unique_ptr construct() { + return make_unique(1); + } + + static constexpr char Expected[] = ""; +}; +constexpr char Helper::Expected[]; + +template <> struct Helper { + static std::unique_ptr construct() { + return make_unique(4, 1, "data"); + } + + static constexpr char Expected[] = + ""; +}; +constexpr char Helper::Expected[]; + +template <> struct Helper { + static std::unique_ptr construct() { + return make_unique(1); + } + + static constexpr char Expected[] = ""; +}; +constexpr char Helper::Expected[]; + +template <> struct Helper { + static std::unique_ptr construct() { + return make_unique(1); + } + + static constexpr char Expected[] = ""; +}; +constexpr char Helper::Expected[]; + +template <> struct Helper { + static std::unique_ptr construct() { + return make_unique(1); + } + + static constexpr char Expected[] = ""; +}; +constexpr char Helper::Expected[]; + +template <> struct Helper { + static std::unique_ptr construct() { + return make_unique(); + } + + static constexpr char Expected[] = ""; +}; +constexpr char Helper::Expected[]; + +template class PrinterTest : public ::testing::Test { +protected: + std::string Data; + raw_string_ostream OS; + RecordPrinter P; + std::unique_ptr R; + +public: + PrinterTest() : Data(), OS(Data), P(OS), R(Helper::construct()) {} +}; + +TYPED_TEST_CASE_P(PrinterTest); + +TYPED_TEST_P(PrinterTest, PrintsRecord) { + ASSERT_NE(nullptr, this->R); + ASSERT_FALSE(errorToBool(this->R->apply(this->P))); + this->OS.flush(); + EXPECT_THAT(this->Data, Eq(Helper::Expected)); +} + +REGISTER_TYPED_TEST_CASE_P(PrinterTest, PrintsRecord); +using FDRRecordTypes = + ::testing::Types; +INSTANTIATE_TYPED_TEST_CASE_P(Records, PrinterTest, FDRRecordTypes); + +TEST(FDRRecordPrinterTest, WriteFunctionRecordEnter) { + std::string Data; + raw_string_ostream OS(Data); + RecordPrinter P(OS); + FunctionRecord R(RecordTypes::ENTER, 1, 2); + ASSERT_FALSE(errorToBool(R.apply(P))); + OS.flush(); + EXPECT_THAT(Data, Eq("")); +} + +TEST(FDRRecordPrinterTest, WriteFunctionRecordExit) { + std::string Data; + raw_string_ostream OS(Data); + RecordPrinter P(OS); + FunctionRecord R(RecordTypes::EXIT, 1, 2); + ASSERT_FALSE(errorToBool(R.apply(P))); + OS.flush(); + EXPECT_THAT(Data, Eq("")); +} + +TEST(FDRRecordPrinterTest, WriteFunctionRecordTailExit) { + std::string Data; + raw_string_ostream OS(Data); + RecordPrinter P(OS); + FunctionRecord R(RecordTypes::TAIL_EXIT, 1, 2); + ASSERT_FALSE(errorToBool(R.apply(P))); + OS.flush(); + EXPECT_THAT(Data, Eq("")); +} + +TEST(FDRRecordPrinterTest, WriteFunctionRecordEnterArg) { + std::string Data; + raw_string_ostream OS(Data); + RecordPrinter P(OS); + FunctionRecord R(RecordTypes::ENTER_ARG, 1, 2); + ASSERT_FALSE(errorToBool(R.apply(P))); + OS.flush(); + EXPECT_THAT(Data, Eq("")); +} + +} // namespace +} // namespace xray +} // namespace llvm