17#ifndef AGX_STATISTICS_H
18#define AGX_STATISTICS_H
20#include <agx/config/AGX_USE_STATISTICS.h>
32#if AGX_USE_STATISTICS()
34#define AGX_STATISTICS_REGISTER_DATA_PROVIDER(INSTANCE_ADDRESS, MODULE_NAME, CONTEXT_ADDRESS) \
35 agx::Statistics::instance()->registerDataProvider( INSTANCE_ADDRESS, MODULE_NAME, CONTEXT_ADDRESS )
37#define AGX_STATISTICS_REGISTER_ROOT_DATA_PROVIDER(INSTANCE_ADDRESS, MODULE_NAME) \
38 agx::Statistics::instance()->registerDataProvider( INSTANCE_ADDRESS, MODULE_NAME )
40#define AGX_STATISTICS_UNREGISTER_DATA_PROVIDER(INSTANCE_ADDRESS) \
41 agx::Statistics::instance()->unregisterDataProvider( INSTANCE_ADDRESS )
44#define AGX_STATISTICS_REPORT_DATA(INSTANCE_ADDRESS, DATA_NAME, VALUE) \
45 agx::Statistics::instance()->report( INSTANCE_ADDRESS, DATA_NAME, VALUE )
47#define AGX_STATISTICS_REPORT_TIMING(INSTANCE_ADDRESS, DATA_NAME, VALUE) \
48 agx::Statistics::instance()->reportTiming( INSTANCE_ADDRESS, DATA_NAME, VALUE )
50#define AGX_STATISTICS_REPORT_ADDITIONAL_TIMING(INSTANCE_ADDRESS, DATA_NAME, VALUE) \
51 agx::Statistics::instance()->reportAdditionalTiming( INSTANCE_ADDRESS, DATA_NAME, VALUE )
57#define AGX_STATISTICS_REGISTER_DATA_PROVIDER(INSTANCE_ADDRESS, MODULE_NAME, CONTEXT_ADDRESS)
59#define AGX_STATISTICS_REGISTER_ROOT_DATA_PROVIDER(INSTANCE_ADDRESS, MODULE_NAME)
61#define AGX_STATISTICS_UNREGISTER_DATA_PROVIDER(INSTANCE_ADDRESS)
64#define AGX_STATISTICS_REPORT_DATA(INSTANCE_ADDRESS, DATA_NAME, VALUE)
66#define AGX_STATISTICS_REPORT_TIMING(INSTANCE_ADDRESS, DATA_NAME, VALUE)
68#define AGX_STATISTICS_REPORT_ADDITIONAL_TIMING(INSTANCE_ADDRESS, DATA_NAME, VALUE)
105 template <
typename T>
class Data;
171 template <
typename T>
172 AbstractData*
report(
void* instance,
const char* name,
const T& value );
183 template <
typename T>
184 AbstractData*
reportTiming(
void* instance,
const char* name,
const T& timing );
195 template <
typename T>
215 template <
typename T>
216 AbstractData* report(
void* instance,
void* context,
const char* name,
const T& value);
224 AbstractData*
getData(
void* instance,
const char* name );
235 AbstractData*
getData(
const char* providerName,
const char* dataName,
size_t providerIndex = 0 );
273 template <
typename T>
310 void print( std::ostream& stream,
bool includeTaskTimers =
false );
327 SINGLETON_CLASSNAME_METHOD();
346 ProviderHandle* getOrCreateParentNode(
void* context );
348 friend class ProviderHandle;
355 bool createNameBinding(
const char* name, ProviderHandle* provider );
362 bool removeNameBinding( ProviderHandle* provider );
418 template <
typename T>
421 template <
typename T>
428 AbstractData*
getData(
const char* name )
const;
488 void clear(
bool recursive =
false,
bool clearAccumulated =
true );
494 void print( std::ostream& stream,
bool includeTaskTimers,
unsigned depth = 0 );
509 template<
typename T >
552 SUBTREE_IS_EMPTY = 0x2
603 template<
typename T >
610 virtual void clear(
bool clearAccumulated =
true ) = 0;
637 template <
typename T>
671 virtual void clear(
bool clearAccumulated =
true )
override;
681 T m_accumulatedValue;
697 TaskTimingReportHandle(
void* owner,
Statistics* statistics );
699 void report(
Real wallTime,
Real computeCost,
Real overhead );
700 void reportWallAndOverhead(
Real wallTime,
Real overhead,
void* context );
701 void reportWallAndOverhead(
Real wallTime,
Real overhead );
702 void reportCostAndAdditionalOverhead(
Real computeCost,
Real additionalOverhead );
716 class StatisticsScopeTimer {
718 StatisticsScopeTimer(
void *instance,
const char *name) : m_instance(instance), m_name(name)
723 ~StatisticsScopeTimer()
738 template <
typename T>
749 ScopeLock<ReentrantMutex> typeSystemLock( getTypeSystemMutex() );
752 ScopeLock<ReentrantMutex> lock( m_mutex );
755 DataProviderTable::iterator providerIt = m_providers.
find(
instance );
756 if ( providerIt == m_providers.
end() )
758 LOGGER_WARNING() <<
"Trying to report statistics from an instance that hasn't been registered as a data provider yet." <<
LOGGER_ENDL();
764 Data<T>* data = provider->getOrCreateData<T>( name );
765 if ( data ==
nullptr )
767 const agxData::Format* actualFormat = provider->getData( name )->getFormat();
770 " to data point \'" << name <<
", which has already received data of type \'" <<
772 "Cannot mix data types, so newest data point is ignored." <<
LOGGER_ENDL();
777 data->value() = value;
778 data->accumulatedValue() += value;
782 template<
typename T >
787 timingData->setIsTiming(
true );
795 timingData->setIsTiming(
true );
799 template <
typename T>
805 ScopeLock<ReentrantMutex> lock( m_mutex );
808 Data<T>* data = this->getData<T>(
instance, name );
809 if ( data ==
nullptr )
815 data->value() += timing;
816 data->accumulatedValue() += timing;
829 template <
typename T>
834 agxAbort1(
"Context altering reporting not yet implemented.");
841 DataProviderTable::iterator it = m_providers.
find(
instance );
842 return it != m_providers.
end() ? it->second->getData( name ) :
nullptr;
846 template <
typename T>
850 if ( data ==
nullptr ||
dynamic_cast<Data<T> *
>(data) ==
nullptr )
852 return static_cast<Data<T> *
>( data );
858 DataProviderTable::iterator it = m_providers.
find(
instance );
859 return it != m_providers.
end() ? it->second :
nullptr;
864 return m_rootProvider;
869 DataProviderNameTable::const_iterator it = m_providerNames.
find( name );
870 if ( it == m_providerNames.
end() )
871 LOGGER_ERROR() <<
"Statistics: Was asked to return a reference to the list of providers with the name \'" << name <<
"\'" <<
872 ", but there are no such providers and thus no vector to return." <<
LOGGER_ENDL();
881 AGX_FORCE_INLINE Statistics::ProviderHandle::ProviderHandle(
void* owner, Statistics* statistics )
882 : m_owner(owner), m_name(nullptr), m_parent(nullptr), m_statistics(statistics), m_flags(0)
913 template<
typename T >
916 return dynamic_cast<T*
>( this->
getOwner() );
919 template<
typename T >
922 return dynamic_cast<const T*
>( this->
getOwner() );
946 LOGGER_ERROR() <<
"Internal Error: Found a root that isn't the same is the Statistics root node. This should not happen?" <<
LOGGER_ENDL();
953 template<
typename T >
954 Statistics::Data<T>* Statistics::ProviderHandle::getOrCreateData(
const char* name )
960 return dynamic_cast< Data<T>*
>( data );
965 Data<T>* newData =
new Data<T>( internalName );
972 AGX_FORCE_INLINE bool Statistics::ProviderHandle::isTaskTimer( AbstractData* )
992 m_computeCost = this->getOrCreateData<Real>(
"ComputeCost" );
993 m_wallTime = this->getOrCreateData<Real>(
"WallTime" );
994 m_overhead = this->getOrCreateData<Real>(
"Overhead" );
1003 if ( context !=
m_parent->getOwner() )
1006 this->
report( wallTime, computeCost, overhead );
1015 m_wallTime->report( wallTime );
1016 m_computeCost->report( computeCost );
1017 m_overhead->report( overhead );
1025 if ( context !=
m_parent->getOwner() )
1028 this->reportWallAndOverhead( wallTime, overhead );
1036 m_wallTime->report( wallTime );
1037 m_overhead->report( overhead );
1040 AGX_FORCE_INLINE void TaskTimingReportHandle::reportCostAndAdditionalOverhead(
Real computeCost,
Real additionalOverhead )
1045 m_computeCost->report( computeCost );
1046 m_overhead->report( additionalOverhead );
1052 return data == m_computeCost || data == m_wallTime || data == m_overhead;
1058 :
m_name(name), m_format(format), m_isTiming( false )
1126 template<
typename T >
1127 AGX_FORCE_INLINE Statistics::Data<T>* Statistics::AbstractData::asTypedData()
1129 return dynamic_cast< Data<T>*
>( this );
1139 template <
typename T>
1145 template<
typename T >
1150 template<
typename T >
1154 value->
set<T>(m_value);
1159 template <
typename T>
1162 template <
typename T>
1165 template <
typename T>
1168 template <
typename T>
1171 template <
typename T>
1174 template <
typename T>
1178 m_accumulatedValue += value;
1182 template <
typename T>
1186 if ( clearAccumulated ) {
1187 m_accumulatedValue = T();
1188 m_numAccumulated = 0;
1192 template <
typename T>
1195 return static_cast< const void*
> ( &m_value );
1198 template <
typename T>
1201 return static_cast< const void*
> ( &m_accumulatedValue );
#define AGX_DECLARE_POINTER_TYPES(type)
#define AGX_STATISTICS_REPORT_ADDITIONAL_TIMING(INSTANCE_ADDRESS, DATA_NAME, VALUE)
#define AGX_DECLARE_VECTOR_TYPES(type)
Format * getFormat(const agx::Name &name)
Only use as member allocated variable.
Abstract representation of a value.
void set(const Value *other)
Explicitly set the value, removes active binding and disables autobinding.
iterator insert(const KeyT &key, const ValueT value)
Insert a key/value pair into the hash table.
iterator end()
Iterator marking end of hash table.
iterator find(const KeyT &key)
Find a key/value pair in the hash table given a key.
Inheritance with partial specialization due to bug with ref_ptr containers.
const char * c_str() const
const agx::Name & getName() const
Representation of a path, a list of name components.
Base class providing referencing counted objects.
Base class for Singletons that should have its shutdown called explicitly before exit of the applicat...
Statistics data storage class.
virtual const void * accumulatedPtr() const override
T & value()
Returns the current value of this statistics data handle.
virtual const void * valuePtr() const override
agx::UInt getNumAccumulated() const
virtual agxData::Value * toValue() const override
T & accumulatedValue()
Returns the accumulated value of this statistics data handle.
const T & accumulatedValue() const
virtual void clear(bool clearAccumulated=true) override
Sets the current and accumulated values to zero, or the equivalent for complex types.
void report(const T &value)
Base statistics gathering class.
String printed(bool includeTaskTimers=false)
Print print statistics in a human-readable form to the given stream.
bool unregisterDataProvider(void *provider)
Removes the provider handle for the given provider from the statistics tree.
bool getEnable() const
Returns true if this Statistics object is currently accepting new data.
String toString() const
Reference-less method to be used with Lua scripts.
bool isTaskNode() const
Returns true if this ProviderHandle was created as a handle for a Task.
void setIsTiming(bool isTiming=true)
void setContext(void *instance, void *context)
Move a sub-tree to a new location.
bool isImplicit() const
Returns true if this ProviderHandle was implicitly created.
friend class ProviderHandle
ProviderHandleRefVector m_children
AbstractData * getData(const char *name) const
Statistics * m_statistics
HashTable< const char *, AbstractDataRef > DataTable
static bool hasInstance()
void setEnable(bool)
Enables or disables gathering of statistics data.
AbstractData * getData(void *instance, const Path &path)
Get a data handle from the statistics tree.
String accumulatedToString() const
Reference-less.
AbstractData * getData(const String &providerName, const String &dataName, size_t providerIndex=0)
Get a data handle from the statistics tree.
bool isEnabled() const
Returns true if this Statistics object is currently accepting new data.
String getTimingString(const String &providerName, const String &dataName, size_t providerIndex=0)
Helper for C# swig binding.
ProviderHandle(void *owner, Statistics *statistics)
void clear(bool recursive=false, bool clearAccumulated=true)
Clears the data help by this ProviderHandle, using the type's default constructor.
virtual const void * valuePtr() const =0
Returns a raw pointer to the memory location where the current value is stored.
ProviderHandle * getParent()
virtual void clear(bool clearAccumulated=true)=0
Sets the current and accumulated values to zero, or the equivalent for complex types.
size_t getNumChildren() const
static const char * copyString(const char *)
All strings held inside the statistics tree is owned by the statistics tree.
void takeChildren(ProviderHandle *currentParent)
Move all children from the given ProviderHandle into 'this'.
void decayBranch(ProviderHandle *leaf)
Remove implicit childless nodes up a branch.
bool registerDataProvider(void *provider, const char *name, void *context=(void *) nullptr)
Register a new data provider.
const char * getName() const
void toString(String &result) const
Converts the current value to a human readable string using the "ascii" format for the type of the va...
bool isRoot() const
Returns true if this node has a nullptr parent.
ProviderHandle * m_parent
AbstractData * reportAdditionalTiming(void *instance, const char *name, const T &timing)
Increments the chosen timing value instead of setting it.
AbstractData * getData(void *instance, const char *name)
Get a data handle from a statistics handle.
void print(std::ostream &stream, bool includeTaskTimers, unsigned depth=0)
Recursively print the data in this ProviderHandle and all its children.
virtual ~ProviderHandle()
ProviderHandle * getProviderHandle(void *instance)
Returns the provider handle for the given statistics provider.
TaskTimingReportHandle * registerTaskTimerProvider(void *provider, const char *name, void *context)
Used by the tasks to create a provider handle designed for task timer reporting.
bool registerDataProvider(void *provider, const String &name, void *context=(void *) nullptr)
Convenience method, calls the const char* version of registerDataProvider(*).
virtual bool isTaskTimer(AbstractData *data)
Tests if the given data handle corresponds to one of the special task timing values.
const ProviderHandleRefVector & getChildren() const
static Statistics * instance()
const DataTable & getDataTable() const
void adoptChild(ProviderHandle *child)
Move a ProviderHandle from its current parent into 'this'.
const ProviderHandleRefVector & getProviderHandles(const char *name)
Returns handles to all providers that have been registered with the given name.
AbstractData * getData(const char *providerName, const char *dataName, size_t providerIndex=0)
Get a data handle from the statistics tree.
void accumulatedToString(String &result) const
Converts the accumulated value to a human readable string using the "ascii" format for the type of th...
AbstractData * reportTiming(void *instance, const char *name, const T &timing)
Report a timing statistics.
T * getOwner()
Convenience method that test if the owner was of a specific type and if so returns a pointer to the o...
void setName(const char *name)
Set the name of the provider.
const agxData::Format * getFormat() const
void setIsTaskNode(bool isTaskNode)
Returns true if the ProviderHandle contains task timing values.
const void * getOwner() const
agx::Vector< ProviderHandleRef > ProviderHandleRefVector
const T * getOwner() const
AbstractData * reportAdditionalTiming(void *instance, const char *name, const Timer &timer)
Convenience method that reads the current value from a timer.
Data< T > * asTypedData()
Converts this abstract representation to a type specified data handle.
ProviderHandle * getRootProviderHandle()
Returns a handle to the root of the statistics tree.
Data< T > * getOrCreateData(const char *name)
Makes sure that a data handle with the given names exists.
ProviderHandle * getChild(const char *name)
virtual agxData::Value * toValue() const =0
ProviderHandle * getChild(size_t index)
void addChild(ProviderHandle *child)
Add the given ProviderHandle to the list of children, and set the parent pointer of the child to the ...
AbstractData * reportTiming(void *instance, const char *name, const Timer &timer)
Convenience report method that reads the current time from a timer.
virtual const void * accumulatedPtr() const =0
Returns a raw pointer to the memory location where the current value is stored.
TimingInfo getTimingInfo(const String &providerName, const String &dataName, size_t providerIndex=0)
Will find a provider by name, and data by name, and return the stored statistics data.
AbstractData * report(void *instance, const char *name, const T &value)
Report statistics.
void clear()
Reset all statistics values, setting both the current and accumulated value using the type's default ...
void print(std::ostream &stream, bool includeTaskTimers=false)
Print print statistics in a human-readable form to the given stream.
AbstractData(const char *name, const agxData::Format *format)
bool updateEmptyFlags()
Recursively updates the SUBTREE_IS_EMPTY flags.
void clearCurrent()
Reset all statistics values, setting the current value using the type's default constructor.
Data< T > * getData(void *instance, const char *name)
Templated convenience method that returns the result from the non-templated 'getData(*)' with the sam...
The Timer class permits timing execution speed with the same refinement as the built in hardware cloc...
#define agxAssertN(expr, format,...)
#define DOXYGEN_END_INTERNAL_BLOCK()
#define DOXYGEN_START_INTERNAL_BLOCK()
Contains classes for low level data storage for AGX.
void AGXCORE_EXPORT transform(void *target, const Format *targetFormat, const void *source, const Format *sourceFormat, size_t numElements)
Transform data between two buffers.
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
AGXCORE_EXPORT agx::ReentrantMutex & getTypeSystemMutex()
AgXString< std::string > String
std::recursive_mutex ReentrantMutex