16#include "sparta/utils/MathUtils.hpp"
19#include "sparta/statistics/Counter.hpp"
55 uint32_t num_vals_per_bin) :
56 lower_val_(lower_val),
57 upper_val_(upper_val),
58 num_vals_per_bin_(num_vals_per_bin)
61 "Histogram: upper value must be greater than lower value");
63 "Histogram: num_vals_per_bin must be power of 2");
64 idx_shift_amount_ = utils::floor_log2(num_vals_per_bin);
65 double actual_num_bins = (upper_val - lower_val)/num_vals_per_bin + 1;
66 num_bins_ = (uint64_t) actual_num_bins;
68 "Histogram: Actual number of bins (" << actual_num_bins
69 <<
") is not an integer");
84 if (val < lower_val_) {
87 else if (val > upper_val_) {
91 uint32_t idx = (val - lower_val_) >> idx_shift_amount_;
94 if (max_counters_.size()) {
109 const double sum = std::accumulate(bin_.begin(), bin_.end(), 0.0)
110 + (*underflow_bin_) + (*overflow_bin_);
115 const std::size_t total_num_bins = bin_.size() + 2;
117 const double mean = sum / total_num_bins;
119 std::for_each(bin_.begin(), bin_.end(), [&](
const sparta::Counter& c) {
120 auto c_get = c.get();
121 accum += pow(c_get - mean, 2);
123 accum += (*underflow_bin_ - mean) * (*underflow_bin_ - mean);
124 accum += (*overflow_bin_ - mean) * (*overflow_bin_ - mean);
125 return std::sqrt(accum / (total_num_bins - 1));
137 const double sum = std::accumulate(bin_.begin(), bin_.end(), 0.0)
138 + (*underflow_bin_) + (*overflow_bin_);
143 const std::size_t total_num_bins = bin_.size() + 2;
145 return sum / total_num_bins;
152 return *total_values_;
166 return *underflow_bin_;
173 return *overflow_bin_;
180 return ((
static_cast<double>(*underflow_bin_))/(
static_cast<double>(*total_values_)));
187 return ((
static_cast<double>(*overflow_bin_))/(
static_cast<double>(*total_values_)));
194 bin_prob_vector_.clear();
195 for(
const auto& b : bin_){
196 bin_prob_vector_.emplace_back(((
static_cast<double>(b))/(
static_cast<double>(*total_values_))));
198 return bin_prob_vector_;
201 uint64_t getHistogramUpperValue()
const {
return upper_val_; }
202 uint64_t getHistogramLowerValue()
const {
return lower_val_; }
203 uint32_t getNumBins()
const {
return num_bins_; }
204 uint32_t getNumValuesPerBin()
const {
return num_vals_per_bin_; }
216 auto min_it = max_values_.begin();
217 if (*min_it >= val) {
221 max_values_.erase(min_it);
222 max_values_.insert(val);
225 for (
auto it = max_values_.begin(); it != max_values_.end(); it++) {
227 max_counters_[idx].set(*it);
238 std::stringstream str;
240 uint64_t running_sum = *underflow_bin_;
241 str <<
"\t" << name <<
"[ UF ] = " << running_sum << std::endl;
242 uint64_t start_val = lower_val_;
243 uint64_t end_val = start_val + num_vals_per_bin_ - 1;
244 for (uint32_t i=0; i<num_bins_; ++i) {
245 if (end_val > upper_val_)
246 end_val = upper_val_;
247 running_sum += (bin_[i]);
249 <<
"[ " << start_val <<
"-" << end_val <<
" ] = "
250 << running_sum << std::endl;
251 end_val += num_vals_per_bin_;
253 running_sum += *overflow_bin_;
254 str <<
"\t" << name <<
"[ OF ] = " << running_sum << std::endl;
267 const std::string & stat_prefix =
"",
270 uint32_t num_max_values = 0,
275 stat_prefix +
"total",
276 "Total values added to the histogram",
282 "Sum of all values added to the histogram",
287 bin_.reserve(num_bins_);
294 stat_prefix +
"UF_probability",
295 "Probability of underflow",
297 stat_prefix +
"UF" +
"/" + stat_prefix +
"total",
300 uint64_t start_val = lower_val_;
301 uint64_t end_val = start_val + num_vals_per_bin_ - 1;
302 for (uint32_t i=0; i<num_bins_; ++i) {
303 if (end_val > upper_val_)
304 end_val = upper_val_;
305 std::stringstream str;
306 str << stat_prefix <<
"bin_" << start_val <<
"_" << end_val;
309 str.str() +
" histogram bin",
313 str.str() +
"_probability",
314 str.str() +
" bin probability",
316 str.str() +
"/" + stat_prefix +
"total",
319 start_val = end_val + 1;
320 end_val += num_vals_per_bin_;
323 stat_prefix +
"overflow bin",
327 stat_prefix +
"OF_probability",
328 "Probability of overflow",
330 stat_prefix +
"OF" +
"/" + stat_prefix +
"total",
335 stat_prefix +
"average",
336 "Average of all values added to the histogram",
338 stat_prefix +
"sum" +
"/" + stat_prefix +
"total",
342 if (num_max_values > 0) {
343 max_counters_.reserve(num_max_values);
344 for (uint32_t idx = 0; idx < num_max_values; idx++) {
345 std::stringstream mvtext;
346 mvtext <<
"maxval" << idx;
347 max_counters_.emplace_back(sset,
348 stat_prefix + mvtext.str(),
349 stat_prefix +
" maximum value",
352 max_counters_[idx].set(0);
353 max_values_.insert(0);
360 const uint64_t lower_val_;
361 const uint64_t upper_val_;
362 const uint32_t num_vals_per_bin_;
364 std::unique_ptr<sparta::Counter> total_values_;
365 std::unique_ptr<sparta::Counter> running_sum_;
368 std::vector<sparta::Counter> bin_;
369 std::unique_ptr<sparta::StatisticDef> underflow_probability_;
370 std::unique_ptr<sparta::StatisticDef> overflow_probability_;
371 std::vector<std::unique_ptr<sparta::StatisticDef>> probabilities_;
372 std::unique_ptr<sparta::StatisticDef> average_;
374 std::vector<sparta::Counter> max_counters_;
375 std::multiset<uint64_t> max_values_;
382 uint32_t idx_shift_amount_;
383 mutable std::vector<double> bin_prob_vector_;
404 const std::string & stat_prefix,
407 uint32_t num_vals_per_bin,
408 uint32_t num_max_vals,
414 initializeStats_(sset, stat_prefix, bin_vis, prob_vis, num_max_vals, max_vis);
464 const std::string & histogram_name,
465 const std::string & description,
468 uint32_t num_vals_per_bin,
471 TreeNode(histogram_name, description),
475 initializeStats_(parent_treenode, 0, bin_vis, prob_vis,
480 const std::string & histogram_name,
481 const std::string & description,
482 const uint64_t lower_val,
483 const uint64_t upper_val,
484 const uint32_t num_vals_per_bin,
485 const uint32_t num_max_values,
488 TreeNode(histogram_name, description),
492 initializeStats_(parent_treenode, num_max_values, bin_vis, prob_vis,
496 std::string getDisplayStringCumulative()
const {
502 void initializeStats_(
TreeNode * parent_treenode,
503 const uint32_t num_max_values,
513 &sset_,
"", bin_vis, prob_vis, num_max_values, max_vis);
516 parent_treenode->addChild(
this);
526typedef HistogramTreeNode Histogram;
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....
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_LATEST
Counter holds the latest value (from most recent activity) and can increase or decrease at any time.
@ COUNT_NORMAL
Counter counts the number of times something happens like one would expect. This is a weakly monotoni...
Represents a counter of type counter_type (uint64_t). 2 and greater than 0 with a ceiling specified....
Histogram base class for uint64_t values.
double getOverflowProbability() const
Return overflow probability.
const sparta::Counter & getUnderflowBin() const
Return count of underflow bin.
const std::vector< double > & recomputeRegularBinProbabilities() const
Return vector of probabilities regular bins.
double getStandardDeviation() const
Calculate Standard Deviation of counts in bins. This API also takes into account the count in underfl...
const sparta::Counter & getAggValues() const
Return aggregate of this histogram.
double getUnderflowProbability() const
Return underflow probability.
const sparta::Counter & getOverflowBin() const
Return count of overflow bin.
void addValue(uint64_t val)
Add a value to histogram.
void initializeStats_(StatisticSet *sset, const std::string &stat_prefix="", InstrumentationNode::Visibility bin_vis=InstrumentationNode::VIS_NORMAL, InstrumentationNode::Visibility prob_vis=InstrumentationNode::VIS_NORMAL, uint32_t num_max_values=0, InstrumentationNode::Visibility max_vis=InstrumentationNode::VIS_SUMMARY)
const std::vector< sparta::Counter > & getRegularBin() const
Return vector of regular bin counts.
void updateMaxValues_(uint64_t val)
HistogramBase(uint64_t lower_val, uint64_t upper_val, uint32_t num_vals_per_bin)
HistogramBase constructor.
std::string getDisplayStringCumulative_(const std::string &name) const
Render the cumulative values of this histogram for use in standalone model.
double getMeanBinCount() const
Calculate the mean bin count of all the bins. This API also takes into account the count in underflow...
HistogramStandalone(StatisticSet *sset, const std::string &stat_prefix, uint64_t lower_val, uint64_t upper_val, uint32_t num_vals_per_bin, uint32_t num_max_vals, InstrumentationNode::Visibility bin_vis=InstrumentationNode::VIS_NORMAL, InstrumentationNode::Visibility prob_vis=InstrumentationNode::VIS_NORMAL, InstrumentationNode::Visibility max_vis=InstrumentationNode::VIS_NORMAL)
HistogramTreeNode(TreeNode *parent_treenode, const std::string &histogram_name, const std::string &description, uint64_t lower_val, uint64_t upper_val, uint32_t num_vals_per_bin, InstrumentationNode::Visibility bin_vis=InstrumentationNode::VIS_NORMAL, InstrumentationNode::Visibility prob_vis=InstrumentationNode::VIS_NORMAL)
HistogramTreeNode constructor.
HistogramTreeNode(HistogramTreeNode &&)=delete
Not move-constructable.
HistogramTreeNode(const HistogramTreeNode &)=delete
Not copy-constructable.
void operator=(const HistogramTreeNode &)=delete
Not assignable.
HistogramTreeNode()=delete
Not default constructable.
Visibility
Common visibility levels. Visibility is a continum (visibility can be anywhere in [0 to MAX_VISIBILIT...
@ VIS_SUMMARY
The next two visibility levels are for High-importance data. These are split up in the (100M,...
@ VIS_NORMAL
Normal visibility (default)
Contains a statistic definition (some useful information which can be computed)
@ VS_FRACTIONAL
A fractional number. This value should be in the range [0,1]. Some report formatters could show this ...
@ VS_ABSOLUTE
An absolute number having no units (typical default)
Set of StatisticDef and CounterBase-derived objects for visiblility through a sparta Tree.
CounterT & createCounter(_Args &&... __args)
Allocates a Counter which is owned by this StatisticSet and deleted at its destruction.
Node in a composite tree representing a sparta Tree item.
TreeNode()=delete
Not default-constructable.
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.