SST  8.0.0
StructuralSimulationToolkit
statbase.h
1 // Copyright 2009-2018 NTESS. Under the terms
2 // of Contract DE-NA0003525 with NTESS, the U.S.
3 // Government retains certain rights in this software.
4 //
5 // Copyright (c) 2009-2018, NTESS
6 // All rights reserved.
7 //
8 // This file is part of the SST software package. For license
9 // information, see the LICENSE file in the top level directory of the
10 // distribution.
11 
12 #ifndef _H_SST_CORE_STATISTICS_BASE
13 #define _H_SST_CORE_STATISTICS_BASE
14 
15 #include <string>
16 
17 #include <sst/core/sst_types.h>
18 #include <sst/core/warnmacros.h>
19 #include <sst/core/params.h>
20 #include <sst/core/oneshot.h>
21 #include <sst/core/statapi/statfieldinfo.h>
22 #include <sst/core/serialization/serializable.h>
23 
24 namespace SST {
25 class BaseComponent;
26 class Factory;
27 namespace Statistics {
28 class StatisticOutput;
29 class StatisticProcessingEngine;
30 class StatisticGroup;
31 
32 
34 public:
35  std::string name;
36  Params params;
37 
38  StatisticInfo(const std::string &name) : name(name) { }
39  StatisticInfo(const std::string &name, const Params &params) : name(name), params(params) { }
40  StatisticInfo() { } /* DO NOT USE: For serialization */
41 
42  void serialize_order(SST::Core::Serialization::serializer &ser) override {
43  ser & name;
44  ser & params;
45  }
46 
47  ImplementSerializable(SST::Statistics::StatisticInfo)
48 };
49 
50 
51 
52 /**
53  \class StatisticBase
54 
55  Forms the base class for statistics gathering within SST. Statistics are
56  gathered and processed into various (extensible) output forms. Statistics
57  are expected to be named so that they can be located in the simulation
58  output files.
59 */
60 
62 {
63 public:
64  /** Statistic collection mode */
65  typedef enum {STAT_MODE_UNDEFINED, STAT_MODE_COUNT, STAT_MODE_PERIODIC} StatMode_t;
66 
67  // Enable/Disable of Statistic
68  /** Enable Statistic for collections */
69  void enable() {m_statEnabled = true;}
70 
71  /** Disable Statistic for collections */
72  void disable() {m_statEnabled = false;}
73 
74  // Handling of Collection Counts and Data
75  /** Inform the Statistic to clear its data */
76  virtual void clearStatisticData() {}
77 
78  /** Set the current collection count to 0 */
79  virtual void resetCollectionCount();
80 
81  /** Increment current collection count */
82  virtual void incrementCollectionCount();
83 
84  /** Set the current collection count to a defined value */
85  virtual void setCollectionCount(uint64_t newCount);
86 
87  /** Set the collection count limit to a defined value */
88  virtual void setCollectionCountLimit(uint64_t newLimit);
89 
90  // Control Statistic Operation Flags
91  /** Set the Reset Count On Output flag.
92  * If Set, the collection count will be reset when statistic is output.
93  */
94  void setFlagResetCountOnOutput(bool flag) {m_resetCountOnOutput = flag;}
95 
96  /** Set the Clear Data On Output flag.
97  * If Set, the data in the statistic will be cleared by calling
98  * clearStatisticData().
99  */
100  void setFlagClearDataOnOutput(bool flag) {m_clearDataOnOutput = flag;}
101 
102  /** Set the Output At End Of Sim flag.
103  * If Set, the statistic will perform an output at the end of simulation.
104  */
105  void setFlagOutputAtEndOfSim(bool flag) {m_outputAtEndOfSim = flag;}
106 
107  // Get Data & Information on Statistic
108  /** Return the Component Name */
109  const std::string& getCompName() const;
110 
111  /** Return the Statistic Name */
112  inline const std::string& getStatName() const {return m_statName;}
113 
114  /** Return the Statistic SubId */
115  inline const std::string& getStatSubId() const {return m_statSubId;}
116 
117  /** Return the full Statistic name of Component.StatName.SubId */
118  inline const std::string& getFullStatName() const {return m_statFullName;} // Return compName.statName.subId
119 
120  /** Return the Statistic type name */
121  inline const std::string& getStatTypeName() const {return m_statTypeName;}
122 
123  /** Return the Statistic data type */
124  inline const StatisticFieldInfo::fieldType_t& getStatDataType() const {return m_statDataType;}
125 
126  /** Return the Statistic data type */
127  inline const char* getStatDataTypeShortName() const {return StatisticFieldInfo::getFieldTypeShortName(m_statDataType);}
128 
129  /** Return the Statistic data type */
130  inline const char* getStatDataTypeFullName() const {return StatisticFieldInfo::getFieldTypeFullName(m_statDataType);}
131 
132  /** Return a pointer to the parent Component */
133  BaseComponent* getComponent() const {return m_component;}
134 
135  /** Return the enable status of the Statistic */
136  bool isEnabled() const {return m_statEnabled;}
137 
138  /** Return the enable status of the Statistic's ability to output data */
139  bool isOutputEnabled() const {return m_outputEnabled;}
140 
141  /** Return the collection count limit */
142  uint64_t getCollectionCountLimit() const {return m_collectionCountLimit;}
143 
144  /** Return the current collection count */
145  uint64_t getCollectionCount() const {return m_currentCollectionCount;}
146 
147  /** Return the ResetCountOnOutput flag value */
148  bool getFlagResetCountOnOutput() const {return m_resetCountOnOutput;}
149 
150  /** Return the ClearDataOnOutput flag value */
151  bool getFlagClearDataOnOutput() const {return m_clearDataOnOutput;}
152 
153  /** Return the OutputAtEndOfSim flag value */
154  bool getFlagOutputAtEndOfSim() const {return m_outputAtEndOfSim;}
155 
156  /** Return the collection mode that is registered */
157  StatMode_t getRegisteredCollectionMode() const {return m_registeredCollectionMode;}
158 
159  // Delay Methods (Uses OneShot to disable Statistic or Collection)
160  /** Delay the statistic from outputting data for a specified delay time
161  * @param delayTime - Value in UnitAlgebra format for delay (i.e. 10ns).
162  */
163  void delayOutput(const char* delayTime);
164 
165  /** Delay the statistic from collecting data for a specified delay time.
166  * @param delayTime - Value in UnitAlgebra format for delay (i.e. 10ns).
167  */
168  void delayCollection(const char* delayTime);
169 
170  // Status of Statistic
171  /** Indicate that the Statistic is Ready to be used */
172  virtual bool isReady() const {return true;}
173 
174  /** Indicate if the Statistic is a NullStatistic */
175  virtual bool isNullStatistic() const {return false;}
176 
177 protected:
180  friend class SST::Statistics::StatisticGroup;
181 
182  /** Construct a StatisticBase
183  * @param comp - Pointer to the parent constructor.
184  * @param statName - Name of the statistic to be registered. This name must
185  * match the name in the ElementInfoStatistic.
186  * @param statSubId - Additional name of the statistic
187  * @param statParams - The parameters for this statistic
188  */
189  // Constructors:
190  StatisticBase(BaseComponent* comp, const std::string& statName, const std::string& statSubId, Params& statParams);
191 
192  // Destructor
193  virtual ~StatisticBase() {}
194 
195  /** Return the Statistic Parameters */
196  Params& getParams() {return m_statParams;}
197 
198  /** Set the Statistic Data Type */
199  void setStatisticDataType(const StatisticFieldInfo::fieldType_t dataType) {m_statDataType = dataType;}
200 
201  /** Set an optional Statistic Type Name */
202  void setStatisticTypeName(const char* typeName) {m_statTypeName = typeName;}
203 
204 private:
205  friend class SST::BaseComponent;
206 
207  /** Set the Registered Collection Mode */
208  void setRegisteredCollectionMode(StatMode_t mode) {m_registeredCollectionMode = mode;}
209 
210  /** Construct a full name of the statistic */
211  static std::string buildStatisticFullName(const char* compName, const char* statName, const char* statSubId);
212  static std::string buildStatisticFullName(const std::string& compName, const std::string& statName, const std::string& statSubId);
213 
214  // Required Virtual Methods:
215  /** Called by the system to tell the Statistic to register its output fields.
216  * by calling statOutput->registerField(...)
217  * @param statOutput - Pointer to the statistic output
218  */
219  virtual void registerOutputFields(StatisticOutput* statOutput) = 0;
220 
221  /** Called by the system to tell the Statistic to send its data to the
222  * StatisticOutput to be output.
223  * @param statOutput - Pointer to the statistic output
224  * @param EndOfSimFlag - Indicates that the output is occurring at the end of simulation.
225  */
226  virtual void outputStatisticData(StatisticOutput* statOutput, bool EndOfSimFlag) = 0;
227 
228  /** Indicate if the Statistic Mode is supported.
229  * This allows Statistics to support STAT_MODE_COUNT and STAT_MODE_PERIODIC modes.
230  * by default, both modes are supported.
231  * @param mode - Mode to test
232  */
233  virtual bool isStatModeSupported(StatMode_t UNUSED(mode)) const {return true;} // Default is to accept all modes
234 
235  /** Verify that the statistic names match */
236  bool operator==(StatisticBase& checkStat);
237 
238  // Support Routines
239  void initializeStatName(const char* compName, const char* statName, const char* statSubId);
240  void initializeStatName(const std::string& compName, const std::string& statName, const std::string& statSubId);
241 
242  void initializeProperties();
243  void checkEventForOutput();
244 
245  // OneShot Callbacks:
246  void delayOutputExpiredHandler(); // Enable Output in handler
247  void delayCollectionExpiredHandler(); // Enable Collection in Handler
248 
249  const StatisticGroup* getGroup() const { return m_group; }
250  void setGroup(const StatisticGroup *group ) { m_group = group; }
251 
252 private:
253  StatisticBase(); // For serialization only
254 
255 private:
256  BaseComponent* m_component;
257  std::string m_statName;
258  std::string m_statSubId;
259  std::string m_statFullName;
260  std::string m_statTypeName;
261  Params m_statParams;
262  StatMode_t m_registeredCollectionMode;
263  uint64_t m_currentCollectionCount;
264  uint64_t m_collectionCountLimit;
265  StatisticFieldInfo::fieldType_t m_statDataType;
266 
267  bool m_statEnabled;
268  bool m_outputEnabled;
269  bool m_resetCountOnOutput;
270  bool m_clearDataOnOutput;
271  bool m_outputAtEndOfSim;
272 
273  bool m_outputDelayed;
274  bool m_collectionDelayed;
275  bool m_savedStatEnabled;
276  bool m_savedOutputEnabled;
277  OneShot::HandlerBase* m_outputDelayedHandler;
278  OneShot::HandlerBase* m_collectionDelayedHandler;
279  const StatisticGroup* m_group;
280 
281 };
282 
283 ////////////////////////////////////////////////////////////////////////////////
284 
285 /**
286  \class Statistic
287 
288  Forms the template defined base class for statistics gathering within SST.
289 
290  @tparam T A template for the basic numerical data stored by this Statistic
291 */
292 
293 template <typename T>
294 class Statistic : public StatisticBase
295 {
296 public:
297  // The main method to add data to the statistic
298  /** Add data to the Statistic
299  * This will call the addData_impl() routine in the derived Statistic.
300  */
301  void addData(T data)
302  {
303  // Call the Derived Statistic's implementation
304  // of addData and increment the count
305  if (true == isEnabled()) {
306  addData_impl(data);
308  }
309  }
310 
311 protected:
312  friend class SST::Factory;
313  friend class SST::BaseComponent;
314  /** Construct a Statistic
315  * @param comp - Pointer to the parent constructor.
316  * @param statName - Name of the statistic to be registered. This name must
317  * match the name in the ElementInfoStatistic.
318  * @param statSubId - Additional name of the statistic
319  * @param statParams - The parameters for this statistic
320  */
321  Statistic(BaseComponent* comp, const std::string& statName, const std::string& statSubId, Params& statParams) :
322  StatisticBase(comp, statName, statSubId, statParams)
323  {
324  setStatisticDataType(StatisticFieldInfo::getFieldTypeFromTemplate<T>());
325  }
326 
327  virtual ~Statistic(){}
328 
329 private:
330  Statistic(){}; // For serialization only
331 
332  // Required Templated Virtual Methods:
333  virtual void addData_impl(T data) = 0;
334 
335 private:
336 };
337 
338 } //namespace Statistics
339 } //namespace SST
340 
341 #endif
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition: serializer.h:35
void setFlagOutputAtEndOfSim(bool flag)
Set the Output At End Of Sim flag.
Definition: statbase.h:105
void addData(T data)
Add data to the Statistic This will call the addData_impl() routine in the derived Statistic...
Definition: statbase.h:301
virtual void setCollectionCountLimit(uint64_t newLimit)
Set the collection count limit to a defined value.
Definition: statbase.cc:56
void delayCollection(const char *delayTime)
Delay the statistic from collecting data for a specified delay time.
Definition: statbase.cc:128
void setFlagClearDataOnOutput(bool flag)
Set the Clear Data On Output flag.
Definition: statbase.h:100
Forms the base class for statistics output generation within the SST core.
Definition: statoutput.h:47
Forms the base class for statistics gathering within SST.
Definition: statbase.h:61
void setStatisticTypeName(const char *typeName)
Set an optional Statistic Type Name.
Definition: statbase.h:202
uint64_t getCollectionCount() const
Return the current collection count.
Definition: statbase.h:145
void enable()
Enable Statistic for collections.
Definition: statbase.h:69
void delayOutput(const char *delayTime)
Delay the statistic from outputting data for a specified delay time.
Definition: statbase.cc:114
StatMode_t getRegisteredCollectionMode() const
Return the collection mode that is registered.
Definition: statbase.h:157
Forms the template defined base class for statistics gathering within SST.
Definition: statbase.h:294
Definition: serializable.h:109
virtual bool isNullStatistic() const
Indicate if the Statistic is a NullStatistic.
Definition: statbase.h:175
const std::string & getCompName() const
Return the Component Name.
Definition: statbase.cc:34
virtual void incrementCollectionCount()
Increment current collection count.
Definition: statbase.cc:39
virtual void resetCollectionCount()
Set the current collection count to 0.
Definition: statbase.cc:51
const StatisticFieldInfo::fieldType_t & getStatDataType() const
Return the Statistic data type.
Definition: statbase.h:124
Statistic(BaseComponent *comp, const std::string &statName, const std::string &statSubId, Params &statParams)
Construct a Statistic.
Definition: statbase.h:321
bool getFlagOutputAtEndOfSim() const
Return the OutputAtEndOfSim flag value.
Definition: statbase.h:154
bool getFlagClearDataOnOutput() const
Return the ClearDataOnOutput flag value.
Definition: statbase.h:151
bool isEnabled() const
Return the enable status of the Statistic.
Definition: statbase.h:136
virtual bool isReady() const
Indicate that the Statistic is Ready to be used.
Definition: statbase.h:172
Main component object for the simulation.
Definition: baseComponent.h:104
void disable()
Disable Statistic for collections.
Definition: statbase.h:72
bool isOutputEnabled() const
Return the enable status of the Statistic&#39;s ability to output data.
Definition: statbase.h:139
StatMode_t
Statistic collection mode.
Definition: statbase.h:65
virtual void clearStatisticData()
Inform the Statistic to clear its data.
Definition: statbase.h:76
void setFlagResetCountOnOutput(bool flag)
Set the Reset Count On Output flag.
Definition: statbase.h:94
Parameter store.
Definition: params.h:45
bool getFlagResetCountOnOutput() const
Return the ResetCountOnOutput flag value.
Definition: statbase.h:148
const std::string & getStatSubId() const
Return the Statistic SubId.
Definition: statbase.h:115
Definition: statbase.h:33
const char * getStatDataTypeFullName() const
Return the Statistic data type.
Definition: statbase.h:130
Class for instantiating Components, Links and the like out of element libraries.
Definition: factory.h:45
const std::string & getFullStatName() const
Return the full Statistic name of Component.StatName.SubId.
Definition: statbase.h:118
virtual void setCollectionCount(uint64_t newCount)
Set the current collection count to a defined value.
Definition: statbase.cc:45
const char * getStatDataTypeShortName() const
Return the Statistic data type.
Definition: statbase.h:127
void setStatisticDataType(const StatisticFieldInfo::fieldType_t dataType)
Set the Statistic Data Type.
Definition: statbase.h:199
An SST core component that handles timing and event processing informing all registered Statistics to...
Definition: statengine.h:49
Definition: statgroup.h:29
Params & getParams()
Return the Statistic Parameters.
Definition: statbase.h:196
uint64_t getCollectionCountLimit() const
Return the collection count limit.
Definition: statbase.h:142
BaseComponent * getComponent() const
Return a pointer to the parent Component.
Definition: statbase.h:133
const std::string & getStatTypeName() const
Return the Statistic type name.
Definition: statbase.h:121
const std::string & getStatName() const
Return the Statistic Name.
Definition: statbase.h:112
StatisticBase(BaseComponent *comp, const std::string &statName, const std::string &statSubId, Params &statParams)
Construct a StatisticBase.
Definition: statbase.cc:20
fieldType_t
Supported Field Types.
Definition: statfieldinfo.h:36