The Sparta Modeling Framework
Loading...
Searching...
No Matches
StateHistogram.hpp
Go to the documentation of this file.
1// <StateHistogram.h> -*- C++ -*-
2
3
9#pragma once
10
11#include <iostream>
12#include <string>
13#include <vector>
14#include "sparta/utils/MathUtils.hpp"
17#include "sparta/statistics/Counter.hpp"
18#include "sparta/statistics/CycleCounter.hpp"
22
23namespace sparta
24{
38template <class StateEnumType>
40{
41public:
42
46 StateHistogram() = delete;
47
52
57
61 void operator=(const StateHistogram&) = delete;
62
73 StateHistogram(TreeNode* parent_treenode,
74 std::string histogram_name,
75 std::string description,
76 const StateEnumType idle_value = StateEnumType::__FIRST) :
77 TreeNode(histogram_name, description),
78 lower_val_(static_cast<uint64_t>(StateEnumType::__FIRST)),
79 upper_val_(static_cast<uint64_t>(StateEnumType::__LAST) - 1),
80 idle_value_(static_cast<uint64_t>(idle_value)),
81 stats_(this)
82 {
83 if(parent_treenode){
84 setExpectedParent_(parent_treenode);
85 }
86
87 sparta_assert_context(upper_val_ > lower_val_,
88 "StateHistogram: upper value must be greater than lower value");
89 num_bins_ = upper_val_ - lower_val_ + 1;
90
91 // Reserve to use emplacement without tree child reordering
92 bin_.reserve(num_bins_);
93
94 uint64_t val = lower_val_;
95 std::string total_str;
96
97 for (uint32_t i = 0; i < num_bins_; ++i) {
98 std::stringstream str;
99 str << "bin_" << std::string(typename sparta::utils::Enum<StateEnumType>::Value(static_cast<StateEnumType>(val)));
100
101 bin_.emplace_back(sparta::CycleCounter(&stats_,
102 str.str(),
103 str.str() + " histogram bin",
105 parent_treenode->getClock()));
106
107 probabilities_.emplace_back(new StatisticDef(&stats_,
108 str.str() + "_probability",
109 str.str() + " bin probability",
110 &stats_,
111 str.str() + "/total"));
112 ++val;
113
114 if (total_str == "") {
115 total_str += str.str();
116 } else {
117 total_str += " + " + str.str();
118 }
119 }
120
121 total_.reset(new StatisticDef(&stats_,
122 "total",
123 "Aggregated total",
124 &stats_,
125 total_str));
126
127 if(parent_treenode){
128 parent_treenode->addChild(this);
129 }
130
131 // Start capturing the idle value
132 startCounting_(idle_value_);
133 }
134
137
141
142 void setState(const StateEnumType new_state)
143 {
144 uint32_t new_val = static_cast<uint32_t>(new_state);
145 if (new_val != curr_value_) {
146 stopCounting_(curr_value_);
147 startCounting_(new_val);
148 }
149 }
150
151 void setNextState(const StateEnumType new_state)
152 {
153 uint32_t new_val = static_cast<uint32_t>(new_state);
154 if (new_val != curr_value_) {
155 stopCounting_(curr_value_, 1);
156 startCounting_(new_val, 1);
157 }
158 }
159
160 StateEnumType getState() const
161 {
162 return static_cast<StateEnumType>(curr_value_);
163 }
164
167
168 uint64_t getHistogramUpperValue() const { return upper_val_; }
169 uint64_t getHistogramLowerValue() const { return lower_val_; }
170 uint32_t getNumBins() const { return num_bins_; }
171 uint32_t getNumValuesPerBin() const { return 1; }
172
177 std::string getDisplayStringCumulative() const
178 {
179 std::stringstream str;
180 str << std::dec;
181 uint64_t val = lower_val_;
182 for (uint32_t i = 0; i < num_bins_; ++i) {
183 str << "\t" << getName()
184 << "[ " << std::string(typename sparta::utils::Enum<StateEnumType>::Value(static_cast<StateEnumType>(val))) << "] = "
185 << bin_[i] << std::endl;
186 ++val;
187 }
188 return str.str();
189 }
190
191private:
199 void startCounting_(uint32_t val, uint32_t delay = 0) {
200 sparta_assert(val >= lower_val_);
201 sparta_assert(val <= upper_val_);
202
203 uint32_t idx = val - lower_val_;
204 sparta_assert((bin_.at(idx)).isCounting() == false);
205 (bin_.at(idx)).startCounting(delay);
206
207 curr_value_ = val;
208 }
209
218 void stopCounting_(uint32_t val, uint32_t delay = 0) {
219 sparta_assert(val >= lower_val_);
220 sparta_assert(val <= upper_val_);
221
222 uint32_t idx = val - lower_val_;
223 sparta_assert((bin_.at(idx)).isCounting() == true);
224 (bin_.at(idx)).stopCounting(delay);
225 }
226
227 const uint64_t lower_val_;
228 const uint64_t upper_val_;
229 const uint32_t idle_value_;
230
231 sparta::StatisticSet stats_;
232 std::unique_ptr<sparta::StatisticDef> total_;
233 std::vector<sparta::CycleCounter> bin_;
234 std::vector<std::unique_ptr<sparta::StatisticDef>> probabilities_;
235
236 uint32_t num_bins_;
237 uint32_t curr_value_ = 0;
238}; // class StateHistogram
239
240} // namespace sparta
File that defines the Resource class. Consider using sparta::Unit instead.
#define sparta_assert_context(e, insertions)
Check condition and throw a sparta exception if condition evaluates to false or 0....
Definition Resource.hpp:402
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.
Contains a statistic definition (some useful information which can be computed)
File that defines the StatisticSet class.
Basic Node framework in sparta device tree composite pattern.
@ COUNT_NORMAL
Counter counts the number of times something happens like one would expect. This is a weakly monotoni...
Represents a cycle counter.
StateHistogram class for uint64_t values.
StateHistogram()=delete
Not default constructable.
StateHistogram(const StateHistogram &)=delete
Not copy-constructable.
StateHistogram(StateHistogram &&)=delete
Not move-constructable.
StateHistogram(TreeNode *parent_treenode, std::string histogram_name, std::string description, const StateEnumType idle_value=StateEnumType::__FIRST)
StateHistogram constructor.
std::string getDisplayStringCumulative() const
Render the cumulative values of this histogram for use in standalone model.
void operator=(const StateHistogram &)=delete
Not assignable.
Contains a statistic definition (some useful information which can be computed)
Set of StatisticDef and CounterBase-derived objects for visiblility through a sparta Tree.
Node in a composite tree representing a sparta Tree item.
Definition TreeNode.hpp:205
void addChild(TreeNode *child, bool inherit_phase=true)
Adds a TreeNode to this node as a child.
const Clock * getClock() override
Walks up parents (starting with self) until a parent with an associated local clock is found,...
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.