Skip to content

Commit

Permalink
Use cpu_late_10ths_percent_limit to set limit on % of late tasks in 1…
Browse files Browse the repository at this point in the history
…0th of a % (betaflight#13330)

* Use cpu_late_10ths_percent_limit to set limit on % of late tasks in 10th of a %
Set CPU load late limit to 1% based on testing

* Update src/main/cli/settings.c

Co-authored-by: Jan Post <[email protected]>

* Update src/main/scheduler/scheduler.h

Co-authored-by: Jan Post <[email protected]>

* Update src/main/fc/core.c

* Update src/test/unit/arming_prevention_unittest.cc

* Update src/main/scheduler/scheduler.c

---------

Co-authored-by: Mark Haslinghuis <[email protected]>
Co-authored-by: Jan Post <[email protected]>
  • Loading branch information
3 people authored Feb 15, 2024
1 parent a190ed9 commit e3e67b2
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/main/cli/settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -1720,6 +1720,10 @@ const clivalue_t valueTable[] = {
{ "scheduler_relax_rx", VAR_UINT16 | HARDWARE_VALUE, .config.minmaxUnsigned = { 0, 500 }, PG_SCHEDULER_CONFIG, PG_ARRAY_ELEMENT_OFFSET(schedulerConfig_t, 0, rxRelaxDeterminism) },
{ "scheduler_relax_osd", VAR_UINT16 | HARDWARE_VALUE, .config.minmaxUnsigned = { 0, 500 }, PG_SCHEDULER_CONFIG, PG_ARRAY_ELEMENT_OFFSET(schedulerConfig_t, 0, osdRelaxDeterminism) },

#ifdef USE_LATE_TASK_STATISTICS
{ "cpu_late_limit_permille", VAR_UINT8 | MASTER_VALUE, .config.minmaxUnsigned = { 0, 100 }, PG_SCHEDULER_CONFIG, offsetof(schedulerConfig_t, cpuLatePercentageLimit) },
#endif

{ "serialmsp_halfduplex", VAR_UINT8 | MASTER_VALUE | MODE_LOOKUP, .config.lookup = { TABLE_OFF_ON }, PG_MSP_CONFIG, offsetof(mspConfig_t, halfDuplex) },

// PG_TIMECONFIG
Expand Down
4 changes: 3 additions & 1 deletion src/main/fc/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -323,11 +323,13 @@ void updateArmingStatus(void)
unsetArmingDisabled(ARMING_DISABLED_ANGLE);
}

if (getAverageSystemLoadPercent() > LOAD_PERCENTAGE_ONE) {
#if defined(USE_LATE_TASK_STATISTICS)
if ((getCpuPercentageLate() > schedulerConfig()->cpuLatePercentageLimit)) {
setArmingDisabled(ARMING_DISABLED_LOAD);
} else {
unsetArmingDisabled(ARMING_DISABLED_LOAD);
}
#endif // USE_LATE_TASK_STATISTICS

if (isCalibrating()) {
setArmingDisabled(ARMING_DISABLED_CALIBRATING);
Expand Down
1 change: 1 addition & 0 deletions src/main/osd/osd.h
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ typedef enum {
OSD_WARNING_RSSI_DBM,
OSD_WARNING_OVER_CAP,
OSD_WARNING_RSNR,
OSD_WARNING_LOAD,
OSD_WARNING_COUNT // MUST BE LAST
} osdWarningsFlags_e;

Expand Down
7 changes: 7 additions & 0 deletions src/main/osd/osd_warnings.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,13 @@ void renderOsdWarning(char *warningText, bool *blinking, uint8_t *displayAttr)
return;
}

if (osdWarnGetState(OSD_WARNING_LOAD) && (getArmingDisableFlags() & ARMING_DISABLED_LOAD)) {
tfp_sprintf(warningText, "CPU OVERLOAD");
*displayAttr = DISPLAYPORT_SEVERITY_CRITICAL;
*blinking = true;
return;
}

#ifdef USE_GPS_RESCUE
if (osdWarnGetState(OSD_WARNING_GPS_RESCUE_UNAVAILABLE) &&
ARMING_FLAG(ARMED) &&
Expand Down
3 changes: 2 additions & 1 deletion src/main/pg/scheduler.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@
#include "pg/pg_ids.h"
#include "pg/scheduler.h"

PG_REGISTER_WITH_RESET_TEMPLATE(schedulerConfig_t, schedulerConfig, PG_SCHEDULER_CONFIG, 0);
PG_REGISTER_WITH_RESET_TEMPLATE(schedulerConfig_t, schedulerConfig, PG_SCHEDULER_CONFIG, 1);

PG_RESET_TEMPLATE(schedulerConfig_t, schedulerConfig,
.rxRelaxDeterminism = SCHEDULER_RELAX_RX,
.osdRelaxDeterminism = SCHEDULER_RELAX_OSD,
.cpuLatePercentageLimit = CPU_LOAD_LATE_LIMIT
);
4 changes: 4 additions & 0 deletions src/main/pg/scheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,13 @@
#define SCHEDULER_RELAX_OSD 25
#endif

// Tenths of a % of tasks late
#define CPU_LOAD_LATE_LIMIT 10

typedef struct schedulerConfig_s {
uint16_t rxRelaxDeterminism;
uint16_t osdRelaxDeterminism;
uint16_t cpuLatePercentageLimit;
} schedulerConfig_t;

PG_DECLARE(schedulerConfig_t, schedulerConfig);
Expand Down
15 changes: 15 additions & 0 deletions src/main/scheduler/scheduler.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
// 1 - Tasks late in last second
// 2 - Total lateness in last second in 10ths us
// 3 - Total tasks run in last second
// 4 - 10ths % of tasks late in last second

extern task_t tasks[];

Expand Down Expand Up @@ -107,6 +108,7 @@ static uint8_t skippedOSDAttempts = 0;
static int16_t lateTaskCount = 0;
static uint32_t lateTaskTotal = 0;
static int16_t taskCount = 0;
static uint32_t lateTaskPercentage = 0;
static uint32_t nextTimingCycles;
#endif

Expand Down Expand Up @@ -199,6 +201,15 @@ void taskSystemLoad(timeUs_t currentTimeUs)
#endif
}

uint32_t getCpuPercentageLate(void)
{
#if defined(USE_LATE_TASK_STATISTICS)
return lateTaskPercentage;
#else
return 0;
#endif
}

timeUs_t checkFuncMaxExecutionTimeUs;
timeUs_t checkFuncTotalExecutionTimeUs;
timeUs_t checkFuncMovingSumExecutionTimeUs;
Expand Down Expand Up @@ -535,6 +546,10 @@ FAST_CODE void scheduler(void)
// Total tasks run in last second
DEBUG_SET(DEBUG_TIMING_ACCURACY, 3, taskCount);

lateTaskPercentage = 1000 * (uint32_t)lateTaskCount / taskCount;
// 10ths % of tasks late in last second
DEBUG_SET(DEBUG_TIMING_ACCURACY, 4, lateTaskPercentage);

lateTaskCount = 0;
lateTaskTotal = 0;
taskCount = 0;
Expand Down
1 change: 1 addition & 0 deletions src/main/scheduler/scheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ void schedulerInit(void);
void scheduler(void);
timeUs_t schedulerExecuteTask(task_t *selectedTask, timeUs_t currentTimeUs);
void taskSystemLoad(timeUs_t currentTimeUs);
uint32_t getCpuPercentageLate(void);
void schedulerEnableGyro(void);
uint16_t getAverageSystemLoadPercent(void);
float schedulerGetCycleTimeMultiplier(void);
1 change: 1 addition & 0 deletions src/test/unit/arming_prevention_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1161,4 +1161,5 @@ extern "C" {
return 0.0f;
}
void getRcDeflectionAbs(void) {}
uint32_t getCpuPercentageLate(void) { return 0; };
}

0 comments on commit e3e67b2

Please sign in to comment.