LLVM API Documentation

InstrProfWriter.cpp
Go to the documentation of this file.
00001 //=-- InstrProfWriter.cpp - Instrumented profiling writer -------------------=//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file contains support for writing profiling data for clang's
00011 // instrumentation based PGO and coverage.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "llvm/ProfileData/InstrProfWriter.h"
00016 #include "llvm/ADT/StringExtras.h"
00017 #include "llvm/Support/EndianStream.h"
00018 #include "llvm/Support/OnDiskHashTable.h"
00019 
00020 #include "InstrProfIndexed.h"
00021 
00022 using namespace llvm;
00023 
00024 namespace {
00025 class InstrProfRecordTrait {
00026 public:
00027   typedef StringRef key_type;
00028   typedef StringRef key_type_ref;
00029 
00030   typedef const InstrProfWriter::CounterData *const data_type;
00031   typedef const InstrProfWriter::CounterData *const data_type_ref;
00032 
00033   typedef uint64_t hash_value_type;
00034   typedef uint64_t offset_type;
00035 
00036   static hash_value_type ComputeHash(key_type_ref K) {
00037     return IndexedInstrProf::ComputeHash(IndexedInstrProf::HashType, K);
00038   }
00039 
00040   static std::pair<offset_type, offset_type>
00041   EmitKeyDataLength(raw_ostream &Out, key_type_ref K, data_type_ref V) {
00042     using namespace llvm::support;
00043     endian::Writer<little> LE(Out);
00044 
00045     offset_type N = K.size();
00046     LE.write<offset_type>(N);
00047 
00048     offset_type M = 0;
00049     for (const auto &Counts : *V)
00050       M += (2 + Counts.second.size()) * sizeof(uint64_t);
00051     LE.write<offset_type>(M);
00052 
00053     return std::make_pair(N, M);
00054   }
00055 
00056   static void EmitKey(raw_ostream &Out, key_type_ref K, offset_type N){
00057     Out.write(K.data(), N);
00058   }
00059 
00060   static void EmitData(raw_ostream &Out, key_type_ref, data_type_ref V,
00061                        offset_type) {
00062     using namespace llvm::support;
00063     endian::Writer<little> LE(Out);
00064 
00065     for (const auto &Counts : *V) {
00066       LE.write<uint64_t>(Counts.first);
00067       LE.write<uint64_t>(Counts.second.size());
00068       for (uint64_t I : Counts.second)
00069         LE.write<uint64_t>(I);
00070     }
00071   }
00072 };
00073 }
00074 
00075 std::error_code
00076 InstrProfWriter::addFunctionCounts(StringRef FunctionName,
00077                                    uint64_t FunctionHash,
00078                                    ArrayRef<uint64_t> Counters) {
00079   auto &CounterData = FunctionData[FunctionName];
00080 
00081   auto Where = CounterData.find(FunctionHash);
00082   if (Where == CounterData.end()) {
00083     // We've never seen a function with this name and hash, add it.
00084     CounterData[FunctionHash] = Counters;
00085     // We keep track of the max function count as we go for simplicity.
00086     if (Counters[0] > MaxFunctionCount)
00087       MaxFunctionCount = Counters[0];
00088     return instrprof_error::success;
00089   }
00090 
00091   // We're updating a function we've seen before.
00092   auto &FoundCounters = Where->second;
00093   // If the number of counters doesn't match we either have bad data or a hash
00094   // collision.
00095   if (FoundCounters.size() != Counters.size())
00096     return instrprof_error::count_mismatch;
00097 
00098   for (size_t I = 0, E = Counters.size(); I < E; ++I) {
00099     if (FoundCounters[I] + Counters[I] < FoundCounters[I])
00100       return instrprof_error::counter_overflow;
00101     FoundCounters[I] += Counters[I];
00102   }
00103   // We keep track of the max function count as we go for simplicity.
00104   if (FoundCounters[0] > MaxFunctionCount)
00105     MaxFunctionCount = FoundCounters[0];
00106 
00107   return instrprof_error::success;
00108 }
00109 
00110 void InstrProfWriter::write(raw_fd_ostream &OS) {
00111   OnDiskChainedHashTableGenerator<InstrProfRecordTrait> Generator;
00112 
00113   // Populate the hash table generator.
00114   std::vector<uint64_t> CounterBuffer;
00115   for (const auto &I : FunctionData)
00116     Generator.insert(I.getKey(), &I.getValue());
00117 
00118   using namespace llvm::support;
00119   endian::Writer<little> LE(OS);
00120 
00121   // Write the header.
00122   LE.write<uint64_t>(IndexedInstrProf::Magic);
00123   LE.write<uint64_t>(IndexedInstrProf::Version);
00124   LE.write<uint64_t>(MaxFunctionCount);
00125   LE.write<uint64_t>(static_cast<uint64_t>(IndexedInstrProf::HashType));
00126 
00127   // Save a space to write the hash table start location.
00128   uint64_t HashTableStartLoc = OS.tell();
00129   LE.write<uint64_t>(0);
00130   // Write the hash table.
00131   uint64_t HashTableStart = Generator.Emit(OS);
00132 
00133   // Go back and fill in the hash table start.
00134   OS.seek(HashTableStartLoc);
00135   LE.write<uint64_t>(HashTableStart);
00136 }