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 class SampleProfErrorCategoryType : public std::error_category {
24 const char *name() const LLVM_NOEXCEPT override { return "llvm.sampleprof"; }
25 std::string message(int IE) const override {
26 sampleprof_error E = static_cast<sampleprof_error>(IE);
28 case sampleprof_error::success:
30 case sampleprof_error::bad_magic:
31 return "Invalid file format (bad magic)";
32 case sampleprof_error::unsupported_version:
33 return "Unsupported format version";
34 case sampleprof_error::too_large:
35 return "Too much profile data";
36 case sampleprof_error::truncated:
37 return "Truncated profile data";
38 case sampleprof_error::malformed:
39 return "Malformed profile data";
40 case sampleprof_error::unrecognized_format:
41 return "Unrecognized profile encoding format";
42 case sampleprof_error::unsupported_writing_format:
43 return "Profile encoding format unsupported for writing operations";
44 case sampleprof_error::truncated_name_table:
45 return "Truncated function name table";
46 case sampleprof_error::not_implemented:
47 return "Unimplemented feature";
49 llvm_unreachable("A value of sampleprof_error has no message.");
54 static ManagedStatic<SampleProfErrorCategoryType> ErrorCategory;
56 const std::error_category &llvm::sampleprof_category() {
57 return *ErrorCategory;
60 /// \brief Print the samples collected for a function on stream \p OS.
62 /// \param OS Stream to emit the output to.
63 void FunctionSamples::print(raw_ostream &OS, unsigned Indent) const {
64 OS << TotalSamples << ", " << TotalHeadSamples << ", " << BodySamples.size()
65 << " sampled lines\n";
66 for (const auto &SI : BodySamples) {
67 LineLocation Loc = SI.first;
68 const SampleRecord &Sample = SI.second;
70 OS << "line offset: " << Loc.LineOffset
71 << ", discriminator: " << Loc.Discriminator
72 << ", number of samples: " << Sample.getSamples();
73 if (Sample.hasCalls()) {
75 for (const auto &I : Sample.getCallTargets())
76 OS << " " << I.first() << ":" << I.second;
80 for (const auto &CS : CallsiteSamples) {
81 CallsiteLocation Loc = CS.first;
82 const FunctionSamples &CalleeSamples = CS.second;
84 OS << "line offset: " << Loc.LineOffset
85 << ", discriminator: " << Loc.Discriminator
86 << ", inlined callee: " << Loc.CalleeName << ": ";
87 CalleeSamples.print(OS, Indent + 2);