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 using namespace llvm;
18 
19 namespace mca {
20 
21 TimelineView::TimelineView(const MCSubtargetInfo &sti, MCInstPrinter &Printer,
22                            const SourceMgr &S, unsigned MaxIterations,
23                            unsigned Cycles)
24     : STI(sti), MCIP(Printer), AsmSequence(S), CurrentCycle(0),
25       MaxCycle(Cycles == 0 ? 80 : Cycles), LastCycle(0), WaitTime(S.size()),
26       UsedBuffer(S.size()) {
27   unsigned NumInstructions = AsmSequence.size();
28   if (!MaxIterations)
29     MaxIterations = DEFAULT_ITERATIONS;
30   NumInstructions *= std::min(MaxIterations, AsmSequence.getNumIterations());
31   Timeline.resize(NumInstructions);
32   TimelineViewEntry InvalidTVEntry = {-1, 0, 0, 0, 0};
33   std::fill(Timeline.begin(), Timeline.end(), InvalidTVEntry);
34 
35   WaitTimeEntry NullWTEntry = {0, 0, 0};
36   std::fill(WaitTime.begin(), WaitTime.end(), NullWTEntry);
37 
38   std::pair<unsigned, int> NullUsedBufferEntry = {/* Invalid resource ID*/ 0,
39                                                   /* unknown buffer size */ -1};
40   std::fill(UsedBuffer.begin(), UsedBuffer.end(), NullUsedBufferEntry);
41 }
42 
43 void TimelineView::onReservedBuffers(const InstRef &IR,
44                                      ArrayRef<unsigned> Buffers) {
45   if (IR.getSourceIndex() >= AsmSequence.size())
46     return;
47 
48   const MCSchedModel &SM = STI.getSchedModel();
49   std::pair<unsigned, int> BufferInfo = {0, -1};
50   for (const unsigned Buffer : Buffers) {
51     const MCProcResourceDesc &MCDesc = *SM.getProcResource(Buffer);
52     if (!BufferInfo.first || BufferInfo.second > MCDesc.BufferSize) {
53       BufferInfo.first = Buffer;
54       BufferInfo.second = MCDesc.BufferSize;
55     }
56   }
57 
58   UsedBuffer[IR.getSourceIndex()] = BufferInfo;
59 }
60 
61 void TimelineView::onEvent(const HWInstructionEvent &Event) {
62   const unsigned Index = Event.IR.getSourceIndex();
63   if (Index >= Timeline.size())
64     return;
65 
66   switch (Event.Type) {
67   case HWInstructionEvent::Retired: {
68     TimelineViewEntry &TVEntry = Timeline[Index];
69     if (CurrentCycle < MaxCycle)
70       TVEntry.CycleRetired = CurrentCycle;
71 
72     // Update the WaitTime entry which corresponds to this Index.
73     assert(TVEntry.CycleDispatched >= 0 && "Invalid TVEntry found!");
74     unsigned CycleDispatched = static_cast<unsigned>(TVEntry.CycleDispatched);
75     WaitTimeEntry &WTEntry = WaitTime[Index % AsmSequence.size()];
76     WTEntry.CyclesSpentInSchedulerQueue +=
77         TVEntry.CycleIssued - CycleDispatched;
78     assert(CycleDispatched <= TVEntry.CycleReady &&
79            "Instruction cannot be ready if it hasn't been dispatched yet!");
80     WTEntry.CyclesSpentInSQWhileReady +=
81         TVEntry.CycleIssued - TVEntry.CycleReady;
82     WTEntry.CyclesSpentAfterWBAndBeforeRetire +=
83         (CurrentCycle - 1) - TVEntry.CycleExecuted;
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   OS << SourceIndex << '.';
139   OS.PadToColumn(7);
140 
141   double AverageTime1, AverageTime2, AverageTime3;
142   AverageTime1 = (double)Entry.CyclesSpentInSchedulerQueue / Executions;
143   AverageTime2 = (double)Entry.CyclesSpentInSQWhileReady / Executions;
144   AverageTime3 = (double)Entry.CyclesSpentAfterWBAndBeforeRetire / Executions;
145 
146   OS << Executions;
147   OS.PadToColumn(13);
148   int BufferSize = UsedBuffer[SourceIndex].second;
149   tryChangeColor(OS, Entry.CyclesSpentInSchedulerQueue, Executions, BufferSize);
150   OS << format("%.1f", floor((AverageTime1 * 10) + 0.5) / 10);
151   OS.PadToColumn(20);
152   tryChangeColor(OS, Entry.CyclesSpentInSQWhileReady, Executions, BufferSize);
153   OS << format("%.1f", floor((AverageTime2 * 10) + 0.5) / 10);
154   OS.PadToColumn(27);
155   tryChangeColor(OS, Entry.CyclesSpentAfterWBAndBeforeRetire, Executions,
156                  STI.getSchedModel().MicroOpBufferSize);
157   OS << format("%.1f", floor((AverageTime3 * 10) + 0.5) / 10);
158 
159   if (OS.has_colors())
160     OS.resetColor();
161   OS.PadToColumn(34);
162 }
163 
164 void TimelineView::printAverageWaitTimes(raw_ostream &OS) const {
165   std::string Header =
166       "\n\nAverage Wait times (based on the timeline view):\n"
167       "[0]: Executions\n"
168       "[1]: Average time spent waiting in a scheduler's queue\n"
169       "[2]: Average time spent waiting in a scheduler's queue while ready\n"
170       "[3]: Average time elapsed from WB until retire stage\n\n"
171       "      [0]    [1]    [2]    [3]\n";
172   OS << Header;
173 
174   // Use a different string stream for printing instructions.
175   std::string Instruction;
176   raw_string_ostream InstrStream(Instruction);
177 
178   formatted_raw_ostream FOS(OS);
179   unsigned Executions = Timeline.size() / AsmSequence.size();
180   for (unsigned I = 0, E = WaitTime.size(); I < E; ++I) {
181     printWaitTimeEntry(FOS, WaitTime[I], I, Executions);
182     // Append the instruction info at the end of the line.
183     const MCInst &Inst = AsmSequence.getMCInstFromIndex(I);
184 
185     MCIP.printInst(&Inst, InstrStream, "", STI);
186     InstrStream.flush();
187 
188     // Consume any tabs or spaces at the beginning of the string.
189     StringRef Str(Instruction);
190     Str = Str.ltrim();
191     FOS << "   " << Str << '\n';
192     FOS.flush();
193     Instruction = "";
194   }
195 }
196 
197 void TimelineView::printTimelineViewEntry(formatted_raw_ostream &OS,
198                                           const TimelineViewEntry &Entry,
199                                           unsigned Iteration,
200                                           unsigned SourceIndex) const {
201   if (Iteration == 0 && SourceIndex == 0)
202     OS << '\n';
203   OS << '[' << Iteration << ',' << SourceIndex << ']';
204   OS.PadToColumn(10);
205   assert(Entry.CycleDispatched >= 0 && "Invalid TimelineViewEntry!");
206   unsigned CycleDispatched = static_cast<unsigned>(Entry.CycleDispatched);
207   for (unsigned I = 0, E = CycleDispatched; I < E; ++I)
208     OS << ((I % 5 == 0) ? '.' : ' ');
209   OS << TimelineView::DisplayChar::Dispatched;
210   if (CycleDispatched != Entry.CycleExecuted) {
211     // Zero latency instructions have the same value for CycleDispatched,
212     // CycleIssued and CycleExecuted.
213     for (unsigned I = CycleDispatched + 1, E = Entry.CycleIssued; I < E; ++I)
214       OS << TimelineView::DisplayChar::Waiting;
215     if (Entry.CycleIssued == Entry.CycleExecuted)
216       OS << TimelineView::DisplayChar::DisplayChar::Executed;
217     else {
218       if (CycleDispatched != Entry.CycleIssued)
219         OS << TimelineView::DisplayChar::Executing;
220       for (unsigned I = Entry.CycleIssued + 1, E = Entry.CycleExecuted; I < E;
221            ++I)
222         OS << TimelineView::DisplayChar::Executing;
223       OS << TimelineView::DisplayChar::Executed;
224     }
225   }
226 
227   for (unsigned I = Entry.CycleExecuted + 1, E = Entry.CycleRetired; I < E; ++I)
228     OS << TimelineView::DisplayChar::RetireLag;
229   OS << TimelineView::DisplayChar::Retired;
230 
231   // Skip other columns.
232   for (unsigned I = Entry.CycleRetired + 1, E = LastCycle; I <= E; ++I)
233     OS << ((I % 5 == 0 || I == LastCycle) ? '.' : ' ');
234 }
235 
236 static void printTimelineHeader(formatted_raw_ostream &OS, unsigned Cycles) {
237   OS << "\n\nTimeline view:\n";
238   if (Cycles >= 10) {
239     OS.PadToColumn(10);
240     for (unsigned I = 0; I <= Cycles; ++I) {
241       if (((I / 10) & 1) == 0)
242         OS << ' ';
243       else
244         OS << I % 10;
245     }
246     OS << '\n';
247   }
248 
249   OS << "Index";
250   OS.PadToColumn(10);
251   for (unsigned I = 0; I <= Cycles; ++I) {
252     if (((I / 10) & 1) == 0)
253       OS << I % 10;
254     else
255       OS << ' ';
256   }
257   OS << '\n';
258 }
259 
260 void TimelineView::printTimeline(raw_ostream &OS) const {
261   formatted_raw_ostream FOS(OS);
262   printTimelineHeader(FOS, LastCycle);
263   FOS.flush();
264 
265   // Use a different string stream for the instruction.
266   std::string Instruction;
267   raw_string_ostream InstrStream(Instruction);
268 
269   for (unsigned I = 0, E = Timeline.size(); I < E; ++I) {
270     const TimelineViewEntry &Entry = Timeline[I];
271     if (Entry.CycleRetired == 0)
272       return;
273 
274     unsigned Iteration = I / AsmSequence.size();
275     unsigned SourceIndex = I % AsmSequence.size();
276     printTimelineViewEntry(FOS, Entry, Iteration, SourceIndex);
277     // Append the instruction info at the end of the line.
278     const MCInst &Inst = AsmSequence.getMCInstFromIndex(I);
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 }
290 } // namespace mca
291