Discussion:
[Linuxptp-devel] [PATCH RFC 0/3] New servo based on linear regression
Miroslav Lichvar
2014-03-13 17:34:16 UTC
Permalink
I've been working on the adaptive servo I mentioned here some time ago
and I think it now works fairly well and could possibly be considered
for inclusion. It's called linreg, suggestions for a better name are
welcome.

Here are two graphs from the simulator that compare time and frequency
accuracy of phc2sys using the PI servo with HW and SW time stamping
constants and linreg.

Loading Image...
Loading Image...

The first patch moves and renames some PI options to common servo
code, so it's available to the new servo. The second patch adds the
servo and the third patch improves delay calculation as the new servo
has a nice feature that it can track frequency offset independently
from time offset.

What do you think?

Miroslav Lichvar (3):
Move PI step threshold and max frequency settings to common servo
code.
Add an adaptive servo based on linear regression.
Include clock rate ratio in delay calculation.

clock.c | 18 +++-
clock.h | 7 ++
config.c | 17 +--
config.h | 8 +-
default.cfg | 6 +-
gPTP.cfg | 6 +-
linreg.c | 326 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
linreg.h | 26 +++++
makefile | 8 +-
phc2sys.8 | 11 +-
phc2sys.c | 21 +++-
pi.c | 59 ++++------
pi.h | 26 +----
port.c | 3 +-
ptp4l.8 | 26 +++--
ptp4l.c | 7 +-
servo.c | 60 ++++++++++-
servo.h | 32 ++++++
servo_private.h | 6 ++
19 files changed, 560 insertions(+), 113 deletions(-)
create mode 100644 linreg.c
create mode 100644 linreg.h
--
1.8.4.2
Miroslav Lichvar
2014-03-13 17:34:17 UTC
Permalink
These settings will be useful for all implemented servos, so move them
to the common servo code to avoid duplication. The configuration options
are renamed, but the they can be still set by their old names.

Signed-off-by: Miroslav Lichvar <***@redhat.com>
---
config.c | 15 +++++++++------
config.h | 8 ++++----
default.cfg | 6 +++---
gPTP.cfg | 6 +++---
phc2sys.8 | 4 ++--
phc2sys.c | 4 ++--
pi.c | 59 ++++++++++++++++++---------------------------------------
pi.h | 26 +------------------------
ptp4l.8 | 22 +++++++++++++--------
ptp4l.c | 7 ++++---
servo.c | 48 ++++++++++++++++++++++++++++++++++++++++++----
servo.h | 24 +++++++++++++++++++++++
servo_private.h | 4 ++++
13 files changed, 132 insertions(+), 101 deletions(-)

diff --git a/config.c b/config.c
index 03a4a45..7fdc1d8 100644
--- a/config.c
+++ b/config.c
@@ -383,23 +383,26 @@ static enum parser_result parse_global_setting(const char *option,
return r;
*cfg->pi_integral_norm_max = df;

- } else if (!strcmp(option, "pi_offset_const")) {
+ } else if (!strcmp(option, "step_threshold") ||
+ !strcmp(option, "pi_offset_const")) {
r = get_ranged_double(value, &df, 0.0, DBL_MAX);
if (r != PARSED_OK)
return r;
- *cfg->pi_offset_const = df;
+ *cfg->step_threshold = df;

- } else if (!strcmp(option, "pi_f_offset_const")) {
+ } else if (!strcmp(option, "first_step_threshold") ||
+ !strcmp(option, "pi_f_offset_const")) {
r = get_ranged_double(value, &df, 0.0, DBL_MAX);
if (r != PARSED_OK)
return r;
- *cfg->pi_f_offset_const = df;
+ *cfg->first_step_threshold = df;

- } else if (!strcmp(option, "pi_max_frequency")) {
+ } else if (!strcmp(option, "max_frequency") ||
+ !strcmp(option, "pi_max_frequency")) {
r = get_ranged_int(value, &val, 0, INT_MAX);
if (r != PARSED_OK)
return r;
- *cfg->pi_max_frequency = val;
+ *cfg->max_frequency = val;

} else if (!strcmp(option, "sanity_freq_limit")) {
r = get_ranged_int(value, &val, 0, INT_MAX);
diff --git a/config.h b/config.h
index a380037..1b8cea3 100644
--- a/config.h
+++ b/config.h
@@ -77,6 +77,10 @@ struct config {

enum servo_type clock_servo;

+ double *step_threshold;
+ double *first_step_threshold;
+ int *max_frequency;
+
double *pi_proportional_const;
double *pi_integral_const;
double *pi_proportional_scale;
@@ -85,10 +89,6 @@ struct config {
double *pi_integral_scale;
double *pi_integral_exponent;
double *pi_integral_norm_max;
- double *pi_offset_const;
- double *pi_f_offset_const;
- int *pi_max_frequency;
-
int *sanity_freq_limit;

unsigned char *ptp_dst_mac;
diff --git a/default.cfg b/default.cfg
index 80c20b9..7038b69 100644
--- a/default.cfg
+++ b/default.cfg
@@ -48,9 +48,9 @@ pi_proportional_norm_max 0.7
pi_integral_scale 0.0
pi_integral_exponent 0.4
pi_integral_norm_max 0.3
-pi_offset_const 0.0
-pi_f_offset_const 0.0000001
-pi_max_frequency 900000000
+step_threshold 0.0
+first_step_threshold 0.0000001
+max_frequency 900000000
clock_servo pi
sanity_freq_limit 200000000
#
diff --git a/gPTP.cfg b/gPTP.cfg
index 6f09215..fac2aa0 100644
--- a/gPTP.cfg
+++ b/gPTP.cfg
@@ -48,9 +48,9 @@ pi_proportional_norm_max 0.7
pi_integral_scale 0.0
pi_integral_exponent 0.4
pi_integral_norm_max 0.3
-pi_offset_const 0.0
-pi_f_offset_const 0.0000001
-pi_max_frequency 900000000
+step_threshold 0.0
+first_step_threshold 0.0000001
+max_frequency 900000000
clock_servo pi
sanity_freq_limit 200000000
#
diff --git a/phc2sys.8 b/phc2sys.8
index 39a121c..812b69e 100644
--- a/phc2sys.8
+++ b/phc2sys.8
@@ -96,8 +96,8 @@ Specify the proportional constant of the PI controller. The default is 0.7.
Specify the integral constant of the PI controller. The default is 0.3.
.TP
.BI \-S " step"
-Specify the step threshold of the PI controller. It is the maximum offset that
-the controller corrects by changing the clock frequency instead of stepping the
+Specify the step threshold of the servo. It is the maximum offset that
+the servo corrects by changing the clock frequency instead of stepping the
clock. The clock is stepped on start regardless of the option if the offset is
larger than 100 nanoseconds (unless the
.BI \-F
diff --git a/phc2sys.c b/phc2sys.c
index 1a045ee..d1bfd2e 100644
--- a/phc2sys.c
+++ b/phc2sys.c
@@ -635,12 +635,12 @@ int main(int argc, char *argv[])
return -1;
break;
case 'S':
- if (get_arg_val_d(c, optarg, &configured_pi_offset,
+ if (get_arg_val_d(c, optarg, &servo_step_threshold,
0.0, DBL_MAX))
return -1;
break;
case 'F':
- if (get_arg_val_d(c, optarg, &configured_pi_f_offset,
+ if (get_arg_val_d(c, optarg, &servo_first_step_threshold,
0.0, DBL_MAX))
return -1;
break;
diff --git a/pi.c b/pi.c
index 52d4c2f..c8b8587 100644
--- a/pi.c
+++ b/pi.c
@@ -32,7 +32,6 @@
#define MAX_KP_NORM_MAX 1.0
#define MAX_KI_NORM_MAX 2.0

-#define NSEC_PER_SEC 1000000000
#define FREQ_EST_MARGIN 0.001

/* These take their values from the configuration file. (see ptp4l.c) */
@@ -44,23 +43,16 @@ double configured_pi_kp_norm_max = 0.7;
double configured_pi_ki_scale = 0.0;
double configured_pi_ki_exponent = 0.4;
double configured_pi_ki_norm_max = 0.3;
-double configured_pi_offset = 0.0;
-double configured_pi_f_offset = 0.0000001; /* 100 nanoseconds */
-int configured_pi_max_freq = 900000000;

struct pi_servo {
struct servo servo;
int64_t offset[2];
uint64_t local[2];
double drift;
- double maxppb;
double kp;
double ki;
- double max_offset;
- double max_f_offset;
double last_freq;
int count;
- int first_update;
};

static void pi_destroy(struct servo *servo)
@@ -111,19 +103,21 @@ static double pi_sample(struct servo *servo,
/* Adjust drift by the measured frequency offset. */
s->drift += (1e9 - s->drift) * (s->offset[1] - s->offset[0]) /
(s->local[1] - s->local[0]);
- if (s->drift < -s->maxppb)
- s->drift = -s->maxppb;
- else if (s->drift > s->maxppb)
- s->drift = s->maxppb;
-
- if ((s->first_update &&
- s->max_f_offset && (s->max_f_offset < fabs(offset))) ||
- (s->max_offset && (s->max_offset < fabs(offset))))
+
+ if (s->drift < -servo->max_frequency)
+ s->drift = -servo->max_frequency;
+ else if (s->drift > servo->max_frequency)
+ s->drift = servo->max_frequency;
+
+ if ((servo->first_update &&
+ servo->first_step_threshold &&
+ servo->first_step_threshold < fabs(offset)) ||
+ (servo->step_threshold &&
+ servo->step_threshold < fabs(offset)))
*state = SERVO_JUMP;
else
*state = SERVO_LOCKED;

- s->first_update = 0;
ppb = s->drift;
s->count = 2;
break;
@@ -135,7 +129,8 @@ static double pi_sample(struct servo *servo,
* immediately. This allows re-calculating drift as in initial
* clock startup.
*/
- if (s->max_offset && (s->max_offset < fabs(offset))) {
+ if (servo->step_threshold &&
+ servo->step_threshold < fabs(offset)) {
*state = SERVO_UNLOCKED;
s->count = 0;
break;
@@ -143,10 +138,10 @@ static double pi_sample(struct servo *servo,

ki_term = s->ki * offset;
ppb = s->kp * offset + s->drift + ki_term;
- if (ppb < -s->maxppb) {
- ppb = -s->maxppb;
- } else if (ppb > s->maxppb) {
- ppb = s->maxppb;
+ if (ppb < -servo->max_frequency) {
+ ppb = -servo->max_frequency;
+ } else if (ppb > servo->max_frequency) {
+ ppb = servo->max_frequency;
} else {
s->drift += ki_term;
}
@@ -181,7 +176,7 @@ static void pi_reset(struct servo *servo)
s->count = 0;
}

-struct servo *pi_servo_create(int fadj, int max_ppb, int sw_ts)
+struct servo *pi_servo_create(int fadj, int sw_ts)
{
struct pi_servo *s;

@@ -195,8 +190,6 @@ struct servo *pi_servo_create(int fadj, int max_ppb, int sw_ts)
s->servo.reset = pi_reset;
s->drift = fadj;
s->last_freq = fadj;
- s->maxppb = max_ppb;
- s->first_update = 1;
s->kp = 0.0;
s->ki = 0.0;

@@ -220,21 +213,5 @@ struct servo *pi_servo_create(int fadj, int max_ppb, int sw_ts)
}
}

- if (configured_pi_offset > 0.0) {
- s->max_offset = configured_pi_offset * NSEC_PER_SEC;
- } else {
- s->max_offset = 0.0;
- }
-
- if (configured_pi_f_offset > 0.0) {
- s->max_f_offset = configured_pi_f_offset * NSEC_PER_SEC;
- } else {
- s->max_f_offset = 0.0;
- }
-
- if (configured_pi_max_freq && s->maxppb > configured_pi_max_freq) {
- s->maxppb = configured_pi_max_freq;
- }
-
return &s->servo;
}
diff --git a/pi.h b/pi.h
index 0d8994b..7b092a0 100644
--- a/pi.h
+++ b/pi.h
@@ -77,30 +77,6 @@ extern double configured_pi_ki_exponent;
*/
extern double configured_pi_ki_norm_max;

-/**
- * When set to a non-zero value, this variable controls the maximum allowed
- * offset before a clock jump occurs instead of the default clock-slewing
- * mechanism.
- *
- * Note that this variable is measured in seconds, and allows fractional values.
- */
-extern double configured_pi_offset;
-
-/**
- * When set to zero, the clock is not stepped on start. When set to a non-zero
- * value, the value bahaves as a threshold and the clock is stepped on start if
- * the offset is bigger than the threshold.
- *
- * Note that this variable is measured in seconds, and allows fractional values.
- */
-extern double configured_pi_f_offset;
-
-/**
- * When set to a non-zero value, this variable sets an additional limit for
- * the frequency adjustment of the clock. It's in ppb.
- */
-extern int configured_pi_max_freq;
-
-struct servo *pi_servo_create(int fadj, int max_ppb, int sw_ts);
+struct servo *pi_servo_create(int fadj, int sw_ts);

#endif
diff --git a/ptp4l.8 b/ptp4l.8
index bce88f9..226123f 100644
--- a/ptp4l.8
+++ b/ptp4l.8
@@ -342,24 +342,30 @@ The ki_norm_max constant in the formula used to set the integral constant of
the PI controller from the sync interval.
The default is 0.3.
.TP
-.B pi_offset_const
-The maximum offset the PI controller will correct by changing the clock
-frequency instead of stepping the clock. When set to 0.0, the controller will
+.B step_threshold
+The maximum offset the servo will correct by changing the clock
+frequency instead of stepping the clock. When set to 0.0, the servo will
never step the clock except on start. It's specified in seconds.
The default is 0.0.
+This option used to be called (and can still be set by)
+.BR pi_offset_const .
.TP
-.B pi_f_offset_const
-The maximum offset the PI controller will correct by changing the clock
+.B first_step_threshold
+The maximum offset the servo will correct by changing the clock
frequency instead of stepping the clock. This is only applied on the first
-update. It's specified in seconds. When set to 0.0, the controller won't step
+update. It's specified in seconds. When set to 0.0, the servo won't step
the clock on start.
The default is 0.0000001 (100 nanoseconds).
+This option used to be called (and can still be set by)
+.BR pi_f_offset_const .
.TP
-.B pi_max_frequency
+.B max_frequency
The maximum allowed frequency adjustment of the clock in parts per billion
(ppb). This is an additional limit to the maximum allowed by the hardware. When
set to 0, the hardware limit will be used.
The default is 900000000 (90%).
+This option used to be called (and can still be set by)
+.BR pi_max_frequency .
.TP
.B sanity_freq_limit
The maximum allowed frequency offset between uncorrected clock and the system
@@ -436,7 +442,7 @@ The default is 00:00:00.
When a leap second is announced, let the kernel apply it by stepping the clock
instead of correcting the one-second offset with servo, which would correct the
one-second offset slowly by changing the clock frequency (unless the
-.B pi_offset_const
+.B step_threshold
option is set to correct such offset by stepping).
Relevant only with software time stamping. The default is 1 (enabled).
.TP
diff --git a/ptp4l.c b/ptp4l.c
index 7e38435..bbf558d 100644
--- a/ptp4l.c
+++ b/ptp4l.c
@@ -104,6 +104,10 @@ static struct config cfg_settings = {

.clock_servo = CLOCK_SERVO_PI,

+ .step_threshold = &servo_step_threshold,
+ .first_step_threshold = &servo_first_step_threshold,
+ .max_frequency = &servo_max_frequency,
+
.pi_proportional_const = &configured_pi_kp,
.pi_integral_const = &configured_pi_ki,
.pi_proportional_scale = &configured_pi_kp_scale,
@@ -112,9 +116,6 @@ static struct config cfg_settings = {
.pi_integral_scale = &configured_pi_ki_scale,
.pi_integral_exponent = &configured_pi_ki_exponent,
.pi_integral_norm_max = &configured_pi_ki_norm_max,
- .pi_offset_const = &configured_pi_offset,
- .pi_f_offset_const = &configured_pi_f_offset,
- .pi_max_frequency = &configured_pi_max_freq,

.ptp_dst_mac = ptp_dst_mac,
.p2p_dst_mac = p2p_dst_mac,
diff --git a/servo.c b/servo.c
index 5af020a..dd99d30 100644
--- a/servo.c
+++ b/servo.c
@@ -21,12 +21,45 @@
#include "pi.h"
#include "servo_private.h"

+#define NSEC_PER_SEC 1000000000
+
+double servo_step_threshold = 0.0;
+double servo_first_step_threshold = 0.0000001; /* 100 nanoseconds */
+int servo_max_frequency = 900000000;
+
struct servo *servo_create(enum servo_type type, int fadj, int max_ppb, int sw_ts)
{
- if (type == CLOCK_SERVO_PI) {
- return pi_servo_create(fadj, max_ppb, sw_ts);
+ struct servo *servo;
+
+ switch (type) {
+ case CLOCK_SERVO_PI:
+ servo = pi_servo_create(fadj, sw_ts);
+ break;
+ default:
+ return NULL;
+ }
+
+ if (servo_step_threshold > 0.0) {
+ servo->step_threshold = servo_step_threshold * NSEC_PER_SEC;
+ } else {
+ servo->step_threshold = 0.0;
+ }
+
+ if (servo_first_step_threshold > 0.0) {
+ servo->first_step_threshold =
+ servo_first_step_threshold * NSEC_PER_SEC;
+ } else {
+ servo->first_step_threshold = 0.0;
}
- return NULL;
+
+ servo->max_frequency = max_ppb;
+ if (servo_max_frequency && servo->max_frequency > servo_max_frequency) {
+ servo->max_frequency = servo_max_frequency;
+ }
+
+ servo->first_update = 1;
+
+ return servo;
}

void servo_destroy(struct servo *servo)
@@ -39,7 +72,14 @@ double servo_sample(struct servo *servo,
uint64_t local_ts,
enum servo_state *state)
{
- return servo->sample(servo, offset, local_ts, state);
+ double r;
+
+ r = servo->sample(servo, offset, local_ts, state);
+
+ if (*state != SERVO_UNLOCKED)
+ servo->first_update = 0;
+
+ return r;
}

void servo_sync_interval(struct servo *servo, double interval)
diff --git a/servo.h b/servo.h
index 7a5d0d3..2439966 100644
--- a/servo.h
+++ b/servo.h
@@ -22,6 +22,30 @@

#include <stdint.h>

+/**
+ * When set to a non-zero value, this variable controls the maximum allowed
+ * offset before a clock jump occurs instead of the default clock-slewing
+ * mechanism.
+ *
+ * Note that this variable is measured in seconds, and allows fractional values.
+ */
+extern double servo_step_threshold;
+
+/**
+ * When set to zero, the clock is not stepped on start. When set to a non-zero
+ * value, the value bahaves as a threshold and the clock is stepped on start if
+ * the offset is bigger than the threshold.
+ *
+ * Note that this variable is measured in seconds, and allows fractional values.
+ */
+extern double servo_first_step_threshold;
+
+/**
+ * When set to a non-zero value, this variable sets an additional limit for
+ * the frequency adjustment of the clock. It's in ppb.
+ */
+extern int servo_max_frequency;
+
/** Opaque type */
struct servo;

diff --git a/servo_private.h b/servo_private.h
index 82e2bf5..6425d1e 100644
--- a/servo_private.h
+++ b/servo_private.h
@@ -22,6 +22,10 @@
#include "contain.h"

struct servo {
+ double max_frequency;
+ double step_threshold;
+ double first_step_threshold;
+ int first_update;

void (*destroy)(struct servo *servo);
--
1.8.4.2
Keller, Jacob E
2014-03-13 18:50:46 UTC
Permalink
Post by Miroslav Lichvar
These settings will be useful for all implemented servos, so move them
to the common servo code to avoid duplication. The configuration options
are renamed, but the they can be still set by their old names.
How difficult would it be to have it complain that the old configuration
name is deprecated in favor of the new name? It might not be worth
doing, but if it's not too difficult, I think that would be valuable. I
think this could be done in a separate section check, without
duplicating the setup code.

Thanks,
Jake
Post by Miroslav Lichvar
---
config.c | 15 +++++++++------
config.h | 8 ++++----
default.cfg | 6 +++---
gPTP.cfg | 6 +++---
phc2sys.8 | 4 ++--
phc2sys.c | 4 ++--
pi.c | 59 ++++++++++++++++++---------------------------------------
pi.h | 26 +------------------------
ptp4l.8 | 22 +++++++++++++--------
ptp4l.c | 7 ++++---
servo.c | 48 ++++++++++++++++++++++++++++++++++++++++++----
servo.h | 24 +++++++++++++++++++++++
servo_private.h | 4 ++++
13 files changed, 132 insertions(+), 101 deletions(-)
diff --git a/config.c b/config.c
index 03a4a45..7fdc1d8 100644
--- a/config.c
+++ b/config.c
@@ -383,23 +383,26 @@ static enum parser_result parse_global_setting(const char *option,
return r;
*cfg->pi_integral_norm_max = df;
- } else if (!strcmp(option, "pi_offset_const")) {
+ } else if (!strcmp(option, "step_threshold") ||
+ !strcmp(option, "pi_offset_const")) {
r = get_ranged_double(value, &df, 0.0, DBL_MAX);
if (r != PARSED_OK)
return r;
- *cfg->pi_offset_const = df;
+ *cfg->step_threshold = df;
- } else if (!strcmp(option, "pi_f_offset_const")) {
+ } else if (!strcmp(option, "first_step_threshold") ||
+ !strcmp(option, "pi_f_offset_const")) {
r = get_ranged_double(value, &df, 0.0, DBL_MAX);
if (r != PARSED_OK)
return r;
- *cfg->pi_f_offset_const = df;
+ *cfg->first_step_threshold = df;
- } else if (!strcmp(option, "pi_max_frequency")) {
+ } else if (!strcmp(option, "max_frequency") ||
+ !strcmp(option, "pi_max_frequency")) {
r = get_ranged_int(value, &val, 0, INT_MAX);
if (r != PARSED_OK)
return r;
- *cfg->pi_max_frequency = val;
+ *cfg->max_frequency = val;
} else if (!strcmp(option, "sanity_freq_limit")) {
r = get_ranged_int(value, &val, 0, INT_MAX);
diff --git a/config.h b/config.h
index a380037..1b8cea3 100644
--- a/config.h
+++ b/config.h
@@ -77,6 +77,10 @@ struct config {
enum servo_type clock_servo;
+ double *step_threshold;
+ double *first_step_threshold;
+ int *max_frequency;
+
double *pi_proportional_const;
double *pi_integral_const;
double *pi_proportional_scale;
@@ -85,10 +89,6 @@ struct config {
double *pi_integral_scale;
double *pi_integral_exponent;
double *pi_integral_norm_max;
- double *pi_offset_const;
- double *pi_f_offset_const;
- int *pi_max_frequency;
-
int *sanity_freq_limit;
unsigned char *ptp_dst_mac;
diff --git a/default.cfg b/default.cfg
index 80c20b9..7038b69 100644
--- a/default.cfg
+++ b/default.cfg
@@ -48,9 +48,9 @@ pi_proportional_norm_max 0.7
pi_integral_scale 0.0
pi_integral_exponent 0.4
pi_integral_norm_max 0.3
-pi_offset_const 0.0
-pi_f_offset_const 0.0000001
-pi_max_frequency 900000000
+step_threshold 0.0
+first_step_threshold 0.0000001
+max_frequency 900000000
clock_servo pi
sanity_freq_limit 200000000
#
diff --git a/gPTP.cfg b/gPTP.cfg
index 6f09215..fac2aa0 100644
--- a/gPTP.cfg
+++ b/gPTP.cfg
@@ -48,9 +48,9 @@ pi_proportional_norm_max 0.7
pi_integral_scale 0.0
pi_integral_exponent 0.4
pi_integral_norm_max 0.3
-pi_offset_const 0.0
-pi_f_offset_const 0.0000001
-pi_max_frequency 900000000
+step_threshold 0.0
+first_step_threshold 0.0000001
+max_frequency 900000000
clock_servo pi
sanity_freq_limit 200000000
#
diff --git a/phc2sys.8 b/phc2sys.8
index 39a121c..812b69e 100644
--- a/phc2sys.8
+++ b/phc2sys.8
@@ -96,8 +96,8 @@ Specify the proportional constant of the PI controller. The default is 0.7.
Specify the integral constant of the PI controller. The default is 0.3.
.TP
.BI \-S " step"
-Specify the step threshold of the PI controller. It is the maximum offset that
-the controller corrects by changing the clock frequency instead of stepping the
+Specify the step threshold of the servo. It is the maximum offset that
+the servo corrects by changing the clock frequency instead of stepping the
clock. The clock is stepped on start regardless of the option if the offset is
larger than 100 nanoseconds (unless the
.BI \-F
diff --git a/phc2sys.c b/phc2sys.c
index 1a045ee..d1bfd2e 100644
--- a/phc2sys.c
+++ b/phc2sys.c
@@ -635,12 +635,12 @@ int main(int argc, char *argv[])
return -1;
break;
- if (get_arg_val_d(c, optarg, &configured_pi_offset,
+ if (get_arg_val_d(c, optarg, &servo_step_threshold,
0.0, DBL_MAX))
return -1;
break;
- if (get_arg_val_d(c, optarg, &configured_pi_f_offset,
+ if (get_arg_val_d(c, optarg, &servo_first_step_threshold,
0.0, DBL_MAX))
return -1;
break;
diff --git a/pi.c b/pi.c
index 52d4c2f..c8b8587 100644
--- a/pi.c
+++ b/pi.c
@@ -32,7 +32,6 @@
#define MAX_KP_NORM_MAX 1.0
#define MAX_KI_NORM_MAX 2.0
-#define NSEC_PER_SEC 1000000000
#define FREQ_EST_MARGIN 0.001
/* These take their values from the configuration file. (see ptp4l.c) */
@@ -44,23 +43,16 @@ double configured_pi_kp_norm_max = 0.7;
double configured_pi_ki_scale = 0.0;
double configured_pi_ki_exponent = 0.4;
double configured_pi_ki_norm_max = 0.3;
-double configured_pi_offset = 0.0;
-double configured_pi_f_offset = 0.0000001; /* 100 nanoseconds */
-int configured_pi_max_freq = 900000000;
struct pi_servo {
struct servo servo;
int64_t offset[2];
uint64_t local[2];
double drift;
- double maxppb;
double kp;
double ki;
- double max_offset;
- double max_f_offset;
double last_freq;
int count;
- int first_update;
};
static void pi_destroy(struct servo *servo)
@@ -111,19 +103,21 @@ static double pi_sample(struct servo *servo,
/* Adjust drift by the measured frequency offset. */
s->drift += (1e9 - s->drift) * (s->offset[1] - s->offset[0]) /
(s->local[1] - s->local[0]);
- if (s->drift < -s->maxppb)
- s->drift = -s->maxppb;
- else if (s->drift > s->maxppb)
- s->drift = s->maxppb;
-
- if ((s->first_update &&
- s->max_f_offset && (s->max_f_offset < fabs(offset))) ||
- (s->max_offset && (s->max_offset < fabs(offset))))
+
+ if (s->drift < -servo->max_frequency)
+ s->drift = -servo->max_frequency;
+ else if (s->drift > servo->max_frequency)
+ s->drift = servo->max_frequency;
+
+ if ((servo->first_update &&
+ servo->first_step_threshold &&
+ servo->first_step_threshold < fabs(offset)) ||
+ (servo->step_threshold &&
+ servo->step_threshold < fabs(offset)))
*state = SERVO_JUMP;
else
*state = SERVO_LOCKED;
- s->first_update = 0;
ppb = s->drift;
s->count = 2;
break;
@@ -135,7 +129,8 @@ static double pi_sample(struct servo *servo,
* immediately. This allows re-calculating drift as in initial
* clock startup.
*/
- if (s->max_offset && (s->max_offset < fabs(offset))) {
+ if (servo->step_threshold &&
+ servo->step_threshold < fabs(offset)) {
*state = SERVO_UNLOCKED;
s->count = 0;
break;
@@ -143,10 +138,10 @@ static double pi_sample(struct servo *servo,
ki_term = s->ki * offset;
ppb = s->kp * offset + s->drift + ki_term;
- if (ppb < -s->maxppb) {
- ppb = -s->maxppb;
- } else if (ppb > s->maxppb) {
- ppb = s->maxppb;
+ if (ppb < -servo->max_frequency) {
+ ppb = -servo->max_frequency;
+ } else if (ppb > servo->max_frequency) {
+ ppb = servo->max_frequency;
} else {
s->drift += ki_term;
}
@@ -181,7 +176,7 @@ static void pi_reset(struct servo *servo)
s->count = 0;
}
-struct servo *pi_servo_create(int fadj, int max_ppb, int sw_ts)
+struct servo *pi_servo_create(int fadj, int sw_ts)
{
struct pi_servo *s;
@@ -195,8 +190,6 @@ struct servo *pi_servo_create(int fadj, int max_ppb, int sw_ts)
s->servo.reset = pi_reset;
s->drift = fadj;
s->last_freq = fadj;
- s->maxppb = max_ppb;
- s->first_update = 1;
s->kp = 0.0;
s->ki = 0.0;
@@ -220,21 +213,5 @@ struct servo *pi_servo_create(int fadj, int max_ppb, int sw_ts)
}
}
- if (configured_pi_offset > 0.0) {
- s->max_offset = configured_pi_offset * NSEC_PER_SEC;
- } else {
- s->max_offset = 0.0;
- }
-
- if (configured_pi_f_offset > 0.0) {
- s->max_f_offset = configured_pi_f_offset * NSEC_PER_SEC;
- } else {
- s->max_f_offset = 0.0;
- }
-
- if (configured_pi_max_freq && s->maxppb > configured_pi_max_freq) {
- s->maxppb = configured_pi_max_freq;
- }
-
return &s->servo;
}
diff --git a/pi.h b/pi.h
index 0d8994b..7b092a0 100644
--- a/pi.h
+++ b/pi.h
@@ -77,30 +77,6 @@ extern double configured_pi_ki_exponent;
*/
extern double configured_pi_ki_norm_max;
-/**
- * When set to a non-zero value, this variable controls the maximum allowed
- * offset before a clock jump occurs instead of the default clock-slewing
- * mechanism.
- *
- * Note that this variable is measured in seconds, and allows fractional values.
- */
-extern double configured_pi_offset;
-
-/**
- * When set to zero, the clock is not stepped on start. When set to a non-zero
- * value, the value bahaves as a threshold and the clock is stepped on start if
- * the offset is bigger than the threshold.
- *
- * Note that this variable is measured in seconds, and allows fractional values.
- */
-extern double configured_pi_f_offset;
-
-/**
- * When set to a non-zero value, this variable sets an additional limit for
- * the frequency adjustment of the clock. It's in ppb.
- */
-extern int configured_pi_max_freq;
-
-struct servo *pi_servo_create(int fadj, int max_ppb, int sw_ts);
+struct servo *pi_servo_create(int fadj, int sw_ts);
#endif
diff --git a/ptp4l.8 b/ptp4l.8
index bce88f9..226123f 100644
--- a/ptp4l.8
+++ b/ptp4l.8
@@ -342,24 +342,30 @@ The ki_norm_max constant in the formula used to set the integral constant of
the PI controller from the sync interval.
The default is 0.3.
.TP
-.B pi_offset_const
-The maximum offset the PI controller will correct by changing the clock
-frequency instead of stepping the clock. When set to 0.0, the controller will
+.B step_threshold
+The maximum offset the servo will correct by changing the clock
+frequency instead of stepping the clock. When set to 0.0, the servo will
never step the clock except on start. It's specified in seconds.
The default is 0.0.
+This option used to be called (and can still be set by)
+.BR pi_offset_const .
.TP
-.B pi_f_offset_const
-The maximum offset the PI controller will correct by changing the clock
+.B first_step_threshold
+The maximum offset the servo will correct by changing the clock
frequency instead of stepping the clock. This is only applied on the first
-update. It's specified in seconds. When set to 0.0, the controller won't step
+update. It's specified in seconds. When set to 0.0, the servo won't step
the clock on start.
The default is 0.0000001 (100 nanoseconds).
+This option used to be called (and can still be set by)
+.BR pi_f_offset_const .
.TP
-.B pi_max_frequency
+.B max_frequency
The maximum allowed frequency adjustment of the clock in parts per billion
(ppb). This is an additional limit to the maximum allowed by the hardware. When
set to 0, the hardware limit will be used.
The default is 900000000 (90%).
+This option used to be called (and can still be set by)
+.BR pi_max_frequency .
.TP
.B sanity_freq_limit
The maximum allowed frequency offset between uncorrected clock and the system
@@ -436,7 +442,7 @@ The default is 00:00:00.
When a leap second is announced, let the kernel apply it by stepping the clock
instead of correcting the one-second offset with servo, which would correct the
one-second offset slowly by changing the clock frequency (unless the
-.B pi_offset_const
+.B step_threshold
option is set to correct such offset by stepping).
Relevant only with software time stamping. The default is 1 (enabled).
.TP
diff --git a/ptp4l.c b/ptp4l.c
index 7e38435..bbf558d 100644
--- a/ptp4l.c
+++ b/ptp4l.c
@@ -104,6 +104,10 @@ static struct config cfg_settings = {
.clock_servo = CLOCK_SERVO_PI,
+ .step_threshold = &servo_step_threshold,
+ .first_step_threshold = &servo_first_step_threshold,
+ .max_frequency = &servo_max_frequency,
+
.pi_proportional_const = &configured_pi_kp,
.pi_integral_const = &configured_pi_ki,
.pi_proportional_scale = &configured_pi_kp_scale,
@@ -112,9 +116,6 @@ static struct config cfg_settings = {
.pi_integral_scale = &configured_pi_ki_scale,
.pi_integral_exponent = &configured_pi_ki_exponent,
.pi_integral_norm_max = &configured_pi_ki_norm_max,
- .pi_offset_const = &configured_pi_offset,
- .pi_f_offset_const = &configured_pi_f_offset,
- .pi_max_frequency = &configured_pi_max_freq,
.ptp_dst_mac = ptp_dst_mac,
.p2p_dst_mac = p2p_dst_mac,
diff --git a/servo.c b/servo.c
index 5af020a..dd99d30 100644
--- a/servo.c
+++ b/servo.c
@@ -21,12 +21,45 @@
#include "pi.h"
#include "servo_private.h"
+#define NSEC_PER_SEC 1000000000
+
+double servo_step_threshold = 0.0;
+double servo_first_step_threshold = 0.0000001; /* 100 nanoseconds */
+int servo_max_frequency = 900000000;
+
struct servo *servo_create(enum servo_type type, int fadj, int max_ppb, int sw_ts)
{
- if (type == CLOCK_SERVO_PI) {
- return pi_servo_create(fadj, max_ppb, sw_ts);
+ struct servo *servo;
+
+ switch (type) {
+ servo = pi_servo_create(fadj, sw_ts);
+ break;
+ return NULL;
+ }
+
+ if (servo_step_threshold > 0.0) {
+ servo->step_threshold = servo_step_threshold * NSEC_PER_SEC;
+ } else {
+ servo->step_threshold = 0.0;
+ }
+
+ if (servo_first_step_threshold > 0.0) {
+ servo->first_step_threshold =
+ servo_first_step_threshold * NSEC_PER_SEC;
+ } else {
+ servo->first_step_threshold = 0.0;
}
- return NULL;
+
+ servo->max_frequency = max_ppb;
+ if (servo_max_frequency && servo->max_frequency > servo_max_frequency) {
+ servo->max_frequency = servo_max_frequency;
+ }
+
+ servo->first_update = 1;
+
+ return servo;
}
void servo_destroy(struct servo *servo)
@@ -39,7 +72,14 @@ double servo_sample(struct servo *servo,
uint64_t local_ts,
enum servo_state *state)
{
- return servo->sample(servo, offset, local_ts, state);
+ double r;
+
+ r = servo->sample(servo, offset, local_ts, state);
+
+ if (*state != SERVO_UNLOCKED)
+ servo->first_update = 0;
+
+ return r;
}
void servo_sync_interval(struct servo *servo, double interval)
diff --git a/servo.h b/servo.h
index 7a5d0d3..2439966 100644
--- a/servo.h
+++ b/servo.h
@@ -22,6 +22,30 @@
#include <stdint.h>
+/**
+ * When set to a non-zero value, this variable controls the maximum allowed
+ * offset before a clock jump occurs instead of the default clock-slewing
+ * mechanism.
+ *
+ * Note that this variable is measured in seconds, and allows fractional values.
+ */
+extern double servo_step_threshold;
+
+/**
+ * When set to zero, the clock is not stepped on start. When set to a non-zero
+ * value, the value bahaves as a threshold and the clock is stepped on start if
+ * the offset is bigger than the threshold.
+ *
+ * Note that this variable is measured in seconds, and allows fractional values.
+ */
+extern double servo_first_step_threshold;
+
+/**
+ * When set to a non-zero value, this variable sets an additional limit for
+ * the frequency adjustment of the clock. It's in ppb.
+ */
+extern int servo_max_frequency;
+
/** Opaque type */
struct servo;
diff --git a/servo_private.h b/servo_private.h
index 82e2bf5..6425d1e 100644
--- a/servo_private.h
+++ b/servo_private.h
@@ -22,6 +22,10 @@
#include "contain.h"
struct servo {
+ double max_frequency;
+ double step_threshold;
+ double first_step_threshold;
+ int first_update;
void (*destroy)(struct servo *servo);
Miroslav Lichvar
2014-03-14 15:16:38 UTC
Permalink
Post by Keller, Jacob E
How difficult would it be to have it complain that the old configuration
name is deprecated in favor of the new name? It might not be worth
doing, but if it's not too difficult, I think that would be valuable. I
think this could be done in a separate section check, without
duplicating the setup code.
I think that's easy to do. If we tell users to update their config,
how long will we wait before actually dropping them from the code?
--
Miroslav Lichvar
Keller, Jacob E
2014-03-14 21:22:18 UTC
Permalink
-----Original Message-----
Sent: Friday, March 14, 2014 8:17 AM
To: Keller, Jacob E
Subject: Re: [Linuxptp-devel] [PATCH RFC 1/3] Move PI step threshold
and max frequency settings to common servo code.
Post by Keller, Jacob E
How difficult would it be to have it complain that the old configuration
name is deprecated in favor of the new name? It might not be worth
doing, but if it's not too difficult, I think that would be valuable. I
think this could be done in a separate section check, without
duplicating the setup code.
I think that's easy to do. If we tell users to update their config,
how long will we wait before actually dropping them from the code?
Not really sure. But I would like to be able to warn about using old option names.

Thanks,
Jake
--
Miroslav Lichvar
Miroslav Lichvar
2014-03-13 17:34:19 UTC
Permalink
With the new linreg servo the frequency offset and time offset are
controlled separately. The ratio between master's frequency and the
current frequency of the local clock is known and can be used when
calculating delay or peer delay to improve their accuracy.

This greatly improves the stability of the delay when the servo is
correcting a large offset.

Signed-off-by: Miroslav Lichvar <***@redhat.com>
---
clock.c | 18 +++++++++++++++---
clock.h | 7 +++++++
linreg.c | 14 ++++++++++++++
port.c | 3 ++-
servo.c | 8 ++++++++
servo.h | 7 +++++++
servo_private.h | 2 ++
7 files changed, 55 insertions(+), 4 deletions(-)

diff --git a/clock.c b/clock.c
index 7ef2852..3aa2407 100644
--- a/clock.c
+++ b/clock.c
@@ -977,6 +977,7 @@ void clock_path_delay(struct clock *c, struct timespec req, struct timestamp rx,
Integer64 correction)
{
tmv_t c1, c2, c3, pd, t1, t2, t3, t4;
+ double rr;

if (tmv_is_zero(c->t1))
return;
@@ -988,21 +989,27 @@ void clock_path_delay(struct clock *c, struct timespec req, struct timestamp rx,
t2 = c->t2;
t3 = timespec_to_tmv(req);
t4 = timestamp_to_tmv(rx);
+ rr = clock_rate_ratio(c);

/*
- * c->path_delay = (t2 - t3) + (t4 - t1);
+ * c->path_delay = (t2 - t3) * rr + (t4 - t1);
* c->path_delay -= c_sync + c_fup + c_delay_resp;
* c->path_delay /= 2.0;
*/
- pd = tmv_add(tmv_sub(t2, t3), tmv_sub(t4, t1));
+
+ pd = tmv_sub(t2, t3);
+ if (rr != 1.0)
+ pd = dbl_tmv(tmv_dbl(pd) * rr);
+ pd = tmv_add(pd, tmv_sub(t4, t1));
pd = tmv_sub(pd, tmv_add(c1, tmv_add(c2, c3)));
pd = tmv_div(pd, 2);

if (pd < 0) {
pr_debug("negative path delay %10" PRId64, pd);
- pr_debug("path_delay = (t2 - t3) + (t4 - t1) - (c1 + c2 + c3)");
+ pr_debug("path_delay = (t2 - t3) * rr + (t4 - t1) - (c1 + c2 + c3)");
pr_debug("t2 - t3 = %+10" PRId64, t2 - t3);
pr_debug("t4 - t1 = %+10" PRId64, t4 - t1);
+ pr_debug("rr = %.9f", rr);
pr_debug("c1 %10" PRId64, c1);
pr_debug("c2 %10" PRId64, c2);
pr_debug("c3 %10" PRId64, c3);
@@ -1246,3 +1253,8 @@ void clock_check_ts(struct clock *c, struct timespec ts)
servo_reset(c->servo);
}
}
+
+double clock_rate_ratio(struct clock *c)
+{
+ return servo_rate_ratio(c->servo);
+}
diff --git a/clock.h b/clock.h
index 52d50b2..804640b 100644
--- a/clock.h
+++ b/clock.h
@@ -253,4 +253,11 @@ int clock_num_ports(struct clock *c);
*/
void clock_check_ts(struct clock *c, struct timespec ts);

+/**
+ * Obtain ratio between master's frequency and current clock frequency.
+ * @param c The clock instance.
+ * @return The rate ratio, 1.0 is returned when not known.
+ */
+double clock_rate_ratio(struct clock *c);
+
#endif
diff --git a/linreg.c b/linreg.c
index add81e4..4513dd1 100644
--- a/linreg.c
+++ b/linreg.c
@@ -74,6 +74,8 @@ struct linreg_servo {
double clock_freq;
/* Expected interval between updates */
double update_interval;
+ /* Current ratio between remote and local frequency */
+ double frequency_ratio;
};

static void linreg_destroy(struct servo *servo)
@@ -269,6 +271,8 @@ static double linreg_sample(struct servo *servo,
else if (s->clock_freq < -servo->max_frequency)
s->clock_freq = -servo->max_frequency;

+ s->frequency_ratio = res->slope / (1.0 + s->clock_freq / 1e9);
+
return -s->clock_freq;
}

@@ -286,6 +290,7 @@ static void linreg_reset(struct servo *servo)

s->num_points = 0;
s->last_update = 0;
+ s->frequency_ratio = 1.0;

for (i = MIN_SIZE; i < MAX_SIZE; i++) {
s->results[i - MIN_SIZE].slope = 0.0;
@@ -293,6 +298,13 @@ static void linreg_reset(struct servo *servo)
}
}

+static double linreg_rate_ratio(struct servo *servo)
+{
+ struct linreg_servo *s = container_of(servo, struct linreg_servo, servo);
+
+ return s->frequency_ratio;
+}
+
struct servo *linreg_servo_create(int fadj)
{
struct linreg_servo *s;
@@ -305,8 +317,10 @@ struct servo *linreg_servo_create(int fadj)
s->servo.sample = linreg_sample;
s->servo.sync_interval = linreg_sync_interval;
s->servo.reset = linreg_reset;
+ s->servo.rate_ratio = linreg_rate_ratio;

s->clock_freq = -fadj;
+ s->frequency_ratio = 1.0;

return &s->servo;
}
diff --git a/port.c b/port.c
index 96a7eb2..1505aca 100644
--- a/port.c
+++ b/port.c
@@ -1795,7 +1795,8 @@ static void port_peer_delay(struct port *p)
t3 = timestamp_to_tmv(fup->ts.pdu);
c2 = correction_to_tmv(fup->header.correction);
calc:
- adj_t41 = p->nrate.ratio * tmv_dbl(tmv_sub(t4, t1));
+ adj_t41 = p->nrate.ratio * clock_rate_ratio(p->clock) *
+ tmv_dbl(tmv_sub(t4, t1));
pd = tmv_sub(dbl_tmv(adj_t41), tmv_sub(t3, t2));
pd = tmv_sub(pd, c1);
pd = tmv_sub(pd, c2);
diff --git a/servo.c b/servo.c
index 5b8b0ed..41e5c9f 100644
--- a/servo.c
+++ b/servo.c
@@ -95,3 +95,11 @@ void servo_reset(struct servo *servo)
{
servo->reset(servo);
}
+
+double servo_rate_ratio(struct servo *servo)
+{
+ if (servo->rate_ratio)
+ return servo->rate_ratio(servo);
+
+ return 1.0;
+}
diff --git a/servo.h b/servo.h
index 7346167..1536c9e 100644
--- a/servo.h
+++ b/servo.h
@@ -124,4 +124,11 @@ void servo_sync_interval(struct servo *servo, double interval);
*/
void servo_reset(struct servo *servo);

+/**
+ * Obtain ratio between master's frequency and current servo frequency.
+ * @param servo Pointer to a servo obtained via @ref servo_create().
+ * @return The rate ratio, 1.0 is returned when not known.
+ */
+double servo_rate_ratio(struct servo *servo);
+
#endif
diff --git a/servo_private.h b/servo_private.h
index 6425d1e..ebb7e3b 100644
--- a/servo_private.h
+++ b/servo_private.h
@@ -36,6 +36,8 @@ struct servo {
void (*sync_interval)(struct servo *servo, double interval);

void (*reset)(struct servo *servo);
+
+ double (*rate_ratio)(struct servo *servo);
};

#endif
--
1.8.4.2
Miroslav Lichvar
2014-03-13 17:34:18 UTC
Permalink
This servo uses linear regression to estimate current time and
frequency error. The number of points used in the regression is
variable (from 4 to 64 in powers of 2) and is selected by a long-term
statistic of the prediction error.

Future improvements could include tracking of sudden frequency changes
(e.g. due to temperature variations), better stability of the error
statistic when a large offset is corrected, options to set the speed of
the adaptation, minimum and maximum number of points, or an option to
prefer frequency accuracy over time accuracy.

Signed-off-by: Miroslav Lichvar <***@redhat.com>
---
config.c | 2 +
linreg.c | 312 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
linreg.h | 26 ++++++
makefile | 8 +-
phc2sys.8 | 7 ++
phc2sys.c | 17 +++-
ptp4l.8 | 4 +-
servo.c | 4 +
servo.h | 1 +
9 files changed, 373 insertions(+), 8 deletions(-)
create mode 100644 linreg.c
create mode 100644 linreg.h

diff --git a/config.c b/config.c
index 7fdc1d8..567256c 100644
--- a/config.c
+++ b/config.c
@@ -497,6 +497,8 @@ static enum parser_result parse_global_setting(const char *option,
} else if (!strcmp(option, "clock_servo")) {
if (!strcasecmp("pi", value))
cfg->clock_servo = CLOCK_SERVO_PI;
+ else if (!strcasecmp("linreg", value))
+ cfg->clock_servo = CLOCK_SERVO_LINREG;
else
return BAD_VALUE;

diff --git a/linreg.c b/linreg.c
new file mode 100644
index 0000000..add81e4
--- /dev/null
+++ b/linreg.c
@@ -0,0 +1,312 @@
+/**
+ * @file linreg.c
+ * @brief Implements an adaptive servo based on linear regression.
+ * @note Copyright (C) 2014 Miroslav Lichvar <***@redhat.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 <stdlib.h>
+#include <math.h>
+
+#include "linreg.h"
+#include "print.h"
+#include "servo_private.h"
+
+/* Maximum and minimum number of points used in regression,
+ defined as a power of 2 */
+#define MAX_SIZE 6
+#define MIN_SIZE 2
+
+#define MAX_POINTS (1 << MAX_SIZE)
+
+/* Smoothing factor used for long-term prediction error */
+#define ERR_SMOOTH 0.02
+/* Number of updates used for initialization */
+#define ERR_INITIAL_UPDATES 10
+/* Maximum ratio of two err values to be considered equal */
+#define ERR_EQUALS 1.05
+
+/* Uncorrected local time vs remote time */
+struct point {
+ uint64_t x;
+ uint64_t y;
+};
+
+struct result {
+ /* Slope and intercept from latest regression */
+ double slope;
+ double intercept;
+ /* Exponential moving average of prediction error */
+ double err;
+ /* Number of initial err updates */
+ int err_updates;
+};
+
+struct linreg_servo {
+ struct servo servo;
+ /* Circular buffer of points */
+ struct point points[MAX_POINTS];
+ /* Current time in x, y */
+ struct point reference;
+ /* Number of stored points */
+ unsigned int num_points;
+ /* Index of the newest point */
+ unsigned int last_point;
+ /* Remainder from last update of reference.x */
+ double x_remainder;
+ /* Local time stamp of last update */
+ uint64_t last_update;
+ /* Regression results for all sizes */
+ struct result results[MAX_SIZE - MIN_SIZE + 1];
+ /* Current frequency offset of the clock */
+ double clock_freq;
+ /* Expected interval between updates */
+ double update_interval;
+};
+
+static void linreg_destroy(struct servo *servo)
+{
+ struct linreg_servo *s = container_of(servo, struct linreg_servo, servo);
+ free(s);
+}
+
+static void move_reference(struct linreg_servo *s, int64_t x, int64_t y)
+{
+ struct result *res;
+ unsigned int i;
+
+ s->reference.x += x;
+ s->reference.y += y;
+
+ /* Update intercepts for new reference */
+ for (i = MIN_SIZE; i <= MAX_SIZE; i++) {
+ res = &s->results[i - MIN_SIZE];
+ res->intercept += x * res->slope - y;
+ }
+}
+
+static void update_reference(struct linreg_servo *s, uint64_t local_ts)
+{
+ double x_interval;
+ int64_t y_interval;
+
+ if (s->last_update) {
+ y_interval = local_ts - s->last_update;
+
+ /* Remove current frequency correction from the interval */
+ x_interval = y_interval / (1.0 + s->clock_freq / 1e9);
+ x_interval += s->x_remainder;
+ s->x_remainder = x_interval - (int64_t)x_interval;
+
+ move_reference(s, (int64_t)x_interval, y_interval);
+ }
+
+ s->last_update = local_ts;
+}
+
+static void add_sample(struct linreg_servo *s, int64_t offset)
+{
+ s->last_point = (s->last_point + 1) % MAX_POINTS;
+
+ s->points[s->last_point].x = s->reference.x;
+ s->points[s->last_point].y = s->reference.y - offset;
+
+ if (s->num_points < MAX_POINTS)
+ s->num_points++;
+}
+
+static void regress(struct linreg_servo *s)
+{
+ double x, y, y0, e, x_sum, y_sum, xy_sum, x2_sum;
+ unsigned int i, l, n, size;
+ struct result *res;
+
+ x_sum = 0.0, y_sum = 0.0, xy_sum = 0.0, x2_sum = 0.0;
+ i = 0;
+
+ y0 = (int64_t)(s->points[s->last_point].y - s->reference.y);
+
+ for (size = MIN_SIZE; size <= MAX_SIZE; size++) {
+ n = 1 << size;
+ if (n > s->num_points)
+ /* Not enough points for this size */
+ break;
+
+ res = &s->results[size - MIN_SIZE];
+
+ /* Update moving average of the prediction error */
+ if (res->slope) {
+ e = fabs(res->intercept - y0);
+ if (res->err_updates < ERR_INITIAL_UPDATES) {
+ res->err *= res->err_updates;
+ res->err += e;
+ res->err_updates++;
+ res->err /= res->err_updates;
+ } else {
+ res->err += ERR_SMOOTH * (e - res->err);
+ }
+ }
+
+ for (; i < n; i++) {
+ /* Iterate points from newest to oldest */
+ l = (MAX_POINTS + s->last_point - i) % MAX_POINTS;
+
+ x = (int64_t)(s->points[l].x - s->reference.x);
+ y = (int64_t)(s->points[l].y - s->reference.y);
+
+ x_sum += x;
+ y_sum += y;
+ xy_sum += x * y;
+ x2_sum += x * x;
+ }
+
+ /* Get new intercept and slope */
+ res->slope = (xy_sum - x_sum * y_sum / n) /
+ (x2_sum - x_sum * x_sum / n);
+ res->intercept = (y_sum - res->slope * x_sum) / n;
+ }
+}
+
+/* Return largest size with smallest prediction error */
+static int get_best_size(struct linreg_servo *s)
+{
+ struct result *res;
+ double best_err;
+ int size, best_size;
+
+ best_size = 0;
+ best_err = 0.0;
+
+ for (size = MIN_SIZE; size <= MAX_SIZE; size++) {
+ res = &s->results[size - MIN_SIZE];
+ if ((!best_size && res->slope) ||
+ (best_err * ERR_EQUALS > res->err &&
+ res->err_updates >= ERR_INITIAL_UPDATES)) {
+ best_size = size;
+ best_err = res->err;
+ }
+ }
+
+ return best_size;
+}
+
+static double linreg_sample(struct servo *servo,
+ int64_t offset,
+ uint64_t local_ts,
+ enum servo_state *state)
+{
+ struct linreg_servo *s = container_of(servo, struct linreg_servo, servo);
+ struct result *res;
+ int size, corr_interval;
+
+ /*
+ * The current time and the time when will be the frequency of the
+ * clock actually updated is assumed here to be equal to local_ts
+ * (which is the time stamp of the received sync message). As long as
+ * the differences are smaller than the update interval, the loop
+ * should be robust enough to handle this simplification.
+ */
+
+ update_reference(s, local_ts);
+ add_sample(s, offset);
+ regress(s);
+
+ size = get_best_size(s);
+
+ if (size < MIN_SIZE) {
+ /* Not enough points, wait for more */
+ *state = SERVO_UNLOCKED;
+ return -s->clock_freq;
+ }
+
+ res = &s->results[size - MIN_SIZE];
+
+ pr_debug("linreg: points %d slope %.9f intercept %.0f err %.0f",
+ 1 << size, res->slope, res->intercept, res->err);
+
+ if ((servo->first_update &&
+ servo->first_step_threshold &&
+ servo->first_step_threshold < fabs(res->intercept)) ||
+ (servo->step_threshold &&
+ servo->step_threshold < fabs(res->intercept))) {
+ /* The clock will be stepped by offset */
+ move_reference(s, 0, -offset);
+ s->last_update -= offset;
+ *state = SERVO_JUMP;
+ } else {
+ *state = SERVO_LOCKED;
+ }
+
+ /* Set clock frequency to the slope */
+ s->clock_freq = 1e9 * (res->slope - 1.0);
+
+ /*
+ * Adjust the frequency to correct the time offset. Use longer
+ * correction interval with larger sizes to reduce the frequency error.
+ * The update interval is assumed to be not affected by the frequency
+ * adjustment. If it is (e.g. phc2sys controlling the system clock), a
+ * correction slowing down the clock will result in an overshoot. With
+ * the system clock's maximum adjustment of 10% that's acceptable.
+ */
+ corr_interval = size <= 4 ? 1 : size / 2;
+ s->clock_freq += res->intercept / s->update_interval / corr_interval;
+
+ /* Clamp the frequency to the allowed maximum */
+ if (s->clock_freq > servo->max_frequency)
+ s->clock_freq = servo->max_frequency;
+ else if (s->clock_freq < -servo->max_frequency)
+ s->clock_freq = -servo->max_frequency;
+
+ return -s->clock_freq;
+}
+
+static void linreg_sync_interval(struct servo *servo, double interval)
+{
+ struct linreg_servo *s = container_of(servo, struct linreg_servo, servo);
+
+ s->update_interval = interval;
+}
+
+static void linreg_reset(struct servo *servo)
+{
+ struct linreg_servo *s = container_of(servo, struct linreg_servo, servo);
+ unsigned int i;
+
+ s->num_points = 0;
+ s->last_update = 0;
+
+ for (i = MIN_SIZE; i < MAX_SIZE; i++) {
+ s->results[i - MIN_SIZE].slope = 0.0;
+ s->results[i - MIN_SIZE].err_updates = 0;
+ }
+}
+
+struct servo *linreg_servo_create(int fadj)
+{
+ struct linreg_servo *s;
+
+ s = calloc(1, sizeof(*s));
+ if (!s)
+ return NULL;
+
+ s->servo.destroy = linreg_destroy;
+ s->servo.sample = linreg_sample;
+ s->servo.sync_interval = linreg_sync_interval;
+ s->servo.reset = linreg_reset;
+
+ s->clock_freq = -fadj;
+
+ return &s->servo;
+}
diff --git a/linreg.h b/linreg.h
new file mode 100644
index 0000000..5c86ea7
--- /dev/null
+++ b/linreg.h
@@ -0,0 +1,26 @@
+/**
+ * @file linreg.h
+ * @note Copyright (C) 2014 Miroslav Lichvar <***@redhat.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.
+ */
+#ifndef HAVE_LINREG_H
+#define HAVE_LINREG_H
+
+#include "servo.h"
+
+struct servo *linreg_servo_create(int fadj);
+
+#endif
diff --git a/makefile b/makefile
index 22e7d0d..eff5109 100644
--- a/makefile
+++ b/makefile
@@ -24,7 +24,7 @@ CFLAGS = -Wall $(VER) $(incdefs) $(DEBUG) $(EXTRA_CFLAGS)
LDLIBS = -lm -lrt $(EXTRA_LDFLAGS)
PRG = ptp4l pmc phc2sys hwstamp_ctl
OBJ = bmc.o clock.o clockadj.o clockcheck.o config.o fault.o \
- filter.o fsm.o mave.o mmedian.o msg.o phc.o pi.o port.o print.o ptp4l.o raw.o \
+ filter.o fsm.o linreg.o mave.o mmedian.o msg.o phc.o pi.o port.o print.o ptp4l.o raw.o \
servo.o sk.o stats.o tlv.o transport.o udp.o udp6.o uds.o util.o version.o

OBJECTS = $(OBJ) hwstamp_ctl.o phc2sys.o pmc.o pmc_common.o sysoff.o
@@ -47,9 +47,9 @@ ptp4l: $(OBJ)
pmc: msg.o pmc.o pmc_common.o print.o raw.o sk.o tlv.o transport.o udp.o \
udp6.o uds.o util.o version.o

-phc2sys: clockadj.o clockcheck.o msg.o phc.o phc2sys.o pi.o pmc_common.o \
- print.o raw.o servo.o sk.o stats.o sysoff.o tlv.o transport.o udp.o udp6.o \
- uds.o util.o version.o
+phc2sys: clockadj.o clockcheck.o linreg.o msg.o phc.o phc2sys.o pi.o \
+ pmc_common.o print.o raw.o servo.o sk.o stats.o sysoff.o tlv.o \
+ transport.o udp.o udp6.o uds.o util.o version.o

hwstamp_ctl: hwstamp_ctl.o version.o

diff --git a/phc2sys.8 b/phc2sys.8
index 812b69e..8688e48 100644
--- a/phc2sys.8
+++ b/phc2sys.8
@@ -15,6 +15,8 @@ phc2sys \- synchronize two clocks
] [
.BI \-O " offset"
] [
+.BI \-E " servo"
+] [
.BI \-P " kp"
] [
.BI \-I " ki"
@@ -89,6 +91,11 @@ should no longer be used.
Specify the slave clock by device (e.g. /dev/ptp1) or interface (e.g. eth1) or
by name. The default is CLOCK_REALTIME (the system clock).
.TP
+.BI \-E " servo"
+Specify which clock servo should be used. Valid values are pi for a PI
+controller and linreg for an adaptive controller using linear regression.
+The default is pi.
+.TP
.BI \-P " kp"
Specify the proportional constant of the PI controller. The default is 0.7.
.TP
diff --git a/phc2sys.c b/phc2sys.c
index d1bfd2e..6221515 100644
--- a/phc2sys.c
+++ b/phc2sys.c
@@ -561,6 +561,7 @@ static void usage(char *progname)
" -c [dev|name] slave clock (CLOCK_REALTIME)\n"
" -d [dev] master PPS device\n"
" -s [dev|name] master clock\n"
+ " -E [pi|linreg] clock servo (pi)\n"
" -P [kp] proportional constant (0.7)\n"
" -I [ki] integration constant (0.3)\n"
" -S [step] step threshold (disabled)\n"
@@ -590,6 +591,7 @@ int main(int argc, char *argv[])
int max_ppb, r, wait_sync = 0, forced_sync_offset = 0;
int print_level = LOG_INFO, use_syslog = 1, verbose = 0;
int sanity_freq_limit = 200000000;
+ enum servo_type servo = CLOCK_SERVO_PI;
double ppb, phc_interval = 1.0, phc_rate;
struct timespec phc_interval_tp;
struct clock dst_clock = {
@@ -605,7 +607,7 @@ int main(int argc, char *argv[])
progname = strrchr(argv[0], '/');
progname = progname ? 1+progname : argv[0];
while (EOF != (c = getopt(argc, argv,
- "c:d:s:P:I:S:F:R:N:O:L:i:u:wn:xl:mqvh"))) {
+ "c:d:s:E:P:I:S:F:R:N:O:L:i:u:wn:xl:mqvh"))) {
switch (c) {
case 'c':
dst_clock.clkid = clock_open(optarg);
@@ -624,6 +626,17 @@ int main(int argc, char *argv[])
case 's':
src = clock_open(optarg);
break;
+ case 'E':
+ if (!strcasecmp(optarg, "pi")) {
+ servo = CLOCK_SERVO_PI;
+ } else if (!strcasecmp(optarg, "linreg")) {
+ servo = CLOCK_SERVO_LINREG;
+ } else {
+ fprintf(stderr,
+ "invalid servo name %s\n", optarg);
+ return -1;
+ }
+ break;
case 'P':
if (get_arg_val_d(c, optarg, &configured_pi_kp,
0.0, DBL_MAX))
@@ -795,7 +808,7 @@ int main(int argc, char *argv[])
}
}

- dst_clock.servo = servo_create(CLOCK_SERVO_PI, -ppb, max_ppb, 0);
+ dst_clock.servo = servo_create(servo, -ppb, max_ppb, 0);

if (pps_fd >= 0) {
servo_sync_interval(dst_clock.servo, 1.0);
diff --git a/ptp4l.8 b/ptp4l.8
index 226123f..edc7045 100644
--- a/ptp4l.8
+++ b/ptp4l.8
@@ -288,8 +288,8 @@ generated by the master.
The default is 0 (disabled).
.TP
.B clock_servo
-The servo which is used to synchronize the local clock. Currently only one
-servo is implemented, a PI controller.
+The servo which is used to synchronize the local clock. Valid values are pi for
+a PI controller and linreg for an adaptive controller using linear regression.
The default is pi.
.TP
.B pi_proportional_const
diff --git a/servo.c b/servo.c
index dd99d30..5b8b0ed 100644
--- a/servo.c
+++ b/servo.c
@@ -18,6 +18,7 @@
*/
#include <string.h>

+#include "linreg.h"
#include "pi.h"
#include "servo_private.h"

@@ -35,6 +36,9 @@ struct servo *servo_create(enum servo_type type, int fadj, int max_ppb, int sw_t
case CLOCK_SERVO_PI:
servo = pi_servo_create(fadj, sw_ts);
break;
+ case CLOCK_SERVO_LINREG:
+ servo = linreg_servo_create(fadj);
+ break;
default:
return NULL;
}
diff --git a/servo.h b/servo.h
index 2439966..7346167 100644
--- a/servo.h
+++ b/servo.h
@@ -54,6 +54,7 @@ struct servo;
*/
enum servo_type {
CLOCK_SERVO_PI,
+ CLOCK_SERVO_LINREG,
};

/**
--
1.8.4.2
Richard Cochran
2014-03-14 08:47:13 UTC
Permalink
Post by Miroslav Lichvar
What do you think?
Can't wait to try this out. The graphics clearly show the advantage of
the adaptive servo, since there is no need to guess the PI weights.

Any idea how quickly this adapts to a changed network, like suddenly
adding a busy, non-transparent switch into the path?

Also, any reason not to merge this right away, to let people try it?

Thanks,
Richard
Miroslav Lichvar
2014-03-14 15:13:06 UTC
Permalink
Post by Richard Cochran
Any idea how quickly this adapts to a changed network, like suddenly
adding a busy, non-transparent switch into the path?
The error statistic currently uses smoothing factor of 0.02, so it
should adapt to the new jitter at most in few hundred updates.

In a simulated test with ptp4l where jitter is switching between 10 ns
and 1 us in 1000 update interval it seems to reach the maximum number
of points in about 20 updates and it goes back in about 200 updates.

Loading Image...
Post by Richard Cochran
Also, any reason not to merge this right away, to let people try it?
If you don't mind including an experimental code, I'll be glad if more
people test it.
--
Miroslav Lichvar
Richard Cochran
2014-03-14 15:48:43 UTC
Permalink
Post by Miroslav Lichvar
If you don't mind including an experimental code, I'll be glad if more
people test it.
Okay, series applied.

Thanks,
Richard
Loading...