dcavalca / rpms / linuxptp

Forked from rpms/linuxptp 2 years ago
Clone
63b4e9
diff --git a/clock.c b/clock.c
63b4e9
index c2d3ca0..02f79bb 100644
63b4e9
--- a/clock.c
63b4e9
+++ b/clock.c
63b4e9
@@ -1712,6 +1712,12 @@ void clock_path_delay(struct clock *c, tmv_t req, tmv_t rx)
63b4e9
 		stats_add_value(c->stats.delay, tmv_dbl(c->path_delay));
63b4e9
 }
63b4e9
 
63b4e9
+void clock_path_delay_corr(struct clock *c, tmv_t req, tmv_t rx, tmv_t c3)
63b4e9
+{
63b4e9
+	tsproc_up_ts_corr(c->tsproc, req, rx, c3);
63b4e9
+	clock_path_delay(c, req, rx);
63b4e9
+}
63b4e9
+
63b4e9
 void clock_peer_delay(struct clock *c, tmv_t ppd, tmv_t req, tmv_t rx,
63b4e9
 		      double nrr)
63b4e9
 {
63b4e9
@@ -1848,6 +1854,9 @@ enum servo_state clock_synchronize(struct clock *c, tmv_t ingress, tmv_t origin)
63b4e9
 	}
63b4e9
 
63b4e9
 	offset = tmv_to_nanoseconds(c->master_offset);
63b4e9
+	if (offset * tmv_sign(c->master_offset) > 10000) {
63b4e9
+		tsproc_dump_state(c->tsproc);
63b4e9
+	}
63b4e9
 	adj = servo_sample(c->servo, offset, tmv_to_nanoseconds(ingress),
63b4e9
 			   weight, &state);
63b4e9
 	c->servo_state = state;
63b4e9
@@ -1898,6 +1907,12 @@ enum servo_state clock_synchronize(struct clock *c, tmv_t ingress, tmv_t origin)
63b4e9
 	return state;
63b4e9
 }
63b4e9
 
63b4e9
+enum servo_state clock_synchronize_corr(struct clock *c, tmv_t ingress, tmv_t origin, tmv_t c2, tmv_t c1)
63b4e9
+{
63b4e9
+	tsproc_down_ts_corr(c->tsproc, ingress, origin, c1, c2);
63b4e9
+	return clock_synchronize(c, ingress, origin);
63b4e9
+}
63b4e9
+
63b4e9
 void clock_sync_interval(struct clock *c, int n)
63b4e9
 {
63b4e9
 	int shift;
63b4e9
diff --git a/clock.h b/clock.h
63b4e9
index 0534f21..17b2e3b 100644
63b4e9
--- a/clock.h
63b4e9
+++ b/clock.h
63b4e9
@@ -228,6 +228,7 @@ struct PortIdentity clock_parent_identity(struct clock *c);
63b4e9
  *                    correction.
63b4e9
  */
63b4e9
 void clock_path_delay(struct clock *c, tmv_t req, tmv_t rx);
63b4e9
+void clock_path_delay_corr(struct clock *c, tmv_t req, tmv_t rx, tmv_t c3);
63b4e9
 
63b4e9
 /**
63b4e9
  * Provide the estimated peer delay from a slave port.
63b4e9
@@ -331,7 +332,7 @@ int clock_switch_phc(struct clock *c, int phc_index);
63b4e9
  */
63b4e9
 enum servo_state clock_synchronize(struct clock *c, tmv_t ingress,
63b4e9
 				   tmv_t origin);
63b4e9
-
63b4e9
+enum servo_state clock_synchronize_corr(struct clock *c, tmv_t ingress, tmv_t origin, tmv_t c2, tmv_t c1);
63b4e9
 /**
63b4e9
  * Inform a slaved clock about the master's sync interval.
63b4e9
  * @param c  The clock instance.
63b4e9
diff --git a/port.c b/port.c
63b4e9
index f2b666c..f919127 100644
63b4e9
--- a/port.c
63b4e9
+++ b/port.c
63b4e9
@@ -1263,7 +1263,7 @@ static void port_synchronize(struct port *p,
63b4e9
 	}
63b4e9
 
63b4e9
 	last_state = clock_servo_state(p->clock);
63b4e9
-	state = clock_synchronize(p->clock, t2, t1c);
63b4e9
+	state = clock_synchronize_corr(p->clock, t2, t1c, c2, c1);
63b4e9
 	switch (state) {
63b4e9
 	case SERVO_UNLOCKED:
63b4e9
 		port_dispatch(p, EV_SYNCHRONIZATION_FAULT, 0);
63b4e9
@@ -2074,7 +2074,7 @@ void process_delay_resp(struct port *p, struct ptp_message *m)
63b4e9
 	monitor_delay(p->slave_event_monitor, clock_parent_identity(p->clock),
63b4e9
 		      m->header.sequenceId, t3, c3, t4);
63b4e9
 
63b4e9
-	clock_path_delay(p->clock, t3, t4c);
63b4e9
+	clock_path_delay_corr(p->clock, t3, t4c, c3);
63b4e9
 
63b4e9
 	TAILQ_REMOVE(&p->delay_req, req, list);
63b4e9
 	msg_put(req);
63b4e9
diff --git a/tsproc.c b/tsproc.c
63b4e9
index a871049..8af3bdd 100644
63b4e9
--- a/tsproc.c
63b4e9
+++ b/tsproc.c
63b4e9
@@ -39,6 +39,11 @@ struct tsproc {
63b4e9
 	tmv_t t3;
63b4e9
 	tmv_t t4;
63b4e9
 
63b4e9
+	/* Corrections */
63b4e9
+	tmv_t c1; // sync correction
63b4e9
+	tmv_t c2; // follow-up correction
63b4e9
+	tmv_t c3; // delay responce correction
63b4e9
+
63b4e9
 	/* Current filtered delay */
63b4e9
 	tmv_t filtered_delay;
63b4e9
 	int filtered_delay_valid;
63b4e9
@@ -104,12 +109,29 @@ void tsproc_down_ts(struct tsproc *tsp, tmv_t remote_ts, tmv_t local_ts)
63b4e9
 	tsp->t2 = local_ts;
63b4e9
 }
63b4e9
 
63b4e9
+void tsproc_down_ts_corr(struct tsproc *tsp, tmv_t remote_ts, tmv_t local_ts, tmv_t c1, tmv_t c2)
63b4e9
+{
63b4e9
+	tsp->t1 = remote_ts;
63b4e9
+	tsp->t2 = local_ts;
63b4e9
+	tsp->c1 = c1;
63b4e9
+	tsp->c2 = c2;
63b4e9
+	pr_debug("t2 - t1 = %+10" PRId64 " c1 = %+10" PRId64, tmv_to_nanoseconds(tmv_sub(tsp->t2, tsp->t1)), tmv_to_nanoseconds(c1));
63b4e9
+}
63b4e9
+
63b4e9
 void tsproc_up_ts(struct tsproc *tsp, tmv_t local_ts, tmv_t remote_ts)
63b4e9
 {
63b4e9
 	tsp->t3 = local_ts;
63b4e9
 	tsp->t4 = remote_ts;
63b4e9
 }
63b4e9
 
63b4e9
+void tsproc_up_ts_corr(struct tsproc *tsp, tmv_t local_ts, tmv_t remote_ts, tmv_t c3)
63b4e9
+{
63b4e9
+	tsp->t3 = local_ts;
63b4e9
+	tsp->t4 = remote_ts;
63b4e9
+	tsp->c3 = c3;
63b4e9
+	pr_debug("t4 - t3 = %+10" PRId64 " c3 = %+10" PRId64, tmv_to_nanoseconds(tmv_sub(tsp->t4, tsp->t3)), tmv_to_nanoseconds(c3));
63b4e9
+}
63b4e9
+
63b4e9
 void tsproc_set_clock_rate_ratio(struct tsproc *tsp, double clock_rate_ratio)
63b4e9
 {
63b4e9
 	tsp->clock_rate_ratio = clock_rate_ratio;
63b4e9
@@ -145,6 +167,22 @@ tmv_t get_raw_delay(struct tsproc *tsp)
63b4e9
 	return delay;
63b4e9
 }
63b4e9
 
63b4e9
+void tsproc_dump_state(struct tsproc *tsp)
63b4e9
+{
63b4e9
+	pr_info("t1 = %+10" PRId64 " t2 = %+10" PRId64, tmv_to_nanoseconds(tsp->t1), tmv_to_nanoseconds(tsp->t2));
63b4e9
+	pr_info("t3 = %+10" PRId64 " t4 = %+10" PRId64, tmv_to_nanoseconds(tsp->t3), tmv_to_nanoseconds(tsp->t4));
63b4e9
+	pr_info("c1 = %+10" PRId64 " c2 = %+10" PRId64, tmv_to_nanoseconds(tsp->c1), tmv_to_nanoseconds(tsp->c2));
63b4e9
+	pr_info("c3 = %+10" PRId64, tmv_to_nanoseconds(tsp->c3));
63b4e9
+
63b4e9
+	pr_info("path delay = (t2 - t3) * rr + (t4 - t1)");
63b4e9
+	pr_info("t2 - t3 = %+10" PRId64, tmv_to_nanoseconds(tmv_sub(tsp->t2, tsp->t3)));
63b4e9
+	pr_info("t4 - t1 = %+10" PRId64, tmv_to_nanoseconds(tmv_sub(tsp->t4, tsp->t1)));
63b4e9
+
63b4e9
+	pr_info("t4 - t3 = %+10" PRId64 " c3 = %+10" PRId64, tmv_to_nanoseconds(tmv_sub(tsp->t4, tsp->t3)), tmv_to_nanoseconds(tsp->c3));
63b4e9
+	pr_info("t2 - t1 = %+10" PRId64 " c1 = %+10" PRId64, tmv_to_nanoseconds(tmv_sub(tsp->t2, tsp->t1)), tmv_to_nanoseconds(tsp->c1));
63b4e9
+
63b4e9
+}
63b4e9
+
63b4e9
 int tsproc_update_delay(struct tsproc *tsp, tmv_t *delay)
63b4e9
 {
63b4e9
 	tmv_t raw_delay;
63b4e9
diff --git a/tsproc.h b/tsproc.h
63b4e9
index fdb35a8..8baa0d9 100644
63b4e9
--- a/tsproc.h
63b4e9
+++ b/tsproc.h
63b4e9
@@ -58,6 +58,7 @@ void tsproc_destroy(struct tsproc *tsp);
63b4e9
  * @param local_ts  The local reception time.
63b4e9
  */
63b4e9
 void tsproc_down_ts(struct tsproc *tsp, tmv_t remote_ts, tmv_t local_ts);
63b4e9
+void tsproc_down_ts_corr(struct tsproc *tsp, tmv_t local_ts, tmv_t remote_ts, tmv_t c1, tmv_t c2);
63b4e9
 
63b4e9
 /**
63b4e9
  * Feed an upstream measurement into a time stamp processor.
63b4e9
@@ -66,6 +67,7 @@ void tsproc_down_ts(struct tsproc *tsp, tmv_t remote_ts, tmv_t local_ts);
63b4e9
  * @param remote_ts The remote reception time.
63b4e9
  */
63b4e9
 void tsproc_up_ts(struct tsproc *tsp, tmv_t local_ts, tmv_t remote_ts);
63b4e9
+void tsproc_up_ts_corr(struct tsproc *tsp, tmv_t local_ts, tmv_t remote_ts, tmv_t c3);
63b4e9
 
63b4e9
 /**
63b4e9
  * Set ratio between remote and local clock frequencies.
63b4e9
@@ -106,5 +108,5 @@ int tsproc_update_offset(struct tsproc *tsp, tmv_t *offset, double *weight);
63b4e9
  *               1 to reset everything (e.g. when remote clock changed).
63b4e9
  */
63b4e9
 void tsproc_reset(struct tsproc *tsp, int full);
63b4e9
-
63b4e9
+void tsproc_dump_state(struct tsproc *tsp);
63b4e9
 #endif