The Sparta Modeling Framework
Loading...
Searching...
No Matches
StatisticSet.hpp
Go to the documentation of this file.
1// <StatisticSet> -*- C++ -*-
2
3
10#pragma once
11
12#include <iostream>
13
15#include "sparta/statistics/CounterBase.hpp"
18
19
20namespace sparta
21{
26 class StatisticSet : public TreeNode
27 {
28 public:
29
31 typedef std::vector<StatisticDef*> StatisticVector;
32
34 typedef std::vector<CounterBase*> CounterVector;
35
37 static constexpr char NODE_NAME[] = "stats";
38
50 "Statistic and Counter Set")
51 {
52 if(parent){
53 setExpectedParent_(parent);
54 parent->addChild(this);
55 }
56 }
57
62 {;}
63
64 // Overload of TreeNode::stringize
65 virtual std::string stringize(bool pretty=false) const override {
66 (void) pretty;
67 std::stringstream ss;
68 ss << '<' << getLocation() << ' ' << (stats_.size()) << " stats, "
69 << (ctrs_.size()) << " counters>";
70 return ss.str();
71 }
72
76
80 uint32_t getNumStatisticDefs() const { return stats_.size(); }
81
87 const StatisticVector& getStatisticDefs() const { return stats_; }
88
96 StatisticDef* getStatisticDef(const std::string& name) {
97 return getChildAs<StatisticDef>(name);
98 };
99
118 template<typename... _Args>
119 StatisticDef & createStatisticDef(_Args&&... __args) {
120 if(isFinalized()){
121 throw SpartaException("Cannot create a new StatisticDef once a StatisticSet is finalized. "
122 "Error with: ")
123 << getLocation();
124 }
125 owned_stats_.emplace_back(new StatisticDef(this, __args...));
126 return *(static_cast<StatisticDef *>(owned_stats_.back().get()));
127 }
128
129
132
136
140 uint32_t getNumCounters() const {
141 return ctrs_.size();
142 }
143
149 const CounterVector& getCounters() const {
150 return ctrs_;
151 }
152
161 const CounterBase* getCounter(const std::string& name) const {
162 return getChildAs<CounterBase>(name);
163 }
164
173 CounterBase* getCounter(const std::string& name) {
174 return getChildAs<CounterBase>(name);
175 }
176
185 template<class CounterT>
186 const CounterT* getCounterAs(const std::string& name) const {
187 return getChildAs<CounterT>(name);
188 }
189
198 template<class CounterT>
199 CounterT* getCounterAs(const std::string& name) {
200 return getChildAs<CounterT>(name);
201 }
202
224 template<class CounterT, typename... _Args>
225 CounterT & createCounter(_Args&&... __args) {
226 if(isFinalized()){
227 throw SpartaException("Cannot create a new Counter once a StatisticSet is finalized. "
228 "Error with: ")
229 << getLocation();
230 }
231 owned_ctrs_.emplace_back(new CounterT(this, __args...));
232 return *((CounterT *)owned_ctrs_.back().get());
233 }
234
237
238 private:
239
250 virtual void onAddingChild_(TreeNode* child) override {
251 if(isFinalized()){
252 throw SpartaException("Cannot add a child Counter once a StatisticSet is finalized. "
253 "Error with: ")
254 << getLocation();
255 }
256
257 StatisticDef* stat = dynamic_cast<StatisticDef*>(child);
258 if(nullptr != stat){
259 // Add stat to stats_ list for tracking.
260 stats_.push_back(stat);
261 return;
262 }
263
264 CounterBase* ctr = dynamic_cast<CounterBase*>(child);
265 if(nullptr != ctr){
266 // Add Counter to ctrs_ list for tracking.
267 ctrs_.push_back(ctr);
268 return;
269 }
270
271 throw SpartaException("Cannot add TreeNode child ")
272 << child->getName() << " to StatisticSet " << getLocation()
273 << " because the child is not a CounterBase or StatisticDef";
274 }
275
279 typedef std::vector<std::unique_ptr<StatisticDef>> OwnedStatisticVector;
280
285 OwnedStatisticVector owned_stats_;
286
291 StatisticVector stats_;
292
296 typedef std::vector<std::unique_ptr<CounterBase>> OwnedCounterVector;
297
302 OwnedCounterVector owned_ctrs_;
303
311 CounterVector ctrs_;
312 };
313
314} // namespace sparta
315
Set of macros for Sparta assertions. Caught by the framework.
Exception class for all of Sparta.
Contains a statistic definition (some useful information which can be computed)
The base class for all Counters.
virtual bool isFinalized() const
Is this node (and thus the entire tree above it) "finalized".
Used to construct and throw a standard C++ exception. Inherits from std::exception.
Contains a statistic definition (some useful information which can be computed)
Set of StatisticDef and CounterBase-derived objects for visiblility through a sparta Tree.
CounterBase * getCounter(const std::string &name)
Retrieves a child that is a Counter with the given dotted path.
std::vector< StatisticDef * > StatisticVector
Type for holding stat defs.
static constexpr char NODE_NAME[]
Name of all StatisticSet nodes.
~StatisticSet()
Destructor.
StatisticDef & createStatisticDef(_Args &&... __args)
Allocates a StatisticDef which is owned by this StatisticSet and deleted at its destruction.
std::vector< CounterBase * > CounterVector
Type for holding Counters.
CounterT & createCounter(_Args &&... __args)
Allocates a Counter which is owned by this StatisticSet and deleted at its destruction.
const CounterBase * getCounter(const std::string &name) const
Retrieves a child that is a Counter with the given dotted path.
const CounterT * getCounterAs(const std::string &name) const
Retrieves a child that is a Counter with the given dotted path.
virtual std::string stringize(bool pretty=false) const override
Create a string representation of this node.
CounterT * getCounterAs(const std::string &name)
Retrieves a child that is a Counter with the given dotted path (non-const).
const StatisticVector & getStatisticDefs() const
Gets the vector of StatisticDefs contained by this set.
uint32_t getNumCounters() const
Gets the number of counters in this Set.
StatisticSet(TreeNode *parent)
Constructor.
const CounterVector & getCounters() const
Gets the vector of Counters contained by this set.
uint32_t getNumStatisticDefs() const
Gets the number of counters in this Set.
StatisticDef * getStatisticDef(const std::string &name)
Retrieves a child that is a StatisticDef with the given dotted path.
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_BUILTIN[]
Reserved name for built-in nodes.
Definition TreeNode.hpp:370
void addChild(TreeNode *child, bool inherit_phase=true)
Adds a TreeNode to this node as a child.
const std::string & getName() const override
Gets the name of this node.
void setExpectedParent_(const TreeNode *parent)
Tracks a node as an expected parent without actually adding this node as a child. This is used almost...
Macros for handling exponential backoff.