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