blob: 8fe85d69bb630d0177b5103d5c24beb9617f4327 [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"
16#include "llvm/Support/ErrorHandling.h"
17#include "llvm/Support/ManagedStatic.h"
18
Diego Novillo4b6bdb52015-11-12 17:58:14 +000019using namespace llvm::sampleprof;
Diego Novilloc572e922014-10-30 18:00:06 +000020using namespace llvm;
21
22namespace {
Peter Collingbourne4718f8b2016-05-24 20:13:46 +000023// 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.
Diego Novilloc572e922014-10-30 18:00:06 +000026class SampleProfErrorCategoryType : public std::error_category {
Reid Kleckner990504e2016-10-19 23:52:38 +000027 const char *name() const noexcept override { return "llvm.sampleprof"; }
Diego Novilloc572e922014-10-30 18:00:06 +000028 std::string message(int IE) const override {
29 sampleprof_error E = static_cast<sampleprof_error>(IE);
30 switch (E) {
31 case sampleprof_error::success:
32 return "Success";
33 case sampleprof_error::bad_magic:
Nathan Slingerland4f823662015-11-13 03:47:58 +000034 return "Invalid sample profile data (bad magic)";
Diego Novilloc572e922014-10-30 18:00:06 +000035 case sampleprof_error::unsupported_version:
Nathan Slingerland4f823662015-11-13 03:47:58 +000036 return "Unsupported sample profile format version";
Diego Novilloc572e922014-10-30 18:00:06 +000037 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:
Nathan Slingerland4f823662015-11-13 03:47:58 +000042 return "Malformed sample profile data";
Diego Novillod5336ae2014-11-01 00:56:55 +000043 case sampleprof_error::unrecognized_format:
Nathan Slingerland4f823662015-11-13 03:47:58 +000044 return "Unrecognized sample profile encoding format";
Diego Novillo760c5a82015-10-13 22:48:46 +000045 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";
Diego Novillo3376a782015-09-17 00:17:24 +000049 case sampleprof_error::not_implemented:
50 return "Unimplemented feature";
Nathan Slingerland48dd0802015-12-16 21:45:43 +000051 case sampleprof_error::counter_overflow:
52 return "Counter overflow";
Diego Novilloc572e922014-10-30 18:00:06 +000053 }
54 llvm_unreachable("A value of sampleprof_error has no message.");
55 }
56};
Alexander Kornienkof00654e2015-06-23 09:49:53 +000057}
Diego Novilloc572e922014-10-30 18:00:06 +000058
59static ManagedStatic<SampleProfErrorCategoryType> ErrorCategory;
60
61const std::error_category &llvm::sampleprof_category() {
62 return *ErrorCategory;
63}
Diego Novillo4b6bdb52015-11-12 17:58:14 +000064
Diego Novilloba920be2015-11-17 19:04:46 +000065void LineLocation::print(raw_ostream &OS) const {
66 OS << LineOffset;
67 if (Discriminator > 0)
68 OS << "." << Discriminator;
69}
70
71raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
72 const LineLocation &Loc) {
73 Loc.print(OS);
74 return OS;
75}
76
Matthias Braun8c209aa2017-01-28 02:02:38 +000077#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Kereneb2a2542016-01-29 20:50:44 +000078LLVM_DUMP_METHOD void LineLocation::dump() const { print(dbgs()); }
Matthias Braun8c209aa2017-01-28 02:02:38 +000079#endif
Diego Novilloba920be2015-11-17 19:04:46 +000080
Diego Novillo8e415a82015-11-13 20:24:28 +000081/// \brief Print the sample record to the stream \p OS indented by \p Indent.
82void SampleRecord::print(raw_ostream &OS, unsigned Indent) const {
83 OS << NumSamples;
84 if (hasCalls()) {
85 OS << ", calls:";
86 for (const auto &I : getCallTargets())
87 OS << " " << I.first() << ":" << I.second;
88 }
89 OS << "\n";
90}
91
Matthias Braun8c209aa2017-01-28 02:02:38 +000092#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Kereneb2a2542016-01-29 20:50:44 +000093LLVM_DUMP_METHOD void SampleRecord::dump() const { print(dbgs(), 0); }
Matthias Braun8c209aa2017-01-28 02:02:38 +000094#endif
Diego Novilloba920be2015-11-17 19:04:46 +000095
96raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
97 const SampleRecord &Sample) {
98 Sample.print(OS, 0);
99 return OS;
100}
101
Diego Novillo4b6bdb52015-11-12 17:58:14 +0000102/// \brief Print the samples collected for a function on stream \p OS.
Diego Novillo4b6bdb52015-11-12 17:58:14 +0000103void FunctionSamples::print(raw_ostream &OS, unsigned Indent) const {
104 OS << TotalSamples << ", " << TotalHeadSamples << ", " << BodySamples.size()
105 << " sampled lines\n";
Diego Novillo8e415a82015-11-13 20:24:28 +0000106
Diego Novillo379cc5e2015-11-19 22:18:30 +0000107 OS.indent(Indent);
108 if (BodySamples.size() > 0) {
109 OS << "Samples collected in the function's body {\n";
110 SampleSorter<LineLocation, SampleRecord> SortedBodySamples(BodySamples);
111 for (const auto &SI : SortedBodySamples.get()) {
112 OS.indent(Indent + 2);
113 OS << SI->first << ": " << SI->second;
114 }
Diego Novillo4b6bdb52015-11-12 17:58:14 +0000115 OS.indent(Indent);
Diego Novillo379cc5e2015-11-19 22:18:30 +0000116 OS << "}\n";
117 } else {
118 OS << "No samples collected in the function's body\n";
Diego Novillo4b6bdb52015-11-12 17:58:14 +0000119 }
Diego Novillo8e415a82015-11-13 20:24:28 +0000120
Diego Novillo379cc5e2015-11-19 22:18:30 +0000121 OS.indent(Indent);
122 if (CallsiteSamples.size() > 0) {
123 OS << "Samples collected in inlined callsites {\n";
Dehao Chen57d1dda2016-03-03 18:09:32 +0000124 SampleSorter<LineLocation, FunctionSamples> SortedCallsiteSamples(
Diego Novillo379cc5e2015-11-19 22:18:30 +0000125 CallsiteSamples);
126 for (const auto &CS : SortedCallsiteSamples.get()) {
127 OS.indent(Indent + 2);
Dehao Chen57d1dda2016-03-03 18:09:32 +0000128 OS << CS->first << ": inlined callee: " << CS->second.getName() << ": ";
Diego Novillo379cc5e2015-11-19 22:18:30 +0000129 CS->second.print(OS, Indent + 4);
130 }
131 OS << "}\n";
132 } else {
133 OS << "No inlined callsites in this function\n";
Diego Novillo4b6bdb52015-11-12 17:58:14 +0000134 }
135}
Diego Novilloba920be2015-11-17 19:04:46 +0000136
137raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
138 const FunctionSamples &FS) {
139 FS.print(OS);
140 return OS;
141}
142
Matthias Braun8c209aa2017-01-28 02:02:38 +0000143#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
144LLVM_DUMP_METHOD void FunctionSamples::dump(void) const { print(dbgs(), 0); }
145#endif