usermode/library/malloc-original/benchmarks/shbench/timer.h

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

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