1 //===--------------------- TimelineView.cpp ---------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 /// \brief 9 /// 10 /// This file implements the TimelineView interface. 11 /// 12 //===----------------------------------------------------------------------===// 13 14 #include "Views/TimelineView.h" 15 #include <numeric> 16 17 namespace llvm { 18 namespace mca { 19 20 TimelineView::TimelineView(const MCSubtargetInfo &sti, MCInstPrinter &Printer, 21 llvm::ArrayRef<llvm::MCInst> S, unsigned Iterations, 22 unsigned Cycles) 23 : InstructionView(sti, Printer, S), CurrentCycle(0), 24 MaxCycle(Cycles == 0 ? 80 : Cycles), LastCycle(0), WaitTime(S.size()), 25 UsedBuffer(S.size()) { 26 unsigned NumInstructions = getSource().size(); 27 assert(Iterations && "Invalid number of iterations specified!"); 28 NumInstructions *= Iterations; 29 Timeline.resize(NumInstructions); 30 TimelineViewEntry InvalidTVEntry = {-1, 0, 0, 0, 0}; 31 std::fill(Timeline.begin(), Timeline.end(), InvalidTVEntry); 32 33 WaitTimeEntry NullWTEntry = {0, 0, 0}; 34 std::fill(WaitTime.begin(), WaitTime.end(), NullWTEntry); 35 36 std::pair<unsigned, int> NullUsedBufferEntry = {/* Invalid resource ID*/ 0, 37 /* unknown buffer size */ -1}; 38 std::fill(UsedBuffer.begin(), UsedBuffer.end(), NullUsedBufferEntry); 39 } 40 41 void TimelineView::onReservedBuffers(const InstRef &IR, 42 ArrayRef<unsigned> Buffers) { 43 if (IR.getSourceIndex() >= getSource().size()) 44 return; 45 46 const MCSchedModel &SM = getSubTargetInfo().getSchedModel(); 47 std::pair<unsigned, int> BufferInfo = {0, -1}; 48 for (const unsigned Buffer : Buffers) { 49 const MCProcResourceDesc &MCDesc = *SM.getProcResource(Buffer); 50 if (!BufferInfo.first || BufferInfo.second > MCDesc.BufferSize) { 51 BufferInfo.first = Buffer; 52 BufferInfo.second = MCDesc.BufferSize; 53 } 54 } 55 56 UsedBuffer[IR.getSourceIndex()] = BufferInfo; 57 } 58 59 void TimelineView::onEvent(const HWInstructionEvent &Event) { 60 const unsigned Index = Event.IR.getSourceIndex(); 61 if (Index >= Timeline.size()) 62 return; 63 64 switch (Event.Type) { 65 case HWInstructionEvent::Retired: { 66 TimelineViewEntry &TVEntry = Timeline[Index]; 67 if (CurrentCycle < MaxCycle) 68 TVEntry.CycleRetired = CurrentCycle; 69 70 // Update the WaitTime entry which corresponds to this Index. 71 assert(TVEntry.CycleDispatched >= 0 && "Invalid TVEntry found!"); 72 unsigned CycleDispatched = static_cast<unsigned>(TVEntry.CycleDispatched); 73 WaitTimeEntry &WTEntry = WaitTime[Index % getSource().size()]; 74 WTEntry.CyclesSpentInSchedulerQueue += 75 TVEntry.CycleIssued - CycleDispatched; 76 assert(CycleDispatched <= TVEntry.CycleReady && 77 "Instruction cannot be ready if it hasn't been dispatched yet!"); 78 WTEntry.CyclesSpentInSQWhileReady += 79 TVEntry.CycleIssued - TVEntry.CycleReady; 80 WTEntry.CyclesSpentAfterWBAndBeforeRetire += 81 (CurrentCycle - 1) - TVEntry.CycleExecuted; 82 break; 83 } 84 case HWInstructionEvent::Ready: 85 Timeline[Index].CycleReady = CurrentCycle; 86 break; 87 case HWInstructionEvent::Issued: 88 Timeline[Index].CycleIssued = CurrentCycle; 89 break; 90 case HWInstructionEvent::Executed: 91 Timeline[Index].CycleExecuted = CurrentCycle; 92 break; 93 case HWInstructionEvent::Dispatched: 94 // There may be multiple dispatch events. Microcoded instructions that are 95 // expanded into multiple uOps may require multiple dispatch cycles. Here, 96 // we want to capture the first dispatch cycle. 97 if (Timeline[Index].CycleDispatched == -1) 98 Timeline[Index].CycleDispatched = static_cast<int>(CurrentCycle); 99 break; 100 default: 101 return; 102 } 103 if (CurrentCycle < MaxCycle) 104 LastCycle = std::max(LastCycle, CurrentCycle); 105 } 106 107 static raw_ostream::Colors chooseColor(unsigned CumulativeCycles, 108 unsigned Executions, int BufferSize) { 109 if (CumulativeCycles && BufferSize < 0) 110 return raw_ostream::MAGENTA; 111 unsigned Size = static_cast<unsigned>(BufferSize); 112 if (CumulativeCycles >= Size * Executions) 113 return raw_ostream::RED; 114 if ((CumulativeCycles * 2) >= Size * Executions) 115 return raw_ostream::YELLOW; 116 return raw_ostream::SAVEDCOLOR; 117 } 118 119 static void tryChangeColor(raw_ostream &OS, unsigned Cycles, 120 unsigned Executions, int BufferSize) { 121 if (!OS.has_colors()) 122 return; 123 124 raw_ostream::Colors Color = chooseColor(Cycles, Executions, BufferSize); 125 if (Color == raw_ostream::SAVEDCOLOR) { 126 OS.resetColor(); 127 return; 128 } 129 OS.changeColor(Color, /* bold */ true, /* BG */ false); 130 } 131 132 void TimelineView::printWaitTimeEntry(formatted_raw_ostream &OS, 133 const WaitTimeEntry &Entry, 134 unsigned SourceIndex, 135 unsigned Executions) const { 136 bool PrintingTotals = SourceIndex == getSource().size(); 137 unsigned CumulativeExecutions = PrintingTotals ? Timeline.size() : Executions; 138 139 if (!PrintingTotals) 140 OS << SourceIndex << '.'; 141 142 OS.PadToColumn(7); 143 144 double AverageTime1, AverageTime2, AverageTime3; 145 AverageTime1 = 146 (double)Entry.CyclesSpentInSchedulerQueue / CumulativeExecutions; 147 AverageTime2 = (double)Entry.CyclesSpentInSQWhileReady / CumulativeExecutions; 148 AverageTime3 = 149 (double)Entry.CyclesSpentAfterWBAndBeforeRetire / CumulativeExecutions; 150 151 OS << Executions; 152 OS.PadToColumn(13); 153 154 int BufferSize = PrintingTotals ? 0 : UsedBuffer[SourceIndex].second; 155 if (!PrintingTotals) 156 tryChangeColor(OS, Entry.CyclesSpentInSchedulerQueue, CumulativeExecutions, 157 BufferSize); 158 OS << format("%.1f", floor((AverageTime1 * 10) + 0.5) / 10); 159 OS.PadToColumn(20); 160 if (!PrintingTotals) 161 tryChangeColor(OS, Entry.CyclesSpentInSQWhileReady, CumulativeExecutions, 162 BufferSize); 163 OS << format("%.1f", floor((AverageTime2 * 10) + 0.5) / 10); 164 OS.PadToColumn(27); 165 if (!PrintingTotals) 166 tryChangeColor(OS, Entry.CyclesSpentAfterWBAndBeforeRetire, 167 CumulativeExecutions, 168 getSubTargetInfo().getSchedModel().MicroOpBufferSize); 169 OS << format("%.1f", floor((AverageTime3 * 10) + 0.5) / 10); 170 171 if (OS.has_colors()) 172 OS.resetColor(); 173 OS.PadToColumn(34); 174 } 175 176 void TimelineView::printAverageWaitTimes(raw_ostream &OS) const { 177 std::string Header = 178 "\n\nAverage Wait times (based on the timeline view):\n" 179 "[0]: Executions\n" 180 "[1]: Average time spent waiting in a scheduler's queue\n" 181 "[2]: Average time spent waiting in a scheduler's queue while ready\n" 182 "[3]: Average time elapsed from WB until retire stage\n\n" 183 " [0] [1] [2] [3]\n"; 184 OS << Header; 185 formatted_raw_ostream FOS(OS); 186 unsigned Executions = Timeline.size() / getSource().size(); 187 unsigned IID = 0; 188 for (const MCInst &Inst : getSource()) { 189 printWaitTimeEntry(FOS, WaitTime[IID], IID, Executions); 190 FOS << " " << printInstructionString(Inst) << '\n'; 191 FOS.flush(); 192 ++IID; 193 } 194 195 // If the timeline contains more than one instruction, 196 // let's also print global averages. 197 if (getSource().size() != 1) { 198 WaitTimeEntry TotalWaitTime = std::accumulate( 199 WaitTime.begin(), WaitTime.end(), WaitTimeEntry{0, 0, 0}, 200 [](const WaitTimeEntry &A, const WaitTimeEntry &B) { 201 return WaitTimeEntry{ 202 A.CyclesSpentInSchedulerQueue + B.CyclesSpentInSchedulerQueue, 203 A.CyclesSpentInSQWhileReady + B.CyclesSpentInSQWhileReady, 204 A.CyclesSpentAfterWBAndBeforeRetire + 205 B.CyclesSpentAfterWBAndBeforeRetire}; 206 }); 207 printWaitTimeEntry(FOS, TotalWaitTime, IID, Executions); 208 FOS << " " 209 << "<total>" << '\n'; 210 FOS.flush(); 211 } 212 } 213 214 void TimelineView::printTimelineViewEntry(formatted_raw_ostream &OS, 215 const TimelineViewEntry &Entry, 216 unsigned Iteration, 217 unsigned SourceIndex) const { 218 if (Iteration == 0 && SourceIndex == 0) 219 OS << '\n'; 220 OS << '[' << Iteration << ',' << SourceIndex << ']'; 221 OS.PadToColumn(10); 222 assert(Entry.CycleDispatched >= 0 && "Invalid TimelineViewEntry!"); 223 unsigned CycleDispatched = static_cast<unsigned>(Entry.CycleDispatched); 224 for (unsigned I = 0, E = CycleDispatched; I < E; ++I) 225 OS << ((I % 5 == 0) ? '.' : ' '); 226 OS << TimelineView::DisplayChar::Dispatched; 227 if (CycleDispatched != Entry.CycleExecuted) { 228 // Zero latency instructions have the same value for CycleDispatched, 229 // CycleIssued and CycleExecuted. 230 for (unsigned I = CycleDispatched + 1, E = Entry.CycleIssued; I < E; ++I) 231 OS << TimelineView::DisplayChar::Waiting; 232 if (Entry.CycleIssued == Entry.CycleExecuted) 233 OS << TimelineView::DisplayChar::DisplayChar::Executed; 234 else { 235 if (CycleDispatched != Entry.CycleIssued) 236 OS << TimelineView::DisplayChar::Executing; 237 for (unsigned I = Entry.CycleIssued + 1, E = Entry.CycleExecuted; I < E; 238 ++I) 239 OS << TimelineView::DisplayChar::Executing; 240 OS << TimelineView::DisplayChar::Executed; 241 } 242 } 243 244 for (unsigned I = Entry.CycleExecuted + 1, E = Entry.CycleRetired; I < E; ++I) 245 OS << TimelineView::DisplayChar::RetireLag; 246 OS << TimelineView::DisplayChar::Retired; 247 248 // Skip other columns. 249 for (unsigned I = Entry.CycleRetired + 1, E = LastCycle; I <= E; ++I) 250 OS << ((I % 5 == 0 || I == LastCycle) ? '.' : ' '); 251 } 252 253 static void printTimelineHeader(formatted_raw_ostream &OS, unsigned Cycles) { 254 OS << "\n\nTimeline view:\n"; 255 if (Cycles >= 10) { 256 OS.PadToColumn(10); 257 for (unsigned I = 0; I <= Cycles; ++I) { 258 if (((I / 10) & 1) == 0) 259 OS << ' '; 260 else 261 OS << I % 10; 262 } 263 OS << '\n'; 264 } 265 266 OS << "Index"; 267 OS.PadToColumn(10); 268 for (unsigned I = 0; I <= Cycles; ++I) { 269 if (((I / 10) & 1) == 0) 270 OS << I % 10; 271 else 272 OS << ' '; 273 } 274 OS << '\n'; 275 } 276 277 void TimelineView::printTimeline(raw_ostream &OS) const { 278 formatted_raw_ostream FOS(OS); 279 printTimelineHeader(FOS, LastCycle); 280 FOS.flush(); 281 282 unsigned IID = 0; 283 ArrayRef<llvm::MCInst> Source = getSource(); 284 const unsigned Iterations = Timeline.size() / Source.size(); 285 for (unsigned Iteration = 0; Iteration < Iterations; ++Iteration) { 286 for (const MCInst &Inst : Source) { 287 const TimelineViewEntry &Entry = Timeline[IID]; 288 if (Entry.CycleRetired == 0) 289 return; 290 291 unsigned SourceIndex = IID % Source.size(); 292 printTimelineViewEntry(FOS, Entry, Iteration, SourceIndex); 293 FOS << " " << printInstructionString(Inst) << '\n'; 294 FOS.flush(); 295 296 ++IID; 297 } 298 } 299 } 300 301 json::Value TimelineView::toJSON() const { 302 json::Array TimelineInfo; 303 304 for (const TimelineViewEntry &TLE : Timeline) { 305 TimelineInfo.push_back( 306 json::Object({{"CycleDispatched", TLE.CycleDispatched}, 307 {"CycleReady", TLE.CycleReady}, 308 {"CycleIssued", TLE.CycleIssued}, 309 {"CycleExecuted", TLE.CycleExecuted}, 310 {"CycleRetired", TLE.CycleRetired}})); 311 } 312 return json::Object({{"TimelineInfo", std::move(TimelineInfo)}}); 313 } 314 } // namespace mca 315 } // namespace llvm 316