blob: 85a4197c10ab9a2669c6511c85ce87ed23261710 [file] [log] [blame]
Diego Novilloc572e922014-10-30 18:00:06 +00001//=-- SampleProf.cpp - Sample profiling format support --------------------===//
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 common definitions used in the reading and writing of
11// sample profile data.
12//
13//===----------------------------------------------------------------------===//
14
Mircea Trofin56950972018-02-22 06:42:57 +000015#include "llvm/IR/DebugInfoMetadata.h"
Diego Novilloc572e922014-10-30 18:00:06 +000016#include "llvm/ProfileData/SampleProf.h"
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000017#include "llvm/Support/Compiler.h"
18#include "llvm/Support/Debug.h"
Diego Novilloc572e922014-10-30 18:00:06 +000019#include "llvm/Support/ErrorHandling.h"
20#include "llvm/Support/ManagedStatic.h"
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000021#include "llvm/Support/raw_ostream.h"
22#include <string>
23#include <system_error>
Diego Novilloc572e922014-10-30 18:00:06 +000024
25using namespace llvm;
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000026using namespace sampleprof;
Diego Novilloc572e922014-10-30 18:00:06 +000027
28namespace {
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000029
Peter Collingbourne4718f8b2016-05-24 20:13:46 +000030// FIXME: This class is only here to support the transition to llvm::Error. It
31// will be removed once this transition is complete. Clients should prefer to
32// deal with the Error value directly, rather than converting to error_code.
Diego Novilloc572e922014-10-30 18:00:06 +000033class SampleProfErrorCategoryType : public std::error_category {
Reid Kleckner990504e2016-10-19 23:52:38 +000034 const char *name() const noexcept override { return "llvm.sampleprof"; }
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000035
Diego Novilloc572e922014-10-30 18:00:06 +000036 std::string message(int IE) const override {
37 sampleprof_error E = static_cast<sampleprof_error>(IE);
38 switch (E) {
39 case sampleprof_error::success:
40 return "Success";
41 case sampleprof_error::bad_magic:
Nathan Slingerland4f823662015-11-13 03:47:58 +000042 return "Invalid sample profile data (bad magic)";
Diego Novilloc572e922014-10-30 18:00:06 +000043 case sampleprof_error::unsupported_version:
Nathan Slingerland4f823662015-11-13 03:47:58 +000044 return "Unsupported sample profile format version";
Diego Novilloc572e922014-10-30 18:00:06 +000045 case sampleprof_error::too_large:
46 return "Too much profile data";
47 case sampleprof_error::truncated:
48 return "Truncated profile data";
49 case sampleprof_error::malformed:
Nathan Slingerland4f823662015-11-13 03:47:58 +000050 return "Malformed sample profile data";
Diego Novillod5336ae2014-11-01 00:56:55 +000051 case sampleprof_error::unrecognized_format:
Nathan Slingerland4f823662015-11-13 03:47:58 +000052 return "Unrecognized sample profile encoding format";
Diego Novillo760c5a82015-10-13 22:48:46 +000053 case sampleprof_error::unsupported_writing_format:
54 return "Profile encoding format unsupported for writing operations";
55 case sampleprof_error::truncated_name_table:
56 return "Truncated function name table";
Diego Novillo3376a782015-09-17 00:17:24 +000057 case sampleprof_error::not_implemented:
58 return "Unimplemented feature";
Nathan Slingerland48dd0802015-12-16 21:45:43 +000059 case sampleprof_error::counter_overflow:
60 return "Counter overflow";
Diego Novilloc572e922014-10-30 18:00:06 +000061 }
62 llvm_unreachable("A value of sampleprof_error has no message.");
63 }
64};
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000065
66} // end anonymous namespace
Diego Novilloc572e922014-10-30 18:00:06 +000067
68static ManagedStatic<SampleProfErrorCategoryType> ErrorCategory;
69
70const std::error_category &llvm::sampleprof_category() {
71 return *ErrorCategory;
72}
Diego Novillo4b6bdb52015-11-12 17:58:14 +000073
Diego Novilloba920be2015-11-17 19:04:46 +000074void LineLocation::print(raw_ostream &OS) const {
75 OS << LineOffset;
76 if (Discriminator > 0)
77 OS << "." << Discriminator;
78}
79
80raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
81 const LineLocation &Loc) {
82 Loc.print(OS);
83 return OS;
84}
85
Aaron Ballman615eb472017-10-15 14:32:27 +000086#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Kereneb2a2542016-01-29 20:50:44 +000087LLVM_DUMP_METHOD void LineLocation::dump() const { print(dbgs()); }
Matthias Braun8c209aa2017-01-28 02:02:38 +000088#endif
Diego Novilloba920be2015-11-17 19:04:46 +000089
Diego Novillo8e415a82015-11-13 20:24:28 +000090/// \brief Print the sample record to the stream \p OS indented by \p Indent.
91void SampleRecord::print(raw_ostream &OS, unsigned Indent) const {
92 OS << NumSamples;
93 if (hasCalls()) {
94 OS << ", calls:";
95 for (const auto &I : getCallTargets())
96 OS << " " << I.first() << ":" << I.second;
97 }
98 OS << "\n";
99}
100
Aaron Ballman615eb472017-10-15 14:32:27 +0000101#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Kereneb2a2542016-01-29 20:50:44 +0000102LLVM_DUMP_METHOD void SampleRecord::dump() const { print(dbgs(), 0); }
Matthias Braun8c209aa2017-01-28 02:02:38 +0000103#endif
Diego Novilloba920be2015-11-17 19:04:46 +0000104
105raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
106 const SampleRecord &Sample) {
107 Sample.print(OS, 0);
108 return OS;
109}
110
Diego Novillo4b6bdb52015-11-12 17:58:14 +0000111/// \brief Print the samples collected for a function on stream \p OS.
Diego Novillo4b6bdb52015-11-12 17:58:14 +0000112void FunctionSamples::print(raw_ostream &OS, unsigned Indent) const {
113 OS << TotalSamples << ", " << TotalHeadSamples << ", " << BodySamples.size()
114 << " sampled lines\n";
Diego Novillo8e415a82015-11-13 20:24:28 +0000115
Diego Novillo379cc5e2015-11-19 22:18:30 +0000116 OS.indent(Indent);
Eugene Zelenkoe78d1312017-03-03 01:07:34 +0000117 if (!BodySamples.empty()) {
Diego Novillo379cc5e2015-11-19 22:18:30 +0000118 OS << "Samples collected in the function's body {\n";
119 SampleSorter<LineLocation, SampleRecord> SortedBodySamples(BodySamples);
120 for (const auto &SI : SortedBodySamples.get()) {
121 OS.indent(Indent + 2);
122 OS << SI->first << ": " << SI->second;
123 }
Diego Novillo4b6bdb52015-11-12 17:58:14 +0000124 OS.indent(Indent);
Diego Novillo379cc5e2015-11-19 22:18:30 +0000125 OS << "}\n";
126 } else {
127 OS << "No samples collected in the function's body\n";
Diego Novillo4b6bdb52015-11-12 17:58:14 +0000128 }
Diego Novillo8e415a82015-11-13 20:24:28 +0000129
Diego Novillo379cc5e2015-11-19 22:18:30 +0000130 OS.indent(Indent);
Eugene Zelenkoe78d1312017-03-03 01:07:34 +0000131 if (!CallsiteSamples.empty()) {
Diego Novillo379cc5e2015-11-19 22:18:30 +0000132 OS << "Samples collected in inlined callsites {\n";
Dehao Chen2c7ca9b2017-04-13 19:52:10 +0000133 SampleSorter<LineLocation, FunctionSamplesMap> SortedCallsiteSamples(
Diego Novillo379cc5e2015-11-19 22:18:30 +0000134 CallsiteSamples);
135 for (const auto &CS : SortedCallsiteSamples.get()) {
Dehao Chen2c7ca9b2017-04-13 19:52:10 +0000136 for (const auto &FS : CS->second) {
137 OS.indent(Indent + 2);
138 OS << CS->first << ": inlined callee: " << FS.second.getName() << ": ";
139 FS.second.print(OS, Indent + 4);
140 }
Diego Novillo379cc5e2015-11-19 22:18:30 +0000141 }
142 OS << "}\n";
143 } else {
144 OS << "No inlined callsites in this function\n";
Diego Novillo4b6bdb52015-11-12 17:58:14 +0000145 }
146}
Diego Novilloba920be2015-11-17 19:04:46 +0000147
148raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
149 const FunctionSamples &FS) {
150 FS.print(OS);
151 return OS;
152}
153
Mircea Trofin56950972018-02-22 06:42:57 +0000154unsigned FunctionSamples::getOffset(const DILocation *DIL) {
155 return (DIL->getLine() - DIL->getScope()->getSubprogram()->getLine()) &
156 0xffff;
157}
158
159const FunctionSamples *
160FunctionSamples::findFunctionSamples(const DILocation *DIL) const {
161 assert(DIL);
162 SmallVector<std::pair<LineLocation, StringRef>, 10> S;
163
164 const DILocation *PrevDIL = DIL;
165 for (DIL = DIL->getInlinedAt(); DIL; DIL = DIL->getInlinedAt()) {
166 S.push_back(std::make_pair(
167 LineLocation(getOffset(DIL), DIL->getBaseDiscriminator()),
168 PrevDIL->getScope()->getSubprogram()->getLinkageName()));
169 PrevDIL = DIL;
170 }
171 if (S.size() == 0)
172 return this;
173 const FunctionSamples *FS = this;
174 for (int i = S.size() - 1; i >= 0 && FS != nullptr; i--) {
175 FS = FS->findFunctionSamplesAt(S[i].first, S[i].second);
176 }
177 return FS;
178}
179
Aaron Ballman615eb472017-10-15 14:32:27 +0000180#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Eugene Zelenkoe78d1312017-03-03 01:07:34 +0000181LLVM_DUMP_METHOD void FunctionSamples::dump() const { print(dbgs(), 0); }
Matthias Braun8c209aa2017-01-28 02:02:38 +0000182#endif