D:/WorkDir/SugoiProjects/SugoiTracer/src/appli.cpp

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 #include "SugoiTracer\base.h"
00022 
00023 namespace SG_TRC{
00024 using namespace SGE;
00025 //================================================================
00026 //================================================================
00027 
00028 CL_TRACE_BASE_PARAMS::CL_TRACE_BASE_PARAMS()
00029                 : SGE::CL_XML_OBJ("params")
00030 {
00031 
00032 }
00033 
00034 CL_TRACE_BASE_PARAMS::
00035 	CL_TRACE_BASE_PARAMS(std::string _paramName, std::string &_ParamStr)
00036                 : SGE::CL_XML_OBJ("params")
00037 {
00038         Params = "|";
00039         Params += _paramName;
00040         Params += ":";
00041         Params +=_ParamStr;
00042         Params += "|";
00043 }
00044 
00045 /*
00046 CL_TRACE_BASE_PARAMS::CL_TRACE_BASE_PARAMS(std::string _paramName,float *_ParamFlt, int _number)
00047 {}
00048 */
00049 CL_TRACE_BASE_PARAMS::CL_TRACE_BASE_PARAMS(std::string _paramName,int *_ParamInt, int _number)
00050         : SGE::CL_XML_OBJ("params")
00051 {
00052         AddInt(_paramName, _ParamInt, _number);
00053 }
00054 
00055 CL_TRACE_BASE_PARAMS::~CL_TRACE_BASE_PARAMS(){}
00056 
00057 void CL_TRACE_BASE_PARAMS::AddInt(std::string _paramName, const int * _IntArray, int _nbr/* =1*/, int _stringSize/*=3*/)
00058 {
00059         Params += "|";
00060         Params +=_paramName;
00061         Params += ":";
00062 
00063         //char buff[16];
00064         for(int i =0; i < _nbr; i++)
00065         {
00066                 //Params += IntToString(_IntArray[i],_stringSize);
00067                 Params += ToCharStr(_IntArray[i]);
00068                 if (i< _nbr - 1)
00069                         Params += "/";
00070         }
00071         Params += "|";
00072 }
00073 
00074 void CL_TRACE_BASE_PARAMS::AddChar(std::string _paramName, const char * _CharArray)
00075 {
00076         Params += "|";
00077         Params +=_paramName;
00078         Params += ":";
00079         Params += _CharArray;
00080         Params += "|";
00081 }
00082 
00083 void CL_TRACE_BASE_PARAMS::ParseParams(int _ParamID, std::string & _DestStrArr)
00084 {
00085         //look into the params and parse it for different parameters
00086         //_ParamID is the offset of the param we are looking for...
00087         std::string tempSource;
00088 
00089         size_t Pos = -1;
00090         size_t Start = 0;
00091         size_t End = 0;
00092         tempSource = Params;
00093         do
00094         {
00095                 //Find the first '|'
00096                 Start = tempSource.find_first_of('|');
00097                 if (Start==-1)
00098                         break;
00099                 tempSource = tempSource.substr(Start+1,Params.size());
00100                 //Find the second '|'
00101                 End = tempSource.find_first_of('|');
00102                 _DestStrArr = tempSource.substr(Start,End);
00103                 tempSource = tempSource.substr(End+1,Params.size());
00104                 Pos++;
00105         }
00106         while(Pos != _ParamID);
00107         
00108         if (Start == -1)
00109                 _DestStrArr = "";
00110         //else
00111         //_DestStrArr = Params.substr(Start+1,End-1);
00112 }
00113 
00114 bool CL_TRACE_BASE_PARAMS::SplitParam(const std::string &_src, std::string &_name, std::string &_val)
00115 {
00116         if(_src== "")
00117                 return false;
00118         int Middle = _src.find_first_of(':');
00119 
00120         if(Middle)
00121         {
00122                 _name = _src.substr(0,Middle);
00123                 _val = _src.substr(Middle+1, _src.size());
00124                 return true;
00125         }
00126         return false;
00127 }
00128 
00129 
00130 /*
00131 void CL_TRACE_BASE_PARAMS::SetParams(int _ParamID, std::string & _DestStrArr)
00132 {
00133         //look into the params and parse it for different parameters
00134         //_ParamID is the offset of the param we are looking for...
00135         std::string tempSource;
00136 
00137         int Pos = -1;
00138         int Start = 0;
00139         int End = 0;
00140         int ParamsOffset;
00141         tempSource = Params;
00142         do
00143         {
00144                 //Find the first '|'
00145                 Start = tempSource.find_first_of('|');
00146                 if (Start==-1)
00147                         break;
00148                 tempSource = tempSource.substr(Start+1,Params.size());
00149                 //Find the second '|'
00150                 End = tempSource.find_first_of('|');
00151                 _DestStrArr = tempSource.substr(Start,End);
00152                 tempSource = tempSource.substr(End+1,Params.size());
00153                 ParamsOffset += Start+1;
00154                 Pos++;
00155         }
00156         while(Pos != _ParamID);
00157         
00158         if (Start == -1)
00159                 _DestStrArr = "";
00160         //else
00161         //_DestStrArr = Params.substr(Start+1,End-1);
00162 }
00163 */
00164 
00165 #if _SG_TLS_XML
00166 
00172 TiXmlElement* CL_TRACE_BASE_PARAMS::XMLLoad(TiXmlElement* _XML_ROOT)
00173 {
00174         SG_Assert(_XML_ROOT, "No XML root");
00175         
00176         XMLReadVal (_XML_ROOT, "params", Params);//keep it for compatibility
00177         int NewParamID=0;
00178         XMLReadVal(_XML_ROOT, "paramNbr", NewParamID);
00179 
00180         std::string ParamTag;
00181         std::string ParamVal;
00182         std::string ParamName;
00183         TiXmlElement* XMLParam = NULL;
00184         for (int i=0; i< NewParamID; i++)
00185         {
00186                 ParamTag = "param";
00187                 //ParamTag += IntToString(i+1, 1);
00188                 ParamTag += ToCharStr(i+1);
00189 
00190                 XMLParam  = XMLGetElement(_XML_ROOT, ParamTag);
00191                 if (XMLParam)
00192                 {
00193                         XMLReadVal(XMLParam, "name", ParamName);
00194                         XMLReadVal(XMLParam, "val", ParamVal);
00195                         if (ParamVal!="" && ParamName=="")
00196                         {//retro compatibility
00197                                 Params += ParamVal;
00198                         }
00199                         else if (ParamName!="" && ParamVal!="")
00200                         {
00201                                 AddChar(ParamName, ParamVal.c_str());
00202                         }
00203                 }
00204         }
00205 
00206         if (Params.size())
00207                 return _XML_ROOT;
00208         else
00209                 return NULL;
00210 }
00211 
00212 
00213 
00219 TiXmlElement* CL_TRACE_BASE_PARAMS::XMLSave(TiXmlElement* _XML_ROOT)
00220 {
00221         SG_Assert(_XML_ROOT, "No XML root");
00222         std::string NewParam="";
00223         std::string ParamTag;
00224         int NewParamID = 0;
00225         TiXmlElement * XML_NewParam = NULL;
00226         int Start = 0;
00227         
00228 #if 0
00229         do{
00230                 ParseParams(NewParamID, NewParam);
00231                 if (NewParam!="")
00232                 {
00233                         ParamTag = "param";
00234                         ParamTag += IntToString(NewParamID+1, 1);
00235                         //Start = NewParam.find_first_of(':');
00236                         if (Start!=-1)
00237                         {
00238                                 XML_NewParam = new TiXmlElement(ParamTag.c_str());
00239                                 XMLWriteVal(XML_NewParam, "val", NewParam);
00240                                 _XML_ROOT->InsertEndChild(*XML_NewParam);
00241                         }
00242                 }
00243                 NewParamID++;
00244         }
00245         while(NewParam!="");
00246 #else
00247         std::string Name, Val;
00248         
00249         do{
00250                 ParseParams(NewParamID, NewParam);
00251                 if (NewParam!="")
00252                 {
00253                         ParamTag = "param";
00254                         //ParamTag += IntToString(NewParamID+1, 1);
00255                         ParamTag += ToCharStr(NewParamID+1);
00256                         if(!SplitParam(NewParam, Name, Val))
00257                         {
00258                                 NewParamID++;
00259                                 continue;
00260                         }
00261                         //Start = NewParam.find_first_of(':');
00262                                 XML_NewParam = new TiXmlElement(ParamTag.c_str());
00263                                 XMLWriteVal(XML_NewParam, "name", Name);
00264                                 XMLWriteVal(XML_NewParam, "val", Val);                          
00265                                 _XML_ROOT->InsertEndChild(*XML_NewParam);
00266                 }
00267                 NewParamID++;
00268         }
00269         while(NewParam!="");
00270 #endif
00271 
00272         XMLWriteVal(_XML_ROOT, "paramNbr", NewParamID);
00273         
00274 //              XMLWriteVal(_XML_ROOT, "params", Params);
00275         return _XML_ROOT;
00276 }
00277 #endif//_SG_TLS_XML
00278 
00279 #if 1
00280 
00281 _SG_TRC_DLL_EXPORT bool XMLSaveBench(const char *Filename)
00282 {
00283 #if _SG_TLS_XML
00284         TTCL_APPLI_TRACER<CL_TimerVal> * CurApplitrc = TTCL_APPLI_TRACER<CL_TimerVal>::MainAppliTracer;
00285         if (!CurApplitrc)//create it
00286         {       
00287                 SG_WARNING_LOG("XMLLoadBench(" << Filename << "), no main TTCL_APPLI_TRACER\n");
00288                 return false;
00289         }
00290 
00291         if (CurApplitrc->XMLSaveToFile(Filename))
00292                 return true;
00293         else
00294                 SG_WARNING_LOG("File : " << Filename << " Could not be saved\n");
00295         return false;
00296 #else
00297         SG_Assert(0, "_SG_TLS_XML is not defined");
00298         return false;
00299 #endif
00300 }
00301 
00302 _SG_TRC_DLL_EXPORT bool XMLLoadBench(const char *Filename)
00303 {
00304 #if _SG_TLS_XML
00305         TTCL_APPLI_TRACER<CL_TimerVal> * CurApplitrc = TTCL_APPLI_TRACER<CL_TimerVal>::MainAppliTracer;
00306         if (!CurApplitrc)//create it
00307         {
00308                 SG_WARNING_LOG("XMLLoadBench(" << Filename << "), no main TTCL_APPLI_TRACER\n");
00309                 //return false;
00310                 CurApplitrc =  CreateMainTracer<SG_TRC_Default_Trc_Type>();//new TTCL_APPLI_TRACER<SG_TRC_Default_Trc_Type>();
00311         }
00312 
00313         if (CurApplitrc->XMLLoadFromFile(Filename))
00314                 return true;
00315         else
00316                 SG_WARNING_LOG("File : " << Filename << " Could not be loaded\n");
00317         return false;
00318 #else
00319         SG_Assert(0, "_SG_TLS_XML is not defined");
00320 #endif
00321 }
00322 
00323 int CreateFunction(const char * _fctName)
00324 {
00325         if(!_fctName)
00326         {
00327                 SG_WARNING_LOG("CreateFunction(): function name is null");
00328                 return -1;
00329         }
00330         //SG_NOTICE_LOG("Bench : Creating Function=>" << _fctName);
00331         //cout << endl;
00332         TTCL_APPLI_TRACER<CL_TimerVal> * CurApplitrc = TTCL_APPLI_TRACER<CL_TimerVal>::MainAppliTracer;
00333         if (!CurApplitrc)//create it
00334                 CurApplitrc = CreateMainTracer<CL_TimerVal>();// new TTCL_APPLI_TRACER<CL_TimerVal>();
00335 
00336         SG_Assert(CurApplitrc, "Could not create the main appli tracer!");
00337 
00338         if (!CurApplitrc->MapFunction)
00339                 return 0;
00340 
00341         //debug
00342         //cout << "FctMngr : Nbr of record : "<< TTCL_APPLI_TRACER<CL_TimerVal>::MainAppliTracer->FunctionsManager->GetCount() << endl;
00343 
00345         TTCL_APPLI_TRACER<CL_TimerVal>::TplFctHdle Fct = TTCL_APPLI_TRACER<CL_TimerVal>::MainAppliTracer->AddFunct(_fctName);
00346 
00347         //look into the map if we find it?
00348         std::map<int, TTCL_APPLI_TRACER<CL_TimerVal>::TplFctHdle>::iterator itFct;
00349         for ( itFct = CurApplitrc->MapFunction->begin( ) ; itFct!= CurApplitrc->MapFunction->end( ) ;itFct++ )
00350         {
00351                 if ((*itFct).second == Fct)
00352                         return (*itFct).first;
00353         }
00354 
00355         //not on the map, create a new entry
00356         CurApplitrc->MapFunction->insert(std::pair<int, TTCL_APPLI_TRACER<CL_TimerVal>::TplFctHdle>(++CurApplitrc->MapFunctionLastID, Fct));
00357         return CurApplitrc->MapFunctionLastID;
00358 
00359 }
00360 
00361 //main version of StartRecording
00362 int StartRecording(const int _FctNumber, CL_TRACE_BASE_PARAMS * _Params)
00363 {
00364         //SG_NOTICE_LOG("Bench : Start recording");
00365         //cout << endl;
00366         TTCL_APPLI_TRACER<CL_TimerVal> * CurApplitrc = TTCL_APPLI_TRACER<CL_TimerVal>::MainAppliTracer;
00367         if (!CurApplitrc) return 0;
00368 
00369         TTCL_APPLI_TRACER<CL_TimerVal>::TplFctHdle CurFct = NULL;
00370         //find the correct function
00371         std::map<int, TTCL_APPLI_TRACER<CL_TimerVal>::TplFctHdle>::iterator iFct = CurApplitrc->MapFunction->find(_FctNumber);
00372                 if (iFct != CurApplitrc->MapFunction->end())
00373             CurFct = (*iFct).second;
00374 
00375         //create a CL_TEMP_TRACER
00376                 CL_TEMP_TRACER<CL_TimerVal> * NewTracer = new CL_TEMP_TRACER<CL_TimerVal>(CurFct, _Params);
00377 
00378         //insert in into the map
00379                 CurApplitrc->MapTempTracer->insert(std::pair<int, CL_TEMP_TRACER<CL_TimerVal> *>(++CurApplitrc->MapTracerLastID, NewTracer));
00380 
00381         //return its number
00382                 return CurApplitrc->MapTracerLastID;
00383 
00384 }
00385 
00386 //C version of startRecording
00387 int StartRecordingInt(const int _FctNumber, const char * ParamsName, const int * _intArray, const int _nbr)
00388 {
00389         return StartRecording(_FctNumber, new CL_TRACE_BASE_PARAMS(ParamsName, (int*)_intArray, _nbr));
00390 }
00391 
00392 int StartRecordingChar(const int _FctNumber, const char * ParamsName, const char *_Value)
00393 {
00394         return StartRecording(_FctNumber, new CL_TRACE_BASE_PARAMS(ParamsName, std::string(_Value)));
00395 }
00396 
00397 //todo add function for adding values to params....
00398 void StopRecording(const int _TracerNumber)
00399 {
00400         CL_TimerVal *CurTimer=NULL;
00401         CL_TimerVal TempTimer;
00402         TempTimer.Run();
00403 
00404         //SG_NOTICE_LOG("Bench : Stop recording");
00405 //      cout << endl;
00406         //TempTimer.Print();
00407         TTCL_APPLI_TRACER<CL_TimerVal> * CurApplitrc = TTCL_APPLI_TRACER<CL_TimerVal>::MainAppliTracer;
00408         if (!CurApplitrc) return;
00409 
00410         //CL_TEMP_TRACER * CurFct = NULL;
00411         //find the correct tracer
00412                 std::map<int, CL_TEMP_TRACER<CL_TimerVal>*>::iterator iTrc = CurApplitrc->MapTempTracer->find(_TracerNumber);
00413                 if (iTrc != CurApplitrc->MapTempTracer->end())
00414                 {
00415                         //(*iTrc).second->GetTracer()->GetResult()->Print();
00416                         //cout << endl;
00417                         CurTimer = (*iTrc).second->GetTracer()->GetResult();
00418 
00419                         //do some tricks to get the real time and not the time of the function StopRecording()...
00420                         if (CurTimer)
00421                         {
00422                                 //CurTimer->Print();
00423                                 CurTimer->Run(&TempTimer);
00424                                 TempTimer = *CurTimer;
00425                                 //cout << endl;
00426                                 //TempTimer.Print();
00427                         }
00428                         //================
00429                         delete (*iTrc).second;
00430                         CurApplitrc->MapTempTracer-> erase(_TracerNumber);
00431                 }
00432 
00433         
00434         if (CurTimer)
00435         {//finish the tricks
00436                 *CurTimer = TempTimer;
00437                 //cout << endl;
00438                 //CurTimer->Print();
00439         }
00440 
00441         //XMLSaveBench("ARTtest.xml");
00442 }
00443 
00444 bool AddParamInt(const int _TracerNumber, const  char * _Name, const int _Value)
00445 {
00446         TTCL_APPLI_TRACER<CL_TimerVal> * CurApplitrc = TTCL_APPLI_TRACER<CL_TimerVal>::MainAppliTracer;
00447         if (!CurApplitrc) return false;
00448 
00449         //find the correct tracer
00450         std::map<int, CL_TEMP_TRACER<CL_TimerVal>*>::iterator iTrc = CurApplitrc->MapTempTracer->find(_TracerNumber);
00451         if (iTrc != CurApplitrc->MapTempTracer->end())
00452         {
00453                 CL_TRACE_BASE_PARAMS * TrcParam = (*iTrc).second->GetTracer()->GetParams();
00454                 if (TrcParam)
00455                 {
00456                         TrcParam->AddInt(_Name, &_Value);
00457                 }
00458                 else
00459                 {std::cout << "AddParam()=> tracer has no params"<< std::endl;}
00460         }
00461         return true;
00462 
00463 }
00464 bool AddParamChar(const int _TracerNumber, const char * _Name, const char * _Value)
00465 {
00466         TTCL_APPLI_TRACER<CL_TimerVal> * CurApplitrc = TTCL_APPLI_TRACER<CL_TimerVal>::MainAppliTracer;
00467         if (!CurApplitrc) return false;
00468 
00469         //find the correct tracer
00470         std::map<int, CL_TEMP_TRACER<CL_TimerVal>*>::iterator iTrc = CurApplitrc->MapTempTracer->find(_TracerNumber);
00471         if (iTrc != CurApplitrc->MapTempTracer->end())
00472         {
00473                 CL_TRACE_BASE_PARAMS * TrcParam = (*iTrc).second->GetTracer()->GetParams();
00474                 if (TrcParam)
00475                 {
00476                         TrcParam->AddChar(_Name, _Value);
00477                 }
00478                 else
00479                 {std::cout << "AddParam()=> tracer has no params"<< std::endl;}
00480         }
00481         return true;
00482 }
00483 
00484 
00485 #endif
00486 
00487 };//namespace SG_TRC

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