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 // This file defines the TimerTree class, which tracks flat and | 10 // This file defines the TimerTree class, which tracks flat and |
(...skipping 27 matching lines...) Expand all Loading... | |
38 TimerIdT TimerStack::getTimerID(const IceString &Name) { | 38 TimerIdT TimerStack::getTimerID(const IceString &Name) { |
39 if (IDsIndex.find(Name) == IDsIndex.end()) { | 39 if (IDsIndex.find(Name) == IDsIndex.end()) { |
40 IDsIndex[Name] = IDs.size(); | 40 IDsIndex[Name] = IDs.size(); |
41 IDs.push_back(Name); | 41 IDs.push_back(Name); |
42 } | 42 } |
43 return IDsIndex[Name]; | 43 return IDsIndex[Name]; |
44 } | 44 } |
45 | 45 |
46 // Pushes a new marker onto the timer stack. | 46 // Pushes a new marker onto the timer stack. |
47 void TimerStack::push(TimerIdT ID) { | 47 void TimerStack::push(TimerIdT ID) { |
48 update(); | 48 const bool UpdateCounts = false; |
49 update(UpdateCounts); | |
49 if (Nodes[StackTop].Children.size() <= ID) | 50 if (Nodes[StackTop].Children.size() <= ID) |
50 Nodes[StackTop].Children.resize(ID + 1); | 51 Nodes[StackTop].Children.resize(ID + 1); |
51 if (Nodes[StackTop].Children[ID] == 0) { | 52 if (Nodes[StackTop].Children[ID] == 0) { |
52 TTindex Size = Nodes.size(); | 53 TTindex Size = Nodes.size(); |
53 Nodes[StackTop].Children[ID] = Size; | 54 Nodes[StackTop].Children[ID] = Size; |
54 Nodes.resize(Size + 1); | 55 Nodes.resize(Size + 1); |
55 Nodes[Size].Parent = StackTop; | 56 Nodes[Size].Parent = StackTop; |
56 Nodes[Size].Interior = ID; | 57 Nodes[Size].Interior = ID; |
57 } | 58 } |
58 StackTop = Nodes[StackTop].Children[ID]; | 59 StackTop = Nodes[StackTop].Children[ID]; |
59 } | 60 } |
60 | 61 |
61 // Pop the top marker from the timer stack. Validates via assert() | 62 // Pop the top marker from the timer stack. Validates via assert() |
62 // that the expected marker is popped. | 63 // that the expected marker is popped. |
63 void TimerStack::pop(TimerIdT ID) { | 64 void TimerStack::pop(TimerIdT ID) { |
64 update(); | 65 const bool UpdateCounts = true; |
66 update(UpdateCounts); | |
65 assert(StackTop); | 67 assert(StackTop); |
66 assert(Nodes[StackTop].Parent < StackTop); | 68 assert(Nodes[StackTop].Parent < StackTop); |
67 // Verify that the expected ID is being popped. | 69 // Verify that the expected ID is being popped. |
68 assert(Nodes[StackTop].Interior == ID); | 70 assert(Nodes[StackTop].Interior == ID); |
69 (void)ID; | 71 (void)ID; |
70 // Verify that the parent's child points to the current stack top. | 72 // Verify that the parent's child points to the current stack top. |
71 assert(Nodes[Nodes[StackTop].Parent].Children[ID] == StackTop); | 73 assert(Nodes[Nodes[StackTop].Parent].Children[ID] == StackTop); |
72 StackTop = Nodes[StackTop].Parent; | 74 StackTop = Nodes[StackTop].Parent; |
73 } | 75 } |
74 | 76 |
75 // At a state change (e.g. push or pop), updates the flat and | 77 // At a state change (e.g. push or pop), updates the flat and |
76 // cumulative timings for everything on the timer stack. | 78 // cumulative timings for everything on the timer stack. |
77 void TimerStack::update() { | 79 void TimerStack::update(bool UpdateCounts) { |
78 ++StateChangeCount; | 80 ++StateChangeCount; |
79 // Whenever the stack is about to change, we grab the time delta | 81 // Whenever the stack is about to change, we grab the time delta |
80 // since the last change and add it to all active cumulative | 82 // since the last change and add it to all active cumulative |
81 // elements and to the flat element for the top of the stack. | 83 // elements and to the flat element for the top of the stack. |
82 double Current = timestamp(); | 84 double Current = timestamp(); |
83 double Delta = Current - LastTimestamp; | 85 double Delta = Current - LastTimestamp; |
84 if (StackTop) { | 86 if (StackTop) { |
85 TimerIdT Leaf = Nodes[StackTop].Interior; | 87 TimerIdT Leaf = Nodes[StackTop].Interior; |
86 if (Leaf >= LeafTimes.size()) | 88 if (Leaf >= LeafTimes.size()) { |
87 LeafTimes.resize(Leaf + 1); | 89 LeafTimes.resize(Leaf + 1); |
90 LeafCounts.resize(Leaf + 1); | |
91 } | |
88 LeafTimes[Leaf] += Delta; | 92 LeafTimes[Leaf] += Delta; |
93 if (UpdateCounts) | |
94 ++LeafCounts[Leaf]; | |
89 } | 95 } |
90 TTindex Prefix = StackTop; | 96 TTindex Prefix = StackTop; |
91 while (Prefix) { | 97 while (Prefix) { |
92 Nodes[Prefix].Time += Delta; | 98 Nodes[Prefix].Time += Delta; |
99 // Only update a leaf node count, not the internal node counts. | |
100 if (UpdateCounts && Prefix == StackTop) | |
101 ++Nodes[Prefix].UpdateCount; | |
93 TTindex Next = Nodes[Prefix].Parent; | 102 TTindex Next = Nodes[Prefix].Parent; |
94 assert(Next < Prefix); | 103 assert(Next < Prefix); |
95 Prefix = Next; | 104 Prefix = Next; |
96 } | 105 } |
97 // Capture the next timestamp *after* the updates are finished. | 106 // Capture the next timestamp *after* the updates are finished. |
98 // This minimizes how much the timer can perturb the reported | 107 // This minimizes how much the timer can perturb the reported |
99 // timing. The numbers may not sum to 100%, and the missing amount | 108 // timing. The numbers may not sum to 100%, and the missing amount |
100 // is indicative of the overhead of timing. | 109 // is indicative of the overhead of timing. |
101 LastTimestamp = timestamp(); | 110 LastTimestamp = timestamp(); |
102 } | 111 } |
103 | 112 |
104 void TimerStack::reset() { | 113 void TimerStack::reset() { |
105 StateChangeCount = 0; | 114 StateChangeCount = 0; |
106 FirstTimestamp = LastTimestamp = timestamp(); | 115 FirstTimestamp = LastTimestamp = timestamp(); |
107 LeafTimes.assign(LeafTimes.size(), 0); | 116 LeafTimes.assign(LeafTimes.size(), 0); |
117 LeafCounts.assign(LeafCounts.size(), 0); | |
108 for (TimerTreeNode &Node : Nodes) { | 118 for (TimerTreeNode &Node : Nodes) { |
109 Node.Time = 0; | 119 Node.Time = 0; |
jvoung (off chromium)
2014/10/14 17:25:50
Node.UpdateCount = 0 also?
Jim Stichnoth
2014/10/14 18:10:09
Oops, done.
| |
110 } | 120 } |
111 } | 121 } |
112 | 122 |
113 namespace { | 123 namespace { |
114 | 124 |
115 typedef std::multimap<double, IceString> DumpMapType; | 125 typedef std::multimap<double, IceString> DumpMapType; |
116 | 126 |
117 // Dump the Map items in reverse order of their time contribution. | 127 // Dump the Map items in reverse order of their time contribution. |
118 void dumpHelper(Ostream &Str, const DumpMapType &Map, double TotalTime) { | 128 void dumpHelper(Ostream &Str, const DumpMapType &Map, double TotalTime) { |
119 // TODO(stichnot): Use llvm::make_range with LLVM 3.5. | 129 // TODO(stichnot): Use llvm::make_range with LLVM 3.5. |
120 for (auto I = Map.rbegin(), E = Map.rend(); I != E; ++I) { | 130 for (auto I = Map.rbegin(), E = Map.rend(); I != E; ++I) { |
121 char buf[80]; | 131 char buf[80]; |
122 snprintf(buf, llvm::array_lengthof(buf), " %10.6f (%4.1f%%): ", I->first, | 132 snprintf(buf, llvm::array_lengthof(buf), " %10.6f (%4.1f%%): ", I->first, |
123 I->first * 100 / TotalTime); | 133 I->first * 100 / TotalTime); |
124 Str << buf << I->second << "\n"; | 134 Str << buf << I->second << "\n"; |
125 } | 135 } |
126 } | 136 } |
127 | 137 |
138 // Write a printf() format string into Buf[], in the format "[%5lu] ", | |
139 // where "5" is actually the number of digits in MaxVal. E.g., | |
140 // MaxVal=0 ==> "[%1lu] " | |
141 // MaxVal=5 ==> "[%1lu] " | |
142 // MaxVal=9876 ==> "[%4lu] " | |
143 void makePrintfFormatString(char *Buf, size_t BufLen, size_t MaxVal) { | |
144 int NumDigits = 0; | |
145 do { | |
146 ++NumDigits; | |
147 MaxVal /= 10; | |
148 } while (MaxVal); | |
149 snprintf(Buf, BufLen, "[%%%dlu] ", NumDigits); | |
150 } | |
151 | |
128 } // end of anonymous namespace | 152 } // end of anonymous namespace |
129 | 153 |
130 void TimerStack::dump(Ostream &Str, bool DumpCumulative) { | 154 void TimerStack::dump(Ostream &Str, bool DumpCumulative) { |
131 update(); | 155 const bool UpdateCounts = true; |
156 update(UpdateCounts); | |
132 double TotalTime = LastTimestamp - FirstTimestamp; | 157 double TotalTime = LastTimestamp - FirstTimestamp; |
133 assert(TotalTime); | 158 assert(TotalTime); |
159 char FmtString[30], PrefixStr[30]; | |
134 if (DumpCumulative) { | 160 if (DumpCumulative) { |
135 Str << Name << " - Cumulative times:\n"; | 161 Str << Name << " - Cumulative times:\n"; |
162 size_t MaxInternalCount = 0; | |
163 for (TimerTreeNode &Node : Nodes) | |
164 MaxInternalCount = std::max(MaxInternalCount, Node.UpdateCount); | |
165 makePrintfFormatString(FmtString, llvm::array_lengthof(FmtString), | |
166 MaxInternalCount); | |
167 | |
136 DumpMapType CumulativeMap; | 168 DumpMapType CumulativeMap; |
137 for (TTindex i = 1; i < Nodes.size(); ++i) { | 169 for (TTindex i = 1; i < Nodes.size(); ++i) { |
138 TTindex Prefix = i; | 170 TTindex Prefix = i; |
139 IceString Suffix = ""; | 171 IceString Suffix = ""; |
140 while (Prefix) { | 172 while (Prefix) { |
141 if (Suffix.empty()) | 173 if (Suffix.empty()) |
142 Suffix = IDs[Nodes[Prefix].Interior]; | 174 Suffix = IDs[Nodes[Prefix].Interior]; |
143 else | 175 else |
144 Suffix = IDs[Nodes[Prefix].Interior] + "." + Suffix; | 176 Suffix = IDs[Nodes[Prefix].Interior] + "." + Suffix; |
145 assert(Nodes[Prefix].Parent < Prefix); | 177 assert(Nodes[Prefix].Parent < Prefix); |
146 Prefix = Nodes[Prefix].Parent; | 178 Prefix = Nodes[Prefix].Parent; |
147 } | 179 } |
148 CumulativeMap.insert(std::make_pair(Nodes[i].Time, Suffix)); | 180 snprintf(PrefixStr, llvm::array_lengthof(PrefixStr), FmtString, |
181 Nodes[i].UpdateCount); | |
182 CumulativeMap.insert(std::make_pair(Nodes[i].Time, PrefixStr + Suffix)); | |
149 } | 183 } |
150 dumpHelper(Str, CumulativeMap, TotalTime); | 184 dumpHelper(Str, CumulativeMap, TotalTime); |
151 } | 185 } |
152 Str << Name << " - Flat times:\n"; | 186 Str << Name << " - Flat times:\n"; |
187 size_t MaxLeafCount = 0; | |
188 for (size_t Count : LeafCounts) | |
189 MaxLeafCount = std::max(MaxLeafCount, Count); | |
190 makePrintfFormatString(FmtString, llvm::array_lengthof(FmtString), | |
191 MaxLeafCount); | |
153 DumpMapType FlatMap; | 192 DumpMapType FlatMap; |
154 for (TimerIdT i = 0; i < LeafTimes.size(); ++i) { | 193 for (TimerIdT i = 0; i < LeafTimes.size(); ++i) { |
155 FlatMap.insert(std::make_pair(LeafTimes[i], IDs[i])); | 194 if (LeafCounts[i]) { |
195 snprintf(PrefixStr, llvm::array_lengthof(PrefixStr), FmtString, | |
196 LeafCounts[i]); | |
197 FlatMap.insert(std::make_pair(LeafTimes[i], PrefixStr + IDs[i])); | |
198 } | |
156 } | 199 } |
157 dumpHelper(Str, FlatMap, TotalTime); | 200 dumpHelper(Str, FlatMap, TotalTime); |
158 Str << "Number of timer updates: " << StateChangeCount << "\n"; | 201 Str << "Number of timer updates: " << StateChangeCount << "\n"; |
159 } | 202 } |
160 | 203 |
161 double TimerStack::timestamp() { | 204 double TimerStack::timestamp() { |
162 // TODO: Implement in terms of std::chrono for C++11. | 205 // TODO: Implement in terms of std::chrono for C++11. |
163 return llvm::TimeRecord::getCurrentTime(false).getWallTime(); | 206 return llvm::TimeRecord::getCurrentTime(false).getWallTime(); |
164 } | 207 } |
165 | 208 |
166 } // end of namespace Ice | 209 } // end of namespace Ice |
OLD | NEW |