class InstrProfReader;
-/// Profiling information for a single function.
-struct InstrProfRecord {
- InstrProfRecord() {}
- InstrProfRecord(StringRef Name, uint64_t Hash, ArrayRef<uint64_t> Counts)
- : Name(Name), Hash(Hash), Counts(Counts) {}
- 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> {
InstrProfIterator begin() { return InstrProfIterator(this); }
InstrProfIterator end() { return InstrProfIterator(); }
-protected:
+ protected:
/// Set the current std::error_code and return same.
std::error_code error(std::error_code EC) {
LastError = EC;
/// Factory method to create an appropriately typed reader for the given
/// instrprof file.
static ErrorOr<std::unique_ptr<InstrProfReader>> create(std::string Path);
+
+ static ErrorOr<std::unique_ptr<InstrProfReader>>
+ create(std::unique_ptr<MemoryBuffer> Buffer);
};
/// Reader for the simple text based instrprof format.
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 &) = delete;
TextInstrProfReader &operator=(const TextInstrProfReader &) = delete;
TextInstrProfReader(std::unique_ptr<MemoryBuffer> DataBuffer_)
: DataBuffer(std::move(DataBuffer_)), Line(*DataBuffer, true, '#') {}
+ /// Return true if the given buffer is in text instrprof format.
+ static bool hasFormat(const MemoryBuffer &Buffer);
+
/// Read the header.
std::error_code readHeader() override { return success(); }
/// Read a single record.
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;
- const IntPtrT NamePtr;
- const IntPtrT CounterPtr;
- };
- 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 RawInstrProf::ProfileData<IntPtrT> *Data;
+ const RawInstrProf::ProfileData<IntPtrT> *DataEnd;
const uint64_t *CountersStart;
const char *NamesStart;
const char *ProfileEnd;
private:
std::error_code readNextHeader(const char *CurrentPos);
- std::error_code readHeader(const RawHeader &Header);
+ std::error_code readHeader(const RawInstrProf::Header &Header);
template <class IntT>
IntT swap(IntT Int) const {
return ShouldSwapBytes ? sys::getSwappedBytes(Int) : Int;
}
+
+ std::error_code readName(InstrProfRecord &Record);
+ std::error_code readFuncHash(InstrProfRecord &Record);
+ std::error_code readRawCounts(InstrProfRecord &Record);
+ bool atEnd() const { return Data == DataEnd; }
+ void advanceData() { Data++; }
+
const uint64_t *getCounter(IntPtrT CounterPtr) const {
ptrdiff_t Offset = (swap(CounterPtr) - CountersDelta) / sizeof(uint64_t);
return CountersStart + Offset;
/// Trait for lookups into the on-disk hash table for the binary instrprof
/// format.
class InstrProfLookupTrait {
- std::vector<uint64_t> DataBuffer;
+ std::vector<InstrProfRecord> DataBuffer;
IndexedInstrProf::HashT HashType;
+ unsigned FormatVersion;
+ // Endianness of the input value profile data.
+ // It should be LE by default, but can be changed
+ // for testing purpose.
+ support::endianness ValueProfDataEndianness;
+ std::vector<std::pair<uint64_t, const char *>> HashKeys;
+
public:
- InstrProfLookupTrait(IndexedInstrProf::HashT HashType) : HashType(HashType) {}
-
- struct data_type {
- data_type(StringRef Name, ArrayRef<uint64_t> Data)
- : Name(Name), Data(Data) {}
- StringRef Name;
- ArrayRef<uint64_t> Data;
- };
+ InstrProfLookupTrait(IndexedInstrProf::HashT HashType, unsigned FormatVersion)
+ : HashType(HashType), FormatVersion(FormatVersion),
+ ValueProfDataEndianness(support::little) {}
+
+ typedef ArrayRef<InstrProfRecord> data_type;
+
typedef StringRef internal_key_type;
typedef StringRef external_key_type;
typedef uint64_t hash_value_type;
static bool EqualKey(StringRef A, StringRef B) { return A == B; }
static StringRef GetInternalKey(StringRef K) { return K; }
+ static StringRef GetExternalKey(StringRef K) { return K; }
hash_value_type ComputeHash(StringRef K);
+ void setHashKeys(std::vector<std::pair<uint64_t, const char *>> HashKeys) {
+ this->HashKeys = std::move(HashKeys);
+ }
static std::pair<offset_type, offset_type>
ReadKeyDataLength(const unsigned char *&D) {
using namespace support;
return StringRef((const char *)D, N);
}
- data_type ReadData(StringRef K, const unsigned char *D, offset_type N) {
- DataBuffer.clear();
- if (N % sizeof(uint64_t))
- // The data is corrupt, don't try to read it.
- return data_type("", DataBuffer);
+ bool ReadValueProfilingData(const unsigned char *&D,
+ const unsigned char *const End);
+ data_type ReadData(StringRef K, const unsigned char *D, offset_type N);
- using namespace support;
- // 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);
+ // Used for testing purpose only.
+ void setValueProfDataEndianness(support::endianness Endianness) {
+ ValueProfDataEndianness = Endianness;
+ }
+};
+
+class InstrProfReaderIndex {
+ private:
+ typedef OnDiskIterableChainedHashTable<InstrProfLookupTrait> IndexType;
+
+ std::unique_ptr<IndexType> Index;
+ IndexType::data_iterator RecordIterator;
+ uint64_t FormatVersion;
+
+ // String table for holding a unique copy of all the strings in the profile.
+ InstrProfStringTable StringTable;
+
+ public:
+ InstrProfReaderIndex() : Index(nullptr) {}
+ void Init(const unsigned char *Buckets, const unsigned char *const Payload,
+ const unsigned char *const Base, IndexedInstrProf::HashT HashType,
+ uint64_t Version);
+
+ // Read all the pofile records with the same key pointed to the current
+ // iterator.
+ std::error_code getRecords(ArrayRef<InstrProfRecord> &Data);
+ // Read all the profile records with the key equal to FuncName
+ std::error_code getRecords(StringRef FuncName,
+ ArrayRef<InstrProfRecord> &Data);
+
+ void advanceToNextKey() { RecordIterator++; }
+ bool atEnd() const { return RecordIterator == Index->data_end(); }
+ // Used for testing purpose only.
+ void setValueProfDataEndianness(support::endianness Endianness) {
+ Index->getInfoObj().setValueProfDataEndianness(Endianness);
}
};
-typedef OnDiskIterableChainedHashTable<InstrProfLookupTrait>
- InstrProfReaderIndex;
/// Reader for the indexed binary instrprof format.
class IndexedInstrProfReader : public InstrProfReader {
/// 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;
- /// Offset into our current data set.
- size_t CurrentOffset;
- /// The file format version of the profile data.
- uint64_t FormatVersion;
+ InstrProfReaderIndex Index;
/// The maximal execution count among all functions.
uint64_t MaxFunctionCount;
IndexedInstrProfReader(const IndexedInstrProfReader &) = delete;
IndexedInstrProfReader &operator=(const IndexedInstrProfReader &) = delete;
-public:
+
+ public:
IndexedInstrProfReader(std::unique_ptr<MemoryBuffer> DataBuffer)
- : DataBuffer(std::move(DataBuffer)), Index(nullptr), CurrentOffset(0) {}
+ : DataBuffer(std::move(DataBuffer)), Index() {}
/// Return true if the given buffer is in an indexed instrprof format.
static bool hasFormat(const MemoryBuffer &DataBuffer);
/// Read a single record.
std::error_code readNextRecord(InstrProfRecord &Record) override;
+ /// Return the pointer to InstrProfRecord associated with FuncName
+ /// and FuncHash
+ ErrorOr<InstrProfRecord> getInstrProfRecord(StringRef FuncName,
+ uint64_t FuncHash);
+
/// Fill Counts with the profile data for the given function name.
std::error_code getFunctionCounts(StringRef FuncName, uint64_t FuncHash,
std::vector<uint64_t> &Counts);
/// Factory method to create an indexed reader.
static ErrorOr<std::unique_ptr<IndexedInstrProfReader>>
create(std::string Path);
+
+ static ErrorOr<std::unique_ptr<IndexedInstrProfReader>>
+ create(std::unique_ptr<MemoryBuffer> Buffer);
+
+ // Used for testing purpose only.
+ void setValueProfDataEndianness(support::endianness Endianness) {
+ Index.setValueProfDataEndianness(Endianness);
+ }
};
} // end namespace llvm