forked from richardcochran/linuxptp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sysoff.c
116 lines (100 loc) · 2.95 KB
/
sysoff.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
112
113
114
115
116
/**
* @file sysoff.c
* @brief Implements the system offset estimation method.
* @note Copyright (C) 2012 Richard Cochran <[email protected]>
*
* 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.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <stdio.h>
#include <sys/ioctl.h>
#include <linux/ptp_clock.h>
#include "sysoff.h"
#define NS_PER_SEC 1000000000LL
#ifdef PTP_SYS_OFFSET
static int64_t pctns(struct ptp_clock_time *t)
{
return t->sec * NS_PER_SEC + t->nsec;
}
static struct {
int64_t interval;
int64_t offset;
uint64_t timestamp;
} samples[PTP_MAX_SAMPLES];
static void insertion_sort(int length, int64_t interval, int64_t offset, uint64_t ts)
{
int i = length - 1;
while (i >= 0) {
if (samples[i].interval < interval)
break;
samples[i+1] = samples[i];
i--;
}
samples[i+1].interval = interval;
samples[i+1].offset = offset;
samples[i+1].timestamp = ts;
}
static int64_t sysoff_estimate(struct ptp_clock_time *pct, int n_samples,
uint64_t *ts, int64_t *delay)
{
int64_t t1, t2, tp;
int64_t interval, offset;
int i;
for (i = 0; i < n_samples; i++) {
t1 = pctns(&pct[2*i]);
tp = pctns(&pct[2*i+1]);
t2 = pctns(&pct[2*i+2]);
interval = t2 - t1;
offset = (t2 + t1) / 2 - tp;
insertion_sort(i, interval, offset, (t2 + t1) / 2);
}
*ts = samples[0].timestamp;
*delay = samples[0].interval;
return samples[0].offset;
}
int sysoff_measure(int fd, int n_samples,
int64_t *result, uint64_t *ts, int64_t *delay)
{
struct ptp_sys_offset pso;
pso.n_samples = n_samples;
if (ioctl(fd, PTP_SYS_OFFSET, &pso)) {
perror("ioctl PTP_SYS_OFFSET");
return SYSOFF_RUN_TIME_MISSING;
}
*result = sysoff_estimate(pso.ts, n_samples, ts, delay);
return SYSOFF_SUPPORTED;
}
int sysoff_probe(int fd, int n_samples)
{
int64_t junk, delay;
uint64_t ts;
if (n_samples > PTP_MAX_SAMPLES) {
fprintf(stderr, "warning: %d exceeds kernel max readings %d\n",
n_samples, PTP_MAX_SAMPLES);
fprintf(stderr, "falling back to clock_gettime method\n");
return SYSOFF_RUN_TIME_MISSING;
}
return sysoff_measure(fd, n_samples, &junk, &ts, &delay);
}
#else /* !PTP_SYS_OFFSET */
int sysoff_measure(int fd, int n_samples,
int64_t *result, uint64_t *ts, int64_t *delay)
{
return SYSOFF_COMPILE_TIME_MISSING;
}
int sysoff_probe(int fd, int n_samples)
{
return SYSOFF_COMPILE_TIME_MISSING;
}
#endif /* PTP_SYS_OFFSET */