LCOV - code coverage report
Current view: top level - lib/ProfileData - InstrProfWriter.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 158 158 100.0 %
Date: 2018-06-17 00:07:59 Functions: 18 18 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- InstrProfWriter.cpp - Instrumented profiling writer ----------------===//
       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 support for writing profiling data for clang's
      11             : // instrumentation based PGO and coverage.
      12             : //
      13             : //===----------------------------------------------------------------------===//
      14             : 
      15             : #include "llvm/ProfileData/InstrProfWriter.h"
      16             : #include "llvm/ADT/STLExtras.h"
      17             : #include "llvm/ADT/StringRef.h"
      18             : #include "llvm/IR/ProfileSummary.h"
      19             : #include "llvm/ProfileData/InstrProf.h"
      20             : #include "llvm/ProfileData/ProfileCommon.h"
      21             : #include "llvm/Support/Endian.h"
      22             : #include "llvm/Support/EndianStream.h"
      23             : #include "llvm/Support/Error.h"
      24             : #include "llvm/Support/MemoryBuffer.h"
      25             : #include "llvm/Support/OnDiskHashTable.h"
      26             : #include "llvm/Support/raw_ostream.h"
      27             : #include <algorithm>
      28             : #include <cstdint>
      29             : #include <memory>
      30             : #include <string>
      31             : #include <tuple>
      32             : #include <utility>
      33             : #include <vector>
      34             : 
      35             : using namespace llvm;
      36             : 
      37             : // A struct to define how the data stream should be patched. For Indexed
      38             : // profiling, only uint64_t data type is needed.
      39             : struct PatchItem {
      40             :   uint64_t Pos; // Where to patch.
      41             :   uint64_t *D;  // Pointer to an array of source data.
      42             :   int N;        // Number of elements in \c D array.
      43             : };
      44             : 
      45             : namespace llvm {
      46             : 
      47             : // A wrapper class to abstract writer stream with support of bytes
      48             : // back patching.
      49             : class ProfOStream {
      50             : public:
      51             :   ProfOStream(raw_fd_ostream &FD)
      52          93 :       : IsFDOStream(true), OS(FD), LE(FD, support::little) {}
      53             :   ProfOStream(raw_string_ostream &STR)
      54         129 :       : IsFDOStream(false), OS(STR), LE(STR, support::little) {}
      55             : 
      56             :   uint64_t tell() { return OS.tell(); }
      57       18843 :   void write(uint64_t V) { LE.write<uint64_t>(V); }
      58             : 
      59             :   // \c patch can only be called when all data is written and flushed.
      60             :   // For raw_string_ostream, the patch is done on the target string
      61             :   // directly and it won't be reflected in the stream's internal buffer.
      62         222 :   void patch(PatchItem *P, int NItems) {
      63             :     using namespace support;
      64             : 
      65         222 :     if (IsFDOStream) {
      66          93 :       raw_fd_ostream &FDOStream = static_cast<raw_fd_ostream &>(OS);
      67         465 :       for (int K = 0; K < NItems; K++) {
      68         186 :         FDOStream.seek(P[K].Pos);
      69       10788 :         for (int I = 0; I < P[K].N; I++)
      70        5301 :           write(P[K].D[I]);
      71             :       }
      72             :     } else {
      73         129 :       raw_string_ostream &SOStream = static_cast<raw_string_ostream &>(OS);
      74             :       std::string &Data = SOStream.str(); // with flush
      75         645 :       for (int K = 0; K < NItems; K++) {
      76       14964 :         for (int I = 0; I < P[K].N; I++) {
      77        7353 :           uint64_t Bytes = endian::byte_swap<uint64_t, little>(P[K].D[I]);
      78       14706 :           Data.replace(P[K].Pos + I * sizeof(uint64_t), sizeof(uint64_t),
      79        7353 :                        (const char *)&Bytes, sizeof(uint64_t));
      80             :         }
      81             :       }
      82             :     }
      83         222 :   }
      84             : 
      85             :   // If \c OS is an instance of \c raw_fd_ostream, this field will be
      86             :   // true. Otherwise, \c OS will be an raw_string_ostream.
      87             :   bool IsFDOStream;
      88             :   raw_ostream &OS;
      89             :   support::endian::Writer LE;
      90             : };
      91             : 
      92             : class InstrProfRecordWriterTrait {
      93             : public:
      94             :   using key_type = StringRef;
      95             :   using key_type_ref = StringRef;
      96             : 
      97             :   using data_type = const InstrProfWriter::ProfilingData *const;
      98             :   using data_type_ref = const InstrProfWriter::ProfilingData *const;
      99             : 
     100             :   using hash_value_type = uint64_t;
     101             :   using offset_type = uint64_t;
     102             : 
     103             :   support::endianness ValueProfDataEndianness = support::little;
     104             :   InstrProfSummaryBuilder *SummaryBuilder;
     105             : 
     106         666 :   InstrProfRecordWriterTrait() = default;
     107             : 
     108             :   static hash_value_type ComputeHash(key_type_ref K) {
     109             :     return IndexedInstrProf::ComputeHash(K);
     110             :   }
     111             : 
     112             :   static std::pair<offset_type, offset_type>
     113         395 :   EmitKeyDataLength(raw_ostream &Out, key_type_ref K, data_type_ref V) {
     114             :     using namespace support;
     115             : 
     116             :     endian::Writer LE(Out, little);
     117             : 
     118             :     offset_type N = K.size();
     119             :     LE.write<offset_type>(N);
     120             : 
     121             :     offset_type M = 0;
     122        1199 :     for (const auto &ProfileData : *V) {
     123             :       const InstrProfRecord &ProfRecord = ProfileData.second;
     124             :       M += sizeof(uint64_t); // The function hash
     125         409 :       M += sizeof(uint64_t); // The size of the Counts vector
     126         818 :       M += ProfRecord.Counts.size() * sizeof(uint64_t);
     127             : 
     128             :       // Value data
     129         409 :       M += ValueProfData::getSize(ProfileData.second);
     130             :     }
     131             :     LE.write<offset_type>(M);
     132             : 
     133         395 :     return std::make_pair(N, M);
     134             :   }
     135             : 
     136             :   void EmitKey(raw_ostream &Out, key_type_ref K, offset_type N) {
     137         395 :     Out.write(K.data(), N);
     138             :   }
     139             : 
     140         395 :   void EmitData(raw_ostream &Out, key_type_ref, data_type_ref V, offset_type) {
     141             :     using namespace support;
     142             : 
     143             :     endian::Writer LE(Out, little);
     144        1199 :     for (const auto &ProfileData : *V) {
     145         409 :       const InstrProfRecord &ProfRecord = ProfileData.second;
     146         409 :       SummaryBuilder->addRecord(ProfRecord);
     147             : 
     148         409 :       LE.write<uint64_t>(ProfileData.first); // Function hash
     149         409 :       LE.write<uint64_t>(ProfRecord.Counts.size());
     150         409 :       for (uint64_t I : ProfRecord.Counts)
     151             :         LE.write<uint64_t>(I);
     152             : 
     153             :       // Write value data
     154             :       std::unique_ptr<ValueProfData> VDataPtr =
     155         409 :           ValueProfData::serializeFrom(ProfileData.second);
     156         409 :       uint32_t S = VDataPtr->getSize();
     157         409 :       VDataPtr->swapBytesFromHost(ValueProfDataEndianness);
     158         818 :       Out.write((const char *)VDataPtr.get(), S);
     159             :     }
     160         395 :   }
     161             : };
     162             : 
     163             : } // end namespace llvm
     164             : 
     165         271 : InstrProfWriter::InstrProfWriter(bool Sparse)
     166         813 :     : Sparse(Sparse), InfoObj(new InstrProfRecordWriterTrait()) {}
     167             : 
     168         268 : InstrProfWriter::~InstrProfWriter() { delete InfoObj; }
     169             : 
     170             : // Internal interface for testing purpose only.
     171           4 : void InstrProfWriter::setValueProfDataEndianness(
     172             :     support::endianness Endianness) {
     173           4 :   InfoObj->ValueProfDataEndianness = Endianness;
     174           4 : }
     175             : 
     176         153 : void InstrProfWriter::setOutputSparse(bool Sparse) {
     177         153 :   this->Sparse = Sparse;
     178         153 : }
     179             : 
     180         527 : void InstrProfWriter::addRecord(NamedInstrProfRecord &&I, uint64_t Weight,
     181             :                                 function_ref<void(Error)> Warn) {
     182         527 :   auto Name = I.Name;
     183         527 :   auto Hash = I.Hash;
     184         527 :   addRecord(Name, Hash, std::move(I), Weight, Warn);
     185         527 : }
     186             : 
     187         571 : void InstrProfWriter::addRecord(StringRef Name, uint64_t Hash,
     188             :                                 InstrProfRecord &&I, uint64_t Weight,
     189             :                                 function_ref<void(Error)> Warn) {
     190         571 :   auto &ProfileDataMap = FunctionData[Name];
     191             : 
     192             :   bool NewFunc;
     193             :   ProfilingData::iterator Where;
     194             :   std::tie(Where, NewFunc) =
     195        1142 :       ProfileDataMap.insert(std::make_pair(Hash, InstrProfRecord()));
     196         571 :   InstrProfRecord &Dest = Where->second;
     197             : 
     198          11 :   auto MapWarn = [&](instrprof_error E) {
     199          33 :     Warn(make_error<InstrProfError>(E));
     200         582 :   };
     201             : 
     202         571 :   if (NewFunc) {
     203             :     // We've never seen a function with this name and hash, add it.
     204         481 :     Dest = std::move(I);
     205         481 :     if (Weight > 1)
     206          10 :       Dest.scale(Weight, MapWarn);
     207             :   } else {
     208             :     // We're updating a function we've seen before.
     209          90 :     Dest.merge(I, Weight, MapWarn);
     210             :   }
     211             : 
     212         571 :   Dest.sortValueData();
     213         571 : }
     214             : 
     215          14 : void InstrProfWriter::mergeRecordsFromWriter(InstrProfWriter &&IPW,
     216             :                                              function_ref<void(Error)> Warn) {
     217          63 :   for (auto &I : IPW.FunctionData)
     218         114 :     for (auto &Func : I.getValue())
     219          88 :       addRecord(I.getKey(), Func.first, std::move(Func.second), 1, Warn);
     220          14 : }
     221             : 
     222         437 : bool InstrProfWriter::shouldEncodeData(const ProfilingData &PD) {
     223         437 :   if (!Sparse)
     224             :     return true;
     225         184 :   for (const auto &Func : PD) {
     226             :     const InstrProfRecord &IPR = Func.second;
     227          86 :     if (llvm::any_of(IPR.Counts, [](uint64_t Count) { return Count > 0; }))
     228          74 :       return true;
     229             :   }
     230          12 :   return false;
     231             : }
     232             : 
     233         222 : static void setSummary(IndexedInstrProf::Summary *TheSummary,
     234             :                        ProfileSummary &PS) {
     235             :   using namespace IndexedInstrProf;
     236             : 
     237             :   std::vector<ProfileSummaryEntry> &Res = PS.getDetailedSummary();
     238         222 :   TheSummary->NumSummaryFields = Summary::NumKinds;
     239         444 :   TheSummary->NumCutoffEntries = Res.size();
     240         222 :   TheSummary->set(Summary::MaxFunctionCount, PS.getMaxFunctionCount());
     241         222 :   TheSummary->set(Summary::MaxBlockCount, PS.getMaxCount());
     242         222 :   TheSummary->set(Summary::MaxInternalBlockCount, PS.getMaxInternalCount());
     243         222 :   TheSummary->set(Summary::TotalBlockCount, PS.getTotalCount());
     244         222 :   TheSummary->set(Summary::TotalNumBlocks, PS.getNumCounts());
     245         222 :   TheSummary->set(Summary::TotalNumFunctions, PS.getNumFunctions());
     246        7326 :   for (unsigned I = 0; I < Res.size(); I++)
     247             :     TheSummary->setEntry(I, Res[I]);
     248         222 : }
     249             : 
     250         222 : void InstrProfWriter::writeImpl(ProfOStream &OS) {
     251             :   using namespace IndexedInstrProf;
     252             : 
     253         444 :   OnDiskChainedHashTableGenerator<InstrProfRecordWriterTrait> Generator;
     254             : 
     255         222 :   InstrProfSummaryBuilder ISB(ProfileSummaryBuilder::DefaultCutoffs);
     256         222 :   InfoObj->SummaryBuilder = &ISB;
     257             : 
     258             :   // Populate the hash table generator.
     259         851 :   for (const auto &I : FunctionData)
     260         407 :     if (shouldEncodeData(I.getValue()))
     261             :       Generator.insert(I.getKey(), &I.getValue());
     262             :   // Write the header.
     263             :   IndexedInstrProf::Header Header;
     264         222 :   Header.Magic = IndexedInstrProf::Magic;
     265         222 :   Header.Version = IndexedInstrProf::ProfVersion::CurrentVersion;
     266         222 :   if (ProfileKind == PF_IRLevel)
     267          26 :     Header.Version |= VARIANT_MASK_IR_PROF;
     268         222 :   Header.Unused = 0;
     269         222 :   Header.HashType = static_cast<uint64_t>(IndexedInstrProf::HashType);
     270         222 :   Header.HashOffset = 0;
     271             :   int N = sizeof(IndexedInstrProf::Header) / sizeof(uint64_t);
     272             : 
     273             :   // Only write out all the fields except 'HashOffset'. We need
     274             :   // to remember the offset of that field to allow back patching
     275             :   // later.
     276        1998 :   for (int I = 0; I < N - 1; I++)
     277         888 :     OS.write(reinterpret_cast<uint64_t *>(&Header)[I]);
     278             : 
     279             :   // Save the location of Header.HashOffset field in \c OS.
     280         222 :   uint64_t HashTableStartFieldOffset = OS.tell();
     281             :   // Reserve the space for HashOffset field.
     282             :   OS.write(0);
     283             : 
     284             :   // Reserve space to write profile summary data.
     285         222 :   uint32_t NumEntries = ProfileSummaryBuilder::DefaultCutoffs.size();
     286             :   uint32_t SummarySize = Summary::getSize(Summary::NumKinds, NumEntries);
     287             :   // Remember the summary offset.
     288         222 :   uint64_t SummaryOffset = OS.tell();
     289       25086 :   for (unsigned I = 0; I < SummarySize / sizeof(uint64_t); I++)
     290             :     OS.write(0);
     291             : 
     292             :   // Write the hash table.
     293         222 :   uint64_t HashTableStart = Generator.Emit(OS.OS, *InfoObj);
     294             : 
     295             :   // Allocate space for data to be serialized out.
     296             :   std::unique_ptr<IndexedInstrProf::Summary> TheSummary =
     297         222 :       IndexedInstrProf::allocSummary(SummarySize);
     298             :   // Compute the Summary and copy the data to the data
     299             :   // structure to be serialized out (to disk or buffer).
     300         222 :   std::unique_ptr<ProfileSummary> PS = ISB.getSummary();
     301         222 :   setSummary(TheSummary.get(), *PS);
     302         222 :   InfoObj->SummaryBuilder = nullptr;
     303             : 
     304             :   // Now do the final patch:
     305         222 :   PatchItem PatchItems[] = {
     306             :       // Patch the Header.HashOffset field.
     307             :       {HashTableStartFieldOffset, &HashTableStart, 1},
     308             :       // Patch the summary data.
     309             :       {SummaryOffset, reinterpret_cast<uint64_t *>(TheSummary.get()),
     310         444 :        (int)(SummarySize / sizeof(uint64_t))}};
     311         222 :   OS.patch(PatchItems, sizeof(PatchItems) / sizeof(*PatchItems));
     312         222 : }
     313             : 
     314          93 : void InstrProfWriter::write(raw_fd_ostream &OS) {
     315             :   // Write the hash table.
     316             :   ProfOStream POS(OS);
     317          93 :   writeImpl(POS);
     318          93 : }
     319             : 
     320         129 : std::unique_ptr<MemoryBuffer> InstrProfWriter::writeBuffer() {
     321             :   std::string Data;
     322         129 :   raw_string_ostream OS(Data);
     323             :   ProfOStream POS(OS);
     324             :   // Write the hash table.
     325         129 :   writeImpl(POS);
     326             :   // Return this in an aligned memory buffer.
     327         258 :   return MemoryBuffer::getMemBufferCopy(Data);
     328             : }
     329             : 
     330             : static const char *ValueProfKindStr[] = {
     331             : #define VALUE_PROF_KIND(Enumerator, Value) #Enumerator,
     332             : #include "llvm/ProfileData/InstrProfData.inc"
     333             : };
     334             : 
     335          23 : void InstrProfWriter::writeRecordInText(StringRef Name, uint64_t Hash,
     336             :                                         const InstrProfRecord &Func,
     337             :                                         InstrProfSymtab &Symtab,
     338             :                                         raw_fd_ostream &OS) {
     339          23 :   OS << Name << "\n";
     340          23 :   OS << "# Func Hash:\n" << Hash << "\n";
     341          46 :   OS << "# Num Counters:\n" << Func.Counts.size() << "\n";
     342          23 :   OS << "# Counter Values:\n";
     343          23 :   for (uint64_t Count : Func.Counts)
     344          57 :     OS << Count << "\n";
     345             : 
     346             :   uint32_t NumValueKinds = Func.getNumValueKinds();
     347          23 :   if (!NumValueKinds) {
     348          19 :     OS << "\n";
     349          19 :     return;
     350             :   }
     351             : 
     352           8 :   OS << "# Num Value Kinds:\n" << Func.getNumValueKinds() << "\n";
     353          20 :   for (uint32_t VK = 0; VK < IPVK_Last + 1; VK++) {
     354             :     uint32_t NS = Func.getNumValueSites(VK);
     355           8 :     if (!NS)
     356           2 :       continue;
     357          12 :     OS << "# ValueKind = " << ValueProfKindStr[VK] << ":\n" << VK << "\n";
     358          12 :     OS << "# NumValueSites:\n" << NS << "\n";
     359          32 :     for (uint32_t S = 0; S < NS; S++) {
     360             :       uint32_t ND = Func.getNumValueDataForSite(VK, S);
     361          13 :       OS << ND << "\n";
     362          13 :       std::unique_ptr<InstrProfValueData[]> VD = Func.getValueForSite(VK, S);
     363          73 :       for (uint32_t I = 0; I < ND; I++) {
     364          30 :         if (VK == IPVK_IndirectCallTarget)
     365          36 :           OS << Symtab.getFuncNameOrExternalSymbol(VD[I].Value) << ":"
     366          12 :              << VD[I].Count << "\n";
     367             :         else
     368          54 :           OS << VD[I].Value << ":" << VD[I].Count << "\n";
     369             :       }
     370             :     }
     371             :   }
     372             : 
     373           4 :   OS << "\n";
     374             : }
     375             : 
     376           7 : Error InstrProfWriter::writeText(raw_fd_ostream &OS) {
     377           7 :   if (ProfileKind == PF_IRLevel)
     378           3 :     OS << "# IR level Instrumentation Flag\n:ir\n";
     379           7 :   InstrProfSymtab Symtab;
     380          29 :   for (const auto &I : FunctionData)
     381          15 :     if (shouldEncodeData(I.getValue()))
     382          30 :       if (Error E = Symtab.addFuncName(I.getKey()))
     383             :         return E;
     384             : 
     385          29 :   for (const auto &I : FunctionData)
     386          15 :     if (shouldEncodeData(I.getValue()))
     387          45 :       for (const auto &Func : I.getValue())
     388          30 :         writeRecordInText(I.getKey(), Func.first, Func.second, Symtab, OS);
     389             :   return Error::success();
     390             : }

Generated by: LCOV version 1.13