LLVM  5.0.0
SampleProf.cpp
Go to the documentation of this file.
1 //=-- SampleProf.cpp - Sample profiling format support --------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains common definitions used in the reading and writing of
11 // sample profile data.
12 //
13 //===----------------------------------------------------------------------===//
14 
16 #include "llvm/Support/Compiler.h"
17 #include "llvm/Support/Debug.h"
21 #include <string>
22 #include <system_error>
23 
24 using namespace llvm;
25 using namespace sampleprof;
26 
27 namespace {
28 
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"; }
34 
35  std::string message(int IE) const override {
36  sampleprof_error E = static_cast<sampleprof_error>(IE);
37  switch (E) {
39  return "Success";
41  return "Invalid sample profile data (bad magic)";
43  return "Unsupported sample profile format version";
45  return "Too much profile data";
47  return "Truncated profile data";
49  return "Malformed sample profile data";
51  return "Unrecognized sample profile encoding format";
53  return "Profile encoding format unsupported for writing operations";
55  return "Truncated function name table";
57  return "Unimplemented feature";
59  return "Counter overflow";
60  }
61  llvm_unreachable("A value of sampleprof_error has no message.");
62  }
63 };
64 
65 } // end anonymous namespace
66 
68 
70  return *ErrorCategory;
71 }
72 
74  OS << LineOffset;
75  if (Discriminator > 0)
76  OS << "." << Discriminator;
77 }
78 
80  const LineLocation &Loc) {
81  Loc.print(OS);
82  return OS;
83 }
84 
85 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
87 #endif
88 
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 {
91  OS << NumSamples;
92  if (hasCalls()) {
93  OS << ", calls:";
94  for (const auto &I : getCallTargets())
95  OS << " " << I.first() << ":" << I.second;
96  }
97  OS << "\n";
98 }
99 
100 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
102 #endif
103 
105  const SampleRecord &Sample) {
106  Sample.print(OS, 0);
107  return OS;
108 }
109 
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";
114 
115  OS.indent(Indent);
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;
122  }
123  OS.indent(Indent);
124  OS << "}\n";
125  } else {
126  OS << "No samples collected in the function's body\n";
127  }
128 
129  OS.indent(Indent);
130  if (!CallsiteSamples.empty()) {
131  OS << "Samples collected in inlined callsites {\n";
133  CallsiteSamples);
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);
139  }
140  }
141  OS << "}\n";
142  } else {
143  OS << "No inlined callsites in this function\n";
144  }
145 }
146 
148  const FunctionSamples &FS) {
149  FS.print(OS);
150  return OS;
151 }
152 
153 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
155 #endif
raw_ostream & operator<<(raw_ostream &OS, const LineLocation &Loc)
Definition: SampleProf.cpp:79
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
Definition: Compiler.h:449
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
const CallTargetMap & getCallTargets() const
Definition: SampleProf.h:163
Representation of the samples collected for a function.
Definition: SampleProf.h:196
Representation of a single sample record.
Definition: SampleProf.h:126
const SamplesWithLocList & get() const
Definition: SampleProf.h:414
Sort a LocationT->SampleT map by LocationT.
Definition: SampleProf.h:400
void print(raw_ostream &OS=dbgs(), unsigned Indent=0) const
Print the samples collected for a function on stream OS.
Definition: SampleProf.cpp:111
sampleprof_error
Definition: SampleProf.h:41
static ManagedStatic< _object_error_category > error_category
Definition: Error.cpp:74
#define E
Definition: LargeTest.cpp:27
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
const std::error_category & sampleprof_category()
Definition: SampleProf.cpp:69
static ManagedStatic< SampleProfErrorCategoryType > ErrorCategory
Definition: SampleProf.cpp:67
void print(raw_ostream &OS) const
Definition: SampleProf.cpp:73
bool hasCalls() const
Return true if this sample record contains function calls.
Definition: SampleProf.h:160
llvm_unreachable("Unknown RegBank!")
Represents the relative location of an instruction.
Definition: SampleProf.h:99
#define I(x, y, z)
Definition: MD5.cpp:58
aarch64 promote const
static const char * name
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
ManagedStatic - This transparently changes the behavior of global statics to be lazily constructed on...
Definition: ManagedStatic.h:61
void print(raw_ostream &OS, unsigned Indent) const
Print the sample record to the stream OS indented by Indent.
Definition: SampleProf.cpp:90