blob: eafdd2154b7be608aa04fb9be07eb4d17b2f7cd3 [file] [log] [blame]
Diego Novilloc572e922014-10-30 18:00:06 +00001//=-- 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
15#include "llvm/ProfileData/SampleProf.h"
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000016#include "llvm/Support/Compiler.h"
17#include "llvm/Support/Debug.h"
Diego Novilloc572e922014-10-30 18:00:06 +000018#include "llvm/Support/ErrorHandling.h"
19#include "llvm/Support/ManagedStatic.h"
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000020#include "llvm/Support/raw_ostream.h"
21#include <string>
22#include <system_error>
Diego Novilloc572e922014-10-30 18:00:06 +000023
24using namespace llvm;
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000025using namespace sampleprof;
Diego Novilloc572e922014-10-30 18:00:06 +000026
27namespace {
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000028
Peter Collingbourne4718f8b2016-05-24 20:13:46 +000029// 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.
Diego Novilloc572e922014-10-30 18:00:06 +000032class SampleProfErrorCategoryType : public std::error_category {
Reid Kleckner990504e2016-10-19 23:52:38 +000033 const char *name() const noexcept override { return "llvm.sampleprof"; }
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000034
Diego Novilloc572e922014-10-30 18:00:06 +000035 std::string message(int IE) const override {
36 sampleprof_error E = static_cast<sampleprof_error>(IE);
37 switch (E) {
38 case sampleprof_error::success:
39 return "Success";
40 case sampleprof_error::bad_magic:
Nathan Slingerland4f823662015-11-13 03:47:58 +000041 return "Invalid sample profile data (bad magic)";
Diego Novilloc572e922014-10-30 18:00:06 +000042 case sampleprof_error::unsupported_version:
Nathan Slingerland4f823662015-11-13 03:47:58 +000043 return "Unsupported sample profile format version";
Diego Novilloc572e922014-10-30 18:00:06 +000044 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:
Nathan Slingerland4f823662015-11-13 03:47:58 +000049 return "Malformed sample profile data";
Diego Novillod5336ae2014-11-01 00:56:55 +000050 case sampleprof_error::unrecognized_format:
Nathan Slingerland4f823662015-11-13 03:47:58 +000051 return "Unrecognized sample profile encoding format";
Diego Novillo760c5a82015-10-13 22:48:46 +000052 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";
Diego Novillo3376a782015-09-17 00:17:24 +000056 case sampleprof_error::not_implemented:
57 return "Unimplemented feature";
Nathan Slingerland48dd0802015-12-16 21:45:43 +000058 case sampleprof_error::counter_overflow:
59 return "Counter overflow";
Diego Novilloc572e922014-10-30 18:00:06 +000060 }
61 llvm_unreachable("A value of sampleprof_error has no message.");
62 }
63};
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000064
65} // end anonymous namespace
Diego Novilloc572e922014-10-30 18:00:06 +000066
67static ManagedStatic<SampleProfErrorCategoryType> ErrorCategory;
68
69const std::error_category &llvm::sampleprof_category() {
70 return *ErrorCategory;
71}
Diego Novillo4b6bdb52015-11-12 17:58:14 +000072
Diego Novilloba920be2015-11-17 19:04:46 +000073void LineLocation::print(raw_ostream &OS) const {
74 OS << LineOffset;
75 if (Discriminator > 0)
76 OS << "." << Discriminator;
77}
78
79raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
80 const LineLocation &Loc) {
81 Loc.print(OS);
82 return OS;
83}
84
Aaron Ballman615eb472017-10-15 14:32:27 +000085#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Kereneb2a2542016-01-29 20:50:44 +000086LLVM_DUMP_METHOD void LineLocation::dump() const { print(dbgs()); }
Matthias Braun8c209aa2017-01-28 02:02:38 +000087#endif
Diego Novilloba920be2015-11-17 19:04:46 +000088
Diego Novillo8e415a82015-11-13 20:24:28 +000089/// \brief Print the sample record to the stream \p OS indented by \p Indent.
90void 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
Aaron Ballman615eb472017-10-15 14:32:27 +0000100#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Kereneb2a2542016-01-29 20:50:44 +0000101LLVM_DUMP_METHOD void SampleRecord::dump() const { print(dbgs(), 0); }
Matthias Braun8c209aa2017-01-28 02:02:38 +0000102#endif
Diego Novilloba920be2015-11-17 19:04:46 +0000103
104raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
105 const SampleRecord &Sample) {
106 Sample.print(OS, 0);
107 return OS;
108}
109
Diego Novillo4b6bdb52015-11-12 17:58:14 +0000110/// \brief Print the samples collected for a function on stream \p OS.
Diego Novillo4b6bdb52015-11-12 17:58:14 +0000111void FunctionSamples::print(raw_ostream &OS, unsigned Indent) const {
112 OS << TotalSamples << ", " << TotalHeadSamples << ", " << BodySamples.size()
113 << " sampled lines\n";
Diego Novillo8e415a82015-11-13 20:24:28 +0000114
Diego Novillo379cc5e2015-11-19 22:18:30 +0000115 OS.indent(Indent);
Eugene Zelenkoe78d1312017-03-03 01:07:34 +0000116 if (!BodySamples.empty()) {
Diego Novillo379cc5e2015-11-19 22:18:30 +0000117 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 }
Diego Novillo4b6bdb52015-11-12 17:58:14 +0000123 OS.indent(Indent);
Diego Novillo379cc5e2015-11-19 22:18:30 +0000124 OS << "}\n";
125 } else {
126 OS << "No samples collected in the function's body\n";
Diego Novillo4b6bdb52015-11-12 17:58:14 +0000127 }
Diego Novillo8e415a82015-11-13 20:24:28 +0000128
Diego Novillo379cc5e2015-11-19 22:18:30 +0000129 OS.indent(Indent);
Eugene Zelenkoe78d1312017-03-03 01:07:34 +0000130 if (!CallsiteSamples.empty()) {
Diego Novillo379cc5e2015-11-19 22:18:30 +0000131 OS << "Samples collected in inlined callsites {\n";
Dehao Chen2c7ca9b2017-04-13 19:52:10 +0000132 SampleSorter<LineLocation, FunctionSamplesMap> SortedCallsiteSamples(
Diego Novillo379cc5e2015-11-19 22:18:30 +0000133 CallsiteSamples);
134 for (const auto &CS : SortedCallsiteSamples.get()) {
Dehao Chen2c7ca9b2017-04-13 19:52:10 +0000135 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 }
Diego Novillo379cc5e2015-11-19 22:18:30 +0000140 }
141 OS << "}\n";
142 } else {
143 OS << "No inlined callsites in this function\n";
Diego Novillo4b6bdb52015-11-12 17:58:14 +0000144 }
145}
Diego Novilloba920be2015-11-17 19:04:46 +0000146
147raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
148 const FunctionSamples &FS) {
149 FS.print(OS);
150 return OS;
151}
152
Aaron Ballman615eb472017-10-15 14:32:27 +0000153#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Eugene Zelenkoe78d1312017-03-03 01:07:34 +0000154LLVM_DUMP_METHOD void FunctionSamples::dump() const { print(dbgs(), 0); }
Matthias Braun8c209aa2017-01-28 02:02:38 +0000155#endif