-
Notifications
You must be signed in to change notification settings - Fork 4
/
timestamp.c
111 lines (91 loc) · 2.72 KB
/
timestamp.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <sys/select.h>
#include "timestamp.h"
int64_t
utime_now (void)
{
struct timeval tv;
gettimeofday (&tv, NULL);
return (int64_t) tv.tv_sec * 1000000 + tv.tv_usec;
}
static int64_t
timestamp_seconds (int64_t v)
{
return v/1000000;
}
static int64_t
timestamp_useconds (int64_t v)
{
return v%1000000;
}
void
utime_to_timeval (int64_t v, struct timeval *tv)
{
tv->tv_sec = timestamp_seconds (v);
tv->tv_usec = timestamp_useconds (v);
}
void
utime_to_timespec (int64_t v, struct timespec *ts)
{
ts->tv_sec = timestamp_seconds (v);
ts->tv_nsec = timestamp_useconds (v) * 1000;
}
timestamp_sync_state_t *
timestamp_sync_init (double dev_ticks_per_second, int64_t dev_ticks_wraparound, double rate)
{
timestamp_sync_state_t *s = calloc (1, sizeof(*s));
if (!s)
return NULL;
s->dev_ticks_per_second = dev_ticks_per_second;
s->dev_ticks_wraparound = dev_ticks_wraparound;
s->max_rate_error = rate;
s->is_valid = 0;
return s;
}
void
timestamp_sync_free (timestamp_sync_state_t *s)
{
free (s);
}
int64_t
timestamp_sync (timestamp_sync_state_t *s, int64_t dev_ticks, int64_t host_utime)
{
if (!s->is_valid) {
/* The first sync has no history */
s->is_valid = 1;
s->sync_host_time = host_utime;
s->last_dev_ticks = dev_ticks;
s->dev_ticks_since_sync = 0;
return host_utime;
}
// how many device ticks since the last invocation?
int64_t dticks = dev_ticks - s->last_dev_ticks;
s->last_dev_ticks = dev_ticks;
if (dticks < 0)
dticks += s->dev_ticks_wraparound;
s->dev_ticks_since_sync += dticks;
// overestimate device time by a factor of s->rate
double rate = 1000000.0 / s->dev_ticks_per_second * s->max_rate_error;
// estimate of the host's time corresponding to the device's time
int64_t dev_utime = s->sync_host_time + (s->dev_ticks_since_sync * rate);
int64_t time_err = host_utime - dev_utime;
/* If time_err is very large, resynchronize, emitting a warning. if
* it is negative, we're just adjusting our timebase (it means
* we got a nice new low-latency measurement.) */
if (time_err > 1000000000LL) { /* 1000 seconds */
fprintf (stderr, "Warning: Time sync has drifted by more than 1000 seconds\n");
s->sync_host_time = host_utime;
s->dev_ticks_since_sync = 0;
dev_utime = host_utime;
}
if (time_err < 0) {
s->sync_host_time = host_utime;
s->dev_ticks_since_sync = 0;
dev_utime = host_utime;
}
//printf ("%llu %llu %lld %lld\n", host_utime, dev_utime, time_err, dev_utime + s->time_offset);
return dev_utime;
}