109467b48Spatrick //===--------------------- SchedulerStatistics.cpp --------------*- C++ -*-===//
209467b48Spatrick //
309467b48Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
409467b48Spatrick // See https://llvm.org/LICENSE.txt for license information.
509467b48Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
609467b48Spatrick //
709467b48Spatrick //===----------------------------------------------------------------------===//
809467b48Spatrick /// \file
909467b48Spatrick ///
1009467b48Spatrick /// This file implements the SchedulerStatistics interface.
1109467b48Spatrick ///
1209467b48Spatrick //===----------------------------------------------------------------------===//
1309467b48Spatrick
1409467b48Spatrick #include "Views/SchedulerStatistics.h"
1509467b48Spatrick #include "llvm/Support/Format.h"
1609467b48Spatrick #include "llvm/Support/FormattedStream.h"
1709467b48Spatrick
1809467b48Spatrick namespace llvm {
1909467b48Spatrick namespace mca {
2009467b48Spatrick
SchedulerStatistics(const llvm::MCSubtargetInfo & STI)2109467b48Spatrick SchedulerStatistics::SchedulerStatistics(const llvm::MCSubtargetInfo &STI)
2209467b48Spatrick : SM(STI.getSchedModel()), LQResourceID(0), SQResourceID(0), NumIssued(0),
2309467b48Spatrick NumCycles(0), MostRecentLoadDispatched(~0U),
2409467b48Spatrick MostRecentStoreDispatched(~0U),
2509467b48Spatrick Usage(STI.getSchedModel().NumProcResourceKinds, {0, 0, 0}) {
2609467b48Spatrick if (SM.hasExtraProcessorInfo()) {
2709467b48Spatrick const MCExtraProcessorInfo &EPI = SM.getExtraProcessorInfo();
2809467b48Spatrick LQResourceID = EPI.LoadQueueID;
2909467b48Spatrick SQResourceID = EPI.StoreQueueID;
3009467b48Spatrick }
3109467b48Spatrick }
3209467b48Spatrick
3309467b48Spatrick // FIXME: This implementation works under the assumption that load/store queue
3409467b48Spatrick // entries are reserved at 'instruction dispatched' stage, and released at
3509467b48Spatrick // 'instruction executed' stage. This currently matches the behavior of LSUnit.
3609467b48Spatrick //
3709467b48Spatrick // The current design minimizes the number of events generated by the
3809467b48Spatrick // Dispatch/Execute stages, at the cost of doing extra bookkeeping in method
3909467b48Spatrick // `onEvent`. However, it introduces a subtle dependency between this view and
4009467b48Spatrick // how the LSUnit works.
4109467b48Spatrick //
4209467b48Spatrick // In future we should add a new "memory queue" event type, so that we stop
4309467b48Spatrick // making assumptions on how LSUnit internally works (See PR39828).
onEvent(const HWInstructionEvent & Event)4409467b48Spatrick void SchedulerStatistics::onEvent(const HWInstructionEvent &Event) {
4509467b48Spatrick if (Event.Type == HWInstructionEvent::Issued) {
4609467b48Spatrick const Instruction &Inst = *Event.IR.getInstruction();
4709467b48Spatrick NumIssued += Inst.getDesc().NumMicroOps;
4809467b48Spatrick } else if (Event.Type == HWInstructionEvent::Dispatched) {
4909467b48Spatrick const Instruction &Inst = *Event.IR.getInstruction();
5009467b48Spatrick const unsigned Index = Event.IR.getSourceIndex();
51*d415bd75Srobert if (LQResourceID && Inst.getMayLoad() &&
5209467b48Spatrick MostRecentLoadDispatched != Index) {
5309467b48Spatrick Usage[LQResourceID].SlotsInUse++;
5409467b48Spatrick MostRecentLoadDispatched = Index;
5509467b48Spatrick }
56*d415bd75Srobert if (SQResourceID && Inst.getMayStore() &&
5709467b48Spatrick MostRecentStoreDispatched != Index) {
5809467b48Spatrick Usage[SQResourceID].SlotsInUse++;
5909467b48Spatrick MostRecentStoreDispatched = Index;
6009467b48Spatrick }
6109467b48Spatrick } else if (Event.Type == HWInstructionEvent::Executed) {
6209467b48Spatrick const Instruction &Inst = *Event.IR.getInstruction();
63*d415bd75Srobert if (LQResourceID && Inst.getMayLoad()) {
6409467b48Spatrick assert(Usage[LQResourceID].SlotsInUse);
6509467b48Spatrick Usage[LQResourceID].SlotsInUse--;
6609467b48Spatrick }
67*d415bd75Srobert if (SQResourceID && Inst.getMayStore()) {
6809467b48Spatrick assert(Usage[SQResourceID].SlotsInUse);
6909467b48Spatrick Usage[SQResourceID].SlotsInUse--;
7009467b48Spatrick }
7109467b48Spatrick }
7209467b48Spatrick }
7309467b48Spatrick
onReservedBuffers(const InstRef &,ArrayRef<unsigned> Buffers)7409467b48Spatrick void SchedulerStatistics::onReservedBuffers(const InstRef & /* unused */,
7509467b48Spatrick ArrayRef<unsigned> Buffers) {
7609467b48Spatrick for (const unsigned Buffer : Buffers) {
7709467b48Spatrick if (Buffer == LQResourceID || Buffer == SQResourceID)
7809467b48Spatrick continue;
7909467b48Spatrick Usage[Buffer].SlotsInUse++;
8009467b48Spatrick }
8109467b48Spatrick }
8209467b48Spatrick
onReleasedBuffers(const InstRef &,ArrayRef<unsigned> Buffers)8309467b48Spatrick void SchedulerStatistics::onReleasedBuffers(const InstRef & /* unused */,
8409467b48Spatrick ArrayRef<unsigned> Buffers) {
8509467b48Spatrick for (const unsigned Buffer : Buffers) {
8609467b48Spatrick if (Buffer == LQResourceID || Buffer == SQResourceID)
8709467b48Spatrick continue;
8809467b48Spatrick Usage[Buffer].SlotsInUse--;
8909467b48Spatrick }
9009467b48Spatrick }
9109467b48Spatrick
updateHistograms()9209467b48Spatrick void SchedulerStatistics::updateHistograms() {
9309467b48Spatrick for (BufferUsage &BU : Usage) {
9409467b48Spatrick BU.CumulativeNumUsedSlots += BU.SlotsInUse;
9509467b48Spatrick BU.MaxUsedSlots = std::max(BU.MaxUsedSlots, BU.SlotsInUse);
9609467b48Spatrick }
9709467b48Spatrick
9809467b48Spatrick IssueWidthPerCycle[NumIssued]++;
9909467b48Spatrick NumIssued = 0;
10009467b48Spatrick }
10109467b48Spatrick
printSchedulerStats(raw_ostream & OS) const10209467b48Spatrick void SchedulerStatistics::printSchedulerStats(raw_ostream &OS) const {
10309467b48Spatrick OS << "\n\nSchedulers - "
10409467b48Spatrick << "number of cycles where we saw N micro opcodes issued:\n";
10509467b48Spatrick OS << "[# issued], [# cycles]\n";
10609467b48Spatrick
10709467b48Spatrick bool HasColors = OS.has_colors();
10809467b48Spatrick const auto It =
10909467b48Spatrick std::max_element(IssueWidthPerCycle.begin(), IssueWidthPerCycle.end());
11009467b48Spatrick for (const std::pair<const unsigned, unsigned> &Entry : IssueWidthPerCycle) {
11109467b48Spatrick unsigned NumIssued = Entry.first;
11209467b48Spatrick if (NumIssued == It->first && HasColors)
11309467b48Spatrick OS.changeColor(raw_ostream::SAVEDCOLOR, true, false);
11409467b48Spatrick
11509467b48Spatrick unsigned IPC = Entry.second;
11609467b48Spatrick OS << " " << NumIssued << ", " << IPC << " ("
11709467b48Spatrick << format("%.1f", ((double)IPC / NumCycles) * 100) << "%)\n";
11809467b48Spatrick if (HasColors)
11909467b48Spatrick OS.resetColor();
12009467b48Spatrick }
12109467b48Spatrick }
12209467b48Spatrick
printSchedulerUsage(raw_ostream & OS) const12309467b48Spatrick void SchedulerStatistics::printSchedulerUsage(raw_ostream &OS) const {
12409467b48Spatrick assert(NumCycles && "Unexpected number of cycles!");
12509467b48Spatrick
12609467b48Spatrick OS << "\nScheduler's queue usage:\n";
12709467b48Spatrick if (all_of(Usage, [](const BufferUsage &BU) { return !BU.MaxUsedSlots; })) {
12809467b48Spatrick OS << "No scheduler resources used.\n";
12909467b48Spatrick return;
13009467b48Spatrick }
13109467b48Spatrick
13209467b48Spatrick OS << "[1] Resource name.\n"
13309467b48Spatrick << "[2] Average number of used buffer entries.\n"
13409467b48Spatrick << "[3] Maximum number of used buffer entries.\n"
13509467b48Spatrick << "[4] Total number of buffer entries.\n\n"
13609467b48Spatrick << " [1] [2] [3] [4]\n";
13709467b48Spatrick
13809467b48Spatrick formatted_raw_ostream FOS(OS);
13909467b48Spatrick bool HasColors = FOS.has_colors();
14009467b48Spatrick for (unsigned I = 0, E = SM.getNumProcResourceKinds(); I < E; ++I) {
14109467b48Spatrick const MCProcResourceDesc &ProcResource = *SM.getProcResource(I);
14209467b48Spatrick if (ProcResource.BufferSize <= 0)
14309467b48Spatrick continue;
14409467b48Spatrick
14509467b48Spatrick const BufferUsage &BU = Usage[I];
14609467b48Spatrick double AvgUsage = (double)BU.CumulativeNumUsedSlots / NumCycles;
14709467b48Spatrick double AlmostFullThreshold = (double)(ProcResource.BufferSize * 4) / 5;
14809467b48Spatrick unsigned NormalizedAvg = floor((AvgUsage * 10) + 0.5) / 10;
14909467b48Spatrick unsigned NormalizedThreshold = floor((AlmostFullThreshold * 10) + 0.5) / 10;
15009467b48Spatrick
15109467b48Spatrick FOS << ProcResource.Name;
15209467b48Spatrick FOS.PadToColumn(17);
15309467b48Spatrick if (HasColors && NormalizedAvg >= NormalizedThreshold)
15409467b48Spatrick FOS.changeColor(raw_ostream::YELLOW, true, false);
15509467b48Spatrick FOS << NormalizedAvg;
15609467b48Spatrick if (HasColors)
15709467b48Spatrick FOS.resetColor();
15809467b48Spatrick FOS.PadToColumn(28);
15909467b48Spatrick if (HasColors &&
16009467b48Spatrick BU.MaxUsedSlots == static_cast<unsigned>(ProcResource.BufferSize))
16109467b48Spatrick FOS.changeColor(raw_ostream::RED, true, false);
16209467b48Spatrick FOS << BU.MaxUsedSlots;
16309467b48Spatrick if (HasColors)
16409467b48Spatrick FOS.resetColor();
16509467b48Spatrick FOS.PadToColumn(39);
16609467b48Spatrick FOS << ProcResource.BufferSize << '\n';
16709467b48Spatrick }
16809467b48Spatrick
16909467b48Spatrick FOS.flush();
17009467b48Spatrick }
17109467b48Spatrick
printView(raw_ostream & OS) const17209467b48Spatrick void SchedulerStatistics::printView(raw_ostream &OS) const {
17309467b48Spatrick printSchedulerStats(OS);
17409467b48Spatrick printSchedulerUsage(OS);
17509467b48Spatrick }
17609467b48Spatrick
17709467b48Spatrick } // namespace mca
17809467b48Spatrick } // namespace llvm
179