1 //=-- SampleProf.cpp - Sample profiling format support --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains common definitions used in the reading and writing of
11 // sample profile data.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/ProfileData/SampleProf.h"
16 #include "llvm/Support/Compiler.h"
17 #include "llvm/Support/Debug.h"
18 #include "llvm/Support/ErrorHandling.h"
19 #include "llvm/Support/ManagedStatic.h"
20 #include "llvm/Support/raw_ostream.h"
22 #include <system_error>
25 using namespace sampleprof;
29 // FIXME: This class is only here to support the transition to llvm::Error. It
30 // will be removed once this transition is complete. Clients should prefer to
31 // deal with the Error value directly, rather than converting to error_code.
32 class SampleProfErrorCategoryType : public std::error_category {
33 const char *name() const noexcept override { return "llvm.sampleprof"; }
35 std::string message(int IE) const override {
36 sampleprof_error E = static_cast<sampleprof_error>(IE);
38 case sampleprof_error::success:
40 case sampleprof_error::bad_magic:
41 return "Invalid sample profile data (bad magic)";
42 case sampleprof_error::unsupported_version:
43 return "Unsupported sample profile format version";
44 case sampleprof_error::too_large:
45 return "Too much profile data";
46 case sampleprof_error::truncated:
47 return "Truncated profile data";
48 case sampleprof_error::malformed:
49 return "Malformed sample profile data";
50 case sampleprof_error::unrecognized_format:
51 return "Unrecognized sample profile encoding format";
52 case sampleprof_error::unsupported_writing_format:
53 return "Profile encoding format unsupported for writing operations";
54 case sampleprof_error::truncated_name_table:
55 return "Truncated function name table";
56 case sampleprof_error::not_implemented:
57 return "Unimplemented feature";
58 case sampleprof_error::counter_overflow:
59 return "Counter overflow";
61 llvm_unreachable("A value of sampleprof_error has no message.");
65 } // end anonymous namespace
67 static ManagedStatic<SampleProfErrorCategoryType> ErrorCategory;
69 const std::error_category &llvm::sampleprof_category() {
70 return *ErrorCategory;
73 void LineLocation::print(raw_ostream &OS) const {
75 if (Discriminator > 0)
76 OS << "." << Discriminator;
79 raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
80 const LineLocation &Loc) {
85 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
86 LLVM_DUMP_METHOD void LineLocation::dump() const { print(dbgs()); }
89 /// \brief Print the sample record to the stream \p OS indented by \p Indent.
90 void SampleRecord::print(raw_ostream &OS, unsigned Indent) const {
94 for (const auto &I : getCallTargets())
95 OS << " " << I.first() << ":" << I.second;
100 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
101 LLVM_DUMP_METHOD void SampleRecord::dump() const { print(dbgs(), 0); }
104 raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
105 const SampleRecord &Sample) {
110 /// \brief Print the samples collected for a function on stream \p OS.
111 void FunctionSamples::print(raw_ostream &OS, unsigned Indent) const {
112 OS << TotalSamples << ", " << TotalHeadSamples << ", " << BodySamples.size()
113 << " sampled lines\n";
116 if (!BodySamples.empty()) {
117 OS << "Samples collected in the function's body {\n";
118 SampleSorter<LineLocation, SampleRecord> SortedBodySamples(BodySamples);
119 for (const auto &SI : SortedBodySamples.get()) {
120 OS.indent(Indent + 2);
121 OS << SI->first << ": " << SI->second;
126 OS << "No samples collected in the function's body\n";
130 if (!CallsiteSamples.empty()) {
131 OS << "Samples collected in inlined callsites {\n";
132 SampleSorter<LineLocation, FunctionSamplesMap> SortedCallsiteSamples(
134 for (const auto &CS : SortedCallsiteSamples.get()) {
135 for (const auto &FS : CS->second) {
136 OS.indent(Indent + 2);
137 OS << CS->first << ": inlined callee: " << FS.second.getName() << ": ";
138 FS.second.print(OS, Indent + 4);
143 OS << "No inlined callsites in this function\n";
147 raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
148 const FunctionSamples &FS) {
153 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
154 LLVM_DUMP_METHOD void FunctionSamples::dump() const { print(dbgs(), 0); }