00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef _SG_TRC_BASE_H
00023 #define _SG_TRC_BASE_H
00024
00025 #include "SugoiTracer\config.h"
00026 #include "SugoiTracer\timer.h"
00027 #include <SugoiTools\cl_xml_mngr.h>
00028 #include "SugoiTracer\function.h"
00029 #include "SugoiTools\cl_pos.h"
00030
00031 #include <iostream>
00032
00033
00034 #ifdef __cplusplus
00035 namespace SG_TRC{
00036
00037
00038
00039
00040
00041
00042
00043
00051 class _SG_TRC_DLL_EXPORT
00052 CL_TRACE_BASE_PARAMS
00053 :public SGE::CL_XML_OBJ
00054 {
00055 public:
00056 CL_TRACE_BASE_PARAMS();
00057 CL_TRACE_BASE_PARAMS(std::string _paramName, std::string &_ParamStr);
00058
00059 CL_TRACE_BASE_PARAMS(std::string _paramName,int *_ParamInt, int _number);
00060 ~CL_TRACE_BASE_PARAMS();
00061 std::string Params;
00062
00063 void AddInt(std::string _paramName, const int * _IntArray, int _nbr =1, int _stringSize=3);
00064 void AddChar(std::string _paramName, const char * _CharArray);
00065 void ParseParams(int _ParamID, std::string & _DestStrArr);
00066 bool SplitParam(const std::string &_src, std::string &_name, std::string &_val);
00067 #if _SG_TLS_XML
00068 public:
00069 _SG_TLS_INLINE TiXmlElement* XMLLoad(TiXmlElement* _XML_ROOT);
00070 _SG_TLS_INLINE TiXmlElement* XMLSave(TiXmlElement* _XML_ROOT);
00071 #endif
00072 };
00073
00074
00075
00076
00077
00078
00079
00085 template <typename TracerType =SG_TRC_Default_Trc_Type>
00086 class CL_TEMP_TRACER
00087 {
00088 private :
00089 bool m_openGL;
00090 bool m_debug;
00091 public:
00092
00093
00094 CL_TEMP_TRACER(CL_FUNCT_TRC<TracerType> *_FctTraced, CL_TRACE_BASE_PARAMS * _Params)
00095 :m_openGL(false),
00096 m_debug(false)
00097 {
00098 SG_Assert(_FctTraced, "CL_TEMP_TRACER(), empty function!");
00099
00100
00101 Tracer = new CL_TRACER<TracerType>(_FctTraced, _Params);
00102
00103
00104 _FctTraced->TracerToProccess.push_back(Tracer);
00105
00106
00107 Tracer->StartRecording();
00108 }
00109
00110 ~CL_TEMP_TRACER()
00111 {
00112 if(m_openGL)
00113 {
00114 glFlush();glFinish();
00115 }
00116 Tracer->StopRecording();
00117 if (m_debug)
00118 {
00119 std::cout << Tracer->GetFctTraced()->GetIDStr() << "("
00120 <<Tracer->GetParams()->Params <<") : "
00121 <<Tracer->GetResult()->GetStr() << std::endl;
00122 }
00123
00124 }
00125
00126 CL_TRACER<TracerType>* GetTracer()
00127 {
00128 SG_Assert(Tracer, "CL_TEMP_TRACER::GetTracer(), empty Tracer!");
00129 return Tracer;
00130 }
00131
00132 void SetOpenGLTracer(bool _val)
00133 {
00134 m_openGL = _val;
00135 }
00136
00137 void SetDebug(bool _val)
00138 {
00139 m_debug = _val;
00140 }
00141
00142
00143 private:
00144 CL_TRACER<TracerType>* Tracer;
00145 };
00146
00147
00148
00149
00150
00151 template <typename TracerType=SG_TRC_Default_Trc_Type>
00152 class CL_VIRTUAL_TRACER
00153 {
00154 public:
00155
00156
00157 CL_VIRTUAL_TRACER(CL_FUNCT_TRC<TracerType> * _FctTraced, CL_TRACE_BASE_PARAMS * _Params)
00158 :FctTraced(_FctTraced),
00159 Params(_Params),
00160 Ready(true),
00161 Val(new TracerType())
00162 {
00163 SG_Assert(_FctTraced, "CL_VIRTUAL_TRACER(), empty function!");
00164 }
00165
00166 ~CL_VIRTUAL_TRACER()
00167 {}
00168
00169
00170
00171 virtual
00172 TracerType* GetResult()
00173 {return Val;};
00174
00175 void SetReady(bool _b){Ready = _b;};
00176 bool GetReady(){return Ready;};
00177 CL_FUNCT_TRC<TracerType> *GetFctTraced()
00178 { return FctTraced; }
00179 CL_TRACE_BASE_PARAMS* GetParams(){return Params;};
00180
00181 protected:
00182 TracerType* Val;
00183 bool Ready;
00184 CL_TRACE_BASE_PARAMS* Params;
00185 CL_FUNCT_TRC<TracerType> * FctTraced;
00186 };
00187
00188
00189
00190
00191
00196 template <typename TracerType=SG_TRC_Default_Trc_Type>
00197 class CL_TRACER :
00198 public CL_VIRTUAL_TRACER<TracerType>
00199 {
00200 public:
00201
00202 typedef CL_TRACER<TracerType> * CL_TRACER_HDLE;
00203 typedef CL_TRACER<TracerType> CL_TRACER_NEW;
00204
00205
00206 CL_TRACER(CL_FUNCT_TRC<TracerType> * _FctTraced, CL_TRACE_BASE_PARAMS * _Params)
00207 : CL_VIRTUAL_TRACER<TracerType>(_FctTraced, _Params)
00208
00209 {
00210 SetReady(false);
00211 }
00212
00213 ~CL_TRACER(){};
00214
00215 void StartRecording()
00216 {
00217 SetReady(false);
00218 Val->Run();
00219 }
00220
00221 void StopRecording()
00222 {
00223 Val->Run();
00224 SetReady (true);
00225 }
00226
00227 };
00228
00229
00230
00231
00236 template <typename TracerType=SG_TRC_Default_Trc_Type>
00237 class CL_TRACER_RECORD:
00238 public SGE::CL_BASE_OBJ<std::string>,
00239 public SGE::CL_XML_OBJ
00240 {
00241 private :
00242 bool UseMinMaxTot;
00243 public:
00244 CL_TRACER_RECORD(std::string _Name, CL_TRACE_BASE_PARAMS*_Params = NULL):
00245 SGE::CL_BASE_OBJ<std::string>(_Name),
00246 SGE::CL_XML_OBJ("record"),
00247 Params(_Params),
00248 ParentFunction(NULL),
00249 TotalRecords(0),
00250 MinTime(new TracerType(10000000)),
00251 MaxTime(new TracerType()),
00252 TotalTime(new TracerType()),
00253 UseMinMaxTot(true),
00254 MaxRecords(_SG_TRC_MAX_NBR_TIMES)
00255 {
00256 }
00257
00259 ~CL_TRACER_RECORD()
00260 {
00261 delete MinTime;
00262 delete MaxTime;
00263 delete TotalTime;
00264 }
00265
00266
00267
00268
00269
00270
00272 void SetParentFunction(CL_FUNCT_TRC<TracerType>* _ParentFunction)
00273 {
00274 ParentFunction = _ParentFunction;
00275 if (ParentFunction)
00276 UseMinMaxTot = _ParentFunction->UseMinMaxTot;
00277 }
00278
00279
00280 std::vector<TracerType*> TimeRecorded;
00281 TracerType *TotalTime;
00282 TracerType *MinTime;
00283 TracerType *MaxTime;
00284
00285 _SG_TLS_INLINE
00286 void
00287 AddTracer(CL_TRACER<TracerType>*_newTracer)
00288 {
00289 TracerType *TracerTime = _newTracer->GetResult();
00290
00291 if (TimeRecorded.size() >= MaxRecords)
00292 {
00293 TimeRecorded.erase(TimeRecorded.begin());
00294 }
00295
00296
00297 TimeRecorded.push_back(TracerTime);
00298
00299 RecordStats(TracerTime);
00300 }
00301
00302 _SG_TLS_INLINE
00303 void
00304 RecordStats(TracerType *_TracerTime)
00305 {
00306 if (ParentFunction)
00307 {
00308 if (ParentFunction->UseMinMaxTot)
00309 {
00310 *TotalTime += (*_TracerTime);
00311
00312 if((*_TracerTime) < *MinTime)
00313 *MinTime = (*_TracerTime);
00314 if ((*_TracerTime) > *MaxTime)
00315 *MaxTime = (*_TracerTime);
00316
00317 }
00318 }
00319 else
00320 {
00321
00322 if((*_TracerTime) < *MinTime)
00323 *MinTime = (*_TracerTime);
00324 if ((*_TracerTime) > *MaxTime)
00325 *MaxTime = (*_TracerTime);
00326
00327 }
00328 TotalRecords++;
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342 }
00343
00344
00345 _SG_TLS_INLINE
00346 void
00347 SetParams(CL_TRACE_BASE_PARAMS*_Params)
00348 {Params = _Params;}
00350
00351 _SG_TLS_INLINE
00352 CL_TRACE_BASE_PARAMS*
00353 GetParams()
00354 {return Params;}
00355
00356 _SG_TLS_INLINE
00357 void
00358 SetMaxRecords(unsigned int _size)
00359 {MaxRecords= _size;}
00361
00362
00363 unsigned int GetTotalRecords() {return TotalRecords;}
00364 unsigned int GetMaxRecords() {return MaxRecords;}
00365
00366 private:
00367 unsigned int TotalRecords;
00368 unsigned int MaxRecords;
00369 CL_TRACE_BASE_PARAMS*Params;
00370 CL_FUNCT_TRC<TracerType> *ParentFunction;
00371
00372
00373 #if _SG_TLS_XML
00374 public:
00375 _SG_TLS_INLINE TiXmlElement* XMLLoad(TiXmlElement* _XML_ROOT)
00376 {
00377 SG_Assert(_XML_ROOT, "No XML root");
00378
00379 if (UseMinMaxTot)
00380 {
00381
00382 TotalTime->XMLLoad(_XML_ROOT, "TotalTime");
00383 MinTime->XMLLoad(_XML_ROOT, "MinTime");
00384 MaxTime->XMLLoad(_XML_ROOT, "MaxTime");
00385
00386 }
00387
00388 XMLReadVal(_XML_ROOT,"TotalRecords", TotalRecords);
00389 XMLReadVal(_XML_ROOT,"MaxRecords", MaxRecords);
00390
00391
00392 #if 0
00393 if (_XML_ROOT->Attribute("params"))
00394 {
00395 Params = new CL_TRACE_BASE_PARAMS("", std::string (""));
00396 if (!Params->XMLLoad(_XML_ROOT))
00397 delete Params;
00398 }
00399 #else
00400
00401 {
00402 Params = new CL_TRACE_BASE_PARAMS("", std::string (""));
00403
00404
00405 Params->Params = GetID();
00406 }
00407
00408 #endif
00409
00410
00411
00412
00413 XMLLoadVector<TracerType>(_XML_ROOT, TimeRecorded, "ValVector", "Val");
00414
00415
00416
00417
00418
00419 return _XML_ROOT;
00420 }
00421
00422 _SG_TLS_INLINE TiXmlElement* XMLSave(TiXmlElement* _XML_ROOT)
00423 {
00424 SG_Assert(_XML_ROOT, "No XML root");
00425
00426
00427 if (ParentFunction)
00428 {
00429 UseMinMaxTot = ParentFunction->UseMinMaxTot;
00430
00431
00432
00433
00434
00435 }
00436
00437
00438 if (Params)
00439 Params->XMLSave(_XML_ROOT);
00440
00441 if(UseMinMaxTot)
00442 {
00443
00444 TotalTime->XMLSave(_XML_ROOT, "TotalTime");
00445 MinTime->XMLSave(_XML_ROOT, "MinTime");
00446 MaxTime->XMLSave(_XML_ROOT, "MaxTime");
00447 }
00448 else
00449 {
00450 XMLSaveVector<TracerType>(_XML_ROOT, TimeRecorded, "ValVector", "Val");
00451 }
00452
00453
00454 XMLWriteVal(_XML_ROOT,"TotalRecords", TotalRecords);
00455 if (MaxRecords!= _SG_TRC_MAX_NBR_TIMES)
00456 XMLWriteVal(_XML_ROOT,"MaxRecords", MaxRecords);
00457
00458
00459
00460
00461
00462
00463
00464 return _XML_ROOT;
00465 }
00466 #endif
00467 };
00468
00469 };
00470 #endif//CPP
00471 #endif //_SG_TRC_BASE_H