1*0b57cec5SDimitry Andric //=-- SampleProf.cpp - Sample profiling format support --------------------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric //
9*0b57cec5SDimitry Andric // This file contains common definitions used in the reading and writing of
10*0b57cec5SDimitry Andric // sample profile data.
11*0b57cec5SDimitry Andric //
12*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
13*0b57cec5SDimitry Andric 
14*0b57cec5SDimitry Andric #include "llvm/ProfileData/SampleProf.h"
15*0b57cec5SDimitry Andric #include "llvm/Config/llvm-config.h"
16*0b57cec5SDimitry Andric #include "llvm/IR/DebugInfoMetadata.h"
17*0b57cec5SDimitry Andric #include "llvm/Support/Compiler.h"
18*0b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
198bcb0991SDimitry Andric #include "llvm/Support/Error.h"
20*0b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
218bcb0991SDimitry Andric #include "llvm/Support/LEB128.h"
22*0b57cec5SDimitry Andric #include "llvm/Support/ManagedStatic.h"
23*0b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
24*0b57cec5SDimitry Andric #include <string>
25*0b57cec5SDimitry Andric #include <system_error>
26*0b57cec5SDimitry Andric 
27*0b57cec5SDimitry Andric using namespace llvm;
28*0b57cec5SDimitry Andric using namespace sampleprof;
29*0b57cec5SDimitry Andric 
30*0b57cec5SDimitry Andric namespace llvm {
31*0b57cec5SDimitry Andric namespace sampleprof {
32*0b57cec5SDimitry Andric SampleProfileFormat FunctionSamples::Format;
33*0b57cec5SDimitry Andric } // namespace sampleprof
34*0b57cec5SDimitry Andric } // namespace llvm
35*0b57cec5SDimitry Andric 
36*0b57cec5SDimitry Andric namespace {
37*0b57cec5SDimitry Andric 
38*0b57cec5SDimitry Andric // FIXME: This class is only here to support the transition to llvm::Error. It
39*0b57cec5SDimitry Andric // will be removed once this transition is complete. Clients should prefer to
40*0b57cec5SDimitry Andric // deal with the Error value directly, rather than converting to error_code.
41*0b57cec5SDimitry Andric class SampleProfErrorCategoryType : public std::error_category {
42*0b57cec5SDimitry Andric   const char *name() const noexcept override { return "llvm.sampleprof"; }
43*0b57cec5SDimitry Andric 
44*0b57cec5SDimitry Andric   std::string message(int IE) const override {
45*0b57cec5SDimitry Andric     sampleprof_error E = static_cast<sampleprof_error>(IE);
46*0b57cec5SDimitry Andric     switch (E) {
47*0b57cec5SDimitry Andric     case sampleprof_error::success:
48*0b57cec5SDimitry Andric       return "Success";
49*0b57cec5SDimitry Andric     case sampleprof_error::bad_magic:
50*0b57cec5SDimitry Andric       return "Invalid sample profile data (bad magic)";
51*0b57cec5SDimitry Andric     case sampleprof_error::unsupported_version:
52*0b57cec5SDimitry Andric       return "Unsupported sample profile format version";
53*0b57cec5SDimitry Andric     case sampleprof_error::too_large:
54*0b57cec5SDimitry Andric       return "Too much profile data";
55*0b57cec5SDimitry Andric     case sampleprof_error::truncated:
56*0b57cec5SDimitry Andric       return "Truncated profile data";
57*0b57cec5SDimitry Andric     case sampleprof_error::malformed:
58*0b57cec5SDimitry Andric       return "Malformed sample profile data";
59*0b57cec5SDimitry Andric     case sampleprof_error::unrecognized_format:
60*0b57cec5SDimitry Andric       return "Unrecognized sample profile encoding format";
61*0b57cec5SDimitry Andric     case sampleprof_error::unsupported_writing_format:
62*0b57cec5SDimitry Andric       return "Profile encoding format unsupported for writing operations";
63*0b57cec5SDimitry Andric     case sampleprof_error::truncated_name_table:
64*0b57cec5SDimitry Andric       return "Truncated function name table";
65*0b57cec5SDimitry Andric     case sampleprof_error::not_implemented:
66*0b57cec5SDimitry Andric       return "Unimplemented feature";
67*0b57cec5SDimitry Andric     case sampleprof_error::counter_overflow:
68*0b57cec5SDimitry Andric       return "Counter overflow";
69*0b57cec5SDimitry Andric     case sampleprof_error::ostream_seek_unsupported:
70*0b57cec5SDimitry Andric       return "Ostream does not support seek";
718bcb0991SDimitry Andric     case sampleprof_error::compress_failed:
728bcb0991SDimitry Andric       return "Compress failure";
738bcb0991SDimitry Andric     case sampleprof_error::uncompress_failed:
748bcb0991SDimitry Andric       return "Uncompress failure";
758bcb0991SDimitry Andric     case sampleprof_error::zlib_unavailable:
768bcb0991SDimitry Andric       return "Zlib is unavailable";
77*0b57cec5SDimitry Andric     }
78*0b57cec5SDimitry Andric     llvm_unreachable("A value of sampleprof_error has no message.");
79*0b57cec5SDimitry Andric   }
80*0b57cec5SDimitry Andric };
81*0b57cec5SDimitry Andric 
82*0b57cec5SDimitry Andric } // end anonymous namespace
83*0b57cec5SDimitry Andric 
84*0b57cec5SDimitry Andric static ManagedStatic<SampleProfErrorCategoryType> ErrorCategory;
85*0b57cec5SDimitry Andric 
86*0b57cec5SDimitry Andric const std::error_category &llvm::sampleprof_category() {
87*0b57cec5SDimitry Andric   return *ErrorCategory;
88*0b57cec5SDimitry Andric }
89*0b57cec5SDimitry Andric 
90*0b57cec5SDimitry Andric void LineLocation::print(raw_ostream &OS) const {
91*0b57cec5SDimitry Andric   OS << LineOffset;
92*0b57cec5SDimitry Andric   if (Discriminator > 0)
93*0b57cec5SDimitry Andric     OS << "." << Discriminator;
94*0b57cec5SDimitry Andric }
95*0b57cec5SDimitry Andric 
96*0b57cec5SDimitry Andric raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
97*0b57cec5SDimitry Andric                                           const LineLocation &Loc) {
98*0b57cec5SDimitry Andric   Loc.print(OS);
99*0b57cec5SDimitry Andric   return OS;
100*0b57cec5SDimitry Andric }
101*0b57cec5SDimitry Andric 
102*0b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
103*0b57cec5SDimitry Andric LLVM_DUMP_METHOD void LineLocation::dump() const { print(dbgs()); }
104*0b57cec5SDimitry Andric #endif
105*0b57cec5SDimitry Andric 
106*0b57cec5SDimitry Andric /// Print the sample record to the stream \p OS indented by \p Indent.
107*0b57cec5SDimitry Andric void SampleRecord::print(raw_ostream &OS, unsigned Indent) const {
108*0b57cec5SDimitry Andric   OS << NumSamples;
109*0b57cec5SDimitry Andric   if (hasCalls()) {
110*0b57cec5SDimitry Andric     OS << ", calls:";
1118bcb0991SDimitry Andric     for (const auto &I : getSortedCallTargets())
1128bcb0991SDimitry Andric       OS << " " << I.first << ":" << I.second;
113*0b57cec5SDimitry Andric   }
114*0b57cec5SDimitry Andric   OS << "\n";
115*0b57cec5SDimitry Andric }
116*0b57cec5SDimitry Andric 
117*0b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
118*0b57cec5SDimitry Andric LLVM_DUMP_METHOD void SampleRecord::dump() const { print(dbgs(), 0); }
119*0b57cec5SDimitry Andric #endif
120*0b57cec5SDimitry Andric 
121*0b57cec5SDimitry Andric raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
122*0b57cec5SDimitry Andric                                           const SampleRecord &Sample) {
123*0b57cec5SDimitry Andric   Sample.print(OS, 0);
124*0b57cec5SDimitry Andric   return OS;
125*0b57cec5SDimitry Andric }
126*0b57cec5SDimitry Andric 
127*0b57cec5SDimitry Andric /// Print the samples collected for a function on stream \p OS.
128*0b57cec5SDimitry Andric void FunctionSamples::print(raw_ostream &OS, unsigned Indent) const {
129*0b57cec5SDimitry Andric   OS << TotalSamples << ", " << TotalHeadSamples << ", " << BodySamples.size()
130*0b57cec5SDimitry Andric      << " sampled lines\n";
131*0b57cec5SDimitry Andric 
132*0b57cec5SDimitry Andric   OS.indent(Indent);
133*0b57cec5SDimitry Andric   if (!BodySamples.empty()) {
134*0b57cec5SDimitry Andric     OS << "Samples collected in the function's body {\n";
135*0b57cec5SDimitry Andric     SampleSorter<LineLocation, SampleRecord> SortedBodySamples(BodySamples);
136*0b57cec5SDimitry Andric     for (const auto &SI : SortedBodySamples.get()) {
137*0b57cec5SDimitry Andric       OS.indent(Indent + 2);
138*0b57cec5SDimitry Andric       OS << SI->first << ": " << SI->second;
139*0b57cec5SDimitry Andric     }
140*0b57cec5SDimitry Andric     OS.indent(Indent);
141*0b57cec5SDimitry Andric     OS << "}\n";
142*0b57cec5SDimitry Andric   } else {
143*0b57cec5SDimitry Andric     OS << "No samples collected in the function's body\n";
144*0b57cec5SDimitry Andric   }
145*0b57cec5SDimitry Andric 
146*0b57cec5SDimitry Andric   OS.indent(Indent);
147*0b57cec5SDimitry Andric   if (!CallsiteSamples.empty()) {
148*0b57cec5SDimitry Andric     OS << "Samples collected in inlined callsites {\n";
149*0b57cec5SDimitry Andric     SampleSorter<LineLocation, FunctionSamplesMap> SortedCallsiteSamples(
150*0b57cec5SDimitry Andric         CallsiteSamples);
151*0b57cec5SDimitry Andric     for (const auto &CS : SortedCallsiteSamples.get()) {
152*0b57cec5SDimitry Andric       for (const auto &FS : CS->second) {
153*0b57cec5SDimitry Andric         OS.indent(Indent + 2);
154*0b57cec5SDimitry Andric         OS << CS->first << ": inlined callee: " << FS.second.getName() << ": ";
155*0b57cec5SDimitry Andric         FS.second.print(OS, Indent + 4);
156*0b57cec5SDimitry Andric       }
157*0b57cec5SDimitry Andric     }
1588bcb0991SDimitry Andric     OS.indent(Indent);
159*0b57cec5SDimitry Andric     OS << "}\n";
160*0b57cec5SDimitry Andric   } else {
161*0b57cec5SDimitry Andric     OS << "No inlined callsites in this function\n";
162*0b57cec5SDimitry Andric   }
163*0b57cec5SDimitry Andric }
164*0b57cec5SDimitry Andric 
165*0b57cec5SDimitry Andric raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
166*0b57cec5SDimitry Andric                                           const FunctionSamples &FS) {
167*0b57cec5SDimitry Andric   FS.print(OS);
168*0b57cec5SDimitry Andric   return OS;
169*0b57cec5SDimitry Andric }
170*0b57cec5SDimitry Andric 
171*0b57cec5SDimitry Andric unsigned FunctionSamples::getOffset(const DILocation *DIL) {
172*0b57cec5SDimitry Andric   return (DIL->getLine() - DIL->getScope()->getSubprogram()->getLine()) &
173*0b57cec5SDimitry Andric       0xffff;
174*0b57cec5SDimitry Andric }
175*0b57cec5SDimitry Andric 
176*0b57cec5SDimitry Andric const FunctionSamples *
177*0b57cec5SDimitry Andric FunctionSamples::findFunctionSamples(const DILocation *DIL) const {
178*0b57cec5SDimitry Andric   assert(DIL);
179*0b57cec5SDimitry Andric   SmallVector<std::pair<LineLocation, StringRef>, 10> S;
180*0b57cec5SDimitry Andric 
181*0b57cec5SDimitry Andric   const DILocation *PrevDIL = DIL;
182*0b57cec5SDimitry Andric   for (DIL = DIL->getInlinedAt(); DIL; DIL = DIL->getInlinedAt()) {
183*0b57cec5SDimitry Andric     S.push_back(std::make_pair(
184*0b57cec5SDimitry Andric         LineLocation(getOffset(DIL), DIL->getBaseDiscriminator()),
185*0b57cec5SDimitry Andric         PrevDIL->getScope()->getSubprogram()->getLinkageName()));
186*0b57cec5SDimitry Andric     PrevDIL = DIL;
187*0b57cec5SDimitry Andric   }
188*0b57cec5SDimitry Andric   if (S.size() == 0)
189*0b57cec5SDimitry Andric     return this;
190*0b57cec5SDimitry Andric   const FunctionSamples *FS = this;
191*0b57cec5SDimitry Andric   for (int i = S.size() - 1; i >= 0 && FS != nullptr; i--) {
192*0b57cec5SDimitry Andric     FS = FS->findFunctionSamplesAt(S[i].first, S[i].second);
193*0b57cec5SDimitry Andric   }
194*0b57cec5SDimitry Andric   return FS;
195*0b57cec5SDimitry Andric }
196*0b57cec5SDimitry Andric 
197*0b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
198*0b57cec5SDimitry Andric LLVM_DUMP_METHOD void FunctionSamples::dump() const { print(dbgs(), 0); }
199*0b57cec5SDimitry Andric #endif
2008bcb0991SDimitry Andric 
2018bcb0991SDimitry Andric std::error_code ProfileSymbolList::read(const uint8_t *Data,
2028bcb0991SDimitry Andric                                         uint64_t ListSize) {
2038bcb0991SDimitry Andric   const char *ListStart = reinterpret_cast<const char *>(Data);
2048bcb0991SDimitry Andric   uint64_t Size = 0;
2058bcb0991SDimitry Andric   while (Size < ListSize) {
2068bcb0991SDimitry Andric     StringRef Str(ListStart + Size);
2078bcb0991SDimitry Andric     add(Str);
2088bcb0991SDimitry Andric     Size += Str.size() + 1;
2098bcb0991SDimitry Andric   }
2108bcb0991SDimitry Andric   if (Size != ListSize)
2118bcb0991SDimitry Andric     return sampleprof_error::malformed;
2128bcb0991SDimitry Andric   return sampleprof_error::success;
2138bcb0991SDimitry Andric }
2148bcb0991SDimitry Andric 
2158bcb0991SDimitry Andric std::error_code ProfileSymbolList::write(raw_ostream &OS) {
2168bcb0991SDimitry Andric   // Sort the symbols before output. If doing compression.
2178bcb0991SDimitry Andric   // It will make the compression much more effective.
2188bcb0991SDimitry Andric   std::vector<StringRef> SortedList;
2198bcb0991SDimitry Andric   SortedList.insert(SortedList.begin(), Syms.begin(), Syms.end());
2208bcb0991SDimitry Andric   llvm::sort(SortedList);
2218bcb0991SDimitry Andric 
2228bcb0991SDimitry Andric   std::string OutputString;
2238bcb0991SDimitry Andric   for (auto &Sym : SortedList) {
2248bcb0991SDimitry Andric     OutputString.append(Sym.str());
2258bcb0991SDimitry Andric     OutputString.append(1, '\0');
2268bcb0991SDimitry Andric   }
2278bcb0991SDimitry Andric 
2288bcb0991SDimitry Andric   OS << OutputString;
2298bcb0991SDimitry Andric   return sampleprof_error::success;
2308bcb0991SDimitry Andric }
2318bcb0991SDimitry Andric 
2328bcb0991SDimitry Andric void ProfileSymbolList::dump(raw_ostream &OS) const {
2338bcb0991SDimitry Andric   OS << "======== Dump profile symbol list ========\n";
2348bcb0991SDimitry Andric   std::vector<StringRef> SortedList;
2358bcb0991SDimitry Andric   SortedList.insert(SortedList.begin(), Syms.begin(), Syms.end());
2368bcb0991SDimitry Andric   llvm::sort(SortedList);
2378bcb0991SDimitry Andric 
2388bcb0991SDimitry Andric   for (auto &Sym : SortedList)
2398bcb0991SDimitry Andric     OS << Sym << "\n";
2408bcb0991SDimitry Andric }
241