TimelineView.cpp 11 KB

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