blob: fcf4d78a080ef309ef354afbb4ffa9b965b1c806 [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"
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
Wei Mi94d44c92018-09-06 22:03:37 +000028namespace llvm {
29namespace sampleprof {
30SampleProfileFormat FunctionSamples::Format;
Wei Mi94d44c92018-09-06 22:03:37 +000031} // namespace sampleprof
32} // namespace llvm
33
Diego Novilloc572e922014-10-30 18:00:06 +000034namespace {
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000035
Peter Collingbourne4718f8b2016-05-24 20:13:46 +000036// FIXME: This class is only here to support the transition to llvm::Error. It
37// will be removed once this transition is complete. Clients should prefer to
38// deal with the Error value directly, rather than converting to error_code.
Diego Novilloc572e922014-10-30 18:00:06 +000039class SampleProfErrorCategoryType : public std::error_category {
Reid Kleckner990504e2016-10-19 23:52:38 +000040 const char *name() const noexcept override { return "llvm.sampleprof"; }
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000041
Diego Novilloc572e922014-10-30 18:00:06 +000042 std::string message(int IE) const override {
43 sampleprof_error E = static_cast<sampleprof_error>(IE);
44 switch (E) {
45 case sampleprof_error::success:
46 return "Success";
47 case sampleprof_error::bad_magic:
Nathan Slingerland4f823662015-11-13 03:47:58 +000048 return "Invalid sample profile data (bad magic)";
Diego Novilloc572e922014-10-30 18:00:06 +000049 case sampleprof_error::unsupported_version:
Nathan Slingerland4f823662015-11-13 03:47:58 +000050 return "Unsupported sample profile format version";
Diego Novilloc572e922014-10-30 18:00:06 +000051 case sampleprof_error::too_large:
52 return "Too much profile data";
53 case sampleprof_error::truncated:
54 return "Truncated profile data";
55 case sampleprof_error::malformed:
Nathan Slingerland4f823662015-11-13 03:47:58 +000056 return "Malformed sample profile data";
Diego Novillod5336ae2014-11-01 00:56:55 +000057 case sampleprof_error::unrecognized_format:
Nathan Slingerland4f823662015-11-13 03:47:58 +000058 return "Unrecognized sample profile encoding format";
Diego Novillo760c5a82015-10-13 22:48:46 +000059 case sampleprof_error::unsupported_writing_format:
60 return "Profile encoding format unsupported for writing operations";
61 case sampleprof_error::truncated_name_table:
62 return "Truncated function name table";
Diego Novillo3376a782015-09-17 00:17:24 +000063 case sampleprof_error::not_implemented:
64 return "Unimplemented feature";
Nathan Slingerland48dd0802015-12-16 21:45:43 +000065 case sampleprof_error::counter_overflow:
66 return "Counter overflow";
Wei Mi6a143252018-09-14 20:52:59 +000067 case sampleprof_error::ostream_seek_unsupported:
68 return "Ostream does not support seek";
Diego Novilloc572e922014-10-30 18:00:06 +000069 }
70 llvm_unreachable("A value of sampleprof_error has no message.");
71 }
72};
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000073
74} // end anonymous namespace
Diego Novilloc572e922014-10-30 18:00:06 +000075
76static ManagedStatic<SampleProfErrorCategoryType> ErrorCategory;
77
78const std::error_category &llvm::sampleprof_category() {
79 return *ErrorCategory;
80}
Diego Novillo4b6bdb52015-11-12 17:58:14 +000081
Diego Novilloba920be2015-11-17 19:04:46 +000082void LineLocation::print(raw_ostream &OS) const {
83 OS << LineOffset;
84 if (Discriminator > 0)
85 OS << "." << Discriminator;
86}
87
88raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
89 const LineLocation &Loc) {
90 Loc.print(OS);
91 return OS;
92}
93
Aaron Ballman615eb472017-10-15 14:32:27 +000094#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Kereneb2a2542016-01-29 20:50:44 +000095LLVM_DUMP_METHOD void LineLocation::dump() const { print(dbgs()); }
Matthias Braun8c209aa2017-01-28 02:02:38 +000096#endif
Diego Novilloba920be2015-11-17 19:04:46 +000097
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000098/// Print the sample record to the stream \p OS indented by \p Indent.
Diego Novillo8e415a82015-11-13 20:24:28 +000099void SampleRecord::print(raw_ostream &OS, unsigned Indent) const {
100 OS << NumSamples;
101 if (hasCalls()) {
102 OS << ", calls:";
103 for (const auto &I : getCallTargets())
104 OS << " " << I.first() << ":" << I.second;
105 }
106 OS << "\n";
107}
108
Aaron Ballman615eb472017-10-15 14:32:27 +0000109#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Kereneb2a2542016-01-29 20:50:44 +0000110LLVM_DUMP_METHOD void SampleRecord::dump() const { print(dbgs(), 0); }
Matthias Braun8c209aa2017-01-28 02:02:38 +0000111#endif
Diego Novilloba920be2015-11-17 19:04:46 +0000112
113raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
114 const SampleRecord &Sample) {
115 Sample.print(OS, 0);
116 return OS;
117}
118
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000119/// Print the samples collected for a function on stream \p OS.
Diego Novillo4b6bdb52015-11-12 17:58:14 +0000120void FunctionSamples::print(raw_ostream &OS, unsigned Indent) const {
121 OS << TotalSamples << ", " << TotalHeadSamples << ", " << BodySamples.size()
122 << " sampled lines\n";
Diego Novillo8e415a82015-11-13 20:24:28 +0000123
Diego Novillo379cc5e2015-11-19 22:18:30 +0000124 OS.indent(Indent);
Eugene Zelenkoe78d1312017-03-03 01:07:34 +0000125 if (!BodySamples.empty()) {
Diego Novillo379cc5e2015-11-19 22:18:30 +0000126 OS << "Samples collected in the function's body {\n";
127 SampleSorter<LineLocation, SampleRecord> SortedBodySamples(BodySamples);
128 for (const auto &SI : SortedBodySamples.get()) {
129 OS.indent(Indent + 2);
130 OS << SI->first << ": " << SI->second;
131 }
Diego Novillo4b6bdb52015-11-12 17:58:14 +0000132 OS.indent(Indent);
Diego Novillo379cc5e2015-11-19 22:18:30 +0000133 OS << "}\n";
134 } else {
135 OS << "No samples collected in the function's body\n";
Diego Novillo4b6bdb52015-11-12 17:58:14 +0000136 }
Diego Novillo8e415a82015-11-13 20:24:28 +0000137
Diego Novillo379cc5e2015-11-19 22:18:30 +0000138 OS.indent(Indent);
Eugene Zelenkoe78d1312017-03-03 01:07:34 +0000139 if (!CallsiteSamples.empty()) {
Diego Novillo379cc5e2015-11-19 22:18:30 +0000140 OS << "Samples collected in inlined callsites {\n";
Dehao Chen2c7ca9b2017-04-13 19:52:10 +0000141 SampleSorter<LineLocation, FunctionSamplesMap> SortedCallsiteSamples(
Diego Novillo379cc5e2015-11-19 22:18:30 +0000142 CallsiteSamples);
143 for (const auto &CS : SortedCallsiteSamples.get()) {
Dehao Chen2c7ca9b2017-04-13 19:52:10 +0000144 for (const auto &FS : CS->second) {
145 OS.indent(Indent + 2);
146 OS << CS->first << ": inlined callee: " << FS.second.getName() << ": ";
147 FS.second.print(OS, Indent + 4);
148 }
Diego Novillo379cc5e2015-11-19 22:18:30 +0000149 }
150 OS << "}\n";
151 } else {
152 OS << "No inlined callsites in this function\n";
Diego Novillo4b6bdb52015-11-12 17:58:14 +0000153 }
154}
Diego Novilloba920be2015-11-17 19:04:46 +0000155
156raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
157 const FunctionSamples &FS) {
158 FS.print(OS);
159 return OS;
160}
161
Mircea Trofin56950972018-02-22 06:42:57 +0000162unsigned FunctionSamples::getOffset(const DILocation *DIL) {
163 return (DIL->getLine() - DIL->getScope()->getSubprogram()->getLine()) &
164 0xffff;
165}
166
167const FunctionSamples *
168FunctionSamples::findFunctionSamples(const DILocation *DIL) const {
169 assert(DIL);
170 SmallVector<std::pair<LineLocation, StringRef>, 10> S;
171
172 const DILocation *PrevDIL = DIL;
173 for (DIL = DIL->getInlinedAt(); DIL; DIL = DIL->getInlinedAt()) {
174 S.push_back(std::make_pair(
175 LineLocation(getOffset(DIL), DIL->getBaseDiscriminator()),
176 PrevDIL->getScope()->getSubprogram()->getLinkageName()));
177 PrevDIL = DIL;
178 }
179 if (S.size() == 0)
180 return this;
181 const FunctionSamples *FS = this;
182 for (int i = S.size() - 1; i >= 0 && FS != nullptr; i--) {
183 FS = FS->findFunctionSamplesAt(S[i].first, S[i].second);
184 }
185 return FS;
186}
187
Aaron Ballman615eb472017-10-15 14:32:27 +0000188#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Eugene Zelenkoe78d1312017-03-03 01:07:34 +0000189LLVM_DUMP_METHOD void FunctionSamples::dump() const { print(dbgs(), 0); }
Matthias Braun8c209aa2017-01-28 02:02:38 +0000190#endif