summaryrefslogtreecommitdiff
path: root/src/Timer.cpp
blob: b3260486dde909b1d4c210e7ceea406987d981cf (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
/*******************************************************************************
 * Copyright (c) 2006 International Business Machines Corporation.             *
 * All rights reserved. This program and the accompanying materials            *
 * are made available under the terms of the Common Public License v1.0        *
 * which accompanies this distribution, and is available at                    *
 * http://www.opensource.org/licenses/cpl1.0.php                               *
 *                                                                             *
 * Contributors:                                                               *
 *    Douglas M. Pase - initial API and implementation                         *
 *******************************************************************************/


#include <stdio.h>
#include <sys/time.h>

#include "Timer.h"

#include "Types.h"

static int64  read_rtc();
static void   calibrate_rtc(int n);
static double wall_seconds();

static int    wall_ticks   = -1;
static int    rtc_ticks    = -1;
static double wall_elapsed = -1;
static int64  rtc_elapsed  = -1;
static double time_factor  = -1;

#if !defined(RTC) && !defined(GTOD)
#define RTC
#endif

#if defined(RTC)

double
Timer::seconds()
{ 
    return (double) read_rtc() * time_factor;
}

int64
Timer::ticks()
{ 
				// See pg. 406 of the AMD x86-64 Architecture
				// Programmer's Manual, Volume 2, System Programming
    unsigned int eax=0, edx=0;

    __asm__ __volatile__(
	"rdtsc ;"
	"movl %%eax,%0;"
	"movl %%edx,%1;"
	""
    : "=r"(eax), "=r"(edx)
    :
    : "%eax", "%edx"
    );

    return ((int64) edx << 32) | (int64) eax;
}

static int64
read_rtc()
{
				// See pg. 406 of the AMD x86-64 Architecture
				// Programmer's Manual, Volume 2, System Programming
    unsigned int eax=0, edx=0;

    __asm__ __volatile__(
	"rdtsc ;"
	"movl %%eax,%0;"
	"movl %%edx,%1;"
	""
    : "=r"(eax), "=r"(edx)
    :
    : "%eax", "%edx"
    );

    return ((int64) edx << 32) | (int64) eax;
}

void
Timer::calibrate()
{
    Timer::calibrate(1000);
}

void
Timer::calibrate(int n)
{
    wall_ticks = n;

    double wall_start,wall_finish,t;
    t = wall_seconds();
    while (t == (wall_start=wall_seconds())) {
	;
    }
    int64 rtc_start = read_rtc();
    for (int i=0; i < wall_ticks; i++) {
	t = wall_seconds();
	while (t == (wall_finish=wall_seconds())) {
	    ;
	}
    }
    int64 rtc_finish = read_rtc();

    wall_elapsed = wall_finish - wall_start;
    rtc_elapsed  = rtc_finish  - rtc_start;
    time_factor  = wall_elapsed / (double) rtc_elapsed;
}

static double
wall_seconds()
{ 
    struct timeval t;
    gettimeofday(&t, NULL);

    return (double) t.tv_sec + (double) t.tv_usec * 1E-6;
}

#else

double
Timer::seconds()
{ 
    struct timeval t;
    gettimeofday(&t, NULL);

    return (double) t.tv_sec + (double) t.tv_usec * 1E-6;
}

int64
Timer::ticks()
{ 
    struct timeval t;
    gettimeofday(&t, NULL);

    return 1000000 * (int64) t.tv_sec + (int64) t.tv_usec;
}

void
Timer::calibrate()
{
}

void
Timer::calibrate(int n)
{
}

#endif

static double
min( double v1, double v2 )
{
    if (v2 < v1) return v2;
    return v1;
}

double
Timer::resolution()
{ 
    double a,b,c=1E9;
    for (int i=0; i < 10; i++) {
	a = Timer::seconds();
	while (a == (b=Timer::seconds()))
	    ;
	a = Timer::seconds();
	while (a == (b=Timer::seconds()))
	    ;
	c = min(b - a, c);
    }

    return c;
}