1 //===--------------------- SchedulerStatistics.h ----------------*- 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 defines class SchedulerStatistics. Class SchedulerStatistics is a
12 /// View that listens to instruction issue events in order to print general
13 /// statistics related to the hardware schedulers.
14 ///
15 /// Example:
16 /// ========
17 ///
18 /// Schedulers - number of cycles where we saw N instructions issued:
19 /// [# issued], [# cycles]
20 ///  0,          6  (2.9%)
21 ///  1,          106  (50.7%)
22 ///  2,          97  (46.4%)
23 ///
24 /// Scheduler's queue usage:
25 /// [1] Resource name.
26 /// [2] Average number of used buffer entries.
27 /// [3] Maximum number of used buffer entries.
28 /// [4] Total number of buffer entries.
29 ///
30 ///  [1]            [2]        [3]        [4]
31 /// JALU01           0          0          20
32 /// JFPU01           15         18         18
33 /// JLSAGU           0          0          12
34 //
35 //===----------------------------------------------------------------------===//
36 
37 #ifndef LLVM_TOOLS_LLVM_MCA_SCHEDULERSTATISTICS_H
38 #define LLVM_TOOLS_LLVM_MCA_SCHEDULERSTATISTICS_H
39 
40 #include "Views/View.h"
41 #include "llvm/ADT/SmallVector.h"
42 #include "llvm/MC/MCSubtargetInfo.h"
43 #include <map>
44 
45 namespace llvm {
46 namespace mca {
47 
48 class SchedulerStatistics final : public View {
49   const llvm::MCSchedModel &SM;
50   unsigned LQResourceID;
51   unsigned SQResourceID;
52 
53   unsigned NumIssued;
54   unsigned NumCycles;
55 
56   unsigned MostRecentLoadDispatched;
57   unsigned MostRecentStoreDispatched;
58 
59   // Tracks the usage of a scheduler's queue.
60   struct BufferUsage {
61     unsigned SlotsInUse;
62     unsigned MaxUsedSlots;
63     uint64_t CumulativeNumUsedSlots;
64   };
65 
66   std::vector<unsigned> IssuedPerCycle;
67   std::vector<BufferUsage> Usage;
68 
69   void updateHistograms();
70   void printSchedulerStats(llvm::raw_ostream &OS) const;
71   void printSchedulerUsage(llvm::raw_ostream &OS) const;
72 
73 public:
74   SchedulerStatistics(const llvm::MCSubtargetInfo &STI);
75   void onEvent(const HWInstructionEvent &Event) override;
onCycleBegin()76   void onCycleBegin() override { NumCycles++; }
onCycleEnd()77   void onCycleEnd() override { updateHistograms(); }
78 
79   // Increases the number of used scheduler queue slots of every buffered
80   // resource in the Buffers set.
81   void onReservedBuffers(const InstRef &IR,
82                          llvm::ArrayRef<unsigned> Buffers) override;
83 
84   // Decreases by one the number of used scheduler queue slots of every
85   // buffered resource in the Buffers set.
86   void onReleasedBuffers(const InstRef &IR,
87                          llvm::ArrayRef<unsigned> Buffers) override;
88 
89   void printView(llvm::raw_ostream &OS) const override;
90 };
91 } // namespace mca
92 } // namespace llvm
93 
94 #endif
95