D:/WorkDir/SugoiProjects/SugoiTracer/include/SugoiTracer/base.h

Go to the documentation of this file.
00001 /* LICENSE_BEGIN
00002 SutoiTracer library supply support profiling functions, class, or evet simple 
00003 piece of code. It is part of the SugoiEngine project.
00004 Copyright (C) 2006 Allusse Yannick (yannick dot allusse at laposte dot net)
00005 
00006 This program is free software; you can redistribute it and/or
00007 modify it under the terms of the GNU General Public License
00008 as published by the Free Software Foundation; either version 2
00009 of the License, or (at your option) any later version.
00010 
00011 This program is distributed in the hope that it will be useful,
00012 but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 GNU General Public License for more details.
00015 
00016 You should have received a copy of the GNU General Public License
00017 along with this program; if not, write to the Free Software
00018 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00019 LICENSE_END
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"//used to include opengl
00030 //#include "SugoiTracer\class.h"
00031 #include <iostream>
00032 //#include <map>                //to create map for C wrapping
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         //CL_TRACE_BASE_PARAMS(float *_ParamFlt, int _number);  //!< Constructor with array of float.
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                 //create a CL_TRACER
00101                 Tracer = new CL_TRACER<TracerType>(_FctTraced, _Params);
00102                 
00103                 //send it to the CL_FUNCT_TRC
00104                 _FctTraced->TracerToProccess.push_back(Tracer);
00105 
00106                 //StartTracing
00107                 Tracer->StartRecording();
00108         }
00109 
00110         ~CL_TEMP_TRACER()
00111         {//stop tracing
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!");//.IsValide()
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         //definitions
00202         typedef  CL_TRACER<TracerType> *        CL_TRACER_HDLE;
00203         typedef  CL_TRACER<TracerType>          CL_TRACER_NEW;
00204 //end definitions...
00205 
00206         CL_TRACER(CL_FUNCT_TRC<TracerType> * _FctTraced, CL_TRACE_BASE_PARAMS * _Params)
00207                 : CL_VIRTUAL_TRACER<TracerType>(_FctTraced, _Params)
00208                 //CL_VIRTUAL_TRACER<TracerType>::Ready(false)
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();//run the second time....
00224                 SetReady (true);
00225         }
00226 //      TracerType * GetResult(){return Val;};
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),//optimise the use of it...??????
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         void EnableMinMaxTot(bool _val)
00267         {
00268                 UseMinMaxTot = _val;
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                 {//we remove the first element
00293                         TimeRecorded.erase(TimeRecorded.begin());
00294                 }
00295 
00296                 //Then we add at the end, FIFO
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                                 //check for min and max..?
00312                                 if((*_TracerTime) < *MinTime)
00313                                         *MinTime = (*_TracerTime);
00314                                 if ((*_TracerTime) > *MaxTime)
00315                                         *MaxTime = (*_TracerTime);
00316                                 //========================
00317                         }
00318                 }
00319                 else
00320                 {
00321                         //check for min and max..?
00322                         if((*_TracerTime) < *MinTime)
00323                                 *MinTime = (*_TracerTime);
00324                         if ((*_TracerTime) > *MaxTime)
00325                                 *MaxTime = (*_TracerTime);
00326                         //========================
00327                 }
00328                 TotalRecords++;
00329 
00330         /*      if (ParentFunction)
00331                 {
00332                         if((*_TracerTime) < *ParentFunction->MinTime)
00333                                 *ParentFunction->MinTime = (*_TracerTime);
00334                         if ((*_TracerTime) > *ParentFunction->MaxTime)
00335                                 *ParentFunction->MaxTime = (*_TracerTime);
00336                         //++ total records
00337                         ParentFunction->TotalRecords++;
00338                         *ParentFunction->TotalTime += (*TracerTime);
00339                         if (ParentFunction->ParentClass)
00340                                 *ParentFunction->ParentClass->TotalTime += (*TracerTime);
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                         //TotalTime, min and maxtime
00382                         TotalTime->XMLLoad(_XML_ROOT, "TotalTime");
00383                         MinTime->XMLLoad(_XML_ROOT, "MinTime");
00384                         MaxTime->XMLLoad(_XML_ROOT, "MaxTime");
00385                         //===========================
00386                 }
00387                 //get nbre of records
00388                 XMLReadVal(_XML_ROOT,"TotalRecords", TotalRecords);
00389                 XMLReadVal(_XML_ROOT,"MaxRecords", MaxRecords);
00390 
00391                 //Load params
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;//no XML tag for params
00398                 }
00399 #else
00400                 //if (_XML_ROOT->Attribute("params"))
00401                 {
00402                         Params = new CL_TRACE_BASE_PARAMS("", std::string (""));
00403                         //if (!Params->XMLLoad(_XML_ROOT))
00404                         //      delete Params;//no XML tag for params
00405                         Params->Params = GetID();
00406                 }
00407 
00408 #endif
00409                 //load all the records...???
00410 
00411                 //if (!UseMinMaxTot)
00412                 //load all if we find them
00413                         XMLLoadVector<TracerType>(_XML_ROOT, TimeRecorded, "ValVector", "Val");         
00414                 
00415                 //..
00416                 /*if (!UseMinMaxTot)
00417                         TcerRecorded->XMLLoad("functlist", _XML_ROOT);
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         //      Process();
00427                 if (ParentFunction)
00428                 {//we synchronize the record with the function it belongs to
00429                         UseMinMaxTot  = ParentFunction->UseMinMaxTot;
00430                         /*if (ParentFunction->UseMinMaxTot==false)
00431                                 cout << "Parent function UseminMaxTot is false" << endl;
00432                         else
00433                                 cout << "Parent function UseminMaxTot is true" << endl;
00434                         */
00435                 }
00436 
00437                 //save params
00438                 if (Params)
00439                         Params->XMLSave(_XML_ROOT);
00440 
00441                 if(UseMinMaxTot)
00442                 {
00443                         //TotalTime, min and maxtime
00444                         TotalTime->XMLSave(_XML_ROOT, "TotalTime");
00445                         MinTime->XMLSave(_XML_ROOT, "MinTime");
00446                         MaxTime->XMLSave(_XML_ROOT, "MaxTime");
00447                 }
00448                 else
00449                 {//load all
00450                         XMLSaveVector<TracerType>(_XML_ROOT, TimeRecorded, "ValVector", "Val");         
00451                 }
00452 
00453                 //set nbre of records
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         /*      if(!UseMinMaxTot)
00462                         TracerRecorded->XMLSave("functlist", _XML_ROOT, true);
00463         */
00464                 return _XML_ROOT;
00465         }
00466 #endif
00467 };
00468 
00469 };//namespace SG_TRC
00470 #endif//CPP
00471 #endif //_SG_TRC_BASE_H

Generated on Mon Mar 19 23:16:20 2007 for SugoiTracer by  doxygen 1.4.6-NO