The Sparta Modeling Framework
Loading...
Searching...
No Matches
CycleCounter.hpp
1// <Counter> -*- C++ -*-
2
3
4#pragma once
5
8#include "sparta/statistics/Counter.hpp"
12
13
14namespace sparta
15{
31 {
32 public:
33
37
55 const std::string& name,
56 const std::string& group,
58 const std::string& desc,
59 CounterBehavior behave,
60 const sparta::Clock * clk,
61 visibility_t visibility) :
62 CounterBase(parent,
63 name,
64 group,
65 group_idx,
66 desc,
67 behave,
68 visibility),
69 clk_(clk)
70 {
71 sparta_assert(clk_ != nullptr, "CycleCounter must be given a clock through it's parent node");
72 }
73
74 // Alternate constructor
75 CycleCounter(TreeNode* parent,
76 const std::string& name,
77 const std::string& desc,
78 CounterBehavior behave,
79 const sparta::Clock * clk,
80 visibility_t visibility) :
81 CycleCounter(parent,
82 name,
85 desc,
86 behave,
87 clk,
88 visibility)
89 {
90 // Initialization handled in delegated constructor
91 }
92
93 // Alternate Constructor
94 CycleCounter(TreeNode* parent,
95 const std::string& name,
96 const std::string& group,
98 const std::string& desc,
99 CounterBehavior behave,
100 const sparta::Clock * clk) :
101 CycleCounter(parent,
102 name,
103 group,
104 group_idx,
105 desc,
106 behave,
107 clk,
109 {
110 sparta_assert(clk_ != nullptr, "CycleCounter must be given a clock through it's parent node");
111 }
112
113 // Alternate constructor
114 CycleCounter(TreeNode* parent,
115 const std::string& name,
116 const std::string& desc,
117 CounterBehavior behave,
118 const sparta::Clock * clk) :
119 CycleCounter(parent,
120 name,
123 desc,
124 behave,
125 clk,
127 {
128 // Initialization handled in delegated constructor
129 }
130
131
137 CounterBase(std::move(rhp)),
138 clk_(rhp.clk_),
139 count_(0),
140 start_count_(0),
141 counting_(false)
142 {
143 TreeNode* parent = rhp.getParent();
144 if(parent != nullptr){
145 parent->addChild(this);
146 }
147 }
148
154
157
161
169 void startCounting(uint32_t delay = 0) {
170 sparta_assert(counting_ == false);
171 mult_ = 1;
172 start_count_ = clk_->elapsedCycles() + delay;
173 counting_ = true;
174 }
175
187 void startCountingWithMultiplier(uint32_t add_per_cycle, uint32_t delay = 0) {
188 sparta_assert(counting_ == false);
189 mult_ = add_per_cycle;
190 start_count_ = clk_->elapsedCycles() + delay;
191 counting_ = true;
192 }
193
194
199 void updateCountingMultiplier(uint32_t add_per_cycle) {
200 if (isCounting()) {
201 stopCounting();
202 }
203 startCountingWithMultiplier(add_per_cycle);
204 }
205
207 void stopCounting(uint32_t delay = 0) {
208 sparta_assert(counting_ == true);
209 sparta_assert ((clk_->elapsedCycles() + delay) >= start_count_);
210 count_ += (clk_->elapsedCycles() + delay - start_count_) * mult_;
211 counting_ = false;
212 }
213
215 bool isCounting() const {
216 return counting_;
217 }
218
220 uint32_t getCurrentMultiplier() const {
221 return mult_;
222 }
223
224
227
231
239 virtual counter_type get() const override {
240 counter_type count = count_;
241 if(counting_) {
242 Clock::Cycle elapsed = clk_->elapsedCycles();
243 if (elapsed > start_count_) {
244 count += (elapsed - start_count_) * mult_;
245 }
246 }
247 return count;
248 }
252 operator counter_type() const {
253 return get();
254 }
255
259 bool operator==(const Counter& rhp) const {
260 return get() == rhp.get();
261 }
262
266 bool operator==(const CycleCounter& rhp) const {
267 return get() == rhp.get();
268 }
269
272
275 virtual bool supportsCompression() const override {
276 return true;
277 }
278
282
283 // Override from TreeNode
284 virtual std::string stringize(bool pretty=false) const override {
285 (void) pretty;
286 std::stringstream ss;
287 ss << '<' << getLocation() << " val:" << std::dec;
288 ss << get() << ' ';
290 ss << " vis:" << getVisibility() << '>';
291 return ss.str();
292 }
293
294 protected:
295
301 virtual void onAddingChild_(TreeNode* child) override {
302 (void) child; throw SpartaException("Cannot add children to a CycleCounter");
303 }
304
305 private:
306
308 const sparta::Clock * clk_ = nullptr;
309
311 uint32_t mult_ = 1;
312
314 counter_type count_ = 0;
315 counter_type start_count_ = 0;
316
318 bool counting_ = false;
319
320 }; // class CycleCounter
321
322} // namespace sparta
Byte order types and byte-swapping routines.
File that defines the Clock class.
Set of macros for Sparta assertions. Caught by the framework.
#define sparta_assert(...)
Simple variadic assertion that will throw a sparta_exception if the condition fails.
Exception class for all of Sparta.
Basic Node framework in sparta device tree composite pattern.
A representation of simulated time.
Definition Clock.hpp:51
Cycle elapsedCycles() const
Return the total elapsed cycles from this Clocks POV.
Definition Clock.hpp:204
The base class for all Counters.
static std::string getBehaviorName(CounterBehavior behave)
Returns a string containing the name of the given behavior.
uint64_t counter_type
Counter value type.
CounterBehavior
Behavior of this counter.
CounterBehavior getBehavior() const
Gets the behavior for this counter specified at construction.
Represents a counter of type counter_type (uint64_t). 2 and greater than 0 with a ceiling specified....
Definition Counter.hpp:27
counter_type get() const override
Gets the value of this counter.
Definition Counter.hpp:241
Represents a cycle counter.
void startCounting(uint32_t delay=0)
Start counting, taking into account the specified delay.
uint32_t getCurrentMultiplier() const
Return the current multipler.
void stopCounting(uint32_t delay=0)
Stop counting and increment internal count, taking into account the specified delay.
void startCountingWithMultiplier(uint32_t add_per_cycle, uint32_t delay=0)
Start counting, taking into account the specified delay.
virtual void onAddingChild_(TreeNode *child) override
React to child registration.
virtual std::string stringize(bool pretty=false) const override
Create a string representation of this node.
CycleCounter(CycleCounter &&rhp)
Move constructor.
void updateCountingMultiplier(uint32_t add_per_cycle)
virtual counter_type get() const override
Gets the value of this counter.
bool operator==(const CycleCounter &rhp) const
Comparison against another counter.
~CycleCounter()
Destructor.
bool operator==(const Counter &rhp) const
Comparison against another counter.
CycleCounter(TreeNode *parent, const std::string &name, const std::string &group, TreeNode::group_idx_type group_idx, const std::string &desc, CounterBehavior behave, const sparta::Clock *clk, visibility_t visibility)
CycleCounter constructor.
bool isCounting() const
Return whether this counter is counting or not.
virtual bool supportsCompression() const override
uint32_t visibility_t
Continuous visibility level. Several key points along continum are indicated within Visibility.
static constexpr visibility_t DEFAULT_VISIBILITY
Default node visibility.
visibility_t getVisibility() const
Gets the visibility hint of this node. This is invariant after construction.
Used to construct and throw a standard C++ exception. Inherits from std::exception.
Node in a composite tree representing a sparta Tree item.
Definition TreeNode.hpp:205
static const group_idx_type GROUP_IDX_NONE
GroupIndex indicating that a node has no group index because it belongs to no group.
Definition TreeNode.hpp:303
std::string getLocation() const override final
static constexpr char GROUP_NAME_NONE[]
Group name indicating that a node belongs to no group.
Definition TreeNode.hpp:314
TreeNode()=delete
Not default-constructable.
void addChild(TreeNode *child, bool inherit_phase=true)
Adds a TreeNode to this node as a child.
virtual TreeNode * getParent()
Gets immediate parent of this node if one exists.
Definition TreeNode.hpp:965
uint32_t group_idx_type
Index within a group.
Definition TreeNode.hpp:261
Macros for handling exponential backoff.