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 "InstrProfIndexed.h"
17 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/Support/EndianStream.h"
19 #include "llvm/Support/OnDiskHashTable.h"
20 
21 using namespace llvm;
22 
23 namespace {
24 class InstrProfRecordTrait {
25 public:
26   typedef StringRef key_type;
27   typedef StringRef key_type_ref;
28 
29   typedef const InstrProfWriter::CounterData *const data_type;
30   typedef const InstrProfWriter::CounterData *const data_type_ref;
31 
32   typedef uint64_t hash_value_type;
33   typedef uint64_t offset_type;
34 
ComputeHash(key_type_ref K)35   static hash_value_type ComputeHash(key_type_ref K) {
36     return IndexedInstrProf::ComputeHash(IndexedInstrProf::HashType, K);
37   }
38 
39   static std::pair<offset_type, offset_type>
EmitKeyDataLength(raw_ostream & Out,key_type_ref K,data_type_ref V)40   EmitKeyDataLength(raw_ostream &Out, key_type_ref K, data_type_ref V) {
41     using namespace llvm::support;
42     endian::Writer<little> LE(Out);
43 
44     offset_type N = K.size();
45     LE.write<offset_type>(N);
46 
47     offset_type M = 0;
48     for (const auto &Counts : *V)
49       M += (2 + Counts.second.size()) * sizeof(uint64_t);
50     LE.write<offset_type>(M);
51 
52     return std::make_pair(N, M);
53   }
54 
EmitKey(raw_ostream & Out,key_type_ref K,offset_type N)55   static void EmitKey(raw_ostream &Out, key_type_ref K, offset_type N){
56     Out.write(K.data(), N);
57   }
58 
EmitData(raw_ostream & Out,key_type_ref,data_type_ref V,offset_type)59   static void EmitData(raw_ostream &Out, key_type_ref, data_type_ref V,
60                        offset_type) {
61     using namespace llvm::support;
62     endian::Writer<little> LE(Out);
63 
64     for (const auto &Counts : *V) {
65       LE.write<uint64_t>(Counts.first);
66       LE.write<uint64_t>(Counts.second.size());
67       for (uint64_t I : Counts.second)
68         LE.write<uint64_t>(I);
69     }
70   }
71 };
72 }
73 
74 std::error_code
addFunctionCounts(StringRef FunctionName,uint64_t FunctionHash,ArrayRef<uint64_t> Counters)75 InstrProfWriter::addFunctionCounts(StringRef FunctionName,
76                                    uint64_t FunctionHash,
77                                    ArrayRef<uint64_t> Counters) {
78   auto &CounterData = FunctionData[FunctionName];
79 
80   auto Where = CounterData.find(FunctionHash);
81   if (Where == CounterData.end()) {
82     // We've never seen a function with this name and hash, add it.
83     CounterData[FunctionHash] = Counters;
84     // We keep track of the max function count as we go for simplicity.
85     if (Counters[0] > MaxFunctionCount)
86       MaxFunctionCount = Counters[0];
87     return instrprof_error::success;
88   }
89 
90   // We're updating a function we've seen before.
91   auto &FoundCounters = Where->second;
92   // If the number of counters doesn't match we either have bad data or a hash
93   // collision.
94   if (FoundCounters.size() != Counters.size())
95     return instrprof_error::count_mismatch;
96 
97   for (size_t I = 0, E = Counters.size(); I < E; ++I) {
98     if (FoundCounters[I] + Counters[I] < FoundCounters[I])
99       return instrprof_error::counter_overflow;
100     FoundCounters[I] += Counters[I];
101   }
102   // We keep track of the max function count as we go for simplicity.
103   if (FoundCounters[0] > MaxFunctionCount)
104     MaxFunctionCount = FoundCounters[0];
105 
106   return instrprof_error::success;
107 }
108 
writeImpl(raw_ostream & OS)109 std::pair<uint64_t, uint64_t> InstrProfWriter::writeImpl(raw_ostream &OS) {
110   OnDiskChainedHashTableGenerator<InstrProfRecordTrait> Generator;
111 
112   // Populate the hash table generator.
113   for (const auto &I : FunctionData)
114     Generator.insert(I.getKey(), &I.getValue());
115 
116   using namespace llvm::support;
117   endian::Writer<little> LE(OS);
118 
119   // Write the header.
120   LE.write<uint64_t>(IndexedInstrProf::Magic);
121   LE.write<uint64_t>(IndexedInstrProf::Version);
122   LE.write<uint64_t>(MaxFunctionCount);
123   LE.write<uint64_t>(static_cast<uint64_t>(IndexedInstrProf::HashType));
124 
125   // Save a space to write the hash table start location.
126   uint64_t HashTableStartLoc = OS.tell();
127   LE.write<uint64_t>(0);
128   // Write the hash table.
129   uint64_t HashTableStart = Generator.Emit(OS);
130 
131   return std::make_pair(HashTableStartLoc, HashTableStart);
132 }
133 
write(raw_fd_ostream & OS)134 void InstrProfWriter::write(raw_fd_ostream &OS) {
135   // Write the hash table.
136   auto TableStart = writeImpl(OS);
137 
138   // Go back and fill in the hash table start.
139   using namespace support;
140   OS.seek(TableStart.first);
141   endian::Writer<little>(OS).write<uint64_t>(TableStart.second);
142 }
143 
writeBuffer()144 std::unique_ptr<MemoryBuffer> InstrProfWriter::writeBuffer() {
145   std::string Data;
146   llvm::raw_string_ostream OS(Data);
147   // Write the hash table.
148   auto TableStart = writeImpl(OS);
149   OS.flush();
150 
151   // Go back and fill in the hash table start.
152   using namespace support;
153   uint64_t Bytes = endian::byte_swap<uint64_t, little>(TableStart.second);
154   Data.replace(TableStart.first, sizeof(uint64_t), (const char *)&Bytes,
155                sizeof(uint64_t));
156 
157   // Return this in an aligned memory buffer.
158   return MemoryBuffer::getMemBufferCopy(Data);
159 }
160