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" |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/StringExtras.h" |
| 17 | #include "llvm/Support/EndianStream.h" |
| 18 | #include "llvm/Support/OnDiskHashTable.h" |
| 19 | |
Justin Bogner | b9bd7f8 | 2014-03-21 17:46:22 +0000 | [diff] [blame] | 20 | using namespace llvm; |
| 21 | |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 22 | namespace { |
| 23 | class InstrProfRecordTrait { |
| 24 | public: |
| 25 | typedef StringRef key_type; |
| 26 | typedef StringRef key_type_ref; |
| 27 | |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 28 | typedef const InstrProfWriter::ProfilingData *const data_type; |
| 29 | typedef const InstrProfWriter::ProfilingData *const data_type_ref; |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 30 | |
| 31 | typedef uint64_t hash_value_type; |
| 32 | typedef uint64_t offset_type; |
| 33 | |
| 34 | static hash_value_type ComputeHash(key_type_ref K) { |
| 35 | return IndexedInstrProf::ComputeHash(IndexedInstrProf::HashType, K); |
| 36 | } |
| 37 | |
| 38 | static std::pair<offset_type, offset_type> |
| 39 | EmitKeyDataLength(raw_ostream &Out, key_type_ref K, data_type_ref V) { |
| 40 | using namespace llvm::support; |
| 41 | endian::Writer<little> LE(Out); |
| 42 | |
Justin Bogner | e808171 | 2014-04-19 00:33:15 +0000 | [diff] [blame] | 43 | offset_type N = K.size(); |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 44 | LE.write<offset_type>(N); |
| 45 | |
Justin Bogner | 821d747 | 2014-08-01 22:50:07 +0000 | [diff] [blame] | 46 | offset_type M = 0; |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 47 | for (const auto &ProfileData : *V) { |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 48 | const InstrProfRecord &ProfRecord = ProfileData.second; |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 49 | M += sizeof(uint64_t); // The function hash |
| 50 | M += sizeof(uint64_t); // The size of the Counts vector |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 51 | M += ProfRecord.Counts.size() * sizeof(uint64_t); |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 52 | |
| 53 | // Value data |
| 54 | M += sizeof(uint64_t); // Number of value kinds with value sites. |
| 55 | for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind) { |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 56 | uint32_t NumValueSites = ProfRecord.getNumValueSites(Kind); |
Xinliang David Li | 6aa216c | 2015-11-06 07:54:21 +0000 | [diff] [blame^] | 57 | if (NumValueSites == 0) |
| 58 | continue; |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 59 | M += sizeof(uint64_t); // Value kind |
| 60 | M += sizeof(uint64_t); // The number of value sites for given value kind |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 61 | for (uint32_t I = 0; I < NumValueSites; I++) { |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 62 | M += sizeof(uint64_t); // Number of value data pairs at a value site |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 63 | uint64_t NumValueDataForSite = |
| 64 | ProfRecord.getNumValueDataForSite(Kind, I); |
Xinliang David Li | 6aa216c | 2015-11-06 07:54:21 +0000 | [diff] [blame^] | 65 | M += 2 * sizeof(uint64_t) * NumValueDataForSite; // Value data pairs |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | } |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 69 | LE.write<offset_type>(M); |
| 70 | |
| 71 | return std::make_pair(N, M); |
| 72 | } |
| 73 | |
Justin Bogner | e808171 | 2014-04-19 00:33:15 +0000 | [diff] [blame] | 74 | 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] | 75 | Out.write(K.data(), N); |
| 76 | } |
| 77 | |
| 78 | 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] | 79 | offset_type) { |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 80 | using namespace llvm::support; |
| 81 | endian::Writer<little> LE(Out); |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 82 | for (const auto &ProfileData : *V) { |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 83 | const InstrProfRecord &ProfRecord = ProfileData.second; |
| 84 | |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 85 | LE.write<uint64_t>(ProfileData.first); // Function hash |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 86 | LE.write<uint64_t>(ProfRecord.Counts.size()); |
Xinliang David Li | 6aa216c | 2015-11-06 07:54:21 +0000 | [diff] [blame^] | 87 | for (uint64_t I : ProfRecord.Counts) |
| 88 | LE.write<uint64_t>(I); |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 89 | |
| 90 | // Compute the number of value kinds with value sites. |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 91 | uint64_t NumValueKinds = ProfRecord.getNumValueKinds(); |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 92 | LE.write<uint64_t>(NumValueKinds); |
| 93 | |
| 94 | // Write value data |
| 95 | for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind) { |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 96 | uint32_t NumValueSites = ProfRecord.getNumValueSites(Kind); |
Xinliang David Li | 6aa216c | 2015-11-06 07:54:21 +0000 | [diff] [blame^] | 97 | if (NumValueSites == 0) |
| 98 | continue; |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 99 | LE.write<uint64_t>(Kind); // Write value kind |
| 100 | // Write number of value sites for current value kind |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 101 | LE.write<uint64_t>(NumValueSites); |
| 102 | |
| 103 | for (uint32_t I = 0; I < NumValueSites; I++) { |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 104 | // Write number of value data pairs at this value site |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 105 | uint64_t NumValueDataForSite = |
| 106 | ProfRecord.getNumValueDataForSite(Kind, I); |
| 107 | LE.write<uint64_t>(NumValueDataForSite); |
| 108 | std::unique_ptr<InstrProfValueData[]> VD = |
| 109 | ProfRecord.getValueForSite(Kind, I); |
| 110 | |
| 111 | for (uint32_t V = 0; V < NumValueDataForSite; V++) { |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 112 | if (Kind == IPVK_IndirectCallTarget) |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 113 | LE.write<uint64_t>(ComputeHash((const char *)VD[V].Value)); |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 114 | else |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 115 | LE.write<uint64_t>(VD[V].Value); |
| 116 | LE.write<uint64_t>(VD[V].Count); |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | } |
Justin Bogner | 821d747 | 2014-08-01 22:50:07 +0000 | [diff] [blame] | 120 | } |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 121 | } |
| 122 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 123 | } |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 124 | |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 125 | static std::error_code combineInstrProfRecords(InstrProfRecord &Dest, |
| 126 | InstrProfRecord &Source, |
| 127 | uint64_t &MaxFunctionCount) { |
| 128 | // If the number of counters doesn't match we either have bad data |
| 129 | // or a hash collision. |
| 130 | if (Dest.Counts.size() != Source.Counts.size()) |
| 131 | return instrprof_error::count_mismatch; |
Justin Bogner | 821d747 | 2014-08-01 22:50:07 +0000 | [diff] [blame] | 132 | |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 133 | for (size_t I = 0, E = Source.Counts.size(); I < E; ++I) { |
| 134 | if (Dest.Counts[I] + Source.Counts[I] < Dest.Counts[I]) |
| 135 | return instrprof_error::counter_overflow; |
| 136 | Dest.Counts[I] += Source.Counts[I]; |
| 137 | } |
| 138 | |
| 139 | for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind) { |
Xinliang David Li | 6aa216c | 2015-11-06 07:54:21 +0000 | [diff] [blame^] | 140 | if (std::error_code EC = Dest.mergeValueProfData(Kind, Source)) |
| 141 | return EC; |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | // We keep track of the max function count as we go for simplicity. |
| 145 | if (Dest.Counts[0] > MaxFunctionCount) |
| 146 | MaxFunctionCount = Dest.Counts[0]; |
| 147 | |
| 148 | return instrprof_error::success; |
| 149 | } |
| 150 | |
| 151 | void InstrProfWriter::updateStringTableReferences(InstrProfRecord &I) { |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 152 | I.updateStrings(&StringTable); |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | std::error_code InstrProfWriter::addRecord(InstrProfRecord &&I) { |
| 156 | updateStringTableReferences(I); |
| 157 | auto &ProfileDataMap = FunctionData[I.Name]; |
| 158 | |
| 159 | auto Where = ProfileDataMap.find(I.Hash); |
| 160 | if (Where == ProfileDataMap.end()) { |
Justin Bogner | 821d747 | 2014-08-01 22:50:07 +0000 | [diff] [blame] | 161 | // We've never seen a function with this name and hash, add it. |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 162 | ProfileDataMap[I.Hash] = I; |
| 163 | |
Justin Bogner | 821d747 | 2014-08-01 22:50:07 +0000 | [diff] [blame] | 164 | // We keep track of the max function count as we go for simplicity. |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 165 | if (I.Counts[0] > MaxFunctionCount) |
| 166 | MaxFunctionCount = I.Counts[0]; |
Justin Bogner | a2bfd66 | 2014-04-19 23:42:50 +0000 | [diff] [blame] | 167 | return instrprof_error::success; |
Justin Bogner | b9bd7f8 | 2014-03-21 17:46:22 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Justin Bogner | 821d747 | 2014-08-01 22:50:07 +0000 | [diff] [blame] | 170 | // We're updating a function we've seen before. |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 171 | return combineInstrProfRecords(Where->second, I, MaxFunctionCount); |
Justin Bogner | b9bd7f8 | 2014-03-21 17:46:22 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Justin Bogner | 2b6c537 | 2015-02-18 01:58:17 +0000 | [diff] [blame] | 174 | std::pair<uint64_t, uint64_t> InstrProfWriter::writeImpl(raw_ostream &OS) { |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 175 | OnDiskChainedHashTableGenerator<InstrProfRecordTrait> Generator; |
Justin Bogner | b9bd7f8 | 2014-03-21 17:46:22 +0000 | [diff] [blame] | 176 | |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 177 | // Populate the hash table generator. |
Justin Bogner | 821d747 | 2014-08-01 22:50:07 +0000 | [diff] [blame] | 178 | for (const auto &I : FunctionData) |
Justin Bogner | fa5b013 | 2014-04-23 18:50:16 +0000 | [diff] [blame] | 179 | Generator.insert(I.getKey(), &I.getValue()); |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 180 | |
| 181 | using namespace llvm::support; |
| 182 | endian::Writer<little> LE(OS); |
| 183 | |
| 184 | // Write the header. |
Xinliang David Li | dab183ed | 2015-10-18 01:02:29 +0000 | [diff] [blame] | 185 | IndexedInstrProf::Header Header; |
| 186 | Header.Magic = IndexedInstrProf::Magic; |
| 187 | Header.Version = IndexedInstrProf::Version; |
| 188 | Header.MaxFunctionCount = MaxFunctionCount; |
| 189 | Header.HashType = static_cast<uint64_t>(IndexedInstrProf::HashType); |
| 190 | Header.HashOffset = 0; |
| 191 | int N = sizeof(IndexedInstrProf::Header) / sizeof(uint64_t); |
| 192 | |
| 193 | // Only write out all the fields execpt 'HashOffset'. We need |
| 194 | // to remember the offset of that field to allow back patching |
| 195 | // later. |
| 196 | for (int I = 0; I < N - 1; I++) |
| 197 | LE.write<uint64_t>(reinterpret_cast<uint64_t *>(&Header)[I]); |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 198 | |
| 199 | // Save a space to write the hash table start location. |
| 200 | uint64_t HashTableStartLoc = OS.tell(); |
Xinliang David Li | dab183ed | 2015-10-18 01:02:29 +0000 | [diff] [blame] | 201 | // Reserve the space for HashOffset field. |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 202 | LE.write<uint64_t>(0); |
| 203 | // Write the hash table. |
| 204 | uint64_t HashTableStart = Generator.Emit(OS); |
| 205 | |
Justin Bogner | 2b6c537 | 2015-02-18 01:58:17 +0000 | [diff] [blame] | 206 | return std::make_pair(HashTableStartLoc, HashTableStart); |
| 207 | } |
| 208 | |
| 209 | void InstrProfWriter::write(raw_fd_ostream &OS) { |
| 210 | // Write the hash table. |
| 211 | auto TableStart = writeImpl(OS); |
| 212 | |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 213 | // Go back and fill in the hash table start. |
Justin Bogner | 2b6c537 | 2015-02-18 01:58:17 +0000 | [diff] [blame] | 214 | using namespace support; |
| 215 | OS.seek(TableStart.first); |
Xinliang David Li | dab183ed | 2015-10-18 01:02:29 +0000 | [diff] [blame] | 216 | // Now patch the HashOffset field previously reserved. |
Justin Bogner | 2b6c537 | 2015-02-18 01:58:17 +0000 | [diff] [blame] | 217 | endian::Writer<little>(OS).write<uint64_t>(TableStart.second); |
| 218 | } |
| 219 | |
| 220 | std::unique_ptr<MemoryBuffer> InstrProfWriter::writeBuffer() { |
| 221 | std::string Data; |
| 222 | llvm::raw_string_ostream OS(Data); |
| 223 | // Write the hash table. |
| 224 | auto TableStart = writeImpl(OS); |
| 225 | OS.flush(); |
| 226 | |
| 227 | // Go back and fill in the hash table start. |
| 228 | using namespace support; |
| 229 | uint64_t Bytes = endian::byte_swap<uint64_t, little>(TableStart.second); |
| 230 | Data.replace(TableStart.first, sizeof(uint64_t), (const char *)&Bytes, |
| 231 | sizeof(uint64_t)); |
| 232 | |
| 233 | // Return this in an aligned memory buffer. |
| 234 | return MemoryBuffer::getMemBufferCopy(Data); |
Justin Bogner | b9bd7f8 | 2014-03-21 17:46:22 +0000 | [diff] [blame] | 235 | } |