1 //=-- SampleProf.cpp - Sample profiling format support --------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file contains common definitions used in the reading and writing of
10 // sample profile data.
12 //===----------------------------------------------------------------------===//
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/Debug.h"
19 #include "llvm/Support/Error.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/Support/LEB128.h"
22 #include "llvm/Support/ManagedStatic.h"
23 #include "llvm/Support/raw_ostream.h"
25 #include <system_error>
28 using namespace sampleprof;
31 namespace sampleprof {
32 SampleProfileFormat FunctionSamples::Format;
33 bool FunctionSamples::UseMD5;
34 } // namespace sampleprof
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"; }
45 std::string message(int IE) const override {
46 sampleprof_error E = static_cast<sampleprof_error>(IE);
48 case sampleprof_error::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";
79 llvm_unreachable("A value of sampleprof_error has no message.");
83 } // end anonymous namespace
85 static ManagedStatic<SampleProfErrorCategoryType> ErrorCategory;
87 const std::error_category &llvm::sampleprof_category() {
88 return *ErrorCategory;
91 void LineLocation::print(raw_ostream &OS) const {
93 if (Discriminator > 0)
94 OS << "." << Discriminator;
97 raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
98 const LineLocation &Loc) {
103 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
104 LLVM_DUMP_METHOD void LineLocation::dump() const { print(dbgs()); }
107 /// Print the sample record to the stream \p OS indented by \p Indent.
108 void SampleRecord::print(raw_ostream &OS, unsigned Indent) const {
112 for (const auto &I : getSortedCallTargets())
113 OS << " " << I.first << ":" << I.second;
118 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
119 LLVM_DUMP_METHOD void SampleRecord::dump() const { print(dbgs(), 0); }
122 raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
123 const SampleRecord &Sample) {
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";
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;
144 OS << "No samples collected in the function's body\n";
148 if (!CallsiteSamples.empty()) {
149 OS << "Samples collected in inlined callsites {\n";
150 SampleSorter<LineLocation, FunctionSamplesMap> SortedCallsiteSamples(
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);
162 OS << "No inlined callsites in this function\n";
166 raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
167 const FunctionSamples &FS) {
172 unsigned FunctionSamples::getOffset(const DILocation *DIL) {
173 return (DIL->getLine() - DIL->getScope()->getSubprogram()->getLine()) &
177 const FunctionSamples *
178 FunctionSamples::findFunctionSamples(const DILocation *DIL) const {
180 SmallVector<std::pair<LineLocation, StringRef>, 10> S;
182 const DILocation *PrevDIL = DIL;
183 for (DIL = DIL->getInlinedAt(); DIL; DIL = DIL->getInlinedAt()) {
184 S.push_back(std::make_pair(
185 LineLocation(getOffset(DIL), DIL->getBaseDiscriminator()),
186 PrevDIL->getScope()->getSubprogram()->getLinkageName()));
191 const FunctionSamples *FS = this;
192 for (int i = S.size() - 1; i >= 0 && FS != nullptr; i--) {
193 FS = FS->findFunctionSamplesAt(S[i].first, S[i].second);
198 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
199 LLVM_DUMP_METHOD void FunctionSamples::dump() const { print(dbgs(), 0); }
202 std::error_code ProfileSymbolList::read(const uint8_t *Data,
204 const char *ListStart = reinterpret_cast<const char *>(Data);
206 while (Size < ListSize) {
207 StringRef Str(ListStart + Size);
209 Size += Str.size() + 1;
211 if (Size != ListSize)
212 return sampleprof_error::malformed;
213 return sampleprof_error::success;
216 std::error_code ProfileSymbolList::write(raw_ostream &OS) {
217 // Sort the symbols before output. If doing compression.
218 // It will make the compression much more effective.
219 std::vector<StringRef> SortedList;
220 SortedList.insert(SortedList.begin(), Syms.begin(), Syms.end());
221 llvm::sort(SortedList);
223 std::string OutputString;
224 for (auto &Sym : SortedList) {
225 OutputString.append(Sym.str());
226 OutputString.append(1, '\0');
230 return sampleprof_error::success;
233 void ProfileSymbolList::dump(raw_ostream &OS) const {
234 OS << "======== Dump profile symbol list ========\n";
235 std::vector<StringRef> SortedList;
236 SortedList.insert(SortedList.begin(), Syms.begin(), Syms.end());
237 llvm::sort(SortedList);
239 for (auto &Sym : SortedList)