aboutsummaryrefslogtreecommitdiff
path: root/src/benchmarks/falsesharing/timer.h
blob: d4d42c795dbff264f65c16f0e1e6fb18d5077fbb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/* -*- C++ -*- */

/*

  Heap Layers: An Extensible Memory Allocation Infrastructure
  
  Copyright (C) 2000-2003 by Emery Berger
  http://www.cs.umass.edu/~emery
  emery@cs.umass.edu
  
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.
  
  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  
  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*/

#include <cassert>
#include <stdio.h>


#ifndef _TIMER_H_
#define _TIMER_H_

/**
 * @class Timer
 * @brief A portable class for high-resolution timing.
 *
 * This class simplifies timing measurements across a number of platforms.
 * 
 * @code
 *  Timer t;
 *  t.start();
 *  // do some work
 *  t.stop();
 *  cout << "That took " << (double) t << " seconds." << endl;
 * @endcode
 *
 */

#ifdef __APPLE__
#include <sys/time.h>
#endif

#if defined(__linux__) && defined(__GNUG__) && defined(__i386__)

#include <stdio.h>
#include <limits.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>

static void getTime (unsigned long& tlo, unsigned long& thi) {
  asm volatile ("rdtsc"
		: "=a"(tlo),
		"=d" (thi));
}


static double getFrequency (void) {
  static double freq = 0.0;
  static bool initialized = false;
  unsigned long LTime0, LTime1, HTime0, HTime1;
  if (!initialized) { 

    freq = 2600000.0;

#if 0
    // Compute MHz directly.
    // Wait for approximately one second.
    
    getTime (LTime0, HTime0);
    //    printf ("waiting...\n");
    struct timespec rqtp, rmtp;
    rqtp.tv_sec = 1;
    rqtp.tv_nsec = 0;
    nanosleep (&rqtp, &rmtp);
    // printf ("done.\n");
    getTime (LTime1, HTime1);

    freq = (double)(LTime1 - LTime0) + (double)(UINT_MAX)*(double)(HTime1 - HTime0);
    if (LTime1 < LTime0) {
      freq -= (double)UINT_MAX;
    }
#endif
    initialized = true;

  } else {
    // printf ("wha?\n");
  }
  return freq;
}


namespace HL {

class Timer {
public:
  Timer (void)
    : timeElapsed (0.0)
  {
    _frequency = getFrequency();
    //    printf ("wooo!\n");
    //  printf ("freq = %lf\n", frequency);
  }
  void start (void) {
    getTime (currentLo, currentHi);
  }
  void stop (void) {
    unsigned long lo, hi;
    getTime (lo, hi);
    double now = (double) hi * 4294967296.0 + lo;
    double prev = (double) currentHi * 4294967296.0 + currentLo;
    timeElapsed = (now - prev) / _frequency;
  }

  operator double (void) {
    return timeElapsed;
  }

private:
  double timeElapsed;
  unsigned long currentLo, currentHi;
  double _frequency;
};

};

#else


#ifdef __SVR4 // Solaris
#include <sys/time.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/procfs.h>
#include <stdio.h>
#endif // __SVR4

#include <time.h>

#if defined(unix) || defined(__linux)
#include <sys/time.h>
#include <unistd.h>
#endif


#ifdef __sgi
#include <sys/types.h>
#include <sys/times.h>
#include <limits.h>
#endif


#if defined(_WIN32)
#include <windows.h>
#endif


#if defined(__BEOS__)
#include <OS.h>
#endif


namespace HL {

class Timer {

public:

  /// Initializes the timer.
  Timer (void)
#if !defined(_WIN32)
    : _starttime (0),
      _elapsedtime (0)
#endif
  {
  }

  /// Start the timer.
  void start (void) { _starttime = _time(); }

  /// Stop the timer.
  void stop (void) { _elapsedtime += _time() - _starttime; }

  /// Reset the timer.
  void reset (void) { _starttime = _elapsedtime; }

#if 0
  // Set the timer.
  void set (double secs) { _starttime = 0; _elapsedtime = _sectotime (secs);}
#endif

  /// Return the number of seconds elapsed.
  operator double (void) { return _timetosec (_elapsedtime); }

  static double currentTime (void) { TimeType t; t = _time(); return _timetosec (t); }


private:

  // The _timer variable will be different depending on the OS.
  // We try to use the best timer available.

#ifdef __sgi
#define TIMER_FOUND

  long _starttime, _elapsedtime;

  long _time (void) {
    struct tms t;
    long ticks = times (&t);
    return ticks;
  }

  static double _timetosec (long t) {
    return ((double) (t) / CLK_TCK);
  }

  static long _sectotime (double sec) {
    return (long) sec * CLK_TCK;
  }
#endif

#ifdef __SVR4 // Solaris
#define TIMER_FOUND
  typedef hrtime_t TimeType;
  TimeType	_starttime, _elapsedtime;

  static TimeType _time (void) {
    return gethrtime();
  }

  static TimeType _sectotime (double sec) { return (hrtime_t) (sec * 1.0e9); }

  static double _timetosec (TimeType& t) {
    return ((double) (t) / 1.0e9);
  }
#endif // __SVR4

#if defined(MAC) || defined(macintosh)
#define TIMER_FOUND
  double		_starttime, _elapsedtime;

  double _time (void) {
    return get_Mac_microseconds();
  }

  double _timetosec (hrtime_t& t) {
    return t;
  }
#endif // MAC

#ifdef _WIN32
#define TIMER_FOUND

#ifndef __GNUC__
  class TimeType {
  public:
    TimeType (void)
    {
      largeInt.QuadPart = 0;
    }
    operator double& (void) { return (double&) largeInt.QuadPart; }
    operator LARGE_INTEGER& (void) { return largeInt; }
    double timeToSec (void) {
      return (double) largeInt.QuadPart / getFreq();
    }
  private:
    double getFreq (void) {
      QueryPerformanceFrequency (&freq);
      return (double) freq.QuadPart;
    }

    LARGE_INTEGER largeInt;
    LARGE_INTEGER freq;
  };

  TimeType _starttime, _elapsedtime;

  static TimeType _time (void) {
    TimeType t;
    int r = QueryPerformanceCounter (&((LARGE_INTEGER&) t));
    assert (r);
    return t;
  }

  static double _timetosec (TimeType& t) {
    return t.timeToSec();
  }
#else
  typedef DWORD TimeType;
  DWORD _starttime, _elapsedtime;
  static DWORD _time (void) {
    return GetTickCount();
  }

  static double _timetosec (DWORD& t) {
    return (double) t / 100000.0;
  }
  static unsigned long _sectotime (double sec) {
    return (unsigned long)(sec);
  }
#endif
#endif // _WIN32


#ifdef __BEOS__
#define TIMER_FOUND
  bigtime_t _starttime, _elapsedtime;
  bigtime_t _time(void) {
    return system_time();
  }
  double _timetosec (bigtime_t& t) {
    return (double) t / 1000000.0;
  }
  
  bigtime_t _sectotime (double sec) {
    return (bigtime_t)(sec * 1000000.0);
  }
#endif // __BEOS__

#ifndef TIMER_FOUND

  typedef long TimeType;
  TimeType _starttime, _elapsedtime;

  static TimeType _time (void) {
    struct timeval t;
    gettimeofday (&t, NULL);
    return t.tv_sec * 1000000 + t.tv_usec;
  }

  static double _timetosec (TimeType t) {
    return ((double) (t) / 1000000.0);
  }

  static TimeType _sectotime (double sec) {
    return (TimeType) (sec * 1000000.0);
  }

#endif // TIMER_FOUND

#undef TIMER_FOUND

};


#ifdef __SVR4 // Solaris
class VirtualTimer : public Timer {
public:
  hrtime_t _time (void) {
    return gethrvtime();
  }
};  
#endif

}

#endif

#endif