00001 /* $Id:$ */ 00002 00003 #include <esg/List.h> 00004 #include <time.h> 00005 #include <string.h> 00006 #include <iostream> 00007 00008 #ifndef STATISTICS_H 00009 #define STATISTICS_H 00010 00015 namespace esg { 00016 00022 class Receptor { 00023 public: 00027 virtual ~Receptor () {} 00028 00032 virtual void init () = 0; 00033 00039 virtual void print (std::ostream& stream) const = 0; 00040 }; 00041 00045 class Counter : public Receptor { 00046 protected: 00047 unsigned long _value; 00048 00049 public: 00053 Counter () : _value(0) {} 00054 00055 virtual void init () { _value = 0; } 00056 virtual void print (std::ostream& stream) const { stream << _value; } 00057 00061 void increment () { _value++; } 00062 00066 void decrement () { _value--; } 00067 00073 void set (unsigned long v) { _value = v; } 00074 00080 void add (unsigned long v) { _value += v; } 00081 00085 unsigned long getValue () const { return _value; } 00086 }; 00087 00088 00095 class Stopwatch : public Receptor { 00096 protected: 00097 time_t _startTime, _stopTime; 00098 double _timeDist; 00099 00100 public: 00104 Stopwatch () : _timeDist(0.) {} 00105 00106 virtual void init () { _timeDist = 0.; } 00107 virtual void print (std::ostream& stream) const { stream << _timeDist; } 00108 00112 virtual void start () { time(&_startTime); } 00113 00118 virtual void stop () { 00119 time(&_stopTime); 00120 _timeDist += difftime(_stopTime, _startTime); 00121 } 00122 00126 double getTime () const { return _timeDist; } 00127 }; 00128 00133 class StopwatchCPU : public Stopwatch { 00134 public: 00135 virtual void start () { _startTime = clock(); } 00136 virtual void stop () { 00137 _stopTime = clock(); 00138 _timeDist += (double)((_stopTime-_startTime)/CLOCKS_PER_SEC); 00139 } 00140 }; 00141 00142 00147 class Statistic { 00148 public: 00149 typedef unsigned OID; 00150 00151 protected: 00152 OID _oid; 00153 char * _name; 00154 Receptor * _pReceptor; 00155 00156 public: 00164 Statistic(OID o, const char* n, Receptor* r) : _oid(o), _pReceptor(r) { 00165 _name = new char [strlen(n)+1]; 00166 strcpy(_name, n); 00167 } 00168 00172 virtual ~Statistic () { 00173 if (_name) delete _name; 00174 if (_pReceptor) delete _pReceptor; 00175 } 00176 00180 const char* name () const { return _name; } 00181 00185 OID oid () const { return _oid; } 00186 00190 Receptor* receptor () const { return _pReceptor; } 00191 }; 00192 00193 00197 class Statistics { 00198 private: 00199 static Statistics * _instance; 00200 00201 public: 00205 static const unsigned NUM_OIDS; 00206 static const Statistic::OID OID_BV_TESTS; 00207 static const Statistic::OID OID_BV_TESTS_SUC; 00208 static const Statistic::OID OID_PRIM_TESTS; 00209 static const Statistic::OID OID_PRIM_TESTS_SUC; 00210 static const Statistic::OID OID_SHADOW_TESTS; 00211 static const Statistic::OID OID_SHADOW_TESTS_SUC; 00212 static const Statistic::OID OID_REFLECTIONS; 00213 static const Statistic::OID OID_REFRACTIONS; 00214 static const Statistic::OID OID_RENDERING_TIME; 00215 static const Statistic::OID OID_CPU_RENDERING_TIME; 00216 static const Statistic::OID OID_PRIMARY_RAYS; 00217 00218 protected: 00219 Statistic* _stats [11]; 00220 00221 protected: 00222 Statistics (); 00223 00224 public: 00228 virtual ~Statistics () { 00229 for (unsigned i = 0; i < NUM_OIDS; i++) delete _stats[i]; 00230 } 00231 00235 static Statistics* instance() { 00236 if (!_instance) _instance = new Statistics; 00237 return _instance; 00238 } 00239 00244 Statistic* get (Statistic::OID i) { return _stats[i]; } 00245 }; 00246 00247 00248 }; // namespace 00249 00250 00251 #endif // STATISTICS_H