LCOV - code coverage report
Current view: top level - lib/ProfileData - SampleProf.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 54 66 81.8 %
Date: 2017-09-14 15:23:50 Functions: 8 11 72.7 %
Legend: Lines: hit not hit

          Line data    Source code
       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             : 
      15             : #include "llvm/ProfileData/SampleProf.h"
      16             : #include "llvm/Support/Compiler.h"
      17             : #include "llvm/Support/Debug.h"
      18             : #include "llvm/Support/ErrorHandling.h"
      19             : #include "llvm/Support/ManagedStatic.h"
      20             : #include "llvm/Support/raw_ostream.h"
      21             : #include <string>
      22             : #include <system_error>
      23             : 
      24             : using namespace llvm;
      25             : using namespace sampleprof;
      26             : 
      27             : namespace {
      28             : 
      29             : // 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.
      32         186 : class SampleProfErrorCategoryType : public std::error_category {
      33           0 :   const char *name() const noexcept override { return "llvm.sampleprof"; }
      34             : 
      35           6 :   std::string message(int IE) const override {
      36           6 :     sampleprof_error E = static_cast<sampleprof_error>(IE);
      37           6 :     switch (E) {
      38             :     case sampleprof_error::success:
      39           0 :       return "Success";
      40             :     case sampleprof_error::bad_magic:
      41           0 :       return "Invalid sample profile data (bad magic)";
      42             :     case sampleprof_error::unsupported_version:
      43           0 :       return "Unsupported sample profile format version";
      44             :     case sampleprof_error::too_large:
      45           0 :       return "Too much profile data";
      46             :     case sampleprof_error::truncated:
      47           0 :       return "Truncated profile data";
      48             :     case sampleprof_error::malformed:
      49           0 :       return "Malformed sample profile data";
      50             :     case sampleprof_error::unrecognized_format:
      51           6 :       return "Unrecognized sample profile encoding format";
      52             :     case sampleprof_error::unsupported_writing_format:
      53           0 :       return "Profile encoding format unsupported for writing operations";
      54             :     case sampleprof_error::truncated_name_table:
      55           0 :       return "Truncated function name table";
      56             :     case sampleprof_error::not_implemented:
      57           0 :       return "Unimplemented feature";
      58             :     case sampleprof_error::counter_overflow:
      59           6 :       return "Counter overflow";
      60             :     }
      61           0 :     llvm_unreachable("A value of sampleprof_error has no message.");
      62             :   }
      63             : };
      64             : 
      65             : } // end anonymous namespace
      66             : 
      67             : static ManagedStatic<SampleProfErrorCategoryType> ErrorCategory;
      68             : 
      69        2523 : const std::error_category &llvm::sampleprof_category() {
      70        2523 :   return *ErrorCategory;
      71             : }
      72             : 
      73         162 : void LineLocation::print(raw_ostream &OS) const {
      74         324 :   OS << LineOffset;
      75         162 :   if (Discriminator > 0)
      76          90 :     OS << "." << Discriminator;
      77         162 : }
      78             : 
      79         162 : raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
      80             :                                           const LineLocation &Loc) {
      81         162 :   Loc.print(OS);
      82         162 :   return OS;
      83             : }
      84             : 
      85             : #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
      86             : LLVM_DUMP_METHOD void LineLocation::dump() const { print(dbgs()); }
      87             : #endif
      88             : 
      89             : /// \brief Print the sample record to the stream \p OS indented by \p Indent.
      90         132 : void SampleRecord::print(raw_ostream &OS, unsigned Indent) const {
      91         132 :   OS << NumSamples;
      92         132 :   if (hasCalls()) {
      93           7 :     OS << ", calls:";
      94          48 :     for (const auto &I : getCallTargets())
      95          20 :       OS << " " << I.first() << ":" << I.second;
      96             :   }
      97         132 :   OS << "\n";
      98         132 : }
      99             : 
     100             : #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
     101             : LLVM_DUMP_METHOD void SampleRecord::dump() const { print(dbgs(), 0); }
     102             : #endif
     103             : 
     104         132 : raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
     105             :                                           const SampleRecord &Sample) {
     106         132 :   Sample.print(OS, 0);
     107         132 :   return OS;
     108             : }
     109             : 
     110             : /// \brief Print the samples collected for a function on stream \p OS.
     111          52 : void FunctionSamples::print(raw_ostream &OS, unsigned Indent) const {
     112         104 :   OS << TotalSamples << ", " << TotalHeadSamples << ", " << BodySamples.size()
     113          52 :      << " sampled lines\n";
     114             : 
     115          52 :   OS.indent(Indent);
     116         104 :   if (!BodySamples.empty()) {
     117          52 :     OS << "Samples collected in the function's body {\n";
     118         104 :     SampleSorter<LineLocation, SampleRecord> SortedBodySamples(BodySamples);
     119         288 :     for (const auto &SI : SortedBodySamples.get()) {
     120         132 :       OS.indent(Indent + 2);
     121         132 :       OS << SI->first << ": " << SI->second;
     122             :     }
     123          52 :     OS.indent(Indent);
     124          52 :     OS << "}\n";
     125             :   } else {
     126           0 :     OS << "No samples collected in the function's body\n";
     127             :   }
     128             : 
     129          52 :   OS.indent(Indent);
     130         104 :   if (!CallsiteSamples.empty()) {
     131          15 :     OS << "Samples collected in inlined callsites {\n";
     132             :     SampleSorter<LineLocation, FunctionSamplesMap> SortedCallsiteSamples(
     133          30 :         CallsiteSamples);
     134          72 :     for (const auto &CS : SortedCallsiteSamples.get()) {
     135         168 :       for (const auto &FS : CS->second) {
     136          30 :         OS.indent(Indent + 2);
     137          60 :         OS << CS->first << ": inlined callee: " << FS.second.getName() << ": ";
     138          30 :         FS.second.print(OS, Indent + 4);
     139             :       }
     140             :     }
     141          15 :     OS << "}\n";
     142             :   } else {
     143          37 :     OS << "No inlined callsites in this function\n";
     144             :   }
     145          52 : }
     146             : 
     147          22 : raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
     148             :                                           const FunctionSamples &FS) {
     149          22 :   FS.print(OS);
     150          22 :   return OS;
     151             : }
     152             : 
     153             : #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
     154             : LLVM_DUMP_METHOD void FunctionSamples::dump() const { print(dbgs(), 0); }
     155             : #endif

Generated by: LCOV version 1.13