OLD | NEW |
1 //===- subzero/src/IceTimerTree.cpp - Pass timer defs ---------------------===// | 1 //===- subzero/src/IceTimerTree.cpp - Pass timer defs ---------------------===// |
2 // | 2 // |
3 // The Subzero Code Generator | 3 // The Subzero Code Generator |
4 // | 4 // |
5 // This file is distributed under the University of Illinois Open Source | 5 // This file is distributed under the University of Illinois Open Source |
6 // License. See LICENSE.TXT for details. | 6 // License. See LICENSE.TXT for details. |
7 // | 7 // |
8 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
9 /// | 9 /// |
10 /// \file | 10 /// \file |
11 /// This file defines the TimerTree class, which tracks flat and | 11 /// This file defines the TimerTree class, which tracks flat and cumulative |
12 /// cumulative execution time collection of call chains. | 12 /// execution time collection of call chains. |
13 /// | 13 /// |
14 //===----------------------------------------------------------------------===// | 14 //===----------------------------------------------------------------------===// |
15 | 15 |
16 #include "IceTimerTree.h" | 16 #include "IceTimerTree.h" |
17 | 17 |
18 #include "IceDefs.h" | 18 #include "IceDefs.h" |
19 | 19 |
20 #pragma clang diagnostic push | 20 #pragma clang diagnostic push |
21 #pragma clang diagnostic ignored "-Wunused-parameter" | 21 #pragma clang diagnostic ignored "-Wunused-parameter" |
22 #include "llvm/Support/Timer.h" | 22 #include "llvm/Support/Timer.h" |
(...skipping 11 matching lines...) Expand all Loading... |
34 LeafCounts.resize(TT__num); | 34 LeafCounts.resize(TT__num); |
35 #define STR(s) #s | 35 #define STR(s) #s |
36 #define X(tag) \ | 36 #define X(tag) \ |
37 IDs[TT_##tag] = STR(tag); \ | 37 IDs[TT_##tag] = STR(tag); \ |
38 IDsIndex[STR(tag)] = TT_##tag; | 38 IDsIndex[STR(tag)] = TT_##tag; |
39 TIMERTREE_TABLE; | 39 TIMERTREE_TABLE; |
40 #undef X | 40 #undef X |
41 #undef STR | 41 #undef STR |
42 } | 42 } |
43 | 43 |
44 // Returns the unique timer ID for the given Name, creating a new ID | 44 // Returns the unique timer ID for the given Name, creating a new ID if needed. |
45 // if needed. | |
46 TimerIdT TimerStack::getTimerID(const IceString &Name) { | 45 TimerIdT TimerStack::getTimerID(const IceString &Name) { |
47 if (!BuildDefs::dump()) | 46 if (!BuildDefs::dump()) |
48 return 0; | 47 return 0; |
49 if (IDsIndex.find(Name) == IDsIndex.end()) { | 48 if (IDsIndex.find(Name) == IDsIndex.end()) { |
50 IDsIndex[Name] = IDs.size(); | 49 IDsIndex[Name] = IDs.size(); |
51 IDs.push_back(Name); | 50 IDs.push_back(Name); |
52 LeafTimes.push_back(decltype(LeafTimes)::value_type()); | 51 LeafTimes.push_back(decltype(LeafTimes)::value_type()); |
53 LeafCounts.push_back(decltype(LeafCounts)::value_type()); | 52 LeafCounts.push_back(decltype(LeafCounts)::value_type()); |
54 } | 53 } |
55 return IDsIndex[Name]; | 54 return IDsIndex[Name]; |
56 } | 55 } |
57 | 56 |
58 // Creates a mapping from TimerIdT (leaf) values in the Src timer | 57 // Creates a mapping from TimerIdT (leaf) values in the Src timer stack into |
59 // stack into TimerIdT values in this timer stack. Creates new | 58 // TimerIdT values in this timer stack. Creates new entries in this timer stack |
60 // entries in this timer stack as needed. | 59 // as needed. |
61 TimerStack::TranslationType | 60 TimerStack::TranslationType |
62 TimerStack::translateIDsFrom(const TimerStack &Src) { | 61 TimerStack::translateIDsFrom(const TimerStack &Src) { |
63 size_t Size = Src.IDs.size(); | 62 size_t Size = Src.IDs.size(); |
64 TranslationType Mapping(Size); | 63 TranslationType Mapping(Size); |
65 for (TimerIdT i = 0; i < Size; ++i) { | 64 for (TimerIdT i = 0; i < Size; ++i) { |
66 Mapping[i] = getTimerID(Src.IDs[i]); | 65 Mapping[i] = getTimerID(Src.IDs[i]); |
67 } | 66 } |
68 return Mapping; | 67 return Mapping; |
69 } | 68 } |
70 | 69 |
71 // Merges two timer stacks, by combining and summing corresponding | 70 // Merges two timer stacks, by combining and summing corresponding entries. |
72 // entries. This timer stack is updated from Src. | 71 // This timer stack is updated from Src. |
73 void TimerStack::mergeFrom(const TimerStack &Src) { | 72 void TimerStack::mergeFrom(const TimerStack &Src) { |
74 if (!BuildDefs::dump()) | 73 if (!BuildDefs::dump()) |
75 return; | 74 return; |
76 TranslationType Mapping = translateIDsFrom(Src); | 75 TranslationType Mapping = translateIDsFrom(Src); |
77 TTindex SrcIndex = 0; | 76 TTindex SrcIndex = 0; |
78 for (const TimerTreeNode &SrcNode : Src.Nodes) { | 77 for (const TimerTreeNode &SrcNode : Src.Nodes) { |
79 // The first node is reserved as a sentinel, so avoid it. | 78 // The first node is reserved as a sentinel, so avoid it. |
80 if (SrcIndex > 0) { | 79 if (SrcIndex > 0) { |
81 // Find the full path to the Src node, translated to path | 80 // Find the full path to the Src node, translated to path components |
82 // components corresponding to this timer stack. | 81 // corresponding to this timer stack. |
83 PathType MyPath = Src.getPath(SrcIndex, Mapping); | 82 PathType MyPath = Src.getPath(SrcIndex, Mapping); |
84 // Find a node in this timer stack corresponding to the given | 83 // Find a node in this timer stack corresponding to the given path, |
85 // path, creating new interior nodes as necessary. | 84 // creating new interior nodes as necessary. |
86 TTindex MyIndex = findPath(MyPath); | 85 TTindex MyIndex = findPath(MyPath); |
87 Nodes[MyIndex].Time += SrcNode.Time; | 86 Nodes[MyIndex].Time += SrcNode.Time; |
88 Nodes[MyIndex].UpdateCount += SrcNode.UpdateCount; | 87 Nodes[MyIndex].UpdateCount += SrcNode.UpdateCount; |
89 } | 88 } |
90 ++SrcIndex; | 89 ++SrcIndex; |
91 } | 90 } |
92 for (TimerIdT i = 0; i < Src.LeafTimes.size(); ++i) { | 91 for (TimerIdT i = 0; i < Src.LeafTimes.size(); ++i) { |
93 LeafTimes[Mapping[i]] += Src.LeafTimes[i]; | 92 LeafTimes[Mapping[i]] += Src.LeafTimes[i]; |
94 LeafCounts[Mapping[i]] += Src.LeafCounts[i]; | 93 LeafCounts[Mapping[i]] += Src.LeafCounts[i]; |
95 } | 94 } |
96 StateChangeCount += Src.StateChangeCount; | 95 StateChangeCount += Src.StateChangeCount; |
97 } | 96 } |
98 | 97 |
99 // Constructs a path consisting of the sequence of leaf values leading | 98 // Constructs a path consisting of the sequence of leaf values leading to a |
100 // to a given node, with the Mapping translation applied to the leaf | 99 // given node, with the Mapping translation applied to the leaf values. The |
101 // values. The path ends up being in "reverse" order, i.e. from leaf | 100 // path ends up being in "reverse" order, i.e. from leaf to root. |
102 // to root. | |
103 TimerStack::PathType TimerStack::getPath(TTindex Index, | 101 TimerStack::PathType TimerStack::getPath(TTindex Index, |
104 const TranslationType &Mapping) const { | 102 const TranslationType &Mapping) const { |
105 PathType Path; | 103 PathType Path; |
106 while (Index) { | 104 while (Index) { |
107 Path.push_back(Mapping[Nodes[Index].Interior]); | 105 Path.push_back(Mapping[Nodes[Index].Interior]); |
108 assert(Nodes[Index].Parent < Index); | 106 assert(Nodes[Index].Parent < Index); |
109 Index = Nodes[Index].Parent; | 107 Index = Nodes[Index].Parent; |
110 } | 108 } |
111 return Path; | 109 return Path; |
112 } | 110 } |
113 | 111 |
114 // Given a parent node and a leaf ID, returns the index of the | 112 // Given a parent node and a leaf ID, returns the index of the parent's child |
115 // parent's child ID, creating a new node for the child as necessary. | 113 // ID, creating a new node for the child as necessary. |
116 TimerStack::TTindex TimerStack::getChildIndex(TimerStack::TTindex Parent, | 114 TimerStack::TTindex TimerStack::getChildIndex(TimerStack::TTindex Parent, |
117 TimerIdT ID) { | 115 TimerIdT ID) { |
118 if (Nodes[Parent].Children.size() <= ID) | 116 if (Nodes[Parent].Children.size() <= ID) |
119 Nodes[Parent].Children.resize(ID + 1); | 117 Nodes[Parent].Children.resize(ID + 1); |
120 if (Nodes[Parent].Children[ID] == 0) { | 118 if (Nodes[Parent].Children[ID] == 0) { |
121 TTindex Size = Nodes.size(); | 119 TTindex Size = Nodes.size(); |
122 Nodes[Parent].Children[ID] = Size; | 120 Nodes[Parent].Children[ID] = Size; |
123 Nodes.resize(Size + 1); | 121 Nodes.resize(Size + 1); |
124 Nodes[Size].Parent = Parent; | 122 Nodes[Size].Parent = Parent; |
125 Nodes[Size].Interior = ID; | 123 Nodes[Size].Interior = ID; |
126 } | 124 } |
127 return Nodes[Parent].Children[ID]; | 125 return Nodes[Parent].Children[ID]; |
128 } | 126 } |
129 | 127 |
130 // Finds a node in the timer stack corresponding to the given path, | 128 // Finds a node in the timer stack corresponding to the given path, creating |
131 // creating new interior nodes as necessary. | 129 // new interior nodes as necessary. |
132 TimerStack::TTindex TimerStack::findPath(const PathType &Path) { | 130 TimerStack::TTindex TimerStack::findPath(const PathType &Path) { |
133 TTindex CurIndex = 0; | 131 TTindex CurIndex = 0; |
134 // The path is in reverse order (leaf to root), so it needs to be | 132 // The path is in reverse order (leaf to root), so it needs to be followed in |
135 // followed in reverse. | 133 // reverse. |
136 for (TTindex Index : reverse_range(Path)) { | 134 for (TTindex Index : reverse_range(Path)) { |
137 CurIndex = getChildIndex(CurIndex, Index); | 135 CurIndex = getChildIndex(CurIndex, Index); |
138 } | 136 } |
139 assert(CurIndex); // shouldn't be the sentinel node | 137 assert(CurIndex); // shouldn't be the sentinel node |
140 return CurIndex; | 138 return CurIndex; |
141 } | 139 } |
142 | 140 |
143 // Pushes a new marker onto the timer stack. | 141 // Pushes a new marker onto the timer stack. |
144 void TimerStack::push(TimerIdT ID) { | 142 void TimerStack::push(TimerIdT ID) { |
145 if (!BuildDefs::dump()) | 143 if (!BuildDefs::dump()) |
146 return; | 144 return; |
147 const bool UpdateCounts = false; | 145 const bool UpdateCounts = false; |
148 update(UpdateCounts); | 146 update(UpdateCounts); |
149 StackTop = getChildIndex(StackTop, ID); | 147 StackTop = getChildIndex(StackTop, ID); |
150 assert(StackTop); | 148 assert(StackTop); |
151 } | 149 } |
152 | 150 |
153 // Pops the top marker from the timer stack. Validates via assert() | 151 // Pops the top marker from the timer stack. Validates via assert() that the |
154 // that the expected marker is popped. | 152 // expected marker is popped. |
155 void TimerStack::pop(TimerIdT ID) { | 153 void TimerStack::pop(TimerIdT ID) { |
156 if (!BuildDefs::dump()) | 154 if (!BuildDefs::dump()) |
157 return; | 155 return; |
158 const bool UpdateCounts = true; | 156 const bool UpdateCounts = true; |
159 update(UpdateCounts); | 157 update(UpdateCounts); |
160 assert(StackTop); | 158 assert(StackTop); |
161 assert(Nodes[StackTop].Parent < StackTop); | 159 assert(Nodes[StackTop].Parent < StackTop); |
162 // Verify that the expected ID is being popped. | 160 // Verify that the expected ID is being popped. |
163 assert(Nodes[StackTop].Interior == ID); | 161 assert(Nodes[StackTop].Interior == ID); |
164 (void)ID; | 162 (void)ID; |
165 // Verify that the parent's child points to the current stack top. | 163 // Verify that the parent's child points to the current stack top. |
166 assert(Nodes[Nodes[StackTop].Parent].Children[ID] == StackTop); | 164 assert(Nodes[Nodes[StackTop].Parent].Children[ID] == StackTop); |
167 StackTop = Nodes[StackTop].Parent; | 165 StackTop = Nodes[StackTop].Parent; |
168 } | 166 } |
169 | 167 |
170 // At a state change (e.g. push or pop), updates the flat and | 168 // At a state change (e.g. push or pop), updates the flat and cumulative |
171 // cumulative timings for everything on the timer stack. | 169 // timings for everything on the timer stack. |
172 void TimerStack::update(bool UpdateCounts) { | 170 void TimerStack::update(bool UpdateCounts) { |
173 if (!BuildDefs::dump()) | 171 if (!BuildDefs::dump()) |
174 return; | 172 return; |
175 ++StateChangeCount; | 173 ++StateChangeCount; |
176 // Whenever the stack is about to change, we grab the time delta | 174 // Whenever the stack is about to change, we grab the time delta since the |
177 // since the last change and add it to all active cumulative | 175 // last change and add it to all active cumulative elements and to the flat |
178 // elements and to the flat element for the top of the stack. | 176 // element for the top of the stack. |
179 double Current = timestamp(); | 177 double Current = timestamp(); |
180 double Delta = Current - LastTimestamp; | 178 double Delta = Current - LastTimestamp; |
181 if (StackTop) { | 179 if (StackTop) { |
182 TimerIdT Leaf = Nodes[StackTop].Interior; | 180 TimerIdT Leaf = Nodes[StackTop].Interior; |
183 if (Leaf >= LeafTimes.size()) { | 181 if (Leaf >= LeafTimes.size()) { |
184 LeafTimes.resize(Leaf + 1); | 182 LeafTimes.resize(Leaf + 1); |
185 LeafCounts.resize(Leaf + 1); | 183 LeafCounts.resize(Leaf + 1); |
186 } | 184 } |
187 LeafTimes[Leaf] += Delta; | 185 LeafTimes[Leaf] += Delta; |
188 if (UpdateCounts) | 186 if (UpdateCounts) |
189 ++LeafCounts[Leaf]; | 187 ++LeafCounts[Leaf]; |
190 } | 188 } |
191 TTindex Prefix = StackTop; | 189 TTindex Prefix = StackTop; |
192 while (Prefix) { | 190 while (Prefix) { |
193 Nodes[Prefix].Time += Delta; | 191 Nodes[Prefix].Time += Delta; |
194 // Only update a leaf node count, not the internal node counts. | 192 // Only update a leaf node count, not the internal node counts. |
195 if (UpdateCounts && Prefix == StackTop) | 193 if (UpdateCounts && Prefix == StackTop) |
196 ++Nodes[Prefix].UpdateCount; | 194 ++Nodes[Prefix].UpdateCount; |
197 TTindex Next = Nodes[Prefix].Parent; | 195 TTindex Next = Nodes[Prefix].Parent; |
198 assert(Next < Prefix); | 196 assert(Next < Prefix); |
199 Prefix = Next; | 197 Prefix = Next; |
200 } | 198 } |
201 // Capture the next timestamp *after* the updates are finished. | 199 // Capture the next timestamp *after* the updates are finished. This |
202 // This minimizes how much the timer can perturb the reported | 200 // minimizes how much the timer can perturb the reported timing. The numbers |
203 // timing. The numbers may not sum to 100%, and the missing amount | 201 // may not sum to 100%, and the missing amount is indicative of the overhead |
204 // is indicative of the overhead of timing. | 202 // of timing. |
205 LastTimestamp = timestamp(); | 203 LastTimestamp = timestamp(); |
206 } | 204 } |
207 | 205 |
208 void TimerStack::reset() { | 206 void TimerStack::reset() { |
209 if (!BuildDefs::dump()) | 207 if (!BuildDefs::dump()) |
210 return; | 208 return; |
211 StateChangeCount = 0; | 209 StateChangeCount = 0; |
212 FirstTimestamp = LastTimestamp = timestamp(); | 210 FirstTimestamp = LastTimestamp = timestamp(); |
213 LeafTimes.assign(LeafTimes.size(), 0); | 211 LeafTimes.assign(LeafTimes.size(), 0); |
214 LeafCounts.assign(LeafCounts.size(), 0); | 212 LeafCounts.assign(LeafCounts.size(), 0); |
(...skipping 12 matching lines...) Expand all Loading... |
227 if (!BuildDefs::dump()) | 225 if (!BuildDefs::dump()) |
228 return; | 226 return; |
229 for (auto &I : reverse_range(Map)) { | 227 for (auto &I : reverse_range(Map)) { |
230 char buf[80]; | 228 char buf[80]; |
231 snprintf(buf, llvm::array_lengthof(buf), " %10.6f (%4.1f%%): ", I.first, | 229 snprintf(buf, llvm::array_lengthof(buf), " %10.6f (%4.1f%%): ", I.first, |
232 I.first * 100 / TotalTime); | 230 I.first * 100 / TotalTime); |
233 Str << buf << I.second << "\n"; | 231 Str << buf << I.second << "\n"; |
234 } | 232 } |
235 } | 233 } |
236 | 234 |
237 // Write a printf() format string into Buf[], in the format "[%5lu] ", | 235 // Write a printf() format string into Buf[], in the format "[%5lu] ", where |
238 // where "5" is actually the number of digits in MaxVal. E.g., | 236 // "5" is actually the number of digits in MaxVal. E.g., |
239 // MaxVal=0 ==> "[%1lu] " | 237 // MaxVal=0 ==> "[%1lu] " |
240 // MaxVal=5 ==> "[%1lu] " | 238 // MaxVal=5 ==> "[%1lu] " |
241 // MaxVal=9876 ==> "[%4lu] " | 239 // MaxVal=9876 ==> "[%4lu] " |
242 void makePrintfFormatString(char *Buf, size_t BufLen, size_t MaxVal) { | 240 void makePrintfFormatString(char *Buf, size_t BufLen, size_t MaxVal) { |
243 if (!BuildDefs::dump()) | 241 if (!BuildDefs::dump()) |
244 return; | 242 return; |
245 int NumDigits = 0; | 243 int NumDigits = 0; |
246 do { | 244 do { |
247 ++NumDigits; | 245 ++NumDigits; |
248 MaxVal /= 10; | 246 MaxVal /= 10; |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
303 dumpHelper(Str, FlatMap, TotalTime); | 301 dumpHelper(Str, FlatMap, TotalTime); |
304 Str << "Number of timer updates: " << StateChangeCount << "\n"; | 302 Str << "Number of timer updates: " << StateChangeCount << "\n"; |
305 } | 303 } |
306 | 304 |
307 double TimerStack::timestamp() { | 305 double TimerStack::timestamp() { |
308 // TODO: Implement in terms of std::chrono for C++11. | 306 // TODO: Implement in terms of std::chrono for C++11. |
309 return llvm::TimeRecord::getCurrentTime(false).getWallTime(); | 307 return llvm::TimeRecord::getCurrentTime(false).getWallTime(); |
310 } | 308 } |
311 | 309 |
312 } // end of namespace Ice | 310 } // end of namespace Ice |
OLD | NEW |