00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef _SG_TRC_FUNCTION_H
00022 #define _SG_TRC_FUNCTION_H
00023
00024 #include "SugoiTracer\config.h"
00025
00026 #ifdef __cplusplus
00027 #include "SugoiTracer\timer.h"
00028
00029 #include "SugoiTracer\class.h"
00030 #include "SugoiTracer\appli.h"
00031 #include <Time.h>
00032 #include <sys/timeb.h>
00033 #include <SugoiTools\cl_base_obj.h>
00034 #include <SugoiTools\cl_xml_mngr.h>
00035
00036
00037 #if SG_TLS_MEMORY_MANAGER
00038 #include "SugoiTools\debug_new.h"
00039 #endif
00040
00041 namespace SG_TRC{
00042
00043
00044
00045
00046 template <typename TracerType = SG_TRC_Default_Trc_Type>
00047 class CL_FUNCT_TRC
00048 :public CL_XML_OBJ,
00049 public CL_BASE_OBJ<std::string>
00050 {
00051 public:
00052
00053
00054
00055
00056
00057
00058
00059
00060 typedef CL_FUNCT_TRC<TracerType> *TplFctHdle;
00061 typedef CL_CLASS_TRACER<TracerType> *TplClassHdle;
00062 typedef CL_FUNCT_TRC<TracerType> *TplFctObj;
00063 typedef CL_CLASS_TRACER<TracerType> *TplClassObj;
00064
00065
00066 private:
00067 bool TracingEnable;
00068 bool Modified;
00069 TracerType *TotalTime;
00070 TracerType *MinTime;
00071 TracerType *MaxTime;
00072 TplClassHdle ParentClass;
00073 int MaxRecords;
00074
00075
00076 public:
00077 int TotalRecords;
00078 bool UseMinMaxTot;
00079 CL_XML_MNGR<CL_TRACER_RECORD<TracerType> , std::string> *TracerRecorded;
00080 std::vector<CL_TRACER<TracerType>*> TracerToProccess;
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093 public:
00095 CL_FUNCT_TRC(std::string _Name)
00096 :CL_XML_OBJ("Function"),
00097 CL_BASE_OBJ<std::string>(_Name),
00098 ParentClass(NULL),
00099 Modified(true),
00100 TracingEnable(true),
00101 MinTime(new TracerType()),
00102 MaxTime(new TracerType()),
00103 TotalTime(new TracerType()),
00104 UseMinMaxTot(true),
00105 TracerRecorded(new CL_XML_MNGR<CL_TRACER_RECORD<TracerType>, std::string>(NULL, "RecordList","Records", XML_TAG_VAL_ID)),
00106 MaxRecords(-1)
00107 {
00108 Clear();
00109 }
00110
00112 ~CL_FUNCT_TRC()
00113 {
00114
00115 delete(TracerRecorded);
00116 delete MinTime;
00117 delete MaxTime;
00118 delete TotalTime;
00119 }
00120
00122 void Clear()
00123 {
00124 MaxTime->Clear();
00125 TotalTime->Clear();
00126 TotalRecords =0;
00127 MinTime->SetToMax();
00128 TracerRecorded->DeleteAllLocal();
00129
00130
00131 TracerToProccess.clear();
00132
00133 }
00134
00136 void SetParrentClass(TplClassHdle _ParentClass)
00137 {ParentClass =_ParentClass;}
00138
00139 void SetMaxRecords(int _size)
00140 {MaxRecords = _size;}
00141
00143 void AddTracerToProccess(CL_TRACER<TracerType> *_NewTracer)
00144 {
00145 SG_Assert(_NewTracer, "CL_FUNCT_TRC<TracerType>::AddTracerToProccess() : Empty Tracer.");
00146 TracerToProccess.push_back(_NewTracer);
00147 Modified = true;
00148 }
00149
00151
00152 int Process()
00153 {
00154 int i=0;
00155 std::vector <CL_TRACER<TracerType>* >::iterator iTracer = TracerToProccess.begin();
00156
00157
00158
00159
00160
00161
00162
00163
00164 while(iTracer != TracerToProccess.end())
00165
00166 {
00167 if (!(*iTracer)->GetReady())
00168 break;
00169
00170 AddTracerProccessed(*iTracer);
00171
00172
00173
00174 TracerToProccess.erase(iTracer);
00175 i++;
00176 iTracer = TracerToProccess.begin();
00177 }
00178 TracerToProccess.clear();
00179
00180 return i;
00181 }
00182
00183
00184 const TracerType & GetMin(){return *MinTime;}
00185 const TracerType & GetMax(){return *MaxTime;}
00186 const TracerType & GetTotal(){return *TotalTime;}
00187 bool IsModified() {return Modified;}
00188
00189 private:
00191 CL_TRACER_RECORD<TracerType> *
00192 AddTracerProccessed(CL_TRACER<TracerType>* _NewTracer)
00193 {
00194 SG_Assert(_NewTracer, "CL_FUNCT_TRC<TracerType>::AddTracerProccessed() : Empty Tracer.");
00195
00196
00197 std::string ParamUniqName;
00198 if (_NewTracer->GetParams())
00199 ParamUniqName = _NewTracer->GetParams()->Params;
00200 else
00201 ParamUniqName = "empty";
00202
00203
00204
00205 CL_XML_MNGR<CL_TRACER_RECORD<TracerType> , std::string>::TplHdle CurTracer = TracerRecorded->Get(ParamUniqName);
00206
00207 if (!CurTracer)
00208 {
00209 CurTracer = TracerRecorded->Add(ParamUniqName);
00210 CurTracer->SetParentFunction(this);
00211 CurTracer->SetParams(_NewTracer->GetParams());
00212 if (MaxRecords!=-1)
00213 CurTracer->SetMaxRecords(MaxRecords);
00214 }
00215
00216
00217
00218
00219 CurTracer->AddTracer(_NewTracer);
00220 return CurTracer;
00221 }
00222
00223 #if _SG_TLS_XML
00224 public:
00225 _SG_TLS_INLINE
00226 TiXmlElement*
00227 XMLLoad(TiXmlElement* _XML_ROOT)
00228 {
00229 SG_Assert(_XML_ROOT, "No XML root");
00230 Process();
00231
00232 if (UseMinMaxTot)
00233 {
00234
00235 TotalTime->XMLLoad(_XML_ROOT, "TotalTime");
00236 MinTime->XMLLoad(_XML_ROOT, "MinTime");
00237 MaxTime->XMLLoad(_XML_ROOT, "MaxTime");
00238 }
00239
00240 XMLReadVal(_XML_ROOT, "TotalRecords", TotalRecords);
00241 XMLReadVal(_XML_ROOT, "MaxRecords", MaxRecords);
00242
00243
00244 TracerRecorded->XMLLoad(_XML_ROOT);
00245
00246
00247 CL_XML_MNGR<CL_TRACER_RECORD<TracerType> , std::string>::iterator itRecord;
00248 for(itRecord = TracerRecorded->GetFirstIter(); itRecord != TracerRecorded->GetLastIter(); itRecord++)
00249 {
00250 (*itRecord).second->SetParentFunction(this);
00251 if (MaxRecords!=-1)
00252 (*itRecord).second->SetMaxRecords(MaxRecords);
00253
00254 }
00255
00256 return _XML_ROOT;
00257 }
00258
00259 _SG_TLS_INLINE
00260 TiXmlElement*
00261 XMLSave(TiXmlElement* _XML_ROOT)
00262 {
00263 SG_Assert(_XML_ROOT, "No XML root");
00264
00265 Process();
00266
00267 if (UseMinMaxTot)
00268 {
00269
00270 TotalTime->XMLSave(_XML_ROOT, "TotalTime");
00271 MinTime->XMLSave(_XML_ROOT, "MinTime");
00272 MaxTime->XMLSave(_XML_ROOT, "MaxTime");
00273 }
00274
00275 XMLWriteVal(_XML_ROOT, "TotalRecords", TotalRecords);
00276 if (MaxRecords!=-1)
00277 XMLWriteVal(_XML_ROOT, "MaxRecords", MaxRecords);
00278
00279
00280 _SG_DEBUG_XML(cout << "Function : " << GetIDStr() << " => record nbr : " << TracerRecorded->GetCount() << endl);
00281 _SG_DEBUG_XML(cout << "Record to process : " << TracerToProccess.size() << endl);
00282
00283 TracerRecorded->XMLSave(_XML_ROOT);
00284
00285
00286
00287 if (TracerRecorded->GetCount() == 1)
00288 {
00289 if (TracerRecorded->GetFirstIter()->second->GetParams() == NULL)
00290 return _XML_ROOT;
00291 else if (TracerRecorded->GetFirstIter()->second->GetParams()->Params == "EMPTY")
00292 return _XML_ROOT;
00293 }
00294
00295 return _XML_ROOT;
00296 }
00297 #endif
00298
00299 #if _SG_TRC_SVG
00300 bool SaveToSVGFile()
00301 {
00302
00303 #ifdef _CVG_SVG_CREATE_FILES
00304 if (CurFct)
00305 {
00306 string Svg;
00307 string Filename;
00308 char TempBuff[256];
00309 CurFct->Sort();
00310 vector <CL_FCT_SUB_ARGS*> Records;
00311 CL_FCT_SUB_ARGS * ActiveRec=NULL;
00312 CL_TimerVal * CurTime =NULL;
00313 CL_TimerVal Average;
00314 long int maxvalue;
00315 int NParamsEqual=1;
00316 int NbParamsFirst=true;
00317 int pst[12];
00318 struct Pts {
00319 unsigned int ImgSize[2];
00320 unsigned long int time;
00321 }ChartPts[128];
00322 string ActiveParams="";
00323 string Params="";
00324
00325
00326 for (unsigned int i=0; i<CurFct->Records.size(); i+=NParamsEqual)
00327 {
00328
00329
00330
00331 if (NParamsEqual==1)
00332 {
00333 ActiveRec = CurFct->Records[i];
00334 for (unsigned int j=i+1; j<CurFct->Records.size(); j++)
00335 if (ActiveRec->Params == CurFct->Records[j]->Params)
00336 NParamsEqual++;
00337 NbParamsFirst=true;
00338 }
00339
00340 for (unsigned int t=i; t<i+NParamsEqual; t++)
00341 {
00342 ActiveRec = CurFct->Records[t];
00343
00344 Average = ActiveRec->TotalTime;
00345 Average/= ActiveRec->NbrOfTime;
00346 ChartPts[t-i].time = Average.GetValue();
00347 }
00348
00349
00350 Filename = _CVG_SVG_EXPORT_PATH;
00351 Filename += "\\"+CurFct->FCT_NAME+"_"+ActiveRec->Params+".svg";
00352
00353 OpenFile((char *)Filename.data());
00354 if (!File) return -1;
00355
00356
00357 Svg = "<svg width='550' height='425'>\n";
00358
00359 Svg+="<line x1='49' x2='480' y1='350' y2='350' style='stroke-width:3 ; fill:none; stroke:black'/>\n";
00360 Svg+="<line x1='470' x2='480' y1='347' y2='350' style='stroke-width:3 ; fill:none; stroke:black'/>\n";
00361 Svg+="<line x1='470' x2='480' y1='353' y2='350' style='stroke-width:3 ; fill:none; stroke:black'/>\n";
00362 fwrite( Svg.data(), strlen(Svg.data())*sizeof(unsigned char),1,File);
00363
00364 for(unsigned int j=0,ps=400;j<6;j++)
00365 {
00366 sprintf(TempBuff, "<line x1='%d' x2='%d' y1='347' y2='350' style='stroke-width:2 ; fill:none; stroke:black'/>\n",50+ps,50+ps);
00367 fwrite( TempBuff, strlen(TempBuff)*sizeof(unsigned char),1,File);
00368 ps/=4;
00369 }
00370
00371 Svg="<line x1='50' x2='47' y1='25' y2='35' style='stroke-width:3 ; fill:none; stroke:black'/>\n";
00372 Svg+="<line x1='50' x2='53' y1='25' y2='35' style='stroke-width:3 ; fill:none; stroke:black'/>\n";
00373 Svg+="<line x1='50' x2='50' y1='351' y2='25' style='stroke-width:3 ; fill:none; stroke:black'/>\n";
00374 fwrite( Svg.data(), strlen(Svg.data())*sizeof(unsigned char),1,File);
00375
00376 for(unsigned int j=0;j<5;j++)
00377 {
00378 sprintf(TempBuff, "<line x1='50' x2='53' y1='%d' y2='%d' style='stroke-width:2 ; fill:none; stroke:black'/>\n",50+60*j,50+60*j);
00379 fwrite( TempBuff, strlen(TempBuff)*sizeof(unsigned char),1,File);
00380 }
00381
00382 Svg="<g style='font-size:16;fill:black'>\n";
00383 Svg+="<text x='20' y='25'>Time(ms)</text>\n";
00384 Svg+="<text x='460' y='365'>Size(pixels)</text>\n";
00385 Svg+="</g>\n";
00386 fwrite( Svg.data(), strlen(Svg.data())*sizeof(unsigned char),1,File);
00387
00388 Svg="<g style='font-size:13'>\n";
00389 Svg+="<text x='20' y='355'>0</text>\n";
00390 Svg+="<g transform='rotate(90)'>\n";
00391 fwrite( Svg.data(), strlen(Svg.data())*sizeof(unsigned char),1,File);
00392
00393 for(unsigned int j=0,pp=2048,ps=400;j<4;j++)
00394 {
00395 sprintf(TempBuff, "<text x='355' y='%d'>%d*%d</text>\n",(-1)*(ps+46),pp,pp);
00396 fwrite( TempBuff, strlen(TempBuff)*sizeof(unsigned char),1,File);
00397 pp/=2;
00398 ps/=4;
00399 }
00400 Svg="</g>\n";
00401 Svg+="</g>\n";
00402 fwrite( Svg.data(), strlen(Svg.data())*sizeof(unsigned char),1,File);
00403
00404 maxvalue=0;
00405 for (unsigned int j=0;j<NParamsEqual;j++)
00406 if (ChartPts[j].time>maxvalue)
00407 maxvalue=ChartPts[j].time;
00408
00409
00410 if (maxvalue<=2500) maxvalue=2500;
00411 else if (maxvalue<=5000) maxvalue=5000;
00412 else if (maxvalue<=10000) maxvalue=10000;
00413 else if (maxvalue<=20000) maxvalue=20000;
00414 else if (maxvalue<=30000) maxvalue=30000;
00415 else if (maxvalue<=40000) maxvalue=40000;
00416 else if (maxvalue<=50000) maxvalue=50000;
00417 else if (maxvalue<=60000) maxvalue=60000;
00418 else if (maxvalue<=70000) maxvalue=70000;
00419 else if (maxvalue<=80000) maxvalue=80000;
00420 else if (maxvalue<=90000) maxvalue=90000;
00421 else if (maxvalue<=100000) maxvalue=100000;
00422 else if (maxvalue<=110000) maxvalue=110000;
00423 else if (maxvalue<=120000) maxvalue=120000;
00424 else if (maxvalue<=130000) maxvalue=130000;
00425 else if (maxvalue<=140000) maxvalue=140000;
00426 else if (maxvalue<=150000) maxvalue=150000;
00427 else if (maxvalue<=160000) maxvalue=160000;
00428 else if (maxvalue<=170000) maxvalue=170000;
00429 else if (maxvalue<=180000) maxvalue=180000;
00430 else if (maxvalue<=190000) maxvalue=190000;
00431 else if (maxvalue<=200000) maxvalue=200000;
00432
00433 for (unsigned int j=0;j<NParamsEqual;j++)
00434 pst[j]=350-300*ChartPts[j].time/maxvalue;
00435
00436 sprintf(TempBuff, "<text x='225' y='25' style='font-family:Verdana; font-size:20; fill:black'>%s</text>\n",CurFct->FCT_NAME.data());
00437 fwrite( TempBuff, strlen(TempBuff)*sizeof(unsigned char),1,File);
00438 sprintf(TempBuff, "<text x='225' y='45' style='font-family:Verdana; font-size:13; fill:black'>%s</text>\n",ActiveRec->Params.data());
00439 fwrite( TempBuff, strlen(TempBuff)*sizeof(unsigned char),1,File);
00440
00441 Svg="<g style='stroke:black; fill:black' >\n";
00442 fwrite( Svg.data(), strlen(Svg.data())*sizeof(unsigned char),1,File);
00443
00444 for (unsigned int j=0,ps=400;j<6;j++)
00445 {
00446 sprintf(TempBuff, "<circle cx='%d' cy='%d' r='3' />\n",ps+50,pst[2*j]);
00447 fwrite( TempBuff, strlen(TempBuff)*sizeof(unsigned char),1,File);
00448 sprintf(TempBuff, "<circle cx='%d' cy='%d' r='3' />\n",ps+50,pst[2*j+1]);
00449 fwrite( TempBuff, strlen(TempBuff)*sizeof(unsigned char),1,File);
00450 ps/=4;
00451 }
00452 Svg="</g>\n";
00453 fwrite( Svg.data(), strlen(Svg.data())*sizeof(unsigned char),1,File);
00454
00455 Svg="<g style='font-size:10; fill:black' >\n";
00456 fwrite( Svg.data(), strlen(Svg.data())*sizeof(unsigned char),1,File);
00457 for (unsigned int j=0,ps=400;j<3;j++)
00458 {
00459 sprintf(TempBuff, "<text x='%d' y='%d'>(%dus)</text>\n",ps+55,pst[2*j]+10,ChartPts[2*j].time);
00460 fwrite( TempBuff, strlen(TempBuff)*sizeof(unsigned char),1,File);
00461 sprintf(TempBuff, "<text x='%d' y='%d'>(%dus)</text>\n",ps+55,pst[2*j+1]+10,ChartPts[2*j+1].time);
00462 fwrite( TempBuff, strlen(TempBuff)*sizeof(unsigned char),1,File);
00463 ps/=4;
00464 }
00465 Svg="</g>\n";
00466 fwrite( Svg.data(), strlen(Svg.data())*sizeof(unsigned char),1,File);
00467
00468 sprintf(TempBuff, "<polyline points='51,%d,52,%d,56,%d,75,%d,150,%d,450,%d' style='stroke-width:3 ; fill:none; stroke:orange'/>\n",pst[10],pst[8],pst[6],pst[4],pst[2],pst[0]);
00469 fwrite( TempBuff, strlen(TempBuff)*sizeof(unsigned char),1,File);
00470 sprintf(TempBuff, "<polyline points='51,%d,52,%d,56,%d,75,%d,150,%d,450,%d' style='stroke-width:3 ; fill:none; stroke:blue'/>\n",pst[11],pst[9],pst[7],pst[5],pst[3],pst[1]);
00471 fwrite( TempBuff, strlen(TempBuff)*sizeof(unsigned char),1,File);
00472
00473 sprintf(TempBuff, "<text x='460' y='%d' style='font-family:Verdana; font-size:15; fill:orange'>OpenCV</text>\n",pst[0]);
00474 fwrite( TempBuff, strlen(TempBuff)*sizeof(unsigned char),1,File);
00475 sprintf(TempBuff, "<text x='460' y='%d' style='font-family:Verdana; font-size:15; fill:blue'>GPUCV</text>\n",pst[1]);
00476 fwrite( TempBuff, strlen(TempBuff)*sizeof(unsigned char),1,File);
00477
00478 Svg="<g style='font-size:13'>\n";
00479 fwrite( Svg.data(), strlen(Svg.data())*sizeof(unsigned char),1,File);
00480 if (maxvalue>3000)
00481 {
00482 for (unsigned int j=1,yy=295; j<6;j++)
00483 {
00484 sprintf(TempBuff, "<text x='20' y='%d'>%d</text>\n",yy,j*maxvalue/5000);
00485 fwrite( TempBuff, strlen(TempBuff)*sizeof(unsigned char),1,File);
00486 yy-=60;
00487 }
00488 }
00489 else
00490 {
00491 for (unsigned int j=1,yy=295; j<6;j++)
00492 {
00493 float m=maxvalue;
00494 sprintf(TempBuff, "<text x='20' y='%d'>%.1f</text>\n",yy,j*m/5000);
00495 fwrite( TempBuff, strlen(TempBuff)*sizeof(unsigned char),1,File);
00496 yy-=60;
00497 }
00498 }
00499
00500 Svg="</g>\n";
00501 Svg+="<!-- Your SVG elements -->\n";
00502 Svg+="</svg>\n";
00503 fwrite( Svg.data(), strlen(Svg.data())*sizeof(unsigned char),1,File);
00504
00505 CloseFile();
00506 }
00507 }
00508 return 1;
00509 #else
00510 printf("\n_CVG_SVG_CREATE_FILES flag is not defined in definitions.h, SVG files not created...");
00511 return 0;
00512 #endif
00513
00514 }
00515 #endif//_SG_TRC_SVG
00516 };
00517
00518
00519 };
00520 #endif //__cplusplus
00521
00522 #if SG_TLS_MEMORY_MANAGER
00523 #include "SugoiTools\debug_new_off.h"
00524 #endif
00525
00526 #endif//_SG_TRC_FUNCTION_H