Report task rate/Hz based on execution of whole state machine, not individual states where appropriate

This commit is contained in:
Steve Evans 2021-03-31 22:57:04 +01:00
parent 07c32e7302
commit 3087d10f53
4 changed files with 18 additions and 13 deletions

View File

@ -170,18 +170,20 @@ bool taskUpdateRxMainInProgress()
static void taskUpdateRxMain(timeUs_t currentTimeUs) static void taskUpdateRxMain(timeUs_t currentTimeUs)
{ {
// Where we are using a state machine call ignoreTaskTime() for all states bar one
if (rxState != MODES) {
ignoreTaskTime();
}
switch (rxState) { switch (rxState) {
default: default:
case CHECK: case CHECK:
ignoreTaskTime();
rxState = PROCESS; rxState = PROCESS;
break; break;
case PROCESS: case PROCESS:
ignoreTaskTime();
if (!processRx(currentTimeUs)) { if (!processRx(currentTimeUs)) {
rxState = CHECK; rxState = CHECK;
break; break;
} }
rxState = MODES; rxState = MODES;
@ -193,7 +195,6 @@ static void taskUpdateRxMain(timeUs_t currentTimeUs)
break; break;
case UPDATE: case UPDATE:
ignoreTaskTime();
// updateRcCommands sets rcCommand, which is needed by updateAltHoldState and updateSonarAltHoldState // updateRcCommands sets rcCommand, which is needed by updateAltHoldState and updateSonarAltHoldState
updateRcCommands(); updateRcCommands();
updateArmingStatus(); updateArmingStatus();

View File

@ -299,7 +299,6 @@ FAST_CODE timeUs_t schedulerExecuteTask(task_t *selectedTask, timeUs_t currentTi
if (selectedTask) { if (selectedTask) {
currentTask = selectedTask; currentTask = selectedTask;
ignoreCurrentTaskTime = false; ignoreCurrentTaskTime = false;
selectedTask->taskLatestDeltaTimeUs = cmpTimeUs(currentTimeUs, selectedTask->lastExecutedAtUs);
#if defined(USE_TASK_STATISTICS) #if defined(USE_TASK_STATISTICS)
float period = currentTimeUs - selectedTask->lastExecutedAtUs; float period = currentTimeUs - selectedTask->lastExecutedAtUs;
#endif #endif
@ -314,6 +313,8 @@ FAST_CODE timeUs_t schedulerExecuteTask(task_t *selectedTask, timeUs_t currentTi
selectedTask->taskFunc(currentTimeBeforeTaskCallUs); selectedTask->taskFunc(currentTimeBeforeTaskCallUs);
taskExecutionTimeUs = micros() - currentTimeBeforeTaskCallUs; taskExecutionTimeUs = micros() - currentTimeBeforeTaskCallUs;
if (!ignoreCurrentTaskTime) { if (!ignoreCurrentTaskTime) {
selectedTask->taskLatestDeltaTimeUs = cmpTimeUs(currentTimeUs, selectedTask->lastStatsAtUs);
selectedTask->lastStatsAtUs = currentTimeUs;
selectedTask->movingSumExecutionTimeUs += taskExecutionTimeUs - selectedTask->movingSumExecutionTimeUs / TASK_STATS_MOVING_SUM_COUNT; selectedTask->movingSumExecutionTimeUs += taskExecutionTimeUs - selectedTask->movingSumExecutionTimeUs / TASK_STATS_MOVING_SUM_COUNT;
selectedTask->movingSumDeltaTimeUs += selectedTask->taskLatestDeltaTimeUs - selectedTask->movingSumDeltaTimeUs / TASK_STATS_MOVING_SUM_COUNT; selectedTask->movingSumDeltaTimeUs += selectedTask->taskLatestDeltaTimeUs - selectedTask->movingSumDeltaTimeUs / TASK_STATS_MOVING_SUM_COUNT;
selectedTask->maxExecutionTimeUs = MAX(selectedTask->maxExecutionTimeUs, taskExecutionTimeUs); selectedTask->maxExecutionTimeUs = MAX(selectedTask->maxExecutionTimeUs, taskExecutionTimeUs);
@ -326,6 +327,7 @@ FAST_CODE timeUs_t schedulerExecuteTask(task_t *selectedTask, timeUs_t currentTi
} else } else
#endif #endif
{ {
selectedTask->taskLatestDeltaTimeUs = cmpTimeUs(currentTimeUs, selectedTask->lastExecutedAtUs);
selectedTask->taskFunc(currentTimeUs); selectedTask->taskFunc(currentTimeUs);
} }
} }

View File

@ -170,24 +170,25 @@ typedef struct {
#endif #endif
bool (*checkFunc)(timeUs_t currentTimeUs, timeDelta_t currentDeltaTimeUs); bool (*checkFunc)(timeUs_t currentTimeUs, timeDelta_t currentDeltaTimeUs);
void (*taskFunc)(timeUs_t currentTimeUs); void (*taskFunc)(timeUs_t currentTimeUs);
timeDelta_t desiredPeriodUs; // target period of execution timeDelta_t desiredPeriodUs; // target period of execution
const int8_t staticPriority; // dynamicPriority grows in steps of this size const int8_t staticPriority; // dynamicPriority grows in steps of this size
// Scheduling // Scheduling
uint16_t dynamicPriority; // measurement of how old task was last executed, used to avoid task starvation uint16_t dynamicPriority; // measurement of how old task was last executed, used to avoid task starvation
uint16_t taskAgeCycles; uint16_t taskAgeCycles;
timeDelta_t taskLatestDeltaTimeUs; timeDelta_t taskLatestDeltaTimeUs;
timeUs_t lastExecutedAtUs; // last time of invocation timeUs_t lastExecutedAtUs; // last time of invocation
timeUs_t lastSignaledAtUs; // time of invocation event for event-driven tasks timeUs_t lastSignaledAtUs; // time of invocation event for event-driven tasks
timeUs_t lastDesiredAt; // time of last desired execution timeUs_t lastDesiredAt; // time of last desired execution
#if defined(USE_TASK_STATISTICS) #if defined(USE_TASK_STATISTICS)
// Statistics // Statistics
float movingAverageCycleTimeUs; float movingAverageCycleTimeUs;
timeUs_t movingSumExecutionTimeUs; // moving sum over 32 samples timeUs_t movingSumExecutionTimeUs; // moving sum over 32 samples
timeUs_t movingSumDeltaTimeUs; // moving sum over 32 samples timeUs_t movingSumDeltaTimeUs; // moving sum over 32 samples
timeUs_t maxExecutionTimeUs; timeUs_t maxExecutionTimeUs;
timeUs_t totalExecutionTimeUs; // total time consumed by task since boot timeUs_t totalExecutionTimeUs; // total time consumed by task since boot
timeUs_t lastStatsAtUs; // time of last stats gathering for rate calculation
#if defined(USE_LATE_TASK_STATISTICS) #if defined(USE_LATE_TASK_STATISTICS)
uint32_t runCount; uint32_t runCount;
uint32_t lateCount; uint32_t lateCount;

View File

@ -353,6 +353,7 @@ TEST(SchedulerUnittest, TestSingleTask)
} }
setTaskEnabled(TASK_ACCEL, true); setTaskEnabled(TASK_ACCEL, true);
tasks[TASK_ACCEL].lastExecutedAtUs = 1000; tasks[TASK_ACCEL].lastExecutedAtUs = 1000;
tasks[TASK_ACCEL].lastStatsAtUs = 1000;
simulatedTime = 2050; simulatedTime = 2050;
// run the scheduler and check the task has executed // run the scheduler and check the task has executed
scheduler(); scheduler();