[176] | 1 | /* -*- C++ -*- */ |
---|
| 2 | |
---|
| 3 | /* |
---|
| 4 | |
---|
| 5 | Heap Layers: An Extensible Memory Allocation Infrastructure |
---|
| 6 | |
---|
| 7 | Copyright (C) 2000-2003 by Emery Berger |
---|
| 8 | http://www.cs.umass.edu/~emery |
---|
| 9 | emery@cs.umass.edu |
---|
| 10 | |
---|
| 11 | This program is free software; you can redistribute it and/or modify |
---|
| 12 | it under the terms of the GNU General Public License as published by |
---|
| 13 | the Free Software Foundation; either version 2 of the License, or |
---|
| 14 | (at your option) any later version. |
---|
| 15 | |
---|
| 16 | This program is distributed in the hope that it will be useful, |
---|
| 17 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 19 | GNU General Public License for more details. |
---|
| 20 | |
---|
| 21 | You should have received a copy of the GNU General Public License |
---|
| 22 | along with this program; if not, write to the Free Software |
---|
| 23 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
| 24 | |
---|
| 25 | */ |
---|
| 26 | |
---|
| 27 | #include <assert.h> |
---|
| 28 | #include <stdio.h> |
---|
| 29 | |
---|
| 30 | |
---|
| 31 | #ifndef _TIMER_H_ |
---|
| 32 | #define _TIMER_H_ |
---|
| 33 | |
---|
| 34 | /** |
---|
| 35 | * @class Timer |
---|
| 36 | * @brief A portable class for high-resolution timing. |
---|
| 37 | * |
---|
| 38 | * This class simplifies timing measurements across a number of platforms. |
---|
| 39 | * |
---|
| 40 | * @code |
---|
| 41 | * Timer t; |
---|
| 42 | * t.start(); |
---|
| 43 | * // do some work |
---|
| 44 | * t.stop(); |
---|
| 45 | * cout << "That took " << (double) t << " seconds." << endl; |
---|
| 46 | * @endcode |
---|
| 47 | * |
---|
| 48 | */ |
---|
| 49 | |
---|
| 50 | #ifdef __APPLE__ |
---|
| 51 | #include <sys/time.h> |
---|
| 52 | #endif |
---|
| 53 | |
---|
| 54 | #if defined(linux) && defined(__GNUG__) && defined(__i386__) |
---|
| 55 | |
---|
| 56 | #include <stdio.h> |
---|
| 57 | #include <limits.h> |
---|
| 58 | #include <time.h> |
---|
| 59 | #include <unistd.h> |
---|
| 60 | #include <fcntl.h> |
---|
| 61 | #include <string.h> |
---|
| 62 | |
---|
| 63 | static void getTime (unsigned long& tlo, unsigned long& thi) { |
---|
| 64 | asm volatile ("rdtsc" |
---|
| 65 | : "=a"(tlo), |
---|
| 66 | "=d" (thi)); |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | |
---|
| 70 | static double getFrequency (void) { |
---|
| 71 | static double freq = 0.0; |
---|
| 72 | static bool initialized = false; |
---|
| 73 | unsigned long LTime0, LTime1, HTime0, HTime1; |
---|
| 74 | if (!initialized) { |
---|
| 75 | |
---|
| 76 | // Compute MHz directly. |
---|
| 77 | // Wait for approximately one second. |
---|
| 78 | |
---|
| 79 | getTime (LTime0, HTime0); |
---|
| 80 | // printf ("waiting...\n"); |
---|
| 81 | sleep (1); |
---|
| 82 | // printf ("done.\n"); |
---|
| 83 | getTime (LTime1, HTime1); |
---|
| 84 | |
---|
| 85 | freq = (double)(LTime1 - LTime0) + (double)(UINT_MAX)*(double)(HTime1 - HTime0); |
---|
| 86 | if (LTime1 < LTime0) { |
---|
| 87 | freq -= (double)UINT_MAX; |
---|
| 88 | } |
---|
| 89 | initialized = true; |
---|
| 90 | |
---|
| 91 | } else { |
---|
| 92 | // printf ("wha?\n"); |
---|
| 93 | } |
---|
| 94 | return freq; |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | // FIX ME |
---|
| 98 | #include <iostream> |
---|
| 99 | using namespace std; |
---|
| 100 | |
---|
| 101 | namespace HL { |
---|
| 102 | |
---|
| 103 | class Timer { |
---|
| 104 | public: |
---|
| 105 | Timer (void) |
---|
| 106 | : timeElapsed (0.0) |
---|
| 107 | { |
---|
| 108 | _frequency = getFrequency(); |
---|
| 109 | // printf ("wooo!\n"); |
---|
| 110 | // printf ("freq = %lf\n", frequency); |
---|
| 111 | } |
---|
| 112 | void start (void) { |
---|
| 113 | getTime (currentLo, currentHi); |
---|
| 114 | } |
---|
| 115 | void stop (void) { |
---|
| 116 | unsigned long lo, hi; |
---|
| 117 | getTime (lo, hi); |
---|
| 118 | double now = (double) hi * 4294967296.0 + lo; |
---|
| 119 | double prev = (double) currentHi * 4294967296.0 + currentLo; |
---|
| 120 | timeElapsed = (now - prev) / _frequency; |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | operator double (void) { |
---|
| 124 | return timeElapsed; |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | private: |
---|
| 128 | double timeElapsed; |
---|
| 129 | unsigned long currentLo, currentHi; |
---|
| 130 | double _frequency; |
---|
| 131 | }; |
---|
| 132 | |
---|
| 133 | }; |
---|
| 134 | |
---|
| 135 | #else |
---|
| 136 | |
---|
| 137 | |
---|
| 138 | #ifdef __SVR4 // Solaris |
---|
| 139 | #include <sys/time.h> |
---|
| 140 | #include <unistd.h> |
---|
| 141 | #include <fcntl.h> |
---|
| 142 | #include <sys/procfs.h> |
---|
| 143 | #include <stdio.h> |
---|
| 144 | #endif // __SVR4 |
---|
| 145 | |
---|
| 146 | #include <time.h> |
---|
| 147 | |
---|
| 148 | #if defined(unix) || defined(__linux) |
---|
| 149 | #include <sys/time.h> |
---|
| 150 | #include <unistd.h> |
---|
| 151 | #endif |
---|
| 152 | |
---|
| 153 | |
---|
| 154 | #ifdef __sgi |
---|
| 155 | #include <sys/types.h> |
---|
| 156 | #include <sys/times.h> |
---|
| 157 | #include <limits.h> |
---|
| 158 | #endif |
---|
| 159 | |
---|
| 160 | |
---|
| 161 | #if defined(_WIN32) |
---|
| 162 | #include <windows.h> |
---|
| 163 | #endif |
---|
| 164 | |
---|
| 165 | |
---|
| 166 | #if defined(__BEOS__) |
---|
| 167 | #include <OS.h> |
---|
| 168 | #endif |
---|
| 169 | |
---|
| 170 | |
---|
| 171 | namespace HL { |
---|
| 172 | |
---|
| 173 | class Timer { |
---|
| 174 | |
---|
| 175 | public: |
---|
| 176 | |
---|
| 177 | /// Initializes the timer. |
---|
| 178 | Timer (void) |
---|
| 179 | #if !defined(_WIN32) |
---|
| 180 | : _starttime (0), |
---|
| 181 | _elapsedtime (0) |
---|
| 182 | #endif |
---|
| 183 | { |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | /// Start the timer. |
---|
| 187 | void start (void) { _starttime = _time(); } |
---|
| 188 | |
---|
| 189 | /// Stop the timer. |
---|
| 190 | void stop (void) { _elapsedtime += _time() - _starttime; } |
---|
| 191 | |
---|
| 192 | /// Reset the timer. |
---|
| 193 | void reset (void) { _starttime = _elapsedtime; } |
---|
| 194 | |
---|
| 195 | #if 0 |
---|
| 196 | // Set the timer. |
---|
| 197 | void set (double secs) { _starttime = 0; _elapsedtime = _sectotime (secs);} |
---|
| 198 | #endif |
---|
| 199 | |
---|
| 200 | /// Return the number of seconds elapsed. |
---|
| 201 | operator double (void) { return _timetosec (_elapsedtime); } |
---|
| 202 | |
---|
| 203 | static double currentTime (void) { TimeType t; t = _time(); return _timetosec (t); } |
---|
| 204 | |
---|
| 205 | |
---|
| 206 | private: |
---|
| 207 | |
---|
| 208 | // The _timer variable will be different depending on the OS. |
---|
| 209 | // We try to use the best timer available. |
---|
| 210 | |
---|
| 211 | #ifdef __sgi |
---|
| 212 | #define TIMER_FOUND |
---|
| 213 | |
---|
| 214 | long _starttime, _elapsedtime; |
---|
| 215 | |
---|
| 216 | long _time (void) { |
---|
| 217 | struct tms t; |
---|
| 218 | long ticks = times (&t); |
---|
| 219 | return ticks; |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | static double _timetosec (long t) { |
---|
| 223 | return ((double) (t) / CLK_TCK); |
---|
| 224 | } |
---|
| 225 | |
---|
| 226 | static long _sectotime (double sec) { |
---|
| 227 | return (long) sec * CLK_TCK; |
---|
| 228 | } |
---|
| 229 | #endif |
---|
| 230 | |
---|
| 231 | #ifdef __SVR4 // Solaris |
---|
| 232 | #define TIMER_FOUND |
---|
| 233 | typedef hrtime_t TimeType; |
---|
| 234 | TimeType _starttime, _elapsedtime; |
---|
| 235 | |
---|
| 236 | static TimeType _time (void) { |
---|
| 237 | return gethrtime(); |
---|
| 238 | } |
---|
| 239 | |
---|
| 240 | static TimeType _sectotime (double sec) { return (hrtime_t) (sec * 1.0e9); } |
---|
| 241 | |
---|
| 242 | static double _timetosec (TimeType& t) { |
---|
| 243 | return ((double) (t) / 1.0e9); |
---|
| 244 | } |
---|
| 245 | #endif // __SVR4 |
---|
| 246 | |
---|
| 247 | #if defined(MAC) || defined(macintosh) |
---|
| 248 | #define TIMER_FOUND |
---|
| 249 | double _starttime, _elapsedtime; |
---|
| 250 | |
---|
| 251 | double _time (void) { |
---|
| 252 | return get_Mac_microseconds(); |
---|
| 253 | } |
---|
| 254 | |
---|
| 255 | double _timetosec (hrtime_t& t) { |
---|
| 256 | return t; |
---|
| 257 | } |
---|
| 258 | #endif // MAC |
---|
| 259 | |
---|
| 260 | #ifdef _WIN32 |
---|
| 261 | #define TIMER_FOUND |
---|
| 262 | |
---|
| 263 | #ifndef __GNUC__ |
---|
| 264 | class TimeType { |
---|
| 265 | public: |
---|
| 266 | TimeType (void) |
---|
| 267 | { |
---|
| 268 | largeInt.QuadPart = 0; |
---|
| 269 | } |
---|
| 270 | operator double& (void) { return (double&) largeInt.QuadPart; } |
---|
| 271 | operator LARGE_INTEGER& (void) { return largeInt; } |
---|
| 272 | double timeToSec (void) { |
---|
| 273 | return (double) largeInt.QuadPart / getFreq(); |
---|
| 274 | } |
---|
| 275 | private: |
---|
| 276 | double getFreq (void) { |
---|
| 277 | QueryPerformanceFrequency (&freq); |
---|
| 278 | return (double) freq.QuadPart; |
---|
| 279 | } |
---|
| 280 | |
---|
| 281 | LARGE_INTEGER largeInt; |
---|
| 282 | LARGE_INTEGER freq; |
---|
| 283 | }; |
---|
| 284 | |
---|
| 285 | TimeType _starttime, _elapsedtime; |
---|
| 286 | |
---|
| 287 | static TimeType _time (void) { |
---|
| 288 | TimeType t; |
---|
| 289 | int r = QueryPerformanceCounter (&((LARGE_INTEGER&) t)); |
---|
| 290 | assert (r); |
---|
| 291 | return t; |
---|
| 292 | } |
---|
| 293 | |
---|
| 294 | static double _timetosec (TimeType& t) { |
---|
| 295 | return t.timeToSec(); |
---|
| 296 | } |
---|
| 297 | #else |
---|
| 298 | typedef DWORD TimeType; |
---|
| 299 | DWORD _starttime, _elapsedtime; |
---|
| 300 | static DWORD _time (void) { |
---|
| 301 | return GetTickCount(); |
---|
| 302 | } |
---|
| 303 | |
---|
| 304 | static double _timetosec (DWORD& t) { |
---|
| 305 | return (double) t / 100000.0; |
---|
| 306 | } |
---|
| 307 | static unsigned long _sectotime (double sec) { |
---|
| 308 | return (unsigned long)(sec); |
---|
| 309 | } |
---|
| 310 | #endif |
---|
| 311 | #endif // _WIN32 |
---|
| 312 | |
---|
| 313 | |
---|
| 314 | #ifdef __BEOS__ |
---|
| 315 | #define TIMER_FOUND |
---|
| 316 | bigtime_t _starttime, _elapsedtime; |
---|
| 317 | bigtime_t _time(void) { |
---|
| 318 | return system_time(); |
---|
| 319 | } |
---|
| 320 | double _timetosec (bigtime_t& t) { |
---|
| 321 | return (double) t / 1000000.0; |
---|
| 322 | } |
---|
| 323 | |
---|
| 324 | bigtime_t _sectotime (double sec) { |
---|
| 325 | return (bigtime_t)(sec * 1000000.0); |
---|
| 326 | } |
---|
| 327 | #endif // __BEOS__ |
---|
| 328 | |
---|
| 329 | #ifndef TIMER_FOUND |
---|
| 330 | |
---|
| 331 | typedef long TimeType; |
---|
| 332 | TimeType _starttime, _elapsedtime; |
---|
| 333 | |
---|
| 334 | static TimeType _time (void) { |
---|
| 335 | struct timeval t; |
---|
| 336 | gettimeofday (&t, NULL); |
---|
| 337 | return t.tv_sec * 1000000 + t.tv_usec; |
---|
| 338 | } |
---|
| 339 | |
---|
| 340 | static double _timetosec (TimeType t) { |
---|
| 341 | return ((double) (t) / 1000000.0); |
---|
| 342 | } |
---|
| 343 | |
---|
| 344 | static TimeType _sectotime (double sec) { |
---|
| 345 | return (TimeType) (sec * 1000000.0); |
---|
| 346 | } |
---|
| 347 | |
---|
| 348 | #endif // TIMER_FOUND |
---|
| 349 | |
---|
| 350 | #undef TIMER_FOUND |
---|
| 351 | |
---|
| 352 | }; |
---|
| 353 | |
---|
| 354 | |
---|
| 355 | #ifdef __SVR4 // Solaris |
---|
| 356 | class VirtualTimer : public Timer { |
---|
| 357 | public: |
---|
| 358 | hrtime_t _time (void) { |
---|
| 359 | return gethrvtime(); |
---|
| 360 | } |
---|
| 361 | }; |
---|
| 362 | #endif |
---|
| 363 | |
---|
| 364 | }; |
---|
| 365 | |
---|
| 366 | #endif |
---|
| 367 | |
---|
| 368 | #endif |
---|