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 if (CurrentCycle > TVEntry.CycleExecuted) { 81 WTEntry.CyclesSpentAfterWBAndBeforeRetire += 82 (CurrentCycle - 1) - TVEntry.CycleExecuted; 83 } 84 break; 85 } 86 case HWInstructionEvent::Ready: 87 Timeline[Index].CycleReady = CurrentCycle; 88 break; 89 case HWInstructionEvent::Issued: 90 Timeline[Index].CycleIssued = CurrentCycle; 91 break; 92 case HWInstructionEvent::Executed: 93 Timeline[Index].CycleExecuted = CurrentCycle; 94 break; 95 case HWInstructionEvent::Dispatched: 96 // There may be multiple dispatch events. Microcoded instructions that are 97 // expanded into multiple uOps may require multiple dispatch cycles. Here, 98 // we want to capture the first dispatch cycle. 99 if (Timeline[Index].CycleDispatched == -1) 100 Timeline[Index].CycleDispatched = static_cast<int>(CurrentCycle); 101 break; 102 default: 103 return; 104 } 105 if (CurrentCycle < MaxCycle) 106 LastCycle = std::max(LastCycle, CurrentCycle); 107 } 108 109 static raw_ostream::Colors chooseColor(unsigned CumulativeCycles, 110 unsigned Executions, int BufferSize) { 111 if (CumulativeCycles && BufferSize < 0) 112 return raw_ostream::MAGENTA; 113 unsigned Size = static_cast<unsigned>(BufferSize); 114 if (CumulativeCycles >= Size * Executions) 115 return raw_ostream::RED; 116 if ((CumulativeCycles * 2) >= Size * Executions) 117 return raw_ostream::YELLOW; 118 return raw_ostream::SAVEDCOLOR; 119 } 120 121 static void tryChangeColor(raw_ostream &OS, unsigned Cycles, 122 unsigned Executions, int BufferSize) { 123 if (!OS.has_colors()) 124 return; 125 126 raw_ostream::Colors Color = chooseColor(Cycles, Executions, BufferSize); 127 if (Color == raw_ostream::SAVEDCOLOR) { 128 OS.resetColor(); 129 return; 130 } 131 OS.changeColor(Color, /* bold */ true, /* BG */ false); 132 } 133 134 void TimelineView::printWaitTimeEntry(formatted_raw_ostream &OS, 135 const WaitTimeEntry &Entry, 136 unsigned SourceIndex, 137 unsigned Executions) const { 138 bool PrintingTotals = SourceIndex == getSource().size(); 139 unsigned CumulativeExecutions = PrintingTotals ? Timeline.size() : Executions; 140 141 if (!PrintingTotals) 142 OS << SourceIndex << '.'; 143 144 OS.PadToColumn(7); 145 146 double AverageTime1, AverageTime2, AverageTime3; 147 AverageTime1 = 148 (double)Entry.CyclesSpentInSchedulerQueue / CumulativeExecutions; 149 AverageTime2 = (double)Entry.CyclesSpentInSQWhileReady / CumulativeExecutions; 150 AverageTime3 = 151 (double)Entry.CyclesSpentAfterWBAndBeforeRetire / CumulativeExecutions; 152 153 OS << Executions; 154 OS.PadToColumn(13); 155 156 int BufferSize = PrintingTotals ? 0 : UsedBuffer[SourceIndex].second; 157 if (!PrintingTotals) 158 tryChangeColor(OS, Entry.CyclesSpentInSchedulerQueue, CumulativeExecutions, 159 BufferSize); 160 OS << format("%.1f", floor((AverageTime1 * 10) + 0.5) / 10); 161 OS.PadToColumn(20); 162 if (!PrintingTotals) 163 tryChangeColor(OS, Entry.CyclesSpentInSQWhileReady, CumulativeExecutions, 164 BufferSize); 165 OS << format("%.1f", floor((AverageTime2 * 10) + 0.5) / 10); 166 OS.PadToColumn(27); 167 if (!PrintingTotals) 168 tryChangeColor(OS, Entry.CyclesSpentAfterWBAndBeforeRetire, 169 CumulativeExecutions, 170 getSubTargetInfo().getSchedModel().MicroOpBufferSize); 171 OS << format("%.1f", floor((AverageTime3 * 10) + 0.5) / 10); 172 173 if (OS.has_colors()) 174 OS.resetColor(); 175 OS.PadToColumn(34); 176 } 177 178 void TimelineView::printAverageWaitTimes(raw_ostream &OS) const { 179 std::string Header = 180 "\n\nAverage Wait times (based on the timeline view):\n" 181 "[0]: Executions\n" 182 "[1]: Average time spent waiting in a scheduler's queue\n" 183 "[2]: Average time spent waiting in a scheduler's queue while ready\n" 184 "[3]: Average time elapsed from WB until retire stage\n\n" 185 " [0] [1] [2] [3]\n"; 186 OS << Header; 187 formatted_raw_ostream FOS(OS); 188 unsigned Executions = Timeline.size() / getSource().size(); 189 unsigned IID = 0; 190 for (const MCInst &Inst : getSource()) { 191 printWaitTimeEntry(FOS, WaitTime[IID], IID, Executions); 192 FOS << " " << printInstructionString(Inst) << '\n'; 193 FOS.flush(); 194 ++IID; 195 } 196 197 // If the timeline contains more than one instruction, 198 // let's also print global averages. 199 if (getSource().size() != 1) { 200 WaitTimeEntry TotalWaitTime = std::accumulate( 201 WaitTime.begin(), WaitTime.end(), WaitTimeEntry{0, 0, 0}, 202 [](const WaitTimeEntry &A, const WaitTimeEntry &B) { 203 return WaitTimeEntry{ 204 A.CyclesSpentInSchedulerQueue + B.CyclesSpentInSchedulerQueue, 205 A.CyclesSpentInSQWhileReady + B.CyclesSpentInSQWhileReady, 206 A.CyclesSpentAfterWBAndBeforeRetire + 207 B.CyclesSpentAfterWBAndBeforeRetire}; 208 }); 209 printWaitTimeEntry(FOS, TotalWaitTime, IID, Executions); 210 FOS << " " 211 << "<total>" << '\n'; 212 FOS.flush(); 213 } 214 } 215 216 void TimelineView::printTimelineViewEntry(formatted_raw_ostream &OS, 217 const TimelineViewEntry &Entry, 218 unsigned Iteration, 219 unsigned SourceIndex) const { 220 if (Iteration == 0 && SourceIndex == 0) 221 OS << '\n'; 222 OS << '[' << Iteration << ',' << SourceIndex << ']'; 223 OS.PadToColumn(10); 224 assert(Entry.CycleDispatched >= 0 && "Invalid TimelineViewEntry!"); 225 unsigned CycleDispatched = static_cast<unsigned>(Entry.CycleDispatched); 226 for (unsigned I = 0, E = CycleDispatched; I < E; ++I) 227 OS << ((I % 5 == 0) ? '.' : ' '); 228 OS << TimelineView::DisplayChar::Dispatched; 229 if (CycleDispatched != Entry.CycleExecuted) { 230 // Zero latency instructions have the same value for CycleDispatched, 231 // CycleIssued and CycleExecuted. 232 for (unsigned I = CycleDispatched + 1, E = Entry.CycleIssued; I < E; ++I) 233 OS << TimelineView::DisplayChar::Waiting; 234 if (Entry.CycleIssued == Entry.CycleExecuted) 235 OS << TimelineView::DisplayChar::DisplayChar::Executed; 236 else { 237 if (CycleDispatched != Entry.CycleIssued) 238 OS << TimelineView::DisplayChar::Executing; 239 for (unsigned I = Entry.CycleIssued + 1, E = Entry.CycleExecuted; I < E; 240 ++I) 241 OS << TimelineView::DisplayChar::Executing; 242 OS << TimelineView::DisplayChar::Executed; 243 } 244 } 245 246 for (unsigned I = Entry.CycleExecuted + 1, E = Entry.CycleRetired; I < E; ++I) 247 OS << TimelineView::DisplayChar::RetireLag; 248 if (Entry.CycleExecuted < Entry.CycleRetired) 249 OS << TimelineView::DisplayChar::Retired; 250 251 // Skip other columns. 252 for (unsigned I = Entry.CycleRetired + 1, E = LastCycle; I <= E; ++I) 253 OS << ((I % 5 == 0 || I == LastCycle) ? '.' : ' '); 254 } 255 256 static void printTimelineHeader(formatted_raw_ostream &OS, unsigned Cycles) { 257 OS << "\n\nTimeline view:\n"; 258 if (Cycles >= 10) { 259 OS.PadToColumn(10); 260 for (unsigned I = 0; I <= Cycles; ++I) { 261 if (((I / 10) & 1) == 0) 262 OS << ' '; 263 else 264 OS << I % 10; 265 } 266 OS << '\n'; 267 } 268 269 OS << "Index"; 270 OS.PadToColumn(10); 271 for (unsigned I = 0; I <= Cycles; ++I) { 272 if (((I / 10) & 1) == 0) 273 OS << I % 10; 274 else 275 OS << ' '; 276 } 277 OS << '\n'; 278 } 279 280 void TimelineView::printTimeline(raw_ostream &OS) const { 281 formatted_raw_ostream FOS(OS); 282 printTimelineHeader(FOS, LastCycle); 283 FOS.flush(); 284 285 unsigned IID = 0; 286 ArrayRef<llvm::MCInst> Source = getSource(); 287 const unsigned Iterations = Timeline.size() / Source.size(); 288 for (unsigned Iteration = 0; Iteration < Iterations; ++Iteration) { 289 for (const MCInst &Inst : Source) { 290 const TimelineViewEntry &Entry = Timeline[IID]; 291 if (Entry.CycleRetired == 0) 292 return; 293 294 unsigned SourceIndex = IID % Source.size(); 295 printTimelineViewEntry(FOS, Entry, Iteration, SourceIndex); 296 FOS << " " << printInstructionString(Inst) << '\n'; 297 FOS.flush(); 298 299 ++IID; 300 } 301 } 302 } 303 304 json::Value TimelineView::toJSON() const { 305 json::Array TimelineInfo; 306 307 for (const TimelineViewEntry &TLE : Timeline) { 308 TimelineInfo.push_back( 309 json::Object({{"CycleDispatched", TLE.CycleDispatched}, 310 {"CycleReady", TLE.CycleReady}, 311 {"CycleIssued", TLE.CycleIssued}, 312 {"CycleExecuted", TLE.CycleExecuted}, 313 {"CycleRetired", TLE.CycleRetired}})); 314 } 315 return json::Object({{"TimelineInfo", std::move(TimelineInfo)}}); 316 } 317 } // namespace mca 318 } // namespace llvm 319