Back to index

lightning-sunbird  0.9+nobinonly
Public Member Functions | Static Public Member Functions | Public Attributes | Private Types | Private Attributes
Stopwatch Class Reference

#include <stopwatch.h>

Collaboration diagram for Stopwatch:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 Stopwatch ()
virtual ~Stopwatch ()
void Start (PRBool reset=PR_TRUE)
void Stop ()
void Continue ()
void SaveState ()
void RestoreState ()
double RealTime ()
double RealTimeInMilliseconds ()
void Reset ()
void ResetCpuTime (double aTime=0)
void ResetRealTime (double aTime=0)
double CpuTime ()
void Print (void)
 Stopwatch ()
void Stop ()
double Elapsed ()
void Print (const char *msg="")

Static Public Member Functions

static double GetRealTime ()
static double GetCPUTime ()

Public Attributes

clock_t start
clock_t stop

Private Types

enum  EState { kUndefined, kStopped, kRunning }

Private Attributes

double fStartRealTime
double fStopRealTime
double fStartCpuTime
double fStopCpuTime
double fTotalCpuTime
double fTotalRealTime
EState fState
nsDeque * mSavedStates
PRBool mCreatedStack

Detailed Description

Definition at line 72 of file stopwatch.h.


Member Enumeration Documentation

enum Stopwatch::EState [private]
Enumerator:
kUndefined 
kStopped 
kRunning 

Definition at line 75 of file stopwatch.h.


Constructor & Destructor Documentation

Definition at line 23 of file stopwatch.cpp.

                     {

#ifdef R__UNIX
   if (!gTicks) gTicks = (clock_t)sysconf(_SC_CLK_TCK);
#endif
   fState         = kUndefined;   
   fTotalCpuTime  = 0;
   fTotalRealTime = 0;   
   mCreatedStack = PR_FALSE;
   mSavedStates = nsnull;
   Start();
}

Here is the call graph for this function:

Stopwatch::~Stopwatch ( ) [virtual]

Definition at line 36 of file stopwatch.cpp.

                      {
  EState* state = 0;
  if (mSavedStates) {
    while ((state = (EState*) mSavedStates->Pop())) {
      delete state;
    } 
    delete mSavedStates;
  }
}
Stopwatch::Stopwatch ( ) [inline]

Definition at line 117 of file nsStringTest.h.

              {
    start=clock();
  }

Member Function Documentation

double Stopwatch::CpuTime ( )

Definition at line 159 of file stopwatch.cpp.

                          {
  if (fState != kUndefined) {

    if (fState == kRunning)
      Stop();

  }
#ifdef MILLISECOND_RESOLUTION
  return fTotalCpuTime / 1000;  // adjust from milliseconds to seconds
#else
  return fTotalCpuTime;
#endif
}

Here is the call graph for this function:

Here is the caller graph for this function:

double Stopwatch::Elapsed ( ) [inline]

Definition at line 125 of file nsStringTest.h.

                   {
    return (double)(stop - start) / CLOCKS_PER_SEC;
  }

Here is the caller graph for this function:

double Stopwatch::GetCPUTime ( ) [static]

Definition at line 195 of file stopwatch.cpp.

                            { 
#if defined(R__MAC)
// return(double)clock() / gTicks;
   return(double)clock();
#elif defined(R__UNIX)
   struct tms cpt;
   times(&cpt);
   return (double)(cpt.tms_utime+cpt.tms_stime) / gTicks;
#elif defined(R__VMS)
   return(double)clock()/gTicks;
#elif defined(WINCE)
   return 0;
#elif defined(WIN32)

  OSVERSIONINFO OsVersionInfo;

//*-*         Value                      Platform
//*-*  ----------------------------------------------------
//*-*  VER_PLATFORM_WIN32_WINDOWS       Win32 on Windows 95
//*-*  VER_PLATFORM_WIN32_NT            Windows NT
//*-*
  OsVersionInfo.dwOSVersionInfoSize=sizeof(OSVERSIONINFO);
  GetVersionEx(&OsVersionInfo);
  if (OsVersionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) {
    DWORD       ret;
    FILETIME    ftCreate,       // when the process was created
                ftExit;         // when the process exited

    union     {FILETIME ftFileTime;
               __int64  ftInt64;
              } ftKernel; // time the process has spent in kernel mode

    union     {FILETIME ftFileTime;
               __int64  ftInt64;
              } ftUser;   // time the process has spent in user mode

    HANDLE hProcess = GetCurrentProcess();
    ret = GetProcessTimes (hProcess, &ftCreate, &ftExit,
                                     &ftKernel.ftFileTime,
                                     &ftUser.ftFileTime);
    if (ret != PR_TRUE){
      ret = GetLastError ();
#ifdef DEBUG
      printf("%s 0x%lx\n"," Error on GetProcessTimes", (int)ret);
#endif
    }

    /*
     * Process times are returned in a 64-bit structure, as the number of
     * 100 nanosecond ticks since 1 January 1601.  User mode and kernel mode
     * times for this process are in separate 64-bit structures.
     * To convert to floating point seconds, we will:
     *
     *          Convert sum of high 32-bit quantities to 64-bit int
     */

      return (double) (ftKernel.ftInt64 + ftUser.ftInt64) * gTicks;
  }
  else
      return GetRealTime();

#endif
}

Here is the call graph for this function:

Here is the caller graph for this function:

double Stopwatch::GetRealTime ( ) [static]

Definition at line 174 of file stopwatch.cpp.

                             { 
#if defined(R__MAC)
// return(double)clock() / gTicks;
   return(double)clock() / 1000000L;
#elif defined(R__UNIX)
   struct tms cpt;
   return (double)times(&cpt) / gTicks;
#elif defined(R__VMS)
  return(double)clock()/gTicks;
#elif defined(WIN32)
  union     {FILETIME ftFileTime;
             __int64  ftInt64;
            } ftRealTime; // time the process has spent in kernel mode
  SYSTEMTIME st;
  GetSystemTime(&st);
  SystemTimeToFileTime(&st,&ftRealTime.ftFileTime);
  return (double)ftRealTime.ftInt64 * gTicks;
#endif
}

Here is the caller graph for this function:

Definition at line 260 of file stopwatch.cpp.

                          {
   // Print the real and cpu time passed between the start and stop events.

   double  realt = RealTimeInMilliseconds();

   int  hours = int(realt / 3600000);
   realt -= hours * 3600000;
   int  min   = int(realt / 60000);
   realt -= min * 60000;
   int  sec   = int(realt/1000);
   realt -= sec * 1000;
#ifdef MOZ_PERF_METRICS
  int ms = int(realt);
   RAPTOR_STOPWATCH_TRACE(("Real time %d:%d:%d.%d, CP time %.3f\n", hours, min, sec, ms, CpuTime()));
#elif defined(DEBUG)
  int ms = int(realt);
   printf("Real time %d:%d:%d.%d, CP time %.3f\n", hours, min, sec, ms, CpuTime());
#endif
}

Here is the call graph for this function:

Here is the caller graph for this function:

void Stopwatch::Print ( const char *  msg = "") [inline]

Definition at line 129 of file nsStringTest.h.

                                  {
    printf("%s %f\n",msg,Elapsed());
  }
double Stopwatch::RealTime ( )

Definition at line 127 of file stopwatch.cpp.

                           {

  if (fState != kUndefined) {
    if (fState == kRunning)
      Stop();
  }

#ifdef MILLISECOND_RESOLUTION
  return fTotalRealTime/1000;
#else
  return fTotalRealTime;
#endif
}

Here is the call graph for this function:

Definition at line 143 of file stopwatch.cpp.

                                         {

  if (fState != kUndefined) {
    if (fState == kRunning)
      Stop();
  }

#ifdef MILLISECOND_RESOLUTION
  return fTotalRealTime;
#else
  return fTotalRealTime * 1000; // we don;t have milliseconds, so fake it
#endif
}

Here is the call graph for this function:

Here is the caller graph for this function:

void Stopwatch::Reset ( void  ) [inline]

Definition at line 98 of file stopwatch.h.

Here is the call graph for this function:

void Stopwatch::ResetCpuTime ( double  aTime = 0) [inline]

Definition at line 99 of file stopwatch.h.

{ Stop();  fTotalCpuTime = aTime; }

Here is the call graph for this function:

Here is the caller graph for this function:

void Stopwatch::ResetRealTime ( double  aTime = 0) [inline]

Definition at line 100 of file stopwatch.h.

{ Stop(); fTotalRealTime = aTime; }

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 96 of file stopwatch.cpp.

                             {
  EState* state = nsnull;
  state = (EState*) mSavedStates->Pop();
  if (state) {
    if (*state == kRunning && fState == kStopped)
      Start(PR_FALSE);
    else if (*state == kStopped && fState == kRunning)
      Stop();
    delete state;
  }
  else {
    NS_WARNING("Stopwatch::RestoreState(): The saved state stack is empty.\n");
  }
}

Here is the call graph for this function:

Definition at line 82 of file stopwatch.cpp.

                          {
  if (!mCreatedStack) {
    mSavedStates = new nsDeque(nsnull);
    if (!mSavedStates)
      return;
    mCreatedStack = PR_TRUE;
  }
  EState* state = new EState();
  if (state) {
    *state = fState;
    mSavedStates->PushFront((void*) state);
  }
}

Definition at line 46 of file stopwatch.cpp.

                                  {
   if (reset) {
      fTotalCpuTime  = 0;
      fTotalRealTime = 0;
   }
   if (fState != kRunning) {
#ifndef R__UNIX
      fStartRealTime = GetRealTime();
      fStartCpuTime  = GetCPUTime();
#else
      struct tms cpt;
      fStartRealTime = (double)times(&cpt) / gTicks;
      fStartCpuTime  = (double)(cpt.tms_utime+cpt.tms_stime) / gTicks;
#endif
   }
   fState = kRunning;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 64 of file stopwatch.cpp.

                     {

#ifndef R__UNIX
   fStopRealTime = GetRealTime();
   fStopCpuTime  = GetCPUTime();
#else
   struct tms cpt;
   fStopRealTime = (double)times(&cpt) / gTicks;
   fStopCpuTime  = (double)(cpt.tms_utime+cpt.tms_stime) / gTicks;
#endif
   if (fState == kRunning) {
      fTotalCpuTime  += fStopCpuTime  - fStartCpuTime;
      fTotalRealTime += fStopRealTime - fStartRealTime;
   }
   fState = kStopped;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void Stopwatch::Stop ( void  ) [inline]

Definition at line 121 of file nsStringTest.h.

              {
    stop=clock();
  }

Member Data Documentation

double Stopwatch::fStartCpuTime [private]

Definition at line 79 of file stopwatch.h.

double Stopwatch::fStartRealTime [private]

Definition at line 77 of file stopwatch.h.

Definition at line 83 of file stopwatch.h.

double Stopwatch::fStopCpuTime [private]

Definition at line 80 of file stopwatch.h.

double Stopwatch::fStopRealTime [private]

Definition at line 78 of file stopwatch.h.

double Stopwatch::fTotalCpuTime [private]

Definition at line 81 of file stopwatch.h.

double Stopwatch::fTotalRealTime [private]

Definition at line 82 of file stopwatch.h.

Definition at line 85 of file stopwatch.h.

nsDeque* Stopwatch::mSavedStates [private]

Definition at line 84 of file stopwatch.h.

Definition at line 133 of file nsStringTest.h.

clock_t Stopwatch::stop

Definition at line 133 of file nsStringTest.h.


The documentation for this class was generated from the following files: