AGX Dynamics  2.35.0.1
Statistics.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2022. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 
17 #ifndef AGX_STATISTICS_H
18 #define AGX_STATISTICS_H
19 
20 #include <agx/config/AGX_USE_STATISTICS.h>
21 #include <agx/Referenced.h>
22 #include <agx/Path.h>
23 #include <agx/Timer.h>
24 #include <agx/Math.h>
25 #include <agx/Vector.h>
26 #include <agx/HashSet.h>
27 #include <agxData/Format.h>
28 
29 #include <agx/Logger.h>
30 #include <agxData/Value.h>
31 
32 #if AGX_USE_STATISTICS()
33 
34 #define AGX_STATISTICS_REGISTER_DATA_PROVIDER(INSTANCE_ADDRESS, MODULE_NAME, CONTEXT_ADDRESS) \
35  agx::Statistics::instance()->registerDataProvider( INSTANCE_ADDRESS, MODULE_NAME, CONTEXT_ADDRESS )
36 
37 #define AGX_STATISTICS_REGISTER_ROOT_DATA_PROVIDER(INSTANCE_ADDRESS, MODULE_NAME) \
38  agx::Statistics::instance()->registerDataProvider( INSTANCE_ADDRESS, MODULE_NAME )
39 
40 #define AGX_STATISTICS_UNREGISTER_DATA_PROVIDER(INSTANCE_ADDRESS) \
41  agx::Statistics::instance()->unregisterDataProvider( INSTANCE_ADDRESS )
42 
43 
44 #define AGX_STATISTICS_REPORT_DATA(INSTANCE_ADDRESS, DATA_NAME, VALUE) \
45  agx::Statistics::instance()->report( INSTANCE_ADDRESS, DATA_NAME, VALUE )
46 
47 #define AGX_STATISTICS_REPORT_TIMING(INSTANCE_ADDRESS, DATA_NAME, VALUE) \
48  agx::Statistics::instance()->reportTiming( INSTANCE_ADDRESS, DATA_NAME, VALUE )
49 
50 #define AGX_STATISTICS_REPORT_ADDITIONAL_TIMING(INSTANCE_ADDRESS, DATA_NAME, VALUE) \
51  agx::Statistics::instance()->reportAdditionalTiming( INSTANCE_ADDRESS, DATA_NAME, VALUE )
52 
53 
54 #else
55 
56 
57 #define AGX_STATISTICS_REGISTER_DATA_PROVIDER(INSTANCE_ADDRESS, MODULE_NAME, CONTEXT_ADDRESS)
58 
59 #define AGX_STATISTICS_REGISTER_ROOT_DATA_PROVIDER(INSTANCE_ADDRESS, MODULE_NAME)
60 
61 #define AGX_STATISTICS_UNREGISTER_DATA_PROVIDER(INSTANCE_ADDRESS)
62 
63 
64 #define AGX_STATISTICS_REPORT_DATA(INSTANCE_ADDRESS, DATA_NAME, VALUE)
65 
66 #define AGX_STATISTICS_REPORT_TIMING(INSTANCE_ADDRESS, DATA_NAME, VALUE)
67 
68 #define AGX_STATISTICS_REPORT_ADDITIONAL_TIMING(INSTANCE_ADDRESS, DATA_NAME, VALUE)
69 
70 
71 #endif
72 
73 
74 
75 
76 namespace agx
77 {
78  class Task;
79  AGX_DECLARE_POINTER_TYPES(TaskTimingReportHandle);
80 
81  struct TimingInfo {
82  // Most people report these as milliseconds.
83  double current;
84  double accumulated;
85  };
86 
87 
99  {
100  public:
101  AGX_DECLARE_POINTER_TYPES(ProviderHandle);
102  AGX_DECLARE_VECTOR_TYPES(ProviderHandle);
104 
105  template <typename T> class Data;
106 
108 
109  public:
110 
112  void setEnable( bool );
113 
115  bool getEnable() const;
116 
118  bool isEnabled() const;
119 
134  bool registerDataProvider( void* provider, const char* name, void* context = (void*)nullptr );
135 
139  bool registerDataProvider( void* provider, const String& name, void* context = (void*)nullptr );
140 
144  TaskTimingReportHandle* registerTaskTimerProvider( void* provider, const char* name, void* context );
145 
146 
151  bool unregisterDataProvider( void* provider );
152 
158  void setContext(void* instance, void* context);
159 
171  template <typename T>
172  AbstractData* report( void* instance, const char* name, const T& value );
173 
183  template <typename T>
184  AbstractData* reportTiming( void* instance, const char* name, const T& timing );
185 
189  AbstractData* reportTiming( void* instance, const char* name, const Timer& timer );
190 
191 
195  template <typename T>
196  AbstractData* reportAdditionalTiming( void* instance, const char* name, const T& timing );
197 
201  AbstractData* reportAdditionalTiming( void* instance, const char* name, const Timer& timer );
202 
203 #if 0
204 // This method was believed to be required, but apparently not yet.
205 
215  template <typename T>
216  AbstractData* report(void* instance, void* context, const char* name, const T& value);
217 #endif
218 
224  AbstractData* getData( void* instance, const char* name );
225 
226 
235  AbstractData* getData( const char* providerName, const char* dataName, size_t providerIndex = 0 );
236 
237 
245  AbstractData* getData( const String& providerName, const String& dataName, size_t providerIndex = 0 );
246 
250  String getTimingString( const String& providerName, const String& dataName, size_t providerIndex = 0 );
251 
256  TimingInfo getTimingInfo( const String& providerName, const String& dataName, size_t providerIndex = 0 );
257 
258 
267  AbstractData* getData( void* instance, const Path& path );
268 
273  template <typename T>
274  Data<T>* getData(void* instance, const char* name);
275 
276 
280  ProviderHandle* getProviderHandle( void* instance );
281 
285  ProviderHandle* getRootProviderHandle();
286 
290  const ProviderHandleRefVector& getProviderHandles( const char* name );
291 
296  void clear();
297 
301  void clearCurrent();
302 
310  void print( std::ostream& stream, bool includeTaskTimers = false );
311 
318  String printed( bool includeTaskTimers = false );
319 
320 
321 
323 
324 
325  static Statistics *instance();
326  static bool hasInstance();
327  SINGLETON_CLASSNAME_METHOD();
328 
329 
330  protected:
331 
333 
335  void decayBranch( ProviderHandle* leaf );
336 
341  static const char* copyString( const char* );
342 
343  virtual ~Statistics();
344 
345  private:
346  ProviderHandle* getOrCreateParentNode( void* context );
347 
348  friend class ProviderHandle;
355  bool createNameBinding( const char* name, ProviderHandle* provider );
356 
362  bool removeNameBinding( ProviderHandle* provider );
363 
364  static Statistics* createInstance();
365 
367 
368  private:
369 
370  static Statistics* s_instance;
371  ReentrantMutex m_mutex;
372 
373 
376  DataProviderNameTable m_providerNames;
377  DataProviderTable m_providers;
378  ProviderHandleRef m_rootProvider;
379  bool m_enabled;
380  };
381 
382 
387  class AGXCORE_EXPORT Statistics::ProviderHandle : public Referenced
388  {
389  public:
390 
395  ProviderHandle( void* owner, Statistics* statistics );
396 
400  const char* getName();
401 
405  void setName(const char *name);
406 
410  void *getOwner();
411  const void *getOwner() const;
412 
413 
418  template <typename T>
419  T *getOwner();
420 
421  template <typename T>
422  const T *getOwner() const;
423 
428  AbstractData* getData( const char* name ) const;
429 
433  ProviderHandle* getParent();
434 
439  void addChild( ProviderHandle* child );
440 
446  ProviderHandle* getChild( const char* name );
447 
451  ProviderHandle* getChild( size_t index );
452 
456  size_t getNumChildren() const;
457 
462 
463 
467  bool isTaskNode() const;
468 
469 
475  bool isImplicit() const;
476 
477 
481  bool isRoot() const;
482 
488  void clear( bool recursive = false, bool clearAccumulated = true );
489 
490 
494  void print( std::ostream& stream, bool includeTaskTimers, unsigned depth = 0 );
495 
499  const DataTable& getDataTable() const;
500 
501  protected:
502  virtual ~ProviderHandle();
503 
504  friend class Statistics;
505 
509  template< typename T >
510  Data<T>* getOrCreateData( const char* name );
511 
512 
516  void takeChildren( ProviderHandle* currentParent );
517 
521  void adoptChild( ProviderHandle* child );
522 
526  virtual bool isTaskTimer( AbstractData* data );
527 
531  void setIsTaskNode( bool isTaskNode );
532 
539 
540  protected:
541  void *m_owner;
542  const char* m_name;
543  ProviderHandle *m_parent;
546  DataTable m_dataTable; // All data elements reported to this handle.
548 
549  // Enum listing the masks that may be used with the 'm_flags' member variable.
550  enum FlagFields {
551  IS_TASK_NODE = 0x1,
552  SUBTREE_IS_EMPTY = 0x2
553  };
554  };
555 
556 
561  class AGXCORE_EXPORT Statistics::AbstractData : public Referenced
562  {
563  public:
564  AbstractData( const char* name, const agxData::Format* format );
565 
566  const agxData::Format *getFormat() const;
567 
568  const char* getName() const;
569  bool isTiming() const;
570 
575  void toString( String& result ) const;
576 
580  String toString() const;
581 
585  virtual agxData::Value *toValue() const = 0;
586 
591  void accumulatedToString( String& result ) const;
592 
598 
603  template< typename T >
605 
610  virtual void clear( bool clearAccumulated = true ) = 0;
611 
612 
613  void setIsTiming( bool isTiming = true );
614 
615  protected:
616  virtual ~AbstractData();
617 
621  virtual const void* valuePtr() const = 0;
625  virtual const void* accumulatedPtr() const = 0;
626 
627  private:
628  const char* m_name;
629  const agxData::Format* m_format;
630  bool m_isTiming;
631  };
632 
637  template <typename T>
638  class Statistics::Data : public AbstractData
639  {
640  public:
641  Data( const char* name);
642 
643  virtual agxData::Value *toValue() const override;
644 
649  T& value();
650  const T& value() const;
651 
657  const T& accumulatedValue() const;
658 
659 
661 
662  /*
663  Sets the current value to 'value' and increments the accumulated value by the same amount.
664  */
665  void report( const T& value );
666 
671  virtual void clear( bool clearAccumulated = true ) override;
672 
673  protected:
674  virtual ~Data();
675 
676  virtual const void* valuePtr() const override;
677  virtual const void* accumulatedPtr() const override;
678 
679  private:
680  T m_value;
681  T m_accumulatedValue;
682  agx::UInt m_numAccumulated;
683  };
684 
685 
686 
688 
689 
694  class AGXCORE_EXPORT TaskTimingReportHandle : public Statistics::ProviderHandle
695  {
696  public:
697  TaskTimingReportHandle( void* owner, Statistics* statistics );
698 // void report( Real wallTime, Real computeCost, Real overhead, void* context );
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 );
703 
704  Statistics::Data<Real>* getWallTime();
705  Statistics::Data<Real>* getOverhead();
706  Statistics::Data<Real>* getComputeCost();
707 
708  protected:
709  virtual bool isTaskTimer( Statistics::AbstractData* data );
710  private:
711  Statistics::Data<Real> *m_computeCost;
712  Statistics::Data<Real> *m_wallTime;
713  Statistics::Data<Real> *m_overhead;
714  };
715 
716  class StatisticsScopeTimer {
717  public:
718  StatisticsScopeTimer(void *instance, const char *name) : m_instance(instance), m_name(name)
719  {
720  m_timer.start();
721  }
722 
723  ~StatisticsScopeTimer()
724  {
725  m_timer.stop();
726  AGX_STATISTICS_REPORT_ADDITIONAL_TIMING(m_instance, m_name, (Real32)m_timer.getTime());
727  }
728 
729  private:
730  void *m_instance;
731  const char *m_name;
732  Timer m_timer;
733  };
734 
735 
736  /* Implementation*/
737 
738  template <typename T>
739  Statistics::AbstractData* Statistics::report( void* instance, const char* name, const T& value )
740  {
741  if ( ! m_enabled )
742  return nullptr;
743 
744  // The typesystem my register task timer providers. Hence the typesystem lock must be taken first
745  // to avoid potential lock order inversion.
746  //
747  // The call to provider->getOrCreateData<T>( name ) might take the typesystem lock so
748  // we must take the typesystem lock here before m_mutex
749  ScopeLock<ReentrantMutex> typeSystemLock( getTypeSystemMutex() );
750 
751 
752  ScopeLock<ReentrantMutex> lock( m_mutex );
753 
754  // Get the provider handle.
755  DataProviderTable::iterator providerIt = m_providers.find( instance );
756  if ( providerIt == m_providers.end() )
757  {
758  LOGGER_WARNING() << "Trying to report statistics from an instance that hasn't been registered as a data provider yet." << LOGGER_ENDL();
759  return nullptr;
760  }
761 
762  // Get the data handle. Make sure it has the expected type.
763  ProviderHandle* provider = providerIt->second;
764  Data<T>* data = provider->getOrCreateData<T>( name );
765  if ( data == nullptr )
766  {
767  const agxData::Format* actualFormat = provider->getData( name )->getFormat();
768  LOGGER_WARNING() << "Reporting data of type \'" <<
769  agxData::getType<T>()->getName() << ":" << agxData::getFormat<T>()->getName() << "\'" <<
770  " to data point \'" << name << ", which has already received data of type \'" <<
771  actualFormat->getType()->getName() << ":" << actualFormat->getName() << "\'. " <<
772  "Cannot mix data types, so newest data point is ignored." << LOGGER_ENDL();
773  return nullptr;
774  }
775 
776  // Update data.
777  data->value() = value;
778  data->accumulatedValue() += value;
779  return data;
780  }
781 
782  template< typename T >
783  AGX_FORCE_INLINE Statistics::AbstractData* Statistics::reportTiming( void* instance, const char* name, const T& timing )
784  {
785  AbstractData* timingData = this->report( instance, name, (Real)timing );
786  if ( timingData )
787  timingData->setIsTiming( true );
788  return timingData;
789  }
790 
791  AGX_FORCE_INLINE Statistics::AbstractData* Statistics::reportTiming( void* instance, const char* name, const Timer& timer )
792  {
793  AbstractData* timingData = this->report( instance, name, (Real)timer.getTime() );
794  if ( timingData )
795  timingData->setIsTiming( true );
796  return timingData;
797  }
798 
799  template <typename T>
800  AGX_FORCE_INLINE Statistics::AbstractData* Statistics::reportAdditionalTiming( void* instance, const char* name, const T& timing )
801  {
802  if ( !m_enabled )
803  return nullptr;
804 
805  ScopeLock<ReentrantMutex> lock( m_mutex );
806 
807  // Get the data that should be incremented. Make sure it has the expected type.
808  Data<T>* data = this->getData<T>( instance, name );
809  if ( data == nullptr )
810  {
811  return reportTiming( instance, name, timing );
812  }
813 
814  // Do the increment.
815  data->value() += timing;
816  data->accumulatedValue() += timing;
817 
818  return data;
819  }
820 
821  AGX_FORCE_INLINE Statistics::AbstractData* Statistics::reportAdditionalTiming( void* instance, const char* name, const Timer& timer )
822  {
823  return this->reportAdditionalTiming( instance, name, (Real)timer.getTime() );
824  }
825 
826 
827 #if 0
828 // This method was believed to be required, but apparently not yet.
829  template <typename T>
830  AGX_FORCE_INLINE Statistics::AbstractData* Statistics::report( void* /*instance*/, void* /*context*/, const char* /*name*/, const T& /*value*/ )
831  {
832  if ( ! m_enabled )
833  return;
834  agxAbort1("Context altering reporting not yet implemented.");
835  }
836 #endif
837 
838 
839  AGX_FORCE_INLINE Statistics::AbstractData* Statistics::getData(void *instance, const char* name)
840  {
841  DataProviderTable::iterator it = m_providers.find( instance );
842  return it != m_providers.end() ? it->second->getData( name ) : nullptr;
843  }
844 
845 
846  template <typename T>
847  AGX_FORCE_INLINE Statistics::Data<T>* Statistics::getData(void* instance, const char* name)
848  {
849  AbstractData *data = this->getData( instance, name );
850  if ( data == nullptr || dynamic_cast<Data<T> *>(data) == nullptr )
851  return nullptr;
852  return static_cast<Data<T> *>( data );
853  }
854 
855 
857  {
858  DataProviderTable::iterator it = m_providers.find( instance );
859  return it != m_providers.end() ? it->second : nullptr;
860  }
861 
863  {
864  return m_rootProvider;
865  }
866 
868  {
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();
873 
874  return it->second;
875  }
876 
877 
878 
879  //---------------------------------------------------------------
880 
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)
883  {
884  this->setIsTaskNode( false );
885  }
886 
887 
888  AGX_FORCE_INLINE Statistics::ProviderHandle::~ProviderHandle()
889  {
890  }
891 
892 
893  AGX_FORCE_INLINE const char* Statistics::ProviderHandle::getName()
894  {
895  return this->isImplicit() ? "<Implicit node>" : m_name;
896  }
897 
898  AGX_FORCE_INLINE void* Statistics::ProviderHandle::getOwner()
899  {
900  return m_owner;
901  }
902 
903  AGX_FORCE_INLINE const void* Statistics::ProviderHandle::getOwner() const
904  {
905  return m_owner;
906  }
907 
908  AGX_FORCE_INLINE Statistics::ProviderHandle* Statistics::ProviderHandle::getParent()
909  {
910  return m_parent;
911  }
912 
913  template< typename T >
914  AGX_FORCE_INLINE T* Statistics::ProviderHandle::getOwner()
915  {
916  return dynamic_cast<T*>( this->getOwner() );
917  }
918 
919  template< typename T >
920  AGX_FORCE_INLINE const T* Statistics::ProviderHandle::getOwner() const
921  {
922  return dynamic_cast<const T*>( this->getOwner() );
923  }
924 
925  AGX_FORCE_INLINE Statistics::ProviderHandle* Statistics::ProviderHandle::getChild( const char* name )
926  {
927  // Linear search for the child with the wanted name.
928  for ( size_t i = 0 ; i < m_children.size() ; ++i )
929  if ( !m_children[i]->isImplicit() && ::strcmp(name, m_children[i]->m_name) == 0 )
930  return m_children[i];
931 
932  return nullptr;
933  }
934 
935  AGX_FORCE_INLINE Statistics::ProviderHandle* Statistics::ProviderHandle::getChild( size_t index ) { return m_children[index]; }
936  AGX_FORCE_INLINE const Statistics::ProviderHandleRefVector& Statistics::ProviderHandle::getChildren() const { return m_children; }
937  AGX_FORCE_INLINE size_t Statistics::ProviderHandle::getNumChildren() const { return m_children.size(); }
938 
939  AGX_FORCE_INLINE bool Statistics::ProviderHandle::isTaskNode() const { return m_flags & IS_TASK_NODE; }
940  AGX_FORCE_INLINE bool Statistics::ProviderHandle::isImplicit() const { return m_name == nullptr; }
941  AGX_FORCE_INLINE const Statistics::DataTable& Statistics::ProviderHandle::getDataTable() const { return m_dataTable; }
942 
943  AGX_FORCE_INLINE bool Statistics::ProviderHandle::isRoot() const
944  {
945  if ( !m_parent && this != m_statistics->getRootProviderHandle() )
946  LOGGER_ERROR() << "Internal Error: Found a root that isn't the same is the Statistics root node. This should not happen?" << LOGGER_ENDL();
947 
948  return !m_parent;
949  }
950 
951 
952 
953  template< typename T >
954  Statistics::Data<T>* Statistics::ProviderHandle::getOrCreateData( const char* name )
955  {
956  DataTable::iterator it = m_dataTable.find( name );
957  if ( it != m_dataTable.end() )
958  {
959  AbstractData* data = it->second;
960  return dynamic_cast< Data<T>* >( data );
961  }
962  else
963  {
964  const char* internalName = Statistics::copyString( name );
965  Data<T>* newData = new Data<T>( internalName ); // Pass ownership of 'internalName' to 'newData'.
966  m_dataTable.insert( newData->getName(), newData );
967  return newData;
968  }
969  }
970 
971 
972  AGX_FORCE_INLINE bool Statistics::ProviderHandle::isTaskTimer( AbstractData* )
973  {
974  return false;
975  }
976 
977 
978  AGX_FORCE_INLINE void Statistics::ProviderHandle::setIsTaskNode( bool isTaskNode )
979  {
980  if ( isTaskNode )
982  else
983  m_flags &= ~IS_TASK_NODE;
984  }
985 
986  //---------------------------------------------------------------
987 
988  AGX_FORCE_INLINE TaskTimingReportHandle::TaskTimingReportHandle(void* owner, Statistics* statistics) :
989  Statistics::ProviderHandle( owner, statistics )
990  {
991  this->setIsTaskNode( true );
992  m_computeCost = this->getOrCreateData<Real>( "ComputeCost" );
993  m_wallTime = this->getOrCreateData<Real>( "WallTime" );
994  m_overhead = this->getOrCreateData<Real>( "Overhead" );
995  }
996 
997 #if 0
998  AGX_FORCE_INLINE void TaskTimingReportHandle::report( Real wallTime, Real computeCost, Real overhead, void* context )
999  {
1000  if ( !m_statistics->getEnable() )
1001  return;
1002 
1003  if ( context != m_parent->getOwner() )
1004  m_statistics->setContext( m_owner, context );
1005 
1006  this->report( wallTime, computeCost, overhead );
1007  }
1008 #endif
1009 
1010  AGX_FORCE_INLINE void TaskTimingReportHandle::report( Real wallTime, Real computeCost, Real overhead )
1011  {
1012  if ( !m_statistics || !m_statistics->getEnable() )
1013  return;
1014 
1015  m_wallTime->report( wallTime );
1016  m_computeCost->report( computeCost );
1017  m_overhead->report( overhead );
1018  }
1019 
1020  AGX_FORCE_INLINE void TaskTimingReportHandle::reportWallAndOverhead( Real wallTime, Real overhead, void* context )
1021  {
1022  if ( !m_statistics || !m_statistics->getEnable() )
1023  return;
1024 
1025  if ( context != m_parent->getOwner() )
1026  m_statistics->setContext( m_owner, context );
1027 
1028  this->reportWallAndOverhead( wallTime, overhead );
1029  }
1030 
1031  AGX_FORCE_INLINE void TaskTimingReportHandle::reportWallAndOverhead( Real wallTime, Real overhead )
1032  {
1033  if ( !m_statistics || !m_statistics->getEnable() )
1034  return;
1035 
1036  m_wallTime->report( wallTime );
1037  m_overhead->report( overhead );
1038  }
1039 
1040  AGX_FORCE_INLINE void TaskTimingReportHandle::reportCostAndAdditionalOverhead( Real computeCost, Real additionalOverhead )
1041  {
1042  if ( !m_statistics || !m_statistics->getEnable() )
1043  return;
1044 
1045  m_computeCost->report( computeCost );
1046  m_overhead->report( additionalOverhead );
1047  }
1048 
1049 
1050  AGX_FORCE_INLINE bool TaskTimingReportHandle::isTaskTimer( Statistics::AbstractData* data )
1051  {
1052  return data == m_computeCost || data == m_wallTime || data == m_overhead;
1053  }
1054 
1055  //---------------------------------------------------------------
1056 
1057  AGX_FORCE_INLINE Statistics::AbstractData::AbstractData( const char* name, const agxData::Format* format )
1058  : m_name(name), m_format(format), m_isTiming( false )
1059  {
1060  }
1061 
1062  AGX_FORCE_INLINE Statistics::AbstractData::~AbstractData()
1063  {
1064  delete [] m_name;
1065  m_name = nullptr;
1066  }
1067 
1068 
1070  {
1071  return m_format;
1072  }
1073 
1074  AGX_FORCE_INLINE const char* Statistics::AbstractData::getName() const
1075  {
1076  return m_name;
1077  }
1078 
1079  AGX_FORCE_INLINE bool Statistics::AbstractData::isTiming() const
1080  {
1081  return m_isTiming;
1082  }
1083 
1084  AGX_FORCE_INLINE void Statistics::AbstractData::toString( String& result ) const
1085  {
1086  agxAssert(m_format);
1087  if (m_format) {
1088  if ( m_format->getType()->getFormat("ascii") == nullptr )
1089  {
1090  result = String("Can not print value of type \'") + m_format->getType()->getName().c_str() + String("\', no ascii format available");
1091  return;
1092  }
1093  agxData::transform(&result, m_format->getType()->getFormat("ascii"), this->valuePtr(), m_format, 1);
1094  }
1095  }
1096 
1097  AGX_FORCE_INLINE String Statistics::AbstractData::toString() const
1098  {
1099  String result;
1100  this->toString( result );
1101  return result;
1102  }
1103 
1104  AGX_FORCE_INLINE String Statistics::AbstractData::accumulatedToString() const
1105  {
1106  String result;
1107  this->accumulatedToString(result);
1108  return result;
1109  }
1110 
1111 
1112  AGX_FORCE_INLINE void Statistics::AbstractData::accumulatedToString( String& result ) const
1113  {
1114  agxAssert(m_format);
1115  if (m_format) {
1116  agxAssertN(m_format->getType()->getFormat("ascii"), "Can not print value of type \'%s\', no ascii format available...", m_format->getType()->getName().c_str());
1117  if ( m_format->getType()->getFormat("ascii") == nullptr )
1118  {
1119  LOGGER_WARNING() << "Can not print value of type \'" << m_format->getType()->getName().c_str() << "\' to String, no ascii format available";
1120  return;
1121  }
1122  agxData::transform(&result, m_format->getType()->getFormat("ascii"), this->accumulatedPtr(), m_format, 1);
1123  }
1124  }
1125 
1126  template< typename T >
1127  AGX_FORCE_INLINE Statistics::Data<T>* Statistics::AbstractData::asTypedData()
1128  {
1129  return dynamic_cast< Data<T>* >( this );
1130  }
1131 
1132  AGX_FORCE_INLINE void Statistics::AbstractData::setIsTiming( bool isTiming )
1133  {
1134  m_isTiming = isTiming;
1135  }
1136 
1137  //---------------------------------------------------------------
1138 
1139  template <typename T>
1140  AGX_FORCE_INLINE Statistics::Data<T>::Data( const char* name)
1141  : AbstractData( name, agxData::getFormat<T>() ), m_value(T()), m_accumulatedValue(T()), m_numAccumulated(0)
1142  {
1143  }
1144 
1145  template< typename T >
1147  {
1148  }
1149 
1150  template< typename T >
1152  {
1153  agxData::Value *value = new agxData::Value(this->getName(), agxData::getFormat<T>());
1154  value->set<T>(m_value);
1155  return value;
1156  }
1157 
1158 
1159  template <typename T>
1160  AGX_FORCE_INLINE T& Statistics::Data<T>::value() { return m_value; }
1161 
1162  template <typename T>
1163  AGX_FORCE_INLINE const T& Statistics::Data<T>::value() const { return m_value; }
1164 
1165  template <typename T>
1166  AGX_FORCE_INLINE T& Statistics::Data<T>::accumulatedValue() { return m_accumulatedValue; }
1167 
1168  template <typename T>
1169  AGX_FORCE_INLINE const T& Statistics::Data<T>::accumulatedValue() const { return m_accumulatedValue; }
1170 
1171  template <typename T>
1172  AGX_FORCE_INLINE agx::UInt Statistics::Data<T>::getNumAccumulated() const { return m_numAccumulated; }
1173 
1174  template <typename T>
1175  AGX_FORCE_INLINE void Statistics::Data<T>::report( const T& value )
1176  {
1177  m_value = value;
1178  m_accumulatedValue += value;
1179  ++m_numAccumulated;
1180  }
1181 
1182  template <typename T>
1183  AGX_FORCE_INLINE void Statistics::Data<T>::clear( bool clearAccumulated )
1184  {
1185  m_value = T();
1186  if ( clearAccumulated ) {
1187  m_accumulatedValue = T();
1188  m_numAccumulated = 0;
1189  }
1190  }
1191 
1192  template <typename T>
1193  const void* Statistics::Data<T>::valuePtr() const
1194  {
1195  return static_cast< const void* > ( &m_value );
1196  }
1197 
1198  template <typename T>
1199  const void* Statistics::Data<T>::accumulatedPtr() const
1200  {
1201  return static_cast< const void* > ( &m_accumulatedValue );
1202  }
1203 
1204  //---------------------------------------------------------------
1205 
1206 
1208 
1209 }
1210 
1211 
1212 #endif /* AGX_STATISTICS_H */
#define LOGGER_WARNING()
Definition: Logger.h:23
#define LOGGER_ERROR()
Definition: Logger.h:22
#define LOGGER_ENDL()
Definition: Logger.h:27
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:248
#define AGX_STATISTICS_REPORT_ADDITIONAL_TIMING(INSTANCE_ADDRESS, DATA_NAME, VALUE)
Definition: Statistics.h:68
#define AGXCORE_EXPORT
#define AGX_DECLARE_VECTOR_TYPES(type)
Definition: agx/Vector.h:34
A format is an implementation of a agxData::Type.
Definition: Format.h:64
Type * getType()
Definition: Format.h:589
Format * getFormat(const agx::Name &name)
Definition: Type.h:230
Abstract representation of a value.
Definition: Value.h:43
void set(const Value *other)
Explicitly set the value, removes active binding and disables autobinding.
size_t size() const
Definition: Container.h:134
iterator end()
Iterator marking end of hash table.
insert_iterator insert(const KeyT &key, const ValueT value)
Insert a key/value pair into the 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
Definition: Name.h:189
const agx::Name & getName() const
Definition: Object.h:369
Representation of a path, a list of name components.
Definition: Path.h:34
Base class providing referencing counted objects.
Definition: Referenced.h:114
Base class for Singletons that should have its shutdown called explicitly before exit of the applicat...
Definition: Singleton.h:31
Statistics data storage class.
Definition: Statistics.h:639
T & accumulatedValue()
Returns the accumulated value of this statistics data handle.
T & value()
Returns the current value of this statistics data handle.
const T & accumulatedValue() const
Data(const char *name)
agx::UInt getNumAccumulated() const
virtual const void * valuePtr() const override
virtual agxData::Value * toValue() const override
const T & value() const
virtual const void * accumulatedPtr() const override
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.
Definition: Statistics.h:99
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.
T * getOwner()
Convenience method that test if the owner was of a specific type and if so returns a pointer to the o...
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
Definition: Statistics.h:348
ProviderHandleRefVector m_children
Definition: Statistics.h:545
AbstractData * getData(const char *name) const
Statistics * m_statistics
Definition: Statistics.h:544
HashTable< const char *, AbstractDataRef > DataTable
Definition: Statistics.h:105
virtual const void * valuePtr() const =0
Returns a raw pointer to the memory location where the current value is stored.
static bool hasInstance()
void setEnable(bool)
Enables or disables gathering of statistics data.
String accumulatedToString() const
Reference-less.
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.
AbstractData * reportAdditionalTiming(void *instance, const char *name, const T &timing)
Increments the chosen timing value instead of setting it.
virtual void clear(bool clearAccumulated=true)=0
Sets the current and accumulated values to zero, or the equivalent for complex types.
Data< T > * asTypedData()
Converts this abstract representation to a type specified data handle.
size_t getNumChildren() const
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.
bool isTiming() const
void createRootNode()
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
Definition: Statistics.h:543
const T * getOwner() const
const DataTable & getDataTable() const
ProviderHandle * getProviderHandle(void *instance)
Returns the provider handle for the given statistics provider.
ProviderHandle * getChild(const char *name)
AbstractData * getData(const char *providerName, const char *dataName, size_t providerIndex=0)
Get a data handle from the statistics tree.
const char * getName()
void print(std::ostream &stream, bool includeTaskTimers, unsigned depth=0)
Recursively print the data in this ProviderHandle and all its children.
const ProviderHandleRefVector & getChildren() const
virtual ~ProviderHandle()
AbstractData * reportTiming(void *instance, const char *name, const T &timing)
Report a timing statistics.
virtual agxData::Value * toValue() const =0
void * getOwner()
bool registerDataProvider(void *provider, const String &name, void *context=(void *) nullptr)
Convenience method, calls the const char* version of registerDataProvider(*).
AbstractData * reportAdditionalTiming(void *instance, const char *name, const Timer &timer)
Convenience method that reads the current value from a timer.
virtual bool isTaskTimer(AbstractData *data)
Tests if the given data handle corresponds to one of the special task timing values.
virtual ~AbstractData()
TaskTimingReportHandle * registerTaskTimerProvider(void *provider, const char *name, void *context)
Used by the tasks to create a provider handle designed for task timer reporting.
void adoptChild(ProviderHandle *child)
Move a ProviderHandle from its current parent into 'this'.
void accumulatedToString(String &result) const
Converts the accumulated value to a human readable string using the "ascii" format for the type of th...
Data< T > * getData(void *instance, const char *name)
Templated convenience method that returns the result from the non-templated 'getData(*)' with the sam...
Data< T > * getOrCreateData(const char *name)
Makes sure that a data handle with the given names exists.
void setName(const char *name)
Set the name of the provider.
const void * getOwner() const
void setIsTaskNode(bool isTaskNode)
Returns true if the ProviderHandle contains task timing values.
const char * m_name
Definition: Statistics.h:542
agx::Vector< ProviderHandleRef > ProviderHandleRefVector
Definition: Statistics.h:102
AbstractData * getData(void *instance, const char *name)
Get a data handle from a statistics handle.
AbstractData * getData(void *instance, const Path &path)
Get a data handle from the statistics tree.
ProviderHandle * getChild(size_t index)
DataTable m_dataTable
Definition: Statistics.h:546
virtual const void * accumulatedPtr() const =0
Returns a raw pointer to the memory location where the current value is stored.
ProviderHandle * getRootProviderHandle()
Returns a handle to the root of the statistics tree.
void addChild(ProviderHandle *child)
Add the given ProviderHandle to the list of children, and set the parent pointer of the child to the ...
const ProviderHandleRefVector & getProviderHandles(const char *name)
Returns handles to all providers that have been registered with the given name.
AbstractData * getData(const String &providerName, const String &dataName, size_t providerIndex=0)
Get a data handle from the statistics tree.
static const char * copyString(const char *)
All strings held inside the statistics tree is owned by the statistics tree.
AbstractData * report(void *instance, const char *name, const T &value)
Report statistics.
ProviderHandle * getParent()
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 * reportTiming(void *instance, const char *name, const Timer &timer)
Convenience report method that reads the current time from a timer.
const agxData::Format * getFormat() const
void clear()
Reset all statistics values, setting both the current and accumulated value using the type's default ...
const char * getName() const
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.
friend class Statistics
Definition: Statistics.h:504
virtual ~Statistics()
static Statistics * instance()
The Timer class permits timing execution speed with the same refinement as the built in hardware cloc...
Definition: Timer.h:75
#define agxAssertN(expr, format,...)
Definition: debug.h:145
#define agxAbort1(msg)
Definition: debug.h:165
#define agxAssert(expr)
Definition: debug.h:143
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: macros.h:97
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: macros.h:96
#define AGX_FORCE_INLINE
Definition: macros.h:58
Contains classes for low level data storage for AGX.
Definition: Container.h:23
AGXCORE_EXPORT Format * getFormat(const agx::String &typeFormatName)
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.
int32_t Int32
Definition: Integer.h:45
AgXString< std::string > String
Definition: String.h:423
uint32_t UInt
Definition: Integer.h:34
double Real
Definition: Real.h:42
float Real32
Definition: Real.h:44
std::recursive_mutex ReentrantMutex
Definition: Object.h:49
AGXCORE_EXPORT agx::ReentrantMutex & getTypeSystemMutex()
double accumulated
Definition: Statistics.h:84
double current
Definition: Statistics.h:83