[llvm-exegesis] Implements a cache of Instruction objects.
[llvm-core.git] / tools / llvm-mca / Views / SchedulerStatistics.cpp
blob958b3b548f474d161a612a2748ee5d93ff401502
1 //===--------------------- SchedulerStatistics.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 SchedulerStatistics interface.
12 ///
13 //===----------------------------------------------------------------------===//
15 #include "Views/SchedulerStatistics.h"
16 #include "llvm/Support/Format.h"
17 #include "llvm/Support/FormattedStream.h"
19 using namespace llvm;
21 namespace mca {
23 void SchedulerStatistics::onEvent(const HWInstructionEvent &Event) {
24 if (Event.Type == HWInstructionEvent::Issued)
25 ++NumIssued;
28 void SchedulerStatistics::onReservedBuffers(const InstRef & /* unused */,
29 ArrayRef<unsigned> Buffers) {
30 for (const unsigned Buffer : Buffers) {
31 BufferUsage &BU = Usage[Buffer];
32 BU.SlotsInUse++;
33 BU.MaxUsedSlots = std::max(BU.MaxUsedSlots, BU.SlotsInUse);
37 void SchedulerStatistics::onReleasedBuffers(const InstRef & /* unused */,
38 ArrayRef<unsigned> Buffers) {
39 for (const unsigned Buffer : Buffers)
40 Usage[Buffer].SlotsInUse--;
43 void SchedulerStatistics::updateHistograms() {
44 for (BufferUsage &BU : Usage)
45 BU.CumulativeNumUsedSlots += BU.SlotsInUse;
46 IssuedPerCycle[NumIssued]++;
47 NumIssued = 0;
50 void SchedulerStatistics::printSchedulerStats(raw_ostream &OS) const {
51 OS << "\n\nSchedulers - "
52 << "number of cycles where we saw N instructions issued:\n";
53 OS << "[# issued], [# cycles]\n";
55 const auto It =
56 std::max_element(IssuedPerCycle.begin(), IssuedPerCycle.end());
57 unsigned Index = std::distance(IssuedPerCycle.begin(), It);
59 bool HasColors = OS.has_colors();
60 for (unsigned I = 0, E = IssuedPerCycle.size(); I < E; ++I) {
61 unsigned IPC = IssuedPerCycle[I];
62 if (!IPC)
63 continue;
65 if (I == Index && HasColors)
66 OS.changeColor(raw_ostream::SAVEDCOLOR, true, false);
68 OS << " " << I << ", " << IPC << " ("
69 << format("%.1f", ((double)IPC / NumCycles) * 100) << "%)\n";
70 if (HasColors)
71 OS.resetColor();
75 void SchedulerStatistics::printSchedulerUsage(raw_ostream &OS) const {
76 assert(NumCycles && "Unexpected number of cycles!");
78 OS << "\nScheduler's queue usage:\n";
79 if (all_of(Usage, [](const BufferUsage &BU) { return !BU.MaxUsedSlots; })) {
80 OS << "No scheduler resources used.\n";
81 return;
84 OS << "[1] Resource name.\n"
85 << "[2] Average number of used buffer entries.\n"
86 << "[3] Maximum number of used buffer entries.\n"
87 << "[4] Total number of buffer entries.\n\n"
88 << " [1] [2] [3] [4]\n";
90 formatted_raw_ostream FOS(OS);
91 bool HasColors = FOS.has_colors();
92 for (unsigned I = 0, E = SM.getNumProcResourceKinds(); I < E; ++I) {
93 const MCProcResourceDesc &ProcResource = *SM.getProcResource(I);
94 if (ProcResource.BufferSize <= 0)
95 continue;
97 const BufferUsage &BU = Usage[I];
98 double AvgUsage = (double)BU.CumulativeNumUsedSlots / NumCycles;
99 double AlmostFullThreshold = (double)(ProcResource.BufferSize * 4) / 5;
100 unsigned NormalizedAvg = floor((AvgUsage * 10) + 0.5) / 10;
101 unsigned NormalizedThreshold = floor((AlmostFullThreshold * 10) + 0.5) / 10;
103 FOS << ProcResource.Name;
104 FOS.PadToColumn(17);
105 if (HasColors && NormalizedAvg >= NormalizedThreshold)
106 FOS.changeColor(raw_ostream::YELLOW, true, false);
107 FOS << NormalizedAvg;
108 if (HasColors)
109 FOS.resetColor();
110 FOS.PadToColumn(28);
111 if (HasColors &&
112 BU.MaxUsedSlots == static_cast<unsigned>(ProcResource.BufferSize))
113 FOS.changeColor(raw_ostream::RED, true, false);
114 FOS << BU.MaxUsedSlots;
115 if (HasColors)
116 FOS.resetColor();
117 FOS.PadToColumn(39);
118 FOS << ProcResource.BufferSize << '\n';
121 FOS.flush();
124 void SchedulerStatistics::printView(raw_ostream &OS) const {
125 printSchedulerStats(OS);
126 printSchedulerUsage(OS);
129 } // namespace mca