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 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
220 for (TimerTreeNode &Node : Nodes) { | 220 for (TimerTreeNode &Node : Nodes) { |
221 Node.Time = 0; | 221 Node.Time = 0; |
222 Node.UpdateCount = 0; | 222 Node.UpdateCount = 0; |
223 } | 223 } |
224 } | 224 } |
225 | 225 |
226 namespace { | 226 namespace { |
227 | 227 |
228 using DumpMapType = std::multimap<double, std::string>; | 228 using DumpMapType = std::multimap<double, std::string>; |
229 | 229 |
230 // Dump the Map items in reverse order of their time contribution. | 230 // Dump the Map items in reverse order of their time contribution. If |
231 void dumpHelper(Ostream &Str, const DumpMapType &Map, double TotalTime) { | 231 // AddPercents is true (i.e. for printing "flat times"), it also prints a |
| 232 // cumulative percentage column, and recalculates TotalTime as the sum of all |
| 233 // the individual times so that cumulative percentage adds up to 100%. |
| 234 void dumpHelper(Ostream &Str, const DumpMapType &Map, double TotalTime, |
| 235 bool AddPercents) { |
232 if (!BuildDefs::timers()) | 236 if (!BuildDefs::timers()) |
233 return; | 237 return; |
234 for (auto &I : reverse_range(Map)) { | 238 if (AddPercents) { |
235 Str << llvm::format(" %10.6f (%4.1f%%): ", I.first, | 239 // Recalculate TotalTime as the sum of the individual times. This is |
236 I.first * 100 / TotalTime) << I.second << "\n"; | 240 // because the individual times generally add up to less than 100% because |
| 241 // of timer overhead. |
| 242 TotalTime = 0; |
| 243 for (const auto &I : Map) { |
| 244 TotalTime += I.first; |
| 245 } |
| 246 } |
| 247 double Sum = 0; |
| 248 for (const auto &I : reverse_range(Map)) { |
| 249 Sum += I.first; |
| 250 if (AddPercents) { |
| 251 Str << llvm::format(" %10.6f %4.1f%% %5.1f%% ", I.first, |
| 252 I.first * 100 / TotalTime, Sum * 100 / TotalTime) |
| 253 << I.second << "\n"; |
| 254 } else { |
| 255 Str << llvm::format(" %10.6f %4.1f%% ", I.first, |
| 256 I.first * 100 / TotalTime) << I.second << "\n"; |
| 257 } |
237 } | 258 } |
238 } | 259 } |
239 | 260 |
240 // Write a printf() format string into Buf[], in the format "[%5lu] ", where | |
241 // "5" is actually the number of digits in MaxVal. E.g., | |
242 // MaxVal=0 ==> "[%1lu] " | |
243 // MaxVal=5 ==> "[%1lu] " | |
244 // MaxVal=9876 ==> "[%4lu] " | |
245 void makePrintfFormatString(char *Buf, size_t BufLen, size_t MaxVal) { | |
246 if (!BuildDefs::timers()) | |
247 return; | |
248 int NumDigits = 0; | |
249 do { | |
250 ++NumDigits; | |
251 MaxVal /= 10; | |
252 } while (MaxVal); | |
253 snprintf(Buf, BufLen, "[%%%dlu] ", NumDigits); | |
254 } | |
255 | |
256 } // end of anonymous namespace | 261 } // end of anonymous namespace |
257 | 262 |
258 void TimerStack::dump(Ostream &Str, bool DumpCumulative) { | 263 void TimerStack::dump(Ostream &Str, bool DumpCumulative) { |
259 if (!BuildDefs::timers()) | 264 if (!BuildDefs::timers()) |
260 return; | 265 return; |
261 constexpr bool UpdateCounts = true; | 266 constexpr bool UpdateCounts = true; |
262 update(UpdateCounts); | 267 update(UpdateCounts); |
263 double TotalTime = LastTimestamp - FirstTimestamp; | 268 double TotalTime = LastTimestamp - FirstTimestamp; |
264 assert(TotalTime); | 269 assert(TotalTime); |
265 char FmtString[30], PrefixStr[30]; | 270 char PrefixStr[30]; |
266 if (DumpCumulative) { | 271 if (DumpCumulative) { |
267 Str << Name << " - Cumulative times:\n"; | 272 Str << Name << " - Cumulative times:\n" |
268 size_t MaxInternalCount = 0; | 273 " Seconds Pct EventCnt TimerPath\n"; |
269 for (TimerTreeNode &Node : Nodes) | |
270 MaxInternalCount = std::max(MaxInternalCount, Node.UpdateCount); | |
271 makePrintfFormatString(FmtString, llvm::array_lengthof(FmtString), | |
272 MaxInternalCount); | |
273 | |
274 DumpMapType CumulativeMap; | 274 DumpMapType CumulativeMap; |
275 for (TTindex i = 1; i < Nodes.size(); ++i) { | 275 for (TTindex i = 1; i < Nodes.size(); ++i) { |
276 TTindex Prefix = i; | 276 TTindex Prefix = i; |
277 std::string Suffix = ""; | 277 std::string Suffix = ""; |
278 while (Prefix) { | 278 while (Prefix) { |
279 if (Suffix.empty()) | 279 if (Suffix.empty()) |
280 Suffix = IDs[Nodes[Prefix].Interior]; | 280 Suffix = IDs[Nodes[Prefix].Interior]; |
281 else | 281 else |
282 Suffix = IDs[Nodes[Prefix].Interior] + "." + Suffix; | 282 Suffix = IDs[Nodes[Prefix].Interior] + "." + Suffix; |
283 assert(Nodes[Prefix].Parent < Prefix); | 283 assert(Nodes[Prefix].Parent < Prefix); |
284 Prefix = Nodes[Prefix].Parent; | 284 Prefix = Nodes[Prefix].Parent; |
285 } | 285 } |
286 snprintf(PrefixStr, llvm::array_lengthof(PrefixStr), FmtString, | 286 snprintf(PrefixStr, llvm::array_lengthof(PrefixStr), "%9zu ", |
287 Nodes[i].UpdateCount); | 287 Nodes[i].UpdateCount); |
288 CumulativeMap.insert(std::make_pair(Nodes[i].Time, PrefixStr + Suffix)); | 288 CumulativeMap.insert(std::make_pair(Nodes[i].Time, PrefixStr + Suffix)); |
289 } | 289 } |
290 dumpHelper(Str, CumulativeMap, TotalTime); | 290 constexpr bool NoAddPercents = false; |
| 291 dumpHelper(Str, CumulativeMap, TotalTime, NoAddPercents); |
291 } | 292 } |
292 Str << Name << " - Flat times:\n"; | 293 Str << Name << " - Flat times:\n" |
293 size_t MaxLeafCount = 0; | 294 " Seconds Pct CumPct EventCnt TimerName\n"; |
294 for (size_t Count : LeafCounts) | |
295 MaxLeafCount = std::max(MaxLeafCount, Count); | |
296 makePrintfFormatString(FmtString, llvm::array_lengthof(FmtString), | |
297 MaxLeafCount); | |
298 DumpMapType FlatMap; | 295 DumpMapType FlatMap; |
299 for (TimerIdT i = 0; i < LeafTimes.size(); ++i) { | 296 for (TimerIdT i = 0; i < LeafTimes.size(); ++i) { |
300 if (LeafCounts[i]) { | 297 if (LeafCounts[i]) { |
301 snprintf(PrefixStr, llvm::array_lengthof(PrefixStr), FmtString, | 298 snprintf(PrefixStr, llvm::array_lengthof(PrefixStr), "%9zu ", |
302 LeafCounts[i]); | 299 LeafCounts[i]); |
303 FlatMap.insert(std::make_pair(LeafTimes[i], PrefixStr + IDs[i])); | 300 FlatMap.insert(std::make_pair(LeafTimes[i], PrefixStr + IDs[i])); |
304 } | 301 } |
305 } | 302 } |
306 dumpHelper(Str, FlatMap, TotalTime); | 303 constexpr bool AddPercents = true; |
| 304 dumpHelper(Str, FlatMap, TotalTime, AddPercents); |
307 Str << "Number of timer updates: " << StateChangeCount << "\n"; | 305 Str << "Number of timer updates: " << StateChangeCount << "\n"; |
308 } | 306 } |
309 | 307 |
310 double TimerStack::timestamp() { | 308 double TimerStack::timestamp() { |
311 // TODO: Implement in terms of std::chrono for C++11. | 309 // TODO: Implement in terms of std::chrono for C++11. |
312 return llvm::TimeRecord::getCurrentTime(false).getWallTime(); | 310 return llvm::TimeRecord::getCurrentTime(false).getWallTime(); |
313 } | 311 } |
314 | 312 |
315 } // end of namespace Ice | 313 } // end of namespace Ice |
OLD | NEW |