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/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"
26 #include <system_error>
29 using namespace sampleprof
;
32 namespace sampleprof
{
33 SampleProfileFormat
FunctionSamples::Format
;
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);
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(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.
208 if (!llvm::zlib::isAvailable())
209 return sampleprof_error::zlib_unavailable
;
211 StringRef
CompressedStrings(reinterpret_cast<const char *>(Data
),
213 char *Buffer
= Allocator
.Allocate
<char>(UncompressSize
);
214 size_t UCSize
= UncompressSize
;
215 llvm::Error E
= zlib::uncompress(CompressedStrings
, Buffer
, UCSize
);
217 return sampleprof_error::uncompress_failed
;
222 while (Size
< UncompressSize
) {
223 StringRef
Str(ListStart
+ Size
);
225 Size
+= Str
.size() + 1;
227 return sampleprof_error::success
;
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
);
237 std::string UncompressedStrings
;
238 for (auto &Sym
: SortedList
) {
239 UncompressedStrings
.append(Sym
.str());
240 UncompressedStrings
.append(1, '\0');
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
);
250 return sampleprof_error::compress_failed
;
251 encodeULEB128(UncompressedStrings
.size(), OS
);
252 encodeULEB128(CompressedStrings
.size(), OS
);
253 OS
<< CompressedStrings
.str();
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
;
261 return sampleprof_error::success
;
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
);
270 for (auto &Sym
: SortedList
)