Blame sysoff.c

Packit 9c3e7e
/**
Packit 9c3e7e
 * @file sysoff.c
Packit 9c3e7e
 * @brief Implements the system offset estimation method.
Packit 9c3e7e
 * @note Copyright (C) 2012 Richard Cochran <richardcochran@gmail.com>
Packit 9c3e7e
 *
Packit 9c3e7e
 * This program is free software; you can redistribute it and/or modify
Packit 9c3e7e
 * it under the terms of the GNU General Public License as published by
Packit 9c3e7e
 * the Free Software Foundation; either version 2 of the License, or
Packit 9c3e7e
 * (at your option) any later version.
Packit 9c3e7e
 *
Packit 9c3e7e
 * This program is distributed in the hope that it will be useful,
Packit 9c3e7e
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 9c3e7e
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 9c3e7e
 * GNU General Public License for more details.
Packit 9c3e7e
 *
Packit 9c3e7e
 * You should have received a copy of the GNU General Public License along
Packit 9c3e7e
 * with this program; if not, write to the Free Software Foundation, Inc.,
Packit 9c3e7e
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Packit 9c3e7e
 */
Packit 9c3e7e
#include <stdio.h>
Packit 9c3e7e
#include <sys/ioctl.h>
Packit 9c3e7e
#include <linux/ptp_clock.h>
Packit 9c3e7e
Packit 9c3e7e
#include "sysoff.h"
Packit 9c3e7e
Packit 9c3e7e
#define NS_PER_SEC 1000000000LL
Packit 9c3e7e
Packit 9c3e7e
#ifdef PTP_SYS_OFFSET
Packit 9c3e7e
Packit 9c3e7e
static int64_t pctns(struct ptp_clock_time *t)
Packit 9c3e7e
{
Packit 9c3e7e
	return t->sec * NS_PER_SEC + t->nsec;
Packit 9c3e7e
}
Packit 9c3e7e
Packit 9c3e7e
static struct {
Packit 9c3e7e
	int64_t interval;
Packit 9c3e7e
	int64_t offset;
Packit 9c3e7e
	uint64_t timestamp;
Packit 9c3e7e
} samples[PTP_MAX_SAMPLES];
Packit 9c3e7e
Packit 9c3e7e
static void insertion_sort(int length, int64_t interval, int64_t offset, uint64_t ts)
Packit 9c3e7e
{
Packit 9c3e7e
	int i = length - 1;
Packit 9c3e7e
	while (i >= 0) {
Packit 9c3e7e
		if (samples[i].interval < interval)
Packit 9c3e7e
			break;
Packit 9c3e7e
		samples[i+1] = samples[i];
Packit 9c3e7e
		i--;
Packit 9c3e7e
	}
Packit 9c3e7e
	samples[i+1].interval = interval;
Packit 9c3e7e
	samples[i+1].offset = offset;
Packit 9c3e7e
	samples[i+1].timestamp = ts;
Packit 9c3e7e
}
Packit 9c3e7e
Packit 9c3e7e
static int64_t sysoff_estimate(struct ptp_clock_time *pct, int n_samples,
Packit 9c3e7e
			       uint64_t *ts, int64_t *delay)
Packit 9c3e7e
{
Packit 9c3e7e
	int64_t t1, t2, tp;
Packit 9c3e7e
	int64_t interval, offset;
Packit 9c3e7e
	int i;
Packit 9c3e7e
Packit 9c3e7e
	for (i = 0; i < n_samples; i++) {
Packit 9c3e7e
		t1 = pctns(&pct[2*i]);
Packit 9c3e7e
		tp = pctns(&pct[2*i+1]);
Packit 9c3e7e
		t2 = pctns(&pct[2*i+2]);
Packit 9c3e7e
		interval = t2 - t1;
Packit 9c3e7e
		offset = (t2 + t1) / 2 - tp;
Packit 9c3e7e
		insertion_sort(i, interval, offset, (t2 + t1) / 2);
Packit 9c3e7e
	}
Packit 9c3e7e
	*ts = samples[0].timestamp;
Packit 9c3e7e
	*delay = samples[0].interval;
Packit 9c3e7e
	return samples[0].offset;
Packit 9c3e7e
}
Packit 9c3e7e
Packit 9c3e7e
int sysoff_measure(int fd, int n_samples,
Packit 9c3e7e
		   int64_t *result, uint64_t *ts, int64_t *delay)
Packit 9c3e7e
{
Packit 9c3e7e
	struct ptp_sys_offset pso;
Packit 9c3e7e
	pso.n_samples = n_samples;
Packit 9c3e7e
	if (ioctl(fd, PTP_SYS_OFFSET, &pso)) {
Packit 9c3e7e
		perror("ioctl PTP_SYS_OFFSET");
Packit 9c3e7e
		return SYSOFF_RUN_TIME_MISSING;
Packit 9c3e7e
	}
Packit 9c3e7e
	*result = sysoff_estimate(pso.ts, n_samples, ts, delay);
Packit 9c3e7e
	return SYSOFF_SUPPORTED;
Packit 9c3e7e
}
Packit 9c3e7e
Packit 9c3e7e
int sysoff_probe(int fd, int n_samples)
Packit 9c3e7e
{
Packit 9c3e7e
	int64_t junk, delay;
Packit 9c3e7e
	uint64_t ts;
Packit 9c3e7e
Packit 9c3e7e
	if (n_samples > PTP_MAX_SAMPLES) {
Packit 9c3e7e
		fprintf(stderr, "warning: %d exceeds kernel max readings %d\n",
Packit 9c3e7e
			n_samples, PTP_MAX_SAMPLES);
Packit 9c3e7e
		fprintf(stderr, "falling back to clock_gettime method\n");
Packit 9c3e7e
		return SYSOFF_RUN_TIME_MISSING;
Packit 9c3e7e
	}
Packit 9c3e7e
Packit 9c3e7e
	return sysoff_measure(fd, n_samples, &junk, &ts, &delay);
Packit 9c3e7e
}
Packit 9c3e7e
Packit 9c3e7e
#else /* !PTP_SYS_OFFSET */
Packit 9c3e7e
Packit 9c3e7e
int sysoff_measure(int fd, int n_samples,
Packit 9c3e7e
		   int64_t *result, uint64_t *ts, int64_t *delay)
Packit 9c3e7e
{
Packit 9c3e7e
	return SYSOFF_COMPILE_TIME_MISSING;
Packit 9c3e7e
}
Packit 9c3e7e
Packit 9c3e7e
int sysoff_probe(int fd, int n_samples)
Packit 9c3e7e
{
Packit 9c3e7e
	return SYSOFF_COMPILE_TIME_MISSING;
Packit 9c3e7e
}
Packit 9c3e7e
Packit 9c3e7e
#endif /* PTP_SYS_OFFSET */