LLVM  3.7.0
InstrProfReader.cpp
Go to the documentation of this file.
1 //=-- InstrProfReader.cpp - Instrumented profiling reader -------------------=//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains support for reading profiling data for clang's
11 // instrumentation based PGO and coverage.
12 //
13 //===----------------------------------------------------------------------===//
14 
16 #include "InstrProfIndexed.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include <cassert>
19 
20 using namespace llvm;
21 
23 setupMemoryBuffer(std::string Path) {
26  if (std::error_code EC = BufferOrErr.getError())
27  return EC;
28  return std::move(BufferOrErr.get());
29 }
30 
31 static std::error_code initializeReader(InstrProfReader &Reader) {
32  return Reader.readHeader();
33 }
34 
36 InstrProfReader::create(std::string Path) {
37  // Set up the buffer to read.
38  auto BufferOrError = setupMemoryBuffer(Path);
39  if (std::error_code EC = BufferOrError.getError())
40  return EC;
41  return InstrProfReader::create(std::move(BufferOrError.get()));
42 }
43 
45 InstrProfReader::create(std::unique_ptr<MemoryBuffer> Buffer) {
46  // Sanity check the buffer.
47  if (Buffer->getBufferSize() > std::numeric_limits<unsigned>::max())
49 
50  std::unique_ptr<InstrProfReader> Result;
51  // Create the reader.
53  Result.reset(new IndexedInstrProfReader(std::move(Buffer)));
54  else if (RawInstrProfReader64::hasFormat(*Buffer))
55  Result.reset(new RawInstrProfReader64(std::move(Buffer)));
56  else if (RawInstrProfReader32::hasFormat(*Buffer))
57  Result.reset(new RawInstrProfReader32(std::move(Buffer)));
58  else
59  Result.reset(new TextInstrProfReader(std::move(Buffer)));
60 
61  // Initialize the reader and return the result.
62  if (std::error_code EC = initializeReader(*Result))
63  return EC;
64 
65  return std::move(Result);
66 }
67 
69 IndexedInstrProfReader::create(std::string Path) {
70  // Set up the buffer to read.
71  auto BufferOrError = setupMemoryBuffer(Path);
72  if (std::error_code EC = BufferOrError.getError())
73  return EC;
74  return IndexedInstrProfReader::create(std::move(BufferOrError.get()));
75 }
76 
77 
79 IndexedInstrProfReader::create(std::unique_ptr<MemoryBuffer> Buffer) {
80  // Sanity check the buffer.
81  if (Buffer->getBufferSize() > std::numeric_limits<unsigned>::max())
83 
84  // Create the reader.
87  auto Result = llvm::make_unique<IndexedInstrProfReader>(std::move(Buffer));
88 
89  // Initialize the reader and return the result.
90  if (std::error_code EC = initializeReader(*Result))
91  return EC;
92 
93  return std::move(Result);
94 }
95 
96 void InstrProfIterator::Increment() {
97  if (Reader->readNextRecord(Record))
98  *this = InstrProfIterator();
99 }
100 
102  // Skip empty lines and comments.
103  while (!Line.is_at_end() && (Line->empty() || Line->startswith("#")))
104  ++Line;
105  // If we hit EOF while looking for a name, we're done.
106  if (Line.is_at_end())
107  return error(instrprof_error::eof);
108 
109  // Read the function name.
110  Record.Name = *Line++;
111 
112  // Read the function hash.
113  if (Line.is_at_end())
115  if ((Line++)->getAsInteger(0, Record.Hash))
117 
118  // Read the number of counters.
119  uint64_t NumCounters;
120  if (Line.is_at_end())
122  if ((Line++)->getAsInteger(10, NumCounters))
124  if (NumCounters == 0)
126 
127  // Read each counter and fill our internal storage with the values.
128  Record.Counts.clear();
129  Record.Counts.reserve(NumCounters);
130  for (uint64_t I = 0; I < NumCounters; ++I) {
131  if (Line.is_at_end())
133  uint64_t Count;
134  if ((Line++)->getAsInteger(10, Count))
136  Record.Counts.push_back(Count);
137  }
138 
139  return success();
140 }
141 
142 template <class IntPtrT>
143 static uint64_t getRawMagic();
144 
145 template <>
147  return
148  uint64_t(255) << 56 |
149  uint64_t('l') << 48 |
150  uint64_t('p') << 40 |
151  uint64_t('r') << 32 |
152  uint64_t('o') << 24 |
153  uint64_t('f') << 16 |
154  uint64_t('r') << 8 |
155  uint64_t(129);
156 }
157 
158 template <>
160  return
161  uint64_t(255) << 56 |
162  uint64_t('l') << 48 |
163  uint64_t('p') << 40 |
164  uint64_t('r') << 32 |
165  uint64_t('o') << 24 |
166  uint64_t('f') << 16 |
167  uint64_t('R') << 8 |
168  uint64_t(129);
169 }
170 
171 template <class IntPtrT>
173  if (DataBuffer.getBufferSize() < sizeof(uint64_t))
174  return false;
175  uint64_t Magic =
176  *reinterpret_cast<const uint64_t *>(DataBuffer.getBufferStart());
177  return getRawMagic<IntPtrT>() == Magic ||
178  sys::getSwappedBytes(getRawMagic<IntPtrT>()) == Magic;
179 }
180 
181 template <class IntPtrT>
183  if (!hasFormat(*DataBuffer))
185  if (DataBuffer->getBufferSize() < sizeof(RawHeader))
187  auto *Header =
188  reinterpret_cast<const RawHeader *>(DataBuffer->getBufferStart());
189  ShouldSwapBytes = Header->Magic != getRawMagic<IntPtrT>();
190  return readHeader(*Header);
191 }
192 
193 template <class IntPtrT>
194 std::error_code
195 RawInstrProfReader<IntPtrT>::readNextHeader(const char *CurrentPos) {
196  const char *End = DataBuffer->getBufferEnd();
197  // Skip zero padding between profiles.
198  while (CurrentPos != End && *CurrentPos == 0)
199  ++CurrentPos;
200  // If there's nothing left, we're done.
201  if (CurrentPos == End)
202  return instrprof_error::eof;
203  // If there isn't enough space for another header, this is probably just
204  // garbage at the end of the file.
205  if (CurrentPos + sizeof(RawHeader) > End)
207  // The writer ensures each profile is padded to start at an aligned address.
208  if (reinterpret_cast<size_t>(CurrentPos) % alignOf<uint64_t>())
210  // The magic should have the same byte order as in the previous header.
211  uint64_t Magic = *reinterpret_cast<const uint64_t *>(CurrentPos);
212  if (Magic != swap(getRawMagic<IntPtrT>()))
214 
215  // There's another profile to read, so we need to process the header.
216  auto *Header = reinterpret_cast<const RawHeader *>(CurrentPos);
217  return readHeader(*Header);
218 }
219 
220 static uint64_t getRawVersion() {
221  return 1;
222 }
223 
224 template <class IntPtrT>
225 std::error_code
226 RawInstrProfReader<IntPtrT>::readHeader(const RawHeader &Header) {
227  if (swap(Header.Version) != getRawVersion())
229 
230  CountersDelta = swap(Header.CountersDelta);
231  NamesDelta = swap(Header.NamesDelta);
232  auto DataSize = swap(Header.DataSize);
233  auto CountersSize = swap(Header.CountersSize);
234  auto NamesSize = swap(Header.NamesSize);
235 
236  ptrdiff_t DataOffset = sizeof(RawHeader);
237  ptrdiff_t CountersOffset = DataOffset + sizeof(ProfileData) * DataSize;
238  ptrdiff_t NamesOffset = CountersOffset + sizeof(uint64_t) * CountersSize;
239  size_t ProfileSize = NamesOffset + sizeof(char) * NamesSize;
240 
241  auto *Start = reinterpret_cast<const char *>(&Header);
242  if (Start + ProfileSize > DataBuffer->getBufferEnd())
244 
245  Data = reinterpret_cast<const ProfileData *>(Start + DataOffset);
246  DataEnd = Data + DataSize;
247  CountersStart = reinterpret_cast<const uint64_t *>(Start + CountersOffset);
248  NamesStart = Start + NamesOffset;
249  ProfileEnd = Start + ProfileSize;
250 
251  return success();
252 }
253 
254 template <class IntPtrT>
255 std::error_code
257  if (Data == DataEnd)
258  if (std::error_code EC = readNextHeader(ProfileEnd))
259  return EC;
260 
261  // Get the raw data.
262  StringRef RawName(getName(Data->NamePtr), swap(Data->NameSize));
263  uint32_t NumCounters = swap(Data->NumCounters);
264  if (NumCounters == 0)
266  auto RawCounts = makeArrayRef(getCounter(Data->CounterPtr), NumCounters);
267 
268  // Check bounds.
269  auto *NamesStartAsCounter = reinterpret_cast<const uint64_t *>(NamesStart);
270  if (RawName.data() < NamesStart ||
271  RawName.data() + RawName.size() > DataBuffer->getBufferEnd() ||
272  RawCounts.data() < CountersStart ||
273  RawCounts.data() + RawCounts.size() > NamesStartAsCounter)
275 
276  // Store the data in Record, byte-swapping as necessary.
277  Record.Hash = swap(Data->FuncHash);
278  Record.Name = RawName;
279  if (ShouldSwapBytes) {
280  Record.Counts.clear();
281  Record.Counts.reserve(RawCounts.size());
282  for (uint64_t Count : RawCounts)
283  Record.Counts.push_back(swap(Count));
284  } else
285  Record.Counts = RawCounts;
286 
287  // Iterate.
288  ++Data;
289  return success();
290 }
291 
292 namespace llvm {
293 template class RawInstrProfReader<uint32_t>;
294 template class RawInstrProfReader<uint64_t>;
295 }
296 
299  return IndexedInstrProf::ComputeHash(HashType, K);
300 }
301 
304 
306  offset_type N) {
307 
308  // Check if the data is corrupt. If so, don't try to read it.
309  if (N % sizeof(uint64_t))
310  return data_type();
311 
312  DataBuffer.clear();
313  uint64_t NumCounts;
314  uint64_t NumEntries = N / sizeof(uint64_t);
315  std::vector<uint64_t> CounterBuffer;
316  for (uint64_t I = 0; I < NumEntries; I += NumCounts) {
317  using namespace support;
318  // The function hash comes first.
319  uint64_t Hash = endian::readNext<uint64_t, little, unaligned>(D);
320 
321  if (++I >= NumEntries)
322  return data_type();
323 
324  // In v1, we have at least one count.
325  // Later, we have the number of counts.
326  NumCounts = (1 == FormatVersion)
327  ? NumEntries - I
328  : endian::readNext<uint64_t, little, unaligned>(D);
329  if (1 != FormatVersion)
330  ++I;
331 
332  // If we have more counts than data, this is bogus.
333  if (I + NumCounts > NumEntries)
334  return data_type();
335 
336  CounterBuffer.clear();
337  for (unsigned J = 0; J < NumCounts; ++J)
338  CounterBuffer.push_back(endian::readNext<uint64_t, little, unaligned>(D));
339 
340  DataBuffer.push_back(InstrProfRecord(K, Hash, std::move(CounterBuffer)));
341  }
342  return DataBuffer;
343 }
344 
346  if (DataBuffer.getBufferSize() < 8)
347  return false;
348  using namespace support;
349  uint64_t Magic =
350  endian::read<uint64_t, little, aligned>(DataBuffer.getBufferStart());
351  return Magic == IndexedInstrProf::Magic;
352 }
353 
355  const unsigned char *Start =
356  (const unsigned char *)DataBuffer->getBufferStart();
357  const unsigned char *Cur = Start;
358  if ((const unsigned char *)DataBuffer->getBufferEnd() - Cur < 24)
360 
361  using namespace support;
362 
363  // Check the magic number.
364  uint64_t Magic = endian::readNext<uint64_t, little, unaligned>(Cur);
365  if (Magic != IndexedInstrProf::Magic)
367 
368  // Read the version.
369  FormatVersion = endian::readNext<uint64_t, little, unaligned>(Cur);
370  if (FormatVersion > IndexedInstrProf::Version)
372 
373  // Read the maximal function count.
374  MaxFunctionCount = endian::readNext<uint64_t, little, unaligned>(Cur);
375 
376  // Read the hash type and start offset.
377  IndexedInstrProf::HashT HashType = static_cast<IndexedInstrProf::HashT>(
378  endian::readNext<uint64_t, little, unaligned>(Cur));
379  if (HashType > IndexedInstrProf::HashT::Last)
381  uint64_t HashOffset = endian::readNext<uint64_t, little, unaligned>(Cur);
382 
383  // The rest of the file is an on disk hash table.
384  Index.reset(InstrProfReaderIndex::Create(
385  Start + HashOffset, Cur, Start,
386  InstrProfLookupTrait(HashType, FormatVersion)));
387  // Set up our iterator for readNextRecord.
388  RecordIterator = Index->data_begin();
389 
390  return success();
391 }
392 
394  StringRef FuncName, uint64_t FuncHash, std::vector<uint64_t> &Counts) {
395  auto Iter = Index->find(FuncName);
396  if (Iter == Index->end())
398 
399  // Found it. Look for counters with the right hash.
401  if (Data.empty())
403 
404  for (unsigned I = 0, E = Data.size(); I < E; ++I) {
405  // Check for a match and fill the vector if there is one.
406  if (Data[I].Hash == FuncHash) {
407  Counts = Data[I].Counts;
408  return success();
409  }
410  }
412 }
413 
414 std::error_code
416  // Are we out of records?
417  if (RecordIterator == Index->data_end())
418  return error(instrprof_error::eof);
419 
420  if ((*RecordIterator).empty())
422 
423  static unsigned RecordIndex = 0;
424  ArrayRef<InstrProfRecord> Data = (*RecordIterator);
425  Record = Data[RecordIndex++];
426  if (RecordIndex >= Data.size()) {
427  ++RecordIterator;
428  RecordIndex = 0;
429  }
430  return success();
431 }
std::error_code readNextRecord(InstrProfRecord &Record) override
Read a single record.
std::error_code getError() const
Definition: ErrorOr.h:178
Represents either an error or a value T.
Definition: ErrorOr.h:82
size_t size() const
size - Get the string size.
Definition: StringRef.h:113
const char * getBufferStart() const
Definition: MemoryBuffer.h:50
virtual std::error_code readHeader()=0
Read the header. Required before reading first record.
static bool hasFormat(const MemoryBuffer &DataBuffer)
Return true if the given buffer is in an indexed instrprof format.
std::error_code success()
Clear the current error code and return a successful one.
std::error_code readNextRecord(InstrProfRecord &Record) override
Read a single record.
static std::error_code initializeReader(InstrProfReader &Reader)
data_type ReadData(StringRef K, const unsigned char *D, offset_type N)
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:308
std::error_code error(std::error_code EC)
Set the current std::error_code and return same.
InstrProfLookupTrait::data_type data_type
static StringRef getName(Value *V)
RawInstrProfReader< uint32_t > RawInstrProfReader32
InstrProfLookupTrait::offset_type offset_type
uint64_t getRawMagic< uint64_t >()
static ErrorOr< std::unique_ptr< MemoryBuffer > > setupMemoryBuffer(std::string Path)
const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:107
static cl::opt< std::string > FuncName("cppfname", cl::desc("Specify the name of the generated function"), cl::value_desc("function name"))
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: ArrayRef.h:31
static std::error_code error(DiagnosticHandlerFunction DiagnosticHandler, std::error_code EC, const Twine &Message)
Reader for the simple text based instrprof format.
uint64_t getRawMagic< uint32_t >()
std::error_code readNextRecord(InstrProfRecord &Record) override
Read a single record.
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:134
static bool hasFormat(const MemoryBuffer &DataBuffer)
static OnDiskIterableChainedHashTable * Create(const unsigned char *Buckets, const unsigned char *const Payload, const unsigned char *const Base, const Info &InfoObj=Info())
Create the hash table.
std::error_code getFunctionCounts(StringRef FuncName, uint64_t FuncHash, std::vector< uint64_t > &Counts)
Fill Counts with the profile data for the given function name.
hash_value_type ComputeHash(StringRef K)
std::error_code readHeader() override
Read the file header.
static uint64_t getRawMagic()
Base class and interface for reading profiling data of any known instrprof format.
bool is_at_end() const
Return true if we're an "end" iterator or have reached EOF.
Definition: LineIterator.h:53
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:129
static const char *const Magic
Definition: Archive.cpp:26
bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:215
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileOrSTDIN(const Twine &Filename, int64_t FileSize=-1)
Open the specified file as a MemoryBuffer, or open stdin if the Filename is "-".
static uint64_t getRawVersion()
Reader for the raw instrprof binary format from runtime.
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:37
static ErrorOr< std::unique_ptr< InstrProfReader > > create(std::string Path)
Factory method to create an appropriately typed reader for the given instrprof file.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:576
std::error_code readHeader() override
Read the header. Required before reading first record.
unsigned char getSwappedBytes(unsigned char C)
Definition: SwapByteOrder.h:70
size_t getBufferSize() const
Definition: MemoryBuffer.h:52
RawInstrProfReader< uint64_t > RawInstrProfReader64
std::vector< uint64_t > Counts
Definition: InstrProf.h:54
virtual std::error_code readNextRecord(InstrProfRecord &Record)=0
Read a single record.
Profiling information for a single function.
Definition: InstrProf.h:48
ArrayRef< InstrProfRecord > data_type
#define I(x, y, z)
Definition: MD5.cpp:54
#define N
static uint64_t ComputeHash(HashT Type, StringRef K)
InstrProfLookupTrait(IndexedInstrProf::HashT HashType, unsigned FormatVersion)
std::string Hash(const Unit &U)
Definition: FuzzerUtil.cpp:39
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
Reader for the indexed binary instrprof format.
reference get()
Definition: ErrorOr.h:175
static ErrorOr< std::unique_ptr< IndexedInstrProfReader > > create(std::string Path)
Factory method to create an indexed reader.
bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:110