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