The Sparta Modeling Framework
Loading...
Searching...
No Matches
StatisticInstance.hpp
Go to the documentation of this file.
1// <StatisticInstance.hpp> -*- C++ -*-
2
9#pragma once
10
11#include <iostream>
12#include <sstream>
13#include <math.h>
14#include <utility>
15
18#include "sparta/statistics/CounterBase.hpp"
23#include "sparta/statistics/Expression.hpp"
25#include "sparta/statistics/dispatch/StatisticSnapshot.hpp"
26
27namespace sparta
28{
29 //Forward declarations related to SimDB
30 class StatInstValueLookup;
31 class StatInstRowIterator;
32 using statistics::expression::Expression;
33
39 {
40 public:
41
47 explicit ReversedStatisticRange(const std::string & reason) :
48 SpartaException(reason)
49 { }
50
54 virtual ~ReversedStatisticRange() noexcept {}
55 };
56
62 {
63 public:
64
70 explicit FutureStatisticRange(const std::string & reason) :
71 SpartaException(reason)
72 { }
73
77 virtual ~FutureStatisticRange() noexcept {}
78 };
79
97 {
101 StatisticInstance() = default;
102
114 const CounterBase* ctr,
115 const ParameterBase* par,
116 const TreeNode* n,
117 std::vector<const TreeNode*>* used);
118
119 public:
120
124
132 stat_expr_(expr)
133 { }
134
139 explicit StatisticInstance(const TreeNode* node) :
140 StatisticInstance(nullptr, nullptr, nullptr, node, nullptr)
141 { }
142
147 StatisticInstance(const TreeNode* node, std::vector<const TreeNode*>& used) :
148 StatisticInstance(nullptr, nullptr, nullptr, node, &used)
149 { }
150
158 StatisticInstance(std::shared_ptr<StatInstCalculator> & calculator,
159 std::vector<const TreeNode*>& used);
160
163
166
172 StatisticInstance(const std::string & location,
173 const std::string & description,
174 const std::string & expression_str,
175 const StatisticDef::ValueSemantic value_semantic,
176 const InstrumentationNode::visibility_t visibility,
178 const std::vector<std::pair<std::string, std::string>> & metadata = {});
179
187 const std::string & location,
188 const std::string & description,
189 const std::shared_ptr<StatInstCalculator> & calculator,
192 const std::vector<std::pair<std::string, std::string>> & metadata = {});
193
198
203 const std::vector<StatisticDef::PendingSubStatCreationInfo> & getSubStatistics() const {
204 return sub_statistics_;
205 }
206
209
212
216
225 void start();
226
235 void end();
236
242 return start_tick_;
243 }
244
251 return end_tick_;
252 }
253
256
260
266 void accumulateStatistic() const {
267 initial_.setIsCumulative(true);
268 std::vector<const StatisticInstance*> stats_in_expr;
269 stat_expr_.getStats(stats_in_expr);
270 for (const auto & stat : stats_in_expr) {
271 stat->accumulateStatistic();
272 }
273 }
274
288 const std::shared_ptr<sparta::StatInstValueLookup> & direct_lookup);
289
298 const StatInstRowIterator & si_row_iterator);
299
306
317 double getValue() const;
318
322 double getInitial() const {
323 return initial_.getValue();
324 }
325
331 double getRawLatest() const;
332
337
348 std::string stringize(bool show_range=true,
349 bool resolve_subexprs=true) const;
350
362 std::string getExpressionString(bool show_range=true,
363 bool resolve_subexprs=true) const;
364
373 std::string getDesc(bool show_stat_node_expressions) const;
374
382 void dump(std::ostream& o, bool show_range=false) const;
383
390 {
391 snapshot_loggers_.emplace_back(snapshot);
392 }
393
398 {
399 snapshot_loggers_.clear();
400 }
401
404
408
417 std::string getLocation() const;
418
427
433
439
445 const std::vector<std::pair<std::string, std::string>> & getMetadata() const {
446 return provided_metadata_;
447 }
448
453 return sdef_;
454 }
455 const StatisticDef* getStatisticDef() const {
456 return sdef_;
457 }
458
464 return stat_expr_;
465 }
466
471 return ctr_;
472 }
473 const CounterBase* getCounter() const {
474 return ctr_;
475 }
476
481 return par_;
482 }
483 const ParameterBase* getParameter() const {
484 return par_;
485 }
486
496 void getClocks(std::vector<const Clock*>& clocks) const;
497
500
505 void setContext(const TreeNode * context) {
506 sparta_assert(nullptr != context->getClock());
507 scheduler_ = context->getClock()->getScheduler();
508 sparta_assert(nullptr != scheduler_);
509 }
510
515 void setContext(const Scheduler * scheduler) {
516 scheduler_ = scheduler;
517 sparta_assert(nullptr != scheduler_);
518 }
519
520 private:
521
528 double computeValue_() const;
529
541 double getCurrentValueFromDirectLookup_() const;
542
547 void addSubStatistic_(const StatisticDef::PendingSubStatCreationInfo & creation_info) const {
548 sub_statistics_.emplace_back(creation_info);
549 }
550
556 TreeNode::ConstWeakPtr node_ref_;
557
563 const StatisticDef* sdef_ = nullptr;
564
569 const CounterBase* ctr_ = nullptr;
570
575 const ParameterBase* par_ = nullptr;
576
586
590 Scheduler::Tick start_tick_{0};
591
598
602 mutable const Scheduler * scheduler_ = nullptr;
603
607 const Scheduler * getScheduler_() const;
608
615 class InitialStatValue {
616 public:
617 InitialStatValue(const double value) :
618 is_cumulative_(false),
619 initial_(value)
620 {
621 if (std::isnan(initial_.getValue())) {
622 initial_.clearValid();
623 }
624 }
625
626 InitialStatValue(const InitialStatValue & rhp) :
627 is_cumulative_(rhp.is_cumulative_),
628 initial_(rhp.initial_)
629 {}
630
631 InitialStatValue & operator=(const InitialStatValue & rhp) {
632 is_cumulative_ = rhp.is_cumulative_;
633 initial_ = rhp.initial_;
634 return *this;
635 }
636
637 void setIsCumulative(const bool is_cumulative) {
638 is_cumulative_ = is_cumulative;
639 }
640
641 double getValue() const {
642 return initial_.isValid() ? initial_.getValue() : 0;
643 }
644
645 void resetValue(const double initial) {
646 if (is_cumulative_) {
647 if (!initial_.isValid()) {
648 initial_ = initial;
649 }
650 return;
651 }
652 initial_ = initial;
653 }
654
655 private:
656 bool is_cumulative_ = false;
657 utils::ValidValue<double> initial_;
658 };
659
663 mutable InitialStatValue initial_{NAN};
664
668 double result_{NAN};
669
673 mutable std::vector<statistics::StatisticSnapshot> snapshot_loggers_;
674
678 mutable std::vector<StatisticDef::PendingSubStatCreationInfo> sub_statistics_;
679
683 std::shared_ptr<StatInstCalculator> user_calculated_si_value_;
684
694 std::shared_ptr<sparta::StatInstValueLookup> direct_lookup_si_value_;
695
707 utils::ValidValue<std::string> provided_location_;
708 utils::ValidValue<std::string> provided_description_;
709 utils::ValidValue<std::string> provided_expr_string_;
710 utils::ValidValue<StatisticDef::ValueSemantic> provided_value_semantic_;
711 utils::ValidValue<InstrumentationNode::visibility_t> provided_visibility_;
712 utils::ValidValue<InstrumentationNode::class_t> provided_class_;
713 std::vector<std::pair<std::string, std::string>> provided_metadata_;
714
715 }; // class StatisticInstance
716
717
719 inline std::ostream& operator<< (std::ostream& out,
720 sparta::StatisticInstance const & si) {
721 out << si.stringize();
722 return out;
723 }
724
726 inline std::ostream& operator<< (std::ostream& out,
727 sparta::StatisticInstance const * si) {
728 if(nullptr == si){
729 out << "null";
730 }else{
731 out << si->stringize();
732 }
733 return out;
734 }
735
736} // namespace sparta
File that defines the Clock class.
Individual Parameter interface base class, container class, and global helpers methods.
A simple time-based, event precedence based scheduler.
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.
Exception class for all of Sparta.
Contains a statistic definition (some useful information which can be computed)
Scheduler * getScheduler() const
Definition Clock.hpp:302
The base class for all Counters.
Exception indicating that the range of a StatisticInstance starts or ends in the future (probably cau...
FutureStatisticRange(const std::string &reason)
Construct with a default string.
virtual ~FutureStatisticRange() noexcept
Destructor.
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.
static constexpr class_t DEFAULT_CLASS
Default node class.
uint32_t class_t
Continuous Class level. Several key points along continum are indicated within Class.
Non-templated base class for generic parameter access and iteration.
Exception indicating that the range of a StatisticInstance was reversed when it was accessed (probabl...
ReversedStatisticRange(const std::string &reason)
Construct with a default string.
virtual ~ReversedStatisticRange() noexcept
Destructor.
A class that lets you schedule events now and in the future.
uint64_t Tick
Typedef for our unit of time.
static const Tick INDEFINITE
Constant for infinite tick count.
Used to construct and throw a standard C++ exception. Inherits from std::exception.
This helper class serves as a placeholder for substatistic creation.
Contains a statistic definition (some useful information which can be computed)
ValueSemantic
How should the value of this statistic be interpreted Certain outputters (e.g. report formatters) may...
Instance of either a StatisticDef or CounterBase or an Expression. Has a sample window (simulator tic...
void accumulateStatistic() const
Tell this statistic to continually accumulate statistic values, always subtracting out the statistic ...
const std::vector< StatisticDef::PendingSubStatCreationInfo > & getSubStatistics() const
Get this statistic instance's list of pending substatistic information (TreeNode* and stat name),...
bool isSIValueDirectLookupValid() const
If this SI is using a StatInstValueLookup object to get its SI values, ask if this direct-lookup obje...
StatisticInstance(const StatisticInstance &rhp)
Copy Constructor.
InstrumentationNode::class_t getClass() const
Gets the Class associated with this statistic instance.
const std::vector< std::pair< std::string, std::string > > & getMetadata() const
Give the reporting infrastructure access to all metadata that has been set. The database report write...
StatisticInstance(const TreeNode *node, std::vector< const TreeNode * > &used)
Construct with a StatisticDef or Counter as a TreeNode*.
const CounterBase * getCounter()
std::string getExpressionString(bool show_range=true, bool resolve_subexprs=true) const
Returns a string containing the expression that this statistic will evaluate.
StatisticInstance(const std::string &location, const std::string &description, const std::string &expression_str, const StatisticDef::ValueSemantic value_semantic, const InstrumentationNode::visibility_t visibility, const InstrumentationNode::class_t cls, const std::vector< std::pair< std::string, std::string > > &metadata={})
Construct a StatisticInstance with its metadata values set directly, as opposed to this SI asking its...
void dump(std::ostream &o, bool show_range=false) const
Renders this StatisticInstance to a string containing computation window, source, and current value.
std::string stringize(bool show_range=true, bool resolve_subexprs=true) const
Renders this StatisticInstance to a string containing computation window, source, and current value.
void addSnapshotLogger(statistics::StatisticSnapshot &snapshot) const
Allow this statistic instance to emit statistic value snapshots for observation purposes....
const ParameterBase * getParameter()
StatisticInstance(StatisticInstance &&rhp)
Move Constructor.
void start()
Start the computation window for this instance.
double getInitial() const
Returns the initial value of this instance at start_tick_.
void setSIValueDirectLookupPlaceholder(const std::shared_ptr< sparta::StatInstValueLookup > &direct_lookup)
During SimDB->report generation, there is a notion of "placeholder" objects which get set on Statisti...
void getClocks(std::vector< const Clock * > &clocks) const
Gets all clocks associated with this Statistic instance (if any) whether it points to a StatisticDef,...
~StatisticInstance()
Non-Virtual destructor.
Scheduler::Tick getEnd() const
Returns the time at which ths computation window was ended.
StatisticInstance & operator=(const StatisticInstance &rhp)
Assignment Operator.
void setContext(const TreeNode *context)
Set the context of this StatisticInstance (sets the scheduler) based on a TreeNode.
const Expression & getStatisticExpression() const
Get the underlying expression representing this SI.
void end()
Ends the window for this instance. Computes and caches the result of the statistic.
void disableSnapshotLogging() const
Remove any SI value loggers we may have been given.
std::string getDesc(bool show_stat_node_expressions) const
Returns a string that describes the statistic instance If this instance points to a TreeNode,...
double getRawLatest() const
Returns the Raw latest value of the this instance for whatever statistic or counter it contains....
StatisticDef::ValueSemantic getValueSemantic() const
Gets the statistic value semantic associated with this statistic instance.
void realizeSIValueDirectLookup(const StatInstRowIterator &si_row_iterator)
Our StatInstValueLookup placeholder object needs to bind itself to a StatInstRowIterator object,...
const StatisticDef * getStatisticDef()
Returns the StatisticDef used to compute this statistic.
StatisticInstance(statistics::expression::Expression &&expr)
Construction with a predefined expression.
double getValue() const
Returns the value computed for this statistic instance at the current time.
InstrumentationNode::visibility_t getVisibility() const
Gets the visibility associated with this statistic instance.
std::string getLocation() const
Get the location associated with this statistic instance.
StatisticInstance(const TreeNode *node)
Construct with a StatisticDef or Counter as a TreeNode*.
StatisticInstance(const std::string &location, const std::string &description, const std::shared_ptr< StatInstCalculator > &calculator, const InstrumentationNode::visibility_t visibility=InstrumentationNode::DEFAULT_VISIBILITY, const InstrumentationNode::class_t cls=InstrumentationNode::DEFAULT_CLASS, const std::vector< std::pair< std::string, std::string > > &metadata={})
Construct a StatisticInstance with its location and description set directly, along with a StatInstCa...
StatisticInstance(std::shared_ptr< StatInstCalculator > &calculator, std::vector< const TreeNode * > &used)
Construct with a StatInstCalculator function (wrapper around a SpartaHandler)
void setContext(const Scheduler *scheduler)
Set the Scheduler context of this StatisticInstance.
bool supportsCompression() const
Scheduler::Tick getStart() const
Returns the time at which this computation window was started. If started multiple times,...
Node in a composite tree representing a sparta Tree item.
Definition TreeNode.hpp:205
std::weak_ptr< const TreeNode > ConstWeakPtr
Weak pointer to a const TreeNode. Acquire with getWeakPtr.
Definition TreeNode.hpp:271
const Clock * getClock() override
Walks up parents (starting with self) until a parent with an associated local clock is found,...
User-friendly wrapper around a double reference. This is like a std::reference_wrapper that connects ...
Expression container/builder. Contains a single ExpressionNode representing the root of an expression...
uint32_t getStats(std::vector< const StatisticInstance * > &results) const
Gets the statistics present in this expression.
Macros for handling exponential backoff.
std::ostream & operator<<(std::ostream &o, const SimulationInfo &info)
ostream insertion operator for SimulationInfo