LLVM  4.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 
18 
19 using namespace llvm::sampleprof;
20 using namespace llvm;
21 
22 namespace {
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);
30  switch (E) {
32  return "Success";
34  return "Invalid sample profile data (bad magic)";
36  return "Unsupported sample profile format version";
38  return "Too much profile data";
40  return "Truncated profile data";
42  return "Malformed sample profile data";
44  return "Unrecognized sample profile encoding format";
46  return "Profile encoding format unsupported for writing operations";
48  return "Truncated function name table";
50  return "Unimplemented feature";
52  return "Counter overflow";
53  }
54  llvm_unreachable("A value of sampleprof_error has no message.");
55  }
56 };
57 }
58 
60 
62  return *ErrorCategory;
63 }
64 
66  OS << LineOffset;
67  if (Discriminator > 0)
68  OS << "." << Discriminator;
69 }
70 
72  const LineLocation &Loc) {
73  Loc.print(OS);
74  return OS;
75 }
76 
77 LLVM_DUMP_METHOD void LineLocation::dump() const { print(dbgs()); }
78 
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 {
81  OS << NumSamples;
82  if (hasCalls()) {
83  OS << ", calls:";
84  for (const auto &I : getCallTargets())
85  OS << " " << I.first() << ":" << I.second;
86  }
87  OS << "\n";
88 }
89 
90 LLVM_DUMP_METHOD void SampleRecord::dump() const { print(dbgs(), 0); }
91 
93  const SampleRecord &Sample) {
94  Sample.print(OS, 0);
95  return OS;
96 }
97 
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";
102 
103  OS.indent(Indent);
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;
110  }
111  OS.indent(Indent);
112  OS << "}\n";
113  } else {
114  OS << "No samples collected in the function's body\n";
115  }
116 
117  OS.indent(Indent);
118  if (CallsiteSamples.size() > 0) {
119  OS << "Samples collected in inlined callsites {\n";
120  SampleSorter<LineLocation, FunctionSamples> SortedCallsiteSamples(
121  CallsiteSamples);
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);
126  }
127  OS << "}\n";
128  } else {
129  OS << "No inlined callsites in this function\n";
130  }
131 }
132 
134  const FunctionSamples &FS) {
135  FS.print(OS);
136  return OS;
137 }
138 
139 void FunctionSamples::dump(void) const { print(dbgs(), 0); }
raw_ostream & operator<<(raw_ostream &OS, const LineLocation &Loc)
Definition: SampleProf.cpp:71
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
Definition: Compiler.h:450
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
Representation of the samples collected for a function.
Definition: SampleProf.h:181
Representation of a single sample record.
Definition: SampleProf.h:113
const SamplesWithLocList & get() const
Definition: SampleProf.h:358
Sort a LocationT->SampleT map by LocationT.
Definition: SampleProf.h:345
static GCRegistry::Add< CoreCLRGC > E("coreclr","CoreCLR-compatible GC")
void print(raw_ostream &OS=dbgs(), unsigned Indent=0) const
Print the samples collected for a function on stream OS.
Definition: SampleProf.cpp:99
sampleprof_error
Definition: SampleProf.h:31
static ManagedStatic< _object_error_category > error_category
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
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:61
static ManagedStatic< SampleProfErrorCategoryType > ErrorCategory
Definition: SampleProf.cpp:59
void print(raw_ostream &OS) const
Definition: SampleProf.cpp:65
Represents the relative location of an instruction.
Definition: SampleProf.h:88
#define I(x, y, z)
Definition: MD5.cpp:54
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:63
void print(raw_ostream &OS, unsigned Indent) const
Print the sample record to the stream OS indented by Indent.
Definition: SampleProf.cpp:80