mirror of
https://github.com/betaflight/betaflight.git
synced 2025-07-22 07:45:29 +03:00
Merge pull request #11261 from daleckystepan/tasks
Rename task id to task attr
This commit is contained in:
commit
51f870f1e8
4 changed files with 32 additions and 32 deletions
|
@ -312,7 +312,7 @@ static void taskCameraControl(uint32_t currentTime)
|
||||||
task_t tasks[TASK_COUNT];
|
task_t tasks[TASK_COUNT];
|
||||||
|
|
||||||
// Task ID data in .data (initialised data)
|
// Task ID data in .data (initialised data)
|
||||||
task_id_t task_ids[TASK_COUNT] = {
|
task_attr_t task_attrs[TASK_COUNT] = {
|
||||||
[TASK_SYSTEM] = DEFINE_TASK("SYSTEM", "LOAD", NULL, taskSystemLoad, TASK_PERIOD_HZ(10), TASK_PRIORITY_MEDIUM_HIGH),
|
[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_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
|
[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
|
||||||
|
@ -423,7 +423,7 @@ task_t *getTask(unsigned taskId)
|
||||||
void tasksInit(void)
|
void tasksInit(void)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < TASK_COUNT; i++) {
|
for (int i = 0; i < TASK_COUNT; i++) {
|
||||||
tasks[i].id = &task_ids[i];
|
tasks[i].attr = &task_attrs[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
schedulerInit();
|
schedulerInit();
|
||||||
|
|
|
@ -126,7 +126,7 @@ bool queueAdd(task_t *task)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
for (int ii = 0; ii <= taskQueueSize; ++ii) {
|
for (int ii = 0; ii <= taskQueueSize; ++ii) {
|
||||||
if (taskQueueArray[ii] == NULL || taskQueueArray[ii]->id->staticPriority < task->id->staticPriority) {
|
if (taskQueueArray[ii] == NULL || taskQueueArray[ii]->attr->staticPriority < task->attr->staticPriority) {
|
||||||
memmove(&taskQueueArray[ii+1], &taskQueueArray[ii], sizeof(task) * (taskQueueSize - ii));
|
memmove(&taskQueueArray[ii+1], &taskQueueArray[ii], sizeof(task) * (taskQueueSize - ii));
|
||||||
taskQueueArray[ii] = task;
|
taskQueueArray[ii] = task;
|
||||||
++taskQueueSize;
|
++taskQueueSize;
|
||||||
|
@ -202,10 +202,10 @@ void getCheckFuncInfo(cfCheckFuncInfo_t *checkFuncInfo)
|
||||||
void getTaskInfo(taskId_e taskId, taskInfo_t * taskInfo)
|
void getTaskInfo(taskId_e taskId, taskInfo_t * taskInfo)
|
||||||
{
|
{
|
||||||
taskInfo->isEnabled = queueContains(getTask(taskId));
|
taskInfo->isEnabled = queueContains(getTask(taskId));
|
||||||
taskInfo->desiredPeriodUs = getTask(taskId)->id->desiredPeriodUs;
|
taskInfo->desiredPeriodUs = getTask(taskId)->attr->desiredPeriodUs;
|
||||||
taskInfo->staticPriority = getTask(taskId)->id->staticPriority;
|
taskInfo->staticPriority = getTask(taskId)->attr->staticPriority;
|
||||||
taskInfo->taskName = getTask(taskId)->id->taskName;
|
taskInfo->taskName = getTask(taskId)->attr->taskName;
|
||||||
taskInfo->subTaskName = getTask(taskId)->id->subTaskName;
|
taskInfo->subTaskName = getTask(taskId)->attr->subTaskName;
|
||||||
taskInfo->maxExecutionTimeUs = getTask(taskId)->maxExecutionTimeUs;
|
taskInfo->maxExecutionTimeUs = getTask(taskId)->maxExecutionTimeUs;
|
||||||
taskInfo->totalExecutionTimeUs = getTask(taskId)->totalExecutionTimeUs;
|
taskInfo->totalExecutionTimeUs = getTask(taskId)->totalExecutionTimeUs;
|
||||||
taskInfo->averageExecutionTimeUs = getTask(taskId)->anticipatedExecutionTime >> TASK_EXEC_TIME_SHIFT;
|
taskInfo->averageExecutionTimeUs = getTask(taskId)->anticipatedExecutionTime >> TASK_EXEC_TIME_SHIFT;
|
||||||
|
@ -230,11 +230,11 @@ void rescheduleTask(taskId_e taskId, timeDelta_t newPeriodUs)
|
||||||
} else {
|
} else {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
task->id->desiredPeriodUs = MAX(SCHEDULER_DELAY_LIMIT, newPeriodUs); // Limit delay to 100us (10 kHz) to prevent scheduler clogging
|
task->attr->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
|
// Catch the case where the gyro loop is adjusted
|
||||||
if (taskId == TASK_GYRO) {
|
if (taskId == TASK_GYRO) {
|
||||||
desiredPeriodCycles = (int32_t)clockMicrosToCycles((uint32_t)getTask(TASK_GYRO)->id->desiredPeriodUs);
|
desiredPeriodCycles = (int32_t)clockMicrosToCycles((uint32_t)getTask(TASK_GYRO)->attr->desiredPeriodUs);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -242,7 +242,7 @@ void setTaskEnabled(taskId_e taskId, bool enabled)
|
||||||
{
|
{
|
||||||
if (taskId == TASK_SELF || taskId < TASK_COUNT) {
|
if (taskId == TASK_SELF || taskId < TASK_COUNT) {
|
||||||
task_t *task = taskId == TASK_SELF ? currentTask : getTask(taskId);
|
task_t *task = taskId == TASK_SELF ? currentTask : getTask(taskId);
|
||||||
if (enabled && task->id->taskFunc) {
|
if (enabled && task->attr->taskFunc) {
|
||||||
queueAdd(task);
|
queueAdd(task);
|
||||||
} else {
|
} else {
|
||||||
queueRemove(task);
|
queueRemove(task);
|
||||||
|
@ -336,7 +336,7 @@ void schedulerInit(void)
|
||||||
taskGuardDeltaDownCycles = clockMicrosToCycles(1) / TASK_GUARD_MARGIN_DOWN_STEP;
|
taskGuardDeltaDownCycles = clockMicrosToCycles(1) / TASK_GUARD_MARGIN_DOWN_STEP;
|
||||||
taskGuardDeltaUpCycles = clockMicrosToCycles(1) / TASK_GUARD_MARGIN_UP_STEP;
|
taskGuardDeltaUpCycles = clockMicrosToCycles(1) / TASK_GUARD_MARGIN_UP_STEP;
|
||||||
|
|
||||||
desiredPeriodCycles = (int32_t)clockMicrosToCycles((uint32_t)getTask(TASK_GYRO)->id->desiredPeriodUs);
|
desiredPeriodCycles = (int32_t)clockMicrosToCycles((uint32_t)getTask(TASK_GYRO)->attr->desiredPeriodUs);
|
||||||
|
|
||||||
lastTargetCycles = getCycleCounter();
|
lastTargetCycles = getCycleCounter();
|
||||||
|
|
||||||
|
@ -367,12 +367,12 @@ FAST_CODE timeUs_t schedulerExecuteTask(task_t *selectedTask, timeUs_t currentTi
|
||||||
taskNextStateTime = -1;
|
taskNextStateTime = -1;
|
||||||
float period = currentTimeUs - selectedTask->lastExecutedAtUs;
|
float period = currentTimeUs - selectedTask->lastExecutedAtUs;
|
||||||
selectedTask->lastExecutedAtUs = currentTimeUs;
|
selectedTask->lastExecutedAtUs = currentTimeUs;
|
||||||
selectedTask->lastDesiredAt += selectedTask->id->desiredPeriodUs;
|
selectedTask->lastDesiredAt += selectedTask->attr->desiredPeriodUs;
|
||||||
selectedTask->dynamicPriority = 0;
|
selectedTask->dynamicPriority = 0;
|
||||||
|
|
||||||
// Execute task
|
// Execute task
|
||||||
const timeUs_t currentTimeBeforeTaskCallUs = micros();
|
const timeUs_t currentTimeBeforeTaskCallUs = micros();
|
||||||
selectedTask->id->taskFunc(currentTimeBeforeTaskCallUs);
|
selectedTask->attr->taskFunc(currentTimeBeforeTaskCallUs);
|
||||||
taskExecutionTimeUs = micros() - currentTimeBeforeTaskCallUs;
|
taskExecutionTimeUs = micros() - currentTimeBeforeTaskCallUs;
|
||||||
taskTotalExecutionTime += taskExecutionTimeUs;
|
taskTotalExecutionTime += taskExecutionTimeUs;
|
||||||
if (!ignoreCurrentTaskExecRate) {
|
if (!ignoreCurrentTaskExecRate) {
|
||||||
|
@ -559,14 +559,14 @@ FAST_CODE void scheduler(void)
|
||||||
|
|
||||||
// Update task dynamic priorities
|
// Update task dynamic priorities
|
||||||
for (task_t *task = queueFirst(); task != NULL; task = queueNext()) {
|
for (task_t *task = queueFirst(); task != NULL; task = queueNext()) {
|
||||||
if (task->id->staticPriority != TASK_PRIORITY_REALTIME) {
|
if (task->attr->staticPriority != TASK_PRIORITY_REALTIME) {
|
||||||
// Task has checkFunc - event driven
|
// Task has checkFunc - event driven
|
||||||
if (task->id->checkFunc) {
|
if (task->attr->checkFunc) {
|
||||||
// Increase priority for event driven tasks
|
// Increase priority for event driven tasks
|
||||||
if (task->dynamicPriority > 0) {
|
if (task->dynamicPriority > 0) {
|
||||||
task->taskAgeCycles = 1 + (cmpTimeUs(currentTimeUs, task->lastSignaledAtUs) / task->id->desiredPeriodUs);
|
task->taskAgeCycles = 1 + (cmpTimeUs(currentTimeUs, task->lastSignaledAtUs) / task->attr->desiredPeriodUs);
|
||||||
task->dynamicPriority = 1 + task->id->staticPriority * task->taskAgeCycles;
|
task->dynamicPriority = 1 + task->attr->staticPriority * task->taskAgeCycles;
|
||||||
} else if (task->id->checkFunc(currentTimeUs, cmpTimeUs(currentTimeUs, task->lastExecutedAtUs))) {
|
} else if (task->attr->checkFunc(currentTimeUs, cmpTimeUs(currentTimeUs, task->lastExecutedAtUs))) {
|
||||||
const uint32_t checkFuncExecutionTimeUs = cmpTimeUs(micros(), currentTimeUs);
|
const uint32_t checkFuncExecutionTimeUs = cmpTimeUs(micros(), currentTimeUs);
|
||||||
#if !defined(UNIT_TEST)
|
#if !defined(UNIT_TEST)
|
||||||
DEBUG_SET(DEBUG_SCHEDULER, 3, checkFuncExecutionTimeUs);
|
DEBUG_SET(DEBUG_SCHEDULER, 3, checkFuncExecutionTimeUs);
|
||||||
|
@ -577,16 +577,16 @@ FAST_CODE void scheduler(void)
|
||||||
checkFuncMaxExecutionTimeUs = MAX(checkFuncMaxExecutionTimeUs, checkFuncExecutionTimeUs);
|
checkFuncMaxExecutionTimeUs = MAX(checkFuncMaxExecutionTimeUs, checkFuncExecutionTimeUs);
|
||||||
task->lastSignaledAtUs = currentTimeUs;
|
task->lastSignaledAtUs = currentTimeUs;
|
||||||
task->taskAgeCycles = 1;
|
task->taskAgeCycles = 1;
|
||||||
task->dynamicPriority = 1 + task->id->staticPriority;
|
task->dynamicPriority = 1 + task->attr->staticPriority;
|
||||||
} else {
|
} else {
|
||||||
task->taskAgeCycles = 0;
|
task->taskAgeCycles = 0;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Task is time-driven, dynamicPriority is last execution age (measured in desiredPeriods)
|
// Task is time-driven, dynamicPriority is last execution age (measured in desiredPeriods)
|
||||||
// Task age is calculated from last execution
|
// Task age is calculated from last execution
|
||||||
task->taskAgeCycles = (cmpTimeUs(currentTimeUs, task->lastExecutedAtUs) / task->id->desiredPeriodUs);
|
task->taskAgeCycles = (cmpTimeUs(currentTimeUs, task->lastExecutedAtUs) / task->attr->desiredPeriodUs);
|
||||||
if (task->taskAgeCycles > 0) {
|
if (task->taskAgeCycles > 0) {
|
||||||
task->dynamicPriority = 1 + task->id->staticPriority * task->taskAgeCycles;
|
task->dynamicPriority = 1 + task->attr->staticPriority * task->taskAgeCycles;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -667,5 +667,5 @@ uint16_t getAverageSystemLoadPercent(void)
|
||||||
|
|
||||||
float schedulerGetCycleTimeMultiplier(void)
|
float schedulerGetCycleTimeMultiplier(void)
|
||||||
{
|
{
|
||||||
return (float)clockMicrosToCycles(getTask(TASK_GYRO)->id->desiredPeriodUs) / desiredPeriodCycles;
|
return (float)clockMicrosToCycles(getTask(TASK_GYRO)->attr->desiredPeriodUs) / desiredPeriodCycles;
|
||||||
}
|
}
|
||||||
|
|
|
@ -189,11 +189,11 @@ typedef struct {
|
||||||
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
|
||||||
} task_id_t;
|
} task_attr_t;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
// Task static data
|
// Task static data
|
||||||
task_id_t *id;
|
task_attr_t *attr;
|
||||||
|
|
||||||
// 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
|
||||||
|
|
|
@ -95,7 +95,7 @@ extern "C" {
|
||||||
extern task_t *queueFirst(void);
|
extern task_t *queueFirst(void);
|
||||||
extern task_t *queueNext(void);
|
extern task_t *queueNext(void);
|
||||||
|
|
||||||
task_id_t task_ids[TASK_COUNT] = {
|
task_attr_t task_attrs[TASK_COUNT] = {
|
||||||
[TASK_SYSTEM] = {
|
[TASK_SYSTEM] = {
|
||||||
.taskName = "SYSTEM",
|
.taskName = "SYSTEM",
|
||||||
.taskFunc = taskSystemLoad,
|
.taskFunc = taskSystemLoad,
|
||||||
|
@ -170,18 +170,18 @@ extern "C" {
|
||||||
TEST(SchedulerUnittest, SetupTasks)
|
TEST(SchedulerUnittest, SetupTasks)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < TASK_COUNT; ++i) {
|
for (int i = 0; i < TASK_COUNT; ++i) {
|
||||||
tasks[i].id = &task_ids[i];
|
tasks[i].attr = &task_attrs[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
TEST(SchedulerUnittest, TestPriorites)
|
TEST(SchedulerUnittest, TestPriorites)
|
||||||
{
|
{
|
||||||
EXPECT_EQ(TASK_PRIORITY_MEDIUM_HIGH, tasks[TASK_SYSTEM].id->staticPriority);
|
EXPECT_EQ(TASK_PRIORITY_MEDIUM_HIGH, tasks[TASK_SYSTEM].attr->staticPriority);
|
||||||
EXPECT_EQ(TASK_PRIORITY_REALTIME, tasks[TASK_GYRO].id->staticPriority);
|
EXPECT_EQ(TASK_PRIORITY_REALTIME, tasks[TASK_GYRO].attr->staticPriority);
|
||||||
EXPECT_EQ(TASK_PRIORITY_MEDIUM, tasks[TASK_ACCEL].id->staticPriority);
|
EXPECT_EQ(TASK_PRIORITY_MEDIUM, tasks[TASK_ACCEL].attr->staticPriority);
|
||||||
EXPECT_EQ(TASK_PRIORITY_LOW, tasks[TASK_SERIAL].id->staticPriority);
|
EXPECT_EQ(TASK_PRIORITY_LOW, tasks[TASK_SERIAL].attr->staticPriority);
|
||||||
EXPECT_EQ(TASK_PRIORITY_MEDIUM, tasks[TASK_BATTERY_VOLTAGE].id->staticPriority);
|
EXPECT_EQ(TASK_PRIORITY_MEDIUM, tasks[TASK_BATTERY_VOLTAGE].attr->staticPriority);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(SchedulerUnittest, TestQueueInit)
|
TEST(SchedulerUnittest, TestQueueInit)
|
||||||
|
@ -283,7 +283,7 @@ TEST(SchedulerUnittest, TestQueueArray)
|
||||||
EXPECT_EQ(enqueuedTasks, taskQueueSize);
|
EXPECT_EQ(enqueuedTasks, taskQueueSize);
|
||||||
|
|
||||||
for (int taskId = 0; taskId < TASK_COUNT_UNITTEST - 1; ++taskId) {
|
for (int taskId = 0; taskId < TASK_COUNT_UNITTEST - 1; ++taskId) {
|
||||||
if (tasks[taskId].id->taskFunc) {
|
if (tasks[taskId].attr->taskFunc) {
|
||||||
setTaskEnabled(static_cast<taskId_e>(taskId), true);
|
setTaskEnabled(static_cast<taskId_e>(taskId), true);
|
||||||
enqueuedTasks++;
|
enqueuedTasks++;
|
||||||
EXPECT_EQ(enqueuedTasks, taskQueueSize);
|
EXPECT_EQ(enqueuedTasks, taskQueueSize);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue