LCOV - code coverage report
Current view: top level - lib/ProfileData - SampleProfWriter.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 152 161 94.4 %
Date: 2017-09-14 15:23:50 Functions: 12 12 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- SampleProfWriter.cpp - Write LLVM sample profile data --------------===//
       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 implements the class that writes LLVM sample profiles. It
      11             : // supports two file formats: text and binary. The textual representation
      12             : // is useful for debugging and testing purposes. The binary representation
      13             : // is more compact, resulting in smaller file sizes. However, they can
      14             : // both be used interchangeably.
      15             : //
      16             : // See lib/ProfileData/SampleProfReader.cpp for documentation on each of the
      17             : // supported formats.
      18             : //
      19             : //===----------------------------------------------------------------------===//
      20             : 
      21             : #include "llvm/ProfileData/SampleProfWriter.h"
      22             : #include "llvm/ADT/StringRef.h"
      23             : #include "llvm/ProfileData/ProfileCommon.h"
      24             : #include "llvm/ProfileData/SampleProf.h"
      25             : #include "llvm/Support/ErrorOr.h"
      26             : #include "llvm/Support/FileSystem.h"
      27             : #include "llvm/Support/LEB128.h"
      28             : #include "llvm/Support/raw_ostream.h"
      29             : #include <algorithm>
      30             : #include <cstdint>
      31             : #include <memory>
      32             : #include <set>
      33             : #include <system_error>
      34             : #include <utility>
      35             : #include <vector>
      36             : 
      37             : using namespace llvm;
      38             : using namespace sampleprof;
      39             : 
      40             : std::error_code
      41          18 : SampleProfileWriter::write(const StringMap<FunctionSamples> &ProfileMap) {
      42          18 :   if (std::error_code EC = writeHeader(ProfileMap))
      43           0 :     return EC;
      44             : 
      45             :   // Sort the ProfileMap by total samples.
      46             :   typedef std::pair<StringRef, const FunctionSamples *> NameFunctionSamples;
      47          18 :   std::vector<NameFunctionSamples> V;
      48         142 :   for (const auto &I : ProfileMap)
      49         140 :     V.push_back(std::make_pair(I.getKey(), &I.second));
      50             : 
      51          36 :   std::stable_sort(
      52             :       V.begin(), V.end(),
      53             :       [](const NameFunctionSamples &A, const NameFunctionSamples &B) {
      54          26 :         if (A.second->getTotalSamples() == B.second->getTotalSamples())
      55           3 :           return A.first > B.first;
      56          23 :         return A.second->getTotalSamples() > B.second->getTotalSamples();
      57             :       });
      58             : 
      59         107 :   for (const auto &I : V) {
      60          35 :     if (std::error_code EC = write(*I.second))
      61           0 :       return EC;
      62             :   }
      63          18 :   return sampleprof_error::success;
      64             : }
      65             : 
      66             : /// \brief Write samples to a text file.
      67             : ///
      68             : /// Note: it may be tempting to implement this in terms of
      69             : /// FunctionSamples::print().  Please don't.  The dump functionality is intended
      70             : /// for debugging and has no specified form.
      71             : ///
      72             : /// The format used here is more structured and deliberate because
      73             : /// it needs to be parsed by the SampleProfileReaderText class.
      74          43 : std::error_code SampleProfileWriterText::write(const FunctionSamples &S) {
      75          86 :   auto &OS = *OutputStream;
      76          43 :   OS << S.getName() << ":" << S.getTotalSamples();
      77          43 :   if (Indent == 0)
      78          17 :     OS << ":" << S.getHeadSamples();
      79          43 :   OS << "\n";
      80             : 
      81          86 :   SampleSorter<LineLocation, SampleRecord> SortedSamples(S.getBodySamples());
      82         278 :   for (const auto &I : SortedSamples.get()) {
      83         149 :     LineLocation Loc = I->first;
      84         149 :     const SampleRecord &Sample = I->second;
      85         149 :     OS.indent(Indent + 1);
      86         149 :     if (Loc.Discriminator == 0)
      87         174 :       OS << Loc.LineOffset << ": ";
      88             :     else
      89         186 :       OS << Loc.LineOffset << "." << Loc.Discriminator << ": ";
      90             : 
      91         149 :     OS << Sample.getSamples();
      92             : 
      93         612 :     for (const auto &J : Sample.getCallTargets())
      94          16 :       OS << " " << J.first() << ":" << J.second;
      95         149 :     OS << "\n";
      96             :   }
      97             : 
      98             :   SampleSorter<LineLocation, FunctionSamplesMap> SortedCallsiteSamples(
      99          86 :       S.getCallsiteSamples());
     100          43 :   Indent += 1;
     101         154 :   for (const auto &I : SortedCallsiteSamples.get())
     102         152 :     for (const auto &FS : I->second) {
     103          26 :       LineLocation Loc = I->first;
     104          26 :       const FunctionSamples &CalleeSamples = FS.second;
     105          26 :       OS.indent(Indent);
     106          26 :       if (Loc.Discriminator == 0)
     107           4 :         OS << Loc.LineOffset << ": ";
     108             :       else
     109          72 :         OS << Loc.LineOffset << "." << Loc.Discriminator << ": ";
     110          26 :       if (std::error_code EC = write(CalleeSamples))
     111           0 :         return EC;
     112             :     }
     113          43 :   Indent -= 1;
     114             : 
     115          43 :   return sampleprof_error::success;
     116             : }
     117             : 
     118          36 : std::error_code SampleProfileWriterBinary::writeNameIdx(StringRef FName) {
     119          36 :   const auto &ret = NameTable.find(FName);
     120         108 :   if (ret == NameTable.end())
     121           0 :     return sampleprof_error::truncated_name_table;
     122          72 :   encodeULEB128(ret->second, *OutputStream);
     123          36 :   return sampleprof_error::success;
     124             : }
     125             : 
     126          36 : void SampleProfileWriterBinary::addName(StringRef FName) {
     127         108 :   NameTable.insert(std::make_pair(FName, 0));
     128          36 : }
     129             : 
     130          27 : void SampleProfileWriterBinary::addNames(const FunctionSamples &S) {
     131             :   // Add all the names in indirect call targets.
     132         134 :   for (const auto &I : S.getBodySamples()) {
     133          53 :     const SampleRecord &Sample = I.second;
     134         230 :     for (const auto &J : Sample.getCallTargets())
     135           9 :       addName(J.first());
     136             :   }
     137             : 
     138             :   // Recursively add all the names for inlined callsites.
     139          88 :   for (const auto &J : S.getCallsiteSamples())
     140          46 :     for (const auto &FS : J.second) {
     141           9 :       const FunctionSamples &CalleeSamples = FS.second;
     142           9 :       addName(CalleeSamples.getName());
     143           9 :       addNames(CalleeSamples);
     144             :     }
     145          27 : }
     146             : 
     147           7 : std::error_code SampleProfileWriterBinary::writeHeader(
     148             :     const StringMap<FunctionSamples> &ProfileMap) {
     149          14 :   auto &OS = *OutputStream;
     150             : 
     151             :   // Write file magic identifier.
     152           7 :   encodeULEB128(SPMagic(), OS);
     153           7 :   encodeULEB128(SPVersion(), OS);
     154             : 
     155           7 :   computeSummary(ProfileMap);
     156           7 :   if (auto EC = writeSummary())
     157           0 :     return EC;
     158             : 
     159             :   // Generate the name table for all the functions referenced in the profile.
     160          64 :   for (const auto &I : ProfileMap) {
     161          18 :     addName(I.first());
     162          18 :     addNames(I.second);
     163             :   }
     164             : 
     165             :   // Sort the names to make NameTable is deterministic.
     166           7 :   std::set<StringRef> V;
     167          52 :   for (const auto &I : NameTable)
     168          48 :     V.insert(I.first);
     169           7 :   int i = 0;
     170          45 :   for (const StringRef &N : V)
     171          24 :     NameTable[N] = i++;
     172             : 
     173             :   // Write out the name table.
     174          14 :   encodeULEB128(NameTable.size(), OS);
     175          45 :   for (auto N : V) {
     176          24 :     OS << N;
     177          24 :     encodeULEB128(0, OS);
     178             :   }
     179           7 :   return sampleprof_error::success;
     180             : }
     181             : 
     182           7 : std::error_code SampleProfileWriterBinary::writeSummary() {
     183          14 :   auto &OS = *OutputStream;
     184          14 :   encodeULEB128(Summary->getTotalCount(), OS);
     185          14 :   encodeULEB128(Summary->getMaxCount(), OS);
     186          14 :   encodeULEB128(Summary->getMaxFunctionCount(), OS);
     187          14 :   encodeULEB128(Summary->getNumCounts(), OS);
     188          14 :   encodeULEB128(Summary->getNumFunctions(), OS);
     189          21 :   std::vector<ProfileSummaryEntry> &Entries = Summary->getDetailedSummary();
     190           7 :   encodeULEB128(Entries.size(), OS);
     191         154 :   for (auto Entry : Entries) {
     192         126 :     encodeULEB128(Entry.Cutoff, OS);
     193         126 :     encodeULEB128(Entry.MinCount, OS);
     194         126 :     encodeULEB128(Entry.NumCounts, OS);
     195             :   }
     196           7 :   return sampleprof_error::success;
     197             : }
     198          27 : std::error_code SampleProfileWriterBinary::writeBody(const FunctionSamples &S) {
     199          54 :   auto &OS = *OutputStream;
     200             : 
     201          27 :   if (std::error_code EC = writeNameIdx(S.getName()))
     202           0 :     return EC;
     203             : 
     204          27 :   encodeULEB128(S.getTotalSamples(), OS);
     205             : 
     206             :   // Emit all the body samples.
     207          54 :   encodeULEB128(S.getBodySamples().size(), OS);
     208         134 :   for (const auto &I : S.getBodySamples()) {
     209          53 :     LineLocation Loc = I.first;
     210          53 :     const SampleRecord &Sample = I.second;
     211          53 :     encodeULEB128(Loc.LineOffset, OS);
     212          53 :     encodeULEB128(Loc.Discriminator, OS);
     213          53 :     encodeULEB128(Sample.getSamples(), OS);
     214          53 :     encodeULEB128(Sample.getCallTargets().size(), OS);
     215         230 :     for (const auto &J : Sample.getCallTargets()) {
     216           9 :       StringRef Callee = J.first();
     217           9 :       uint64_t CalleeSamples = J.second;
     218           9 :       if (std::error_code EC = writeNameIdx(Callee))
     219           0 :         return EC;
     220           9 :       encodeULEB128(CalleeSamples, OS);
     221             :     }
     222             :   }
     223             : 
     224             :   // Recursively emit all the callsite samples.
     225          27 :   uint64_t NumCallsites = 0;
     226          88 :   for (const auto &J : S.getCallsiteSamples())
     227           7 :     NumCallsites += J.second.size();
     228          27 :   encodeULEB128(NumCallsites, OS);
     229          88 :   for (const auto &J : S.getCallsiteSamples())
     230          46 :     for (const auto &FS : J.second) {
     231           9 :       LineLocation Loc = J.first;
     232           9 :       const FunctionSamples &CalleeSamples = FS.second;
     233           9 :       encodeULEB128(Loc.LineOffset, OS);
     234           9 :       encodeULEB128(Loc.Discriminator, OS);
     235           9 :       if (std::error_code EC = writeBody(CalleeSamples))
     236           0 :         return EC;
     237             :     }
     238             : 
     239          27 :   return sampleprof_error::success;
     240             : }
     241             : 
     242             : /// \brief Write samples of a top-level function to a binary file.
     243             : ///
     244             : /// \returns true if the samples were written successfully, false otherwise.
     245          18 : std::error_code SampleProfileWriterBinary::write(const FunctionSamples &S) {
     246          36 :   encodeULEB128(S.getHeadSamples(), *OutputStream);
     247          18 :   return writeBody(S);
     248             : }
     249             : 
     250             : /// \brief Create a sample profile file writer based on the specified format.
     251             : ///
     252             : /// \param Filename The file to create.
     253             : ///
     254             : /// \param Writer The writer to instantiate according to the specified format.
     255             : ///
     256             : /// \param Format Encoding format for the profile file.
     257             : ///
     258             : /// \returns an error code indicating the status of the created writer.
     259             : ErrorOr<std::unique_ptr<SampleProfileWriter>>
     260          16 : SampleProfileWriter::create(StringRef Filename, SampleProfileFormat Format) {
     261          16 :   std::error_code EC;
     262          32 :   std::unique_ptr<raw_ostream> OS;
     263          16 :   if (Format == SPF_Binary)
     264          12 :     OS.reset(new raw_fd_ostream(Filename, EC, sys::fs::F_None));
     265             :   else
     266          20 :     OS.reset(new raw_fd_ostream(Filename, EC, sys::fs::F_Text));
     267          16 :   if (EC)
     268             :     return EC;
     269             : 
     270          16 :   return create(OS, Format);
     271             : }
     272             : 
     273             : /// \brief Create a sample profile stream writer based on the specified format.
     274             : ///
     275             : /// \param OS The output stream to store the profile data to.
     276             : ///
     277             : /// \param Writer The writer to instantiate according to the specified format.
     278             : ///
     279             : /// \param Format Encoding format for the profile file.
     280             : ///
     281             : /// \returns an error code indicating the status of the created writer.
     282             : ErrorOr<std::unique_ptr<SampleProfileWriter>>
     283          18 : SampleProfileWriter::create(std::unique_ptr<raw_ostream> &OS,
     284             :                             SampleProfileFormat Format) {
     285          18 :   std::error_code EC;
     286          36 :   std::unique_ptr<SampleProfileWriter> Writer;
     287             : 
     288          18 :   if (Format == SPF_Binary)
     289          14 :     Writer.reset(new SampleProfileWriterBinary(OS));
     290          11 :   else if (Format == SPF_Text)
     291          22 :     Writer.reset(new SampleProfileWriterText(OS));
     292           0 :   else if (Format == SPF_GCC)
     293             :     EC = sampleprof_error::unsupported_writing_format;
     294             :   else
     295             :     EC = sampleprof_error::unrecognized_format;
     296             : 
     297          18 :   if (EC)
     298             :     return EC;
     299             : 
     300          18 :   return std::move(Writer);
     301             : }
     302             : 
     303           7 : void SampleProfileWriter::computeSummary(
     304             :     const StringMap<FunctionSamples> &ProfileMap) {
     305          28 :   SampleProfileSummaryBuilder Builder(ProfileSummaryBuilder::DefaultCutoffs);
     306          64 :   for (const auto &I : ProfileMap) {
     307          18 :     const FunctionSamples &Profile = I.second;
     308          18 :     Builder.addRecord(Profile);
     309             :   }
     310          21 :   Summary = Builder.getSummary();
     311           7 : }

Generated by: LCOV version 1.13