Back to index

lightning-sunbird  0.9+nobinonly
stopwatch.h
Go to the documentation of this file.
00001 #ifndef __STOPWATCH_H
00002 #define __STOPWATCH_H
00003 #include "nscore.h"
00004 #include "prlog.h"
00005 #include "nsDeque.h"
00006 
00007 #ifdef XP_MAC
00008 #define R__MAC
00009 #endif
00010 
00011 #ifdef XP_UNIX
00012 #define R__UNIX
00013 #endif
00014 
00015 #ifdef MOZ_PERF_METRICS
00016 #  define NS_RESET_AND_START_STOPWATCH(_sw)          \
00017     _sw.Start(PR_TRUE);
00018 
00019 #  define NS_START_STOPWATCH(_sw)                    \
00020     _sw.Start(PR_FALSE);
00021 
00022 #  define NS_STOP_STOPWATCH(_sw)                     \
00023     _sw.Stop();
00024 
00025 #  define NS_SAVE_STOPWATCH_STATE(_sw)               \
00026     _sw.SaveState();
00027 
00028 #  define NS_RESTORE_STOPWATCH_STATE(_sw)            \
00029     _sw.RestoreState();
00030 
00031 #else
00032 #  define NS_RESET_AND_START_STOPWATCH(_sw) 
00033 #  define NS_START_STOPWATCH(_sw)
00034 #  define NS_STOP_STOPWATCH(_sw)
00035 #  define NS_SAVE_STOPWATCH_STATE(_sw)
00036 #  define NS_RESTORE_STOPWATCH_STATE(_sw)
00037 #endif
00038 
00039 
00040 #ifdef MOZ_PERF_METRICS
00041 
00042 static PRLogModuleInfo* gLogStopwatchModule = PR_NewLogModule("timing");
00043 
00044 #if 0
00045 #define RAPTOR_TRACE_STOPWATCHES        0x1
00046 
00047 #define RAPTOR_STOPWATCH_TRACE(_args)                               \
00048   PR_BEGIN_MACRO                                                    \
00049   PR_LOG(gLogStopwatchModule, RAPTOR_TRACE_STOPWATCHES, _args);     \
00050   PR_END_MACRO
00051 #endif
00052 
00053 #define RAPTOR_STOPWATCH_TRACE(_args)      \
00054   PR_BEGIN_MACRO                           \
00055   printf _args ;                           \
00056   PR_END_MACRO
00057 
00058 #else
00059 #define RAPTOR_TRACE_STOPWATCHES 
00060 #define RAPTOR_STOPWATCH_TRACE(_args) 
00061 #endif
00062 
00063 #ifdef DEBUG_STOPWATCH
00064 #define RAPTOR_STOPWATCH_DEBUGTRACE(_args)      \
00065   PR_BEGIN_MACRO                                \
00066   printf _args ;                                \
00067   PR_END_MACRO
00068 #else
00069 #define RAPTOR_STOPWATCH_DEBUGTRACE(_args) 
00070 #endif
00071 
00072 class Stopwatch {
00073 
00074 private:
00075    enum EState { kUndefined, kStopped, kRunning };
00076 
00077    double         fStartRealTime;   //wall clock start time
00078    double         fStopRealTime;    //wall clock stop time
00079    double         fStartCpuTime;    //cpu start time
00080    double         fStopCpuTime;     //cpu stop time
00081    double         fTotalCpuTime;    //total cpu time
00082    double         fTotalRealTime;   //total real time
00083    EState         fState;           //stopwatch state
00084    nsDeque*       mSavedStates;     //stack of saved states
00085    PRBool         mCreatedStack;    //Initially false.  Set to true in first SaveState() call.
00086 
00087 public:
00088    Stopwatch();
00089    virtual ~Stopwatch();
00090 
00091    void           Start(PRBool reset = PR_TRUE);
00092    void           Stop();
00093    void           Continue();
00094    void           SaveState();      // record current state of stopwatch
00095    void           RestoreState();   // restore last recored state of stopwatch
00096    double         RealTime();
00097    double         RealTimeInMilliseconds();
00098    void           Reset() { ResetCpuTime(); ResetRealTime(); }
00099    void           ResetCpuTime(double aTime = 0) { Stop();  fTotalCpuTime = aTime; }
00100    void           ResetRealTime(double aTime = 0) { Stop(); fTotalRealTime = aTime; }
00101    double         CpuTime();
00102    void           Print(void);
00103    static double  GetRealTime();
00104    static double  GetCPUTime();
00105 
00106 };
00107 #endif