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 15 #include "llvm/ProfileData/InstrProfReader.h" 16 #include "llvm/ADT/STLExtras.h" 17 #include <cassert> 18 19 using namespace llvm; 20 21 static ErrorOr<std::unique_ptr<MemoryBuffer>> 22 setupMemoryBuffer(std::string Path) { 23 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr = 24 MemoryBuffer::getFileOrSTDIN(Path); 25 if (std::error_code EC = BufferOrErr.getError()) 26 return EC; 27 return std::move(BufferOrErr.get()); 28 } 29 30 static std::error_code initializeReader(InstrProfReader &Reader) { 31 return Reader.readHeader(); 32 } 33 34 ErrorOr<std::unique_ptr<InstrProfReader>> 35 InstrProfReader::create(std::string Path) { 36 // Set up the buffer to read. 37 auto BufferOrError = setupMemoryBuffer(Path); 38 if (std::error_code EC = BufferOrError.getError()) 39 return EC; 40 return InstrProfReader::create(std::move(BufferOrError.get())); 41 } 42 43 ErrorOr<std::unique_ptr<InstrProfReader>> 44 InstrProfReader::create(std::unique_ptr<MemoryBuffer> Buffer) { 45 // Sanity check the buffer. 46 if (Buffer->getBufferSize() > std::numeric_limits<unsigned>::max()) 47 return instrprof_error::too_large; 48 49 std::unique_ptr<InstrProfReader> Result; 50 // Create the reader. 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))); 57 else 58 Result.reset(new TextInstrProfReader(std::move(Buffer))); 59 60 // Initialize the reader and return the result. 61 if (std::error_code EC = initializeReader(*Result)) 62 return EC; 63 64 return std::move(Result); 65 } 66 67 ErrorOr<std::unique_ptr<IndexedInstrProfReader>> 68 IndexedInstrProfReader::create(std::string Path) { 69 // Set up the buffer to read. 70 auto BufferOrError = setupMemoryBuffer(Path); 71 if (std::error_code EC = BufferOrError.getError()) 72 return EC; 73 return IndexedInstrProfReader::create(std::move(BufferOrError.get())); 74 } 75 76 77 ErrorOr<std::unique_ptr<IndexedInstrProfReader>> 78 IndexedInstrProfReader::create(std::unique_ptr<MemoryBuffer> Buffer) { 79 // Sanity check the buffer. 80 if (Buffer->getBufferSize() > std::numeric_limits<unsigned>::max()) 81 return instrprof_error::too_large; 82 83 // Create the reader. 84 if (!IndexedInstrProfReader::hasFormat(*Buffer)) 85 return instrprof_error::bad_magic; 86 auto Result = llvm::make_unique<IndexedInstrProfReader>(std::move(Buffer)); 87 88 // Initialize the reader and return the result. 89 if (std::error_code EC = initializeReader(*Result)) 90 return EC; 91 92 return std::move(Result); 93 } 94 95 void InstrProfIterator::Increment() { 96 if (Reader->readNextRecord(Record)) 97 *this = InstrProfIterator(); 98 } 99 100 std::error_code TextInstrProfReader::readNextRecord(InstrProfRecord &Record) { 101 // Skip empty lines and comments. 102 while (!Line.is_at_end() && (Line->empty() || Line->startswith("#"))) 103 ++Line; 104 // If we hit EOF while looking for a name, we're done. 105 if (Line.is_at_end()) 106 return error(instrprof_error::eof); 107 108 // Read the function name. 109 Record.Name = *Line++; 110 111 // Read the function hash. 112 if (Line.is_at_end()) 113 return error(instrprof_error::truncated); 114 if ((Line++)->getAsInteger(0, Record.Hash)) 115 return error(instrprof_error::malformed); 116 117 // Read the number of counters. 118 uint64_t NumCounters; 119 if (Line.is_at_end()) 120 return error(instrprof_error::truncated); 121 if ((Line++)->getAsInteger(10, NumCounters)) 122 return error(instrprof_error::malformed); 123 if (NumCounters == 0) 124 return error(instrprof_error::malformed); 125 126 // Read each counter and fill our internal storage with the values. 127 Record.Counts.clear(); 128 Record.Counts.reserve(NumCounters); 129 for (uint64_t I = 0; I < NumCounters; ++I) { 130 if (Line.is_at_end()) 131 return error(instrprof_error::truncated); 132 uint64_t Count; 133 if ((Line++)->getAsInteger(10, Count)) 134 return error(instrprof_error::malformed); 135 Record.Counts.push_back(Count); 136 } 137 138 return success(); 139 } 140 141 template <class IntPtrT> 142 bool RawInstrProfReader<IntPtrT>::hasFormat(const MemoryBuffer &DataBuffer) { 143 if (DataBuffer.getBufferSize() < sizeof(uint64_t)) 144 return false; 145 uint64_t Magic = 146 *reinterpret_cast<const uint64_t *>(DataBuffer.getBufferStart()); 147 return RawInstrProf::getMagic<IntPtrT>() == Magic || 148 sys::getSwappedBytes(RawInstrProf::getMagic<IntPtrT>()) == Magic; 149 } 150 151 template <class IntPtrT> 152 std::error_code RawInstrProfReader<IntPtrT>::readHeader() { 153 if (!hasFormat(*DataBuffer)) 154 return error(instrprof_error::bad_magic); 155 if (DataBuffer->getBufferSize() < sizeof(RawInstrProf::Header)) 156 return error(instrprof_error::bad_header); 157 auto *Header = reinterpret_cast<const RawInstrProf::Header *>( 158 DataBuffer->getBufferStart()); 159 ShouldSwapBytes = Header->Magic != RawInstrProf::getMagic<IntPtrT>(); 160 return readHeader(*Header); 161 } 162 163 template <class IntPtrT> 164 std::error_code 165 RawInstrProfReader<IntPtrT>::readNextHeader(const char *CurrentPos) { 166 const char *End = DataBuffer->getBufferEnd(); 167 // Skip zero padding between profiles. 168 while (CurrentPos != End && *CurrentPos == 0) 169 ++CurrentPos; 170 // If there's nothing left, we're done. 171 if (CurrentPos == End) 172 return instrprof_error::eof; 173 // If there isn't enough space for another header, this is probably just 174 // garbage at the end of the file. 175 if (CurrentPos + sizeof(RawInstrProf::Header) > End) 176 return instrprof_error::malformed; 177 // The writer ensures each profile is padded to start at an aligned address. 178 if (reinterpret_cast<size_t>(CurrentPos) % alignOf<uint64_t>()) 179 return instrprof_error::malformed; 180 // The magic should have the same byte order as in the previous header. 181 uint64_t Magic = *reinterpret_cast<const uint64_t *>(CurrentPos); 182 if (Magic != swap(RawInstrProf::getMagic<IntPtrT>())) 183 return instrprof_error::bad_magic; 184 185 // There's another profile to read, so we need to process the header. 186 auto *Header = reinterpret_cast<const RawInstrProf::Header *>(CurrentPos); 187 return readHeader(*Header); 188 } 189 190 template <class IntPtrT> 191 std::error_code RawInstrProfReader<IntPtrT>::readHeader( 192 const RawInstrProf::Header &Header) { 193 if (swap(Header.Version) != RawInstrProf::Version) 194 return error(instrprof_error::unsupported_version); 195 196 CountersDelta = swap(Header.CountersDelta); 197 NamesDelta = swap(Header.NamesDelta); 198 auto DataSize = swap(Header.DataSize); 199 auto CountersSize = swap(Header.CountersSize); 200 auto NamesSize = swap(Header.NamesSize); 201 202 ptrdiff_t DataOffset = sizeof(RawInstrProf::Header); 203 ptrdiff_t CountersOffset = 204 DataOffset + sizeof(RawInstrProf::ProfileData<IntPtrT>) * DataSize; 205 ptrdiff_t NamesOffset = CountersOffset + sizeof(uint64_t) * CountersSize; 206 size_t ProfileSize = NamesOffset + sizeof(char) * NamesSize; 207 208 auto *Start = reinterpret_cast<const char *>(&Header); 209 if (Start + ProfileSize > DataBuffer->getBufferEnd()) 210 return error(instrprof_error::bad_header); 211 212 Data = reinterpret_cast<const RawInstrProf::ProfileData<IntPtrT> *>( 213 Start + DataOffset); 214 DataEnd = Data + DataSize; 215 CountersStart = reinterpret_cast<const uint64_t *>(Start + CountersOffset); 216 NamesStart = Start + NamesOffset; 217 ProfileEnd = Start + ProfileSize; 218 219 return success(); 220 } 221 222 template <class IntPtrT> 223 std::error_code RawInstrProfReader<IntPtrT>::readName(InstrProfRecord &Record) { 224 Record.Name = StringRef(getName(Data->NamePtr), swap(Data->NameSize)); 225 if (Record.Name.data() < NamesStart || 226 Record.Name.data() + Record.Name.size() > DataBuffer->getBufferEnd()) 227 return error(instrprof_error::malformed); 228 229 return success(); 230 } 231 232 template <class IntPtrT> 233 std::error_code RawInstrProfReader<IntPtrT>::readFuncHash( 234 InstrProfRecord &Record) { 235 Record.Hash = swap(Data->FuncHash); 236 return success(); 237 } 238 239 template <class IntPtrT> 240 std::error_code RawInstrProfReader<IntPtrT>::readRawCounts( 241 InstrProfRecord &Record) { 242 uint32_t NumCounters = swap(Data->NumCounters); 243 IntPtrT CounterPtr = Data->CounterPtr; 244 if (NumCounters == 0) 245 return error(instrprof_error::malformed); 246 247 auto RawCounts = makeArrayRef(getCounter(CounterPtr), NumCounters); 248 auto *NamesStartAsCounter = reinterpret_cast<const uint64_t *>(NamesStart); 249 250 // Check bounds. 251 if (RawCounts.data() < CountersStart || 252 RawCounts.data() + RawCounts.size() > NamesStartAsCounter) 253 return error(instrprof_error::malformed); 254 255 if (ShouldSwapBytes) { 256 Record.Counts.clear(); 257 Record.Counts.reserve(RawCounts.size()); 258 for (uint64_t Count : RawCounts) 259 Record.Counts.push_back(swap(Count)); 260 } else 261 Record.Counts = RawCounts; 262 263 return success(); 264 } 265 266 template <class IntPtrT> 267 std::error_code RawInstrProfReader<IntPtrT>::readNextRecord( 268 InstrProfRecord &Record) { 269 if (atEnd()) 270 if (std::error_code EC = readNextHeader(ProfileEnd)) return EC; 271 272 // Read name ad set it in Record. 273 if (std::error_code EC = readName(Record)) return EC; 274 275 // Read FuncHash and set it in Record. 276 if (std::error_code EC = readFuncHash(Record)) return EC; 277 278 // Read raw counts and set Record. 279 if (std::error_code EC = readRawCounts(Record)) return EC; 280 281 // Iterate. 282 advanceData(); 283 return success(); 284 } 285 286 namespace llvm { 287 template class RawInstrProfReader<uint32_t>; 288 template class RawInstrProfReader<uint64_t>; 289 } 290 291 InstrProfLookupTrait::hash_value_type 292 InstrProfLookupTrait::ComputeHash(StringRef K) { 293 return IndexedInstrProf::ComputeHash(HashType, K); 294 } 295 296 typedef InstrProfLookupTrait::data_type data_type; 297 typedef InstrProfLookupTrait::offset_type offset_type; 298 299 bool InstrProfLookupTrait::ReadValueProfilingData( 300 const unsigned char *&D, const unsigned char *const End) { 301 302 using namespace support; 303 // Read number of value kinds with value sites. 304 if (D + sizeof(uint64_t) > End) 305 return false; 306 uint64_t ValueKindCount = endian::readNext<uint64_t, little, unaligned>(D); 307 308 for (uint32_t Kind = 0; Kind < ValueKindCount; ++Kind) { 309 310 // Read value kind and number of value sites for kind. 311 if (D + 2 * sizeof(uint64_t) > End) 312 return false; 313 uint64_t ValueKind = endian::readNext<uint64_t, little, unaligned>(D); 314 uint64_t ValueSiteCount = endian::readNext<uint64_t, little, unaligned>(D); 315 316 std::vector<InstrProfValueSiteRecord> &ValueSites = 317 DataBuffer.back().getValueSitesForKind(ValueKind); 318 ValueSites.reserve(ValueSiteCount); 319 for (uint64_t VSite = 0; VSite < ValueSiteCount; ++VSite) { 320 // Read number of value data pairs at value site. 321 if (D + sizeof(uint64_t) > End) 322 return false; 323 uint64_t ValueDataCount = 324 endian::readNext<uint64_t, little, unaligned>(D); 325 326 // Check if there are as many ValueDataPairs as ValueDataCount in memory. 327 if (D + (ValueDataCount << 1) * sizeof(uint64_t) > End) 328 return false; 329 330 InstrProfValueSiteRecord VSiteRecord; 331 for (uint64_t VCount = 0; VCount < ValueDataCount; ++VCount) { 332 uint64_t Value = endian::readNext<uint64_t, little, unaligned>(D); 333 uint64_t NumTaken = endian::readNext<uint64_t, little, unaligned>(D); 334 switch (ValueKind) { 335 case IPVK_IndirectCallTarget: { 336 auto Result = 337 std::lower_bound(HashKeys.begin(), HashKeys.end(), Value, 338 [](const std::pair<uint64_t, const char *> &LHS, 339 uint64_t RHS) { return LHS.first < RHS; }); 340 assert(Result != HashKeys.end() && 341 "Hash does not match any known keys\n"); 342 Value = (uint64_t)Result->second; 343 break; 344 } 345 } 346 VSiteRecord.ValueData.push_back(std::make_pair(Value, NumTaken)); 347 } 348 ValueSites.push_back(std::move(VSiteRecord)); 349 } 350 } 351 return true; 352 } 353 354 data_type InstrProfLookupTrait::ReadData(StringRef K, const unsigned char *D, 355 offset_type N) { 356 // Check if the data is corrupt. If so, don't try to read it. 357 if (N % sizeof(uint64_t)) 358 return data_type(); 359 360 DataBuffer.clear(); 361 std::vector<uint64_t> CounterBuffer; 362 363 using namespace support; 364 const unsigned char *End = D + N; 365 while (D < End) { 366 // Read hash. 367 if (D + sizeof(uint64_t) >= End) 368 return data_type(); 369 uint64_t Hash = endian::readNext<uint64_t, little, unaligned>(D); 370 371 // Initialize number of counters for FormatVersion == 1. 372 uint64_t CountsSize = N / sizeof(uint64_t) - 1; 373 // If format version is different then read the number of counters. 374 if (FormatVersion != 1) { 375 if (D + sizeof(uint64_t) > End) 376 return data_type(); 377 CountsSize = endian::readNext<uint64_t, little, unaligned>(D); 378 } 379 // Read counter values. 380 if (D + CountsSize * sizeof(uint64_t) > End) 381 return data_type(); 382 383 CounterBuffer.clear(); 384 CounterBuffer.reserve(CountsSize); 385 for (uint64_t J = 0; J < CountsSize; ++J) 386 CounterBuffer.push_back(endian::readNext<uint64_t, little, unaligned>(D)); 387 388 DataBuffer.push_back(InstrProfRecord(K, Hash, std::move(CounterBuffer))); 389 390 // Read value profiling data. 391 if (FormatVersion > 2 && !ReadValueProfilingData(D, End)) { 392 DataBuffer.clear(); 393 return data_type(); 394 } 395 } 396 return DataBuffer; 397 } 398 399 std::error_code InstrProfReaderIndex::getRecords( 400 StringRef FuncName, ArrayRef<InstrProfRecord> &Data) { 401 auto Iter = Index->find(FuncName); 402 if (Iter == Index->end()) return instrprof_error::unknown_function; 403 404 Data = (*Iter); 405 if (Data.empty()) return instrprof_error::malformed; 406 407 return instrprof_error::success; 408 } 409 410 std::error_code InstrProfReaderIndex::getRecords( 411 ArrayRef<InstrProfRecord> &Data) { 412 if (atEnd()) return instrprof_error::eof; 413 414 Data = *RecordIterator; 415 416 if (Data.empty()) return instrprof_error::malformed; 417 418 return instrprof_error::success; 419 } 420 421 void InstrProfReaderIndex::Init(const unsigned char *Buckets, 422 const unsigned char *const Payload, 423 const unsigned char *const Base, 424 IndexedInstrProf::HashT HashType, 425 uint64_t Version) { 426 FormatVersion = Version; 427 Index.reset(IndexType::Create(Buckets, Payload, Base, 428 InstrProfLookupTrait(HashType, Version))); 429 // Form the map of hash values to const char* keys in profiling data. 430 std::vector<std::pair<uint64_t, const char *>> HashKeys; 431 for (auto Key : Index->keys()) { 432 const char *KeyTableRef = StringTable.insertString(Key); 433 HashKeys.push_back(std::make_pair(ComputeHash(HashType, Key), KeyTableRef)); 434 } 435 std::sort(HashKeys.begin(), HashKeys.end(), less_first()); 436 HashKeys.erase(std::unique(HashKeys.begin(), HashKeys.end()), HashKeys.end()); 437 // Set the hash key map for the InstrLookupTrait 438 Index->getInfoObj().setHashKeys(std::move(HashKeys)); 439 RecordIterator = Index->data_begin(); 440 } 441 442 bool IndexedInstrProfReader::hasFormat(const MemoryBuffer &DataBuffer) { 443 if (DataBuffer.getBufferSize() < 8) return false; 444 using namespace support; 445 uint64_t Magic = 446 endian::read<uint64_t, little, aligned>(DataBuffer.getBufferStart()); 447 // Verify that it's magical. 448 return Magic == IndexedInstrProf::Magic; 449 } 450 451 std::error_code IndexedInstrProfReader::readHeader() { 452 const unsigned char *Start = 453 (const unsigned char *)DataBuffer->getBufferStart(); 454 const unsigned char *Cur = Start; 455 if ((const unsigned char *)DataBuffer->getBufferEnd() - Cur < 24) 456 return error(instrprof_error::truncated); 457 458 using namespace support; 459 460 auto *Header = reinterpret_cast<const IndexedInstrProf::Header *>(Cur); 461 Cur += sizeof(IndexedInstrProf::Header); 462 463 // Check the magic number. 464 uint64_t Magic = endian::byte_swap<uint64_t, little>(Header->Magic); 465 if (Magic != IndexedInstrProf::Magic) 466 return error(instrprof_error::bad_magic); 467 468 // Read the version. 469 uint64_t FormatVersion = endian::byte_swap<uint64_t, little>(Header->Version); 470 if (FormatVersion > IndexedInstrProf::Version) 471 return error(instrprof_error::unsupported_version); 472 473 // Read the maximal function count. 474 MaxFunctionCount = 475 endian::byte_swap<uint64_t, little>(Header->MaxFunctionCount); 476 477 // Read the hash type and start offset. 478 IndexedInstrProf::HashT HashType = static_cast<IndexedInstrProf::HashT>( 479 endian::byte_swap<uint64_t, little>(Header->HashType)); 480 if (HashType > IndexedInstrProf::HashT::Last) 481 return error(instrprof_error::unsupported_hash_type); 482 483 uint64_t HashOffset = endian::byte_swap<uint64_t, little>(Header->HashOffset); 484 485 // The rest of the file is an on disk hash table. 486 Index.Init(Start + HashOffset, Cur, Start, HashType, FormatVersion); 487 488 return success(); 489 } 490 491 std::error_code IndexedInstrProfReader::getFunctionCounts( 492 StringRef FuncName, uint64_t FuncHash, std::vector<uint64_t> &Counts) { 493 ArrayRef<InstrProfRecord> Data; 494 495 std::error_code EC = Index.getRecords(FuncName, Data); 496 if (EC != instrprof_error::success) return EC; 497 498 // Found it. Look for counters with the right hash. 499 for (unsigned I = 0, E = Data.size(); I < E; ++I) { 500 // Check for a match and fill the vector if there is one. 501 if (Data[I].Hash == FuncHash) { 502 Counts = Data[I].Counts; 503 return success(); 504 } 505 } 506 return error(instrprof_error::hash_mismatch); 507 } 508 509 std::error_code IndexedInstrProfReader::readNextRecord( 510 InstrProfRecord &Record) { 511 static unsigned RecordIndex = 0; 512 513 ArrayRef<InstrProfRecord> Data; 514 515 std::error_code EC = Index.getRecords(Data); 516 if (EC != instrprof_error::success) return error(EC); 517 518 Record = Data[RecordIndex++]; 519 if (RecordIndex >= Data.size()) { 520 Index.advanceToNextKey(); 521 RecordIndex = 0; 522 } 523 return success(); 524 } 525