1//===--------------------- SchedulerStatistics.h ----------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8/// \file
9///
10/// This file defines class SchedulerStatistics. Class SchedulerStatistics is a
11/// View that listens to instruction issue events in order to print general
12/// statistics related to the hardware schedulers.
13///
14/// Example:
15/// ========
16///
17/// Schedulers - number of cycles where we saw N instructions issued:
18/// [# issued], [# cycles]
19/// 0, 6 (2.9%)
20/// 1, 106 (50.7%)
21/// 2, 97 (46.4%)
22///
23/// Scheduler's queue usage:
24/// [1] Resource name.
25/// [2] Average number of used buffer entries.
26/// [3] Maximum number of used buffer entries.
27/// [4] Total number of buffer entries.
28///
29/// [1] [2] [3] [4]
30/// JALU01 0 0 20
31/// JFPU01 15 18 18
32/// JLSAGU 0 0 12
33//
34//===----------------------------------------------------------------------===//
35
36#ifndef LLVM_TOOLS_LLVM_MCA_SCHEDULERSTATISTICS_H
37#define LLVM_TOOLS_LLVM_MCA_SCHEDULERSTATISTICS_H
38
39#include "llvm/ADT/SmallVector.h"
40#include "llvm/MC/MCSubtargetInfo.h"
41#include "llvm/MCA/View.h"
42#include <map>
43
44namespace llvm {
45namespace mca {
46
47class SchedulerStatistics final : public View {
48 const llvm::MCSchedModel &SM;
49 float ReservationStationScaleFactor;
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 using Histogram = std::map<unsigned, unsigned>;
67 Histogram IssueWidthPerCycle;
68
69 std::vector<BufferUsage> Usage;
70
71 void updateHistograms();
72 void printSchedulerStats(llvm::raw_ostream &OS) const;
73 void printSchedulerUsage(llvm::raw_ostream &OS) const;
74
75public:
76 SchedulerStatistics(const llvm::MCSubtargetInfo &STI,
77 float ReservationStationScaleFactor);
78 void onEvent(const HWInstructionEvent &Event) override;
79 void onCycleBegin() override { NumCycles++; }
80 void onCycleEnd() override { updateHistograms(); }
81
82 // Increases the number of used scheduler queue slots of every buffered
83 // resource in the Buffers set.
84 void onReservedBuffers(const InstRef &IR,
85 llvm::ArrayRef<unsigned> Buffers) override;
86
87 // Decreases by one the number of used scheduler queue slots of every
88 // buffered resource in the Buffers set.
89 void onReleasedBuffers(const InstRef &IR,
90 llvm::ArrayRef<unsigned> Buffers) override;
91
92 void printView(llvm::raw_ostream &OS) const override;
93 StringRef getNameAsString() const override { return "SchedulerStatistics"; }
94 bool isSerializable() const override { return false; }
95};
96} // namespace mca
97} // namespace llvm
98
99#endif
100