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

Generated by: LCOV version 1.13