D:/WorkDir/SugoiProjects/SugoiTracer/src/timer.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 
00007 This program is free software; you can redistribute it and/or
00008 modify it under the terms of the GNU General Public License
00009 as published by the Free Software Foundation; either version 2
00010 of the License, or (at your option) any later version.
00011 
00012 This program is distributed in the hope that it will be useful,
00013 but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 GNU General Public License for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with this program; if not, write to the Free Software
00019 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00020 LICENSE_END
00021 */
00022 
00023 #include "SugoiTracer\timer.h"
00024 
00025 
00026 namespace SG_TRC{
00027 CL_TimerVal::CL_TimerVal()
00028 {
00029         Clear(); 
00030 }
00031 
00032 CL_TimerVal::CL_TimerVal(const CL_TimerVal &Time2)
00033 {
00034         Clear();
00035         (*this) = Time2; 
00036 }
00037 
00038 CL_TimerVal::CL_TimerVal(const CL_Virtual_Record &Time2)
00039 {
00040         Clear();
00041         //(*this) = *(CL_TimerVal*)Time2; 
00042 }
00043 
00044 CL_TimerVal::CL_TimerVal(const long T)
00045 {
00046         Clear();
00047         (*this) = T; 
00048 }
00049 
00050 CL_TimerVal::CL_TimerVal(const timeval &_Timeval)
00051 { (*this) = _Timeval;}
00052 
00053 void CL_TimerVal::Clear()
00054 {
00055          SetToMin();
00056          succes = true;
00057          returnValue = 0;        
00058 }
00059 void CL_TimerVal:: SetToMax()
00060 {
00061         tv_sec  = 999999;
00062         tv_usec = 999999;
00063 }
00064 void CL_TimerVal:: SetToMin()
00065 {
00066         tv_sec  = 0;
00067         tv_usec = 0;
00068 }
00069 #if 1
00070 CL_TimerVal& CL_TimerVal:: operator =( const CL_TimerVal& Time2)
00071 {
00072         this->tv_sec = Time2.tv_sec;             
00073         this->tv_usec = Time2.tv_usec;
00074         return (*this);
00075 }
00076 /*
00077 CL_TimerVal&   CL_TimerVal::operator  =( const CL_Virtual_Record *Time2)
00078 {
00079         (*this)  = *(CL_TimerVal*)Time2;
00080         return (*this);
00081 }
00082 */
00083 CL_TimerVal&  CL_TimerVal:: operator =( const long T)
00084 {
00085         this->tv_sec = T/1000000;
00086         this->tv_usec = T - this->tv_sec * 1000000;
00087         return (*this);
00088 }
00089 
00090 CL_TimerVal&  CL_TimerVal:: operator =( const ST_Timeval &_Timeval)
00091 {
00092         tv_sec  = _Timeval.tv_sec;
00093         tv_usec = _Timeval.tv_usec;
00094         return (*this);
00095 }
00096 
00097 bool  CL_TimerVal::operator ==(const CL_TimerVal&  Time2)
00098 {
00099         if ((tv_sec == Time2.tv_sec) && (tv_usec == Time2.tv_usec))
00100            return true;
00101         return false;
00102 }
00103 /*
00104 bool  CL_TimerVal::operator ==(const CL_TimerVal&  T)
00105 {
00106         if ((tv_sec == Time2.tv_sec) && (tv_usec == Time2.tv_usec))
00107            return true;
00108         return false;
00109 }
00110 */
00111 bool  CL_TimerVal::operator !=(const CL_TimerVal&  Time2)
00112 {
00113         if ((tv_sec == Time2.tv_sec) && (tv_usec == Time2.tv_usec))
00114            return false;
00115         return true;
00116 }
00117 /*
00118 bool  CL_TimerVal::operator !=(const CL_TimerVal&  T)
00119 {
00120         if ((tv_sec == Time2.tv_sec) && (tv_usec == Time2.tv_usec))
00121            return true;
00122         return false;
00123 }
00124 */
00125 CL_TimerVal&   CL_TimerVal::operator +=( const CL_TimerVal  &Time2)
00126 {
00127         //micro seconde test
00128         this->tv_usec += Time2.tv_usec;
00129         while (this->tv_usec > 1000000)
00130         {
00131                 this->tv_sec ++;
00132                 this->tv_usec -= 1000000;
00133         }
00134         //seconde test
00135         this->tv_sec += Time2.tv_sec;
00136 
00137         return (*this);
00138 }
00139 
00140 CL_TimerVal&   CL_TimerVal::operator -=( const CL_TimerVal &Time2)
00141 {
00142         //micro seconde test
00143         this->tv_usec -= Time2.tv_usec;
00144         while (this->tv_usec < 0)
00145         {
00146                 this->tv_sec --;
00147                 this->tv_usec += 1000000;
00148         }
00149         //seconde test
00150         this->tv_sec -= Time2.tv_sec;
00151 
00152         return (*this);
00153 }
00154 
00155 CL_TimerVal&   CL_TimerVal::operator /=(const long T)
00156 {
00157         SG_Assert(T, "Divide by 0 is not allowed!");
00158         long TempTime = GetValue();
00159         return (*this) = TempTime / T;
00160 }
00161 
00162 CL_TimerVal&   CL_TimerVal::operator *=(const long T)
00163 {
00164         long TempTime = GetValue();
00165         return (*this) = TempTime * T;
00166 }
00167 
00168 
00169 CL_TimerVal CL_TimerVal::operator+(const CL_TimerVal &T2)const
00170 {
00171         CL_TimerVal TempTime((*this));
00172         return TempTime += T2;
00173 }
00174 
00175 CL_TimerVal CL_TimerVal::operator-(const CL_TimerVal &T2)const
00176 {
00177         CL_TimerVal TempTime((*this));
00178         return TempTime -= T2;
00179 }
00180 
00181 CL_TimerVal   CL_TimerVal::operator *(const long T)const
00182 {
00183         CL_TimerVal TempTime((*this));
00184         return TempTime *= T;
00185 }
00186         
00187 CL_TimerVal   CL_TimerVal::operator /(const long T)const
00188 {
00189         SG_Assert(T, "Divide by 0 is not allowed!");
00190         CL_TimerVal TempTime((*this));
00191         return TempTime /= T;
00192 }
00193 
00194 bool   CL_TimerVal::operator <(const CL_TimerVal &Time2)
00195 {
00196         return !((*this) > Time2);
00197 }
00198 
00199 bool   CL_TimerVal::operator >(const CL_TimerVal &Time2)
00200 {
00201         if (this->tv_sec > Time2.tv_sec )
00202                 return true;
00203         else if (this->tv_sec < Time2.tv_sec)
00204                 return false;
00205         else if(this->tv_usec > Time2.tv_usec)
00206                 return true;
00207         else if(this->tv_usec < Time2.tv_usec)
00208                 return false;
00209         return false;
00210 }
00211 
00212 long   CL_TimerVal::GetValue(void)
00213 {  
00214         return (this->tv_sec * 1000000+this->tv_usec);
00215 }
00216 
00217 std::string CL_TimerVal::GetStr(bool _Apprixmimate/*=true*/)const
00218 {
00219         std::string TempString="";
00220         char TempBuff[16];
00221 
00222         if (tv_sec > 0)
00223         {
00224                 TempString = itoa(tv_sec, TempBuff, 10);
00225                 TempString+= " s ";
00226         }
00227 
00228         long uSec = tv_usec;
00229         if (tv_usec > 1000)
00230         {
00231                 TempString += itoa(tv_usec/1000, TempBuff, 10);
00232                 uSec -= (tv_usec/1000) *1000;
00233                 TempString+=" ms ";
00234         }
00235 
00236         if (uSec > 0)
00237         {
00238                 TempString += itoa(uSec, TempBuff, 10);
00239                 TempString+=" us";
00240         }
00241         return TempString;
00242 }
00243 
00244 void CL_TimerVal::Print()
00245 {
00246      printf("%d'%6.d", tv_sec,tv_usec);
00247 }
00248 
00249 //the first time the run function is executed, we take the time, the second time(and more, we compare time)
00250 void CL_TimerVal::Run(CL_TimerVal * StopTime/*=NULL*/)
00251 {
00252         if (StopTime)
00253         {
00254                 (*this)= (*StopTime)- (*this);
00255         }
00256         else
00257         {
00258                 timeval TempTime;
00259                 Ygettimeofday(&TempTime);
00260                 CL_TimerVal Temp(TempTime);
00261                 (*this)= Temp- (*this);
00262         }
00263 }
00264 
00265 #if _SG_TLS_XML
00266 
00272 TiXmlElement* CL_TimerVal::XMLLoad(TiXmlElement* _XML_ROOT, std::string _Name/* =""*/)
00273 {
00274         SG_Assert(_XML_ROOT, "CL_TimerVal::XMLLoad() => XML root element empty");
00275         SG_Assert(_Name!="", "XML tag name is empty.");
00276 
00277         Clear();//Init all positions values to default
00278 
00279         if (_Name!= "")
00280                 SetMainTag(_Name);
00281 
00282         //TiXmlElement * XMLTimer = CL_XML_OBJ::XMLLoad(_XML_ROOT);
00283         TiXmlElement* XMLTimer = XMLGetElement(_XML_ROOT, _Name);
00284         if (!XMLTimer)
00285                 return NULL;
00286         
00287         XMLReadVal (XMLTimer, "sec", this->tv_sec);
00288         XMLReadVal (XMLTimer, "usec", this->tv_usec);
00289 
00290         return XMLTimer;
00291 }
00292 
00299 TiXmlElement* CL_TimerVal::XMLSave(TiXmlElement* _XML_ROOT, std::string _Name/*=""*/)
00300 {
00301         SG_Assert(_XML_ROOT, "CL_TimerVal::XMLSave() => XML root element empty");
00302         SG_Assert(_Name!="", "XML tag name is empty.");
00303         
00304         if (this->tv_sec == 0 && this->tv_usec == 0)
00305                 return _XML_ROOT;
00306         else if (this->tv_sec == 999999 && this->tv_usec == 999999)
00307                 return _XML_ROOT;
00308 
00309         if (_Name!= "")
00310                 SetMainTag(_Name);
00311 
00312 
00313         TiXmlElement * XMLTimer = CL_XML_OBJ::XMLLoad(_XML_ROOT);
00314         if (!XMLTimer) 
00315         //CL_XML_OBJ::XMLSave(_XML_ROOT);
00316                 XMLTimer = new TiXmlElement( _Name.data() );
00317         
00318         if (this->tv_sec != 0)
00319                 XMLWriteVal (XMLTimer, "sec", this->tv_sec);
00320         if (this->tv_usec != 0)
00321                 XMLWriteVal (XMLTimer, "usec", this->tv_usec);
00322 
00323         _XML_ROOT->InsertEndChild(*XMLTimer);
00324 
00325         return XMLTimer;
00326 }
00327 #endif //XML
00328 
00336 int Ygettimeofday(struct timeval * tp)
00337 {
00338         #if defined (_WIN32)
00339                 LARGE_INTEGER ticksPerSecond;
00340                 LARGE_INTEGER tick;   // A point in time
00341                 LARGE_INTEGER time;   // For converting tick into real time
00342 
00343                 // get the high resolution counter's accuracy
00344                 QueryPerformanceFrequency(&ticksPerSecond);
00345 
00346                 // what time is it?
00347                 QueryPerformanceCounter(&tick);
00348 
00349                 // convert the tick number into the number of seconds
00350                 // since the system was started...
00351                 time.QuadPart = tick.QuadPart/ticksPerSecond.QuadPart;
00352                 tp->tv_sec = (long)time.QuadPart;
00353 
00354                 //get the number of hours
00355                 LONGLONG hours = time.QuadPart/3600;
00356 
00357                 //get the number of minutes
00358                 time.QuadPart = time.QuadPart - (hours * 3600);
00359                 LONGLONG minutes = time.QuadPart/60;
00360 
00361                 //get the number of seconds
00362                 LONGLONG  seconds = time.QuadPart - (minutes * 60);
00363                 
00364                 tp->tv_usec = long(tick.QuadPart % ticksPerSecond.QuadPart / (float)ticksPerSecond.QuadPart * 1000000);
00365 
00366                 return 0;//normal return
00367         #elif defined(__linux) || defined(__APPLE__)
00368                 return gettimeofday(tp, NULL );
00369         #else
00370                 return gettimeofday(tp );
00371         #endif  
00372 }
00373 #endif
00374 
00375 };//namespace SG_TRC

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