forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeatmap.cpp
255 lines (214 loc) · 7.29 KB
/
Heatmap.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
//===- bolt/Profile/Heatmap.cpp -------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "bolt/Profile/Heatmap.h"
#include "bolt/Utils/CommandLineOpts.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
#include <cmath>
#include <vector>
#define DEBUG_TYPE "bolt-heatmap"
using namespace llvm;
namespace llvm {
namespace bolt {
void Heatmap::registerAddressRange(uint64_t StartAddress, uint64_t EndAddress,
uint64_t Count) {
if (ignoreAddress(StartAddress)) {
++NumSkippedRanges;
return;
}
if (StartAddress > EndAddress || EndAddress - StartAddress > 64 * 1024) {
LLVM_DEBUG(dbgs() << "invalid range : 0x" << Twine::utohexstr(StartAddress)
<< " -> 0x" << Twine::utohexstr(EndAddress) << '\n');
++NumSkippedRanges;
return;
}
for (uint64_t Bucket = StartAddress / BucketSize;
Bucket <= EndAddress / BucketSize; ++Bucket)
Map[Bucket] += Count;
}
void Heatmap::print(StringRef FileName) const {
std::error_code EC;
raw_fd_ostream OS(FileName, EC, sys::fs::OpenFlags::OF_None);
if (EC) {
errs() << "error opening output file: " << EC.message() << '\n';
exit(1);
}
print(OS);
}
void Heatmap::print(raw_ostream &OS) const {
const char FillChar = '.';
const auto DefaultColor = raw_ostream::WHITE;
auto changeColor = [&](raw_ostream::Colors Color) -> void {
static auto CurrentColor = raw_ostream::BLACK;
if (CurrentColor == Color)
return;
OS.changeColor(Color);
CurrentColor = Color;
};
const uint64_t BytesPerLine = opts::BucketsPerLine * BucketSize;
// Calculate the max value for scaling.
uint64_t MaxValue = 0;
for (const std::pair<const uint64_t, uint64_t> &Entry : Map)
MaxValue = std::max<uint64_t>(MaxValue, Entry.second);
// Print start of the line and fill it with an empty space right before
// the Address.
auto startLine = [&](uint64_t Address, bool Empty = false) {
changeColor(DefaultColor);
const uint64_t LineAddress = Address / BytesPerLine * BytesPerLine;
if (MaxAddress > 0xffffffff)
OS << format("0x%016" PRIx64 ": ", LineAddress);
else
OS << format("0x%08" PRIx64 ": ", LineAddress);
if (Empty)
Address = LineAddress + BytesPerLine;
for (uint64_t Fill = LineAddress; Fill < Address; Fill += BucketSize)
OS << FillChar;
};
// Finish line after \p Address was printed.
auto finishLine = [&](uint64_t Address) {
const uint64_t End = alignTo(Address + 1, BytesPerLine);
for (uint64_t Fill = Address + BucketSize; Fill < End; Fill += BucketSize)
OS << FillChar;
OS << '\n';
};
// Fill empty space in (Start, End) range.
auto fillRange = [&](uint64_t Start, uint64_t End) {
if ((Start / BytesPerLine) == (End / BytesPerLine)) {
for (uint64_t Fill = Start + BucketSize; Fill < End; Fill += BucketSize) {
changeColor(DefaultColor);
OS << FillChar;
}
return;
}
changeColor(DefaultColor);
finishLine(Start);
Start = alignTo(Start, BytesPerLine);
uint64_t NumEmptyLines = (End - Start) / BytesPerLine;
if (NumEmptyLines > 32) {
OS << '\n';
} else {
while (NumEmptyLines--) {
startLine(Start, /*Empty=*/true);
OS << '\n';
Start += BytesPerLine;
}
}
startLine(End);
};
static raw_ostream::Colors Colors[] = {
raw_ostream::WHITE, raw_ostream::WHITE, raw_ostream::CYAN,
raw_ostream::GREEN, raw_ostream::YELLOW, raw_ostream::RED};
constexpr size_t NumRanges = sizeof(Colors) / sizeof(Colors[0]);
uint64_t Range[NumRanges];
for (uint64_t I = 0; I < NumRanges; ++I)
Range[I] = std::max(I + 1, (uint64_t)std::pow((double)MaxValue,
(double)(I + 1) / NumRanges));
Range[NumRanges - 1] = std::max((uint64_t)NumRanges, MaxValue);
// Print scaled value
auto printValue = [&](uint64_t Value, bool ResetColor = false) {
assert(Value && "should only print positive values");
for (unsigned I = 0; I < sizeof(Range) / sizeof(Range[0]); ++I) {
if (Value <= Range[I]) {
changeColor(Colors[I]);
break;
}
}
if (Value <= Range[0])
OS << 'o';
else
OS << 'O';
if (ResetColor)
changeColor(DefaultColor);
};
// Print against black background
OS.changeColor(raw_ostream::BLACK, /*Bold=*/false, /*Background=*/true);
changeColor(DefaultColor);
// Print map legend
OS << "Legend:\n";
uint64_t PrevValue = 0;
for (unsigned I = 0; I < sizeof(Range) / sizeof(Range[0]); ++I) {
const uint64_t Value = Range[I];
OS << " ";
printValue(Value, true);
OS << " : (" << PrevValue << ", " << Value << "]\n";
PrevValue = Value;
}
// Pos - character position from right in hex form.
auto printHeader = [&](unsigned Pos) {
OS << " ";
if (MaxAddress > 0xffffffff)
OS << " ";
unsigned PrevValue = unsigned(-1);
for (unsigned I = 0; I < BytesPerLine; I += BucketSize) {
const unsigned Value = (I & ((1 << Pos * 4) - 1)) >> (Pos - 1) * 4;
if (Value != PrevValue) {
OS << Twine::utohexstr(Value);
PrevValue = Value;
} else {
OS << ' ';
}
}
OS << '\n';
};
for (unsigned I = 5; I > 0; --I)
printHeader(I);
uint64_t PrevAddress = 0;
for (auto MI = Map.begin(), ME = Map.end(); MI != ME; ++MI) {
const std::pair<const uint64_t, uint64_t> &Entry = *MI;
uint64_t Address = Entry.first * BucketSize;
if (PrevAddress)
fillRange(PrevAddress, Address);
else
startLine(Address);
printValue(Entry.second);
PrevAddress = Address;
}
if (PrevAddress) {
changeColor(DefaultColor);
finishLine(PrevAddress);
}
}
void Heatmap::printCDF(StringRef FileName) const {
std::error_code EC;
raw_fd_ostream OS(FileName, EC, sys::fs::OpenFlags::OF_None);
if (EC) {
errs() << "error opening output file: " << EC.message() << '\n';
exit(1);
}
printCDF(OS);
}
void Heatmap::printCDF(raw_ostream &OS) const {
uint64_t NumTotalCounts = 0;
std::vector<uint64_t> Counts;
for (const std::pair<const uint64_t, uint64_t> &KV : Map) {
Counts.push_back(KV.second);
NumTotalCounts += KV.second;
}
std::sort(Counts.begin(), Counts.end(), std::greater<uint64_t>());
double RatioLeftInKB = (1.0 * BucketSize) / 1024;
assert(NumTotalCounts > 0 &&
"total number of heatmap buckets should be greater than 0");
double RatioRightInPercent = 100.0 / NumTotalCounts;
uint64_t RunningCount = 0;
OS << "Bucket counts, Size (KB), CDF (%)\n";
for (uint64_t I = 0; I < Counts.size(); I++) {
RunningCount += Counts[I];
OS << format("%llu", (I + 1)) << ", "
<< format("%.4f", RatioLeftInKB * (I + 1)) << ", "
<< format("%.4f", RatioRightInPercent * (RunningCount)) << "\n";
}
Counts.clear();
}
} // namespace bolt
} // namespace llvm