xref: /freebsd-src/contrib/llvm-project/llvm/tools/llvm-mca/Views/SchedulerStatistics.h (revision 349cc55c9796c4596a5b9904cd3281af295f878f)
10b57cec5SDimitry Andric //===--------------------- SchedulerStatistics.h ----------------*- 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 defines class SchedulerStatistics. Class SchedulerStatistics is a
110b57cec5SDimitry Andric /// View that listens to instruction issue events in order to print general
120b57cec5SDimitry Andric /// statistics related to the hardware schedulers.
130b57cec5SDimitry Andric ///
140b57cec5SDimitry Andric /// Example:
150b57cec5SDimitry Andric /// ========
160b57cec5SDimitry Andric ///
170b57cec5SDimitry Andric /// Schedulers - number of cycles where we saw N instructions issued:
180b57cec5SDimitry Andric /// [# issued], [# cycles]
190b57cec5SDimitry Andric ///  0,          6  (2.9%)
200b57cec5SDimitry Andric ///  1,          106  (50.7%)
210b57cec5SDimitry Andric ///  2,          97  (46.4%)
220b57cec5SDimitry Andric ///
230b57cec5SDimitry Andric /// Scheduler's queue usage:
240b57cec5SDimitry Andric /// [1] Resource name.
250b57cec5SDimitry Andric /// [2] Average number of used buffer entries.
260b57cec5SDimitry Andric /// [3] Maximum number of used buffer entries.
270b57cec5SDimitry Andric /// [4] Total number of buffer entries.
280b57cec5SDimitry Andric ///
290b57cec5SDimitry Andric ///  [1]            [2]        [3]        [4]
300b57cec5SDimitry Andric /// JALU01           0          0          20
310b57cec5SDimitry Andric /// JFPU01           15         18         18
320b57cec5SDimitry Andric /// JLSAGU           0          0          12
330b57cec5SDimitry Andric //
340b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
350b57cec5SDimitry Andric 
360b57cec5SDimitry Andric #ifndef LLVM_TOOLS_LLVM_MCA_SCHEDULERSTATISTICS_H
370b57cec5SDimitry Andric #define LLVM_TOOLS_LLVM_MCA_SCHEDULERSTATISTICS_H
380b57cec5SDimitry Andric 
390b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h"
400b57cec5SDimitry Andric #include "llvm/MC/MCSubtargetInfo.h"
41*349cc55cSDimitry Andric #include "llvm/MCA/View.h"
420b57cec5SDimitry Andric #include <map>
430b57cec5SDimitry Andric 
440b57cec5SDimitry Andric namespace llvm {
450b57cec5SDimitry Andric namespace mca {
460b57cec5SDimitry Andric 
470b57cec5SDimitry Andric class SchedulerStatistics final : public View {
480b57cec5SDimitry Andric   const llvm::MCSchedModel &SM;
490b57cec5SDimitry Andric   unsigned LQResourceID;
500b57cec5SDimitry Andric   unsigned SQResourceID;
510b57cec5SDimitry Andric 
520b57cec5SDimitry Andric   unsigned NumIssued;
530b57cec5SDimitry Andric   unsigned NumCycles;
540b57cec5SDimitry Andric 
550b57cec5SDimitry Andric   unsigned MostRecentLoadDispatched;
560b57cec5SDimitry Andric   unsigned MostRecentStoreDispatched;
570b57cec5SDimitry Andric 
580b57cec5SDimitry Andric   // Tracks the usage of a scheduler's queue.
590b57cec5SDimitry Andric   struct BufferUsage {
600b57cec5SDimitry Andric     unsigned SlotsInUse;
610b57cec5SDimitry Andric     unsigned MaxUsedSlots;
620b57cec5SDimitry Andric     uint64_t CumulativeNumUsedSlots;
630b57cec5SDimitry Andric   };
640b57cec5SDimitry Andric 
650b57cec5SDimitry Andric   using Histogram = std::map<unsigned, unsigned>;
660b57cec5SDimitry Andric   Histogram IssueWidthPerCycle;
670b57cec5SDimitry Andric 
680b57cec5SDimitry Andric   std::vector<BufferUsage> Usage;
690b57cec5SDimitry Andric 
700b57cec5SDimitry Andric   void updateHistograms();
710b57cec5SDimitry Andric   void printSchedulerStats(llvm::raw_ostream &OS) const;
720b57cec5SDimitry Andric   void printSchedulerUsage(llvm::raw_ostream &OS) const;
730b57cec5SDimitry Andric 
740b57cec5SDimitry Andric public:
750b57cec5SDimitry Andric   SchedulerStatistics(const llvm::MCSubtargetInfo &STI);
760b57cec5SDimitry Andric   void onEvent(const HWInstructionEvent &Event) override;
onCycleBegin()770b57cec5SDimitry Andric   void onCycleBegin() override { NumCycles++; }
onCycleEnd()780b57cec5SDimitry Andric   void onCycleEnd() override { updateHistograms(); }
790b57cec5SDimitry Andric 
800b57cec5SDimitry Andric   // Increases the number of used scheduler queue slots of every buffered
810b57cec5SDimitry Andric   // resource in the Buffers set.
820b57cec5SDimitry Andric   void onReservedBuffers(const InstRef &IR,
830b57cec5SDimitry Andric                          llvm::ArrayRef<unsigned> Buffers) override;
840b57cec5SDimitry Andric 
850b57cec5SDimitry Andric   // Decreases by one the number of used scheduler queue slots of every
860b57cec5SDimitry Andric   // buffered resource in the Buffers set.
870b57cec5SDimitry Andric   void onReleasedBuffers(const InstRef &IR,
880b57cec5SDimitry Andric                          llvm::ArrayRef<unsigned> Buffers) override;
890b57cec5SDimitry Andric 
900b57cec5SDimitry Andric   void printView(llvm::raw_ostream &OS) const override;
getNameAsString()91e8d8bef9SDimitry Andric   StringRef getNameAsString() const override { return "SchedulerStatistics"; }
isSerializable()92fe6060f1SDimitry Andric   bool isSerializable() const override { return false; }
930b57cec5SDimitry Andric };
940b57cec5SDimitry Andric } // namespace mca
950b57cec5SDimitry Andric } // namespace llvm
960b57cec5SDimitry Andric 
970b57cec5SDimitry Andric #endif
98