blob: 1c4a4fede2868f1fc655b6cf57528bb81e590bfa [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"
Justin Bognerb7aa2632014-04-18 21:48:40 +000016#include "llvm/ADT/StringExtras.h"
17#include "llvm/Support/EndianStream.h"
18#include "llvm/Support/OnDiskHashTable.h"
19
20#include "InstrProfIndexed.h"
Justin Bognerb9bd7f82014-03-21 17:46:22 +000021
22using namespace llvm;
23
Justin Bognerb7aa2632014-04-18 21:48:40 +000024namespace {
25class InstrProfRecordTrait {
26public:
27 typedef StringRef key_type;
28 typedef StringRef key_type_ref;
29
Justin Bognerfa5b0132014-04-23 18:50:16 +000030 typedef const InstrProfWriter::CounterData *const data_type;
31 typedef const InstrProfWriter::CounterData *const data_type_ref;
Justin Bognerb7aa2632014-04-18 21:48:40 +000032
33 typedef uint64_t hash_value_type;
34 typedef uint64_t offset_type;
35
36 static hash_value_type ComputeHash(key_type_ref K) {
37 return IndexedInstrProf::ComputeHash(IndexedInstrProf::HashType, K);
38 }
39
40 static std::pair<offset_type, offset_type>
41 EmitKeyDataLength(raw_ostream &Out, key_type_ref K, data_type_ref V) {
42 using namespace llvm::support;
43 endian::Writer<little> LE(Out);
44
Justin Bognere8081712014-04-19 00:33:15 +000045 offset_type N = K.size();
Justin Bognerb7aa2632014-04-18 21:48:40 +000046 LE.write<offset_type>(N);
47
Justin Bogner821d7472014-08-01 22:50:07 +000048 offset_type M = 0;
49 for (const auto &Counts : *V)
50 M += (2 + Counts.second.size()) * sizeof(uint64_t);
Justin Bognerb7aa2632014-04-18 21:48:40 +000051 LE.write<offset_type>(M);
52
53 return std::make_pair(N, M);
54 }
55
Justin Bognere8081712014-04-19 00:33:15 +000056 static void EmitKey(raw_ostream &Out, key_type_ref K, offset_type N){
Justin Bognerb7aa2632014-04-18 21:48:40 +000057 Out.write(K.data(), N);
58 }
59
60 static void EmitData(raw_ostream &Out, key_type_ref, data_type_ref V,
Justin Bognere8081712014-04-19 00:33:15 +000061 offset_type) {
Justin Bognerb7aa2632014-04-18 21:48:40 +000062 using namespace llvm::support;
63 endian::Writer<little> LE(Out);
Justin Bogner821d7472014-08-01 22:50:07 +000064
65 for (const auto &Counts : *V) {
66 LE.write<uint64_t>(Counts.first);
67 LE.write<uint64_t>(Counts.second.size());
68 for (uint64_t I : Counts.second)
69 LE.write<uint64_t>(I);
70 }
Justin Bognerb7aa2632014-04-18 21:48:40 +000071 }
72};
73}
74
Rafael Espindola3acea392014-06-12 21:46:39 +000075std::error_code
76InstrProfWriter::addFunctionCounts(StringRef FunctionName,
77 uint64_t FunctionHash,
78 ArrayRef<uint64_t> Counters) {
Justin Bogner821d7472014-08-01 22:50:07 +000079 auto &CounterData = FunctionData[FunctionName];
80
81 auto Where = CounterData.find(FunctionHash);
82 if (Where == CounterData.end()) {
83 // We've never seen a function with this name and hash, add it.
84 CounterData[FunctionHash] = Counters;
85 // We keep track of the max function count as we go for simplicity.
86 if (Counters[0] > MaxFunctionCount)
87 MaxFunctionCount = Counters[0];
Justin Bognera2bfd662014-04-19 23:42:50 +000088 return instrprof_error::success;
Justin Bognerb9bd7f82014-03-21 17:46:22 +000089 }
90
Justin Bogner821d7472014-08-01 22:50:07 +000091 // We're updating a function we've seen before.
92 auto &FoundCounters = Where->second;
93 // If the number of counters doesn't match we either have bad data or a hash
94 // collision.
95 if (FoundCounters.size() != Counters.size())
Justin Bognerb9bd7f82014-03-21 17:46:22 +000096 return instrprof_error::count_mismatch;
Justin Bogner821d7472014-08-01 22:50:07 +000097
Justin Bognerb9bd7f82014-03-21 17:46:22 +000098 for (size_t I = 0, E = Counters.size(); I < E; ++I) {
Justin Bogner821d7472014-08-01 22:50:07 +000099 if (FoundCounters[I] + Counters[I] < FoundCounters[I])
Justin Bognerb9bd7f82014-03-21 17:46:22 +0000100 return instrprof_error::counter_overflow;
Justin Bogner821d7472014-08-01 22:50:07 +0000101 FoundCounters[I] += Counters[I];
Justin Bognerb9bd7f82014-03-21 17:46:22 +0000102 }
Justin Bogner821d7472014-08-01 22:50:07 +0000103 // We keep track of the max function count as we go for simplicity.
104 if (FoundCounters[0] > MaxFunctionCount)
105 MaxFunctionCount = FoundCounters[0];
106
Justin Bognerb9bd7f82014-03-21 17:46:22 +0000107 return instrprof_error::success;
108}
109
Justin Bognerb7aa2632014-04-18 21:48:40 +0000110void InstrProfWriter::write(raw_fd_ostream &OS) {
111 OnDiskChainedHashTableGenerator<InstrProfRecordTrait> Generator;
Justin Bognerb9bd7f82014-03-21 17:46:22 +0000112
Justin Bognerb7aa2632014-04-18 21:48:40 +0000113 // Populate the hash table generator.
Justin Bogner821d7472014-08-01 22:50:07 +0000114 std::vector<uint64_t> CounterBuffer;
115 for (const auto &I : FunctionData)
Justin Bognerfa5b0132014-04-23 18:50:16 +0000116 Generator.insert(I.getKey(), &I.getValue());
Justin Bognerb7aa2632014-04-18 21:48:40 +0000117
118 using namespace llvm::support;
119 endian::Writer<little> LE(OS);
120
121 // Write the header.
122 LE.write<uint64_t>(IndexedInstrProf::Magic);
123 LE.write<uint64_t>(IndexedInstrProf::Version);
124 LE.write<uint64_t>(MaxFunctionCount);
125 LE.write<uint64_t>(static_cast<uint64_t>(IndexedInstrProf::HashType));
126
127 // Save a space to write the hash table start location.
128 uint64_t HashTableStartLoc = OS.tell();
129 LE.write<uint64_t>(0);
130 // Write the hash table.
131 uint64_t HashTableStart = Generator.Emit(OS);
132
133 // Go back and fill in the hash table start.
134 OS.seek(HashTableStartLoc);
135 LE.write<uint64_t>(HashTableStart);
Justin Bognerb9bd7f82014-03-21 17:46:22 +0000136}