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