Eneboo - Documentación para desarrolladores
src/hoard/src/heaplayers/util/timer.h
Ir a la documentación de este archivo.
00001 /* -*- C++ -*- */
00002 
00003 /*
00004 
00005   Heap Layers: An Extensible Memory Allocation Infrastructure
00006   
00007   Copyright (C) 2000-2003 by Emery Berger
00008   http://www.cs.umass.edu/~emery
00009   emery@cs.umass.edu
00010   
00011   This program is free software; you can redistribute it and/or modify
00012   it under the terms of the GNU General Public License as published by
00013   the Free Software Foundation; either version 2 of the License, or
00014   (at your option) any later version.
00015   
00016   This program is distributed in the hope that it will be useful,
00017   but WITHOUT ANY WARRANTY; without even the implied warranty of
00018   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019   GNU General Public License for more details.
00020   
00021   You should have received a copy of the GNU General Public License
00022   along with this program; if not, write to the Free Software
00023   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00024 
00025 */
00026 
00027 #include <cassert>
00028 #include <cstdio>
00029 
00030 
00031 #ifndef _TIMER_H_
00032 #define _TIMER_H_
00033 
00050 #ifdef __APPLE__
00051 #include <sys/time.h>
00052 #endif
00053 
00054 #if defined(linux) && defined(__GNUG__) && defined(__i386__)
00055 
00056 #include <cstdio>
00057 #include <limits.h>
00058 #include <time.h>
00059 #include <unistd.h>
00060 #include <fcntl.h>
00061 #include <string.h>
00062 
00063 static void getTime (unsigned long& tlo, unsigned long& thi) {
00064   asm volatile ("rdtsc"
00065                 : "=a"(tlo),
00066                 "=d" (thi));
00067 }
00068 
00069 
00070 static double getFrequency (void) {
00071   static double freq = 0.0;
00072   static bool initialized = false;
00073   unsigned long LTime0, LTime1, HTime0, HTime1;
00074   if (!initialized) { 
00075 
00076     // Compute MHz directly.
00077     // Wait for approximately one second.
00078     
00079     getTime (LTime0, HTime0);
00080     //    printf ("waiting...\n");
00081     sleep (1);
00082     // printf ("done.\n");
00083     getTime (LTime1, HTime1);
00084 
00085     freq = (double)(LTime1 - LTime0) + (double)(UINT_MAX)*(double)(HTime1 - HTime0);
00086     if (LTime1 < LTime0) {
00087       freq -= (double)UINT_MAX;
00088     }
00089     initialized = true;
00090 
00091   } else {
00092     // printf ("wha?\n");
00093   }
00094   return freq;
00095 }
00096 
00097 
00098 namespace HL {
00099 
00100 class Timer {
00101 public:
00102   Timer (void)
00103     : timeElapsed (0.0)
00104   {
00105     _frequency = getFrequency();
00106     //    printf ("wooo!\n");
00107     //  printf ("freq = %lf\n", frequency);
00108   }
00109   void start (void) {
00110     getTime (currentLo, currentHi);
00111   }
00112   void stop (void) {
00113     unsigned long lo, hi;
00114     getTime (lo, hi);
00115     double now = (double) hi * 4294967296.0 + lo;
00116     double prev = (double) currentHi * 4294967296.0 + currentLo;
00117     timeElapsed = (now - prev) / _frequency;
00118   }
00119 
00120   operator double (void) {
00121     return timeElapsed;
00122   }
00123 
00124 private:
00125   double timeElapsed;
00126   unsigned long currentLo, currentHi;
00127   double _frequency;
00128 };
00129 
00130 };
00131 
00132 #else
00133 
00134 
00135 #ifdef __SVR4 // Solaris
00136 #include <sys/time.h>
00137 #include <unistd.h>
00138 #include <fcntl.h>
00139 #include <sys/procfs.h>
00140 #include <cstdio>
00141 #endif // __SVR4
00142 
00143 #include <time.h>
00144 
00145 #if defined(unix) || defined(__linux)
00146 #include <sys/time.h>
00147 #include <unistd.h>
00148 #endif
00149 
00150 
00151 #ifdef __sgi
00152 #include <sys/types.h>
00153 #include <sys/times.h>
00154 #include <limits.h>
00155 #endif
00156 
00157 
00158 #if defined(_WIN32)
00159 #include <windows.h>
00160 #endif
00161 
00162 
00163 #if defined(__BEOS__)
00164 #include <OS.h>
00165 #endif
00166 
00167 
00168 namespace HL {
00169 
00170 class Timer {
00171 
00172 public:
00173 
00175   Timer (void)
00176 #if !defined(_WIN32)
00177     : _starttime (0),
00178       _elapsedtime (0)
00179 #endif
00180   {
00181   }
00182 
00184   void start (void) { _starttime = _time(); }
00185 
00187   void stop (void) { _elapsedtime += _time() - _starttime; }
00188 
00190   void reset (void) { _starttime = _elapsedtime; }
00191 
00192 #if 0
00193   // Set the timer.
00194   void set (double secs) { _starttime = 0; _elapsedtime = _sectotime (secs);}
00195 #endif
00196 
00198   operator double (void) { return _timetosec (_elapsedtime); }
00199 
00200   static double currentTime (void) { TimeType t; t = _time(); return _timetosec (t); }
00201 
00202 
00203 private:
00204 
00205   // The _timer variable will be different depending on the OS.
00206   // We try to use the best timer available.
00207 
00208 #ifdef __sgi
00209 #define TIMER_FOUND
00210 
00211   long _starttime, _elapsedtime;
00212 
00213   long _time (void) {
00214     struct tms t;
00215     long ticks = times (&t);
00216     return ticks;
00217   }
00218 
00219   static double _timetosec (long t) {
00220     return ((double) (t) / CLK_TCK);
00221   }
00222 
00223   static long _sectotime (double sec) {
00224     return (long) sec * CLK_TCK;
00225   }
00226 #endif
00227 
00228 #ifdef __SVR4 // Solaris
00229 #define TIMER_FOUND
00230   typedef hrtime_t TimeType;
00231   TimeType      _starttime, _elapsedtime;
00232 
00233   static TimeType _time (void) {
00234     return gethrtime();
00235   }
00236 
00237   static TimeType _sectotime (double sec) { return (hrtime_t) (sec * 1.0e9); }
00238 
00239   static double _timetosec (TimeType& t) {
00240     return ((double) (t) / 1.0e9);
00241   }
00242 #endif // __SVR4
00243 
00244 #if defined(MAC) || defined(macintosh)
00245 #define TIMER_FOUND
00246   double                _starttime, _elapsedtime;
00247 
00248   double _time (void) {
00249     return get_Mac_microseconds();
00250   }
00251 
00252   double _timetosec (hrtime_t& t) {
00253     return t;
00254   }
00255 #endif // MAC
00256 
00257 #ifdef _WIN32
00258 #define TIMER_FOUND
00259 
00260 #ifndef __GNUC__
00261   class TimeType {
00262   public:
00263     TimeType (void)
00264     {
00265       largeInt.QuadPart = 0;
00266     }
00267     operator double& (void) { return (double&) largeInt.QuadPart; }
00268     operator LARGE_INTEGER& (void) { return largeInt; }
00269     double timeToSec (void) {
00270       return (double) largeInt.QuadPart / getFreq();
00271     }
00272   private:
00273     double getFreq (void) {
00274       QueryPerformanceFrequency (&freq);
00275       return (double) freq.QuadPart;
00276     }
00277 
00278     LARGE_INTEGER largeInt;
00279     LARGE_INTEGER freq;
00280   };
00281 
00282   TimeType _starttime, _elapsedtime;
00283 
00284   static TimeType _time (void) {
00285     TimeType t;
00286     int r = QueryPerformanceCounter (&((LARGE_INTEGER&) t));
00287     assert (r);
00288     return t;
00289   }
00290 
00291   static double _timetosec (TimeType& t) {
00292     return t.timeToSec();
00293   }
00294 #else
00295   typedef DWORD TimeType;
00296   DWORD _starttime, _elapsedtime;
00297   static DWORD _time (void) {
00298     return GetTickCount();
00299   }
00300 
00301   static double _timetosec (DWORD& t) {
00302     return (double) t / 100000.0;
00303   }
00304   static unsigned long _sectotime (double sec) {
00305     return (unsigned long)(sec);
00306   }
00307 #endif
00308 #endif // _WIN32
00309 
00310 
00311 #ifdef __BEOS__
00312 #define TIMER_FOUND
00313   bigtime_t _starttime, _elapsedtime;
00314   bigtime_t _time(void) {
00315     return system_time();
00316   }
00317   double _timetosec (bigtime_t& t) {
00318     return (double) t / 1000000.0;
00319   }
00320   
00321   bigtime_t _sectotime (double sec) {
00322     return (bigtime_t)(sec * 1000000.0);
00323   }
00324 #endif // __BEOS__
00325 
00326 #ifndef TIMER_FOUND
00327 
00328   typedef long TimeType;
00329   TimeType _starttime, _elapsedtime;
00330 
00331   static TimeType _time (void) {
00332     struct timeval t;
00333     gettimeofday (&t, NULL);
00334     return t.tv_sec * 1000000 + t.tv_usec;
00335   }
00336 
00337   static double _timetosec (TimeType t) {
00338     return ((double) (t) / 1000000.0);
00339   }
00340 
00341   static TimeType _sectotime (double sec) {
00342     return (TimeType) (sec * 1000000.0);
00343   }
00344 
00345 #endif // TIMER_FOUND
00346 
00347 #undef TIMER_FOUND
00348 
00349 };
00350 
00351 
00352 #ifdef __SVR4 // Solaris
00353 class VirtualTimer : public Timer {
00354 public:
00355   hrtime_t _time (void) {
00356     return gethrvtime();
00357   }
00358 };  
00359 #endif
00360 
00361 };
00362 
00363 #endif
00364 
00365 #endif
 Todo Clases Namespaces Archivos Funciones Variables 'typedefs' Enumeraciones Valores de enumeraciones Propiedades Amigas 'defines'