1 //=-- InstrProfReader.cpp - Instrumented profiling reader -------------------=//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains support for reading profiling data for clang's
11 // instrumentation based PGO and coverage.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/ProfileData/InstrProfReader.h"
16 #include "llvm/ProfileData/InstrProf.h"
18 #include "InstrProfIndexed.h"
24 static std::error_code
25 setupMemoryBuffer(std::string Path, std::unique_ptr<MemoryBuffer> &Buffer) {
26 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
27 MemoryBuffer::getFileOrSTDIN(Path);
28 if (std::error_code EC = BufferOrErr.getError())
30 Buffer = std::move(BufferOrErr.get());
32 // Sanity check the file.
33 if (Buffer->getBufferSize() > std::numeric_limits<unsigned>::max())
34 return instrprof_error::too_large;
35 return instrprof_error::success;
38 static std::error_code initializeReader(InstrProfReader &Reader) {
39 return Reader.readHeader();
43 InstrProfReader::create(std::string Path,
44 std::unique_ptr<InstrProfReader> &Result) {
45 // Set up the buffer to read.
46 std::unique_ptr<MemoryBuffer> Buffer;
47 if (std::error_code EC = setupMemoryBuffer(Path, Buffer))
51 if (IndexedInstrProfReader::hasFormat(*Buffer))
52 Result.reset(new IndexedInstrProfReader(std::move(Buffer)));
53 else if (RawInstrProfReader64::hasFormat(*Buffer))
54 Result.reset(new RawInstrProfReader64(std::move(Buffer)));
55 else if (RawInstrProfReader32::hasFormat(*Buffer))
56 Result.reset(new RawInstrProfReader32(std::move(Buffer)));
58 Result.reset(new TextInstrProfReader(std::move(Buffer)));
60 // Initialize the reader and return the result.
61 return initializeReader(*Result);
64 std::error_code IndexedInstrProfReader::create(
65 std::string Path, std::unique_ptr<IndexedInstrProfReader> &Result) {
66 // Set up the buffer to read.
67 std::unique_ptr<MemoryBuffer> Buffer;
68 if (std::error_code EC = setupMemoryBuffer(Path, Buffer))
72 if (!IndexedInstrProfReader::hasFormat(*Buffer))
73 return instrprof_error::bad_magic;
74 Result.reset(new IndexedInstrProfReader(std::move(Buffer)));
76 // Initialize the reader and return the result.
77 return initializeReader(*Result);
80 void InstrProfIterator::Increment() {
81 if (Reader->readNextRecord(Record))
82 *this = InstrProfIterator();
85 std::error_code TextInstrProfReader::readNextRecord(InstrProfRecord &Record) {
86 // Skip empty lines and comments.
87 while (!Line.is_at_end() && (Line->empty() || Line->startswith("#")))
89 // If we hit EOF while looking for a name, we're done.
91 return error(instrprof_error::eof);
93 // Read the function name.
94 Record.Name = *Line++;
96 // Read the function hash.
98 return error(instrprof_error::truncated);
99 if ((Line++)->getAsInteger(10, Record.Hash))
100 return error(instrprof_error::malformed);
102 // Read the number of counters.
103 uint64_t NumCounters;
104 if (Line.is_at_end())
105 return error(instrprof_error::truncated);
106 if ((Line++)->getAsInteger(10, NumCounters))
107 return error(instrprof_error::malformed);
108 if (NumCounters == 0)
109 return error(instrprof_error::malformed);
111 // Read each counter and fill our internal storage with the values.
113 Counts.reserve(NumCounters);
114 for (uint64_t I = 0; I < NumCounters; ++I) {
115 if (Line.is_at_end())
116 return error(instrprof_error::truncated);
118 if ((Line++)->getAsInteger(10, Count))
119 return error(instrprof_error::malformed);
120 Counts.push_back(Count);
122 // Give the record a reference to our internal counter storage.
123 Record.Counts = Counts;
128 template <class IntPtrT>
129 static uint64_t getRawMagic();
132 uint64_t getRawMagic<uint64_t>() {
134 uint64_t(255) << 56 |
135 uint64_t('l') << 48 |
136 uint64_t('p') << 40 |
137 uint64_t('r') << 32 |
138 uint64_t('o') << 24 |
139 uint64_t('f') << 16 |
145 uint64_t getRawMagic<uint32_t>() {
147 uint64_t(255) << 56 |
148 uint64_t('l') << 48 |
149 uint64_t('p') << 40 |
150 uint64_t('r') << 32 |
151 uint64_t('o') << 24 |
152 uint64_t('f') << 16 |
157 template <class IntPtrT>
158 bool RawInstrProfReader<IntPtrT>::hasFormat(const MemoryBuffer &DataBuffer) {
159 if (DataBuffer.getBufferSize() < sizeof(uint64_t))
162 *reinterpret_cast<const uint64_t *>(DataBuffer.getBufferStart());
163 return getRawMagic<IntPtrT>() == Magic ||
164 sys::getSwappedBytes(getRawMagic<IntPtrT>()) == Magic;
167 template <class IntPtrT>
168 std::error_code RawInstrProfReader<IntPtrT>::readHeader() {
169 if (!hasFormat(*DataBuffer))
170 return error(instrprof_error::bad_magic);
171 if (DataBuffer->getBufferSize() < sizeof(RawHeader))
172 return error(instrprof_error::bad_header);
174 reinterpret_cast<const RawHeader *>(DataBuffer->getBufferStart());
175 ShouldSwapBytes = Header->Magic != getRawMagic<IntPtrT>();
176 return readHeader(*Header);
179 template <class IntPtrT>
181 RawInstrProfReader<IntPtrT>::readNextHeader(const char *CurrentPos) {
182 const char *End = DataBuffer->getBufferEnd();
183 // Skip zero padding between profiles.
184 while (CurrentPos != End && *CurrentPos == 0)
186 // If there's nothing left, we're done.
187 if (CurrentPos == End)
188 return instrprof_error::eof;
189 // If there isn't enough space for another header, this is probably just
190 // garbage at the end of the file.
191 if (CurrentPos + sizeof(RawHeader) > End)
192 return instrprof_error::malformed;
193 // The magic should have the same byte order as in the previous header.
194 uint64_t Magic = *reinterpret_cast<const uint64_t *>(CurrentPos);
195 if (Magic != swap(getRawMagic<IntPtrT>()))
196 return instrprof_error::bad_magic;
198 // There's another profile to read, so we need to process the header.
199 auto *Header = reinterpret_cast<const RawHeader *>(CurrentPos);
200 return readHeader(*Header);
203 static uint64_t getRawVersion() {
207 template <class IntPtrT>
209 RawInstrProfReader<IntPtrT>::readHeader(const RawHeader &Header) {
210 if (swap(Header.Version) != getRawVersion())
211 return error(instrprof_error::unsupported_version);
213 CountersDelta = swap(Header.CountersDelta);
214 NamesDelta = swap(Header.NamesDelta);
215 auto DataSize = swap(Header.DataSize);
216 auto CountersSize = swap(Header.CountersSize);
217 auto NamesSize = swap(Header.NamesSize);
219 ptrdiff_t DataOffset = sizeof(RawHeader);
220 ptrdiff_t CountersOffset = DataOffset + sizeof(ProfileData) * DataSize;
221 ptrdiff_t NamesOffset = CountersOffset + sizeof(uint64_t) * CountersSize;
222 size_t ProfileSize = NamesOffset + sizeof(char) * NamesSize;
224 auto *Start = reinterpret_cast<const char *>(&Header);
225 if (Start + ProfileSize > DataBuffer->getBufferEnd())
226 return error(instrprof_error::bad_header);
228 Data = reinterpret_cast<const ProfileData *>(Start + DataOffset);
229 DataEnd = Data + DataSize;
230 CountersStart = reinterpret_cast<const uint64_t *>(Start + CountersOffset);
231 NamesStart = Start + NamesOffset;
232 ProfileEnd = Start + ProfileSize;
237 template <class IntPtrT>
239 RawInstrProfReader<IntPtrT>::readNextRecord(InstrProfRecord &Record) {
241 if (std::error_code EC = readNextHeader(ProfileEnd))
245 StringRef RawName(getName(Data->NamePtr), swap(Data->NameSize));
246 uint32_t NumCounters = swap(Data->NumCounters);
247 if (NumCounters == 0)
248 return error(instrprof_error::malformed);
249 auto RawCounts = makeArrayRef(getCounter(Data->CounterPtr), NumCounters);
252 auto *NamesStartAsCounter = reinterpret_cast<const uint64_t *>(NamesStart);
253 if (RawName.data() < NamesStart ||
254 RawName.data() + RawName.size() > DataBuffer->getBufferEnd() ||
255 RawCounts.data() < CountersStart ||
256 RawCounts.data() + RawCounts.size() > NamesStartAsCounter)
257 return error(instrprof_error::malformed);
259 // Store the data in Record, byte-swapping as necessary.
260 Record.Hash = swap(Data->FuncHash);
261 Record.Name = RawName;
262 if (ShouldSwapBytes) {
264 Counts.reserve(RawCounts.size());
265 for (uint64_t Count : RawCounts)
266 Counts.push_back(swap(Count));
267 Record.Counts = Counts;
269 Record.Counts = RawCounts;
277 template class RawInstrProfReader<uint32_t>;
278 template class RawInstrProfReader<uint64_t>;
281 InstrProfLookupTrait::hash_value_type
282 InstrProfLookupTrait::ComputeHash(StringRef K) {
283 return IndexedInstrProf::ComputeHash(HashType, K);
286 bool IndexedInstrProfReader::hasFormat(const MemoryBuffer &DataBuffer) {
287 if (DataBuffer.getBufferSize() < 8)
289 using namespace support;
291 endian::read<uint64_t, little, aligned>(DataBuffer.getBufferStart());
292 return Magic == IndexedInstrProf::Magic;
295 std::error_code IndexedInstrProfReader::readHeader() {
296 const unsigned char *Start =
297 (const unsigned char *)DataBuffer->getBufferStart();
298 const unsigned char *Cur = Start;
299 if ((const unsigned char *)DataBuffer->getBufferEnd() - Cur < 24)
300 return error(instrprof_error::truncated);
302 using namespace support;
304 // Check the magic number.
305 uint64_t Magic = endian::readNext<uint64_t, little, unaligned>(Cur);
306 if (Magic != IndexedInstrProf::Magic)
307 return error(instrprof_error::bad_magic);
310 FormatVersion = endian::readNext<uint64_t, little, unaligned>(Cur);
311 if (FormatVersion > IndexedInstrProf::Version)
312 return error(instrprof_error::unsupported_version);
314 // Read the maximal function count.
315 MaxFunctionCount = endian::readNext<uint64_t, little, unaligned>(Cur);
317 // Read the hash type and start offset.
318 IndexedInstrProf::HashT HashType = static_cast<IndexedInstrProf::HashT>(
319 endian::readNext<uint64_t, little, unaligned>(Cur));
320 if (HashType > IndexedInstrProf::HashT::Last)
321 return error(instrprof_error::unsupported_hash_type);
322 uint64_t HashOffset = endian::readNext<uint64_t, little, unaligned>(Cur);
324 // The rest of the file is an on disk hash table.
325 Index.reset(InstrProfReaderIndex::Create(Start + HashOffset, Cur, Start,
326 InstrProfLookupTrait(HashType)));
327 // Set up our iterator for readNextRecord.
328 RecordIterator = Index->data_begin();
333 std::error_code IndexedInstrProfReader::getFunctionCounts(
334 StringRef FuncName, uint64_t FuncHash, std::vector<uint64_t> &Counts) {
335 auto Iter = Index->find(FuncName);
336 if (Iter == Index->end())
337 return error(instrprof_error::unknown_function);
339 // Found it. Look for counters with the right hash.
340 ArrayRef<uint64_t> Data = (*Iter).Data;
342 for (uint64_t I = 0, E = Data.size(); I != E; I += NumCounts) {
343 // The function hash comes first.
344 uint64_t FoundHash = Data[I++];
345 // In v1, we have at least one count. Later, we have the number of counts.
347 return error(instrprof_error::malformed);
348 NumCounts = FormatVersion == 1 ? E - I : Data[I++];
349 // If we have more counts than data, this is bogus.
350 if (I + NumCounts > E)
351 return error(instrprof_error::malformed);
352 // Check for a match and fill the vector if there is one.
353 if (FoundHash == FuncHash) {
354 Counts = Data.slice(I, NumCounts);
358 return error(instrprof_error::hash_mismatch);
362 IndexedInstrProfReader::readNextRecord(InstrProfRecord &Record) {
363 // Are we out of records?
364 if (RecordIterator == Index->data_end())
365 return error(instrprof_error::eof);
367 // Record the current function name.
368 Record.Name = (*RecordIterator).Name;
370 ArrayRef<uint64_t> Data = (*RecordIterator).Data;
371 // Valid data starts with a hash and either a count or the number of counts.
372 if (CurrentOffset + 1 > Data.size())
373 return error(instrprof_error::malformed);
374 // First we have a function hash.
375 Record.Hash = Data[CurrentOffset++];
376 // In version 1 we knew the number of counters implicitly, but in newer
377 // versions we store the number of counters next.
379 FormatVersion == 1 ? Data.size() - CurrentOffset : Data[CurrentOffset++];
380 if (CurrentOffset + NumCounts > Data.size())
381 return error(instrprof_error::malformed);
382 // And finally the counts themselves.
383 Record.Counts = Data.slice(CurrentOffset, NumCounts);
385 // If we've exhausted this function's data, increment the record.
386 CurrentOffset += NumCounts;
387 if (CurrentOffset == Data.size()) {