1 //===--------------------- TimelineView.cpp ---------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// This file implements the TimelineView interface.
13 //===----------------------------------------------------------------------===//
15 #include "Views/TimelineView.h"
20 TimelineView::TimelineView(const MCSubtargetInfo &sti, MCInstPrinter &Printer,
21 llvm::ArrayRef<llvm::MCInst> S, unsigned Iterations,
23 : STI(sti), MCIP(Printer), Source(S), CurrentCycle(0),
24 MaxCycle(Cycles == 0 ? 80 : Cycles), LastCycle(0), WaitTime(S.size()),
25 UsedBuffer(S.size()) {
26 unsigned NumInstructions = Source.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);
33 WaitTimeEntry NullWTEntry = {0, 0, 0};
34 std::fill(WaitTime.begin(), WaitTime.end(), NullWTEntry);
36 std::pair<unsigned, int> NullUsedBufferEntry = {/* Invalid resource ID*/ 0,
37 /* unknown buffer size */ -1};
38 std::fill(UsedBuffer.begin(), UsedBuffer.end(), NullUsedBufferEntry);
41 void TimelineView::onReservedBuffers(const InstRef &IR,
42 ArrayRef<unsigned> Buffers) {
43 if (IR.getSourceIndex() >= Source.size())
46 const MCSchedModel &SM = STI.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;
56 UsedBuffer[IR.getSourceIndex()] = BufferInfo;
59 void TimelineView::onEvent(const HWInstructionEvent &Event) {
60 const unsigned Index = Event.IR.getSourceIndex();
61 if (Index >= Timeline.size())
65 case HWInstructionEvent::Retired: {
66 TimelineViewEntry &TVEntry = Timeline[Index];
67 if (CurrentCycle < MaxCycle)
68 TVEntry.CycleRetired = CurrentCycle;
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 % Source.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;
84 case HWInstructionEvent::Ready:
85 Timeline[Index].CycleReady = CurrentCycle;
87 case HWInstructionEvent::Issued:
88 Timeline[Index].CycleIssued = CurrentCycle;
90 case HWInstructionEvent::Executed:
91 Timeline[Index].CycleExecuted = CurrentCycle;
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);
103 if (CurrentCycle < MaxCycle)
104 LastCycle = std::max(LastCycle, CurrentCycle);
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;
119 static void tryChangeColor(raw_ostream &OS, unsigned Cycles,
120 unsigned Executions, int BufferSize) {
121 if (!OS.has_colors())
124 raw_ostream::Colors Color = chooseColor(Cycles, Executions, BufferSize);
125 if (Color == raw_ostream::SAVEDCOLOR) {
129 OS.changeColor(Color, /* bold */ true, /* BG */ false);
132 void TimelineView::printWaitTimeEntry(formatted_raw_ostream &OS,
133 const WaitTimeEntry &Entry,
134 unsigned SourceIndex,
135 unsigned Executions) const {
136 OS << SourceIndex << '.';
139 double AverageTime1, AverageTime2, AverageTime3;
140 AverageTime1 = (double)Entry.CyclesSpentInSchedulerQueue / Executions;
141 AverageTime2 = (double)Entry.CyclesSpentInSQWhileReady / Executions;
142 AverageTime3 = (double)Entry.CyclesSpentAfterWBAndBeforeRetire / Executions;
146 int BufferSize = UsedBuffer[SourceIndex].second;
147 tryChangeColor(OS, Entry.CyclesSpentInSchedulerQueue, Executions, BufferSize);
148 OS << format("%.1f", floor((AverageTime1 * 10) + 0.5) / 10);
150 tryChangeColor(OS, Entry.CyclesSpentInSQWhileReady, Executions, BufferSize);
151 OS << format("%.1f", floor((AverageTime2 * 10) + 0.5) / 10);
153 tryChangeColor(OS, Entry.CyclesSpentAfterWBAndBeforeRetire, Executions,
154 STI.getSchedModel().MicroOpBufferSize);
155 OS << format("%.1f", floor((AverageTime3 * 10) + 0.5) / 10);
162 void TimelineView::printAverageWaitTimes(raw_ostream &OS) const {
164 "\n\nAverage Wait times (based on the timeline view):\n"
166 "[1]: Average time spent waiting in a scheduler's queue\n"
167 "[2]: Average time spent waiting in a scheduler's queue while ready\n"
168 "[3]: Average time elapsed from WB until retire stage\n\n"
169 " [0] [1] [2] [3]\n";
172 // Use a different string stream for printing instructions.
173 std::string Instruction;
174 raw_string_ostream InstrStream(Instruction);
176 formatted_raw_ostream FOS(OS);
177 unsigned Executions = Timeline.size() / Source.size();
179 for (const MCInst &Inst : Source) {
180 printWaitTimeEntry(FOS, WaitTime[IID], IID, Executions);
181 // Append the instruction info at the end of the line.
182 MCIP.printInst(&Inst, InstrStream, "", STI);
185 // Consume any tabs or spaces at the beginning of the string.
186 StringRef Str(Instruction);
188 FOS << " " << Str << '\n';
196 void TimelineView::printTimelineViewEntry(formatted_raw_ostream &OS,
197 const TimelineViewEntry &Entry,
199 unsigned SourceIndex) const {
200 if (Iteration == 0 && SourceIndex == 0)
202 OS << '[' << Iteration << ',' << SourceIndex << ']';
204 assert(Entry.CycleDispatched >= 0 && "Invalid TimelineViewEntry!");
205 unsigned CycleDispatched = static_cast<unsigned>(Entry.CycleDispatched);
206 for (unsigned I = 0, E = CycleDispatched; I < E; ++I)
207 OS << ((I % 5 == 0) ? '.' : ' ');
208 OS << TimelineView::DisplayChar::Dispatched;
209 if (CycleDispatched != Entry.CycleExecuted) {
210 // Zero latency instructions have the same value for CycleDispatched,
211 // CycleIssued and CycleExecuted.
212 for (unsigned I = CycleDispatched + 1, E = Entry.CycleIssued; I < E; ++I)
213 OS << TimelineView::DisplayChar::Waiting;
214 if (Entry.CycleIssued == Entry.CycleExecuted)
215 OS << TimelineView::DisplayChar::DisplayChar::Executed;
217 if (CycleDispatched != Entry.CycleIssued)
218 OS << TimelineView::DisplayChar::Executing;
219 for (unsigned I = Entry.CycleIssued + 1, E = Entry.CycleExecuted; I < E;
221 OS << TimelineView::DisplayChar::Executing;
222 OS << TimelineView::DisplayChar::Executed;
226 for (unsigned I = Entry.CycleExecuted + 1, E = Entry.CycleRetired; I < E; ++I)
227 OS << TimelineView::DisplayChar::RetireLag;
228 OS << TimelineView::DisplayChar::Retired;
230 // Skip other columns.
231 for (unsigned I = Entry.CycleRetired + 1, E = LastCycle; I <= E; ++I)
232 OS << ((I % 5 == 0 || I == LastCycle) ? '.' : ' ');
235 static void printTimelineHeader(formatted_raw_ostream &OS, unsigned Cycles) {
236 OS << "\n\nTimeline view:\n";
239 for (unsigned I = 0; I <= Cycles; ++I) {
240 if (((I / 10) & 1) == 0)
250 for (unsigned I = 0; I <= Cycles; ++I) {
251 if (((I / 10) & 1) == 0)
259 void TimelineView::printTimeline(raw_ostream &OS) const {
260 formatted_raw_ostream FOS(OS);
261 printTimelineHeader(FOS, LastCycle);
264 // Use a different string stream for the instruction.
265 std::string Instruction;
266 raw_string_ostream InstrStream(Instruction);
269 const unsigned Iterations = Timeline.size() / Source.size();
270 for (unsigned Iteration = 0; Iteration < Iterations; ++Iteration) {
271 for (const MCInst &Inst : Source) {
272 const TimelineViewEntry &Entry = Timeline[IID];
273 if (Entry.CycleRetired == 0)
276 unsigned SourceIndex = IID % Source.size();
277 printTimelineViewEntry(FOS, Entry, Iteration, SourceIndex);
278 // Append the instruction info at the end of the line.
279 MCIP.printInst(&Inst, InstrStream, "", STI);
282 // Consume any tabs or spaces at the beginning of the string.
283 StringRef Str(Instruction);
285 FOS << " " << Str << '\n';