2012-11-26 20:22:07 +08:00
|
|
|
/**
|
|
|
|
* @file sysoff.c
|
|
|
|
* @brief Implements the system offset estimation method.
|
|
|
|
* @note Copyright (C) 2012 Richard Cochran <richardcochran@gmail.com>
|
|
|
|
*
|
|
|
|
* 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>
|
2018-11-13 00:27:58 +08:00
|
|
|
#include <string.h>
|
2012-11-26 20:22:07 +08:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <linux/ptp_clock.h>
|
|
|
|
|
2018-11-13 00:28:00 +08:00
|
|
|
#include "print.h"
|
2012-11-26 20:22:07 +08:00
|
|
|
#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;
|
|
|
|
}
|
|
|
|
|
2018-11-13 00:28:00 +08:00
|
|
|
static int sysoff_precise(int fd, int64_t *result, uint64_t *ts)
|
|
|
|
{
|
|
|
|
#ifdef PTP_SYS_OFFSET_PRECISE
|
|
|
|
struct ptp_sys_offset_precise pso;
|
|
|
|
memset(&pso, 0, sizeof(pso));
|
|
|
|
if (ioctl(fd, PTP_SYS_OFFSET_PRECISE, &pso)) {
|
|
|
|
pr_debug("ioctl PTP_SYS_OFFSET_PRECISE: %m");
|
|
|
|
return SYSOFF_RUN_TIME_MISSING;
|
|
|
|
}
|
|
|
|
*result = pctns(&pso.sys_realtime) - pctns(&pso.device);
|
|
|
|
*ts = pctns(&pso.sys_realtime);
|
|
|
|
return SYSOFF_PRECISE;
|
|
|
|
#else
|
|
|
|
return SYSOFF_COMPILE_TIME_MISSING;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-11-13 00:28:01 +08:00
|
|
|
static int64_t sysoff_estimate(struct ptp_clock_time *pct, int extended,
|
|
|
|
int n_samples, uint64_t *ts, int64_t *delay)
|
2012-11-26 20:22:07 +08:00
|
|
|
{
|
|
|
|
int64_t t1, t2, tp;
|
2020-06-21 17:14:45 +08:00
|
|
|
int64_t interval, timestamp, offset;
|
|
|
|
int64_t shortest_interval, best_timestamp, best_offset;
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
if (extended) {
|
|
|
|
t1 = pctns(&pct[3*i]);
|
|
|
|
tp = pctns(&pct[3*i+1]);
|
|
|
|
t2 = pctns(&pct[3*i+2]);
|
|
|
|
} else {
|
|
|
|
t1 = pctns(&pct[2*i]);
|
|
|
|
tp = pctns(&pct[2*i+1]);
|
|
|
|
t2 = pctns(&pct[2*i+2]);
|
|
|
|
}
|
|
|
|
shortest_interval = t2 - t1;
|
|
|
|
best_timestamp = (t2 + t1) / 2;
|
|
|
|
best_offset = best_timestamp - tp;
|
2012-11-26 20:22:07 +08:00
|
|
|
|
2020-06-21 17:14:45 +08:00
|
|
|
for (i = 1; i < n_samples; i++) {
|
2018-11-13 00:28:01 +08:00
|
|
|
if (extended) {
|
|
|
|
t1 = pctns(&pct[3*i]);
|
|
|
|
tp = pctns(&pct[3*i+1]);
|
|
|
|
t2 = pctns(&pct[3*i+2]);
|
|
|
|
} else {
|
|
|
|
t1 = pctns(&pct[2*i]);
|
|
|
|
tp = pctns(&pct[2*i+1]);
|
|
|
|
t2 = pctns(&pct[2*i+2]);
|
|
|
|
}
|
2012-11-26 20:22:07 +08:00
|
|
|
interval = t2 - t1;
|
2020-06-21 17:14:45 +08:00
|
|
|
timestamp = (t2 + t1) / 2;
|
|
|
|
offset = timestamp - tp;
|
|
|
|
if (interval < shortest_interval) {
|
|
|
|
shortest_interval = interval;
|
|
|
|
best_timestamp = timestamp;
|
|
|
|
best_offset = offset;
|
|
|
|
}
|
2012-11-26 20:22:07 +08:00
|
|
|
}
|
2020-06-21 17:14:45 +08:00
|
|
|
*ts = best_timestamp;
|
|
|
|
*delay = shortest_interval;
|
|
|
|
return best_offset;
|
2012-11-26 20:22:07 +08:00
|
|
|
}
|
|
|
|
|
2018-11-13 00:28:01 +08:00
|
|
|
static int sysoff_extended(int fd, int n_samples,
|
|
|
|
int64_t *result, uint64_t *ts, int64_t *delay)
|
|
|
|
{
|
|
|
|
#ifdef PTP_SYS_OFFSET_EXTENDED
|
|
|
|
struct ptp_sys_offset_extended pso;
|
|
|
|
memset(&pso, 0, sizeof(pso));
|
|
|
|
pso.n_samples = n_samples;
|
|
|
|
if (ioctl(fd, PTP_SYS_OFFSET_EXTENDED, &pso)) {
|
|
|
|
pr_debug("ioctl PTP_SYS_OFFSET_EXTENDED: %m");
|
|
|
|
return SYSOFF_RUN_TIME_MISSING;
|
|
|
|
}
|
|
|
|
*result = sysoff_estimate(&pso.ts[0][0], 1, n_samples, ts, delay);
|
|
|
|
return SYSOFF_EXTENDED;
|
|
|
|
#else
|
|
|
|
return SYSOFF_COMPILE_TIME_MISSING;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-11-13 00:27:59 +08:00
|
|
|
static int sysoff_basic(int fd, int n_samples,
|
|
|
|
int64_t *result, uint64_t *ts, int64_t *delay)
|
2012-11-26 20:22:07 +08:00
|
|
|
{
|
|
|
|
struct ptp_sys_offset pso;
|
2018-11-13 00:27:58 +08:00
|
|
|
memset(&pso, 0, sizeof(pso));
|
2012-11-26 20:22:07 +08:00
|
|
|
pso.n_samples = n_samples;
|
|
|
|
if (ioctl(fd, PTP_SYS_OFFSET, &pso)) {
|
|
|
|
perror("ioctl PTP_SYS_OFFSET");
|
|
|
|
return SYSOFF_RUN_TIME_MISSING;
|
|
|
|
}
|
2018-11-13 00:28:01 +08:00
|
|
|
*result = sysoff_estimate(pso.ts, 0, n_samples, ts, delay);
|
2018-11-13 00:27:59 +08:00
|
|
|
return SYSOFF_BASIC;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sysoff_measure(int fd, int method, int n_samples,
|
|
|
|
int64_t *result, uint64_t *ts, int64_t *delay)
|
|
|
|
{
|
|
|
|
switch (method) {
|
2018-11-13 00:28:00 +08:00
|
|
|
case SYSOFF_PRECISE:
|
|
|
|
*delay = 0;
|
|
|
|
return sysoff_precise(fd, result, ts);
|
2018-11-13 00:28:01 +08:00
|
|
|
case SYSOFF_EXTENDED:
|
|
|
|
return sysoff_extended(fd, n_samples, result, ts, delay);
|
2018-11-13 00:27:59 +08:00
|
|
|
case SYSOFF_BASIC:
|
|
|
|
return sysoff_basic(fd, n_samples, result, ts, delay);
|
|
|
|
}
|
|
|
|
return SYSOFF_COMPILE_TIME_MISSING;
|
2012-11-26 20:22:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int sysoff_probe(int fd, int n_samples)
|
|
|
|
{
|
2013-02-08 02:56:52 +08:00
|
|
|
int64_t junk, delay;
|
2012-11-26 20:22:07 +08:00
|
|
|
uint64_t ts;
|
2018-11-13 00:27:59 +08:00
|
|
|
int i;
|
2012-11-26 20:22:07 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-11-13 00:27:59 +08:00
|
|
|
for (i = 0; i < SYSOFF_LAST; i++) {
|
|
|
|
if (sysoff_measure(fd, i, n_samples, &junk, &ts, &delay) < 0)
|
|
|
|
continue;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SYSOFF_RUN_TIME_MISSING;
|
2012-11-26 20:22:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#else /* !PTP_SYS_OFFSET */
|
|
|
|
|
2019-12-23 00:26:48 +08:00
|
|
|
int sysoff_measure(int fd, int method, int n_samples,
|
2013-02-08 02:56:52 +08:00
|
|
|
int64_t *result, uint64_t *ts, int64_t *delay)
|
2012-11-26 20:22:07 +08:00
|
|
|
{
|
|
|
|
return SYSOFF_COMPILE_TIME_MISSING;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sysoff_probe(int fd, int n_samples)
|
|
|
|
{
|
|
|
|
return SYSOFF_COMPILE_TIME_MISSING;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* PTP_SYS_OFFSET */
|