blob: 003e8d4d429690e59103cd146df1f50360c87775 [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"
18#include "llvm/Support/Debug.h"
Wei Mi798e59b2019-08-31 02:27:26 +000019#include "llvm/Support/Error.h"
Diego Novilloc572e922014-10-30 18:00:06 +000020#include "llvm/Support/ErrorHandling.h"
Wei Mi798e59b2019-08-31 02:27:26 +000021#include "llvm/Support/LEB128.h"
Diego Novilloc572e922014-10-30 18:00:06 +000022#include "llvm/Support/ManagedStatic.h"
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000023#include "llvm/Support/raw_ostream.h"
24#include <string>
25#include <system_error>
Diego Novilloc572e922014-10-30 18:00:06 +000026
27using namespace llvm;
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000028using namespace sampleprof;
Diego Novilloc572e922014-10-30 18:00:06 +000029
Wei Mi94d44c92018-09-06 22:03:37 +000030namespace llvm {
31namespace sampleprof {
32SampleProfileFormat FunctionSamples::Format;
Wei Mi94d44c92018-09-06 22:03:37 +000033} // namespace sampleprof
34} // namespace llvm
35
Diego Novilloc572e922014-10-30 18:00:06 +000036namespace {
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000037
Peter Collingbourne4718f8b2016-05-24 20:13:46 +000038// FIXME: This class is only here to support the transition to llvm::Error. It
39// will be removed once this transition is complete. Clients should prefer to
40// deal with the Error value directly, rather than converting to error_code.
Diego Novilloc572e922014-10-30 18:00:06 +000041class SampleProfErrorCategoryType : public std::error_category {
Reid Kleckner990504e2016-10-19 23:52:38 +000042 const char *name() const noexcept override { return "llvm.sampleprof"; }
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000043
Diego Novilloc572e922014-10-30 18:00:06 +000044 std::string message(int IE) const override {
45 sampleprof_error E = static_cast<sampleprof_error>(IE);
46 switch (E) {
47 case sampleprof_error::success:
48 return "Success";
49 case sampleprof_error::bad_magic:
Nathan Slingerland4f823662015-11-13 03:47:58 +000050 return "Invalid sample profile data (bad magic)";
Diego Novilloc572e922014-10-30 18:00:06 +000051 case sampleprof_error::unsupported_version:
Nathan Slingerland4f823662015-11-13 03:47:58 +000052 return "Unsupported sample profile format version";
Diego Novilloc572e922014-10-30 18:00:06 +000053 case sampleprof_error::too_large:
54 return "Too much profile data";
55 case sampleprof_error::truncated:
56 return "Truncated profile data";
57 case sampleprof_error::malformed:
Nathan Slingerland4f823662015-11-13 03:47:58 +000058 return "Malformed sample profile data";
Diego Novillod5336ae2014-11-01 00:56:55 +000059 case sampleprof_error::unrecognized_format:
Nathan Slingerland4f823662015-11-13 03:47:58 +000060 return "Unrecognized sample profile encoding format";
Diego Novillo760c5a82015-10-13 22:48:46 +000061 case sampleprof_error::unsupported_writing_format:
62 return "Profile encoding format unsupported for writing operations";
63 case sampleprof_error::truncated_name_table:
64 return "Truncated function name table";
Diego Novillo3376a782015-09-17 00:17:24 +000065 case sampleprof_error::not_implemented:
66 return "Unimplemented feature";
Nathan Slingerland48dd0802015-12-16 21:45:43 +000067 case sampleprof_error::counter_overflow:
68 return "Counter overflow";
Wei Mi6a143252018-09-14 20:52:59 +000069 case sampleprof_error::ostream_seek_unsupported:
70 return "Ostream does not support seek";
Wei Mi798e59b2019-08-31 02:27:26 +000071 case sampleprof_error::compress_failed:
72 return "Compress failure";
73 case sampleprof_error::uncompress_failed:
74 return "Uncompress failure";
75 case sampleprof_error::zlib_unavailable:
76 return "Zlib is unavailable";
Diego Novilloc572e922014-10-30 18:00:06 +000077 }
78 llvm_unreachable("A value of sampleprof_error has no message.");
79 }
80};
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000081
82} // end anonymous namespace
Diego Novilloc572e922014-10-30 18:00:06 +000083
84static ManagedStatic<SampleProfErrorCategoryType> ErrorCategory;
85
86const std::error_category &llvm::sampleprof_category() {
87 return *ErrorCategory;
88}
Diego Novillo4b6bdb52015-11-12 17:58:14 +000089
Diego Novilloba920be2015-11-17 19:04:46 +000090void LineLocation::print(raw_ostream &OS) const {
91 OS << LineOffset;
92 if (Discriminator > 0)
93 OS << "." << Discriminator;
94}
95
96raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
97 const LineLocation &Loc) {
98 Loc.print(OS);
99 return OS;
100}
101
Aaron Ballman615eb472017-10-15 14:32:27 +0000102#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Kereneb2a2542016-01-29 20:50:44 +0000103LLVM_DUMP_METHOD void LineLocation::dump() const { print(dbgs()); }
Matthias Braun8c209aa2017-01-28 02:02:38 +0000104#endif
Diego Novilloba920be2015-11-17 19:04:46 +0000105
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000106/// Print the sample record to the stream \p OS indented by \p Indent.
Diego Novillo8e415a82015-11-13 20:24:28 +0000107void SampleRecord::print(raw_ostream &OS, unsigned Indent) const {
108 OS << NumSamples;
109 if (hasCalls()) {
110 OS << ", calls:";
Wenlei He5adace32019-08-20 20:52:00 +0000111 for (const auto &I : getSortedCallTargets())
112 OS << " " << I.first << ":" << I.second;
Diego Novillo8e415a82015-11-13 20:24:28 +0000113 }
114 OS << "\n";
115}
116
Aaron Ballman615eb472017-10-15 14:32:27 +0000117#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Kereneb2a2542016-01-29 20:50:44 +0000118LLVM_DUMP_METHOD void SampleRecord::dump() const { print(dbgs(), 0); }
Matthias Braun8c209aa2017-01-28 02:02:38 +0000119#endif
Diego Novilloba920be2015-11-17 19:04:46 +0000120
121raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
122 const SampleRecord &Sample) {
123 Sample.print(OS, 0);
124 return OS;
125}
126
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000127/// Print the samples collected for a function on stream \p OS.
Diego Novillo4b6bdb52015-11-12 17:58:14 +0000128void FunctionSamples::print(raw_ostream &OS, unsigned Indent) const {
129 OS << TotalSamples << ", " << TotalHeadSamples << ", " << BodySamples.size()
130 << " sampled lines\n";
Diego Novillo8e415a82015-11-13 20:24:28 +0000131
Diego Novillo379cc5e2015-11-19 22:18:30 +0000132 OS.indent(Indent);
Eugene Zelenkoe78d1312017-03-03 01:07:34 +0000133 if (!BodySamples.empty()) {
Diego Novillo379cc5e2015-11-19 22:18:30 +0000134 OS << "Samples collected in the function's body {\n";
135 SampleSorter<LineLocation, SampleRecord> SortedBodySamples(BodySamples);
136 for (const auto &SI : SortedBodySamples.get()) {
137 OS.indent(Indent + 2);
138 OS << SI->first << ": " << SI->second;
139 }
Diego Novillo4b6bdb52015-11-12 17:58:14 +0000140 OS.indent(Indent);
Diego Novillo379cc5e2015-11-19 22:18:30 +0000141 OS << "}\n";
142 } else {
143 OS << "No samples collected in the function's body\n";
Diego Novillo4b6bdb52015-11-12 17:58:14 +0000144 }
Diego Novillo8e415a82015-11-13 20:24:28 +0000145
Diego Novillo379cc5e2015-11-19 22:18:30 +0000146 OS.indent(Indent);
Eugene Zelenkoe78d1312017-03-03 01:07:34 +0000147 if (!CallsiteSamples.empty()) {
Diego Novillo379cc5e2015-11-19 22:18:30 +0000148 OS << "Samples collected in inlined callsites {\n";
Dehao Chen2c7ca9b2017-04-13 19:52:10 +0000149 SampleSorter<LineLocation, FunctionSamplesMap> SortedCallsiteSamples(
Diego Novillo379cc5e2015-11-19 22:18:30 +0000150 CallsiteSamples);
151 for (const auto &CS : SortedCallsiteSamples.get()) {
Dehao Chen2c7ca9b2017-04-13 19:52:10 +0000152 for (const auto &FS : CS->second) {
153 OS.indent(Indent + 2);
154 OS << CS->first << ": inlined callee: " << FS.second.getName() << ": ";
155 FS.second.print(OS, Indent + 4);
156 }
Diego Novillo379cc5e2015-11-19 22:18:30 +0000157 }
Wenlei Heb3342e12019-10-07 16:30:31 +0000158 OS.indent(Indent);
Diego Novillo379cc5e2015-11-19 22:18:30 +0000159 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
Wei Mib5237902019-10-07 16:12:37 +0000201std::error_code ProfileSymbolList::read(const uint8_t *Data,
202 uint64_t ListSize) {
Wei Mi798e59b2019-08-31 02:27:26 +0000203 const char *ListStart = reinterpret_cast<const char *>(Data);
Wei Mi798e59b2019-08-31 02:27:26 +0000204 uint64_t Size = 0;
Wei Mib5237902019-10-07 16:12:37 +0000205 while (Size < ListSize) {
Wei Mi798e59b2019-08-31 02:27:26 +0000206 StringRef Str(ListStart + Size);
207 add(Str);
208 Size += Str.size() + 1;
209 }
Wei Mib5237902019-10-07 16:12:37 +0000210 if (Size != ListSize)
211 return sampleprof_error::malformed;
Wei Mi798e59b2019-08-31 02:27:26 +0000212 return sampleprof_error::success;
213}
214
215std::error_code ProfileSymbolList::write(raw_ostream &OS) {
Wei Mib5237902019-10-07 16:12:37 +0000216 // Sort the symbols before output. If doing compression.
217 // It will make the compression much more effective.
Wei Mi798e59b2019-08-31 02:27:26 +0000218 std::vector<StringRef> SortedList;
219 SortedList.insert(SortedList.begin(), Syms.begin(), Syms.end());
220 llvm::sort(SortedList);
221
Wei Mib5237902019-10-07 16:12:37 +0000222 std::string OutputString;
Wei Mi798e59b2019-08-31 02:27:26 +0000223 for (auto &Sym : SortedList) {
Wei Mib5237902019-10-07 16:12:37 +0000224 OutputString.append(Sym.str());
225 OutputString.append(1, '\0');
Wei Mi798e59b2019-08-31 02:27:26 +0000226 }
227
Wei Mib5237902019-10-07 16:12:37 +0000228 OS << OutputString;
Wei Mi798e59b2019-08-31 02:27:26 +0000229 return sampleprof_error::success;
230}
231
232void ProfileSymbolList::dump(raw_ostream &OS) const {
233 OS << "======== Dump profile symbol list ========\n";
234 std::vector<StringRef> SortedList;
235 SortedList.insert(SortedList.begin(), Syms.begin(), Syms.end());
236 llvm::sort(SortedList);
237
238 for (auto &Sym : SortedList)
239 OS << Sym << "\n";
240}