1
- // ===------ CalcCacheMetrics .cpp - Calculate metrics of cache lines --- ----===//
1
+ // ===------ CacheMetrics .cpp - Calculate metrics for instruction cache ----===//
2
2
//
3
3
// Functions to show metrics of cache lines
4
4
//
7
7
//
8
8
// ===----------------------------------------------------------------------===//
9
9
10
-
11
- #include " BinaryBasicBlock.h"
12
- #include " BinaryContext.h"
13
- #include " BinaryFunction.h"
14
- #include " BinaryPassManager.h"
15
- #include " CalcCacheMetrics.h"
16
- #include " Exceptions.h"
17
- #include " RewriteInstance.h"
18
- #include " llvm/MC/MCAsmLayout.h"
19
- #include " llvm/MC/MCObjectStreamer.h"
20
- #include " llvm/MC/MCSectionELF.h"
21
- #include < fstream>
10
+ #include " CacheMetrics.h"
22
11
23
12
using namespace llvm ;
24
- using namespace object ;
25
13
using namespace bolt ;
26
14
using Traversal = std::vector<BinaryBasicBlock *>;
27
15
28
- namespace opts {
29
-
30
- extern cl::OptionCategory BoltOptCategory;
31
-
32
- } // namespace opts
33
-
34
-
35
16
namespace {
36
17
37
18
// / Initialize and return a position map for binary basic blocks.
@@ -47,10 +28,8 @@ getPositionMap(const BinaryFunction &Function) {
47
28
return DistMap;
48
29
}
49
30
50
- // / Initialize and return a vector of traversals for a given function and its
51
- // / entry point
52
- std::vector<Traversal> getTraversals (const BinaryFunction &Function,
53
- BinaryBasicBlock *EntryBB) {
31
+ // / Initialize and return a vector of traversals for a given entry block
32
+ std::vector<Traversal> getTraversals (BinaryBasicBlock *EntryBB) {
54
33
std::vector<Traversal> AllTraversals;
55
34
std::stack<std::pair<BinaryBasicBlock *, Traversal>> Stack;
56
35
Stack.push (std::make_pair (EntryBB, Traversal ()));
@@ -105,10 +84,6 @@ std::vector<Traversal> getTraversals(const BinaryFunction &Function,
105
84
double
106
85
getTraversalLength (std::unordered_map<BinaryBasicBlock *, double > &DistMap,
107
86
Traversal const &Path) {
108
- if (Path.size () <= 1 ) {
109
- return 0.0 ;
110
- }
111
-
112
87
double Length = 0.0 ;
113
88
BinaryBasicBlock *PrevBB = Path.front ();
114
89
for (auto BBI = std::next (Path.begin ()); BBI != Path.end (); ++BBI) {
@@ -119,56 +94,62 @@ getTraversalLength(std::unordered_map<BinaryBasicBlock *, double> &DistMap,
119
94
return Length;
120
95
}
121
96
122
- // / Helper function of calcGraphDistance to go through the call traversals of
123
- // / certain function and to calculate and record the length of each
124
- // / traversal.
125
- void graphDistHelper (std::vector<Traversal> &AllTraversals,
126
- const BinaryFunction &Function,
127
- std::unordered_map<uint64_t , double > &TraversalMap,
128
- uint64_t &TraversalCount) {
129
- auto DistMap = getPositionMap (Function);
130
-
131
- for (auto const &Path : AllTraversals) {
132
- TraversalMap[++TraversalCount] = getTraversalLength (DistMap, Path);
133
- }
134
- }
135
- }
136
-
137
- void CalcCacheMetrics::calcGraphDistance (
138
- const std::map<uint64_t , BinaryFunction> &BinaryFunctions) {
139
-
140
- double TotalFuncValue = 0 ;
141
- uint64_t FuncCount = 0 ;
142
- for (auto &BFI : BinaryFunctions) {
143
- auto &Function = BFI.second ;
97
+ // / Calculate average number of call distance for every graph traversal
98
+ double calcGraphDistance (const std::vector<BinaryFunction *> &BinaryFunctions) {
99
+ double TotalTraversalLength = 0 ;
100
+ double NumTraversals = 0 ;
101
+ for (auto BF : BinaryFunctions) {
144
102
// Only consider functions which are known to be executed
145
- if (Function. getKnownExecutionCount () == 0 )
103
+ if (BF-> getKnownExecutionCount () == 0 )
146
104
continue ;
147
105
148
- std::unordered_map<uint64_t , double > TraversalMap;
149
- uint64_t TraversalCount = 0 ;
150
- for (auto *BB : Function.layout ()) {
106
+ for (auto BB : BF->layout ()) {
151
107
if (BB->isEntryPoint ()) {
152
- auto AllTraversals = getTraversals (Function, BB);
153
- graphDistHelper (AllTraversals, Function, TraversalMap, TraversalCount);
108
+ auto AllTraversals = getTraversals (BB);
109
+ auto DistMap = getPositionMap (*BF);
110
+ for (auto const &Path : AllTraversals) {
111
+ // Ignore short traversals
112
+ if (Path.size () <= 1 )
113
+ continue ;
114
+ TotalTraversalLength += getTraversalLength (DistMap, Path);
115
+ NumTraversals++;
116
+ }
154
117
}
155
118
}
119
+ }
156
120
157
- double TotalValue = 0 ;
158
- for (auto const &Entry : TraversalMap) {
159
- TotalValue += Entry.second ;
160
- }
121
+ return TotalTraversalLength / NumTraversals;
122
+ }
161
123
162
- double AverageValue =
163
- TraversalMap.empty () ? 0 : (TotalValue * 1.0 / TraversalMap.size ());
164
- TotalFuncValue += AverageValue;
165
- FuncCount += TraversalMap.empty () ? 0 : 1 ;
124
+ }
125
+
126
+ void CacheMetrics::printAll (
127
+ const std::vector<BinaryFunction *> &BinaryFunctions) {
128
+
129
+ size_t NumFunctions = 0 ;
130
+ size_t NumHotFunctions = 0 ;
131
+ size_t NumBlocks = 0 ;
132
+ size_t NumHotBlocks = 0 ;
133
+
134
+ for (auto BF : BinaryFunctions) {
135
+ NumFunctions++;
136
+ if (BF->getKnownExecutionCount () > 0 )
137
+ NumHotFunctions++;
138
+ for (auto BB : BF->layout ()) {
139
+ NumBlocks++;
140
+ if (BB->getKnownExecutionCount () > 0 )
141
+ NumHotBlocks++;
142
+ }
166
143
}
167
144
168
- outs () << format (" Sum of averages of traversal distance for all "
169
- " functions is: %.2f\n " ,
170
- TotalFuncValue)
171
- << format (" There are %u functions in total\n " , FuncCount)
172
- << format (" On average, every traversal is %.2f long\n\n " ,
173
- TotalFuncValue / FuncCount);
145
+ outs () << format (" There are %zu functions;" , NumFunctions)
146
+ << format (" %zu (%.2lf%%) have non-empty execution count\n " ,
147
+ NumHotFunctions, 100.0 * NumHotFunctions / NumFunctions);
148
+ outs () << format (" There are %zu basic blocks;" , NumBlocks)
149
+ << format (" %zu (%.2lf%%) have non-empty execution count\n " ,
150
+ NumHotBlocks, 100.0 * NumHotBlocks / NumBlocks);
151
+
152
+ const auto GraphDistance = calcGraphDistance (BinaryFunctions);
153
+ outs () << " An average length of graph traversal is "
154
+ << format (" %.2lf\n " , GraphDistance);
174
155
}
0 commit comments