blob: 30438ba7962ad0b8ecfd1629d08c58d6d375e7d9 [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
15#include "llvm/ProfileData/SampleProf.h"
Nico Weber432a3882018-04-30 14:59:11 +000016#include "llvm/Config/llvm-config.h"
17#include "llvm/IR/DebugInfoMetadata.h"
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000018#include "llvm/Support/Compiler.h"
19#include "llvm/Support/Debug.h"
Diego Novilloc572e922014-10-30 18:00:06 +000020#include "llvm/Support/ErrorHandling.h"
21#include "llvm/Support/ManagedStatic.h"
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000022#include "llvm/Support/raw_ostream.h"
23#include <string>
24#include <system_error>
Diego Novilloc572e922014-10-30 18:00:06 +000025
26using namespace llvm;
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000027using namespace sampleprof;
Diego Novilloc572e922014-10-30 18:00:06 +000028
29namespace {
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000030
Peter Collingbourne4718f8b2016-05-24 20:13:46 +000031// FIXME: This class is only here to support the transition to llvm::Error. It
32// will be removed once this transition is complete. Clients should prefer to
33// deal with the Error value directly, rather than converting to error_code.
Diego Novilloc572e922014-10-30 18:00:06 +000034class SampleProfErrorCategoryType : public std::error_category {
Reid Kleckner990504e2016-10-19 23:52:38 +000035 const char *name() const noexcept override { return "llvm.sampleprof"; }
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000036
Diego Novilloc572e922014-10-30 18:00:06 +000037 std::string message(int IE) const override {
38 sampleprof_error E = static_cast<sampleprof_error>(IE);
39 switch (E) {
40 case sampleprof_error::success:
41 return "Success";
42 case sampleprof_error::bad_magic:
Nathan Slingerland4f823662015-11-13 03:47:58 +000043 return "Invalid sample profile data (bad magic)";
Diego Novilloc572e922014-10-30 18:00:06 +000044 case sampleprof_error::unsupported_version:
Nathan Slingerland4f823662015-11-13 03:47:58 +000045 return "Unsupported sample profile format version";
Diego Novilloc572e922014-10-30 18:00:06 +000046 case sampleprof_error::too_large:
47 return "Too much profile data";
48 case sampleprof_error::truncated:
49 return "Truncated profile data";
50 case sampleprof_error::malformed:
Nathan Slingerland4f823662015-11-13 03:47:58 +000051 return "Malformed sample profile data";
Diego Novillod5336ae2014-11-01 00:56:55 +000052 case sampleprof_error::unrecognized_format:
Nathan Slingerland4f823662015-11-13 03:47:58 +000053 return "Unrecognized sample profile encoding format";
Diego Novillo760c5a82015-10-13 22:48:46 +000054 case sampleprof_error::unsupported_writing_format:
55 return "Profile encoding format unsupported for writing operations";
56 case sampleprof_error::truncated_name_table:
57 return "Truncated function name table";
Diego Novillo3376a782015-09-17 00:17:24 +000058 case sampleprof_error::not_implemented:
59 return "Unimplemented feature";
Nathan Slingerland48dd0802015-12-16 21:45:43 +000060 case sampleprof_error::counter_overflow:
61 return "Counter overflow";
Diego Novilloc572e922014-10-30 18:00:06 +000062 }
63 llvm_unreachable("A value of sampleprof_error has no message.");
64 }
65};
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000066
67} // end anonymous namespace
Diego Novilloc572e922014-10-30 18:00:06 +000068
69static ManagedStatic<SampleProfErrorCategoryType> ErrorCategory;
70
71const std::error_category &llvm::sampleprof_category() {
72 return *ErrorCategory;
73}
Diego Novillo4b6bdb52015-11-12 17:58:14 +000074
Diego Novilloba920be2015-11-17 19:04:46 +000075void LineLocation::print(raw_ostream &OS) const {
76 OS << LineOffset;
77 if (Discriminator > 0)
78 OS << "." << Discriminator;
79}
80
81raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
82 const LineLocation &Loc) {
83 Loc.print(OS);
84 return OS;
85}
86
Aaron Ballman615eb472017-10-15 14:32:27 +000087#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Kereneb2a2542016-01-29 20:50:44 +000088LLVM_DUMP_METHOD void LineLocation::dump() const { print(dbgs()); }
Matthias Braun8c209aa2017-01-28 02:02:38 +000089#endif
Diego Novilloba920be2015-11-17 19:04:46 +000090
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000091/// Print the sample record to the stream \p OS indented by \p Indent.
Diego Novillo8e415a82015-11-13 20:24:28 +000092void SampleRecord::print(raw_ostream &OS, unsigned Indent) const {
93 OS << NumSamples;
94 if (hasCalls()) {
95 OS << ", calls:";
96 for (const auto &I : getCallTargets())
97 OS << " " << I.first() << ":" << I.second;
98 }
99 OS << "\n";
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 SampleRecord::dump() const { print(dbgs(), 0); }
Matthias Braun8c209aa2017-01-28 02:02:38 +0000104#endif
Diego Novilloba920be2015-11-17 19:04:46 +0000105
106raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
107 const SampleRecord &Sample) {
108 Sample.print(OS, 0);
109 return OS;
110}
111
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000112/// Print the samples collected for a function on stream \p OS.
Diego Novillo4b6bdb52015-11-12 17:58:14 +0000113void FunctionSamples::print(raw_ostream &OS, unsigned Indent) const {
114 OS << TotalSamples << ", " << TotalHeadSamples << ", " << BodySamples.size()
115 << " sampled lines\n";
Diego Novillo8e415a82015-11-13 20:24:28 +0000116
Diego Novillo379cc5e2015-11-19 22:18:30 +0000117 OS.indent(Indent);
Eugene Zelenkoe78d1312017-03-03 01:07:34 +0000118 if (!BodySamples.empty()) {
Diego Novillo379cc5e2015-11-19 22:18:30 +0000119 OS << "Samples collected in the function's body {\n";
120 SampleSorter<LineLocation, SampleRecord> SortedBodySamples(BodySamples);
121 for (const auto &SI : SortedBodySamples.get()) {
122 OS.indent(Indent + 2);
123 OS << SI->first << ": " << SI->second;
124 }
Diego Novillo4b6bdb52015-11-12 17:58:14 +0000125 OS.indent(Indent);
Diego Novillo379cc5e2015-11-19 22:18:30 +0000126 OS << "}\n";
127 } else {
128 OS << "No samples collected in the function's body\n";
Diego Novillo4b6bdb52015-11-12 17:58:14 +0000129 }
Diego Novillo8e415a82015-11-13 20:24:28 +0000130
Diego Novillo379cc5e2015-11-19 22:18:30 +0000131 OS.indent(Indent);
Eugene Zelenkoe78d1312017-03-03 01:07:34 +0000132 if (!CallsiteSamples.empty()) {
Diego Novillo379cc5e2015-11-19 22:18:30 +0000133 OS << "Samples collected in inlined callsites {\n";
Dehao Chen2c7ca9b2017-04-13 19:52:10 +0000134 SampleSorter<LineLocation, FunctionSamplesMap> SortedCallsiteSamples(
Diego Novillo379cc5e2015-11-19 22:18:30 +0000135 CallsiteSamples);
136 for (const auto &CS : SortedCallsiteSamples.get()) {
Dehao Chen2c7ca9b2017-04-13 19:52:10 +0000137 for (const auto &FS : CS->second) {
138 OS.indent(Indent + 2);
139 OS << CS->first << ": inlined callee: " << FS.second.getName() << ": ";
140 FS.second.print(OS, Indent + 4);
141 }
Diego Novillo379cc5e2015-11-19 22:18:30 +0000142 }
143 OS << "}\n";
144 } else {
145 OS << "No inlined callsites in this function\n";
Diego Novillo4b6bdb52015-11-12 17:58:14 +0000146 }
147}
Diego Novilloba920be2015-11-17 19:04:46 +0000148
149raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
150 const FunctionSamples &FS) {
151 FS.print(OS);
152 return OS;
153}
154
Mircea Trofin56950972018-02-22 06:42:57 +0000155unsigned FunctionSamples::getOffset(const DILocation *DIL) {
156 return (DIL->getLine() - DIL->getScope()->getSubprogram()->getLine()) &
157 0xffff;
158}
159
160const FunctionSamples *
161FunctionSamples::findFunctionSamples(const DILocation *DIL) const {
162 assert(DIL);
163 SmallVector<std::pair<LineLocation, StringRef>, 10> S;
164
165 const DILocation *PrevDIL = DIL;
166 for (DIL = DIL->getInlinedAt(); DIL; DIL = DIL->getInlinedAt()) {
167 S.push_back(std::make_pair(
168 LineLocation(getOffset(DIL), DIL->getBaseDiscriminator()),
169 PrevDIL->getScope()->getSubprogram()->getLinkageName()));
170 PrevDIL = DIL;
171 }
172 if (S.size() == 0)
173 return this;
174 const FunctionSamples *FS = this;
175 for (int i = S.size() - 1; i >= 0 && FS != nullptr; i--) {
176 FS = FS->findFunctionSamplesAt(S[i].first, S[i].second);
177 }
178 return FS;
179}
180
Aaron Ballman615eb472017-10-15 14:32:27 +0000181#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Eugene Zelenkoe78d1312017-03-03 01:07:34 +0000182LLVM_DUMP_METHOD void FunctionSamples::dump() const { print(dbgs(), 0); }
Matthias Braun8c209aa2017-01-28 02:02:38 +0000183#endif