2014-03-22 01:24:48 +08:00
|
|
|
//=-- InstrProfReader.cpp - Instrumented profiling reader -------------------=//
|
|
|
|
//
|
|
|
|
// 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 clang's
|
|
|
|
// instrumentation based PGO and coverage.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/ProfileData/InstrProfReader.h"
|
2015-03-02 05:28:53 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2014-03-22 01:24:48 +08:00
|
|
|
#include <cassert>
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-11-03 08:51:45 +08:00
|
|
|
static ErrorOr<std::unique_ptr<MemoryBuffer>>
|
|
|
|
setupMemoryBuffer(std::string Path) {
|
2014-07-07 01:43:13 +08:00
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
|
|
|
|
MemoryBuffer::getFileOrSTDIN(Path);
|
|
|
|
if (std::error_code EC = BufferOrErr.getError())
|
2014-03-22 01:24:48 +08:00
|
|
|
return EC;
|
2015-02-18 09:58:17 +08:00
|
|
|
return std::move(BufferOrErr.get());
|
2014-04-19 05:48:40 +08:00
|
|
|
}
|
|
|
|
|
2014-06-13 10:24:39 +08:00
|
|
|
static std::error_code initializeReader(InstrProfReader &Reader) {
|
2014-04-19 05:48:40 +08:00
|
|
|
return Reader.readHeader();
|
|
|
|
}
|
|
|
|
|
2014-11-03 08:51:45 +08:00
|
|
|
ErrorOr<std::unique_ptr<InstrProfReader>>
|
|
|
|
InstrProfReader::create(std::string Path) {
|
2014-04-19 05:48:40 +08:00
|
|
|
// Set up the buffer to read.
|
2014-11-03 08:51:45 +08:00
|
|
|
auto BufferOrError = setupMemoryBuffer(Path);
|
|
|
|
if (std::error_code EC = BufferOrError.getError())
|
2014-04-19 05:48:40 +08:00
|
|
|
return EC;
|
2015-02-18 09:58:17 +08:00
|
|
|
return InstrProfReader::create(std::move(BufferOrError.get()));
|
|
|
|
}
|
2015-02-17 07:31:07 +08:00
|
|
|
|
2015-02-18 09:58:17 +08:00
|
|
|
ErrorOr<std::unique_ptr<InstrProfReader>>
|
|
|
|
InstrProfReader::create(std::unique_ptr<MemoryBuffer> Buffer) {
|
|
|
|
// Sanity check the buffer.
|
|
|
|
if (Buffer->getBufferSize() > std::numeric_limits<unsigned>::max())
|
|
|
|
return instrprof_error::too_large;
|
2015-02-17 17:21:43 +08:00
|
|
|
|
2015-02-18 09:58:17 +08:00
|
|
|
std::unique_ptr<InstrProfReader> Result;
|
2014-03-22 04:42:31 +08:00
|
|
|
// Create the reader.
|
2014-04-19 05:48:40 +08:00
|
|
|
if (IndexedInstrProfReader::hasFormat(*Buffer))
|
|
|
|
Result.reset(new IndexedInstrProfReader(std::move(Buffer)));
|
|
|
|
else if (RawInstrProfReader64::hasFormat(*Buffer))
|
2014-03-23 11:38:12 +08:00
|
|
|
Result.reset(new RawInstrProfReader64(std::move(Buffer)));
|
|
|
|
else if (RawInstrProfReader32::hasFormat(*Buffer))
|
|
|
|
Result.reset(new RawInstrProfReader32(std::move(Buffer)));
|
2014-03-22 02:26:05 +08:00
|
|
|
else
|
2014-03-22 04:42:34 +08:00
|
|
|
Result.reset(new TextInstrProfReader(std::move(Buffer)));
|
2014-03-22 04:42:31 +08:00
|
|
|
|
2014-04-19 05:48:40 +08:00
|
|
|
// Initialize the reader and return the result.
|
2014-11-03 08:51:45 +08:00
|
|
|
if (std::error_code EC = initializeReader(*Result))
|
|
|
|
return EC;
|
|
|
|
|
|
|
|
return std::move(Result);
|
2014-04-19 05:48:40 +08:00
|
|
|
}
|
|
|
|
|
2015-02-17 05:28:58 +08:00
|
|
|
ErrorOr<std::unique_ptr<IndexedInstrProfReader>>
|
|
|
|
IndexedInstrProfReader::create(std::string Path) {
|
2014-04-19 05:48:40 +08:00
|
|
|
// Set up the buffer to read.
|
2014-11-03 08:51:45 +08:00
|
|
|
auto BufferOrError = setupMemoryBuffer(Path);
|
|
|
|
if (std::error_code EC = BufferOrError.getError())
|
2014-04-19 05:48:40 +08:00
|
|
|
return EC;
|
2015-02-18 09:58:17 +08:00
|
|
|
return IndexedInstrProfReader::create(std::move(BufferOrError.get()));
|
|
|
|
}
|
|
|
|
|
2014-04-19 05:48:40 +08:00
|
|
|
|
2015-02-18 09:58:17 +08:00
|
|
|
ErrorOr<std::unique_ptr<IndexedInstrProfReader>>
|
|
|
|
IndexedInstrProfReader::create(std::unique_ptr<MemoryBuffer> Buffer) {
|
|
|
|
// Sanity check the buffer.
|
|
|
|
if (Buffer->getBufferSize() > std::numeric_limits<unsigned>::max())
|
|
|
|
return instrprof_error::too_large;
|
2015-02-17 05:28:58 +08:00
|
|
|
|
2014-04-19 05:48:40 +08:00
|
|
|
// Create the reader.
|
|
|
|
if (!IndexedInstrProfReader::hasFormat(*Buffer))
|
|
|
|
return instrprof_error::bad_magic;
|
2015-02-18 09:58:17 +08:00
|
|
|
auto Result = llvm::make_unique<IndexedInstrProfReader>(std::move(Buffer));
|
2014-04-19 05:48:40 +08:00
|
|
|
|
|
|
|
// Initialize the reader and return the result.
|
2015-02-17 05:28:58 +08:00
|
|
|
if (std::error_code EC = initializeReader(*Result))
|
|
|
|
return EC;
|
|
|
|
|
|
|
|
return std::move(Result);
|
2014-03-22 01:24:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void InstrProfIterator::Increment() {
|
|
|
|
if (Reader->readNextRecord(Record))
|
|
|
|
*this = InstrProfIterator();
|
|
|
|
}
|
|
|
|
|
2014-06-13 10:24:39 +08:00
|
|
|
std::error_code TextInstrProfReader::readNextRecord(InstrProfRecord &Record) {
|
2014-07-30 06:29:23 +08:00
|
|
|
// Skip empty lines and comments.
|
|
|
|
while (!Line.is_at_end() && (Line->empty() || Line->startswith("#")))
|
2014-03-22 01:24:48 +08:00
|
|
|
++Line;
|
|
|
|
// If we hit EOF while looking for a name, we're done.
|
|
|
|
if (Line.is_at_end())
|
|
|
|
return error(instrprof_error::eof);
|
|
|
|
|
|
|
|
// Read the function name.
|
|
|
|
Record.Name = *Line++;
|
|
|
|
|
|
|
|
// Read the function hash.
|
|
|
|
if (Line.is_at_end())
|
|
|
|
return error(instrprof_error::truncated);
|
2015-03-10 02:54:49 +08:00
|
|
|
if ((Line++)->getAsInteger(0, Record.Hash))
|
2014-03-22 01:24:48 +08:00
|
|
|
return error(instrprof_error::malformed);
|
|
|
|
|
|
|
|
// Read the number of counters.
|
|
|
|
uint64_t NumCounters;
|
|
|
|
if (Line.is_at_end())
|
|
|
|
return error(instrprof_error::truncated);
|
|
|
|
if ((Line++)->getAsInteger(10, NumCounters))
|
|
|
|
return error(instrprof_error::malformed);
|
2014-04-25 10:45:33 +08:00
|
|
|
if (NumCounters == 0)
|
|
|
|
return error(instrprof_error::malformed);
|
2014-03-22 01:24:48 +08:00
|
|
|
|
|
|
|
// Read each counter and fill our internal storage with the values.
|
2015-06-23 07:58:05 +08:00
|
|
|
Record.Counts.clear();
|
|
|
|
Record.Counts.reserve(NumCounters);
|
2014-03-22 01:24:48 +08:00
|
|
|
for (uint64_t I = 0; I < NumCounters; ++I) {
|
|
|
|
if (Line.is_at_end())
|
|
|
|
return error(instrprof_error::truncated);
|
|
|
|
uint64_t Count;
|
|
|
|
if ((Line++)->getAsInteger(10, Count))
|
|
|
|
return error(instrprof_error::malformed);
|
2015-06-23 07:58:05 +08:00
|
|
|
Record.Counts.push_back(Count);
|
2014-03-22 01:24:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return success();
|
|
|
|
}
|
2014-03-22 02:26:05 +08:00
|
|
|
|
2014-03-23 11:38:12 +08:00
|
|
|
template <class IntPtrT>
|
|
|
|
bool RawInstrProfReader<IntPtrT>::hasFormat(const MemoryBuffer &DataBuffer) {
|
2014-03-24 08:47:18 +08:00
|
|
|
if (DataBuffer.getBufferSize() < sizeof(uint64_t))
|
2014-03-22 04:42:31 +08:00
|
|
|
return false;
|
2014-03-24 08:47:18 +08:00
|
|
|
uint64_t Magic =
|
|
|
|
*reinterpret_cast<const uint64_t *>(DataBuffer.getBufferStart());
|
2015-10-18 09:02:29 +08:00
|
|
|
return RawInstrProf::getMagic<IntPtrT>() == Magic ||
|
|
|
|
sys::getSwappedBytes(RawInstrProf::getMagic<IntPtrT>()) == Magic;
|
2014-03-22 04:42:31 +08:00
|
|
|
}
|
|
|
|
|
2014-03-23 11:38:12 +08:00
|
|
|
template <class IntPtrT>
|
2014-06-13 10:24:39 +08:00
|
|
|
std::error_code RawInstrProfReader<IntPtrT>::readHeader() {
|
2014-03-22 04:42:31 +08:00
|
|
|
if (!hasFormat(*DataBuffer))
|
|
|
|
return error(instrprof_error::bad_magic);
|
2015-10-18 09:02:29 +08:00
|
|
|
if (DataBuffer->getBufferSize() < sizeof(RawInstrProf::Header))
|
2014-03-22 04:42:28 +08:00
|
|
|
return error(instrprof_error::bad_header);
|
2015-10-18 09:02:29 +08:00
|
|
|
auto *Header = reinterpret_cast<const RawInstrProf::Header *>(
|
|
|
|
DataBuffer->getBufferStart());
|
|
|
|
ShouldSwapBytes = Header->Magic != RawInstrProf::getMagic<IntPtrT>();
|
2014-03-22 02:26:05 +08:00
|
|
|
return readHeader(*Header);
|
|
|
|
}
|
|
|
|
|
2014-05-16 08:38:00 +08:00
|
|
|
template <class IntPtrT>
|
2014-06-13 10:24:39 +08:00
|
|
|
std::error_code
|
|
|
|
RawInstrProfReader<IntPtrT>::readNextHeader(const char *CurrentPos) {
|
2014-05-16 08:38:00 +08:00
|
|
|
const char *End = DataBuffer->getBufferEnd();
|
|
|
|
// Skip zero padding between profiles.
|
|
|
|
while (CurrentPos != End && *CurrentPos == 0)
|
|
|
|
++CurrentPos;
|
|
|
|
// If there's nothing left, we're done.
|
|
|
|
if (CurrentPos == End)
|
|
|
|
return instrprof_error::eof;
|
|
|
|
// If there isn't enough space for another header, this is probably just
|
|
|
|
// garbage at the end of the file.
|
2015-10-18 09:02:29 +08:00
|
|
|
if (CurrentPos + sizeof(RawInstrProf::Header) > End)
|
2014-05-16 08:38:00 +08:00
|
|
|
return instrprof_error::malformed;
|
2014-09-13 05:22:55 +08:00
|
|
|
// The writer ensures each profile is padded to start at an aligned address.
|
|
|
|
if (reinterpret_cast<size_t>(CurrentPos) % alignOf<uint64_t>())
|
|
|
|
return instrprof_error::malformed;
|
2014-05-16 08:38:00 +08:00
|
|
|
// The magic should have the same byte order as in the previous header.
|
|
|
|
uint64_t Magic = *reinterpret_cast<const uint64_t *>(CurrentPos);
|
2015-10-18 09:02:29 +08:00
|
|
|
if (Magic != swap(RawInstrProf::getMagic<IntPtrT>()))
|
2014-05-16 08:38:00 +08:00
|
|
|
return instrprof_error::bad_magic;
|
|
|
|
|
|
|
|
// There's another profile to read, so we need to process the header.
|
2015-10-18 09:02:29 +08:00
|
|
|
auto *Header = reinterpret_cast<const RawInstrProf::Header *>(CurrentPos);
|
2014-05-16 08:38:00 +08:00
|
|
|
return readHeader(*Header);
|
|
|
|
}
|
|
|
|
|
2014-03-23 11:38:12 +08:00
|
|
|
template <class IntPtrT>
|
2015-10-18 09:02:29 +08:00
|
|
|
std::error_code RawInstrProfReader<IntPtrT>::readHeader(
|
|
|
|
const RawInstrProf::Header &Header) {
|
|
|
|
if (swap(Header.Version) != RawInstrProf::Version)
|
2014-03-22 02:26:05 +08:00
|
|
|
return error(instrprof_error::unsupported_version);
|
|
|
|
|
|
|
|
CountersDelta = swap(Header.CountersDelta);
|
|
|
|
NamesDelta = swap(Header.NamesDelta);
|
|
|
|
auto DataSize = swap(Header.DataSize);
|
|
|
|
auto CountersSize = swap(Header.CountersSize);
|
|
|
|
auto NamesSize = swap(Header.NamesSize);
|
|
|
|
|
2015-10-18 09:02:29 +08:00
|
|
|
ptrdiff_t DataOffset = sizeof(RawInstrProf::Header);
|
|
|
|
ptrdiff_t CountersOffset =
|
|
|
|
DataOffset + sizeof(RawInstrProf::ProfileData<IntPtrT>) * DataSize;
|
2014-03-22 02:26:05 +08:00
|
|
|
ptrdiff_t NamesOffset = CountersOffset + sizeof(uint64_t) * CountersSize;
|
2014-05-16 08:38:00 +08:00
|
|
|
size_t ProfileSize = NamesOffset + sizeof(char) * NamesSize;
|
2014-03-22 02:26:05 +08:00
|
|
|
|
2014-05-16 08:38:00 +08:00
|
|
|
auto *Start = reinterpret_cast<const char *>(&Header);
|
|
|
|
if (Start + ProfileSize > DataBuffer->getBufferEnd())
|
2014-03-22 04:42:28 +08:00
|
|
|
return error(instrprof_error::bad_header);
|
2014-03-22 02:26:05 +08:00
|
|
|
|
2015-10-18 09:02:29 +08:00
|
|
|
Data = reinterpret_cast<const RawInstrProf::ProfileData<IntPtrT> *>(
|
|
|
|
Start + DataOffset);
|
2014-03-22 02:26:05 +08:00
|
|
|
DataEnd = Data + DataSize;
|
2014-03-24 08:47:18 +08:00
|
|
|
CountersStart = reinterpret_cast<const uint64_t *>(Start + CountersOffset);
|
|
|
|
NamesStart = Start + NamesOffset;
|
2014-05-16 08:38:00 +08:00
|
|
|
ProfileEnd = Start + ProfileSize;
|
2014-03-22 02:26:05 +08:00
|
|
|
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2014-03-23 11:38:12 +08:00
|
|
|
template <class IntPtrT>
|
2015-10-29 03:34:04 +08:00
|
|
|
std::error_code RawInstrProfReader<IntPtrT>::readName(InstrProfRecord &Record) {
|
|
|
|
Record.Name = StringRef(getName(Data->NamePtr), swap(Data->NameSize));
|
|
|
|
if (Record.Name.data() < NamesStart ||
|
|
|
|
Record.Name.data() + Record.Name.size() > DataBuffer->getBufferEnd())
|
|
|
|
return error(instrprof_error::malformed);
|
|
|
|
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class IntPtrT>
|
|
|
|
std::error_code RawInstrProfReader<IntPtrT>::readFuncHash(
|
|
|
|
InstrProfRecord &Record) {
|
|
|
|
Record.Hash = swap(Data->FuncHash);
|
|
|
|
return success();
|
|
|
|
}
|
2014-03-22 02:26:05 +08:00
|
|
|
|
2015-10-29 03:34:04 +08:00
|
|
|
template <class IntPtrT>
|
|
|
|
std::error_code RawInstrProfReader<IntPtrT>::readRawCounts(
|
|
|
|
InstrProfRecord &Record) {
|
2014-04-25 10:45:33 +08:00
|
|
|
uint32_t NumCounters = swap(Data->NumCounters);
|
2015-10-29 03:34:04 +08:00
|
|
|
IntPtrT CounterPtr = Data->CounterPtr;
|
2014-04-25 10:45:33 +08:00
|
|
|
if (NumCounters == 0)
|
|
|
|
return error(instrprof_error::malformed);
|
2014-03-22 02:26:05 +08:00
|
|
|
|
2015-10-29 03:34:04 +08:00
|
|
|
auto RawCounts = makeArrayRef(getCounter(CounterPtr), NumCounters);
|
2014-03-24 08:47:18 +08:00
|
|
|
auto *NamesStartAsCounter = reinterpret_cast<const uint64_t *>(NamesStart);
|
2015-10-29 03:34:04 +08:00
|
|
|
|
|
|
|
// Check bounds.
|
|
|
|
if (RawCounts.data() < CountersStart ||
|
2014-03-24 08:47:18 +08:00
|
|
|
RawCounts.data() + RawCounts.size() > NamesStartAsCounter)
|
2014-03-22 02:26:05 +08:00
|
|
|
return error(instrprof_error::malformed);
|
|
|
|
|
|
|
|
if (ShouldSwapBytes) {
|
2015-06-23 07:58:05 +08:00
|
|
|
Record.Counts.clear();
|
|
|
|
Record.Counts.reserve(RawCounts.size());
|
2014-03-22 02:26:05 +08:00
|
|
|
for (uint64_t Count : RawCounts)
|
2015-06-23 07:58:05 +08:00
|
|
|
Record.Counts.push_back(swap(Count));
|
2014-03-22 02:26:05 +08:00
|
|
|
} else
|
|
|
|
Record.Counts = RawCounts;
|
|
|
|
|
2015-10-29 03:34:04 +08:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class IntPtrT>
|
|
|
|
std::error_code RawInstrProfReader<IntPtrT>::readNextRecord(
|
|
|
|
InstrProfRecord &Record) {
|
|
|
|
if (atEnd())
|
|
|
|
if (std::error_code EC = readNextHeader(ProfileEnd)) return EC;
|
|
|
|
|
|
|
|
// Read name ad set it in Record.
|
|
|
|
if (std::error_code EC = readName(Record)) return EC;
|
|
|
|
|
|
|
|
// Read FuncHash and set it in Record.
|
|
|
|
if (std::error_code EC = readFuncHash(Record)) return EC;
|
|
|
|
|
|
|
|
// Read raw counts and set Record.
|
|
|
|
if (std::error_code EC = readRawCounts(Record)) return EC;
|
|
|
|
|
2014-03-22 02:26:05 +08:00
|
|
|
// Iterate.
|
2015-10-29 03:34:04 +08:00
|
|
|
advanceData();
|
2014-03-22 02:26:05 +08:00
|
|
|
return success();
|
|
|
|
}
|
2014-03-23 11:38:12 +08:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
template class RawInstrProfReader<uint32_t>;
|
|
|
|
template class RawInstrProfReader<uint64_t>;
|
|
|
|
}
|
2014-04-19 05:48:40 +08:00
|
|
|
|
2014-04-19 06:00:22 +08:00
|
|
|
InstrProfLookupTrait::hash_value_type
|
|
|
|
InstrProfLookupTrait::ComputeHash(StringRef K) {
|
|
|
|
return IndexedInstrProf::ComputeHash(HashType, K);
|
|
|
|
}
|
|
|
|
|
2015-06-23 07:58:05 +08:00
|
|
|
typedef InstrProfLookupTrait::data_type data_type;
|
|
|
|
typedef InstrProfLookupTrait::offset_type offset_type;
|
|
|
|
|
2015-09-30 06:13:58 +08:00
|
|
|
bool InstrProfLookupTrait::ReadValueProfilingData(
|
|
|
|
const unsigned char *&D, const unsigned char *const End) {
|
|
|
|
|
|
|
|
using namespace support;
|
|
|
|
// Read number of value kinds with value sites.
|
|
|
|
if (D + sizeof(uint64_t) > End)
|
|
|
|
return false;
|
|
|
|
uint64_t ValueKindCount = endian::readNext<uint64_t, little, unaligned>(D);
|
|
|
|
|
|
|
|
for (uint32_t Kind = 0; Kind < ValueKindCount; ++Kind) {
|
|
|
|
|
|
|
|
// Read value kind and number of value sites for kind.
|
|
|
|
if (D + 2 * sizeof(uint64_t) > End)
|
|
|
|
return false;
|
|
|
|
uint64_t ValueKind = endian::readNext<uint64_t, little, unaligned>(D);
|
|
|
|
uint64_t ValueSiteCount = endian::readNext<uint64_t, little, unaligned>(D);
|
|
|
|
|
|
|
|
std::vector<InstrProfValueSiteRecord> &ValueSites =
|
|
|
|
DataBuffer.back().getValueSitesForKind(ValueKind);
|
|
|
|
ValueSites.reserve(ValueSiteCount);
|
|
|
|
for (uint64_t VSite = 0; VSite < ValueSiteCount; ++VSite) {
|
|
|
|
// Read number of value data pairs at value site.
|
|
|
|
if (D + sizeof(uint64_t) > End)
|
|
|
|
return false;
|
|
|
|
uint64_t ValueDataCount =
|
|
|
|
endian::readNext<uint64_t, little, unaligned>(D);
|
|
|
|
|
|
|
|
// Check if there are as many ValueDataPairs as ValueDataCount in memory.
|
|
|
|
if (D + (ValueDataCount << 1) * sizeof(uint64_t) > End)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
InstrProfValueSiteRecord VSiteRecord;
|
|
|
|
for (uint64_t VCount = 0; VCount < ValueDataCount; ++VCount) {
|
|
|
|
uint64_t Value = endian::readNext<uint64_t, little, unaligned>(D);
|
|
|
|
uint64_t NumTaken = endian::readNext<uint64_t, little, unaligned>(D);
|
|
|
|
switch (ValueKind) {
|
|
|
|
case IPVK_IndirectCallTarget: {
|
|
|
|
auto Result =
|
|
|
|
std::lower_bound(HashKeys.begin(), HashKeys.end(), Value,
|
|
|
|
[](const std::pair<uint64_t, const char *> &LHS,
|
|
|
|
uint64_t RHS) { return LHS.first < RHS; });
|
|
|
|
assert(Result != HashKeys.end() &&
|
|
|
|
"Hash does not match any known keys\n");
|
|
|
|
Value = (uint64_t)Result->second;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
VSiteRecord.ValueData.push_back(std::make_pair(Value, NumTaken));
|
|
|
|
}
|
|
|
|
ValueSites.push_back(std::move(VSiteRecord));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-06-23 07:58:05 +08:00
|
|
|
data_type InstrProfLookupTrait::ReadData(StringRef K, const unsigned char *D,
|
|
|
|
offset_type N) {
|
|
|
|
// Check if the data is corrupt. If so, don't try to read it.
|
|
|
|
if (N % sizeof(uint64_t))
|
|
|
|
return data_type();
|
|
|
|
|
|
|
|
DataBuffer.clear();
|
|
|
|
std::vector<uint64_t> CounterBuffer;
|
|
|
|
|
2015-09-30 06:13:58 +08:00
|
|
|
using namespace support;
|
|
|
|
const unsigned char *End = D + N;
|
|
|
|
while (D < End) {
|
2015-10-14 00:35:59 +08:00
|
|
|
// Read hash.
|
2015-09-30 06:13:58 +08:00
|
|
|
if (D + sizeof(uint64_t) >= End)
|
2015-06-23 07:58:05 +08:00
|
|
|
return data_type();
|
2015-09-30 06:13:58 +08:00
|
|
|
uint64_t Hash = endian::readNext<uint64_t, little, unaligned>(D);
|
2015-06-23 07:58:05 +08:00
|
|
|
|
2015-10-14 00:35:59 +08:00
|
|
|
// Initialize number of counters for FormatVersion == 1.
|
2015-09-30 06:13:58 +08:00
|
|
|
uint64_t CountsSize = N / sizeof(uint64_t) - 1;
|
2015-10-14 00:35:59 +08:00
|
|
|
// If format version is different then read the number of counters.
|
2015-09-30 06:13:58 +08:00
|
|
|
if (FormatVersion != 1) {
|
|
|
|
if (D + sizeof(uint64_t) > End)
|
|
|
|
return data_type();
|
|
|
|
CountsSize = endian::readNext<uint64_t, little, unaligned>(D);
|
|
|
|
}
|
2015-10-14 00:35:59 +08:00
|
|
|
// Read counter values.
|
2015-09-30 06:13:58 +08:00
|
|
|
if (D + CountsSize * sizeof(uint64_t) > End)
|
2015-06-23 07:58:05 +08:00
|
|
|
return data_type();
|
|
|
|
|
|
|
|
CounterBuffer.clear();
|
2015-09-30 06:13:58 +08:00
|
|
|
CounterBuffer.reserve(CountsSize);
|
|
|
|
for (uint64_t J = 0; J < CountsSize; ++J)
|
2015-06-23 07:58:05 +08:00
|
|
|
CounterBuffer.push_back(endian::readNext<uint64_t, little, unaligned>(D));
|
|
|
|
|
|
|
|
DataBuffer.push_back(InstrProfRecord(K, Hash, std::move(CounterBuffer)));
|
2015-09-30 06:13:58 +08:00
|
|
|
|
2015-10-14 00:35:59 +08:00
|
|
|
// Read value profiling data.
|
2015-09-30 06:13:58 +08:00
|
|
|
if (FormatVersion > 2 && !ReadValueProfilingData(D, End)) {
|
|
|
|
DataBuffer.clear();
|
|
|
|
return data_type();
|
|
|
|
}
|
2015-06-23 07:58:05 +08:00
|
|
|
}
|
|
|
|
return DataBuffer;
|
|
|
|
}
|
|
|
|
|
2015-10-28 12:20:31 +08:00
|
|
|
std::error_code InstrProfReaderIndex::getRecords(
|
|
|
|
StringRef FuncName, ArrayRef<InstrProfRecord> &Data) {
|
|
|
|
auto Iter = Index->find(FuncName);
|
|
|
|
if (Iter == Index->end()) return instrprof_error::unknown_function;
|
|
|
|
|
|
|
|
Data = (*Iter);
|
|
|
|
if (Data.empty()) return instrprof_error::malformed;
|
|
|
|
|
|
|
|
return instrprof_error::success;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::error_code InstrProfReaderIndex::getRecords(
|
|
|
|
ArrayRef<InstrProfRecord> &Data) {
|
|
|
|
if (atEnd()) return instrprof_error::eof;
|
|
|
|
|
|
|
|
Data = *RecordIterator;
|
|
|
|
|
|
|
|
if (Data.empty()) return instrprof_error::malformed;
|
|
|
|
|
|
|
|
return instrprof_error::success;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InstrProfReaderIndex::Init(const unsigned char *Buckets,
|
|
|
|
const unsigned char *const Payload,
|
|
|
|
const unsigned char *const Base,
|
|
|
|
IndexedInstrProf::HashT HashType,
|
|
|
|
uint64_t Version) {
|
|
|
|
FormatVersion = Version;
|
|
|
|
Index.reset(IndexType::Create(Buckets, Payload, Base,
|
|
|
|
InstrProfLookupTrait(HashType, Version)));
|
|
|
|
// Form the map of hash values to const char* keys in profiling data.
|
|
|
|
std::vector<std::pair<uint64_t, const char *>> HashKeys;
|
|
|
|
for (auto Key : Index->keys()) {
|
|
|
|
const char *KeyTableRef = StringTable.insertString(Key);
|
|
|
|
HashKeys.push_back(std::make_pair(ComputeHash(HashType, Key), KeyTableRef));
|
|
|
|
}
|
|
|
|
std::sort(HashKeys.begin(), HashKeys.end(), less_first());
|
|
|
|
HashKeys.erase(std::unique(HashKeys.begin(), HashKeys.end()), HashKeys.end());
|
|
|
|
// Set the hash key map for the InstrLookupTrait
|
|
|
|
Index->getInfoObj().setHashKeys(std::move(HashKeys));
|
|
|
|
RecordIterator = Index->data_begin();
|
|
|
|
}
|
|
|
|
|
2014-04-19 05:48:40 +08:00
|
|
|
bool IndexedInstrProfReader::hasFormat(const MemoryBuffer &DataBuffer) {
|
2015-10-28 12:20:31 +08:00
|
|
|
if (DataBuffer.getBufferSize() < 8) return false;
|
2014-04-19 05:48:40 +08:00
|
|
|
using namespace support;
|
|
|
|
uint64_t Magic =
|
|
|
|
endian::read<uint64_t, little, aligned>(DataBuffer.getBufferStart());
|
2015-10-14 00:35:59 +08:00
|
|
|
// Verify that it's magical.
|
2014-04-19 05:48:40 +08:00
|
|
|
return Magic == IndexedInstrProf::Magic;
|
|
|
|
}
|
|
|
|
|
2014-06-13 10:24:39 +08:00
|
|
|
std::error_code IndexedInstrProfReader::readHeader() {
|
2014-05-02 01:16:24 +08:00
|
|
|
const unsigned char *Start =
|
|
|
|
(const unsigned char *)DataBuffer->getBufferStart();
|
2014-04-19 05:48:40 +08:00
|
|
|
const unsigned char *Cur = Start;
|
2014-05-02 01:16:24 +08:00
|
|
|
if ((const unsigned char *)DataBuffer->getBufferEnd() - Cur < 24)
|
2014-04-19 05:48:40 +08:00
|
|
|
return error(instrprof_error::truncated);
|
|
|
|
|
|
|
|
using namespace support;
|
|
|
|
|
2015-10-18 09:02:29 +08:00
|
|
|
auto *Header = reinterpret_cast<const IndexedInstrProf::Header *>(Cur);
|
|
|
|
Cur += sizeof(IndexedInstrProf::Header);
|
|
|
|
|
2014-04-19 05:48:40 +08:00
|
|
|
// Check the magic number.
|
2015-10-18 09:02:29 +08:00
|
|
|
uint64_t Magic = endian::byte_swap<uint64_t, little>(Header->Magic);
|
2014-04-19 05:48:40 +08:00
|
|
|
if (Magic != IndexedInstrProf::Magic)
|
|
|
|
return error(instrprof_error::bad_magic);
|
|
|
|
|
|
|
|
// Read the version.
|
2015-10-28 12:20:31 +08:00
|
|
|
uint64_t FormatVersion = endian::byte_swap<uint64_t, little>(Header->Version);
|
2014-08-02 06:50:07 +08:00
|
|
|
if (FormatVersion > IndexedInstrProf::Version)
|
2014-04-19 05:48:40 +08:00
|
|
|
return error(instrprof_error::unsupported_version);
|
|
|
|
|
|
|
|
// Read the maximal function count.
|
2015-10-18 09:02:29 +08:00
|
|
|
MaxFunctionCount =
|
|
|
|
endian::byte_swap<uint64_t, little>(Header->MaxFunctionCount);
|
2014-04-19 05:48:40 +08:00
|
|
|
|
|
|
|
// Read the hash type and start offset.
|
|
|
|
IndexedInstrProf::HashT HashType = static_cast<IndexedInstrProf::HashT>(
|
2015-10-18 09:02:29 +08:00
|
|
|
endian::byte_swap<uint64_t, little>(Header->HashType));
|
2014-04-19 05:48:40 +08:00
|
|
|
if (HashType > IndexedInstrProf::HashT::Last)
|
|
|
|
return error(instrprof_error::unsupported_hash_type);
|
2015-10-18 09:02:29 +08:00
|
|
|
|
|
|
|
uint64_t HashOffset = endian::byte_swap<uint64_t, little>(Header->HashOffset);
|
2014-04-19 05:48:40 +08:00
|
|
|
|
|
|
|
// The rest of the file is an on disk hash table.
|
2015-10-28 12:20:31 +08:00
|
|
|
Index.Init(Start + HashOffset, Cur, Start, HashType, FormatVersion);
|
2014-04-19 05:48:40 +08:00
|
|
|
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2014-06-13 10:24:39 +08:00
|
|
|
std::error_code IndexedInstrProfReader::getFunctionCounts(
|
2014-08-02 06:50:07 +08:00
|
|
|
StringRef FuncName, uint64_t FuncHash, std::vector<uint64_t> &Counts) {
|
2015-10-28 12:20:31 +08:00
|
|
|
ArrayRef<InstrProfRecord> Data;
|
2014-04-19 05:48:40 +08:00
|
|
|
|
2015-10-28 12:20:31 +08:00
|
|
|
std::error_code EC = Index.getRecords(FuncName, Data);
|
|
|
|
if (EC != instrprof_error::success) return EC;
|
2015-06-23 07:58:05 +08:00
|
|
|
|
2015-10-28 12:20:31 +08:00
|
|
|
// Found it. Look for counters with the right hash.
|
2015-06-23 07:58:05 +08:00
|
|
|
for (unsigned I = 0, E = Data.size(); I < E; ++I) {
|
2014-08-02 06:50:07 +08:00
|
|
|
// Check for a match and fill the vector if there is one.
|
2015-06-23 07:58:05 +08:00
|
|
|
if (Data[I].Hash == FuncHash) {
|
|
|
|
Counts = Data[I].Counts;
|
2014-08-02 06:50:07 +08:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return error(instrprof_error::hash_mismatch);
|
2014-04-19 05:48:40 +08:00
|
|
|
}
|
|
|
|
|
2015-10-28 12:20:31 +08:00
|
|
|
std::error_code IndexedInstrProfReader::readNextRecord(
|
|
|
|
InstrProfRecord &Record) {
|
|
|
|
static unsigned RecordIndex = 0;
|
2014-04-19 05:48:40 +08:00
|
|
|
|
2015-10-28 12:20:31 +08:00
|
|
|
ArrayRef<InstrProfRecord> Data;
|
|
|
|
|
|
|
|
std::error_code EC = Index.getRecords(Data);
|
|
|
|
if (EC != instrprof_error::success) return error(EC);
|
2014-08-02 06:50:07 +08:00
|
|
|
|
2015-06-23 07:58:05 +08:00
|
|
|
Record = Data[RecordIndex++];
|
|
|
|
if (RecordIndex >= Data.size()) {
|
2015-10-28 12:20:31 +08:00
|
|
|
Index.advanceToNextKey();
|
2015-06-23 07:58:05 +08:00
|
|
|
RecordIndex = 0;
|
2014-08-02 06:50:07 +08:00
|
|
|
}
|
2014-04-19 05:48:40 +08:00
|
|
|
return success();
|
|
|
|
}
|