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 // FIXME: This class is only here to support the transition to llvm::Error. It
24 // will be removed once this transition is complete. Clients should prefer to
25 // deal with the Error value directly, rather than converting to error_code.
26 class SampleProfErrorCategoryType : public std::error_category {
27 const char *name() const noexcept override { return "llvm.sampleprof"; }
28 std::string message(int IE) const override {
29 sampleprof_error E = static_cast<sampleprof_error>(IE);
31 case sampleprof_error::success:
33 case sampleprof_error::bad_magic:
34 return "Invalid sample profile data (bad magic)";
35 case sampleprof_error::unsupported_version:
36 return "Unsupported sample profile format version";
37 case sampleprof_error::too_large:
38 return "Too much profile data";
39 case sampleprof_error::truncated:
40 return "Truncated profile data";
41 case sampleprof_error::malformed:
42 return "Malformed sample profile data";
43 case sampleprof_error::unrecognized_format:
44 return "Unrecognized sample profile encoding format";
45 case sampleprof_error::unsupported_writing_format:
46 return "Profile encoding format unsupported for writing operations";
47 case sampleprof_error::truncated_name_table:
48 return "Truncated function name table";
49 case sampleprof_error::not_implemented:
50 return "Unimplemented feature";
51 case sampleprof_error::counter_overflow:
52 return "Counter overflow";
54 llvm_unreachable("A value of sampleprof_error has no message.");
59 static ManagedStatic<SampleProfErrorCategoryType> ErrorCategory;
61 const std::error_category &llvm::sampleprof_category() {
62 return *ErrorCategory;
65 void LineLocation::print(raw_ostream &OS) const {
67 if (Discriminator > 0)
68 OS << "." << Discriminator;
71 raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
72 const LineLocation &Loc) {
77 LLVM_DUMP_METHOD void LineLocation::dump() const { print(dbgs()); }
79 /// \brief Print the sample record to the stream \p OS indented by \p Indent.
80 void SampleRecord::print(raw_ostream &OS, unsigned Indent) const {
84 for (const auto &I : getCallTargets())
85 OS << " " << I.first() << ":" << I.second;
90 LLVM_DUMP_METHOD void SampleRecord::dump() const { print(dbgs(), 0); }
92 raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
93 const SampleRecord &Sample) {
98 /// \brief Print the samples collected for a function on stream \p OS.
99 void FunctionSamples::print(raw_ostream &OS, unsigned Indent) const {
100 OS << TotalSamples << ", " << TotalHeadSamples << ", " << BodySamples.size()
101 << " sampled lines\n";
104 if (BodySamples.size() > 0) {
105 OS << "Samples collected in the function's body {\n";
106 SampleSorter<LineLocation, SampleRecord> SortedBodySamples(BodySamples);
107 for (const auto &SI : SortedBodySamples.get()) {
108 OS.indent(Indent + 2);
109 OS << SI->first << ": " << SI->second;
114 OS << "No samples collected in the function's body\n";
118 if (CallsiteSamples.size() > 0) {
119 OS << "Samples collected in inlined callsites {\n";
120 SampleSorter<LineLocation, FunctionSamples> SortedCallsiteSamples(
122 for (const auto &CS : SortedCallsiteSamples.get()) {
123 OS.indent(Indent + 2);
124 OS << CS->first << ": inlined callee: " << CS->second.getName() << ": ";
125 CS->second.print(OS, Indent + 4);
129 OS << "No inlined callsites in this function\n";
133 raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
134 const FunctionSamples &FS) {
139 void FunctionSamples::dump(void) const { print(dbgs(), 0); }