Andrea Di Biagio | 3562248 | 2018-03-22 10:19:20 +0000 | [diff] [blame] | 1 | //===--------------------- TimelineView.cpp ---------------------*- C++ -*-===// |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | /// \brief |
| 10 | /// |
| 11 | /// This file implements the TimelineView interface. |
| 12 | /// |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Matt Davis | 10aa09f | 2018-08-24 20:24:53 +0000 | [diff] [blame^] | 15 | #include "Views/TimelineView.h" |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 16 | |
| 17 | using namespace llvm; |
| 18 | |
| 19 | namespace mca { |
| 20 | |
| 21 | void TimelineView::initialize(unsigned MaxIterations) { |
| 22 | unsigned NumInstructions = |
| 23 | AsmSequence.getNumIterations() * AsmSequence.size(); |
| 24 | if (!MaxIterations) |
| 25 | MaxIterations = DEFAULT_ITERATIONS; |
| 26 | unsigned NumEntries = |
| 27 | std::min(NumInstructions, MaxIterations * AsmSequence.size()); |
| 28 | Timeline.resize(NumEntries); |
| 29 | TimelineViewEntry NullTVEntry = {0, 0, 0, 0, 0}; |
| 30 | std::fill(Timeline.begin(), Timeline.end(), NullTVEntry); |
| 31 | |
| 32 | WaitTime.resize(AsmSequence.size()); |
| 33 | WaitTimeEntry NullWTEntry = {0, 0, 0, 0}; |
| 34 | std::fill(WaitTime.begin(), WaitTime.end(), NullWTEntry); |
| 35 | } |
| 36 | |
Matt Davis | 0906a7f | 2018-07-12 16:56:17 +0000 | [diff] [blame] | 37 | void TimelineView::onEvent(const HWInstructionEvent &Event) { |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 38 | const unsigned Index = Event.IR.getSourceIndex(); |
| 39 | if (CurrentCycle >= MaxCycle || Index >= Timeline.size()) |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 40 | return; |
Clement Courbet | 844f22d | 2018-03-13 13:11:01 +0000 | [diff] [blame] | 41 | switch (Event.Type) { |
| 42 | case HWInstructionEvent::Retired: { |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 43 | TimelineViewEntry &TVEntry = Timeline[Index]; |
Clement Courbet | 844f22d | 2018-03-13 13:11:01 +0000 | [diff] [blame] | 44 | TVEntry.CycleRetired = CurrentCycle; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 45 | |
Clement Courbet | 844f22d | 2018-03-13 13:11:01 +0000 | [diff] [blame] | 46 | // Update the WaitTime entry which corresponds to this Index. |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 47 | WaitTimeEntry &WTEntry = WaitTime[Index % AsmSequence.size()]; |
Clement Courbet | 844f22d | 2018-03-13 13:11:01 +0000 | [diff] [blame] | 48 | WTEntry.Executions++; |
| 49 | WTEntry.CyclesSpentInSchedulerQueue += |
| 50 | TVEntry.CycleIssued - TVEntry.CycleDispatched; |
| 51 | assert(TVEntry.CycleDispatched <= TVEntry.CycleReady); |
| 52 | WTEntry.CyclesSpentInSQWhileReady += |
| 53 | TVEntry.CycleIssued - TVEntry.CycleReady; |
| 54 | WTEntry.CyclesSpentAfterWBAndBeforeRetire += |
| 55 | (TVEntry.CycleRetired - 1) - TVEntry.CycleExecuted; |
| 56 | break; |
| 57 | } |
| 58 | case HWInstructionEvent::Ready: |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 59 | Timeline[Index].CycleReady = CurrentCycle; |
Clement Courbet | 844f22d | 2018-03-13 13:11:01 +0000 | [diff] [blame] | 60 | break; |
| 61 | case HWInstructionEvent::Issued: |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 62 | Timeline[Index].CycleIssued = CurrentCycle; |
Clement Courbet | 844f22d | 2018-03-13 13:11:01 +0000 | [diff] [blame] | 63 | break; |
| 64 | case HWInstructionEvent::Executed: |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 65 | Timeline[Index].CycleExecuted = CurrentCycle; |
Clement Courbet | 844f22d | 2018-03-13 13:11:01 +0000 | [diff] [blame] | 66 | break; |
| 67 | case HWInstructionEvent::Dispatched: |
Matt Davis | 21a8d32 | 2018-05-07 18:29:15 +0000 | [diff] [blame] | 68 | Timeline[Index].CycleDispatched = CurrentCycle; |
Clement Courbet | 844f22d | 2018-03-13 13:11:01 +0000 | [diff] [blame] | 69 | break; |
| 70 | default: |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 71 | return; |
Clement Courbet | 844f22d | 2018-03-13 13:11:01 +0000 | [diff] [blame] | 72 | } |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 73 | LastCycle = std::max(LastCycle, CurrentCycle); |
| 74 | } |
| 75 | |
Andrea Di Biagio | 039349a | 2018-05-15 18:11:45 +0000 | [diff] [blame] | 76 | void TimelineView::printWaitTimeEntry(formatted_raw_ostream &OS, |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 77 | const WaitTimeEntry &Entry, |
| 78 | unsigned SourceIndex) const { |
| 79 | OS << SourceIndex << '.'; |
Andrea Di Biagio | 039349a | 2018-05-15 18:11:45 +0000 | [diff] [blame] | 80 | OS.PadToColumn(7); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 81 | |
| 82 | if (Entry.Executions == 0) { |
Andrea Di Biagio | 039349a | 2018-05-15 18:11:45 +0000 | [diff] [blame] | 83 | OS << "- - - - "; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 84 | } else { |
| 85 | double AverageTime1, AverageTime2, AverageTime3; |
| 86 | unsigned Executions = Entry.Executions; |
| 87 | AverageTime1 = (double)Entry.CyclesSpentInSchedulerQueue / Executions; |
| 88 | AverageTime2 = (double)Entry.CyclesSpentInSQWhileReady / Executions; |
| 89 | AverageTime3 = (double)Entry.CyclesSpentAfterWBAndBeforeRetire / Executions; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 90 | |
Andrea Di Biagio | 039349a | 2018-05-15 18:11:45 +0000 | [diff] [blame] | 91 | OS << Executions; |
| 92 | OS.PadToColumn(13); |
| 93 | |
| 94 | OS << format("%.1f", floor((AverageTime1 * 10) + 0.5) / 10); |
| 95 | OS.PadToColumn(20); |
| 96 | OS << format("%.1f", floor((AverageTime2 * 10) + 0.5) / 10); |
| 97 | OS.PadToColumn(27); |
| 98 | OS << format("%.1f", floor((AverageTime3 * 10) + 0.5) / 10); |
| 99 | OS.PadToColumn(34); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
| 103 | void TimelineView::printAverageWaitTimes(raw_ostream &OS) const { |
| 104 | if (WaitTime.empty()) |
| 105 | return; |
| 106 | |
| 107 | std::string Buffer; |
| 108 | raw_string_ostream TempStream(Buffer); |
Andrea Di Biagio | 039349a | 2018-05-15 18:11:45 +0000 | [diff] [blame] | 109 | formatted_raw_ostream FOS(TempStream); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 110 | |
Andrea Di Biagio | 039349a | 2018-05-15 18:11:45 +0000 | [diff] [blame] | 111 | FOS << "\n\nAverage Wait times (based on the timeline view):\n" |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 112 | << "[0]: Executions\n" |
| 113 | << "[1]: Average time spent waiting in a scheduler's queue\n" |
| 114 | << "[2]: Average time spent waiting in a scheduler's queue while ready\n" |
| 115 | << "[3]: Average time elapsed from WB until retire stage\n\n"; |
Andrea Di Biagio | 039349a | 2018-05-15 18:11:45 +0000 | [diff] [blame] | 116 | FOS << " [0] [1] [2] [3]\n"; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 117 | |
Andrea Di Biagio | a7c3c45 | 2018-05-15 15:18:05 +0000 | [diff] [blame] | 118 | // Use a different string stream for the instruction. |
| 119 | std::string Instruction; |
| 120 | raw_string_ostream InstrStream(Instruction); |
| 121 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 122 | for (unsigned I = 0, E = WaitTime.size(); I < E; ++I) { |
Andrea Di Biagio | 039349a | 2018-05-15 18:11:45 +0000 | [diff] [blame] | 123 | printWaitTimeEntry(FOS, WaitTime[I], I); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 124 | // Append the instruction info at the end of the line. |
| 125 | const MCInst &Inst = AsmSequence.getMCInstFromIndex(I); |
Andrea Di Biagio | a7c3c45 | 2018-05-15 15:18:05 +0000 | [diff] [blame] | 126 | |
| 127 | MCIP.printInst(&Inst, InstrStream, "", STI); |
| 128 | InstrStream.flush(); |
| 129 | |
| 130 | // Consume any tabs or spaces at the beginning of the string. |
| 131 | StringRef Str(Instruction); |
| 132 | Str = Str.ltrim(); |
Andrea Di Biagio | 039349a | 2018-05-15 18:11:45 +0000 | [diff] [blame] | 133 | FOS << " " << Str << '\n'; |
| 134 | FOS.flush(); |
Andrea Di Biagio | a7c3c45 | 2018-05-15 15:18:05 +0000 | [diff] [blame] | 135 | Instruction = ""; |
| 136 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 137 | OS << Buffer; |
| 138 | Buffer = ""; |
| 139 | } |
| 140 | } |
| 141 | |
Andrea Di Biagio | 039349a | 2018-05-15 18:11:45 +0000 | [diff] [blame] | 142 | void TimelineView::printTimelineViewEntry(formatted_raw_ostream &OS, |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 143 | const TimelineViewEntry &Entry, |
| 144 | unsigned Iteration, |
| 145 | unsigned SourceIndex) const { |
Andrea Di Biagio | c9f409e | 2018-04-10 09:55:33 +0000 | [diff] [blame] | 146 | if (Iteration == 0 && SourceIndex == 0) |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 147 | OS << '\n'; |
Andrea Di Biagio | 039349a | 2018-05-15 18:11:45 +0000 | [diff] [blame] | 148 | OS << '[' << Iteration << ',' << SourceIndex << ']'; |
| 149 | OS.PadToColumn(10); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 150 | for (unsigned I = 0, E = Entry.CycleDispatched; I < E; ++I) |
| 151 | OS << ((I % 5 == 0) ? '.' : ' '); |
Matt Davis | 35df8b24 | 2018-05-04 17:19:40 +0000 | [diff] [blame] | 152 | OS << TimelineView::DisplayChar::Dispatched; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 153 | if (Entry.CycleDispatched != Entry.CycleExecuted) { |
| 154 | // Zero latency instructions have the same value for CycleDispatched, |
| 155 | // CycleIssued and CycleExecuted. |
Andrea Di Biagio | 53e6ade | 2018-03-09 12:50:42 +0000 | [diff] [blame] | 156 | for (unsigned I = Entry.CycleDispatched + 1, E = Entry.CycleIssued; I < E; |
| 157 | ++I) |
Matt Davis | 35df8b24 | 2018-05-04 17:19:40 +0000 | [diff] [blame] | 158 | OS << TimelineView::DisplayChar::Waiting; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 159 | if (Entry.CycleIssued == Entry.CycleExecuted) |
Matt Davis | 35df8b24 | 2018-05-04 17:19:40 +0000 | [diff] [blame] | 160 | OS << TimelineView::DisplayChar::DisplayChar::Executed; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 161 | else { |
| 162 | if (Entry.CycleDispatched != Entry.CycleIssued) |
Matt Davis | 35df8b24 | 2018-05-04 17:19:40 +0000 | [diff] [blame] | 163 | OS << TimelineView::DisplayChar::Executing; |
Andrea Di Biagio | 53e6ade | 2018-03-09 12:50:42 +0000 | [diff] [blame] | 164 | for (unsigned I = Entry.CycleIssued + 1, E = Entry.CycleExecuted; I < E; |
| 165 | ++I) |
Matt Davis | 35df8b24 | 2018-05-04 17:19:40 +0000 | [diff] [blame] | 166 | OS << TimelineView::DisplayChar::Executing; |
| 167 | OS << TimelineView::DisplayChar::Executed; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
| 171 | for (unsigned I = Entry.CycleExecuted + 1, E = Entry.CycleRetired; I < E; ++I) |
Matt Davis | 35df8b24 | 2018-05-04 17:19:40 +0000 | [diff] [blame] | 172 | OS << TimelineView::DisplayChar::RetireLag; |
| 173 | OS << TimelineView::DisplayChar::Retired; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 174 | |
| 175 | // Skip other columns. |
| 176 | for (unsigned I = Entry.CycleRetired + 1, E = LastCycle; I <= E; ++I) |
| 177 | OS << ((I % 5 == 0 || I == LastCycle) ? '.' : ' '); |
| 178 | } |
| 179 | |
Andrea Di Biagio | 039349a | 2018-05-15 18:11:45 +0000 | [diff] [blame] | 180 | static void printTimelineHeader(formatted_raw_ostream &OS, unsigned Cycles) { |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 181 | OS << "\n\nTimeline view:\n"; |
Andrea Di Biagio | cb1ed40 | 2018-05-21 17:11:56 +0000 | [diff] [blame] | 182 | if (Cycles >= 10) { |
| 183 | OS.PadToColumn(10); |
| 184 | for (unsigned I = 0; I <= Cycles; ++I) { |
| 185 | if (((I / 10) & 1) == 0) |
| 186 | OS << ' '; |
| 187 | else |
| 188 | OS << I % 10; |
| 189 | } |
| 190 | OS << '\n'; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Andrea Di Biagio | cb1ed40 | 2018-05-21 17:11:56 +0000 | [diff] [blame] | 193 | OS << "Index"; |
Andrea Di Biagio | 039349a | 2018-05-15 18:11:45 +0000 | [diff] [blame] | 194 | OS.PadToColumn(10); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 195 | for (unsigned I = 0; I <= Cycles; ++I) { |
| 196 | if (((I / 10) & 1) == 0) |
| 197 | OS << I % 10; |
| 198 | else |
| 199 | OS << ' '; |
| 200 | } |
| 201 | OS << '\n'; |
| 202 | } |
| 203 | |
| 204 | void TimelineView::printTimeline(raw_ostream &OS) const { |
| 205 | std::string Buffer; |
Andrea Di Biagio | 039349a | 2018-05-15 18:11:45 +0000 | [diff] [blame] | 206 | raw_string_ostream StringStream(Buffer); |
| 207 | formatted_raw_ostream FOS(StringStream); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 208 | |
Andrea Di Biagio | 039349a | 2018-05-15 18:11:45 +0000 | [diff] [blame] | 209 | printTimelineHeader(FOS, LastCycle); |
| 210 | FOS.flush(); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 211 | OS << Buffer; |
| 212 | |
Andrea Di Biagio | a7c3c45 | 2018-05-15 15:18:05 +0000 | [diff] [blame] | 213 | // Use a different string stream for the instruction. |
| 214 | std::string Instruction; |
| 215 | raw_string_ostream InstrStream(Instruction); |
| 216 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 217 | for (unsigned I = 0, E = Timeline.size(); I < E; ++I) { |
| 218 | Buffer = ""; |
| 219 | const TimelineViewEntry &Entry = Timeline[I]; |
| 220 | if (Entry.CycleRetired == 0) |
| 221 | return; |
| 222 | |
| 223 | unsigned Iteration = I / AsmSequence.size(); |
| 224 | unsigned SourceIndex = I % AsmSequence.size(); |
Andrea Di Biagio | 039349a | 2018-05-15 18:11:45 +0000 | [diff] [blame] | 225 | printTimelineViewEntry(FOS, Entry, Iteration, SourceIndex); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 226 | // Append the instruction info at the end of the line. |
| 227 | const MCInst &Inst = AsmSequence.getMCInstFromIndex(I); |
Andrea Di Biagio | a7c3c45 | 2018-05-15 15:18:05 +0000 | [diff] [blame] | 228 | MCIP.printInst(&Inst, InstrStream, "", STI); |
| 229 | InstrStream.flush(); |
| 230 | |
| 231 | // Consume any tabs or spaces at the beginning of the string. |
| 232 | StringRef Str(Instruction); |
| 233 | Str = Str.ltrim(); |
Andrea Di Biagio | 039349a | 2018-05-15 18:11:45 +0000 | [diff] [blame] | 234 | FOS << " " << Str << '\n'; |
| 235 | FOS.flush(); |
Andrea Di Biagio | a7c3c45 | 2018-05-15 15:18:05 +0000 | [diff] [blame] | 236 | Instruction = ""; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 237 | OS << Buffer; |
| 238 | } |
| 239 | } |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 240 | } // namespace mca |