blob: b3ee61a7b978e01d5144e8b359ff60fdefdca662 [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 {
23class 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);
27 switch (E) {
28 case sampleprof_error::success:
29 return "Success";
30 case sampleprof_error::bad_magic:
Nathan Slingerland4f823662015-11-13 03:47:58 +000031 return "Invalid sample profile data (bad magic)";
Diego Novilloc572e922014-10-30 18:00:06 +000032 case sampleprof_error::unsupported_version:
Nathan Slingerland4f823662015-11-13 03:47:58 +000033 return "Unsupported sample profile format version";
Diego Novilloc572e922014-10-30 18:00:06 +000034 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:
Nathan Slingerland4f823662015-11-13 03:47:58 +000039 return "Malformed sample profile data";
Diego Novillod5336ae2014-11-01 00:56:55 +000040 case sampleprof_error::unrecognized_format:
Nathan Slingerland4f823662015-11-13 03:47:58 +000041 return "Unrecognized sample profile encoding format";
Diego Novillo760c5a82015-10-13 22:48:46 +000042 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";
Diego Novillo3376a782015-09-17 00:17:24 +000046 case sampleprof_error::not_implemented:
47 return "Unimplemented feature";
Diego Novilloc572e922014-10-30 18:00:06 +000048 }
49 llvm_unreachable("A value of sampleprof_error has no message.");
50 }
51};
Alexander Kornienkof00654e2015-06-23 09:49:53 +000052}
Diego Novilloc572e922014-10-30 18:00:06 +000053
54static ManagedStatic<SampleProfErrorCategoryType> ErrorCategory;
55
56const std::error_category &llvm::sampleprof_category() {
57 return *ErrorCategory;
58}
Diego Novillo4b6bdb52015-11-12 17:58:14 +000059
Diego Novillo8e415a82015-11-13 20:24:28 +000060/// \brief Print the sample record to the stream \p OS indented by \p Indent.
61void SampleRecord::print(raw_ostream &OS, unsigned Indent) const {
62 OS << NumSamples;
63 if (hasCalls()) {
64 OS << ", calls:";
65 for (const auto &I : getCallTargets())
66 OS << " " << I.first() << ":" << I.second;
67 }
68 OS << "\n";
69}
70
Diego Novillo4b6bdb52015-11-12 17:58:14 +000071/// \brief Print the samples collected for a function on stream \p OS.
Diego Novillo4b6bdb52015-11-12 17:58:14 +000072void FunctionSamples::print(raw_ostream &OS, unsigned Indent) const {
73 OS << TotalSamples << ", " << TotalHeadSamples << ", " << BodySamples.size()
74 << " sampled lines\n";
Diego Novillo8e415a82015-11-13 20:24:28 +000075
Diego Novillo4b6bdb52015-11-12 17:58:14 +000076 for (const auto &SI : BodySamples) {
Diego Novillo4b6bdb52015-11-12 17:58:14 +000077 OS.indent(Indent);
Diego Novillo8e415a82015-11-13 20:24:28 +000078 OS << SI.first << ": " << SI.second;
Diego Novillo4b6bdb52015-11-12 17:58:14 +000079 }
Diego Novillo8e415a82015-11-13 20:24:28 +000080
Diego Novillo4b6bdb52015-11-12 17:58:14 +000081 for (const auto &CS : CallsiteSamples) {
Diego Novillo4b6bdb52015-11-12 17:58:14 +000082 OS.indent(Indent);
Diego Novillo8e415a82015-11-13 20:24:28 +000083 OS << CS.first << ": ";
84 CS.second.print(OS, Indent + 2);
Diego Novillo4b6bdb52015-11-12 17:58:14 +000085 }
86}