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 } // namespace sampleprof
38 // FIXME: This class is only here to support the transition to llvm::Error. It
39 // will be removed once this transition is complete. Clients should prefer to
40 // deal with the Error value directly, rather than converting to error_code.
41 class SampleProfErrorCategoryType
: public std::error_category
{
42 const char *name() const noexcept override
{ return "llvm.sampleprof"; }
44 std::string
message(int IE
) const override
{
45 sampleprof_error E
= static_cast<sampleprof_error
>(IE
);
47 case sampleprof_error::success
:
49 case sampleprof_error::bad_magic
:
50 return "Invalid sample profile data (bad magic)";
51 case sampleprof_error::unsupported_version
:
52 return "Unsupported sample profile format version";
53 case sampleprof_error::too_large
:
54 return "Too much profile data";
55 case sampleprof_error::truncated
:
56 return "Truncated profile data";
57 case sampleprof_error::malformed
:
58 return "Malformed sample profile data";
59 case sampleprof_error::unrecognized_format
:
60 return "Unrecognized sample profile encoding format";
61 case sampleprof_error::unsupported_writing_format
:
62 return "Profile encoding format unsupported for writing operations";
63 case sampleprof_error::truncated_name_table
:
64 return "Truncated function name table";
65 case sampleprof_error::not_implemented
:
66 return "Unimplemented feature";
67 case sampleprof_error::counter_overflow
:
68 return "Counter overflow";
69 case sampleprof_error::ostream_seek_unsupported
:
70 return "Ostream does not support seek";
71 case sampleprof_error::compress_failed
:
72 return "Compress failure";
73 case sampleprof_error::uncompress_failed
:
74 return "Uncompress failure";
75 case sampleprof_error::zlib_unavailable
:
76 return "Zlib is unavailable";
78 llvm_unreachable("A value of sampleprof_error has no message.");
82 } // end anonymous namespace
84 static ManagedStatic
<SampleProfErrorCategoryType
> ErrorCategory
;
86 const std::error_category
&llvm::sampleprof_category() {
87 return *ErrorCategory
;
90 void LineLocation::print(raw_ostream
&OS
) const {
92 if (Discriminator
> 0)
93 OS
<< "." << Discriminator
;
96 raw_ostream
&llvm::sampleprof::operator<<(raw_ostream
&OS
,
97 const LineLocation
&Loc
) {
102 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
103 LLVM_DUMP_METHOD
void LineLocation::dump() const { print(dbgs()); }
106 /// Print the sample record to the stream \p OS indented by \p Indent.
107 void SampleRecord::print(raw_ostream
&OS
, unsigned Indent
) const {
111 for (const auto &I
: getSortedCallTargets())
112 OS
<< " " << I
.first
<< ":" << I
.second
;
117 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
118 LLVM_DUMP_METHOD
void SampleRecord::dump() const { print(dbgs(), 0); }
121 raw_ostream
&llvm::sampleprof::operator<<(raw_ostream
&OS
,
122 const SampleRecord
&Sample
) {
127 /// Print the samples collected for a function on stream \p OS.
128 void FunctionSamples::print(raw_ostream
&OS
, unsigned Indent
) const {
129 OS
<< TotalSamples
<< ", " << TotalHeadSamples
<< ", " << BodySamples
.size()
130 << " sampled lines\n";
133 if (!BodySamples
.empty()) {
134 OS
<< "Samples collected in the function's body {\n";
135 SampleSorter
<LineLocation
, SampleRecord
> SortedBodySamples(BodySamples
);
136 for (const auto &SI
: SortedBodySamples
.get()) {
137 OS
.indent(Indent
+ 2);
138 OS
<< SI
->first
<< ": " << SI
->second
;
143 OS
<< "No samples collected in the function's body\n";
147 if (!CallsiteSamples
.empty()) {
148 OS
<< "Samples collected in inlined callsites {\n";
149 SampleSorter
<LineLocation
, FunctionSamplesMap
> SortedCallsiteSamples(
151 for (const auto &CS
: SortedCallsiteSamples
.get()) {
152 for (const auto &FS
: CS
->second
) {
153 OS
.indent(Indent
+ 2);
154 OS
<< CS
->first
<< ": inlined callee: " << FS
.second
.getName() << ": ";
155 FS
.second
.print(OS
, Indent
+ 4);
161 OS
<< "No inlined callsites in this function\n";
165 raw_ostream
&llvm::sampleprof::operator<<(raw_ostream
&OS
,
166 const FunctionSamples
&FS
) {
171 unsigned FunctionSamples::getOffset(const DILocation
*DIL
) {
172 return (DIL
->getLine() - DIL
->getScope()->getSubprogram()->getLine()) &
176 const FunctionSamples
*
177 FunctionSamples::findFunctionSamples(const DILocation
*DIL
) const {
179 SmallVector
<std::pair
<LineLocation
, StringRef
>, 10> S
;
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()));
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
);
197 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
198 LLVM_DUMP_METHOD
void FunctionSamples::dump() const { print(dbgs(), 0); }
201 std::error_code
ProfileSymbolList::read(const uint8_t *Data
,
203 const char *ListStart
= reinterpret_cast<const char *>(Data
);
205 while (Size
< ListSize
) {
206 StringRef
Str(ListStart
+ Size
);
208 Size
+= Str
.size() + 1;
210 if (Size
!= ListSize
)
211 return sampleprof_error::malformed
;
212 return sampleprof_error::success
;
215 std::error_code
ProfileSymbolList::write(raw_ostream
&OS
) {
216 // Sort the symbols before output. If doing compression.
217 // It will make the compression much more effective.
218 std::vector
<StringRef
> SortedList
;
219 SortedList
.insert(SortedList
.begin(), Syms
.begin(), Syms
.end());
220 llvm::sort(SortedList
);
222 std::string OutputString
;
223 for (auto &Sym
: SortedList
) {
224 OutputString
.append(Sym
.str());
225 OutputString
.append(1, '\0');
229 return sampleprof_error::success
;
232 void ProfileSymbolList::dump(raw_ostream
&OS
) const {
233 OS
<< "======== Dump profile symbol list ========\n";
234 std::vector
<StringRef
> SortedList
;
235 SortedList
.insert(SortedList
.begin(), Syms
.begin(), Syms
.end());
236 llvm::sort(SortedList
);
238 for (auto &Sym
: SortedList
)