TimelineView.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 ? std::numeric_limits<unsigned>::max() : Cycles),
  22. LastCycle(0), WaitTime(S.size()), 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. if (CurrentCycle > TVEntry.CycleExecuted) {
  70. WTEntry.CyclesSpentAfterWBAndBeforeRetire +=
  71. (CurrentCycle - 1) - TVEntry.CycleExecuted;
  72. }
  73. break;
  74. }
  75. case HWInstructionEvent::Ready:
  76. Timeline[Index].CycleReady = CurrentCycle;
  77. break;
  78. case HWInstructionEvent::Issued:
  79. Timeline[Index].CycleIssued = CurrentCycle;
  80. break;
  81. case HWInstructionEvent::Executed:
  82. Timeline[Index].CycleExecuted = CurrentCycle;
  83. break;
  84. case HWInstructionEvent::Dispatched:
  85. // There may be multiple dispatch events. Microcoded instructions that are
  86. // expanded into multiple uOps may require multiple dispatch cycles. Here,
  87. // we want to capture the first dispatch cycle.
  88. if (Timeline[Index].CycleDispatched == -1)
  89. Timeline[Index].CycleDispatched = static_cast<int>(CurrentCycle);
  90. break;
  91. default:
  92. return;
  93. }
  94. if (CurrentCycle < MaxCycle)
  95. LastCycle = std::max(LastCycle, CurrentCycle);
  96. }
  97. static raw_ostream::Colors chooseColor(unsigned CumulativeCycles,
  98. unsigned Executions, int BufferSize) {
  99. if (CumulativeCycles && BufferSize < 0)
  100. return raw_ostream::MAGENTA;
  101. unsigned Size = static_cast<unsigned>(BufferSize);
  102. if (CumulativeCycles >= Size * Executions)
  103. return raw_ostream::RED;
  104. if ((CumulativeCycles * 2) >= Size * Executions)
  105. return raw_ostream::YELLOW;
  106. return raw_ostream::SAVEDCOLOR;
  107. }
  108. static void tryChangeColor(raw_ostream &OS, unsigned Cycles,
  109. unsigned Executions, int BufferSize) {
  110. if (!OS.has_colors())
  111. return;
  112. raw_ostream::Colors Color = chooseColor(Cycles, Executions, BufferSize);
  113. if (Color == raw_ostream::SAVEDCOLOR) {
  114. OS.resetColor();
  115. return;
  116. }
  117. OS.changeColor(Color, /* bold */ true, /* BG */ false);
  118. }
  119. void TimelineView::printWaitTimeEntry(formatted_raw_ostream &OS,
  120. const WaitTimeEntry &Entry,
  121. unsigned SourceIndex,
  122. unsigned Executions) const {
  123. bool PrintingTotals = SourceIndex == getSource().size();
  124. unsigned CumulativeExecutions = PrintingTotals ? Timeline.size() : Executions;
  125. if (!PrintingTotals)
  126. OS << SourceIndex << '.';
  127. OS.PadToColumn(7);
  128. double AverageTime1, AverageTime2, AverageTime3;
  129. AverageTime1 =
  130. (double)(Entry.CyclesSpentInSchedulerQueue * 10) / CumulativeExecutions;
  131. AverageTime2 =
  132. (double)(Entry.CyclesSpentInSQWhileReady * 10) / CumulativeExecutions;
  133. AverageTime3 = (double)(Entry.CyclesSpentAfterWBAndBeforeRetire * 10) /
  134. CumulativeExecutions;
  135. OS << Executions;
  136. OS.PadToColumn(13);
  137. int BufferSize = PrintingTotals ? 0 : UsedBuffer[SourceIndex].second;
  138. if (!PrintingTotals)
  139. tryChangeColor(OS, Entry.CyclesSpentInSchedulerQueue, CumulativeExecutions,
  140. BufferSize);
  141. OS << format("%.1f", floor(AverageTime1 + 0.5) / 10);
  142. OS.PadToColumn(20);
  143. if (!PrintingTotals)
  144. tryChangeColor(OS, Entry.CyclesSpentInSQWhileReady, CumulativeExecutions,
  145. BufferSize);
  146. OS << format("%.1f", floor(AverageTime2 + 0.5) / 10);
  147. OS.PadToColumn(27);
  148. if (!PrintingTotals)
  149. tryChangeColor(OS, Entry.CyclesSpentAfterWBAndBeforeRetire,
  150. CumulativeExecutions,
  151. getSubTargetInfo().getSchedModel().MicroOpBufferSize);
  152. OS << format("%.1f", floor(AverageTime3 + 0.5) / 10);
  153. if (OS.has_colors())
  154. OS.resetColor();
  155. OS.PadToColumn(34);
  156. }
  157. void TimelineView::printAverageWaitTimes(raw_ostream &OS) const {
  158. std::string Header =
  159. "\n\nAverage Wait times (based on the timeline view):\n"
  160. "[0]: Executions\n"
  161. "[1]: Average time spent waiting in a scheduler's queue\n"
  162. "[2]: Average time spent waiting in a scheduler's queue while ready\n"
  163. "[3]: Average time elapsed from WB until retire stage\n\n"
  164. " [0] [1] [2] [3]\n";
  165. OS << Header;
  166. formatted_raw_ostream FOS(OS);
  167. unsigned Executions = Timeline.size() / getSource().size();
  168. unsigned IID = 0;
  169. for (const MCInst &Inst : getSource()) {
  170. printWaitTimeEntry(FOS, WaitTime[IID], IID, Executions);
  171. FOS << " " << printInstructionString(Inst) << '\n';
  172. FOS.flush();
  173. ++IID;
  174. }
  175. // If the timeline contains more than one instruction,
  176. // let's also print global averages.
  177. if (getSource().size() != 1) {
  178. WaitTimeEntry TotalWaitTime = std::accumulate(
  179. WaitTime.begin(), WaitTime.end(), WaitTimeEntry{0, 0, 0},
  180. [](const WaitTimeEntry &A, const WaitTimeEntry &B) {
  181. return WaitTimeEntry{
  182. A.CyclesSpentInSchedulerQueue + B.CyclesSpentInSchedulerQueue,
  183. A.CyclesSpentInSQWhileReady + B.CyclesSpentInSQWhileReady,
  184. A.CyclesSpentAfterWBAndBeforeRetire +
  185. B.CyclesSpentAfterWBAndBeforeRetire};
  186. });
  187. printWaitTimeEntry(FOS, TotalWaitTime, IID, Executions);
  188. FOS << " "
  189. << "<total>" << '\n';
  190. FOS.flush();
  191. }
  192. }
  193. void TimelineView::printTimelineViewEntry(formatted_raw_ostream &OS,
  194. const TimelineViewEntry &Entry,
  195. unsigned Iteration,
  196. unsigned SourceIndex) const {
  197. if (Iteration == 0 && SourceIndex == 0)
  198. OS << '\n';
  199. OS << '[' << Iteration << ',' << SourceIndex << ']';
  200. OS.PadToColumn(10);
  201. assert(Entry.CycleDispatched >= 0 && "Invalid TimelineViewEntry!");
  202. unsigned CycleDispatched = static_cast<unsigned>(Entry.CycleDispatched);
  203. for (unsigned I = 0, E = CycleDispatched; I < E; ++I)
  204. OS << ((I % 5 == 0) ? '.' : ' ');
  205. OS << TimelineView::DisplayChar::Dispatched;
  206. if (CycleDispatched != Entry.CycleExecuted) {
  207. // Zero latency instructions have the same value for CycleDispatched,
  208. // CycleIssued and CycleExecuted.
  209. for (unsigned I = CycleDispatched + 1, E = Entry.CycleIssued; I < E; ++I)
  210. OS << TimelineView::DisplayChar::Waiting;
  211. if (Entry.CycleIssued == Entry.CycleExecuted)
  212. OS << TimelineView::DisplayChar::DisplayChar::Executed;
  213. else {
  214. if (CycleDispatched != Entry.CycleIssued)
  215. OS << TimelineView::DisplayChar::Executing;
  216. for (unsigned I = Entry.CycleIssued + 1, E = Entry.CycleExecuted; I < E;
  217. ++I)
  218. OS << TimelineView::DisplayChar::Executing;
  219. OS << TimelineView::DisplayChar::Executed;
  220. }
  221. }
  222. for (unsigned I = Entry.CycleExecuted + 1, E = Entry.CycleRetired; I < E; ++I)
  223. OS << TimelineView::DisplayChar::RetireLag;
  224. if (Entry.CycleExecuted < Entry.CycleRetired)
  225. OS << TimelineView::DisplayChar::Retired;
  226. // Skip other columns.
  227. for (unsigned I = Entry.CycleRetired + 1, E = LastCycle; I <= E; ++I)
  228. OS << ((I % 5 == 0 || I == LastCycle) ? '.' : ' ');
  229. }
  230. static void printTimelineHeader(formatted_raw_ostream &OS, unsigned Cycles) {
  231. OS << "\n\nTimeline view:\n";
  232. if (Cycles >= 10) {
  233. OS.PadToColumn(10);
  234. for (unsigned I = 0; I <= Cycles; ++I) {
  235. if (((I / 10) & 1) == 0)
  236. OS << ' ';
  237. else
  238. OS << I % 10;
  239. }
  240. OS << '\n';
  241. }
  242. OS << "Index";
  243. OS.PadToColumn(10);
  244. for (unsigned I = 0; I <= Cycles; ++I) {
  245. if (((I / 10) & 1) == 0)
  246. OS << I % 10;
  247. else
  248. OS << ' ';
  249. }
  250. OS << '\n';
  251. }
  252. void TimelineView::printTimeline(raw_ostream &OS) const {
  253. formatted_raw_ostream FOS(OS);
  254. printTimelineHeader(FOS, LastCycle);
  255. FOS.flush();
  256. unsigned IID = 0;
  257. ArrayRef<llvm::MCInst> Source = getSource();
  258. const unsigned Iterations = Timeline.size() / Source.size();
  259. for (unsigned Iteration = 0; Iteration < Iterations; ++Iteration) {
  260. for (const MCInst &Inst : Source) {
  261. const TimelineViewEntry &Entry = Timeline[IID];
  262. // When an instruction is retired after timeline-max-cycles,
  263. // its CycleRetired is left at 0. However, it's possible for
  264. // a 0 latency instruction to be retired during cycle 0 and we
  265. // don't want to early exit in that case. The CycleExecuted
  266. // attribute is set correctly whether or not it is greater
  267. // than timeline-max-cycles so we can use that to ensure
  268. // we don't early exit because of a 0 latency instruction.
  269. if (Entry.CycleRetired == 0 && Entry.CycleExecuted != 0) {
  270. FOS << "Truncated display due to cycle limit\n";
  271. return;
  272. }
  273. unsigned SourceIndex = IID % Source.size();
  274. printTimelineViewEntry(FOS, Entry, Iteration, SourceIndex);
  275. FOS << " " << printInstructionString(Inst) << '\n';
  276. FOS.flush();
  277. ++IID;
  278. }
  279. }
  280. }
  281. json::Value TimelineView::toJSON() const {
  282. json::Array TimelineInfo;
  283. for (const TimelineViewEntry &TLE : Timeline) {
  284. TimelineInfo.push_back(
  285. json::Object({{"CycleDispatched", TLE.CycleDispatched},
  286. {"CycleReady", TLE.CycleReady},
  287. {"CycleIssued", TLE.CycleIssued},
  288. {"CycleExecuted", TLE.CycleExecuted},
  289. {"CycleRetired", TLE.CycleRetired}}));
  290. }
  291. return json::Object({{"TimelineInfo", std::move(TimelineInfo)}});
  292. }
  293. } // namespace mca
  294. } // namespace llvm