On 8/31/21 5:27 AM, Anton Ertl wrote:
...
> If you cannot get high precision from the OS, one
> alternative is to use the OS nanosleep(), pselect() and the like for,
> n-e ms (where e depends on the imprecision of the OS call), and use
> busy waiting for the rest. This does not guarantee that the scheduler
> will not interrupt your thread at an inconvenient time, but it lowers
> the probability.
>
All approaches to implementing a delay on a system with interrupts will
result in a probability distribution for the deltas, where delta is the
actual elapsed time minus the requested delay time. However, the polling
implementation dramatically reduces the width of the distribution
compared to calling a sleep function. Representative statistics for the
distributions as a function of delay are given below for the two
approaches. The mu and sigma are the mean and standard deviation in
microseconds for the distribution of deltas in 1000 trials for the word MS.
Gforth 64-bit (nanosleep call)
-------
Delay (ms), mu (us), sigma (us)
1, 118, 31.4
10, 300, 110.
100, 388, 86.7
500, 791, 114.
1000, 1223, 253.
kForth-64 (polling system clock)
---------
Delay (ms), mu (us), sigma (us)
1, 0.009, 0.226
10, 0.037, 0.553
100, 0.017, 0.339
500, 0.021, 0.216
1000, 0.035, 0.382
The nanosleep call used by Gforth leads to a clear systematic drift to
larger positive delta in the mean, with increasing delay, and this is to
be expected. At 1000 ms delay, the average delay produced by MS is more
than 1 ms greater. The width of the distribution for this approach also
shows an overall systematic drift to increasing width with delay.
It is difficult to tell, over this range of delays, whether or not there
is a similar, but considerably smaller, systematic drift in the mean vs
delay for the polling approach -- I expect there to be a systematic
error with increasing delay. There is also no clear indication that the
width of the distribution is increasing with delay, over this range of
delays, for the polling approach. The ratio of the sigmas vs delay is
given below.
Delay (ms) sigma(nanosleep)/sigma(polling)
1, 3490
10, 199
100, 255
500, 528
1000, 663
The above results are probably best case results on my system, where
nothing beyond system processes were running while the measurements were
performed, although several applications were open.
kForth provides the following timing words:
Precision Delays: MS US
Non-Time-Critical Delays: USLEEP
Fetch Time: MS@ US2@ TIME&DATE
--
Krishna