-
Notifications
You must be signed in to change notification settings - Fork 2
/
invtsc.c
91 lines (69 loc) · 1.66 KB
/
invtsc.c
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
/*
* TSC vs gettimeofday benchmark
*
* Copyright (c) 2017 Alexander Graf
*
* SPDX-License-Identifier: GPL-2.0+
*/
#include <assert.h>
#include <stdio.h>
#include <stdint.h>
#include <sys/time.h>
#include <unistd.h>
#include "tsc.h"
static void benchmark_gettimeofday_fast(void)
{
struct timeval tv_start;
struct timeval tv_end;
struct timeval tv;
uint64_t loops = 0;
assert(!gettimeofday(&tv_start, NULL));
tv_end = tv_start;
tv_end.tv_sec += 1;
while(1) {
assert(!gettimeofday(&tv, NULL));
if ((tv.tv_sec >= tv_end.tv_sec) && (tv.tv_usec >= tv_end.tv_usec))
break;
loops++;
}
printf("Timer loops finished with optimized gettimeofday(): %ld\n", loops);
}
//////////
static uint64_t get_us_gettimeofday(void)
{
struct timeval tv;
assert(!gettimeofday(&tv, NULL));
return tv.tv_sec * 1000000ULL + tv.tv_usec;
}
static void benchmark_gettimeofday(void)
{
uint64_t us_start;
uint64_t us_end;
uint64_t loops = 0;
us_start = get_us_gettimeofday();
us_end = us_start + 1000000ULL;
while(get_us_gettimeofday() < us_end)
loops++;
printf("Timer loops finished with gettimeofday(): %ld\n", loops);
}
//////////
static void benchmark_tsc(void)
{
uint64_t tsc_start;
uint64_t tsc_end;
uint64_t loops = 0;
tsc_start = rdtsc();
tsc_end = tsc_start + tsc_per_sec;
while(rdtsc() < tsc_end)
loops++;
printf("Timer loops finished with TSC: %ld\n", loops);
}
//////////
int main(int argc, char **argv)
{
benchmark_gettimeofday();
benchmark_gettimeofday_fast();
setup_tsc();
benchmark_tsc();
return 0;
}