The Sparta Modeling Framework
Loading...
Searching...
No Matches
Counter.hpp
1// <Counter> -*- C++ -*-
2
3
4#pragma once
5
6#include <iostream>
7#include <sstream>
8#include <limits>
9
12#include "sparta/statistics/CounterBase.hpp"
13
14
15namespace sparta
16{
26 class Counter final : public CounterBase
27 {
28 public:
29
33
51 const std::string& name,
52 const std::string& group,
54 const std::string& desc,
55 CounterBehavior behave,
56 visibility_t visibility) :
57 CounterBase(parent,
58 name,
59 group,
60 group_idx,
61 desc,
62 behave,
63 visibility),
64 val_(0)
65 { }
66
69 const std::string& name,
70 const std::string& group,
72 const std::string& desc,
73 CounterBehavior behave) :
74 Counter(parent,
75 name,
76 group,
77 group_idx,
78 desc,
79 behave,
81 {
82 // Initialization handled in constructor delegation
83 }
84
87 const std::string& name,
88 const std::string& desc,
89 CounterBehavior behave,
90 visibility_t visibility) :
91 Counter(parent,
92 name,
95 desc,
96 behave,
97 visibility)
98 {
99 // Initialization handled in constructor delegation
100 }
101
104 const std::string& name,
105 const std::string& desc,
106 CounterBehavior behave) :
107 Counter(parent,
108 name,
111 desc,
112 behave)
113 {
114 // Initialization handled in constructor delegation
115 }
116
120 Counter(const Counter& rhp) = delete;
121
127 CounterBase(std::move(rhp)),
128 val_(rhp.val_)
129 {
130 TreeNode* parent = rhp.getParent();
131 if(parent != nullptr){
132 parent->addChild(this);
133 }
134 }
135
141
144
148
161 if(__builtin_expect(getBehavior() != COUNT_LATEST, 0) == true){
162 throw SpartaException("Cannot write a new counter value for ")
163 << getLocation() << " because its behavior is not COUNT_LATEST. "
164 << "Other behaviors should only support incrementing or adding";
165 }
166
167 val_ = val;
168 return val;
169 }
170
173 set(val);
174 return *this;
175 }
176
178 Counter& operator=(const Counter& rhp) {
179 set(rhp.get());
180 return *this;
181 }
182
192 //See if we need to throw any increment exceptions.
193 static_assert(std::numeric_limits<counter_type>::is_signed == false,
194 "Counter type expected to be unsigned. If counter type "
195 "is changed or made templated, ths following test should "
196 "probably be enabled and increment should be changed to accept an unsigned value only.");
197
198 // Tempting idea, but very costly in performance.
199 // Specifically, the assert is expensive. :(
200 //
201 // static_assert(sizeof(counter_type) == sizeof(uint64_t),
202 // "Counter type expected to be the size of an unsigned long (64-bits)");
203 // const bool ret = __builtin_uaddl_overflow(val_, add, &val_);
204 // sparta_assert(ret != true, "Encountered an overflowing Counter: " << getLocation());
205
206 val_ += add;
207 return val_;
208 }
209
217 return ++val_;
218 }
219
227 return val_++;
228 }
229
233 return increment(add);
234 }
235
241 counter_type get() const override {
242 return val_;
243 }
244
246 operator counter_type() const {
247 return get();
248 }
249
253 bool operator==(const Counter& rhp) const {
254 return get() == rhp.get();
255 }
256
259
262 virtual bool supportsCompression() const override {
263 return true;
264 }
265
269
270 // Override from TreeNode
271 virtual std::string stringize(bool pretty=false) const override {
272 (void) pretty;
273 std::stringstream ss;
274 ss << '<' << getLocation() << " val:" << std::dec;
275 ss << get() << ' ';
277 ss << " vis:" << getVisibility();
278 stringizeTags(ss);
279 ss << '>';
280 return ss.str();
281 }
282
285
286 protected:
287
293 virtual void onAddingChild_(TreeNode* child) override {
294 (void) child;
295 throw SpartaException("Cannot add children to a Counter");
296 }
297
298 private:
299
303 uint64_t val_;
304 };
305
306} // namespace sparta
307
Set of macros for Sparta assertions. Caught by the framework.
Exception class for all of Sparta.
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.
@ COUNT_LATEST
Counter holds the latest value (from most recent activity) and can increase or decrease at any time.
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
virtual bool supportsCompression() const override
Definition Counter.hpp:262
Counter(Counter &&rhp)
Move constructor.
Definition Counter.hpp:126
Counter(TreeNode *parent, const std::string &name, const std::string &group, TreeNode::group_idx_type group_idx, const std::string &desc, CounterBehavior behave, visibility_t visibility)
Counter constructor.
Definition Counter.hpp:50
Counter & operator=(const Counter &rhp)
Sets a new value.
Definition Counter.hpp:178
virtual void onAddingChild_(TreeNode *child) override
React to child registration.
Definition Counter.hpp:293
counter_type increment(counter_type add)
Increments the value with overflow detection.
Definition Counter.hpp:191
Counter(const Counter &rhp)=delete
Copy construction not allowed.
Counter & operator=(counter_type val)
Sets a new value.
Definition Counter.hpp:172
virtual std::string stringize(bool pretty=false) const override
Create a string representation of this node.
Definition Counter.hpp:271
counter_type get() const override
Gets the value of this counter.
Definition Counter.hpp:241
counter_type operator++()
Pre-increments the value with overflow detection.
Definition Counter.hpp:216
Counter(TreeNode *parent, const std::string &name, const std::string &desc, CounterBehavior behave, visibility_t visibility)
Alternate constructor.
Definition Counter.hpp:86
counter_type set(counter_type val)
Sets a new value.
Definition Counter.hpp:160
~Counter()
Destructor.
Definition Counter.hpp:140
bool operator==(const Counter &rhp) const
Compare value to value of another counter.
Definition Counter.hpp:253
Counter(TreeNode *parent, const std::string &name, const std::string &desc, CounterBehavior behave)
Alternate constructor.
Definition Counter.hpp:103
Counter(TreeNode *parent, const std::string &name, const std::string &group, TreeNode::group_idx_type group_idx, const std::string &desc, CounterBehavior behave)
Alternate constructor.
Definition Counter.hpp:68
counter_type operator+=(counter_type add)
Increment this value withi overflow detection.
Definition Counter.hpp:232
counter_type operator++(int)
Post-increments the value with overflow detection.
Definition Counter.hpp:226
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
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
void stringizeTags(std::stringstream &ss) const
Render tags to a string in the form: " tags:[tag0, tag1]" If there are any tags. The leading space ma...
Definition TreeNode.hpp:743
Macros for handling exponential backoff.