The Sparta Modeling Framework
Loading...
Searching...
No Matches
StatisticDef.hpp
Go to the documentation of this file.
1// <StatisticDef> -*- C++ -*-
2
3
10#pragma once
11
12#include <iostream>
13#include <sstream>
14#include <cstdarg>
15#include <memory>
16#include <string>
17#include <type_traits>
18#include <utility>
19#include <vector>
20
25#include "sparta/utils/StringManager.hpp"
26#include "sparta/statistics/Expression.hpp"
27
28namespace sparta
29{
30namespace trigger {
31class ContextCounterTrigger;
32} // namespace trigger
33
39 {
40 public:
41
45
53
54 ExpressionArg(const std::string& str) :
55 expr_str(str)
56 {;}
57
58 ExpressionArg(const char str[]) :
59 expr_str(str)
60 {;}
61
64 expr_str("")
65 {;}
66
67 ExpressionArg(const ExpressionArg& rhp) :
68 expr_obj(rhp.expr_obj ? new sparta::statistics::expression::Expression(*rhp.expr_obj) : nullptr),
69 expr_str(rhp.expr_str)
70 {;}
71
72 std::unique_ptr<sparta::statistics::expression::Expression> expr_obj;
73 std::string expr_str;
74 };
75
83
89
94
102
108 VS_FRACTIONAL = 3
109
111 // * \brief A number (typically an absolute number) having specific
112 // * units (i.e. not a percentage/fraction). See CounterBase::getUnits
113 // * and StatisticDef::getUnits.
114 // */
115 //VS_UNITS = 4
116 };
117
120
124
126 StatisticDef(const StatisticDef&) = delete;
127
129 StatisticDef(const StatisticDef&&) = delete;
130
136 InstrumentationNode(std::move(rhp)),
137 prebuilt_expr_(std::move(rhp.prebuilt_expr_)),
138 expr_str_(rhp.expr_str_),
139 context_(rhp.context_),
140 semantic_(rhp.semantic_)
141
142 {
143 // Note: this must happen in leaf type
144 TreeNode* parent = rhp.getParent();
145 if(parent != nullptr){
146 parent->addChild(this);
147 }
148 }
149
152
172 const std::string& name,
173 const std::string& group,
174 group_idx_type group_idx,
175 const std::string& desc,
176 TreeNode* context,
177 ExpressionArg expression,
178 ValueSemantic semantic,
179 visibility_t visibility) :
181 group,
182 group_idx,
183 desc,
185 visibility),
186 prebuilt_expr_(std::move(expression.expr_obj)),
187 expr_str_(std::move(expression.expr_str)),
188 context_(context),
189 semantic_(semantic)
190 {
191 setExpectedParent_(parent);
192
193 sparta_assert(semantic_ != VS_INVALID,
194 "Cannot construct a StatisticDef with VS_INVALID value semantic");
195
196 if(prebuilt_expr_ != nullptr){
197 expr_str_ = prebuilt_expr_->stringize();
198 }else{
199 if(context_ == nullptr){
200 throw SpartaException("When constructing StatisticDef")
201 << getLocation() << " context must not be nullptr. It must be a TreeNode which "
202 "will be used to look up any Node names found in the expression";
203 }
204
205 if(expr_str_ == ""){
206 throw SpartaException("When constructing StatisticDef ")
207 << getLocation() << " without a prebuilt expression, the expression string "
208 "must not be \"\". It must be a non-empty string containing an arithmetic "
209 "expression referring to nodes relative to the context \""
210 << context_->getLocation() << "\"";
211 }
212 }
213
214 ensureParentIsStatisticSet_(parent);
215
216 // Other initialization here
217 // ...
218
219 if(parent){
220 parent->addChild(this);
221 }
222 }
223
227 StatisticDef(const std::string& name,
228 const std::string& group,
229 group_idx_type group_idx,
230 const std::string& desc,
231 TreeNode* context,
232 ExpressionArg expression,
233 ValueSemantic semantic,
234 visibility_t visibility) :
235 StatisticDef(nullptr,
236 name,
237 group,
238 group_idx,
239 desc,
240 context,
241 expression,
242 semantic,
243 visibility)
244 {
245 // Delegated constructor
246 }
247
256 const std::string& name,
257 const std::string& desc,
258 TreeNode* context,
259 ExpressionArg expression,
260 ValueSemantic semantic,
261 visibility_t visibility) :
262 StatisticDef(parent,
263 name,
266 desc,
267 context,
268 expression,
269 semantic,
270 visibility)
271 {
272 // Delegated constructor
273 }
274
282 StatisticDef(const std::string& name,
283 const std::string& desc,
284 TreeNode* context,
285 ExpressionArg expression,
286 ValueSemantic semantic,
287 visibility_t visibility) :
288 StatisticDef(nullptr,
289 name,
292 desc,
293 context,
294 expression,
295 semantic,
296 visibility)
297 {
298 // Delegated Constructor
299 }
300
308 const std::string& name,
309 const std::string& group,
310 group_idx_type group_idx,
311 const std::string& desc,
312 TreeNode* context,
313 ExpressionArg expression) :
314 StatisticDef(parent,
315 name,
316 group,
317 group_idx,
318 desc,
319 context,
320 expression,
323 {
324 // Delegated constructor
325 }
326
330 StatisticDef(const std::string& name,
331 const std::string& group,
332 group_idx_type group_idx,
333 const std::string& desc,
334 TreeNode* context,
335 ExpressionArg expression) :
336 StatisticDef(nullptr,
337 name,
338 group,
339 group_idx,
340 desc,
341 context,
342 expression)
343 {
344 // Delegated constructor
345 }
346
355 const std::string& name,
356 const std::string& desc,
357 TreeNode* context,
358 ExpressionArg expression) :
359 StatisticDef(parent,
360 name,
363 desc,
364 context,
365 expression)
366 {
367 // Delegated constructor
368 }
369
377 StatisticDef(const std::string& name,
378 const std::string& desc,
379 TreeNode* context,
380 ExpressionArg expression) :
381 StatisticDef(nullptr,
382 name,
385 desc,
386 context,
387 expression)
388 {
389 // Delegated Constructor
390 }
391
396 const std::string& name,
397 const std::string& group,
398 group_idx_type group_idx,
399 const std::string& desc,
400 TreeNode* context,
401 ExpressionArg expression,
402 ValueSemantic semantic) :
403 StatisticDef(parent,
404 name,
405 group,
406 group_idx,
407 desc,
408 context,
409 expression,
410 semantic,
412 {
413 // Delegated constructor
414 }
415
419 StatisticDef(const std::string& name,
420 const std::string& group,
421 group_idx_type group_idx,
422 const std::string& desc,
423 TreeNode* context,
424 ExpressionArg expression,
425 ValueSemantic semantic) :
426 StatisticDef(nullptr,
427 name,
428 group,
429 group_idx,
430 desc,
431 context,
432 expression,
433 semantic)
434 {
435 // Delegated constructor
436 }
437
446 const std::string& name,
447 const std::string& desc,
448 TreeNode* context,
449 ExpressionArg expression,
450 ValueSemantic semantic) :
451 StatisticDef(parent,
452 name,
455 desc,
456 context,
457 expression,
458 semantic)
459 {
460 // Delegated constructor
461 }
462
470 StatisticDef(const std::string& name,
471 const std::string& desc,
472 TreeNode* context,
473 ExpressionArg expression,
474 ValueSemantic semantic) :
475 StatisticDef(nullptr,
476 name,
479 desc,
480 context,
481 expression,
482 semantic)
483 {
484 // Delegated Constructor
485 }
486
491 { }
492
495
499
505 {
506 public:
507 PendingSubStatCreationInfo(const TreeNode * stat_node,
508 const std::string & stat_name) :
509 stat_node_(stat_node),
510 stat_name_(stat_name)
511 {}
512 const TreeNode * getNode() const {
513 return stat_node_;
514 }
515 const std::string & getName() const {
516 return stat_name_;
517 }
518 private:
519 const TreeNode * stat_node_ = nullptr;
520 std::string stat_name_;
521 };
522
523 inline const std::vector<PendingSubStatCreationInfo> & getSubStatistics() const {
524 return sub_statistics_;
525 }
526
533 std::string getContextLocation() const {
534 return context_ ? context_->getLocation() : "";
535 }
536
537 private:
538
544 void deregisterAggregationFcnUponDestruction_() const {
545 //It is possible that our auto-deregister object has been set already, which
546 //happens when one ContextCounter has more than one registered aggregation
547 //callback. So we need to check for null or we will inadvertently deregister
548 //our aggregation callback too soon (calling .reset on a non-null unique_ptr
549 //will trigger the AutoContextCounterDeregistration object's destructor of
550 //course).
551 if (auto_cc_deregister_ == nullptr) {
552 auto_cc_deregister_.reset(new AutoContextCounterDeregistration(this));
553 }
554 }
555
559
560 protected:
561
568 inline void addSubStatistic_(const TreeNode * stat_node,
569 const std::string & stat_name) const {
570 sub_statistics_.emplace_back(stat_node, stat_name);
571 }
572
573 public:
574
581 std::string getExpression() const {
582 return expr_str_;
583 }
584
594 realizeExpression(std::vector<const TreeNode*>& used) const {
595 if(prebuilt_expr_){
596 return *prebuilt_expr_; // Returns a copy
597 }
598 // This is deferred until this point because the expression can
599 // contain variables populated using the 'used' vector
600 return sparta::statistics::expression::Expression(expr_str_, context_, used);
601 }
602
608 return semantic_;
609 }
610
613
617
618 // Override from TreeNode
619 virtual std::string stringize(bool pretty=false) const override {
620 (void) pretty;
621 std::stringstream ss;
622 ss << '<' << getLocation() << " expr:" << getExpression();
623 ss << " vis:" << getVisibility();
624 stringizeTags(ss);
625 ss << '>';
626 return ss.str();
627 }
628
631
632private:
633
639 void ensureParentIsStatisticSet_(TreeNode*);
640
645 virtual void validateNode_() const override {
646 if(prebuilt_expr_){
647 // Guaranteed OK because expression was built before this node was constructed
648 }else{
649 try{
651 }catch(SpartaException &ex){
652 throw SpartaException("Failed to validate StatisticDef: \"") << getLocation()
653 << "\": " << ex.what();
654 }
655 }
656 }
657
663 std::unique_ptr<sparta::statistics::expression::Expression> prebuilt_expr_;
664
672 std::string expr_str_;
673
677 TreeNode* const context_;
678
682 const ValueSemantic semantic_;
683
688 mutable std::vector<PendingSubStatCreationInfo> sub_statistics_;
689
695 class AutoContextCounterDeregistration {
696 public:
697 explicit AutoContextCounterDeregistration(const StatisticDef * sd);
698 ~AutoContextCounterDeregistration();
699 private:
700 const StatisticDef *const sd_;
701 };
702
703 mutable std::unique_ptr<AutoContextCounterDeregistration> auto_cc_deregister_;
704 };
705
706} // namespace sparta
707
Virtual interface node for simulator instrumentation (e.g. counters, stats, nontifications).
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.
Basic Node framework in sparta device tree composite pattern.
@ TYPE_STATISTICDEF
Statisitic definition.
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.
visibility_t getVisibility() const
Gets the visibility hint of this node. This is invariant after construction.
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)
virtual std::string stringize(bool pretty=false) const override
Create a string representation of this node.
std::string getExpression() const
Returns a reference to the expression string which this node was constructed with or a rendering of t...
friend class trigger::ContextCounterTrigger
StatisticDef(const std::string &name, const std::string &group, group_idx_type group_idx, const std::string &desc, TreeNode *context, ExpressionArg expression, ValueSemantic semantic)
Constructor.
sparta::statistics::expression::Expression realizeExpression(std::vector< const TreeNode * > &used) const
Returns a unique Expression for this StatisticInstance given a set of substitutions that the expressi...
StatisticDef(const std::string &name, const std::string &desc, TreeNode *context, ExpressionArg expression)
TreeNode constructor with no parent node or group information.
StatisticDef(const StatisticDef &)=delete
Not copy-constructable.
StatisticDef(TreeNode *parent, const std::string &name, const std::string &group, group_idx_type group_idx, const std::string &desc, TreeNode *context, ExpressionArg expression, ValueSemantic semantic, visibility_t visibility)
Constructor with string expression.
void addSubStatistic_(const TreeNode *stat_node, const std::string &stat_name) const
Allow subclasses to forward along substatistic information to this stat definition....
StatisticDef(TreeNode *parent, const std::string &name, const std::string &group, group_idx_type group_idx, const std::string &desc, TreeNode *context, ExpressionArg expression, ValueSemantic semantic)
Constructor.
ValueSemantic getValueSemantic() const
Retuns the value-semantic associated with this node at construction.
StatisticDef & operator=(const StatisticDef &)=delete
Not assign-constructable.
StatisticDef(const StatisticDef &&)=delete
Not move-constructable (too complicated)
StatisticDef(TreeNode *parent, const std::string &name, const std::string &desc, TreeNode *context, ExpressionArg expression, ValueSemantic semantic, visibility_t visibility)
Constructor.
StatisticDef(const std::string &name, const std::string &group, group_idx_type group_idx, const std::string &desc, TreeNode *context, ExpressionArg expression, ValueSemantic semantic, visibility_t visibility)
Constructor.
ValueSemantic
How should the value of this statistic be interpreted Certain outputters (e.g. report formatters) may...
@ VS_INVALID
Invalid semantic. No StatisticDef should have this value semantic.
@ VS_FRACTIONAL
A fractional number. This value should be in the range [0,1]. Some report formatters could show this ...
@ VS_PERCENTAGE
A percentage. This value should be in the range [0,100]. Some report formatters may add a '' when dis...
@ VS_ABSOLUTE
An absolute number having no units (typical default)
StatisticDef(TreeNode *parent, const std::string &name, const std::string &desc, TreeNode *context, ExpressionArg expression)
Constructor.
StatisticDef(const std::string &name, const std::string &desc, TreeNode *context, ExpressionArg expression, ValueSemantic semantic)
TreeNode constructor with no parent node or group information.
std::string getContextLocation() const
Get the TreeNode location where this StatisticDef lives. Returns an empty string if the TreeNode* giv...
StatisticDef(const std::string &name, const std::string &desc, TreeNode *context, ExpressionArg expression, ValueSemantic semantic, visibility_t visibility)
TreeNode constructor with no parent node or group information.
StatisticDef(const std::string &name, const std::string &group, group_idx_type group_idx, const std::string &desc, TreeNode *context, ExpressionArg expression)
Constructor.
StatisticDef(TreeNode *parent, const std::string &name, const std::string &desc, TreeNode *context, ExpressionArg expression, ValueSemantic semantic)
Constructor.
StatisticDef(StatisticDef &&rhp)
Move constructor.
StatisticDef(TreeNode *parent, const std::string &name, const std::string &group, group_idx_type group_idx, const std::string &desc, TreeNode *context, ExpressionArg expression)
Constructor.
virtual ~StatisticDef()
Virtual destructor.
Node in a composite tree representing a sparta Tree item.
Definition TreeNode.hpp:205
static const group_idx_type GROUP_IDX_NONE
GroupIndex indicating that a node has no group index because it belongs to no group.
Definition TreeNode.hpp:303
std::string getLocation() const override final
static constexpr char GROUP_NAME_NONE[]
Group name indicating that a node belongs to no group.
Definition TreeNode.hpp:314
TreeNode()=delete
Not default-constructable.
void addChild(TreeNode *child, bool inherit_phase=true)
Adds a TreeNode to this node as a child.
virtual TreeNode * getParent()
Gets immediate parent of this node if one exists.
Definition TreeNode.hpp:965
uint32_t group_idx_type
Index within a group.
Definition TreeNode.hpp:261
void stringizeTags(std::stringstream &ss) const
Render tags to a string in the form: " tags:[tag0, tag1]" If there are any tags. The leading space ma...
Definition TreeNode.hpp:743
void setExpectedParent_(const TreeNode *parent)
Tracks a node as an expected parent without actually adding this node as a child. This is used almost...
Expression container/builder. Contains a single ExpressionNode representing the root of an expression...
Macros for handling exponential backoff.
Intermediate type for minimizing the number of distinct constructors that must be created for this cl...