usermode/library/pmalloc/src/timer.h

00001 
00002 //
00003 // The Hoard Multiprocessor Memory Allocator
00004 // www.hoard.org
00005 //
00006 // Author: Emery Berger, http://www.cs.utexas.edu/users/emery
00007 //
00008 // Copyright (c) 1998-2001, The University of Texas at Austin.
00009 //
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Library General Public License as
00012 // published by the Free Software Foundation, http://www.fsf.org.
00013 //
00014 // This library is distributed in the hope that it will be useful, but
00015 // WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017 // Library General Public License for more details.
00018 //
00020 
00021 
00022 #ifndef _TIMER_H_
00023 #define _TIMER_H_
00024 
00025 #ifdef __SVR4 // Solaris
00026 #include <sys/time.h>
00027 #include <strstream.h>
00028 #include <unistd.h>
00029 #include <fcntl.h>
00030 #include <sys/procfs.h>
00031 #include <stdio.h>
00032 #endif // __SVR4
00033 
00034 #include <time.h>
00035 
00036 #if defined(unix) || defined(__linux)
00037 #include <sys/time.h>
00038 #include <unistd.h>
00039 #endif
00040 
00041 
00042 #ifdef __sgi
00043 #include <sys/types.h>
00044 #include <sys/times.h>
00045 #include <limits.h>
00046 #endif
00047 
00048 
00049 #if defined(WIN32) || defined(__WIN32__) || defined(_WIN32)
00050 #ifndef WIN32
00051 #define WIN32 1
00052 #endif
00053 #include <windows.h>
00054 #endif
00055 
00056 /*** USAGE: ***
00057 
00058    Timer t;
00059 
00060    t.start();
00061    // do hairy computation 1.
00062    t.stop();
00063 
00064    cout << "The first hairy computation took " << (double) t << " seconds." << endl;
00065 
00066    t.reset();
00067 
00068    t.start();
00069    // do hairy computation 2.
00070    t.stop();
00071 
00072    cout << "The second hairy computation took " << (double) t << " seconds." << endl;
00073 
00074 */
00075 
00076 class Timer {
00077 
00078 public:
00079 
00080   Timer (void)
00081     : _starttime (0),
00082       _elapsedtime (0)
00083     {}
00084 
00085   // Start the timer.
00086   void start (void) { _starttime = _time(); }
00087 
00088   // Stop the timer.
00089   void stop (void) { _elapsedtime += _time() - _starttime; }
00090 
00091   // Reset the timer.
00092   void reset (void) { _starttime = _elapsedtime = 0; }
00093 
00094   // Set the timer.
00095   void set (double secs) { _starttime = 0; _elapsedtime = _sectotime (secs);}
00096 
00097   // Return the number of seconds elapsed.
00098   operator double (void) { return _timetosec (_elapsedtime); }
00099 
00100 
00101 private:
00102 
00103   // The _timer variable will be different depending on the OS.
00104   // We try to use the best timer available.
00105 
00106 #ifdef __sgi
00107 #define TIMER_FOUND
00108 
00109   long _starttime, _elapsedtime;
00110 
00111   long _time (void) {
00112     struct tms t;
00113     long ticks = times (&t);
00114     return ticks;
00115   }
00116 
00117   double _timetosec (long t) {
00118     return ((double) (t) / CLK_TCK);
00119   }
00120 
00121   long _sectotime (double sec) {
00122     return (long) sec * CLK_TCK;
00123   }
00124 #endif
00125 
00126 #ifdef __SVR4 // Solaris
00127 #define TIMER_FOUND
00128   hrtime_t      _starttime, _elapsedtime;
00129 
00130   virtual hrtime_t _time (void) {
00131     return gethrtime();
00132   }
00133 
00134   hrtime_t _sectotime (double sec) { return (hrtime_t) (sec * 1.0e9); }
00135 
00136   double _timetosec (hrtime_t& t) {
00137     return ((double) (t) / 1.0e9);
00138   }
00139 #endif // __SVR4
00140 
00141 #if defined(MAC) || defined(macintosh)
00142 #define TIMER_FOUND
00143   double                _starttime, _elapsedtime;
00144 
00145   double _time (void) {
00146     return get_Mac_microseconds();
00147   }
00148 
00149   double _timetosec (hrtime_t& t) {
00150     return t;
00151   }
00152 #endif // MAC
00153 
00154 #ifdef WIN32
00155 #define TIMER_FOUND
00156   DWORD _starttime, _elapsedtime;
00157 
00158   DWORD _time (void) {
00159     return GetTickCount();
00160   }
00161 
00162   double _timetosec (DWORD& t) {
00163     return (double) t / 1000.0;
00164   }
00165 
00166   unsigned long _sectotime (double sec) {
00167                 return (unsigned long)(sec * 1000);
00168         }
00169 
00170 #endif // WIN32
00171 
00172 
00173 #ifndef TIMER_FOUND
00174 
00175   long _starttime, _elapsedtime;
00176 
00177   long _time (void) {
00178     struct timeval t;
00179     struct timezone tz;
00180     gettimeofday (&t, &tz);
00181     return t.tv_sec * 1000000 + t.tv_usec;
00182   }
00183 
00184   double _timetosec (long t) {
00185     return ((double) (t) / 1000000);
00186   }
00187 
00188   long _sectotime (double sec) {
00189     return (long) sec * 1000000;
00190   }
00191 
00192 #endif // TIMER_FOUND
00193 
00194 #undef TIMER_FOUND
00195 
00196 };
00197 
00198 
00199 #ifdef __SVR4 // Solaris
00200 class VirtualTimer : public Timer {
00201 public:
00202   hrtime_t _time (void) {
00203     return gethrvtime();
00204   }
00205 };  
00206 #endif
00207 
00208 #endif

Generated on Sat Apr 23 11:43:35 2011 for Mnemosyne by  doxygen 1.4.7