blob: 333865395766943701a13b67b892348cd0f1dba6 [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"
19
Justin Bognerb9bd7f82014-03-21 17:46:22 +000020using namespace llvm;
21
Justin Bognerb7aa2632014-04-18 21:48:40 +000022namespace {
23class InstrProfRecordTrait {
24public:
25 typedef StringRef key_type;
26 typedef StringRef key_type_ref;
27
Justin Bogner9e9a0572015-09-29 22:13:58 +000028 typedef const InstrProfWriter::ProfilingData *const data_type;
29 typedef const InstrProfWriter::ProfilingData *const data_type_ref;
Justin Bognerb7aa2632014-04-18 21:48:40 +000030
31 typedef uint64_t hash_value_type;
32 typedef uint64_t offset_type;
33
34 static hash_value_type ComputeHash(key_type_ref K) {
35 return IndexedInstrProf::ComputeHash(IndexedInstrProf::HashType, K);
36 }
37
38 static std::pair<offset_type, offset_type>
39 EmitKeyDataLength(raw_ostream &Out, key_type_ref K, data_type_ref V) {
40 using namespace llvm::support;
41 endian::Writer<little> LE(Out);
42
Justin Bognere8081712014-04-19 00:33:15 +000043 offset_type N = K.size();
Justin Bognerb7aa2632014-04-18 21:48:40 +000044 LE.write<offset_type>(N);
45
Justin Bogner821d7472014-08-01 22:50:07 +000046 offset_type M = 0;
Justin Bogner9e9a0572015-09-29 22:13:58 +000047 for (const auto &ProfileData : *V) {
Xinliang David Li2004f002015-11-02 05:08:23 +000048 const InstrProfRecord &ProfRecord = ProfileData.second;
Justin Bogner9e9a0572015-09-29 22:13:58 +000049 M += sizeof(uint64_t); // The function hash
50 M += sizeof(uint64_t); // The size of the Counts vector
Xinliang David Li2004f002015-11-02 05:08:23 +000051 M += ProfRecord.Counts.size() * sizeof(uint64_t);
Justin Bogner9e9a0572015-09-29 22:13:58 +000052
53 // Value data
54 M += sizeof(uint64_t); // Number of value kinds with value sites.
55 for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind) {
Xinliang David Li2004f002015-11-02 05:08:23 +000056 uint32_t NumValueSites = ProfRecord.getNumValueSites(Kind);
57 if (NumValueSites == 0) continue;
Justin Bogner9e9a0572015-09-29 22:13:58 +000058 M += sizeof(uint64_t); // Value kind
59 M += sizeof(uint64_t); // The number of value sites for given value kind
Xinliang David Li2004f002015-11-02 05:08:23 +000060 for (uint32_t I = 0; I < NumValueSites; I++) {
Justin Bogner9e9a0572015-09-29 22:13:58 +000061 M += sizeof(uint64_t); // Number of value data pairs at a value site
Xinliang David Li2004f002015-11-02 05:08:23 +000062 uint64_t NumValueDataForSite =
63 ProfRecord.getNumValueDataForSite(Kind, I);
64 M += 2 * sizeof(uint64_t) * NumValueDataForSite; // Value data pairs
Justin Bogner9e9a0572015-09-29 22:13:58 +000065 }
66 }
67 }
Justin Bognerb7aa2632014-04-18 21:48:40 +000068 LE.write<offset_type>(M);
69
70 return std::make_pair(N, M);
71 }
72
Justin Bognere8081712014-04-19 00:33:15 +000073 static void EmitKey(raw_ostream &Out, key_type_ref K, offset_type N){
Justin Bognerb7aa2632014-04-18 21:48:40 +000074 Out.write(K.data(), N);
75 }
76
77 static void EmitData(raw_ostream &Out, key_type_ref, data_type_ref V,
Justin Bognere8081712014-04-19 00:33:15 +000078 offset_type) {
Justin Bognerb7aa2632014-04-18 21:48:40 +000079 using namespace llvm::support;
80 endian::Writer<little> LE(Out);
Justin Bogner9e9a0572015-09-29 22:13:58 +000081 for (const auto &ProfileData : *V) {
Xinliang David Li2004f002015-11-02 05:08:23 +000082 const InstrProfRecord &ProfRecord = ProfileData.second;
83
Justin Bogner9e9a0572015-09-29 22:13:58 +000084 LE.write<uint64_t>(ProfileData.first); // Function hash
Xinliang David Li2004f002015-11-02 05:08:23 +000085 LE.write<uint64_t>(ProfRecord.Counts.size());
86 for (uint64_t I : ProfRecord.Counts) LE.write<uint64_t>(I);
Justin Bogner9e9a0572015-09-29 22:13:58 +000087
88 // Compute the number of value kinds with value sites.
Xinliang David Li2004f002015-11-02 05:08:23 +000089 uint64_t NumValueKinds = ProfRecord.getNumValueKinds();
Justin Bogner9e9a0572015-09-29 22:13:58 +000090 LE.write<uint64_t>(NumValueKinds);
91
92 // Write value data
93 for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind) {
Xinliang David Li2004f002015-11-02 05:08:23 +000094 uint32_t NumValueSites = ProfRecord.getNumValueSites(Kind);
95 if (NumValueSites == 0) continue;
Justin Bogner9e9a0572015-09-29 22:13:58 +000096 LE.write<uint64_t>(Kind); // Write value kind
97 // Write number of value sites for current value kind
Xinliang David Li2004f002015-11-02 05:08:23 +000098 LE.write<uint64_t>(NumValueSites);
99
100 for (uint32_t I = 0; I < NumValueSites; I++) {
Justin Bogner9e9a0572015-09-29 22:13:58 +0000101 // Write number of value data pairs at this value site
Xinliang David Li2004f002015-11-02 05:08:23 +0000102 uint64_t NumValueDataForSite =
103 ProfRecord.getNumValueDataForSite(Kind, I);
104 LE.write<uint64_t>(NumValueDataForSite);
105 std::unique_ptr<InstrProfValueData[]> VD =
106 ProfRecord.getValueForSite(Kind, I);
107
108 for (uint32_t V = 0; V < NumValueDataForSite; V++) {
Justin Bogner9e9a0572015-09-29 22:13:58 +0000109 if (Kind == IPVK_IndirectCallTarget)
Xinliang David Li2004f002015-11-02 05:08:23 +0000110 LE.write<uint64_t>(ComputeHash((const char *)VD[V].Value));
Justin Bogner9e9a0572015-09-29 22:13:58 +0000111 else
Xinliang David Li2004f002015-11-02 05:08:23 +0000112 LE.write<uint64_t>(VD[V].Value);
113 LE.write<uint64_t>(VD[V].Count);
Justin Bogner9e9a0572015-09-29 22:13:58 +0000114 }
115 }
116 }
Justin Bogner821d7472014-08-01 22:50:07 +0000117 }
Justin Bognerb7aa2632014-04-18 21:48:40 +0000118 }
119};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000120}
Justin Bognerb7aa2632014-04-18 21:48:40 +0000121
Justin Bogner9e9a0572015-09-29 22:13:58 +0000122static std::error_code combineInstrProfRecords(InstrProfRecord &Dest,
123 InstrProfRecord &Source,
124 uint64_t &MaxFunctionCount) {
125 // If the number of counters doesn't match we either have bad data
126 // or a hash collision.
127 if (Dest.Counts.size() != Source.Counts.size())
128 return instrprof_error::count_mismatch;
Justin Bogner821d7472014-08-01 22:50:07 +0000129
Justin Bogner9e9a0572015-09-29 22:13:58 +0000130 for (size_t I = 0, E = Source.Counts.size(); I < E; ++I) {
131 if (Dest.Counts[I] + Source.Counts[I] < Dest.Counts[I])
132 return instrprof_error::counter_overflow;
133 Dest.Counts[I] += Source.Counts[I];
134 }
135
136 for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind) {
Xinliang David Li2004f002015-11-02 05:08:23 +0000137 if (std::error_code EC = Dest.mergeValueProfData(Kind, Source)) return EC;
Justin Bogner9e9a0572015-09-29 22:13:58 +0000138 }
139
140 // We keep track of the max function count as we go for simplicity.
141 if (Dest.Counts[0] > MaxFunctionCount)
142 MaxFunctionCount = Dest.Counts[0];
143
144 return instrprof_error::success;
145}
146
147void InstrProfWriter::updateStringTableReferences(InstrProfRecord &I) {
Xinliang David Li2004f002015-11-02 05:08:23 +0000148 I.updateStrings(&StringTable);
Justin Bogner9e9a0572015-09-29 22:13:58 +0000149}
150
151std::error_code InstrProfWriter::addRecord(InstrProfRecord &&I) {
152 updateStringTableReferences(I);
153 auto &ProfileDataMap = FunctionData[I.Name];
154
155 auto Where = ProfileDataMap.find(I.Hash);
156 if (Where == ProfileDataMap.end()) {
Justin Bogner821d7472014-08-01 22:50:07 +0000157 // We've never seen a function with this name and hash, add it.
Justin Bogner9e9a0572015-09-29 22:13:58 +0000158 ProfileDataMap[I.Hash] = I;
159
Justin Bogner821d7472014-08-01 22:50:07 +0000160 // We keep track of the max function count as we go for simplicity.
Justin Bogner9e9a0572015-09-29 22:13:58 +0000161 if (I.Counts[0] > MaxFunctionCount)
162 MaxFunctionCount = I.Counts[0];
Justin Bognera2bfd662014-04-19 23:42:50 +0000163 return instrprof_error::success;
Justin Bognerb9bd7f82014-03-21 17:46:22 +0000164 }
165
Justin Bogner821d7472014-08-01 22:50:07 +0000166 // We're updating a function we've seen before.
Justin Bogner9e9a0572015-09-29 22:13:58 +0000167 return combineInstrProfRecords(Where->second, I, MaxFunctionCount);
Justin Bognerb9bd7f82014-03-21 17:46:22 +0000168}
169
Justin Bogner2b6c5372015-02-18 01:58:17 +0000170std::pair<uint64_t, uint64_t> InstrProfWriter::writeImpl(raw_ostream &OS) {
Justin Bognerb7aa2632014-04-18 21:48:40 +0000171 OnDiskChainedHashTableGenerator<InstrProfRecordTrait> Generator;
Justin Bognerb9bd7f82014-03-21 17:46:22 +0000172
Justin Bognerb7aa2632014-04-18 21:48:40 +0000173 // Populate the hash table generator.
Justin Bogner821d7472014-08-01 22:50:07 +0000174 for (const auto &I : FunctionData)
Justin Bognerfa5b0132014-04-23 18:50:16 +0000175 Generator.insert(I.getKey(), &I.getValue());
Justin Bognerb7aa2632014-04-18 21:48:40 +0000176
177 using namespace llvm::support;
178 endian::Writer<little> LE(OS);
179
180 // Write the header.
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000181 IndexedInstrProf::Header Header;
182 Header.Magic = IndexedInstrProf::Magic;
183 Header.Version = IndexedInstrProf::Version;
184 Header.MaxFunctionCount = MaxFunctionCount;
185 Header.HashType = static_cast<uint64_t>(IndexedInstrProf::HashType);
186 Header.HashOffset = 0;
187 int N = sizeof(IndexedInstrProf::Header) / sizeof(uint64_t);
188
189 // Only write out all the fields execpt 'HashOffset'. We need
190 // to remember the offset of that field to allow back patching
191 // later.
192 for (int I = 0; I < N - 1; I++)
193 LE.write<uint64_t>(reinterpret_cast<uint64_t *>(&Header)[I]);
Justin Bognerb7aa2632014-04-18 21:48:40 +0000194
195 // Save a space to write the hash table start location.
196 uint64_t HashTableStartLoc = OS.tell();
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000197 // Reserve the space for HashOffset field.
Justin Bognerb7aa2632014-04-18 21:48:40 +0000198 LE.write<uint64_t>(0);
199 // Write the hash table.
200 uint64_t HashTableStart = Generator.Emit(OS);
201
Justin Bogner2b6c5372015-02-18 01:58:17 +0000202 return std::make_pair(HashTableStartLoc, HashTableStart);
203}
204
205void InstrProfWriter::write(raw_fd_ostream &OS) {
206 // Write the hash table.
207 auto TableStart = writeImpl(OS);
208
Justin Bognerb7aa2632014-04-18 21:48:40 +0000209 // Go back and fill in the hash table start.
Justin Bogner2b6c5372015-02-18 01:58:17 +0000210 using namespace support;
211 OS.seek(TableStart.first);
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000212 // Now patch the HashOffset field previously reserved.
Justin Bogner2b6c5372015-02-18 01:58:17 +0000213 endian::Writer<little>(OS).write<uint64_t>(TableStart.second);
214}
215
216std::unique_ptr<MemoryBuffer> InstrProfWriter::writeBuffer() {
217 std::string Data;
218 llvm::raw_string_ostream OS(Data);
219 // Write the hash table.
220 auto TableStart = writeImpl(OS);
221 OS.flush();
222
223 // Go back and fill in the hash table start.
224 using namespace support;
225 uint64_t Bytes = endian::byte_swap<uint64_t, little>(TableStart.second);
226 Data.replace(TableStart.first, sizeof(uint64_t), (const char *)&Bytes,
227 sizeof(uint64_t));
228
229 // Return this in an aligned memory buffer.
230 return MemoryBuffer::getMemBufferCopy(Data);
Justin Bognerb9bd7f82014-03-21 17:46:22 +0000231}