xref: /llvm-project/llvm/tools/llvm-mca/Views/DispatchStatistics.cpp (revision 14f77576c9c4f502267a92992abe3bdcbeb96b2c)
1*14f77576SMarcos Horro //===--------------------- DispatchStatistics.cpp ---------------*- C++ -*-===//
210aa09f0SMatt Davis //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
610aa09f0SMatt Davis //
710aa09f0SMatt Davis //===----------------------------------------------------------------------===//
810aa09f0SMatt Davis /// \file
910aa09f0SMatt Davis ///
1010aa09f0SMatt Davis /// This file implements the DispatchStatistics interface.
1110aa09f0SMatt Davis ///
1210aa09f0SMatt Davis //===----------------------------------------------------------------------===//
1310aa09f0SMatt Davis 
1410aa09f0SMatt Davis #include "Views/DispatchStatistics.h"
1510aa09f0SMatt Davis #include "llvm/Support/Format.h"
1610aa09f0SMatt Davis 
175a8fd657SFangrui Song namespace llvm {
1810aa09f0SMatt Davis namespace mca {
1910aa09f0SMatt Davis 
onEvent(const HWStallEvent & Event)2010aa09f0SMatt Davis void DispatchStatistics::onEvent(const HWStallEvent &Event) {
2110aa09f0SMatt Davis   if (Event.Type < HWStallEvent::LastGenericEvent)
2210aa09f0SMatt Davis     HWStalls[Event.Type]++;
2310aa09f0SMatt Davis }
2410aa09f0SMatt Davis 
onEvent(const HWInstructionEvent & Event)2510aa09f0SMatt Davis void DispatchStatistics::onEvent(const HWInstructionEvent &Event) {
268b647dcfSAndrea Di Biagio   if (Event.Type != HWInstructionEvent::Dispatched)
278b647dcfSAndrea Di Biagio     return;
288b647dcfSAndrea Di Biagio 
298b647dcfSAndrea Di Biagio   const auto &DE = static_cast<const HWInstructionDispatchedEvent &>(Event);
308b647dcfSAndrea Di Biagio   NumDispatched += DE.MicroOpcodes;
3110aa09f0SMatt Davis }
3210aa09f0SMatt Davis 
printDispatchHistogram(raw_ostream & OS) const33db158be6SAndrea Di Biagio void DispatchStatistics::printDispatchHistogram(raw_ostream &OS) const {
3410aa09f0SMatt Davis   std::string Buffer;
3510aa09f0SMatt Davis   raw_string_ostream TempStream(Buffer);
3610aa09f0SMatt Davis   TempStream << "\n\nDispatch Logic - "
378b647dcfSAndrea Di Biagio              << "number of cycles where we saw N micro opcodes dispatched:\n";
3810aa09f0SMatt Davis   TempStream << "[# dispatched], [# cycles]\n";
39536c9a60SMark de Wever   for (const std::pair<const unsigned, unsigned> &Entry :
40536c9a60SMark de Wever        DispatchGroupSizePerCycle) {
418b647dcfSAndrea Di Biagio     double Percentage = ((double)Entry.second / NumCycles) * 100.0;
4210aa09f0SMatt Davis     TempStream << " " << Entry.first << ",              " << Entry.second
438b647dcfSAndrea Di Biagio                << "  (" << format("%.1f", floor((Percentage * 10) + 0.5) / 10)
4410aa09f0SMatt Davis                << "%)\n";
4510aa09f0SMatt Davis   }
4610aa09f0SMatt Davis 
4710aa09f0SMatt Davis   TempStream.flush();
4810aa09f0SMatt Davis   OS << Buffer;
4910aa09f0SMatt Davis }
5010aa09f0SMatt Davis 
printStalls(raw_ostream & OS,unsigned NumStalls,unsigned NumCycles)518b647dcfSAndrea Di Biagio static void printStalls(raw_ostream &OS, unsigned NumStalls,
528b647dcfSAndrea Di Biagio                         unsigned NumCycles) {
538b647dcfSAndrea Di Biagio   if (!NumStalls) {
548b647dcfSAndrea Di Biagio     OS << NumStalls;
558b647dcfSAndrea Di Biagio     return;
568b647dcfSAndrea Di Biagio   }
578b647dcfSAndrea Di Biagio 
588b647dcfSAndrea Di Biagio   double Percentage = ((double)NumStalls / NumCycles) * 100.0;
598b647dcfSAndrea Di Biagio   OS << NumStalls << "  ("
608b647dcfSAndrea Di Biagio      << format("%.1f", floor((Percentage * 10) + 0.5) / 10) << "%)";
618b647dcfSAndrea Di Biagio }
628b647dcfSAndrea Di Biagio 
printDispatchStalls(raw_ostream & OS) const6310aa09f0SMatt Davis void DispatchStatistics::printDispatchStalls(raw_ostream &OS) const {
6410aa09f0SMatt Davis   std::string Buffer;
658b647dcfSAndrea Di Biagio   raw_string_ostream SS(Buffer);
668b647dcfSAndrea Di Biagio   SS << "\n\nDynamic Dispatch Stall Cycles:\n";
678b647dcfSAndrea Di Biagio   SS << "RAT     - Register unavailable:                      ";
688b647dcfSAndrea Di Biagio   printStalls(SS, HWStalls[HWStallEvent::RegisterFileStall], NumCycles);
698b647dcfSAndrea Di Biagio   SS << "\nRCU     - Retire tokens unavailable:                 ";
708b647dcfSAndrea Di Biagio   printStalls(SS, HWStalls[HWStallEvent::RetireControlUnitStall], NumCycles);
718b647dcfSAndrea Di Biagio   SS << "\nSCHEDQ  - Scheduler full:                            ";
728b647dcfSAndrea Di Biagio   printStalls(SS, HWStalls[HWStallEvent::SchedulerQueueFull], NumCycles);
738b647dcfSAndrea Di Biagio   SS << "\nLQ      - Load queue full:                           ";
748b647dcfSAndrea Di Biagio   printStalls(SS, HWStalls[HWStallEvent::LoadQueueFull], NumCycles);
758b647dcfSAndrea Di Biagio   SS << "\nSQ      - Store queue full:                          ";
768b647dcfSAndrea Di Biagio   printStalls(SS, HWStalls[HWStallEvent::StoreQueueFull], NumCycles);
778b647dcfSAndrea Di Biagio   SS << "\nGROUP   - Static restrictions on the dispatch group: ";
788b647dcfSAndrea Di Biagio   printStalls(SS, HWStalls[HWStallEvent::DispatchGroupStall], NumCycles);
79ef16c8eaSPatrick Holland   SS << "\nUSH     - Uncategorised Structural Hazard:           ";
80ef16c8eaSPatrick Holland   printStalls(SS, HWStalls[HWStallEvent::CustomBehaviourStall], NumCycles);
818b647dcfSAndrea Di Biagio   SS << '\n';
828b647dcfSAndrea Di Biagio   SS.flush();
8310aa09f0SMatt Davis   OS << Buffer;
8410aa09f0SMatt Davis }
8510aa09f0SMatt Davis 
toJSON() const86aa13e4feSMarcos Horro json::Value DispatchStatistics::toJSON() const {
87aa13e4feSMarcos Horro   json::Object JO({{"RAT", HWStalls[HWStallEvent::RegisterFileStall]},
88aa13e4feSMarcos Horro                    {"RCU", HWStalls[HWStallEvent::RetireControlUnitStall]},
89aa13e4feSMarcos Horro                    {"SCHEDQ", HWStalls[HWStallEvent::SchedulerQueueFull]},
90aa13e4feSMarcos Horro                    {"LQ", HWStalls[HWStallEvent::LoadQueueFull]},
91aa13e4feSMarcos Horro                    {"SQ", HWStalls[HWStallEvent::StoreQueueFull]},
92aa13e4feSMarcos Horro                    {"GROUP", HWStalls[HWStallEvent::DispatchGroupStall]},
93aa13e4feSMarcos Horro                    {"USH", HWStalls[HWStallEvent::CustomBehaviourStall]}});
94aa13e4feSMarcos Horro   return JO;
95aa13e4feSMarcos Horro }
96aa13e4feSMarcos Horro 
9710aa09f0SMatt Davis } // namespace mca
985a8fd657SFangrui Song } // namespace llvm
99