CamProfile Class Reference

To time various bits of the Camelot subsytem Notes: In the OIL See Also: CamProfiler. More...

#include <camprofile.h>

Inheritance diagram for CamProfile:

CCObject SimpleCCObject List of all members.

Public Member Functions

 CamProfile (CamProfileMode myMode=CAMPROFILE_NONE)
 Default constructor.
virtual ~CamProfile ()
 Destructor.
void SetMode (CamProfileMode myMode, BOOL Base=FALSE)
 Changes the mode of a profiler.

Static Public Member Functions

static BOOL Init ()
 Starts up the profiling system.
static BOOL ClearList ()
static BOOL ResetCounters ()
 Resets the profiling counters.
static BOOL ActivateProfiling (BOOL Run=TRUE)
 Starts up the profiling system.
static void AtBase (CamProfileMode myMode=CAMPROFILE_NONE)
 Indicates to the profiling system we are at the base of the system.
static void UpdateOilTimes ()
 Updates the current oil times.
static void GetTimeString (TCHAR *pTime, UINT32 length)
 Returns the current Oil time.

Private Attributes

CamProfilepPrev
CamProfilepNext
CamProfileMode Mode
UINT64 OilTime
BOOL Zombie

Static Private Attributes

static CamProfilepHead = NULL
static CamProfilepTail = NULL
static CamProfilepCurrent = NULL
static BOOL Running = 0
static BOOL Inited = 0
static CamProfileOurHead = NULL
static UINT64 LastOilTime = 0
static UINT64 OilTimeArray [CAMPROFILE_NONE]

Friends

class CamProfiler

Detailed Description

To time various bits of the Camelot subsytem Notes: In the OIL See Also: CamProfiler.

Author:
Alex_Bligh <alex@alex.org.uk>
Date:
24/11/2005
This class allows a limited degree of built in profiling of Camelot operations.

Around something you want to time, construct a new CamProfile object

So for instance:

void myslowfunc() { ... do something ... CamProfile myCamProfile(CAMPROFILE_MYSLOWFUNC)l

... do the thing you want timed ...

Object destroyed as it goes out of scope. }

Note that you do not need a specific destructor. We cope with nested profiles in the obvious manner. When an exception is thrown, Camelot at various points cleans off the list of nested profiles. This may make them inaccurate.

Definition at line 150 of file camprofile.h.


Constructor & Destructor Documentation

CamProfile::CamProfile CamProfileMode  myMode = CAMPROFILE_NONE  ) 
 

Default constructor.

Author:
Alex_Bligh <alex@alex.org.uk>
Date:
24/11/2005
Parameters:
- [INPUTS]
- [OUTPUTS]
Returns:
-

Errors: -

See also:
-

Definition at line 143 of file camprofile.cpp.

00144 {
00145     // Set up in isolation
00146 
00147     Mode = CAMPROFILE_NONE;
00148     pPrev = NULL;
00149     pNext = NULL;
00150     OilTime = 0;
00151     
00152     Zombie = FALSE ;
00153 
00154     if (!Running)
00155     {
00156         Zombie = TRUE;
00157         return;
00158     }
00159 
00160     ERROR3IF(!Inited, "Profiling system not yet initialized");
00161 
00162     // Add us to the end of the list
00163     if (pTail)
00164     {
00165         pTail->pNext = this;
00166         pPrev = pTail;
00167     }
00168     pTail = this;
00169 
00170     if (!pHead)
00171     {
00172         pHead = this;
00173     }   
00174 
00175     // note we didn't alter pCurrent above, so it's still set to the old one
00176     SetMode(myMode);
00177 
00178 }

CamProfile::~CamProfile  )  [virtual]
 

Destructor.

Author:
Alex_Bligh <alex@alex.org.uk>
Date:
24/11/2005
Parameters:
- [INPUTS]
- [OUTPUTS]
Returns:
-

Errors: -

See also:
-

Definition at line 196 of file camprofile.cpp.

00197 {
00198     if (Zombie)
00199     {
00200         return;
00201     }
00202     
00203     // If it's not a Zombie, but we are still running, we need to do
00204     // list maintenance etc.
00205     
00206     ERROR3IF(!Inited, "Profiling system not yet initialized");
00207 
00208     if (pPrev)
00209     {
00210         pPrev->pNext = pNext;
00211     }
00212     else
00213     {
00214         // No previous, we must have been the head
00215         pHead = pNext;
00216     }
00217 
00218     if (pNext)
00219     {
00220         pNext->pPrev = pPrev;
00221     }
00222     else
00223     {
00224         // We must have been the tail
00225         pTail = pPrev;
00226     }
00227 
00228     // Make sure we don't appear to be in the list still
00229     pNext=NULL;
00230     pPrev=NULL;
00231 
00232     // Reawake the previous profiler
00233     if (pTail)
00234     {
00235         // this should wipe us off as pCurrent too.
00236         pTail->SetMode(pTail->Mode);
00237     }
00238     else
00239     {   
00240         pCurrent = NULL;
00241     }
00242 
00243     ERROR3IF(pCurrent == this, "Somehow current profiler did not get unset");
00244 }


Member Function Documentation

BOOL CamProfile::ActivateProfiling BOOL  Run = TRUE  )  [static]
 

Starts up the profiling system.

Author:
Alex_Bligh <alex@alex.org.uk>
Date:
24/11/2005
Parameters:
None [INPUTS]
None [OUTPUTS]
Returns:
TRUE if worked, FALSE if failed (out of memory)

Errors: Returns FALSE on failure. Scope: Static

This is the heart of the profiling system. We've just been told our timer has changed from its current state, to myMode. Therefore, if we were profiling before, we attribute the time passed since that timer started to the relevant array, and mark the old profiler as being dormant. We then set our own time, and set our start position.

Definition at line 607 of file camprofile.cpp.

00608 {
00609     // Do nothing if we are already in that state
00610     if (Running == Run)
00611     {
00612         return TRUE;
00613     }
00614 
00615     ERROR3IF(!Inited, "Profiling system not yet initialized");
00616 
00617     if (Run)
00618     {
00619         // Ignore old linked list
00620         pHead = NULL;
00621         pTail = NULL;
00622 
00623         Running=TRUE;
00624         if (!pHead)
00625         {
00626             // Note we allocate this, and discard the pointer. That's because it has already
00627             // added itself to the linked list
00628             if (OurHead) delete(OurHead);
00629             OurHead = new CamProfile;   
00630             if (!OurHead) return FALSE;         
00631         }
00632         // We can't wake the zombies up as we don't have a list of them
00633         ResetCounters();
00634         return TRUE;
00635     }
00636     else
00637     {
00638         if (pCurrent)
00639         {
00640             pCurrent->SetMode(CAMPROFILE_NONE);
00641         }
00642         ResetCounters();
00643 
00644         // Now go through the list removing them all
00645         CamProfile * p = pTail;
00646         while (p)
00647         {
00648             CamProfile* pn = p->pPrev;
00649             p->Zombie = TRUE;
00650             p->pPrev = NULL;
00651             p->pNext = NULL;
00652             p = pn;
00653             // sadly they are not ours to delete
00654         }
00655         pHead = NULL;
00656         pTail = NULL;
00657 
00658         if (OurHead)
00659         {
00660             delete (OurHead);
00661             OurHead = NULL;
00662         }
00663         Running = FALSE;
00664 
00665     }
00666     return TRUE;
00667 }

void CamProfile::AtBase CamProfileMode  myMode = CAMPROFILE_NONE  )  [static]
 

Indicates to the profiling system we are at the base of the system.

Author:
Alex_Bligh <alex@alex.org.uk>
Date:
24/11/2005
Parameters:
None [INPUTS]
None [OUTPUTS]
Returns:
None

Errors: Returns FALSE on failure. Scope: Static

This is the heart of the profiling system. We've just been told our timer has changed from its current state, to myMode. Therefore, if we were profiling before, we attribute the time passed since that timer started to the relevant array, and mark the old profiler as being dormant. We then set our own time, and set our start position.

Definition at line 508 of file camprofile.cpp.

00509 {
00510     ERROR3IF(!Inited, "Profiling system not yet initialized");
00511 
00512     if (!Running)
00513     {
00514         return;
00515     }
00516 
00517     if (!pTail)
00518     {
00519         // Note we allocate this, and discard the pointer. That's because it has already
00520         // added itself to the linked list
00521         if (OurHead) delete(OurHead);
00522         OurHead = new CamProfile;   
00523         if (!OurHead) return;
00524     }
00525 
00526     ERROR3IF(!pTail,"Didn't get a tail pointer");
00527     if (pTail)
00528     {
00529         pTail->SetMode(myMode, TRUE);
00530     }
00531 }

static BOOL CamProfile::ClearList  )  [static]
 

void CamProfile::GetTimeString TCHAR pTime,
UINT32  length
[static]
 

Returns the current Oil time.

Author:
Alex_Bligh <alex@alex.org.uk>
Date:
24/11/2005
Parameters:
length - maximum length of string to be filled in, in CHARACTERS including NULL [INPUTS]
pTime - filled in with string [OUTPUTS]
Returns:
None

Errors: Returns FALSE on failure. Scope: static

Definition at line 442 of file camprofile.cpp.

00443 {
00444     ERROR3IF(!Inited, "Profiling system not yet initialized");
00445     UpdateOilTimes();
00446     if ((length < 26+6) || pCurrent && Running && !pCurrent->Zombie)
00447     {
00448         // Read from LastOilTime
00449         UINT64 Time=LastOilTime;
00450 #if WIN32
00451         // Convert LastOil time to secs since 1 Jan 1970. We don't care about the date,
00452         // we only care about the time, so we pick a roughly correct number of DAYS
00453         // to add.
00454         // That's 369 years, which would have 92 leap years, except for the fact that
00455         // 1700, 1800, 1900 were not leap years. So that's 369 years of 365 days plus 89
00456         // leap year days, or 134,774 days, or 11,644,473,600 seconds
00457         // For the purist, I suppose we might be a few leap seconds out. Yawn.
00458         // Another test (thta's all one line):
00459         // 11perl -e '{use DateTime;$o=DateTime->new(year=>1601,month=>1,day=>1); 
00460         //            $n=DateTime->new(year=>1970,month=>1,day=>1), 
00461         //            printf "%d\n", $n->subtract_datetime_absolute($o)->in_units(seconds)}'
00462         //
00463         // 11644473600
00464         //
00465         Time = Time-11644473600LL*10000000LL;
00466         Time = (Time+5)/10;
00467 #endif
00468         // Time is now in microseconds since 1 Jan 1970
00469         UINT64 uSecs = Time % 1000000;
00470         UINT64 Secs = Time / 1000000;
00471         time_t t = Secs;
00472 
00473         // Don't use ctime_r as it isn't present on Windows
00474 
00475         //Ask wx for a string with the time
00476         wxDateTime TheTime(t);
00477         wxString sTime(TheTime.FormatISOTime());
00478 
00479         // Now camSnprintf this into the string. This turns it into UNICODE if appropriate
00480         camSnprintf (pTime, length, _T("%s.%06d"), sTime.c_str(), uSecs);
00481     }
00482     else camStrncpy(pTime, _T("[UNAVAILABLE]"), length);
00483     pTime[length-1]=0; // ensure string terminated - string copy primatives do funny thing
00484 }

BOOL CamProfile::Init void   )  [static]
 

Starts up the profiling system.

Author:
Alex_Bligh <alex@alex.org.uk>
Date:
24/11/2005
Parameters:
None [INPUTS]
None [OUTPUTS]
Returns:
TRUE if worked, FALSE if failed (out of memory)

Errors: Returns FALSE on failure. Scope: Static

Reimplemented from SimpleCCObject.

Definition at line 263 of file camprofile.cpp.

00264 {
00265     if (Inited) return(FALSE);
00266 
00267     for (INT32 i=0; i<CAMPROFILE_NONE; i++)
00268     {
00269         OilTimeArray[i]=0;
00270     }
00271 
00272     pHead = NULL;
00273     pTail = NULL;
00274     pCurrent = NULL;
00275     OurHead = new CamProfile;
00276     Running = FALSE;
00277 
00278     if (!OurHead) return FALSE;
00279 
00280     Inited = TRUE;
00281     
00282     // Give the lists a work through
00283     ActivateProfiling (TRUE);
00284     ActivateProfiling (FALSE);
00285 
00286     return TRUE;    
00287 
00288 }

BOOL CamProfile::ResetCounters  )  [static]
 

Resets the profiling counters.

Author:
Alex_Bligh <alex@alex.org.uk>
Date:
24/11/2005
Parameters:
None [INPUTS]
None [OUTPUTS]
Returns:
TRUE if worked, FALSE if failed (out of memory)

Errors: Returns FALSE on failure. Scope: Static

Reset the counters. DO NOT CALL THIS unless you know what you are doing as it will mean that anyone with stored counter values is going to get a nasty shock.

Definition at line 552 of file camprofile.cpp.

00553 {
00554     if (!Running)
00555     {
00556         return TRUE;
00557     }
00558 
00559     ERROR3IF(!Inited, "Profiling system not yet initialized");
00560 
00561     CamProfileMode OldMode=CAMPROFILE_NONE;
00562 
00563     // Save the old mode of the current operator so we do not credit
00564     // already elapsed time
00565 
00566     CamProfile * pActive = pCurrent;
00567 
00568     if (pActive)
00569     {
00570         OldMode = pActive->Mode;
00571         pActive->SetMode(CAMPROFILE_NONE);
00572     }   
00573 
00574     for (INT32 i=0; i<CAMPROFILE_NONE; i++)
00575     {
00576         OilTimeArray[i]=0;
00577     }
00578 
00579     if (pActive)
00580     {
00581         pActive->SetMode(OldMode);
00582     }
00583     return TRUE;
00584 }

void CamProfile::SetMode CamProfileMode  myMode,
BOOL  Base = FALSE
 

Changes the mode of a profiler.

Author:
Alex_Bligh <alex@alex.org.uk>
Date:
24/11/2005
Parameters:
myMode - the new mode [INPUTS] Base - TRUE to ditch the existing stack of modes
None [OUTPUTS]
Returns:
TRUE if worked, FALSE if failed (out of memory)

Errors: Returns FALSE on failure. Scope:

This is the heart of the profiling system. We've just been told our timer has changed from its current state, to myMode. Therefore, if we were profiling before, we attribute the time passed since that timer started to the relevant array, and mark the old profiler as being dormant. We then set our own time, and set our start position.

Definition at line 313 of file camprofile.cpp.

00314 {
00315     if (Zombie || !Running)
00316     {
00317         return;
00318     }
00319 
00320     ERROR3IF(!Inited, "Profiling system not yet initialized");
00321 
00322     UINT64 NewTime;
00323 #if WIN32
00324     // This windows code completely untested by Alex
00325     GetSystemTimeAsFileTime ((LPFILETIME)(&NewTime));
00326 #else
00327     timeval tv;
00328     gettimeofday(&tv, NULL);
00329     NewTime = (UINT64)(tv.tv_usec)+ ((UINT64)(tv.tv_sec) * 1000000);
00330 #endif
00331 
00332     // Mainly for debugging
00333     LastOilTime = NewTime;
00334 
00335     // TRACE(_T("CAMPROFILER OilTime is %lld\n"),NewTime);
00336 
00337     // If there is a current profiler, stop it.
00338     if (pCurrent && ( pCurrent->Mode != CAMPROFILE_NONE))
00339     {
00340         // Stop the current profiler running - note this may be us
00341         UINT64 Elapsed = NewTime - pCurrent->OilTime;
00342         OilTimeArray[pCurrent->Mode] += Elapsed;
00343         
00344         // TRACE(_T("CAMPROFILER Credit %lld to %d\n"),Elapsed,(INT32)pCurrent->Mode);
00345 
00346         pCurrent->OilTime = 0; // So we can detect screw-ups - we'll set it again when we start it
00347         pCurrent=NULL;
00348     }
00349 #if 0
00350     else
00351     {
00352         TRACE(_T("CAMPROFILER AWOOGA1 pCurrent=%llx\n"),pCurrent);
00353         if (pCurrent)
00354         {
00355             TRACE(_T("CAMPROFILER AWOOGA2 Not crediting %llx ticks\n"),NewTime - pCurrent->OilTime);
00356         }
00357     }
00358 #endif
00359 
00360     // If the Base flag is set, junk all the others on the list.
00361     if (Base && ( (pTail != this) || (pHead != this)))  // don't bother if we're the only one on the list
00362     {
00363         // We are going to junk all the existing profiles except us. Bwahahaha
00364         TRACE(_T("CAMPROFILER Clearing up redudant profiling entries\n"));
00365 
00366         // First junk them all
00367         CamProfile * p = pTail;
00368         while (p)
00369         {
00370             CamProfile* pn = p->pPrev;
00371             p->Zombie = TRUE;
00372             p->pPrev = NULL;
00373             p->pNext = NULL;
00374             p = pn;
00375             // sadly they are not ours to delete
00376         }
00377         
00378         // Now unjunk us
00379         Zombie = FALSE;
00380         pHead = this;
00381         pTail = this;
00382     }   
00383 
00384 
00385     // Now start us
00386     if (myMode != CAMPROFILE_NONE)
00387     {
00388         OilTime = NewTime;
00389         Mode = myMode;
00390         pCurrent = this;
00391     }
00392 
00393 }

void CamProfile::UpdateOilTimes  )  [static]
 

Updates the current oil times.

Author:
Alex_Bligh <alex@alex.org.uk>
Date:
24/11/2005
Parameters:
None [INPUTS]
None [OUTPUTS]
Returns:
None

Errors: Returns FALSE on failure. Scope: static

This writes the OIL time back into the cumulative time. It should be used before reading the OilTimesArray to ensure the current profiler hasn't got uncredited time on.

Definition at line 415 of file camprofile.cpp.

00416 {
00417     ERROR3IF(!Inited, "Profiling system not yet initialized");
00418     if (pCurrent && Running && !pCurrent->Zombie)
00419     {
00420         // Resetting the mode is sufficient
00421         pCurrent->SetMode(pCurrent->Mode);
00422     }
00423 }


Friends And Related Function Documentation

friend class CamProfiler [friend]
 

Definition at line 154 of file camprofile.h.


Member Data Documentation

BOOL CamProfile::Inited = 0 [static, private]
 

Definition at line 183 of file camprofile.h.

UINT64 CamProfile::LastOilTime = 0 [static, private]
 

Definition at line 188 of file camprofile.h.

CamProfileMode CamProfile::Mode [private]
 

Definition at line 176 of file camprofile.h.

UINT64 CamProfile::OilTime [private]
 

Definition at line 187 of file camprofile.h.

UINT64 CamProfile::OilTimeArray [static, private]
 

Definition at line 192 of file camprofile.h.

CamProfile * CamProfile::OurHead = NULL [static, private]
 

Definition at line 185 of file camprofile.h.

CamProfile * CamProfile::pCurrent = NULL [static, private]
 

Definition at line 180 of file camprofile.h.

CamProfile * CamProfile::pHead = NULL [static, private]
 

Definition at line 178 of file camprofile.h.

CamProfile* CamProfile::pNext [private]
 

Definition at line 174 of file camprofile.h.

CamProfile* CamProfile::pPrev [private]
 

Definition at line 173 of file camprofile.h.

CamProfile * CamProfile::pTail = NULL [static, private]
 

Definition at line 179 of file camprofile.h.

BOOL CamProfile::Running = 0 [static, private]
 

Definition at line 182 of file camprofile.h.

BOOL CamProfile::Zombie [private]
 

Definition at line 190 of file camprofile.h.


The documentation for this class was generated from the following files:
Generated on Sat Nov 10 03:51:49 2007 for Camelot by  doxygen 1.4.4