Index: include/llvm/XRay/LogReader.h =================================================================== --- include/llvm/XRay/LogReader.h +++ include/llvm/XRay/LogReader.h @@ -1,4 +1,4 @@ -//===- xray-log-reader.h - XRay Log Reader Interface ----------------------===// +//===- XRayLogReader.h - XRay Log Reader Interface ------------------------===// // // The LLVM Compiler Infrastructure // @@ -7,25 +7,35 @@ // //===----------------------------------------------------------------------===// // -// Define the interface for an XRay log reader. Currently we only support one -// version of the log (naive log) with fixed-sized records. +// Define the interface for an XRay log reader. // //===----------------------------------------------------------------------===// -#ifndef LLVM_TOOLS_LLVM_XRAY_XRAY_LOG_READER_H -#define LLVM_TOOLS_LLVM_XRAY_XRAY_LOG_READER_H +#ifndef LLVM_XRAY_LOG_READER_H +#define LLVM_XRAY_LOG_READER_H #include -#include #include -#include "xray-record-yaml.h" -#include "xray-record.h" #include "llvm/Support/Error.h" #include "llvm/Support/FileSystem.h" +#include "llvm/XRay/XRayRecord.h" namespace llvm { namespace xray { +/// The LogReader takes a filename from which we load XRay records from. This is +/// useful for writing tools that intend to perform analysis on XRay trace +/// files. +/// +/// Usage: +/// +/// llvm::Error E; +/// LogReader Reader("xray-log.something.xray", E, true, NaiveLogLoader); +/// // Reader.getFileHeader() will provide information from the trace header. +/// for (const XRayRecord& R : Reader) { +/// // ... do something with R here. +/// } +/// class LogReader { XRayFileHeader FileHeader; std::vector Records; @@ -33,12 +43,16 @@ typedef std::vector::const_iterator citerator; public: + /// This function type defines how individual records are dealt with. typedef std::function &)> LoaderFunction; + /// Loads records from |Filename| invoking the |Loader| function for each + /// record. LogReader(StringRef Filename, Error &Err, bool Sort, LoaderFunction Loader); + /// Provides access to the loaded XRay trace file header. const XRayFileHeader &getFileHeader() const { return FileHeader; } citerator begin() const { return Records.begin(); } @@ -46,12 +60,17 @@ size_t size() const { return Records.size(); } }; +/// This Loader function is used to parse the simple "naive" log format used by +/// XRay. Error NaiveLogLoader(StringRef Data, XRayFileHeader &FileHeader, std::vector &Records); + +/// Use this loader function for loading YAML representations of the simple log +/// format. Error YAMLLogLoader(StringRef Data, XRayFileHeader &FileHeader, std::vector &Records); } // namespace xray } // namespace llvm -#endif // LLVM_TOOLS_LLVM_XRAY_XRAY_LOG_READER_H +#endif // LLVM_XRAY_LOG_READER_H Index: include/llvm/XRay/XRayRecord.h =================================================================== --- /dev/null +++ include/llvm/XRay/XRayRecord.h @@ -0,0 +1,76 @@ +//===- XRayRecord.h - XRay Trace Record -----------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file replicates the record definition for XRay log entries. This should +// follow the evolution of the log record versions supported in the compiler-rt +// xray project. +// +//===----------------------------------------------------------------------===// +#ifndef LLVM_XRAY_XRAY_RECORD_H +#define LLVM_XRAY_XRAY_RECORD_H + +#include + +namespace llvm { +namespace xray { + +/// XRay traces all have a header providing some top-matter information useful +/// to help tools determine how to interpret the information available in the +/// trace. +struct XRayFileHeader { + /// Version of the XRay implementation that produced this file. + uint16_t Version = 0; + + /// A numeric identifier for the type of file this is. Best used in + /// combination with Version. + uint16_t Type = 0; + + /// Whether the CPU that produced the timestamp counters (TSC) move at a + /// constant rate. + bool ConstantTSC; + + /// Whether the CPU that produced the timestamp counters (TSC) do not stop. + bool NonstopTSC; + + /// The number of cycles per second for the CPU that produced the timestamp + /// counter (TSC) values. Useful for estimating the amount of time that + /// elapsed between two TSCs on some platforms. + uint64_t CycleFrequency = 0; +}; + +/// Determines the supported types of records that could be seen in XRay traces. +/// This may or may not correspond to actual record types in the raw trace (as +/// the loader implementation may synthesize this information in the process of +/// of loading). +enum class RecordTypes { ENTER, EXIT }; + +struct XRayRecord { + /// The type of record. + uint16_t RecordType; + + /// The CPU where the thread is running. We assume number of CPUs <= 256. + uint8_t CPU; + + /// Identifies the type of record. + RecordTypes Type; + + /// The function ID for the record. + int32_t FuncId; + + /// Get the full 8 bytes of the TSC when we get the log record. + uint64_t TSC; + + /// The thread ID for the currently running thread. + uint32_t TId; +}; + +} // namespace xray +} // namespace llvm + +#endif // LLVM_XRAY_XRAY_RECORD_H Index: include/llvm/XRay/YAMLXRayRecord.h =================================================================== --- include/llvm/XRay/YAMLXRayRecord.h +++ include/llvm/XRay/YAMLXRayRecord.h @@ -1,4 +1,4 @@ -//===- xray-record-yaml.h - XRay Record YAML Support Definitions ----------===// +//===- YAMLXRayRecord.h - XRay Record YAML Support Definitions ------------===// // // The LLVM Compiler Infrastructure // @@ -10,13 +10,13 @@ // Types and traits specialisations for YAML I/O of XRay log entries. // //===----------------------------------------------------------------------===// -#ifndef LLVM_TOOLS_LLVM_XRAY_XRAY_RECORD_YAML_H -#define LLVM_TOOLS_LLVM_XRAY_XRAY_RECORD_YAML_H +#ifndef LLVM_XRAY_YAML_XRAY_RECORD_H +#define LLVM_XRAY_YAML_XRAY_RECORD_H #include -#include "xray-record.h" #include "llvm/Support/YAMLTraits.h" +#include "llvm/XRay/XRayRecord.h" namespace llvm { namespace xray { @@ -44,9 +44,6 @@ std::vector Records; }; -using XRayRecordStorage = - std::aligned_storage::type; - } // namespace xray namespace yaml { @@ -97,6 +94,6 @@ } // namespace yaml } // namespace llvm -LLVM_YAML_IS_SEQUENCE_VECTOR(xray::YAMLXRayRecord) +LLVM_YAML_IS_SEQUENCE_VECTOR(xray::YAMLXRayRecord) -#endif // LLVM_TOOLS_LLVM_XRAY_XRAY_RECORD_YAML_H +#endif // LLVM_XRAY_YAML_XRAY_RECORD_H Index: lib/CMakeLists.txt =================================================================== --- lib/CMakeLists.txt +++ lib/CMakeLists.txt @@ -22,3 +22,4 @@ add_subdirectory(Fuzzer) add_subdirectory(Passes) add_subdirectory(LibDriver) +add_subdirectory(XRay) Index: lib/XRay/CMakeLists.txt =================================================================== --- /dev/null +++ lib/XRay/CMakeLists.txt @@ -0,0 +1,13 @@ +add_llvm_library(LLVMXRay + LogReader.cpp + + ADDITIONAL_HEADER_DIRS + ${LLVM_MAIN_INCLUDE_DIR}/llvm/ADT + ${LLVM_MAIN_INCLUDE_DIR}/llvm/XRay + + DEPENDS + LLVMSupport + + LINK_LIBS + LLVMSupport + ) Index: lib/XRay/LogReader.cpp =================================================================== --- lib/XRay/LogReader.cpp +++ lib/XRay/LogReader.cpp @@ -12,15 +12,18 @@ //===----------------------------------------------------------------------===// #include -#include "xray-log-reader.h" -#include "xray-record-yaml.h" #include "llvm/Support/DataExtractor.h" #include "llvm/Support/FileSystem.h" +#include "llvm/XRay/LogReader.h" +#include "llvm/XRay/YAMLXRayRecord.h" using namespace llvm; using namespace llvm::xray; using llvm::yaml::Input; +using XRayRecordStorage = + std::aligned_storage::type; + LogReader::LogReader( StringRef Filename, Error &Err, bool Sort, std::function &)> Index: tools/llvm-xray/CMakeLists.txt =================================================================== --- tools/llvm-xray/CMakeLists.txt +++ tools/llvm-xray/CMakeLists.txt @@ -3,14 +3,14 @@ DebugInfoDWARF Object Support - Symbolize) + Symbolize + XRay) set(LLVM_XRAY_TOOLS func-id-helper.cc xray-converter.cc xray-extract.cc xray-extract.cc - xray-log-reader.cc xray-registry.cc) add_llvm_tool(llvm-xray llvm-xray.cc ${LLVM_XRAY_TOOLS}) Index: tools/llvm-xray/xray-converter.h =================================================================== --- tools/llvm-xray/xray-converter.h +++ tools/llvm-xray/xray-converter.h @@ -15,8 +15,8 @@ #define LLVM_TOOLS_LLVM_XRAY_XRAY_CONVERTER_H #include "func-id-helper.h" -#include "xray-log-reader.h" -#include "xray-record.h" +#include "llvm/XRay/XRayRecord.h" +#include "llvm/XRay/LogReader.h" namespace llvm { namespace xray { Index: tools/llvm-xray/xray-converter.cc =================================================================== --- tools/llvm-xray/xray-converter.cc +++ tools/llvm-xray/xray-converter.cc @@ -13,13 +13,12 @@ #include "xray-converter.h" #include "xray-extract.h" -#include "xray-record-yaml.h" #include "xray-registry.h" #include "llvm/DebugInfo/Symbolize/Symbolize.h" #include "llvm/Support/EndianStream.h" #include "llvm/Support/FileSystem.h" -#include "llvm/Support/YAMLTraits.h" #include "llvm/Support/raw_ostream.h" +#include "llvm/XRay/YAMLXRayRecord.h" #include using namespace llvm; @@ -92,8 +91,6 @@ cl::desc("Alias for -instr-map-format"), cl::sub(Convert)); -using llvm::yaml::MappingTraits; -using llvm::yaml::ScalarEnumerationTraits; using llvm::yaml::IO; using llvm::yaml::Output; Index: tools/llvm-xray/xray-record.h =================================================================== --- tools/llvm-xray/xray-record.h +++ /dev/null @@ -1,55 +0,0 @@ -//===- xray-record.h - XRay Trace Record ----------------------------------===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// This file replicates the record definition for XRay log entries. This should -// follow the evolution of the log record versions supported in the compiler-rt -// xray project. -// -//===----------------------------------------------------------------------===// -#ifndef LLVM_TOOLS_LLVM_XRAY_XRAY_RECORD_H -#define LLVM_TOOLS_LLVM_XRAY_XRAY_RECORD_H - -#include - -namespace llvm { -namespace xray { - -struct XRayFileHeader { - uint16_t Version = 0; - uint16_t Type = 0; - bool ConstantTSC; - bool NonstopTSC; - uint64_t CycleFrequency = 0; -}; - -enum class RecordTypes { ENTER, EXIT }; - -struct XRayRecord { - uint16_t RecordType; - - // The CPU where the thread is running. We assume number of CPUs <= 256. - uint8_t CPU; - - // Identifies the type of record. - RecordTypes Type; - - // The function ID for the record. - int32_t FuncId; - - // Get the full 8 bytes of the TSC when we get the log record. - uint64_t TSC; - - // The thread ID for the currently running thread. - uint32_t TId; -}; - -} // namespace xray -} // namespace llvm - -#endif // LLVM_TOOLS_LLVM_XRAY_XRAY_RECORD_H