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

Make the scheduler tickless #201

Merged
merged 1 commit into from
Jun 7, 2024
Merged

Make the scheduler tickless #201

merged 1 commit into from
Jun 7, 2024

Conversation

davidchisnall
Copy link
Collaborator

@davidchisnall davidchisnall commented Apr 12, 2024

Timers are now not set for a regular tick, they are set when a thread
may be preempted. Specifically, the timer is set to the next timeout
that will trigger a scheduling operation. This avoids timers
triggering a switch to the scheduler to do nothing (resume the currently
running thread).

This means that if a thread sleeps for ten ticks while another runs, we
will get one timer interrupt ten ticks in the future, rather than ten
interrupts one tick apart.

This means that ticks are now calculated retroactively based on elapsed
time, rather than counted on each context switch.

This, in turn, necessitates some small API changes. We previously
conflated two things:

  • Sleep for N * (tick duration)
  • Yield and allow lower-priority threads to run for, at most, N * (tick
    duration)

These are now deconflated by adding a second parameter to thread_sleep.
Most sleeps are of the second form and so this is the default.

This reduces the time taken to run the test suite on Sonata by around
30% and in the Ibex SAFE simulator by 13%.

@davidchisnall davidchisnall marked this pull request as draft April 12, 2024 16:55
@davidchisnall davidchisnall marked this pull request as ready for review June 5, 2024 12:27
@davidchisnall davidchisnall force-pushed the tickless branch 5 times, most recently from 7066e1d to 26bd4a5 Compare June 5, 2024 12:46
Copy link
Collaborator

@rmn30 rmn30 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't understand scheduler enough to fully comment but some suggestions.

@@ -335,7 +336,13 @@ debug_log_message_write(const char *context,
DebugPrinter printer;
printer.write("\x1b[35m");
printer.write(context);
#if 0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make make this a compile time option?

Suggested change
#if 0
#if defined(DEBUG_SHOW_THREAD_ID)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This probably wants wiring up in an option, it's mostly disabled for now so that I can do timing checks without it there (it slows things down quite a bit with a real UART).

sdk/core/scheduler/thread.h Outdated Show resolved Hide resolved
sdk/core/scheduler/timer.h Outdated Show resolved Hide resolved
sdk/core/scheduler/timer.h Outdated Show resolved Hide resolved
sdk/core/switcher/entry.S Outdated Show resolved Hide resolved
sdk/include/FreeRTOS-Compat/task.h Outdated Show resolved Hide resolved
sdk/core/scheduler/timer.h Outdated Show resolved Hide resolved
sdk/core/scheduler/timer.h Outdated Show resolved Hide resolved
sdk/include/platform/generic-riscv/platform-timer.hh Outdated Show resolved Hide resolved
@davidchisnall davidchisnall force-pushed the tickless branch 3 times, most recently from 4d12dee to cd95d17 Compare June 5, 2024 15:39
sdk/core/scheduler/main.cc Outdated Show resolved Hide resolved
ThreadState state : 2;
/**
* If the thread is yielding, it may be scheduled before its timeout
* expires, as long as no other threads are runnable.
Copy link
Contributor

@nwf-msr nwf-msr Jun 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might leave off the "as long as..." part here and just point at the docs for ThreadSleepNoEarlyWake or the comment down in the actual wake code.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's fine to have duplicated explanations, especially in doc comments. If you mouse over a use of this field in VS Code (for example), you'll get this doc comment pop up. Having it tell you to go and read other docs can be annoying.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fair. In which case...

Suggested change
* expires, as long as no other threads are runnable.
* expires, as long as no other threads are runnable or
* sleeping with shorter timeouts.

Copy link
Contributor

@nwf-msr nwf-msr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two nits, but it LGTM.

/**
* Wake any threads that were sleeping until a timeout before the
* current time. This also wakes yielded threads if there are no
* runnable threads.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* runnable threads.
* threads runnable or sleeping with earlier timeouts.

ThreadState state : 2;
/**
* If the thread is yielding, it may be scheduled before its timeout
* expires, as long as no other threads are runnable.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fair. In which case...

Suggested change
* expires, as long as no other threads are runnable.
* expires, as long as no other threads are runnable or
* sleeping with shorter timeouts.

This commit incorporates several changes:

Timers are now not set for a regular tick, they are set when a thread
may be preempted.  Specifically, the timer is set to the next timeout
that will trigger a scheduling operation.  This avoids timers
triggering a switch to the scheduler to do nothing (resume the currently
running thread).

This means that if a thread sleeps for ten ticks while another runs, we
will get one timer interrupt ten ticks in the future, rather than ten
interrupts one tick apart.

This means that ticks are now calculated retroactively based on elapsed
time, rather than counted on each context switch.

This, in turn, necessitates some small API changes.  We previously
conflated two things:

 - Sleep for N * (tick duration)
 - Yield and allow lower-priority threads to run for, at most, N * (tick
   duration)

These are now deconflated by adding a second parameter to thread_sleep.
Most sleeps are of the second form and so this is the default.

This reduces the time taken to run the test suite on Sonata by around
30% and in the Ibex SAFE simulator by 13%.
@davidchisnall davidchisnall enabled auto-merge (rebase) June 7, 2024 10:28
@davidchisnall davidchisnall merged commit dcbb196 into main Jun 7, 2024
7 checks passed
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.

3 participants