1 //=-- SampleProf.cpp - Sample profiling format support --------------------===//
2 //
3 // 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
6 //
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"
15 #include "llvm/Config/llvm-config.h"
16 #include "llvm/IR/DebugInfoMetadata.h"
17 #include "llvm/Support/Compiler.h"
18 #include "llvm/Support/Compression.h"
19 #include "llvm/Support/Debug.h"
20 #include "llvm/Support/Error.h"
21 #include "llvm/Support/ErrorHandling.h"
22 #include "llvm/Support/LEB128.h"
23 #include "llvm/Support/ManagedStatic.h"
24 #include "llvm/Support/raw_ostream.h"
25 #include <string>
26 #include <system_error>
27 
28 using namespace llvm;
29 using namespace sampleprof;
30 
31 namespace llvm {
32 namespace sampleprof {
33 SampleProfileFormat FunctionSamples::Format;
34 } // namespace sampleprof
35 } // namespace llvm
36 
37 namespace {
38 
39 // FIXME: This class is only here to support the transition to llvm::Error. It
40 // will be removed once this transition is complete. Clients should prefer to
41 // deal with the Error value directly, rather than converting to error_code.
42 class SampleProfErrorCategoryType : public std::error_category {
43   const char *name() const noexcept override { return "llvm.sampleprof"; }
44 
45   std::string message(int IE) const override {
46     sampleprof_error E = static_cast<sampleprof_error>(IE);
47     switch (E) {
48     case sampleprof_error::success:
49       return "Success";
50     case sampleprof_error::bad_magic:
51       return "Invalid sample profile data (bad magic)";
52     case sampleprof_error::unsupported_version:
53       return "Unsupported sample profile format version";
54     case sampleprof_error::too_large:
55       return "Too much profile data";
56     case sampleprof_error::truncated:
57       return "Truncated profile data";
58     case sampleprof_error::malformed:
59       return "Malformed sample profile data";
60     case sampleprof_error::unrecognized_format:
61       return "Unrecognized sample profile encoding format";
62     case sampleprof_error::unsupported_writing_format:
63       return "Profile encoding format unsupported for writing operations";
64     case sampleprof_error::truncated_name_table:
65       return "Truncated function name table";
66     case sampleprof_error::not_implemented:
67       return "Unimplemented feature";
68     case sampleprof_error::counter_overflow:
69       return "Counter overflow";
70     case sampleprof_error::ostream_seek_unsupported:
71       return "Ostream does not support seek";
72     case sampleprof_error::compress_failed:
73       return "Compress failure";
74     case sampleprof_error::uncompress_failed:
75       return "Uncompress failure";
76     case sampleprof_error::zlib_unavailable:
77       return "Zlib is unavailable";
78     }
79     llvm_unreachable("A value of sampleprof_error has no message.");
80   }
81 };
82 
83 } // end anonymous namespace
84 
85 static ManagedStatic<SampleProfErrorCategoryType> ErrorCategory;
86 
87 const std::error_category &llvm::sampleprof_category() {
88   return *ErrorCategory;
89 }
90 
91 void LineLocation::print(raw_ostream &OS) const {
92   OS << LineOffset;
93   if (Discriminator > 0)
94     OS << "." << Discriminator;
95 }
96 
97 raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
98                                           const LineLocation &Loc) {
99   Loc.print(OS);
100   return OS;
101 }
102 
103 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
104 LLVM_DUMP_METHOD void LineLocation::dump() const { print(dbgs()); }
105 #endif
106 
107 /// Print the sample record to the stream \p OS indented by \p Indent.
108 void SampleRecord::print(raw_ostream &OS, unsigned Indent) const {
109   OS << NumSamples;
110   if (hasCalls()) {
111     OS << ", calls:";
112     for (const auto &I : getSortedCallTargets())
113       OS << " " << I.first << ":" << I.second;
114   }
115   OS << "\n";
116 }
117 
118 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
119 LLVM_DUMP_METHOD void SampleRecord::dump() const { print(dbgs(), 0); }
120 #endif
121 
122 raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
123                                           const SampleRecord &Sample) {
124   Sample.print(OS, 0);
125   return OS;
126 }
127 
128 /// Print the samples collected for a function on stream \p OS.
129 void FunctionSamples::print(raw_ostream &OS, unsigned Indent) const {
130   OS << TotalSamples << ", " << TotalHeadSamples << ", " << BodySamples.size()
131      << " sampled lines\n";
132 
133   OS.indent(Indent);
134   if (!BodySamples.empty()) {
135     OS << "Samples collected in the function's body {\n";
136     SampleSorter<LineLocation, SampleRecord> SortedBodySamples(BodySamples);
137     for (const auto &SI : SortedBodySamples.get()) {
138       OS.indent(Indent + 2);
139       OS << SI->first << ": " << SI->second;
140     }
141     OS.indent(Indent);
142     OS << "}\n";
143   } else {
144     OS << "No samples collected in the function's body\n";
145   }
146 
147   OS.indent(Indent);
148   if (!CallsiteSamples.empty()) {
149     OS << "Samples collected in inlined callsites {\n";
150     SampleSorter<LineLocation, FunctionSamplesMap> SortedCallsiteSamples(
151         CallsiteSamples);
152     for (const auto &CS : SortedCallsiteSamples.get()) {
153       for (const auto &FS : CS->second) {
154         OS.indent(Indent + 2);
155         OS << CS->first << ": inlined callee: " << FS.second.getName() << ": ";
156         FS.second.print(OS, Indent + 4);
157       }
158     }
159     OS << "}\n";
160   } else {
161     OS << "No inlined callsites in this function\n";
162   }
163 }
164 
165 raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
166                                           const FunctionSamples &FS) {
167   FS.print(OS);
168   return OS;
169 }
170 
171 unsigned FunctionSamples::getOffset(const DILocation *DIL) {
172   return (DIL->getLine() - DIL->getScope()->getSubprogram()->getLine()) &
173       0xffff;
174 }
175 
176 const FunctionSamples *
177 FunctionSamples::findFunctionSamples(const DILocation *DIL) const {
178   assert(DIL);
179   SmallVector<std::pair<LineLocation, StringRef>, 10> S;
180 
181   const DILocation *PrevDIL = DIL;
182   for (DIL = DIL->getInlinedAt(); DIL; DIL = DIL->getInlinedAt()) {
183     S.push_back(std::make_pair(
184         LineLocation(getOffset(DIL), DIL->getBaseDiscriminator()),
185         PrevDIL->getScope()->getSubprogram()->getLinkageName()));
186     PrevDIL = DIL;
187   }
188   if (S.size() == 0)
189     return this;
190   const FunctionSamples *FS = this;
191   for (int i = S.size() - 1; i >= 0 && FS != nullptr; i--) {
192     FS = FS->findFunctionSamplesAt(S[i].first, S[i].second);
193   }
194   return FS;
195 }
196 
197 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
198 LLVM_DUMP_METHOD void FunctionSamples::dump() const { print(dbgs(), 0); }
199 #endif
200 
201 std::error_code ProfileSymbolList::read(uint64_t CompressSize,
202                                         uint64_t UncompressSize,
203                                         const uint8_t *Data) {
204   const char *ListStart = reinterpret_cast<const char *>(Data);
205   // CompressSize being non-zero means the profile is compressed and
206   // needs to be uncompressed first.
207   if (CompressSize) {
208     if (!llvm::zlib::isAvailable())
209       return sampleprof_error::zlib_unavailable;
210 
211     StringRef CompressedStrings(reinterpret_cast<const char *>(Data),
212                                 CompressSize);
213     char *Buffer = Allocator.Allocate<char>(UncompressSize);
214     size_t UCSize = UncompressSize;
215     llvm::Error E = zlib::uncompress(CompressedStrings, Buffer, UCSize);
216     if (E)
217       return sampleprof_error::uncompress_failed;
218     ListStart = Buffer;
219   }
220 
221   uint64_t Size = 0;
222   while (Size < UncompressSize) {
223     StringRef Str(ListStart + Size);
224     add(Str);
225     Size += Str.size() + 1;
226   }
227   return sampleprof_error::success;
228 }
229 
230 std::error_code ProfileSymbolList::write(raw_ostream &OS) {
231   // Sort the symbols before doing compression. It will make the
232   // compression much more effective.
233   std::vector<StringRef> SortedList;
234   SortedList.insert(SortedList.begin(), Syms.begin(), Syms.end());
235   llvm::sort(SortedList);
236 
237   std::string UncompressedStrings;
238   for (auto &Sym : SortedList) {
239     UncompressedStrings.append(Sym.str());
240     UncompressedStrings.append(1, '\0');
241   }
242 
243   if (ToCompress) {
244     if (!llvm::zlib::isAvailable())
245       return sampleprof_error::zlib_unavailable;
246     SmallString<128> CompressedStrings;
247     llvm::Error E = zlib::compress(UncompressedStrings, CompressedStrings,
248                                    zlib::BestSizeCompression);
249     if (E)
250       return sampleprof_error::compress_failed;
251     encodeULEB128(UncompressedStrings.size(), OS);
252     encodeULEB128(CompressedStrings.size(), OS);
253     OS << CompressedStrings.str();
254   } else {
255     encodeULEB128(UncompressedStrings.size(), OS);
256     // If profile symbol list is not compressed, we will still save
257     // a compressed size value, but the value of the size is 0.
258     encodeULEB128(0, OS);
259     OS << UncompressedStrings;
260   }
261   return sampleprof_error::success;
262 }
263 
264 void ProfileSymbolList::dump(raw_ostream &OS) const {
265   OS << "======== Dump profile symbol list ========\n";
266   std::vector<StringRef> SortedList;
267   SortedList.insert(SortedList.begin(), Syms.begin(), Syms.end());
268   llvm::sort(SortedList);
269 
270   for (auto &Sym : SortedList)
271     OS << Sym << "\n";
272 }
273