2014-03-21 17:24:48 +00:00
|
|
|
//=-- InstrProfReader.h - Instrumented profiling readers ----------*- C++ -*-=//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains support for reading profiling data for instrumentation
|
|
|
|
// based PGO and coverage.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-08-13 16:26:38 +00:00
|
|
|
#ifndef LLVM_PROFILEDATA_INSTRPROFREADER_H
|
|
|
|
#define LLVM_PROFILEDATA_INSTRPROFREADER_H
|
2014-03-21 17:24:48 +00:00
|
|
|
|
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
2014-04-18 21:48:40 +00:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2014-03-21 17:24:48 +00:00
|
|
|
#include "llvm/ProfileData/InstrProf.h"
|
2015-01-14 11:23:27 +00:00
|
|
|
#include "llvm/Support/EndianStream.h"
|
2014-11-03 00:51:45 +00:00
|
|
|
#include "llvm/Support/ErrorOr.h"
|
2014-03-21 17:24:48 +00:00
|
|
|
#include "llvm/Support/LineIterator.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2014-04-18 21:48:40 +00:00
|
|
|
#include "llvm/Support/OnDiskHashTable.h"
|
2014-03-21 17:24:48 +00:00
|
|
|
#include <iterator>
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
class InstrProfReader;
|
|
|
|
|
|
|
|
/// Profiling information for a single function.
|
|
|
|
struct InstrProfRecord {
|
2014-04-18 21:48:40 +00:00
|
|
|
InstrProfRecord() {}
|
|
|
|
InstrProfRecord(StringRef Name, uint64_t Hash, ArrayRef<uint64_t> Counts)
|
|
|
|
: Name(Name), Hash(Hash), Counts(Counts) {}
|
2014-03-21 17:24:48 +00:00
|
|
|
StringRef Name;
|
|
|
|
uint64_t Hash;
|
|
|
|
ArrayRef<uint64_t> Counts;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// A file format agnostic iterator over profiling data.
|
|
|
|
class InstrProfIterator : public std::iterator<std::input_iterator_tag,
|
|
|
|
InstrProfRecord> {
|
|
|
|
InstrProfReader *Reader;
|
|
|
|
InstrProfRecord Record;
|
|
|
|
|
|
|
|
void Increment();
|
|
|
|
public:
|
|
|
|
InstrProfIterator() : Reader(nullptr) {}
|
|
|
|
InstrProfIterator(InstrProfReader *Reader) : Reader(Reader) { Increment(); }
|
|
|
|
|
|
|
|
InstrProfIterator &operator++() { Increment(); return *this; }
|
|
|
|
bool operator==(const InstrProfIterator &RHS) { return Reader == RHS.Reader; }
|
|
|
|
bool operator!=(const InstrProfIterator &RHS) { return Reader != RHS.Reader; }
|
|
|
|
InstrProfRecord &operator*() { return Record; }
|
|
|
|
InstrProfRecord *operator->() { return &Record; }
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Base class and interface for reading profiling data of any known instrprof
|
|
|
|
/// format. Provides an iterator over InstrProfRecords.
|
|
|
|
class InstrProfReader {
|
2014-06-12 21:46:39 +00:00
|
|
|
std::error_code LastError;
|
|
|
|
|
2014-03-21 17:24:48 +00:00
|
|
|
public:
|
|
|
|
InstrProfReader() : LastError(instrprof_error::success) {}
|
|
|
|
virtual ~InstrProfReader() {}
|
|
|
|
|
2014-03-21 18:26:05 +00:00
|
|
|
/// Read the header. Required before reading first record.
|
2014-06-12 21:46:39 +00:00
|
|
|
virtual std::error_code readHeader() = 0;
|
2014-03-21 17:24:48 +00:00
|
|
|
/// Read a single record.
|
2014-06-12 21:46:39 +00:00
|
|
|
virtual std::error_code readNextRecord(InstrProfRecord &Record) = 0;
|
2014-03-21 17:24:48 +00:00
|
|
|
/// Iterator over profile data.
|
|
|
|
InstrProfIterator begin() { return InstrProfIterator(this); }
|
|
|
|
InstrProfIterator end() { return InstrProfIterator(); }
|
|
|
|
|
2014-03-21 18:26:05 +00:00
|
|
|
protected:
|
2014-06-12 21:46:39 +00:00
|
|
|
/// Set the current std::error_code and return same.
|
|
|
|
std::error_code error(std::error_code EC) {
|
2014-03-21 17:24:48 +00:00
|
|
|
LastError = EC;
|
|
|
|
return EC;
|
|
|
|
}
|
2014-03-21 18:26:05 +00:00
|
|
|
|
2014-03-21 17:24:48 +00:00
|
|
|
/// Clear the current error code and return a successful one.
|
2014-06-12 21:46:39 +00:00
|
|
|
std::error_code success() { return error(instrprof_error::success); }
|
2014-03-21 17:24:48 +00:00
|
|
|
|
2014-03-21 18:26:05 +00:00
|
|
|
public:
|
2014-03-21 17:24:48 +00:00
|
|
|
/// Return true if the reader has finished reading the profile data.
|
|
|
|
bool isEOF() { return LastError == instrprof_error::eof; }
|
|
|
|
/// Return true if the reader encountered an error reading profiling data.
|
|
|
|
bool hasError() { return LastError && !isEOF(); }
|
|
|
|
/// Get the current error code.
|
2014-06-12 21:46:39 +00:00
|
|
|
std::error_code getError() { return LastError; }
|
2014-03-21 17:24:48 +00:00
|
|
|
|
|
|
|
/// Factory method to create an appropriately typed reader for the given
|
|
|
|
/// instrprof file.
|
2014-11-03 00:51:45 +00:00
|
|
|
static ErrorOr<std::unique_ptr<InstrProfReader>> create(std::string Path);
|
2014-03-21 17:24:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Reader for the simple text based instrprof format.
|
|
|
|
///
|
|
|
|
/// This format is a simple text format that's suitable for test data. Records
|
|
|
|
/// are separated by one or more blank lines, and record fields are separated by
|
|
|
|
/// new lines.
|
|
|
|
///
|
|
|
|
/// Each record consists of a function name, a function hash, a number of
|
|
|
|
/// counters, and then each counter value, in that order.
|
|
|
|
class TextInstrProfReader : public InstrProfReader {
|
|
|
|
private:
|
|
|
|
/// The profile data file contents.
|
|
|
|
std::unique_ptr<MemoryBuffer> DataBuffer;
|
|
|
|
/// Iterator over the profile data.
|
|
|
|
line_iterator Line;
|
|
|
|
/// The current set of counter values.
|
|
|
|
std::vector<uint64_t> Counts;
|
|
|
|
|
|
|
|
TextInstrProfReader(const TextInstrProfReader &) LLVM_DELETED_FUNCTION;
|
|
|
|
TextInstrProfReader &operator=(const TextInstrProfReader &)
|
|
|
|
LLVM_DELETED_FUNCTION;
|
|
|
|
public:
|
2014-03-21 20:42:34 +00:00
|
|
|
TextInstrProfReader(std::unique_ptr<MemoryBuffer> DataBuffer_)
|
2014-09-17 15:43:01 +00:00
|
|
|
: DataBuffer(std::move(DataBuffer_)), Line(*DataBuffer, true, '#') {}
|
2014-03-21 17:24:48 +00:00
|
|
|
|
2014-03-21 18:26:05 +00:00
|
|
|
/// Read the header.
|
2014-06-12 21:46:39 +00:00
|
|
|
std::error_code readHeader() override { return success(); }
|
2014-03-21 17:24:48 +00:00
|
|
|
/// Read a single record.
|
2014-06-12 21:46:39 +00:00
|
|
|
std::error_code readNextRecord(InstrProfRecord &Record) override;
|
2014-03-21 17:24:48 +00:00
|
|
|
};
|
|
|
|
|
2014-03-21 18:26:05 +00:00
|
|
|
/// Reader for the raw instrprof binary format from runtime.
|
|
|
|
///
|
|
|
|
/// This format is a raw memory dump of the instrumentation-baed profiling data
|
|
|
|
/// from the runtime. It has no index.
|
2014-03-23 03:38:12 +00:00
|
|
|
///
|
|
|
|
/// Templated on the unsigned type whose size matches pointers on the platform
|
|
|
|
/// that wrote the profile.
|
|
|
|
template <class IntPtrT>
|
2014-03-21 18:26:05 +00:00
|
|
|
class RawInstrProfReader : public InstrProfReader {
|
|
|
|
private:
|
|
|
|
/// The profile data file contents.
|
|
|
|
std::unique_ptr<MemoryBuffer> DataBuffer;
|
|
|
|
/// The current set of counter values.
|
|
|
|
std::vector<uint64_t> Counts;
|
|
|
|
struct ProfileData {
|
|
|
|
const uint32_t NameSize;
|
|
|
|
const uint32_t NumCounters;
|
|
|
|
const uint64_t FuncHash;
|
2014-03-23 03:38:12 +00:00
|
|
|
const IntPtrT NamePtr;
|
|
|
|
const IntPtrT CounterPtr;
|
2014-03-21 18:26:05 +00:00
|
|
|
};
|
|
|
|
struct RawHeader {
|
|
|
|
const uint64_t Magic;
|
|
|
|
const uint64_t Version;
|
|
|
|
const uint64_t DataSize;
|
|
|
|
const uint64_t CountersSize;
|
|
|
|
const uint64_t NamesSize;
|
|
|
|
const uint64_t CountersDelta;
|
|
|
|
const uint64_t NamesDelta;
|
|
|
|
};
|
|
|
|
|
|
|
|
bool ShouldSwapBytes;
|
|
|
|
uint64_t CountersDelta;
|
|
|
|
uint64_t NamesDelta;
|
|
|
|
const ProfileData *Data;
|
|
|
|
const ProfileData *DataEnd;
|
|
|
|
const uint64_t *CountersStart;
|
|
|
|
const char *NamesStart;
|
2014-05-16 00:38:00 +00:00
|
|
|
const char *ProfileEnd;
|
2014-03-21 18:26:05 +00:00
|
|
|
|
2014-06-18 18:20:44 +00:00
|
|
|
RawInstrProfReader(const RawInstrProfReader &) LLVM_DELETED_FUNCTION;
|
|
|
|
RawInstrProfReader &operator=(const RawInstrProfReader &)
|
2014-03-21 18:26:05 +00:00
|
|
|
LLVM_DELETED_FUNCTION;
|
|
|
|
public:
|
2014-03-21 20:59:19 +00:00
|
|
|
RawInstrProfReader(std::unique_ptr<MemoryBuffer> DataBuffer)
|
|
|
|
: DataBuffer(std::move(DataBuffer)) { }
|
2014-03-21 18:26:05 +00:00
|
|
|
|
2014-03-21 20:42:31 +00:00
|
|
|
static bool hasFormat(const MemoryBuffer &DataBuffer);
|
2014-06-12 21:46:39 +00:00
|
|
|
std::error_code readHeader() override;
|
|
|
|
std::error_code readNextRecord(InstrProfRecord &Record) override;
|
2014-03-21 18:26:05 +00:00
|
|
|
|
|
|
|
private:
|
2014-06-12 21:46:39 +00:00
|
|
|
std::error_code readNextHeader(const char *CurrentPos);
|
|
|
|
std::error_code readHeader(const RawHeader &Header);
|
2014-03-21 18:26:05 +00:00
|
|
|
template <class IntT>
|
|
|
|
IntT swap(IntT Int) const {
|
2014-06-14 11:36:01 +00:00
|
|
|
return ShouldSwapBytes ? sys::getSwappedBytes(Int) : Int;
|
2014-03-21 18:26:05 +00:00
|
|
|
}
|
2014-03-23 03:38:12 +00:00
|
|
|
const uint64_t *getCounter(IntPtrT CounterPtr) const {
|
2014-03-21 18:26:05 +00:00
|
|
|
ptrdiff_t Offset = (swap(CounterPtr) - CountersDelta) / sizeof(uint64_t);
|
|
|
|
return CountersStart + Offset;
|
|
|
|
}
|
2014-03-23 03:38:12 +00:00
|
|
|
const char *getName(IntPtrT NamePtr) const {
|
2014-03-21 18:26:05 +00:00
|
|
|
ptrdiff_t Offset = (swap(NamePtr) - NamesDelta) / sizeof(char);
|
|
|
|
return NamesStart + Offset;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-03-23 03:38:12 +00:00
|
|
|
typedef RawInstrProfReader<uint32_t> RawInstrProfReader32;
|
|
|
|
typedef RawInstrProfReader<uint64_t> RawInstrProfReader64;
|
|
|
|
|
2014-04-18 21:48:40 +00:00
|
|
|
namespace IndexedInstrProf {
|
|
|
|
enum class HashT : uint32_t;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Trait for lookups into the on-disk hash table for the binary instrprof
|
|
|
|
/// format.
|
|
|
|
class InstrProfLookupTrait {
|
2014-08-01 22:50:07 +00:00
|
|
|
std::vector<uint64_t> DataBuffer;
|
2014-04-18 21:48:40 +00:00
|
|
|
IndexedInstrProf::HashT HashType;
|
|
|
|
public:
|
|
|
|
InstrProfLookupTrait(IndexedInstrProf::HashT HashType) : HashType(HashType) {}
|
|
|
|
|
2014-08-01 22:50:07 +00:00
|
|
|
struct data_type {
|
|
|
|
data_type(StringRef Name, ArrayRef<uint64_t> Data)
|
|
|
|
: Name(Name), Data(Data) {}
|
|
|
|
StringRef Name;
|
|
|
|
ArrayRef<uint64_t> Data;
|
|
|
|
};
|
2014-04-18 21:48:40 +00:00
|
|
|
typedef StringRef internal_key_type;
|
|
|
|
typedef StringRef external_key_type;
|
|
|
|
typedef uint64_t hash_value_type;
|
|
|
|
typedef uint64_t offset_type;
|
|
|
|
|
|
|
|
static bool EqualKey(StringRef A, StringRef B) { return A == B; }
|
|
|
|
static StringRef GetInternalKey(StringRef K) { return K; }
|
|
|
|
|
2014-04-18 22:00:22 +00:00
|
|
|
hash_value_type ComputeHash(StringRef K);
|
2014-04-18 21:48:40 +00:00
|
|
|
|
|
|
|
static std::pair<offset_type, offset_type>
|
|
|
|
ReadKeyDataLength(const unsigned char *&D) {
|
|
|
|
using namespace support;
|
2014-04-19 00:33:12 +00:00
|
|
|
offset_type KeyLen = endian::readNext<offset_type, little, unaligned>(D);
|
|
|
|
offset_type DataLen = endian::readNext<offset_type, little, unaligned>(D);
|
|
|
|
return std::make_pair(KeyLen, DataLen);
|
2014-04-18 21:48:40 +00:00
|
|
|
}
|
|
|
|
|
2014-04-19 00:33:15 +00:00
|
|
|
StringRef ReadKey(const unsigned char *D, offset_type N) {
|
2014-04-18 21:48:40 +00:00
|
|
|
return StringRef((const char *)D, N);
|
|
|
|
}
|
|
|
|
|
2014-08-01 22:50:07 +00:00
|
|
|
data_type ReadData(StringRef K, const unsigned char *D, offset_type N) {
|
|
|
|
DataBuffer.clear();
|
|
|
|
if (N % sizeof(uint64_t))
|
2014-04-18 21:48:40 +00:00
|
|
|
// The data is corrupt, don't try to read it.
|
2014-08-01 22:50:07 +00:00
|
|
|
return data_type("", DataBuffer);
|
2014-04-18 21:48:40 +00:00
|
|
|
|
|
|
|
using namespace support;
|
2014-08-01 22:50:07 +00:00
|
|
|
// We just treat the data as opaque here. It's simpler to handle in
|
|
|
|
// IndexedInstrProfReader.
|
|
|
|
unsigned NumEntries = N / sizeof(uint64_t);
|
|
|
|
DataBuffer.reserve(NumEntries);
|
|
|
|
for (unsigned I = 0; I < NumEntries; ++I)
|
|
|
|
DataBuffer.push_back(endian::readNext<uint64_t, little, unaligned>(D));
|
|
|
|
return data_type(K, DataBuffer);
|
2014-04-18 21:48:40 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
typedef OnDiskIterableChainedHashTable<InstrProfLookupTrait>
|
|
|
|
InstrProfReaderIndex;
|
|
|
|
|
|
|
|
/// Reader for the indexed binary instrprof format.
|
|
|
|
class IndexedInstrProfReader : public InstrProfReader {
|
|
|
|
private:
|
|
|
|
/// The profile data file contents.
|
|
|
|
std::unique_ptr<MemoryBuffer> DataBuffer;
|
|
|
|
/// The index into the profile data.
|
|
|
|
std::unique_ptr<InstrProfReaderIndex> Index;
|
|
|
|
/// Iterator over the profile data.
|
|
|
|
InstrProfReaderIndex::data_iterator RecordIterator;
|
2014-08-01 22:50:07 +00:00
|
|
|
/// Offset into our current data set.
|
|
|
|
size_t CurrentOffset;
|
|
|
|
/// The file format version of the profile data.
|
|
|
|
uint64_t FormatVersion;
|
|
|
|
/// The maximal execution count among all functions.
|
2014-04-18 21:48:40 +00:00
|
|
|
uint64_t MaxFunctionCount;
|
|
|
|
|
|
|
|
IndexedInstrProfReader(const IndexedInstrProfReader &) LLVM_DELETED_FUNCTION;
|
|
|
|
IndexedInstrProfReader &operator=(const IndexedInstrProfReader &)
|
|
|
|
LLVM_DELETED_FUNCTION;
|
|
|
|
public:
|
|
|
|
IndexedInstrProfReader(std::unique_ptr<MemoryBuffer> DataBuffer)
|
2014-08-01 22:50:07 +00:00
|
|
|
: DataBuffer(std::move(DataBuffer)), Index(nullptr), CurrentOffset(0) {}
|
2014-04-18 21:48:40 +00:00
|
|
|
|
|
|
|
/// Return true if the given buffer is in an indexed instrprof format.
|
|
|
|
static bool hasFormat(const MemoryBuffer &DataBuffer);
|
|
|
|
|
|
|
|
/// Read the file header.
|
2014-06-12 21:46:39 +00:00
|
|
|
std::error_code readHeader() override;
|
2014-04-18 21:48:40 +00:00
|
|
|
/// Read a single record.
|
2014-06-12 21:46:39 +00:00
|
|
|
std::error_code readNextRecord(InstrProfRecord &Record) override;
|
2014-04-18 21:48:40 +00:00
|
|
|
|
|
|
|
/// Fill Counts with the profile data for the given function name.
|
2014-08-01 22:50:07 +00:00
|
|
|
std::error_code getFunctionCounts(StringRef FuncName, uint64_t FuncHash,
|
2014-06-12 21:46:39 +00:00
|
|
|
std::vector<uint64_t> &Counts);
|
2014-04-18 21:48:40 +00:00
|
|
|
/// Return the maximum of all known function counts.
|
|
|
|
uint64_t getMaximumFunctionCount() { return MaxFunctionCount; }
|
|
|
|
|
|
|
|
/// Factory method to create an indexed reader.
|
2014-06-12 21:46:39 +00:00
|
|
|
static std::error_code
|
|
|
|
create(std::string Path, std::unique_ptr<IndexedInstrProfReader> &Result);
|
2014-04-18 21:48:40 +00:00
|
|
|
};
|
|
|
|
|
2014-03-21 17:24:48 +00:00
|
|
|
} // end namespace llvm
|
|
|
|
|
2014-08-13 16:26:38 +00:00
|
|
|
#endif
|