Home | History | Annotate | Download | only in llvm-mca
      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 //===----------------------------------------------------------------------===//
     14 
     15 #include "SchedulerStatistics.h"
     16 #include "llvm/Support/Format.h"
     17 
     18 using namespace llvm;
     19 
     20 namespace mca {
     21 
     22 void SchedulerStatistics::onEvent(const HWInstructionEvent &Event) {
     23   if (Event.Type == HWInstructionEvent::Issued)
     24     ++NumIssued;
     25 }
     26 
     27 void SchedulerStatistics::onReservedBuffers(ArrayRef<unsigned> Buffers) {
     28   for (const unsigned Buffer : Buffers) {
     29     if (BufferedResources.find(Buffer) != BufferedResources.end()) {
     30       BufferUsage &BU = BufferedResources[Buffer];
     31       BU.SlotsInUse++;
     32       BU.MaxUsedSlots = std::max(BU.MaxUsedSlots, BU.SlotsInUse);
     33       continue;
     34     }
     35 
     36     BufferedResources.insert(
     37         std::pair<unsigned, BufferUsage>(Buffer, {1U, 1U}));
     38   }
     39 }
     40 
     41 void SchedulerStatistics::onReleasedBuffers(ArrayRef<unsigned> Buffers) {
     42   for (const unsigned Buffer : Buffers) {
     43     assert(BufferedResources.find(Buffer) != BufferedResources.end() &&
     44            "Buffered resource not in map?");
     45     BufferUsage &BU = BufferedResources[Buffer];
     46     BU.SlotsInUse--;
     47   }
     48 }
     49 
     50 void SchedulerStatistics::printSchedulerStatistics(
     51     llvm::raw_ostream &OS) const {
     52   std::string Buffer;
     53   raw_string_ostream TempStream(Buffer);
     54   TempStream << "\n\nSchedulers - number of cycles where we saw N instructions "
     55                 "issued:\n";
     56   TempStream << "[# issued], [# cycles]\n";
     57   for (const std::pair<unsigned, unsigned> &Entry : IssuedPerCycle) {
     58     TempStream << " " << Entry.first << ",          " << Entry.second << "  ("
     59                << format("%.1f", ((double)Entry.second / NumCycles) * 100)
     60                << "%)\n";
     61   }
     62 
     63   TempStream.flush();
     64   OS << Buffer;
     65 }
     66 
     67 void SchedulerStatistics::printSchedulerUsage(raw_ostream &OS) const {
     68   std::string Buffer;
     69   raw_string_ostream TempStream(Buffer);
     70   TempStream << "\n\nScheduler's queue usage:\n";
     71   // Early exit if no buffered resources were consumed.
     72   if (BufferedResources.empty()) {
     73     TempStream << "No scheduler resources used.\n";
     74     TempStream.flush();
     75     OS << Buffer;
     76     return;
     77   }
     78 
     79   for (unsigned I = 0, E = SM.getNumProcResourceKinds(); I < E; ++I) {
     80     const MCProcResourceDesc &ProcResource = *SM.getProcResource(I);
     81     if (ProcResource.BufferSize <= 0)
     82       continue;
     83 
     84     const auto It = BufferedResources.find(I);
     85     unsigned MaxUsedSlots =
     86         It == BufferedResources.end() ? 0 : It->second.MaxUsedSlots;
     87     TempStream << ProcResource.Name << ",  " << MaxUsedSlots << '/'
     88                << ProcResource.BufferSize << '\n';
     89   }
     90 
     91   TempStream.flush();
     92   OS << Buffer;
     93 }
     94 } // namespace mca
     95