1 //===--------------------- TimelineView.cpp ---------------------*- C++ -*-===//
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 
15 #include "Views/TimelineView.h"
16 
17 namespace llvm {
18 namespace mca {
19 
TimelineView(const MCSubtargetInfo & sti,MCInstPrinter & Printer,llvm::ArrayRef<llvm::MCInst> S,unsigned Iterations,unsigned Cycles)20 TimelineView::TimelineView(const MCSubtargetInfo &sti, MCInstPrinter &Printer,
21                            llvm::ArrayRef<llvm::MCInst> S, unsigned Iterations,
22                            unsigned Cycles)
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);
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 
onReservedBuffers(const InstRef & IR,ArrayRef<unsigned> Buffers)41 void TimelineView::onReservedBuffers(const InstRef &IR,
42                                      ArrayRef<unsigned> Buffers) {
43   if (IR.getSourceIndex() >= Source.size())
44     return;
45 
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;
53     }
54   }
55 
56   UsedBuffer[IR.getSourceIndex()] = BufferInfo;
57 }
58 
onEvent(const HWInstructionEvent & Event)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 % 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;
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 
chooseColor(unsigned CumulativeCycles,unsigned Executions,int BufferSize)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 
tryChangeColor(raw_ostream & OS,unsigned Cycles,unsigned Executions,int BufferSize)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 
printWaitTimeEntry(formatted_raw_ostream & OS,const WaitTimeEntry & Entry,unsigned SourceIndex,unsigned Executions) const132 void TimelineView::printWaitTimeEntry(formatted_raw_ostream &OS,
133                                       const WaitTimeEntry &Entry,
134                                       unsigned SourceIndex,
135                                       unsigned Executions) const {
136   OS << SourceIndex << '.';
137   OS.PadToColumn(7);
138 
139   double AverageTime1, AverageTime2, AverageTime3;
140   AverageTime1 = (double)Entry.CyclesSpentInSchedulerQueue / Executions;
141   AverageTime2 = (double)Entry.CyclesSpentInSQWhileReady / Executions;
142   AverageTime3 = (double)Entry.CyclesSpentAfterWBAndBeforeRetire / Executions;
143 
144   OS << Executions;
145   OS.PadToColumn(13);
146   int BufferSize = UsedBuffer[SourceIndex].second;
147   tryChangeColor(OS, Entry.CyclesSpentInSchedulerQueue, Executions, BufferSize);
148   OS << format("%.1f", floor((AverageTime1 * 10) + 0.5) / 10);
149   OS.PadToColumn(20);
150   tryChangeColor(OS, Entry.CyclesSpentInSQWhileReady, Executions, BufferSize);
151   OS << format("%.1f", floor((AverageTime2 * 10) + 0.5) / 10);
152   OS.PadToColumn(27);
153   tryChangeColor(OS, Entry.CyclesSpentAfterWBAndBeforeRetire, Executions,
154                  STI.getSchedModel().MicroOpBufferSize);
155   OS << format("%.1f", floor((AverageTime3 * 10) + 0.5) / 10);
156 
157   if (OS.has_colors())
158     OS.resetColor();
159   OS.PadToColumn(34);
160 }
161 
printAverageWaitTimes(raw_ostream & OS) const162 void TimelineView::printAverageWaitTimes(raw_ostream &OS) const {
163   std::string Header =
164       "\n\nAverage Wait times (based on the timeline view):\n"
165       "[0]: Executions\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";
170   OS << Header;
171 
172   // Use a different string stream for printing instructions.
173   std::string Instruction;
174   raw_string_ostream InstrStream(Instruction);
175 
176   formatted_raw_ostream FOS(OS);
177   unsigned Executions = Timeline.size() / Source.size();
178   unsigned IID = 0;
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);
183     InstrStream.flush();
184 
185     // Consume any tabs or spaces at the beginning of the string.
186     StringRef Str(Instruction);
187     Str = Str.ltrim();
188     FOS << "   " << Str << '\n';
189     FOS.flush();
190     Instruction = "";
191 
192     ++IID;
193   }
194 }
195 
printTimelineViewEntry(formatted_raw_ostream & OS,const TimelineViewEntry & Entry,unsigned Iteration,unsigned SourceIndex) const196 void TimelineView::printTimelineViewEntry(formatted_raw_ostream &OS,
197                                           const TimelineViewEntry &Entry,
198                                           unsigned Iteration,
199                                           unsigned SourceIndex) const {
200   if (Iteration == 0 && SourceIndex == 0)
201     OS << '\n';
202   OS << '[' << Iteration << ',' << SourceIndex << ']';
203   OS.PadToColumn(10);
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;
216     else {
217       if (CycleDispatched != Entry.CycleIssued)
218         OS << TimelineView::DisplayChar::Executing;
219       for (unsigned I = Entry.CycleIssued + 1, E = Entry.CycleExecuted; I < E;
220            ++I)
221         OS << TimelineView::DisplayChar::Executing;
222       OS << TimelineView::DisplayChar::Executed;
223     }
224   }
225 
226   for (unsigned I = Entry.CycleExecuted + 1, E = Entry.CycleRetired; I < E; ++I)
227     OS << TimelineView::DisplayChar::RetireLag;
228   OS << TimelineView::DisplayChar::Retired;
229 
230   // Skip other columns.
231   for (unsigned I = Entry.CycleRetired + 1, E = LastCycle; I <= E; ++I)
232     OS << ((I % 5 == 0 || I == LastCycle) ? '.' : ' ');
233 }
234 
printTimelineHeader(formatted_raw_ostream & OS,unsigned Cycles)235 static void printTimelineHeader(formatted_raw_ostream &OS, unsigned Cycles) {
236   OS << "\n\nTimeline view:\n";
237   if (Cycles >= 10) {
238     OS.PadToColumn(10);
239     for (unsigned I = 0; I <= Cycles; ++I) {
240       if (((I / 10) & 1) == 0)
241         OS << ' ';
242       else
243         OS << I % 10;
244     }
245     OS << '\n';
246   }
247 
248   OS << "Index";
249   OS.PadToColumn(10);
250   for (unsigned I = 0; I <= Cycles; ++I) {
251     if (((I / 10) & 1) == 0)
252       OS << I % 10;
253     else
254       OS << ' ';
255   }
256   OS << '\n';
257 }
258 
printTimeline(raw_ostream & OS) const259 void TimelineView::printTimeline(raw_ostream &OS) const {
260   formatted_raw_ostream FOS(OS);
261   printTimelineHeader(FOS, LastCycle);
262   FOS.flush();
263 
264   // Use a different string stream for the instruction.
265   std::string Instruction;
266   raw_string_ostream InstrStream(Instruction);
267 
268   unsigned IID = 0;
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)
274         return;
275 
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);
280       InstrStream.flush();
281 
282       // Consume any tabs or spaces at the beginning of the string.
283       StringRef Str(Instruction);
284       Str = Str.ltrim();
285       FOS << "   " << Str << '\n';
286       FOS.flush();
287       Instruction = "";
288 
289       ++IID;
290     }
291   }
292 }
293 } // namespace mca
294 } // namespace llvm
295