fome-fw/firmware/controllers/system/timer/single_timer_executor.cpp

181 lines
5.4 KiB
C++
Raw Normal View History

2015-07-10 06:01:56 -07:00
/**
* @file SingleTimerExecutor.cpp
*
2019-05-05 06:41:44 -07:00
* This class combines the powers of a 1MHz hardware timer from microsecond_timer.cpp
2015-07-10 06:01:56 -07:00
* and pending events queue event_queue.cpp
*
* As of version 2.6.x, ChibiOS tick-based kernel is not capable of scheduling events
* with the level of precision we need, and realistically it should not.
*
2019-05-05 06:41:44 -07:00
* Update: actually newer ChibiOS has tickless mode and what we have here is pretty much the same thing :)
* open question if rusEfi should simply migrate to ChibiOS tickless scheduling (which would increase coupling with ChibiOS)
*
* See https://rusefi.com/forum/viewtopic.php?f=5&t=373&start=360#p30895
2018-12-24 16:36:03 -08:00
* for some performance data: with 'debug' firmware we spend about 5% of CPU in TIM5 handler which seem to be executed
* about 1500 times a second
*
2015-07-10 06:01:56 -07:00
* http://sourceforge.net/p/rusefi/tickets/24/
*
* @date: Apr 18, 2014
2020-01-13 18:57:43 -08:00
* @author Andrey Belomutskiy, (c) 2012-2020
2015-07-10 06:01:56 -07:00
*/
2019-07-05 17:03:32 -07:00
#include "global.h"
#include "os_access.h"
2019-03-29 06:11:13 -07:00
#include "single_timer_executor.h"
2015-07-10 06:01:56 -07:00
#include "efitime.h"
2019-10-11 17:43:21 -07:00
#include "perf_trace.h"
2015-07-10 06:01:56 -07:00
2019-04-12 19:07:03 -07:00
#if EFI_SIGNAL_EXECUTOR_ONE_TIMER
2015-07-10 06:01:56 -07:00
#include "microsecond_timer.h"
2020-05-25 10:02:05 -07:00
#include "tunerstudio_outputs.h"
2019-07-06 17:15:49 -07:00
#include "os_util.h"
2015-07-10 06:01:56 -07:00
#include "engine.h"
EXTERN_ENGINE;
2015-07-10 06:01:56 -07:00
2017-05-21 07:25:35 -07:00
uint32_t hwSetTimerDuration;
2015-07-10 06:01:56 -07:00
2020-04-24 18:21:04 -07:00
void globalTimerCallback() {
efiAssertVoid(CUSTOM_ERR_6624, getCurrentRemainingStack() > EXPECTED_REMAINING_STACK, "lowstck#2y");
2015-07-10 06:01:56 -07:00
2019-01-27 23:59:14 -08:00
___engine.executor.onTimerCallback();
2015-07-10 06:01:56 -07:00
}
SingleTimerExecutor::SingleTimerExecutor()
// 8us is roughly the cost of the interrupt + overhead of a single timer event
: queue(US2NT(8))
{
2015-07-10 06:01:56 -07:00
}
void SingleTimerExecutor::scheduleForLater(scheduling_s *scheduling, int delayUs, action_s action) {
scheduleByTimestamp(scheduling, getTimeNowUs() + delayUs, action);
}
2018-09-10 19:42:24 -07:00
/**
* @brief Schedule an event at specific delay after now
*
* Invokes event callback after the specified amount of time.
2018-09-10 19:42:24 -07:00
* callback would be executed either on ISR thread or current thread if we would need to execute right away
*
* @param [in, out] scheduling Data structure to keep this event in the collection.
* @param [in] delayUs the number of microseconds before the output signal immediate output if delay is zero.
* @param [in] dwell the number of ticks of output duration.
2018-09-10 19:42:24 -07:00
*/
void SingleTimerExecutor::scheduleByTimestamp(scheduling_s *scheduling, efitimeus_t timeUs, action_s action) {
scheduleByTimestampNt(scheduling, US2NT(timeUs), action);
}
void SingleTimerExecutor::scheduleByTimestampNt(scheduling_s* scheduling, efitime_t nt, action_s action) {
2019-10-13 13:14:08 -07:00
ScopePerf perf(PE::SingleTimerExecutorScheduleByTimestamp);
2020-01-18 21:28:58 -08:00
#if EFI_ENABLE_ASSERTS
int32_t deltaTimeNt = (int32_t)nt - getTimeNowLowerNt();
2020-01-18 21:28:58 -08:00
if (deltaTimeNt >= TOO_FAR_INTO_FUTURE_NT) {
2020-01-18 21:28:58 -08:00
// we are trying to set callback for too far into the future. This does not look right at all
firmwareError(CUSTOM_ERR_TASK_TIMER_OVERFLOW, "scheduleByTimestampNt() too far: %d", deltaTimeNt);
2020-01-18 21:28:58 -08:00
return;
}
#endif
2017-06-07 19:55:05 -07:00
scheduleCounter++;
// Lock for queue insertion - we may already be locked, but that's ok
chibios_rt::CriticalSectionLocker csl;
bool needToResetTimer = queue.insertTask(scheduling, nt, action);
2015-07-10 06:01:56 -07:00
if (!reentrantFlag) {
executeAllPendingActions();
2015-07-10 06:01:56 -07:00
if (needToResetTimer) {
scheduleTimerCallback();
}
}
}
void SingleTimerExecutor::onTimerCallback() {
2017-06-07 19:55:05 -07:00
timerCallbackCounter++;
chibios_rt::CriticalSectionLocker csl;
executeAllPendingActions();
2015-07-10 06:01:56 -07:00
scheduleTimerCallback();
}
/*
* this private method is executed under lock
*/
void SingleTimerExecutor::executeAllPendingActions() {
2019-10-11 17:43:21 -07:00
ScopePerf perf(PE::SingleTimerExecutorDoExecute);
executeAllPendingActionsInvocationCounter++;
2015-07-10 06:01:56 -07:00
/**
* Let's execute actions we should execute at this point.
* reentrantFlag takes care of the use case where the actions we are executing are scheduling
* further invocations
*/
reentrantFlag = true;
2015-07-10 06:01:56 -07:00
/**
* 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?
*/
2020-10-05 05:57:00 -07:00
int executeCounter = 0;
2020-10-05 05:57:00 -07:00
bool didExecute;
do {
2015-07-10 06:01:56 -07:00
efitick_t nowNt = getTimeNowNt();
2020-10-05 05:57:00 -07:00
didExecute = queue.executeOne(nowNt);
if (executeCounter++ == 10000) {
firmwareError(CUSTOM_ERR_LOCK_ISSUE, "Looks like firmware is really busy");
}
2020-10-05 05:57:00 -07:00
} while (didExecute);
2015-07-10 06:01:56 -07:00
if (!isLocked()) {
2017-05-21 07:15:57 -07:00
firmwareError(CUSTOM_ERR_LOCK_ISSUE, "Someone has stolen my lock");
2015-07-10 06:01:56 -07:00
return;
}
reentrantFlag = false;
}
/**
* This method is always invoked under a lock
*/
void SingleTimerExecutor::scheduleTimerCallback() {
2019-10-13 13:14:08 -07:00
ScopePerf perf(PE::SingleTimerExecutorScheduleTimerCallback);
2015-07-10 06:01:56 -07:00
/**
* Let's grab fresh time value
*/
efitick_t nowNt = getTimeNowNt();
expected<efitick_t> nextEventTimeNt = queue.getNextEventTime(nowNt);
if (!nextEventTimeNt) {
2015-07-10 06:01:56 -07:00
return; // no pending events in the queue
}
efiAssertVoid(CUSTOM_ERR_6625, nextEventTimeNt.Value > nowNt, "setTimer constraint");
setHardwareSchedulerTimer(nowNt, nextEventTimeNt.Value);
2015-07-10 06:01:56 -07:00
}
2019-02-27 14:12:52 -08:00
void initSingleTimerExecutorHardware(void) {
2015-07-10 06:01:56 -07:00
initMicrosecondTimer();
}
2017-06-07 19:55:05 -07:00
void executorStatistics() {
2017-06-07 20:04:56 -07:00
if (engineConfiguration->debugMode == DBG_EXECUTOR) {
2019-04-12 19:07:03 -07:00
#if EFI_TUNER_STUDIO && EFI_SIGNAL_EXECUTOR_ONE_TIMER
2019-01-27 23:59:14 -08:00
tsOutputChannels.debugIntField1 = ___engine.executor.timerCallbackCounter;
tsOutputChannels.debugIntField2 = ___engine.executor.executeAllPendingActionsInvocationCounter;
2019-01-27 23:59:14 -08:00
tsOutputChannels.debugIntField3 = ___engine.executor.scheduleCounter;
#endif /* EFI_TUNER_STUDIO */
2017-06-07 19:55:05 -07:00
}
}
#endif /* EFI_SIGNAL_EXECUTOR_ONE_TIMER */
2015-07-10 06:01:56 -07:00