Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve maitake timer wheel #475

Merged
merged 10 commits into from
Mar 17, 2024
Merged

improve maitake timer wheel #475

merged 10 commits into from
Mar 17, 2024

Conversation

hawkw
Copy link
Owner

@hawkw hawkw commented Jan 28, 2024

fixes #474

This commit adds a new type to the `maitake::time` module, called
`Clock`. A `Clock` bundles together a base duration and a `now()`
function, making it easier for libraries to define time sources to be
used with `maitake::time`.
Now that we have a trait for hardware clocks, we expect the hardware
timestamp to always come from the clock, rather than being passed in
when the timer wheel is turned. This commit changes the timer APIs so
that, rather than advancing the timer wheel to a particular `now` time,
we instead call `Timer::turn`, which advances the wheel to "whatever the
`Clock` thinks the current time is". This also means we can get rid of
`pending_ticks`, which I always kinda hated.
It would be really nice to be able to use RDTSC as the timestamp source
on x86_64, as it's the nicest hardware timer available on newer x86
CPUs. This commit starts on implementing a calibration routine for
RDTSC, although it's not quite done.
Unfortunately, my RDTSC calibration code from 9d549a3 is not that good,
and using RDTSC currently results in a bunch of weird time travel bugs.
This commit puts back the less accurate interrupt-driven clock for now.
I'll finish RDTSC later once the timer wheel changes land.
This commit changes the construction of new `Sleep`s to use the nice
`Clock` API.
This commit changes the `Sleep` future in `maitake::time` to turn the
timer to the `Clock`'s current `now()` time when the timer wheel lock is
acquired to register a new `Sleep`. This way, the timer is (at least
occasionally) turned while polling `Sleep`s, in addition to when
scheduled by an interrupt or by a scheduler tick completing. This way,
we fire completing sleeps much more frequently, which should help
improve the timer's accuracy substantially. Because we only do this when
the timer wheel lock already has to be acquired to register the `Sleep`
in the first place, this shouldn't increase contention for the wheel
lock all that much.

Fixes #474
@hawkw hawkw marked this pull request as ready for review February 23, 2024 17:39
@hawkw hawkw enabled auto-merge (rebase) February 23, 2024 17:40
hawkw added a commit that referenced this pull request Feb 25, 2024
hawkw added a commit that referenced this pull request Mar 5, 2024
@hawkw hawkw changed the title [WIP]: improve maitake timer wheel improve maitake timer wheel Mar 17, 2024
@hawkw hawkw disabled auto-merge March 17, 2024 17:07
@hawkw hawkw enabled auto-merge (rebase) March 17, 2024 17:07
This commit adds examples for the new `time::Clock` API, and removes
references to `Timer::advance`/`Timer::force_advance`.
@hawkw hawkw merged commit bab3d4f into main Mar 17, 2024
16 of 17 checks passed
@hawkw hawkw deleted the eliza/clocky branch March 17, 2024 17:28
hawkw added a commit that referenced this pull request Mar 17, 2024
This commit adds a new type to the `maitake::time` module, called
`Clock`. A `Clock` bundles together a base duration and a `now()`
function, making it easier for libraries to define time sources to be
used with `maitake::time`.
hawkw added a commit that referenced this pull request Mar 17, 2024
Now that we have a trait for hardware clocks, we expect the hardware
timestamp to always come from the clock, rather than being passed in
when the timer wheel is turned. This commit changes the timer APIs so
that, rather than advancing the timer wheel to a particular `now` time,
we instead call `Timer::turn`, which advances the wheel to "whatever the
`Clock` thinks the current time is". This also means we can get rid of
`pending_ticks`, which I always kinda hated.
hawkw added a commit that referenced this pull request Mar 17, 2024
It would be really nice to be able to use RDTSC as the timestamp source
on x86_64, as it's the nicest hardware timer available on newer x86
CPUs. This commit starts on implementing a calibration routine for
RDTSC, although it's not quite done.
hawkw added a commit that referenced this pull request Mar 17, 2024
Unfortunately, my RDTSC calibration code from 9d549a3 is not that good,
and using RDTSC currently results in a bunch of weird time travel bugs.
This commit puts back the less accurate interrupt-driven clock for now.
I'll finish RDTSC later once the timer wheel changes land.
hawkw added a commit that referenced this pull request Mar 17, 2024
hawkw added a commit that referenced this pull request Mar 17, 2024
hawkw added a commit that referenced this pull request Mar 17, 2024
This commit changes the construction of new `Sleep`s to use the nice
`Clock` API.
hawkw added a commit that referenced this pull request Mar 17, 2024
This commit changes the `Sleep` future in `maitake::time` to turn the
timer to the `Clock`'s current `now()` time when the timer wheel lock is
acquired to register a new `Sleep`. This way, the timer is (at least
occasionally) turned while polling `Sleep`s, in addition to when
scheduled by an interrupt or by a scheduler tick completing. This way,
we fire completing sleeps much more frequently, which should help
improve the timer's accuracy substantially. Because we only do this when
the timer wheel lock already has to be acquired to register the `Sleep`
in the first place, this shouldn't increase contention for the wheel
lock all that much.

Fixes #474
hawkw added a commit that referenced this pull request Mar 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

fix maitake timer inaccuracy
1 participant