blob: 4c7f5de26aa95e3fe33d600df75273e3042bf4ef [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.
Xinliang David Li285d7bd2016-01-15 19:22:41 +000034class ProfOStream {
Xinliang David Lib6066382016-01-15 19:01:04 +000035
Xinliang David Li285d7bd2016-01-15 19:22:41 +000036public:
Xinliang David Lib6066382016-01-15 19:01:04 +000037 ProfOStream(llvm::raw_fd_ostream &FD) : IsFDOStream(true), OS(FD), LE(FD) {}
38 ProfOStream(llvm::raw_string_ostream &STR)
39 : IsFDOStream(false), OS(STR), LE(STR) {}
40
41 uint64_t tell() { return OS.tell(); }
42 void write(uint64_t V) { LE.write<uint64_t>(V); }
43 // \c patch can only be called when all data is written and flushed.
44 // For raw_string_ostream, the patch is done on the target string
45 // directly and it won't be reflected in the stream's internal buffer.
46 void patch(PatchItem *P, int NItems) {
47 using namespace support;
48 if (IsFDOStream) {
49 llvm::raw_fd_ostream &FDOStream = static_cast<llvm::raw_fd_ostream &>(OS);
50 for (int K = 0; K < NItems; K++) {
51 FDOStream.seek(P[K].Pos);
52 for (int I = 0; I < P[K].N; I++)
53 write(P[K].D[I]);
54 }
55 } else {
56 llvm::raw_string_ostream &SOStream =
57 static_cast<llvm::raw_string_ostream &>(OS);
58 std::string &Data = SOStream.str(); // with flush
59 for (int K = 0; K < NItems; K++) {
60 for (int I = 0; I < P[K].N; I++) {
61 uint64_t Bytes = endian::byte_swap<uint64_t, little>(P[K].D[I]);
62 Data.replace(P[K].Pos + I * sizeof(uint64_t), sizeof(uint64_t),
63 (const char *)&Bytes, sizeof(uint64_t));
64 }
65 }
66 }
67 }
68 // If \c OS is an instance of \c raw_fd_ostream, this field will be
69 // true. Otherwise, \c OS will be an raw_string_ostream.
70 bool IsFDOStream;
71 raw_ostream &OS;
72 support::endian::Writer<support::little> LE;
73};
74}
75
Justin Bognerb7aa2632014-04-18 21:48:40 +000076namespace {
Xinliang David Liee415892015-11-10 00:24:45 +000077static support::endianness ValueProfDataEndianness = support::little;
78
Justin Bognerb7aa2632014-04-18 21:48:40 +000079class InstrProfRecordTrait {
80public:
81 typedef StringRef key_type;
82 typedef StringRef key_type_ref;
83
Justin Bogner9e9a0572015-09-29 22:13:58 +000084 typedef const InstrProfWriter::ProfilingData *const data_type;
85 typedef const InstrProfWriter::ProfilingData *const data_type_ref;
Justin Bognerb7aa2632014-04-18 21:48:40 +000086
87 typedef uint64_t hash_value_type;
88 typedef uint64_t offset_type;
89
90 static hash_value_type ComputeHash(key_type_ref K) {
Xinliang David Li49ee76d2015-12-18 22:22:12 +000091 return IndexedInstrProf::ComputeHash(K);
Justin Bognerb7aa2632014-04-18 21:48:40 +000092 }
93
94 static std::pair<offset_type, offset_type>
95 EmitKeyDataLength(raw_ostream &Out, key_type_ref K, data_type_ref V) {
96 using namespace llvm::support;
97 endian::Writer<little> LE(Out);
98
Justin Bognere8081712014-04-19 00:33:15 +000099 offset_type N = K.size();
Justin Bognerb7aa2632014-04-18 21:48:40 +0000100 LE.write<offset_type>(N);
101
Justin Bogner821d7472014-08-01 22:50:07 +0000102 offset_type M = 0;
Justin Bogner9e9a0572015-09-29 22:13:58 +0000103 for (const auto &ProfileData : *V) {
Xinliang David Li2004f002015-11-02 05:08:23 +0000104 const InstrProfRecord &ProfRecord = ProfileData.second;
Justin Bogner9e9a0572015-09-29 22:13:58 +0000105 M += sizeof(uint64_t); // The function hash
106 M += sizeof(uint64_t); // The size of the Counts vector
Xinliang David Li2004f002015-11-02 05:08:23 +0000107 M += ProfRecord.Counts.size() * sizeof(uint64_t);
Justin Bogner9e9a0572015-09-29 22:13:58 +0000108
109 // Value data
Xinliang David Li99556872015-11-17 23:00:40 +0000110 M += ValueProfData::getSize(ProfileData.second);
Justin Bogner9e9a0572015-09-29 22:13:58 +0000111 }
Justin Bognerb7aa2632014-04-18 21:48:40 +0000112 LE.write<offset_type>(M);
113
114 return std::make_pair(N, M);
115 }
116
Justin Bognere8081712014-04-19 00:33:15 +0000117 static void EmitKey(raw_ostream &Out, key_type_ref K, offset_type N){
Justin Bognerb7aa2632014-04-18 21:48:40 +0000118 Out.write(K.data(), N);
119 }
120
121 static void EmitData(raw_ostream &Out, key_type_ref, data_type_ref V,
Justin Bognere8081712014-04-19 00:33:15 +0000122 offset_type) {
Justin Bognerb7aa2632014-04-18 21:48:40 +0000123 using namespace llvm::support;
124 endian::Writer<little> LE(Out);
Justin Bogner9e9a0572015-09-29 22:13:58 +0000125 for (const auto &ProfileData : *V) {
Xinliang David Li2004f002015-11-02 05:08:23 +0000126 const InstrProfRecord &ProfRecord = ProfileData.second;
127
Justin Bogner9e9a0572015-09-29 22:13:58 +0000128 LE.write<uint64_t>(ProfileData.first); // Function hash
Xinliang David Li2004f002015-11-02 05:08:23 +0000129 LE.write<uint64_t>(ProfRecord.Counts.size());
Xinliang David Li6aa216c2015-11-06 07:54:21 +0000130 for (uint64_t I : ProfRecord.Counts)
131 LE.write<uint64_t>(I);
Justin Bogner9e9a0572015-09-29 22:13:58 +0000132
Justin Bogner9e9a0572015-09-29 22:13:58 +0000133 // Write value data
Xinliang David Li99556872015-11-17 23:00:40 +0000134 std::unique_ptr<ValueProfData> VDataPtr =
135 ValueProfData::serializeFrom(ProfileData.second);
Xinliang David Liee415892015-11-10 00:24:45 +0000136 uint32_t S = VDataPtr->getSize();
137 VDataPtr->swapBytesFromHost(ValueProfDataEndianness);
138 Out.write((const char *)VDataPtr.get(), S);
Justin Bogner821d7472014-08-01 22:50:07 +0000139 }
Justin Bognerb7aa2632014-04-18 21:48:40 +0000140 }
141};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000142}
Justin Bognerb7aa2632014-04-18 21:48:40 +0000143
Xinliang David Liee415892015-11-10 00:24:45 +0000144// Internal interface for testing purpose only.
145void InstrProfWriter::setValueProfDataEndianness(
146 support::endianness Endianness) {
147 ValueProfDataEndianness = Endianness;
148}
149
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000150std::error_code InstrProfWriter::addRecord(InstrProfRecord &&I,
151 uint64_t Weight) {
Justin Bogner9e9a0572015-09-29 22:13:58 +0000152 auto &ProfileDataMap = FunctionData[I.Name];
153
Nathan Slingerlanda7318292015-11-20 19:12:43 +0000154 bool NewFunc;
155 ProfilingData::iterator Where;
156 std::tie(Where, NewFunc) =
157 ProfileDataMap.insert(std::make_pair(I.Hash, InstrProfRecord()));
158 InstrProfRecord &Dest = Where->second;
Nathan Slingerlandaa5702d2015-12-02 18:19:24 +0000159
Xinliang David Li51dc04c2016-01-08 03:49:59 +0000160 instrprof_error Result = instrprof_error::success;
Nathan Slingerlanda7318292015-11-20 19:12:43 +0000161 if (NewFunc) {
Justin Bogner821d7472014-08-01 22:50:07 +0000162 // We've never seen a function with this name and hash, add it.
Nathan Slingerlanda7318292015-11-20 19:12:43 +0000163 Dest = std::move(I);
Xinliang David Li60057282015-12-20 08:46:18 +0000164 // Fix up the name to avoid dangling reference.
165 Dest.Name = FunctionData.find(Dest.Name)->getKey();
Xinliang David Li51dc04c2016-01-08 03:49:59 +0000166 if (Weight > 1)
167 Result = Dest.scale(Weight);
Nathan Slingerlanda7318292015-11-20 19:12:43 +0000168 } else {
169 // We're updating a function we've seen before.
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000170 Result = Dest.merge(I, Weight);
Justin Bognerb9bd7f82014-03-21 17:46:22 +0000171 }
172
Xinliang David Li062cde92016-01-08 05:45:21 +0000173 Dest.sortValueData();
174
Nathan Slingerlanda7318292015-11-20 19:12:43 +0000175 // We keep track of the max function count as we go for simplicity.
Nathan Slingerlandaa5702d2015-12-02 18:19:24 +0000176 // Update this statistic no matter the result of the merge.
Nathan Slingerlanda7318292015-11-20 19:12:43 +0000177 if (Dest.Counts[0] > MaxFunctionCount)
178 MaxFunctionCount = Dest.Counts[0];
179
Nathan Slingerlandaa5702d2015-12-02 18:19:24 +0000180 return Result;
Justin Bognerb9bd7f82014-03-21 17:46:22 +0000181}
182
Xinliang David Lib6066382016-01-15 19:01:04 +0000183void InstrProfWriter::writeImpl(ProfOStream &OS) {
Justin Bognerb7aa2632014-04-18 21:48:40 +0000184 OnDiskChainedHashTableGenerator<InstrProfRecordTrait> Generator;
Justin Bognerb7aa2632014-04-18 21:48:40 +0000185 // Populate the hash table generator.
Justin Bogner821d7472014-08-01 22:50:07 +0000186 for (const auto &I : FunctionData)
Justin Bognerfa5b0132014-04-23 18:50:16 +0000187 Generator.insert(I.getKey(), &I.getValue());
Justin Bognerb7aa2632014-04-18 21:48:40 +0000188 // Write the header.
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000189 IndexedInstrProf::Header Header;
190 Header.Magic = IndexedInstrProf::Magic;
Xinliang David Lia6b2c4f2016-01-14 02:47:01 +0000191 Header.Version = IndexedInstrProf::ProfVersion::CurrentVersion;
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000192 Header.MaxFunctionCount = MaxFunctionCount;
193 Header.HashType = static_cast<uint64_t>(IndexedInstrProf::HashType);
194 Header.HashOffset = 0;
195 int N = sizeof(IndexedInstrProf::Header) / sizeof(uint64_t);
196
197 // Only write out all the fields execpt 'HashOffset'. We need
198 // to remember the offset of that field to allow back patching
199 // later.
200 for (int I = 0; I < N - 1; I++)
Xinliang David Lib6066382016-01-15 19:01:04 +0000201 OS.write(reinterpret_cast<uint64_t *>(&Header)[I]);
Justin Bognerb7aa2632014-04-18 21:48:40 +0000202
203 // Save a space to write the hash table start location.
204 uint64_t HashTableStartLoc = OS.tell();
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000205 // Reserve the space for HashOffset field.
Xinliang David Lib6066382016-01-15 19:01:04 +0000206 OS.write(0);
Justin Bognerb7aa2632014-04-18 21:48:40 +0000207 // Write the hash table.
Xinliang David Lib6066382016-01-15 19:01:04 +0000208 uint64_t HashTableStart = Generator.Emit(OS.OS);
Justin Bognerb7aa2632014-04-18 21:48:40 +0000209
Xinliang David Lib6066382016-01-15 19:01:04 +0000210 // Now do the final patch:
211 PatchItem PatchItems[1] = {{HashTableStartLoc, &HashTableStart, 1}};
212 OS.patch(PatchItems, sizeof(PatchItems) / sizeof(*PatchItems));
Justin Bogner2b6c5372015-02-18 01:58:17 +0000213}
214
215void InstrProfWriter::write(raw_fd_ostream &OS) {
216 // Write the hash table.
Xinliang David Lib6066382016-01-15 19:01:04 +0000217 ProfOStream POS(OS);
218 writeImpl(POS);
219}
Justin Bogner2b6c5372015-02-18 01:58:17 +0000220
Xinliang David Lib6066382016-01-15 19:01:04 +0000221std::unique_ptr<MemoryBuffer> InstrProfWriter::writeBuffer() {
222 std::string Data;
223 llvm::raw_string_ostream OS(Data);
224 ProfOStream POS(OS);
225 // Write the hash table.
226 writeImpl(POS);
227 // Return this in an aligned memory buffer.
228 return MemoryBuffer::getMemBufferCopy(Data);
Justin Bogner2b6c5372015-02-18 01:58:17 +0000229}
230
Xinliang David Lie3bf4fd32015-12-14 18:44:01 +0000231static const char *ValueProfKindStr[] = {
232#define VALUE_PROF_KIND(Enumerator, Value) #Enumerator,
233#include "llvm/ProfileData/InstrProfData.inc"
234};
235
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000236void InstrProfWriter::writeRecordInText(const InstrProfRecord &Func,
Xinliang David Lia716cc52015-12-20 06:22:13 +0000237 InstrProfSymtab &Symtab,
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000238 raw_fd_ostream &OS) {
Xinliang David Lic6676832015-11-23 22:31:22 +0000239 OS << Func.Name << "\n";
240 OS << "# Func Hash:\n" << Func.Hash << "\n";
Xinliang David Lie3bf4fd32015-12-14 18:44:01 +0000241 OS << "# Num Counters:\n" << Func.Counts.size() << "\n";
Xinliang David Lic6676832015-11-23 22:31:22 +0000242 OS << "# Counter Values:\n";
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000243 for (uint64_t Count : Func.Counts)
244 OS << Count << "\n";
245
Xinliang David Lie3bf4fd32015-12-14 18:44:01 +0000246 uint32_t NumValueKinds = Func.getNumValueKinds();
247 if (!NumValueKinds) {
248 OS << "\n";
249 return;
250 }
251
252 OS << "# Num Value Kinds:\n" << Func.getNumValueKinds() << "\n";
253 for (uint32_t VK = 0; VK < IPVK_Last + 1; VK++) {
254 uint32_t NS = Func.getNumValueSites(VK);
255 if (!NS)
256 continue;
257 OS << "# ValueKind = " << ValueProfKindStr[VK] << ":\n" << VK << "\n";
258 OS << "# NumValueSites:\n" << NS << "\n";
259 for (uint32_t S = 0; S < NS; S++) {
260 uint32_t ND = Func.getNumValueDataForSite(VK, S);
261 OS << ND << "\n";
262 std::unique_ptr<InstrProfValueData[]> VD = Func.getValueForSite(VK, S);
263 for (uint32_t I = 0; I < ND; I++) {
264 if (VK == IPVK_IndirectCallTarget)
Xinliang David Lia716cc52015-12-20 06:22:13 +0000265 OS << Symtab.getFuncName(VD[I].Value) << ":" << VD[I].Count << "\n";
Xinliang David Lie3bf4fd32015-12-14 18:44:01 +0000266 else
267 OS << VD[I].Value << ":" << VD[I].Count << "\n";
268 }
269 }
270 }
271
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000272 OS << "\n";
273}
274
275void InstrProfWriter::writeText(raw_fd_ostream &OS) {
Xinliang David Lia716cc52015-12-20 06:22:13 +0000276 InstrProfSymtab Symtab;
277 for (const auto &I : FunctionData)
278 Symtab.addFuncName(I.getKey());
279 Symtab.finalizeSymtab();
280
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000281 for (const auto &I : FunctionData)
282 for (const auto &Func : I.getValue())
Xinliang David Lia716cc52015-12-20 06:22:13 +0000283 writeRecordInText(Func.second, Symtab, OS);
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000284}