gem5 v25.0.0.1
Loading...
Searching...
No Matches
pmu.hh
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011-2014, 2017-2018, 2022-2023, 2025 Arm Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are
16 * met: redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer;
18 * redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution;
21 * neither the name of the copyright holders nor the names of its
22 * contributors may be used to endorse or promote products derived from
23 * this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#ifndef __ARCH_ARM_PMU_HH__
39#define __ARCH_ARM_PMU_HH__
40
41#include <map>
42#include <memory>
43#include <vector>
44
46#include "arch/arm/system.hh"
47#include "base/cprintf.hh"
48#include "cpu/base.hh"
49#include "debug/PMUVerbose.hh"
50#include "enums/EventTypeId.hh"
52#include "sim/eventq.hh"
53#include "sim/sim_object.hh"
54#include "sim/system.hh"
55
56namespace gem5
57{
58
59struct ArmPMUParams;
60class Platform;
61class ThreadContext;
62class ArmInterruptPin;
63
64namespace ArmISA {
65
66
98class PMU : public SimObject, public ArmISA::BaseISADevice
99{
100 public:
101 using EventTypeId = std::underlying_type_t<enums::EventTypeId>;
102 PMU(const ArmPMUParams &p);
103 ~PMU();
104
105 void addEventProbe(EventTypeId id, SimObject *obj, const char *name);
107
108 void registerEvent(EventTypeId id);
109
110 public: // SimObject and related interfaces
111 void serialize(CheckpointOut &cp) const override;
112 void unserialize(CheckpointIn &cp) override;
113
114 void drainResume() override;
115
116 void regProbeListeners() override;
117
118 public: // ISA Device interface
119 void setThreadContext(ThreadContext *tc) override;
120
127 void setMiscReg(int misc_reg, RegVal val) override;
134 RegVal readMiscReg(int misc_reg) override;
135
136 protected: // PMU register types and constants
138 // PMU Enable
139 Bitfield<0> e;
140 // Event counter reset
141 Bitfield<1> p;
142 // Cycle counter reset
143 Bitfield<2> c;
144 // Cycle counter divider enable
145 Bitfield<3> d;
146 // Export enable
147 Bitfield<4> x;
148 // Disable PMCCNTR when event counting is prohibited
149 Bitfield<5> dp;
150 // Long Cycle counter enable
151 Bitfield<6> lc;
152 // Number of event counters implemented
153 Bitfield<15, 11> n;
154 // Implementation ID
155 Bitfield<23, 16> idcode;
156 // Implementer code
157 Bitfield<31, 24> imp;
159
160 BitUnion32(PMSELR_t)
161 // Performance counter selector
162 Bitfield<4, 0> sel;
163 EndBitUnion(PMSELR_t)
164
165 BitUnion32(PMEVTYPER_t)
166 Bitfield<15, 0> evtCount;
167
168 // Secure EL3 filtering
169 Bitfield<26> m;
170 // Non-secure EL2 mode filtering
171 Bitfield<27> nsh;
172 // Non-secure EL0 mode filtering
173 Bitfield<28> nsu;
174 // Non-secure EL1 mode filtering
175 Bitfield<29> nsk;
176 // EL0 filtering
177 Bitfield<30> u;
178 // EL1 filtering
179 Bitfield<31> p;
180 EndBitUnion(PMEVTYPER_t)
181
189 typedef unsigned int CounterId;
190
192 static const CounterId PMCCNTR = 31;
193
194 protected: /* High-level register and interrupt handling */
196
205 void setControlReg(PMCR_t val);
206
210 void resetEventCounts();
211
215 void raiseInterrupt();
216
220 void clearInterrupt();
221
232 uint64_t getCounterValue(CounterId id) const {
233 return isValidCounter(id) ? getCounter(id).getValue() : 0;
234 }
235
243 void setCounterValue(CounterId id, uint64_t val);
244
256 PMEVTYPER_t getCounterTypeRegister(CounterId id) const;
257
271 void setCounterTypeRegister(CounterId id, PMEVTYPER_t type);
272
283 void setOverflowStatus(RegVal new_val);
284
285 protected: /* Probe handling and counter state */
286 struct CounterState;
287 struct Stats;
288
292 struct PMUEvent
293 {
301 PMUEvent(EventTypeId _id, Stats *pmu_stats)
302 : id(_id), pmuStats(pmu_stats)
303 {}
304
305 virtual ~PMUEvent()
306 {}
307
313 void attachEvent(PMU::CounterState *user);
314
320 void detachEvent(PMU::CounterState *user);
321
329 virtual void increment(const uint64_t val);
330
334 virtual void enable() = 0;
335
339 virtual void disable() = 0;
340
345 virtual void updateAttachedCounters() {}
346
347 protected:
350
353
355 std::set<PMU::CounterState*> userCounters;
356 };
357
358 struct RegularEvent : public PMUEvent
359 {
361
363 std::string name) {
364
365 panic_if(!object,"malformed probe-point"
366 " definition with name %s\n", name);
367
368 microArchitectureEventSet.emplace(object, name);
369 }
370
371 protected:
372 struct RegularProbe : public ProbeListenerArgBase<uint64_t>
373 {
374 RegularProbe(RegularEvent *parent, std::string name)
375 : ProbeListenerArgBase(std::move(name)), parentEvent(parent)
376 {}
377
378 RegularProbe() = delete;
379
380 void notify(const uint64_t &val) override;
381
382 protected:
384 };
385
386 struct CacheProbe : public ProbeListenerArgBase<CacheAccessProbeArg>
387 {
388 CacheProbe(RegularEvent *parent, std::string name)
389 : ProbeListenerArgBase(std::move(name)), parentEvent(parent)
390 {}
391
392 CacheProbe() = delete;
393
394 void notify(const CacheAccessProbeArg &val) override
395 {
396 parentEvent->increment(1);
397 };
398
399 protected:
401 };
402
404 std::set<EventTypeEntry> microArchitectureEventSet;
405
410
411 using PMUEvent::PMUEvent;
412
413 void enable() override;
414
415 void disable() override;
416 };
417
419 {
420 void enable() override {}
421 void disable() override {}
422
423 public:
424 using PMUEvent::PMUEvent;
425
432 void write(uint64_t val);
433 };
434
441 std::shared_ptr<PMUEvent> getEvent(EventTypeId eventId);
442
445 {
446 CounterState(PMU &pmuReference, uint64_t counter_id,
447 const bool is_64_bit)
448 : eventId(0), filter(0), enabled(false),
449 overflow64(is_64_bit), sourceEvent(nullptr),
450 counterId(counter_id), value(0), resetValue(false),
451 pmu(pmuReference) {}
452
453 void serialize(CheckpointOut &cp) const override;
454 void unserialize(CheckpointIn &cp) override;
455
462 uint64_t add(uint64_t delta);
463
464 bool isFiltered() const;
465
469 void detach();
470
476 void attach(const std::shared_ptr<PMUEvent> &event);
477
483 uint64_t getCounterId() const{
484 return counterId;
485 }
486
492 uint64_t getValue() const;
493
499 void setValue(uint64_t val);
500
501 public: /* Serializable state */
504
506 PMEVTYPER_t filter;
507
510
513
514 protected: /* Configuration */
516 std::shared_ptr<PMUEvent> sourceEvent;
517
519 uint64_t counterId;
520
522 uint64_t value;
523
526
528
529 template <typename ...Args>
530 void debugCounter(const char* mainString, Args &...args) const {
531
532 std::string userString = csprintf(mainString, args...);
533
534 warn("[counterId = %d, eventId = %d, sourceEvent = 0x%x] %s",
535 counterId, eventId, sourceEvent, userString.c_str());
536
537 }
538 };
539
548 bool isValidCounter(CounterId id) const {
549 return id < counters.size() || id == PMCCNTR;
550 }
551
559 CounterState &getCounter(CounterId id) {
560 assert(isValidCounter(id));
561 return id == PMCCNTR ? cycleCounter : counters[id];
562 }
563
571 const CounterState &getCounter(CounterId id) const {
572 assert(isValidCounter(id));
573 return id == PMCCNTR ? cycleCounter : counters[id];
574 }
575
587 void updateCounter(CounterState &ctr);
588
595 bool isFiltered(const CounterState &ctr) const;
596
603 void updateAllCounters();
604
605 protected: /* State that needs to be serialized */
608
613 std::set<EventTypeId> statCounters;
614
617
619 PMCR_t reg_pmcr;
620
622 PMSELR_t reg_pmselr;
623
626
629
636 uint64_t reg_pmceid0;
637 uint64_t reg_pmceid1;
638
641
644
647
650
653
655 std::shared_ptr<SWIncrementEvent> swIncrementEvent;
656
657 protected: /* Configuration and constants */
660
663
666
670 std::map<EventTypeId, std::shared_ptr<PMUEvent>> eventMap;
671
676
681
682 struct Stats : public statistics::Group
683 {
684 public:
685 Stats(PMU *parent);
686
688 void add(EventTypeId id, uint64_t value);
689
690 private:
691 void registerEvent(EventTypeId id, const char *stat_name);
692
693 private:
695 std::unordered_map<EventTypeId, statistics::Scalar> map;
697};
698
699} // namespace ArmISA
700} // namespace gem5
701
702#endif
#define BitUnion32(name)
Definition bitunion.hh:495
Base class for devices that use the MiscReg interfaces.
Definition isa_device.hh:62
PMUEvent(EventTypeId _id, Stats *pmu_stats)
PMUEvent constructor.
Definition pmu.hh:301
void enable() override
Enable the current event.
Definition pmu.hh:420
void disable() override
Disable the current event.
Definition pmu.hh:421
void write(uint64_t val)
write on the sw increment register inducing an increment of the counters with this event selected acc...
Definition pmu.cc:865
std::shared_ptr< PMUEvent > getEvent(EventTypeId eventId)
Obtain the event of a given id.
Definition pmu.cc:800
BitUnion32(PMCR_t) Bitfield< 0 > e
void regProbeListeners() override
Register probe listeners for this object.
Definition pmu.cc:184
Bitfield< 31, 24 > imp
Definition pmu.hh:157
void unserialize(CheckpointIn &cp) override
Unserialize an object.
Definition pmu.cc:771
Bitfield< 26 > m
Definition pmu.hh:169
void setControlReg(PMCR_t val)
PMCR write handling.
Definition pmu.cc:421
Bitfield< 30 > u
Definition pmu.hh:177
void raiseInterrupt()
Deliver a PMU interrupt to the GIC.
Definition pmu.cc:722
void addEventProbe(EventTypeId id, SimObject *obj, const char *name)
Definition pmu.cc:138
Bitfield< 5 > dp
Definition pmu.hh:149
PMCR_t reg_pmcr_conf
Constant (configuration-dependent) part of the PMCR.
Definition pmu.hh:659
RegVal readMiscReg(int misc_reg) override
Read a register within the PMU.
Definition pmu.cc:323
RegVal reg_pmcnten
Performance Monitor Count Enable Register.
Definition pmu.hh:616
void clearInterrupt()
Clear a PMU interrupt.
Definition pmu.cc:738
uint64_t maximumCounterCount
The number of regular event counters.
Definition pmu.hh:643
static const RegVal reg_pmcr_wr_mask
PMCR write mask when accessed from the guest.
Definition pmu.hh:662
Bitfield< 2 > c
Definition pmu.hh:143
bool isFiltered(const CounterState &ctr) const
Check if a counter's settings allow it to be counted.
PMEVTYPER_t getCounterTypeRegister(CounterId id) const
Get the type and filter settings of a counter (PMEVTYPER)
Definition pmu.cc:670
unsigned clock_remainder
Remainder part when the clock counter is divided by 64.
Definition pmu.hh:640
uint64_t reg_pmceid1
Definition pmu.hh:637
void updateAllCounters()
Call updateCounter() for each counter in the PMU if the counter's state has changed.
Definition pmu.cc:459
void resetEventCounts()
Reset all event counters excluding the cycle counter to zero.
Definition pmu.cc:650
void setOverflowStatus(RegVal new_val)
Used for writing the Overflow Flag Status Register (SET/CLR)
Definition pmu.cc:708
PMSELR_t reg_pmselr
Performance Monitor Selection Register.
Definition pmu.hh:622
std::map< EventTypeId, std::shared_ptr< PMUEvent > > eventMap
List of event types supported by this PMU.
Definition pmu.hh:670
void addSoftwareIncrementEvent(EventTypeId id)
Definition pmu.cc:114
Bitfield< 29 > nsk
Definition pmu.hh:175
static const CounterId PMCCNTR
Cycle Count Register Number.
Definition pmu.hh:192
void setThreadContext(ThreadContext *tc) override
Definition pmu.cc:104
std::vector< CounterState > counters
State of all general-purpose counters supported by PMU.
Definition pmu.hh:646
void setCounterTypeRegister(CounterId id, PMEVTYPER_t type)
Set the type and filter settings of a performance counter (PMEVTYPER)
Definition pmu.cc:684
Bitfield< 27 > nsh
Definition pmu.hh:171
Bitfield< 3 > d
Definition pmu.hh:145
void drainResume() override
Resume execution after a successful drain.
Definition pmu.cc:177
PMU(const ArmPMUParams &p)
Definition pmu.cc:58
Bitfield< 6 > lc
Definition pmu.hh:151
RegVal readMiscRegInt(int misc_reg)
Definition pmu.cc:332
Bitfield< 15, 11 > n
Definition pmu.hh:153
std::set< EventTypeId > statCounters
Determine whether we merge event counting with the stats framework.
Definition pmu.hh:613
Bitfield< 4 > x
Definition pmu.hh:147
void setMiscReg(int misc_reg, RegVal val) override
Set a register within the PMU.
Definition pmu.cc:208
CounterState cycleCounter
State of the cycle counter.
Definition pmu.hh:649
uint64_t reg_pmceid0
Performance counter ID register.
Definition pmu.hh:636
const EventTypeId cycleCounterEventId
The id of the counter hardwired to the cpu cycle counter.
Definition pmu.hh:652
bool isValidCounter(CounterId id) const
Is this a valid counter ID?
Definition pmu.hh:548
PMCR_t reg_pmcr
Performance Monitor Control Register.
Definition pmu.hh:619
Bitfield< 1 > p
Definition pmu.hh:141
Bitfield< 23, 16 > idcode
Definition pmu.hh:155
RegVal reg_pmovsr
Performance Monitor Overflow Status Register.
Definition pmu.hh:628
Bitfield< 28 > nsu
Definition pmu.hh:173
std::underlying_type_t< enums::EventTypeId > EventTypeId
Definition pmu.hh:101
ArmInterruptPin * interrupt
Performance monitor interrupt number.
Definition pmu.hh:665
void registerEvent(EventTypeId id)
Definition pmu.cc:161
CounterState & getCounter(CounterId id)
Return the state of a counter.
Definition pmu.hh:559
bool use64bitCounters
Determine whether to use 64-bit or 32-bit counters.
Definition pmu.hh:607
std::shared_ptr< SWIncrementEvent > swIncrementEvent
The event that implements the software increment.
Definition pmu.hh:655
uint64_t getCounterValue(CounterId id) const
Get the value of a performance counter.
Definition pmu.hh:232
bool exitOnPMUInterrupt
Exit simloop on PMU interrupt.
Definition pmu.hh:680
gem5::ArmISA::PMU::Stats stats
void setCounterValue(CounterId id, uint64_t val)
Set the value of a performance counter.
Definition pmu.cc:657
EndBitUnion(PMCR_t) BitUnion32(PMSELR_t) Bitfield< 4
RegVal reg_pminten
Performance Monitor Interrupt Enable Register.
Definition pmu.hh:625
void updateCounter(CounterState &ctr)
Depending on counter configuration, add or remove the probes driving the counter.
Definition pmu.cc:626
void serialize(CheckpointOut &cp) const override
Serialize an object.
Definition pmu.cc:750
const CounterState & getCounter(CounterId id) const
Return the state of a counter.
Definition pmu.hh:571
const bool exitOnPMUControl
Exit simloop on PMU reset or disable.
Definition pmu.hh:675
Generic representation of an Arm interrupt pin.
Definition base_gic.hh:200
Information provided to probes on a cache event.
virtual std::string name() const
Definition named.hh:60
ProbeListenerArgBase(std::string name)
Definition probe.hh:226
const std::string name
Definition probe.hh:114
ThreadContext is the external interface to all thread state for anything outside of the CPU.
Statistics container.
Definition group.hh:93
STL pair class.
Definition stl.hh:58
STL vector class.
Definition stl.hh:37
#define panic_if(cond,...)
Conditional panic macro that checks the supplied condition and only panics if the condition is true a...
Definition logging.hh:246
SimObject(const Params &p)
Definition sim_object.cc:58
#define warn(...)
Definition logging.hh:288
Bitfield< 9 > e
Definition misc_types.hh:65
Bitfield< 33 > id
Bitfield< 10, 5 > event
Bitfield< 63 > val
Definition misc.hh:804
Copyright (c) 2024 Arm Limited All rights reserved.
Definition binary32.hh:36
uint64_t RegVal
Definition types.hh:173
std::ostream CheckpointOut
Definition serialize.hh:66
std::string csprintf(const char *format, const Args &...args)
Definition cprintf.hh:161
Overload hash function for BasicBlockRange type.
Definition binary32.hh:81
State of a counter within the PMU.
Definition pmu.hh:445
bool resetValue
Flag keeping track if the counter has been reset.
Definition pmu.hh:525
void attach(const std::shared_ptr< PMUEvent > &event)
Attach this counter to an event.
Definition pmu.cc:589
PMEVTYPER_t filter
Filtering settings (evtCount is unused)
Definition pmu.hh:506
uint64_t value
Current value of the counter.
Definition pmu.hh:522
bool overflow64
Is this a 64-bit counter?
Definition pmu.hh:512
uint64_t counterId
id of the counter instance
Definition pmu.hh:519
void detach()
Detach the counter from its event.
Definition pmu.cc:576
EventTypeId eventId
Counter event ID.
Definition pmu.hh:503
void unserialize(CheckpointIn &cp) override
Unserialize an object.
Definition pmu.cc:821
CounterState(PMU &pmuReference, uint64_t counter_id, const bool is_64_bit)
Definition pmu.hh:446
uint64_t getCounterId() const
Obtain the counter id.
Definition pmu.hh:483
uint64_t getValue() const
rReturn the counter value
Definition pmu.cc:600
std::shared_ptr< PMUEvent > sourceEvent
PmuEvent currently in use (if any)
Definition pmu.hh:516
bool enabled
Is the counter enabled?
Definition pmu.hh:509
uint64_t add(uint64_t delta)
Add an event count to the counter and check for overflow.
Definition pmu.cc:829
void serialize(CheckpointOut &cp) const override
Serialize an object.
Definition pmu.cc:813
void debugCounter(const char *mainString, Args &...args) const
Definition pmu.hh:530
void setValue(uint64_t val)
overwrite the value of the counter
Definition pmu.cc:612
void attachEvent(PMU::CounterState *user)
attach this event to a given counter
Definition pmu.cc:480
PMUEvent(EventTypeId _id, Stats *pmu_stats)
PMUEvent constructor.
Definition pmu.hh:301
virtual void enable()=0
Enable the current event.
virtual void disable()=0
Disable the current event.
Stats *const pmuStats
True is event is reported as a stat.
Definition pmu.hh:352
const EventTypeId id
ID of this event.
Definition pmu.hh:349
std::set< PMU::CounterState * > userCounters
set of counters using this event
Definition pmu.hh:355
virtual void updateAttachedCounters()
Method called immediately before a counter access in order for the associated event to update its sta...
Definition pmu.hh:345
void detachEvent(PMU::CounterState *user)
detach this event from a given counter
Definition pmu.cc:504
virtual void increment(const uint64_t val)
notify an event increment of val units, all the attached counters' value is incremented by val units.
Definition pmu.cc:492
void notify(const CacheAccessProbeArg &val) override
Definition pmu.hh:394
CacheProbe(RegularEvent *parent, std::string name)
Definition pmu.hh:388
void notify(const uint64_t &val) override
Definition pmu.cc:516
RegularProbe(RegularEvent *parent, std::string name)
Definition pmu.hh:374
std::set< EventTypeEntry > microArchitectureEventSet
The set of events driving the event value.
Definition pmu.hh:404
PMUEvent(EventTypeId _id, Stats *pmu_stats)
PMUEvent constructor.
Definition pmu.hh:301
void enable() override
Enable the current event.
Definition pmu.cc:522
void disable() override
Disable the current event.
Definition pmu.cc:543
void addMicroarchitectureProbe(SimObject *object, std::string name)
Definition pmu.hh:362
std::vector< ProbeListenerPtr<> > attachedProbePointList
Set of probe listeners tapping onto each of the input micro-arch events which compose this pmu event.
Definition pmu.hh:409
std::pair< SimObject *, std::string > EventTypeEntry
Definition pmu.hh:360
void registerEvent(EventTypeId id, const char *stat_name)
Definition pmu.cc:884
void add(EventTypeId id, uint64_t value)
Increment the map[id] stat by value.
Definition pmu.cc:897
Stats(PMU *parent)
Definition pmu.cc:874
std::unordered_map< EventTypeId, statistics::Scalar > map
Definition pmu.hh:695

Generated on Sat Oct 18 2025 08:06:37 for gem5 by doxygen 1.14.0