Re: thread on sleepqueue does not wake up after timeout

From: Andriy Gapon <avg_at_FreeBSD.org>
Date: Wed, 10 Nov 2021 13:45:06 UTC
On 10/11/2021 11:30, Andriy Gapon wrote:
> On 09/11/2021 17:56, Andriy Gapon wrote:
>> So, as I was saying, when the delta is large the calculations in tc_windup and 
>> bintime_off give slightly different results and that can lead to a 
>> discontinuity of the time when timehands are switched.
> 
> A quick follow-up.
> I think that both tc_windup and bintime_off have fundamentally correct 
> calculations but with different precision.  Both seem to produce values slightly 
> greater than a "true" value where the bintime fractional delta would be 
> calculated as tc_delta * 2^64 / tc_frequency.  That's because of how th_scale is 
> calculated.
> 
> When the timecounter delta is greater than the frequency then the value in 
> tc_windup is closer to the true value because it accounts for whole seconds 
> precisely: a tc_frequency number of timecounter ticks is equal to one second. 
> bintime_off, however, converts both whole seconds and fractions using th_scale. 
>   So, its result is consistently greater when the delta is longer than a second.
> 
> E.g., in my environment: tc_frequency = 14318180, th_scale = 1288420532460.
> For a delta of 14318180 (== tc_frequency) tc_windup calculates a one second 
> advance, bt = { 1, 0 }.
> bintime_off for the same delta will produce bt = { 1, 1093027638570944 }.
> The difference is minuscule, just 59 ppm in relative terms.
> But it's 59 microseconds of "jumping back in time".
> 
> I think that the precision of bintime_off is sufficient and its calculations are 
> faster, so I think that it's better to use the same calculations in tc_windup as 
> well.  Especially given that they are identical for sub-second deltas and longer 
> deltas should be extremely rare.
> 
> I am working on patch to implement this.

The promised patch: https://people.freebsd.org/~avg/kern-tc-add-delta.diff

-- 
Andriy Gapon