Justin Bogner | b9bd7f8 | 2014-03-21 17:46:22 +0000 | [diff] [blame] | 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" |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/StringExtras.h" |
| 17 | #include "llvm/Support/EndianStream.h" |
| 18 | #include "llvm/Support/OnDiskHashTable.h" |
| 19 | |
| 20 | #include "InstrProfIndexed.h" |
Justin Bogner | b9bd7f8 | 2014-03-21 17:46:22 +0000 | [diff] [blame] | 21 | |
| 22 | using namespace llvm; |
| 23 | |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 24 | namespace { |
| 25 | class InstrProfRecordTrait { |
| 26 | public: |
| 27 | typedef StringRef key_type; |
| 28 | typedef StringRef key_type_ref; |
| 29 | |
Justin Bogner | fa5b013 | 2014-04-23 18:50:16 +0000 | [diff] [blame] | 30 | typedef const InstrProfWriter::CounterData *const data_type; |
| 31 | typedef const InstrProfWriter::CounterData *const data_type_ref; |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 32 | |
| 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 Bogner | e808171 | 2014-04-19 00:33:15 +0000 | [diff] [blame] | 45 | offset_type N = K.size(); |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 46 | LE.write<offset_type>(N); |
| 47 | |
Justin Bogner | 821d747 | 2014-08-01 22:50:07 +0000 | [diff] [blame] | 48 | offset_type M = 0; |
| 49 | for (const auto &Counts : *V) |
| 50 | M += (2 + Counts.second.size()) * sizeof(uint64_t); |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 51 | LE.write<offset_type>(M); |
| 52 | |
| 53 | return std::make_pair(N, M); |
| 54 | } |
| 55 | |
Justin Bogner | e808171 | 2014-04-19 00:33:15 +0000 | [diff] [blame] | 56 | static void EmitKey(raw_ostream &Out, key_type_ref K, offset_type N){ |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 57 | Out.write(K.data(), N); |
| 58 | } |
| 59 | |
| 60 | static void EmitData(raw_ostream &Out, key_type_ref, data_type_ref V, |
Justin Bogner | e808171 | 2014-04-19 00:33:15 +0000 | [diff] [blame] | 61 | offset_type) { |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 62 | using namespace llvm::support; |
| 63 | endian::Writer<little> LE(Out); |
Justin Bogner | 821d747 | 2014-08-01 22:50:07 +0000 | [diff] [blame] | 64 | |
| 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 Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 71 | } |
| 72 | }; |
| 73 | } |
| 74 | |
Rafael Espindola | 3acea39 | 2014-06-12 21:46:39 +0000 | [diff] [blame] | 75 | std::error_code |
| 76 | InstrProfWriter::addFunctionCounts(StringRef FunctionName, |
| 77 | uint64_t FunctionHash, |
| 78 | ArrayRef<uint64_t> Counters) { |
Justin Bogner | 821d747 | 2014-08-01 22:50:07 +0000 | [diff] [blame] | 79 | 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 Bogner | a2bfd66 | 2014-04-19 23:42:50 +0000 | [diff] [blame] | 88 | return instrprof_error::success; |
Justin Bogner | b9bd7f8 | 2014-03-21 17:46:22 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Justin Bogner | 821d747 | 2014-08-01 22:50:07 +0000 | [diff] [blame] | 91 | // 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 Bogner | b9bd7f8 | 2014-03-21 17:46:22 +0000 | [diff] [blame] | 96 | return instrprof_error::count_mismatch; |
Justin Bogner | 821d747 | 2014-08-01 22:50:07 +0000 | [diff] [blame] | 97 | |
Justin Bogner | b9bd7f8 | 2014-03-21 17:46:22 +0000 | [diff] [blame] | 98 | for (size_t I = 0, E = Counters.size(); I < E; ++I) { |
Justin Bogner | 821d747 | 2014-08-01 22:50:07 +0000 | [diff] [blame] | 99 | if (FoundCounters[I] + Counters[I] < FoundCounters[I]) |
Justin Bogner | b9bd7f8 | 2014-03-21 17:46:22 +0000 | [diff] [blame] | 100 | return instrprof_error::counter_overflow; |
Justin Bogner | 821d747 | 2014-08-01 22:50:07 +0000 | [diff] [blame] | 101 | FoundCounters[I] += Counters[I]; |
Justin Bogner | b9bd7f8 | 2014-03-21 17:46:22 +0000 | [diff] [blame] | 102 | } |
Justin Bogner | 821d747 | 2014-08-01 22:50:07 +0000 | [diff] [blame] | 103 | // We keep track of the max function count as we go for simplicity. |
| 104 | if (FoundCounters[0] > MaxFunctionCount) |
| 105 | MaxFunctionCount = FoundCounters[0]; |
| 106 | |
Justin Bogner | b9bd7f8 | 2014-03-21 17:46:22 +0000 | [diff] [blame] | 107 | return instrprof_error::success; |
| 108 | } |
| 109 | |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 110 | void InstrProfWriter::write(raw_fd_ostream &OS) { |
| 111 | OnDiskChainedHashTableGenerator<InstrProfRecordTrait> Generator; |
Justin Bogner | b9bd7f8 | 2014-03-21 17:46:22 +0000 | [diff] [blame] | 112 | |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 113 | // Populate the hash table generator. |
Justin Bogner | 821d747 | 2014-08-01 22:50:07 +0000 | [diff] [blame] | 114 | std::vector<uint64_t> CounterBuffer; |
| 115 | for (const auto &I : FunctionData) |
Justin Bogner | fa5b013 | 2014-04-23 18:50:16 +0000 | [diff] [blame] | 116 | Generator.insert(I.getKey(), &I.getValue()); |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 117 | |
| 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 Bogner | b9bd7f8 | 2014-03-21 17:46:22 +0000 | [diff] [blame] | 136 | } |