LCOV - code coverage report
Current view: top level - lib/ProfileData - SampleProf.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 64 76 84.2 %
Date: 2018-07-13 00:08:38 Functions: 10 13 76.9 %
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/Config/llvm-config.h"
      17             : #include "llvm/IR/DebugInfoMetadata.h"
      18             : #include "llvm/Support/Compiler.h"
      19             : #include "llvm/Support/Debug.h"
      20             : #include "llvm/Support/ErrorHandling.h"
      21             : #include "llvm/Support/ManagedStatic.h"
      22             : #include "llvm/Support/raw_ostream.h"
      23             : #include <string>
      24             : #include <system_error>
      25             : 
      26             : using namespace llvm;
      27             : using namespace sampleprof;
      28             : 
      29             : namespace {
      30             : 
      31             : // FIXME: This class is only here to support the transition to llvm::Error. It
      32             : // will be removed once this transition is complete. Clients should prefer to
      33             : // deal with the Error value directly, rather than converting to error_code.
      34         203 : class SampleProfErrorCategoryType : public std::error_category {
      35           0 :   const char *name() const noexcept override { return "llvm.sampleprof"; }
      36             : 
      37           6 :   std::string message(int IE) const override {
      38             :     sampleprof_error E = static_cast<sampleprof_error>(IE);
      39           6 :     switch (E) {
      40             :     case sampleprof_error::success:
      41           0 :       return "Success";
      42             :     case sampleprof_error::bad_magic:
      43           0 :       return "Invalid sample profile data (bad magic)";
      44             :     case sampleprof_error::unsupported_version:
      45           0 :       return "Unsupported sample profile format version";
      46             :     case sampleprof_error::too_large:
      47           0 :       return "Too much profile data";
      48             :     case sampleprof_error::truncated:
      49           0 :       return "Truncated profile data";
      50             :     case sampleprof_error::malformed:
      51           0 :       return "Malformed sample profile data";
      52             :     case sampleprof_error::unrecognized_format:
      53           3 :       return "Unrecognized sample profile encoding format";
      54             :     case sampleprof_error::unsupported_writing_format:
      55           0 :       return "Profile encoding format unsupported for writing operations";
      56             :     case sampleprof_error::truncated_name_table:
      57           0 :       return "Truncated function name table";
      58             :     case sampleprof_error::not_implemented:
      59           0 :       return "Unimplemented feature";
      60             :     case sampleprof_error::counter_overflow:
      61           3 :       return "Counter overflow";
      62             :     }
      63           0 :     llvm_unreachable("A value of sampleprof_error has no message.");
      64             :   }
      65             : };
      66             : 
      67             : } // end anonymous namespace
      68             : 
      69             : static ManagedStatic<SampleProfErrorCategoryType> ErrorCategory;
      70             : 
      71        2898 : const std::error_category &llvm::sampleprof_category() {
      72        2898 :   return *ErrorCategory;
      73             : }
      74             : 
      75         162 : void LineLocation::print(raw_ostream &OS) const {
      76         162 :   OS << LineOffset;
      77         162 :   if (Discriminator > 0)
      78          90 :     OS << "." << Discriminator;
      79         162 : }
      80             : 
      81         162 : raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
      82             :                                           const LineLocation &Loc) {
      83         162 :   Loc.print(OS);
      84         162 :   return OS;
      85             : }
      86             : 
      87             : #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
      88             : LLVM_DUMP_METHOD void LineLocation::dump() const { print(dbgs()); }
      89             : #endif
      90             : 
      91             : /// Print the sample record to the stream \p OS indented by \p Indent.
      92         132 : void SampleRecord::print(raw_ostream &OS, unsigned Indent) const {
      93         132 :   OS << NumSamples;
      94         132 :   if (hasCalls()) {
      95           7 :     OS << ", calls:";
      96          24 :     for (const auto &I : getCallTargets())
      97          20 :       OS << " " << I.first() << ":" << I.second;
      98             :   }
      99         132 :   OS << "\n";
     100         132 : }
     101             : 
     102             : #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
     103             : LLVM_DUMP_METHOD void SampleRecord::dump() const { print(dbgs(), 0); }
     104             : #endif
     105             : 
     106         132 : raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
     107             :                                           const SampleRecord &Sample) {
     108         132 :   Sample.print(OS, 0);
     109         132 :   return OS;
     110             : }
     111             : 
     112             : /// Print the samples collected for a function on stream \p OS.
     113          52 : void FunctionSamples::print(raw_ostream &OS, unsigned Indent) const {
     114         104 :   OS << TotalSamples << ", " << TotalHeadSamples << ", " << BodySamples.size()
     115          52 :      << " sampled lines\n";
     116             : 
     117          52 :   OS.indent(Indent);
     118          52 :   if (!BodySamples.empty()) {
     119          52 :     OS << "Samples collected in the function's body {\n";
     120          52 :     SampleSorter<LineLocation, SampleRecord> SortedBodySamples(BodySamples);
     121         316 :     for (const auto &SI : SortedBodySamples.get()) {
     122         132 :       OS.indent(Indent + 2);
     123         132 :       OS << SI->first << ": " << SI->second;
     124             :     }
     125          52 :     OS.indent(Indent);
     126          52 :     OS << "}\n";
     127             :   } else {
     128           0 :     OS << "No samples collected in the function's body\n";
     129             :   }
     130             : 
     131          52 :   OS.indent(Indent);
     132          52 :   if (!CallsiteSamples.empty()) {
     133          15 :     OS << "Samples collected in inlined callsites {\n";
     134             :     SampleSorter<LineLocation, FunctionSamplesMap> SortedCallsiteSamples(
     135          15 :         CallsiteSamples);
     136          69 :     for (const auto &CS : SortedCallsiteSamples.get()) {
     137          84 :       for (const auto &FS : CS->second) {
     138          30 :         OS.indent(Indent + 2);
     139          60 :         OS << CS->first << ": inlined callee: " << FS.second.getName() << ": ";
     140          30 :         FS.second.print(OS, Indent + 4);
     141             :       }
     142             :     }
     143          15 :     OS << "}\n";
     144             :   } else {
     145          37 :     OS << "No inlined callsites in this function\n";
     146             :   }
     147          52 : }
     148             : 
     149          22 : raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
     150             :                                           const FunctionSamples &FS) {
     151          22 :   FS.print(OS);
     152          22 :   return OS;
     153             : }
     154             : 
     155        1268 : unsigned FunctionSamples::getOffset(const DILocation *DIL) {
     156        2536 :   return (DIL->getLine() - DIL->getScope()->getSubprogram()->getLine()) &
     157        1268 :       0xffff;
     158             : }
     159             : 
     160             : const FunctionSamples *
     161        1448 : FunctionSamples::findFunctionSamples(const DILocation *DIL) const {
     162             :   assert(DIL);
     163             :   SmallVector<std::pair<LineLocation, StringRef>, 10> S;
     164             : 
     165             :   const DILocation *PrevDIL = DIL;
     166        1603 :   for (DIL = DIL->getInlinedAt(); DIL; DIL = DIL->getInlinedAt()) {
     167         155 :     S.push_back(std::make_pair(
     168         155 :         LineLocation(getOffset(DIL), DIL->getBaseDiscriminator()),
     169         155 :         PrevDIL->getScope()->getSubprogram()->getLinkageName()));
     170             :     PrevDIL = DIL;
     171             :   }
     172        1448 :   if (S.size() == 0)
     173             :     return this;
     174             :   const FunctionSamples *FS = this;
     175         310 :   for (int i = S.size() - 1; i >= 0 && FS != nullptr; i--) {
     176         310 :     FS = FS->findFunctionSamplesAt(S[i].first, S[i].second);
     177             :   }
     178             :   return FS;
     179             : }
     180             : 
     181             : #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
     182             : LLVM_DUMP_METHOD void FunctionSamples::dump() const { print(dbgs(), 0); }
     183             : #endif

Generated by: LCOV version 1.13