quantiles: add parameter to limit negative step

Add a new parameter to limit the negative value of the step state
variable. It's set as a maximum delay in number of updates before the
actual step applied to the quantile estimate starts growing from the
minimum step when the input value is consistently larger or smaller than
the estimate.

This prevents the algorithm from effectively becoming the slower 1U
variant if the quantile estimate is stable most of the time.

Set it to 100 updates for the NTP delay and 1000 updates for the hwclock
delay. An option could be added later to make it configurable.
This commit is contained in:
Miroslav Lichvar
2024-11-21 14:30:14 +01:00
parent 2da4e3ce53
commit d22c8fbcb2
5 changed files with 19 additions and 8 deletions

View File

@@ -49,6 +49,7 @@
#define DELAY_QUANT_MAX_K 2 #define DELAY_QUANT_MAX_K 2
#define DELAY_QUANT_Q 10 #define DELAY_QUANT_Q 10
#define DELAY_QUANT_REPEAT 7 #define DELAY_QUANT_REPEAT 7
#define DELAY_QUANT_LARGE_STEP_DELAY 1000
#define DELAY_QUANT_MIN_STEP 1.0e-9 #define DELAY_QUANT_MIN_STEP 1.0e-9
struct HCL_Instance_Record { struct HCL_Instance_Record {
@@ -127,6 +128,7 @@ HCL_CreateInstance(int min_samples, int max_samples, double min_separation, doub
clock->precision = precision; clock->precision = precision;
clock->delay_quants = QNT_CreateInstance(DELAY_QUANT_MIN_K, DELAY_QUANT_MAX_K, clock->delay_quants = QNT_CreateInstance(DELAY_QUANT_MIN_K, DELAY_QUANT_MAX_K,
DELAY_QUANT_Q, DELAY_QUANT_REPEAT, DELAY_QUANT_Q, DELAY_QUANT_REPEAT,
DELAY_QUANT_LARGE_STEP_DELAY,
DELAY_QUANT_MIN_STEP); DELAY_QUANT_MIN_STEP);
LCL_AddParameterChangeHandler(handle_slew, clock); LCL_AddParameterChangeHandler(handle_slew, clock);

View File

@@ -286,6 +286,7 @@ static ARR_Instance broadcasts;
/* Parameters for the peer delay quantile */ /* Parameters for the peer delay quantile */
#define DELAY_QUANT_Q 100 #define DELAY_QUANT_Q 100
#define DELAY_QUANT_LARGE_STEP_DELAY 100
#define DELAY_QUANT_REPEAT 7 #define DELAY_QUANT_REPEAT 7
/* Minimum and maximum allowed poll interval */ /* Minimum and maximum allowed poll interval */
@@ -690,6 +691,7 @@ NCR_CreateInstance(NTP_Remote_Address *remote_addr, NTP_Source_Type type,
if (params->max_delay_quant > 0.0) { if (params->max_delay_quant > 0.0) {
int k = round(CLAMP(0.05, params->max_delay_quant, 0.95) * DELAY_QUANT_Q); int k = round(CLAMP(0.05, params->max_delay_quant, 0.95) * DELAY_QUANT_Q);
result->delay_quant = QNT_CreateInstance(k, k, DELAY_QUANT_Q, DELAY_QUANT_REPEAT, result->delay_quant = QNT_CreateInstance(k, k, DELAY_QUANT_Q, DELAY_QUANT_REPEAT,
DELAY_QUANT_LARGE_STEP_DELAY,
LCL_GetSysPrecisionAsQuantum() / 2.0); LCL_GetSysPrecisionAsQuantum() / 2.0);
} else { } else {
result->delay_quant = NULL; result->delay_quant = NULL;

View File

@@ -49,19 +49,21 @@ struct QNT_Instance_Record {
int q; int q;
int min_k; int min_k;
double min_step; double min_step;
double neg_step_limit;
int n_set; int n_set;
}; };
/* ================================================== */ /* ================================================== */
QNT_Instance QNT_Instance
QNT_CreateInstance(int min_k, int max_k, int q, int repeat, double min_step) QNT_CreateInstance(int min_k, int max_k, int q, int repeat,
int large_step_delay, double min_step)
{ {
QNT_Instance inst; QNT_Instance inst;
long seed; long seed;
if (q < 2 || min_k > max_k || min_k < 1 || max_k >= q || if (q < 2 || min_k > max_k || min_k < 1 || max_k >= q ||
repeat < 1 || repeat > MAX_REPEAT || min_step <= 0.0) repeat < 1 || repeat > MAX_REPEAT || min_step <= 0.0 || large_step_delay < 0)
assert(0); assert(0);
inst = MallocNew(struct QNT_Instance_Record); inst = MallocNew(struct QNT_Instance_Record);
@@ -71,6 +73,7 @@ QNT_CreateInstance(int min_k, int max_k, int q, int repeat, double min_step)
inst->q = q; inst->q = q;
inst->min_k = min_k; inst->min_k = min_k;
inst->min_step = min_step; inst->min_step = min_step;
inst->neg_step_limit = -large_step_delay * min_step;
QNT_Reset(inst); QNT_Reset(inst);
@@ -136,7 +139,7 @@ insert_initial_value(QNT_Instance inst, double value)
static void static void
update_estimate(struct Quantile *quantile, double value, double p, double rand, update_estimate(struct Quantile *quantile, double value, double p, double rand,
double min_step) double min_step, double neg_step_limit)
{ {
if (value >= quantile->est) { if (value >= quantile->est) {
if (rand < (1.0 - p)) if (rand < (1.0 - p))
@@ -163,6 +166,9 @@ update_estimate(struct Quantile *quantile, double value, double p, double rand,
quantile->step = min_step; quantile->step = min_step;
quantile->sign = -1; quantile->sign = -1;
} }
if (quantile->step < neg_step_limit)
quantile->step = neg_step_limit;
} }
/* ================================================== */ /* ================================================== */
@@ -183,7 +189,7 @@ QNT_Accumulate(QNT_Instance inst, double value)
p = (double)(i / inst->repeat + inst->min_k) / inst->q; p = (double)(i / inst->repeat + inst->min_k) / inst->q;
rand = (double)random() / ((1U << 31) - 1); rand = (double)random() / ((1U << 31) - 1);
update_estimate(&inst->quants[i], value, p, rand, inst->min_step); update_estimate(&inst->quants[i], value, p, rand, inst->min_step, inst->neg_step_limit);
} }
} }

View File

@@ -30,7 +30,8 @@
typedef struct QNT_Instance_Record *QNT_Instance; typedef struct QNT_Instance_Record *QNT_Instance;
extern QNT_Instance QNT_CreateInstance(int min_k, int max_k, int q, int repeat, double min_step); extern QNT_Instance QNT_CreateInstance(int min_k, int max_k, int q, int repeat,
int large_step_delay, double min_step);
extern void QNT_DestroyInstance(QNT_Instance inst); extern void QNT_DestroyInstance(QNT_Instance inst);
extern void QNT_Reset(QNT_Instance inst); extern void QNT_Reset(QNT_Instance inst);

View File

@@ -40,7 +40,7 @@ test_unit(void)
max_k = random() % (q - 1) + 1; max_k = random() % (q - 1) + 1;
} while (min_k > max_k); } while (min_k > max_k);
inst = QNT_CreateInstance(min_k, max_k, q, r, 1e-9); inst = QNT_CreateInstance(min_k, max_k, q, r, 900, 1e-9);
TEST_CHECK(min_k == QNT_GetMinK(inst)); TEST_CHECK(min_k == QNT_GetMinK(inst));
TEST_CHECK(max_k == QNT_GetMaxK(inst)); TEST_CHECK(max_k == QNT_GetMaxK(inst));
@@ -76,11 +76,11 @@ test_unit(void)
TEST_CHECK(inst->quants[k].est > x - 0.4e-9); TEST_CHECK(inst->quants[k].est > x - 0.4e-9);
TEST_CHECK(inst->quants[k].est < x2 + 0.4e-9); TEST_CHECK(inst->quants[k].est < x2 + 0.4e-9);
TEST_CHECK(inst->quants[k].step < -15e-9); TEST_CHECK(inst->quants[k].step < -15e-9);
TEST_CHECK(inst->quants[k].step > -1000e-9); TEST_CHECK(inst->quants[k].step > -901e-9);
if (min_k * 2 == q && k < inst->repeat) { if (min_k * 2 == q && k < inst->repeat) {
if (x == x2) { if (x == x2) {
TEST_CHECK(inst->quants[k].step < -750e-9); TEST_CHECK(inst->quants[k].step < -750e-9);
TEST_CHECK(inst->quants[k].step > -1000e-9); TEST_CHECK(inst->quants[k].step > -901e-9);
} else { } else {
TEST_CHECK(inst->quants[k].step < -350e-9); TEST_CHECK(inst->quants[k].step < -350e-9);
TEST_CHECK(inst->quants[k].step > -600e-9); TEST_CHECK(inst->quants[k].step > -600e-9);