blob: f684dde336adce7aefd3f470c762bcaea692e7a1 [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
Xinliang David Lib6066382016-01-15 19:01:04 +000023// A struct to define how the data stream should be patched. For Indexed
24// profiling, only uint64_t data type is needed.
25struct PatchItem {
26 uint64_t Pos; // Where to patch.
27 uint64_t *D; // Pointer to an array of source data.
28 int N; // Number of elements in \c D array.
29};
30
31namespace llvm {
32// A wrapper class to abstract writer stream with support of bytes
33// back patching.
34struct ProfOStream {
35
36 ProfOStream(llvm::raw_fd_ostream &FD) : IsFDOStream(true), OS(FD), LE(FD) {}
37 ProfOStream(llvm::raw_string_ostream &STR)
38 : IsFDOStream(false), OS(STR), LE(STR) {}
39
40 uint64_t tell() { return OS.tell(); }
41 void write(uint64_t V) { LE.write<uint64_t>(V); }
42 // \c patch can only be called when all data is written and flushed.
43 // For raw_string_ostream, the patch is done on the target string
44 // directly and it won't be reflected in the stream's internal buffer.
45 void patch(PatchItem *P, int NItems) {
46 using namespace support;
47 if (IsFDOStream) {
48 llvm::raw_fd_ostream &FDOStream = static_cast<llvm::raw_fd_ostream &>(OS);
49 for (int K = 0; K < NItems; K++) {
50 FDOStream.seek(P[K].Pos);
51 for (int I = 0; I < P[K].N; I++)
52 write(P[K].D[I]);
53 }
54 } else {
55 llvm::raw_string_ostream &SOStream =
56 static_cast<llvm::raw_string_ostream &>(OS);
57 std::string &Data = SOStream.str(); // with flush
58 for (int K = 0; K < NItems; K++) {
59 for (int I = 0; I < P[K].N; I++) {
60 uint64_t Bytes = endian::byte_swap<uint64_t, little>(P[K].D[I]);
61 Data.replace(P[K].Pos + I * sizeof(uint64_t), sizeof(uint64_t),
62 (const char *)&Bytes, sizeof(uint64_t));
63 }
64 }
65 }
66 }
67 // If \c OS is an instance of \c raw_fd_ostream, this field will be
68 // true. Otherwise, \c OS will be an raw_string_ostream.
69 bool IsFDOStream;
70 raw_ostream &OS;
71 support::endian::Writer<support::little> LE;
72};
73}
74
Justin Bognerb7aa2632014-04-18 21:48:40 +000075namespace {
Xinliang David Liee415892015-11-10 00:24:45 +000076static support::endianness ValueProfDataEndianness = support::little;
77
Justin Bognerb7aa2632014-04-18 21:48:40 +000078class InstrProfRecordTrait {
79public:
80 typedef StringRef key_type;
81 typedef StringRef key_type_ref;
82
Justin Bogner9e9a0572015-09-29 22:13:58 +000083 typedef const InstrProfWriter::ProfilingData *const data_type;
84 typedef const InstrProfWriter::ProfilingData *const data_type_ref;
Justin Bognerb7aa2632014-04-18 21:48:40 +000085
86 typedef uint64_t hash_value_type;
87 typedef uint64_t offset_type;
88
89 static hash_value_type ComputeHash(key_type_ref K) {
Xinliang David Li49ee76d2015-12-18 22:22:12 +000090 return IndexedInstrProf::ComputeHash(K);
Justin Bognerb7aa2632014-04-18 21:48:40 +000091 }
92
93 static std::pair<offset_type, offset_type>
94 EmitKeyDataLength(raw_ostream &Out, key_type_ref K, data_type_ref V) {
95 using namespace llvm::support;
96 endian::Writer<little> LE(Out);
97
Justin Bognere8081712014-04-19 00:33:15 +000098 offset_type N = K.size();
Justin Bognerb7aa2632014-04-18 21:48:40 +000099 LE.write<offset_type>(N);
100
Justin Bogner821d7472014-08-01 22:50:07 +0000101 offset_type M = 0;
Justin Bogner9e9a0572015-09-29 22:13:58 +0000102 for (const auto &ProfileData : *V) {
Xinliang David Li2004f002015-11-02 05:08:23 +0000103 const InstrProfRecord &ProfRecord = ProfileData.second;
Justin Bogner9e9a0572015-09-29 22:13:58 +0000104 M += sizeof(uint64_t); // The function hash
105 M += sizeof(uint64_t); // The size of the Counts vector
Xinliang David Li2004f002015-11-02 05:08:23 +0000106 M += ProfRecord.Counts.size() * sizeof(uint64_t);
Justin Bogner9e9a0572015-09-29 22:13:58 +0000107
108 // Value data
Xinliang David Li99556872015-11-17 23:00:40 +0000109 M += ValueProfData::getSize(ProfileData.second);
Justin Bogner9e9a0572015-09-29 22:13:58 +0000110 }
Justin Bognerb7aa2632014-04-18 21:48:40 +0000111 LE.write<offset_type>(M);
112
113 return std::make_pair(N, M);
114 }
115
Justin Bognere8081712014-04-19 00:33:15 +0000116 static void EmitKey(raw_ostream &Out, key_type_ref K, offset_type N){
Justin Bognerb7aa2632014-04-18 21:48:40 +0000117 Out.write(K.data(), N);
118 }
119
120 static void EmitData(raw_ostream &Out, key_type_ref, data_type_ref V,
Justin Bognere8081712014-04-19 00:33:15 +0000121 offset_type) {
Justin Bognerb7aa2632014-04-18 21:48:40 +0000122 using namespace llvm::support;
123 endian::Writer<little> LE(Out);
Justin Bogner9e9a0572015-09-29 22:13:58 +0000124 for (const auto &ProfileData : *V) {
Xinliang David Li2004f002015-11-02 05:08:23 +0000125 const InstrProfRecord &ProfRecord = ProfileData.second;
126
Justin Bogner9e9a0572015-09-29 22:13:58 +0000127 LE.write<uint64_t>(ProfileData.first); // Function hash
Xinliang David Li2004f002015-11-02 05:08:23 +0000128 LE.write<uint64_t>(ProfRecord.Counts.size());
Xinliang David Li6aa216c2015-11-06 07:54:21 +0000129 for (uint64_t I : ProfRecord.Counts)
130 LE.write<uint64_t>(I);
Justin Bogner9e9a0572015-09-29 22:13:58 +0000131
Justin Bogner9e9a0572015-09-29 22:13:58 +0000132 // Write value data
Xinliang David Li99556872015-11-17 23:00:40 +0000133 std::unique_ptr<ValueProfData> VDataPtr =
134 ValueProfData::serializeFrom(ProfileData.second);
Xinliang David Liee415892015-11-10 00:24:45 +0000135 uint32_t S = VDataPtr->getSize();
136 VDataPtr->swapBytesFromHost(ValueProfDataEndianness);
137 Out.write((const char *)VDataPtr.get(), S);
Justin Bogner821d7472014-08-01 22:50:07 +0000138 }
Justin Bognerb7aa2632014-04-18 21:48:40 +0000139 }
140};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000141}
Justin Bognerb7aa2632014-04-18 21:48:40 +0000142
Xinliang David Liee415892015-11-10 00:24:45 +0000143// Internal interface for testing purpose only.
144void InstrProfWriter::setValueProfDataEndianness(
145 support::endianness Endianness) {
146 ValueProfDataEndianness = Endianness;
147}
148
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000149std::error_code InstrProfWriter::addRecord(InstrProfRecord &&I,
150 uint64_t Weight) {
Justin Bogner9e9a0572015-09-29 22:13:58 +0000151 auto &ProfileDataMap = FunctionData[I.Name];
152
Nathan Slingerlanda7318292015-11-20 19:12:43 +0000153 bool NewFunc;
154 ProfilingData::iterator Where;
155 std::tie(Where, NewFunc) =
156 ProfileDataMap.insert(std::make_pair(I.Hash, InstrProfRecord()));
157 InstrProfRecord &Dest = Where->second;
Nathan Slingerlandaa5702d2015-12-02 18:19:24 +0000158
Xinliang David Li51dc04c2016-01-08 03:49:59 +0000159 instrprof_error Result = instrprof_error::success;
Nathan Slingerlanda7318292015-11-20 19:12:43 +0000160 if (NewFunc) {
Justin Bogner821d7472014-08-01 22:50:07 +0000161 // We've never seen a function with this name and hash, add it.
Nathan Slingerlanda7318292015-11-20 19:12:43 +0000162 Dest = std::move(I);
Xinliang David Li60057282015-12-20 08:46:18 +0000163 // Fix up the name to avoid dangling reference.
164 Dest.Name = FunctionData.find(Dest.Name)->getKey();
Xinliang David Li51dc04c2016-01-08 03:49:59 +0000165 if (Weight > 1)
166 Result = Dest.scale(Weight);
Nathan Slingerlanda7318292015-11-20 19:12:43 +0000167 } else {
168 // We're updating a function we've seen before.
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000169 Result = Dest.merge(I, Weight);
Justin Bognerb9bd7f82014-03-21 17:46:22 +0000170 }
171
Xinliang David Li062cde92016-01-08 05:45:21 +0000172 Dest.sortValueData();
173
Nathan Slingerlanda7318292015-11-20 19:12:43 +0000174 // We keep track of the max function count as we go for simplicity.
Nathan Slingerlandaa5702d2015-12-02 18:19:24 +0000175 // Update this statistic no matter the result of the merge.
Nathan Slingerlanda7318292015-11-20 19:12:43 +0000176 if (Dest.Counts[0] > MaxFunctionCount)
177 MaxFunctionCount = Dest.Counts[0];
178
Nathan Slingerlandaa5702d2015-12-02 18:19:24 +0000179 return Result;
Justin Bognerb9bd7f82014-03-21 17:46:22 +0000180}
181
Xinliang David Lib6066382016-01-15 19:01:04 +0000182void InstrProfWriter::writeImpl(ProfOStream &OS) {
Justin Bognerb7aa2632014-04-18 21:48:40 +0000183 OnDiskChainedHashTableGenerator<InstrProfRecordTrait> Generator;
Justin Bognerb7aa2632014-04-18 21:48:40 +0000184 // Populate the hash table generator.
Justin Bogner821d7472014-08-01 22:50:07 +0000185 for (const auto &I : FunctionData)
Justin Bognerfa5b0132014-04-23 18:50:16 +0000186 Generator.insert(I.getKey(), &I.getValue());
Justin Bognerb7aa2632014-04-18 21:48:40 +0000187 // Write the header.
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000188 IndexedInstrProf::Header Header;
189 Header.Magic = IndexedInstrProf::Magic;
Xinliang David Lia6b2c4f2016-01-14 02:47:01 +0000190 Header.Version = IndexedInstrProf::ProfVersion::CurrentVersion;
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000191 Header.MaxFunctionCount = MaxFunctionCount;
192 Header.HashType = static_cast<uint64_t>(IndexedInstrProf::HashType);
193 Header.HashOffset = 0;
194 int N = sizeof(IndexedInstrProf::Header) / sizeof(uint64_t);
195
196 // Only write out all the fields execpt 'HashOffset'. We need
197 // to remember the offset of that field to allow back patching
198 // later.
199 for (int I = 0; I < N - 1; I++)
Xinliang David Lib6066382016-01-15 19:01:04 +0000200 OS.write(reinterpret_cast<uint64_t *>(&Header)[I]);
Justin Bognerb7aa2632014-04-18 21:48:40 +0000201
202 // Save a space to write the hash table start location.
203 uint64_t HashTableStartLoc = OS.tell();
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000204 // Reserve the space for HashOffset field.
Xinliang David Lib6066382016-01-15 19:01:04 +0000205 OS.write(0);
Justin Bognerb7aa2632014-04-18 21:48:40 +0000206 // Write the hash table.
Xinliang David Lib6066382016-01-15 19:01:04 +0000207 uint64_t HashTableStart = Generator.Emit(OS.OS);
Justin Bognerb7aa2632014-04-18 21:48:40 +0000208
Xinliang David Lib6066382016-01-15 19:01:04 +0000209 // Now do the final patch:
210 PatchItem PatchItems[1] = {{HashTableStartLoc, &HashTableStart, 1}};
211 OS.patch(PatchItems, sizeof(PatchItems) / sizeof(*PatchItems));
Justin Bogner2b6c5372015-02-18 01:58:17 +0000212}
213
214void InstrProfWriter::write(raw_fd_ostream &OS) {
215 // Write the hash table.
Xinliang David Lib6066382016-01-15 19:01:04 +0000216 ProfOStream POS(OS);
217 writeImpl(POS);
218}
Justin Bogner2b6c5372015-02-18 01:58:17 +0000219
Xinliang David Lib6066382016-01-15 19:01:04 +0000220std::unique_ptr<MemoryBuffer> InstrProfWriter::writeBuffer() {
221 std::string Data;
222 llvm::raw_string_ostream OS(Data);
223 ProfOStream POS(OS);
224 // Write the hash table.
225 writeImpl(POS);
226 // Return this in an aligned memory buffer.
227 return MemoryBuffer::getMemBufferCopy(Data);
Justin Bogner2b6c5372015-02-18 01:58:17 +0000228}
229
Xinliang David Lie3bf4fd32015-12-14 18:44:01 +0000230static const char *ValueProfKindStr[] = {
231#define VALUE_PROF_KIND(Enumerator, Value) #Enumerator,
232#include "llvm/ProfileData/InstrProfData.inc"
233};
234
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000235void InstrProfWriter::writeRecordInText(const InstrProfRecord &Func,
Xinliang David Lia716cc52015-12-20 06:22:13 +0000236 InstrProfSymtab &Symtab,
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000237 raw_fd_ostream &OS) {
Xinliang David Lic6676832015-11-23 22:31:22 +0000238 OS << Func.Name << "\n";
239 OS << "# Func Hash:\n" << Func.Hash << "\n";
Xinliang David Lie3bf4fd32015-12-14 18:44:01 +0000240 OS << "# Num Counters:\n" << Func.Counts.size() << "\n";
Xinliang David Lic6676832015-11-23 22:31:22 +0000241 OS << "# Counter Values:\n";
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000242 for (uint64_t Count : Func.Counts)
243 OS << Count << "\n";
244
Xinliang David Lie3bf4fd32015-12-14 18:44:01 +0000245 uint32_t NumValueKinds = Func.getNumValueKinds();
246 if (!NumValueKinds) {
247 OS << "\n";
248 return;
249 }
250
251 OS << "# Num Value Kinds:\n" << Func.getNumValueKinds() << "\n";
252 for (uint32_t VK = 0; VK < IPVK_Last + 1; VK++) {
253 uint32_t NS = Func.getNumValueSites(VK);
254 if (!NS)
255 continue;
256 OS << "# ValueKind = " << ValueProfKindStr[VK] << ":\n" << VK << "\n";
257 OS << "# NumValueSites:\n" << NS << "\n";
258 for (uint32_t S = 0; S < NS; S++) {
259 uint32_t ND = Func.getNumValueDataForSite(VK, S);
260 OS << ND << "\n";
261 std::unique_ptr<InstrProfValueData[]> VD = Func.getValueForSite(VK, S);
262 for (uint32_t I = 0; I < ND; I++) {
263 if (VK == IPVK_IndirectCallTarget)
Xinliang David Lia716cc52015-12-20 06:22:13 +0000264 OS << Symtab.getFuncName(VD[I].Value) << ":" << VD[I].Count << "\n";
Xinliang David Lie3bf4fd32015-12-14 18:44:01 +0000265 else
266 OS << VD[I].Value << ":" << VD[I].Count << "\n";
267 }
268 }
269 }
270
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000271 OS << "\n";
272}
273
274void InstrProfWriter::writeText(raw_fd_ostream &OS) {
Xinliang David Lia716cc52015-12-20 06:22:13 +0000275 InstrProfSymtab Symtab;
276 for (const auto &I : FunctionData)
277 Symtab.addFuncName(I.getKey());
278 Symtab.finalizeSymtab();
279
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000280 for (const auto &I : FunctionData)
281 for (const auto &Func : I.getValue())
Xinliang David Lia716cc52015-12-20 06:22:13 +0000282 writeRecordInText(Func.second, Symtab, OS);
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000283}