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