Skip to content

Commit

Permalink
Use attributes template to save instruction or two on dereference
Browse files Browse the repository at this point in the history
  • Loading branch information
haslinghuis committed Jan 11, 2022
1 parent 0da08e1 commit b6409db
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 31 deletions.
4 changes: 2 additions & 2 deletions src/main/fc/tasks.c
Expand Up @@ -312,7 +312,7 @@ static void taskCameraControl(uint32_t currentTime)
task_t tasks[TASK_COUNT];

// Task ID data in .data (initialised data)
task_attributes_t task_attributes[TASK_COUNT] = {
static const task_attributes_t task_attributes[TASK_COUNT] = {
[TASK_SYSTEM] = DEFINE_TASK("SYSTEM", "LOAD", NULL, taskSystemLoad, TASK_PERIOD_HZ(10), TASK_PRIORITY_MEDIUM_HIGH),
[TASK_MAIN] = DEFINE_TASK("SYSTEM", "UPDATE", NULL, taskMain, TASK_PERIOD_HZ(1000), TASK_PRIORITY_MEDIUM_HIGH),
[TASK_SERIAL] = DEFINE_TASK("SERIAL", NULL, NULL, taskHandleSerial, TASK_PERIOD_HZ(100), TASK_PRIORITY_LOW), // 100 Hz should be enough to flush up to 115 bytes @ 115200 baud
Expand Down Expand Up @@ -423,7 +423,7 @@ task_t *getTask(unsigned taskId)
void tasksInit(void)
{
for (int i = 0; i < TASK_COUNT; i++) {
tasks[i].attributes = &task_attributes[i];
tasks[i].attributes = task_attributes[i];
}

schedulerInit();
Expand Down
40 changes: 20 additions & 20 deletions src/main/scheduler/scheduler.c
Expand Up @@ -126,7 +126,7 @@ bool queueAdd(task_t *task)
return false;
}
for (int ii = 0; ii <= taskQueueSize; ++ii) {
if (taskQueueArray[ii] == NULL || taskQueueArray[ii]->attributes->staticPriority < task->attributes->staticPriority) {
if (taskQueueArray[ii] == NULL || taskQueueArray[ii]->attributes.staticPriority < task->attributes.staticPriority) {
memmove(&taskQueueArray[ii+1], &taskQueueArray[ii], sizeof(task) * (taskQueueSize - ii));
taskQueueArray[ii] = task;
++taskQueueSize;
Expand Down Expand Up @@ -202,10 +202,10 @@ void getCheckFuncInfo(cfCheckFuncInfo_t *checkFuncInfo)
void getTaskInfo(taskId_e taskId, taskInfo_t * taskInfo)
{
taskInfo->isEnabled = queueContains(getTask(taskId));
taskInfo->desiredPeriodUs = getTask(taskId)->attributes->desiredPeriodUs;
taskInfo->staticPriority = getTask(taskId)->attributes->staticPriority;
taskInfo->taskName = getTask(taskId)->attributes->taskName;
taskInfo->subTaskName = getTask(taskId)->attributes->subTaskName;
taskInfo->desiredPeriodUs = getTask(taskId)->attributes.desiredPeriodUs;
taskInfo->staticPriority = getTask(taskId)->attributes.staticPriority;
taskInfo->taskName = getTask(taskId)->attributes.taskName;
taskInfo->subTaskName = getTask(taskId)->attributes.subTaskName;
taskInfo->maxExecutionTimeUs = getTask(taskId)->maxExecutionTimeUs;
taskInfo->totalExecutionTimeUs = getTask(taskId)->totalExecutionTimeUs;
taskInfo->averageExecutionTimeUs = getTask(taskId)->anticipatedExecutionTime >> TASK_EXEC_TIME_SHIFT;
Expand All @@ -230,19 +230,19 @@ void rescheduleTask(taskId_e taskId, timeDelta_t newPeriodUs)
} else {
return;
}
task->attributes->desiredPeriodUs = MAX(SCHEDULER_DELAY_LIMIT, newPeriodUs); // Limit delay to 100us (10 kHz) to prevent scheduler clogging
task->attributes.desiredPeriodUs = MAX(SCHEDULER_DELAY_LIMIT, newPeriodUs); // Limit delay to 100us (10 kHz) to prevent scheduler clogging

// Catch the case where the gyro loop is adjusted
if (taskId == TASK_GYRO) {
desiredPeriodCycles = (int32_t)clockMicrosToCycles((uint32_t)getTask(TASK_GYRO)->attributes->desiredPeriodUs);
desiredPeriodCycles = (int32_t)clockMicrosToCycles((uint32_t)getTask(TASK_GYRO)->attributes.desiredPeriodUs);
}
}

void setTaskEnabled(taskId_e taskId, bool enabled)
{
if (taskId == TASK_SELF || taskId < TASK_COUNT) {
task_t *task = taskId == TASK_SELF ? currentTask : getTask(taskId);
if (enabled && task->attributes->taskFunc) {
if (enabled && task->attributes.taskFunc) {
queueAdd(task);
} else {
queueRemove(task);
Expand Down Expand Up @@ -336,7 +336,7 @@ void schedulerInit(void)
taskGuardDeltaDownCycles = clockMicrosToCycles(1) / TASK_GUARD_MARGIN_DOWN_STEP;
taskGuardDeltaUpCycles = clockMicrosToCycles(1) / TASK_GUARD_MARGIN_UP_STEP;

desiredPeriodCycles = (int32_t)clockMicrosToCycles((uint32_t)getTask(TASK_GYRO)->attributes->desiredPeriodUs);
desiredPeriodCycles = (int32_t)clockMicrosToCycles((uint32_t)getTask(TASK_GYRO)->attributes.desiredPeriodUs);

lastTargetCycles = getCycleCounter();

Expand Down Expand Up @@ -367,12 +367,12 @@ FAST_CODE timeUs_t schedulerExecuteTask(task_t *selectedTask, timeUs_t currentTi
taskNextStateTime = -1;
float period = currentTimeUs - selectedTask->lastExecutedAtUs;
selectedTask->lastExecutedAtUs = currentTimeUs;
selectedTask->lastDesiredAt += selectedTask->attributes->desiredPeriodUs;
selectedTask->lastDesiredAt += selectedTask->attributes.desiredPeriodUs;
selectedTask->dynamicPriority = 0;

// Execute task
const timeUs_t currentTimeBeforeTaskCallUs = micros();
selectedTask->attributes->taskFunc(currentTimeBeforeTaskCallUs);
selectedTask->attributes.taskFunc(currentTimeBeforeTaskCallUs);
taskExecutionTimeUs = micros() - currentTimeBeforeTaskCallUs;
taskTotalExecutionTime += taskExecutionTimeUs;
if (!ignoreCurrentTaskExecRate) {
Expand Down Expand Up @@ -559,14 +559,14 @@ FAST_CODE void scheduler(void)

// Update task dynamic priorities
for (task_t *task = queueFirst(); task != NULL; task = queueNext()) {
if (task->attributes->staticPriority != TASK_PRIORITY_REALTIME) {
if (task->attributes.staticPriority != TASK_PRIORITY_REALTIME) {
// Task has checkFunc - event driven
if (task->attributes->checkFunc) {
if (task->attributes.checkFunc) {
// Increase priority for event driven tasks
if (task->dynamicPriority > 0) {
task->taskAgeCycles = 1 + (cmpTimeUs(currentTimeUs, task->lastSignaledAtUs) / task->attributes->desiredPeriodUs);
task->dynamicPriority = 1 + task->attributes->staticPriority * task->taskAgeCycles;
} else if (task->attributes->checkFunc(currentTimeUs, cmpTimeUs(currentTimeUs, task->lastExecutedAtUs))) {
task->taskAgeCycles = 1 + (cmpTimeUs(currentTimeUs, task->lastSignaledAtUs) / task->attributes.desiredPeriodUs);
task->dynamicPriority = 1 + task->attributes.staticPriority * task->taskAgeCycles;
} else if (task->attributes.checkFunc(currentTimeUs, cmpTimeUs(currentTimeUs, task->lastExecutedAtUs))) {
const uint32_t checkFuncExecutionTimeUs = cmpTimeUs(micros(), currentTimeUs);
#if !defined(UNIT_TEST)
DEBUG_SET(DEBUG_SCHEDULER, 3, checkFuncExecutionTimeUs);
Expand All @@ -577,16 +577,16 @@ FAST_CODE void scheduler(void)
checkFuncMaxExecutionTimeUs = MAX(checkFuncMaxExecutionTimeUs, checkFuncExecutionTimeUs);
task->lastSignaledAtUs = currentTimeUs;
task->taskAgeCycles = 1;
task->dynamicPriority = 1 + task->attributes->staticPriority;
task->dynamicPriority = 1 + task->attributes.staticPriority;
} else {
task->taskAgeCycles = 0;
}
} else {
// Task is time-driven, dynamicPriority is last execution age (measured in desiredPeriods)
// Task age is calculated from last execution
task->taskAgeCycles = (cmpTimeUs(currentTimeUs, task->lastExecutedAtUs) / task->attributes->desiredPeriodUs);
task->taskAgeCycles = (cmpTimeUs(currentTimeUs, task->lastExecutedAtUs) / task->attributes.desiredPeriodUs);
if (task->taskAgeCycles > 0) {
task->dynamicPriority = 1 + task->attributes->staticPriority * task->taskAgeCycles;
task->dynamicPriority = 1 + task->attributes.staticPriority * task->taskAgeCycles;
}
}

Expand Down Expand Up @@ -667,5 +667,5 @@ uint16_t getAverageSystemLoadPercent(void)

float schedulerGetCycleTimeMultiplier(void)
{
return (float)clockMicrosToCycles(getTask(TASK_GYRO)->attributes->desiredPeriodUs) / desiredPeriodCycles;
return (float)clockMicrosToCycles(getTask(TASK_GYRO)->attributes.desiredPeriodUs) / desiredPeriodCycles;
}
4 changes: 2 additions & 2 deletions src/main/scheduler/scheduler.h
Expand Up @@ -188,12 +188,12 @@ typedef struct {
bool (*checkFunc)(timeUs_t currentTimeUs, timeDelta_t currentDeltaTimeUs);
void (*taskFunc)(timeUs_t currentTimeUs);
timeDelta_t desiredPeriodUs; // target period of execution
const int8_t staticPriority; // dynamicPriority grows in steps of this size
int8_t staticPriority; // dynamicPriority grows in steps of this size
} task_attributes_t;

typedef struct {
// Task static data
task_attributes_t *attributes;
task_attributes_t attributes;

// Scheduling
uint16_t dynamicPriority; // measurement of how old task was last executed, used to avoid task starvation
Expand Down
14 changes: 7 additions & 7 deletions src/test/unit/scheduler_unittest.cc
Expand Up @@ -170,18 +170,18 @@ extern "C" {
TEST(SchedulerUnittest, SetupTasks)
{
for (int i = 0; i < TASK_COUNT; ++i) {
tasks[i].attributes = &task_attributes[i];
tasks[i].attributes = task_attributes[i];
}
}


TEST(SchedulerUnittest, TestPriorites)
{
EXPECT_EQ(TASK_PRIORITY_MEDIUM_HIGH, tasks[TASK_SYSTEM].attributes->staticPriority);
EXPECT_EQ(TASK_PRIORITY_REALTIME, tasks[TASK_GYRO].attributes->staticPriority);
EXPECT_EQ(TASK_PRIORITY_MEDIUM, tasks[TASK_ACCEL].attributes->staticPriority);
EXPECT_EQ(TASK_PRIORITY_LOW, tasks[TASK_SERIAL].attributes->staticPriority);
EXPECT_EQ(TASK_PRIORITY_MEDIUM, tasks[TASK_BATTERY_VOLTAGE].attributes->staticPriority);
EXPECT_EQ(TASK_PRIORITY_MEDIUM_HIGH, tasks[TASK_SYSTEM].attributes.staticPriority);
EXPECT_EQ(TASK_PRIORITY_REALTIME, tasks[TASK_GYRO].attributes.staticPriority);
EXPECT_EQ(TASK_PRIORITY_MEDIUM, tasks[TASK_ACCEL].attributes.staticPriority);
EXPECT_EQ(TASK_PRIORITY_LOW, tasks[TASK_SERIAL].attributes.staticPriority);
EXPECT_EQ(TASK_PRIORITY_MEDIUM, tasks[TASK_BATTERY_VOLTAGE].attributes.staticPriority);
}

TEST(SchedulerUnittest, TestQueueInit)
Expand Down Expand Up @@ -283,7 +283,7 @@ TEST(SchedulerUnittest, TestQueueArray)
EXPECT_EQ(enqueuedTasks, taskQueueSize);

for (int taskId = 0; taskId < TASK_COUNT_UNITTEST - 1; ++taskId) {
if (tasks[taskId].attributes->taskFunc) {
if (tasks[taskId].attributes.taskFunc) {
setTaskEnabled(static_cast<taskId_e>(taskId), true);
enqueuedTasks++;
EXPECT_EQ(enqueuedTasks, taskQueueSize);
Expand Down

0 comments on commit b6409db

Please sign in to comment.