xref: /freebsd/contrib/llvm-project/llvm/tools/llvm-mca/Views/DispatchStatistics.cpp (revision fe6060f10f634930ff71b7c50291ddc610da2475)
1*fe6060f1SDimitry Andric //===--------------------- DispatchStatistics.cpp ---------------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric /// \file
90b57cec5SDimitry Andric ///
100b57cec5SDimitry Andric /// This file implements the DispatchStatistics interface.
110b57cec5SDimitry Andric ///
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric 
140b57cec5SDimitry Andric #include "Views/DispatchStatistics.h"
150b57cec5SDimitry Andric #include "llvm/Support/Format.h"
160b57cec5SDimitry Andric 
170b57cec5SDimitry Andric namespace llvm {
180b57cec5SDimitry Andric namespace mca {
190b57cec5SDimitry Andric 
onEvent(const HWStallEvent & Event)200b57cec5SDimitry Andric void DispatchStatistics::onEvent(const HWStallEvent &Event) {
210b57cec5SDimitry Andric   if (Event.Type < HWStallEvent::LastGenericEvent)
220b57cec5SDimitry Andric     HWStalls[Event.Type]++;
230b57cec5SDimitry Andric }
240b57cec5SDimitry Andric 
onEvent(const HWInstructionEvent & Event)250b57cec5SDimitry Andric void DispatchStatistics::onEvent(const HWInstructionEvent &Event) {
260b57cec5SDimitry Andric   if (Event.Type != HWInstructionEvent::Dispatched)
270b57cec5SDimitry Andric     return;
280b57cec5SDimitry Andric 
290b57cec5SDimitry Andric   const auto &DE = static_cast<const HWInstructionDispatchedEvent &>(Event);
300b57cec5SDimitry Andric   NumDispatched += DE.MicroOpcodes;
310b57cec5SDimitry Andric }
320b57cec5SDimitry Andric 
printDispatchHistogram(raw_ostream & OS) const330b57cec5SDimitry Andric void DispatchStatistics::printDispatchHistogram(raw_ostream &OS) const {
340b57cec5SDimitry Andric   std::string Buffer;
350b57cec5SDimitry Andric   raw_string_ostream TempStream(Buffer);
360b57cec5SDimitry Andric   TempStream << "\n\nDispatch Logic - "
370b57cec5SDimitry Andric              << "number of cycles where we saw N micro opcodes dispatched:\n";
380b57cec5SDimitry Andric   TempStream << "[# dispatched], [# cycles]\n";
39480093f4SDimitry Andric   for (const std::pair<const unsigned, unsigned> &Entry :
40480093f4SDimitry Andric        DispatchGroupSizePerCycle) {
410b57cec5SDimitry Andric     double Percentage = ((double)Entry.second / NumCycles) * 100.0;
420b57cec5SDimitry Andric     TempStream << " " << Entry.first << ",              " << Entry.second
430b57cec5SDimitry Andric                << "  (" << format("%.1f", floor((Percentage * 10) + 0.5) / 10)
440b57cec5SDimitry Andric                << "%)\n";
450b57cec5SDimitry Andric   }
460b57cec5SDimitry Andric 
470b57cec5SDimitry Andric   TempStream.flush();
480b57cec5SDimitry Andric   OS << Buffer;
490b57cec5SDimitry Andric }
500b57cec5SDimitry Andric 
printStalls(raw_ostream & OS,unsigned NumStalls,unsigned NumCycles)510b57cec5SDimitry Andric static void printStalls(raw_ostream &OS, unsigned NumStalls,
520b57cec5SDimitry Andric                         unsigned NumCycles) {
530b57cec5SDimitry Andric   if (!NumStalls) {
540b57cec5SDimitry Andric     OS << NumStalls;
550b57cec5SDimitry Andric     return;
560b57cec5SDimitry Andric   }
570b57cec5SDimitry Andric 
580b57cec5SDimitry Andric   double Percentage = ((double)NumStalls / NumCycles) * 100.0;
590b57cec5SDimitry Andric   OS << NumStalls << "  ("
600b57cec5SDimitry Andric      << format("%.1f", floor((Percentage * 10) + 0.5) / 10) << "%)";
610b57cec5SDimitry Andric }
620b57cec5SDimitry Andric 
printDispatchStalls(raw_ostream & OS) const630b57cec5SDimitry Andric void DispatchStatistics::printDispatchStalls(raw_ostream &OS) const {
640b57cec5SDimitry Andric   std::string Buffer;
650b57cec5SDimitry Andric   raw_string_ostream SS(Buffer);
660b57cec5SDimitry Andric   SS << "\n\nDynamic Dispatch Stall Cycles:\n";
670b57cec5SDimitry Andric   SS << "RAT     - Register unavailable:                      ";
680b57cec5SDimitry Andric   printStalls(SS, HWStalls[HWStallEvent::RegisterFileStall], NumCycles);
690b57cec5SDimitry Andric   SS << "\nRCU     - Retire tokens unavailable:                 ";
700b57cec5SDimitry Andric   printStalls(SS, HWStalls[HWStallEvent::RetireControlUnitStall], NumCycles);
710b57cec5SDimitry Andric   SS << "\nSCHEDQ  - Scheduler full:                            ";
720b57cec5SDimitry Andric   printStalls(SS, HWStalls[HWStallEvent::SchedulerQueueFull], NumCycles);
730b57cec5SDimitry Andric   SS << "\nLQ      - Load queue full:                           ";
740b57cec5SDimitry Andric   printStalls(SS, HWStalls[HWStallEvent::LoadQueueFull], NumCycles);
750b57cec5SDimitry Andric   SS << "\nSQ      - Store queue full:                          ";
760b57cec5SDimitry Andric   printStalls(SS, HWStalls[HWStallEvent::StoreQueueFull], NumCycles);
770b57cec5SDimitry Andric   SS << "\nGROUP   - Static restrictions on the dispatch group: ";
780b57cec5SDimitry Andric   printStalls(SS, HWStalls[HWStallEvent::DispatchGroupStall], NumCycles);
79*fe6060f1SDimitry Andric   SS << "\nUSH     - Uncategorised Structural Hazard:           ";
80*fe6060f1SDimitry Andric   printStalls(SS, HWStalls[HWStallEvent::CustomBehaviourStall], NumCycles);
810b57cec5SDimitry Andric   SS << '\n';
820b57cec5SDimitry Andric   SS.flush();
830b57cec5SDimitry Andric   OS << Buffer;
840b57cec5SDimitry Andric }
850b57cec5SDimitry Andric 
toJSON() const86*fe6060f1SDimitry Andric json::Value DispatchStatistics::toJSON() const {
87*fe6060f1SDimitry Andric   json::Object JO({{"RAT", HWStalls[HWStallEvent::RegisterFileStall]},
88*fe6060f1SDimitry Andric                    {"RCU", HWStalls[HWStallEvent::RetireControlUnitStall]},
89*fe6060f1SDimitry Andric                    {"SCHEDQ", HWStalls[HWStallEvent::SchedulerQueueFull]},
90*fe6060f1SDimitry Andric                    {"LQ", HWStalls[HWStallEvent::LoadQueueFull]},
91*fe6060f1SDimitry Andric                    {"SQ", HWStalls[HWStallEvent::StoreQueueFull]},
92*fe6060f1SDimitry Andric                    {"GROUP", HWStalls[HWStallEvent::DispatchGroupStall]},
93*fe6060f1SDimitry Andric                    {"USH", HWStalls[HWStallEvent::CustomBehaviourStall]}});
94*fe6060f1SDimitry Andric   return JO;
95*fe6060f1SDimitry Andric }
96*fe6060f1SDimitry Andric 
970b57cec5SDimitry Andric } // namespace mca
980b57cec5SDimitry Andric } // namespace llvm
99