diff --git a/firmware/controllers/system/SingleTimerExecutor.cpp b/firmware/controllers/system/SingleTimerExecutor.cpp index c14b975ce2..d6ff2011a2 100644 --- a/firmware/controllers/system/SingleTimerExecutor.cpp +++ b/firmware/controllers/system/SingleTimerExecutor.cpp @@ -15,6 +15,7 @@ #include "SingleTimerExecutor.h" #include "efitime.h" +#include "efilib2.h" #if EFI_PROD_CODE #include "microsecond_timer.h" @@ -35,6 +36,10 @@ extern schfunc_t globalTimerCallback; static uint64_t nextEventTimeNt = 0; static uint64_t hwAlarmTime = 0; +uint32_t beforeHwSetTimer; +uint32_t hwSetTimerTime; +uint32_t lastExecutionCount; + static void executorCallback(void *arg) { (void)arg; efiAssertVoid(getRemainingStack(chThdSelf()) > 256, "lowstck#2y"); @@ -97,19 +102,22 @@ void Executor::doExecute() { * further invocations */ reentrantFlag = true; - bool shouldExecute = true; + int shouldExecute = 1; /** * in real life it could be that while we executing listeners time passes and it's already time to execute * next listeners. * TODO: add a counter & figure out a limit of iterations? */ - while (shouldExecute) { + int totalExecuted = 0; + while (shouldExecute > 0) { /** * It's worth noting that that the actions might be adding new actions into the queue */ uint64_t nowNt = getTimeNowNt(); shouldExecute = queue.executeAll(nowNt); + totalExecuted += shouldExecute; } + lastExecutionCount = totalExecuted; if (!isLocked()) { firmwareError("Someone has stolen my lock"); return; @@ -127,7 +135,9 @@ void Executor::doExecute() { if (nextEventTimeNt == EMPTY_QUEUE) return; // no pending events in the queue hwAlarmTime = NT2US(nextEventTimeNt - nowNt); + beforeHwSetTimer = GET_TIMESTAMP(); setHardwareUsTimer(hwAlarmTime == 0 ? 1 : hwAlarmTime); + hwSetTimerTime = GET_TIMESTAMP() - beforeHwSetTimer; } /** diff --git a/firmware/controllers/system/event_queue.cpp b/firmware/controllers/system/event_queue.cpp index 7a433e7389..7828291371 100644 --- a/firmware/controllers/system/event_queue.cpp +++ b/firmware/controllers/system/event_queue.cpp @@ -82,9 +82,9 @@ uint32_t lastEventQueueTime; /** * Invoke all pending actions prior to specified timestamp - * @return true if at least one action was executed + * @return number of executed actions */ -bool EventQueue::executeAll(uint64_t now) { +int EventQueue::executeAll(uint64_t now) { scheduling_s * current, *tmp; scheduling_s * executionList = NULL; @@ -109,7 +109,6 @@ bool EventQueue::executeAll(uint64_t now) { * we need safe iteration here because 'callback' might change change 'current->next' * while re-inserting it into the queue from within the callback */ - bool result = (executionList != NULL); LL_FOREACH_SAFE(executionList, current, tmp) { uint32_t before = GET_TIMESTAMP(); @@ -123,7 +122,7 @@ bool EventQueue::executeAll(uint64_t now) { // cost++; // } } - return result; + return executionCounter; } int EventQueue::size(void) { diff --git a/firmware/controllers/system/event_queue.h b/firmware/controllers/system/event_queue.h index 7306f376e0..fffa34d461 100644 --- a/firmware/controllers/system/event_queue.h +++ b/firmware/controllers/system/event_queue.h @@ -45,7 +45,7 @@ public: void insertTask(scheduling_s *scheduling, uint64_t timeX, schfunc_t callback, void *param); - bool executeAll(uint64_t now); + int executeAll(uint64_t now); uint64_t getNextEventTime(uint64_t nowUs); void clear(void);