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