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; |
| 120 Node.UpdateCount = 0; |
110 } | 121 } |
111 } | 122 } |
112 | 123 |
113 namespace { | 124 namespace { |
114 | 125 |
115 typedef std::multimap<double, IceString> DumpMapType; | 126 typedef std::multimap<double, IceString> DumpMapType; |
116 | 127 |
117 // Dump the Map items in reverse order of their time contribution. | 128 // Dump the Map items in reverse order of their time contribution. |
118 void dumpHelper(Ostream &Str, const DumpMapType &Map, double TotalTime) { | 129 void dumpHelper(Ostream &Str, const DumpMapType &Map, double TotalTime) { |
119 // TODO(stichnot): Use llvm::make_range with LLVM 3.5. | 130 // TODO(stichnot): Use llvm::make_range with LLVM 3.5. |
120 for (auto I = Map.rbegin(), E = Map.rend(); I != E; ++I) { | 131 for (auto I = Map.rbegin(), E = Map.rend(); I != E; ++I) { |
121 char buf[80]; | 132 char buf[80]; |
122 snprintf(buf, llvm::array_lengthof(buf), " %10.6f (%4.1f%%): ", I->first, | 133 snprintf(buf, llvm::array_lengthof(buf), " %10.6f (%4.1f%%): ", I->first, |
123 I->first * 100 / TotalTime); | 134 I->first * 100 / TotalTime); |
124 Str << buf << I->second << "\n"; | 135 Str << buf << I->second << "\n"; |
125 } | 136 } |
126 } | 137 } |
127 | 138 |
| 139 // Write a printf() format string into Buf[], in the format "[%5lu] ", |
| 140 // where "5" is actually the number of digits in MaxVal. E.g., |
| 141 // MaxVal=0 ==> "[%1lu] " |
| 142 // MaxVal=5 ==> "[%1lu] " |
| 143 // MaxVal=9876 ==> "[%4lu] " |
| 144 void makePrintfFormatString(char *Buf, size_t BufLen, size_t MaxVal) { |
| 145 int NumDigits = 0; |
| 146 do { |
| 147 ++NumDigits; |
| 148 MaxVal /= 10; |
| 149 } while (MaxVal); |
| 150 snprintf(Buf, BufLen, "[%%%dlu] ", NumDigits); |
| 151 } |
| 152 |
128 } // end of anonymous namespace | 153 } // end of anonymous namespace |
129 | 154 |
130 void TimerStack::dump(Ostream &Str, bool DumpCumulative) { | 155 void TimerStack::dump(Ostream &Str, bool DumpCumulative) { |
131 update(); | 156 const bool UpdateCounts = true; |
| 157 update(UpdateCounts); |
132 double TotalTime = LastTimestamp - FirstTimestamp; | 158 double TotalTime = LastTimestamp - FirstTimestamp; |
133 assert(TotalTime); | 159 assert(TotalTime); |
| 160 char FmtString[30], PrefixStr[30]; |
134 if (DumpCumulative) { | 161 if (DumpCumulative) { |
135 Str << Name << " - Cumulative times:\n"; | 162 Str << Name << " - Cumulative times:\n"; |
| 163 size_t MaxInternalCount = 0; |
| 164 for (TimerTreeNode &Node : Nodes) |
| 165 MaxInternalCount = std::max(MaxInternalCount, Node.UpdateCount); |
| 166 makePrintfFormatString(FmtString, llvm::array_lengthof(FmtString), |
| 167 MaxInternalCount); |
| 168 |
136 DumpMapType CumulativeMap; | 169 DumpMapType CumulativeMap; |
137 for (TTindex i = 1; i < Nodes.size(); ++i) { | 170 for (TTindex i = 1; i < Nodes.size(); ++i) { |
138 TTindex Prefix = i; | 171 TTindex Prefix = i; |
139 IceString Suffix = ""; | 172 IceString Suffix = ""; |
140 while (Prefix) { | 173 while (Prefix) { |
141 if (Suffix.empty()) | 174 if (Suffix.empty()) |
142 Suffix = IDs[Nodes[Prefix].Interior]; | 175 Suffix = IDs[Nodes[Prefix].Interior]; |
143 else | 176 else |
144 Suffix = IDs[Nodes[Prefix].Interior] + "." + Suffix; | 177 Suffix = IDs[Nodes[Prefix].Interior] + "." + Suffix; |
145 assert(Nodes[Prefix].Parent < Prefix); | 178 assert(Nodes[Prefix].Parent < Prefix); |
146 Prefix = Nodes[Prefix].Parent; | 179 Prefix = Nodes[Prefix].Parent; |
147 } | 180 } |
148 CumulativeMap.insert(std::make_pair(Nodes[i].Time, Suffix)); | 181 snprintf(PrefixStr, llvm::array_lengthof(PrefixStr), FmtString, |
| 182 Nodes[i].UpdateCount); |
| 183 CumulativeMap.insert(std::make_pair(Nodes[i].Time, PrefixStr + Suffix)); |
149 } | 184 } |
150 dumpHelper(Str, CumulativeMap, TotalTime); | 185 dumpHelper(Str, CumulativeMap, TotalTime); |
151 } | 186 } |
152 Str << Name << " - Flat times:\n"; | 187 Str << Name << " - Flat times:\n"; |
| 188 size_t MaxLeafCount = 0; |
| 189 for (size_t Count : LeafCounts) |
| 190 MaxLeafCount = std::max(MaxLeafCount, Count); |
| 191 makePrintfFormatString(FmtString, llvm::array_lengthof(FmtString), |
| 192 MaxLeafCount); |
153 DumpMapType FlatMap; | 193 DumpMapType FlatMap; |
154 for (TimerIdT i = 0; i < LeafTimes.size(); ++i) { | 194 for (TimerIdT i = 0; i < LeafTimes.size(); ++i) { |
155 FlatMap.insert(std::make_pair(LeafTimes[i], IDs[i])); | 195 if (LeafCounts[i]) { |
| 196 snprintf(PrefixStr, llvm::array_lengthof(PrefixStr), FmtString, |
| 197 LeafCounts[i]); |
| 198 FlatMap.insert(std::make_pair(LeafTimes[i], PrefixStr + IDs[i])); |
| 199 } |
156 } | 200 } |
157 dumpHelper(Str, FlatMap, TotalTime); | 201 dumpHelper(Str, FlatMap, TotalTime); |
158 Str << "Number of timer updates: " << StateChangeCount << "\n"; | 202 Str << "Number of timer updates: " << StateChangeCount << "\n"; |
159 } | 203 } |
160 | 204 |
161 double TimerStack::timestamp() { | 205 double TimerStack::timestamp() { |
162 // TODO: Implement in terms of std::chrono for C++11. | 206 // TODO: Implement in terms of std::chrono for C++11. |
163 return llvm::TimeRecord::getCurrentTime(false).getWallTime(); | 207 return llvm::TimeRecord::getCurrentTime(false).getWallTime(); |
164 } | 208 } |
165 | 209 |
166 } // end of namespace Ice | 210 } // end of namespace Ice |
OLD | NEW |