xref: /llvm-project/llvm/tools/llvm-mca/Views/RetireControlUnitStatistics.cpp (revision 07a8255a7879451269960276113f97c98c7f0cfc)
1 //===--------------------- RetireControlUnitStatistics.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 /// \file
10 ///
11 /// This file implements the RetireControlUnitStatistics interface.
12 ///
13 //===----------------------------------------------------------------------===//
14 
15 #include "Views/RetireControlUnitStatistics.h"
16 #include "llvm/Support/Format.h"
17 
18 namespace llvm {
19 namespace mca {
20 
21 RetireControlUnitStatistics::RetireControlUnitStatistics(const MCSchedModel &SM)
22     : NumRetired(0), NumCycles(0), EntriesInUse(0), MaxUsedEntries(0),
23       SumOfUsedEntries(0) {
24   TotalROBEntries = SM.MicroOpBufferSize;
25   if (SM.hasExtraProcessorInfo()) {
26     const MCExtraProcessorInfo &EPI = SM.getExtraProcessorInfo();
27     if (EPI.ReorderBufferSize)
28       TotalROBEntries = EPI.ReorderBufferSize;
29   }
30 }
31 
32 void RetireControlUnitStatistics::onEvent(const HWInstructionEvent &Event) {
33   if (Event.Type == HWInstructionEvent::Dispatched) {
34     unsigned NumEntries =
35         static_cast<const HWInstructionDispatchedEvent &>(Event).MicroOpcodes;
36     EntriesInUse += NumEntries;
37   }
38 
39   if (Event.Type == HWInstructionEvent::Retired) {
40     unsigned ReleasedEntries = Event.IR.getInstruction()->getDesc().NumMicroOps;
41     assert(EntriesInUse >= ReleasedEntries && "Invalid internal state!");
42     EntriesInUse -= ReleasedEntries;
43     ++NumRetired;
44   }
45 }
46 
47 void RetireControlUnitStatistics::onCycleEnd() {
48   // Update histogram
49   RetiredPerCycle[NumRetired]++;
50   NumRetired = 0;
51   ++NumCycles;
52   MaxUsedEntries = std::max(MaxUsedEntries, EntriesInUse);
53   SumOfUsedEntries += EntriesInUse;
54 }
55 
56 void RetireControlUnitStatistics::printView(raw_ostream &OS) const {
57   std::string Buffer;
58   raw_string_ostream TempStream(Buffer);
59   TempStream << "\n\nRetire Control Unit - "
60              << "number of cycles where we saw N instructions retired:\n";
61   TempStream << "[# retired], [# cycles]\n";
62 
63   for (const std::pair<unsigned, unsigned> &Entry : RetiredPerCycle) {
64     TempStream << " " << Entry.first;
65     if (Entry.first < 10)
66       TempStream << ",           ";
67     else
68       TempStream << ",          ";
69     TempStream << Entry.second << "  ("
70                << format("%.1f", ((double)Entry.second / NumCycles) * 100.0)
71                << "%)\n";
72   }
73 
74   unsigned AvgUsage = (double)SumOfUsedEntries / NumCycles;
75   double MaxUsagePercentage = ((double)MaxUsedEntries / TotalROBEntries) * 100.0;
76   double NormalizedMaxPercentage = floor((MaxUsagePercentage * 10) + 0.5) / 10;
77   double AvgUsagePercentage = ((double)AvgUsage / TotalROBEntries) * 100.0;
78   double NormalizedAvgPercentage = floor((AvgUsagePercentage * 10) + 0.5) / 10;
79 
80   TempStream << "\nTotal ROB Entries:                " << TotalROBEntries
81              << "\nMax Used ROB Entries:             " << MaxUsedEntries
82              << format("  ( %.1f%% )", NormalizedMaxPercentage)
83              << "\nAverage Used ROB Entries per cy:  " << AvgUsage
84              << format("  ( %.1f%% )\n", NormalizedAvgPercentage);
85 
86   TempStream.flush();
87   OS << Buffer;
88 }
89 
90 } // namespace mca
91 } // namespace llvm
92