xref: /netbsd-src/external/apache2/llvm/dist/llvm/tools/llvm-mca/Views/RetireControlUnitStatistics.cpp (revision 82d56013d7b633d116a93943de88e08335357a7c)
17330f729Sjoerg //===--------------------- RetireControlUnitStatistics.cpp ------*- C++ -*-===//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg /// \file
97330f729Sjoerg ///
107330f729Sjoerg /// This file implements the RetireControlUnitStatistics interface.
117330f729Sjoerg ///
127330f729Sjoerg //===----------------------------------------------------------------------===//
137330f729Sjoerg 
147330f729Sjoerg #include "Views/RetireControlUnitStatistics.h"
157330f729Sjoerg #include "llvm/Support/Format.h"
167330f729Sjoerg 
177330f729Sjoerg namespace llvm {
187330f729Sjoerg namespace mca {
197330f729Sjoerg 
RetireControlUnitStatistics(const MCSchedModel & SM)207330f729Sjoerg RetireControlUnitStatistics::RetireControlUnitStatistics(const MCSchedModel &SM)
217330f729Sjoerg     : NumRetired(0), NumCycles(0), EntriesInUse(0), MaxUsedEntries(0),
227330f729Sjoerg       SumOfUsedEntries(0) {
237330f729Sjoerg   TotalROBEntries = SM.MicroOpBufferSize;
247330f729Sjoerg   if (SM.hasExtraProcessorInfo()) {
257330f729Sjoerg     const MCExtraProcessorInfo &EPI = SM.getExtraProcessorInfo();
267330f729Sjoerg     if (EPI.ReorderBufferSize)
277330f729Sjoerg       TotalROBEntries = EPI.ReorderBufferSize;
287330f729Sjoerg   }
297330f729Sjoerg }
307330f729Sjoerg 
onEvent(const HWInstructionEvent & Event)317330f729Sjoerg void RetireControlUnitStatistics::onEvent(const HWInstructionEvent &Event) {
327330f729Sjoerg   if (Event.Type == HWInstructionEvent::Dispatched) {
337330f729Sjoerg     unsigned NumEntries =
347330f729Sjoerg         static_cast<const HWInstructionDispatchedEvent &>(Event).MicroOpcodes;
357330f729Sjoerg     EntriesInUse += NumEntries;
367330f729Sjoerg   }
377330f729Sjoerg 
387330f729Sjoerg   if (Event.Type == HWInstructionEvent::Retired) {
397330f729Sjoerg     unsigned ReleasedEntries = Event.IR.getInstruction()->getDesc().NumMicroOps;
407330f729Sjoerg     assert(EntriesInUse >= ReleasedEntries && "Invalid internal state!");
417330f729Sjoerg     EntriesInUse -= ReleasedEntries;
427330f729Sjoerg     ++NumRetired;
437330f729Sjoerg   }
447330f729Sjoerg }
457330f729Sjoerg 
onCycleEnd()467330f729Sjoerg void RetireControlUnitStatistics::onCycleEnd() {
477330f729Sjoerg   // Update histogram
487330f729Sjoerg   RetiredPerCycle[NumRetired]++;
497330f729Sjoerg   NumRetired = 0;
507330f729Sjoerg   ++NumCycles;
517330f729Sjoerg   MaxUsedEntries = std::max(MaxUsedEntries, EntriesInUse);
527330f729Sjoerg   SumOfUsedEntries += EntriesInUse;
537330f729Sjoerg }
547330f729Sjoerg 
printView(raw_ostream & OS) const557330f729Sjoerg void RetireControlUnitStatistics::printView(raw_ostream &OS) const {
567330f729Sjoerg   std::string Buffer;
577330f729Sjoerg   raw_string_ostream TempStream(Buffer);
587330f729Sjoerg   TempStream << "\n\nRetire Control Unit - "
597330f729Sjoerg              << "number of cycles where we saw N instructions retired:\n";
607330f729Sjoerg   TempStream << "[# retired], [# cycles]\n";
617330f729Sjoerg 
62*82d56013Sjoerg   for (const std::pair<const unsigned, unsigned> &Entry : RetiredPerCycle) {
637330f729Sjoerg     TempStream << " " << Entry.first;
647330f729Sjoerg     if (Entry.first < 10)
657330f729Sjoerg       TempStream << ",           ";
667330f729Sjoerg     else
677330f729Sjoerg       TempStream << ",          ";
687330f729Sjoerg     TempStream << Entry.second << "  ("
697330f729Sjoerg                << format("%.1f", ((double)Entry.second / NumCycles) * 100.0)
707330f729Sjoerg                << "%)\n";
717330f729Sjoerg   }
727330f729Sjoerg 
737330f729Sjoerg   unsigned AvgUsage = (double)SumOfUsedEntries / NumCycles;
747330f729Sjoerg   double MaxUsagePercentage = ((double)MaxUsedEntries / TotalROBEntries) * 100.0;
757330f729Sjoerg   double NormalizedMaxPercentage = floor((MaxUsagePercentage * 10) + 0.5) / 10;
767330f729Sjoerg   double AvgUsagePercentage = ((double)AvgUsage / TotalROBEntries) * 100.0;
777330f729Sjoerg   double NormalizedAvgPercentage = floor((AvgUsagePercentage * 10) + 0.5) / 10;
787330f729Sjoerg 
797330f729Sjoerg   TempStream << "\nTotal ROB Entries:                " << TotalROBEntries
807330f729Sjoerg              << "\nMax Used ROB Entries:             " << MaxUsedEntries
817330f729Sjoerg              << format("  ( %.1f%% )", NormalizedMaxPercentage)
827330f729Sjoerg              << "\nAverage Used ROB Entries per cy:  " << AvgUsage
837330f729Sjoerg              << format("  ( %.1f%% )\n", NormalizedAvgPercentage);
847330f729Sjoerg 
857330f729Sjoerg   TempStream.flush();
867330f729Sjoerg   OS << Buffer;
877330f729Sjoerg }
887330f729Sjoerg 
897330f729Sjoerg } // namespace mca
907330f729Sjoerg } // namespace llvm
91