From c1d27b5a65642ddf732f8ae3fbc65a14df8dc250 Mon Sep 17 00:00:00 2001 From: Martin Budden Date: Sun, 8 Jan 2017 11:45:25 +0000 Subject: [PATCH] Added CLI option to turn off task statistics --- src/main/config/config_master.h | 1 + src/main/fc/config.c | 1 + src/main/fc/fc_init.c | 2 +- src/main/fc/serial_cli.c | 3 ++ src/main/scheduler/scheduler.c | 56 ++++++++++++++++++++++----------- src/main/scheduler/scheduler.h | 1 + 6 files changed, 45 insertions(+), 19 deletions(-) diff --git a/src/main/config/config_master.h b/src/main/config/config_master.h index f704f72f51..60f2624a59 100644 --- a/src/main/config/config_master.h +++ b/src/main/config/config_master.h @@ -137,6 +137,7 @@ typedef struct master_s { pidConfig_t pidConfig; uint8_t debug_mode; // Processing denominator for PID controller vs gyro sampling rate + uint8_t task_statistics; gyroConfig_t gyroConfig; compassConfig_t compassConfig; diff --git a/src/main/fc/config.c b/src/main/fc/config.c index 2ad65b488c..3ab78bc41b 100755 --- a/src/main/fc/config.c +++ b/src/main/fc/config.c @@ -620,6 +620,7 @@ void createDefaultConfig(master_t *config) config->gyroConfig.gyro_soft_notch_cutoff_2 = 100; config->debug_mode = DEBUG_MODE; + config->task_statistics = true; resetAccelerometerTrims(&config->accelerometerConfig.accZero); diff --git a/src/main/fc/fc_init.c b/src/main/fc/fc_init.c index a46d2dcaca..b9a5e95a6e 100644 --- a/src/main/fc/fc_init.c +++ b/src/main/fc/fc_init.c @@ -568,6 +568,6 @@ void init(void) motorControlEnable = true; fcTasksInit(); + schedulerSetCalulateTaskStatistics(masterConfig.task_statistics); systemState |= SYSTEM_STATE_READY; } - diff --git a/src/main/fc/serial_cli.c b/src/main/fc/serial_cli.c index 9126c7ccb3..424d51b49a 100755 --- a/src/main/fc/serial_cli.c +++ b/src/main/fc/serial_cli.c @@ -483,6 +483,9 @@ typedef struct { } clivalue_t; const clivalue_t valueTable[] = { +#ifndef SKIP_TASK_STATISTICS + { "task_statistics", VAR_INT8 | MASTER_VALUE | MODE_LOOKUP, &masterConfig.task_statistics, .config.lookup = { TABLE_OFF_ON } }, +#endif { "mid_rc", VAR_UINT16 | MASTER_VALUE, &rxConfig()->midrc, .config.minmax = { 1200, 1700 } }, { "min_check", VAR_UINT16 | MASTER_VALUE, &rxConfig()->mincheck, .config.minmax = { PWM_RANGE_ZERO, PWM_RANGE_MAX } }, { "max_check", VAR_UINT16 | MASTER_VALUE, &rxConfig()->maxcheck, .config.minmax = { PWM_RANGE_ZERO, PWM_RANGE_MAX } }, diff --git a/src/main/scheduler/scheduler.c b/src/main/scheduler/scheduler.c index 83a130a0d2..137dba6702 100755 --- a/src/main/scheduler/scheduler.c +++ b/src/main/scheduler/scheduler.c @@ -44,6 +44,7 @@ static cfTask_t *currentTask = NULL; static uint32_t totalWaitingTasks; static uint32_t totalWaitingTasksSamples; +static bool calculateTaskStatistics; uint16_t averageSystemLoadPercent = 0; @@ -156,8 +157,11 @@ void getTaskInfo(cfTaskId_e taskId, cfTaskInfo_t * taskInfo) void rescheduleTask(cfTaskId_e taskId, uint32_t newPeriodMicros) { - if (taskId == TASK_SELF || taskId < TASK_COUNT) { - cfTask_t *task = taskId == TASK_SELF ? currentTask : &cfTasks[taskId]; + if (taskId == TASK_SELF) { + cfTask_t *task = currentTask; + task->desiredPeriod = MAX(SCHEDULER_DELAY_LIMIT, newPeriodMicros); // Limit delay to 100us (10 kHz) to prevent scheduler clogging + } else if (taskId < TASK_COUNT) { + cfTask_t *task = &cfTasks[taskId]; task->desiredPeriod = MAX(SCHEDULER_DELAY_LIMIT, newPeriodMicros); // Limit delay to 100us (10 kHz) to prevent scheduler clogging } } @@ -185,6 +189,11 @@ uint32_t getTaskDeltaTime(cfTaskId_e taskId) } } +void schedulerSetCalulateTaskStatistics(bool calculateTaskStatisticsToUse) +{ + calculateTaskStatistics = calculateTaskStatisticsToUse; +} + void schedulerResetTaskStatistics(cfTaskId_e taskId) { #ifdef SKIP_TASK_STATISTICS @@ -204,6 +213,7 @@ void schedulerResetTaskStatistics(cfTaskId_e taskId) void schedulerInit(void) { + calculateTaskStatistics = true; queueClear(); queueAdd(&cfTasks[TASK_SYSTEM]); } @@ -234,24 +244,28 @@ void scheduler(void) uint16_t waitingTasks = 0; for (cfTask_t *task = queueFirst(); task != NULL; task = queueNext()) { // Task has checkFunc - event driven - if (task->checkFunc != NULL) { + if (task->checkFunc) { +#if defined(SCHEDULER_DEBUG) const timeUs_t currentTimeBeforeCheckFuncCall = micros(); +#else + const timeUs_t currentTimeBeforeCheckFuncCall = currentTimeUs; +#endif // Increase priority for event driven tasks if (task->dynamicPriority > 0) { task->taskAgeCycles = 1 + ((currentTimeUs - task->lastSignaledAt) / task->desiredPeriod); task->dynamicPriority = 1 + task->staticPriority * task->taskAgeCycles; waitingTasks++; } else if (task->checkFunc(currentTimeBeforeCheckFuncCall, currentTimeBeforeCheckFuncCall - task->lastExecutedAt)) { -#if defined(SCHEDULER_DEBUG) || !defined(SKIP_TASK_STATISTICS) - const uint32_t checkFuncExecutionTime = micros() - currentTimeBeforeCheckFuncCall; -#endif #if defined(SCHEDULER_DEBUG) - DEBUG_SET(DEBUG_SCHEDULER, 3, checkFuncExecutionTime); + DEBUG_SET(DEBUG_SCHEDULER, 3, micros() - currentTimeBeforeCheckFuncCall); #endif #ifndef SKIP_TASK_STATISTICS - checkFuncMovingSumExecutionTime += checkFuncExecutionTime - checkFuncMovingSumExecutionTime / MOVING_SUM_COUNT; - checkFuncTotalExecutionTime += checkFuncExecutionTime; // time consumed by scheduler + task - checkFuncMaxExecutionTime = MAX(checkFuncMaxExecutionTime, checkFuncExecutionTime); + if (calculateTaskStatistics) { + const uint32_t checkFuncExecutionTime = micros() - currentTimeBeforeCheckFuncCall; + checkFuncMovingSumExecutionTime += checkFuncExecutionTime - checkFuncMovingSumExecutionTime / MOVING_SUM_COUNT; + checkFuncTotalExecutionTime += checkFuncExecutionTime; // time consumed by scheduler + task + checkFuncMaxExecutionTime = MAX(checkFuncMaxExecutionTime, checkFuncExecutionTime); + } #endif task->lastSignaledAt = currentTimeBeforeCheckFuncCall; task->taskAgeCycles = 1; @@ -287,21 +301,27 @@ void scheduler(void) currentTask = selectedTask; - if (selectedTask != NULL) { + if (selectedTask) { // Found a task that should be run selectedTask->taskLatestDeltaTime = currentTimeUs - selectedTask->lastExecutedAt; selectedTask->lastExecutedAt = currentTimeUs; selectedTask->dynamicPriority = 0; // Execute task - const timeUs_t currentTimeBeforeTaskCall = micros(); - selectedTask->taskFunc(currentTimeBeforeTaskCall); +#ifdef SKIP_TASK_STATISTICS + selectedTask->taskFunc(currentTimeUs); +#else + if (calculateTaskStatistics) { + const timeUs_t currentTimeBeforeTaskCall = micros(); + selectedTask->taskFunc(currentTimeBeforeTaskCall); + const timeUs_t taskExecutionTime = micros() - currentTimeBeforeTaskCall; + selectedTask->movingSumExecutionTime += taskExecutionTime - selectedTask->movingSumExecutionTime / MOVING_SUM_COUNT; + selectedTask->totalExecutionTime += taskExecutionTime; // time consumed by scheduler + task + selectedTask->maxExecutionTime = MAX(selectedTask->maxExecutionTime, taskExecutionTime); + } else { + selectedTask->taskFunc(currentTimeUs); + } -#ifndef SKIP_TASK_STATISTICS - const timeUs_t taskExecutionTime = micros() - currentTimeBeforeTaskCall; - selectedTask->movingSumExecutionTime += taskExecutionTime - selectedTask->movingSumExecutionTime / MOVING_SUM_COUNT; - selectedTask->totalExecutionTime += taskExecutionTime; // time consumed by scheduler + task - selectedTask->maxExecutionTime = MAX(selectedTask->maxExecutionTime, taskExecutionTime); #endif #if defined(SCHEDULER_DEBUG) DEBUG_SET(DEBUG_SCHEDULER, 2, micros() - currentTimeUs - taskExecutionTime); // time spent in scheduler diff --git a/src/main/scheduler/scheduler.h b/src/main/scheduler/scheduler.h index 8fbcd8c246..1df8e1866e 100644 --- a/src/main/scheduler/scheduler.h +++ b/src/main/scheduler/scheduler.h @@ -144,6 +144,7 @@ void getTaskInfo(cfTaskId_e taskId, cfTaskInfo_t *taskInfo); void rescheduleTask(cfTaskId_e taskId, uint32_t newPeriodMicros); void setTaskEnabled(cfTaskId_e taskId, bool newEnabledState); uint32_t getTaskDeltaTime(cfTaskId_e taskId); +void schedulerSetCalulateTaskStatistics(bool calculateTaskStatistics); void schedulerResetTaskStatistics(cfTaskId_e taskId); void schedulerInit(void);