1 //===--------------------- SchedulerStatistics.cpp --------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// This file implements the SchedulerStatistics interface.
13 //===----------------------------------------------------------------------===//
15 #include "Views/SchedulerStatistics.h"
16 #include "llvm/Support/Format.h"
17 #include "llvm/Support/FormattedStream.h"
23 void SchedulerStatistics::onEvent(const HWInstructionEvent
&Event
) {
24 if (Event
.Type
== HWInstructionEvent::Issued
)
28 void SchedulerStatistics::onReservedBuffers(const InstRef
& /* unused */,
29 ArrayRef
<unsigned> Buffers
) {
30 for (const unsigned Buffer
: Buffers
) {
31 BufferUsage
&BU
= Usage
[Buffer
];
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
]++;
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";
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
];
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";
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";
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)
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
;
105 if (HasColors
&& NormalizedAvg
>= NormalizedThreshold
)
106 FOS
.changeColor(raw_ostream::YELLOW
, true, false);
107 FOS
<< NormalizedAvg
;
112 BU
.MaxUsedSlots
== static_cast<unsigned>(ProcResource
.BufferSize
))
113 FOS
.changeColor(raw_ostream::RED
, true, false);
114 FOS
<< BU
.MaxUsedSlots
;
118 FOS
<< ProcResource
.BufferSize
<< '\n';
124 void SchedulerStatistics::printView(raw_ostream
&OS
) const {
125 printSchedulerStats(OS
);
126 printSchedulerUsage(OS
);