-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtimer.c
More file actions
150 lines (138 loc) · 3.06 KB
/
timer.c
File metadata and controls
150 lines (138 loc) · 3.06 KB
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
#include <stdio.h>
#include <time.h>
#include <sys/time.h>
#include <sys/types.h>
#include "llcbench.h"
#if defined(__i386__)
#define RDTSC_X86
#define USE_RDTSC
#elif defined(__x86_64__)
#define RDTSC_AMD64
#define USE_RDTSC
#endif
#if defined(USE_RDTSC)
# if defined(RDTSC_AMD64)
# define RDTSC(llptr) ({\
__asm__ __volatile__ (\
"rdtsc\n\t"\
"shlq\t$32, %%rdx\n\t"\
"orq\t%%rdx, %%rax\n\t"\
: "=A" (llptr)\
: /* no input vars */\
: "%rdx"\
);\
}\
)
# elif defined(RDTSC_X86)
# define RDTSC(llptr) ({\
__asm__ __volatile__ (\
"rdtsc\n\t"\
: "=A" (llptr)\
);\
}\
)
# else
# error "Need to define RDTSC_AMD64 or RDTSC_X86 with USE_RDTSC."
# endif
static unsigned long long cycles_per_second()
{
double seconds;
unsigned long long starttick, endtick;
unsigned long long starttime, endtime;
struct timeval tv;
gettimeofday(&tv, 0);
starttime = tv.tv_sec*1000 + tv.tv_usec/1000;
while ( 1 )
{
gettimeofday(&tv, 0);
endtime = tv.tv_sec*1000 + tv.tv_usec/1000;
if ( endtime != starttime )
{
break;
}
}
while ( 1 )
{
gettimeofday(&tv, 0);
endtime = tv.tv_sec*1000 + tv.tv_usec/1000;
if ( (endtime - starttime) > 1 )
{
RDTSC(starttick);
break;
}
}
starttime = endtime;
while ( 1 )
{
gettimeofday(&tv, 0);
endtime = tv.tv_sec*1000 + tv.tv_usec/1000;
if ( (endtime - starttime) > 1000 )
{
RDTSC(endtick);
break;
}
}
return (endtick - starttick);
}
static unsigned long long cps = 0;
#endif
static double t1, t2;
void timer_start(void)
{
#if defined(USE_GETTIMEOFDAY)
{
struct timeval ts;
gettimeofday(&ts, (struct timezone*)0);
t1 = (double)ts.tv_sec*1000000000.0 + (double)ts.tv_usec*1000.0;
}
#elif defined(USE_RDTSC)
{
unsigned long long ticks;
if ( cps == 0 )
cps = cycles_per_second();
RDTSC(ticks);
t1 = (ticks / (double)cps) * 1.0e9;
}
#else
{
struct timespec ts;
clock_gettime(CLOCK_REALTIME,&ts);
t1 = (double)ts.tv_sec*1000000000.0 + (double)ts.tv_nsec;
}
#endif
DBG(printf("START %f\n",t1))
}
void timer_stop(void)
{
#if defined(USE_GETTIMEOFDAY)
{
struct timeval ts;
gettimeofday(&ts, (struct timezone*)0);
t2 = (double)ts.tv_sec*1000000000.0 + (double)ts.tv_usec*1000.0;
}
#elif defined(USE_RDTSC)
{
unsigned long long ticks;
if ( cps == 0 )
cps = cycles_per_second(); /* probably not necessary, but safe */
RDTSC(ticks);
t2 = (ticks / (double)cps) * 1.0e9;
}
#else
{
struct timespec ts;
clock_gettime(CLOCK_REALTIME,&ts);
t2 = (double)ts.tv_sec*1000000000.0 + (double)ts.tv_nsec;
}
#endif
DBG(printf("STOP %f\n",t2))
}
double timer_elapsed(void)
{
if (t2-t1 <= 0.0)
{
fprintf(stderr,"Warning! The timer is not precise enough. Consider increasing\nthe iteration count or changing the timer in timer.c\n");
return(0.0);
}
return((t2-t1)/1000.0);
}