blob: e94848df24e20fa5c7075580364c0ba7ed72b0fc [file] [log] [blame]
Diego Novilloc572e922014-10-30 18:00:06 +00001//=-- SampleProf.cpp - Sample profiling format support --------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Diego Novilloc572e922014-10-30 18:00:06 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file contains common definitions used in the reading and writing of
10// sample profile data.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/ProfileData/SampleProf.h"
Nico Weber432a3882018-04-30 14:59:11 +000015#include "llvm/Config/llvm-config.h"
16#include "llvm/IR/DebugInfoMetadata.h"
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000017#include "llvm/Support/Compiler.h"
Wei Mi798e59b2019-08-31 02:27:26 +000018#include "llvm/Support/Compression.h"
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000019#include "llvm/Support/Debug.h"
Wei Mi798e59b2019-08-31 02:27:26 +000020#include "llvm/Support/Error.h"
Diego Novilloc572e922014-10-30 18:00:06 +000021#include "llvm/Support/ErrorHandling.h"
Wei Mi798e59b2019-08-31 02:27:26 +000022#include "llvm/Support/LEB128.h"
Diego Novilloc572e922014-10-30 18:00:06 +000023#include "llvm/Support/ManagedStatic.h"
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000024#include "llvm/Support/raw_ostream.h"
25#include <string>
26#include <system_error>
Diego Novilloc572e922014-10-30 18:00:06 +000027
28using namespace llvm;
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000029using namespace sampleprof;
Diego Novilloc572e922014-10-30 18:00:06 +000030
Wei Mi94d44c92018-09-06 22:03:37 +000031namespace llvm {
32namespace sampleprof {
33SampleProfileFormat FunctionSamples::Format;
Wei Mi94d44c92018-09-06 22:03:37 +000034} // namespace sampleprof
35} // namespace llvm
36
Diego Novilloc572e922014-10-30 18:00:06 +000037namespace {
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000038
Peter Collingbourne4718f8b2016-05-24 20:13:46 +000039// FIXME: This class is only here to support the transition to llvm::Error. It
40// will be removed once this transition is complete. Clients should prefer to
41// deal with the Error value directly, rather than converting to error_code.
Diego Novilloc572e922014-10-30 18:00:06 +000042class SampleProfErrorCategoryType : public std::error_category {
Reid Kleckner990504e2016-10-19 23:52:38 +000043 const char *name() const noexcept override { return "llvm.sampleprof"; }
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000044
Diego Novilloc572e922014-10-30 18:00:06 +000045 std::string message(int IE) const override {
46 sampleprof_error E = static_cast<sampleprof_error>(IE);
47 switch (E) {
48 case sampleprof_error::success:
49 return "Success";
50 case sampleprof_error::bad_magic:
Nathan Slingerland4f823662015-11-13 03:47:58 +000051 return "Invalid sample profile data (bad magic)";
Diego Novilloc572e922014-10-30 18:00:06 +000052 case sampleprof_error::unsupported_version:
Nathan Slingerland4f823662015-11-13 03:47:58 +000053 return "Unsupported sample profile format version";
Diego Novilloc572e922014-10-30 18:00:06 +000054 case sampleprof_error::too_large:
55 return "Too much profile data";
56 case sampleprof_error::truncated:
57 return "Truncated profile data";
58 case sampleprof_error::malformed:
Nathan Slingerland4f823662015-11-13 03:47:58 +000059 return "Malformed sample profile data";
Diego Novillod5336ae2014-11-01 00:56:55 +000060 case sampleprof_error::unrecognized_format:
Nathan Slingerland4f823662015-11-13 03:47:58 +000061 return "Unrecognized sample profile encoding format";
Diego Novillo760c5a82015-10-13 22:48:46 +000062 case sampleprof_error::unsupported_writing_format:
63 return "Profile encoding format unsupported for writing operations";
64 case sampleprof_error::truncated_name_table:
65 return "Truncated function name table";
Diego Novillo3376a782015-09-17 00:17:24 +000066 case sampleprof_error::not_implemented:
67 return "Unimplemented feature";
Nathan Slingerland48dd0802015-12-16 21:45:43 +000068 case sampleprof_error::counter_overflow:
69 return "Counter overflow";
Wei Mi6a143252018-09-14 20:52:59 +000070 case sampleprof_error::ostream_seek_unsupported:
71 return "Ostream does not support seek";
Wei Mi798e59b2019-08-31 02:27:26 +000072 case sampleprof_error::compress_failed:
73 return "Compress failure";
74 case sampleprof_error::uncompress_failed:
75 return "Uncompress failure";
76 case sampleprof_error::zlib_unavailable:
77 return "Zlib is unavailable";
Diego Novilloc572e922014-10-30 18:00:06 +000078 }
79 llvm_unreachable("A value of sampleprof_error has no message.");
80 }
81};
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000082
83} // end anonymous namespace
Diego Novilloc572e922014-10-30 18:00:06 +000084
85static ManagedStatic<SampleProfErrorCategoryType> ErrorCategory;
86
87const std::error_category &llvm::sampleprof_category() {
88 return *ErrorCategory;
89}
Diego Novillo4b6bdb52015-11-12 17:58:14 +000090
Diego Novilloba920be2015-11-17 19:04:46 +000091void LineLocation::print(raw_ostream &OS) const {
92 OS << LineOffset;
93 if (Discriminator > 0)
94 OS << "." << Discriminator;
95}
96
97raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
98 const LineLocation &Loc) {
99 Loc.print(OS);
100 return OS;
101}
102
Aaron Ballman615eb472017-10-15 14:32:27 +0000103#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Kereneb2a2542016-01-29 20:50:44 +0000104LLVM_DUMP_METHOD void LineLocation::dump() const { print(dbgs()); }
Matthias Braun8c209aa2017-01-28 02:02:38 +0000105#endif
Diego Novilloba920be2015-11-17 19:04:46 +0000106
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000107/// Print the sample record to the stream \p OS indented by \p Indent.
Diego Novillo8e415a82015-11-13 20:24:28 +0000108void SampleRecord::print(raw_ostream &OS, unsigned Indent) const {
109 OS << NumSamples;
110 if (hasCalls()) {
111 OS << ", calls:";
Wenlei He5adace32019-08-20 20:52:00 +0000112 for (const auto &I : getSortedCallTargets())
113 OS << " " << I.first << ":" << I.second;
Diego Novillo8e415a82015-11-13 20:24:28 +0000114 }
115 OS << "\n";
116}
117
Aaron Ballman615eb472017-10-15 14:32:27 +0000118#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Kereneb2a2542016-01-29 20:50:44 +0000119LLVM_DUMP_METHOD void SampleRecord::dump() const { print(dbgs(), 0); }
Matthias Braun8c209aa2017-01-28 02:02:38 +0000120#endif
Diego Novilloba920be2015-11-17 19:04:46 +0000121
122raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
123 const SampleRecord &Sample) {
124 Sample.print(OS, 0);
125 return OS;
126}
127
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000128/// Print the samples collected for a function on stream \p OS.
Diego Novillo4b6bdb52015-11-12 17:58:14 +0000129void FunctionSamples::print(raw_ostream &OS, unsigned Indent) const {
130 OS << TotalSamples << ", " << TotalHeadSamples << ", " << BodySamples.size()
131 << " sampled lines\n";
Diego Novillo8e415a82015-11-13 20:24:28 +0000132
Diego Novillo379cc5e2015-11-19 22:18:30 +0000133 OS.indent(Indent);
Eugene Zelenkoe78d1312017-03-03 01:07:34 +0000134 if (!BodySamples.empty()) {
Diego Novillo379cc5e2015-11-19 22:18:30 +0000135 OS << "Samples collected in the function's body {\n";
136 SampleSorter<LineLocation, SampleRecord> SortedBodySamples(BodySamples);
137 for (const auto &SI : SortedBodySamples.get()) {
138 OS.indent(Indent + 2);
139 OS << SI->first << ": " << SI->second;
140 }
Diego Novillo4b6bdb52015-11-12 17:58:14 +0000141 OS.indent(Indent);
Diego Novillo379cc5e2015-11-19 22:18:30 +0000142 OS << "}\n";
143 } else {
144 OS << "No samples collected in the function's body\n";
Diego Novillo4b6bdb52015-11-12 17:58:14 +0000145 }
Diego Novillo8e415a82015-11-13 20:24:28 +0000146
Diego Novillo379cc5e2015-11-19 22:18:30 +0000147 OS.indent(Indent);
Eugene Zelenkoe78d1312017-03-03 01:07:34 +0000148 if (!CallsiteSamples.empty()) {
Diego Novillo379cc5e2015-11-19 22:18:30 +0000149 OS << "Samples collected in inlined callsites {\n";
Dehao Chen2c7ca9b2017-04-13 19:52:10 +0000150 SampleSorter<LineLocation, FunctionSamplesMap> SortedCallsiteSamples(
Diego Novillo379cc5e2015-11-19 22:18:30 +0000151 CallsiteSamples);
152 for (const auto &CS : SortedCallsiteSamples.get()) {
Dehao Chen2c7ca9b2017-04-13 19:52:10 +0000153 for (const auto &FS : CS->second) {
154 OS.indent(Indent + 2);
155 OS << CS->first << ": inlined callee: " << FS.second.getName() << ": ";
156 FS.second.print(OS, Indent + 4);
157 }
Diego Novillo379cc5e2015-11-19 22:18:30 +0000158 }
159 OS << "}\n";
160 } else {
161 OS << "No inlined callsites in this function\n";
Diego Novillo4b6bdb52015-11-12 17:58:14 +0000162 }
163}
Diego Novilloba920be2015-11-17 19:04:46 +0000164
165raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
166 const FunctionSamples &FS) {
167 FS.print(OS);
168 return OS;
169}
170
Mircea Trofin56950972018-02-22 06:42:57 +0000171unsigned FunctionSamples::getOffset(const DILocation *DIL) {
172 return (DIL->getLine() - DIL->getScope()->getSubprogram()->getLine()) &
173 0xffff;
174}
175
176const FunctionSamples *
177FunctionSamples::findFunctionSamples(const DILocation *DIL) const {
178 assert(DIL);
179 SmallVector<std::pair<LineLocation, StringRef>, 10> S;
180
181 const DILocation *PrevDIL = DIL;
182 for (DIL = DIL->getInlinedAt(); DIL; DIL = DIL->getInlinedAt()) {
183 S.push_back(std::make_pair(
184 LineLocation(getOffset(DIL), DIL->getBaseDiscriminator()),
185 PrevDIL->getScope()->getSubprogram()->getLinkageName()));
186 PrevDIL = DIL;
187 }
188 if (S.size() == 0)
189 return this;
190 const FunctionSamples *FS = this;
191 for (int i = S.size() - 1; i >= 0 && FS != nullptr; i--) {
192 FS = FS->findFunctionSamplesAt(S[i].first, S[i].second);
193 }
194 return FS;
195}
196
Aaron Ballman615eb472017-10-15 14:32:27 +0000197#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Eugene Zelenkoe78d1312017-03-03 01:07:34 +0000198LLVM_DUMP_METHOD void FunctionSamples::dump() const { print(dbgs(), 0); }
Matthias Braun8c209aa2017-01-28 02:02:38 +0000199#endif
Wei Mi798e59b2019-08-31 02:27:26 +0000200
201std::error_code ProfileSymbolList::read(uint64_t CompressSize,
202 uint64_t UncompressSize,
203 const uint8_t *Data) {
204 const char *ListStart = reinterpret_cast<const char *>(Data);
205 // CompressSize being non-zero means the profile is compressed and
206 // needs to be uncompressed first.
207 if (CompressSize) {
208 if (!llvm::zlib::isAvailable())
209 return sampleprof_error::zlib_unavailable;
210
211 StringRef CompressedStrings(reinterpret_cast<const char *>(Data),
212 CompressSize);
213 char *Buffer = Allocator.Allocate<char>(UncompressSize);
Wei Mi198009a2019-08-31 03:17:49 +0000214 size_t UCSize = UncompressSize;
215 llvm::Error E = zlib::uncompress(CompressedStrings, Buffer, UCSize);
Wei Mi798e59b2019-08-31 02:27:26 +0000216 if (E)
217 return sampleprof_error::uncompress_failed;
218 ListStart = Buffer;
219 }
220
221 uint64_t Size = 0;
222 while (Size < UncompressSize) {
223 StringRef Str(ListStart + Size);
224 add(Str);
225 Size += Str.size() + 1;
226 }
227 return sampleprof_error::success;
228}
229
230std::error_code ProfileSymbolList::write(raw_ostream &OS) {
231 // Sort the symbols before doing compression. It will make the
232 // compression much more effective.
233 std::vector<StringRef> SortedList;
234 SortedList.insert(SortedList.begin(), Syms.begin(), Syms.end());
235 llvm::sort(SortedList);
236
237 std::string UncompressedStrings;
238 for (auto &Sym : SortedList) {
239 UncompressedStrings.append(Sym.str());
240 UncompressedStrings.append(1, '\0');
241 }
242
243 if (ToCompress) {
244 if (!llvm::zlib::isAvailable())
245 return sampleprof_error::zlib_unavailable;
246 SmallString<128> CompressedStrings;
247 llvm::Error E = zlib::compress(UncompressedStrings, CompressedStrings,
248 zlib::BestSizeCompression);
249 if (E)
250 return sampleprof_error::compress_failed;
251 encodeULEB128(UncompressedStrings.size(), OS);
252 encodeULEB128(CompressedStrings.size(), OS);
253 OS << CompressedStrings.str();
254 } else {
255 encodeULEB128(UncompressedStrings.size(), OS);
256 // If profile symbol list is not compressed, we will still save
257 // a compressed size value, but the value of the size is 0.
258 encodeULEB128(0, OS);
259 OS << UncompressedStrings;
260 }
261 return sampleprof_error::success;
262}
263
264void ProfileSymbolList::dump(raw_ostream &OS) const {
265 OS << "======== Dump profile symbol list ========\n";
266 std::vector<StringRef> SortedList;
267 SortedList.insert(SortedList.begin(), Syms.begin(), Syms.end());
268 llvm::sort(SortedList);
269
270 for (auto &Sym : SortedList)
271 OS << Sym << "\n";
272}