1 //=-- InstrProfWriter.cpp - Instrumented profiling writer -------------------=//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains support for writing profiling data for clang's
11 // instrumentation based PGO and coverage.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/ProfileData/InstrProfWriter.h"
16 #include "llvm/ADT/StringExtras.h"
17 #include "llvm/Support/EndianStream.h"
18 #include "llvm/Support/OnDiskHashTable.h"
24 static support::endianness ValueProfDataEndianness = support::little;
26 class InstrProfRecordTrait {
28 typedef StringRef key_type;
29 typedef StringRef key_type_ref;
31 typedef const InstrProfWriter::ProfilingData *const data_type;
32 typedef const InstrProfWriter::ProfilingData *const data_type_ref;
34 typedef uint64_t hash_value_type;
35 typedef uint64_t offset_type;
37 static hash_value_type ComputeHash(key_type_ref K) {
38 return IndexedInstrProf::ComputeHash(K);
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);
46 offset_type N = K.size();
47 LE.write<offset_type>(N);
50 for (const auto &ProfileData : *V) {
51 const InstrProfRecord &ProfRecord = ProfileData.second;
52 M += sizeof(uint64_t); // The function hash
53 M += sizeof(uint64_t); // The size of the Counts vector
54 M += ProfRecord.Counts.size() * sizeof(uint64_t);
57 M += ValueProfData::getSize(ProfileData.second);
59 LE.write<offset_type>(M);
61 return std::make_pair(N, M);
64 static void EmitKey(raw_ostream &Out, key_type_ref K, offset_type N){
65 Out.write(K.data(), N);
68 static void EmitData(raw_ostream &Out, key_type_ref, data_type_ref V,
70 using namespace llvm::support;
71 endian::Writer<little> LE(Out);
72 for (const auto &ProfileData : *V) {
73 const InstrProfRecord &ProfRecord = ProfileData.second;
75 LE.write<uint64_t>(ProfileData.first); // Function hash
76 LE.write<uint64_t>(ProfRecord.Counts.size());
77 for (uint64_t I : ProfRecord.Counts)
78 LE.write<uint64_t>(I);
81 std::unique_ptr<ValueProfData> VDataPtr =
82 ValueProfData::serializeFrom(ProfileData.second);
83 uint32_t S = VDataPtr->getSize();
84 VDataPtr->swapBytesFromHost(ValueProfDataEndianness);
85 Out.write((const char *)VDataPtr.get(), S);
91 // Internal interface for testing purpose only.
92 void InstrProfWriter::setValueProfDataEndianness(
93 support::endianness Endianness) {
94 ValueProfDataEndianness = Endianness;
97 void InstrProfWriter::updateStringTableReferences(InstrProfRecord &I) {
98 I.updateStrings(&StringTable);
101 std::error_code InstrProfWriter::addRecord(InstrProfRecord &&I,
103 updateStringTableReferences(I);
104 auto &ProfileDataMap = FunctionData[I.Name];
107 ProfilingData::iterator Where;
108 std::tie(Where, NewFunc) =
109 ProfileDataMap.insert(std::make_pair(I.Hash, InstrProfRecord()));
110 InstrProfRecord &Dest = Where->second;
112 instrprof_error Result;
114 // We've never seen a function with this name and hash, add it.
116 Result = instrprof_error::success;
118 for (auto &Count : Dest.Counts) {
120 Count = SaturatingMultiply(Count, Weight, &Overflowed);
121 if (Overflowed && Result == instrprof_error::success) {
122 Result = instrprof_error::counter_overflow;
127 // We're updating a function we've seen before.
128 Result = Dest.merge(I, Weight);
131 // We keep track of the max function count as we go for simplicity.
132 // Update this statistic no matter the result of the merge.
133 if (Dest.Counts[0] > MaxFunctionCount)
134 MaxFunctionCount = Dest.Counts[0];
139 std::pair<uint64_t, uint64_t> InstrProfWriter::writeImpl(raw_ostream &OS) {
140 OnDiskChainedHashTableGenerator<InstrProfRecordTrait> Generator;
142 // Populate the hash table generator.
143 for (const auto &I : FunctionData)
144 Generator.insert(I.getKey(), &I.getValue());
146 using namespace llvm::support;
147 endian::Writer<little> LE(OS);
150 IndexedInstrProf::Header Header;
151 Header.Magic = IndexedInstrProf::Magic;
152 Header.Version = IndexedInstrProf::Version;
153 Header.MaxFunctionCount = MaxFunctionCount;
154 Header.HashType = static_cast<uint64_t>(IndexedInstrProf::HashType);
155 Header.HashOffset = 0;
156 int N = sizeof(IndexedInstrProf::Header) / sizeof(uint64_t);
158 // Only write out all the fields execpt 'HashOffset'. We need
159 // to remember the offset of that field to allow back patching
161 for (int I = 0; I < N - 1; I++)
162 LE.write<uint64_t>(reinterpret_cast<uint64_t *>(&Header)[I]);
164 // Save a space to write the hash table start location.
165 uint64_t HashTableStartLoc = OS.tell();
166 // Reserve the space for HashOffset field.
167 LE.write<uint64_t>(0);
168 // Write the hash table.
169 uint64_t HashTableStart = Generator.Emit(OS);
171 return std::make_pair(HashTableStartLoc, HashTableStart);
174 void InstrProfWriter::write(raw_fd_ostream &OS) {
175 // Write the hash table.
176 auto TableStart = writeImpl(OS);
178 // Go back and fill in the hash table start.
179 using namespace support;
180 OS.seek(TableStart.first);
181 // Now patch the HashOffset field previously reserved.
182 endian::Writer<little>(OS).write<uint64_t>(TableStart.second);
185 static const char *ValueProfKindStr[] = {
186 #define VALUE_PROF_KIND(Enumerator, Value) #Enumerator,
187 #include "llvm/ProfileData/InstrProfData.inc"
190 void InstrProfWriter::writeRecordInText(const InstrProfRecord &Func,
191 raw_fd_ostream &OS) {
192 OS << Func.Name << "\n";
193 OS << "# Func Hash:\n" << Func.Hash << "\n";
194 OS << "# Num Counters:\n" << Func.Counts.size() << "\n";
195 OS << "# Counter Values:\n";
196 for (uint64_t Count : Func.Counts)
199 uint32_t NumValueKinds = Func.getNumValueKinds();
200 if (!NumValueKinds) {
205 OS << "# Num Value Kinds:\n" << Func.getNumValueKinds() << "\n";
206 for (uint32_t VK = 0; VK < IPVK_Last + 1; VK++) {
207 uint32_t NS = Func.getNumValueSites(VK);
210 OS << "# ValueKind = " << ValueProfKindStr[VK] << ":\n" << VK << "\n";
211 OS << "# NumValueSites:\n" << NS << "\n";
212 for (uint32_t S = 0; S < NS; S++) {
213 uint32_t ND = Func.getNumValueDataForSite(VK, S);
215 std::unique_ptr<InstrProfValueData[]> VD = Func.getValueForSite(VK, S);
216 for (uint32_t I = 0; I < ND; I++) {
217 if (VK == IPVK_IndirectCallTarget)
218 OS << reinterpret_cast<const char *>(VD[I].Value) << ":"
219 << VD[I].Count << "\n";
221 OS << VD[I].Value << ":" << VD[I].Count << "\n";
229 void InstrProfWriter::writeText(raw_fd_ostream &OS) {
230 for (const auto &I : FunctionData)
231 for (const auto &Func : I.getValue())
232 writeRecordInText(Func.second, OS);
235 std::unique_ptr<MemoryBuffer> InstrProfWriter::writeBuffer() {
237 llvm::raw_string_ostream OS(Data);
238 // Write the hash table.
239 auto TableStart = writeImpl(OS);
242 // Go back and fill in the hash table start.
243 using namespace support;
244 uint64_t Bytes = endian::byte_swap<uint64_t, little>(TableStart.second);
245 Data.replace(TableStart.first, sizeof(uint64_t), (const char *)&Bytes,
248 // Return this in an aligned memory buffer.
249 return MemoryBuffer::getMemBufferCopy(Data);