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/ErrorHandling.h"
17 #include "llvm/Support/ManagedStatic.h"
19 using namespace llvm::sampleprof;
23 class SampleProfErrorCategoryType : public std::error_category {
24 const char *name() const LLVM_NOEXCEPT override { return "llvm.sampleprof"; }
25 std::string message(int IE) const override {
26 sampleprof_error E = static_cast<sampleprof_error>(IE);
28 case sampleprof_error::success:
30 case sampleprof_error::bad_magic:
31 return "Invalid sample profile data (bad magic)";
32 case sampleprof_error::unsupported_version:
33 return "Unsupported sample profile format version";
34 case sampleprof_error::too_large:
35 return "Too much profile data";
36 case sampleprof_error::truncated:
37 return "Truncated profile data";
38 case sampleprof_error::malformed:
39 return "Malformed sample profile data";
40 case sampleprof_error::unrecognized_format:
41 return "Unrecognized sample profile encoding format";
42 case sampleprof_error::unsupported_writing_format:
43 return "Profile encoding format unsupported for writing operations";
44 case sampleprof_error::truncated_name_table:
45 return "Truncated function name table";
46 case sampleprof_error::not_implemented:
47 return "Unimplemented feature";
48 case sampleprof_error::counter_overflow:
49 return "Counter overflow";
51 llvm_unreachable("A value of sampleprof_error has no message.");
56 static ManagedStatic<SampleProfErrorCategoryType> ErrorCategory;
58 const std::error_category &llvm::sampleprof_category() {
59 return *ErrorCategory;
62 void LineLocation::print(raw_ostream &OS) const {
64 if (Discriminator > 0)
65 OS << "." << Discriminator;
68 raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
69 const LineLocation &Loc) {
74 void LineLocation::dump() const { print(dbgs()); }
76 void CallsiteLocation::print(raw_ostream &OS) const {
77 LineLocation::print(OS);
78 OS << ": inlined callee: " << CalleeName;
81 void CallsiteLocation::dump() const { print(dbgs()); }
83 inline raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
84 const CallsiteLocation &Loc) {
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 void SampleRecord::dump() const { print(dbgs(), 0); }
102 raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
103 const SampleRecord &Sample) {
108 /// \brief Print the samples collected for a function on stream \p OS.
109 void FunctionSamples::print(raw_ostream &OS, unsigned Indent) const {
110 OS << TotalSamples << ", " << TotalHeadSamples << ", " << BodySamples.size()
111 << " sampled lines\n";
114 if (BodySamples.size() > 0) {
115 OS << "Samples collected in the function's body {\n";
116 SampleSorter<LineLocation, SampleRecord> SortedBodySamples(BodySamples);
117 for (const auto &SI : SortedBodySamples.get()) {
118 OS.indent(Indent + 2);
119 OS << SI->first << ": " << SI->second;
124 OS << "No samples collected in the function's body\n";
128 if (CallsiteSamples.size() > 0) {
129 OS << "Samples collected in inlined callsites {\n";
130 SampleSorter<CallsiteLocation, FunctionSamples> SortedCallsiteSamples(
132 for (const auto &CS : SortedCallsiteSamples.get()) {
133 OS.indent(Indent + 2);
134 OS << CS->first << ": ";
135 CS->second.print(OS, Indent + 4);
139 OS << "No inlined callsites in this function\n";
143 raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
144 const FunctionSamples &FS) {
149 void FunctionSamples::dump(void) const { print(dbgs(), 0); }