blob: 818efc30bb4b2e389d98aaca57301f0d88685965 [file] [log] [blame]
Jim Stichnothc4554d72014-09-30 16:49:38 -07001//===- subzero/src/IceTimerTree.cpp - Pass timer defs ---------------------===//
2//
3// The Subzero Code Generator
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Andrew Scull9612d322015-07-06 14:53:25 -07009///
10/// \file
11/// This file defines the TimerTree class, which tracks flat and
12/// cumulative execution time collection of call chains.
13///
Jim Stichnothc4554d72014-09-30 16:49:38 -070014//===----------------------------------------------------------------------===//
15
John Porto67f8de92015-06-25 10:14:17 -070016#include "IceTimerTree.h"
Jim Stichnoth9c234e22014-10-01 09:28:21 -070017
Jim Stichnothc4554d72014-09-30 16:49:38 -070018#include "IceDefs.h"
Jim Stichnoth98da9662015-06-27 06:38:08 -070019
20#pragma clang diagnostic push
21#pragma clang diagnostic ignored "-Wunused-parameter"
John Porto67f8de92015-06-25 10:14:17 -070022#include "llvm/Support/Timer.h"
Jim Stichnoth98da9662015-06-27 06:38:08 -070023#pragma clang diagnostic pop
Jim Stichnothc4554d72014-09-30 16:49:38 -070024
25namespace Ice {
26
Jim Stichnoth8363a062014-10-07 10:02:38 -070027TimerStack::TimerStack(const IceString &Name)
Jim Stichnotheafb56c2015-06-22 10:35:22 -070028 : Name(Name), FirstTimestamp(timestamp()), LastTimestamp(FirstTimestamp) {
Jim Stichnoth20b71f52015-06-24 15:52:24 -070029 if (!BuildDefs::dump())
Jim Stichnoth1c44d812014-12-08 14:57:52 -080030 return;
Jim Stichnoth380d7b92015-01-30 13:10:39 -080031 Nodes.resize(1); // Reserve Nodes[0] for the root node (sentinel).
Jim Stichnoth8363a062014-10-07 10:02:38 -070032 IDs.resize(TT__num);
Jim Stichnoth380d7b92015-01-30 13:10:39 -080033 LeafTimes.resize(TT__num);
34 LeafCounts.resize(TT__num);
Jim Stichnoth8363a062014-10-07 10:02:38 -070035#define STR(s) #s
36#define X(tag) \
37 IDs[TT_##tag] = STR(tag); \
38 IDsIndex[STR(tag)] = TT_##tag;
39 TIMERTREE_TABLE;
40#undef X
41#undef STR
Jim Stichnothc4554d72014-09-30 16:49:38 -070042}
43
44// Returns the unique timer ID for the given Name, creating a new ID
Jim Stichnoth8363a062014-10-07 10:02:38 -070045// if needed.
Jim Stichnothc4554d72014-09-30 16:49:38 -070046TimerIdT TimerStack::getTimerID(const IceString &Name) {
Jim Stichnoth20b71f52015-06-24 15:52:24 -070047 if (!BuildDefs::dump())
Jim Stichnoth1c44d812014-12-08 14:57:52 -080048 return 0;
Jim Stichnoth8363a062014-10-07 10:02:38 -070049 if (IDsIndex.find(Name) == IDsIndex.end()) {
50 IDsIndex[Name] = IDs.size();
51 IDs.push_back(Name);
Jim Stichnoth380d7b92015-01-30 13:10:39 -080052 LeafTimes.push_back(decltype(LeafTimes)::value_type());
53 LeafCounts.push_back(decltype(LeafCounts)::value_type());
Jim Stichnothc4554d72014-09-30 16:49:38 -070054 }
Jim Stichnoth8363a062014-10-07 10:02:38 -070055 return IDsIndex[Name];
Jim Stichnothc4554d72014-09-30 16:49:38 -070056}
57
Jim Stichnoth380d7b92015-01-30 13:10:39 -080058// Creates a mapping from TimerIdT (leaf) values in the Src timer
59// stack into TimerIdT values in this timer stack. Creates new
60// entries in this timer stack as needed.
61TimerStack::TranslationType
62TimerStack::translateIDsFrom(const TimerStack &Src) {
63 size_t Size = Src.IDs.size();
64 TranslationType Mapping(Size);
65 for (TimerIdT i = 0; i < Size; ++i) {
66 Mapping[i] = getTimerID(Src.IDs[i]);
67 }
68 return Mapping;
69}
70
71// Merges two timer stacks, by combining and summing corresponding
72// entries. This timer stack is updated from Src.
73void TimerStack::mergeFrom(const TimerStack &Src) {
Jim Stichnoth20b71f52015-06-24 15:52:24 -070074 if (!BuildDefs::dump())
Jim Stichnoth380d7b92015-01-30 13:10:39 -080075 return;
76 TranslationType Mapping = translateIDsFrom(Src);
77 TTindex SrcIndex = 0;
78 for (const TimerTreeNode &SrcNode : Src.Nodes) {
79 // The first node is reserved as a sentinel, so avoid it.
80 if (SrcIndex > 0) {
81 // Find the full path to the Src node, translated to path
82 // components corresponding to this timer stack.
83 PathType MyPath = Src.getPath(SrcIndex, Mapping);
84 // Find a node in this timer stack corresponding to the given
85 // path, creating new interior nodes as necessary.
86 TTindex MyIndex = findPath(MyPath);
87 Nodes[MyIndex].Time += SrcNode.Time;
88 Nodes[MyIndex].UpdateCount += SrcNode.UpdateCount;
89 }
90 ++SrcIndex;
91 }
92 for (TimerIdT i = 0; i < Src.LeafTimes.size(); ++i) {
93 LeafTimes[Mapping[i]] += Src.LeafTimes[i];
94 LeafCounts[Mapping[i]] += Src.LeafCounts[i];
95 }
96 StateChangeCount += Src.StateChangeCount;
97}
98
99// Constructs a path consisting of the sequence of leaf values leading
100// to a given node, with the Mapping translation applied to the leaf
101// values. The path ends up being in "reverse" order, i.e. from leaf
102// to root.
103TimerStack::PathType TimerStack::getPath(TTindex Index,
104 const TranslationType &Mapping) const {
105 PathType Path;
106 while (Index) {
107 Path.push_back(Mapping[Nodes[Index].Interior]);
108 assert(Nodes[Index].Parent < Index);
109 Index = Nodes[Index].Parent;
110 }
111 return Path;
112}
113
114// Given a parent node and a leaf ID, returns the index of the
115// parent's child ID, creating a new node for the child as necessary.
116TimerStack::TTindex TimerStack::getChildIndex(TimerStack::TTindex Parent,
117 TimerIdT ID) {
118 if (Nodes[Parent].Children.size() <= ID)
119 Nodes[Parent].Children.resize(ID + 1);
120 if (Nodes[Parent].Children[ID] == 0) {
121 TTindex Size = Nodes.size();
122 Nodes[Parent].Children[ID] = Size;
123 Nodes.resize(Size + 1);
124 Nodes[Size].Parent = Parent;
125 Nodes[Size].Interior = ID;
126 }
127 return Nodes[Parent].Children[ID];
128}
129
130// Finds a node in the timer stack corresponding to the given path,
131// creating new interior nodes as necessary.
132TimerStack::TTindex TimerStack::findPath(const PathType &Path) {
133 TTindex CurIndex = 0;
134 // The path is in reverse order (leaf to root), so it needs to be
135 // followed in reverse.
136 for (TTindex Index : reverse_range(Path)) {
137 CurIndex = getChildIndex(CurIndex, Index);
138 }
139 assert(CurIndex); // shouldn't be the sentinel node
140 return CurIndex;
141}
142
Jim Stichnothc4554d72014-09-30 16:49:38 -0700143// Pushes a new marker onto the timer stack.
144void TimerStack::push(TimerIdT ID) {
Jim Stichnoth20b71f52015-06-24 15:52:24 -0700145 if (!BuildDefs::dump())
Jim Stichnoth1c44d812014-12-08 14:57:52 -0800146 return;
Jim Stichnothabce6e52014-10-14 11:09:27 -0700147 const bool UpdateCounts = false;
148 update(UpdateCounts);
Jim Stichnoth380d7b92015-01-30 13:10:39 -0800149 StackTop = getChildIndex(StackTop, ID);
150 assert(StackTop);
Jim Stichnothc4554d72014-09-30 16:49:38 -0700151}
152
Jim Stichnoth380d7b92015-01-30 13:10:39 -0800153// Pops the top marker from the timer stack. Validates via assert()
Jim Stichnothc4554d72014-09-30 16:49:38 -0700154// that the expected marker is popped.
155void TimerStack::pop(TimerIdT ID) {
Jim Stichnoth20b71f52015-06-24 15:52:24 -0700156 if (!BuildDefs::dump())
Jim Stichnoth1c44d812014-12-08 14:57:52 -0800157 return;
Jim Stichnothabce6e52014-10-14 11:09:27 -0700158 const bool UpdateCounts = true;
159 update(UpdateCounts);
Jim Stichnothc4554d72014-09-30 16:49:38 -0700160 assert(StackTop);
161 assert(Nodes[StackTop].Parent < StackTop);
162 // Verify that the expected ID is being popped.
163 assert(Nodes[StackTop].Interior == ID);
164 (void)ID;
165 // Verify that the parent's child points to the current stack top.
166 assert(Nodes[Nodes[StackTop].Parent].Children[ID] == StackTop);
167 StackTop = Nodes[StackTop].Parent;
168}
169
170// At a state change (e.g. push or pop), updates the flat and
171// cumulative timings for everything on the timer stack.
Jim Stichnothabce6e52014-10-14 11:09:27 -0700172void TimerStack::update(bool UpdateCounts) {
Jim Stichnoth20b71f52015-06-24 15:52:24 -0700173 if (!BuildDefs::dump())
Jim Stichnoth1c44d812014-12-08 14:57:52 -0800174 return;
Jim Stichnothc4554d72014-09-30 16:49:38 -0700175 ++StateChangeCount;
176 // Whenever the stack is about to change, we grab the time delta
177 // since the last change and add it to all active cumulative
178 // elements and to the flat element for the top of the stack.
179 double Current = timestamp();
180 double Delta = Current - LastTimestamp;
Jim Stichnothc4554d72014-09-30 16:49:38 -0700181 if (StackTop) {
182 TimerIdT Leaf = Nodes[StackTop].Interior;
Jim Stichnothabce6e52014-10-14 11:09:27 -0700183 if (Leaf >= LeafTimes.size()) {
Jim Stichnothc4554d72014-09-30 16:49:38 -0700184 LeafTimes.resize(Leaf + 1);
Jim Stichnothabce6e52014-10-14 11:09:27 -0700185 LeafCounts.resize(Leaf + 1);
186 }
Jim Stichnothc4554d72014-09-30 16:49:38 -0700187 LeafTimes[Leaf] += Delta;
Jim Stichnothabce6e52014-10-14 11:09:27 -0700188 if (UpdateCounts)
189 ++LeafCounts[Leaf];
Jim Stichnothc4554d72014-09-30 16:49:38 -0700190 }
191 TTindex Prefix = StackTop;
192 while (Prefix) {
193 Nodes[Prefix].Time += Delta;
Jim Stichnothabce6e52014-10-14 11:09:27 -0700194 // Only update a leaf node count, not the internal node counts.
195 if (UpdateCounts && Prefix == StackTop)
196 ++Nodes[Prefix].UpdateCount;
Jim Stichnothc4554d72014-09-30 16:49:38 -0700197 TTindex Next = Nodes[Prefix].Parent;
198 assert(Next < Prefix);
199 Prefix = Next;
200 }
Jim Stichnoth47752552014-10-13 17:15:08 -0700201 // Capture the next timestamp *after* the updates are finished.
202 // This minimizes how much the timer can perturb the reported
203 // timing. The numbers may not sum to 100%, and the missing amount
204 // is indicative of the overhead of timing.
205 LastTimestamp = timestamp();
Jim Stichnothc4554d72014-09-30 16:49:38 -0700206}
207
Jim Stichnothd14b1a02014-10-08 08:28:36 -0700208void TimerStack::reset() {
Jim Stichnoth20b71f52015-06-24 15:52:24 -0700209 if (!BuildDefs::dump())
Jim Stichnoth1c44d812014-12-08 14:57:52 -0800210 return;
Jim Stichnothd14b1a02014-10-08 08:28:36 -0700211 StateChangeCount = 0;
212 FirstTimestamp = LastTimestamp = timestamp();
213 LeafTimes.assign(LeafTimes.size(), 0);
Jim Stichnothabce6e52014-10-14 11:09:27 -0700214 LeafCounts.assign(LeafCounts.size(), 0);
Jim Stichnothd14b1a02014-10-08 08:28:36 -0700215 for (TimerTreeNode &Node : Nodes) {
216 Node.Time = 0;
Jim Stichnothabce6e52014-10-14 11:09:27 -0700217 Node.UpdateCount = 0;
Jim Stichnothd14b1a02014-10-08 08:28:36 -0700218 }
219}
220
Jim Stichnothc4554d72014-09-30 16:49:38 -0700221namespace {
222
223typedef std::multimap<double, IceString> DumpMapType;
224
225// Dump the Map items in reverse order of their time contribution.
226void dumpHelper(Ostream &Str, const DumpMapType &Map, double TotalTime) {
Jim Stichnoth20b71f52015-06-24 15:52:24 -0700227 if (!BuildDefs::dump())
Karl Schimpfb6c96af2014-11-17 10:58:39 -0800228 return;
Jim Stichnoth7e571362015-01-09 11:43:26 -0800229 for (auto &I : reverse_range(Map)) {
Jim Stichnothc4554d72014-09-30 16:49:38 -0700230 char buf[80];
Jim Stichnoth7e571362015-01-09 11:43:26 -0800231 snprintf(buf, llvm::array_lengthof(buf), " %10.6f (%4.1f%%): ", I.first,
232 I.first * 100 / TotalTime);
233 Str << buf << I.second << "\n";
Jim Stichnothc4554d72014-09-30 16:49:38 -0700234 }
235}
236
Jim Stichnothabce6e52014-10-14 11:09:27 -0700237// Write a printf() format string into Buf[], in the format "[%5lu] ",
238// where "5" is actually the number of digits in MaxVal. E.g.,
239// MaxVal=0 ==> "[%1lu] "
240// MaxVal=5 ==> "[%1lu] "
241// MaxVal=9876 ==> "[%4lu] "
242void makePrintfFormatString(char *Buf, size_t BufLen, size_t MaxVal) {
Jim Stichnoth20b71f52015-06-24 15:52:24 -0700243 if (!BuildDefs::dump())
Karl Schimpfb6c96af2014-11-17 10:58:39 -0800244 return;
Jim Stichnothabce6e52014-10-14 11:09:27 -0700245 int NumDigits = 0;
246 do {
247 ++NumDigits;
248 MaxVal /= 10;
249 } while (MaxVal);
250 snprintf(Buf, BufLen, "[%%%dlu] ", NumDigits);
251}
252
Jim Stichnothc4554d72014-09-30 16:49:38 -0700253} // end of anonymous namespace
254
Jim Stichnoth8363a062014-10-07 10:02:38 -0700255void TimerStack::dump(Ostream &Str, bool DumpCumulative) {
Jim Stichnoth20b71f52015-06-24 15:52:24 -0700256 if (!BuildDefs::dump())
Karl Schimpfb6c96af2014-11-17 10:58:39 -0800257 return;
Jim Stichnothabce6e52014-10-14 11:09:27 -0700258 const bool UpdateCounts = true;
259 update(UpdateCounts);
Jim Stichnothc4554d72014-09-30 16:49:38 -0700260 double TotalTime = LastTimestamp - FirstTimestamp;
261 assert(TotalTime);
Jim Stichnothabce6e52014-10-14 11:09:27 -0700262 char FmtString[30], PrefixStr[30];
Jim Stichnoth8363a062014-10-07 10:02:38 -0700263 if (DumpCumulative) {
264 Str << Name << " - Cumulative times:\n";
Jim Stichnothabce6e52014-10-14 11:09:27 -0700265 size_t MaxInternalCount = 0;
266 for (TimerTreeNode &Node : Nodes)
267 MaxInternalCount = std::max(MaxInternalCount, Node.UpdateCount);
268 makePrintfFormatString(FmtString, llvm::array_lengthof(FmtString),
269 MaxInternalCount);
270
Jim Stichnoth8363a062014-10-07 10:02:38 -0700271 DumpMapType CumulativeMap;
272 for (TTindex i = 1; i < Nodes.size(); ++i) {
273 TTindex Prefix = i;
274 IceString Suffix = "";
275 while (Prefix) {
276 if (Suffix.empty())
277 Suffix = IDs[Nodes[Prefix].Interior];
278 else
279 Suffix = IDs[Nodes[Prefix].Interior] + "." + Suffix;
280 assert(Nodes[Prefix].Parent < Prefix);
281 Prefix = Nodes[Prefix].Parent;
282 }
Jim Stichnothabce6e52014-10-14 11:09:27 -0700283 snprintf(PrefixStr, llvm::array_lengthof(PrefixStr), FmtString,
284 Nodes[i].UpdateCount);
285 CumulativeMap.insert(std::make_pair(Nodes[i].Time, PrefixStr + Suffix));
Jim Stichnothc4554d72014-09-30 16:49:38 -0700286 }
Jim Stichnoth8363a062014-10-07 10:02:38 -0700287 dumpHelper(Str, CumulativeMap, TotalTime);
Jim Stichnothc4554d72014-09-30 16:49:38 -0700288 }
Jim Stichnoth8363a062014-10-07 10:02:38 -0700289 Str << Name << " - Flat times:\n";
Jim Stichnothabce6e52014-10-14 11:09:27 -0700290 size_t MaxLeafCount = 0;
291 for (size_t Count : LeafCounts)
292 MaxLeafCount = std::max(MaxLeafCount, Count);
293 makePrintfFormatString(FmtString, llvm::array_lengthof(FmtString),
294 MaxLeafCount);
Jim Stichnothc4554d72014-09-30 16:49:38 -0700295 DumpMapType FlatMap;
296 for (TimerIdT i = 0; i < LeafTimes.size(); ++i) {
Jim Stichnothabce6e52014-10-14 11:09:27 -0700297 if (LeafCounts[i]) {
298 snprintf(PrefixStr, llvm::array_lengthof(PrefixStr), FmtString,
299 LeafCounts[i]);
300 FlatMap.insert(std::make_pair(LeafTimes[i], PrefixStr + IDs[i]));
301 }
Jim Stichnothc4554d72014-09-30 16:49:38 -0700302 }
303 dumpHelper(Str, FlatMap, TotalTime);
304 Str << "Number of timer updates: " << StateChangeCount << "\n";
305}
306
Jim Stichnoth9c234e22014-10-01 09:28:21 -0700307double TimerStack::timestamp() {
308 // TODO: Implement in terms of std::chrono for C++11.
309 return llvm::TimeRecord::getCurrentTime(false).getWallTime();
310}
311
Jim Stichnothc4554d72014-09-30 16:49:38 -0700312} // end of namespace Ice