InstrProfWriter.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. //=-- InstrProfWriter.cpp - Instrumented profiling writer -------------------=//
  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 writing profiling data for clang's
  11. // instrumentation based PGO and coverage.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "llvm/ProfileData/InstrProfWriter.h"
  15. #include "InstrProfIndexed.h"
  16. #include "llvm/ADT/StringExtras.h"
  17. #include "llvm/Support/EndianStream.h"
  18. #include "llvm/Support/OnDiskHashTable.h"
  19. using namespace llvm;
  20. namespace {
  21. class InstrProfRecordTrait {
  22. public:
  23. typedef StringRef key_type;
  24. typedef StringRef key_type_ref;
  25. typedef const InstrProfWriter::CounterData *const data_type;
  26. typedef const InstrProfWriter::CounterData *const data_type_ref;
  27. typedef uint64_t hash_value_type;
  28. typedef uint64_t offset_type;
  29. static hash_value_type ComputeHash(key_type_ref K) {
  30. return IndexedInstrProf::ComputeHash(IndexedInstrProf::HashType, K);
  31. }
  32. static std::pair<offset_type, offset_type>
  33. EmitKeyDataLength(raw_ostream &Out, key_type_ref K, data_type_ref V) {
  34. using namespace llvm::support;
  35. endian::Writer<little> LE(Out);
  36. offset_type N = K.size();
  37. LE.write<offset_type>(N);
  38. offset_type M = 0;
  39. for (const auto &Counts : *V)
  40. M += (2 + Counts.second.size()) * sizeof(uint64_t);
  41. LE.write<offset_type>(M);
  42. return std::make_pair(N, M);
  43. }
  44. static void EmitKey(raw_ostream &Out, key_type_ref K, offset_type N){
  45. Out.write(K.data(), N);
  46. }
  47. static void EmitData(raw_ostream &Out, key_type_ref, data_type_ref V,
  48. offset_type) {
  49. using namespace llvm::support;
  50. endian::Writer<little> LE(Out);
  51. for (const auto &Counts : *V) {
  52. LE.write<uint64_t>(Counts.first);
  53. LE.write<uint64_t>(Counts.second.size());
  54. for (uint64_t I : Counts.second)
  55. LE.write<uint64_t>(I);
  56. }
  57. }
  58. };
  59. }
  60. std::error_code
  61. InstrProfWriter::addFunctionCounts(StringRef FunctionName,
  62. uint64_t FunctionHash,
  63. ArrayRef<uint64_t> Counters) {
  64. auto &CounterData = FunctionData[FunctionName];
  65. auto Where = CounterData.find(FunctionHash);
  66. if (Where == CounterData.end()) {
  67. // We've never seen a function with this name and hash, add it.
  68. CounterData[FunctionHash] = Counters;
  69. // We keep track of the max function count as we go for simplicity.
  70. if (Counters[0] > MaxFunctionCount)
  71. MaxFunctionCount = Counters[0];
  72. return instrprof_error::success;
  73. }
  74. // We're updating a function we've seen before.
  75. auto &FoundCounters = Where->second;
  76. // If the number of counters doesn't match we either have bad data or a hash
  77. // collision.
  78. if (FoundCounters.size() != Counters.size())
  79. return instrprof_error::count_mismatch;
  80. for (size_t I = 0, E = Counters.size(); I < E; ++I) {
  81. if (FoundCounters[I] + Counters[I] < FoundCounters[I])
  82. return instrprof_error::counter_overflow;
  83. FoundCounters[I] += Counters[I];
  84. }
  85. // We keep track of the max function count as we go for simplicity.
  86. if (FoundCounters[0] > MaxFunctionCount)
  87. MaxFunctionCount = FoundCounters[0];
  88. return instrprof_error::success;
  89. }
  90. std::pair<uint64_t, uint64_t> InstrProfWriter::writeImpl(raw_ostream &OS) {
  91. OnDiskChainedHashTableGenerator<InstrProfRecordTrait> Generator;
  92. // Populate the hash table generator.
  93. for (const auto &I : FunctionData)
  94. Generator.insert(I.getKey(), &I.getValue());
  95. using namespace llvm::support;
  96. endian::Writer<little> LE(OS);
  97. // Write the header.
  98. LE.write<uint64_t>(IndexedInstrProf::Magic);
  99. LE.write<uint64_t>(IndexedInstrProf::Version);
  100. LE.write<uint64_t>(MaxFunctionCount);
  101. LE.write<uint64_t>(static_cast<uint64_t>(IndexedInstrProf::HashType));
  102. // Save a space to write the hash table start location.
  103. uint64_t HashTableStartLoc = OS.tell();
  104. LE.write<uint64_t>(0);
  105. // Write the hash table.
  106. uint64_t HashTableStart = Generator.Emit(OS);
  107. return std::make_pair(HashTableStartLoc, HashTableStart);
  108. }
  109. void InstrProfWriter::write(raw_fd_ostream &OS) {
  110. // Write the hash table.
  111. auto TableStart = writeImpl(OS);
  112. // Go back and fill in the hash table start.
  113. using namespace support;
  114. OS.seek(TableStart.first);
  115. endian::Writer<little>(OS).write<uint64_t>(TableStart.second);
  116. }
  117. std::unique_ptr<MemoryBuffer> InstrProfWriter::writeBuffer() {
  118. std::string Data;
  119. llvm::raw_string_ostream OS(Data);
  120. // Write the hash table.
  121. auto TableStart = writeImpl(OS);
  122. OS.flush();
  123. // Go back and fill in the hash table start.
  124. using namespace support;
  125. uint64_t Bytes = endian::byte_swap<uint64_t, little>(TableStart.second);
  126. Data.replace(TableStart.first, sizeof(uint64_t), (const char *)&Bytes,
  127. sizeof(uint64_t));
  128. // Return this in an aligned memory buffer.
  129. return MemoryBuffer::getMemBufferCopy(Data);
  130. }