The Sparta Modeling Framework
Loading...
Searching...
No Matches
EnumCycleHistogram.hpp
Go to the documentation of this file.
1// <EnumCycleHistogram.h> -*- C++ -*-
2
3
12#pragma once
13
14#include <iostream>
15#include <string>
16#include <vector>
17#include "sparta/utils/MathUtils.hpp"
22#include "sparta/statistics/Counter.hpp"
23#include "sparta/statistics/CycleCounter.hpp"
28
29namespace sparta{
30
32// Case when it is not a sparta::utils::Enum type.
33template<typename T>
34struct is_sparta_enum : public std::false_type{};
35
37// Case when it is a sparta::utils::Enum type.
38template<typename T>
39struct is_sparta_enum<sparta::utils::Enum<T>> : public std::true_type{};
40
42// name equivalents of the different enum constants. For this,
43// we need to invoke the << operator on individual enum constants.
44// This template overload is SFINAEd out to enable if this enum
45// type U has a << operator overloaded for it.
46template<typename U>
47MetaStruct::enable_if_t<not sparta::is_sparta_enum<U>::value and
49getHumanReadableHistogramBinNames(std::vector<std::string>& enum_name_strings){
50 typedef typename std::underlying_type<U>::type enum_type;
51 constexpr enum_type last_index = static_cast<enum_type>(U::__LAST);
52 enum_name_strings.reserve(last_index);
53 std::stringstream ss;
54 for(enum_type e = 0; e < last_index; ++e){
55 auto val = static_cast<U>(e);
56 ss << val;
57 enum_name_strings.emplace_back(ss.str());
58 ss.str("");
59 ss.clear();
60 }
61}
62
64// name equivalents of the different enum constants. For this,
65// we need to invoke the << operator on individual enum constants.
66// This template overload is SFINAEd out to disable if this enum
67// type U does not have a << operator overloaded for it. We fill
68// the resulting vector with empty strings.
69template<typename U>
70MetaStruct::enable_if_t<not sparta::is_sparta_enum<U>::value and
72getHumanReadableHistogramBinNames(std::vector<std::string>&){}
73
75// name equivalents of the different enum constants.
76// Template specialization for types which are actually sparta::utils::Enum type.
77template<typename U>
78MetaStruct::enable_if_t<sparta::is_sparta_enum<U>::value, void>
79getHumanReadableHistogramBinNames(std::vector<std::string>& enum_name_strings){
80 U::populateNames(enum_name_strings);
81}
82
96template<typename EnumType>
98public:
99
104
109
114
119
124
153 template<typename T = EnumType>
154 EnumCycleHistogram(TreeNode* parent_treenode,
155 const std::string& histogram_name,
156 const std::string& description,
157 const T idle_value = T::__FIRST,
158 const InstrumentationNode::visibility_t stat_vis_general =
160 const InstrumentationNode::visibility_t stat_vis_detailed =
166 MetaStruct::enable_if_t<not sparta::is_sparta_enum<T>::value>* = 0) :
167 sparta::TreeNode(histogram_name, description),
168 sparta::CycleHistogramBase(static_cast<uint64_t>(T::__FIRST),
169 static_cast<uint64_t>(T::__LAST) - 1,
170 1,
171 static_cast<uint64_t>(idle_value)),
172 stats_(this)
173 {
174 static_assert(std::is_enum<T>::value, "The template type must be a std::enum class type.");
175 sparta_assert(parent_treenode and parent_treenode->getClock());
176 setExpectedParent_(parent_treenode);
177 std::vector<std::string> enum_name_strings;
179 getHumanReadableHistogramBinNames<T>(enum_name_strings);
180 initializeStats_ (&stats_, parent_treenode->getClock(), std::string(),
181 description, stat_vis_general, stat_vis_detailed,
182 stat_vis_max, stat_vis_avg, enum_name_strings);
183 parent_treenode->addChild(this);
184 total_->startCounting();
185 // Start capturing the idle value
188 }
189
218 template<typename T = EnumType>
219 EnumCycleHistogram(TreeNode* parent_treenode,
220 const std::string& histogram_name,
221 const std::string& description,
222 const typename T::value_type idle_value = T::value_type::__FIRST,
223 const InstrumentationNode::visibility_t stat_vis_general =
225 const InstrumentationNode::visibility_t stat_vis_detailed =
231 MetaStruct::enable_if_t<sparta::is_sparta_enum<T>::value>* = 0) :
232 sparta::TreeNode(histogram_name, description),
233 sparta::CycleHistogramBase(static_cast<uint64_t>(T::value_type::__FIRST),
234 static_cast<uint64_t>(T::value_type::__LAST) - 1,
235 1,
236 static_cast<uint64_t>(idle_value)),
237 stats_(this)
238 {
239 static_assert(sparta::is_sparta_enum<T>::value, "The template type must be sparta enum class type.");
240 sparta_assert(parent_treenode and parent_treenode->getClock());
241 setExpectedParent_(parent_treenode);
242 std::vector<std::string> enum_name_strings;
244 getHumanReadableHistogramBinNames<T>(enum_name_strings);
245 initializeStats_ (&stats_, parent_treenode->getClock(), std::string(),
246 description, stat_vis_general, stat_vis_detailed,
247 stat_vis_max, stat_vis_avg, enum_name_strings);
248 parent_treenode->addChild(this);
249 total_->startCounting();
250 // Start capturing the idle value
253 }
254
262 template<typename T = EnumType>
263 void startCounting(const T enum_val, uint64_t delay = 0){
265 startCounting_(static_cast<uint64_t>(enum_val), delay);
266 updateMaxValues_(static_cast<uint64_t>(enum_val));
267 }
268
277 template<typename T = EnumType>
278 void stopCounting(const T enum_val, uint64_t delay = 0){
279 stopCounting_(static_cast<uint64_t>(enum_val), delay);
281 }
282
284 uint64_t getHistogramUpperValue() const{
285 return this->sparta::CycleHistogramBase::getHistogramUpperValue();
286 }
287
289 uint64_t getHistogramLowerValue() const{
290 return this->sparta::CycleHistogramBase::getHistogramLowerValue();
291 }
292
294 uint64_t getNumBins() const{
295 return this->sparta::CycleHistogramBase::getNumBins();
296 }
297
299 uint64_t getNumValuesPerBin() const{
300 return this->sparta::CycleHistogramBase::getNumValuesPerBin();
301 }
302
303 private:
306}; // class EnumCycleHistogram
307} // namespace sparta
CycleHistogram implementation using sparta CycleCounter.
Compile-time SFINAE techniques to detect presence of operators, member-fields and methods by name in ...
Contains a collection implementation of various compile-time metaprogramming and Type-Detection APIs ...
File that defines the Resource class. Consider using sparta::Unit instead.
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.
CycleHistogramBase class for uint64_t values.
const uint64_t idle_value_
Value to capture when nothing is captured.
void stopCounting_(uint64_t val, uint64_t delay=0)
Stop counting and increment internal count, taking into account the specified delay.
void updateMaxValues_(uint64_t val)
void startCounting_(uint64_t val, uint64_t delay=0)
Start counting, taking into account the specified delay.
std::unique_ptr< sparta::CycleCounter > total_
Total values.
uint64_t last_value_
Last value updated.
EnumCycleHistogram class for C++ Enum values.
EnumCycleHistogram(TreeNode *parent_treenode, const std::string &histogram_name, const std::string &description, const T idle_value=T::__FIRST, const InstrumentationNode::visibility_t stat_vis_general=InstrumentationNode::AUTO_VISIBILITY, const InstrumentationNode::visibility_t stat_vis_detailed=InstrumentationNode::AUTO_VISIBILITY, InstrumentationNode::visibility_t stat_vis_max=InstrumentationNode::AUTO_VISIBILITY, InstrumentationNode::visibility_t stat_vis_avg=InstrumentationNode::AUTO_VISIBILITY, MetaStruct::enable_if_t< not sparta::is_sparta_enum< T >::value > *=0)
EnumCycleHistogram constructor.
EnumCycleHistogram(const EnumCycleHistogram &)=delete
Not copy-constructable.
uint64_t getHistogramUpperValue() const
Method to return the upper value of the histogram.
uint64_t getNumValuesPerBin() const
Method to return the number of values per bin of the histogram.
void stopCounting(const T enum_val, uint64_t delay=0)
Stop counting and increment internal count, taking into account the specified delay.
EnumCycleHistogram(TreeNode *parent_treenode, const std::string &histogram_name, const std::string &description, const typename T::value_type idle_value=T::value_type::__FIRST, const InstrumentationNode::visibility_t stat_vis_general=InstrumentationNode::AUTO_VISIBILITY, const InstrumentationNode::visibility_t stat_vis_detailed=InstrumentationNode::AUTO_VISIBILITY, InstrumentationNode::visibility_t stat_vis_max=InstrumentationNode::AUTO_VISIBILITY, InstrumentationNode::visibility_t stat_vis_avg=InstrumentationNode::AUTO_VISIBILITY, MetaStruct::enable_if_t< sparta::is_sparta_enum< T >::value > *=0)
EnumCycleHistogram constructor.
uint64_t getHistogramLowerValue() const
Method to return the lower value of the histogram.
EnumCycleHistogram(EnumCycleHistogram &&)=delete
Not move-constructable.
EnumCycleHistogram & operator=(const EnumCycleHistogram &)=delete
Not copy-assignable.
uint64_t getNumBins() const
Method to return the number of bins of the histogram.
void startCounting(const T enum_val, uint64_t delay=0)
Start counting, taking into account the specified delay.
EnumCycleHistogram()=delete
Not default constructable.
uint32_t visibility_t
Continuous visibility level. Several key points along continum are indicated within Visibility.
static constexpr visibility_t AUTO_VISIBILITY
The default sparta resource visibility value that should be used. This is an alias of VIS_MAX at the ...
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,...
void setExpectedParent_(const TreeNode *parent)
Tracks a node as an expected parent without actually adding this node as a child. This is used almost...
Class that wraps a C++ class enum and allows conversion between POD and the enum type....
Definition Enum.hpp:22
Macros for handling exponential backoff.
MetaStruct::enable_if_t< not sparta::is_sparta_enum< U >::value and sparta::utils::has_ostream_operator< U >::value, void > getHumanReadableHistogramBinNames(std::vector< std::string > &enum_name_strings)
Given an enum class type, this method figures out the string.
Detect whether template parameter is sparta::utils::Enum type.