Back to index

lightning-sunbird  0.9+nobinonly
nsTimer.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla Communicator client code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #ifndef __NSTIMER_H
00039 #define __NSTIMER_H
00040 
00041 #ifdef MOZ_PERF_METRICS
00042 
00043 #include "stopwatch.h"
00044 
00045 class nsStackBasedTimer 
00046 {
00047 public:
00048   nsStackBasedTimer(Stopwatch* aStopwatch) { sw = aStopwatch; }
00049   ~nsStackBasedTimer() { if (sw) sw->Stop(); }
00050 
00051   void Start(PRBool aReset) { if (sw) sw->Start(aReset); }
00052   void Stop(void) { if (sw) sw->Stop(); }
00053   void Reset(void) { if (sw) sw->Reset(); }
00054   void SaveState(void) { if (sw) sw->SaveState(); }
00055   void RestoreState(void) { if (sw) sw->RestoreState(); }
00056   void Print(void) { if (sw) sw->Print(); }
00057   
00058 private:
00059   Stopwatch* sw;
00060 };
00061 
00062 // This should be set from preferences at runtime.  For now, make it a compile time flag.
00063 #define ENABLE_DEBUG_OUTPUT PR_FALSE
00064 
00065 // Uncomment and re-build to use the Mac Instrumentation SDK on a Mac.
00066 // #define MOZ_TIMER_USE_MAC_ISDK
00067 
00068 // Uncomment and re-build to use Quantify on Windows
00069 // #define MOZ_TIMER_USE_QUANTIFY
00070 
00071 // Timer macros for the Mac
00072 #ifdef XP_MAC
00073 
00074 #ifdef MOZ_TIMER_USE_MAC_ISDK
00075 
00076 #include "InstrumentationHelpers.h"
00077 #  define MOZ_TIMER_DECLARE(name)  
00078 #  define MOZ_TIMER_CREATE(name)   \
00079   static InstTraceClassRef name = 0;  StInstrumentationLog __traceLog("Creating name..."), name)
00080 
00081 #  define MOZ_TIMER_RESET(name, msg)
00082 #  define MOZ_TIMER_START(name, msg)
00083 #  define MOZ_TIMER_STOP(name, msg)
00084 #  define MOZ_TIMER_SAVE(name, msg)
00085 #  define MOZ_TIMER_RESTORE(name, msg)
00086 #  define MOZ_TIMER_LOG(msg) \
00087   do { __traceLog.LogMiddleEvent(); } while(0)
00088 
00089 #  define MOZ_TIMER_DEBUGLOG(msg) \
00090   if (ENABLE_DEBUG_OUTPUT) printf msg
00091 
00092 #  define MOZ_TIMER_MACISDK_LOGDATA(msg, data) \
00093   do { traceLog.LogMiddleEventWithData((msg), (data)); } while (0)
00094 
00095 #else
00096 
00097 #define MOZ_TIMER_USE_STOPWATCH
00098 
00099 #endif  // MOZ_TIMER_USE_MAC_ISDK
00100 
00101 #endif  // XP_MAC
00102 
00103 
00104 // Timer macros for Windows
00105 #ifdef XP_WIN
00106 
00107 #ifdef MOZ_TIMER_USE_QUANTIFY
00108 
00109 #include "pure.h"
00110 #include "prprf.h"
00111 
00112 #  define MOZ_TIMER_DECLARE(name)
00113 #  define MOZ_TIMER_CREATE(name)
00114 #  define MOZ_TIMER_RESET(name)  \
00115   QuantifyClearData()
00116 
00117 #  define MOZ_TIMER_START(name)  \
00118   QuantifyStartRecordingData()
00119   
00120 #  define MOZ_TIMER_STOP(name) \
00121   QuantifyStopRecordingData()
00122 
00123 #  define MOZ_TIMER_SAVE(name)
00124 #  define MOZ_TIMER_RESTORE(name)
00125 
00126 #  define MOZ_TIMER_PRINT(name)
00127  
00128 #  define MOZ_TIMER_LOG(msg)    \
00129 do {                            \
00130   char* str = PR_smprintf msg;  \
00131   QuantifyAddAnnotation(str);   \
00132   PR_smprintf_free(str);        \
00133 } while (0)
00134 
00135 #  define MOZ_TIMER_DEBUGLOG(msg) \
00136   if (ENABLE_DEBUG_OUTPUT) printf msg
00137 
00138 #  define MOZ_TIMER_MACISDK_LOGDATA(msg, data)
00139 
00140 #else
00141 
00142 #define MOZ_TIMER_USE_STOPWATCH
00143 
00144 #endif  // MOZ_TIMER_USE_QUANTIFY
00145 
00146 #endif  // XP_WIN
00147 
00148 // Timer macros for Unix
00149 #ifdef XP_UNIX
00150 
00151 #define MOZ_TIMER_USE_STOPWATCH
00152 
00153 #endif  // XP_UNIX
00154 
00155 #ifdef MOZ_TIMER_USE_STOPWATCH
00156 
00157 #  define MOZ_TIMER_DECLARE(name)  \
00158   Stopwatch name;
00159 
00160 #  define MOZ_TIMER_CREATE(name)    \
00161   static Stopwatch __sw_name;  nsStackBasedTimer name(&__sw_name)
00162 
00163 #  define MOZ_TIMER_RESET(name)  \
00164   name.Reset();
00165 
00166 #  define MOZ_TIMER_START(name)  \
00167   name.Start(PR_FALSE);
00168 
00169 #  define MOZ_TIMER_STOP(name) \
00170   name.Stop();
00171 
00172 #  define MOZ_TIMER_SAVE(name) \
00173   name.SaveState();
00174 
00175 #  define MOZ_TIMER_RESTORE(name)  \
00176   name.RestoreState();
00177 
00178 #  define MOZ_TIMER_PRINT(name)   \
00179   name.Print();
00180 
00181 #  define MOZ_TIMER_LOG(msg)  \
00182   printf msg
00183 
00184 #  define MOZ_TIMER_DEBUGLOG(msg) \
00185   if (ENABLE_DEBUG_OUTPUT) printf msg
00186 
00187 #  define MOZ_TIMER_MACISDK_LOGDATA(msg, data)
00188 
00189 #endif // MOZ_TIMER_USE_STOPWATCH
00190 
00191 #else
00192 #  define MOZ_TIMER_DECLARE(name)
00193 #  define MOZ_TIMER_CREATE(name)
00194 #  define MOZ_TIMER_RESET(name)
00195 #  define MOZ_TIMER_START(name)
00196 #  define MOZ_TIMER_STOP(name)
00197 #  define MOZ_TIMER_SAVE(name)
00198 #  define MOZ_TIMER_RESTORE(name)
00199 #  define MOZ_TIMER_PRINT(name)
00200 #  define MOZ_TIMER_LOG(msg)
00201 #  define MOZ_TIMER_DEBUGLOG(msg)
00202 #  define MOZ_TIMER_MACISDK_LOGDATA(msg, data)
00203 #endif  // MOZ_PERF_METRICS
00204 
00205 #endif  // __NSTIMER_H
00206