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{
30
31 namespace trigger {
32 class ContextCounterTrigger;
33 } // namespace trigger
34
40 {
41 public:
42
46
54
55 ExpressionArg(const std::string& str) :
56 expr_str(str)
57 {;}
58
59 ExpressionArg(const char str[]) :
60 expr_str(str)
61 {;}
62
65 expr_str("")
66 {;}
67
68 ExpressionArg(const ExpressionArg& rhp) :
69 expr_obj(rhp.expr_obj ? new sparta::statistics::expression::Expression(*rhp.expr_obj) : nullptr),
70 expr_str(rhp.expr_str)
71 {;}
72
73 std::unique_ptr<sparta::statistics::expression::Expression> expr_obj;
74 std::string expr_str;
75 };
76
84
90
95
103
109 VS_FRACTIONAL = 3
110
112 // * \brief A number (typically an absolute number) having specific
113 // * units (i.e. not a percentage/fraction). See CounterBase::getUnits
114 // * and StatisticDef::getUnits.
115 // */
116 //VS_UNITS = 4
117 };
118
163 class ValueSemanticDetailed
164 {
165 public:
172 ValueSemanticDetailed(semantic, {})
173 {}
174
181 const InstrumentationNode::MetadataPairs & meta_data) :
182 semantic_(semantic),
183 meta_data_(meta_data)
184 {}
187 operator ValueSemantic() const { return semantic_; }
190 const InstrumentationNode::MetadataPairs & getMetadata() const {
191 return meta_data_;
192 }
193
194 private:
195 const ValueSemantic semantic_;
196 const InstrumentationNode::MetadataPairs meta_data_;
197 };
198
201
207 StatisticDef(const StatisticDef&) = delete;
210 StatisticDef(const StatisticDef&&) = delete;
211
217 InstrumentationNode(std::move(rhp)),
218 prebuilt_expr_(std::move(rhp.prebuilt_expr_)),
219 expr_str_(rhp.expr_str_),
220 context_(rhp.context_),
221 semantic_(rhp.semantic_)
222 {
223 // Note: this must happen in leaf type
224 TreeNode* parent = rhp.getParent();
225 if(parent != nullptr){
226 parent->addChild(this);
227 }
228 }
231 StatisticDef& operator=(const StatisticDef&) = delete;
232
251 StatisticDef(TreeNode* parent,
252 const std::string& name,
253 const std::string& group,
254 group_idx_type group_idx,
255 const std::string& desc,
256 TreeNode* context,
257 ExpressionArg expression,
258 ValueSemanticDetailed semantic,
259 visibility_t visibility) :
261 group,
262 group_idx,
263 desc,
265 visibility),
266 prebuilt_expr_(std::move(expression.expr_obj)),
267 expr_str_(std::move(expression.expr_str)),
268 context_(context),
269 semantic_(semantic)
270 {
271 setExpectedParent_(parent);
272
273 sparta_assert(semantic_ != VS_INVALID,
274 "Cannot construct a StatisticDef with VS_INVALID value semantic");
275
276 if(prebuilt_expr_ != nullptr){
277 expr_str_ = prebuilt_expr_->stringize();
278 }else{
279 if(context_ == nullptr){
280 throw SpartaException("When constructing StatisticDef")
281 << getLocation() << " context must not be nullptr. It must be a TreeNode which "
282 "will be used to look up any Node names found in the expression";
283 }
284
285 if(expr_str_ == ""){
286 throw SpartaException("When constructing StatisticDef ")
287 << getLocation() << " without a prebuilt expression, the expression string "
288 "must not be \"\". It must be a non-empty string containing an arithmetic "
289 "expression referring to nodes relative to the context \""
290 << context_->getLocation() << "\"";
291 }
292 }
293
294 ensureParentIsStatisticSet_(parent);
295
296 // Other initialization here
297 // ...
298
299 if(parent){
300 parent->addChild(this);
301 }
302
303 // Copy over the meta data to the InstrumentationNode.
304 meta_data_.insert(meta_data_.begin(),
305 semantic.getMetadata().begin(),
306 semantic.getMetadata().end());
307 }
308
312 StatisticDef(const std::string& name,
313 const std::string& group,
314 group_idx_type group_idx,
315 const std::string& desc,
316 TreeNode* context,
317 ExpressionArg expression,
318 ValueSemanticDetailed semantic,
319 visibility_t visibility) :
320 StatisticDef(nullptr,
321 name,
322 group,
323 group_idx,
324 desc,
325 context,
326 expression,
327 semantic,
328 visibility)
329 {
330 // Delegated constructor
331 }
332
340 StatisticDef(TreeNode* parent,
341 const std::string& name,
342 const std::string& desc,
343 TreeNode* context,
344 ExpressionArg expression,
345 ValueSemanticDetailed semantic,
346 visibility_t visibility) :
347 StatisticDef(parent,
348 name,
351 desc,
352 context,
353 expression,
354 semantic,
355 visibility)
356 {
357 // Delegated constructor
358 }
359
367 StatisticDef(const std::string& name,
368 const std::string& desc,
369 TreeNode* context,
370 ExpressionArg expression,
371 ValueSemanticDetailed semantic,
372 visibility_t visibility) :
373 StatisticDef(nullptr,
374 name,
377 desc,
378 context,
379 expression,
380 semantic,
381 visibility)
382 {
383 // Delegated Constructor
384 }
385
392 StatisticDef(TreeNode* parent,
393 const std::string& name,
394 const std::string& group,
395 group_idx_type group_idx,
396 const std::string& desc,
397 TreeNode* context,
398 ExpressionArg expression) :
399 StatisticDef(parent,
400 name,
401 group,
402 group_idx,
403 desc,
404 context,
405 expression,
408 {
409 // Delegated constructor
410 }
411
415 StatisticDef(const std::string& name,
416 const std::string& group,
417 group_idx_type group_idx,
418 const std::string& desc,
419 TreeNode* context,
420 ExpressionArg expression) :
421 StatisticDef(nullptr,
422 name,
423 group,
424 group_idx,
425 desc,
426 context,
427 expression)
428 {
429 // Delegated constructor
430 }
431
439 StatisticDef(TreeNode* parent,
440 const std::string& name,
441 const std::string& desc,
442 TreeNode* context,
443 ExpressionArg expression) :
444 StatisticDef(parent,
445 name,
448 desc,
449 context,
450 expression)
451 {
452 // Delegated constructor
453 }
454
462 StatisticDef(const std::string& name,
463 const std::string& desc,
464 TreeNode* context,
465 ExpressionArg expression) :
466 StatisticDef(nullptr,
467 name,
470 desc,
471 context,
472 expression)
473 {
474 // Delegated Constructor
475 }
476
480 StatisticDef(TreeNode* parent,
481 const std::string& name,
482 const std::string& group,
483 group_idx_type group_idx,
484 const std::string& desc,
485 TreeNode* context,
486 ExpressionArg expression,
487 ValueSemanticDetailed semantic) :
488 StatisticDef(parent,
489 name,
490 group,
491 group_idx,
492 desc,
493 context,
494 expression,
495 semantic,
497 {
498 // Delegated constructor
499 }
500
504 StatisticDef(const std::string& name,
505 const std::string& group,
506 group_idx_type group_idx,
507 const std::string& desc,
508 TreeNode* context,
509 ExpressionArg expression,
510 ValueSemanticDetailed semantic) :
511 StatisticDef(nullptr,
512 name,
513 group,
514 group_idx,
515 desc,
516 context,
517 expression,
518 semantic)
519 {
520 // Delegated constructor
521 }
522
530 StatisticDef(TreeNode* parent,
531 const std::string& name,
532 const std::string& desc,
533 TreeNode* context,
534 ExpressionArg expression,
535 ValueSemanticDetailed semantic) :
536 StatisticDef(parent,
537 name,
540 desc,
541 context,
542 expression,
543 semantic)
544 {
545 // Delegated constructor
546 }
547
555 StatisticDef(const std::string& name,
556 const std::string& desc,
557 TreeNode* context,
558 ExpressionArg expression,
559 ValueSemanticDetailed semantic) :
560 StatisticDef(nullptr,
561 name,
564 desc,
565 context,
566 expression,
567 semantic)
568 {
569 // Delegated Constructor
570 }
571
575 virtual ~StatisticDef()
576 { }
577
580
584
589 class PendingSubStatCreationInfo
590 {
591 public:
592 PendingSubStatCreationInfo(const TreeNode * stat_node,
593 const std::string & stat_name) :
594 stat_node_(stat_node),
595 stat_name_(stat_name)
596 {}
597 const TreeNode * getNode() const {
598 return stat_node_;
599 }
600 const std::string & getName() const {
601 return stat_name_;
602 }
603 private:
604 const TreeNode * stat_node_ = nullptr;
605 std::string stat_name_;
606 };
607
608 inline const std::vector<PendingSubStatCreationInfo> & getSubStatistics() const {
609 return sub_statistics_;
610 }
611
618 std::string getContextLocation() const {
619 return context_ ? context_->getLocation() : "";
620 }
621
622 private:
623
629 void deregisterAggregationFcnUponDestruction_() const {
630 //It is possible that our auto-deregister object has been set already, which
631 //happens when one ContextCounter has more than one registered aggregation
632 //callback. So we need to check for null or we will inadvertently deregister
633 //our aggregation callback too soon (calling .reset on a non-null unique_ptr
634 //will trigger the AutoContextCounterDeregistration object's destructor of
635 //course).
636 if (auto_cc_deregister_ == nullptr) {
637 auto_cc_deregister_.reset(new AutoContextCounterDeregistration(this));
638 }
639 }
640
644
645 protected:
646
653 inline void addSubStatistic_(const TreeNode * stat_node,
654 const std::string & stat_name) const {
655 sub_statistics_.emplace_back(stat_node, stat_name);
656 }
657
658 public:
659
666 std::string getExpression() const {
667 return expr_str_;
668 }
669
679 realizeExpression(std::vector<const TreeNode*>& used) const {
680 if(prebuilt_expr_){
681 return *prebuilt_expr_; // Returns a copy
682 }
683 // This is deferred until this point because the expression can
684 // contain variables populated using the 'used' vector
685 return sparta::statistics::expression::Expression(expr_str_, context_, used);
686 }
687
693 return semantic_;
694 }
695
698
703 // Override from TreeNode
704 virtual std::string stringize(bool pretty=false) const override {
705 (void) pretty;
706 std::stringstream ss;
707 ss << '<' << getLocation() << " expr:" << getExpression();
708 ss << " vis:" << getVisibility();
709 stringizeTags(ss);
710 ss << '>';
711 return ss.str();
712 }
713
716
717 private:
718
724 void ensureParentIsStatisticSet_(TreeNode*);
725
730 virtual void validateNode_() const override {
731 if(prebuilt_expr_){
732 // Guaranteed OK because expression was built before this node was constructed
733 }else{
734 try{
736 }catch(SpartaException &ex){
737 throw SpartaException("Failed to validate StatisticDef: \"") << getLocation()
738 << "\": " << ex.what();
739 }
740 }
741 }
742
748 std::unique_ptr<sparta::statistics::expression::Expression> prebuilt_expr_;
749
757 std::string expr_str_;
758
762 TreeNode* const context_;
763
767 const ValueSemanticDetailed semantic_;
768
773 mutable std::vector<PendingSubStatCreationInfo> sub_statistics_;
774
780 class AutoContextCounterDeregistration {
781 public:
782 explicit AutoContextCounterDeregistration(const StatisticDef * sd);
783 ~AutoContextCounterDeregistration();
784 private:
785 const StatisticDef *const sd_;
786 };
787
788 mutable std::unique_ptr<AutoContextCounterDeregistration> auto_cc_deregister_;
789 };
790
791} // namespace sparta
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.
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.
uint32_t visibility_t
Continuous visibility level. Several key points along continum are indicated within Visibility.
MetadataPairs meta_data_
Add any arbitrary metadata as strings to this object. Used to add extra information to statistics rep...
Used to construct and throw a standard C++ exception. Inherits from std::exception.
Wrapper class to add meta information to a statistic definition.
const InstrumentationNode::MetadataPairs & getMetadata() const
Get the meta data provided by the user.
ValueSemanticDetailed(ValueSemantic semantic)
Construct a ValueSemanticDetailed without meta data.
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
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 StatisticDef &)=delete
Not copy-constructable.
void addSubStatistic_(const TreeNode *stat_node, const std::string &stat_name) const
Allow subclasses to forward along substatistic information to this stat definition....
ValueSemantic getValueSemantic() const
Retuns the value-semantic associated with this node at construction.
StatisticDef & operator=(const StatisticDef &)=delete
Not assign-constructable.
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)
std::string getContextLocation() const
Get the TreeNode location where this StatisticDef lives. Returns an empty string if the TreeNode* giv...
virtual ~StatisticDef()
Virtual destructor.
Node in a composite tree representing a sparta Tree item.
Definition TreeNode.hpp:204
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:302
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:313
TreeNode()=delete
Not default-constructable.
void addChild(TreeNode *child, bool inherit_phase=true)
Adds a TreeNode to this node as a child.
uint32_t group_idx_type
Index within a group.
Definition TreeNode.hpp:260
virtual TreeNode * getParent()
Gets immediate parent of this node if one exists.
Definition TreeNode.hpp:964
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:742
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...