Skip to content

Commit

Permalink
Tickless scheduler step 3: Correct for tick lengths.
Browse files Browse the repository at this point in the history
Collect the error in the tick rate and correct the current tick to be a
close approximation of the value that we'd have with a ticky scheduler.
  • Loading branch information
davidchisnall committed Apr 12, 2024
1 parent 7023916 commit df66aa3
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions sdk/core/scheduler/timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ namespace

class Timer final : private TimerCore
{
inline static uint64_t lastTickTime = 0;
inline static uint64_t lastTickTime = 0;
inline static uint32_t accumulatedTickError = 0;

public:
static void interrupt_setup()
{
Expand All @@ -41,25 +43,40 @@ namespace

static void update()
{

if (Thread::waitingList == nullptr)
{
clear();
}
else
{
setnext(TIMERCYCLES_PER_TICK * (Thread::waitingList->expiryTime - Thread::ticksSinceBoot));
setnext(
TIMERCYCLES_PER_TICK *
(Thread::waitingList->expiryTime - Thread::ticksSinceBoot));
}
}

static void expiretimers()
{
// TODO: Should be reading the timer's time, not the core's time.
// They are currently the same value, but that's not guaranteed.
uint64_t now = rdcycle64();
uint64_t now = rdcycle64();
uint32_t elapsed = now - lastTickTime;
int32_t error = elapsed % TIMERCYCLES_PER_TICK;
if (elapsed < TIMERCYCLES_PER_TICK)
{
error = TIMERCYCLES_PER_TICK - error;
}
accumulatedTickError += error;
int32_t errorDirection = accumulatedTickError < 0 ? -1 : 1;
int32_t absoluteError = accumulatedTickError * errorDirection;
if (absoluteError >= TIMERCYCLES_PER_TICK)
{
Thread::ticksSinceBoot += errorDirection;
accumulatedTickError += TIMERCYCLES_PER_TICK * -errorDirection;
}
lastTickTime = now;
Thread::ticksSinceBoot += std::max(1U, elapsed / TIMERCYCLES_PER_TICK);
Thread::ticksSinceBoot +=
std::max(1U, elapsed / TIMERCYCLES_PER_TICK);
if (Thread::waitingList == nullptr)
{
return;
Expand Down

0 comments on commit df66aa3

Please sign in to comment.