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 using statistics::expression::Expression;
30
36 {
37 public:
38
44 explicit ReversedStatisticRange(const std::string & reason) :
45 SpartaException(reason)
46 { }
47
51 virtual ~ReversedStatisticRange() noexcept {}
52 };
53
59 {
60 public:
61
67 explicit FutureStatisticRange(const std::string & reason) :
68 SpartaException(reason)
69 { }
70
74 virtual ~FutureStatisticRange() noexcept {}
75 };
76
94 {
98 StatisticInstance() = default;
99
111 const CounterBase* ctr,
112 const ParameterBase* par,
113 const TreeNode* n,
114 std::vector<const TreeNode*>* used);
115
116 public:
117
121
129 stat_expr_(expr)
130 { }
131
136 explicit StatisticInstance(const TreeNode* node) :
137 StatisticInstance(nullptr, nullptr, nullptr, node, nullptr)
138 { }
139
144 StatisticInstance(const TreeNode* node, std::vector<const TreeNode*>& used) :
145 StatisticInstance(nullptr, nullptr, nullptr, node, &used)
146 { }
147
155 StatisticInstance(std::shared_ptr<StatInstCalculator> & calculator,
156 std::vector<const TreeNode*>& used);
157
160
163
168
173 const std::vector<StatisticDef::PendingSubStatCreationInfo> & getSubStatistics() const {
174 return sub_statistics_;
175 }
176
179
182
186
195 void start();
196
205 void end();
206
212 return start_tick_;
213 }
214
221 return end_tick_;
222 }
223
226
230
236 void accumulateStatistic() const {
237 initial_.setIsCumulative(true);
238 std::vector<const StatisticInstance*> stats_in_expr;
239 stat_expr_.getStats(stats_in_expr);
240 for (const auto & stat : stats_in_expr) {
241 stat->accumulateStatistic();
242 }
243 }
244
255 double getValue() const;
256
260 double getInitial() const {
261 return initial_.getValue();
262 }
263
269 double getRawLatest() const;
270
275
286 std::string stringize(bool show_range=true,
287 bool resolve_subexprs=true) const;
288
300 std::string getExpressionString(bool show_range=true,
301 bool resolve_subexprs=true) const;
302
311 std::string getDesc(bool show_stat_node_expressions) const;
312
320 void dump(std::ostream& o, bool show_range=false) const;
321
328 {
329 snapshot_loggers_.emplace_back(snapshot);
330 }
331
336 {
337 snapshot_loggers_.clear();
338 }
339
342
346
355 std::string getLocation() const;
356
365
371
377
382 return sdef_;
383 }
384 const StatisticDef* getStatisticDef() const {
385 return sdef_;
386 }
387
393 return stat_expr_;
394 }
395
400 return ctr_;
401 }
402 const CounterBase* getCounter() const {
403 return ctr_;
404 }
405
410 return par_;
411 }
412 const ParameterBase* getParameter() const {
413 return par_;
414 }
415
425 void getClocks(std::vector<const Clock*>& clocks) const;
426
429
434 void setContext(const TreeNode * context) {
435 sparta_assert(nullptr != context->getClock());
436 scheduler_ = context->getClock()->getScheduler();
437 sparta_assert(nullptr != scheduler_);
438 }
439
444 void setContext(const Scheduler * scheduler) {
445 scheduler_ = scheduler;
446 sparta_assert(nullptr != scheduler_);
447 }
448
449 private:
450
457 double computeValue_() const;
458
463 void addSubStatistic_(const StatisticDef::PendingSubStatCreationInfo & creation_info) const {
464 sub_statistics_.emplace_back(creation_info);
465 }
466
472 TreeNode::ConstWeakPtr node_ref_;
473
479 const StatisticDef* sdef_ = nullptr;
480
485 const CounterBase* ctr_ = nullptr;
486
491 const ParameterBase* par_ = nullptr;
492
502
506 Scheduler::Tick start_tick_{0};
507
514
518 mutable const Scheduler * scheduler_ = nullptr;
519
523 const Scheduler * getScheduler_() const;
524
531 class InitialStatValue {
532 public:
533 InitialStatValue(const double value) :
534 is_cumulative_(false),
535 initial_(value)
536 {
537 if (std::isnan(initial_.getValue())) {
538 initial_.clearValid();
539 }
540 }
541
542 InitialStatValue(const InitialStatValue & rhp) :
543 is_cumulative_(rhp.is_cumulative_),
544 initial_(rhp.initial_)
545 {}
546
547 InitialStatValue & operator=(const InitialStatValue & rhp) {
548 is_cumulative_ = rhp.is_cumulative_;
549 initial_ = rhp.initial_;
550 return *this;
551 }
552
553 void setIsCumulative(const bool is_cumulative) {
554 is_cumulative_ = is_cumulative;
555 }
556
557 double getValue() const {
558 return initial_.isValid() ? initial_.getValue() : 0;
559 }
560
561 void resetValue(const double initial) {
562 if (is_cumulative_) {
563 if (!initial_.isValid()) {
564 initial_ = initial;
565 }
566 return;
567 }
568 initial_ = initial;
569 }
570
571 private:
572 bool is_cumulative_ = false;
573 utils::ValidValue<double> initial_;
574 };
575
579 mutable InitialStatValue initial_{NAN};
580
584 double result_{NAN};
585
589 mutable std::vector<statistics::StatisticSnapshot> snapshot_loggers_;
590
594 mutable std::vector<StatisticDef::PendingSubStatCreationInfo> sub_statistics_;
595
599 std::shared_ptr<StatInstCalculator> user_calculated_si_value_;
600 }; // class StatisticInstance
601
602
604 inline std::ostream& operator<< (std::ostream& out,
605 sparta::StatisticInstance const & si) {
606 out << si.stringize();
607 return out;
608 }
609
611 inline std::ostream& operator<< (std::ostream& out,
612 sparta::StatisticInstance const * si) {
613 if(nullptr == si){
614 out << "null";
615 }else{
616 out << si->stringize();
617 }
618 return out;
619 }
620
621} // 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:289
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.
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),...
StatisticInstance(const StatisticInstance &rhp)
Copy Constructor.
InstrumentationNode::class_t getClass() const
Gets the Class associated with this statistic instance.
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.
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 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.
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(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
const Clock * getClock() override
Walks up parents (starting with self) until a parent with an associated local clock is found,...
std::weak_ptr< const TreeNode > ConstWeakPtr
Weak pointer to a const TreeNode. Acquire with getWeakPtr.
Definition TreeNode.hpp:271
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.
const value_type & getValue() const
Get the value - const version.
bool isValid() const
Is this value valid.
Macros for handling exponential backoff.
std::ostream & operator<<(std::ostream &o, const SimulationInfo &info)
ostream insertion operator for SimulationInfo