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