forked from westerndigitalcorporation/swerv-ISS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPerfRegs.hpp
191 lines (158 loc) · 6.74 KB
/
PerfRegs.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright 2018 Western Digital Corporation or its affiliates.
//
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License along with
// this program. If not, see <https://www.gnu.org/licenses/>.
//
#pragma once
#include <cstdint>
#include <cstddef>
#include <vector>
#include <unordered_map>
#include <type_traits>
#include <cassert>
namespace WdRiscv
{
/// Symbolic names for performance events.
enum class EventNumber
{
None,
ClockActive, // 1: Cycles clock active
ICacheHits, // 2: Instruction cache hits
ICacheMisses, // 3: Instruction cache misses
InstCommited, // 4: Instructions committed
Inst16Commited, // 5: 16-bit instructions committed
Inst32Commited, // 6: 32-bit instructions committed
InstAligned, // 7 4-byte aligned instructions
InstDecode, // 8: Instructions decoded
Mult, // 9: Multiply instructions committed
Div, // 10: Divide instructions committed
Load, // 11: Loads committed
Store, // 12: stores committed
MisalignLoad, // 13: misaligned loads
MisalignStore, // 14: misaligned stores
Alu, // 15: alu instructions committed
CsrRead, // 16: Csr read instructions committed
CsrReadWrite, // 17: Csr read/write instructions committed
CsrWrite, // 18: Csr write instructions committed
Ebreak, // 19: Ebreak instructions committed
Ecall, // 20: Ecall instructions committed
Fence, // 21: Fence instructions committed
Fencei, // 22: Fence.i instructions committed
Mret, // 23: Mret instructions committed
Branch, // 24: Branch instructions committed
BranchMiss, // 25: Mis-predicted branches
BranchTaken, // 26: Taken branches
BranchUnpredict, // 27: Unpredictable branches
FetchStall, // 28: Fetcher stall cycles
AlignStall, // 29: Aligner stall cycles
DecodeStall, // 30: Decoder stall cycles
PostSyncStall, // 31: Post sync stall cycles
PreSynchStall, // 32: Pre sync stall cycles
PipeFrozen, // 33: Cycles pipeline is frozen
StoreStall, // 34: LSU store stalls cycles
DmaDccmStall, // 35: DMA DCCM stall cycles
DmaIccmStall, // 36: DMA ICCM stall cycles
Exception, // 37: Exception count
TimerInterrupt, // 38: Timer interrupts
ExternalInterrupt, // 39: External interrupts
TluFlush, // 40: TLU flushes (flush lower)
TluFlushError, // 41: Branch error flushes
BusFetch, // 42: Fetch bus transactions
BusTransactions, // 43: Load/store bus transactions
BusMisalign, // 44: Misaligned load/store bus transactions
IbusError, // 45: I-bus errors
DbusError, // 46: D-bus errors
IbusBusy, // 47: Cycles stalled due to Ibus busy
DbusBusy, // 48: Cycles stalled due to Dbus busy
InetrruptDisabled, // 49: Cycles interrupts disabled
InterrutpStall, // 50: Cycles interrupts stalled while disabled
Atomic, // 51: Atomic (amo) instruction
Lr, // 52: Load-reserve instruction
Sc, // 53: Store-conditional instruction
Bitmanip, // 54: Bit-manipulation
BusLoad, // 55: Bus load instructions committed
BusStore, // 56: Bus store instructions committed
_End // 57: Non-event serving as count of events
};
template <typename URV>
class CsRegs;
template <typename URV>
class Hart;
/// Model a set of consecutive performance counters. Theses
/// correspond to a set of consecutive performance counter CSR.
class PerfRegs
{
public:
friend class Hart<uint32_t>;
friend class Hart<uint64_t>;
friend class CsRegs<uint32_t>;
friend class CsRegs<uint64_t>;
/// Define numCounters counters. These correspond to mhp
PerfRegs(unsigned numCounters = 0);
/// Configure numCounters counters initialized to zero. This
/// should not be used if some CSR registers are tied to the
/// counters in here.
void config(unsigned numCounters);
/// Update (count-up) all the performance counters currently
/// associated with the given event.
bool updateCounters(EventNumber event, uint32_t perfControl)
{
size_t eventIx = size_t(event);
if (eventIx >= countersOfEvent_.size())
return false;
const auto& counterIndices = countersOfEvent_.at(eventIx);
for (auto counterIx : counterIndices)
{
// Performance counters handeled in here are MHPMCOUNTER3 to
// MHPMCOUNTER31 and they are indexed 0 to 29.
if ((perfControl >> (3+counterIx)) & 1)
{
counters_.at(counterIx)++;
modified_.at(counterIx) = true;
}
}
return true;
}
/// Associate given event number with given counter.
/// Subsequent calls to updatePerofrmanceCounters(en) will cause
/// given counter to count up by 1. Return true on success. Return
/// false if counter number is out of bounds.
bool assignEventToCounter(EventNumber event, unsigned counter);
protected:
/// Unmark registers marked as modified by current instruction. This
/// is done at the end of each instruction.
void clearModified()
{
for (auto& m : modified_)
m = false;
}
/// Return true if given number corresponds to a valid performance
/// counter and if that counter was modified by the current
/// instruction.
bool isModified(unsigned ix)
{
if (ix < modified_.size()) return modified_[ix];
return false;
}
private:
// Map counter index to event currently associated with counter.
std::vector<EventNumber> eventOfCounter_;
// Map an event number to a vector containing the indices of the
// counters currently associated with that event.
std::vector< std::vector<unsigned> > countersOfEvent_;
std::vector<uint64_t> counters_;
std::vector<unsigned> modified_;
};
}