blob: 2188543ed61c3002f93825dddca18940026b8534 [file] [log] [blame]
Justin Bognerb9bd7f82014-03-21 17:46:22 +00001//=-- 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"
Chandler Carruthd9903882015-01-14 11:23:27 +000016#include "InstrProfIndexed.h"
Justin Bognerb7aa2632014-04-18 21:48:40 +000017#include "llvm/ADT/StringExtras.h"
18#include "llvm/Support/EndianStream.h"
19#include "llvm/Support/OnDiskHashTable.h"
20
Justin Bognerb9bd7f82014-03-21 17:46:22 +000021using namespace llvm;
22
Justin Bognerb7aa2632014-04-18 21:48:40 +000023namespace {
24class InstrProfRecordTrait {
25public:
26 typedef StringRef key_type;
27 typedef StringRef key_type_ref;
28
Justin Bognerfa5b0132014-04-23 18:50:16 +000029 typedef const InstrProfWriter::CounterData *const data_type;
30 typedef const InstrProfWriter::CounterData *const data_type_ref;
Justin Bognerb7aa2632014-04-18 21:48:40 +000031
32 typedef uint64_t hash_value_type;
33 typedef uint64_t offset_type;
34
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>
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
Justin Bognere8081712014-04-19 00:33:15 +000044 offset_type N = K.size();
Justin Bognerb7aa2632014-04-18 21:48:40 +000045 LE.write<offset_type>(N);
46
Justin Bogner821d7472014-08-01 22:50:07 +000047 offset_type M = 0;
48 for (const auto &Counts : *V)
49 M += (2 + Counts.second.size()) * sizeof(uint64_t);
Justin Bognerb7aa2632014-04-18 21:48:40 +000050 LE.write<offset_type>(M);
51
52 return std::make_pair(N, M);
53 }
54
Justin Bognere8081712014-04-19 00:33:15 +000055 static void EmitKey(raw_ostream &Out, key_type_ref K, offset_type N){
Justin Bognerb7aa2632014-04-18 21:48:40 +000056 Out.write(K.data(), N);
57 }
58
59 static void EmitData(raw_ostream &Out, key_type_ref, data_type_ref V,
Justin Bognere8081712014-04-19 00:33:15 +000060 offset_type) {
Justin Bognerb7aa2632014-04-18 21:48:40 +000061 using namespace llvm::support;
62 endian::Writer<little> LE(Out);
Justin Bogner821d7472014-08-01 22:50:07 +000063
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 }
Justin Bognerb7aa2632014-04-18 21:48:40 +000070 }
71};
Alexander Kornienkof00654e2015-06-23 09:49:53 +000072}
Justin Bognerb7aa2632014-04-18 21:48:40 +000073
Rafael Espindola3acea392014-06-12 21:46:39 +000074std::error_code
75InstrProfWriter::addFunctionCounts(StringRef FunctionName,
76 uint64_t FunctionHash,
77 ArrayRef<uint64_t> Counters) {
Justin Bogner821d7472014-08-01 22:50:07 +000078 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];
Justin Bognera2bfd662014-04-19 23:42:50 +000087 return instrprof_error::success;
Justin Bognerb9bd7f82014-03-21 17:46:22 +000088 }
89
Justin Bogner821d7472014-08-01 22:50:07 +000090 // 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())
Justin Bognerb9bd7f82014-03-21 17:46:22 +000095 return instrprof_error::count_mismatch;
Justin Bogner821d7472014-08-01 22:50:07 +000096
Justin Bognerb9bd7f82014-03-21 17:46:22 +000097 for (size_t I = 0, E = Counters.size(); I < E; ++I) {
Justin Bogner821d7472014-08-01 22:50:07 +000098 if (FoundCounters[I] + Counters[I] < FoundCounters[I])
Justin Bognerb9bd7f82014-03-21 17:46:22 +000099 return instrprof_error::counter_overflow;
Justin Bogner821d7472014-08-01 22:50:07 +0000100 FoundCounters[I] += Counters[I];
Justin Bognerb9bd7f82014-03-21 17:46:22 +0000101 }
Justin Bogner821d7472014-08-01 22:50:07 +0000102 // We keep track of the max function count as we go for simplicity.
103 if (FoundCounters[0] > MaxFunctionCount)
104 MaxFunctionCount = FoundCounters[0];
105
Justin Bognerb9bd7f82014-03-21 17:46:22 +0000106 return instrprof_error::success;
107}
108
Justin Bogner2b6c5372015-02-18 01:58:17 +0000109std::pair<uint64_t, uint64_t> InstrProfWriter::writeImpl(raw_ostream &OS) {
Justin Bognerb7aa2632014-04-18 21:48:40 +0000110 OnDiskChainedHashTableGenerator<InstrProfRecordTrait> Generator;
Justin Bognerb9bd7f82014-03-21 17:46:22 +0000111
Justin Bognerb7aa2632014-04-18 21:48:40 +0000112 // Populate the hash table generator.
Justin Bogner821d7472014-08-01 22:50:07 +0000113 for (const auto &I : FunctionData)
Justin Bognerfa5b0132014-04-23 18:50:16 +0000114 Generator.insert(I.getKey(), &I.getValue());
Justin Bognerb7aa2632014-04-18 21:48:40 +0000115
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
Justin Bogner2b6c5372015-02-18 01:58:17 +0000131 return std::make_pair(HashTableStartLoc, HashTableStart);
132}
133
134void InstrProfWriter::write(raw_fd_ostream &OS) {
135 // Write the hash table.
136 auto TableStart = writeImpl(OS);
137
Justin Bognerb7aa2632014-04-18 21:48:40 +0000138 // Go back and fill in the hash table start.
Justin Bogner2b6c5372015-02-18 01:58:17 +0000139 using namespace support;
140 OS.seek(TableStart.first);
141 endian::Writer<little>(OS).write<uint64_t>(TableStart.second);
142}
143
144std::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);
Justin Bognerb9bd7f82014-03-21 17:46:22 +0000159}