blob: 44ad016f1a8da1820b87ef6640ec4422321bdb78 [file] [log] [blame]
Justin Bognerb9bd7f82014-03-21 17:46:22 +00001//=-- 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 Bognerb7aa2632014-04-18 21:48:40 +000016#include "llvm/ADT/StringExtras.h"
17#include "llvm/Support/EndianStream.h"
18#include "llvm/Support/OnDiskHashTable.h"
Reid Kleckner437b1b32015-11-20 19:29:40 +000019#include <tuple>
Justin Bognerb7aa2632014-04-18 21:48:40 +000020
Justin Bognerb9bd7f82014-03-21 17:46:22 +000021using namespace llvm;
22
Justin Bognerb7aa2632014-04-18 21:48:40 +000023namespace {
Xinliang David Liee415892015-11-10 00:24:45 +000024static support::endianness ValueProfDataEndianness = support::little;
25
Justin Bognerb7aa2632014-04-18 21:48:40 +000026class InstrProfRecordTrait {
27public:
28 typedef StringRef key_type;
29 typedef StringRef key_type_ref;
30
Justin Bogner9e9a0572015-09-29 22:13:58 +000031 typedef const InstrProfWriter::ProfilingData *const data_type;
32 typedef const InstrProfWriter::ProfilingData *const data_type_ref;
Justin Bognerb7aa2632014-04-18 21:48:40 +000033
34 typedef uint64_t hash_value_type;
35 typedef uint64_t offset_type;
36
37 static hash_value_type ComputeHash(key_type_ref K) {
Xinliang David Li49ee76d2015-12-18 22:22:12 +000038 return IndexedInstrProf::ComputeHash(K);
Justin Bognerb7aa2632014-04-18 21:48:40 +000039 }
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 Bognere8081712014-04-19 00:33:15 +000046 offset_type N = K.size();
Justin Bognerb7aa2632014-04-18 21:48:40 +000047 LE.write<offset_type>(N);
48
Justin Bogner821d7472014-08-01 22:50:07 +000049 offset_type M = 0;
Justin Bogner9e9a0572015-09-29 22:13:58 +000050 for (const auto &ProfileData : *V) {
Xinliang David Li2004f002015-11-02 05:08:23 +000051 const InstrProfRecord &ProfRecord = ProfileData.second;
Justin Bogner9e9a0572015-09-29 22:13:58 +000052 M += sizeof(uint64_t); // The function hash
53 M += sizeof(uint64_t); // The size of the Counts vector
Xinliang David Li2004f002015-11-02 05:08:23 +000054 M += ProfRecord.Counts.size() * sizeof(uint64_t);
Justin Bogner9e9a0572015-09-29 22:13:58 +000055
56 // Value data
Xinliang David Li99556872015-11-17 23:00:40 +000057 M += ValueProfData::getSize(ProfileData.second);
Justin Bogner9e9a0572015-09-29 22:13:58 +000058 }
Justin Bognerb7aa2632014-04-18 21:48:40 +000059 LE.write<offset_type>(M);
60
61 return std::make_pair(N, M);
62 }
63
Justin Bognere8081712014-04-19 00:33:15 +000064 static void EmitKey(raw_ostream &Out, key_type_ref K, offset_type N){
Justin Bognerb7aa2632014-04-18 21:48:40 +000065 Out.write(K.data(), N);
66 }
67
68 static void EmitData(raw_ostream &Out, key_type_ref, data_type_ref V,
Justin Bognere8081712014-04-19 00:33:15 +000069 offset_type) {
Justin Bognerb7aa2632014-04-18 21:48:40 +000070 using namespace llvm::support;
71 endian::Writer<little> LE(Out);
Justin Bogner9e9a0572015-09-29 22:13:58 +000072 for (const auto &ProfileData : *V) {
Xinliang David Li2004f002015-11-02 05:08:23 +000073 const InstrProfRecord &ProfRecord = ProfileData.second;
74
Justin Bogner9e9a0572015-09-29 22:13:58 +000075 LE.write<uint64_t>(ProfileData.first); // Function hash
Xinliang David Li2004f002015-11-02 05:08:23 +000076 LE.write<uint64_t>(ProfRecord.Counts.size());
Xinliang David Li6aa216c2015-11-06 07:54:21 +000077 for (uint64_t I : ProfRecord.Counts)
78 LE.write<uint64_t>(I);
Justin Bogner9e9a0572015-09-29 22:13:58 +000079
Justin Bogner9e9a0572015-09-29 22:13:58 +000080 // Write value data
Xinliang David Li99556872015-11-17 23:00:40 +000081 std::unique_ptr<ValueProfData> VDataPtr =
82 ValueProfData::serializeFrom(ProfileData.second);
Xinliang David Liee415892015-11-10 00:24:45 +000083 uint32_t S = VDataPtr->getSize();
84 VDataPtr->swapBytesFromHost(ValueProfDataEndianness);
85 Out.write((const char *)VDataPtr.get(), S);
Justin Bogner821d7472014-08-01 22:50:07 +000086 }
Justin Bognerb7aa2632014-04-18 21:48:40 +000087 }
88};
Alexander Kornienkof00654e2015-06-23 09:49:53 +000089}
Justin Bognerb7aa2632014-04-18 21:48:40 +000090
Xinliang David Liee415892015-11-10 00:24:45 +000091// Internal interface for testing purpose only.
92void InstrProfWriter::setValueProfDataEndianness(
93 support::endianness Endianness) {
94 ValueProfDataEndianness = Endianness;
95}
96
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +000097std::error_code InstrProfWriter::addRecord(InstrProfRecord &&I,
98 uint64_t Weight) {
Justin Bogner9e9a0572015-09-29 22:13:58 +000099 auto &ProfileDataMap = FunctionData[I.Name];
100
Nathan Slingerlanda7318292015-11-20 19:12:43 +0000101 bool NewFunc;
102 ProfilingData::iterator Where;
103 std::tie(Where, NewFunc) =
104 ProfileDataMap.insert(std::make_pair(I.Hash, InstrProfRecord()));
105 InstrProfRecord &Dest = Where->second;
Nathan Slingerlandaa5702d2015-12-02 18:19:24 +0000106
Xinliang David Li51dc04c2016-01-08 03:49:59 +0000107 instrprof_error Result = instrprof_error::success;
Nathan Slingerlanda7318292015-11-20 19:12:43 +0000108 if (NewFunc) {
Justin Bogner821d7472014-08-01 22:50:07 +0000109 // We've never seen a function with this name and hash, add it.
Nathan Slingerlanda7318292015-11-20 19:12:43 +0000110 Dest = std::move(I);
Xinliang David Li60057282015-12-20 08:46:18 +0000111 // Fix up the name to avoid dangling reference.
112 Dest.Name = FunctionData.find(Dest.Name)->getKey();
Xinliang David Li51dc04c2016-01-08 03:49:59 +0000113 if (Weight > 1)
114 Result = Dest.scale(Weight);
Nathan Slingerlanda7318292015-11-20 19:12:43 +0000115 } else {
116 // We're updating a function we've seen before.
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000117 Result = Dest.merge(I, Weight);
Justin Bognerb9bd7f82014-03-21 17:46:22 +0000118 }
119
Xinliang David Li062cde92016-01-08 05:45:21 +0000120 Dest.sortValueData();
121
Nathan Slingerlanda7318292015-11-20 19:12:43 +0000122 // We keep track of the max function count as we go for simplicity.
Nathan Slingerlandaa5702d2015-12-02 18:19:24 +0000123 // Update this statistic no matter the result of the merge.
Nathan Slingerlanda7318292015-11-20 19:12:43 +0000124 if (Dest.Counts[0] > MaxFunctionCount)
125 MaxFunctionCount = Dest.Counts[0];
126
Nathan Slingerlandaa5702d2015-12-02 18:19:24 +0000127 return Result;
Justin Bognerb9bd7f82014-03-21 17:46:22 +0000128}
129
Justin Bogner2b6c5372015-02-18 01:58:17 +0000130std::pair<uint64_t, uint64_t> InstrProfWriter::writeImpl(raw_ostream &OS) {
Justin Bognerb7aa2632014-04-18 21:48:40 +0000131 OnDiskChainedHashTableGenerator<InstrProfRecordTrait> Generator;
Justin Bognerb9bd7f82014-03-21 17:46:22 +0000132
Justin Bognerb7aa2632014-04-18 21:48:40 +0000133 // Populate the hash table generator.
Justin Bogner821d7472014-08-01 22:50:07 +0000134 for (const auto &I : FunctionData)
Justin Bognerfa5b0132014-04-23 18:50:16 +0000135 Generator.insert(I.getKey(), &I.getValue());
Justin Bognerb7aa2632014-04-18 21:48:40 +0000136
137 using namespace llvm::support;
138 endian::Writer<little> LE(OS);
139
140 // Write the header.
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000141 IndexedInstrProf::Header Header;
142 Header.Magic = IndexedInstrProf::Magic;
Xinliang David Lia6b2c4f2016-01-14 02:47:01 +0000143 Header.Version = IndexedInstrProf::ProfVersion::CurrentVersion;
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000144 Header.MaxFunctionCount = MaxFunctionCount;
145 Header.HashType = static_cast<uint64_t>(IndexedInstrProf::HashType);
146 Header.HashOffset = 0;
147 int N = sizeof(IndexedInstrProf::Header) / sizeof(uint64_t);
148
149 // Only write out all the fields execpt 'HashOffset'. We need
150 // to remember the offset of that field to allow back patching
151 // later.
152 for (int I = 0; I < N - 1; I++)
153 LE.write<uint64_t>(reinterpret_cast<uint64_t *>(&Header)[I]);
Justin Bognerb7aa2632014-04-18 21:48:40 +0000154
155 // Save a space to write the hash table start location.
156 uint64_t HashTableStartLoc = OS.tell();
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000157 // Reserve the space for HashOffset field.
Justin Bognerb7aa2632014-04-18 21:48:40 +0000158 LE.write<uint64_t>(0);
159 // Write the hash table.
160 uint64_t HashTableStart = Generator.Emit(OS);
161
Justin Bogner2b6c5372015-02-18 01:58:17 +0000162 return std::make_pair(HashTableStartLoc, HashTableStart);
163}
164
165void InstrProfWriter::write(raw_fd_ostream &OS) {
166 // Write the hash table.
167 auto TableStart = writeImpl(OS);
168
Justin Bognerb7aa2632014-04-18 21:48:40 +0000169 // Go back and fill in the hash table start.
Justin Bogner2b6c5372015-02-18 01:58:17 +0000170 using namespace support;
171 OS.seek(TableStart.first);
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000172 // Now patch the HashOffset field previously reserved.
Justin Bogner2b6c5372015-02-18 01:58:17 +0000173 endian::Writer<little>(OS).write<uint64_t>(TableStart.second);
174}
175
Xinliang David Lie3bf4fd32015-12-14 18:44:01 +0000176static const char *ValueProfKindStr[] = {
177#define VALUE_PROF_KIND(Enumerator, Value) #Enumerator,
178#include "llvm/ProfileData/InstrProfData.inc"
179};
180
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000181void InstrProfWriter::writeRecordInText(const InstrProfRecord &Func,
Xinliang David Lia716cc52015-12-20 06:22:13 +0000182 InstrProfSymtab &Symtab,
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000183 raw_fd_ostream &OS) {
Xinliang David Lic6676832015-11-23 22:31:22 +0000184 OS << Func.Name << "\n";
185 OS << "# Func Hash:\n" << Func.Hash << "\n";
Xinliang David Lie3bf4fd32015-12-14 18:44:01 +0000186 OS << "# Num Counters:\n" << Func.Counts.size() << "\n";
Xinliang David Lic6676832015-11-23 22:31:22 +0000187 OS << "# Counter Values:\n";
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000188 for (uint64_t Count : Func.Counts)
189 OS << Count << "\n";
190
Xinliang David Lie3bf4fd32015-12-14 18:44:01 +0000191 uint32_t NumValueKinds = Func.getNumValueKinds();
192 if (!NumValueKinds) {
193 OS << "\n";
194 return;
195 }
196
197 OS << "# Num Value Kinds:\n" << Func.getNumValueKinds() << "\n";
198 for (uint32_t VK = 0; VK < IPVK_Last + 1; VK++) {
199 uint32_t NS = Func.getNumValueSites(VK);
200 if (!NS)
201 continue;
202 OS << "# ValueKind = " << ValueProfKindStr[VK] << ":\n" << VK << "\n";
203 OS << "# NumValueSites:\n" << NS << "\n";
204 for (uint32_t S = 0; S < NS; S++) {
205 uint32_t ND = Func.getNumValueDataForSite(VK, S);
206 OS << ND << "\n";
207 std::unique_ptr<InstrProfValueData[]> VD = Func.getValueForSite(VK, S);
208 for (uint32_t I = 0; I < ND; I++) {
209 if (VK == IPVK_IndirectCallTarget)
Xinliang David Lia716cc52015-12-20 06:22:13 +0000210 OS << Symtab.getFuncName(VD[I].Value) << ":" << VD[I].Count << "\n";
Xinliang David Lie3bf4fd32015-12-14 18:44:01 +0000211 else
212 OS << VD[I].Value << ":" << VD[I].Count << "\n";
213 }
214 }
215 }
216
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000217 OS << "\n";
218}
219
220void InstrProfWriter::writeText(raw_fd_ostream &OS) {
Xinliang David Lia716cc52015-12-20 06:22:13 +0000221 InstrProfSymtab Symtab;
222 for (const auto &I : FunctionData)
223 Symtab.addFuncName(I.getKey());
224 Symtab.finalizeSymtab();
225
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000226 for (const auto &I : FunctionData)
227 for (const auto &Func : I.getValue())
Xinliang David Lia716cc52015-12-20 06:22:13 +0000228 writeRecordInText(Func.second, Symtab, OS);
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000229}
230
Justin Bogner2b6c5372015-02-18 01:58:17 +0000231std::unique_ptr<MemoryBuffer> InstrProfWriter::writeBuffer() {
232 std::string Data;
233 llvm::raw_string_ostream OS(Data);
234 // Write the hash table.
235 auto TableStart = writeImpl(OS);
236 OS.flush();
237
238 // Go back and fill in the hash table start.
239 using namespace support;
240 uint64_t Bytes = endian::byte_swap<uint64_t, little>(TableStart.second);
241 Data.replace(TableStart.first, sizeof(uint64_t), (const char *)&Bytes,
242 sizeof(uint64_t));
243
244 // Return this in an aligned memory buffer.
245 return MemoryBuffer::getMemBufferCopy(Data);
Justin Bognerb9bd7f82014-03-21 17:46:22 +0000246}