LLVM  3.7.0
InstrProfWriter.cpp
Go to the documentation of this file.
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 
16 #include "InstrProfIndexed.h"
17 #include "llvm/ADT/StringExtras.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 
35  static hash_value_type ComputeHash(key_type_ref K) {
37  }
38 
39  static std::pair<offset_type, offset_type>
40  EmitKeyDataLength(raw_ostream &Out, key_type_ref K, data_type_ref V) {
41  using namespace llvm::support;
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 
55  static void EmitKey(raw_ostream &Out, key_type_ref K, offset_type N){
56  Out.write(K.data(), N);
57  }
58 
59  static void EmitData(raw_ostream &Out, key_type_ref, data_type_ref V,
60  offset_type) {
61  using namespace llvm::support;
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
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];
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())
96 
97  for (size_t I = 0, E = Counters.size(); I < E; ++I) {
98  if (FoundCounters[I] + Counters[I] < FoundCounters[I])
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 
107 }
108 
109 std::pair<uint64_t, uint64_t> InstrProfWriter::writeImpl(raw_ostream &OS) {
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;
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 
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 
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 }
Defines facilities for reading and writing on-disk hash tables.
uint64_t seek(uint64_t off)
Flushes the stream and repositions the underlying file descriptor position to the offset specified fr...
InstrProfLookupTrait::data_type data_type
InstrProfLookupTrait::offset_type offset_type
uint64_t tell() const
tell - Return the current offset with the file.
Definition: raw_ostream.h:92
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:134
raw_ostream & write(unsigned char C)
offset_type Emit(raw_ostream &Out)
Emit the table to Out, which must not be at offset 0.
static std::unique_ptr< MemoryBuffer > getMemBufferCopy(StringRef InputData, const Twine &BufferName="")
Open the specified memory range as a MemoryBuffer, copying the contents and taking ownership of it...
Generates an on disk hash table.
Adapter to write values to a stream in a particular byte order.
Definition: EndianStream.h:26
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:345
#define I(x, y, z)
Definition: MD5.cpp:54
#define N
static uint64_t ComputeHash(HashT Type, StringRef K)
iterator end()
Definition: DenseMap.h:68
iterator find(const KeyT &Val)
Definition: DenseMap.h:124
std::error_code addFunctionCounts(StringRef FunctionName, uint64_t FunctionHash, ArrayRef< uint64_t > Counters)
Add function counts for the given function.
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:465
void write(raw_fd_ostream &OS)
Write the profile to OS.
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:38
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
std::unique_ptr< MemoryBuffer > writeBuffer()
Write the profile, returning the raw data. For testing.