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 |
(...skipping 14 matching lines...) Expand all Loading... |
25 #include "llvm/Support/Timer.h" | 25 #include "llvm/Support/Timer.h" |
26 | 26 |
27 #ifdef __clang__ | 27 #ifdef __clang__ |
28 #pragma clang diagnostic pop | 28 #pragma clang diagnostic pop |
29 #endif // __clang__ | 29 #endif // __clang__ |
30 | 30 |
31 namespace Ice { | 31 namespace Ice { |
32 | 32 |
33 TimerStack::TimerStack(const IceString &Name) | 33 TimerStack::TimerStack(const IceString &Name) |
34 : Name(Name), FirstTimestamp(timestamp()), LastTimestamp(FirstTimestamp) { | 34 : Name(Name), FirstTimestamp(timestamp()), LastTimestamp(FirstTimestamp) { |
35 if (!BuildDefs::dump()) | 35 if (!BuildDefs::timers()) |
36 return; | 36 return; |
37 Nodes.resize(1); // Reserve Nodes[0] for the root node (sentinel). | 37 Nodes.resize(1); // Reserve Nodes[0] for the root node (sentinel). |
38 IDs.resize(TT__num); | 38 IDs.resize(TT__num); |
39 LeafTimes.resize(TT__num); | 39 LeafTimes.resize(TT__num); |
40 LeafCounts.resize(TT__num); | 40 LeafCounts.resize(TT__num); |
41 #define STR(s) #s | 41 #define STR(s) #s |
42 #define X(tag) \ | 42 #define X(tag) \ |
43 IDs[TT_##tag] = STR(tag); \ | 43 IDs[TT_##tag] = STR(tag); \ |
44 IDsIndex[STR(tag)] = TT_##tag; | 44 IDsIndex[STR(tag)] = TT_##tag; |
45 TIMERTREE_TABLE; | 45 TIMERTREE_TABLE; |
46 #undef X | 46 #undef X |
47 #undef STR | 47 #undef STR |
48 } | 48 } |
49 | 49 |
50 // Returns the unique timer ID for the given Name, creating a new ID if needed. | 50 // Returns the unique timer ID for the given Name, creating a new ID if needed. |
51 TimerIdT TimerStack::getTimerID(const IceString &Name) { | 51 TimerIdT TimerStack::getTimerID(const IceString &Name) { |
52 if (!BuildDefs::dump()) | 52 if (!BuildDefs::timers()) |
53 return 0; | 53 return 0; |
54 if (IDsIndex.find(Name) == IDsIndex.end()) { | 54 if (IDsIndex.find(Name) == IDsIndex.end()) { |
55 IDsIndex[Name] = IDs.size(); | 55 IDsIndex[Name] = IDs.size(); |
56 IDs.push_back(Name); | 56 IDs.push_back(Name); |
57 LeafTimes.push_back(decltype(LeafTimes)::value_type()); | 57 LeafTimes.push_back(decltype(LeafTimes)::value_type()); |
58 LeafCounts.push_back(decltype(LeafCounts)::value_type()); | 58 LeafCounts.push_back(decltype(LeafCounts)::value_type()); |
59 } | 59 } |
60 return IDsIndex[Name]; | 60 return IDsIndex[Name]; |
61 } | 61 } |
62 | 62 |
63 // Creates a mapping from TimerIdT (leaf) values in the Src timer stack into | 63 // Creates a mapping from TimerIdT (leaf) values in the Src timer stack into |
64 // TimerIdT values in this timer stack. Creates new entries in this timer stack | 64 // TimerIdT values in this timer stack. Creates new entries in this timer stack |
65 // as needed. | 65 // as needed. |
66 TimerStack::TranslationType | 66 TimerStack::TranslationType |
67 TimerStack::translateIDsFrom(const TimerStack &Src) { | 67 TimerStack::translateIDsFrom(const TimerStack &Src) { |
68 size_t Size = Src.IDs.size(); | 68 size_t Size = Src.IDs.size(); |
69 TranslationType Mapping(Size); | 69 TranslationType Mapping(Size); |
70 for (TimerIdT i = 0; i < Size; ++i) { | 70 for (TimerIdT i = 0; i < Size; ++i) { |
71 Mapping[i] = getTimerID(Src.IDs[i]); | 71 Mapping[i] = getTimerID(Src.IDs[i]); |
72 } | 72 } |
73 return Mapping; | 73 return Mapping; |
74 } | 74 } |
75 | 75 |
76 // Merges two timer stacks, by combining and summing corresponding entries. | 76 // Merges two timer stacks, by combining and summing corresponding entries. |
77 // This timer stack is updated from Src. | 77 // This timer stack is updated from Src. |
78 void TimerStack::mergeFrom(const TimerStack &Src) { | 78 void TimerStack::mergeFrom(const TimerStack &Src) { |
79 if (!BuildDefs::dump()) | 79 if (!BuildDefs::timers()) |
80 return; | 80 return; |
81 TranslationType Mapping = translateIDsFrom(Src); | 81 TranslationType Mapping = translateIDsFrom(Src); |
82 TTindex SrcIndex = 0; | 82 TTindex SrcIndex = 0; |
83 for (const TimerTreeNode &SrcNode : Src.Nodes) { | 83 for (const TimerTreeNode &SrcNode : Src.Nodes) { |
84 // The first node is reserved as a sentinel, so avoid it. | 84 // The first node is reserved as a sentinel, so avoid it. |
85 if (SrcIndex > 0) { | 85 if (SrcIndex > 0) { |
86 // Find the full path to the Src node, translated to path components | 86 // Find the full path to the Src node, translated to path components |
87 // corresponding to this timer stack. | 87 // corresponding to this timer stack. |
88 PathType MyPath = Src.getPath(SrcIndex, Mapping); | 88 PathType MyPath = Src.getPath(SrcIndex, Mapping); |
89 // Find a node in this timer stack corresponding to the given path, | 89 // Find a node in this timer stack corresponding to the given path, |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 // reverse. | 139 // reverse. |
140 for (TTindex Index : reverse_range(Path)) { | 140 for (TTindex Index : reverse_range(Path)) { |
141 CurIndex = getChildIndex(CurIndex, Index); | 141 CurIndex = getChildIndex(CurIndex, Index); |
142 } | 142 } |
143 assert(CurIndex); // shouldn't be the sentinel node | 143 assert(CurIndex); // shouldn't be the sentinel node |
144 return CurIndex; | 144 return CurIndex; |
145 } | 145 } |
146 | 146 |
147 // Pushes a new marker onto the timer stack. | 147 // Pushes a new marker onto the timer stack. |
148 void TimerStack::push(TimerIdT ID) { | 148 void TimerStack::push(TimerIdT ID) { |
149 if (!BuildDefs::dump()) | 149 if (!BuildDefs::timers()) |
150 return; | 150 return; |
151 constexpr bool UpdateCounts = false; | 151 constexpr bool UpdateCounts = false; |
152 update(UpdateCounts); | 152 update(UpdateCounts); |
153 StackTop = getChildIndex(StackTop, ID); | 153 StackTop = getChildIndex(StackTop, ID); |
154 assert(StackTop); | 154 assert(StackTop); |
155 } | 155 } |
156 | 156 |
157 // Pops the top marker from the timer stack. Validates via assert() that the | 157 // Pops the top marker from the timer stack. Validates via assert() that the |
158 // expected marker is popped. | 158 // expected marker is popped. |
159 void TimerStack::pop(TimerIdT ID) { | 159 void TimerStack::pop(TimerIdT ID) { |
160 if (!BuildDefs::dump()) | 160 if (!BuildDefs::timers()) |
161 return; | 161 return; |
162 constexpr bool UpdateCounts = true; | 162 constexpr bool UpdateCounts = true; |
163 update(UpdateCounts); | 163 update(UpdateCounts); |
164 assert(StackTop); | 164 assert(StackTop); |
165 assert(Nodes[StackTop].Parent < StackTop); | 165 assert(Nodes[StackTop].Parent < StackTop); |
166 // Verify that the expected ID is being popped. | 166 // Verify that the expected ID is being popped. |
167 assert(Nodes[StackTop].Interior == ID); | 167 assert(Nodes[StackTop].Interior == ID); |
168 (void)ID; | 168 (void)ID; |
169 // Verify that the parent's child points to the current stack top. | 169 // Verify that the parent's child points to the current stack top. |
170 assert(Nodes[Nodes[StackTop].Parent].Children[ID] == StackTop); | 170 assert(Nodes[Nodes[StackTop].Parent].Children[ID] == StackTop); |
171 StackTop = Nodes[StackTop].Parent; | 171 StackTop = Nodes[StackTop].Parent; |
172 } | 172 } |
173 | 173 |
174 // At a state change (e.g. push or pop), updates the flat and cumulative | 174 // At a state change (e.g. push or pop), updates the flat and cumulative |
175 // timings for everything on the timer stack. | 175 // timings for everything on the timer stack. |
176 void TimerStack::update(bool UpdateCounts) { | 176 void TimerStack::update(bool UpdateCounts) { |
177 if (!BuildDefs::dump()) | 177 if (!BuildDefs::timers()) |
178 return; | 178 return; |
179 ++StateChangeCount; | 179 ++StateChangeCount; |
180 // Whenever the stack is about to change, we grab the time delta since the | 180 // Whenever the stack is about to change, we grab the time delta since the |
181 // last change and add it to all active cumulative elements and to the flat | 181 // last change and add it to all active cumulative elements and to the flat |
182 // element for the top of the stack. | 182 // element for the top of the stack. |
183 double Current = timestamp(); | 183 double Current = timestamp(); |
184 double Delta = Current - LastTimestamp; | 184 double Delta = Current - LastTimestamp; |
185 if (StackTop) { | 185 if (StackTop) { |
186 TimerIdT Leaf = Nodes[StackTop].Interior; | 186 TimerIdT Leaf = Nodes[StackTop].Interior; |
187 if (Leaf >= LeafTimes.size()) { | 187 if (Leaf >= LeafTimes.size()) { |
(...skipping 15 matching lines...) Expand all Loading... |
203 Prefix = Next; | 203 Prefix = Next; |
204 } | 204 } |
205 // Capture the next timestamp *after* the updates are finished. This | 205 // Capture the next timestamp *after* the updates are finished. This |
206 // minimizes how much the timer can perturb the reported timing. The numbers | 206 // minimizes how much the timer can perturb the reported timing. The numbers |
207 // may not sum to 100%, and the missing amount is indicative of the overhead | 207 // may not sum to 100%, and the missing amount is indicative of the overhead |
208 // of timing. | 208 // of timing. |
209 LastTimestamp = timestamp(); | 209 LastTimestamp = timestamp(); |
210 } | 210 } |
211 | 211 |
212 void TimerStack::reset() { | 212 void TimerStack::reset() { |
213 if (!BuildDefs::dump()) | 213 if (!BuildDefs::timers()) |
214 return; | 214 return; |
215 StateChangeCount = 0; | 215 StateChangeCount = 0; |
216 FirstTimestamp = LastTimestamp = timestamp(); | 216 FirstTimestamp = LastTimestamp = timestamp(); |
217 LeafTimes.assign(LeafTimes.size(), 0); | 217 LeafTimes.assign(LeafTimes.size(), 0); |
218 LeafCounts.assign(LeafCounts.size(), 0); | 218 LeafCounts.assign(LeafCounts.size(), 0); |
219 for (TimerTreeNode &Node : Nodes) { | 219 for (TimerTreeNode &Node : Nodes) { |
220 Node.Time = 0; | 220 Node.Time = 0; |
221 Node.UpdateCount = 0; | 221 Node.UpdateCount = 0; |
222 } | 222 } |
223 } | 223 } |
224 | 224 |
225 namespace { | 225 namespace { |
226 | 226 |
227 using DumpMapType = std::multimap<double, IceString>; | 227 using DumpMapType = std::multimap<double, IceString>; |
228 | 228 |
229 // Dump the Map items in reverse order of their time contribution. | 229 // Dump the Map items in reverse order of their time contribution. |
230 void dumpHelper(Ostream &Str, const DumpMapType &Map, double TotalTime) { | 230 void dumpHelper(Ostream &Str, const DumpMapType &Map, double TotalTime) { |
231 if (!BuildDefs::dump()) | 231 if (!BuildDefs::timers()) |
232 return; | 232 return; |
233 for (auto &I : reverse_range(Map)) { | 233 for (auto &I : reverse_range(Map)) { |
234 char buf[80]; | 234 char buf[80]; |
235 snprintf(buf, llvm::array_lengthof(buf), " %10.6f (%4.1f%%): ", I.first, | 235 snprintf(buf, llvm::array_lengthof(buf), " %10.6f (%4.1f%%): ", I.first, |
236 I.first * 100 / TotalTime); | 236 I.first * 100 / TotalTime); |
237 Str << buf << I.second << "\n"; | 237 Str << buf << I.second << "\n"; |
238 } | 238 } |
239 } | 239 } |
240 | 240 |
241 // Write a printf() format string into Buf[], in the format "[%5lu] ", where | 241 // Write a printf() format string into Buf[], in the format "[%5lu] ", where |
242 // "5" is actually the number of digits in MaxVal. E.g., | 242 // "5" is actually the number of digits in MaxVal. E.g., |
243 // MaxVal=0 ==> "[%1lu] " | 243 // MaxVal=0 ==> "[%1lu] " |
244 // MaxVal=5 ==> "[%1lu] " | 244 // MaxVal=5 ==> "[%1lu] " |
245 // MaxVal=9876 ==> "[%4lu] " | 245 // MaxVal=9876 ==> "[%4lu] " |
246 void makePrintfFormatString(char *Buf, size_t BufLen, size_t MaxVal) { | 246 void makePrintfFormatString(char *Buf, size_t BufLen, size_t MaxVal) { |
247 if (!BuildDefs::dump()) | 247 if (!BuildDefs::timers()) |
248 return; | 248 return; |
249 int NumDigits = 0; | 249 int NumDigits = 0; |
250 do { | 250 do { |
251 ++NumDigits; | 251 ++NumDigits; |
252 MaxVal /= 10; | 252 MaxVal /= 10; |
253 } while (MaxVal); | 253 } while (MaxVal); |
254 snprintf(Buf, BufLen, "[%%%dlu] ", NumDigits); | 254 snprintf(Buf, BufLen, "[%%%dlu] ", NumDigits); |
255 } | 255 } |
256 | 256 |
257 } // end of anonymous namespace | 257 } // end of anonymous namespace |
258 | 258 |
259 void TimerStack::dump(Ostream &Str, bool DumpCumulative) { | 259 void TimerStack::dump(Ostream &Str, bool DumpCumulative) { |
260 if (!BuildDefs::dump()) | 260 if (!BuildDefs::timers()) |
261 return; | 261 return; |
262 constexpr bool UpdateCounts = true; | 262 constexpr bool UpdateCounts = true; |
263 update(UpdateCounts); | 263 update(UpdateCounts); |
264 double TotalTime = LastTimestamp - FirstTimestamp; | 264 double TotalTime = LastTimestamp - FirstTimestamp; |
265 assert(TotalTime); | 265 assert(TotalTime); |
266 char FmtString[30], PrefixStr[30]; | 266 char FmtString[30], PrefixStr[30]; |
267 if (DumpCumulative) { | 267 if (DumpCumulative) { |
268 Str << Name << " - Cumulative times:\n"; | 268 Str << Name << " - Cumulative times:\n"; |
269 size_t MaxInternalCount = 0; | 269 size_t MaxInternalCount = 0; |
270 for (TimerTreeNode &Node : Nodes) | 270 for (TimerTreeNode &Node : Nodes) |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
307 dumpHelper(Str, FlatMap, TotalTime); | 307 dumpHelper(Str, FlatMap, TotalTime); |
308 Str << "Number of timer updates: " << StateChangeCount << "\n"; | 308 Str << "Number of timer updates: " << StateChangeCount << "\n"; |
309 } | 309 } |
310 | 310 |
311 double TimerStack::timestamp() { | 311 double TimerStack::timestamp() { |
312 // TODO: Implement in terms of std::chrono for C++11. | 312 // TODO: Implement in terms of std::chrono for C++11. |
313 return llvm::TimeRecord::getCurrentTime(false).getWallTime(); | 313 return llvm::TimeRecord::getCurrentTime(false).getWallTime(); |
314 } | 314 } |
315 | 315 |
316 } // end of namespace Ice | 316 } // end of namespace Ice |
OLD | NEW |